@zenfs/core 0.9.2 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,468 @@
1
+ /*
2
+ Copyright Joyent, Inc. and other Node contributors.
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a
5
+ copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to permit
9
+ persons to whom the Software is furnished to do so, subject to the
10
+ following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included
13
+ in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
18
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ /*
25
+ This is the POSIX path code of NodeJS
26
+ https://raw.githubusercontent.com/nodejs/node/3907bd1/lib/path.js
27
+ */
28
+
29
+ import type { ParsedPath } from 'node:path';
30
+
31
+ export let cwd = '/';
32
+
33
+ export function cd(path: string): void {
34
+ cwd = resolve(cwd, path);
35
+ }
36
+
37
+ export const sep = '/';
38
+
39
+ function validateString(str: unknown, name: string): void {
40
+ if (typeof str != 'string') {
41
+ throw new TypeError(`"${name}" is not a string`);
42
+ }
43
+ }
44
+
45
+ function validateObject(str: unknown, name: string): void {
46
+ if (typeof str != 'object') {
47
+ throw new TypeError(`"${name}" is not an object`);
48
+ }
49
+ }
50
+
51
+ // Resolves . and .. elements in a path with directory names
52
+ export function normalizeString(path: string, allowAboveRoot: boolean): string {
53
+ let res = '';
54
+ let lastSegmentLength = 0;
55
+ let lastSlash = -1;
56
+ let dots = 0;
57
+ let char = '\x00';
58
+ for (let i = 0; i <= path.length; ++i) {
59
+ if (i < path.length) {
60
+ char = path[i];
61
+ } else if (char == '/') {
62
+ break;
63
+ } else {
64
+ char = '/';
65
+ }
66
+
67
+ if (char == '/') {
68
+ if (lastSlash === i - 1 || dots === 1) {
69
+ // NOOP
70
+ } else if (dots === 2) {
71
+ if (res.length < 2 || lastSegmentLength !== 2 || res.at(-1) !== '.' || res.at(-2) !== '.') {
72
+ if (res.length > 2) {
73
+ const lastSlashIndex = res.lastIndexOf('/');
74
+ if (lastSlashIndex === -1) {
75
+ res = '';
76
+ lastSegmentLength = 0;
77
+ } else {
78
+ res = res.slice(0, lastSlashIndex);
79
+ lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
80
+ }
81
+ lastSlash = i;
82
+ dots = 0;
83
+ continue;
84
+ } else if (res.length !== 0) {
85
+ res = '';
86
+ lastSegmentLength = 0;
87
+ lastSlash = i;
88
+ dots = 0;
89
+ continue;
90
+ }
91
+ }
92
+ if (allowAboveRoot) {
93
+ res += res.length > 0 ? '/..' : '..';
94
+ lastSegmentLength = 2;
95
+ }
96
+ } else {
97
+ if (res.length > 0) res += '/' + path.slice(lastSlash + 1, i);
98
+ else res = path.slice(lastSlash + 1, i);
99
+ lastSegmentLength = i - lastSlash - 1;
100
+ }
101
+ lastSlash = i;
102
+ dots = 0;
103
+ } else if (char === '.' && dots !== -1) {
104
+ ++dots;
105
+ } else {
106
+ dots = -1;
107
+ }
108
+ }
109
+ return res;
110
+ }
111
+
112
+ export function formatExt(ext: string): string {
113
+ return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
114
+ }
115
+
116
+ export function resolve(...args: string[]): string {
117
+ let resolved = '';
118
+ let absolute = false;
119
+
120
+ for (let i = args.length - 1; i >= -1 && !absolute; i--) {
121
+ const path = i >= 0 ? args[i] : cwd;
122
+ validateString(path, `paths[${i}]`);
123
+
124
+ // Skip empty entries
125
+ if (!path.length) {
126
+ continue;
127
+ }
128
+
129
+ resolved = `${path}/${resolved}`;
130
+ absolute = path[0] == '/';
131
+ }
132
+
133
+ // At this point the path should be resolved to a full absolute path, but
134
+ // handle relative paths to be safe (might happen when cwd fails)
135
+
136
+ // Normalize the path
137
+ resolved = normalizeString(resolved, !absolute);
138
+
139
+ if (absolute) {
140
+ return `/${resolved}`;
141
+ }
142
+ return resolved.length > 0 ? resolved : '/';
143
+ }
144
+
145
+ export function normalize(path: string): string {
146
+ validateString(path, 'path');
147
+
148
+ if (path.length === 0) return '.';
149
+
150
+ const isAbsolute = path[0] === '/';
151
+ const trailingSeparator = path.at(-1) === '/';
152
+
153
+ // Normalize the path
154
+ path = normalizeString(path, !isAbsolute);
155
+
156
+ if (path.length === 0) {
157
+ if (isAbsolute) return '/';
158
+ return trailingSeparator ? './' : '.';
159
+ }
160
+ if (trailingSeparator) path += '/';
161
+
162
+ return isAbsolute ? `/${path}` : path;
163
+ }
164
+
165
+ export function isAbsolute(path: string): boolean {
166
+ validateString(path, 'path');
167
+ return path.length > 0 && path[0] === '/';
168
+ }
169
+
170
+ export function join(...args: string[]): string {
171
+ if (args.length === 0) return '.';
172
+ let joined;
173
+ for (let i = 0; i < args.length; ++i) {
174
+ const arg = args[i];
175
+ validateString(arg, 'path');
176
+ if (arg.length > 0) {
177
+ if (joined === undefined) joined = arg;
178
+ else joined += `/${arg}`;
179
+ }
180
+ }
181
+ if (joined === undefined) return '.';
182
+ return normalize(joined);
183
+ }
184
+
185
+ export function relative(from: string, to: string) {
186
+ validateString(from, 'from');
187
+ validateString(to, 'to');
188
+
189
+ if (from === to) return '';
190
+
191
+ // Trim leading forward slashes.
192
+ from = resolve(from);
193
+ to = resolve(to);
194
+
195
+ if (from === to) return '';
196
+
197
+ const fromStart = 1;
198
+ const fromEnd = from.length;
199
+ const fromLen = fromEnd - fromStart;
200
+ const toStart = 1;
201
+ const toLen = to.length - toStart;
202
+
203
+ // Compare paths to find the longest common path from root
204
+ const length = fromLen < toLen ? fromLen : toLen;
205
+ let lastCommonSep = -1;
206
+ let i = 0;
207
+ for (; i < length; i++) {
208
+ const fromCode = from[fromStart + i];
209
+ if (fromCode !== to[toStart + i]) break;
210
+ else if (fromCode === '/') lastCommonSep = i;
211
+ }
212
+ if (i === length) {
213
+ if (toLen > length) {
214
+ if (to[toStart + i] === '/') {
215
+ // We get here if `from` is the exact base path for `to`.
216
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
217
+ return to.slice(toStart + i + 1);
218
+ }
219
+ if (i === 0) {
220
+ // We get here if `from` is the root
221
+ // For example: from='/'; to='/foo'
222
+ return to.slice(toStart + i);
223
+ }
224
+ } else if (fromLen > length) {
225
+ if (from[fromStart + i] === '/') {
226
+ // We get here if `to` is the exact base path for `from`.
227
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
228
+ lastCommonSep = i;
229
+ } else if (i === 0) {
230
+ // We get here if `to` is the root.
231
+ // For example: from='/foo/bar'; to='/'
232
+ lastCommonSep = 0;
233
+ }
234
+ }
235
+ }
236
+
237
+ let out = '';
238
+ // Generate the relative path based on the path difference between `to`
239
+ // and `from`.
240
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
241
+ if (i === fromEnd || from[i] === '/') {
242
+ out += out.length === 0 ? '..' : '/..';
243
+ }
244
+ }
245
+
246
+ // Lastly, append the rest of the destination (`to`) path that comes after
247
+ // the common path parts.
248
+ return `${out}${to.slice(toStart + lastCommonSep)}`;
249
+ }
250
+
251
+ export function dirname(path: string): string {
252
+ validateString(path, 'path');
253
+ if (path.length === 0) return '.';
254
+ const hasRoot = path[0] === '/';
255
+ let end = -1;
256
+ let matchedSlash = true;
257
+ for (let i = path.length - 1; i >= 1; --i) {
258
+ if (path[i] === '/') {
259
+ if (!matchedSlash) {
260
+ end = i;
261
+ break;
262
+ }
263
+ } else {
264
+ // We saw the first non-path separator
265
+ matchedSlash = false;
266
+ }
267
+ }
268
+
269
+ if (end === -1) return hasRoot ? '/' : '.';
270
+ if (hasRoot && end === 1) return '//';
271
+ return path.slice(0, end);
272
+ }
273
+
274
+ export function basename(path: string, suffix?: string): string {
275
+ if (suffix !== undefined) validateString(suffix, 'ext');
276
+ validateString(path, 'path');
277
+
278
+ let start = 0;
279
+ let end = -1;
280
+ let matchedSlash = true;
281
+
282
+ if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
283
+ if (suffix === path) return '';
284
+ let extIdx = suffix.length - 1;
285
+ let firstNonSlashEnd = -1;
286
+ for (let i = path.length - 1; i >= 0; --i) {
287
+ if (path[i] === '/') {
288
+ // If we reached a path separator that was not part of a set of path
289
+ // separators at the end of the string, stop now
290
+ if (!matchedSlash) {
291
+ start = i + 1;
292
+ break;
293
+ }
294
+ } else {
295
+ if (firstNonSlashEnd === -1) {
296
+ // We saw the first non-path separator, remember this index in case
297
+ // we need it if the extension ends up not matching
298
+ matchedSlash = false;
299
+ firstNonSlashEnd = i + 1;
300
+ }
301
+ if (extIdx >= 0) {
302
+ // Try to match the explicit extension
303
+ if (path[i] === suffix[extIdx]) {
304
+ if (--extIdx === -1) {
305
+ // We matched the extension, so mark this as the end of our path
306
+ // component
307
+ end = i;
308
+ }
309
+ } else {
310
+ // Extension does not match, so our result is the entire path
311
+ // component
312
+ extIdx = -1;
313
+ end = firstNonSlashEnd;
314
+ }
315
+ }
316
+ }
317
+ }
318
+
319
+ if (start === end) end = firstNonSlashEnd;
320
+ else if (end === -1) end = path.length;
321
+ return path.slice(start, end);
322
+ }
323
+ for (let i = path.length - 1; i >= 0; --i) {
324
+ if (path[i] === '/') {
325
+ // If we reached a path separator that was not part of a set of path
326
+ // separators at the end of the string, stop now
327
+ if (!matchedSlash) {
328
+ start = i + 1;
329
+ break;
330
+ }
331
+ } else if (end === -1) {
332
+ // We saw the first non-path separator, mark this as the end of our
333
+ // path component
334
+ matchedSlash = false;
335
+ end = i + 1;
336
+ }
337
+ }
338
+
339
+ if (end === -1) return '';
340
+ return path.slice(start, end);
341
+ }
342
+
343
+ export function extname(path: string): string {
344
+ validateString(path, 'path');
345
+ let startDot = -1;
346
+ let startPart = 0;
347
+ let end = -1;
348
+ let matchedSlash = true;
349
+ // Track the state of characters (if any) we see before our first dot and
350
+ // after any path separator we find
351
+ let preDotState = 0;
352
+ for (let i = path.length - 1; i >= 0; --i) {
353
+ if (path[i] === '/') {
354
+ // If we reached a path separator that was not part of a set of path
355
+ // separators at the end of the string, stop now
356
+ if (!matchedSlash) {
357
+ startPart = i + 1;
358
+ break;
359
+ }
360
+ continue;
361
+ }
362
+ if (end === -1) {
363
+ // We saw the first non-path separator, mark this as the end of our
364
+ // extension
365
+ matchedSlash = false;
366
+ end = i + 1;
367
+ }
368
+ if (path[i] === '.') {
369
+ // If this is our first dot, mark it as the start of our extension
370
+ if (startDot === -1) startDot = i;
371
+ else if (preDotState !== 1) preDotState = 1;
372
+ } else if (startDot !== -1) {
373
+ // We saw a non-dot and non-path separator before our dot, so we should
374
+ // have a good chance at having a non-empty extension
375
+ preDotState = -1;
376
+ }
377
+ }
378
+
379
+ if (
380
+ startDot === -1 ||
381
+ end === -1 ||
382
+ // We saw a non-dot character immediately before the dot
383
+ preDotState === 0 ||
384
+ // The (right-most) trimmed path component is exactly '..'
385
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
386
+ ) {
387
+ return '';
388
+ }
389
+ return path.slice(startDot, end);
390
+ }
391
+
392
+ export function format(pathObject: ParsedPath): string {
393
+ validateObject(pathObject, 'pathObject');
394
+ const dir = pathObject.dir || pathObject.root;
395
+ const base = pathObject.base || `${pathObject.name || ''}${formatExt(pathObject.ext)}`;
396
+ if (!dir) {
397
+ return base;
398
+ }
399
+ return dir === pathObject.root ? `${dir}${base}` : `${dir}/${base}`;
400
+ }
401
+
402
+ export function parse(path: string): ParsedPath {
403
+ validateString(path, 'path');
404
+ const isAbsolute = path[0] === '/';
405
+ const ret = { root: isAbsolute ? '/' : '', dir: '', base: '', ext: '', name: '' };
406
+ if (path.length === 0) return ret;
407
+
408
+ const start = isAbsolute ? 1 : 0;
409
+ let startDot = -1;
410
+ let startPart = 0;
411
+ let end = -1;
412
+ let matchedSlash = true;
413
+ let i = path.length - 1;
414
+
415
+ // Track the state of characters (if any) we see before our first dot and
416
+ // after any path separator we find
417
+ let preDotState = 0;
418
+
419
+ // Get non-dir info
420
+ for (; i >= start; --i) {
421
+ if (path[i] === '/') {
422
+ // If we reached a path separator that was not part of a set of path
423
+ // separators at the end of the string, stop now
424
+ if (!matchedSlash) {
425
+ startPart = i + 1;
426
+ break;
427
+ }
428
+ continue;
429
+ }
430
+ if (end === -1) {
431
+ // We saw the first non-path separator, mark this as the end of our
432
+ // extension
433
+ matchedSlash = false;
434
+ end = i + 1;
435
+ }
436
+ if (path[i] === '.') {
437
+ // If this is our first dot, mark it as the start of our extension
438
+ if (startDot === -1) startDot = i;
439
+ else if (preDotState !== 1) preDotState = 1;
440
+ } else if (startDot !== -1) {
441
+ // We saw a non-dot and non-path separator before our dot, so we should
442
+ // have a good chance at having a non-empty extension
443
+ preDotState = -1;
444
+ }
445
+ }
446
+
447
+ if (end !== -1) {
448
+ const start = startPart === 0 && isAbsolute ? 1 : startPart;
449
+ if (
450
+ startDot === -1 ||
451
+ // We saw a non-dot character immediately before the dot
452
+ preDotState === 0 ||
453
+ // The (right-most) trimmed path component is exactly '..'
454
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
455
+ ) {
456
+ ret.base = ret.name = path.slice(start, end);
457
+ } else {
458
+ ret.name = path.slice(start, startDot);
459
+ ret.base = path.slice(start, end);
460
+ ret.ext = path.slice(startDot, end);
461
+ }
462
+ }
463
+
464
+ if (startPart > 0) ret.dir = path.slice(0, startPart - 1);
465
+ else if (isAbsolute) ret.dir = '/';
466
+
467
+ return ret;
468
+ }