33// Each function and variable below must have a comment linking to the source in node's github repo.
44
55const path = require ( 'path' ) ;
6- const fs = require ( 'fs' ) ;
6+ const packageJsonReader = require ( './node-package-json-reader' ) ;
7+ const { JSONParse} = require ( './node-primordials' ) ;
78
89module . exports . assertScriptCanLoadAsCJSImpl = assertScriptCanLoadAsCJSImpl ;
910
1011// copied from Module._extensions['.js']
11- // https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770 /lib/internal/modules/cjs/loader.js#L1211-L1217
12+ // https://github.com/nodejs/node/blob/v15.3.0 /lib/internal/modules/cjs/loader.js#L1113-L1120
1213function assertScriptCanLoadAsCJSImpl ( filename ) {
1314 const pkg = readPackageScope ( filename ) ;
1415 // Function require shouldn't be used in ES modules.
@@ -41,31 +42,27 @@ function readPackageScope(checkPath) {
4142// Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L249
4243const packageJsonCache = new Map ( ) ;
4344
44- // Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770 /lib/internal/modules/cjs/loader.js#L251-L283
45+ // Copied from https://github.com/nodejs/node/blob/v15.3.0 /lib/internal/modules/cjs/loader.js#L275-L304
4546function readPackage ( requestPath ) {
4647 const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
4748
4849 const existing = packageJsonCache . get ( jsonPath ) ;
4950 if ( existing !== undefined ) return existing ;
5051
51- const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
52+ const result = packageJsonReader . read ( jsonPath ) ;
53+ const json = result . containsKeys === false ? '{}' : result . string ;
5254 if ( json === undefined ) {
5355 packageJsonCache . set ( jsonPath , false ) ;
5456 return false ;
5557 }
5658
57- // TODO Related to `--experimental-policy`? Disabling for now
58- // if (manifest) {
59- // const jsonURL = pathToFileURL(jsonPath);
60- // manifest.assertIntegrity(jsonURL, json);
61- // }
62-
6359 try {
64- const parsed = JSON . parse ( json ) ;
60+ const parsed = JSONParse ( json ) ;
6561 const filtered = {
6662 name : parsed . name ,
6763 main : parsed . main ,
6864 exports : parsed . exports ,
65+ imports : parsed . imports ,
6966 type : parsed . type
7067 } ;
7168 packageJsonCache . set ( jsonPath , filtered ) ;
@@ -77,17 +74,6 @@ function readPackage(requestPath) {
7774 }
7875}
7976
80- // In node's core, this is implemented in C
81- // https://github.com/nodejs/node/blob/e9f293750760d59243020d0376edf242c9a26b67/src/node_file.cc#L845-L939
82- function internalModuleReadJSON ( path ) {
83- try {
84- return fs . readFileSync ( path , 'utf8' )
85- } catch ( e ) {
86- if ( e . code === 'ENOENT' ) return undefined
87- throw e
88- }
89- }
90-
9177// Native ERR_REQUIRE_ESM Error is declared here:
9278// https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L1294-L1313
9379// Error class factory is implemented here:
0 commit comments