-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
34 lines (29 loc) · 1011 Bytes
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
// This module is browser compatible.
import type { CrossOriginOpenerPolicy } from "./types.ts";
import { isString, Item, Parameters, stringifySfv, Token } from "./deps.ts";
const enum Param {
ReportTo = "report-to",
}
/** Serialize {@link CrossOriginOpenerPolicy} into string.
* @throws {TypeError} If the {@link CrossOriginOpenerPolicy} is invalid.
*/
export function stringifyCrossOriginOpenerPolicy(
policy: CrossOriginOpenerPolicy,
): string {
const token = new Token(policy.value);
const parameters = isString(policy.reportTo)
? new Parameters({
[Param.ReportTo]: new Token(policy.reportTo),
})
: new Parameters();
const item = new Item([token, parameters]);
try {
return stringifySfv(item);
} catch (cause) {
throw TypeError(Msg.InvalidCrossOriginOpenerPolicy, { cause });
}
}
const enum Msg {
InvalidCrossOriginOpenerPolicy = "invalid CrossOriginOpenerPolicy format.",
}