3
3
// Each function and variable below must have a comment linking to the source in node's github repo.
4
4
5
5
const path = require ( 'path' ) ;
6
- const fs = require ( 'fs' ) ;
6
+ const packageJsonReader = require ( './node-package-json-reader' ) ;
7
+ const { JSONParse} = require ( './node-primordials' ) ;
7
8
8
9
module . exports . assertScriptCanLoadAsCJSImpl = assertScriptCanLoadAsCJSImpl ;
9
10
10
11
// 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
12
13
function assertScriptCanLoadAsCJSImpl ( filename ) {
13
14
const pkg = readPackageScope ( filename ) ;
14
15
// Function require shouldn't be used in ES modules.
@@ -41,31 +42,27 @@ function readPackageScope(checkPath) {
41
42
// Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L249
42
43
const packageJsonCache = new Map ( ) ;
43
44
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
45
46
function readPackage ( requestPath ) {
46
47
const jsonPath = path . resolve ( requestPath , 'package.json' ) ;
47
48
48
49
const existing = packageJsonCache . get ( jsonPath ) ;
49
50
if ( existing !== undefined ) return existing ;
50
51
51
- const json = internalModuleReadJSON ( path . toNamespacedPath ( jsonPath ) ) ;
52
+ const result = packageJsonReader . read ( jsonPath ) ;
53
+ const json = result . containsKeys === false ? '{}' : result . string ;
52
54
if ( json === undefined ) {
53
55
packageJsonCache . set ( jsonPath , false ) ;
54
56
return false ;
55
57
}
56
58
57
- // TODO Related to `--experimental-policy`? Disabling for now
58
- // if (manifest) {
59
- // const jsonURL = pathToFileURL(jsonPath);
60
- // manifest.assertIntegrity(jsonURL, json);
61
- // }
62
-
63
59
try {
64
- const parsed = JSON . parse ( json ) ;
60
+ const parsed = JSONParse ( json ) ;
65
61
const filtered = {
66
62
name : parsed . name ,
67
63
main : parsed . main ,
68
64
exports : parsed . exports ,
65
+ imports : parsed . imports ,
69
66
type : parsed . type
70
67
} ;
71
68
packageJsonCache . set ( jsonPath , filtered ) ;
@@ -77,17 +74,6 @@ function readPackage(requestPath) {
77
74
}
78
75
}
79
76
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
-
91
77
// Native ERR_REQUIRE_ESM Error is declared here:
92
78
// https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L1294-L1313
93
79
// Error class factory is implemented here:
0 commit comments