@zenfs/core 1.3.6 → 1.4.0

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.
Files changed (88) hide show
  1. package/dist/backends/memory.d.ts +4 -4
  2. package/dist/backends/memory.js +4 -4
  3. package/dist/backends/overlay.d.ts +5 -2
  4. package/dist/backends/overlay.js +7 -10
  5. package/dist/backends/port/fs.js +1 -4
  6. package/dist/config.js +4 -8
  7. package/dist/context.d.ts +32 -0
  8. package/dist/context.js +23 -0
  9. package/dist/credentials.d.ts +5 -5
  10. package/dist/credentials.js +10 -6
  11. package/dist/emulation/async.d.ts +90 -89
  12. package/dist/emulation/async.js +76 -75
  13. package/dist/emulation/dir.d.ts +3 -1
  14. package/dist/emulation/dir.js +6 -7
  15. package/dist/emulation/index.d.ts +1 -1
  16. package/dist/emulation/index.js +1 -1
  17. package/dist/emulation/promises.d.ts +50 -48
  18. package/dist/emulation/promises.js +78 -77
  19. package/dist/emulation/shared.d.ts +35 -8
  20. package/dist/emulation/shared.js +37 -11
  21. package/dist/emulation/sync.d.ts +63 -62
  22. package/dist/emulation/sync.js +72 -73
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/stats.d.ts +2 -1
  26. package/dist/stats.js +5 -4
  27. package/package.json +3 -5
  28. package/scripts/test.js +78 -17
  29. package/tests/assignment.ts +1 -1
  30. package/tests/common/context.test.ts +19 -0
  31. package/tests/{devices.test.ts → common/devices.test.ts} +3 -3
  32. package/tests/{handle.test.ts → common/handle.test.ts} +1 -1
  33. package/tests/common/mounts.test.ts +36 -0
  34. package/tests/{mutex.test.ts → common/mutex.test.ts} +3 -3
  35. package/tests/common/path.test.ts +34 -0
  36. package/tests/common.ts +4 -3
  37. package/tests/fs/dir.test.ts +11 -11
  38. package/tests/fs/directory.test.ts +17 -17
  39. package/tests/fs/errors.test.ts +29 -39
  40. package/tests/fs/watch.test.ts +2 -2
  41. package/tests/setup/context.ts +9 -0
  42. package/tests/setup/cow+fetch.ts +1 -1
  43. package/tests/setup/memory.ts +1 -1
  44. package/tests/{setup/common.ts → setup.ts} +6 -5
  45. package/src/backends/backend.ts +0 -161
  46. package/src/backends/fetch.ts +0 -180
  47. package/src/backends/file_index.ts +0 -206
  48. package/src/backends/memory.ts +0 -45
  49. package/src/backends/overlay.ts +0 -560
  50. package/src/backends/port/fs.ts +0 -329
  51. package/src/backends/port/readme.md +0 -54
  52. package/src/backends/port/rpc.ts +0 -167
  53. package/src/backends/readme.md +0 -3
  54. package/src/backends/store/fs.ts +0 -667
  55. package/src/backends/store/readme.md +0 -9
  56. package/src/backends/store/simple.ts +0 -154
  57. package/src/backends/store/store.ts +0 -189
  58. package/src/config.ts +0 -227
  59. package/src/credentials.ts +0 -49
  60. package/src/devices.ts +0 -521
  61. package/src/emulation/async.ts +0 -834
  62. package/src/emulation/cache.ts +0 -86
  63. package/src/emulation/config.ts +0 -21
  64. package/src/emulation/constants.ts +0 -182
  65. package/src/emulation/dir.ts +0 -138
  66. package/src/emulation/index.ts +0 -8
  67. package/src/emulation/path.ts +0 -440
  68. package/src/emulation/promises.ts +0 -1140
  69. package/src/emulation/shared.ts +0 -172
  70. package/src/emulation/streams.ts +0 -34
  71. package/src/emulation/sync.ts +0 -863
  72. package/src/emulation/watchers.ts +0 -194
  73. package/src/error.ts +0 -307
  74. package/src/file.ts +0 -631
  75. package/src/filesystem.ts +0 -174
  76. package/src/index.ts +0 -35
  77. package/src/inode.ts +0 -128
  78. package/src/mixins/async.ts +0 -230
  79. package/src/mixins/index.ts +0 -5
  80. package/src/mixins/mutexed.ts +0 -257
  81. package/src/mixins/readonly.ts +0 -96
  82. package/src/mixins/shared.ts +0 -25
  83. package/src/mixins/sync.ts +0 -58
  84. package/src/polyfills.ts +0 -21
  85. package/src/stats.ts +0 -405
  86. package/src/utils.ts +0 -276
  87. package/tests/mounts.test.ts +0 -18
  88. package/tests/path.test.ts +0 -34
@@ -1,440 +0,0 @@
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 derived from 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 type AbsolutePath = `/${string}`;
32
-
33
- export let cwd = '/';
34
-
35
- export function cd(path: string): void {
36
- cwd = resolve(cwd, path);
37
- }
38
-
39
- export const sep = '/';
40
-
41
- function validateObject(str: unknown, name: string): void {
42
- if (typeof str != 'object') {
43
- throw new TypeError(`"${name}" is not an object`);
44
- }
45
- }
46
-
47
- // Resolves . and .. elements in a path with directory names
48
- export function normalizeString(path: string, allowAboveRoot: boolean): string {
49
- let res = '';
50
- let lastSegmentLength = 0;
51
- let lastSlash = -1;
52
- let dots = 0;
53
- let char = '\x00';
54
- for (let i = 0; i <= path.length; ++i) {
55
- if (i < path.length) {
56
- char = path[i];
57
- } else if (char == '/') {
58
- break;
59
- } else {
60
- char = '/';
61
- }
62
-
63
- if (char == '/') {
64
- if (lastSlash === i - 1 || dots === 1) {
65
- // NOOP
66
- } else if (dots === 2) {
67
- if (res.length < 2 || lastSegmentLength !== 2 || res.at(-1) !== '.' || res.at(-2) !== '.') {
68
- if (res.length > 2) {
69
- const lastSlashIndex = res.lastIndexOf('/');
70
- if (lastSlashIndex === -1) {
71
- res = '';
72
- lastSegmentLength = 0;
73
- } else {
74
- res = res.slice(0, lastSlashIndex);
75
- lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
76
- }
77
- lastSlash = i;
78
- dots = 0;
79
- continue;
80
- } else if (res.length !== 0) {
81
- res = '';
82
- lastSegmentLength = 0;
83
- lastSlash = i;
84
- dots = 0;
85
- continue;
86
- }
87
- }
88
- if (allowAboveRoot) {
89
- res += res.length > 0 ? '/..' : '..';
90
- lastSegmentLength = 2;
91
- }
92
- } else {
93
- if (res.length > 0) res += '/' + path.slice(lastSlash + 1, i);
94
- else res = path.slice(lastSlash + 1, i);
95
- lastSegmentLength = i - lastSlash - 1;
96
- }
97
- lastSlash = i;
98
- dots = 0;
99
- } else if (char === '.' && dots !== -1) {
100
- ++dots;
101
- } else {
102
- dots = -1;
103
- }
104
- }
105
- return res;
106
- }
107
-
108
- export function formatExt(ext: string): string {
109
- return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
110
- }
111
-
112
- export function resolve(...parts: string[]): AbsolutePath {
113
- let resolved = '';
114
-
115
- for (const part of [...parts.reverse(), cwd]) {
116
- if (!part.length) {
117
- continue;
118
- }
119
-
120
- resolved = `${part}/${resolved}`;
121
-
122
- if (part.startsWith('/')) {
123
- break;
124
- }
125
- }
126
-
127
- const absolute = resolved.startsWith('/');
128
-
129
- // At this point the path should be resolved to a full absolute path, but
130
- // handle relative paths to be safe (might happen when cwd fails)
131
-
132
- // Normalize the path
133
- resolved = normalizeString(resolved, !absolute);
134
-
135
- if (absolute) {
136
- return `/${resolved}`;
137
- }
138
- return resolved.length ? (resolved as AbsolutePath) : '/';
139
- }
140
-
141
- export function normalize(path: string): string {
142
- if (!path.length) return '.';
143
-
144
- const isAbsolute = path.startsWith('/');
145
- const trailingSeparator = path.endsWith('/');
146
-
147
- // Normalize the path
148
- path = normalizeString(path, !isAbsolute);
149
-
150
- if (!path.length) {
151
- if (isAbsolute) return '/';
152
- return trailingSeparator ? './' : '.';
153
- }
154
- if (trailingSeparator) path += '/';
155
-
156
- return isAbsolute ? `/${path}` : path;
157
- }
158
-
159
- export function isAbsolute(path: string): path is AbsolutePath {
160
- return path.startsWith('/');
161
- }
162
-
163
- export function join(...parts: string[]): string {
164
- if (!parts.length) return '.';
165
- const joined = parts.join('/');
166
- if (!joined?.length) return '.';
167
- return normalize(joined);
168
- }
169
-
170
- export function relative(from: string, to: string): string {
171
- if (from === to) return '';
172
-
173
- // Trim leading forward slashes.
174
- from = resolve(from);
175
- to = resolve(to);
176
-
177
- if (from === to) return '';
178
-
179
- const fromStart = 1;
180
- const fromEnd = from.length;
181
- const fromLen = fromEnd - fromStart;
182
- const toStart = 1;
183
- const toLen = to.length - toStart;
184
-
185
- // Compare paths to find the longest common path from root
186
- const length = fromLen < toLen ? fromLen : toLen;
187
- let lastCommonSep = -1;
188
- let i = 0;
189
- for (; i < length; i++) {
190
- const fromCode = from[fromStart + i];
191
- if (fromCode !== to[toStart + i]) break;
192
- else if (fromCode === '/') lastCommonSep = i;
193
- }
194
- if (i === length) {
195
- if (toLen > length) {
196
- if (to[toStart + i] === '/') {
197
- // We get here if `from` is the exact base path for `to`.
198
- // For example: from='/foo/bar'; to='/foo/bar/baz'
199
- return to.slice(toStart + i + 1);
200
- }
201
- if (i === 0) {
202
- // We get here if `from` is the root
203
- // For example: from='/'; to='/foo'
204
- return to.slice(toStart + i);
205
- }
206
- } else if (fromLen > length) {
207
- if (from[fromStart + i] === '/') {
208
- // We get here if `to` is the exact base path for `from`.
209
- // For example: from='/foo/bar/baz'; to='/foo/bar'
210
- lastCommonSep = i;
211
- } else if (i === 0) {
212
- // We get here if `to` is the root.
213
- // For example: from='/foo/bar'; to='/'
214
- lastCommonSep = 0;
215
- }
216
- }
217
- }
218
-
219
- let out = '';
220
- // Generate the relative path based on the path difference between `to`
221
- // and `from`.
222
- for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
223
- if (i === fromEnd || from[i] === '/') {
224
- out += out.length === 0 ? '..' : '/..';
225
- }
226
- }
227
-
228
- // Lastly, append the rest of the destination (`to`) path that comes after
229
- // the common path parts.
230
- return `${out}${to.slice(toStart + lastCommonSep)}`;
231
- }
232
-
233
- export function dirname(path: string): string {
234
- if (path.length === 0) return '.';
235
- const hasRoot = path[0] === '/';
236
- let end = -1;
237
- let matchedSlash = true;
238
- for (let i = path.length - 1; i >= 1; --i) {
239
- if (path[i] === '/') {
240
- if (!matchedSlash) {
241
- end = i;
242
- break;
243
- }
244
- } else {
245
- // We saw the first non-path separator
246
- matchedSlash = false;
247
- }
248
- }
249
-
250
- if (end === -1) return hasRoot ? '/' : '.';
251
- if (hasRoot && end === 1) return '//';
252
- return path.slice(0, end);
253
- }
254
-
255
- export function basename(path: string, suffix?: string): string {
256
- let start = 0;
257
- let end = -1;
258
- let matchedSlash = true;
259
-
260
- if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
261
- if (suffix === path) return '';
262
- let extIdx = suffix.length - 1;
263
- let firstNonSlashEnd = -1;
264
- for (let i = path.length - 1; i >= 0; --i) {
265
- if (path[i] === '/') {
266
- // If we reached a path separator that was not part of a set of path
267
- // separators at the end of the string, stop now
268
- if (!matchedSlash) {
269
- start = i + 1;
270
- break;
271
- }
272
- } else {
273
- if (firstNonSlashEnd === -1) {
274
- // We saw the first non-path separator, remember this index in case
275
- // we need it if the extension ends up not matching
276
- matchedSlash = false;
277
- firstNonSlashEnd = i + 1;
278
- }
279
- if (extIdx >= 0) {
280
- // Try to match the explicit extension
281
- if (path[i] === suffix[extIdx]) {
282
- if (--extIdx === -1) {
283
- // We matched the extension, so mark this as the end of our path component
284
- end = i;
285
- }
286
- } else {
287
- // Extension does not match, so our result is the entire path component
288
- extIdx = -1;
289
- end = firstNonSlashEnd;
290
- }
291
- }
292
- }
293
- }
294
-
295
- if (start === end) end = firstNonSlashEnd;
296
- else if (end === -1) end = path.length;
297
- return path.slice(start, end);
298
- }
299
- for (let i = path.length - 1; i >= 0; --i) {
300
- if (path[i] === '/') {
301
- // If we reached a path separator that was not part of a set of path separators at the end of the string, stop now
302
- if (!matchedSlash) {
303
- start = i + 1;
304
- break;
305
- }
306
- } else if (end === -1) {
307
- // We saw the first non-path separator, mark this as the end of our path component
308
- matchedSlash = false;
309
- end = i + 1;
310
- }
311
- }
312
-
313
- if (end === -1) return '';
314
- return path.slice(start, end);
315
- }
316
-
317
- export function extname(path: string): string {
318
- let startDot = -1;
319
- let startPart = 0;
320
- let end = -1;
321
- let matchedSlash = true;
322
- // Track the state of characters (if any) we see before our first dot and
323
- // after any path separator we find
324
- let preDotState = 0;
325
- for (let i = path.length - 1; i >= 0; --i) {
326
- if (path[i] === '/') {
327
- // If we reached a path separator that was not part of a set of path
328
- // separators at the end of the string, stop now
329
- if (!matchedSlash) {
330
- startPart = i + 1;
331
- break;
332
- }
333
- continue;
334
- }
335
- if (end === -1) {
336
- // We saw the first non-path separator, mark this as the end of our
337
- // extension
338
- matchedSlash = false;
339
- end = i + 1;
340
- }
341
- if (path[i] === '.') {
342
- // If this is our first dot, mark it as the start of our extension
343
- if (startDot === -1) startDot = i;
344
- else if (preDotState !== 1) preDotState = 1;
345
- } else if (startDot !== -1) {
346
- // We saw a non-dot and non-path separator before our dot, so we should
347
- // have a good chance at having a non-empty extension
348
- preDotState = -1;
349
- }
350
- }
351
-
352
- if (
353
- startDot === -1 ||
354
- end === -1 ||
355
- // We saw a non-dot character immediately before the dot
356
- preDotState === 0 ||
357
- // The (right-most) trimmed path component is exactly '..'
358
- (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
359
- ) {
360
- return '';
361
- }
362
- return path.slice(startDot, end);
363
- }
364
-
365
- export function format(pathObject: ParsedPath): string {
366
- validateObject(pathObject, 'pathObject');
367
- const dir = pathObject.dir || pathObject.root;
368
- const base = pathObject.base || `${pathObject.name || ''}${formatExt(pathObject.ext)}`;
369
- if (!dir) {
370
- return base;
371
- }
372
- return dir === pathObject.root ? `${dir}${base}` : `${dir}/${base}`;
373
- }
374
-
375
- export function parse(path: string): ParsedPath {
376
- const isAbsolute = path.startsWith('/');
377
- const ret = { root: isAbsolute ? '/' : '', dir: '', base: '', ext: '', name: '' };
378
- if (path.length === 0) return ret;
379
-
380
- const start = isAbsolute ? 1 : 0;
381
- let startDot = -1;
382
- let startPart = 0;
383
- let end = -1;
384
- let matchedSlash = true;
385
- let i = path.length - 1;
386
-
387
- // Track the state of characters (if any) we see before our first dot and
388
- // after any path separator we find
389
- let preDotState = 0;
390
-
391
- // Get non-dir info
392
- for (; i >= start; --i) {
393
- if (path[i] === '/') {
394
- // If we reached a path separator that was not part of a set of path
395
- // separators at the end of the string, stop now
396
- if (!matchedSlash) {
397
- startPart = i + 1;
398
- break;
399
- }
400
- continue;
401
- }
402
- if (end === -1) {
403
- // We saw the first non-path separator, mark this as the end of our
404
- // extension
405
- matchedSlash = false;
406
- end = i + 1;
407
- }
408
- if (path[i] === '.') {
409
- // If this is our first dot, mark it as the start of our extension
410
- if (startDot === -1) startDot = i;
411
- else if (preDotState !== 1) preDotState = 1;
412
- } else if (startDot !== -1) {
413
- // We saw a non-dot and non-path separator before our dot, so we should
414
- // have a good chance at having a non-empty extension
415
- preDotState = -1;
416
- }
417
- }
418
-
419
- if (end !== -1) {
420
- const start = startPart === 0 && isAbsolute ? 1 : startPart;
421
- if (
422
- startDot === -1 ||
423
- // We saw a non-dot character immediately before the dot
424
- preDotState === 0 ||
425
- // The (right-most) trimmed path component is exactly '..'
426
- (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
427
- ) {
428
- ret.base = ret.name = path.slice(start, end);
429
- } else {
430
- ret.name = path.slice(start, startDot);
431
- ret.base = path.slice(start, end);
432
- ret.ext = path.slice(startDot, end);
433
- }
434
- }
435
-
436
- if (startPart > 0) ret.dir = path.slice(0, startPart - 1);
437
- else if (isAbsolute) ret.dir = '/';
438
-
439
- return ret;
440
- }