forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload_file.ts
More file actions
407 lines (382 loc) · 12.2 KB
/
preload_file.ts
File metadata and controls
407 lines (382 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import {BaseFile, File} from '../core/file';
import {FileSystem, BFSOneArgCallback, BFSCallback, BFSThreeArgCallback} from '../core/file_system';
import Stats from '../core/node_fs_stats';
import {FileFlag} from '../core/file_flag';
import {ApiError, ErrorCode} from '../core/api_error';
import fs from '../core/node_fs';
import {emptyBuffer} from '../core/util';
/**
* An implementation of the File interface that operates on a file that is
* completely in-memory. PreloadFiles are backed by a Buffer.
*
* This is also an abstract class, as it lacks an implementation of 'sync' and
* 'close'. Each filesystem that wishes to use this file representation must
* extend this class and implement those two methods.
* @todo 'close' lever that disables functionality once closed.
*/
export default class PreloadFile<T extends FileSystem> extends BaseFile {
protected _fs: T;
private _pos: number = 0;
private _path: string;
private _stat: Stats;
private _flag: FileFlag;
private _buffer: Buffer;
private _dirty: boolean = false;
/**
* Creates a file with the given path and, optionally, the given contents. Note
* that, if contents is specified, it will be mutated by the file!
* @param _fs The file system that created the file.
* @param _path
* @param _mode The mode that the file was opened using.
* Dictates permissions and where the file pointer starts.
* @param _stat The stats object for the given file.
* PreloadFile will mutate this object. Note that this object must contain
* the appropriate mode that the file was opened as.
* @param contents A buffer containing the entire
* contents of the file. PreloadFile will mutate this buffer. If not
* specified, we assume it is a new file.
*/
constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer) {
super();
this._fs = _fs;
this._path = _path;
this._flag = _flag;
this._stat = _stat;
this._buffer = contents ? contents : emptyBuffer();
// Note: This invariant is *not* maintained once the file starts getting
// modified.
// Note: Only actually matters if file is readable, as writeable modes may
// truncate/append to file.
if (this._stat.size !== this._buffer.length && this._flag.isReadable()) {
throw new Error(`Invalid buffer: Buffer is ${this._buffer.length} long, yet Stats object specifies that file is ${this._stat.size} long.`);
}
}
/**
* NONSTANDARD: Get the underlying buffer for this file. !!DO NOT MUTATE!! Will mess up dirty tracking.
*/
public getBuffer(): Buffer {
return this._buffer;
}
/**
* NONSTANDARD: Get underlying stats for this file. !!DO NOT MUTATE!!
*/
public getStats(): Stats {
return this._stat;
}
public getFlag(): FileFlag {
return this._flag;
}
/**
* Get the path to this file.
* @return [String] The path to the file.
*/
public getPath(): string {
return this._path;
}
/**
* Get the current file position.
*
* We emulate the following bug mentioned in the Node documentation:
* > On Linux, positional writes don't work when the file is opened in append
* mode. The kernel ignores the position argument and always appends the data
* to the end of the file.
* @return [Number] The current file position.
*/
public getPos(): number {
if (this._flag.isAppendable()) {
return this._stat.size;
}
return this._pos;
}
/**
* Advance the current file position by the indicated number of positions.
* @param [Number] delta
*/
public advancePos(delta: number): number {
return this._pos += delta;
}
/**
* Set the file position.
* @param [Number] newPos
*/
public setPos(newPos: number): number {
return this._pos = newPos;
}
/**
* **Core**: Asynchronous sync. Must be implemented by subclasses of this
* class.
* @param [Function(BrowserFS.ApiError)] cb
*/
public sync(cb: BFSOneArgCallback): void {
try {
this.syncSync();
cb();
} catch (e) {
cb(e);
}
}
/**
* **Core**: Synchronous sync.
*/
public syncSync(): void {
throw new ApiError(ErrorCode.ENOTSUP);
}
/**
* **Core**: Asynchronous close. Must be implemented by subclasses of this
* class.
* @param [Function(BrowserFS.ApiError)] cb
*/
public close(cb: BFSOneArgCallback): void {
try {
this.closeSync();
cb();
} catch (e) {
cb(e);
}
}
/**
* **Core**: Synchronous close.
*/
public closeSync(): void {
throw new ApiError(ErrorCode.ENOTSUP);
}
/**
* Asynchronous `stat`.
* @param [Function(BrowserFS.ApiError, BrowserFS.node.fs.Stats)] cb
*/
public stat(cb: BFSCallback<Stats>): void {
try {
cb(null, Stats.clone(this._stat));
} catch (e) {
cb(e);
}
}
/**
* Synchronous `stat`.
*/
public statSync(): Stats {
return Stats.clone(this._stat);
}
/**
* Asynchronous truncate.
* @param [Number] len
* @param [Function(BrowserFS.ApiError)] cb
*/
public truncate(len: number, cb: BFSOneArgCallback): void {
try {
this.truncateSync(len);
if (this._flag.isSynchronous() && !fs.getRootFS()!.supportsSynch()) {
this.sync(cb);
}
cb();
} catch (e) {
return cb(e);
}
}
/**
* Synchronous truncate.
* @param [Number] len
*/
public truncateSync(len: number): void {
this._dirty = true;
if (!this._flag.isWriteable()) {
throw new ApiError(ErrorCode.EPERM, 'File not opened with a writeable mode.');
}
this._stat.mtimeMs = Date.now();
if (len > this._buffer.length) {
const buf = Buffer.alloc(len - this._buffer.length, 0);
// Write will set @_stat.size for us.
this.writeSync(buf, 0, buf.length, this._buffer.length);
if (this._flag.isSynchronous() && fs.getRootFS()!.supportsSynch()) {
this.syncSync();
}
return;
}
this._stat.size = len;
// Truncate buffer to 'len'.
const newBuff = Buffer.alloc(len);
this._buffer.copy(newBuff, 0, 0, len);
this._buffer = newBuff;
if (this._flag.isSynchronous() && fs.getRootFS()!.supportsSynch()) {
this.syncSync();
}
}
/**
* Write buffer to the file.
* Note that it is unsafe to use fs.write multiple times on the same file
* without waiting for the callback.
* @param [BrowserFS.node.Buffer] buffer Buffer containing the data to write to
* the file.
* @param [Number] offset Offset in the buffer to start reading data from.
* @param [Number] length The amount of bytes to write to the file.
* @param [Number] position Offset from the beginning of the file where this
* data should be written. If position is null, the data will be written at
* the current position.
* @param [Function(BrowserFS.ApiError, Number, BrowserFS.node.Buffer)]
* cb The number specifies the number of bytes written into the file.
*/
public write(buffer: Buffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
try {
cb(null, this.writeSync(buffer, offset, length, position), buffer);
} catch (e) {
cb(e);
}
}
/**
* Write buffer to the file.
* Note that it is unsafe to use fs.writeSync multiple times on the same file
* without waiting for the callback.
* @param [BrowserFS.node.Buffer] buffer Buffer containing the data to write to
* the file.
* @param [Number] offset Offset in the buffer to start reading data from.
* @param [Number] length The amount of bytes to write to the file.
* @param [Number] position Offset from the beginning of the file where this
* data should be written. If position is null, the data will be written at
* the current position.
* @return [Number]
*/
public writeSync(buffer: Buffer, offset: number, length: number, position: number): number {
this._dirty = true;
if (position === undefined || position === null) {
position = this.getPos();
}
if (!this._flag.isWriteable()) {
throw new ApiError(ErrorCode.EPERM, 'File not opened with a writeable mode.');
}
const endFp = position + length;
if (endFp > this._stat.size) {
this._stat.size = endFp;
if (endFp > this._buffer.length) {
// Extend the buffer!
const newBuff = Buffer.alloc(endFp);
this._buffer.copy(newBuff);
this._buffer = newBuff;
}
}
const len = buffer.copy(this._buffer, position, offset, offset + length);
this._stat.mtimeMs = Date.now();
if (this._flag.isSynchronous()) {
this.syncSync();
return len;
}
this.setPos(position + len);
return len;
}
/**
* Read data from the file.
* @param [BrowserFS.node.Buffer] buffer The buffer that the data will be
* written to.
* @param [Number] offset The offset within the buffer where writing will
* start.
* @param [Number] length An integer specifying the number of bytes to read.
* @param [Number] position An integer specifying where to begin reading from
* in the file. If position is null, data will be read from the current file
* position.
* @param [Function(BrowserFS.ApiError, Number, BrowserFS.node.Buffer)] cb The
* number is the number of bytes read
*/
public read(buffer: Buffer, offset: number, length: number, position: number, cb: BFSThreeArgCallback<number, Buffer>): void {
try {
cb(null, this.readSync(buffer, offset, length, position), buffer);
} catch (e) {
cb(e);
}
}
/**
* Read data from the file.
* @param [BrowserFS.node.Buffer] buffer The buffer that the data will be
* written to.
* @param [Number] offset The offset within the buffer where writing will
* start.
* @param [Number] length An integer specifying the number of bytes to read.
* @param [Number] position An integer specifying where to begin reading from
* in the file. If position is null, data will be read from the current file
* position.
* @return [Number]
*/
public readSync(buffer: Buffer, offset: number, length: number, position: number): number {
if (!this._flag.isReadable()) {
throw new ApiError(ErrorCode.EPERM, 'File not opened with a readable mode.');
}
if (position === undefined || position === null) {
position = this.getPos();
}
const endRead = position + length;
if (endRead > this._stat.size) {
length = this._stat.size - position;
}
const rv = this._buffer.copy(buffer, offset, position, position + length);
this._stat.atimeMs = Date.now();
this._pos = position + length;
return rv;
}
/**
* Asynchronous `fchmod`.
* @param [Number|String] mode
* @param [Function(BrowserFS.ApiError)] cb
*/
public chmod(mode: number, cb: BFSOneArgCallback): void {
try {
this.chmodSync(mode);
cb();
} catch (e) {
cb(e);
}
}
/**
* Asynchronous `fchmod`.
* @param [Number] mode
*/
public chmodSync(mode: number): void {
if (!this._fs.supportsProps()) {
throw new ApiError(ErrorCode.ENOTSUP);
}
this._dirty = true;
this._stat.chmod(mode);
this.syncSync();
}
protected isDirty(): boolean {
return this._dirty;
}
/**
* Resets the dirty bit. Should only be called after a sync has completed successfully.
*/
protected resetDirty() {
this._dirty = false;
}
}
/**
* File class for the InMemory and XHR file systems.
* Doesn't sync to anything, so it works nicely for memory-only files.
*/
export class NoSyncFile<T extends FileSystem> extends PreloadFile<T> implements File {
constructor(_fs: T, _path: string, _flag: FileFlag, _stat: Stats, contents?: Buffer) {
super(_fs, _path, _flag, _stat, contents);
}
/**
* Asynchronous sync. Doesn't do anything, simply calls the cb.
* @param [Function(BrowserFS.ApiError)] cb
*/
public sync(cb: BFSOneArgCallback): void {
cb();
}
/**
* Synchronous sync. Doesn't do anything.
*/
public syncSync(): void {
// NOP.
}
/**
* Asynchronous close. Doesn't do anything, simply calls the cb.
* @param [Function(BrowserFS.ApiError)] cb
*/
public close(cb: BFSOneArgCallback): void {
cb();
}
/**
* Synchronous close. Doesn't do anything.
*/
public closeSync(): void {
// NOP.
}
}