@zenfs/core 0.0.7 → 0.0.9

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.
@@ -1,6 +1,7 @@
1
1
  import { ApiError, ErrorCode } from '../ApiError.js';
2
2
  import { FileFlag } from '../file.js';
3
3
  import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError, mounts } from './shared.js';
4
+ import { encode } from '../utils.js';
4
5
  function doOp(...[name, resolveSymlinks, path, ...args]) {
5
6
  path = normalizePath(path);
6
7
  const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && existsSync(path) ? realpathSync(path) : path);
@@ -175,7 +176,7 @@ export function writeSync(fd, arg2, arg3, arg4, arg5) {
175
176
  position = typeof arg3 === 'number' ? arg3 : null;
176
177
  const encoding = (typeof arg4 === 'string' ? arg4 : 'utf8');
177
178
  offset = 0;
178
- buffer = Buffer.from(arg2, encoding);
179
+ buffer = encode(arg2);
179
180
  length = buffer.length;
180
181
  }
181
182
  else {
package/dist/file.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Stats } from './stats.js';
3
2
  import { FileSystem } from './filesystem.js';
4
3
  export declare enum ActionType {
@@ -127,7 +126,7 @@ export interface File {
127
126
  * **Core**: Write buffer to the file.
128
127
  * Note that it is unsafe to use fs.write multiple times on the same file
129
128
  * without waiting for the callback.
130
- * @param buffer Buffer containing the data to write to
129
+ * @param buffer Uint8Array containing the data to write to
131
130
  * the file.
132
131
  * @param offset Offset in the buffer to start reading data from.
133
132
  * @param length The amount of bytes to write to the file.
@@ -136,12 +135,12 @@ export interface File {
136
135
  * the current position.
137
136
  * @returns Promise resolving to the new length of the buffer
138
137
  */
139
- write(buffer: Buffer, offset: number, length: number, position: number | null): Promise<number>;
138
+ write(buffer: Uint8Array, offset: number, length: number, position: number | null): Promise<number>;
140
139
  /**
141
140
  * **Core**: Write buffer to the file.
142
141
  * Note that it is unsafe to use fs.writeSync multiple times on the same file
143
142
  * without waiting for it to return.
144
- * @param buffer Buffer containing the data to write to
143
+ * @param buffer Uint8Array containing the data to write to
145
144
  * the file.
146
145
  * @param offset Offset in the buffer to start reading data from.
147
146
  * @param length The amount of bytes to write to the file.
@@ -149,7 +148,7 @@ export interface File {
149
148
  * data should be written. If position is null, the data will be written at
150
149
  * the current position.
151
150
  */
152
- writeSync(buffer: Buffer, offset: number, length: number, position: number | null): number;
151
+ writeSync(buffer: Uint8Array, offset: number, length: number, position: number | null): number;
153
152
  /**
154
153
  * **Core**: Read data from the file.
155
154
  * @param buffer The buffer that the data will be
@@ -162,9 +161,9 @@ export interface File {
162
161
  * position.
163
162
  * @returns Promise resolving to the new length of the buffer
164
163
  */
165
- read(buffer: Buffer, offset: number, length: number, position: number | null): Promise<{
164
+ read(buffer: Uint8Array, offset: number, length: number, position: number | null): Promise<{
166
165
  bytesRead: number;
167
- buffer: Buffer;
166
+ buffer: Uint8Array;
168
167
  }>;
169
168
  /**
170
169
  * **Core**: Read data from the file.
@@ -175,7 +174,7 @@ export interface File {
175
174
  * in the file. If position is null, data will be read from the current file
176
175
  * position.
177
176
  */
178
- readSync(buffer: Buffer, offset: number, length: number, position: number): number;
177
+ readSync(buffer: Uint8Array, offset: number, length: number, position: number): number;
179
178
  /**
180
179
  * **Supplementary**: Asynchronous `datasync`.
181
180
  *
@@ -231,7 +230,7 @@ export declare class BaseFile {
231
230
  }
232
231
  /**
233
232
  * An implementation of the File interface that operates on a file that is
234
- * completely in-memory. PreloadFiles are backed by a Buffer.
233
+ * completely in-memory. PreloadFiles are backed by a Uint8Array.
235
234
  *
236
235
  * This is also an abstract class, as it lacks an implementation of 'sync' and
237
236
  * 'close'. Each filesystem that wishes to use this file representation must
@@ -244,7 +243,7 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
244
243
  protected _path: string;
245
244
  protected _stat: Stats;
246
245
  protected _flag: FileFlag;
247
- protected _buffer: Buffer;
246
+ protected _buffer: Uint8Array;
248
247
  protected _dirty: boolean;
249
248
  /**
250
249
  * Creates a file with the given path and, optionally, the given contents. Note
@@ -260,11 +259,11 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
260
259
  * contents of the file. PreloadFile will mutate this buffer. If not
261
260
  * specified, we assume it is a new file.
262
261
  */
263
- constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer);
262
+ constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Uint8Array);
264
263
  /**
265
264
  * NONSTANDARD: Get the underlying buffer for this file. !!DO NOT MUTATE!! Will mess up dirty tracking.
266
265
  */
267
- getBuffer(): Buffer;
266
+ getBuffer(): Uint8Array;
268
267
  /**
269
268
  * NONSTANDARD: Get underlying stats for this file. !!DO NOT MUTATE!!
270
269
  */
@@ -339,22 +338,22 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
339
338
  * Write buffer to the file.
340
339
  * Note that it is unsafe to use fs.write multiple times on the same file
341
340
  * without waiting for the callback.
342
- * @param [ZenFS.node.Buffer] buffer Buffer containing the data to write to
341
+ * @param [ZenFS.node.Uint8Array] buffer Uint8Array containing the data to write to
343
342
  * the file.
344
343
  * @param [Number] offset Offset in the buffer to start reading data from.
345
344
  * @param [Number] length The amount of bytes to write to the file.
346
345
  * @param [Number] position Offset from the beginning of the file where this
347
346
  * data should be written. If position is null, the data will be written at
348
347
  * the current position.
349
- * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Buffer)]
348
+ * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Uint8Array)]
350
349
  * cb The number specifies the number of bytes written into the file.
351
350
  */
352
- write(buffer: Buffer, offset: number, length: number, position: number): Promise<number>;
351
+ write(buffer: Uint8Array, offset: number, length: number, position: number): Promise<number>;
353
352
  /**
354
353
  * Write buffer to the file.
355
354
  * Note that it is unsafe to use fs.writeSync multiple times on the same file
356
355
  * without waiting for the callback.
357
- * @param [ZenFS.node.Buffer] buffer Buffer containing the data to write to
356
+ * @param [ZenFS.node.Uint8Array] buffer Uint8Array containing the data to write to
358
357
  * the file.
359
358
  * @param [Number] offset Offset in the buffer to start reading data from.
360
359
  * @param [Number] length The amount of bytes to write to the file.
@@ -363,10 +362,10 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
363
362
  * the current position.
364
363
  * @return [Number]
365
364
  */
366
- writeSync(buffer: Buffer, offset: number, length: number, position: number): number;
365
+ writeSync(buffer: Uint8Array, offset: number, length: number, position: number): number;
367
366
  /**
368
367
  * Read data from the file.
369
- * @param [ZenFS.node.Buffer] buffer The buffer that the data will be
368
+ * @param [ZenFS.node.Uint8Array] buffer The buffer that the data will be
370
369
  * written to.
371
370
  * @param [Number] offset The offset within the buffer where writing will
372
371
  * start.
@@ -374,16 +373,16 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
374
373
  * @param [Number] position An integer specifying where to begin reading from
375
374
  * in the file. If position is null, data will be read from the current file
376
375
  * position.
377
- * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Buffer)] cb The
376
+ * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Uint8Array)] cb The
378
377
  * number is the number of bytes read
379
378
  */
380
- read(buffer: Buffer, offset: number, length: number, position: number): Promise<{
379
+ read(buffer: Uint8Array, offset: number, length: number, position: number): Promise<{
381
380
  bytesRead: number;
382
- buffer: Buffer;
381
+ buffer: Uint8Array;
383
382
  }>;
384
383
  /**
385
384
  * Read data from the file.
386
- * @param [ZenFS.node.Buffer] buffer The buffer that the data will be
385
+ * @param [ZenFS.node.Uint8Array] buffer The buffer that the data will be
387
386
  * written to.
388
387
  * @param [Number] offset The offset within the buffer where writing will
389
388
  * start.
@@ -393,7 +392,7 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
393
392
  * position.
394
393
  * @return [Number]
395
394
  */
396
- readSync(buffer: Buffer, offset: number, length: number, position: number): number;
395
+ readSync(buffer: Uint8Array, offset: number, length: number, position: number): number;
397
396
  /**
398
397
  * Asynchronous `fchmod`.
399
398
  * @param [Number|String] mode
@@ -427,7 +426,7 @@ export declare class PreloadFile<T extends FileSystem> extends BaseFile {
427
426
  * Doesn't sync to anything, so it works nicely for memory-only files.
428
427
  */
429
428
  export declare class NoSyncFile<T extends FileSystem> extends PreloadFile<T> implements File {
430
- constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer);
429
+ constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Uint8Array);
431
430
  /**
432
431
  * Asynchronous sync. Doesn't do anything, simply calls the cb.
433
432
  * @param [Function(ZenFS.ApiError)] cb
package/dist/file.js CHANGED
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { ApiError, ErrorCode } from './ApiError.js';
11
11
  import { Stats } from './stats.js';
12
12
  import { getMount } from './emulation/shared.js';
13
- import { Buffer } from 'buffer';
14
13
  export var ActionType;
15
14
  (function (ActionType) {
16
15
  // Indicates that the code should not do anything.
@@ -200,7 +199,7 @@ export class BaseFile {
200
199
  }
201
200
  /**
202
201
  * An implementation of the File interface that operates on a file that is
203
- * completely in-memory. PreloadFiles are backed by a Buffer.
202
+ * completely in-memory. PreloadFiles are backed by a Uint8Array.
204
203
  *
205
204
  * This is also an abstract class, as it lacks an implementation of 'sync' and
206
205
  * 'close'. Each filesystem that wishes to use this file representation must
@@ -230,13 +229,13 @@ export class PreloadFile extends BaseFile {
230
229
  this._path = _path;
231
230
  this._flag = _flag;
232
231
  this._stat = _stat;
233
- this._buffer = contents ? contents : Buffer.alloc(0);
232
+ this._buffer = contents ? contents : new Uint8Array(0);
234
233
  // Note: This invariant is *not* maintained once the file starts getting
235
234
  // modified.
236
235
  // Note: Only actually matters if file is readable, as writeable modes may
237
236
  // truncate/append to file.
238
237
  if (this._stat.size !== this._buffer.length && this._flag.isReadable()) {
239
- throw new Error(`Invalid buffer: Buffer is ${this._buffer.length} long, yet Stats object specifies that file is ${this._stat.size} long.`);
238
+ throw new Error(`Invalid buffer: Uint8Array is ${this._buffer.length} long, yet Stats object specifies that file is ${this._stat.size} long.`);
240
239
  }
241
240
  }
242
241
  /**
@@ -359,7 +358,7 @@ export class PreloadFile extends BaseFile {
359
358
  }
360
359
  this._stat.mtimeMs = Date.now();
361
360
  if (len > this._buffer.length) {
362
- const buf = Buffer.alloc(len - this._buffer.length, 0);
361
+ const buf = new Uint8Array(len - this._buffer.length);
363
362
  // Write will set @_stat.size for us.
364
363
  this.writeSync(buf, 0, buf.length, this._buffer.length);
365
364
  if (this._flag.isSynchronous() && getMount('/').metadata.synchronous) {
@@ -369,9 +368,7 @@ export class PreloadFile extends BaseFile {
369
368
  }
370
369
  this._stat.size = len;
371
370
  // Truncate buffer to 'len'.
372
- const newBuff = Buffer.alloc(len);
373
- this._buffer.copy(newBuff, 0, 0, len);
374
- this._buffer = newBuff;
371
+ this._buffer = this._buffer.subarray(0, len);
375
372
  if (this._flag.isSynchronous() && getMount('/').metadata.synchronous) {
376
373
  this.syncSync();
377
374
  }
@@ -380,14 +377,14 @@ export class PreloadFile extends BaseFile {
380
377
  * Write buffer to the file.
381
378
  * Note that it is unsafe to use fs.write multiple times on the same file
382
379
  * without waiting for the callback.
383
- * @param [ZenFS.node.Buffer] buffer Buffer containing the data to write to
380
+ * @param [ZenFS.node.Uint8Array] buffer Uint8Array containing the data to write to
384
381
  * the file.
385
382
  * @param [Number] offset Offset in the buffer to start reading data from.
386
383
  * @param [Number] length The amount of bytes to write to the file.
387
384
  * @param [Number] position Offset from the beginning of the file where this
388
385
  * data should be written. If position is null, the data will be written at
389
386
  * the current position.
390
- * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Buffer)]
387
+ * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Uint8Array)]
391
388
  * cb The number specifies the number of bytes written into the file.
392
389
  */
393
390
  write(buffer, offset, length, position) {
@@ -399,7 +396,7 @@ export class PreloadFile extends BaseFile {
399
396
  * Write buffer to the file.
400
397
  * Note that it is unsafe to use fs.writeSync multiple times on the same file
401
398
  * without waiting for the callback.
402
- * @param [ZenFS.node.Buffer] buffer Buffer containing the data to write to
399
+ * @param [ZenFS.node.Uint8Array] buffer Uint8Array containing the data to write to
403
400
  * the file.
404
401
  * @param [Number] offset Offset in the buffer to start reading data from.
405
402
  * @param [Number] length The amount of bytes to write to the file.
@@ -421,12 +418,13 @@ export class PreloadFile extends BaseFile {
421
418
  this._stat.size = endFp;
422
419
  if (endFp > this._buffer.length) {
423
420
  // Extend the buffer!
424
- const newBuff = Buffer.alloc(endFp);
425
- this._buffer.copy(newBuff);
426
- this._buffer = newBuff;
421
+ const newBuffer = new Uint8Array(endFp);
422
+ newBuffer.set(this._buffer);
423
+ this._buffer = newBuffer;
427
424
  }
428
425
  }
429
- const len = buffer.copy(this._buffer, position, offset, offset + length);
426
+ this._buffer.set(buffer.slice(offset, offset + length), position);
427
+ const len = this._buffer.length;
430
428
  this._stat.mtimeMs = Date.now();
431
429
  if (this._flag.isSynchronous()) {
432
430
  this.syncSync();
@@ -437,7 +435,7 @@ export class PreloadFile extends BaseFile {
437
435
  }
438
436
  /**
439
437
  * Read data from the file.
440
- * @param [ZenFS.node.Buffer] buffer The buffer that the data will be
438
+ * @param [ZenFS.node.Uint8Array] buffer The buffer that the data will be
441
439
  * written to.
442
440
  * @param [Number] offset The offset within the buffer where writing will
443
441
  * start.
@@ -445,7 +443,7 @@ export class PreloadFile extends BaseFile {
445
443
  * @param [Number] position An integer specifying where to begin reading from
446
444
  * in the file. If position is null, data will be read from the current file
447
445
  * position.
448
- * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Buffer)] cb The
446
+ * @param [Function(ZenFS.ApiError, Number, ZenFS.node.Uint8Array)] cb The
449
447
  * number is the number of bytes read
450
448
  */
451
449
  read(buffer, offset, length, position) {
@@ -455,7 +453,7 @@ export class PreloadFile extends BaseFile {
455
453
  }
456
454
  /**
457
455
  * Read data from the file.
458
- * @param [ZenFS.node.Buffer] buffer The buffer that the data will be
456
+ * @param [ZenFS.node.Uint8Array] buffer The buffer that the data will be
459
457
  * written to.
460
458
  * @param [Number] offset The offset within the buffer where writing will
461
459
  * start.
@@ -476,10 +474,10 @@ export class PreloadFile extends BaseFile {
476
474
  if (endRead > this._stat.size) {
477
475
  length = this._stat.size - position;
478
476
  }
479
- const rv = this._buffer.copy(buffer, offset, position, position + length);
477
+ this._buffer.set(buffer.slice(offset, offset + length), position);
480
478
  this._stat.atimeMs = Date.now();
481
479
  this._pos = position + length;
482
- return rv;
480
+ return this._buffer.length;
483
481
  }
484
482
  /**
485
483
  * Asynchronous `fchmod`.
@@ -6,7 +6,7 @@ import { Cred } from './cred.js';
6
6
  export type BFSOneArgCallback = (e?: ApiError) => unknown;
7
7
  export type BFSCallback<T> = (e?: ApiError, rv?: T) => unknown;
8
8
  export type BFSThreeArgCallback<T, U> = (e?: ApiError, arg1?: T, arg2?: U) => unknown;
9
- export type FileContents = Buffer | string;
9
+ export type FileContents = Uint8Array | string;
10
10
  /**
11
11
  * Metadata about a FileSystem
12
12
  */
@@ -181,7 +181,7 @@ export declare abstract class FileSystem {
181
181
  * Asynchronously reads the entire contents of a file.
182
182
  * @param encoding If non-null, the file's contents should be decoded
183
183
  * into a string using that encoding. Otherwise, if encoding is null, fetch
184
- * the file's contents as a Buffer.
184
+ * the file's contents as a Uint8Array.
185
185
  * If no encoding is specified, then the raw buffer is returned.
186
186
  */
187
187
  abstract readFile(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): Promise<FileContents>;
@@ -189,7 +189,7 @@ export declare abstract class FileSystem {
189
189
  * Synchronously reads the entire contents of a file.
190
190
  * @param encoding If non-null, the file's contents should be decoded
191
191
  * into a string using that encoding. Otherwise, if encoding is null, fetch
192
- * the file's contents as a Buffer.
192
+ * the file's contents as a Uint8Array.
193
193
  */
194
194
  abstract readFileSync(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): FileContents;
195
195
  /**
@@ -12,8 +12,8 @@ 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';
16
- import { Buffer } from 'buffer';
15
+ import * as path from './emulation/path.js';
16
+ import { decode, encode } from './utils.js';
17
17
  /**
18
18
  * Structure for a filesystem. **All** ZenFS FileSystems must implement
19
19
  * this.
@@ -324,13 +324,13 @@ export class BaseFileSystem extends FileSystem {
324
324
  try {
325
325
  const stat = yield fd.stat();
326
326
  // Allocate buffer.
327
- const buf = Buffer.alloc(stat.size);
327
+ const buf = new Uint8Array(stat.size);
328
328
  yield fd.read(buf, 0, stat.size, 0);
329
329
  yield fd.close();
330
330
  if (encoding === null) {
331
331
  return buf;
332
332
  }
333
- return buf.toString(encoding);
333
+ return decode(buf);
334
334
  }
335
335
  finally {
336
336
  yield fd.close();
@@ -343,13 +343,13 @@ export class BaseFileSystem extends FileSystem {
343
343
  try {
344
344
  const stat = fd.statSync();
345
345
  // Allocate buffer.
346
- const buf = Buffer.alloc(stat.size);
346
+ const buf = new Uint8Array(stat.size);
347
347
  fd.readSync(buf, 0, stat.size, 0);
348
348
  fd.closeSync();
349
349
  if (encoding === null) {
350
350
  return buf;
351
351
  }
352
- return buf.toString(encoding);
352
+ return decode(buf);
353
353
  }
354
354
  finally {
355
355
  fd.closeSync();
@@ -361,7 +361,7 @@ export class BaseFileSystem extends FileSystem {
361
361
  const fd = yield this.open(fname, flag, mode, cred);
362
362
  try {
363
363
  if (typeof data === 'string') {
364
- data = Buffer.from(data, encoding);
364
+ data = encode(data);
365
365
  }
366
366
  // Write into file.
367
367
  yield fd.write(data, 0, data.length, 0);
@@ -376,7 +376,7 @@ export class BaseFileSystem extends FileSystem {
376
376
  const fd = this.openSync(fname, flag, mode, cred);
377
377
  try {
378
378
  if (typeof data === 'string') {
379
- data = Buffer.from(data, encoding);
379
+ data = encode(data);
380
380
  }
381
381
  // Write into file.
382
382
  fd.writeSync(data, 0, data.length, 0);
@@ -390,7 +390,7 @@ export class BaseFileSystem extends FileSystem {
390
390
  const fd = yield this.open(fname, flag, mode, cred);
391
391
  try {
392
392
  if (typeof data === 'string') {
393
- data = Buffer.from(data, encoding);
393
+ data = encode(data);
394
394
  }
395
395
  yield fd.write(data, 0, data.length, null);
396
396
  }
@@ -403,7 +403,7 @@ export class BaseFileSystem extends FileSystem {
403
403
  const fd = this.openSync(fname, flag, mode, cred);
404
404
  try {
405
405
  if (typeof data === 'string') {
406
- data = Buffer.from(data, encoding);
406
+ data = encode(data);
407
407
  }
408
408
  fd.writeSync(data, 0, data.length, null);
409
409
  }
package/dist/inode.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Stats } from './stats.js';
3
2
  /**
4
3
  * Generic inode definition that can easily be serialized.
@@ -15,7 +14,7 @@ export default class Inode {
15
14
  /**
16
15
  * Converts the buffer into an Inode.
17
16
  */
18
- static fromBuffer(buffer: Buffer): Inode;
17
+ static Deserialize(data: ArrayBufferLike | ArrayBufferView): Inode;
19
18
  constructor(id: string, size: number, mode: number, atime: number, mtime: number, ctime: number, uid: number, gid: number);
20
19
  /**
21
20
  * Handy function that converts the Inode to a Node Stats object.
@@ -28,7 +27,7 @@ export default class Inode {
28
27
  /**
29
28
  * Writes the inode into the start of the buffer.
30
29
  */
31
- toBuffer(buff?: Buffer): Buffer;
30
+ serialize(data?: ArrayBufferLike | ArrayBufferView): Uint8Array;
32
31
  /**
33
32
  * Updates the Inode using information from the stats object. Used by file
34
33
  * systems at sync time, e.g.:
package/dist/inode.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Stats, FileType } from './stats.js';
2
- import { Buffer } from 'buffer';
2
+ import { decode, encode } from './utils.js';
3
3
  /**
4
4
  * Generic inode definition that can easily be serialized.
5
5
  */
@@ -7,11 +7,9 @@ export default class Inode {
7
7
  /**
8
8
  * Converts the buffer into an Inode.
9
9
  */
10
- static fromBuffer(buffer) {
11
- if (buffer === undefined) {
12
- throw new Error('NO');
13
- }
14
- return new Inode(buffer.toString('ascii', 38), buffer.readUInt32LE(0), buffer.readUInt16LE(4), buffer.readDoubleLE(6), buffer.readDoubleLE(14), buffer.readDoubleLE(22), buffer.readUInt32LE(30), buffer.readUInt32LE(34));
10
+ static Deserialize(data) {
11
+ const view = new DataView('buffer' in data ? data.buffer : data);
12
+ return new Inode(decode(view.buffer.slice(38)), view.getUint32(0, true), view.getUint16(4, true), view.getFloat64(6, true), view.getFloat64(14, true), view.getFloat64(22, true), view.getUint32(30, true), view.getUint32(34, true));
15
13
  }
16
14
  constructor(id, size, mode, atime, mtime, ctime, uid, gid) {
17
15
  this.id = id;
@@ -33,22 +31,24 @@ export default class Inode {
33
31
  * Get the size of this Inode, in bytes.
34
32
  */
35
33
  getSize() {
36
- // ASSUMPTION: ID is ASCII (1 byte per char).
34
+ // ASSUMPTION: ID is 1 byte per char.
37
35
  return 38 + this.id.length;
38
36
  }
39
37
  /**
40
38
  * Writes the inode into the start of the buffer.
41
39
  */
42
- toBuffer(buff = Buffer.alloc(this.getSize())) {
43
- buff.writeUInt32LE(this.size, 0);
44
- buff.writeUInt16LE(this.mode, 4);
45
- buff.writeDoubleLE(this.atime, 6);
46
- buff.writeDoubleLE(this.mtime, 14);
47
- buff.writeDoubleLE(this.ctime, 22);
48
- buff.writeUInt32LE(this.uid, 30);
49
- buff.writeUInt32LE(this.gid, 34);
50
- buff.write(this.id, 38, this.id.length, 'ascii');
51
- return buff;
40
+ serialize(data = new Uint8Array(this.getSize())) {
41
+ const view = new DataView('buffer' in data ? data.buffer : data);
42
+ view.setUint32(0, this.size, true);
43
+ view.setUint16(4, this.mode, true);
44
+ view.setFloat64(6, this.atime, true);
45
+ view.setFloat64(14, this.mtime, true);
46
+ view.setFloat64(22, this.ctime, true);
47
+ view.setUint32(30, this.uid, true);
48
+ view.setUint32(34, this.gid, true);
49
+ const buffer = new Uint8Array(view.buffer);
50
+ buffer.set(encode(this.id), 38);
51
+ return buffer;
52
52
  }
53
53
  /**
54
54
  * Updates the Inode using information from the stats object. Used by file
package/dist/stats.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
- /// <reference types="node" resolution-mode="require"/>
3
2
  import type { StatsBase } from 'fs';
4
3
  import { Cred } from './cred.js';
5
4
  /**
@@ -18,7 +17,7 @@ export declare enum FileType {
18
17
  * @see http://man7.org/linux/man-pages/man2/stat.2.html
19
18
  */
20
19
  export declare class Stats implements StatsBase<number> {
21
- static fromBuffer(buffer: Buffer): Stats;
20
+ static Deserialize(data: ArrayBufferLike | ArrayBufferView): Stats;
22
21
  /**
23
22
  * Clones the stats object.
24
23
  */
@@ -32,7 +31,7 @@ export declare class Stats implements StatsBase<number> {
32
31
  blksize: number;
33
32
  uid: number;
34
33
  gid: number;
35
- fileData: Buffer | null;
34
+ fileData: Uint8Array | null;
36
35
  atimeMs: number;
37
36
  mtimeMs: number;
38
37
  ctimeMs: number;
@@ -56,7 +55,7 @@ export declare class Stats implements StatsBase<number> {
56
55
  * @param birthtimeMs time of file creation, in milliseconds since epoch
57
56
  */
58
57
  constructor(itemType: FileType, size: number, mode?: number, atimeMs?: number, mtimeMs?: number, ctimeMs?: number, uid?: number, gid?: number, birthtimeMs?: number);
59
- toBuffer(): Buffer;
58
+ serialize(): Uint8Array;
60
59
  /**
61
60
  * @return [Boolean] True if this item is a file.
62
61
  */
package/dist/stats.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Cred } from './cred.js';
2
- import { Buffer } from 'buffer';
3
2
  import { S_IFDIR, S_IFLNK, S_IFMT, S_IFREG } from './emulation/constants.js';
4
3
  /**
5
4
  * Indicates the type of the given file. Applied to 'mode'.
@@ -18,8 +17,9 @@ export var FileType;
18
17
  * @see http://man7.org/linux/man-pages/man2/stat.2.html
19
18
  */
20
19
  export class Stats {
21
- static fromBuffer(buffer) {
22
- const size = buffer.readUInt32LE(0), mode = buffer.readUInt32LE(4), atime = buffer.readDoubleLE(8), mtime = buffer.readDoubleLE(16), ctime = buffer.readDoubleLE(24), uid = buffer.readUInt32LE(32), gid = buffer.readUInt32LE(36);
20
+ static Deserialize(data) {
21
+ const view = new DataView('buffer' in data ? data.buffer : data);
22
+ const size = view.getUint32(0, true), mode = view.getUint32(4, true), atime = view.getFloat64(8, true), mtime = view.getFloat64(16, true), ctime = view.getFloat64(24, true), uid = view.getUint32(32, true), gid = view.getUint32(36, true);
23
23
  return new Stats(mode & S_IFMT, size, mode & ~S_IFMT, atime, mtime, ctime, uid, gid);
24
24
  }
25
25
  /**
@@ -125,16 +125,16 @@ export class Stats {
125
125
  this.mode |= itemType;
126
126
  }
127
127
  }
128
- toBuffer() {
129
- const buffer = Buffer.alloc(32);
130
- buffer.writeUInt32LE(this.size, 0);
131
- buffer.writeUInt32LE(this.mode, 4);
132
- buffer.writeDoubleLE(this.atime.getTime(), 8);
133
- buffer.writeDoubleLE(this.mtime.getTime(), 16);
134
- buffer.writeDoubleLE(this.ctime.getTime(), 24);
135
- buffer.writeUInt32LE(this.uid, 32);
136
- buffer.writeUInt32LE(this.gid, 36);
137
- return buffer;
128
+ serialize() {
129
+ const data = new Uint8Array(32), view = new DataView(data.buffer);
130
+ view.setUint32(0, this.size, true);
131
+ view.setUint32(4, this.mode, true);
132
+ view.setFloat64(8, this.atime.getTime(), true);
133
+ view.setFloat64(16, this.mtime.getTime(), true);
134
+ view.setFloat64(24, this.ctime.getTime(), true);
135
+ view.setUint32(32, this.uid, true);
136
+ view.setUint32(36, this.gid, true);
137
+ return data;
138
138
  }
139
139
  /**
140
140
  * @return [Boolean] True if this item is a file.
package/dist/utils.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
2
3
  /**
3
4
  * Grab bag of utility functions used across the code.
4
5
  */
5
6
  import { FileSystem } from './filesystem.js';
6
7
  import { Cred } from './cred.js';
7
8
  import type { BaseBackendConstructor } from './backends/backend.js';
9
+ import type { TextEncoder as TextEncoderType, TextDecoder as TextDecoderType } from 'node:util';
8
10
  /**
9
11
  * Synchronous recursive makedir.
10
12
  * @internal
@@ -30,11 +32,14 @@ export declare const setImmediate: typeof globalThis.setImmediate | ((cb: any) =
30
32
  * @internal
31
33
  */
32
34
  export declare const ROOT_NODE_ID: string;
33
- /**
34
- * Returns an empty directory node.
35
- * @internal
36
- */
37
- export declare function getEmptyDirNode(): Buffer;
35
+ declare global {
36
+ const TextEncoder: typeof TextEncoderType;
37
+ const TextDecoder: typeof TextDecoderType;
38
+ }
39
+ export declare const encode: (input?: string) => Uint8Array;
40
+ export declare const decode: (input?: ArrayBuffer | NodeJS.ArrayBufferView, options?: {
41
+ stream?: boolean;
42
+ }) => string;
38
43
  /**
39
44
  * Generates a random ID.
40
45
  * @internal
package/dist/utils.js CHANGED
@@ -8,8 +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';
12
- import { Buffer } from 'buffer';
11
+ import * as path from './emulation/path.js';
13
12
  /**
14
13
  * Synchronous recursive makedir.
15
14
  * @internal
@@ -213,13 +212,8 @@ export const setImmediate = typeof globalThis.setImmediate == 'function' ? globa
213
212
  * @internal
214
213
  */
215
214
  export const ROOT_NODE_ID = '/';
216
- /**
217
- * Returns an empty directory node.
218
- * @internal
219
- */
220
- export function getEmptyDirNode() {
221
- return Buffer.from('{}');
222
- }
215
+ export const encode = new TextEncoder().encode;
216
+ export const decode = new TextDecoder().decode;
223
217
  /**
224
218
  * Generates a random ID.
225
219
  * @internal