@zenfs/core 0.0.6 → 0.0.8

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,16 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { ParsedPath } from 'node:path';
3
+ export declare const cwd = "/";
4
+ export declare const sep = "/";
5
+ export declare function normalizeString(path: string, allowAboveRoot: boolean): string;
6
+ export declare function formatExt(ext: string): string;
7
+ export declare function resolve(...args: string[]): string;
8
+ export declare function normalize(path: string): string;
9
+ export declare function isAbsolute(path: string): boolean;
10
+ export declare function join(...args: string[]): string;
11
+ export declare function relative(from: string, to: string): string;
12
+ export declare function dirname(path: string): string;
13
+ export declare function basename(path: string, suffix?: string): string;
14
+ export declare function extname(path: string): string;
15
+ export declare function format(pathObject: ParsedPath): string;
16
+ export declare function parse(path: string): ParsedPath;
@@ -0,0 +1,451 @@
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
+ export const cwd = '/';
24
+ export const sep = '/';
25
+ function validateString(str, name) {
26
+ if (typeof str != 'string') {
27
+ throw new TypeError(`"${name}" is not a string`);
28
+ }
29
+ }
30
+ function validateObject(str, name) {
31
+ if (typeof str != 'object') {
32
+ throw new TypeError(`"${name}" is not an object`);
33
+ }
34
+ }
35
+ // Resolves . and .. elements in a path with directory names
36
+ export function normalizeString(path, allowAboveRoot) {
37
+ let res = '';
38
+ let lastSegmentLength = 0;
39
+ let lastSlash = -1;
40
+ let dots = 0;
41
+ let char = '\x00';
42
+ for (let i = 0; i <= path.length; ++i) {
43
+ if (i < path.length) {
44
+ char = path[i];
45
+ }
46
+ else if (char == '/') {
47
+ break;
48
+ }
49
+ else {
50
+ char = '/';
51
+ }
52
+ if (char == '/') {
53
+ if (lastSlash === i - 1 || dots === 1) {
54
+ // NOOP
55
+ }
56
+ else if (dots === 2) {
57
+ if (res.length < 2 || lastSegmentLength !== 2 || res.at(-1) !== '.' || res.at(-2) !== '.') {
58
+ if (res.length > 2) {
59
+ const lastSlashIndex = res.lastIndexOf('/');
60
+ if (lastSlashIndex === -1) {
61
+ res = '';
62
+ lastSegmentLength = 0;
63
+ }
64
+ else {
65
+ res = res.slice(0, lastSlashIndex);
66
+ lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
67
+ }
68
+ lastSlash = i;
69
+ dots = 0;
70
+ continue;
71
+ }
72
+ else if (res.length !== 0) {
73
+ res = '';
74
+ lastSegmentLength = 0;
75
+ lastSlash = i;
76
+ dots = 0;
77
+ continue;
78
+ }
79
+ }
80
+ if (allowAboveRoot) {
81
+ res += res.length > 0 ? '/..' : '..';
82
+ lastSegmentLength = 2;
83
+ }
84
+ }
85
+ else {
86
+ if (res.length > 0)
87
+ res += '/' + path.slice(lastSlash + 1, i);
88
+ else
89
+ res = path.slice(lastSlash + 1, i);
90
+ lastSegmentLength = i - lastSlash - 1;
91
+ }
92
+ lastSlash = i;
93
+ dots = 0;
94
+ }
95
+ else if (char === '.' && dots !== -1) {
96
+ ++dots;
97
+ }
98
+ else {
99
+ dots = -1;
100
+ }
101
+ }
102
+ return res;
103
+ }
104
+ export function formatExt(ext) {
105
+ return ext ? `${ext[0] === '.' ? '' : '.'}${ext}` : '';
106
+ }
107
+ export function resolve(...args) {
108
+ let resolvedPath = '';
109
+ let resolvedAbsolute = false;
110
+ for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
111
+ const path = i >= 0 ? args[i] : cwd;
112
+ validateString(path, `paths[${i}]`);
113
+ // Skip empty entries
114
+ if (path.length === 0) {
115
+ continue;
116
+ }
117
+ resolvedPath = `${path}/${resolvedPath}`;
118
+ resolvedAbsolute = path[0] === '/';
119
+ }
120
+ // At this point the path should be resolved to a full absolute path, but
121
+ // handle relative paths to be safe (might happen when process.cwd() fails)
122
+ // Normalize the path
123
+ resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
124
+ if (resolvedAbsolute) {
125
+ return `/${resolvedPath}`;
126
+ }
127
+ return resolvedPath.length > 0 ? resolvedPath : '.';
128
+ }
129
+ export function normalize(path) {
130
+ validateString(path, 'path');
131
+ if (path.length === 0)
132
+ return '.';
133
+ const isAbsolute = path[0] === '/';
134
+ const trailingSeparator = path.at(-1) === '/';
135
+ // Normalize the path
136
+ path = normalizeString(path, !isAbsolute);
137
+ if (path.length === 0) {
138
+ if (isAbsolute)
139
+ return '/';
140
+ return trailingSeparator ? './' : '.';
141
+ }
142
+ if (trailingSeparator)
143
+ path += '/';
144
+ return isAbsolute ? `/${path}` : path;
145
+ }
146
+ export function isAbsolute(path) {
147
+ validateString(path, 'path');
148
+ return path.length > 0 && path[0] === '/';
149
+ }
150
+ export function join(...args) {
151
+ if (args.length === 0)
152
+ return '.';
153
+ let joined;
154
+ for (let i = 0; i < args.length; ++i) {
155
+ const arg = args[i];
156
+ validateString(arg, 'path');
157
+ if (arg.length > 0) {
158
+ if (joined === undefined)
159
+ joined = arg;
160
+ else
161
+ joined += `/${arg}`;
162
+ }
163
+ }
164
+ if (joined === undefined)
165
+ return '.';
166
+ return normalize(joined);
167
+ }
168
+ export function relative(from, to) {
169
+ validateString(from, 'from');
170
+ validateString(to, 'to');
171
+ if (from === to)
172
+ return '';
173
+ // Trim leading forward slashes.
174
+ from = resolve(from);
175
+ to = resolve(to);
176
+ if (from === to)
177
+ return '';
178
+ const fromStart = 1;
179
+ const fromEnd = from.length;
180
+ const fromLen = fromEnd - fromStart;
181
+ const toStart = 1;
182
+ const toLen = to.length - toStart;
183
+ // Compare paths to find the longest common path from root
184
+ const length = fromLen < toLen ? fromLen : toLen;
185
+ let lastCommonSep = -1;
186
+ let i = 0;
187
+ for (; i < length; i++) {
188
+ const fromCode = from[fromStart + i];
189
+ if (fromCode !== to[toStart + i])
190
+ break;
191
+ else if (fromCode === '/')
192
+ 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
+ }
207
+ else if (fromLen > length) {
208
+ if (from[fromStart + i] === '/') {
209
+ // We get here if `to` is the exact base path for `from`.
210
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
211
+ lastCommonSep = i;
212
+ }
213
+ else if (i === 0) {
214
+ // We get here if `to` is the root.
215
+ // For example: from='/foo/bar'; to='/'
216
+ lastCommonSep = 0;
217
+ }
218
+ }
219
+ }
220
+ let out = '';
221
+ // Generate the relative path based on the path difference between `to`
222
+ // and `from`.
223
+ for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
224
+ if (i === fromEnd || from[i] === '/') {
225
+ out += out.length === 0 ? '..' : '/..';
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
+ export function dirname(path) {
233
+ validateString(path, 'path');
234
+ if (path.length === 0)
235
+ return '.';
236
+ const hasRoot = path[0] === '/';
237
+ let end = -1;
238
+ let matchedSlash = true;
239
+ for (let i = path.length - 1; i >= 1; --i) {
240
+ if (path[i] === '/') {
241
+ if (!matchedSlash) {
242
+ end = i;
243
+ break;
244
+ }
245
+ }
246
+ else {
247
+ // We saw the first non-path separator
248
+ matchedSlash = false;
249
+ }
250
+ }
251
+ if (end === -1)
252
+ return hasRoot ? '/' : '.';
253
+ if (hasRoot && end === 1)
254
+ return '//';
255
+ return path.slice(0, end);
256
+ }
257
+ export function basename(path, suffix) {
258
+ if (suffix !== undefined)
259
+ validateString(suffix, 'ext');
260
+ validateString(path, 'path');
261
+ let start = 0;
262
+ let end = -1;
263
+ let matchedSlash = true;
264
+ if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) {
265
+ if (suffix === path)
266
+ return '';
267
+ let extIdx = suffix.length - 1;
268
+ let firstNonSlashEnd = -1;
269
+ for (let i = path.length - 1; i >= 0; --i) {
270
+ if (path[i] === '/') {
271
+ // If we reached a path separator that was not part of a set of path
272
+ // separators at the end of the string, stop now
273
+ if (!matchedSlash) {
274
+ start = i + 1;
275
+ break;
276
+ }
277
+ }
278
+ else {
279
+ if (firstNonSlashEnd === -1) {
280
+ // We saw the first non-path separator, remember this index in case
281
+ // we need it if the extension ends up not matching
282
+ matchedSlash = false;
283
+ firstNonSlashEnd = i + 1;
284
+ }
285
+ if (extIdx >= 0) {
286
+ // Try to match the explicit extension
287
+ if (path[i] === suffix[extIdx]) {
288
+ if (--extIdx === -1) {
289
+ // We matched the extension, so mark this as the end of our path
290
+ // component
291
+ end = i;
292
+ }
293
+ }
294
+ else {
295
+ // Extension does not match, so our result is the entire path
296
+ // component
297
+ extIdx = -1;
298
+ end = firstNonSlashEnd;
299
+ }
300
+ }
301
+ }
302
+ }
303
+ if (start === end)
304
+ end = firstNonSlashEnd;
305
+ else if (end === -1)
306
+ end = path.length;
307
+ return path.slice(start, end);
308
+ }
309
+ for (let i = path.length - 1; i >= 0; --i) {
310
+ if (path[i] === '/') {
311
+ // If we reached a path separator that was not part of a set of path
312
+ // separators at the end of the string, stop now
313
+ if (!matchedSlash) {
314
+ start = i + 1;
315
+ break;
316
+ }
317
+ }
318
+ else if (end === -1) {
319
+ // We saw the first non-path separator, mark this as the end of our
320
+ // path component
321
+ matchedSlash = false;
322
+ end = i + 1;
323
+ }
324
+ }
325
+ if (end === -1)
326
+ return '';
327
+ return path.slice(start, end);
328
+ }
329
+ export function extname(path) {
330
+ validateString(path, 'path');
331
+ let startDot = -1;
332
+ let startPart = 0;
333
+ let end = -1;
334
+ let matchedSlash = true;
335
+ // Track the state of characters (if any) we see before our first dot and
336
+ // after any path separator we find
337
+ let preDotState = 0;
338
+ for (let i = path.length - 1; i >= 0; --i) {
339
+ if (path[i] === '/') {
340
+ // If we reached a path separator that was not part of a set of path
341
+ // separators at the end of the string, stop now
342
+ if (!matchedSlash) {
343
+ startPart = i + 1;
344
+ break;
345
+ }
346
+ continue;
347
+ }
348
+ if (end === -1) {
349
+ // We saw the first non-path separator, mark this as the end of our
350
+ // extension
351
+ matchedSlash = false;
352
+ end = i + 1;
353
+ }
354
+ if (path[i] === '.') {
355
+ // If this is our first dot, mark it as the start of our extension
356
+ if (startDot === -1)
357
+ startDot = i;
358
+ else if (preDotState !== 1)
359
+ preDotState = 1;
360
+ }
361
+ else if (startDot !== -1) {
362
+ // We saw a non-dot and non-path separator before our dot, so we should
363
+ // have a good chance at having a non-empty extension
364
+ preDotState = -1;
365
+ }
366
+ }
367
+ if (startDot === -1 ||
368
+ end === -1 ||
369
+ // We saw a non-dot character immediately before the dot
370
+ preDotState === 0 ||
371
+ // The (right-most) trimmed path component is exactly '..'
372
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) {
373
+ return '';
374
+ }
375
+ return path.slice(startDot, end);
376
+ }
377
+ export function format(pathObject) {
378
+ validateObject(pathObject, 'pathObject');
379
+ const dir = pathObject.dir || pathObject.root;
380
+ const base = pathObject.base || `${pathObject.name || ''}${formatExt(pathObject.ext)}`;
381
+ if (!dir) {
382
+ return base;
383
+ }
384
+ return dir === pathObject.root ? `${dir}${base}` : `${dir}/${base}`;
385
+ }
386
+ export function parse(path) {
387
+ validateString(path, 'path');
388
+ const isAbsolute = path[0] === '/';
389
+ const ret = { root: isAbsolute ? '/' : '', dir: '', base: '', ext: '', name: '' };
390
+ if (path.length === 0)
391
+ return ret;
392
+ const start = isAbsolute ? 1 : 0;
393
+ let startDot = -1;
394
+ let startPart = 0;
395
+ let end = -1;
396
+ let matchedSlash = true;
397
+ let i = path.length - 1;
398
+ // Track the state of characters (if any) we see before our first dot and
399
+ // after any path separator we find
400
+ let preDotState = 0;
401
+ // Get non-dir info
402
+ for (; i >= start; --i) {
403
+ if (path[i] === '/') {
404
+ // If we reached a path separator that was not part of a set of path
405
+ // separators at the end of the string, stop now
406
+ if (!matchedSlash) {
407
+ startPart = i + 1;
408
+ break;
409
+ }
410
+ continue;
411
+ }
412
+ if (end === -1) {
413
+ // We saw the first non-path separator, mark this as the end of our
414
+ // extension
415
+ matchedSlash = false;
416
+ end = i + 1;
417
+ }
418
+ if (path[i] === '.') {
419
+ // If this is our first dot, mark it as the start of our extension
420
+ if (startDot === -1)
421
+ startDot = i;
422
+ else if (preDotState !== 1)
423
+ preDotState = 1;
424
+ }
425
+ else if (startDot !== -1) {
426
+ // We saw a non-dot and non-path separator before our dot, so we should
427
+ // have a good chance at having a non-empty extension
428
+ preDotState = -1;
429
+ }
430
+ }
431
+ if (end !== -1) {
432
+ const start = startPart === 0 && isAbsolute ? 1 : startPart;
433
+ if (startDot === -1 ||
434
+ // We saw a non-dot character immediately before the dot
435
+ preDotState === 0 ||
436
+ // The (right-most) trimmed path component is exactly '..'
437
+ (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) {
438
+ ret.base = ret.name = path.slice(start, end);
439
+ }
440
+ else {
441
+ ret.name = path.slice(start, startDot);
442
+ ret.base = path.slice(start, end);
443
+ ret.ext = path.slice(startDot, end);
444
+ }
445
+ }
446
+ if (startPart > 0)
447
+ ret.dir = path.slice(0, startPart - 1);
448
+ else if (isAbsolute)
449
+ ret.dir = '/';
450
+ return ret;
451
+ }
@@ -1,5 +1,5 @@
1
1
  // Utilities and shared data
2
- import { posix as path } from 'path';
2
+ import { resolve } from './path.js';
3
3
  import { ApiError, ErrorCode } from '../ApiError.js';
4
4
  import { Cred } from '../cred.js';
5
5
  //import { BackendConstructor } from '../backends.js';
@@ -52,7 +52,7 @@ export function normalizePath(p) {
52
52
  throw new ApiError(ErrorCode.EINVAL, 'Path must not be empty.');
53
53
  }
54
54
  p = p.replaceAll(/\/+/g, '/');
55
- return path.resolve(p);
55
+ return resolve(p);
56
56
  }
57
57
  export function normalizeOptions(options, defEnc, defFlag, defMode) {
58
58
  // typeof null === 'object' so special-case handing is needed.
@@ -129,7 +129,7 @@ export function mount(mountPoint, fs) {
129
129
  if (mountPoint[0] !== '/') {
130
130
  mountPoint = '/' + mountPoint;
131
131
  }
132
- mountPoint = path.resolve(mountPoint);
132
+ mountPoint = resolve(mountPoint);
133
133
  if (mounts.has(mountPoint)) {
134
134
  throw new ApiError(ErrorCode.EINVAL, 'Mount point ' + mountPoint + ' is already in use.');
135
135
  }
@@ -142,7 +142,7 @@ export function umount(mountPoint) {
142
142
  if (mountPoint[0] !== '/') {
143
143
  mountPoint = `/${mountPoint}`;
144
144
  }
145
- mountPoint = path.resolve(mountPoint);
145
+ mountPoint = resolve(mountPoint);
146
146
  if (!mounts.has(mountPoint)) {
147
147
  throw new ApiError(ErrorCode.EINVAL, 'Mount point ' + mountPoint + ' is already unmounted.');
148
148
  }
@@ -12,7 +12,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
12
12
  var _a;
13
13
  import { ApiError, ErrorCode } from './ApiError.js';
14
14
  import { FileFlag, ActionType } from './file.js';
15
- import * as path from 'path';
15
+ import * as path from './emulation/path.js';
16
16
  import { Buffer } from 'buffer';
17
17
  /**
18
18
  * Structure for a filesystem. **All** ZenFS FileSystems must implement
package/dist/utils.d.ts CHANGED
@@ -10,16 +10,6 @@ import type { BaseBackendConstructor } from './backends/backend.js';
10
10
  * @internal
11
11
  */
12
12
  export declare function mkdirpSync(p: string, mode: number, cred: Cred, fs: FileSystem): void;
13
- /**
14
- * Copies a slice of the given buffer
15
- * @internal
16
- */
17
- export declare function copyingSlice(buff: Buffer, start?: number, end?: number): Buffer;
18
- /**
19
- * Option validator for a Buffer file system option.
20
- * @internal
21
- */
22
- export declare function bufferValidator(v: object): Promise<void>;
23
13
  /**
24
14
  * Checks that the given options object is valid for the file system options.
25
15
  * @internal
package/dist/utils.js CHANGED
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { ErrorCode, ApiError } from './ApiError.js';
11
- import * as path from 'path';
11
+ import * as path from './emulation/path.js';
12
12
  import { Buffer } from 'buffer';
13
13
  /**
14
14
  * Synchronous recursive makedir.
@@ -20,33 +20,6 @@ export function mkdirpSync(p, mode, cred, fs) {
20
20
  fs.mkdirSync(p, mode, cred);
21
21
  }
22
22
  }
23
- /**
24
- * Copies a slice of the given buffer
25
- * @internal
26
- */
27
- export function copyingSlice(buff, start = 0, end = buff.length) {
28
- if (start < 0 || end < 0 || end > buff.length || start > end) {
29
- throw new TypeError(`Invalid slice bounds on buffer of length ${buff.length}: [${start}, ${end}]`);
30
- }
31
- if (buff.length === 0) {
32
- // Avoid s0 corner case in ArrayBuffer case.
33
- return Buffer.alloc(0);
34
- }
35
- else {
36
- return buff.subarray(start, end);
37
- }
38
- }
39
- /**
40
- * Option validator for a Buffer file system option.
41
- * @internal
42
- */
43
- export function bufferValidator(v) {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- if (!Buffer.isBuffer(v)) {
46
- throw new ApiError(ErrorCode.EINVAL, 'option must be a Buffer.');
47
- }
48
- });
49
- }
50
23
  /*
51
24
  * Levenshtein distance, from the `js-levenshtein` NPM module.
52
25
  * Copied here to avoid complexity of adding another CommonJS module dependency.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "A filesystem in your browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist",
@@ -53,10 +53,8 @@
53
53
  "buffer": "~5.1.0",
54
54
  "cross-env": "^7.0.3",
55
55
  "esbuild": "^0.17.18",
56
- "esbuild-plugin-polyfill-node": "^0.3.0",
57
56
  "eslint": "^8.36.0",
58
57
  "jest": "^29.5.0",
59
- "path": "^0.12.7",
60
58
  "prettier": "^2.8.7",
61
59
  "ts-jest": "^29.1.0",
62
60
  "typedoc": "^0.25.1",