Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async fetch(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: RAW_LINK_PROTOCOL});
// If the link target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch = ppath.isAbsolute(path)
? {packageFs: new NodeFS(), prefixPath: PortablePath.root, localPath: PortablePath.root}
: await opts.fetcher.fetch(parentLocator, opts);
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
const sourceFs = effectiveParentFetch.packageFs;
const sourcePath = ppath.resolve(effectiveParentFetch.prefixPath, path);
if (parentFetch.localPath) {
async fetchFromDisk(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PROTOCOL});
// If the file target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch = ppath.isAbsolute(path)
? {packageFs: new NodeFS(), prefixPath: PortablePath.root, localPath: PortablePath.root}
: await opts.fetcher.fetch(parentLocator, opts);
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
const sourceFs = effectiveParentFetch.packageFs;
const sourcePath = ppath.resolve(effectiveParentFetch.prefixPath, path);
const sourceBuffer = await sourceFs.readFilePromise(sourcePath);
export async function loadPatchFiles(parentLocator: Locator | null, patchPaths: Array, opts: FetchOptions) {
// When the patch files use absolute paths we can directly access them via
// their location on the disk. Otherwise we must go through the package fs.
const parentFetch = parentLocator !== null
? await opts.fetcher.fetch(parentLocator, opts)
: null;
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch && parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath, releaseFs: undefined}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch && parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
// First we obtain the specification for all the patches that we'll have to
// apply to the original package.
return await miscUtils.releaseAfterUseAsync(async () => {
return await Promise.all(patchPaths.map(async patchPath => visitPatchPath({
onAbsolute: async () => {
return await xfs.readFilePromise(patchPath, `utf8`);
},
onRelative: async () => {
if (parentFetch === null)
async fetch(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: LINK_PROTOCOL});
// If the link target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch = ppath.isAbsolute(path)
? {packageFs: new NodeFS(), prefixPath: PortablePath.root, localPath: PortablePath.root}
: await opts.fetcher.fetch(parentLocator, opts);
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
const sourceFs = effectiveParentFetch.packageFs;
const sourcePath = ppath.resolve(effectiveParentFetch.prefixPath, path);
if (parentFetch.localPath) {
private async fetchFromDisk(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PROTOCOL});
// If the file target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch = ppath.isAbsolute(path)
? {packageFs: new NodeFS(), prefixPath: PortablePath.root, localPath: PortablePath.root}
: await opts.fetcher.fetch(parentLocator, opts);
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
const sourceFs = effectiveParentFetch.packageFs;
const sourcePath = ppath.resolve(effectiveParentFetch.prefixPath, path);
return await miscUtils.releaseAfterUseAsync(async () => {
return await tgzUtils.makeArchiveFromDirectory(sourcePath, {
baseFs: sourceFs,
prefixPath: structUtils.getIdentVendorPath(locator),
});
}, effectiveParentFetch.releaseFs);
}
import StringDecoder from 'string_decoder';
import {RuntimeState, PnpApi} from '../types';
import {applyPatch} from './applyPatch';
import {hydrateRuntimeState} from './hydrateRuntimeState';
import {MakeApiOptions, makeApi} from './makeApi';
declare var __non_webpack_module__: NodeModule;
declare var $$SETUP_STATE: (hrs: typeof hydrateRuntimeState, basePath?: NativePath) => RuntimeState;
// We must copy the fs into a local, because otherwise
// 1. we would make the NodeFS instance use the function that we patched (infinite loop)
// 2. Object.create(fs) isn't enough, since it won't prevent the proto from being modified
const localFs: typeof fs = {...fs};
const nodeFs = new NodeFS(localFs);
const defaultRuntimeState = $$SETUP_STATE(hydrateRuntimeState);
const defaultPnpapiResolution = path.resolve(__dirname, __filename);
let defaultFsLayer: FakeFS = new ZipOpenFS({baseFs: nodeFs});
for (const virtualRoot of defaultRuntimeState.virtualRoots)
defaultFsLayer = new VirtualFS(virtualRoot, {baseFs: defaultFsLayer});
const defaultApi = Object.assign(makeApi(defaultRuntimeState, {
fakeFs: defaultFsLayer,
pnpapiResolution: defaultPnpapiResolution,
}), {
/**
* Can be used to generate a different API than the default one (for example
* to map it on `/` rather than the local directory path, or to use a
* different FS layer than the default one).
return new Promise((resolve, reject) => {
const fs = new NodeFS();
const hash = createHash(`sha512`);
const stream = fs.createReadStream(path, {});
stream.on(`data`, chunk => {
hash.update(chunk);
});
stream.on(`error`, error => {
reject(error);
});
stream.on(`end`, () => {
resolve(hash.digest(`hex`));
});
});
export async function makeArchiveFromDirectory(source: PortablePath, {baseFs = new NodeFS(), prefixPath = PortablePath.root}: MakeArchiveFromDirectoryOptions = {}): Promise {
const zipFs = new ZipFS(NodeFS.toPortablePath(tmpNameSync()), {create: true});
const target = ppath.resolve(PortablePath.root, prefixPath!);
await zipFs.copyPromise(target, source, {baseFs});
return zipFs;
}
static async find(path: PortablePath, {baseFs = new NodeFS()}: {baseFs?: FakeFS} = {}) {
return await Manifest.fromFile(ppath.join(path, toFilename(`package.json`)), {baseFs});
}
static async fromFile(path: PortablePath, {baseFs = new NodeFS()}: {baseFs?: FakeFS} = {}) {
const manifest = new Manifest();
await manifest.loadFile(path, {baseFs});
return manifest;
}