@zenfs/core 0.5.2 → 0.5.3
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/backends/AsyncStore.d.ts +7 -17
- package/dist/backends/AsyncStore.js +10 -10
- package/dist/backends/InMemory.d.ts +2 -2
- package/dist/backends/InMemory.js +2 -2
- package/dist/backends/SyncStore.d.ts +10 -20
- package/dist/backends/SyncStore.js +10 -10
- package/dist/browser.min.js +4 -4
- package/dist/browser.min.js.map +3 -3
- package/dist/emulation/promises.d.ts +7 -7
- package/dist/emulation/promises.js +43 -32
- package/package.json +1 -1
|
@@ -49,13 +49,13 @@ export declare class FileHandle implements BufferToUint8Array<Node.promises.File
|
|
|
49
49
|
* Asynchronously append data to a file, creating the file if it does not exist. The underlying file will _not_ be closed automatically.
|
|
50
50
|
* The `FileHandle` must have been opened for appending.
|
|
51
51
|
* @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
|
|
52
|
-
* @param
|
|
52
|
+
* @param _options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
|
53
53
|
* If `encoding` is not supplied, the default of `'utf8'` is used.
|
|
54
54
|
* If `mode` is not supplied, the default of `0o666` is used.
|
|
55
55
|
* If `mode` is a string, it is parsed as an octal integer.
|
|
56
56
|
* If `flag` is not supplied, the default of `'a'` is used.
|
|
57
57
|
*/
|
|
58
|
-
appendFile(data: string | Uint8Array,
|
|
58
|
+
appendFile(data: string | Uint8Array, _options?: {
|
|
59
59
|
encoding?: BufferEncoding;
|
|
60
60
|
mode?: Node.Mode;
|
|
61
61
|
flag?: Node.OpenMode;
|
|
@@ -75,13 +75,13 @@ export declare class FileHandle implements BufferToUint8Array<Node.promises.File
|
|
|
75
75
|
/**
|
|
76
76
|
* Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
|
|
77
77
|
* The `FileHandle` must have been opened for reading.
|
|
78
|
-
* @param
|
|
78
|
+
* @param _options An object that may contain an optional flag.
|
|
79
79
|
* If a flag is not provided, it defaults to `'r'`.
|
|
80
80
|
*/
|
|
81
|
-
readFile(
|
|
81
|
+
readFile(_options?: {
|
|
82
82
|
flag?: Node.OpenMode;
|
|
83
83
|
}): Promise<Uint8Array>;
|
|
84
|
-
readFile(
|
|
84
|
+
readFile(_options: {
|
|
85
85
|
encoding: BufferEncoding;
|
|
86
86
|
flag?: Node.OpenMode;
|
|
87
87
|
} | BufferEncoding): Promise<string>;
|
|
@@ -126,13 +126,13 @@ export declare class FileHandle implements BufferToUint8Array<Node.promises.File
|
|
|
126
126
|
* The `FileHandle` must have been opened for writing.
|
|
127
127
|
* It is unsafe to call `writeFile()` multiple times on the same file without waiting for the `Promise` to be resolved (or rejected).
|
|
128
128
|
* @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
|
|
129
|
-
* @param
|
|
129
|
+
* @param _options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
|
130
130
|
* If `encoding` is not supplied, the default of `'utf8'` is used.
|
|
131
131
|
* If `mode` is not supplied, the default of `0o666` is used.
|
|
132
132
|
* If `mode` is a string, it is parsed as an octal integer.
|
|
133
133
|
* If `flag` is not supplied, the default of `'w'` is used.
|
|
134
134
|
*/
|
|
135
|
-
writeFile(data: string | Uint8Array,
|
|
135
|
+
writeFile(data: string | Uint8Array, _options?: Node.WriteFileOptions): Promise<void>;
|
|
136
136
|
/**
|
|
137
137
|
* See `fs.writev` promisified version.
|
|
138
138
|
*/
|
|
@@ -65,14 +65,23 @@ export class FileHandle {
|
|
|
65
65
|
* Asynchronously append data to a file, creating the file if it does not exist. The underlying file will _not_ be closed automatically.
|
|
66
66
|
* The `FileHandle` must have been opened for appending.
|
|
67
67
|
* @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
|
|
68
|
-
* @param
|
|
68
|
+
* @param _options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
|
69
69
|
* If `encoding` is not supplied, the default of `'utf8'` is used.
|
|
70
70
|
* If `mode` is not supplied, the default of `0o666` is used.
|
|
71
71
|
* If `mode` is a string, it is parsed as an octal integer.
|
|
72
72
|
* If `flag` is not supplied, the default of `'a'` is used.
|
|
73
73
|
*/
|
|
74
|
-
appendFile(data,
|
|
75
|
-
|
|
74
|
+
async appendFile(data, _options) {
|
|
75
|
+
const options = normalizeOptions(_options, 'utf8', 'a', 0o644);
|
|
76
|
+
const flag = parseFlag(options.flag);
|
|
77
|
+
if (!isAppendable(flag)) {
|
|
78
|
+
throw new ApiError(ErrorCode.EINVAL, 'Flag passed to appendFile must allow for appending.');
|
|
79
|
+
}
|
|
80
|
+
if (typeof data != 'string' && !options.encoding) {
|
|
81
|
+
throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
|
|
82
|
+
}
|
|
83
|
+
const encodedData = typeof data == 'string' ? encode(data) : data;
|
|
84
|
+
await fd2file(this.fd).write(encodedData, 0, encodedData.length, null);
|
|
76
85
|
}
|
|
77
86
|
/**
|
|
78
87
|
* Asynchronously reads data from the file.
|
|
@@ -88,11 +97,20 @@ export class FileHandle {
|
|
|
88
97
|
}
|
|
89
98
|
return fd2file(this.fd).read(buffer, offset, length, position);
|
|
90
99
|
}
|
|
91
|
-
readFile(
|
|
92
|
-
|
|
100
|
+
async readFile(_options) {
|
|
101
|
+
const options = normalizeOptions(_options, null, 'r', null);
|
|
102
|
+
const flag = parseFlag(options.flag);
|
|
103
|
+
if (!isReadable(flag)) {
|
|
104
|
+
throw new ApiError(ErrorCode.EINVAL, 'Flag passed must allow for reading.');
|
|
105
|
+
}
|
|
106
|
+
const { size } = await this.stat();
|
|
107
|
+
const data = new Uint8Array(size);
|
|
108
|
+
await fd2file(this.fd).read(data, 0, size, 0);
|
|
109
|
+
return options.encoding ? decode(data, options.encoding) : data;
|
|
93
110
|
}
|
|
94
|
-
stat(opts) {
|
|
95
|
-
|
|
111
|
+
async stat(opts) {
|
|
112
|
+
const stats = await fd2file(this.fd).stat();
|
|
113
|
+
return opts?.bigint ? new BigIntStats(stats) : stats;
|
|
96
114
|
}
|
|
97
115
|
async write(data, posOrOff, lenOrEnc, position) {
|
|
98
116
|
let buffer, offset = 0, length;
|
|
@@ -120,14 +138,23 @@ export class FileHandle {
|
|
|
120
138
|
* The `FileHandle` must have been opened for writing.
|
|
121
139
|
* It is unsafe to call `writeFile()` multiple times on the same file without waiting for the `Promise` to be resolved (or rejected).
|
|
122
140
|
* @param data The data to write. If something other than a `Buffer` or `Uint8Array` is provided, the value is coerced to a string.
|
|
123
|
-
* @param
|
|
141
|
+
* @param _options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
|
|
124
142
|
* If `encoding` is not supplied, the default of `'utf8'` is used.
|
|
125
143
|
* If `mode` is not supplied, the default of `0o666` is used.
|
|
126
144
|
* If `mode` is a string, it is parsed as an octal integer.
|
|
127
145
|
* If `flag` is not supplied, the default of `'w'` is used.
|
|
128
146
|
*/
|
|
129
|
-
writeFile(data,
|
|
130
|
-
|
|
147
|
+
async writeFile(data, _options) {
|
|
148
|
+
const options = normalizeOptions(_options, 'utf8', 'w', 0o644);
|
|
149
|
+
const flag = parseFlag(options.flag);
|
|
150
|
+
if (!isWriteable(flag)) {
|
|
151
|
+
throw new ApiError(ErrorCode.EINVAL, 'Flag passed must allow for writing.');
|
|
152
|
+
}
|
|
153
|
+
if (typeof data != 'string' && !options.encoding) {
|
|
154
|
+
throw new ApiError(ErrorCode.EINVAL, 'Encoding not specified');
|
|
155
|
+
}
|
|
156
|
+
const encodedData = typeof data == 'string' ? encode(data, options.encoding) : data;
|
|
157
|
+
await fd2file(this.fd).write(encodedData, 0, encodedData.length, 0);
|
|
131
158
|
}
|
|
132
159
|
/**
|
|
133
160
|
* See `fs.writev` promisified version.
|
|
@@ -323,6 +350,7 @@ async function _readFile(fname, flag, resolveSymlinks) {
|
|
|
323
350
|
}
|
|
324
351
|
catch (e) {
|
|
325
352
|
await file.close();
|
|
353
|
+
throw e;
|
|
326
354
|
}
|
|
327
355
|
}
|
|
328
356
|
export async function readFile(filename, _options) {
|
|
@@ -335,21 +363,6 @@ export async function readFile(filename, _options) {
|
|
|
335
363
|
return options.encoding ? decode(data, options.encoding) : data;
|
|
336
364
|
}
|
|
337
365
|
readFile;
|
|
338
|
-
/**
|
|
339
|
-
* Asynchronously writes data to a file, replacing the file
|
|
340
|
-
* if it already exists.
|
|
341
|
-
*
|
|
342
|
-
* The encoding option is ignored if data is a buffer.
|
|
343
|
-
*/
|
|
344
|
-
async function _writeFile(fname, data, flag, mode, resolveSymlinks) {
|
|
345
|
-
const file = await _open(fname, flag, mode, resolveSymlinks);
|
|
346
|
-
try {
|
|
347
|
-
await file.write(data, 0, data.length, 0);
|
|
348
|
-
}
|
|
349
|
-
finally {
|
|
350
|
-
await file.close();
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
366
|
/**
|
|
354
367
|
* Synchronously writes data to a file, replacing the file if it already exists.
|
|
355
368
|
*
|
|
@@ -363,15 +376,13 @@ async function _writeFile(fname, data, flag, mode, resolveSymlinks) {
|
|
|
363
376
|
*/
|
|
364
377
|
export async function writeFile(filename, data, _options) {
|
|
365
378
|
const options = normalizeOptions(_options, 'utf8', 'w', 0o644);
|
|
366
|
-
const
|
|
367
|
-
|
|
368
|
-
|
|
379
|
+
const handle = await open(filename, options.flag, options.mode);
|
|
380
|
+
try {
|
|
381
|
+
await handle.writeFile(data, options);
|
|
369
382
|
}
|
|
370
|
-
|
|
371
|
-
|
|
383
|
+
finally {
|
|
384
|
+
await handle.close();
|
|
372
385
|
}
|
|
373
|
-
const encodedData = typeof data == 'string' ? encode(data, options.encoding) : data;
|
|
374
|
-
await _writeFile(filename, encodedData, options.flag, options.mode, true);
|
|
375
386
|
}
|
|
376
387
|
writeFile;
|
|
377
388
|
/**
|