forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncMirror.ts
More file actions
340 lines (311 loc) · 9.76 KB
/
AsyncMirror.ts
File metadata and controls
340 lines (311 loc) · 9.76 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
import {FileSystem, SynchronousFileSystem, BFSOneArgCallback, BFSCallback, FileSystemOptions} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import {FileFlag} from '../core/file_flag';
import {File} from '../core/file';
import Stats from '../core/node_fs_stats';
import PreloadFile from '../generic/preload_file';
import * as path from 'path';
/**
* @hidden
*/
interface IAsyncOperation {
apiMethod: string;
arguments: any[];
}
/**
* We define our own file to interpose on syncSync() for mirroring purposes.
*/
class MirrorFile extends PreloadFile<AsyncMirror> implements File {
constructor(fs: AsyncMirror, path: string, flag: FileFlag, stat: Stats, data: Buffer) {
super(fs, path, flag, stat, data);
}
public syncSync(): void {
if (this.isDirty()) {
this._fs._syncSync(this);
this.resetDirty();
}
}
public closeSync(): void {
this.syncSync();
}
}
/**
* Configuration options for the AsyncMirror file system.
*/
export interface AsyncMirrorOptions {
// The synchronous file system to mirror the asynchronous file system to.
sync: FileSystem;
// The asynchronous file system to mirror.
async: FileSystem;
}
/**
* AsyncMirrorFS mirrors a synchronous filesystem into an asynchronous filesystem
* by:
*
* * Performing operations over the in-memory copy, while asynchronously pipelining them
* to the backing store.
* * During application loading, the contents of the async file system can be reloaded into
* the synchronous store, if desired.
*
* The two stores will be kept in sync. The most common use-case is to pair a synchronous
* in-memory filesystem with an asynchronous backing store.
*
* Example: Mirroring an IndexedDB file system to an in memory file system. Now, you can use
* IndexedDB synchronously.
*
* ```javascript
* BrowserFS.configure({
* fs: "AsyncMirror",
* options: {
* sync: { fs: "InMemory" },
* async: { fs: "IndexedDB" }
* }
* }, function(e) {
* // BrowserFS is initialized and ready-to-use!
* });
* ```
*
* Or, alternatively:
*
* ```javascript
* BrowserFS.FileSystem.IndexedDB.Create(function(e, idbfs) {
* BrowserFS.FileSystem.InMemory.Create(function(e, inMemory) {
* BrowserFS.FileSystem.AsyncMirror({
* sync: inMemory, async: idbfs
* }, function(e, mirrored) {
* BrowserFS.initialize(mirrored);
* });
* });
* });
* ```
*/
export default class AsyncMirror extends SynchronousFileSystem implements FileSystem {
public static readonly Name = "AsyncMirror";
public static readonly Options: FileSystemOptions = {
sync: {
type: "object",
description: "The synchronous file system to mirror the asynchronous file system to.",
validator: (v: any, cb: BFSOneArgCallback) => {
if (v && typeof(v['supportsSynch']) === "function" && v.supportsSynch()) {
cb();
} else {
cb(new ApiError(ErrorCode.EINVAL, `'sync' option must be a file system that supports synchronous operations`));
}
}
},
async: {
type: "object",
description: "The asynchronous file system to mirror."
}
};
/**
* Constructs and initializes an AsyncMirror file system with the given options.
*/
public static Create(opts: AsyncMirrorOptions, cb: BFSCallback<AsyncMirror>): void {
try {
const fs = new AsyncMirror(opts.sync, opts.async);
fs._initialize((e?) => {
if (e) {
cb(e);
} else {
cb(null, fs);
}
});
} catch (e) {
cb(e);
}
}
public static isAvailable(): boolean {
return true;
}
/**
* Queue of pending asynchronous operations.
*/
private _queue: IAsyncOperation[] = [];
private _queueRunning: boolean = false;
private _sync: FileSystem;
private _async: FileSystem;
private _isInitialized: boolean = false;
private _initializeCallbacks: ((e?: ApiError) => void)[] = [];
/**
* **Deprecated; use AsyncMirror.Create() method instead.**
*
* Mirrors the synchronous file system into the asynchronous file system.
*
* **IMPORTANT**: You must call `initialize` on the file system before it can be used.
* @param sync The synchronous file system to mirror the asynchronous file system to.
* @param async The asynchronous file system to mirror.
*/
constructor(sync: FileSystem, async: FileSystem) {
super();
this._sync = sync;
this._async = async;
}
public getName(): string {
return AsyncMirror.Name;
}
public _syncSync(fd: PreloadFile<any>) {
this._sync.writeFileSync(fd.getPath(), fd.getBuffer(), null, FileFlag.getFileFlag('w'), fd.getStats().mode);
this.enqueueOp({
apiMethod: 'writeFile',
arguments: [fd.getPath(), fd.getBuffer(), null, fd.getFlag(), fd.getStats().mode]
});
}
public isReadOnly(): boolean { return false; }
public supportsSynch(): boolean { return true; }
public supportsLinks(): boolean { return false; }
public supportsProps(): boolean { return this._sync.supportsProps() && this._async.supportsProps(); }
public renameSync(oldPath: string, newPath: string): void {
this._sync.renameSync(oldPath, newPath);
this.enqueueOp({
apiMethod: 'rename',
arguments: [oldPath, newPath]
});
}
public statSync(p: string, isLstat: boolean): Stats {
return this._sync.statSync(p, isLstat);
}
public openSync(p: string, flag: FileFlag, mode: number): File {
// Sanity check: Is this open/close permitted?
const fd = this._sync.openSync(p, flag, mode);
fd.closeSync();
return new MirrorFile(this, p, flag, this._sync.statSync(p, false), this._sync.readFileSync(p, null, FileFlag.getFileFlag('r')));
}
public unlinkSync(p: string): void {
this._sync.unlinkSync(p);
this.enqueueOp({
apiMethod: 'unlink',
arguments: [p]
});
}
public rmdirSync(p: string): void {
this._sync.rmdirSync(p);
this.enqueueOp({
apiMethod: 'rmdir',
arguments: [p]
});
}
public mkdirSync(p: string, mode: number): void {
this._sync.mkdirSync(p, mode);
this.enqueueOp({
apiMethod: 'mkdir',
arguments: [p, mode]
});
}
public readdirSync(p: string): string[] {
return this._sync.readdirSync(p);
}
public existsSync(p: string): boolean {
return this._sync.existsSync(p);
}
public chmodSync(p: string, isLchmod: boolean, mode: number): void {
this._sync.chmodSync(p, isLchmod, mode);
this.enqueueOp({
apiMethod: 'chmod',
arguments: [p, isLchmod, mode]
});
}
public chownSync(p: string, isLchown: boolean, uid: number, gid: number): void {
this._sync.chownSync(p, isLchown, uid, gid);
this.enqueueOp({
apiMethod: 'chown',
arguments: [p, isLchown, uid, gid]
});
}
public utimesSync(p: string, atime: Date, mtime: Date): void {
this._sync.utimesSync(p, atime, mtime);
this.enqueueOp({
apiMethod: 'utimes',
arguments: [p, atime, mtime]
});
}
/**
* Called once to load up files from async storage into sync storage.
*/
private _initialize(userCb: BFSOneArgCallback): void {
const callbacks = this._initializeCallbacks;
const end = (e?: ApiError): void => {
this._isInitialized = !e;
this._initializeCallbacks = [];
callbacks.forEach((cb) => cb(e));
};
if (!this._isInitialized) {
// First call triggers initialization, the rest wait.
if (callbacks.push(userCb) === 1) {
const copyDirectory = (p: string, mode: number, cb: BFSOneArgCallback) => {
if (p !== '/') {
this._sync.mkdirSync(p, mode);
}
this._async.readdir(p, (err, files) => {
let i = 0;
// NOTE: This function must not be in a lexically nested statement,
// such as an if or while statement. Safari refuses to run the
// script since it is undefined behavior.
function copyNextFile(err?: ApiError) {
if (err) {
cb(err);
} else if (i < files!.length) {
copyItem(path.join(p, files![i]), copyNextFile);
i++;
} else {
cb();
}
}
if (err) {
cb(err);
} else {
copyNextFile();
}
});
}, copyFile = (p: string, mode: number, cb: BFSOneArgCallback) => {
this._async.readFile(p, null, FileFlag.getFileFlag('r'), (err, data) => {
if (err) {
cb(err);
} else {
try {
this._sync.writeFileSync(p, data!, null, FileFlag.getFileFlag('w'), mode);
} catch (e) {
err = e;
} finally {
cb(err);
}
}
});
}, copyItem = (p: string, cb: BFSOneArgCallback) => {
this._async.stat(p, false, (err, stats) => {
if (err) {
cb(err);
} else if (stats!.isDirectory()) {
copyDirectory(p, stats!.mode, cb);
} else {
copyFile(p, stats!.mode, cb);
}
});
};
copyDirectory('/', 0, end);
}
} else {
userCb();
}
}
private enqueueOp(op: IAsyncOperation) {
this._queue.push(op);
if (!this._queueRunning) {
this._queueRunning = true;
const doNextOp = (err?: ApiError) => {
if (err) {
throw new Error(`WARNING: File system has desynchronized. Received following error: ${err}\n$`);
}
if (this._queue.length > 0) {
const op = this._queue.shift()!,
args = op.arguments;
args.push(doNextOp);
(<Function> (<any> this._async)[op.apiMethod]).apply(this._async, args);
} else {
this._queueRunning = false;
}
};
doNextOp();
}
}
}