forked from shapeshift/web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
54 lines (50 loc) · 1.52 KB
/
util.ts
File metadata and controls
54 lines (50 loc) · 1.52 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { Csp, CspEntry } from './types'
/**
* Splits a CSP object into a list of [directive, source] entries.
*/
export function cspToEntries(x: Csp): CspEntry[] {
return Object.entries(x).flatMap(([k, v]) => {
return v.map(entry => {
const out: [string, string] = [k, entry]
return out
})
})
}
/**
* Collects a list of [directive, source] entries into a CSP object. Filters out
* the 'none' source if any other sources are present, and returns the result in
* sorted form.
*/
export function entriesToCsp(x: CspEntry[]): Csp {
const acc: Csp = {}
return x
.sort(([k1, v1], [k2, v2]) => {
if (k1 < k2) return -1
if (k1 > k2) return 1
if (v1 < v2) return -1
if (v1 > v2) return 1
return 0
})
.reduce((a, [k, v]) => {
a[k] ??= []
if (v && (a[k].length === 0 || v !== "'none'") && !a[k].includes(v)) a[k].push(v)
if (a[k].length > 1) a[k] = a[k].filter(x => x !== "'none'")
return a
}, acc)
}
/**
* Produces a CSP that allows the union of the set of things allowed by the source
* CSPs. (Applying multiple CSPs, on the other hand, will enforce their intersection.)
*/
export function cspMerge(...args: Csp[]): Csp {
return entriesToCsp(args.flatMap(x => cspToEntries(x)))
}
/**
* Serializes a CSP object to the format which the browser will process.
*/
export function serializeCsp(x: Csp): string {
return Object.entries(x)
.map(([k, v]) => [k, v.filter(x => !!x)])
.map(([k, v]) => `${[k, ...v].join(' ')}`)
.join('; ')
}