-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxJS.js
1058 lines (781 loc) · 39.6 KB
/
LinuxJS.js
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(global => {
let JSZip;
if(typeof require === "function"){
try {
JSZip = require('jszip');
} catch (e) {
JSZip = null;
}
}
async function LinuxJS(options = {}){
if(!JSZip && !global.JSZip && !options.JSZip){
throw "JSZip is required for LinuxJS to run. Either expose it globally as 'JSZip' or provide it via options."
} else if(!JSZip) JSZip = global.JSZip || options.JSZip;
if(!options.image) throw "You must specify a virtual system image/copy!";
// Create a virtual filesystem;
let os, fs = LinuxJS.initRootFileSystem();
// Add JSZipFS to available filesystems
fs.register_filesystem("JSZipFS", JSZipFS);
let root_disk = options.disk || new JSZip;
fs.mount("/", "JSZipFS", root_disk)
if(!!options.disk? !!options.image: true){
// Allows patches with multiple images/packages
if(!Array.isArray(options.image)) options.image = [options.image];
if(options.image.length < 1){
throw "You must include at least one system image/patch!"
}
// Patch the virtual disk with data the images
for(let image of options.image){
root_disk = await JSZipFS.patch(root_disk, image)
}
}
let lastPID = 1, _bootComplete = false;
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
// Init the "os" object;
let encoder = new TextEncoder;
let decoder = new TextDecoder;
os = {
get _bootComplete() {
return _bootComplete
},
// Default global environmnet variables
env: {
PATH: "/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",
HOSTNAME: (global.location && location.hostname) || (global.process && global.process.env.HOSTNAME) || "localhost",
LANG: global.navigator? navigator.language + ".UTF-8": global.process? global.process.env.LANG: "en_US.UTF-8",
// FIXME: Update (move) later when managing user accounts
HOME: "/root",
USER: "root",
LOGNAME: "root",
_: "/usr/bin/env",
...options.env? options.env: {}
},
fs,
boot(){
if(_bootComplete) throw "Can only boot once";
return new Promise((resolve, reject) => {
fs.register_filesystem("proc", class {
list(){
}
})
// Location, filesystem, device
fs.mount("/proc", "proc", "proc")
// Temporary
resolve()
})
},
reboot(){
// Currently unsupported
throw 0
},
shutdown(){
// Currently unsupported
throw 0
},
get lastPID(){
// "readonly"
return lastPID
},
proc: {},
shared: {},
async process(executable, pwd, args = [], options = {}) {
let prepared = false, _this;
let processRunnerInstance = new class Process {
constructor(){
_this = this;
_this.deafultOptions = options;
}
getWorker(env = {}){
if(!prepared) throw "Process was not prepared yet!";
let worker = new Worker(_this.workerSource)
// Initialize worker
env.type = "init";
worker.postMessage(env)
return worker
}
async _init_(){
/*
This function just prepares the procces for launch.
*/
if(prepared) throw "Cannot call ._init_() twice!";
// First, we need to find the executable
executable = "/" + os.fs.normalizePath(await os.fs.search(os.env.PATH, executable));
let executable_path = "/" + executable.split("/").filter(garbage => garbage).slice(0, -1).join("/") + "/";
let object = await os.fs.getObject(executable, pwd);
let descriptor = object.fileSystem.getDescriptor(object.relativePath);
// Initialize input output
if(!descriptor || descriptor.dir) {
return 2
}
if(!pwd) pwd = executable_path;
_this.fileDescriptor = descriptor
_this.fileLocation = executable
_this.fileDirectory = executable_path
_this.defaultPwd = "/" + os.fs.normalizePath(pwd + "/");
_this.defaultArgs = args
_this.source_data = await os.fs.read(_this.fileLocation, "arraybuffer");
if(decoder.decode(_this.source_data.slice(0, 2)) === "#!") {
// The executable is a script starting with a "shebang".
_this.source_data = decoder.decode(_this.source_data)
let match = _this.source_data.match(/^#!(.*)\n/);
if(match && match[1]){
_this.interpreter = match[1].trim();
_this.source_data = _this.source_data.slice(match[0].length);
switch(_this.interpreter){
// Some hard-coded interpreters
// FIXME: Fix this
case "/bin/js": case "/usr/bin/js": case "/usr/bin/node": case "/bin/node": case "/usr/bin/env node":
default:
// TODO: Implement interpreters properly!
break;
}
} else {
// TODO: Better error handling
throw "bad interpreter: No such file or directory"
}
}
_this.workerSource = LinuxJS.createProccessSource(_this.source_data);
prepared = true;
}
run(pwd, args, options){
if(!prepared) throw "Process was not prepared yet!";
options = options? {..._this.deafultOptions, ...options}: _this.deafultOptions;
let worker, started = false, pid = lastPID++;
let instance = {
pid,
alive: true,
terminate(){
instance.signal(15)
},
kill(){
// Normally a signal 9 would be sent, but we are just directly terminating the program anyway, so this step was skipped.
worker.terminate()
instance.invoke("exit", 137)
instance.alive = false
},
signal(value){
worker.postMessage({
type: 'signal',
value
});
},
write(data){
worker.postMessage({
type: 'stdin',
data
});
},
resume(){
// Not to be confused with pausing/resuming the process state, this only resumes the execution if the pause option is set to true for delayed start.
if(!started) execute()
},
childProcesses: []
}
LinuxJS.signalHandler(instance, options);
function execute(){
started = true;
instance.promise = new Promise((resolve, reject) => {
worker = _this.getWorker({
args: args || _this.defaultArgs || [],
pwd: pwd || _this.defaultPwd || _this.fileDirectory,
env: {...os.env, ...options.env || {}},
})
worker.onmessage = async function(event) {
// Signal received from host
let signal = event.data;
if(!signal || !signal.type) return;
switch(signal.type){
case "stdout":
instance.invoke("stdout", signal.data)
break
case "stderr":
instance.invoke("stderr", signal.data)
break
case "exit": case "terminate": case "kill":
worker.terminate()
instance.invoke("exit", signal.code || 0)
instance.alive = false
break
case "stream_pipe_in":
if(os.proc[signal.pid]) os.proc[signal.pid].write(signal.data)
break
case "stream_pipe_signal":
if(os.proc[signal.pid]) {
if(signal.value === 9) return os.proc[signal.pid].kill();
os.proc[signal.pid].signal(signal.value)
}
break
case "syscall":
switch(signal.to){
case "exec":
// Spawn a child process
let childRunner;
try{
childRunner = await os.process(signal.args[0], signal.args[1], signal.args[2]);
} catch (error) {
worker.postMessage({
type: "callback",
callback: signal.callback,
data: {error}
})
return
}
// "pipe" events to the parent
let child = childRunner.run(null, null, {
onstdout(data){
worker.postMessage({
type: "stream_pipe_out",
pid: child.pid,
data
})
},
onstderr(data){
worker.postMessage({
type: "stream_pipe_err",
pid: child.pid,
data
})
},
onexit(code){
worker.postMessage({
type: "stream_pipe_exit",
pid: child.pid,
code
})
},
env: signal.env || {},
pause: true
})
worker.postMessage({
type: "callback",
callback: signal.callback,
data: child.pid
})
child.resume()
instance.childProcesses.push(child)
break
case "fs":
// Filesystem operation ("high-level way", not following the actual GNU/Linux way)
if(os.fs[signal.args[0]] && typeof os.fs[signal.args[0]] === "function"){
let result, error;
try{
result = await os.fs[signal.args[0]](...signal.args[1])
} catch (errorMessage) { error = errorMessage.toString() }
worker.postMessage({
type: "callback",
callback: signal.callback,
data: {result, error}
})
}
break
case "export":
// Filesystem operation ("high-level way", not following the actual GNU/Linux way)
for(let env of signal.args){
env = env.split("=");
os.env[env[0]] = env[1]
}
worker.postMessage({
type: "callback",
callback: signal.callback
})
break
}
break
}
}
worker.onerror = function(event) {
instance.invoke("stderr", event.toString())
}
})
}
if(!options.pause) execute()
return os.proc[pid] = instance;
}
}
let errorCode = await processRunnerInstance._init_()
if(errorCode) throw errorCode;
return processRunnerInstance
}
}
return os;
}
// VFS to handle a JSZip as a filesystem
class JSZipFS {
constructor(device){
this.resolveSymlinks = true;
this.directWrites = true;
this.storage = device;
}
getDescriptor(path){
if(!this.storage.files[path]){
// Try flipping the trailing "/"
path = path.endsWith("/")? path.slice(0, -1) : path + "/";
}
return this.storage.files[path];
}
async read(descriptor){
return await descriptor.async("arraybuffer")
}
write(path, buffer, permissions){
// TODO: many things
return this.storage.file(path, buffer).files[path]
}
createFile(path){
return this.storage.file(path, "").files[path]
}
remove(path){
return this.storage.remove(path)
}
mkdir(path){
this.storage.file(path, "")
}
list(path, listDir = true, recursive = false, hidden = true){
path = LinuxJS.normalizePath(path);
let pathLength = path.split("/").length;
return Object.keys(this.storage.files).filter(found_path => {
found_path = LinuxJS.normalizePath(found_path);
// Do not include itself
if(found_path === path) return false;
// Do not include hidden files
if(!hidden && (found_path.startsWith(".") || found_path.split("/").at(-1).startsWith("."))) return false;
// Skip paths that are not a part of the parth being searched
if(!found_path.startsWith(path)) return false;
// Get depth and statistics
let stat = this.getDescriptor(found_path),
depth = (found_path.split("/").length - 1) - pathLength
;
return (listDir? true : !stat.dir) && (recursive? true : depth == 0 )
})
}
stat(){
// Currently, the getDescriptor acts as stat, which is obviously incorrect, this will be fixed later.
}
// Non-standard method
async export(type = "uint8array"){
return await this.storage.generateAsync({
type,
platform: "UNIX"
})
}
static async patch(zip, data){
if(data instanceof ArrayBuffer || data instanceof Uint8Array){
return await zip.loadAsync(data, { createFolders: true });
} else throw "Image must be of type ArrayBuffer or Uint8Array"
}
}
let utf8_decoder = new TextDecoder("utf-8");
let utf8_encoder = new TextEncoder("utf-8");
let ascii_decoder = new TextDecoder("ascii");
let ascii_encoder = new TextEncoder("ascii");
let utf16le_decoder = new TextDecoder("utf-16le");
let utf16le_encoder = new TextEncoder("utf-16le");
let latin1_decoder = new TextDecoder("latin1");
let latin1_encoder = new TextEncoder("latin1");
LinuxJS.initRootFileSystem = function () {
let fs = {
mount_points: {},
fileSystems: {},
register_filesystem(name, constructor){
fs.fileSystems[name] = constructor
},
mount(path, filesystem, device){
path = fs.normalizePath(path);
// TODO: Implement other arguments
if(!fs.fileSystems[filesystem]) throw "unknown filesystem type '" + filesystem + "'.";
// if(!fs.exists(path)) throw path + ": mount point does not exist.";
fs.mount_points[path] = new fs.fileSystems[filesystem](device)
},
normalizePath(path, pwd){
return LinuxJS.normalizePath(path, pwd)
},
async open(path, pwd){
let object = await fs.getObject(path, pwd);
return object.fileSystem.getDescriptor(object.relativePath);
},
async read(path, encoding = "uint8array", pwd){
let object = await fs.getObject(path, pwd);
let descriptor = object.fileSystem.getDescriptor(object.relativePath);
if(typeof descriptor === "undefined") throw "ENOENT";
if(descriptor.dir) throw "EISDIR";
encoding = encoding.toLowerCase();
let data = await object.fileSystem.read(descriptor);
function toBinaryString(){
const uint8Array = new Uint8Array(data);
let result = '';
for(byte of uint8Array){
result += String.fromCharCode(byte)
}
return result
}
switch(encoding){
case "arraybuffer":
return data;
case "uint8array":
return new Uint8Array(data);
case "uint16array":
return new Uint16Array(data);
case "uint32array":
return new Uint32Array(data);
case "string": case "utf8": case "utf-8": case "text":
return utf8_decoder.decode(data);
case "blob":
return new Blob([data]);
case "url":
return new URL.createObjectURL(Blob([data]));
case "json":
return JSON.parse(utf8_decoder.decode(data));
case "ucs2": case "ucs-2": case "utf16le": case "utf-16le":
return utf16le_decoder.decode(data);
case "latin1": case "binary":
return latin1_decoder.decode(data);
case "ascii":
return ascii_decoder.decode(data);
case "binarystring":
return toBinaryString(data);
case "base64":
return btoa(toBinaryString(data));
default:
throw "Unsupported encoding"
}
},
async write(path, data, pwd){
let object = await fs.getObject(path, pwd), descriptor;
if(object.fileSystem.directWrites){
descriptor = object.relativePath
} else {
descriptor = object.fileSystem.getDescriptor(object.relativePath);
if(typeof descriptor === "undefined") {
descriptor = object.fileSystem.createFile(path)
}
if(descriptor.dir) throw "EISDIR";
}
return await object.fileSystem.write(descriptor, data);
},
async rm(path, pwd){
let object = await fs.getObject(path, pwd)
object.fileSystem.remove(path)
},
async ls(path = "/", options = {}, pwd){
let object = await fs.getObject(path, pwd), descriptor;
descriptor = object.fileSystem.getDescriptor(object.relativePath);
if(!descriptor) throw "ENOENT";
if(!descriptor.dir) throw "ENOTDIR";
let list = object.fileSystem.list(object.relativePath, !!options.directories, !!options.recursive);
return list.map(thing => {
if(options.fullPath) return "/" + thing;
return thing.replace(object.relativePath, "").replace("/", "")
})
},
async exists(path, pwd){
let object = await fs.getObject(path, pwd)
return !!object.fileSystem.getDescriptor(object.relativePath)
},
getNearestMountPointOf(path){
let mountPoint = path, relativePath = ""
if(fs.mount_points.hasOwnProperty(path)){
return [path, "", fs.mount_points[path]];
} else while (mountPoint.length >= 1) {
let index = mountPoint.lastIndexOf("/");
relativePath = mountPoint.slice(index > 0? index: 0) + relativePath;
if(index > 0) mountPoint = mountPoint.slice(0, index); else mountPoint = "";
if(fs.mount_points[mountPoint]){
return [mountPoint, fs.normalizePath(relativePath), fs.mount_points[mountPoint]];
}
}
throw "No filesystem"
},
async getObject(path, pwd, options = {}){
// Resolves a path to its mountpoint, resolves symbolic links, and finds the real path
if(!fs.mount_points.hasOwnProperty("")) throw "Could not find a root filesystem. Please make sure you have something mounted at '/'";
path = fs.normalizePath(path, pwd);
if(fs.mount_points.hasOwnProperty(path)){
return {
fileSystem: fs.mount_points[path],
relativePath: "",
path,
mountPoint: path
}
}
let [mountPoint, relativePath, fileSystem] = fs.getNearestMountPointOf(path);
if(fileSystem.resolveSymlinks){
let parts = path.split('/');
let resolvedPath = [];
for (let part of parts) {
resolvedPath.push(part);
let partialPath = resolvedPath.join('/');
let descriptor = fileSystem.getDescriptor(partialPath);
// The path is not a part of the filesystem anymore or was not found, so cancel the symlink lookup
if(!descriptor) break;
if(descriptor && !descriptor.dir && descriptor.unixPermissions && descriptor.unixPermissions.toString(8).startsWith("120")){
// Symlink found, follow it
// TODO: Proper implementation i guess. This implementation only works with JSZipFS, but that should not be a big problem, as other "filesystems" or handles will probably resolve symlinks on their own.
let symlinkContent = fs.normalizePath(await (await fileSystem.getDescriptor(partialPath)).async("text"));
if(symlinkContent === partialPath) throw "ELOOP";
// Get any other symbolic links recursively across multiple filesystems, uh, maybeee?? idk
// let targetPath = (await fs.getObject(symlinkContent)).path;
// resolvedPath = targetPath.split('/');
if(options.__iteration > (options.maxSymlinkIteration || 100)) {
// The link has reached too large recursion level
throw "ELOOP"
// throw "EMLINK"
}
return await fs.getObject(fs.normalizePath(symlinkContent + "/" + parts.slice(resolvedPath.length).join("/")), null, {
__iteration: (options.__iteration || 0) +1
})
}
}
}
return {fileSystem, relativePath, path, mountPoint}
},
async isDirectory(path, pwd){
let object = await fs.getObject(path, pwd)
let descriptor = object.fileSystem.getDescriptor(object.relativePath);
return !!(descriptor && descriptor.dir)
},
join(...path){
return fs.normalizePath(path.join("/"))
},
async search(list, file, pwd){
if(file.includes("/")) return "/" + fs.normalizePath(file, pwd);
for(let searchPath of (Array.isArray(list)? list: list.split(":"))){
let files;
try {
files = await fs.ls(searchPath);
} catch { continue }
if(files && files.includes(file)){
return "/" + fs.normalizePath(searchPath + "/" + file)
}
}
return file;
},
resolve(target, pwd){
throw "This method is deprecated"
},
pathFind(file){
throw "This method is deprecated"
}
}
return fs
}
LinuxJS.normalizePath = function(path, pwd){
// Relative paths
if((path[0] === "." || path.slice(0, 2) === "..") && pwd) path = `${pwd}/${path}`;
path = path.replace(/\\/g, '/');
// Resolve '..' and '.' in the path
const parts = path.split('/');
const normalizedParts = [];
for (const part of parts) {
if (part === '..') {
normalizedParts.pop();
} else if (part !== '.' && part !== '') {
normalizedParts.push(part);
}
}
const normalizedPath = normalizedParts.join('/');
return normalizedPath == "/"? "" : normalizedPath;
}
// Mostly just a helper for browsers
LinuxJS.remoteImage = async function (url, callback){
try {
let request = await fetch(url);
if(!request.ok) return callback(false);
let data = await request.arrayBuffer()
if(callback) callback(data)
return data
} catch (e) {
if(callback) callback(false)
throw e
}
}
LinuxJS.signalHandler = function (target, parent) {
let listeners = {};
target.on = function (type, callback){
if(!listeners[type]) listeners[type] = [];
listeners[type].push(callback)
}
target.invoke = function (type, ...data){
if(typeof parent[`on${type}`] === "function") parent[`on${type}`](...data)
if(listeners[type]){
for(let listener of listeners[type]) listener(...data)
}
}
}
processWorkerCode = (async function (run){
let initialized = false, stdinlisteners = [], syscallCallbackID = 0, syscallCallbackListeners = {}, childProcessEvents = {};
function registerChildStreamListeners(pid, listeners = {}){
childProcessEvents[pid] = {
out: listeners.out,
err: listeners.err,
exit: listeners.exit
}
}
function syscall(call, options = {}){
let callback = syscallCallbackID++
postMessage({
type: "syscall",
to: call,
callback,
args: options.args || [],
env
})
syscallCallbackListeners[callback] = options.callback || (() => {})
}
// Proxy FS
fs = new Proxy({}, {
get(target, key){
return function proxyFunction(...args){
return new Promise((resolve, reject) => {
syscall("fs", {
args: [key, args],
callback(result){
if(result.error) return reject(result.error);
resolve(result.result)
}
})
})
}
}
})
self.onmessage = function (event) {
// Signal received from host
let signal = event.data;
if(!signal || !signal.type) return;
switch(signal.type){
case "init":
if(initialized){
throw new Error("Forking (re-initialization) a process is currently not supported")
}
env = signal.env;
let globals = {
args: signal.args,
pwd: signal.pwd,
exit(code = 0){
postMessage({
type: "exit",
code
})
},
std: {
write(data){
postMessage({
type: "stdout",
data
})
},
writeError(data){
postMessage({
type: "stderr",
data
})
},
onData(listener){
stdinlisteners.push(listener)
}
},
exec(command, pwd, arguments, listeners){
return new Promise(resolve => {
globals.call("exec", {
args: [command, pwd, arguments],
callback: pid => {
if(pid.error){
resolve({error: pid.error})
}
if(listeners){
registerChildStreamListeners(pid, listeners)
}
resolve({
pid,
write(data){
postMessage({
type: "stream_pipe_in",
pid,
data
})
},
terminate(){
postMessage({
type: "stream_pipe_signal",
pid,
value: 15
})
},
kill(){
console.log(pid);
postMessage({
type: "stream_pipe_signal",
pid,
value: 9
})
},
signal(value){
postMessage({
type: "stream_pipe_signal",
pid,
value
})
}
})
}
})
})
},
registerChildStreamListeners,
call: syscall
};
run(globals)
break
case "signal":
// TODO: Handle signals
switch(signal.value){
case 15: // Terminate
// TODO: Handle graceful termination
postMessage({
type: "exit",
code: 143
})
break