forked from jvilk/BrowserFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerFS.ts
889 lines (820 loc) · 25.2 KB
/
WorkerFS.ts
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
import {BaseFileSystem, FileSystem, BFSOneArgCallback, BFSCallback, FileSystemOptions} from '../core/file_system';
import {ApiError, ErrorCode} from '../core/api_error';
import {FileFlag} from '../core/file_flag';
import {buffer2ArrayBuffer, arrayBuffer2Buffer, emptyBuffer} from '../core/util';
import {File, BaseFile} from '../core/file';
import {default as Stats} from '../core/node_fs_stats';
import PreloadFile from '../generic/preload_file';
import global from '../core/global';
import fs from '../core/node_fs';
/**
* @hidden
*/
declare const importScripts: Function;
/**
* @hidden
*/
interface IBrowserFSMessage {
browserfsMessage: boolean;
}
/**
* @hidden
*/
enum SpecialArgType {
// Callback
CB,
// File descriptor
FD,
// API error
API_ERROR,
// Stats object
STATS,
// Initial probe for file system information.
PROBE,
// FileFlag object.
FILEFLAG,
// Buffer object.
BUFFER,
// Generic Error object.
ERROR
}
/**
* @hidden
*/
interface ISpecialArgument {
type: SpecialArgType;
}
/**
* @hidden
*/
interface IProbeResponse extends ISpecialArgument {
isReadOnly: boolean;
supportsLinks: boolean;
supportsProps: boolean;
}
/**
* @hidden
*/
interface ICallbackArgument extends ISpecialArgument {
// The callback ID.
id: number;
}
/**
* Converts callback arguments into ICallbackArgument objects, and back
* again.
* @hidden
*/
class CallbackArgumentConverter {
private _callbacks: { [id: number]: Function } = {};
private _nextId: number = 0;
public toRemoteArg(cb: Function): ICallbackArgument {
const id = this._nextId++;
this._callbacks[id] = cb;
return {
type: SpecialArgType.CB,
id: id
};
}
public toLocalArg(id: number): Function {
const cb = this._callbacks[id];
delete this._callbacks[id];
return cb;
}
}
/**
* @hidden
*/
interface IFileDescriptorArgument extends ISpecialArgument {
// The file descriptor's id on the remote side.
id: number;
// The entire file's data, as an array buffer.
data: ArrayBuffer | SharedArrayBuffer;
// The file's stat object, as an array buffer.
stat: ArrayBuffer | SharedArrayBuffer;
// The path to the file.
path: string;
// The flag of the open file descriptor.
flag: string;
}
/**
* @hidden
*/
class FileDescriptorArgumentConverter {
private _fileDescriptors: { [id: number]: File } = {};
private _nextId: number = 0;
public toRemoteArg(fd: File, p: string, flag: FileFlag, cb: BFSCallback<IFileDescriptorArgument>): void {
const id = this._nextId++;
let data: ArrayBuffer | SharedArrayBuffer;
let stat: ArrayBuffer | SharedArrayBuffer;
this._fileDescriptors[id] = fd;
// Extract needed information asynchronously.
fd.stat((err, stats) => {
if (err) {
cb(err);
} else {
stat = bufferToTransferrableObject(stats!.toBuffer());
// If it's a readable flag, we need to grab contents.
if (flag.isReadable()) {
fd.read(Buffer.alloc(stats!.size), 0, stats!.size, 0, (err?: ApiError | null, bytesRead?: number, buff?: Buffer) => {
if (err) {
cb(err);
} else {
data = bufferToTransferrableObject(buff!);
cb(null, {
type: SpecialArgType.FD,
id: id,
data: data,
stat: stat,
path: p,
flag: flag.getFlagString()
});
}
});
} else {
// File is not readable, which means writing to it will append or
// truncate/replace existing contents. Return an empty arraybuffer.
cb(null, {
type: SpecialArgType.FD,
id: id,
data: new ArrayBuffer(0),
stat: stat,
path: p,
flag: flag.getFlagString()
});
}
}
});
}
public applyFdAPIRequest(request: IAPIRequest, cb: BFSOneArgCallback): void {
const fdArg = <IFileDescriptorArgument> request.args[0];
this._applyFdChanges(fdArg, (err, fd?) => {
if (err) {
cb(err);
} else {
// Apply method on now-changed file descriptor.
(<any> fd)[request.method]((e?: ApiError) => {
if (request.method === 'close') {
delete this._fileDescriptors[fdArg.id];
}
cb(e);
});
}
});
}
private _applyFdChanges(remoteFd: IFileDescriptorArgument, cb: BFSCallback<File>): void {
const fd = this._fileDescriptors[remoteFd.id],
data = transferrableObjectToBuffer(remoteFd.data),
remoteStats = Stats.fromBuffer(transferrableObjectToBuffer(remoteFd.stat));
// Write data if the file is writable.
const flag = FileFlag.getFileFlag(remoteFd.flag);
if (flag.isWriteable()) {
// Appendable: Write to end of file.
// Writeable: Replace entire contents of file.
fd.write(data, 0, data.length, flag.isAppendable() ? fd.getPos()! : 0, (e?: ApiError | null) => {
function applyStatChanges() {
// Check if mode changed.
fd.stat((e, stats?) => {
if (e) {
cb(e);
} else {
if (stats!.mode !== remoteStats.mode) {
fd.chmod(remoteStats.mode, (e: any) => {
cb(e, fd);
});
} else {
cb(e, fd);
}
}
});
}
if (e) {
cb(e);
} else {
// If writeable & not appendable, we need to ensure file contents are
// identical to those from the remote FD. Thus, we truncate to the
// length of the remote file.
if (!flag.isAppendable()) {
fd.truncate(data.length, () => {
applyStatChanges();
});
} else {
applyStatChanges();
}
}
});
} else {
cb(null, fd);
}
}
}
/**
* @hidden
*/
interface IAPIErrorArgument extends ISpecialArgument {
// The error object, as an array buffer.
errorData: ArrayBuffer | SharedArrayBuffer;
}
/**
* @hidden
*/
function apiErrorLocal2Remote(e: ApiError): IAPIErrorArgument {
return {
type: SpecialArgType.API_ERROR,
errorData: bufferToTransferrableObject(e.writeToBuffer())
};
}
/**
* @hidden
*/
function apiErrorRemote2Local(e: IAPIErrorArgument): ApiError {
return ApiError.fromBuffer(transferrableObjectToBuffer(e.errorData));
}
/**
* @hidden
*/
interface IErrorArgument extends ISpecialArgument {
// The name of the error (e.g. 'TypeError').
name: string;
// The message associated with the error.
message: string;
// The stack associated with the error.
stack: string;
}
/**
* @hidden
*/
function errorLocal2Remote(e: Error): IErrorArgument {
return {
type: SpecialArgType.ERROR,
name: e.name,
message: e.message,
stack: e.stack!
};
}
/**
* @hidden
*/
function errorRemote2Local(e: IErrorArgument): Error {
let cnstr: {
new (msg: string): Error;
} = global[e.name];
if (typeof(cnstr) !== 'function') {
cnstr = Error;
}
const err = new cnstr(e.message);
err.stack = e.stack;
return err;
}
/**
* @hidden
*/
interface IStatsArgument extends ISpecialArgument {
// The stats object as an array buffer.
statsData: ArrayBuffer | SharedArrayBuffer;
}
/**
* @hidden
*/
function statsLocal2Remote(stats: Stats): IStatsArgument {
return {
type: SpecialArgType.STATS,
statsData: bufferToTransferrableObject(stats.toBuffer())
};
}
/**
* @hidden
*/
function statsRemote2Local(stats: IStatsArgument): Stats {
return Stats.fromBuffer(transferrableObjectToBuffer(stats.statsData));
}
/**
* @hidden
*/
interface IFileFlagArgument extends ISpecialArgument {
flagStr: string;
}
/**
* @hidden
*/
function fileFlagLocal2Remote(flag: FileFlag): IFileFlagArgument {
return {
type: SpecialArgType.FILEFLAG,
flagStr: flag.getFlagString()
};
}
/**
* @hidden
*/
function fileFlagRemote2Local(remoteFlag: IFileFlagArgument): FileFlag {
return FileFlag.getFileFlag(remoteFlag.flagStr);
}
/**
* @hidden
*/
interface IBufferArgument extends ISpecialArgument {
data: ArrayBuffer | SharedArrayBuffer;
}
/**
* @hidden
*/
function bufferToTransferrableObject(buff: Buffer): ArrayBuffer | SharedArrayBuffer {
return buffer2ArrayBuffer(buff);
}
/**
* @hidden
*/
function transferrableObjectToBuffer(buff: ArrayBuffer | SharedArrayBuffer): Buffer {
return arrayBuffer2Buffer(buff);
}
/**
* @hidden
*/
function bufferLocal2Remote(buff: Buffer): IBufferArgument {
return {
type: SpecialArgType.BUFFER,
data: bufferToTransferrableObject(buff)
};
}
/**
* @hidden
*/
function bufferRemote2Local(buffArg: IBufferArgument): Buffer {
return transferrableObjectToBuffer(buffArg.data);
}
/**
* @hidden
*/
interface IAPIRequest extends IBrowserFSMessage {
method: string;
args: Array<number | string | ISpecialArgument>;
}
/**
* @hidden
*/
function isAPIRequest(data: any): data is IAPIRequest {
return data && typeof data === 'object' && data.hasOwnProperty('browserfsMessage') && data['browserfsMessage'];
}
/**
* @hidden
*/
interface IAPIResponse extends IBrowserFSMessage {
cbId: number;
args: Array<number | string | ISpecialArgument>;
}
/**
* @hidden
*/
function isAPIResponse(data: any): data is IAPIResponse {
return data && typeof data === 'object' && data.hasOwnProperty('browserfsMessage') && data['browserfsMessage'];
}
/**
* Represents a remote file in a different worker/thread.
*/
class WorkerFile extends PreloadFile<WorkerFS> {
private _remoteFdId: number;
constructor(_fs: WorkerFS, _path: string, _flag: FileFlag, _stat: Stats, remoteFdId: number, contents?: Buffer) {
super(_fs, _path, _flag, _stat, contents);
this._remoteFdId = remoteFdId;
}
public getRemoteFdId() {
return this._remoteFdId;
}
/**
* @hidden
*/
public toRemoteArg(): IFileDescriptorArgument {
return {
type: SpecialArgType.FD,
id: this._remoteFdId,
data: bufferToTransferrableObject(this.getBuffer()),
stat: bufferToTransferrableObject(this.getStats().toBuffer()),
path: this.getPath(),
flag: this.getFlag().getFlagString()
};
}
public sync(cb: BFSOneArgCallback): void {
this._syncClose('sync', cb);
}
public close(cb: BFSOneArgCallback): void {
this._syncClose('close', cb);
}
private _syncClose(type: string, cb: BFSOneArgCallback): void {
if (this.isDirty()) {
(<WorkerFS> this._fs).syncClose(type, this, (e?: ApiError) => {
if (!e) {
this.resetDirty();
}
cb(e);
});
} else {
cb();
}
}
}
export interface WorkerFSOptions {
// The target worker that you want to connect to, or the current worker if in a worker context.
worker: Worker;
}
/**
* WorkerFS lets you access a BrowserFS instance that is running in a different
* JavaScript context (e.g. access BrowserFS in one of your WebWorkers, or
* access BrowserFS running on the main page from a WebWorker).
*
* For example, to have a WebWorker access files in the main browser thread,
* do the following:
*
* MAIN BROWSER THREAD:
*
* ```javascript
* // Listen for remote file system requests.
* BrowserFS.FileSystem.WorkerFS.attachRemoteListener(webWorkerObject);
* ```
*
* WEBWORKER THREAD:
*
* ```javascript
* // Set the remote file system as the root file system.
* BrowserFS.configure({ fs: "WorkerFS", options: { worker: self }}, function(e) {
* // Ready!
* });
* ```
*
* Note that synchronous operations are not permitted on the WorkerFS, regardless
* of the configuration option of the remote FS.
*/
export default class WorkerFS extends BaseFileSystem implements FileSystem {
public static readonly Name = "WorkerFS";
public static readonly Options: FileSystemOptions = {
worker: {
type: "object",
description: "The target worker that you want to connect to, or the current worker if in a worker context.",
validator: function(v: object, cb: BFSOneArgCallback): void {
// Check for a `postMessage` function.
if ((<any> v)['postMessage']) {
cb();
} else {
cb(new ApiError(ErrorCode.EINVAL, `option must be a Web Worker instance.`));
}
}
}
};
public static Create(opts: WorkerFSOptions, cb: BFSCallback<WorkerFS>): void {
const fs = new WorkerFS(opts.worker);
fs._initialize(() => {
cb(null, fs);
});
}
public static isAvailable(): boolean {
return typeof(importScripts) !== 'undefined' || typeof(Worker) !== 'undefined';
}
/**
* Attaches a listener to the remote worker for file system requests.
*/
public static attachRemoteListener(worker: Worker) {
const fdConverter = new FileDescriptorArgumentConverter();
function argLocal2Remote(arg: any, requestArgs: any[], cb: BFSCallback<any>): void {
switch (typeof arg) {
case 'object':
if (arg instanceof Stats) {
cb(null, statsLocal2Remote(arg));
} else if (arg instanceof ApiError) {
cb(null, apiErrorLocal2Remote(arg));
} else if (arg instanceof BaseFile) {
// Pass in p and flags from original request.
cb(null, fdConverter.toRemoteArg(<File> arg, requestArgs[0], requestArgs[1], cb));
} else if (arg instanceof FileFlag) {
cb(null, fileFlagLocal2Remote(arg));
} else if (arg instanceof Buffer) {
cb(null, bufferLocal2Remote(arg));
} else if (arg instanceof Error) {
cb(null, errorLocal2Remote(arg));
} else {
cb(null, arg);
}
break;
default:
cb(null, arg);
break;
}
}
function argRemote2Local(arg: any, fixedRequestArgs: any[]): any {
if (!arg) {
return arg;
}
switch (typeof arg) {
case 'object':
if (typeof arg['type'] === 'number') {
const specialArg = <ISpecialArgument> arg;
switch (specialArg.type) {
case SpecialArgType.CB:
const cbId = (<ICallbackArgument> arg).id;
return function() {
let i: number;
const fixedArgs = new Array(arguments.length);
let message: IAPIResponse,
countdown = arguments.length;
function abortAndSendError(err: ApiError) {
if (countdown > 0) {
countdown = -1;
message = {
browserfsMessage: true,
cbId: cbId,
args: [apiErrorLocal2Remote(err)]
};
worker.postMessage(message);
}
}
for (i = 0; i < arguments.length; i++) {
// Capture i and argument.
((i: number, arg: any) => {
argLocal2Remote(arg, fixedRequestArgs, (err, fixedArg?) => {
fixedArgs[i] = fixedArg;
if (err) {
abortAndSendError(err);
} else if (--countdown === 0) {
message = {
browserfsMessage: true,
cbId: cbId,
args: fixedArgs
};
worker.postMessage(message);
}
});
})(i, arguments[i]);
}
if (arguments.length === 0) {
message = {
browserfsMessage: true,
cbId: cbId,
args: fixedArgs
};
worker.postMessage(message);
}
};
case SpecialArgType.API_ERROR:
return apiErrorRemote2Local(<IAPIErrorArgument> specialArg);
case SpecialArgType.STATS:
return statsRemote2Local(<IStatsArgument> specialArg);
case SpecialArgType.FILEFLAG:
return fileFlagRemote2Local(<IFileFlagArgument> specialArg);
case SpecialArgType.BUFFER:
return bufferRemote2Local(<IBufferArgument> specialArg);
case SpecialArgType.ERROR:
return errorRemote2Local(<IErrorArgument> specialArg);
default:
// No idea what this is.
return arg;
}
} else {
return arg;
}
default:
return arg;
}
}
worker.addEventListener('message', (e: MessageEvent) => {
const request: object = e.data;
if (isAPIRequest(request)) {
const args = request.args,
fixedArgs = new Array<any>(args.length);
switch (request.method) {
case 'close':
case 'sync':
(() => {
// File descriptor-relative methods.
const remoteCb = <ICallbackArgument> args[1];
fdConverter.applyFdAPIRequest(request, (err?: ApiError) => {
// Send response.
const response: IAPIResponse = {
browserfsMessage: true,
cbId: remoteCb.id,
args: err ? [apiErrorLocal2Remote(err)] : []
};
worker.postMessage(response);
});
})();
break;
case 'probe':
(() => {
const rootFs = <FileSystem> fs.getRootFS(),
remoteCb = <ICallbackArgument> args[1],
probeResponse: IProbeResponse = {
type: SpecialArgType.PROBE,
isReadOnly: rootFs.isReadOnly(),
supportsLinks: rootFs.supportsLinks(),
supportsProps: rootFs.supportsProps()
},
response: IAPIResponse = {
browserfsMessage: true,
cbId: remoteCb.id,
args: [probeResponse]
};
worker.postMessage(response);
})();
break;
default:
// File system methods.
for (let i = 0; i < args.length; i++) {
fixedArgs[i] = argRemote2Local(args[i], fixedArgs);
}
const rootFS = fs.getRootFS();
(<Function> (<any> rootFS)[request.method]).apply(rootFS, fixedArgs);
break;
}
}
});
}
private _worker: Worker;
private _callbackConverter = new CallbackArgumentConverter();
private _isInitialized: boolean = false;
private _isReadOnly: boolean = false;
private _supportLinks: boolean = false;
private _supportProps: boolean = false;
/**
* Constructs a new WorkerFS instance that connects with BrowserFS running on
* the specified worker.
*/
private constructor(worker: Worker) {
super();
this._worker = worker;
this._worker.addEventListener('message', (e: MessageEvent) => {
const resp: object = e.data;
if (isAPIResponse(resp)) {
let i: number;
const args = resp.args;
const fixedArgs = new Array(args.length);
// Dispatch event to correct id.
for (i = 0; i < fixedArgs.length; i++) {
fixedArgs[i] = this._argRemote2Local(args[i]);
}
this._callbackConverter.toLocalArg(resp.cbId).apply(null, fixedArgs);
}
});
}
public getName(): string {
return WorkerFS.Name;
}
public isReadOnly(): boolean { return this._isReadOnly; }
public supportsSynch(): boolean { return false; }
public supportsLinks(): boolean { return this._supportLinks; }
public supportsProps(): boolean { return this._supportProps; }
public rename(oldPath: string, newPath: string, cb: BFSOneArgCallback): void {
this._rpc('rename', arguments);
}
public stat(p: string, isLstat: boolean, cb: BFSCallback<Stats>): void {
this._rpc('stat', arguments);
}
public open(p: string, flag: FileFlag, mode: number, cb: BFSCallback<File>): void {
this._rpc('open', arguments);
}
public unlink(p: string, cb: Function): void {
this._rpc('unlink', arguments);
}
public rmdir(p: string, cb: Function): void {
this._rpc('rmdir', arguments);
}
public mkdir(p: string, mode: number, cb: Function): void {
this._rpc('mkdir', arguments);
}
public readdir(p: string, cb: BFSCallback<string[]>): void {
this._rpc('readdir', arguments);
}
public exists(p: string, cb: (exists: boolean) => void): void {
this._rpc('exists', arguments);
}
public realpath(p: string, cache: { [path: string]: string }, cb: BFSCallback<string>): void {
this._rpc('realpath', arguments);
}
public truncate(p: string, len: number, cb: Function): void {
this._rpc('truncate', arguments);
}
public readFile(fname: string, encoding: string, flag: FileFlag, cb: BFSCallback<any>): void {
this._rpc('readFile', arguments);
}
public writeFile(fname: string, data: any, encoding: string, flag: FileFlag, mode: number, cb: BFSOneArgCallback): void {
this._rpc('writeFile', arguments);
}
public appendFile(fname: string, data: any, encoding: string, flag: FileFlag, mode: number, cb: BFSOneArgCallback): void {
this._rpc('appendFile', arguments);
}
public chmod(p: string, isLchmod: boolean, mode: number, cb: Function): void {
this._rpc('chmod', arguments);
}
public chown(p: string, isLchown: boolean, uid: number, gid: number, cb: Function): void {
this._rpc('chown', arguments);
}
public utimes(p: string, atime: Date, mtime: Date, cb: Function): void {
this._rpc('utimes', arguments);
}
public link(srcpath: string, dstpath: string, cb: Function): void {
this._rpc('link', arguments);
}
public symlink(srcpath: string, dstpath: string, type: string, cb: Function): void {
this._rpc('symlink', arguments);
}
public readlink(p: string, cb: Function): void {
this._rpc('readlink', arguments);
}
public syncClose(method: string, fd: File, cb: BFSOneArgCallback): void {
this._worker.postMessage({
browserfsMessage: true,
method: method,
args: [(<WorkerFile> fd).toRemoteArg(), this._callbackConverter.toRemoteArg(cb)]
});
}
/**
* Called once both local and remote sides are set up.
*/
private _initialize(cb: () => void): void {
if (!this._isInitialized) {
const message: IAPIRequest = {
browserfsMessage: true,
method: 'probe',
args: [this._argLocal2Remote(emptyBuffer()), this._callbackConverter.toRemoteArg((probeResponse: IProbeResponse) => {
this._isInitialized = true;
this._isReadOnly = probeResponse.isReadOnly;
this._supportLinks = probeResponse.supportsLinks;
this._supportProps = probeResponse.supportsProps;
cb();
})]
};
this._worker.postMessage(message);
} else {
cb();
}
}
private _argRemote2Local(arg: any): any {
if (!arg) {
return arg;
}
switch (typeof arg) {
case 'object':
if (typeof arg['type'] === 'number') {
const specialArg = <ISpecialArgument> arg;
switch (specialArg.type) {
case SpecialArgType.API_ERROR:
return apiErrorRemote2Local(<IAPIErrorArgument> specialArg);
case SpecialArgType.FD:
const fdArg = <IFileDescriptorArgument> specialArg;
return new WorkerFile(this, fdArg.path, FileFlag.getFileFlag(fdArg.flag), Stats.fromBuffer(transferrableObjectToBuffer(fdArg.stat)), fdArg.id, transferrableObjectToBuffer(fdArg.data));
case SpecialArgType.STATS:
return statsRemote2Local(<IStatsArgument> specialArg);
case SpecialArgType.FILEFLAG:
return fileFlagRemote2Local(<IFileFlagArgument> specialArg);
case SpecialArgType.BUFFER:
return bufferRemote2Local(<IBufferArgument> specialArg);
case SpecialArgType.ERROR:
return errorRemote2Local(<IErrorArgument> specialArg);
default:
return arg;
}
} else {
return arg;
}
default:
return arg;
}
}
private _rpc(methodName: string, args: IArguments) {
const fixedArgs = new Array(args.length);
for (let i = 0; i < args.length; i++) {
fixedArgs[i] = this._argLocal2Remote(args[i]);
}
const message: IAPIRequest = {
browserfsMessage: true,
method: methodName,
args: fixedArgs
};
this._worker.postMessage(message);
}
/**
* Converts a local argument into a remote argument. Public so WorkerFile objects can call it.
*/
private _argLocal2Remote(arg: any): any {
if (!arg) {
return arg;
}
switch (typeof arg) {
case "object":
if (arg instanceof Stats) {
return statsLocal2Remote(arg);
} else if (arg instanceof ApiError) {
return apiErrorLocal2Remote(arg);
} else if (arg instanceof WorkerFile) {
return (<WorkerFile> arg).toRemoteArg();
} else if (arg instanceof FileFlag) {
return fileFlagLocal2Remote(arg);
} else if (arg instanceof Buffer) {
return bufferLocal2Remote(arg);
} else if (arg instanceof Error) {
return errorLocal2Remote(arg);
} else {
return "Unknown argument";
}
case "function":
return this._callbackConverter.toRemoteArg(arg);
default:
return arg;
}
}
}