-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
applyPatch.ts
443 lines (336 loc) Β· 13.7 KB
/
applyPatch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import {FakeFS, PosixFS, npath, ppath, patchFs, PortablePath, xfs, Filename, NativePath} from '@yarnpkg/fslib';
import fs from 'fs';
import {Module} from 'module';
import path from 'path';
import {PackageLocator, PnpApi} from '../types';
import {ErrorCode, makeError, getIssuerModule} from './internalTools';
export type ApplyPatchOptions = {
compatibilityMode?: boolean,
fakeFs: FakeFS<PortablePath>,
};
export type ApiMetadata = {
cache: typeof Module._cache,
instance: PnpApi,
stats: fs.Stats,
};
export function applyPatch(pnpapi: PnpApi, opts: ApplyPatchOptions) {
// @ts-ignore
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding('natives')));
// The callback function gets called to wrap the return value of the module names matching the regexp
const patchedModules: Array<[RegExp, (issuer: PackageLocator | null, exports: any) => any]> = [];
const initialApiPath = npath.toPortablePath(pnpapi.resolveToUnqualified(`pnpapi`, null)!);
const initialApiStats = opts.fakeFs.statSync(npath.toPortablePath(initialApiPath));
const defaultCache: typeof Module._cache = {};
const apiMetadata: Map<PortablePath, ApiMetadata> = new Map([
[initialApiPath, {
cache: Module._cache,
instance: pnpapi,
stats: initialApiStats,
}],
]);
if (opts.compatibilityMode !== false) {
// Modern versions of `resolve` support a specific entry point that custom resolvers can use
// to inject a specific resolution logic without having to patch the whole package.
//
// Cf: https://github.com/browserify/resolve/pull/174
patchedModules.push([
/^\.\/normalize-options\.js$/,
(issuer, normalizeOptions) => {
if (!issuer || issuer.name !== 'resolve')
return normalizeOptions;
return (request: string, opts: {[key: string]: any}) => {
opts = opts || {};
if (opts.forceNodeResolution)
return opts;
opts.paths = function (request: string, basedir: string, getNodeModulesDir: () => Array<string>, opts: any) {
// Extract the name of the package being requested (1=full name, 2=scope name, 3=local name)
const parts = request.match(/^((?:(@[^\/]+)\/)?([^\/]+))/);
if (!parts)
throw new Error(`Assertion failed: Expected the "resolve" package to call the "paths" callback with package names only (got "${request}")`);
// make sure that basedir ends with a slash
if (basedir.charAt(basedir.length - 1) !== '/')
basedir = path.join(basedir, '/');
const apiPath = findApiPathFor(basedir);
if (apiPath === null)
return getNodeModulesDir();
const apiEntry = getApiEntry(apiPath, true);
const api = apiEntry.instance;
// TODO Handle portable paths
// This is guaranteed to return the path to the "package.json" file from the given package
const manifestPath = api.resolveToUnqualified(`${parts[1]}/package.json`, basedir, {
considerBuiltins: false,
});
if (manifestPath === null)
throw new Error(`Assertion failed: The resolution thinks that "${parts[1]}" is a Node builtin`);
// The first dirname strips the package.json, the second strips the local named folder
let nodeModules = path.dirname(path.dirname(manifestPath));
// Strips the scope named folder if needed
if (parts[2])
nodeModules = path.dirname(nodeModules);
return [nodeModules];
};
return opts;
};
},
]);
}
/**
* Used to disable the resolution hooks (for when we want to fallback to the previous resolution - we then need
* a way to "reset" the environment temporarily)
*/
let enableNativeHooks = true;
// @ts-ignore
process.versions.pnp = String(pnpapi.VERSIONS.std);
function getRequireStack(parent: Module | null | undefined) {
const requireStack = [];
for (let cursor = parent; cursor; cursor = cursor.parent)
requireStack.push(cursor.filename || cursor.id);
return requireStack;
}
function loadApiInstance(pnpApiPath: PortablePath): PnpApi {
// @ts-ignore
const module = new Module(npath.fromPortablePath(pnpApiPath), null);
module.load(pnpApiPath);
return module.exports;
}
function refreshApiEntry(pnpApiPath: PortablePath, apiEntry: ApiMetadata) {
const stats = opts.fakeFs.statSync(pnpApiPath);
if (stats.mtime > apiEntry.stats.mtime) {
console.warn(`[Warning] The runtime detected new informations in a PnP file; reloading the API instance (${pnpApiPath})`);
apiEntry.instance = loadApiInstance(pnpApiPath);
apiEntry.stats = stats;
}
}
function getApiEntry(pnpApiPath: PortablePath, refresh = false) {
let apiEntry = apiMetadata.get(pnpApiPath);
if (typeof apiEntry !== `undefined`) {
if (refresh) {
refreshApiEntry(pnpApiPath, apiEntry);
}
} else {
apiMetadata.set(pnpApiPath, apiEntry = {
cache: {},
instance: loadApiInstance(pnpApiPath),
stats: opts.fakeFs.statSync(pnpApiPath),
});
}
return apiEntry;
}
function findApiPathFor(modulePath: NativePath) {
let curr: PortablePath;
let next = npath.toPortablePath(modulePath);
do {
curr = next;
const candidate = ppath.join(curr, `.pnp.js` as Filename);
if (xfs.existsSync(candidate) && xfs.statSync(candidate).isFile())
return candidate;
next = ppath.dirname(curr);
} while (curr !== PortablePath.root);
return null;
}
function getApiPathFromParent(parent: Module | null | undefined): PortablePath | null {
if (parent == null)
return initialApiPath;
if (typeof parent.pnpApiPath === `undefined`) {
if (parent.filename !== null) {
return findApiPathFor(parent.filename);
} else {
return initialApiPath;
}
}
if (parent.pnpApiPath !== null)
return parent.pnpApiPath;
return null;
}
// A small note: we don't replace the cache here (and instead use the native one). This is an effort to not
// break code similar to "delete require.cache[require.resolve(FOO)]", where FOO is a package located outside
// of the Yarn dependency tree. In this case, we defer the load to the native loader. If we were to replace the
// cache by our own, the native loader would populate its own cache, which wouldn't be exposed anymore, so the
// delete call would be broken.
const originalModuleLoad = Module._load;
Module._load = function(request: string, parent: NodeModule | null | undefined, isMain: boolean) {
if (!enableNativeHooks)
return originalModuleLoad.call(Module, request, parent, isMain);
// Builtins are managed by the regular Node loader
if (builtinModules.has(request)) {
try {
enableNativeHooks = false;
return originalModuleLoad.call(Module, request, parent, isMain);
} finally {
enableNativeHooks = true;
}
}
const parentApiPath = getApiPathFromParent(parent);
const parentApi = parentApiPath !== null
? getApiEntry(parentApiPath, true).instance
: null;
// The 'pnpapi' name is reserved to return the PnP api currently in use
// by the program
if (parentApi !== null && request === `pnpapi`)
return parentApi;
// Request `Module._resolveFilename` (ie. `resolveRequest`) to tell us
// which file we should load
const modulePath = Module._resolveFilename(request, parent, isMain);
// We check whether the module is owned by the dependency tree of the
// module that required it. If it isn't, then we need to create a new
// store and possibly load its sandboxed PnP runtime.
const isOwnedByRuntime = parentApi !== null
? parentApi.findPackageLocator(modulePath) !== null
: false;
const moduleApiPath = isOwnedByRuntime
? parentApiPath
: findApiPathFor(npath.dirname(modulePath));
const entry = moduleApiPath !== null
? getApiEntry(moduleApiPath)
: {instance: null, cache: defaultCache};
// Check if the module has already been created for the given file
const cacheEntry = entry.cache[modulePath];
if (cacheEntry)
return cacheEntry.exports;
// Create a new module and store it into the cache
// @ts-ignore
const module = new Module(modulePath, parent);
module.pnpApiPath = moduleApiPath;
entry.cache[modulePath] = module;
// The main module is exposed as global variable
if (isMain) {
// @ts-ignore
process.mainModule = module;
module.id = '.';
}
// Try to load the module, and remove it from the cache if it fails
let hasThrown = true;
try {
module.load(modulePath);
hasThrown = false;
} finally {
if (hasThrown) {
delete Module._cache[modulePath];
}
}
// Some modules might have to be patched for compatibility purposes
if (entry.instance !== null) {
for (const [filter, patchFn] of patchedModules) {
if (filter.test(request)) {
const issuer = parent && parent.filename
? entry.instance.findPackageLocator(parent.filename)
: null;
module.exports = patchFn(issuer, module.exports);
}
}
}
return module.exports;
};
const originalModuleResolveFilename = Module._resolveFilename;
Module._resolveFilename = function(request: string, parent: NodeModule | null | undefined, isMain: boolean, options?: {[key: string]: any}) {
if (builtinModules.has(request))
return request;
if (!enableNativeHooks)
return originalModuleResolveFilename.call(Module, request, parent, isMain, options);
if (options && options.plugnplay === false) {
const {plugnplay, ...rest} = options;
// Workaround a bug present in some version of Node (now fixed)
// https://github.com/nodejs/node/pull/28078
const forwardedOptions = Object.keys(rest).length > 0
? rest
: undefined;
try {
enableNativeHooks = false;
return originalModuleResolveFilename.call(Module, request, parent, isMain, forwardedOptions);
} finally {
enableNativeHooks = true;
}
}
// We check that all the options present here are supported; better
// to fail fast than to introduce subtle bugs in the runtime.
if (options) {
const optionNames = new Set(Object.keys(options));
optionNames.delete(`paths`);
optionNames.delete(`plugnplay`);
if (optionNames.size > 0) {
throw makeError(
ErrorCode.UNSUPPORTED,
`Some options passed to require() aren't supported by PnP yet (${Array.from(optionNames).join(', ')})`
);
}
}
const getIssuerSpecsFromPaths = (paths: Array<NativePath>) => {
return paths.map(path => ({
apiPath: findApiPathFor(path),
path: npath.toPortablePath(path),
module: null,
}));
};
const getIssuerSpecsFromModule = (module: NodeModule | null | undefined) => {
const issuer = getIssuerModule(module);
const issuerPath = issuer !== null
? npath.dirname(issuer.filename)
: process.cwd();
return [{
apiPath: getApiPathFromParent(issuer),
path: npath.toPortablePath(issuerPath),
module,
}];
};
const makeFakeParent = (path: PortablePath) => {
const fakeParent = new Module(``);
const fakeFilePath = ppath.join(path, `[file]` as Filename);
fakeParent.paths = Module._nodeModulePaths(npath.fromPortablePath(fakeFilePath));
return fakeParent;
};
const issuerSpecs = options && options.paths
? getIssuerSpecsFromPaths(options.paths)
: getIssuerSpecsFromModule(parent);
let firstError;
for (const {apiPath, path, module} of issuerSpecs) {
let resolution;
const issuerApi = apiPath !== null
? getApiEntry(apiPath, true).instance
: null;
try {
if (issuerApi !== null) {
resolution = issuerApi.resolveRequest(request, `${path}/`);
} else {
resolution = originalModuleResolveFilename.call(Module, request, module || makeFakeParent(path), isMain);
}
} catch (error) {
firstError = firstError || error;
continue;
}
if (resolution !== null) {
return resolution;
}
}
const requireStack = getRequireStack(parent);
firstError.requireStack = requireStack;
if (requireStack.length > 0)
firstError.message += `\nRequire stack:\n- ${requireStack.join(`\n- `)}`;
throw firstError;
};
const originalFindPath = Module._findPath;
Module._findPath = function(request: string, paths: Array<string> | null | undefined, isMain: boolean) {
if (request === `pnpapi`)
return false;
if (!enableNativeHooks)
return originalFindPath.call(Module, request, paths, isMain);
for (const path of paths || []) {
let resolution: string | false;
try {
const pnpApiPath = findApiPathFor(path);
if (pnpApiPath !== null) {
const api = getApiEntry(pnpApiPath, true).instance;
resolution = api.resolveRequest(request, path) || false;
} else {
resolution = originalFindPath.call(Module, request, [path], isMain);
}
} catch (error) {
continue;
}
if (resolution) {
return resolution;
}
}
return false;
};
patchFs(fs, new PosixFS(opts.fakeFs));
};