@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.
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
@@ -9,7 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import { ErrorCode, ApiError } from './ApiError.js';
11
11
  import * as path from './emulation/path.js';
12
- import { Buffer } from 'buffer';
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "A filesystem in your browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist",
@@ -44,12 +44,10 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@jest/globals": "^29.5.0",
47
- "@types/archiver": "~2.1.2",
48
47
  "@types/jest": "^29.5.1",
49
48
  "@types/node": "^14.18.62",
50
49
  "@typescript-eslint/eslint-plugin": "^5.55.0",
51
50
  "@typescript-eslint/parser": "^5.55.0",
52
- "archiver": "~2.1.1",
53
51
  "buffer": "~5.1.0",
54
52
  "cross-env": "^7.0.3",
55
53
  "esbuild": "^0.17.18",