@zenfs/core 0.0.8 → 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.
@@ -44,7 +44,7 @@ export declare class ApiError extends Error implements NodeJS.ErrnoException {
44
44
  /**
45
45
  * Creates an ApiError object from a buffer.
46
46
  */
47
- static fromBuffer(buffer: Buffer, i?: number): ApiError;
47
+ static Derserialize(data: ArrayBufferLike | ArrayBufferView, i?: number): ApiError;
48
48
  static FileError(code: ErrorCode, p: string): ApiError;
49
49
  static EACCES(path: string): ApiError;
50
50
  static ENOENT(path: string): ApiError;
@@ -77,7 +77,7 @@ export declare class ApiError extends Error implements NodeJS.ErrnoException {
77
77
  /**
78
78
  * Writes the API error into a buffer.
79
79
  */
80
- writeToBuffer(buffer?: Buffer, i?: number): Buffer;
80
+ serialize(data?: ArrayBufferLike | ArrayBufferView, i?: number): Uint8Array;
81
81
  /**
82
82
  * The size of the API error in buffer-form in bytes.
83
83
  */
package/dist/ApiError.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Buffer } from 'buffer';
1
+ import { decode, encode } from './utils.js';
2
2
  /**
3
3
  * Standard libc error codes. More will be added to this enum and ErrorStrings as they are
4
4
  * needed.
@@ -56,8 +56,10 @@ export class ApiError extends Error {
56
56
  /**
57
57
  * Creates an ApiError object from a buffer.
58
58
  */
59
- static fromBuffer(buffer, i = 0) {
60
- return ApiError.fromJSON(JSON.parse(buffer.toString('utf8', i + 4, i + 4 + buffer.readUInt32LE(i))));
59
+ static Derserialize(data, i = 0) {
60
+ const view = new DataView('buffer' in data ? data.buffer : data);
61
+ const dataText = decode(view.buffer.slice(i + 4, i + 4 + view.getUint32(i, true)));
62
+ return ApiError.fromJSON(JSON.parse(dataText));
61
63
  }
62
64
  static FileError(code, p) {
63
65
  return new ApiError(code, ErrorStrings[code], p);
@@ -120,9 +122,11 @@ export class ApiError extends Error {
120
122
  /**
121
123
  * Writes the API error into a buffer.
122
124
  */
123
- writeToBuffer(buffer = Buffer.alloc(this.bufferSize()), i = 0) {
124
- const bytesWritten = buffer.write(JSON.stringify(this.toJSON()), i + 4);
125
- buffer.writeUInt32LE(bytesWritten, i);
125
+ serialize(data = new Uint8Array(this.bufferSize()), i = 0) {
126
+ const view = new DataView('buffer' in data ? data.buffer : data), buffer = new Uint8Array(view.buffer);
127
+ const newData = encode(JSON.stringify(this.toJSON()));
128
+ buffer.set(newData);
129
+ view.setUint32(i, newData.byteLength, true);
126
130
  return buffer;
127
131
  }
128
132
  /**
@@ -130,6 +134,6 @@ export class ApiError extends Error {
130
134
  */
131
135
  bufferSize() {
132
136
  // 4 bytes for string length.
133
- return 4 + Buffer.byteLength(JSON.stringify(this.toJSON()));
137
+ return 4 + JSON.stringify(this.toJSON()).length;
134
138
  }
135
139
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Cred } from '../cred.js';
3
2
  import { PreloadFile, File, FileFlag } from '../file.js';
4
3
  import { BaseFileSystem } from '../filesystem.js';
@@ -33,7 +32,7 @@ export interface AsyncKeyValueROTransaction {
33
32
  * Retrieves the data at the given key.
34
33
  * @param key The key to look under for data.
35
34
  */
36
- get(key: string): Promise<Buffer>;
35
+ get(key: string): Promise<Uint8Array>;
37
36
  }
38
37
  /**
39
38
  * Represents an asynchronous read-write transaction.
@@ -47,7 +46,7 @@ export interface AsyncKeyValueRWTransaction extends AsyncKeyValueROTransaction {
47
46
  * @param overwrite If 'true', overwrite any existing data. If 'false',
48
47
  * avoids writing the data if the key exists.
49
48
  */
50
- put(key: string, data: Buffer, overwrite: boolean): Promise<boolean>;
49
+ put(key: string, data: Uint8Array, overwrite: boolean): Promise<boolean>;
51
50
  /**
52
51
  * Deletes the data at the given key.
53
52
  * @param key The key to delete from the store.
@@ -63,7 +62,7 @@ export interface AsyncKeyValueRWTransaction extends AsyncKeyValueROTransaction {
63
62
  abort(): Promise<void>;
64
63
  }
65
64
  export declare class AsyncKeyValueFile extends PreloadFile<AsyncKeyValueFileSystem> implements File {
66
- constructor(_fs: AsyncKeyValueFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer);
65
+ constructor(_fs: AsyncKeyValueFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Uint8Array);
67
66
  sync(): Promise<void>;
68
67
  close(): Promise<void>;
69
68
  }
@@ -104,7 +103,7 @@ export declare class AsyncKeyValueFileSystem extends BaseFileSystem {
104
103
  readdir(p: string, cred: Cred): Promise<string[]>;
105
104
  chmod(p: string, mode: number, cred: Cred): Promise<void>;
106
105
  chown(p: string, new_uid: number, new_gid: number, cred: Cred): Promise<void>;
107
- _sync(p: string, data: Buffer, stats: Stats): Promise<void>;
106
+ _sync(p: string, data: Uint8Array, stats: Stats): Promise<void>;
108
107
  /**
109
108
  * Checks if the root directory exists. Creates it if it doesn't.
110
109
  */
@@ -14,7 +14,7 @@ import { PreloadFile, FileFlag } from '../file.js';
14
14
  import { BaseFileSystem } from '../filesystem.js';
15
15
  import Inode from '../inode.js';
16
16
  import { FileType } from '../stats.js';
17
- import { ROOT_NODE_ID, randomUUID, getEmptyDirNode } from '../utils.js';
17
+ import { ROOT_NODE_ID, randomUUID, encode } from '../utils.js';
18
18
  class LRUNode {
19
19
  constructor(key, value) {
20
20
  this.key = key;
@@ -255,8 +255,8 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
255
255
  newDirList[newName] = nodeId;
256
256
  // Commit the two changed directory listings.
257
257
  try {
258
- yield tx.put(oldDirNode.id, Buffer.from(JSON.stringify(oldDirList)), true);
259
- yield tx.put(newDirNode.id, Buffer.from(JSON.stringify(newDirList)), true);
258
+ yield tx.put(oldDirNode.id, encode(JSON.stringify(oldDirList)), true);
259
+ yield tx.put(newDirNode.id, encode(JSON.stringify(newDirList)), true);
260
260
  }
261
261
  catch (e) {
262
262
  yield tx.abort();
@@ -284,7 +284,7 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
284
284
  }
285
285
  createFile(p, flag, mode, cred) {
286
286
  return __awaiter(this, void 0, void 0, function* () {
287
- const tx = this.store.beginTransaction('readwrite'), data = Buffer.alloc(0), newFile = yield this.commitNewFile(tx, p, FileType.FILE, mode, cred, data);
287
+ const tx = this.store.beginTransaction('readwrite'), data = new Uint8Array(0), newFile = yield this.commitNewFile(tx, p, FileType.FILE, mode, cred, data);
288
288
  // Open the file.
289
289
  return new AsyncKeyValueFile(this, p, flag, newFile.toStats(), data);
290
290
  });
@@ -318,7 +318,7 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
318
318
  }
319
319
  mkdir(p, mode, cred) {
320
320
  return __awaiter(this, void 0, void 0, function* () {
321
- const tx = this.store.beginTransaction('readwrite'), data = Buffer.from('{}');
321
+ const tx = this.store.beginTransaction('readwrite'), data = encode('{}');
322
322
  yield this.commitNewFile(tx, p, FileType.DIRECTORY, mode, cred, data);
323
323
  });
324
324
  }
@@ -356,7 +356,7 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
356
356
  yield tx.put(fileInode.id, data, true);
357
357
  // Sync metadata.
358
358
  if (inodeChanged) {
359
- yield tx.put(fileInodeId, fileInode.toBuffer(), true);
359
+ yield tx.put(fileInodeId, fileInode.serialize(), true);
360
360
  }
361
361
  }
362
362
  catch (e) {
@@ -379,8 +379,8 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
379
379
  dirInode = new Inode(randomUUID(), 4096, 511 | FileType.DIRECTORY, currTime, currTime, currTime, 0, 0);
380
380
  // If the root doesn't exist, the first random ID shouldn't exist,
381
381
  // either.
382
- yield tx.put(dirInode.id, getEmptyDirNode(), false);
383
- yield tx.put(ROOT_NODE_ID, dirInode.toBuffer(), false);
382
+ yield tx.put(dirInode.id, encode('{}'), false);
383
+ yield tx.put(ROOT_NODE_ID, dirInode.serialize(), false);
384
384
  yield tx.commit();
385
385
  }
386
386
  });
@@ -469,7 +469,7 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
469
469
  if (!data) {
470
470
  throw ApiError.ENOENT(p);
471
471
  }
472
- return Inode.fromBuffer(data);
472
+ return Inode.Deserialize(data);
473
473
  });
474
474
  }
475
475
  /**
@@ -553,10 +553,10 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
553
553
  const dataId = yield this.addNewNode(tx, data);
554
554
  const fileNode = new Inode(dataId, data.length, mode | type, currTime, currTime, currTime, cred.uid, cred.gid);
555
555
  // Commit file node.
556
- const fileNodeId = yield this.addNewNode(tx, fileNode.toBuffer());
556
+ const fileNodeId = yield this.addNewNode(tx, fileNode.serialize());
557
557
  // Update and commit parent directory listing.
558
558
  dirListing[fname] = fileNodeId;
559
- yield tx.put(parentNode.id, Buffer.from(JSON.stringify(dirListing)), true);
559
+ yield tx.put(parentNode.id, encode(JSON.stringify(dirListing)), true);
560
560
  yield tx.commit();
561
561
  return fileNode;
562
562
  }
@@ -607,7 +607,7 @@ export class AsyncKeyValueFileSystem extends BaseFileSystem {
607
607
  // Delete node.
608
608
  yield tx.del(fileNodeId);
609
609
  // Update directory listing.
610
- yield tx.put(parentNode.id, Buffer.from(JSON.stringify(parentListing)), true);
610
+ yield tx.put(parentNode.id, encode(JSON.stringify(parentListing)), true);
611
611
  }
612
612
  catch (e) {
613
613
  yield tx.abort();
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { SyncKeyValueStore, SimpleSyncStore, SyncKeyValueRWTransaction, SyncKeyValueFileSystem } from './SyncStore.js';
3
2
  import { type BackendOptions } from './backend.js';
4
3
  /**
@@ -9,8 +8,8 @@ export declare class InMemoryStore implements SyncKeyValueStore, SimpleSyncStore
9
8
  name(): string;
10
9
  clear(): void;
11
10
  beginTransaction(type: string): SyncKeyValueRWTransaction;
12
- get(key: string): Buffer;
13
- put(key: string, data: Buffer, overwrite: boolean): boolean;
11
+ get(key: string): Uint8Array;
12
+ put(key: string, data: Uint8Array, overwrite: boolean): boolean;
14
13
  del(key: string): void;
15
14
  }
16
15
  /**
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { Cred } from '../cred.js';
3
2
  import { FileFlag, PreloadFile } from '../file.js';
4
3
  import { SynchronousFileSystem } from '../filesystem.js';
@@ -35,7 +34,7 @@ export interface SyncKeyValueROTransaction {
35
34
  * @param key The key to look under for data.
36
35
  * @return The data stored under the key, or undefined if not present.
37
36
  */
38
- get(key: string): Buffer | undefined;
37
+ get(key: string): Uint8Array | undefined;
39
38
  }
40
39
  /**
41
40
  * A read-write transaction for a synchronous key value store.
@@ -49,7 +48,7 @@ export interface SyncKeyValueRWTransaction extends SyncKeyValueROTransaction {
49
48
  * avoids storing the data if the key exists.
50
49
  * @return True if storage succeeded, false otherwise.
51
50
  */
52
- put(key: string, data: Buffer, overwrite: boolean): boolean;
51
+ put(key: string, data: Uint8Array, overwrite: boolean): boolean;
53
52
  /**
54
53
  * Deletes the data at the given key.
55
54
  * @param key The key to delete from the store.
@@ -69,8 +68,8 @@ export interface SyncKeyValueRWTransaction extends SyncKeyValueROTransaction {
69
68
  * support for transactions and such.
70
69
  */
71
70
  export interface SimpleSyncStore {
72
- get(key: string): Buffer | undefined;
73
- put(key: string, data: Buffer, overwrite: boolean): boolean;
71
+ get(key: string): Uint8Array | undefined;
72
+ put(key: string, data: Uint8Array, overwrite: boolean): boolean;
74
73
  del(key: string): void;
75
74
  }
76
75
  /**
@@ -88,8 +87,8 @@ export declare class SimpleSyncRWTransaction implements SyncKeyValueRWTransactio
88
87
  */
89
88
  private modifiedKeys;
90
89
  constructor(store: SimpleSyncStore);
91
- get(key: string): Buffer | undefined;
92
- put(key: string, data: Buffer, overwrite: boolean): boolean;
90
+ get(key: string): Uint8Array | undefined;
91
+ put(key: string, data: Uint8Array, overwrite: boolean): boolean;
93
92
  del(key: string): void;
94
93
  commit(): void;
95
94
  abort(): void;
@@ -127,7 +126,7 @@ export interface SyncKeyValueFileSystemOptions {
127
126
  supportLinks?: boolean;
128
127
  }
129
128
  export declare class SyncKeyValueFile extends PreloadFile<SyncKeyValueFileSystem> {
130
- constructor(_fs: SyncKeyValueFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer);
129
+ constructor(_fs: SyncKeyValueFileSystem, _path: string, _flag: FileFlag, _stat: Stats, contents?: Uint8Array);
131
130
  syncSync(): void;
132
131
  closeSync(): void;
133
132
  }
@@ -164,7 +163,7 @@ export declare class SyncKeyValueFileSystem extends SynchronousFileSystem {
164
163
  readdirSync(p: string, cred: Cred): string[];
165
164
  chmodSync(p: string, mode: number, cred: Cred): void;
166
165
  chownSync(p: string, new_uid: number, new_gid: number, cred: Cred): void;
167
- _syncSync(p: string, data: Buffer, stats: Stats): void;
166
+ _syncSync(p: string, data: Uint8Array, stats: Stats): void;
168
167
  /**
169
168
  * Checks if the root directory exists. Creates it if it doesn't.
170
169
  */
@@ -5,7 +5,7 @@ import { FileFlag, PreloadFile } from '../file.js';
5
5
  import { SynchronousFileSystem } from '../filesystem.js';
6
6
  import Inode from '../inode.js';
7
7
  import { FileType } from '../stats.js';
8
- import { randomUUID, getEmptyDirNode, ROOT_NODE_ID } from '../utils.js';
8
+ import { encode, randomUUID, ROOT_NODE_ID } from '../utils.js';
9
9
  /**
10
10
  * A simple RW transaction for simple synchronous key-value stores.
11
11
  */
@@ -194,8 +194,8 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
194
194
  newDirList[newName] = nodeId;
195
195
  // Commit the two changed directory listings.
196
196
  try {
197
- tx.put(oldDirNode.id, Buffer.from(JSON.stringify(oldDirList)), true);
198
- tx.put(newDirNode.id, Buffer.from(JSON.stringify(newDirList)), true);
197
+ tx.put(oldDirNode.id, encode(JSON.stringify(oldDirList)), true);
198
+ tx.put(newDirNode.id, encode(JSON.stringify(newDirList)), true);
199
199
  }
200
200
  catch (e) {
201
201
  tx.abort();
@@ -212,7 +212,7 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
212
212
  return stats;
213
213
  }
214
214
  createFileSync(p, flag, mode, cred) {
215
- const tx = this.store.beginTransaction('readwrite'), data = Buffer.alloc(0), newFile = this.commitNewFile(tx, p, FileType.FILE, mode, cred, data);
215
+ const tx = this.store.beginTransaction('readwrite'), data = new Uint8Array(0), newFile = this.commitNewFile(tx, p, FileType.FILE, mode, cred, data);
216
216
  // Open the file.
217
217
  return new SyncKeyValueFile(this, p, flag, newFile.toStats(), data);
218
218
  }
@@ -239,7 +239,7 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
239
239
  }
240
240
  }
241
241
  mkdirSync(p, mode, cred) {
242
- const tx = this.store.beginTransaction('readwrite'), data = Buffer.from('{}');
242
+ const tx = this.store.beginTransaction('readwrite'), data = encode('{}');
243
243
  this.commitNewFile(tx, p, FileType.DIRECTORY, mode, cred, data);
244
244
  }
245
245
  readdirSync(p, cred) {
@@ -269,7 +269,7 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
269
269
  tx.put(fileInode.id, data, true);
270
270
  // Sync metadata.
271
271
  if (inodeChanged) {
272
- tx.put(fileInodeId, fileInode.toBuffer(), true);
272
+ tx.put(fileInodeId, fileInode.serialize(), true);
273
273
  }
274
274
  }
275
275
  catch (e) {
@@ -290,8 +290,8 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
290
290
  dirInode = new Inode(randomUUID(), 4096, 511 | FileType.DIRECTORY, currTime, currTime, currTime, 0, 0);
291
291
  // If the root doesn't exist, the first random ID shouldn't exist,
292
292
  // either.
293
- tx.put(dirInode.id, getEmptyDirNode(), false);
294
- tx.put(ROOT_NODE_ID, dirInode.toBuffer(), false);
293
+ tx.put(dirInode.id, encode('{}'), false);
294
+ tx.put(ROOT_NODE_ID, dirInode.serialize(), false);
295
295
  tx.commit();
296
296
  }
297
297
  }
@@ -353,7 +353,7 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
353
353
  if (inode === undefined) {
354
354
  throw ApiError.ENOENT(p);
355
355
  }
356
- return Inode.fromBuffer(inode);
356
+ return Inode.Deserialize(inode);
357
357
  }
358
358
  /**
359
359
  * Given the Inode of a directory, retrieves the corresponding directory
@@ -421,10 +421,10 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
421
421
  const dataId = this.addNewNode(tx, data);
422
422
  fileNode = new Inode(dataId, data.length, mode | type, currTime, currTime, currTime, cred.uid, cred.gid);
423
423
  // Commit file node.
424
- const fileNodeId = this.addNewNode(tx, fileNode.toBuffer());
424
+ const fileNodeId = this.addNewNode(tx, fileNode.serialize());
425
425
  // Update and commit parent directory listing.
426
426
  dirListing[fname] = fileNodeId;
427
- tx.put(parentNode.id, Buffer.from(JSON.stringify(dirListing)), true);
427
+ tx.put(parentNode.id, encode(JSON.stringify(dirListing)), true);
428
428
  }
429
429
  catch (e) {
430
430
  tx.abort();
@@ -464,7 +464,7 @@ export class SyncKeyValueFileSystem extends SynchronousFileSystem {
464
464
  // Delete node.
465
465
  tx.del(fileNodeId);
466
466
  // Update directory listing.
467
- tx.put(parentNode.id, Buffer.from(JSON.stringify(parentListing)), true);
467
+ tx.put(parentNode.id, encode(JSON.stringify(parentListing)), true);
468
468
  }
469
469
  catch (e) {
470
470
  tx.abort();