bot/.pnp.loader.mjs

2619 lines
72 KiB
JavaScript
Raw Normal View History

2024-05-29 14:52:30 +00:00
/* eslint-disable */
// @ts-nocheck
2024-06-19 10:03:24 +00:00
import fs from 'fs'
import { URL as URL$1, fileURLToPath, pathToFileURL } from 'url'
import path from 'path'
import { createHash } from 'crypto'
import { EOL } from 'os'
import moduleExports, { isBuiltin } from 'module'
import assert from 'assert'
2023-09-23 05:12:52 +00:00
2024-06-19 10:03:24 +00:00
const SAFE_TIME = 456789e3
2023-09-23 05:12:52 +00:00
const PortablePath = {
root: `/`,
dot: `.`,
2024-06-19 10:03:24 +00:00
parent: `..`,
}
const npath = Object.create(path)
const ppath = Object.create(path.posix)
npath.cwd = () => process.cwd()
ppath.cwd =
process.platform === `win32`
? () => toPortablePath(process.cwd())
: process.cwd
2023-11-23 11:30:10 +00:00
if (process.platform === `win32`) {
ppath.resolve = (...segments) => {
if (segments.length > 0 && ppath.isAbsolute(segments[0])) {
2024-06-19 10:03:24 +00:00
return path.posix.resolve(...segments)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return path.posix.resolve(ppath.cwd(), ...segments)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
}
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00: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
2023-09-23 05:12:52 +00:00
if (to.startsWith(from)) {
2024-06-19 10:03:24 +00:00
return to.slice(from.length)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
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-23 11:30:10 +00:00
function fromPortablePathWin32(p) {
2024-06-19 10:03:24 +00: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-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
function toPortablePathWin32(p) {
2024-06-19 10:03:24 +00: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-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const toPortablePath =
process.platform === `win32` ? toPortablePathWin32 : p => p
const fromPortablePath =
process.platform === `win32` ? fromPortablePathWin32 : p => p
npath.fromPortablePath = fromPortablePath
npath.toPortablePath = toPortablePath
2023-09-23 05:12:52 +00:00
function convertPath(targetPathUtils, sourcePath) {
2024-06-19 10:03:24 +00:00
return targetPathUtils === npath
? fromPortablePath(sourcePath)
: toPortablePath(sourcePath)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const defaultTime = new Date(SAFE_TIME * 1e3)
const defaultTimeMs = defaultTime.getTime()
2023-09-23 05:12:52 +00:00
async function copyPromise(destinationFs, destination, sourceFs, source, opts) {
2024-06-19 10:03:24 +00:00
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] },
)
await copyImpl(
prelayout,
postlayout,
destinationFs,
normalizedDestination,
sourceFs,
normalizedSource,
{ ...opts, didParentExist: true },
)
for (const operation of prelayout) await operation()
await Promise.all(
postlayout.map(operation => {
return operation()
}),
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
async function copyImpl(
prelayout,
postlayout,
destinationFs,
destination,
sourceFs,
source,
opts,
) {
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
2023-09-23 05:12:52 +00:00
switch (true) {
case sourceStat.isDirectory():
{
2024-06-19 10:03:24 +00:00
updated = await copyFolder(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
break
2023-09-23 05:12:52 +00:00
case sourceStat.isFile():
{
2024-06-19 10:03:24 +00:00
updated = await copyFile(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
break
2023-09-23 05:12:52 +00:00
case sourceStat.isSymbolicLink():
{
2024-06-19 10:03:24 +00:00
updated = await copySymlink(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
break
2024-05-29 14:52:30 +00:00
default: {
2024-06-19 10:03:24 +00:00
throw new Error(`Unsupported file type (${sourceStat.mode})`)
2024-05-29 14:52:30 +00:00
}
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
if (opts.linkStrategy?.type !== `HardlinkFromIndex` || !sourceStat.isFile()) {
2024-06-19 10:03:24 +00:00
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-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
return updated
2023-09-23 05:12:52 +00:00
}
async function maybeLStat(baseFs, p) {
try {
2024-06-19 10:03:24 +00:00
return await baseFs.lstatPromise(p)
2023-09-23 05:12:52 +00:00
} catch (e) {
2024-06-19 10:03:24 +00:00
return null
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
async function copyFolder(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
) {
2023-09-23 05:12:52 +00:00
if (destinationStat !== null && !destinationStat.isDirectory()) {
if (opts.overwrite) {
2024-06-19 10:03:24 +00:00
prelayout.push(async () => destinationFs.removePromise(destination))
destinationStat = null
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
let updated = false
2023-09-23 05:12:52 +00:00
if (destinationStat === null) {
prelayout.push(async () => {
try {
2024-06-19 10:03:24 +00:00
await destinationFs.mkdirPromise(destination, { mode: sourceStat.mode })
2023-09-23 05:12:52 +00:00
} catch (err) {
if (err.code !== `EEXIST`) {
2024-06-19 10:03:24 +00:00
throw err
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
})
updated = true
}
const entries = await sourceFs.readdirPromise(source)
const nextOpts =
opts.didParentExist && !destinationStat
? { ...opts, didParentExist: false }
: opts
2023-09-23 05:12:52 +00:00
if (opts.stableSort) {
for (const entry of entries.sort()) {
2024-06-19 10:03:24 +00:00
if (
await copyImpl(
prelayout,
postlayout,
destinationFs,
destinationFs.pathUtils.join(destination, entry),
sourceFs,
sourceFs.pathUtils.join(source, entry),
nextOpts,
)
) {
updated = true
2023-09-23 05:12:52 +00:00
}
}
} else {
2024-06-19 10:03:24 +00:00
const entriesUpdateStatus = await Promise.all(
entries.map(async entry => {
await copyImpl(
prelayout,
postlayout,
destinationFs,
destinationFs.pathUtils.join(destination, entry),
sourceFs,
sourceFs.pathUtils.join(source, entry),
nextOpts,
)
}),
)
if (entriesUpdateStatus.some(status => status)) {
updated = true
}
}
return updated
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
async function copyFileViaIndex(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
linkStrategy,
) {
const sourceHash = await sourceFs.checksumFilePromise(source, {
algorithm: `sha1`,
})
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`,
)
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)
2023-11-23 11:30:10 +00:00
if (destinationStat) {
2024-06-19 10:03:24 +00:00
const isDestinationHardlinkedFromIndex =
indexStat &&
destinationStat.dev === indexStat.dev &&
destinationStat.ino === indexStat.ino
const isIndexModified = indexStat?.mtimeMs !== defaultTimeMs
2023-11-23 11:30:10 +00:00
if (isDestinationHardlinkedFromIndex) {
if (isIndexModified && linkStrategy.autoRepair) {
2024-06-19 10:03:24 +00:00
atomicBehavior = 0 /* Lock */
indexStat = null
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
if (!isDestinationHardlinkedFromIndex) {
if (opts.overwrite) {
2024-06-19 10:03:24 +00:00
prelayout.push(async () => destinationFs.removePromise(destination))
destinationStat = null
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
}
}
2024-06-19 10:03:24 +00:00
const tempPath =
!indexStat && atomicBehavior === 1 /* Rename */
? `${indexPath}.${Math.floor(Math.random() * 4294967296)
.toString(16)
.padStart(8, `0`)}`
: null
let tempPathCleaned = false
2023-11-23 11:30:10 +00:00
prelayout.push(async () => {
if (!indexStat) {
if (atomicBehavior === 0 /* Lock */) {
await destinationFs.lockPromise(indexPath, async () => {
2024-06-19 10:03:24 +00:00
const content = await sourceFs.readFilePromise(source)
await destinationFs.writeFilePromise(indexPath, content)
})
2023-11-23 11:30:10 +00:00
}
if (atomicBehavior === 1 /* Rename */ && tempPath) {
2024-06-19 10:03:24 +00:00
const content = await sourceFs.readFilePromise(source)
await destinationFs.writeFilePromise(tempPath, content)
2023-11-23 11:30:10 +00:00
try {
2024-06-19 10:03:24 +00:00
await destinationFs.linkPromise(tempPath, indexPath)
2023-11-23 11:30:10 +00:00
} catch (err) {
if (err.code === `EEXIST`) {
2024-06-19 10:03:24 +00:00
tempPathCleaned = true
await destinationFs.unlinkPromise(tempPath)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw err
2023-11-23 11:30:10 +00:00
}
}
}
}
if (!destinationStat) {
2024-06-19 10:03:24 +00:00
await destinationFs.linkPromise(indexPath, destination)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-11-23 11:30:10 +00:00
postlayout.push(async () => {
2024-05-29 14:52:30 +00:00
if (!indexStat) {
2024-06-19 10:03:24 +00:00
await destinationFs.lutimesPromise(indexPath, defaultTime, defaultTime)
2024-05-29 14:52:30 +00:00
if (sourceMode !== defaultMode) {
2024-06-19 10:03:24 +00:00
await destinationFs.chmodPromise(indexPath, sourceMode)
2024-05-29 14:52:30 +00:00
}
}
2023-11-23 11:30:10 +00:00
if (tempPath && !tempPathCleaned) {
2024-06-19 10:03:24 +00:00
await destinationFs.unlinkPromise(tempPath)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
return false
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
async function copyFileDirect(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
) {
2023-09-23 05:12:52 +00:00
if (destinationStat !== null) {
if (opts.overwrite) {
2024-06-19 10:03:24 +00:00
prelayout.push(async () => destinationFs.removePromise(destination))
destinationStat = null
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
}
2023-11-23 11:30:10 +00:00
prelayout.push(async () => {
2024-06-19 10:03:24 +00:00
const content = await sourceFs.readFilePromise(source)
await destinationFs.writeFilePromise(destination, content)
})
return true
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
async function copyFile(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
) {
2023-11-23 11:30:10 +00:00
if (opts.linkStrategy?.type === `HardlinkFromIndex`) {
2024-06-19 10:03:24 +00:00
return copyFileViaIndex(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
opts.linkStrategy,
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return copyFileDirect(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
)
2023-11-23 11:30:10 +00:00
}
}
2024-06-19 10:03:24 +00:00
async function copySymlink(
prelayout,
postlayout,
destinationFs,
destination,
destinationStat,
sourceFs,
source,
sourceStat,
opts,
) {
2023-09-23 05:12:52 +00:00
if (destinationStat !== null) {
if (opts.overwrite) {
2024-06-19 10:03:24 +00:00
prelayout.push(async () => destinationFs.removePromise(destination))
destinationStat = null
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
}
prelayout.push(async () => {
2024-06-19 10:03:24 +00:00
await destinationFs.symlinkPromise(
convertPath(
destinationFs.pathUtils,
await sourceFs.readlinkPromise(source),
),
destination,
)
})
return true
2023-09-23 05:12:52 +00:00
}
class FakeFS {
constructor(pathUtils) {
2024-06-19 10:03:24 +00:00
this.pathUtils = pathUtils
2023-09-23 05:12:52 +00:00
}
async *genTraversePromise(init, { stableSort = false } = {}) {
2024-06-19 10:03:24 +00:00
const stack = [init]
2023-09-23 05:12:52 +00:00
while (stack.length > 0) {
2024-06-19 10:03:24 +00:00
const p = stack.shift()
const entry = await this.lstatPromise(p)
2023-09-23 05:12:52 +00:00
if (entry.isDirectory()) {
2024-06-19 10:03:24 +00:00
const entries = await this.readdirPromise(p)
2023-09-23 05:12:52 +00:00
if (stableSort) {
for (const entry2 of entries.sort()) {
2024-06-19 10:03:24 +00:00
stack.push(this.pathUtils.join(p, entry2))
2023-09-23 05:12:52 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
throw new Error(`Not supported`)
2023-09-23 05:12:52 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
yield p
2023-09-23 05:12:52 +00:00
}
}
}
2023-11-23 11:30:10 +00:00
async checksumFilePromise(path, { algorithm = `sha512` } = {}) {
2024-06-19 10:03:24 +00:00
const fd = await this.openPromise(path, `r`)
2023-11-23 11:30:10 +00:00
try {
2024-06-19 10:03:24 +00:00
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`)
2023-11-23 11:30:10 +00:00
} finally {
2024-06-19 10:03:24 +00:00
await this.closePromise(fd)
2023-11-23 11:30:10 +00:00
}
}
2023-09-23 05:12:52 +00:00
async removePromise(p, { recursive = true, maxRetries = 5 } = {}) {
2024-06-19 10:03:24 +00:00
let stat
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
stat = await this.lstatPromise(p)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `ENOENT`) {
2024-06-19 10:03:24 +00:00
return
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
if (stat.isDirectory()) {
if (recursive) {
2024-06-19 10:03:24 +00:00
const entries = await this.readdirPromise(p)
await Promise.all(
entries.map(entry => {
return this.removePromise(this.pathUtils.resolve(p, entry))
}),
)
2023-09-23 05:12:52 +00:00
}
for (let t = 0; t <= maxRetries; t++) {
try {
2024-06-19 10:03:24 +00:00
await this.rmdirPromise(p)
break
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code !== `EBUSY` && error.code !== `ENOTEMPTY`) {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
} else if (t < maxRetries) {
2024-06-19 10:03:24 +00:00
await new Promise(resolve => setTimeout(resolve, t * 100))
2023-09-23 05:12:52 +00:00
}
}
}
} else {
2024-06-19 10:03:24 +00:00
await this.unlinkPromise(p)
2023-09-23 05:12:52 +00:00
}
}
removeSync(p, { recursive = true } = {}) {
2024-06-19 10:03:24 +00:00
let stat
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
stat = this.lstatSync(p)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `ENOENT`) {
2024-06-19 10:03:24 +00:00
return
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
if (stat.isDirectory()) {
if (recursive)
for (const entry of this.readdirSync(p))
2024-06-19 10:03:24 +00:00
this.removeSync(this.pathUtils.resolve(p, entry))
this.rmdirSync(p)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.unlinkSync(p)
2023-09-23 05:12:52 +00:00
}
}
async mkdirpPromise(p, { chmod, utimes } = {}) {
2024-06-19 10:03:24 +00:00
p = this.resolve(p)
if (p === this.pathUtils.dirname(p)) return void 0
const parts = p.split(this.pathUtils.sep)
let createdDirectory
2023-09-23 05:12:52 +00:00
for (let u = 2; u <= parts.length; ++u) {
2024-06-19 10:03:24 +00:00
const subPath = parts.slice(0, u).join(this.pathUtils.sep)
2023-09-23 05:12:52 +00:00
if (!this.existsSync(subPath)) {
try {
2024-06-19 10:03:24 +00:00
await this.mkdirPromise(subPath)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `EEXIST`) {
2024-06-19 10:03:24 +00:00
continue
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
createdDirectory ??= subPath
if (chmod != null) await this.chmodPromise(subPath, chmod)
2023-09-23 05:12:52 +00:00
if (utimes != null) {
2024-06-19 10:03:24 +00:00
await this.utimesPromise(subPath, utimes[0], utimes[1])
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
const parentStat = await this.statPromise(
this.pathUtils.dirname(subPath),
)
await this.utimesPromise(subPath, parentStat.atime, parentStat.mtime)
2023-09-23 05:12:52 +00:00
}
}
}
2024-06-19 10:03:24 +00:00
return createdDirectory
2023-09-23 05:12:52 +00:00
}
mkdirpSync(p, { chmod, utimes } = {}) {
2024-06-19 10:03:24 +00:00
p = this.resolve(p)
if (p === this.pathUtils.dirname(p)) return void 0
const parts = p.split(this.pathUtils.sep)
let createdDirectory
2023-09-23 05:12:52 +00:00
for (let u = 2; u <= parts.length; ++u) {
2024-06-19 10:03:24 +00:00
const subPath = parts.slice(0, u).join(this.pathUtils.sep)
2023-09-23 05:12:52 +00:00
if (!this.existsSync(subPath)) {
try {
2024-06-19 10:03:24 +00:00
this.mkdirSync(subPath)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `EEXIST`) {
2024-06-19 10:03:24 +00:00
continue
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
createdDirectory ??= subPath
if (chmod != null) this.chmodSync(subPath, chmod)
2023-09-23 05:12:52 +00:00
if (utimes != null) {
2024-06-19 10:03:24 +00:00
this.utimesSync(subPath, utimes[0], utimes[1])
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
const parentStat = this.statSync(this.pathUtils.dirname(subPath))
this.utimesSync(subPath, parentStat.atime, parentStat.mtime)
2023-09-23 05:12:52 +00:00
}
}
}
2024-06-19 10:03:24 +00:00
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,
})
2023-09-23 05:12:52 +00:00
}
copySync(destination, source, { baseFs = this, overwrite = true } = {}) {
2024-06-19 10:03:24 +00:00
const stat = baseFs.lstatSync(source)
const exists = this.existsSync(destination)
2023-09-23 05:12:52 +00:00
if (stat.isDirectory()) {
2024-06-19 10:03:24 +00:00
this.mkdirpSync(destination)
const directoryListing = baseFs.readdirSync(source)
2023-09-23 05:12:52 +00:00
for (const entry of directoryListing) {
2024-06-19 10:03:24 +00:00
this.copySync(
this.pathUtils.join(destination, entry),
baseFs.pathUtils.join(source, entry),
{ baseFs, overwrite },
)
2023-09-23 05:12:52 +00:00
}
} else if (stat.isFile()) {
if (!exists || overwrite) {
2024-06-19 10:03:24 +00:00
if (exists) this.removeSync(destination)
const content = baseFs.readFileSync(source)
this.writeFileSync(destination, content)
2023-09-23 05:12:52 +00:00
}
} else if (stat.isSymbolicLink()) {
if (!exists || overwrite) {
2024-06-19 10:03:24 +00:00
if (exists) this.removeSync(destination)
const target = baseFs.readlinkSync(source)
this.symlinkSync(convertPath(this.pathUtils, target), destination)
2023-09-23 05:12:52 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
throw new Error(
`Unsupported file type (file: ${source}, mode: 0o${stat.mode.toString(8).padStart(6, `0`)})`,
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const mode = stat.mode & 511
this.chmodSync(destination, mode)
2023-09-23 05:12:52 +00:00
}
async changeFilePromise(p, content, opts = {}) {
if (Buffer.isBuffer(content)) {
2024-06-19 10:03:24 +00:00
return this.changeFileBufferPromise(p, content, opts)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.changeFileTextPromise(p, content, opts)
2023-09-23 05:12:52 +00:00
}
}
async changeFileBufferPromise(p, content, { mode } = {}) {
2024-06-19 10:03:24 +00:00
let current = Buffer.alloc(0)
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
current = await this.readFilePromise(p)
} catch (error) {}
if (Buffer.compare(current, content) === 0) return
await this.writeFilePromise(p, content, { mode })
2023-09-23 05:12:52 +00:00
}
async changeFileTextPromise(p, content, { automaticNewlines, mode } = {}) {
2024-06-19 10:03:24 +00:00
let current = ``
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
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 })
2023-09-23 05:12:52 +00:00
}
changeFileSync(p, content, opts = {}) {
if (Buffer.isBuffer(content)) {
2024-06-19 10:03:24 +00:00
return this.changeFileBufferSync(p, content, opts)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.changeFileTextSync(p, content, opts)
2023-09-23 05:12:52 +00:00
}
}
changeFileBufferSync(p, content, { mode } = {}) {
2024-06-19 10:03:24 +00:00
let current = Buffer.alloc(0)
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
current = this.readFileSync(p)
} catch (error) {}
if (Buffer.compare(current, content) === 0) return
this.writeFileSync(p, content, { mode })
2023-09-23 05:12:52 +00:00
}
changeFileTextSync(p, content, { automaticNewlines = false, mode } = {}) {
2024-06-19 10:03:24 +00:00
let current = ``
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
current = this.readFileSync(p, `utf8`)
} catch (error) {}
const normalizedContent = automaticNewlines
? normalizeLineEndings(current, content)
: content
if (current === normalizedContent) return
this.writeFileSync(p, normalizedContent, { mode })
2023-09-23 05:12:52 +00:00
}
async movePromise(fromP, toP) {
try {
2024-06-19 10:03:24 +00:00
await this.renamePromise(fromP, toP)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `EXDEV`) {
2024-06-19 10:03:24 +00:00
await this.copyPromise(toP, fromP)
await this.removePromise(fromP)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
}
moveSync(fromP, toP) {
try {
2024-06-19 10:03:24 +00:00
this.renameSync(fromP, toP)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `EXDEV`) {
2024-06-19 10:03:24 +00:00
this.copySync(toP, fromP)
this.removeSync(fromP)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
}
async lockPromise(affectedPath, callback) {
2024-06-19 10:03:24 +00:00
const lockPath = `${affectedPath}.flock`
const interval = 1e3 / 60
const startTime = Date.now()
let fd = null
2023-09-23 05:12:52 +00:00
const isAlive = async () => {
2024-06-19 10:03:24 +00:00
let pid
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
;[pid] = await this.readJsonPromise(lockPath)
2023-09-23 05:12:52 +00:00
} catch (error) {
2024-06-19 10:03:24 +00:00
return Date.now() - startTime < 500
2023-09-23 05:12:52 +00:00
}
try {
2024-06-19 10:03:24 +00:00
process.kill(pid, 0)
return true
2023-09-23 05:12:52 +00:00
} catch (error) {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
}
2023-09-23 05:12:52 +00:00
while (fd === null) {
try {
2024-06-19 10:03:24 +00:00
fd = await this.openPromise(lockPath, `wx`)
2023-09-23 05:12:52 +00:00
} catch (error) {
if (error.code === `EEXIST`) {
2024-06-19 10:03:24 +00:00
if (!(await isAlive())) {
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
await this.unlinkPromise(lockPath)
continue
} catch (error2) {}
2023-09-23 05:12:52 +00:00
}
if (Date.now() - startTime < 60 * 1e3) {
2024-06-19 10:03:24 +00:00
await new Promise(resolve => setTimeout(resolve, interval))
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
throw new Error(
`Couldn't acquire a lock in a reasonable time (via ${lockPath})`,
)
2023-09-23 05:12:52 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
throw error
2023-09-23 05:12:52 +00:00
}
}
}
2024-06-19 10:03:24 +00:00
await this.writePromise(fd, JSON.stringify([process.pid]))
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
return await callback()
2023-09-23 05:12:52 +00:00
} finally {
try {
2024-06-19 10:03:24 +00:00
await this.closePromise(fd)
await this.unlinkPromise(lockPath)
} catch (error) {}
2023-09-23 05:12:52 +00:00
}
}
async readJsonPromise(p) {
2024-06-19 10:03:24 +00:00
const content = await this.readFilePromise(p, `utf8`)
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
return JSON.parse(content)
2023-09-23 05:12:52 +00:00
} catch (error) {
2024-06-19 10:03:24 +00:00
error.message += ` (in ${p})`
throw error
2023-09-23 05:12:52 +00:00
}
}
readJsonSync(p) {
2024-06-19 10:03:24 +00:00
const content = this.readFileSync(p, `utf8`)
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
return JSON.parse(content)
2023-09-23 05:12:52 +00:00
} catch (error) {
2024-06-19 10:03:24 +00:00
error.message += ` (in ${p})`
throw error
2023-09-23 05:12:52 +00:00
}
}
2023-11-23 11:30:10 +00:00
async writeJsonPromise(p, data, { compact = false } = {}) {
2024-06-19 10:03:24 +00:00
const space = compact ? 0 : 2
return await this.writeFilePromise(
p,
`${JSON.stringify(data, null, space)}
`,
)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
writeJsonSync(p, data, { compact = false } = {}) {
2024-06-19 10:03:24 +00:00
const space = compact ? 0 : 2
return this.writeFileSync(
p,
`${JSON.stringify(data, null, space)}
`,
)
2023-09-23 05:12:52 +00:00
}
async preserveTimePromise(p, cb) {
2024-06-19 10:03:24 +00:00
const stat = await this.lstatPromise(p)
const result = await cb()
if (typeof result !== `undefined`) p = result
await this.lutimesPromise(p, stat.atime, stat.mtime)
2023-09-23 05:12:52 +00:00
}
async preserveTimeSync(p, cb) {
2024-06-19 10:03:24 +00:00
const stat = this.lstatSync(p)
const result = cb()
if (typeof result !== `undefined`) p = result
this.lutimesSync(p, stat.atime, stat.mtime)
2023-09-23 05:12:52 +00:00
}
}
class BasePortableFakeFS extends FakeFS {
constructor() {
2024-06-19 10:03:24 +00:00
super(ppath)
2023-09-23 05:12:52 +00:00
}
}
function getEndOfLine(content) {
2024-06-19 10:03:24 +00:00
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
`
: `
`
2023-09-23 05:12:52 +00:00
}
function normalizeLineEndings(originalContent, newContent) {
2024-06-19 10:03:24 +00:00
return newContent.replace(/\r?\n/g, getEndOfLine(originalContent))
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
class ProxiedFS extends FakeFS {
getExtractHint(hints) {
2024-06-19 10:03:24 +00:00
return this.baseFs.getExtractHint(hints)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
resolve(path) {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(this.baseFs.resolve(this.mapToBase(path)))
2023-09-23 05:12:52 +00:00
}
getRealPath() {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(this.baseFs.getRealPath())
2023-09-23 05:12:52 +00:00
}
async openPromise(p, flags, mode) {
2024-06-19 10:03:24 +00:00
return this.baseFs.openPromise(this.mapToBase(p), flags, mode)
2023-09-23 05:12:52 +00:00
}
openSync(p, flags, mode) {
2024-06-19 10:03:24 +00:00
return this.baseFs.openSync(this.mapToBase(p), flags, mode)
2023-09-23 05:12:52 +00:00
}
async opendirPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return Object.assign(
await this.baseFs.opendirPromise(this.mapToBase(p), opts),
{ path: p },
)
2023-09-23 05:12:52 +00:00
}
opendirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return Object.assign(this.baseFs.opendirSync(this.mapToBase(p), opts), {
path: p,
})
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
async readPromise(fd, buffer, offset, length, position) {
2024-06-19 10:03:24 +00:00
return await this.baseFs.readPromise(fd, buffer, offset, length, position)
2023-09-23 05:12:52 +00:00
}
readSync(fd, buffer, offset, length, position) {
2024-06-19 10:03:24 +00:00
return this.baseFs.readSync(fd, buffer, offset, length, position)
2023-09-23 05:12:52 +00:00
}
async writePromise(fd, buffer, offset, length, position) {
2023-11-23 11:30:10 +00:00
if (typeof buffer === `string`) {
2024-06-19 10:03:24 +00:00
return await this.baseFs.writePromise(fd, buffer, offset)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return await this.baseFs.writePromise(
fd,
buffer,
offset,
length,
position,
)
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
writeSync(fd, buffer, offset, length, position) {
if (typeof buffer === `string`) {
2024-06-19 10:03:24 +00:00
return this.baseFs.writeSync(fd, buffer, offset)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.baseFs.writeSync(fd, buffer, offset, length, position)
2023-09-23 05:12:52 +00:00
}
}
async closePromise(fd) {
2024-06-19 10:03:24 +00:00
return this.baseFs.closePromise(fd)
2023-09-23 05:12:52 +00:00
}
closeSync(fd) {
2024-06-19 10:03:24 +00:00
this.baseFs.closeSync(fd)
2023-09-23 05:12:52 +00:00
}
createReadStream(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.createReadStream(
p !== null ? this.mapToBase(p) : p,
opts,
)
2023-09-23 05:12:52 +00:00
}
createWriteStream(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.createWriteStream(
p !== null ? this.mapToBase(p) : p,
opts,
)
2023-09-23 05:12:52 +00:00
}
async realpathPromise(p) {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(
await this.baseFs.realpathPromise(this.mapToBase(p)),
)
2023-09-23 05:12:52 +00:00
}
realpathSync(p) {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(this.baseFs.realpathSync(this.mapToBase(p)))
2023-09-23 05:12:52 +00:00
}
async existsPromise(p) {
2024-06-19 10:03:24 +00:00
return this.baseFs.existsPromise(this.mapToBase(p))
2023-11-23 11:30:10 +00:00
}
existsSync(p) {
2024-06-19 10:03:24 +00:00
return this.baseFs.existsSync(this.mapToBase(p))
2023-09-23 05:12:52 +00:00
}
accessSync(p, mode) {
2024-06-19 10:03:24 +00:00
return this.baseFs.accessSync(this.mapToBase(p), mode)
2023-09-23 05:12:52 +00:00
}
async accessPromise(p, mode) {
2024-06-19 10:03:24 +00:00
return this.baseFs.accessPromise(this.mapToBase(p), mode)
2023-09-23 05:12:52 +00:00
}
async statPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.statPromise(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
statSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.statSync(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
async fstatPromise(fd, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fstatPromise(fd, opts)
2023-09-23 05:12:52 +00:00
}
fstatSync(fd, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fstatSync(fd, opts)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
lstatPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.lstatPromise(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
lstatSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.lstatSync(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
async fchmodPromise(fd, mask) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fchmodPromise(fd, mask)
2023-09-23 05:12:52 +00:00
}
fchmodSync(fd, mask) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fchmodSync(fd, mask)
2023-09-23 05:12:52 +00:00
}
async chmodPromise(p, mask) {
2024-06-19 10:03:24 +00:00
return this.baseFs.chmodPromise(this.mapToBase(p), mask)
2023-09-23 05:12:52 +00:00
}
chmodSync(p, mask) {
2024-06-19 10:03:24 +00:00
return this.baseFs.chmodSync(this.mapToBase(p), mask)
2023-09-23 05:12:52 +00:00
}
async fchownPromise(fd, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fchownPromise(fd, uid, gid)
2023-09-23 05:12:52 +00:00
}
fchownSync(fd, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.baseFs.fchownSync(fd, uid, gid)
2023-09-23 05:12:52 +00:00
}
async chownPromise(p, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.baseFs.chownPromise(this.mapToBase(p), uid, gid)
2023-09-23 05:12:52 +00:00
}
chownSync(p, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.baseFs.chownSync(this.mapToBase(p), uid, gid)
2023-09-23 05:12:52 +00:00
}
async renamePromise(oldP, newP) {
2024-06-19 10:03:24 +00:00
return this.baseFs.renamePromise(this.mapToBase(oldP), this.mapToBase(newP))
2023-09-23 05:12:52 +00:00
}
renameSync(oldP, newP) {
2024-06-19 10:03:24 +00:00
return this.baseFs.renameSync(this.mapToBase(oldP), this.mapToBase(newP))
2023-09-23 05:12:52 +00:00
}
async copyFilePromise(sourceP, destP, flags = 0) {
2024-06-19 10:03:24 +00:00
return this.baseFs.copyFilePromise(
this.mapToBase(sourceP),
this.mapToBase(destP),
flags,
)
2023-09-23 05:12:52 +00:00
}
copyFileSync(sourceP, destP, flags = 0) {
2024-06-19 10:03:24 +00:00
return this.baseFs.copyFileSync(
this.mapToBase(sourceP),
this.mapToBase(destP),
flags,
)
2023-09-23 05:12:52 +00:00
}
async appendFilePromise(p, content, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.appendFilePromise(this.fsMapToBase(p), content, opts)
2023-09-23 05:12:52 +00:00
}
appendFileSync(p, content, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.appendFileSync(this.fsMapToBase(p), content, opts)
2023-09-23 05:12:52 +00:00
}
async writeFilePromise(p, content, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.writeFilePromise(this.fsMapToBase(p), content, opts)
2023-09-23 05:12:52 +00:00
}
writeFileSync(p, content, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.writeFileSync(this.fsMapToBase(p), content, opts)
2023-09-23 05:12:52 +00:00
}
async unlinkPromise(p) {
2024-06-19 10:03:24 +00:00
return this.baseFs.unlinkPromise(this.mapToBase(p))
2023-09-23 05:12:52 +00:00
}
unlinkSync(p) {
2024-06-19 10:03:24 +00:00
return this.baseFs.unlinkSync(this.mapToBase(p))
2023-09-23 05:12:52 +00:00
}
async utimesPromise(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
return this.baseFs.utimesPromise(this.mapToBase(p), atime, mtime)
2023-09-23 05:12:52 +00:00
}
utimesSync(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
return this.baseFs.utimesSync(this.mapToBase(p), atime, mtime)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
async lutimesPromise(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
return this.baseFs.lutimesPromise(this.mapToBase(p), atime, mtime)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
lutimesSync(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
return this.baseFs.lutimesSync(this.mapToBase(p), atime, mtime)
2023-09-23 05:12:52 +00:00
}
async mkdirPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.mkdirPromise(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
mkdirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.mkdirSync(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
async rmdirPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.rmdirPromise(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
rmdirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.rmdirSync(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
2024-05-29 14:52:30 +00:00
async rmPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.rmPromise(this.mapToBase(p), opts)
2024-05-29 14:52:30 +00:00
}
rmSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.rmSync(this.mapToBase(p), opts)
2024-05-29 14:52:30 +00:00
}
2023-09-23 05:12:52 +00:00
async linkPromise(existingP, newP) {
2024-06-19 10:03:24 +00:00
return this.baseFs.linkPromise(
this.mapToBase(existingP),
this.mapToBase(newP),
)
2023-09-23 05:12:52 +00:00
}
linkSync(existingP, newP) {
2024-06-19 10:03:24 +00:00
return this.baseFs.linkSync(this.mapToBase(existingP), this.mapToBase(newP))
2023-09-23 05:12:52 +00:00
}
async symlinkPromise(target, p, type) {
2024-06-19 10:03:24 +00:00
const mappedP = this.mapToBase(p)
2023-11-23 11:30:10 +00:00
if (this.pathUtils.isAbsolute(target))
2024-06-19 10:03:24 +00:00
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-09-23 05:12:52 +00:00
}
symlinkSync(target, p, type) {
2024-06-19 10:03:24 +00:00
const mappedP = this.mapToBase(p)
2023-11-23 11:30:10 +00:00
if (this.pathUtils.isAbsolute(target))
2024-06-19 10:03:24 +00:00
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-09-23 05:12:52 +00:00
}
async readFilePromise(p, encoding) {
2024-06-19 10:03:24 +00:00
return this.baseFs.readFilePromise(this.fsMapToBase(p), encoding)
2023-09-23 05:12:52 +00:00
}
readFileSync(p, encoding) {
2024-06-19 10:03:24 +00:00
return this.baseFs.readFileSync(this.fsMapToBase(p), encoding)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
readdirPromise(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.readdirPromise(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
readdirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.baseFs.readdirSync(this.mapToBase(p), opts)
2023-09-23 05:12:52 +00:00
}
async readlinkPromise(p) {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(
await this.baseFs.readlinkPromise(this.mapToBase(p)),
)
2023-09-23 05:12:52 +00:00
}
readlinkSync(p) {
2024-06-19 10:03:24 +00:00
return this.mapFromBase(this.baseFs.readlinkSync(this.mapToBase(p)))
2023-09-23 05:12:52 +00:00
}
async truncatePromise(p, len) {
2024-06-19 10:03:24 +00:00
return this.baseFs.truncatePromise(this.mapToBase(p), len)
2023-09-23 05:12:52 +00:00
}
truncateSync(p, len) {
2024-06-19 10:03:24 +00:00
return this.baseFs.truncateSync(this.mapToBase(p), len)
2023-09-23 05:12:52 +00:00
}
async ftruncatePromise(fd, len) {
2024-06-19 10:03:24 +00:00
return this.baseFs.ftruncatePromise(fd, len)
2023-09-23 05:12:52 +00:00
}
ftruncateSync(fd, len) {
2024-06-19 10:03:24 +00:00
return this.baseFs.ftruncateSync(fd, len)
2023-09-23 05:12:52 +00:00
}
watch(p, a, b) {
2024-06-19 10:03:24 +00:00
return this.baseFs.watch(this.mapToBase(p), a, b)
2023-09-23 05:12:52 +00:00
}
watchFile(p, a, b) {
2024-06-19 10:03:24 +00:00
return this.baseFs.watchFile(this.mapToBase(p), a, b)
2023-09-23 05:12:52 +00:00
}
unwatchFile(p, cb) {
2024-06-19 10:03:24 +00:00
return this.baseFs.unwatchFile(this.mapToBase(p), cb)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
fsMapToBase(p) {
if (typeof p === `number`) {
2024-06-19 10:03:24 +00:00
return p
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.mapToBase(p)
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
}
2023-11-23 11:30:10 +00:00
function direntToPortable(dirent) {
2024-06-19 10:03:24 +00:00
const portableDirent = dirent
2023-11-23 11:30:10 +00:00
if (typeof dirent.path === `string`)
2024-06-19 10:03:24 +00:00
portableDirent.path = npath.toPortablePath(dirent.path)
return portableDirent
2023-11-23 11:30:10 +00:00
}
class NodeFS extends BasePortableFakeFS {
constructor(realFs = fs) {
2024-06-19 10:03:24 +00:00
super()
this.realFs = realFs
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
getExtractHint() {
2024-06-19 10:03:24 +00:00
return false
2023-09-23 05:12:52 +00:00
}
getRealPath() {
2024-06-19 10:03:24 +00:00
return PortablePath.root
2023-11-23 11:30:10 +00:00
}
resolve(p) {
2024-06-19 10:03:24 +00:00
return ppath.resolve(p)
2023-09-23 05:12:52 +00:00
}
async openPromise(p, flags, mode) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.open(
npath.fromPortablePath(p),
flags,
mode,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
openSync(p, flags, mode) {
2024-06-19 10:03:24 +00:00
return this.realFs.openSync(npath.fromPortablePath(p), flags, mode)
2023-09-23 05:12:52 +00:00
}
async opendirPromise(p, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (typeof opts !== `undefined`) {
2024-06-19 10:03:24 +00:00
this.realFs.opendir(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.opendir(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
}).then(dir => {
const dirWithFixedPath = dir
2023-11-23 11:30:10 +00:00
Object.defineProperty(dirWithFixedPath, `path`, {
value: p,
configurable: true,
2024-06-19 10:03:24 +00:00
writable: true,
})
return dirWithFixedPath
})
2023-09-23 05:12:52 +00:00
}
opendirSync(p, opts) {
2024-06-19 10:03:24 +00:00
const dir =
typeof opts !== `undefined`
? this.realFs.opendirSync(npath.fromPortablePath(p), opts)
: this.realFs.opendirSync(npath.fromPortablePath(p))
const dirWithFixedPath = dir
2023-11-23 11:30:10 +00:00
Object.defineProperty(dirWithFixedPath, `path`, {
value: p,
configurable: true,
2024-06-19 10:03:24 +00:00
writable: true,
})
return dirWithFixedPath
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
async readPromise(fd, buffer, offset = 0, length = 0, position = -1) {
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.read(
fd,
buffer,
offset,
length,
position,
(error, bytesRead) => {
if (error) {
reject(error)
} else {
resolve(bytesRead)
}
},
)
})
2023-09-23 05:12:52 +00:00
}
readSync(fd, buffer, offset, length, position) {
2024-06-19 10:03:24 +00:00
return this.realFs.readSync(fd, buffer, offset, length, position)
2023-09-23 05:12:52 +00:00
}
async writePromise(fd, buffer, offset, length, position) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (typeof buffer === `string`) {
2024-06-19 10:03:24 +00:00
return this.realFs.write(
fd,
buffer,
offset,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.write(
fd,
buffer,
offset,
length,
position,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
writeSync(fd, buffer, offset, length, position) {
if (typeof buffer === `string`) {
2024-06-19 10:03:24 +00:00
return this.realFs.writeSync(fd, buffer, offset)
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.writeSync(fd, buffer, offset, length, position)
2023-09-23 05:12:52 +00:00
}
}
async closePromise(fd) {
2023-11-23 11:30:10 +00:00
await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.close(fd, this.makeCallback(resolve, reject))
})
2023-09-23 05:12:52 +00:00
}
closeSync(fd) {
2024-06-19 10:03:24 +00:00
this.realFs.closeSync(fd)
2023-09-23 05:12:52 +00:00
}
createReadStream(p, opts) {
2024-06-19 10:03:24 +00:00
const realPath = p !== null ? npath.fromPortablePath(p) : p
return this.realFs.createReadStream(realPath, opts)
2023-09-23 05:12:52 +00:00
}
createWriteStream(p, opts) {
2024-06-19 10:03:24 +00:00
const realPath = p !== null ? npath.fromPortablePath(p) : p
return this.realFs.createWriteStream(realPath, opts)
2023-09-23 05:12:52 +00:00
}
async realpathPromise(p) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.realpath(
npath.fromPortablePath(p),
{},
this.makeCallback(resolve, reject),
)
}).then(path => {
return npath.toPortablePath(path)
})
2023-09-23 05:12:52 +00:00
}
realpathSync(p) {
2024-06-19 10:03:24 +00:00
return npath.toPortablePath(
this.realFs.realpathSync(npath.fromPortablePath(p), {}),
)
2023-09-23 05:12:52 +00:00
}
async existsPromise(p) {
2024-06-19 10:03:24 +00:00
return await new Promise(resolve => {
this.realFs.exists(npath.fromPortablePath(p), resolve)
})
2023-09-23 05:12:52 +00:00
}
accessSync(p, mode) {
2024-06-19 10:03:24 +00:00
return this.realFs.accessSync(npath.fromPortablePath(p), mode)
2023-09-23 05:12:52 +00:00
}
async accessPromise(p, mode) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.access(
npath.fromPortablePath(p),
mode,
this.makeCallback(resolve, reject),
)
})
2023-11-23 11:30:10 +00:00
}
existsSync(p) {
2024-06-19 10:03:24 +00:00
return this.realFs.existsSync(npath.fromPortablePath(p))
2023-09-23 05:12:52 +00:00
}
async statPromise(p, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.stat(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.stat(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
statSync(p, opts) {
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.statSync(npath.fromPortablePath(p), opts)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.statSync(npath.fromPortablePath(p))
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
async fstatPromise(fd, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.fstat(fd, opts, this.makeCallback(resolve, reject))
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.fstat(fd, this.makeCallback(resolve, reject))
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
fstatSync(fd, opts) {
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.fstatSync(fd, opts)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.fstatSync(fd)
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
async lstatPromise(p, opts) {
return await new Promise((resolve, reject) => {
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.lstat(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.lstat(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
lstatSync(p, opts) {
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.lstatSync(npath.fromPortablePath(p), opts)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.lstatSync(npath.fromPortablePath(p))
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
async fchmodPromise(fd, mask) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.fchmod(fd, mask, this.makeCallback(resolve, reject))
})
2023-09-23 05:12:52 +00:00
}
fchmodSync(fd, mask) {
2024-06-19 10:03:24 +00:00
return this.realFs.fchmodSync(fd, mask)
2023-09-23 05:12:52 +00:00
}
async chmodPromise(p, mask) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.chmod(
npath.fromPortablePath(p),
mask,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
chmodSync(p, mask) {
2024-06-19 10:03:24 +00:00
return this.realFs.chmodSync(npath.fromPortablePath(p), mask)
2023-09-23 05:12:52 +00:00
}
async fchownPromise(fd, uid, gid) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.fchown(fd, uid, gid, this.makeCallback(resolve, reject))
})
2023-09-23 05:12:52 +00:00
}
fchownSync(fd, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.realFs.fchownSync(fd, uid, gid)
2023-09-23 05:12:52 +00:00
}
async chownPromise(p, uid, gid) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.chown(
npath.fromPortablePath(p),
uid,
gid,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
chownSync(p, uid, gid) {
2024-06-19 10:03:24 +00:00
return this.realFs.chownSync(npath.fromPortablePath(p), uid, gid)
2023-09-23 05:12:52 +00:00
}
async renamePromise(oldP, newP) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.rename(
npath.fromPortablePath(oldP),
npath.fromPortablePath(newP),
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
renameSync(oldP, newP) {
2024-06-19 10:03:24 +00:00
return this.realFs.renameSync(
npath.fromPortablePath(oldP),
npath.fromPortablePath(newP),
)
2023-09-23 05:12:52 +00:00
}
async copyFilePromise(sourceP, destP, flags = 0) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.copyFile(
npath.fromPortablePath(sourceP),
npath.fromPortablePath(destP),
flags,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
copyFileSync(sourceP, destP, flags = 0) {
2024-06-19 10:03:24 +00:00
return this.realFs.copyFileSync(
npath.fromPortablePath(sourceP),
npath.fromPortablePath(destP),
flags,
)
2023-09-23 05:12:52 +00:00
}
async appendFilePromise(p, content, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.appendFile(
fsNativePath,
content,
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.appendFile(
fsNativePath,
content,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
appendFileSync(p, content, opts) {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.appendFileSync(fsNativePath, content, opts)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.appendFileSync(fsNativePath, content)
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
async writeFilePromise(p, content, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.writeFile(
fsNativePath,
content,
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.writeFile(
fsNativePath,
content,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
writeFileSync(p, content, opts) {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
2023-11-23 11:30:10 +00:00
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.writeFileSync(fsNativePath, content, opts)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.writeFileSync(fsNativePath, content)
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
async unlinkPromise(p) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.unlink(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
unlinkSync(p) {
2024-06-19 10:03:24 +00:00
return this.realFs.unlinkSync(npath.fromPortablePath(p))
2023-09-23 05:12:52 +00:00
}
async utimesPromise(p, atime, mtime) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.utimes(
npath.fromPortablePath(p),
atime,
mtime,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
utimesSync(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
this.realFs.utimesSync(npath.fromPortablePath(p), atime, mtime)
2023-11-23 11:30:10 +00:00
}
async lutimesPromise(p, atime, mtime) {
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.lutimes(
npath.fromPortablePath(p),
atime,
mtime,
this.makeCallback(resolve, reject),
)
})
2023-11-23 11:30:10 +00:00
}
lutimesSync(p, atime, mtime) {
2024-06-19 10:03:24 +00:00
this.realFs.lutimesSync(npath.fromPortablePath(p), atime, mtime)
2023-09-23 05:12:52 +00:00
}
async mkdirPromise(p, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.mkdir(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
mkdirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.mkdirSync(npath.fromPortablePath(p), opts)
2023-09-23 05:12:52 +00:00
}
async rmdirPromise(p, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.rmdir(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.rmdir(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
rmdirSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.rmdirSync(npath.fromPortablePath(p), opts)
2023-09-23 05:12:52 +00:00
}
2024-05-29 14:52:30 +00:00
async rmPromise(p, opts) {
return await new Promise((resolve, reject) => {
if (opts) {
2024-06-19 10:03:24 +00:00
this.realFs.rm(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2024-05-29 14:52:30 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.rm(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2024-05-29 14:52:30 +00:00
}
2024-06-19 10:03:24 +00:00
})
2024-05-29 14:52:30 +00:00
}
rmSync(p, opts) {
2024-06-19 10:03:24 +00:00
return this.realFs.rmSync(npath.fromPortablePath(p), opts)
2024-05-29 14:52:30 +00:00
}
2023-09-23 05:12:52 +00:00
async linkPromise(existingP, newP) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.link(
npath.fromPortablePath(existingP),
npath.fromPortablePath(newP),
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
linkSync(existingP, newP) {
2024-06-19 10:03:24 +00:00
return this.realFs.linkSync(
npath.fromPortablePath(existingP),
npath.fromPortablePath(newP),
)
2023-09-23 05:12:52 +00:00
}
async symlinkPromise(target, p, type) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.symlink(
npath.fromPortablePath(target.replace(/\/+$/, ``)),
npath.fromPortablePath(p),
type,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
symlinkSync(target, p, type) {
2024-06-19 10:03:24 +00:00
return this.realFs.symlinkSync(
npath.fromPortablePath(target.replace(/\/+$/, ``)),
npath.fromPortablePath(p),
type,
)
2023-09-23 05:12:52 +00:00
}
async readFilePromise(p, encoding) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
this.realFs.readFile(
fsNativePath,
encoding,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
readFileSync(p, encoding) {
2024-06-19 10:03:24 +00:00
const fsNativePath = typeof p === `string` ? npath.fromPortablePath(p) : p
return this.realFs.readFileSync(fsNativePath, encoding)
2023-09-23 05:12:52 +00:00
}
async readdirPromise(p, opts) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
if (opts) {
if (opts.recursive && process.platform === `win32`) {
if (opts.withFileTypes) {
2024-06-19 10:03:24 +00:00
this.realFs.readdir(
npath.fromPortablePath(p),
opts,
this.makeCallback(
results => resolve(results.map(direntToPortable)),
reject,
),
)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
this.realFs.readdir(
npath.fromPortablePath(p),
opts,
this.makeCallback(
results => resolve(results.map(npath.toPortablePath)),
reject,
),
)
2023-11-23 11:30:10 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
this.realFs.readdir(
npath.fromPortablePath(p),
opts,
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
this.realFs.readdir(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
})
2023-09-23 05:12:52 +00:00
}
readdirSync(p, opts) {
2023-11-23 11:30:10 +00:00
if (opts) {
if (opts.recursive && process.platform === `win32`) {
if (opts.withFileTypes) {
2024-06-19 10:03:24 +00:00
return this.realFs
.readdirSync(npath.fromPortablePath(p), opts)
.map(direntToPortable)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
return this.realFs
.readdirSync(npath.fromPortablePath(p), opts)
.map(npath.toPortablePath)
2023-11-23 11:30:10 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.readdirSync(npath.fromPortablePath(p), opts)
2023-11-23 11:30:10 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
return this.realFs.readdirSync(npath.fromPortablePath(p))
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
async readlinkPromise(p) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.readlink(
npath.fromPortablePath(p),
this.makeCallback(resolve, reject),
)
}).then(path => {
return npath.toPortablePath(path)
})
2023-09-23 05:12:52 +00:00
}
readlinkSync(p) {
2024-06-19 10:03:24 +00:00
return npath.toPortablePath(
this.realFs.readlinkSync(npath.fromPortablePath(p)),
)
2023-09-23 05:12:52 +00:00
}
async truncatePromise(p, len) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.truncate(
npath.fromPortablePath(p),
len,
this.makeCallback(resolve, reject),
)
})
2023-09-23 05:12:52 +00:00
}
truncateSync(p, len) {
2024-06-19 10:03:24 +00:00
return this.realFs.truncateSync(npath.fromPortablePath(p), len)
2023-09-23 05:12:52 +00:00
}
async ftruncatePromise(fd, len) {
2023-11-23 11:30:10 +00:00
return await new Promise((resolve, reject) => {
2024-06-19 10:03:24 +00:00
this.realFs.ftruncate(fd, len, this.makeCallback(resolve, reject))
})
2023-09-23 05:12:52 +00:00
}
ftruncateSync(fd, len) {
2024-06-19 10:03:24 +00:00
return this.realFs.ftruncateSync(fd, len)
2023-09-23 05:12:52 +00:00
}
watch(p, a, b) {
2024-06-19 10:03:24 +00:00
return this.realFs.watch(npath.fromPortablePath(p), a, b)
2023-09-23 05:12:52 +00:00
}
watchFile(p, a, b) {
2024-06-19 10:03:24 +00:00
return this.realFs.watchFile(npath.fromPortablePath(p), a, b)
2023-09-23 05:12:52 +00:00
}
unwatchFile(p, cb) {
2024-06-19 10:03:24 +00:00
return this.realFs.unwatchFile(npath.fromPortablePath(p), cb)
2023-09-23 05:12:52 +00:00
}
2023-11-23 11:30:10 +00:00
makeCallback(resolve, reject) {
return (err, result) => {
if (err) {
2024-06-19 10:03:24 +00:00
reject(err)
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
resolve(result)
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
}
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
const NUMBER_REGEXP = /^[0-9]+$/
const VIRTUAL_REGEXP =
/^(\/(?:[^/]+\/)*?(?:\$\$virtual|__virtual__))((?:\/((?:[^/]+-)?[a-f0-9]+)(?:\/([^/]+))?)?((?:\/.*)?))$/
const VALID_COMPONENT = /^([^/]+-)?[a-f0-9]+$/
2023-09-23 05:12:52 +00:00
class VirtualFS extends ProxiedFS {
constructor({ baseFs = new NodeFS() } = {}) {
2024-06-19 10:03:24 +00:00
super(ppath)
this.baseFs = baseFs
2023-09-23 05:12:52 +00:00
}
static makeVirtualPath(base, component, to) {
if (ppath.basename(base) !== `__virtual__`)
2024-06-19 10:03:24 +00:00
throw new Error(
`Assertion failed: Virtual folders must be named "__virtual__"`,
)
2023-09-23 05:12:52 +00:00
if (!ppath.basename(component).match(VALID_COMPONENT))
2024-06-19 10:03:24 +00:00
throw new Error(
`Assertion failed: Virtual components must be ended by an hexadecimal hash`,
)
const target = ppath.relative(ppath.dirname(base), to)
const segments = target.split(`/`)
let depth = 0
while (depth < segments.length && segments[depth] === `..`) depth += 1
const finalSegments = segments.slice(depth)
const fullVirtualPath = ppath.join(
base,
component,
String(depth),
...finalSegments,
)
return fullVirtualPath
2023-09-23 05:12:52 +00:00
}
static resolveVirtual(p) {
2024-06-19 10:03:24 +00:00
const match = p.match(VIRTUAL_REGEXP)
if (!match || (!match[3] && match[5])) return p
const target = ppath.dirname(match[1])
if (!match[3] || !match[4]) return target
const isnum = NUMBER_REGEXP.test(match[4])
if (!isnum) return p
const depth = Number(match[4])
const backstep = `../`.repeat(depth)
const subpath = match[5] || `.`
return VirtualFS.resolveVirtual(ppath.join(target, backstep, subpath))
2023-09-23 05:12:52 +00:00
}
getExtractHint(hints) {
2024-06-19 10:03:24 +00:00
return this.baseFs.getExtractHint(hints)
2023-09-23 05:12:52 +00:00
}
getRealPath() {
2024-06-19 10:03:24 +00:00
return this.baseFs.getRealPath()
2023-09-23 05:12:52 +00:00
}
realpathSync(p) {
2024-06-19 10:03:24 +00:00
const match = p.match(VIRTUAL_REGEXP)
if (!match) return this.baseFs.realpathSync(p)
if (!match[5]) return p
const realpath = this.baseFs.realpathSync(this.mapToBase(p))
return VirtualFS.makeVirtualPath(match[1], match[3], realpath)
2023-09-23 05:12:52 +00:00
}
async realpathPromise(p) {
2024-06-19 10:03:24 +00:00
const match = p.match(VIRTUAL_REGEXP)
if (!match) return await this.baseFs.realpathPromise(p)
if (!match[5]) return p
const realpath = await this.baseFs.realpathPromise(this.mapToBase(p))
return VirtualFS.makeVirtualPath(match[1], match[3], realpath)
2023-09-23 05:12:52 +00:00
}
mapToBase(p) {
2024-06-19 10:03:24 +00:00
if (p === ``) return p
if (this.pathUtils.isAbsolute(p)) return VirtualFS.resolveVirtual(p)
const resolvedRoot = VirtualFS.resolveVirtual(
this.baseFs.resolve(PortablePath.dot),
)
const resolvedP = VirtualFS.resolveVirtual(this.baseFs.resolve(p))
return ppath.relative(resolvedRoot, resolvedP) || PortablePath.dot
2023-09-23 05:12:52 +00:00
}
mapFromBase(p) {
2024-06-19 10:03:24 +00:00
return p
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
const URL =
Number(process.versions.node.split('.', 1)[0]) < 20 ? URL$1 : globalThis.URL
2024-05-29 14:52:30 +00:00
2024-06-19 10:03:24 +00:00
const [major, minor] = process.versions.node
.split(`.`)
.map(value => parseInt(value, 10))
const WATCH_MODE_MESSAGE_USES_ARRAYS =
major > 19 || (major === 19 && minor >= 2) || (major === 18 && minor >= 13)
const HAS_LAZY_LOADED_TRANSLATORS =
(major === 20 && minor < 6) || (major === 19 && minor >= 3)
const SUPPORTS_IMPORT_ATTRIBUTES =
major >= 21 || (major === 20 && minor >= 10) || (major === 18 && minor >= 20)
const SUPPORTS_IMPORT_ATTRIBUTES_ONLY = major >= 22
2023-09-23 05:12:52 +00:00
function readPackageScope(checkPath) {
2024-06-19 10:03:24 +00:00
const rootSeparatorIndex = checkPath.indexOf(npath.sep)
let separatorIndex
2023-09-23 05:12:52 +00:00
do {
2024-06-19 10:03:24 +00:00
separatorIndex = checkPath.lastIndexOf(npath.sep)
checkPath = checkPath.slice(0, separatorIndex)
if (checkPath.endsWith(`${npath.sep}node_modules`)) return false
const pjson = readPackage(checkPath + npath.sep)
2023-09-23 05:12:52 +00:00
if (pjson) {
return {
data: pjson,
2024-06-19 10:03:24 +00:00
path: checkPath,
}
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
} while (separatorIndex > rootSeparatorIndex)
return false
2023-09-23 05:12:52 +00:00
}
function readPackage(requestPath) {
2024-06-19 10:03:24 +00:00
const jsonPath = npath.resolve(requestPath, `package.json`)
if (!fs.existsSync(jsonPath)) return null
return JSON.parse(fs.readFileSync(jsonPath, `utf8`))
2023-09-23 05:12:52 +00:00
}
async function tryReadFile$1(path2) {
try {
2024-06-19 10:03:24 +00:00
return await fs.promises.readFile(path2, `utf8`)
2023-09-23 05:12:52 +00:00
} catch (error) {
2024-06-19 10:03:24 +00:00
if (error.code === `ENOENT`) return null
throw error
2023-09-23 05:12:52 +00:00
}
}
function tryParseURL(str, base) {
try {
2024-06-19 10:03:24 +00:00
return new URL(str, base)
2023-09-23 05:12:52 +00:00
} catch {
2024-06-19 10:03:24 +00:00
return null
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
let entrypointPath = null
2023-09-23 05:12:52 +00:00
function setEntrypointPath(file) {
2024-06-19 10:03:24 +00:00
entrypointPath = file
2023-09-23 05:12:52 +00:00
}
function getFileFormat(filepath) {
2024-06-19 10:03:24 +00:00
const ext = path.extname(filepath)
2023-09-23 05:12:52 +00:00
switch (ext) {
case `.mjs`: {
2024-06-19 10:03:24 +00:00
return `module`
2023-09-23 05:12:52 +00:00
}
case `.cjs`: {
2024-06-19 10:03:24 +00:00
return `commonjs`
2023-09-23 05:12:52 +00:00
}
case `.wasm`: {
2024-06-19 10:03:24 +00:00
throw new Error(`Unknown file extension ".wasm" for ${filepath}`)
2023-09-23 05:12:52 +00:00
}
case `.json`: {
2024-06-19 10:03:24 +00:00
return `json`
2023-09-23 05:12:52 +00:00
}
case `.js`: {
2024-06-19 10:03:24 +00:00
const pkg = readPackageScope(filepath)
if (!pkg) return `commonjs`
return pkg.data.type ?? `commonjs`
2023-09-23 05:12:52 +00:00
}
default: {
2024-06-19 10:03:24 +00:00
if (entrypointPath !== filepath) return null
const pkg = readPackageScope(filepath)
if (!pkg) return `commonjs`
if (pkg.data.type === `module`) return null
return pkg.data.type ?? `commonjs`
2023-09-23 05:12:52 +00:00
}
}
}
async function load$1(urlString, context, nextLoad) {
2024-06-19 10:03:24 +00:00
const url = tryParseURL(urlString)
if (url?.protocol !== `file:`) return nextLoad(urlString, context, nextLoad)
const filePath = fileURLToPath(url)
const format = getFileFormat(filePath)
if (!format) return nextLoad(urlString, context, nextLoad)
2024-05-29 14:52:30 +00:00
if (format === `json`) {
if (SUPPORTS_IMPORT_ATTRIBUTES_ONLY) {
if (context.importAttributes?.type !== `json`) {
2024-06-19 10:03:24 +00:00
const err = new TypeError(
`[ERR_IMPORT_ATTRIBUTE_MISSING]: Module "${urlString}" needs an import attribute of "type: json"`,
)
err.code = `ERR_IMPORT_ATTRIBUTE_MISSING`
throw err
2024-05-29 14:52:30 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
const type =
`importAttributes` in context
? context.importAttributes?.type
: context.importAssertions?.type
2024-05-29 14:52:30 +00:00
if (type !== `json`) {
2024-06-19 10:03:24 +00:00
const err = new TypeError(
`[ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "${urlString}" needs an import ${SUPPORTS_IMPORT_ATTRIBUTES ? `attribute` : `assertion`} of type "json"`,
)
err.code = `ERR_IMPORT_ASSERTION_TYPE_MISSING`
throw err
2024-05-29 14:52:30 +00:00
}
}
2023-09-23 05:12:52 +00:00
}
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
const pathToSend = pathToFileURL(
npath.fromPortablePath(
2024-06-19 10:03:24 +00:00
VirtualFS.resolveVirtual(npath.toPortablePath(filePath)),
),
).href
2023-09-23 05:12:52 +00:00
process.send({
2024-06-19 10:03:24 +00:00
'watch:import': WATCH_MODE_MESSAGE_USES_ARRAYS
? [pathToSend]
: pathToSend,
})
2023-09-23 05:12:52 +00:00
}
return {
format,
2024-06-19 10:03:24 +00:00
source:
format === `commonjs`
? void 0
: await fs.promises.readFile(filePath, `utf8`),
shortCircuit: true,
}
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const ArrayIsArray = Array.isArray
const JSONStringify = JSON.stringify
const ObjectGetOwnPropertyNames = Object.getOwnPropertyNames
const ObjectPrototypeHasOwnProperty = (obj, prop) =>
Object.prototype.hasOwnProperty.call(obj, prop)
const RegExpPrototypeExec = (obj, string) =>
RegExp.prototype.exec.call(obj, string)
const RegExpPrototypeSymbolReplace = (obj, ...rest) =>
RegExp.prototype[Symbol.replace].apply(obj, rest)
const StringPrototypeEndsWith = (str, ...rest) =>
String.prototype.endsWith.apply(str, rest)
const StringPrototypeIncludes = (str, ...rest) =>
String.prototype.includes.apply(str, rest)
const StringPrototypeLastIndexOf = (str, ...rest) =>
String.prototype.lastIndexOf.apply(str, rest)
const StringPrototypeIndexOf = (str, ...rest) =>
String.prototype.indexOf.apply(str, rest)
const StringPrototypeReplace = (str, ...rest) =>
String.prototype.replace.apply(str, rest)
const StringPrototypeSlice = (str, ...rest) =>
String.prototype.slice.apply(str, rest)
const StringPrototypeStartsWith = (str, ...rest) =>
String.prototype.startsWith.apply(str, rest)
const SafeMap = Map
const JSONParse = JSON.parse
2023-09-23 05:12:52 +00:00
function createErrorType(code, messageCreator, errorType) {
return class extends errorType {
constructor(...args) {
2024-06-19 10:03:24 +00:00
super(messageCreator(...args))
this.code = code
this.name = `${errorType.name} [${code}]`
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
}
2023-09-23 05:12:52 +00:00
}
const ERR_PACKAGE_IMPORT_NOT_DEFINED = createErrorType(
`ERR_PACKAGE_IMPORT_NOT_DEFINED`,
(specifier, packagePath, base) => {
2024-06-19 10:03:24 +00:00
return `Package import specifier "${specifier}" is not defined${packagePath ? ` in package ${packagePath}package.json` : ``} imported from ${base}`
2023-09-23 05:12:52 +00:00
},
2024-06-19 10:03:24 +00:00
TypeError,
)
2023-09-23 05:12:52 +00:00
const ERR_INVALID_MODULE_SPECIFIER = createErrorType(
`ERR_INVALID_MODULE_SPECIFIER`,
(request, reason, base = void 0) => {
2024-06-19 10:03:24 +00:00
return `Invalid module "${request}" ${reason}${base ? ` imported from ${base}` : ``}`
2023-09-23 05:12:52 +00:00
},
2024-06-19 10:03:24 +00:00
TypeError,
)
2023-09-23 05:12:52 +00:00
const ERR_INVALID_PACKAGE_TARGET = createErrorType(
`ERR_INVALID_PACKAGE_TARGET`,
(pkgPath, key, target, isImport = false, base = void 0) => {
2024-06-19 10:03:24 +00:00
const relError =
typeof target === `string` &&
!isImport &&
target.length &&
!StringPrototypeStartsWith(target, `./`)
2023-09-23 05:12:52 +00:00
if (key === `.`) {
2024-06-19 10:03:24 +00:00
assert(isImport === false)
return `Invalid "exports" main target ${JSONStringify(target)} defined in the package config ${pkgPath}package.json${base ? ` imported from ${base}` : ``}${relError ? `; targets must start with "./"` : ``}`
2023-09-23 05:12:52 +00:00
}
return `Invalid "${isImport ? `imports` : `exports`}" target ${JSONStringify(
2024-06-19 10:03:24 +00:00
target,
)} defined for '${key}' in the package config ${pkgPath}package.json${base ? ` imported from ${base}` : ``}${relError ? `; targets must start with "./"` : ``}`
2023-09-23 05:12:52 +00:00
},
2024-06-19 10:03:24 +00:00
Error,
)
2023-09-23 05:12:52 +00:00
const ERR_INVALID_PACKAGE_CONFIG = createErrorType(
`ERR_INVALID_PACKAGE_CONFIG`,
(path, base, message) => {
2024-06-19 10:03:24 +00:00
return `Invalid package config ${path}${base ? ` while importing ${base}` : ``}${message ? `. ${message}` : ``}`
2023-09-23 05:12:52 +00:00
},
2024-06-19 10:03:24 +00:00
Error,
)
2023-09-23 05:12:52 +00:00
function filterOwnProperties(source, keys) {
2024-06-19 10:03:24 +00:00
const filtered = /* @__PURE__ */ Object.create(null)
2023-09-23 05:12:52 +00:00
for (let i = 0; i < keys.length; i++) {
2024-06-19 10:03:24 +00:00
const key = keys[i]
2023-09-23 05:12:52 +00:00
if (ObjectPrototypeHasOwnProperty(source, key)) {
2024-06-19 10:03:24 +00:00
filtered[key] = source[key]
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
return filtered
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const packageJSONCache = new SafeMap()
2023-09-23 05:12:52 +00:00
function getPackageConfig(path, specifier, base, readFileSyncFn) {
2024-06-19 10:03:24 +00:00
const existing = packageJSONCache.get(path)
2023-09-23 05:12:52 +00:00
if (existing !== void 0) {
2024-06-19 10:03:24 +00:00
return existing
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const source = readFileSyncFn(path)
2023-09-23 05:12:52 +00:00
if (source === void 0) {
const packageConfig2 = {
pjsonPath: path,
exists: false,
main: void 0,
name: void 0,
2024-06-19 10:03:24 +00:00
type: 'none',
2023-09-23 05:12:52 +00:00
exports: void 0,
2024-06-19 10:03:24 +00:00
imports: void 0,
}
packageJSONCache.set(path, packageConfig2)
return packageConfig2
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
let packageJSON
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
packageJSON = JSONParse(source)
2023-09-23 05:12:52 +00:00
} catch (error) {
throw new ERR_INVALID_PACKAGE_CONFIG(
path,
2024-06-19 10:03:24 +00:00
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
error.message,
)
2023-09-23 05:12:52 +00:00
}
let { imports, main, name, type } = filterOwnProperties(packageJSON, [
2024-06-19 10:03:24 +00:00
'imports',
'main',
'name',
'type',
])
const exports = ObjectPrototypeHasOwnProperty(packageJSON, 'exports')
? packageJSON.exports
: void 0
if (typeof imports !== 'object' || imports === null) {
imports = void 0
}
if (typeof main !== 'string') {
main = void 0
}
if (typeof name !== 'string') {
name = void 0
}
if (type !== 'module' && type !== 'commonjs') {
type = 'none'
2023-09-23 05:12:52 +00:00
}
const packageConfig = {
pjsonPath: path,
exists: true,
main,
name,
type,
exports,
2024-06-19 10:03:24 +00:00
imports,
}
packageJSONCache.set(path, packageConfig)
return packageConfig
2023-09-23 05:12:52 +00:00
}
function getPackageScopeConfig(resolved, readFileSyncFn) {
2024-06-19 10:03:24 +00:00
let packageJSONUrl = new URL('./package.json', resolved)
2023-09-23 05:12:52 +00:00
while (true) {
2024-06-19 10:03:24 +00:00
const packageJSONPath2 = packageJSONUrl.pathname
if (
StringPrototypeEndsWith(packageJSONPath2, 'node_modules/package.json')
) {
break
2023-09-23 05:12:52 +00:00
}
const packageConfig2 = getPackageConfig(
fileURLToPath(packageJSONUrl),
resolved,
void 0,
2024-06-19 10:03:24 +00:00
readFileSyncFn,
)
2023-09-23 05:12:52 +00:00
if (packageConfig2.exists) {
2024-06-19 10:03:24 +00:00
return packageConfig2
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const lastPackageJSONUrl = packageJSONUrl
packageJSONUrl = new URL('../package.json', packageJSONUrl)
2023-09-23 05:12:52 +00:00
if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) {
2024-06-19 10:03:24 +00:00
break
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
const packageJSONPath = fileURLToPath(packageJSONUrl)
2023-09-23 05:12:52 +00:00
const packageConfig = {
pjsonPath: packageJSONPath,
exists: false,
main: void 0,
name: void 0,
2024-06-19 10:03:24 +00:00
type: 'none',
2023-09-23 05:12:52 +00:00
exports: void 0,
2024-06-19 10:03:24 +00:00
imports: void 0,
}
packageJSONCache.set(packageJSONPath, packageConfig)
return packageConfig
2023-09-23 05:12:52 +00:00
}
function throwImportNotDefined(specifier, packageJSONUrl, base) {
throw new ERR_PACKAGE_IMPORT_NOT_DEFINED(
specifier,
2024-06-19 10:03:24 +00:00
packageJSONUrl && fileURLToPath(new URL('.', packageJSONUrl)),
fileURLToPath(base),
)
2023-09-23 05:12:52 +00:00
}
function throwInvalidSubpath(subpath, packageJSONUrl, internal, base) {
2024-06-19 10:03:24 +00:00
const reason = `request is not a valid subpath for the "${internal ? 'imports' : 'exports'}" resolution of ${fileURLToPath(packageJSONUrl)}`
2023-09-23 05:12:52 +00:00
throw new ERR_INVALID_MODULE_SPECIFIER(
subpath,
reason,
2024-06-19 10:03:24 +00:00
base && fileURLToPath(base),
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
function throwInvalidPackageTarget(
subpath,
target,
packageJSONUrl,
internal,
base,
) {
if (typeof target === 'object' && target !== null) {
target = JSONStringify(target, null, '')
2023-09-23 05:12:52 +00:00
} else {
2024-06-19 10:03:24 +00:00
target = `${target}`
2023-09-23 05:12:52 +00:00
}
throw new ERR_INVALID_PACKAGE_TARGET(
2024-06-19 10:03:24 +00:00
fileURLToPath(new URL('.', packageJSONUrl)),
2023-09-23 05:12:52 +00:00
subpath,
target,
internal,
2024-06-19 10:03:24 +00:00
base && fileURLToPath(base),
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const invalidSegmentRegEx =
/(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i
const patternRegEx = /\*/g
function resolvePackageTargetString(
target,
subpath,
match,
packageJSONUrl,
base,
pattern,
internal,
conditions,
) {
if (subpath !== '' && !pattern && target[target.length - 1] !== '/')
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base)
if (!StringPrototypeStartsWith(target, './')) {
if (
internal &&
!StringPrototypeStartsWith(target, '../') &&
!StringPrototypeStartsWith(target, '/')
) {
let isURL = false
2023-09-23 05:12:52 +00:00
try {
2024-06-19 10:03:24 +00:00
new URL(target)
isURL = true
} catch {}
2023-09-23 05:12:52 +00:00
if (!isURL) {
2024-06-19 10:03:24 +00:00
const exportTarget = pattern
? RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath)
: target + subpath
return exportTarget
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base)
}
if (
RegExpPrototypeExec(
invalidSegmentRegEx,
StringPrototypeSlice(target, 2),
) !== null
)
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base)
const resolved = new URL(target, packageJSONUrl)
const resolvedPath = resolved.pathname
const packagePath = new URL('.', packageJSONUrl).pathname
2023-09-23 05:12:52 +00:00
if (!StringPrototypeStartsWith(resolvedPath, packagePath))
2024-06-19 10:03:24 +00:00
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base)
if (subpath === '') return resolved
2023-09-23 05:12:52 +00:00
if (RegExpPrototypeExec(invalidSegmentRegEx, subpath) !== null) {
2024-06-19 10:03:24 +00:00
const request = pattern
? StringPrototypeReplace(match, '*', () => subpath)
: match + subpath
throwInvalidSubpath(request, packageJSONUrl, internal, base)
2023-09-23 05:12:52 +00:00
}
if (pattern) {
return new URL(
2024-06-19 10:03:24 +00:00
RegExpPrototypeSymbolReplace(patternRegEx, resolved.href, () => subpath),
)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
return new URL(subpath, resolved)
2023-09-23 05:12:52 +00:00
}
function isArrayIndex(key) {
2024-06-19 10:03:24 +00:00
const keyNum = +key
if (`${keyNum}` !== key) return false
return keyNum >= 0 && keyNum < 4294967295
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
function resolvePackageTarget(
packageJSONUrl,
target,
subpath,
packageSubpath,
base,
pattern,
internal,
conditions,
) {
if (typeof target === 'string') {
2023-09-23 05:12:52 +00:00
return resolvePackageTargetString(
target,
subpath,
packageSubpath,
packageJSONUrl,
base,
pattern,
2024-06-19 10:03:24 +00:00
internal,
)
2023-09-23 05:12:52 +00:00
} else if (ArrayIsArray(target)) {
if (target.length === 0) {
2024-06-19 10:03:24 +00:00
return null
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
let lastException
2023-09-23 05:12:52 +00:00
for (let i = 0; i < target.length; i++) {
2024-06-19 10:03:24 +00:00
const targetItem = target[i]
let resolveResult
2023-09-23 05:12:52 +00:00
try {
resolveResult = resolvePackageTarget(
packageJSONUrl,
targetItem,
subpath,
packageSubpath,
base,
pattern,
internal,
2024-06-19 10:03:24 +00:00
conditions,
)
2023-09-23 05:12:52 +00:00
} catch (e) {
2024-06-19 10:03:24 +00:00
lastException = e
if (e.code === 'ERR_INVALID_PACKAGE_TARGET') {
continue
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
throw e
2023-09-23 05:12:52 +00:00
}
if (resolveResult === void 0) {
2024-06-19 10:03:24 +00:00
continue
2023-09-23 05:12:52 +00:00
}
if (resolveResult === null) {
2024-06-19 10:03:24 +00:00
lastException = null
continue
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
return resolveResult
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
if (lastException === void 0 || lastException === null) return lastException
throw lastException
} else if (typeof target === 'object' && target !== null) {
const keys = ObjectGetOwnPropertyNames(target)
2023-09-23 05:12:52 +00:00
for (let i = 0; i < keys.length; i++) {
2024-06-19 10:03:24 +00:00
const key = keys[i]
2023-09-23 05:12:52 +00:00
if (isArrayIndex(key)) {
throw new ERR_INVALID_PACKAGE_CONFIG(
fileURLToPath(packageJSONUrl),
base,
2024-06-19 10:03:24 +00:00
'"exports" cannot contain numeric property keys.',
)
2023-09-23 05:12:52 +00:00
}
}
for (let i = 0; i < keys.length; i++) {
2024-06-19 10:03:24 +00:00
const key = keys[i]
if (key === 'default' || conditions.has(key)) {
const conditionalTarget = target[key]
2023-09-23 05:12:52 +00:00
const resolveResult = resolvePackageTarget(
packageJSONUrl,
conditionalTarget,
subpath,
packageSubpath,
base,
pattern,
internal,
2024-06-19 10:03:24 +00:00
conditions,
)
if (resolveResult === void 0) continue
return resolveResult
2023-09-23 05:12:52 +00:00
}
}
2024-06-19 10:03:24 +00:00
return void 0
2023-09-23 05:12:52 +00:00
} else if (target === null) {
2024-06-19 10:03:24 +00:00
return null
2023-09-23 05:12:52 +00:00
}
throwInvalidPackageTarget(
packageSubpath,
target,
packageJSONUrl,
internal,
2024-06-19 10:03:24 +00:00
base,
)
2023-09-23 05:12:52 +00:00
}
function patternKeyCompare(a, b) {
2024-06-19 10:03:24 +00:00
const aPatternIndex = StringPrototypeIndexOf(a, '*')
const bPatternIndex = StringPrototypeIndexOf(b, '*')
const baseLenA = aPatternIndex === -1 ? a.length : aPatternIndex + 1
const baseLenB = bPatternIndex === -1 ? b.length : bPatternIndex + 1
if (baseLenA > baseLenB) return -1
if (baseLenB > baseLenA) return 1
if (aPatternIndex === -1) return 1
if (bPatternIndex === -1) return -1
if (a.length > b.length) return -1
if (b.length > a.length) return 1
return 0
2023-09-23 05:12:52 +00:00
}
function packageImportsResolve({ name, base, conditions, readFileSyncFn }) {
2024-06-19 10:03:24 +00:00
if (
name === '#' ||
StringPrototypeStartsWith(name, '#/') ||
StringPrototypeEndsWith(name, '/')
) {
const reason = 'is not a valid internal imports specifier name'
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base))
}
let packageJSONUrl
const packageConfig = getPackageScopeConfig(base, readFileSyncFn)
2023-09-23 05:12:52 +00:00
if (packageConfig.exists) {
2024-06-19 10:03:24 +00:00
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath)
const imports = packageConfig.imports
2023-09-23 05:12:52 +00:00
if (imports) {
2024-06-19 10:03:24 +00:00
if (
ObjectPrototypeHasOwnProperty(imports, name) &&
!StringPrototypeIncludes(name, '*')
) {
2023-09-23 05:12:52 +00:00
const resolveResult = resolvePackageTarget(
packageJSONUrl,
imports[name],
2024-06-19 10:03:24 +00:00
'',
2023-09-23 05:12:52 +00:00
name,
base,
false,
true,
2024-06-19 10:03:24 +00:00
conditions,
)
2023-09-23 05:12:52 +00:00
if (resolveResult != null) {
2024-06-19 10:03:24 +00:00
return resolveResult
2023-09-23 05:12:52 +00:00
}
} else {
2024-06-19 10:03:24 +00:00
let bestMatch = ''
let bestMatchSubpath
const keys = ObjectGetOwnPropertyNames(imports)
2023-09-23 05:12:52 +00:00
for (let i = 0; i < keys.length; i++) {
2024-06-19 10:03:24 +00:00
const key = keys[i]
const patternIndex = StringPrototypeIndexOf(key, '*')
if (
patternIndex !== -1 &&
StringPrototypeStartsWith(
name,
StringPrototypeSlice(key, 0, patternIndex),
)
) {
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1)
if (
name.length >= key.length &&
StringPrototypeEndsWith(name, patternTrailer) &&
patternKeyCompare(bestMatch, key) === 1 &&
StringPrototypeLastIndexOf(key, '*') === patternIndex
) {
bestMatch = key
2023-09-23 05:12:52 +00:00
bestMatchSubpath = StringPrototypeSlice(
name,
patternIndex,
2024-06-19 10:03:24 +00:00
name.length - patternTrailer.length,
)
2023-09-23 05:12:52 +00:00
}
}
}
if (bestMatch) {
2024-06-19 10:03:24 +00:00
const target = imports[bestMatch]
2023-09-23 05:12:52 +00:00
const resolveResult = resolvePackageTarget(
packageJSONUrl,
target,
bestMatchSubpath,
bestMatch,
base,
true,
true,
2024-06-19 10:03:24 +00:00
conditions,
)
2023-09-23 05:12:52 +00:00
if (resolveResult != null) {
2024-06-19 10:03:24 +00:00
return resolveResult
2023-09-23 05:12:52 +00:00
}
}
}
}
}
2024-06-19 10:03:24 +00:00
throwImportNotDefined(name, packageJSONUrl, base)
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const pathRegExp =
/^(?![a-zA-Z]:[\\/]|\\\\|\.{0,2}(?:\/|$))((?:node:)?(?:@[^/]+\/)?[^/]+)\/*(.*|)$/
const isRelativeRegexp = /^\.{0,2}\//
2023-09-23 05:12:52 +00:00
function tryReadFile(filePath) {
try {
2024-06-19 10:03:24 +00:00
return fs.readFileSync(filePath, `utf8`)
2023-09-23 05:12:52 +00:00
} catch (err) {
2024-06-19 10:03:24 +00:00
if (err.code === `ENOENT`) return void 0
throw err
2023-09-23 05:12:52 +00:00
}
}
async function resolvePrivateRequest(specifier, issuer, context, nextResolve) {
const resolved = packageImportsResolve({
name: specifier,
base: pathToFileURL(issuer),
conditions: new Set(context.conditions),
2024-06-19 10:03:24 +00:00
readFileSyncFn: tryReadFile,
})
2023-11-23 11:30:10 +00:00
if (resolved instanceof URL) {
2024-06-19 10:03:24 +00:00
return { url: resolved.href, shortCircuit: true }
2023-09-23 05:12:52 +00:00
} else {
if (resolved.startsWith(`#`))
2024-06-19 10:03:24 +00:00
throw new Error(
`Mapping from one private import to another isn't allowed`,
)
return resolve$1(resolved, context, nextResolve)
2023-09-23 05:12:52 +00:00
}
}
async function resolve$1(originalSpecifier, context, nextResolve) {
2024-06-19 10:03:24 +00:00
const { findPnpApi } = moduleExports
2023-11-23 11:30:10 +00:00
if (!findPnpApi || isBuiltin(originalSpecifier))
2024-06-19 10:03:24 +00:00
return nextResolve(originalSpecifier, context, nextResolve)
let specifier = originalSpecifier
const url = tryParseURL(
specifier,
isRelativeRegexp.test(specifier) ? context.parentURL : void 0,
)
2023-09-23 05:12:52 +00:00
if (url) {
if (url.protocol !== `file:`)
2024-06-19 10:03:24 +00:00
return nextResolve(originalSpecifier, context, nextResolve)
specifier = fileURLToPath(url)
}
const { parentURL, conditions = [] } = context
const issuer =
parentURL && tryParseURL(parentURL)?.protocol === `file:`
? fileURLToPath(parentURL)
: process.cwd()
const pnpapi = findPnpApi(issuer) ?? (url ? findPnpApi(specifier) : null)
if (!pnpapi) return nextResolve(originalSpecifier, context, nextResolve)
2023-09-23 05:12:52 +00:00
if (specifier.startsWith(`#`))
2024-06-19 10:03:24 +00:00
return resolvePrivateRequest(specifier, issuer, context, nextResolve)
const dependencyNameMatch = specifier.match(pathRegExp)
let allowLegacyResolve = false
2023-09-23 05:12:52 +00:00
if (dependencyNameMatch) {
2024-06-19 10:03:24 +00:00
const [, dependencyName, subPath] = dependencyNameMatch
2023-09-23 05:12:52 +00:00
if (subPath === `` && dependencyName !== `pnpapi`) {
2024-06-19 10:03:24 +00:00
const resolved = pnpapi.resolveToUnqualified(
`${dependencyName}/package.json`,
issuer,
)
2023-09-23 05:12:52 +00:00
if (resolved) {
2024-06-19 10:03:24 +00:00
const content = await tryReadFile$1(resolved)
2023-09-23 05:12:52 +00:00
if (content) {
2024-06-19 10:03:24 +00:00
const pkg = JSON.parse(content)
allowLegacyResolve = pkg.exports == null
2023-09-23 05:12:52 +00:00
}
}
}
}
2024-06-19 10:03:24 +00:00
let result
2023-09-23 05:12:52 +00:00
try {
result = pnpapi.resolveRequest(specifier, issuer, {
conditions: new Set(conditions),
2024-06-19 10:03:24 +00:00
extensions: allowLegacyResolve ? void 0 : [],
})
2023-09-23 05:12:52 +00:00
} catch (err) {
2024-06-19 10:03:24 +00:00
if (
err instanceof Error &&
`code` in err &&
err.code === `MODULE_NOT_FOUND`
)
err.code = `ERR_MODULE_NOT_FOUND`
throw err
2023-09-23 05:12:52 +00:00
}
if (!result)
2024-06-19 10:03:24 +00:00
throw new Error(`Resolving '${specifier}' from '${issuer}' failed`)
const resultURL = pathToFileURL(result)
2023-09-23 05:12:52 +00:00
if (url) {
2024-06-19 10:03:24 +00:00
resultURL.search = url.search
resultURL.hash = url.hash
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
if (!parentURL) setEntrypointPath(fileURLToPath(resultURL))
2023-09-23 05:12:52 +00:00
return {
url: resultURL.href,
2024-06-19 10:03:24 +00:00
shortCircuit: true,
}
2023-09-23 05:12:52 +00:00
}
if (!HAS_LAZY_LOADED_TRANSLATORS) {
2024-06-19 10:03:24 +00:00
const binding = process.binding(`fs`)
const originalReadFile = binding.readFileUtf8 || binding.readFileSync
2023-11-23 11:30:10 +00:00
if (originalReadFile) {
2024-06-19 10:03:24 +00:00
binding[originalReadFile.name] = function (...args) {
2023-09-23 05:12:52 +00:00
try {
2023-11-23 11:30:10 +00:00
return fs.readFileSync(args[0], {
encoding: `utf8`,
2024-06-19 10:03:24 +00:00
flag: args[1],
})
} catch {}
return originalReadFile.apply(this, args)
}
2023-11-23 11:30:10 +00:00
} else {
2024-06-19 10:03:24 +00:00
const binding2 = process.binding(`fs`)
const originalfstat = binding2.fstat
const ZIP_MASK = 4278190080
const ZIP_MAGIC = 704643072
binding2.fstat = function (...args) {
const [fd, useBigint, req] = args
if (
(fd & ZIP_MASK) === ZIP_MAGIC &&
useBigint === false &&
req === void 0
) {
2023-11-23 11:30:10 +00:00
try {
2024-06-19 10:03:24 +00:00
const stats = fs.fstatSync(fd)
2023-11-23 11:30:10 +00:00
return new Float64Array([
stats.dev,
stats.mode,
stats.nlink,
stats.uid,
stats.gid,
stats.rdev,
stats.blksize,
stats.ino,
stats.size,
2024-06-19 10:03:24 +00:00
stats.blocks,
])
} catch {}
2023-11-23 11:30:10 +00:00
}
2024-06-19 10:03:24 +00:00
return originalfstat.apply(this, args)
}
2023-11-23 11:30:10 +00:00
}
2023-09-23 05:12:52 +00:00
}
2024-06-19 10:03:24 +00:00
const resolve = resolve$1
const load = load$1
2023-09-23 05:12:52 +00:00
2024-06-19 10:03:24 +00:00
export { load, resolve }