@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.
@@ -90,10 +90,10 @@ export declare function open(path: string, flag: string, mode: number | string,
90
90
  * @option options [String] flag Defaults to `'r'`.
91
91
  * @param callback If no encoding is specified, then the raw buffer is returned.
92
92
  */
93
- export declare function readFile(filename: string, cb: BFSCallback<Buffer>): void;
93
+ export declare function readFile(filename: string, cb: BFSCallback<Uint8Array>): void;
94
94
  export declare function readFile(filename: string, options: {
95
95
  flag?: string;
96
- }, callback?: BFSCallback<Buffer>): void;
96
+ }, callback?: BFSCallback<Uint8Array>): void;
97
97
  export declare function readFile(filename: string, options: {
98
98
  encoding: string;
99
99
  flag?: string;
@@ -188,7 +188,7 @@ export declare function fdatasync(fd: number, cb?: BFSOneArgCallback): void;
188
188
  * Note that it is unsafe to use fs.write multiple times on the same file
189
189
  * without waiting for the callback.
190
190
  * @param fd
191
- * @param buffer Buffer containing the data to write to
191
+ * @param buffer Uint8Array containing the data to write to
192
192
  * the file.
193
193
  * @param offset Offset in the buffer to start reading data from.
194
194
  * @param length The amount of bytes to write to the file.
@@ -197,8 +197,8 @@ export declare function fdatasync(fd: number, cb?: BFSOneArgCallback): void;
197
197
  * the current position.
198
198
  * @param callback The number specifies the number of bytes written into the file.
199
199
  */
200
- export declare function write(fd: number, buffer: Buffer, offset: number, length: number, cb?: BFSThreeArgCallback<number, Buffer>): void;
201
- export declare function write(fd: number, buffer: Buffer, offset: number, length: number, position: number | null, cb?: BFSThreeArgCallback<number, Buffer>): void;
200
+ export declare function write(fd: number, buffer: Uint8Array, offset: number, length: number, cb?: BFSThreeArgCallback<number, Uint8Array>): void;
201
+ export declare function write(fd: number, buffer: Uint8Array, offset: number, length: number, position: number | null, cb?: BFSThreeArgCallback<number, Uint8Array>): void;
202
202
  export declare function write(fd: number, data: FileContents, cb?: BFSThreeArgCallback<number, string>): void;
203
203
  export declare function write(fd: number, data: FileContents, position: number | null, cb?: BFSThreeArgCallback<number, string>): void;
204
204
  export declare function write(fd: number, data: FileContents, position: number | null, encoding: BufferEncoding, cb?: BFSThreeArgCallback<number, string>): void;
@@ -214,7 +214,7 @@ export declare function write(fd: number, data: FileContents, position: number |
214
214
  * position.
215
215
  * @param callback The number is the number of bytes read
216
216
  */
217
- export declare function read(fd: number, buffer: Buffer, offset: number, length: number, position?: number, cb?: BFSThreeArgCallback<number, Buffer>): void;
217
+ export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number, cb?: BFSThreeArgCallback<number, Uint8Array>): void;
218
218
  /**
219
219
  * Asynchronous `fchown`.
220
220
  * @param fd
@@ -2,6 +2,7 @@ import { ApiError, ErrorCode } from '../ApiError.js';
2
2
  import { nop, normalizeMode } from './shared.js';
3
3
  import * as promises from './promises.js';
4
4
  import { R_OK } from './constants.js';
5
+ import { decode, encode } from '../utils.js';
5
6
  /**
6
7
  * Asynchronous rename. No arguments other than a possible exception are given
7
8
  * to the completion callback.
@@ -167,13 +168,13 @@ export function write(fd, arg2, arg3, arg4, arg5, cb = nop) {
167
168
  cb(new ApiError(ErrorCode.EINVAL, 'Invalid arguments.'));
168
169
  return;
169
170
  }
170
- buffer = Buffer.from(arg2, encoding);
171
+ buffer = encode(arg2);
171
172
  offset = 0;
172
173
  length = buffer.length;
173
174
  const _cb = cb;
174
175
  promises
175
176
  .write(fd, buffer, offset, length, position)
176
- .then(bytesWritten => _cb(null, bytesWritten, buffer.toString(encoding)))
177
+ .then(bytesWritten => _cb(null, bytesWritten, decode(buffer)))
177
178
  .catch(_cb);
178
179
  }
179
180
  else {
@@ -55,11 +55,11 @@ export declare function open(path: string, flag: string, mode?: number | string)
55
55
  * @param options
56
56
  * @option options [String] encoding The string encoding for the file contents. Defaults to `null`.
57
57
  * @option options [String] flag Defaults to `'r'`.
58
- * @return [String | ZenFS.node.Buffer]
58
+ * @return [String | ZenFS.node.Uint8Array]
59
59
  */
60
60
  export declare function readFile(filename: string, options?: {
61
61
  flag?: string;
62
- }): Promise<Buffer>;
62
+ }): Promise<Uint8Array>;
63
63
  export declare function readFile(filename: string, options: {
64
64
  encoding: string;
65
65
  flag?: string;
@@ -144,7 +144,7 @@ export declare function fdatasync(fd: number): Promise<void>;
144
144
  * Note that it is unsafe to use fs.write multiple times on the same file
145
145
  * without waiting for it to return.
146
146
  * @param fd
147
- * @param buffer Buffer containing the data to write to
147
+ * @param buffer Uint8Array containing the data to write to
148
148
  * the file.
149
149
  * @param offset Offset in the buffer to start reading data from.
150
150
  * @param length The amount of bytes to write to the file.
@@ -152,7 +152,7 @@ export declare function fdatasync(fd: number): Promise<void>;
152
152
  * data should be written. If position is null, the data will be written at
153
153
  * the current position.
154
154
  */
155
- export declare function write(fd: number, buffer: Buffer, offset: number, length: number, position?: number): Promise<number>;
155
+ export declare function write(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number): Promise<number>;
156
156
  export declare function write(fd: number, data: string, position?: number | null, encoding?: BufferEncoding): Promise<number>;
157
157
  /**
158
158
  * Read data from the file specified by `fd`.
@@ -166,9 +166,9 @@ export declare function write(fd: number, data: string, position?: number | null
166
166
  * in the file. If position is null, data will be read from the current file
167
167
  * position.
168
168
  */
169
- export declare function read(fd: number, buffer: Buffer, offset: number, length: number, position?: number): Promise<{
169
+ export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number): Promise<{
170
170
  bytesRead: number;
171
- buffer: Buffer;
171
+ buffer: Uint8Array;
172
172
  }>;
173
173
  /**
174
174
  * `fchown`.
@@ -12,6 +12,7 @@ import * as constants from './constants.js';
12
12
  export { constants };
13
13
  import { FileFlag } from '../file.js';
14
14
  import { normalizePath, normalizeMode, getFdForFile, normalizeOptions, fd2file, fdMap, normalizeTime, cred, nop, resolveFS, fixError, mounts } from './shared.js';
15
+ import { encode } from '../utils.js';
15
16
  /**
16
17
  * Utility for FS ops. It handles
17
18
  * - path normalization (for the first parameter to the FS op)
@@ -234,7 +235,7 @@ export function write(fd, arg2, arg3, arg4, arg5) {
234
235
  position = typeof arg3 === 'number' ? arg3 : null;
235
236
  const encoding = (typeof arg4 === 'string' ? arg4 : 'utf8');
236
237
  offset = 0;
237
- buffer = Buffer.from(arg2, encoding);
238
+ buffer = encode(arg2);
238
239
  length = buffer.length;
239
240
  }
240
241
  else {
@@ -54,11 +54,11 @@ export declare function openSync(path: string, flag: string, mode?: number | str
54
54
  * @param options
55
55
  * @option options [String] encoding The string encoding for the file contents. Defaults to `null`.
56
56
  * @option options [String] flag Defaults to `'r'`.
57
- * @return [String | ZenFS.node.Buffer]
57
+ * @return [String | ZenFS.node.Uint8Array]
58
58
  */
59
59
  export declare function readFileSync(filename: string, options?: {
60
60
  flag?: string;
61
- }): Buffer;
61
+ }): Uint8Array;
62
62
  export declare function readFileSync(filename: string, options: {
63
63
  encoding: string;
64
64
  flag?: string;
@@ -138,7 +138,7 @@ export declare function fdatasyncSync(fd: number): void;
138
138
  * Note that it is unsafe to use fs.write multiple times on the same file
139
139
  * without waiting for it to return.
140
140
  * @param fd
141
- * @param buffer Buffer containing the data to write to
141
+ * @param buffer Uint8Array containing the data to write to
142
142
  * the file.
143
143
  * @param offset Offset in the buffer to start reading data from.
144
144
  * @param length The amount of bytes to write to the file.
@@ -146,7 +146,7 @@ export declare function fdatasyncSync(fd: number): void;
146
146
  * data should be written. If position is null, the data will be written at
147
147
  * the current position.
148
148
  */
149
- export declare function writeSync(fd: number, buffer: Buffer, offset: number, length: number, position?: number | null): number;
149
+ export declare function writeSync(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number | null): number;
150
150
  export declare function writeSync(fd: number, data: string, position?: number | null, encoding?: BufferEncoding): number;
151
151
  /**
152
152
  * Read data from the file specified by `fd`.
@@ -160,8 +160,8 @@ export declare function writeSync(fd: number, data: string, position?: number |
160
160
  * in the file. If position is null, data will be read from the current file
161
161
  * position.
162
162
  */
163
- export declare function readSync(fd: number, buffer: Buffer, opts?: ReadSyncOptions): number;
164
- export declare function readSync(fd: number, buffer: Buffer, offset: number, length: number, position?: number): number;
163
+ export declare function readSync(fd: number, buffer: Uint8Array, opts?: ReadSyncOptions): number;
164
+ export declare function readSync(fd: number, buffer: Uint8Array, offset: number, length: number, position?: number): number;
165
165
  /**
166
166
  * Synchronous `fchown`.
167
167
  * @param fd
@@ -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
  /**
@@ -13,7 +13,7 @@ var _a;
13
13
  import { ApiError, ErrorCode } from './ApiError.js';
14
14
  import { FileFlag, ActionType } from './file.js';
15
15
  import * as path from './emulation/path.js';
16
- import { Buffer } from 'buffer';
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.: