@zenfs/core 0.0.11 → 0.0.12

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.
@@ -12,7 +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
+ import { decode, encode } from '../utils.js';
16
16
  /**
17
17
  * Utility for FS ops. It handles
18
18
  * - path normalization (for the first parameter to the FS op)
@@ -148,7 +148,14 @@ export function readFile(filename, arg2 = {}) {
148
148
  if (!flag.isReadable()) {
149
149
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to readFile must allow for reading.');
150
150
  }
151
- return doOp('readFile', true, filename, options.encoding, flag, cred);
151
+ const data = yield doOp('readFile', true, filename, flag, cred);
152
+ switch (options.encoding) {
153
+ case 'utf8':
154
+ case 'utf-8':
155
+ return decode(data);
156
+ default:
157
+ return data;
158
+ }
152
159
  });
153
160
  }
154
161
  export function writeFile(filename, data, arg3) {
@@ -158,7 +165,11 @@ export function writeFile(filename, data, arg3) {
158
165
  if (!flag.isWriteable()) {
159
166
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to writeFile must allow for writing.');
160
167
  }
161
- return doOp('writeFile', true, filename, data, options.encoding, flag, options.mode, cred);
168
+ if (typeof data != 'string' && !options.encoding) {
169
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
170
+ }
171
+ const encodedData = typeof data == 'string' ? encode(data) : data;
172
+ return doOp('writeFile', true, filename, encodedData, flag, options.mode, cred);
162
173
  });
163
174
  }
164
175
  export function appendFile(filename, data, arg3) {
@@ -168,7 +179,11 @@ export function appendFile(filename, data, arg3) {
168
179
  if (!flag.isAppendable()) {
169
180
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to appendFile must allow for appending.');
170
181
  }
171
- return doOp('appendFile', true, filename, data, options.encoding, flag, options.mode, cred);
182
+ if (typeof data != 'string' && !options.encoding) {
183
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
184
+ }
185
+ const encodedData = typeof data == 'string' ? encode(data) : data;
186
+ return doOp('appendFile', true, filename, encodedData, flag, options.mode, cred);
172
187
  });
173
188
  }
174
189
  // FILE DESCRIPTOR METHODS
@@ -1,7 +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
+ import { decode, encode } from '../utils.js';
5
5
  function doOp(...[name, resolveSymlinks, path, ...args]) {
6
6
  path = normalizePath(path);
7
7
  const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && existsSync(path) ? realpathSync(path) : path);
@@ -107,7 +107,14 @@ export function readFileSync(filename, arg2 = {}) {
107
107
  if (!flag.isReadable()) {
108
108
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to readFile must allow for reading.');
109
109
  }
110
- return doOp('readFileSync', true, filename, options.encoding, flag, cred);
110
+ const data = doOp('readFileSync', true, filename, flag, cred);
111
+ switch (options.encoding) {
112
+ case 'utf8':
113
+ case 'utf-8':
114
+ return decode(data);
115
+ default:
116
+ return data;
117
+ }
111
118
  }
112
119
  export function writeFileSync(filename, data, arg3) {
113
120
  const options = normalizeOptions(arg3, 'utf8', 'w', 0o644);
@@ -115,7 +122,11 @@ export function writeFileSync(filename, data, arg3) {
115
122
  if (!flag.isWriteable()) {
116
123
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to writeFile must allow for writing.');
117
124
  }
118
- return doOp('writeFileSync', true, filename, data, options.encoding, flag, options.mode, cred);
125
+ if (typeof data != 'string' && !options.encoding) {
126
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
127
+ }
128
+ const encodedData = typeof data == 'string' ? encode(data) : data;
129
+ return doOp('writeFileSync', true, filename, encodedData, flag, options.mode, cred);
119
130
  }
120
131
  export function appendFileSync(filename, data, arg3) {
121
132
  const options = normalizeOptions(arg3, 'utf8', 'a', 0o644);
@@ -123,7 +134,11 @@ export function appendFileSync(filename, data, arg3) {
123
134
  if (!flag.isAppendable()) {
124
135
  throw new ApiError(ErrorCode.EINVAL, 'Flag passed to appendFile must allow for appending.');
125
136
  }
126
- return doOp('appendFileSync', true, filename, data, options.encoding, flag, options.mode, cred);
137
+ if (typeof data != 'string' && !options.encoding) {
138
+ throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
139
+ }
140
+ const encodedData = typeof data == 'string' ? encode(data) : data;
141
+ return doOp('appendFileSync', true, filename, encodedData, flag, options.mode, cred);
127
142
  }
128
143
  /**
129
144
  * Synchronous `fstat`.
@@ -1,4 +1,3 @@
1
- /// <reference types="node" resolution-mode="require"/>
2
1
  import { ApiError } from './ApiError.js';
3
2
  import { Stats } from './stats.js';
4
3
  import { File, FileFlag } from './file.js';
@@ -179,43 +178,36 @@ export declare abstract class FileSystem {
179
178
  abstract truncateSync(p: string, len: number, cred: Cred): void;
180
179
  /**
181
180
  * Asynchronously reads the entire contents of a file.
182
- * @param encoding If non-null, the file's contents should be decoded
183
- * into a string using that encoding. Otherwise, if encoding is null, fetch
184
- * the file's contents as a Uint8Array.
185
- * If no encoding is specified, then the raw buffer is returned.
186
181
  */
187
- abstract readFile(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): Promise<FileContents>;
182
+ abstract readFile(fname: string, flag: FileFlag, cred: Cred): Promise<Uint8Array>;
188
183
  /**
189
184
  * Synchronously reads the entire contents of a file.
190
- * @param encoding If non-null, the file's contents should be decoded
191
- * into a string using that encoding. Otherwise, if encoding is null, fetch
192
- * the file's contents as a Uint8Array.
193
185
  */
194
- abstract readFileSync(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): FileContents;
186
+ abstract readFileSync(fname: string, flag: FileFlag, cred: Cred): Uint8Array;
195
187
  /**
196
188
  * Asynchronously writes data to a file, replacing the file
197
189
  * if it already exists.
198
190
  *
199
191
  * The encoding option is ignored if data is a buffer.
200
192
  */
201
- abstract writeFile(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
193
+ abstract writeFile(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
202
194
  /**
203
195
  * Synchronously writes data to a file, replacing the file
204
196
  * if it already exists.
205
197
  *
206
198
  * The encoding option is ignored if data is a buffer.
207
199
  */
208
- abstract writeFileSync(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): void;
200
+ abstract writeFileSync(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): void;
209
201
  /**
210
202
  * Asynchronously append data to a file, creating the file if
211
203
  * it not yet exists.
212
204
  */
213
- abstract appendFile(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
205
+ abstract appendFile(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
214
206
  /**
215
207
  * Synchronously append data to a file, creating the file if
216
208
  * it not yet exists.
217
209
  */
218
- abstract appendFileSync(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): void;
210
+ abstract appendFileSync(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): void;
219
211
  /**
220
212
  * Asynchronous `chmod`.
221
213
  */
@@ -326,12 +318,12 @@ export declare class BaseFileSystem extends FileSystem {
326
318
  realpathSync(p: string, cred: Cred): string;
327
319
  truncate(p: string, len: number, cred: Cred): Promise<void>;
328
320
  truncateSync(p: string, len: number, cred: Cred): void;
329
- readFile(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): Promise<FileContents>;
330
- readFileSync(fname: string, encoding: BufferEncoding | null, flag: FileFlag, cred: Cred): FileContents;
331
- writeFile(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
332
- writeFileSync(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): void;
333
- appendFile(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
334
- appendFileSync(fname: string, data: FileContents, encoding: BufferEncoding | null, flag: FileFlag, mode: number, cred: Cred): void;
321
+ readFile(fname: string, flag: FileFlag, cred: Cred): Promise<Uint8Array>;
322
+ readFileSync(fname: string, flag: FileFlag, cred: Cred): Uint8Array;
323
+ writeFile(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
324
+ writeFileSync(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): void;
325
+ appendFile(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): Promise<void>;
326
+ appendFileSync(fname: string, data: Uint8Array, flag: FileFlag, mode: number, cred: Cred): void;
335
327
  chmod(p: string, mode: number, cred: Cred): Promise<void>;
336
328
  chmodSync(p: string, mode: number, cred: Cred): void;
337
329
  chown(p: string, new_uid: number, new_gid: number, cred: Cred): Promise<void>;
@@ -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 { decode, encode } from './utils.js';
16
+ import { encode } from './utils.js';
17
17
  /**
18
18
  * Structure for a filesystem. **All** ZenFS FileSystems must implement
19
19
  * this.
@@ -317,7 +317,7 @@ export class BaseFileSystem extends FileSystem {
317
317
  fd.closeSync();
318
318
  }
319
319
  }
320
- readFile(fname, encoding, flag, cred) {
320
+ readFile(fname, flag, cred) {
321
321
  return __awaiter(this, void 0, void 0, function* () {
322
322
  // Get file.
323
323
  const fd = yield this.open(fname, flag, 0o644, cred);
@@ -327,17 +327,14 @@ export class BaseFileSystem extends FileSystem {
327
327
  const buf = new Uint8Array(stat.size);
328
328
  yield fd.read(buf, 0, stat.size, 0);
329
329
  yield fd.close();
330
- if (encoding === null) {
331
- return buf;
332
- }
333
- return decode(buf);
330
+ return buf;
334
331
  }
335
332
  finally {
336
333
  yield fd.close();
337
334
  }
338
335
  });
339
336
  }
340
- readFileSync(fname, encoding, flag, cred) {
337
+ readFileSync(fname, flag, cred) {
341
338
  // Get file.
342
339
  const fd = this.openSync(fname, flag, 0o644, cred);
343
340
  try {
@@ -346,16 +343,13 @@ export class BaseFileSystem extends FileSystem {
346
343
  const buf = new Uint8Array(stat.size);
347
344
  fd.readSync(buf, 0, stat.size, 0);
348
345
  fd.closeSync();
349
- if (encoding === null) {
350
- return buf;
351
- }
352
- return decode(buf);
346
+ return buf;
353
347
  }
354
348
  finally {
355
349
  fd.closeSync();
356
350
  }
357
351
  }
358
- writeFile(fname, data, encoding, flag, mode, cred) {
352
+ writeFile(fname, data, flag, mode, cred) {
359
353
  return __awaiter(this, void 0, void 0, function* () {
360
354
  // Get file.
361
355
  const fd = yield this.open(fname, flag, mode, cred);
@@ -371,7 +365,7 @@ export class BaseFileSystem extends FileSystem {
371
365
  }
372
366
  });
373
367
  }
374
- writeFileSync(fname, data, encoding, flag, mode, cred) {
368
+ writeFileSync(fname, data, flag, mode, cred) {
375
369
  // Get file.
376
370
  const fd = this.openSync(fname, flag, mode, cred);
377
371
  try {
@@ -385,7 +379,7 @@ export class BaseFileSystem extends FileSystem {
385
379
  fd.closeSync();
386
380
  }
387
381
  }
388
- appendFile(fname, data, encoding, flag, mode, cred) {
382
+ appendFile(fname, data, flag, mode, cred) {
389
383
  return __awaiter(this, void 0, void 0, function* () {
390
384
  const fd = yield this.open(fname, flag, mode, cred);
391
385
  try {
@@ -399,7 +393,7 @@ export class BaseFileSystem extends FileSystem {
399
393
  }
400
394
  });
401
395
  }
402
- appendFileSync(fname, data, encoding, flag, mode, cred) {
396
+ appendFileSync(fname, data, flag, mode, cred) {
403
397
  const fd = this.openSync(fname, flag, mode, cred);
404
398
  try {
405
399
  if (typeof data === 'string') {
package/dist/utils.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  import { FileSystem } from './filesystem.js';
6
6
  import { Cred } from './cred.js';
7
7
  import type { BaseBackendConstructor } from './backends/backend.js';
8
+ import type { TextEncoder as TextEncoderType, TextDecoder as TextDecoderType } from 'node:util';
8
9
  /**
9
10
  * Synchronous recursive makedir.
10
11
  * @internal
@@ -30,12 +31,13 @@ export declare const setImmediate: (callback: () => unknown) => void;
30
31
  * @internal
31
32
  */
32
33
  export declare const ROOT_NODE_ID: string;
33
- export declare const encode: (input?: string) => Uint8Array;
34
- export declare const decode: (input?: ArrayBuffer | NodeJS.ArrayBufferView, options?: {
35
- stream?: boolean;
36
- }) => string;
34
+ declare const textEncoder: TextEncoderType;
35
+ export declare const encode: typeof textEncoder.encode;
36
+ declare const textDecoder: TextDecoderType;
37
+ export declare const decode: typeof textDecoder.decode;
37
38
  /**
38
39
  * Generates a random ID.
39
40
  * @internal
40
41
  */
41
42
  export declare function randomUUID(): string;
43
+ export {};
package/dist/utils.js CHANGED
@@ -212,8 +212,10 @@ export const setImmediate = typeof globalThis.setImmediate == 'function' ? globa
212
212
  * @internal
213
213
  */
214
214
  export const ROOT_NODE_ID = '/';
215
- export const encode = new globalThis.TextEncoder().encode;
216
- export const decode = new globalThis.TextDecoder().decode;
215
+ const textEncoder = new globalThis.TextEncoder();
216
+ export const encode = textEncoder.encode.bind(textEncoder);
217
+ const textDecoder = new globalThis.TextDecoder();
218
+ export const decode = textDecoder.decode.bind(textDecoder);
217
219
  /**
218
220
  * Generates a random ID.
219
221
  * @internal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenfs/core",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "A filesystem in your browser",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist",