forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 2
/
policy.js
77 lines (69 loc) · 1.41 KB
/
policy.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const {
JSONParse,
ObjectFreeze,
ReflectSetPrototypeOf,
} = primordials;
const {
ERR_MANIFEST_TDZ,
} = require('internal/errors').codes;
const { Manifest } = require('internal/policy/manifest');
/**
* @type {InstanceType<Manifest>}
*/
let manifest;
/**
* @type {string}
*/
let manifestSrc;
/**
* @type {string}
*/
let manifestURL;
module.exports = ObjectFreeze({
__proto__: null,
/**
* @param {string | null} src null to not use a policy
* @param {string} url
* @returns {void}
*/
setup(src, url) {
manifestSrc = src;
manifestURL = url;
if (src === null) {
manifest = null;
manifestSrc = null;
manifestURL = null;
return;
}
const json = JSONParse(src, (_, o) => {
if (o && typeof o === 'object') {
ReflectSetPrototypeOf(o, null);
ObjectFreeze(o);
}
return o;
});
manifest = new Manifest(json, url);
},
get manifest() {
if (typeof manifest === 'undefined') {
throw new ERR_MANIFEST_TDZ();
}
return manifest;
},
get src() {
if (typeof manifestSrc === 'undefined') {
throw new ERR_MANIFEST_TDZ();
}
return manifestSrc;
},
get url() {
if (typeof manifestURL === 'undefined') {
throw new ERR_MANIFEST_TDZ();
}
return manifestURL;
},
assertIntegrity(moduleURL, content) {
this.manifest.assertIntegrity(moduleURL, content);
}
});