Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
348 lines (344 loc) · 13.7 KB
/
Copy pathapi.js
File metadata and controls
348 lines (344 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
'use strict'
// Public policy layer. Native batches remain the default for the measured surface;
// Node handles options that require JS callbacks, cancellation or richer input types.
const native = require('./index.js')
const fs = require('node:fs')
const promises = require('node:fs/promises')
const { fileURLToPath } = require('node:url')
const { isUtf8 } = require('node:buffer')
const { getSystemErrorMap } = require('node:util')
let systemErrors
function nativeError(error) {
if (error && typeof error.code === 'string') {
systemErrors ??= new Map([...getSystemErrorMap()].map(([errno, [code]]) => [code, errno]))
if (systemErrors.has(error.code)) error.errno = systemErrors.get(error.code)
if (error.code === 'ERR_FS_CP_EEXIST') error.errno = require('node:os').constants.errno.EEXIST
if (error.code === 'ERR_FS_EISDIR') error.errno = require('node:os').constants.errno.EISDIR
const copyErrno = {
ERR_FS_CP_EINVAL: 'EINVAL',
ERR_FS_CP_DIR_TO_NON_DIR: 'EISDIR',
ERR_FS_CP_NON_DIR_TO_DIR: 'ENOTDIR',
ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY: 'EINVAL',
ERR_FS_CP_FIFO_PIPE: 'EINVAL',
ERR_FS_CP_SOCKET: 'EINVAL',
}[error.code]
if (copyErrno) error.errno = require('node:os').constants.errno[copyErrno]
}
return error
}
function syncCall(name, args) {
try {
return native[name](...args)
} catch (error) {
throw nativeError(error)
}
}
async function asyncCall(name, args) {
try {
return await native[name](...args)
} catch (error) {
throw nativeError(error)
}
}
function nativePath(path) {
if (path instanceof URL) return fileURLToPath(path)
if (Buffer.isBuffer(path)) return isUtf8(path) && !path.includes(0) ? path.toString() : undefined
return typeof path === 'string' && !path.includes('\0') ? path : undefined
}
function optionsObject(options) {
return options == null ? {} : typeof options === 'string' ? { encoding: options } : options
}
function validOptions(options, keys) {
return (
options &&
typeof options === 'object' &&
!Array.isArray(options) &&
(Object.getPrototypeOf(options) === Object.prototype || Object.getPrototypeOf(options) === null) &&
Object.getOwnPropertyNames(options).every((key) => keys.includes(key))
)
}
function concurrency(options) {
const count = options?.concurrency
if (count !== undefined && (!Number.isInteger(count) || count < 0 || count > 1024)) {
const error = new RangeError('concurrency must be an integer between 0 and 1024')
error.code = 'ERR_OUT_OF_RANGE'
throw error
}
}
function booleans(options, keys) {
return keys.every((key) => options[key] === undefined || typeof options[key] === 'boolean')
}
const encodings = new Set(['utf8', 'utf-8', 'ascii', 'latin1', 'binary', 'base64', 'base64url', 'hex'])
function encodingSupported(encoding, buffer = false) {
return (
encoding == null ||
(typeof encoding === 'string' && (encodings.has(encoding.toLowerCase()) || (buffer && encoding === 'buffer')))
)
}
// Node treats null encoding as the default; napi-rs optional strings accept undefined.
function nativeEncodingOptions(options) {
return options.encoding === null ? Object.create(options, { encoding: { value: undefined } }) : options
}
function directoryRoute(path, options) {
const p = nativePath(path)
const opts = optionsObject(options)
concurrency(opts)
const supported =
validOptions(opts, ['encoding', 'recursive', 'withFileTypes', 'skipHidden', 'concurrency']) &&
booleans(opts, ['recursive', 'withFileTypes', 'skipHidden']) &&
encodingSupported(opts.encoding, true)
// Node's recursive Buffer result currently has runtime-specific behavior.
return p !== undefined && supported && !(opts.recursive && opts.encoding === 'buffer')
? [p, nativeEncodingOptions(opts)]
: undefined
}
function copyRoute(src, dest, options) {
const a = nativePath(src),
b = nativePath(dest)
const opts = options ?? {}
concurrency(opts)
const supported =
options !== null &&
validOptions(opts, [
'recursive',
'force',
'errorOnExist',
'preserveTimestamps',
'dereference',
'verbatimSymlinks',
'concurrency',
]) &&
booleans(opts, ['recursive', 'force', 'errorOnExist', 'preserveTimestamps', 'dereference', 'verbatimSymlinks'])
// Dereference cycles, callbacks and copy modes use Node's full implementation.
return a !== undefined && b !== undefined && supported && !opts.dereference && process.platform !== 'win32'
? [a, b, opts]
: undefined
}
function removeRoute(path, options) {
const p = nativePath(path),
opts = options ?? {}
concurrency(opts)
const supported =
options !== null &&
validOptions(opts, ['recursive', 'force', 'maxRetries', 'retryDelay', 'concurrency']) &&
booleans(opts, ['recursive', 'force']) &&
['maxRetries', 'retryDelay'].every(
(key) => opts[key] === undefined || (Number.isInteger(opts[key]) && opts[key] >= 0 && opts[key] <= 0xffffffff),
)
return p !== undefined && supported && opts.recursive && process.platform !== 'win32' ? [p, opts] : undefined
}
function readRoute(path, options) {
const p = nativePath(path),
opts = optionsObject(options)
const supported =
validOptions(opts, ['encoding', 'flag', 'lines']) &&
encodingSupported(opts.encoding) &&
(opts.flag === undefined || opts.flag === 'r')
if (opts?.lines !== undefined && (!supported || p === undefined)) {
const error = new TypeError(
'lines requires a string/UTF-8 Buffer/file URL path, a supported encoding, and flag r; signal and file handles are not supported with lines',
)
error.code = 'ERR_INVALID_ARG_VALUE'
throw error
}
return p !== undefined && supported ? [p, nativeEncodingOptions(opts)] : undefined
}
function nativePattern(pattern) {
if (typeof pattern !== 'string' || pattern.length === 0 || pattern.endsWith('/')) return false
if (!pattern.includes('*') && !pattern.includes('{')) return false
// Globset's ?/classes are byte-based; Node matches Unicode characters.
// Non-globstar directory wildcards also have different symlink traversal rules.
if (/[?[\]!()+]|\\/.test(pattern) || pattern.startsWith('/') || /\{[^}]*[.{]/.test(pattern)) return false
if (/\{[^},]*\}|\{,|,,|,\}/.test(pattern)) return false
const components = pattern.split('/')
if (components.some((part) => part.startsWith('.'))) return false
if (components.slice(0, -1).some((part) => part !== '**' && /[*{}]/.test(part))) return false
let braces = 0
for (const character of pattern) {
if (character === '{') braces++
if (character === '}' && --braces < 0) return false
}
return braces === 0
}
function globRoute(pattern, options) {
const opts = options ?? {}
concurrency(opts)
const patterns = typeof pattern === 'string' ? [pattern] : pattern
const cwd = opts.cwd === undefined ? undefined : nativePath(opts.cwd)
const supported =
options !== null &&
Array.isArray(patterns) &&
patterns.every(nativePattern) &&
validOptions(opts, ['cwd', 'withFileTypes', 'exclude', 'concurrency', 'gitIgnore']) &&
booleans(opts, ['withFileTypes', 'gitIgnore']) &&
(opts.exclude === undefined ||
(Array.isArray(opts.exclude) &&
opts.exclude.every(
(pattern) =>
typeof pattern === 'string' &&
!/[?[\]!()+{}]|\\/.test(pattern) &&
!pattern.endsWith('/') &&
(opts.gitIgnore ||
(!pattern.includes('*') &&
!pattern.includes(':') &&
pattern.split('/').every((part) => part !== '' && part !== '.' && part !== '..'))),
))) &&
(opts.cwd === undefined || cwd !== undefined)
if (!supported && opts.gitIgnore) {
const error = new TypeError('gitIgnore requires native-supported glob patterns and string-array excludes')
error.code = 'ERR_INVALID_ARG_VALUE'
throw error
}
// Adjacent globstars are one globstar in Node's matcher, including root inclusion.
const normalized = supported
? patterns.map((value) =>
value
.split('/')
.filter((part, i, parts) => part !== '**' || parts[i - 1] !== '**')
.join('/'),
)
: undefined
return supported
? [typeof pattern === 'string' ? normalized[0] : normalized, { ...opts, cwd }, !opts.gitIgnore]
: undefined
}
// Explicit assignments preserve CommonJS named exports for ESM consumers.
module.exports.Dirent = native.Dirent
module.exports.Stats = native.Stats
module.exports.access = native.access
module.exports.accessSync = native.accessSync
module.exports.appendFile = native.appendFile
module.exports.appendFileSync = native.appendFileSync
module.exports.chmod = native.chmod
module.exports.chmodSync = native.chmodSync
module.exports.chown = native.chown
module.exports.chownSync = native.chownSync
module.exports.copyFile = native.copyFile
module.exports.copyFileSync = native.copyFileSync
module.exports.cp = native.cp
module.exports.cpSync = native.cpSync
module.exports.exists = native.exists
module.exports.existsSync = native.existsSync
module.exports.glob = native.glob
module.exports.globSync = native.globSync
module.exports.link = native.link
module.exports.linkSync = native.linkSync
module.exports.lstat = native.lstat
module.exports.lstatSync = native.lstatSync
module.exports.mkdir = native.mkdir
module.exports.mkdirSync = native.mkdirSync
module.exports.mkdtemp = native.mkdtemp
module.exports.mkdtempSync = native.mkdtempSync
module.exports.readdir = native.readdir
module.exports.readdirSync = native.readdirSync
module.exports.readFile = native.readFile
module.exports.readFileSync = native.readFileSync
module.exports.readlink = native.readlink
module.exports.readlinkSync = native.readlinkSync
module.exports.realpath = native.realpath
module.exports.realpathSync = native.realpathSync
module.exports.rename = native.rename
module.exports.renameSync = native.renameSync
module.exports.rm = native.rm
module.exports.rmdir = native.rmdir
module.exports.rmdirSync = native.rmdirSync
module.exports.rmSync = native.rmSync
module.exports.scan = native.scan
module.exports.scanSync = native.scanSync
module.exports.stat = native.stat
module.exports.statSync = native.statSync
module.exports.symlink = native.symlink
module.exports.symlinkSync = native.symlinkSync
module.exports.truncate = native.truncate
module.exports.truncateSync = native.truncateSync
module.exports.unlink = native.unlink
module.exports.unlinkSync = native.unlinkSync
module.exports.utimes = native.utimes
module.exports.utimesSync = native.utimesSync
module.exports.writeFile = native.writeFile
module.exports.writeFileSync = native.writeFileSync
module.exports.constants = fs.constants
module.exports.readdirSync = function readdirSync(path, options) {
const args = directoryRoute(path, options)
return args ? syncCall('readdirSync', args) : fs.readdirSync(path, options)
}
module.exports.readdir = async function readdir(path, options) {
const args = directoryRoute(path, options)
return args ? asyncCall('readdir', args) : promises.readdir(path, options)
}
module.exports.cpSync = function cpSync(src, dest, options) {
concurrency(options)
// Node's synchronous copy already executes natively and differs from Promise cp
// on symlink errors. Retain its runtime-specific semantics.
return fs.cpSync(src, dest, options)
}
module.exports.cp = async function cp(src, dest, options) {
const args = copyRoute(src, dest, options)
return args ? asyncCall('cp', args) : promises.cp(src, dest, options)
}
module.exports.rmSync = function rmSync(path, options) {
concurrency(options)
// Node 24's synchronous native remover reports different permission errors.
return fs.rmSync(path, options)
}
module.exports.rm = async function rm(path, options) {
const args = removeRoute(path, options)
return args ? asyncCall('rm', args) : promises.rm(path, options)
}
module.exports.readFileSync = function readFileSync(path, options) {
const args = readRoute(path, options)
return args ? syncCall('readFileSync', args) : fs.readFileSync(path, options)
}
module.exports.readFile = async function readFile(path, options) {
const args = readRoute(path, options)
return args ? asyncCall('readFile', args) : promises.readFile(path, options)
}
module.exports.globSync = function globSync(pattern, options) {
const args = globRoute(pattern, options)
if (args) {
try {
return syncCall('globSync', args)
} catch (error) {
if (error?.code !== 'ERR_VOOYA_GLOB_NODE_FALLBACK') throw error
}
}
return fs.globSync(pattern, options)
}
module.exports.glob = function glob(pattern, options) {
// Preserve the actual Promise batch API while accepting Node's for-await usage.
const batch = (async () => {
const args = globRoute(pattern, options)
if (args) {
try {
return await asyncCall('glob', args)
} catch (error) {
if (error?.code !== 'ERR_VOOYA_GLOB_NODE_FALLBACK') throw error
}
}
const entries = []
for await (const entry of promises.glob(pattern, options)) entries.push(entry)
return entries
})()
const iterator = (async function* () {
yield* await batch
})()
batch.next = iterator.next.bind(iterator)
batch.return = iterator.return.bind(iterator)
batch.throw = iterator.throw.bind(iterator)
batch[Symbol.asyncIterator] = function () {
return this
}
return batch
}
module.exports.scanSync = function scanSync(root, options) {
const path = nativePath(root)
concurrency(options)
if (path === undefined) throw new TypeError('scan requires a string, UTF-8 Buffer or file URL path')
return syncCall('scanSync', [path, options])
}
module.exports.scan = async function scan(root, options) {
const path = nativePath(root)
concurrency(options)
if (path === undefined) throw new TypeError('scan requires a string, UTF-8 Buffer or file URL path')
return asyncCall('scan', [path, options])
}