3
0
Fork 0
iceshrimp/.pnp.loader.mjs

2077 lines
68 KiB
JavaScript
Raw Permalink Normal View History

2023-07-27 15:57:32 +02:00
import fs from 'fs';
2023-11-10 05:08:12 +01:00
import { URL as URL$1, fileURLToPath, pathToFileURL } from 'url';
2023-07-27 15:57:32 +02:00
import path from 'path';
2023-11-10 05:08:12 +01:00
import { createHash } from 'crypto';
2023-07-27 15:57:32 +02:00
import { EOL } from 'os';
2023-11-10 05:08:12 +01:00
import moduleExports, { isBuiltin } from 'module';
2023-07-27 15:57:32 +02:00
import assert from 'assert';
const SAFE_TIME = 456789e3;
const PortablePath = {
root: `/`,
dot: `.`,
parent: `..`
};
const npath = Object.create(path);
const ppath = Object.create(path.posix);
npath.cwd = () => process.cwd();
2023-11-10 05:08:12 +01:00
ppath.cwd = process.platform === `win32` ? () => toPortablePath(process.cwd()) : process.cwd;
if (process.platform === `win32`) {
ppath.resolve = (...segments) => {
if (segments.length > 0 && ppath.isAbsolute(segments[0])) {
return path.posix.resolve(...segments);
} else {
return path.posix.resolve(ppath.cwd(), ...segments);
}
};
}
2023-07-27 15:57:32 +02:00
const contains = function(pathUtils, from, to) {
from = pathUtils.normalize(from);
to = pathUtils.normalize(to);
if (from === to)
return `.`;
if (!from.endsWith(pathUtils.sep))
from = from + pathUtils.sep;
if (to.startsWith(from)) {
return to.slice(from.length);
} else {
return null;
}
};
npath.contains = (from, to) => contains(npath, from, to);
ppath.contains = (from, to) => contains(ppath, from, to);
const WINDOWS_PATH_REGEXP = /^([a-zA-Z]:.*)$/;
const UNC_WINDOWS_PATH_REGEXP = /^\/\/(\.\/)?(.*)$/;
const PORTABLE_PATH_REGEXP = /^\/([a-zA-Z]:.*)$/;
const UNC_PORTABLE_PATH_REGEXP = /^\/unc\/(\.dot\/)?(.*)$/;
2023-11-10 05:08:12 +01:00
function fromPortablePathWin32(p) {
2023-07-27 15:57:32 +02:00
let portablePathMatch, uncPortablePathMatch;
if (portablePathMatch = p.match(PORTABLE_PATH_REGEXP))
p = portablePathMatch[1];
else if (uncPortablePathMatch = p.match(UNC_PORTABLE_PATH_REGEXP))
p = `\\\\${uncPortablePathMatch[1] ? `.\\` : ``}${uncPortablePathMatch[2]}`;
else
return p;
return p.replace(/\//g, `\\`);
}
2023-11-10 05:08:12 +01:00
function toPortablePathWin32(p) {
2023-07-27 15:57:32 +02:00
p = p.replace(/\\/g, `/`);
let windowsPathMatch, uncWindowsPathMatch;
if (windowsPathMatch = p.match(WINDOWS_PATH_REGEXP))
p = `/${windowsPathMatch[1]}`;
else if (uncWindowsPathMatch = p.match(UNC_WINDOWS_PATH_REGEXP))
p = `/unc/${uncWindowsPathMatch[1] ? `.dot/` : ``}${uncWindowsPathMatch[2]}`;
return p;
}
2023-11-10 05:08:12 +01:00
const toPortablePath = process.platform === `win32` ? toPortablePathWin32 : (p) => p;
const fromPortablePath = process.platform === `win32` ? fromPortablePathWin32 : (p) => p;
npath.fromPortablePath = fromPortablePath;
npath.toPortablePath = toPortablePath;
2023-07-27 15:57:32 +02:00
function convertPath(targetPathUtils, sourcePath) {
return targetPathUtils === npath ? fromPortablePath(sourcePath) : toPortablePath(sourcePath);
}
const defaultTime = new Date(SAFE_TIME * 1e3);
2023-11-10 05:08:12 +01:00
const defaultTimeMs = defaultTime.getTime();
2023-07-27 15:57:32 +02:00
async function copyPromise(destinationFs, destination, sourceFs, source, opts) {
const normalizedDestination = destinationFs.pathUtils.normalize(destination);
const normalizedSource = sourceFs.pathUtils.normalize(source);
const prelayout = [];
const postlayout = [];
const { atime, mtime } = opts.stableTime ? { atime: defaultTime, mtime: defaultTime } : await sourceFs.lstatPromise(normalizedSource);
await destinationFs.mkdirpPromise(destinationFs.pathUtils.dirname(destination), { utimes: [atime, mtime] });
2023-11-10 05:08:12 +01:00
await copyImpl(prelayout, postlayout, destinationFs, normalizedDestination, sourceFs, normalizedSource, { ...opts, didParentExist: true });
2023-07-27 15:57:32 +02:00
for (const operation of prelayout)
await operation();
await Promise.all(postlayout.map((operation) => {
return operation();
}));
}
2023-11-10 05:08:12 +01:00
async function copyImpl(prelayout, postlayout, destinationFs, destination, sourceFs, source, opts) {
2023-07-27 15:57:32 +02:00
const destinationStat = opts.didParentExist ? await maybeLStat(destinationFs, destination) : null;
const sourceStat = await sourceFs.lstatPromise(source);
const { atime, mtime } = opts.stableTime ? { atime: defaultTime, mtime: defaultTime } : sourceStat;
let updated;
switch (true) {
case sourceStat.isDirectory():
{
2023-11-10 05:08:12 +01:00
updated = await copyFolder(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts);
2023-07-27 15:57:32 +02:00
}
break;
case sourceStat.isFile():
{
2023-11-10 05:08:12 +01:00
updated = await copyFile(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts);
2023-07-27 15:57:32 +02:00
}
break;
case sourceStat.isSymbolicLink():
{
2023-11-10 05:08:12 +01:00
updated = await copySymlink(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts);
2023-07-27 15:57:32 +02:00
}
break;
2024-01-31 22:06:22 +01:00
default: {
throw new Error(`Unsupported file type (${sourceStat.mode})`);
}
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
if (opts.linkStrategy?.type !== `HardlinkFromIndex` || !sourceStat.isFile()) {
if (updated || destinationStat?.mtime?.getTime() !== mtime.getTime() || destinationStat?.atime?.getTime() !== atime.getTime()) {
postlayout.push(() => destinationFs.lutimesPromise(destination, atime, mtime));
updated = true;
}
if (destinationStat === null || (destinationStat.mode & 511) !== (sourceStat.mode & 511)) {
postlayout.push(() => destinationFs.chmodPromise(destination, sourceStat.mode & 511));
updated = true;
}
2023-07-27 15:57:32 +02:00
}
return updated;
}
async function maybeLStat(baseFs, p) {
try {
return await baseFs.lstatPromise(p);
} catch (e) {
return null;
}
}
2023-11-10 05:08:12 +01:00
async function copyFolder(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts) {
2023-07-27 15:57:32 +02:00
if (destinationStat !== null && !destinationStat.isDirectory()) {
if (opts.overwrite) {
prelayout.push(async () => destinationFs.removePromise(destination));
destinationStat = null;
} else {
return false;
}
}
let updated = false;
if (destinationStat === null) {
prelayout.push(async () => {
try {
await destinationFs.mkdirPromise(destination, { mode: sourceStat.mode });
} catch (err) {
if (err.code !== `EEXIST`) {
throw err;
}
}
});
updated = true;
}
const entries = await sourceFs.readdirPromise(source);
const nextOpts = opts.didParentExist && !destinationStat ? { ...opts, didParentExist: false } : opts;
if (opts.stableSort) {
for (const entry of entries.sort()) {
2023-11-10 05:08:12 +01:00
if (await copyImpl(prelayout, postlayout, destinationFs, destinationFs.pathUtils.join(destination, entry), sourceFs, sourceFs.pathUtils.join(source, entry), nextOpts)) {
2023-07-27 15:57:32 +02:00
updated = true;
}
}
} else {
const entriesUpdateStatus = await Promise.all(entries.map(async (entry) => {
2023-11-10 05:08:12 +01:00
await copyImpl(prelayout, postlayout, destinationFs, destinationFs.pathUtils.join(destination, entry), sourceFs, sourceFs.pathUtils.join(source, entry), nextOpts);
2023-07-27 15:57:32 +02:00
}));
if (entriesUpdateStatus.some((status) => status)) {
updated = true;
}
}
return updated;
}
2023-11-10 05:08:12 +01:00
async function copyFileViaIndex(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts, linkStrategy) {
const sourceHash = await sourceFs.checksumFilePromise(source, { algorithm: `sha1` });
2024-01-31 22:06:22 +01:00
const defaultMode = 420;
const sourceMode = sourceStat.mode & 511;
const indexFileName = `${sourceHash}${sourceMode !== defaultMode ? sourceMode.toString(8) : ``}`;
const indexPath = destinationFs.pathUtils.join(linkStrategy.indexPath, sourceHash.slice(0, 2), `${indexFileName}.dat`);
2023-11-10 05:08:12 +01:00
let AtomicBehavior;
((AtomicBehavior2) => {
AtomicBehavior2[AtomicBehavior2["Lock"] = 0] = "Lock";
AtomicBehavior2[AtomicBehavior2["Rename"] = 1] = "Rename";
})(AtomicBehavior || (AtomicBehavior = {}));
let atomicBehavior = 1 /* Rename */;
let indexStat = await maybeLStat(destinationFs, indexPath);
if (destinationStat) {
const isDestinationHardlinkedFromIndex = indexStat && destinationStat.dev === indexStat.dev && destinationStat.ino === indexStat.ino;
const isIndexModified = indexStat?.mtimeMs !== defaultTimeMs;
if (isDestinationHardlinkedFromIndex) {
if (isIndexModified && linkStrategy.autoRepair) {
atomicBehavior = 0 /* Lock */;
indexStat = null;
}
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
if (!isDestinationHardlinkedFromIndex) {
if (opts.overwrite) {
prelayout.push(async () => destinationFs.removePromise(destination));
destinationStat = null;
} else {
return false;
2023-07-27 15:57:32 +02:00
}
}
}
2023-11-10 05:08:12 +01:00
const tempPath = !indexStat && atomicBehavior === 1 /* Rename */ ? `${indexPath}.${Math.floor(Math.random() * 4294967296).toString(16).padStart(8, `0`)}` : null;
let tempPathCleaned = false;
prelayout.push(async () => {
if (!indexStat) {
if (atomicBehavior === 0 /* Lock */) {
await destinationFs.lockPromise(indexPath, async () => {
const content = await sourceFs.readFilePromise(source);
await destinationFs.writeFilePromise(indexPath, content);
});
}
if (atomicBehavior === 1 /* Rename */ && tempPath) {
const content = await sourceFs.readFilePromise(source);
await destinationFs.writeFilePromise(tempPath, content);
try {
await destinationFs.linkPromise(tempPath, indexPath);
} catch (err) {
if (err.code === `EEXIST`) {
tempPathCleaned = true;
await destinationFs.unlinkPromise(tempPath);
} else {
throw err;
}
}
}
}
if (!destinationStat) {
await destinationFs.linkPromise(indexPath, destination);
}
});
postlayout.push(async () => {
2024-01-31 22:06:22 +01:00
if (!indexStat) {
2023-11-10 05:08:12 +01:00
await destinationFs.lutimesPromise(indexPath, defaultTime, defaultTime);
2024-01-31 22:06:22 +01:00
if (sourceMode !== defaultMode) {
await destinationFs.chmodPromise(indexPath, sourceMode);
}
}
2023-11-10 05:08:12 +01:00
if (tempPath && !tempPathCleaned) {
await destinationFs.unlinkPromise(tempPath);
}
});
return false;
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
async function copyFileDirect(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts) {
2023-07-27 15:57:32 +02:00
if (destinationStat !== null) {
if (opts.overwrite) {
prelayout.push(async () => destinationFs.removePromise(destination));
destinationStat = null;
} else {
return false;
}
}
2023-11-10 05:08:12 +01:00
prelayout.push(async () => {
const content = await sourceFs.readFilePromise(source);
await destinationFs.writeFilePromise(destination, content);
});
2023-07-27 15:57:32 +02:00
return true;
}
2023-11-10 05:08:12 +01:00
async function copyFile(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts) {
if (opts.linkStrategy?.type === `HardlinkFromIndex`) {
return copyFileViaIndex(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts, opts.linkStrategy);
} else {
return copyFileDirect(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts);
}
}
async function copySymlink(prelayout, postlayout, destinationFs, destination, destinationStat, sourceFs, source, sourceStat, opts) {
2023-07-27 15:57:32 +02:00
if (destinationStat !== null) {
if (opts.overwrite) {
prelayout.push(async () => destinationFs.removePromise(destination));
destinationStat = null;
} else {
return false;
}
}
prelayout.push(async () => {
await destinationFs.symlinkPromise(convertPath(destinationFs.pathUtils, await sourceFs.readlinkPromise(source)), destination);
});
return true;
}
class FakeFS {
constructor(pathUtils) {
this.pathUtils = pathUtils;
}
async *genTraversePromise(init, { stableSort = false } = {}) {
const stack = [init];
while (stack.length > 0) {
const p = stack.shift();
const entry = await this.lstatPromise(p);
if (entry.isDirectory()) {
const entries = await this.readdirPromise(p);
if (stableSort) {
for (const entry2 of entries.sort()) {
stack.push(this.pathUtils.join(p, entry2));
}
} else {
throw new Error(`Not supported`);
}
} else {
yield p;
}
}
}
2023-11-10 05:08:12 +01:00
async checksumFilePromise(path, { algorithm = `sha512` } = {}) {
const fd = await this.openPromise(path, `r`);
try {
const CHUNK_SIZE = 65536;
const chunk = Buffer.allocUnsafeSlow(CHUNK_SIZE);
const hash = createHash(algorithm);
let bytesRead = 0;
while ((bytesRead = await this.readPromise(fd, chunk, 0, CHUNK_SIZE)) !== 0)
hash.update(bytesRead === CHUNK_SIZE ? chunk : chunk.slice(0, bytesRead));
return hash.digest(`hex`);
} finally {
await this.closePromise(fd);
}
}
2023-07-27 15:57:32 +02:00
async removePromise(p, { recursive = true, maxRetries = 5 } = {}) {
let stat;
try {
stat = await this.lstatPromise(p);
} catch (error) {
if (error.code === `ENOENT`) {
return;
} else {
throw error;
}
}
if (stat.isDirectory()) {
if (recursive) {
const entries = await this.readdirPromise(p);
await Promise.all(entries.map((entry) => {
return this.removePromise(this.pathUtils.resolve(p, entry));
}));
}
for (let t = 0; t <= maxRetries; t++) {
try {
await this.rmdirPromise(p);
break;
} catch (error) {
if (error.code !== `EBUSY` && error.code !== `ENOTEMPTY`) {
throw error;
} else if (t < maxRetries) {
await new Promise((resolve) => setTimeout(resolve, t * 100));
}
}
}
} else {
await this.unlinkPromise(p);
}
}
removeSync(p, { recursive = true } = {}) {
let stat;
try {
stat = this.lstatSync(p);
} catch (error) {
if (error.code === `ENOENT`) {
return;
} else {
throw error;
}
}
if (stat.isDirectory()) {
if (recursive)
for (const entry of this.readdirSync(p))
this.removeSync(this.pathUtils.resolve(p, entry));
this.rmdirSync(p);
} else {
this.unlinkSync(p);
}
}
async mkdirpPromise(p, { chmod, utimes } = {}) {
p = this.resolve(p);
if (p === this.pathUtils.dirname(p))
return void 0;
const parts = p.split(this.pathUtils.sep);
let createdDirectory;
for (let u = 2; u <= parts.length; ++u) {
const subPath = parts.slice(0, u).join(this.pathUtils.sep);
if (!this.existsSync(subPath)) {
try {
await this.mkdirPromise(subPath);
} catch (error) {
if (error.code === `EEXIST`) {
continue;
} else {
throw error;
}
}
2023-11-10 05:08:12 +01:00
createdDirectory ??= subPath;
2023-07-27 15:57:32 +02:00
if (chmod != null)
await this.chmodPromise(subPath, chmod);
if (utimes != null) {
await this.utimesPromise(subPath, utimes[0], utimes[1]);
} else {
const parentStat = await this.statPromise(this.pathUtils.dirname(subPath));
await this.utimesPromise(subPath, parentStat.atime, parentStat.mtime);
}
}
}
return createdDirectory;
}
mkdirpSync(p, { chmod, utimes } = {}) {
p = this.resolve(p);
if (p === this.pathUtils.dirname(p))
return void 0;
const parts = p.split(this.pathUtils.sep);
let createdDirectory;
for (let u = 2; u <= parts.length; ++u) {
const subPath = parts.slice(0, u).join(this.pathUtils.sep);
if (!this.existsSync(subPath)) {
try {
this.mkdirSync(subPath);
} catch (error) {
if (error.code === `EEXIST`) {
continue;
} else {
throw error;
}
}
2023-11-10 05:08:12 +01:00
createdDirectory ??= subPath;
2023-07-27 15:57:32 +02:00
if (chmod != null)
this.chmodSync(subPath, chmod);
if (utimes != null) {
this.utimesSync(subPath, utimes[0], utimes[1]);
} else {
const parentStat = this.statSync(this.pathUtils.dirname(subPath));
this.utimesSync(subPath, parentStat.atime, parentStat.mtime);
}
}
}
return createdDirectory;
}
async copyPromise(destination, source, { baseFs = this, overwrite = true, stableSort = false, stableTime = false, linkStrategy = null } = {}) {
return await copyPromise(this, destination, baseFs, source, { overwrite, stableSort, stableTime, linkStrategy });
}
copySync(destination, source, { baseFs = this, overwrite = true } = {}) {
const stat = baseFs.lstatSync(source);
const exists = this.existsSync(destination);
if (stat.isDirectory()) {
this.mkdirpSync(destination);
const directoryListing = baseFs.readdirSync(source);
for (const entry of directoryListing) {
this.copySync(this.pathUtils.join(destination, entry), baseFs.pathUtils.join(source, entry), { baseFs, overwrite });
}
} else if (stat.isFile()) {
if (!exists || overwrite) {
if (exists)
this.removeSync(destination);
const content = baseFs.readFileSync(source);
this.writeFileSync(destination, content);
}
} else if (stat.isSymbolicLink()) {
if (!exists || overwrite) {
if (exists)
this.removeSync(destination);
const target = baseFs.readlinkSync(source);
this.symlinkSync(convertPath(this.pathUtils, target), destination);
}
} else {
throw new Error(`Unsupported file type (file: ${source}, mode: 0o${stat.mode.toString(8).padStart(6, `0`)})`);
}
const mode = stat.mode & 511;
this.chmodSync(destination, mode);
}
async changeFilePromise(p, content, opts = {}) {
if (Buffer.isBuffer(content)) {
return this.changeFileBufferPromise(p, content, opts);
} else {
return this.changeFileTextPromise(p, content, opts);
}
}
async changeFileBufferPromise(p, content, { mode } = {}) {
let current = Buffer.alloc(0);
try {
current = await this.readFilePromise(p);
} catch (error) {
}
if (Buffer.compare(current, content) === 0)
return;
await this.writeFilePromise(p, content, { mode });
}
async changeFileTextPromise(p, content, { automaticNewlines, mode } = {}) {
let current = ``;
try {
current = await this.readFilePromise(p, `utf8`);
} catch (error) {
}
const normalizedContent = automaticNewlines ? normalizeLineEndings(current, content) : content;
if (current === normalizedContent)
return;
await this.writeFilePromise(p, normalizedContent, { mode });
}
changeFileSync(p, content, opts = {}) {
if (Buffer.isBuffer(content)) {
return this.changeFileBufferSync(p, content, opts);
} else {
return this.changeFileTextSync(p, content, opts);
}
}
changeFileBufferSync(p, content, { mode } = {}) {
let current = Buffer.alloc(0);
try {
current = this.readFileSync(p);
} catch (error) {
}
if (Buffer.compare(current, content) === 0)
return;
this.writeFileSync(p, content, { mode });
}
changeFileTextSync(p, content, { automaticNewlines = false, mode } = {}) {
let current = ``;
try {
current = this.readFileSync(p, `utf8`);
} catch (error) {
}
const normalizedContent = automaticNewlines ? normalizeLineEndings(current, content) : content;
if (current === normalizedContent)
return;
this.writeFileSync(p, normalizedContent, { mode });
}
async movePromise(fromP, toP) {
try {
await this.renamePromise(fromP, toP);
} catch (error) {
if (error.code === `EXDEV`) {
await this.copyPromise(toP, fromP);
await this.removePromise(fromP);
} else {
throw error;
}
}
}
moveSync(fromP, toP) {
try {
this.renameSync(fromP, toP);
} catch (error) {
if (error.code === `EXDEV`) {
this.copySync(toP, fromP);
this.removeSync(fromP);
} else {
throw error;
}
}
}
async lockPromise(affectedPath, callback) {
const lockPath = `${affectedPath}.flock`;
const interval = 1e3 / 60;
const startTime = Date.now();
let fd = null;
const isAlive = async () => {
let pid;
try {
[pid] = await this.readJsonPromise(lockPath);
} catch (error) {
return Date.now() - startTime < 500;
}
try {
process.kill(pid, 0);
return true;
} catch (error) {
return false;
}
};
while (fd === null) {
try {
fd = await this.openPromise(lockPath, `wx`);
} catch (error) {
if (error.code === `EEXIST`) {
if (!await isAlive()) {
try {
await this.unlinkPromise(lockPath);
continue;
} catch (error2) {
}
}
if (Date.now() - startTime < 60 * 1e3) {
await new Promise((resolve) => setTimeout(resolve, interval));
} else {
throw new Error(`Couldn't acquire a lock in a reasonable time (via ${lockPath})`);
}
} else {
throw error;
}
}
}
await this.writePromise(fd, JSON.stringify([process.pid]));
try {
return await callback();
} finally {
try {
await this.closePromise(fd);
await this.unlinkPromise(lockPath);
} catch (error) {
}
}
}
async readJsonPromise(p) {
const content = await this.readFilePromise(p, `utf8`);
try {
return JSON.parse(content);
} catch (error) {
error.message += ` (in ${p})`;
throw error;
}
}
readJsonSync(p) {
const content = this.readFileSync(p, `utf8`);
try {
return JSON.parse(content);
} catch (error) {
error.message += ` (in ${p})`;
throw error;
}
}
2023-11-10 05:08:12 +01:00
async writeJsonPromise(p, data, { compact = false } = {}) {
const space = compact ? 0 : 2;
return await this.writeFilePromise(p, `${JSON.stringify(data, null, space)}
2023-07-27 15:57:32 +02:00
`);
}
2023-11-10 05:08:12 +01:00
writeJsonSync(p, data, { compact = false } = {}) {
const space = compact ? 0 : 2;
return this.writeFileSync(p, `${JSON.stringify(data, null, space)}
2023-07-27 15:57:32 +02:00
`);
}
async preserveTimePromise(p, cb) {
const stat = await this.lstatPromise(p);
const result = await cb();
if (typeof result !== `undefined`)
p = result;
2023-11-10 05:08:12 +01:00
await this.lutimesPromise(p, stat.atime, stat.mtime);
2023-07-27 15:57:32 +02:00
}
async preserveTimeSync(p, cb) {
const stat = this.lstatSync(p);
const result = cb();
if (typeof result !== `undefined`)
p = result;
2023-11-10 05:08:12 +01:00
this.lutimesSync(p, stat.atime, stat.mtime);
2023-07-27 15:57:32 +02:00
}
}
class BasePortableFakeFS extends FakeFS {
constructor() {
super(ppath);
}
}
function getEndOfLine(content) {
const matches = content.match(/\r?\n/g);
if (matches === null)
return EOL;
const crlf = matches.filter((nl) => nl === `\r
`).length;
const lf = matches.length - crlf;
return crlf > lf ? `\r
` : `
`;
}
function normalizeLineEndings(originalContent, newContent) {
return newContent.replace(/\r?\n/g, getEndOfLine(originalContent));
}
2023-11-10 05:08:12 +01:00
class ProxiedFS extends FakeFS {
getExtractHint(hints) {
return this.baseFs.getExtractHint(hints);
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
resolve(path) {
return this.mapFromBase(this.baseFs.resolve(this.mapToBase(path)));
2023-07-27 15:57:32 +02:00
}
getRealPath() {
2023-11-10 05:08:12 +01:00
return this.mapFromBase(this.baseFs.getRealPath());
2023-07-27 15:57:32 +02:00
}
async openPromise(p, flags, mode) {
2023-11-10 05:08:12 +01:00
return this.baseFs.openPromise(this.mapToBase(p), flags, mode);
2023-07-27 15:57:32 +02:00
}
openSync(p, flags, mode) {
2023-11-10 05:08:12 +01:00
return this.baseFs.openSync(this.mapToBase(p), flags, mode);
2023-07-27 15:57:32 +02:00
}
async opendirPromise(p, opts) {
2023-11-10 05:08:12 +01:00
return Object.assign(await this.baseFs.opendirPromise(this.mapToBase(p), opts), { path: p });
2023-07-27 15:57:32 +02:00
}
opendirSync(p, opts) {
2023-11-10 05:08:12 +01:00
return Object.assign(this.baseFs.opendirSync(this.mapToBase(p), opts), { path: p });
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
async readPromise(fd, buffer, offset, length, position) {
return await this.baseFs.readPromise(fd, buffer, offset, length, position);
2023-07-27 15:57:32 +02:00
}
readSync(fd, buffer, offset, length, position) {
2023-11-10 05:08:12 +01:00
return this.baseFs.readSync(fd, buffer, offset, length, position);
2023-07-27 15:57:32 +02:00
}
async writePromise(fd, buffer, offset, length, position) {
2023-11-10 05:08:12 +01:00
if (typeof buffer === `string`) {
return await this.baseFs.writePromise(fd, buffer, offset);
} else {
return await this.baseFs.writePromise(fd, buffer, offset, length, position);
}
2023-07-27 15:57:32 +02:00
}
writeSync(fd, buffer, offset, length, position) {
if (typeof buffer === `string`) {
2023-11-10 05:08:12 +01:00
return this.baseFs.writeSync(fd, buffer, offset);
2023-07-27 15:57:32 +02:00
} else {
2023-11-10 05:08:12 +01:00
return this.baseFs.writeSync(fd, buffer, offset, length, position);
2023-07-27 15:57:32 +02:00
}
}
async closePromise(fd) {
2023-11-10 05:08:12 +01:00
return this.baseFs.closePromise(fd);
2023-07-27 15:57:32 +02:00
}
closeSync(fd) {
2023-11-10 05:08:12 +01:00
this.baseFs.closeSync(fd);
2023-07-27 15:57:32 +02:00
}
createReadStream(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.createReadStream(p !== null ? this.mapToBase(p) : p, opts);
2023-07-27 15:57:32 +02:00
}
createWriteStream(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.createWriteStream(p !== null ? this.mapToBase(p) : p, opts);
2023-07-27 15:57:32 +02:00
}
async realpathPromise(p) {
2023-11-10 05:08:12 +01:00
return this.mapFromBase(await this.baseFs.realpathPromise(this.mapToBase(p)));
2023-07-27 15:57:32 +02:00
}
realpathSync(p) {
2023-11-10 05:08:12 +01:00
return this.mapFromBase(this.baseFs.realpathSync(this.mapToBase(p)));
2023-07-27 15:57:32 +02:00
}
async existsPromise(p) {
2023-11-10 05:08:12 +01:00
return this.baseFs.existsPromise(this.mapToBase(p));
}
existsSync(p) {
return this.baseFs.existsSync(this.mapToBase(p));
2023-07-27 15:57:32 +02:00
}
accessSync(p, mode) {
2023-11-10 05:08:12 +01:00
return this.baseFs.accessSync(this.mapToBase(p), mode);
2023-07-27 15:57:32 +02:00
}
async accessPromise(p, mode) {
2023-11-10 05:08:12 +01:00
return this.baseFs.accessPromise(this.mapToBase(p), mode);
2023-07-27 15:57:32 +02:00
}
async statPromise(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.statPromise(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
statSync(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.statSync(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
async fstatPromise(fd, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fstatPromise(fd, opts);
2023-07-27 15:57:32 +02:00
}
fstatSync(fd, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fstatSync(fd, opts);
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
lstatPromise(p, opts) {
return this.baseFs.lstatPromise(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
lstatSync(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.lstatSync(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
async fchmodPromise(fd, mask) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fchmodPromise(fd, mask);
2023-07-27 15:57:32 +02:00
}
fchmodSync(fd, mask) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fchmodSync(fd, mask);
2023-07-27 15:57:32 +02:00
}
async chmodPromise(p, mask) {
2023-11-10 05:08:12 +01:00
return this.baseFs.chmodPromise(this.mapToBase(p), mask);
2023-07-27 15:57:32 +02:00
}
chmodSync(p, mask) {
2023-11-10 05:08:12 +01:00
return this.baseFs.chmodSync(this.mapToBase(p), mask);
2023-07-27 15:57:32 +02:00
}
async fchownPromise(fd, uid, gid) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fchownPromise(fd, uid, gid);
2023-07-27 15:57:32 +02:00
}
fchownSync(fd, uid, gid) {
2023-11-10 05:08:12 +01:00
return this.baseFs.fchownSync(fd, uid, gid);
2023-07-27 15:57:32 +02:00
}
async chownPromise(p, uid, gid) {
2023-11-10 05:08:12 +01:00
return this.baseFs.chownPromise(this.mapToBase(p), uid, gid);
2023-07-27 15:57:32 +02:00
}
chownSync(p, uid, gid) {
2023-11-10 05:08:12 +01:00
return this.baseFs.chownSync(this.mapToBase(p), uid, gid);
2023-07-27 15:57:32 +02:00
}
async renamePromise(oldP, newP) {
2023-11-10 05:08:12 +01:00
return this.baseFs.renamePromise(this.mapToBase(oldP), this.mapToBase(newP));
2023-07-27 15:57:32 +02:00
}
renameSync(oldP, newP) {
2023-11-10 05:08:12 +01:00
return this.baseFs.renameSync(this.mapToBase(oldP), this.mapToBase(newP));
2023-07-27 15:57:32 +02:00
}
async copyFilePromise(sourceP, destP, flags = 0) {
2023-11-10 05:08:12 +01:00
return this.baseFs.copyFilePromise(this.mapToBase(sourceP), this.mapToBase(destP), flags);
2023-07-27 15:57:32 +02:00
}
copyFileSync(sourceP, destP, flags = 0) {
2023-11-10 05:08:12 +01:00
return this.baseFs.copyFileSync(this.mapToBase(sourceP), this.mapToBase(destP), flags);
2023-07-27 15:57:32 +02:00
}
async appendFilePromise(p, content, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.appendFilePromise(this.fsMapToBase(p), content, opts);
2023-07-27 15:57:32 +02:00
}
appendFileSync(p, content, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.appendFileSync(this.fsMapToBase(p), content, opts);
2023-07-27 15:57:32 +02:00
}
async writeFilePromise(p, content, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.writeFilePromise(this.fsMapToBase(p), content, opts);
2023-07-27 15:57:32 +02:00
}
writeFileSync(p, content, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.writeFileSync(this.fsMapToBase(p), content, opts);
2023-07-27 15:57:32 +02:00
}
async unlinkPromise(p) {
2023-11-10 05:08:12 +01:00
return this.baseFs.unlinkPromise(this.mapToBase(p));
2023-07-27 15:57:32 +02:00
}
unlinkSync(p) {
2023-11-10 05:08:12 +01:00
return this.baseFs.unlinkSync(this.mapToBase(p));
2023-07-27 15:57:32 +02:00
}
async utimesPromise(p, atime, mtime) {
2023-11-10 05:08:12 +01:00
return this.baseFs.utimesPromise(this.mapToBase(p), atime, mtime);
2023-07-27 15:57:32 +02:00
}
utimesSync(p, atime, mtime) {
2023-11-10 05:08:12 +01:00
return this.baseFs.utimesSync(this.mapToBase(p), atime, mtime);
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
async lutimesPromise(p, atime, mtime) {
return this.baseFs.lutimesPromise(this.mapToBase(p), atime, mtime);
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
lutimesSync(p, atime, mtime) {
return this.baseFs.lutimesSync(this.mapToBase(p), atime, mtime);
2023-07-27 15:57:32 +02:00
}
async mkdirPromise(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.mkdirPromise(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
mkdirSync(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.mkdirSync(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
async rmdirPromise(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.rmdirPromise(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
rmdirSync(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.rmdirSync(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
async linkPromise(existingP, newP) {
2023-11-10 05:08:12 +01:00
return this.baseFs.linkPromise(this.mapToBase(existingP), this.mapToBase(newP));
2023-07-27 15:57:32 +02:00
}
linkSync(existingP, newP) {
2023-11-10 05:08:12 +01:00
return this.baseFs.linkSync(this.mapToBase(existingP), this.mapToBase(newP));
2023-07-27 15:57:32 +02:00
}
async symlinkPromise(target, p, type) {
2023-11-10 05:08:12 +01:00
const mappedP = this.mapToBase(p);
if (this.pathUtils.isAbsolute(target))
return this.baseFs.symlinkPromise(this.mapToBase(target), mappedP, type);
const mappedAbsoluteTarget = this.mapToBase(this.pathUtils.join(this.pathUtils.dirname(p), target));
const mappedTarget = this.baseFs.pathUtils.relative(this.baseFs.pathUtils.dirname(mappedP), mappedAbsoluteTarget);
return this.baseFs.symlinkPromise(mappedTarget, mappedP, type);
2023-07-27 15:57:32 +02:00
}
symlinkSync(target, p, type) {
2023-11-10 05:08:12 +01:00
const mappedP = this.mapToBase(p);
if (this.pathUtils.isAbsolute(target))
return this.baseFs.symlinkSync(this.mapToBase(target), mappedP, type);
const mappedAbsoluteTarget = this.mapToBase(this.pathUtils.join(this.pathUtils.dirname(p), target));
const mappedTarget = this.baseFs.pathUtils.relative(this.baseFs.pathUtils.dirname(mappedP), mappedAbsoluteTarget);
return this.baseFs.symlinkSync(mappedTarget, mappedP, type);
2023-07-27 15:57:32 +02:00
}
async readFilePromise(p, encoding) {
2023-11-10 05:08:12 +01:00
return this.baseFs.readFilePromise(this.fsMapToBase(p), encoding);
2023-07-27 15:57:32 +02:00
}
readFileSync(p, encoding) {
2023-11-10 05:08:12 +01:00
return this.baseFs.readFileSync(this.fsMapToBase(p), encoding);
2023-07-27 15:57:32 +02:00
}
2023-11-10 05:08:12 +01:00
readdirPromise(p, opts) {
return this.baseFs.readdirPromise(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
readdirSync(p, opts) {
2023-11-10 05:08:12 +01:00
return this.baseFs.readdirSync(this.mapToBase(p), opts);
2023-07-27 15:57:32 +02:00
}
async readlinkPromise(p) {
2023-11-10 05:08:12 +01:00
return this.mapFromBase(await this.baseFs.readlinkPromise(this.mapToBase(p)));
2023-07-27 15:57:32 +02:00
}
readlinkSync(p) {
2023-11-10 05:08:12 +01:00
return this.mapFromBase(this.baseFs.readlinkSync(this.mapToBase(p)));
2023-07-27 15:57:32 +02:00
}
async truncatePromise(p, len) {
2023-11-10 05:08:12 +01:00
return this.baseFs.truncatePromise(this.mapToBase(p), len);
2023-07-27 15:57:32 +02:00
}
truncateSync(p, len) {
2023-11-10 05:08:12 +01:00
return this.baseFs.truncateSync(this.mapToBase(p), len);
2023-07-27 15:57:32 +02:00
}
async ftruncatePromise(fd, len) {
2023-11-10 05:08:12 +01:00
return this.baseFs.ftruncatePromise(fd, len);
2023-07-27 15:57:32 +02:00
}
ftruncateSync(fd, len) {
2023-11-10 05:08:12 +01:00
return this.baseFs.ftruncateSync(fd, len);
2023-07-27 15:57:32 +02:00
}
watch(p, a, b) {
2023-11-10 05:08:12 +01:00
return this.baseFs.watch(
this.mapToBase(p),
2023-07-27 15:57:32 +02:00
a,
b
);
}
watchFile(p, a, b) {
2023-11-10 05:08:12 +01:00
return this.baseFs.watchFile(