@@ -11,12 +11,19 @@ const modulesBinding = internalBinding('modules');
11
11
const { resolve, sep } = require ( 'path' ) ;
12
12
const { kEmptyObject } = require ( 'internal/util' ) ;
13
13
14
+ /**
15
+ * @typedef {import('typings/internalBinding/modules').FullPackageConfig } FullPackageConfig
16
+ * @typedef {import('typings/internalBinding/modules').PackageConfig } PackageConfig
17
+ * @typedef {import('typings/internalBinding/modules').SerializedPackageConfig } SerializedPackageConfig
18
+ */
19
+
14
20
/**
15
21
* @param {string } path
16
- * @param {import('typings/internalBinding/modules').SerializedPackageConfig } contents
17
- * @returns {import('typings/internalBinding/modules').PackageConfig }
22
+ * @param {SerializedPackageConfig } contents
23
+ * @param {boolean } everything
24
+ * @returns {everything extends true ? FullPackageConfig : PackageConfig }
18
25
*/
19
- function deserializePackageJSON ( path , contents ) {
26
+ function deserializePackageJSON ( path , contents , everything = false ) {
20
27
if ( contents === undefined ) {
21
28
return {
22
29
__proto__ : null ,
@@ -64,6 +71,7 @@ function deserializePackageJSON(path, contents) {
64
71
ObjectDefineProperty ( this , 'exports' , { __proto__ : null , value } ) ;
65
72
return this . exports ;
66
73
} ,
74
+ ...( everything && contents [ 6 ] ) ,
67
75
} ;
68
76
}
69
77
@@ -75,7 +83,7 @@ function deserializePackageJSON(path, contents) {
75
83
* specifier?: URL | string,
76
84
* isESM?: boolean,
77
85
* }} options
78
- * @returns {import('typings/internalBinding/modules'). PackageConfig }
86
+ * @returns {PackageConfig }
79
87
*/
80
88
function read ( jsonPath , { base, specifier, isESM } = kEmptyObject ) {
81
89
// This function will be called by both CJS and ESM, so we need to make sure
@@ -105,16 +113,17 @@ function readPackage(requestPath) {
105
113
* Get the nearest parent package.json file from a given path.
106
114
* Return the package.json data and the path to the package.json file, or undefined.
107
115
* @param {string } checkPath The path to start searching from.
108
- * @returns {undefined | {data: import('typings/internalBinding/modules').PackageConfig, path: string} }
116
+ * @param {boolean } everything Whether to include unrecognised fields.
117
+ * @returns {undefined | {data: PackageConfig, path: string} }
109
118
*/
110
- function getNearestParentPackageJSON ( checkPath ) {
119
+ function getNearestParentPackageJSON ( checkPath , everything = false ) {
111
120
const result = modulesBinding . getNearestParentPackageJSON ( checkPath ) ;
112
121
113
122
if ( result === undefined ) {
114
123
return undefined ;
115
124
}
116
125
117
- const data = deserializePackageJSON ( checkPath , result ) ;
126
+ const data = deserializePackageJSON ( checkPath , result , everything ) ;
118
127
119
128
// Path should be the root folder of the matched package.json
120
129
// For example for ~/path/package.json, it should be ~/path
@@ -123,16 +132,26 @@ function getNearestParentPackageJSON(checkPath) {
123
132
return { data, path } ;
124
133
}
125
134
135
+ /**
136
+ * Find the nearest package.json
137
+ * @param {URL['pathname'] } origin Where to start searching.
138
+ * @returns {URL['pathname'] } The fully resolved location of the package.json file.
139
+ */
140
+ function findNearestPackageJSON ( origin ) {
141
+ return modulesBinding . findNearestPackageJSON ( origin ) ;
142
+ }
143
+
126
144
/**
127
145
* Returns the package configuration for the given resolved URL.
128
146
* @param {URL | string } resolved - The resolved URL.
129
- * @returns {import('typings/internalBinding/modules').PackageConfig } - The package configuration.
147
+ * @param {boolean } everything - Whether to include unrecognised fields.
148
+ * @returns {PackageConfig } - The package configuration.
130
149
*/
131
- function getPackageScopeConfig ( resolved ) {
150
+ function getPackageScopeConfig ( resolved , everything = false ) {
132
151
const result = modulesBinding . getPackageScopeConfig ( `${ resolved } ` ) ;
133
152
134
153
if ( ArrayIsArray ( result ) ) {
135
- return deserializePackageJSON ( `${ resolved } ` , result ) ;
154
+ return deserializePackageJSON ( `${ resolved } ` , result , everything ) ;
136
155
}
137
156
138
157
// This means that the response is a string
@@ -160,4 +179,5 @@ module.exports = {
160
179
getNearestParentPackageJSON,
161
180
getPackageScopeConfig,
162
181
getPackageType,
182
+ findNearestPackageJSON,
163
183
} ;
0 commit comments