@zenfs/core 1.0.10 → 1.1.0
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/backend.d.ts +4 -8
- package/dist/backends/backend.js +7 -11
- package/dist/backends/fetch.d.ts +2 -4
- package/dist/backends/fetch.js +1 -3
- package/dist/backends/file_index.js +3 -3
- package/dist/backends/memory.d.ts +1 -1
- package/dist/backends/overlay.d.ts +7 -3
- package/dist/backends/overlay.js +13 -9
- package/dist/backends/port/fs.d.ts +12 -17
- package/dist/backends/port/fs.js +5 -8
- package/dist/backends/port/rpc.d.ts +1 -1
- package/dist/backends/store/fs.d.ts +13 -15
- package/dist/backends/store/fs.js +35 -54
- package/dist/backends/store/simple.d.ts +1 -1
- package/dist/backends/store/simple.js +1 -1
- package/dist/backends/store/store.d.ts +7 -13
- package/dist/config.d.ts +13 -5
- package/dist/config.js +36 -26
- package/dist/devices.d.ts +158 -0
- package/dist/devices.js +423 -0
- package/dist/emulation/async.d.ts +21 -176
- package/dist/emulation/async.js +17 -111
- package/dist/emulation/constants.d.ts +5 -0
- package/dist/emulation/constants.js +5 -0
- package/dist/emulation/dir.d.ts +0 -1
- package/dist/emulation/path.d.ts +0 -4
- package/dist/emulation/path.js +4 -8
- package/dist/emulation/promises.d.ts +31 -121
- package/dist/emulation/promises.js +30 -97
- package/dist/emulation/shared.d.ts +7 -3
- package/dist/emulation/shared.js +11 -7
- package/dist/emulation/streams.d.ts +0 -3
- package/dist/emulation/sync.d.ts +25 -178
- package/dist/emulation/sync.js +36 -129
- package/dist/emulation/watchers.d.ts +0 -4
- package/dist/error.d.ts +11 -11
- package/dist/error.js +8 -10
- package/dist/file.d.ts +50 -171
- package/dist/file.js +34 -117
- package/dist/filesystem.d.ts +10 -62
- package/dist/filesystem.js +5 -6
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/inode.d.ts +0 -5
- package/dist/inode.js +0 -5
- package/dist/mixins/async.d.ts +4 -6
- package/dist/mixins/async.js +3 -1
- package/dist/mixins/mutexed.d.ts +4 -4
- package/dist/mixins/mutexed.js +7 -5
- package/dist/mixins/readonly.js +14 -15
- package/dist/mixins/shared.d.ts +5 -5
- package/dist/mixins/sync.d.ts +2 -2
- package/dist/stats.d.ts +21 -37
- package/dist/stats.js +10 -23
- package/dist/utils.d.ts +15 -7
- package/dist/utils.js +28 -6
- package/package.json +4 -4
- package/readme.md +58 -2
- package/src/backends/backend.ts +7 -11
- package/src/backends/fetch.ts +2 -4
- package/src/backends/file_index.ts +3 -3
- package/src/backends/memory.ts +1 -1
- package/src/backends/overlay.ts +11 -9
- package/src/backends/port/fs.ts +11 -14
- package/src/backends/port/rpc.ts +1 -0
- package/src/backends/store/fs.ts +40 -55
- package/src/backends/store/simple.ts +1 -1
- package/src/backends/store/store.ts +7 -13
- package/src/config.ts +48 -26
- package/src/devices.ts +469 -0
- package/src/emulation/async.ts +28 -178
- package/src/emulation/constants.ts +6 -0
- package/src/emulation/path.ts +4 -11
- package/src/emulation/promises.ts +34 -116
- package/src/emulation/shared.ts +11 -8
- package/src/emulation/sync.ts +41 -185
- package/src/error.ts +7 -11
- package/src/file.ts +48 -182
- package/src/filesystem.ts +14 -65
- package/src/index.ts +2 -0
- package/src/inode.ts +0 -6
- package/src/mixins/async.ts +4 -6
- package/src/mixins/mutexed.ts +4 -4
- package/src/mixins/readonly.ts +15 -15
- package/src/mixins/shared.ts +5 -5
- package/src/mixins/sync.ts +3 -3
- package/src/stats.ts +22 -40
- package/src/utils.ts +33 -6
package/dist/file.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
/// <reference types="node" resolution-mode="require"/>
|
|
3
1
|
import type { FileReadResult } from 'node:fs/promises';
|
|
4
2
|
import type { FileSystem } from './filesystem.js';
|
|
5
3
|
import { Stats, type FileType } from './stats.js';
|
|
6
4
|
import './polyfills.js';
|
|
5
|
+
/**
|
|
6
|
+
Typescript does not include a type declaration for resizable array buffers.
|
|
7
|
+
It has been standardized into ECMAScript though
|
|
8
|
+
@todo Remove this if TS adds them to lib declarations
|
|
9
|
+
*/
|
|
7
10
|
declare global {
|
|
8
11
|
interface ArrayBuffer {
|
|
9
12
|
readonly resizable: boolean;
|
|
@@ -41,95 +44,53 @@ export declare abstract class File {
|
|
|
41
44
|
* The file system that created the file
|
|
42
45
|
*/
|
|
43
46
|
fs: FileSystem;
|
|
44
|
-
/**
|
|
45
|
-
* The path to the file
|
|
46
|
-
*/
|
|
47
47
|
readonly path: string;
|
|
48
48
|
constructor(
|
|
49
49
|
/**
|
|
50
50
|
* @internal
|
|
51
51
|
* The file system that created the file
|
|
52
52
|
*/
|
|
53
|
-
fs: FileSystem,
|
|
54
|
-
/**
|
|
55
|
-
* The path to the file
|
|
56
|
-
*/
|
|
57
|
-
path: string);
|
|
53
|
+
fs: FileSystem, path: string);
|
|
58
54
|
/**
|
|
59
55
|
* Get the current file position.
|
|
60
56
|
*/
|
|
61
57
|
abstract position: number;
|
|
62
|
-
/**
|
|
63
|
-
* Asynchronous `stat`.
|
|
64
|
-
*/
|
|
65
58
|
abstract stat(): Promise<Stats>;
|
|
66
|
-
/**
|
|
67
|
-
* Synchronous `stat`.
|
|
68
|
-
*/
|
|
69
59
|
abstract statSync(): Stats;
|
|
70
|
-
/**
|
|
71
|
-
* Asynchronous close.
|
|
72
|
-
*/
|
|
73
60
|
abstract close(): Promise<void>;
|
|
74
|
-
/**
|
|
75
|
-
* Synchronous close.
|
|
76
|
-
*/
|
|
77
61
|
abstract closeSync(): void;
|
|
78
62
|
[Symbol.asyncDispose](): Promise<void>;
|
|
79
63
|
[Symbol.dispose](): void;
|
|
80
|
-
/**
|
|
81
|
-
* Asynchronous truncate.
|
|
82
|
-
*/
|
|
83
64
|
abstract truncate(len: number): Promise<void>;
|
|
84
|
-
/**
|
|
85
|
-
* Synchronous truncate.
|
|
86
|
-
*/
|
|
87
65
|
abstract truncateSync(len: number): void;
|
|
88
|
-
/**
|
|
89
|
-
* Asynchronous sync.
|
|
90
|
-
*/
|
|
91
66
|
abstract sync(): Promise<void>;
|
|
92
|
-
/**
|
|
93
|
-
* Synchronous sync.
|
|
94
|
-
*/
|
|
95
67
|
abstract syncSync(): void;
|
|
96
68
|
/**
|
|
97
69
|
* Write buffer to the file.
|
|
98
|
-
*
|
|
99
|
-
* without waiting for the callback.
|
|
100
|
-
* @param buffer Uint8Array containing the data to write to
|
|
101
|
-
* the file.
|
|
70
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
102
71
|
* @param offset Offset in the buffer to start reading data from.
|
|
103
72
|
* @param length The amount of bytes to write to the file.
|
|
104
|
-
* @param position Offset from the beginning of the file where this
|
|
105
|
-
*
|
|
106
|
-
* the current position.
|
|
73
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
74
|
+
* If position is null, the data will be written at the current position.
|
|
107
75
|
* @returns Promise resolving to the new length of the buffer
|
|
108
76
|
*/
|
|
109
77
|
abstract write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<number>;
|
|
110
78
|
/**
|
|
111
79
|
* Write buffer to the file.
|
|
112
|
-
*
|
|
113
|
-
* without waiting for it to return.
|
|
114
|
-
* @param buffer Uint8Array containing the data to write to
|
|
115
|
-
* the file.
|
|
80
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
116
81
|
* @param offset Offset in the buffer to start reading data from.
|
|
117
82
|
* @param length The amount of bytes to write to the file.
|
|
118
|
-
* @param position Offset from the beginning of the file where this
|
|
119
|
-
*
|
|
120
|
-
* the current position.
|
|
83
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
84
|
+
* If position is null, the data will be written at the current position.
|
|
121
85
|
*/
|
|
122
86
|
abstract writeSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number;
|
|
123
87
|
/**
|
|
124
88
|
* Read data from the file.
|
|
125
|
-
* @param buffer The buffer that the data will be
|
|
126
|
-
*
|
|
127
|
-
* @param offset The offset within the buffer where writing will
|
|
128
|
-
* start.
|
|
89
|
+
* @param buffer The buffer that the data will be written to.
|
|
90
|
+
* @param offset The offset within the buffer where writing will start.
|
|
129
91
|
* @param length An integer specifying the number of bytes to read.
|
|
130
|
-
* @param position An integer specifying where to begin reading from
|
|
131
|
-
*
|
|
132
|
-
* position.
|
|
92
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
93
|
+
* If position is null, data will be read from the current file position.
|
|
133
94
|
* @returns Promise resolving to the new length of the buffer
|
|
134
95
|
*/
|
|
135
96
|
abstract read<TBuffer extends NodeJS.ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<FileReadResult<TBuffer>>;
|
|
@@ -138,38 +99,21 @@ export declare abstract class File {
|
|
|
138
99
|
* @param buffer The buffer that the data will be written to.
|
|
139
100
|
* @param offset The offset within the buffer where writing will start.
|
|
140
101
|
* @param length An integer specifying the number of bytes to read.
|
|
141
|
-
* @param position An integer specifying where to begin reading from
|
|
142
|
-
*
|
|
143
|
-
* position.
|
|
102
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
103
|
+
* If position is null, data will be read from the current file position.
|
|
144
104
|
*/
|
|
145
105
|
abstract readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number;
|
|
146
106
|
/**
|
|
147
|
-
* Asynchronous `datasync`.
|
|
148
|
-
*
|
|
149
107
|
* Default implementation maps to `sync`.
|
|
150
108
|
*/
|
|
151
109
|
datasync(): Promise<void>;
|
|
152
110
|
/**
|
|
153
|
-
* Synchronous `datasync`.
|
|
154
|
-
*
|
|
155
111
|
* Default implementation maps to `syncSync`.
|
|
156
112
|
*/
|
|
157
113
|
datasyncSync(): void;
|
|
158
|
-
/**
|
|
159
|
-
* Asynchronous `chown`.
|
|
160
|
-
*/
|
|
161
114
|
abstract chown(uid: number, gid: number): Promise<void>;
|
|
162
|
-
/**
|
|
163
|
-
* Synchronous `chown`.
|
|
164
|
-
*/
|
|
165
115
|
abstract chownSync(uid: number, gid: number): void;
|
|
166
|
-
/**
|
|
167
|
-
* Asynchronous `fchmod`.
|
|
168
|
-
*/
|
|
169
116
|
abstract chmod(mode: number): Promise<void>;
|
|
170
|
-
/**
|
|
171
|
-
* Synchronous `fchmod`.
|
|
172
|
-
*/
|
|
173
117
|
abstract chmodSync(mode: number): void;
|
|
174
118
|
/**
|
|
175
119
|
* Change the file timestamps of the file.
|
|
@@ -191,9 +135,8 @@ export declare abstract class File {
|
|
|
191
135
|
abstract _setTypeSync(type: FileType): void;
|
|
192
136
|
}
|
|
193
137
|
/**
|
|
194
|
-
* An implementation of
|
|
195
|
-
*
|
|
196
|
-
*
|
|
138
|
+
* An implementation of `File` that operates completely in-memory.
|
|
139
|
+
* `PreloadFile`s are backed by a `Uint8Array`.
|
|
197
140
|
*/
|
|
198
141
|
export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
199
142
|
/**
|
|
@@ -203,6 +146,9 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
|
203
146
|
fs: FS;
|
|
204
147
|
readonly flag: string;
|
|
205
148
|
readonly stats: Stats;
|
|
149
|
+
/**
|
|
150
|
+
* A buffer containing the entire contents of the file.
|
|
151
|
+
*/
|
|
206
152
|
protected _buffer: Uint8Array;
|
|
207
153
|
/**
|
|
208
154
|
* Current position
|
|
@@ -217,23 +163,19 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
|
217
163
|
*/
|
|
218
164
|
protected closed: boolean;
|
|
219
165
|
/**
|
|
220
|
-
* Creates a file with
|
|
221
|
-
* that, if contents is specified, it will be mutated by the file
|
|
222
|
-
* @param _mode The mode that the file was opened using.
|
|
223
|
-
* Dictates permissions and where the file pointer starts.
|
|
224
|
-
* @param stats The stats object for the given file.
|
|
225
|
-
* PreloadFile will mutate this object. Note that this object must contain
|
|
226
|
-
* the appropriate mode that the file was opened as.
|
|
227
|
-
* @param buffer A buffer containing the entire
|
|
228
|
-
* contents of the file. PreloadFile will mutate this buffer. If not
|
|
229
|
-
* specified, we assume it is a new file.
|
|
166
|
+
* Creates a file with `path` and, optionally, the given contents.
|
|
167
|
+
* Note that, if contents is specified, it will be mutated by the file.
|
|
230
168
|
*/
|
|
231
169
|
constructor(
|
|
232
170
|
/**
|
|
233
171
|
* The file system that created the file.
|
|
234
172
|
* @internal
|
|
235
173
|
*/
|
|
236
|
-
fs: FS, path: string, flag: string, stats: Stats,
|
|
174
|
+
fs: FS, path: string, flag: string, stats: Stats,
|
|
175
|
+
/**
|
|
176
|
+
* A buffer containing the entire contents of the file.
|
|
177
|
+
*/
|
|
178
|
+
_buffer?: Uint8Array);
|
|
237
179
|
/**
|
|
238
180
|
* Get the underlying buffer for this file. Mutating not recommended and will mess up dirty tracking.
|
|
239
181
|
*/
|
|
@@ -242,84 +184,54 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
|
242
184
|
* Get the current file position.
|
|
243
185
|
*
|
|
244
186
|
* We emulate the following bug mentioned in the Node documentation:
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
* @
|
|
187
|
+
*
|
|
188
|
+
* On Linux, positional writes don't work when the file is opened in append mode.
|
|
189
|
+
* The kernel ignores the position argument and always appends the data to the end of the file.
|
|
190
|
+
* @returns The current file position.
|
|
249
191
|
*/
|
|
250
192
|
get position(): number;
|
|
251
|
-
|
|
252
|
-
* Set the file position.
|
|
253
|
-
* @param newPos new position
|
|
254
|
-
*/
|
|
255
|
-
set position(newPos: number);
|
|
193
|
+
set position(value: number);
|
|
256
194
|
sync(): Promise<void>;
|
|
257
195
|
syncSync(): void;
|
|
258
196
|
close(): Promise<void>;
|
|
259
197
|
closeSync(): void;
|
|
260
198
|
/**
|
|
261
|
-
* Cleans up
|
|
262
|
-
* This will *not* sync the file data to the FS
|
|
199
|
+
* Cleans up. This will *not* sync the file data to the FS
|
|
263
200
|
*/
|
|
264
201
|
protected dispose(force?: boolean): void;
|
|
265
|
-
/**
|
|
266
|
-
* Asynchronous `stat`.
|
|
267
|
-
*/
|
|
268
202
|
stat(): Promise<Stats>;
|
|
269
|
-
/**
|
|
270
|
-
* Synchronous `stat`.
|
|
271
|
-
*/
|
|
272
203
|
statSync(): Stats;
|
|
273
204
|
protected _truncate(length: number): void;
|
|
274
|
-
/**
|
|
275
|
-
* Asynchronous truncate.
|
|
276
|
-
* @param length
|
|
277
|
-
*/
|
|
278
205
|
truncate(length: number): Promise<void>;
|
|
279
|
-
/**
|
|
280
|
-
* Synchronous truncate.
|
|
281
|
-
* @param length
|
|
282
|
-
*/
|
|
283
206
|
truncateSync(length: number): void;
|
|
284
207
|
protected _write(buffer: Uint8Array, offset?: number, length?: number, position?: number): number;
|
|
285
208
|
/**
|
|
286
209
|
* Write buffer to the file.
|
|
287
|
-
*
|
|
288
|
-
* without waiting for the callback.
|
|
289
|
-
* @param buffer Uint8Array containing the data to write to
|
|
290
|
-
* the file.
|
|
210
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
291
211
|
* @param offset Offset in the buffer to start reading data from.
|
|
292
212
|
* @param length The amount of bytes to write to the file.
|
|
293
|
-
* @param position Offset from the beginning of the file where this
|
|
294
|
-
*
|
|
295
|
-
* the current position.
|
|
213
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
214
|
+
* If position is null, the data will be written at the current position.
|
|
296
215
|
*/
|
|
297
216
|
write(buffer: Uint8Array, offset?: number, length?: number, position?: number): Promise<number>;
|
|
298
217
|
/**
|
|
299
218
|
* Write buffer to the file.
|
|
300
|
-
*
|
|
301
|
-
* without waiting for the callback.
|
|
302
|
-
* @param buffer Uint8Array containing the data to write to
|
|
303
|
-
* the file.
|
|
219
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
304
220
|
* @param offset Offset in the buffer to start reading data from.
|
|
305
221
|
* @param length The amount of bytes to write to the file.
|
|
306
|
-
* @param position Offset from the beginning of the file where this
|
|
307
|
-
*
|
|
308
|
-
* the current position.
|
|
222
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
223
|
+
* If position is null, the data will be written at the current position.
|
|
309
224
|
* @returns bytes written
|
|
310
225
|
*/
|
|
311
226
|
writeSync(buffer: Uint8Array, offset?: number, length?: number, position?: number): number;
|
|
312
227
|
protected _read(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number;
|
|
313
228
|
/**
|
|
314
229
|
* Read data from the file.
|
|
315
|
-
* @param buffer The buffer that the data will be
|
|
316
|
-
*
|
|
317
|
-
* @param offset The offset within the buffer where writing will
|
|
318
|
-
* start.
|
|
230
|
+
* @param buffer The buffer that the data will be written to.
|
|
231
|
+
* @param offset The offset within the buffer where writing will start.
|
|
319
232
|
* @param length An integer specifying the number of bytes to read.
|
|
320
|
-
* @param position An integer specifying where to begin reading from
|
|
321
|
-
*
|
|
322
|
-
* position.
|
|
233
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
234
|
+
* If position is null, data will be read from the current file position.
|
|
323
235
|
*/
|
|
324
236
|
read<TBuffer extends ArrayBufferView>(buffer: TBuffer, offset?: number, length?: number, position?: number): Promise<{
|
|
325
237
|
bytesRead: number;
|
|
@@ -327,37 +239,17 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
|
327
239
|
}>;
|
|
328
240
|
/**
|
|
329
241
|
* Read data from the file.
|
|
330
|
-
* @param buffer The buffer that the data will be
|
|
331
|
-
* written to.
|
|
242
|
+
* @param buffer The buffer that the data will be written to.
|
|
332
243
|
* @param offset The offset within the buffer where writing will start.
|
|
333
244
|
* @param length An integer specifying the number of bytes to read.
|
|
334
|
-
* @param position An integer specifying where to begin reading from
|
|
335
|
-
*
|
|
336
|
-
* position.
|
|
245
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
246
|
+
* If position is null, data will be read from the current file position.
|
|
337
247
|
* @returns number of bytes written
|
|
338
248
|
*/
|
|
339
249
|
readSync(buffer: ArrayBufferView, offset?: number, length?: number, position?: number): number;
|
|
340
|
-
/**
|
|
341
|
-
* Asynchronous `fchmod`.
|
|
342
|
-
* @param mode the mode
|
|
343
|
-
*/
|
|
344
250
|
chmod(mode: number): Promise<void>;
|
|
345
|
-
/**
|
|
346
|
-
* Synchronous `fchmod`.
|
|
347
|
-
* @param mode
|
|
348
|
-
*/
|
|
349
251
|
chmodSync(mode: number): void;
|
|
350
|
-
/**
|
|
351
|
-
* Asynchronous `fchown`.
|
|
352
|
-
* @param uid
|
|
353
|
-
* @param gid
|
|
354
|
-
*/
|
|
355
252
|
chown(uid: number, gid: number): Promise<void>;
|
|
356
|
-
/**
|
|
357
|
-
* Synchronous `fchown`.
|
|
358
|
-
* @param uid
|
|
359
|
-
* @param gid
|
|
360
|
-
*/
|
|
361
253
|
chownSync(uid: number, gid: number): void;
|
|
362
254
|
utimes(atime: Date, mtime: Date): Promise<void>;
|
|
363
255
|
utimesSync(atime: Date, mtime: Date): void;
|
|
@@ -367,24 +259,11 @@ export declare class PreloadFile<FS extends FileSystem> extends File {
|
|
|
367
259
|
[Symbol.dispose](): void;
|
|
368
260
|
}
|
|
369
261
|
/**
|
|
370
|
-
* For the
|
|
262
|
+
* For the file systems which do not sync to anything.
|
|
371
263
|
*/
|
|
372
264
|
export declare class NoSyncFile<T extends FileSystem> extends PreloadFile<T> {
|
|
373
|
-
constructor(fs: T, path: string, flag: string, stats: Stats, contents?: Uint8Array);
|
|
374
|
-
/**
|
|
375
|
-
* Asynchronous sync. Doesn't do anything, simply calls the cb.
|
|
376
|
-
*/
|
|
377
265
|
sync(): Promise<void>;
|
|
378
|
-
/**
|
|
379
|
-
* Synchronous sync. Doesn't do anything.
|
|
380
|
-
*/
|
|
381
266
|
syncSync(): void;
|
|
382
|
-
/**
|
|
383
|
-
* Asynchronous close. Doesn't do anything, simply calls the cb.
|
|
384
|
-
*/
|
|
385
267
|
close(): Promise<void>;
|
|
386
|
-
/**
|
|
387
|
-
* Synchronous close. Doesn't do anything.
|
|
388
|
-
*/
|
|
389
268
|
closeSync(): void;
|
|
390
269
|
}
|
package/dist/file.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY, S_IFMT } from './emulation/constants.js';
|
|
1
|
+
import { O_APPEND, O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_SYNC, O_TRUNC, O_WRONLY, S_IFMT, size_max } from './emulation/constants.js';
|
|
2
2
|
import { Errno, ErrnoError } from './error.js';
|
|
3
|
-
import { size_max } from './inode.js';
|
|
4
3
|
import { Stats } from './stats.js';
|
|
5
4
|
import './polyfills.js';
|
|
6
5
|
const validFlags = ['r', 'r+', 'rs', 'rs+', 'w', 'wx', 'w+', 'wx+', 'a', 'ax', 'a+', 'ax+'];
|
|
@@ -110,11 +109,7 @@ export class File {
|
|
|
110
109
|
* @internal
|
|
111
110
|
* The file system that created the file
|
|
112
111
|
*/
|
|
113
|
-
fs,
|
|
114
|
-
/**
|
|
115
|
-
* The path to the file
|
|
116
|
-
*/
|
|
117
|
-
path) {
|
|
112
|
+
fs, path) {
|
|
118
113
|
this.fs = fs;
|
|
119
114
|
this.path = path;
|
|
120
115
|
}
|
|
@@ -125,16 +120,12 @@ export class File {
|
|
|
125
120
|
return this.closeSync();
|
|
126
121
|
}
|
|
127
122
|
/**
|
|
128
|
-
* Asynchronous `datasync`.
|
|
129
|
-
*
|
|
130
123
|
* Default implementation maps to `sync`.
|
|
131
124
|
*/
|
|
132
125
|
datasync() {
|
|
133
126
|
return this.sync();
|
|
134
127
|
}
|
|
135
128
|
/**
|
|
136
|
-
* Synchronous `datasync`.
|
|
137
|
-
*
|
|
138
129
|
* Default implementation maps to `syncSync`.
|
|
139
130
|
*/
|
|
140
131
|
datasyncSync() {
|
|
@@ -142,29 +133,24 @@ export class File {
|
|
|
142
133
|
}
|
|
143
134
|
}
|
|
144
135
|
/**
|
|
145
|
-
* An implementation of
|
|
146
|
-
*
|
|
147
|
-
*
|
|
136
|
+
* An implementation of `File` that operates completely in-memory.
|
|
137
|
+
* `PreloadFile`s are backed by a `Uint8Array`.
|
|
148
138
|
*/
|
|
149
139
|
export class PreloadFile extends File {
|
|
150
140
|
/**
|
|
151
|
-
* Creates a file with
|
|
152
|
-
* that, if contents is specified, it will be mutated by the file
|
|
153
|
-
* @param _mode The mode that the file was opened using.
|
|
154
|
-
* Dictates permissions and where the file pointer starts.
|
|
155
|
-
* @param stats The stats object for the given file.
|
|
156
|
-
* PreloadFile will mutate this object. Note that this object must contain
|
|
157
|
-
* the appropriate mode that the file was opened as.
|
|
158
|
-
* @param buffer A buffer containing the entire
|
|
159
|
-
* contents of the file. PreloadFile will mutate this buffer. If not
|
|
160
|
-
* specified, we assume it is a new file.
|
|
141
|
+
* Creates a file with `path` and, optionally, the given contents.
|
|
142
|
+
* Note that, if contents is specified, it will be mutated by the file.
|
|
161
143
|
*/
|
|
162
144
|
constructor(
|
|
163
145
|
/**
|
|
164
146
|
* The file system that created the file.
|
|
165
147
|
* @internal
|
|
166
148
|
*/
|
|
167
|
-
fs, path, flag, stats,
|
|
149
|
+
fs, path, flag, stats,
|
|
150
|
+
/**
|
|
151
|
+
* A buffer containing the entire contents of the file.
|
|
152
|
+
*/
|
|
153
|
+
_buffer = new Uint8Array(new ArrayBuffer(0, fs.metadata().noResizableBuffers ? {} : { maxByteLength: size_max }))) {
|
|
168
154
|
super(fs, path);
|
|
169
155
|
this.fs = fs;
|
|
170
156
|
this.flag = flag;
|
|
@@ -205,10 +191,10 @@ export class PreloadFile extends File {
|
|
|
205
191
|
* Get the current file position.
|
|
206
192
|
*
|
|
207
193
|
* We emulate the following bug mentioned in the Node documentation:
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* @
|
|
194
|
+
*
|
|
195
|
+
* On Linux, positional writes don't work when the file is opened in append mode.
|
|
196
|
+
* The kernel ignores the position argument and always appends the data to the end of the file.
|
|
197
|
+
* @returns The current file position.
|
|
212
198
|
*/
|
|
213
199
|
get position() {
|
|
214
200
|
if (isAppendable(this.flag)) {
|
|
@@ -216,12 +202,8 @@ export class PreloadFile extends File {
|
|
|
216
202
|
}
|
|
217
203
|
return this._position;
|
|
218
204
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
* @param newPos new position
|
|
222
|
-
*/
|
|
223
|
-
set position(newPos) {
|
|
224
|
-
this._position = newPos;
|
|
205
|
+
set position(value) {
|
|
206
|
+
this._position = value;
|
|
225
207
|
}
|
|
226
208
|
async sync() {
|
|
227
209
|
if (this.closed) {
|
|
@@ -258,8 +240,7 @@ export class PreloadFile extends File {
|
|
|
258
240
|
this.dispose();
|
|
259
241
|
}
|
|
260
242
|
/**
|
|
261
|
-
* Cleans up
|
|
262
|
-
* This will *not* sync the file data to the FS
|
|
243
|
+
* Cleans up. This will *not* sync the file data to the FS
|
|
263
244
|
*/
|
|
264
245
|
dispose(force) {
|
|
265
246
|
if (this.closed) {
|
|
@@ -274,18 +255,12 @@ export class PreloadFile extends File {
|
|
|
274
255
|
delete this.stats;
|
|
275
256
|
this.closed = true;
|
|
276
257
|
}
|
|
277
|
-
/**
|
|
278
|
-
* Asynchronous `stat`.
|
|
279
|
-
*/
|
|
280
258
|
stat() {
|
|
281
259
|
if (this.closed) {
|
|
282
260
|
throw ErrnoError.With('EBADF', this.path, 'File.stat');
|
|
283
261
|
}
|
|
284
262
|
return Promise.resolve(new Stats(this.stats));
|
|
285
263
|
}
|
|
286
|
-
/**
|
|
287
|
-
* Synchronous `stat`.
|
|
288
|
-
*/
|
|
289
264
|
statSync() {
|
|
290
265
|
if (this.closed) {
|
|
291
266
|
throw ErrnoError.With('EBADF', this.path, 'File.stat');
|
|
@@ -311,18 +286,10 @@ export class PreloadFile extends File {
|
|
|
311
286
|
// Truncate.
|
|
312
287
|
this._buffer = this._buffer.slice(0, length);
|
|
313
288
|
}
|
|
314
|
-
/**
|
|
315
|
-
* Asynchronous truncate.
|
|
316
|
-
* @param length
|
|
317
|
-
*/
|
|
318
289
|
async truncate(length) {
|
|
319
290
|
this._truncate(length);
|
|
320
291
|
await this.sync();
|
|
321
292
|
}
|
|
322
|
-
/**
|
|
323
|
-
* Synchronous truncate.
|
|
324
|
-
* @param length
|
|
325
|
-
*/
|
|
326
293
|
truncateSync(length) {
|
|
327
294
|
this._truncate(length);
|
|
328
295
|
this.syncSync();
|
|
@@ -358,15 +325,11 @@ export class PreloadFile extends File {
|
|
|
358
325
|
}
|
|
359
326
|
/**
|
|
360
327
|
* Write buffer to the file.
|
|
361
|
-
*
|
|
362
|
-
* without waiting for the callback.
|
|
363
|
-
* @param buffer Uint8Array containing the data to write to
|
|
364
|
-
* the file.
|
|
328
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
365
329
|
* @param offset Offset in the buffer to start reading data from.
|
|
366
330
|
* @param length The amount of bytes to write to the file.
|
|
367
|
-
* @param position Offset from the beginning of the file where this
|
|
368
|
-
*
|
|
369
|
-
* the current position.
|
|
331
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
332
|
+
* If position is null, the data will be written at the current position.
|
|
370
333
|
*/
|
|
371
334
|
async write(buffer, offset, length, position) {
|
|
372
335
|
const bytesWritten = this._write(buffer, offset, length, position);
|
|
@@ -375,15 +338,11 @@ export class PreloadFile extends File {
|
|
|
375
338
|
}
|
|
376
339
|
/**
|
|
377
340
|
* Write buffer to the file.
|
|
378
|
-
*
|
|
379
|
-
* without waiting for the callback.
|
|
380
|
-
* @param buffer Uint8Array containing the data to write to
|
|
381
|
-
* the file.
|
|
341
|
+
* @param buffer Uint8Array containing the data to write to the file.
|
|
382
342
|
* @param offset Offset in the buffer to start reading data from.
|
|
383
343
|
* @param length The amount of bytes to write to the file.
|
|
384
|
-
* @param position Offset from the beginning of the file where this
|
|
385
|
-
*
|
|
386
|
-
* the current position.
|
|
344
|
+
* @param position Offset from the beginning of the file where this data should be written.
|
|
345
|
+
* If position is null, the data will be written at the current position.
|
|
387
346
|
* @returns bytes written
|
|
388
347
|
*/
|
|
389
348
|
writeSync(buffer, offset = 0, length = this.stats.size, position = this.position) {
|
|
@@ -416,14 +375,11 @@ export class PreloadFile extends File {
|
|
|
416
375
|
}
|
|
417
376
|
/**
|
|
418
377
|
* Read data from the file.
|
|
419
|
-
* @param buffer The buffer that the data will be
|
|
420
|
-
*
|
|
421
|
-
* @param offset The offset within the buffer where writing will
|
|
422
|
-
* start.
|
|
378
|
+
* @param buffer The buffer that the data will be written to.
|
|
379
|
+
* @param offset The offset within the buffer where writing will start.
|
|
423
380
|
* @param length An integer specifying the number of bytes to read.
|
|
424
|
-
* @param position An integer specifying where to begin reading from
|
|
425
|
-
*
|
|
426
|
-
* position.
|
|
381
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
382
|
+
* If position is null, data will be read from the current file position.
|
|
427
383
|
*/
|
|
428
384
|
async read(buffer, offset, length, position) {
|
|
429
385
|
const bytesRead = this._read(buffer, offset, length, position);
|
|
@@ -432,13 +388,11 @@ export class PreloadFile extends File {
|
|
|
432
388
|
}
|
|
433
389
|
/**
|
|
434
390
|
* Read data from the file.
|
|
435
|
-
* @param buffer The buffer that the data will be
|
|
436
|
-
* written to.
|
|
391
|
+
* @param buffer The buffer that the data will be written to.
|
|
437
392
|
* @param offset The offset within the buffer where writing will start.
|
|
438
393
|
* @param length An integer specifying the number of bytes to read.
|
|
439
|
-
* @param position An integer specifying where to begin reading from
|
|
440
|
-
*
|
|
441
|
-
* position.
|
|
394
|
+
* @param position An integer specifying where to begin reading from in the file.
|
|
395
|
+
* If position is null, data will be read from the current file position.
|
|
442
396
|
* @returns number of bytes written
|
|
443
397
|
*/
|
|
444
398
|
readSync(buffer, offset, length, position) {
|
|
@@ -446,10 +400,6 @@ export class PreloadFile extends File {
|
|
|
446
400
|
this.statSync();
|
|
447
401
|
return bytesRead;
|
|
448
402
|
}
|
|
449
|
-
/**
|
|
450
|
-
* Asynchronous `fchmod`.
|
|
451
|
-
* @param mode the mode
|
|
452
|
-
*/
|
|
453
403
|
async chmod(mode) {
|
|
454
404
|
if (this.closed) {
|
|
455
405
|
throw ErrnoError.With('EBADF', this.path, 'File.chmod');
|
|
@@ -458,10 +408,6 @@ export class PreloadFile extends File {
|
|
|
458
408
|
this.stats.chmod(mode);
|
|
459
409
|
await this.sync();
|
|
460
410
|
}
|
|
461
|
-
/**
|
|
462
|
-
* Synchronous `fchmod`.
|
|
463
|
-
* @param mode
|
|
464
|
-
*/
|
|
465
411
|
chmodSync(mode) {
|
|
466
412
|
if (this.closed) {
|
|
467
413
|
throw ErrnoError.With('EBADF', this.path, 'File.chmod');
|
|
@@ -470,11 +416,6 @@ export class PreloadFile extends File {
|
|
|
470
416
|
this.stats.chmod(mode);
|
|
471
417
|
this.syncSync();
|
|
472
418
|
}
|
|
473
|
-
/**
|
|
474
|
-
* Asynchronous `fchown`.
|
|
475
|
-
* @param uid
|
|
476
|
-
* @param gid
|
|
477
|
-
*/
|
|
478
419
|
async chown(uid, gid) {
|
|
479
420
|
if (this.closed) {
|
|
480
421
|
throw ErrnoError.With('EBADF', this.path, 'File.chown');
|
|
@@ -483,11 +424,6 @@ export class PreloadFile extends File {
|
|
|
483
424
|
this.stats.chown(uid, gid);
|
|
484
425
|
await this.sync();
|
|
485
426
|
}
|
|
486
|
-
/**
|
|
487
|
-
* Synchronous `fchown`.
|
|
488
|
-
* @param uid
|
|
489
|
-
* @param gid
|
|
490
|
-
*/
|
|
491
427
|
chownSync(uid, gid) {
|
|
492
428
|
if (this.closed) {
|
|
493
429
|
throw ErrnoError.With('EBADF', this.path, 'File.chown');
|
|
@@ -538,34 +474,15 @@ export class PreloadFile extends File {
|
|
|
538
474
|
}
|
|
539
475
|
}
|
|
540
476
|
/**
|
|
541
|
-
* For the
|
|
477
|
+
* For the file systems which do not sync to anything.
|
|
542
478
|
*/
|
|
543
479
|
export class NoSyncFile extends PreloadFile {
|
|
544
|
-
constructor(fs, path, flag, stats, contents) {
|
|
545
|
-
super(fs, path, flag, stats, contents);
|
|
546
|
-
}
|
|
547
|
-
/**
|
|
548
|
-
* Asynchronous sync. Doesn't do anything, simply calls the cb.
|
|
549
|
-
*/
|
|
550
480
|
sync() {
|
|
551
481
|
return Promise.resolve();
|
|
552
482
|
}
|
|
553
|
-
|
|
554
|
-
* Synchronous sync. Doesn't do anything.
|
|
555
|
-
*/
|
|
556
|
-
syncSync() {
|
|
557
|
-
// NOP.
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Asynchronous close. Doesn't do anything, simply calls the cb.
|
|
561
|
-
*/
|
|
483
|
+
syncSync() { }
|
|
562
484
|
close() {
|
|
563
485
|
return Promise.resolve();
|
|
564
486
|
}
|
|
565
|
-
|
|
566
|
-
* Synchronous close. Doesn't do anything.
|
|
567
|
-
*/
|
|
568
|
-
closeSync() {
|
|
569
|
-
// NOP.
|
|
570
|
-
}
|
|
487
|
+
closeSync() { }
|
|
571
488
|
}
|