@@ -26,10 +26,6 @@ const assert = require('internal/assert');
26
26
const internalFS = require ( 'internal/fs/utils' ) ;
27
27
const { NativeModule } = require ( 'internal/bootstrap/loaders' ) ;
28
28
const {
29
- closeSync,
30
- fstatSync,
31
- openSync,
32
- readFileSync,
33
29
realpathSync,
34
30
statSync,
35
31
Stats,
@@ -53,6 +49,7 @@ const {
53
49
ERR_UNSUPPORTED_ESM_URL_SCHEME ,
54
50
} = require ( 'internal/errors' ) . codes ;
55
51
52
+ const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
56
53
const DEFAULT_CONDITIONS = ObjectFreeze ( [ 'node' , 'import' ] ) ;
57
54
const DEFAULT_CONDITIONS_SET = new SafeSet ( DEFAULT_CONDITIONS ) ;
58
55
@@ -78,37 +75,25 @@ function tryStatSync(path) {
78
75
}
79
76
}
80
77
81
- function readIfFile ( path ) {
82
- let fd ;
83
- try {
84
- fd = openSync ( path , 'r' ) ;
85
- } catch {
86
- return undefined ;
87
- }
88
- try {
89
- if ( ! fstatSync ( fd ) . isFile ( ) ) return undefined ;
90
- return readFileSync ( fd , 'utf8' ) ;
91
- } finally {
92
- closeSync ( fd ) ;
93
- }
78
+ /**
79
+ *
80
+ * '/foo/package.json' -> '/foo'
81
+ */
82
+ function removePackageJsonFromPath ( path ) {
83
+ return StringPrototypeSlice ( path , 0 , path . length - 13 ) ;
94
84
}
95
85
96
- function getPackageConfig ( path , base ) {
86
+ function getPackageConfig ( path ) {
97
87
const existing = packageJSONCache . get ( path ) ;
98
88
if ( existing !== undefined ) {
99
- if ( ! existing . isValid ) {
100
- throw new ERR_INVALID_PACKAGE_CONFIG ( path , fileURLToPath ( base ) , false ) ;
101
- }
102
89
return existing ;
103
90
}
104
-
105
- const source = readIfFile ( path ) ;
91
+ const source = packageJsonReader . read ( path ) . string ;
106
92
if ( source === undefined ) {
107
93
const packageConfig = {
108
94
exists : false ,
109
95
main : undefined ,
110
96
name : undefined ,
111
- isValid : true ,
112
97
type : 'none' ,
113
98
exports : undefined
114
99
} ;
@@ -119,17 +104,9 @@ function getPackageConfig(path, base) {
119
104
let packageJSON ;
120
105
try {
121
106
packageJSON = JSONParse ( source ) ;
122
- } catch {
123
- const packageConfig = {
124
- exists : true ,
125
- main : undefined ,
126
- name : undefined ,
127
- isValid : false ,
128
- type : 'none' ,
129
- exports : undefined
130
- } ;
131
- packageJSONCache . set ( path , packageConfig ) ;
132
- return packageConfig ;
107
+ } catch ( error ) {
108
+ const errorPath = removePackageJsonFromPath ( path ) ;
109
+ throw new ERR_INVALID_PACKAGE_CONFIG ( errorPath , error . message , true ) ;
133
110
}
134
111
135
112
let { main, name, type } = packageJSON ;
@@ -143,7 +120,6 @@ function getPackageConfig(path, base) {
143
120
exists : true ,
144
121
main,
145
122
name,
146
- isValid : true ,
147
123
type,
148
124
exports
149
125
} ;
@@ -171,7 +147,6 @@ function getPackageScopeConfig(resolved, base) {
171
147
exists : false ,
172
148
main : undefined ,
173
149
name : undefined ,
174
- isValid : true ,
175
150
type : 'none' ,
176
151
exports : undefined
177
152
} ;
@@ -590,8 +565,7 @@ function packageResolve(specifier, base, conditions) {
590
565
let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
591
566
let lastPath ;
592
567
do {
593
- const stat = tryStatSync (
594
- StringPrototypeSlice ( packageJSONPath , 0 , packageJSONPath . length - 13 ) ) ;
568
+ const stat = tryStatSync ( removePackageJsonFromPath ( packageJSONPath ) ) ;
595
569
if ( ! stat . isDirectory ( ) ) {
596
570
lastPath = packageJSONPath ;
597
571
packageJSONUrl = new URL ( ( isScoped ?
0 commit comments