summaryrefslogtreecommitdiff
path: root/lib/internal/process/policy.js
blob: 0b037d4ef53cf413b52bf928ee00f4c833e4b328 (plain)
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
'use strict';

const {
  ERR_MANIFEST_TDZ,
} = require('internal/errors').codes;
const { Manifest } = require('internal/policy/manifest');
let manifest;

module.exports = Object.freeze({
  __proto__: null,
  setup(src, url) {
    if (src === null) {
      manifest = null;
      return;
    }

    const json = JSON.parse(src, (_, o) => {
      if (o && typeof o === 'object') {
        Reflect.setPrototypeOf(o, null);
        Object.freeze(o);
      }
      return o;
    });
    manifest = new Manifest(json, url);
  },

  get manifest() {
    if (typeof manifest === 'undefined') {
      throw new ERR_MANIFEST_TDZ();
    }
    return manifest;
  },

  assertIntegrity(moduleURL, content) {
    this.manifest.matchesIntegrity(moduleURL, content);
  }
});