@yarnpkg/pnp
Advanced tools
Comparing version 2.0.0-rc.13 to 2.0.0-rc.14
@@ -14,4 +14,2 @@ "use strict"; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _string_decoder = _interopRequireDefault(require("string_decoder")); | ||
@@ -25,2 +23,4 @@ | ||
var _makeManager = require("./makeManager"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -35,4 +35,3 @@ | ||
const defaultRuntimeState = $$SETUP_STATE(_hydrateRuntimeState.hydrateRuntimeState); | ||
const defaultPnpapiResolution = _path.default.resolve(__dirname, __filename); // We create a virtual filesystem that will do three things: | ||
const defaultPnpapiResolution = __filename; // We create a virtual filesystem that will do three things: | ||
// 1. all requests inside a folder named "$$virtual" will be remapped according the virtual folder rules | ||
@@ -42,3 +41,2 @@ // 2. all requests going inside a Zip archive will be handled by the Zip fs implementation | ||
const defaultFsLayer = new _fslib.VirtualFS({ | ||
@@ -51,2 +49,3 @@ baseFs: new _fslib.ZipOpenFS({ | ||
}); | ||
let manager; | ||
const defaultApi = Object.assign((0, _makeApi.makeApi)(defaultRuntimeState, { | ||
@@ -76,2 +75,15 @@ fakeFs: defaultFsLayer, | ||
/** | ||
* Can be used to retrieve the API for a particular file, or `null` if the | ||
* specified location doesn't seem to be part of a PnP runtime. Note that | ||
* this function return value may change when the underlying PnP hook gets | ||
* overriden (Yarn automatically reloads them when this happen). | ||
*/ | ||
findApiFromPath: modulePath => { | ||
const apiPath = manager.findApiPathFor(modulePath); | ||
if (apiPath === null) return null; | ||
const apiEntry = manager.getApiEntry(apiPath, true); | ||
return apiEntry.instance; | ||
}, | ||
/** | ||
* Will inject the specified API into the environment, monkey-patching FS. Is | ||
@@ -82,5 +94,9 @@ * automatically called when the hook is loaded through `--require`. | ||
(0, _applyPatch.applyPatch)(api || defaultApi, { | ||
fakeFs: defaultFsLayer | ||
fakeFs: defaultFsLayer, | ||
manager | ||
}); | ||
} | ||
}); | ||
manager = (0, _makeManager.makeManager)(defaultApi, { | ||
fakeFs: defaultFsLayer | ||
}); // eslint-disable-next-line arca/no-default-export | ||
@@ -87,0 +103,0 @@ |
@@ -14,4 +14,2 @@ "use strict"; | ||
var _path = _interopRequireDefault(require("path")); | ||
var _internalTools = require("./internalTools"); | ||
@@ -23,55 +21,8 @@ | ||
// @ts-ignore | ||
const builtinModules = new Set(_module.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 builtinModules = new Set(_module.Module.builtinModules || Object.keys(process.binding('natives'))); | ||
/** | ||
* The cache that will be used for all accesses occuring outside of a PnP context. | ||
*/ | ||
const patchedModules = []; | ||
const initialApiPath = _fslib.npath.toPortablePath(pnpapi.resolveToUnqualified(`pnpapi`, null)); | ||
const initialApiStats = opts.fakeFs.statSync(_fslib.npath.toPortablePath(initialApiPath)); | ||
const defaultCache = {}; | ||
const apiMetadata = new Map([[initialApiPath, { | ||
cache: _module.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, opts) => { | ||
opts = opts || {}; | ||
if (opts.forceNodeResolution) return opts; | ||
opts.paths = function (request, basedir, getNodeModulesDir, opts) { | ||
// 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.default.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.default.dirname(_path.default.dirname(manifestPath)); // Strips the scope named folder if needed | ||
if (parts[2]) nodeModules = _path.default.dirname(nodeModules); | ||
return [nodeModules]; | ||
}; | ||
return opts; | ||
}; | ||
}]); | ||
} | ||
/** | ||
@@ -82,3 +33,2 @@ * Used to disable the resolution hooks (for when we want to fallback to the previous resolution - we then need | ||
let enableNativeHooks = true; // @ts-ignore | ||
@@ -94,69 +44,2 @@ | ||
return requireStack; | ||
} | ||
function loadApiInstance(pnpApiPath) { | ||
// @ts-ignore | ||
const module = new _module.Module(_fslib.npath.fromPortablePath(pnpApiPath), null); | ||
module.load(pnpApiPath); | ||
return module.exports; | ||
} | ||
function refreshApiEntry(pnpApiPath, apiEntry) { | ||
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, 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) { | ||
let curr; | ||
let next = _fslib.npath.toPortablePath(modulePath); | ||
do { | ||
curr = next; | ||
const candidate = _fslib.ppath.join(curr, `.pnp.js`); | ||
if (_fslib.xfs.existsSync(candidate) && _fslib.xfs.statSync(candidate).isFile()) return candidate; | ||
next = _fslib.ppath.dirname(curr); | ||
} while (curr !== _fslib.PortablePath.root); | ||
return null; | ||
} | ||
function getApiPathFromParent(parent) { | ||
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 | ||
@@ -183,4 +66,4 @@ // break code similar to "delete require.cache[require.resolve(FOO)]", where FOO is a package located outside | ||
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 | ||
const parentApiPath = opts.manager.getApiPathFromParent(parent); | ||
const parentApi = parentApiPath !== null ? opts.manager.getApiEntry(parentApiPath, true).instance : null; // The 'pnpapi' name is reserved to return the PnP api currently in use | ||
// by the program | ||
@@ -197,4 +80,4 @@ | ||
const isOwnedByRuntime = parentApi !== null ? parentApi.findPackageLocator(modulePath) !== null : false; | ||
const moduleApiPath = isOwnedByRuntime ? parentApiPath : findApiPathFor(_fslib.npath.dirname(modulePath)); | ||
const entry = moduleApiPath !== null ? getApiEntry(moduleApiPath) : { | ||
const moduleApiPath = isOwnedByRuntime ? parentApiPath : opts.manager.findApiPathFor(_fslib.npath.dirname(modulePath)); | ||
const entry = moduleApiPath !== null ? opts.manager.getApiEntry(moduleApiPath) : { | ||
instance: null, | ||
@@ -228,12 +111,2 @@ cache: defaultCache | ||
} | ||
} // 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); | ||
} | ||
} | ||
} | ||
@@ -281,3 +154,3 @@ | ||
return paths.map(path => ({ | ||
apiPath: findApiPathFor(path), | ||
apiPath: opts.manager.findApiPathFor(path), | ||
path: _fslib.npath.toPortablePath(path), | ||
@@ -292,3 +165,3 @@ module: null | ||
return [{ | ||
apiPath: getApiPathFromParent(issuer), | ||
apiPath: opts.manager.getApiPathFromParent(issuer), | ||
path: _fslib.npath.toPortablePath(issuerPath), | ||
@@ -317,3 +190,3 @@ module | ||
let resolution; | ||
const issuerApi = apiPath !== null ? getApiEntry(apiPath, true).instance : null; | ||
const issuerApi = apiPath !== null ? opts.manager.getApiEntry(apiPath, true).instance : null; | ||
@@ -352,6 +225,6 @@ try { | ||
try { | ||
const pnpApiPath = findApiPathFor(path); | ||
const pnpApiPath = opts.manager.findApiPathFor(path); | ||
if (pnpApiPath !== null) { | ||
const api = getApiEntry(pnpApiPath, true).instance; | ||
const api = opts.manager.getApiEntry(pnpApiPath, true).instance; | ||
resolution = api.resolveRequest(request, path) || false; | ||
@@ -358,0 +231,0 @@ } else { |
{ | ||
"name": "@yarnpkg/pnp", | ||
"version": "2.0.0-rc.13", | ||
"version": "2.0.0-rc.14", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16
0
672330
1556
8