UNPKG

1.26 kBJavaScriptView Raw
1// copied from https://github.com/nodejs/node/blob/v15.3.0/lib/internal/modules/package_json_reader.js
2'use strict';
3
4const { SafeMap } = require('./node-primordials');
5const { internalModuleReadJSON } = require('./node-internalBinding-fs');
6const { pathToFileURL } = require('url');
7const { toNamespacedPath } = require('path');
8// const { getOptionValue } = require('./node-options');
9
10const cache = new SafeMap();
11
12let manifest;
13
14/**
15 * @param {string} jsonPath
16 * @return {{string: string, containsKeys: boolean}}
17 */
18function read(jsonPath) {
19 if (cache.has(jsonPath)) {
20 return cache.get(jsonPath);
21 }
22
23 const [string, containsKeys] = internalModuleReadJSON(
24 toNamespacedPath(jsonPath)
25 );
26 const result = { string, containsKeys };
27 if (string !== undefined) {
28 if (manifest === undefined) {
29 // manifest = getOptionValue('--experimental-policy') ?
30 // require('internal/process/policy').manifest :
31 // null;
32 // disabled for now. I am not sure if/how we should support this
33 manifest = null;
34 }
35 if (manifest !== null) {
36 const jsonURL = pathToFileURL(jsonPath);
37 manifest.assertIntegrity(jsonURL, string);
38 }
39 }
40 cache.set(jsonPath, result);
41 return result;
42}
43
44module.exports = { read };