node-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-internal-fs');
  5. const { pathToFileURL } = require('url');
  6. const { toNamespacedPath } = require('path');
  7. const cache = new SafeMap();
  8. let manifest;
  9. /**
  10. * @param {string} jsonPath
  11. * @return {[string, boolean]}
  12. */
  13. function read(jsonPath) {
  14. if (cache.has(jsonPath)) {
  15. return cache.get(jsonPath);
  16. }
  17. const [string, containsKeys] = internalModuleReadJSON(
  18. toNamespacedPath(jsonPath)
  19. );
  20. const result = { string, containsKeys };
  21. const { getOptionValue } = require('./node-options');
  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 };