node-internal-modules-package_json_reader.js 1.2 KB

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