-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWasmWebTerm.js
1011 lines (837 loc) · 32.1 KB
/
WasmWebTerm.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
import { proxy, wrap } from "comlink"
import { FitAddon } from "xterm-addon-fit"
import XtermEchoAddon from "local-echo"
import parse from "shell-quote/parse"
import { inflate } from "pako" // fallback for DecompressionStream API (free as its used in WapmFetchUtil)
import LineBuffer from "./LineBuffer"
import WasmWorkerRAW from "./runners/WasmWorker" // will be prebuilt using webpack
import {
default as PromptsFallback,
MODULE_ID as WasmRunnerID, // get the id of this module in the final webpack bundle
} from "./runners/WasmRunner"
import WapmFetchUtil from "./WapmFetchUtil"
class WasmWebTerm {
isRunningCommand
onActivated
onDisposed
onFileSystemUpdate
onBeforeCommandRun
onCommandRunFinish
_xterm
_xtermEcho
_xtermPrompt
_worker
_wasmRunner // prompts fallback
_jsCommands
_wasmModules
_wasmFsFiles
_stdoutBuffer
_stderrBuffer
_outputBuffer
_lastOutputTime
constructor(wasmBinaryPath) {
this.wasmBinaryPath = wasmBinaryPath
this._jsCommands = new Map() // js commands and their callback functions
this.isRunningCommand = false // allow running only 1 command in parallel
this._worker = false // fallback (do not use worker until it is initialized)
this._wasmModules = [] // [{ name: "abc", type: "emscripten|wasmer", module: WebAssembly.Module, [runtime: Blob] }]
this._wasmFsFiles = [] // files created during wasm execution (will be written to wasm runtime's FS)
this._outputBuffer = "" // buffers outputs to determine if it ended with line break
this._lastOutputTime = 0 // can be used for guessing if output is complete on stdin calls
this.onActivated = () => {} // can be overwritten to know when activation is complete
this.onDisposed = () => {} // can be overwritten to know when disposition is complete
this.onFileSystemUpdate = () => {} // can be overwritten to handle emscr file changes
this.onBeforeCommandRun = () => {} // can be overwritten to show load animation (etc)
this.onCommandRunFinish = () => {} // can be overwritten to hide load animation (etc)
// check if browser support for web working is available
if (
![typeof Worker, typeof SharedArrayBuffer, typeof Atomics].includes(
"undefined"
)
)
// if yes, initialize worker
this._initWorker()
// if no support -> use prompts as fallback
else this._wasmRunner = new PromptsFallback()
this._suppressOutputs = false
window.term = this // todo: debug
}
/* xterm.js addon life cycle */
async activate(xterm) {
this._xterm = xterm
// create xterm addon to fit size
this._xtermFitAddon = new FitAddon()
this._xtermFitAddon.activate(this._xterm)
// fit xterm size to container
setTimeout(() => this._xtermFitAddon.fit(), 1)
// handle container resize
window.addEventListener("resize", () => {
this._xtermFitAddon.fit()
})
// handle module drag and drop
setTimeout(() => this._initWasmModuleDragAndDrop(), 1)
// set xterm prompt
this._xtermPrompt = async () => "$ "
// async to be able to fetch sth here
// create xterm local echo addon
this._xtermEcho = new XtermEchoAddon()
this._xtermEcho.activate(this._xterm)
// register available js commands
this.registerJsCommand("help", async function* (argv) {
yield "todo: show helping things\n"
})
this.registerJsCommand("about", async (argv) => {
return (
"Wasm-WebTerm version " +
__VERSION__ +
".\nBackend: " +
(this._worker ? "WebWorker" : "Prompts Fallback") +
".\n"
)
})
this.registerJsCommand(
"clear",
async (argv) => await this.printWelcomeMessagePlusControlSequences()
)
// if using webworker -> wait until initialized
if (this._worker instanceof Promise) await this._worker
// register xterm data handler for Ctrl+C
this._xterm.onData((data) => this._onXtermData(data))
// notify that we're ready
await this.onActivated()
// write welcome message to terminal
this._xterm.writeln(
await this.printWelcomeMessagePlusControlSequences(),
// callback for when welcome message was printed
() => {
// start REPL
this.repl()
// focus terminal cursor
setTimeout(() => this._xterm.focus(), 1)
}
)
}
async dispose() {
await this._xtermEcho.dispose()
await this._xtermFitAddon.dispose()
if (this._worker) this._terminateWorker()
await this.onDisposed()
}
/* js command handling */
registerJsCommand(name, callback, autocomplete) {
this._jsCommands.set(name, { name, callback, autocomplete })
return this // to be able to stack these calls
}
unregisterJsCommand(name) {
return this._jsCommands.delete(name)
}
get jsCommands() {
return this._jsCommands
}
/* read eval print loop */
async repl() {
try {
// read
const prompt = await this._xtermPrompt()
const line = await this._xtermEcho.read(prompt)
// empty input -> prompt again
if (line.trim() == "") return this.repl()
// give user possibility to exec sth before run
await this.onBeforeCommandRun()
// print newline before
this._xterm.write("\r\n")
// eval and print
await this.runLine(line)
// print extra newline if output does not end with one
if (this._outputBuffer.slice(-1) != "\n") this._xterm.write("\u23CE\r\n")
// print newline after
this._xterm.write("\r\n")
// give user possibility to run sth after exec
await this.onCommandRunFinish()
// loop
this.repl()
} catch (e) {
/* console.error("Error during REPL:", e) */
}
}
/* parse line as commands and handle them */
_parseCommands(line) {
let usesEnvironmentVars = false
let usesBashFeatures = false
// parse line into tokens (respect escaped spaces and quotation marks)
const commandLine = parse(line, (_key) => {
usesEnvironmentVars = true
return undefined
})
const commands = []
let cmd = []
splitter: {
for (let idx = 0; idx < commandLine.length; ++idx) {
const item = commandLine[idx]
if (typeof item === "string") {
if (cmd.length === 0 && item.match(/^\w+=.*$/)) {
usesEnvironmentVars = true
continue
} else {
cmd.push(item)
}
} else {
switch (item.op) {
case "|":
commands.push(cmd)
cmd = []
break
default:
usesBashFeatures = true
console.error("Unsupported shell operator:", item.op)
break splitter
}
}
}
}
commands.push(cmd)
if (usesEnvironmentVars) {
this._stderr(
"\x1b[1m[\x1b[33mWARN\x1b[39m]\x1b[0m Environment variables are not supported!\n"
)
}
if (usesBashFeatures) {
this._stderr(
"\x1b[1m[\x1b[33mWARN\x1b[39m]\x1b[0m Advanced bash features are not supported! Only the pipe '|' works for now.\n"
)
}
return commands
}
async runLine(line) {
try {
let stdinPreset = null
this._suppressOutputs = false
const commandsInLine = this._parseCommands(line)
for (const [index, argv] of commandsInLine.entries()) {
// split into command name and argv
const commandName = argv.shift()
const command = this._jsCommands.get(commandName)
// try user registered js commands first
if (typeof command?.callback == "function") {
// todo: move this to a method like "runJsCommand"?
// call registered user function
const result = command.callback(argv, stdinPreset)
let output // where user function outputs are stored
/**
* user functions are another word for custom js
* commands and can pass outputs in various ways:
*
* 1) return value normally via "return"
* 2) pass value through promise resolve() / async
* 3) yield values via generator functions
*/
// await promises if any (2)
if (result.then) output = ((await result) || "").toString()
// await yielding generator functions (3)
else if (result.next)
for await (let data of result)
output = output == null ? data : output + data
// default: when functions return "normally" (1)
else output = result.toString()
// if is last command in pipe -> print output to xterm
if (index == commandsInLine.length - 1) this._stdout(output)
else stdinPreset = output || null // else -> use output as stdinPreset
// todo: make it possible for user functions to use stdERR.
// exceptions? they end function execution..
}
// otherwise try wasm commands
else if (command == undefined) {
// if is not last command in pipe
if (index < commandsInLine.length - 1) {
const output = await this.runWasmCommandHeadless(
commandName,
argv,
stdinPreset
)
stdinPreset = output.stdout // apply last stdout to next stdin
}
// if is last command -> run normally and reset stdinPreset
else {
await this.runWasmCommand(commandName, argv, stdinPreset)
stdinPreset = null
}
}
// command is defined but has no function -> can not handle
else
console.error("command is defined but has no function:", commandName)
}
} catch (e) {
// catch errors (print to terminal and developer console)
if (this._outputBuffer.slice(-1) != "\n") this._stderr("\n")
this._stderr(`\x1b[1m[\x1b[31mERROR\x1b[39m]\x1b[0m ${e.toString()}\n`)
console.error("Error running line:", e)
}
}
/* running single wasm commands */
runWasmCommand(programName, argv, stdinPreset, onFinishCallback) {
console.log("called runWasmCommand:", programName, argv)
if (this.isRunningCommand) throw "WasmWebTerm is already running a command"
else this.isRunningCommand = true
// enable outputs if they were suppressed
this._suppressOutputs = false
this._outputBuffer = ""
// define callback for when command has finished
const onFinish = proxy(async (files) => {
console.log("command finished:", programName, argv)
// enable commands to run again
this.isRunningCommand = false
// store created files
this._wasmFsFiles = files
await this.onFileSystemUpdate(this._wasmFsFiles)
// wait until outputs are rendered
await this._waitForOutputPause()
// flush out any pending outputs
this._stdoutBuffer.flush()
this._stderrBuffer.flush()
// wait until the rest is rendered
this._waitForOutputPause().then(() => {
// notify caller that command run is over
if (typeof onFinishCallback == "function") onFinishCallback()
// resolve await from shell
this._runWasmCommandPromise?.resolve()
})
})
// define callback for when errors occur
const onError = proxy((value) => this._stderr(value + "\n"))
// get or initialize wasm module
this._stdout("loading web assembly ...")
this._getOrFetchWasmModule(programName)
.then((wasmModule) => {
// clear last line
this._xterm.write("\x1b[2K\r")
// check if we can run on worker
if (this._worker)
// delegate command execution to worker thread
this._worker.runCommand(
programName,
wasmModule.module,
wasmModule.type,
argv,
this._stdinProxy,
this._stdoutProxy,
this._stderrProxy,
this._wasmFsFiles,
onFinish,
onError,
null,
stdinPreset,
wasmModule.runtime
)
// if not -> fallback with prompts
// start execution on the MAIN thread (freezes terminal)
else
this._wasmRunner.runCommand(
programName,
wasmModule.module,
wasmModule.type,
argv,
null,
this._stdoutProxy,
this._stderrProxy,
this._wasmFsFiles,
onFinish,
onError,
null,
stdinPreset,
wasmModule.runtime
)
})
// catch errors (command not running anymore + reject (returns to shell))
.catch((e) => {
this.isRunningCommand = false
this._runWasmCommandPromise?.reject(e)
})
// return promise (makes shell await)
return new Promise(
(resolve, reject) => (this._runWasmCommandPromise = { resolve, reject })
)
}
runWasmCommandHeadless(programName, argv, stdinPreset, onFinishCallback) {
if (this.isRunningCommand) throw "WasmWebTerm is already running a command"
else this.isRunningCommand = true
// promise for resolving / rejecting command execution
let runWasmCommandHeadlessPromise = { resolve: () => {}, reject: () => {} }
// define callback for when command has finished
const onFinish = proxy((outBuffers) => {
// enable commands to run again
this.isRunningCommand = false
// flush outputs
this._stdoutBuffer.flush()
this._stderrBuffer.flush()
// call on finish callback
if (typeof onFinishCallback == "function") onFinishCallback(outBuffers)
// resolve promise
runWasmCommandHeadlessPromise.resolve(outBuffers)
})
// define callback for when errors occur
const onError = proxy((value) => this._stderr(value + "\n"))
// define callback for onSuccess (contains files)
const onSuccess = proxy(() => {}) // not used currently
// get or initialize wasm module
this._getOrFetchWasmModule(programName)
.then((wasmModule) => {
if (this._worker)
// check if we can run on worker
// delegate command execution to worker thread
this._worker.runCommandHeadless(
programName,
wasmModule.module,
wasmModule.type,
argv,
this._wasmFsFiles,
onFinish,
onError,
onSuccess,
stdinPreset,
wasmModule.runtime
)
// if not -> use fallback
// start execution on the MAIN thread (freezes terminal)
else
this._wasmRunner.runCommandHeadless(
programName,
wasmModule.module,
wasmModule.type,
argv,
this._wasmFsFiles,
onFinish,
onError,
onSuccess,
stdinPreset,
wasmModule.runtime
)
})
// catch errors (command not running anymore + reject promise)
.catch((e) => {
this.isRunningCommand = false
runWasmCommandHeadlessPromise.reject(e)
})
// return promise (makes shell await)
return new Promise(
(resolve, reject) => (runWasmCommandHeadlessPromise = { resolve, reject })
)
}
/* wasm module handling */
_getOrFetchWasmModule(programName) {
return new Promise(async (resolve, reject) => {
let wasmModule,
wasmBinary,
localBinaryFound = false
// check if there is an initialized module already
this._wasmModules.forEach((moduleObj) => {
if (moduleObj.name == programName) wasmModule = moduleObj
})
// if a module was found -> resolve
if (wasmModule?.module instanceof WebAssembly.Module) resolve(wasmModule)
else
try {
// if none is found -> initialize a new one
// create wasm module object (to resolve and to store)
wasmModule = {
name: programName,
type: "emscripten",
module: undefined,
}
// try to find local wasm binary
// (only if wasmBinaryPath is provided, otherwise use wapm directly)
if (this.wasmBinaryPath != undefined) {
// try to fetch local wasm binaries first
const localBinaryResponse = await fetch(
this.wasmBinaryPath + "/" + programName + ".wasm"
)
wasmBinary = await localBinaryResponse.arrayBuffer()
// validate if localBinaryResponse contains a wasm binary
if (localBinaryResponse?.ok && WebAssembly.validate(wasmBinary)) {
// try to fetch emscripten js runtime
const jsRuntimeResponse = await fetch(
this.wasmBinaryPath + "/" + programName + ".js"
)
if (jsRuntimeResponse?.ok) {
// read js runtime from response
const jsRuntimeCode = await jsRuntimeResponse.arrayBuffer()
// check if the first char of the response is not "<"
// (because dumb parcel does not return http errors but an html page)
const firstChar = String.fromCharCode(
new Uint8Array(jsRuntimeCode).subarray(0, 1).toString()
)
if (firstChar != "<")
// set this module's runtime
wasmModule.runtime = jsRuntimeCode
}
// if no valid js runtime was found -> it's considered a wasmer binary
if (!wasmModule.runtime) wasmModule.type = "wasmer"
// local binary was found -> do not fetch wapm
localBinaryFound = true
}
// if none was found or it was invalid -> try for a .lnk file
else {
// explanation: .lnk files can contain a different module/runtime name.
// this enables `echo` and `ls` to both use `coreutils.wasm`, for example.
// try to fetch .lnk file
const linkResponse = await fetch(
this.wasmBinaryPath + "/" + programName + ".lnk"
)
if (linkResponse?.ok) {
// read new program name from .lnk file
const linkedProgramName = await linkResponse.text()
const linkDestination =
this.wasmBinaryPath + "/" + linkedProgramName + ".wasm"
// try to fetch the new binary
const linkedBinaryResponse = await fetch(linkDestination)
if (linkedBinaryResponse?.ok) {
// read binary from response
wasmBinary = await linkedBinaryResponse.arrayBuffer()
// validate if linkedBinaryResponse contains a wasm binary
if (WebAssembly.validate(wasmBinary)) {
// try to fetch emscripten js runtime
const jsRuntimeResponse = await fetch(
this.wasmBinaryPath + "/" + linkedProgramName + ".js"
)
if (jsRuntimeResponse?.ok) {
// todo: note that this code is redundant, maybe use a function?
// read js runtime from response
const jsRuntimeCode =
await jsRuntimeResponse.arrayBuffer()
// check if the first char of the response is not "<"
// (because dumb parcel does not return http errors but an html page)
const firstChar = String.fromCharCode(
new Uint8Array(jsRuntimeCode).subarray(0, 1).toString()
)
if (firstChar != "<")
// set this module's runtime
wasmModule.runtime = jsRuntimeCode
}
// if no valid js runtime was found -> it's considered a wasmer binary
if (!wasmModule.runtime) wasmModule.type = "wasmer"
// local binary was found -> do not fetch wapm
localBinaryFound = true
}
}
}
}
}
// if no local binary was found -> fetch from wapm.io
if (!localBinaryFound) {
wasmBinary =
await WapmFetchUtil.getWasmBinaryFromCommand(programName)
wasmModule.type = "wasmer"
}
// compile fetched bytes into wasm module
wasmModule.module = await WebAssembly.compile(wasmBinary)
// store compiled module
this._wasmModules.push(wasmModule)
// continue execution
resolve(wasmModule)
} catch (e) {
reject(e)
}
})
}
_initWasmModuleDragAndDrop() {
// event handler for when user starts to drag file
this._xterm.element.addEventListener("dragenter", (e) => {
this._xterm.element.style.opacity = "0.8"
})
// needed for drop event to be fired on div
this._xterm.element.addEventListener("dragover", (e) => {
e.preventDefault()
this._xterm.element.style.opacity = "0.8"
})
// event handler for when user stops to drag file
this._xterm.element.addEventListener("dragleave", (e) => {
this._xterm.element.style.opacity = ""
})
// event handler for when the user drops the file
this._xterm.element.addEventListener(
"drop",
async (e) => {
e.preventDefault()
let files = []
if (e.dataTransfer.items)
// read files from .items
for (let i = 0; i < e.dataTransfer.items.length; i++)
if (e.dataTransfer.items[i].kind == "file")
files.push(e.dataTransfer.items[i].getAsFile())
// read files from .files (other browsers)
else
for (let i = 0; i < e.dataTransfer.files.length; i++)
files.push(e.dataTransfer.files[i])
// parse dropped files into modules
for (let i = 0; i < files.length; i++) {
const file = files[i]
if (file.name.endsWith(".wasm")) {
// todo: also support dropping .lnk files?
const programName = file.name.replace(/\.wasm$/, "")
// remove existing modules with that name
this._wasmModules = this._wasmModules.filter(
(mod) => mod.name != programName
)
// if has .js file -> it's an emscripten binary
if (files.some((f) => f.name == programName + ".js")) {
// load emscripten js runtime and compile emscripten wasm binary
const emscrJsRuntime = files.find(
(f) => f.name == programName + ".js"
)
const emscrWasmModule = await WebAssembly.compile(
await file.arrayBuffer()
)
// add compiled emscripten module to this._wasmModules
this._wasmModules.push({
name: programName,
type: "emscripten",
runtime: await emscrJsRuntime.arrayBuffer(),
module: emscrWasmModule,
})
alert("Emscripten Wasm Module added: " + programName)
} else {
// if not -> its considered a wasmer binary
// compile wasmer module and store in this._wasmModules
const wasmerModule = await WebAssembly.compile(
await file.arrayBuffer()
)
this._wasmModules.push({
name: programName,
type: "wasmer",
module: wasmerModule,
})
alert("WASI Module added: " + programName)
}
}
}
this._xterm.element.style.opacity = ""
},
false
)
}
/* worker execution flow */
_initWorker() {
this._worker = new Promise(async (resolve) => {
// init buffers for pausing worker and passing stdin values
this._pauseBuffer = new Int32Array(
new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 1)
) // 1 bit to shift
this._stdinBuffer = new Int32Array(
new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 1000)
) // 1000 chars buffer
// create blob including webworker and its dependencies
const workerSource = fetch(WasmWorkerRAW).then((response) => {
if ("DecompressionStream" in self) {
const stream = response.body.pipeThrough(
new DecompressionStream("gzip")
)
const decompressed = new Response(stream)
return decompressed.text()
} else {
return response.bytes().then((data) => inflate(data))
}
})
// HACK: Forward all modules for the `WasmRunner`(`PromptsFallback`) to the worker
// instead of bundling them directly. This saves about 90 KiB by not serializing
// these dependencies twice. This works fine, since we already use the identical
// module already as a fallback.
const extraModules = {}
try {
const webpackModuleIds = Object.keys(__webpack_modules__)
let dependencies = [WasmRunnerID] // start with the `WasmRunner` module
for (
let dep = dependencies.shift();
dep != undefined;
dep = dependencies.shift()
) {
// put the module into the list of modules to forward
const mod = __webpack_modules__[dep]
extraModules[dep] = mod
// parse module for imports of other module dependencies (like `r(MODULE_ID)`)
const matches = mod.toString().matchAll(/\br\((\d+)\)/g)
for (const match of matches) {
if (webpackModuleIds.includes(match[1])) dependencies.push(match[1])
}
}
} catch (err) {
// Could not collect the `WasmRunner` module and its dependencies for forwarding
console.error(
`Cannot collect the \`WasmRunner\` module and its dependencies for use in the Worker: ${err}`
)
console.error(
"Falling back to excuting `WasmRunner` in the PromptsFallback"
)
// -> use prompts as fallback
this._wasmRunner = new PromptsFallback()
this._worker = false
resolve(this._worker)
return
}
// prepend source of collected modules as well as the ID for the
// `WasmRunner` module to the original worker source
let prelude = "self._modules={"
for (const [id, module] of Object.entries(extraModules)) {
prelude += `${id}:${module.toString()},`
}
prelude += `};self.WasmRunnerID=${WasmRunnerID}\n`
const blob = new Blob([prelude, await workerSource], {
type: "application/javascript",
})
// init webworker from blob (no separate file, no cross-origin problems)
this._workerRAW = new Worker(URL.createObjectURL(blob))
const WasmWorker = wrap(this._workerRAW)
this._worker = await new WasmWorker(this._pauseBuffer, this._stdinBuffer)
resolve(this._worker) // webworker is now initialized
})
}
_resumeWorker() {
console.log("resuming worker (request)")
Atomics.store(this._pauseBuffer, 0, 0) // mem[0] = 0 (means do not hold)
Atomics.notify(this._pauseBuffer, 0) // awake waiting
}
_terminateWorker() {
console.log("called terminate worker")
this._workerRAW.terminate()
}
_waitForOutputPause(pauseDuration = 80, interval = 20) {
// note: timeout because web worker outputs are not always rendered to
// the term directly. therefore we wait until we guess it's all there.
return new Promise((resolve) => {
const timeout = () => {
setTimeout(() => {
// if there has been output in the last pauseDuration -> run again
if (this._lastOutputTime > Date.now() - pauseDuration) timeout()
// if not -> resolve
else resolve()
}, interval)
}
timeout()
})
}
/* input output handling -> web worker */
_setStdinBuffer(string) {
for (let i = 0; i < this._stdinBuffer.length; i++)
this._stdinBuffer[i] = string[i] ? string[i].charCodeAt(0) : 0 // 0 = null (empty bits = end of string)
}
_stdinProxy = proxy((message) => {
this._waitForOutputPause().then(async () => {
console.log("called _stdinProxy", message)
// flush outputs (to show the prompt)
this._stdoutBuffer.flush()
this._stderrBuffer.flush()
// read input until RETURN (LF), CTRL+D (EOF), or CTRL+C
const input = await new Promise((resolve, reject) => {
let buffer = ""
const handler = this._xterm.onData((data) => {
// CTRL + C -> return without data
if (data == "\x03") {
this._xterm.write("^C")
handler.dispose()
return resolve("")
}
// CTRL + D -> return input buffer
else if (data == "\x04") {
handler.dispose()
return resolve(buffer)
}
// map return to '\n'
if (data == "\r") data = "\n"
// map backspace to CTRL+H
else if (data == "\x7f") data = "\x08"
// add character or delete last one
if (data == "\x08") buffer = buffer.slice(0, -1)
else buffer += data
// line complete -> return the input buffer
if (data == "\n") {
// only echo the linebreak when there is no prompt (i.e. we assume multi-line input)
if (!message) this._xterm.write("\r\n")
handler.dispose()
return resolve(buffer)
}
// echo input back (special handling for backspace and escape sequences)
if (data == "\x08") this._xterm.write("^H")
else if (data.charCodeAt(0) == 0x1b)
this._xterm.write("^[" + data.slice(1))
else this._xterm.write(data)
})
})
// pass value to webworker
this._setStdinBuffer(input)
this._resumeWorker()
})
})
_stdoutProxy = proxy((value) => this._stdoutBuffer.write(value))
_stderrProxy = proxy((value) => this._stderrBuffer.write(value))
/* input output handling -> term */
_stdoutBuffer = new LineBuffer(this._stdout.bind(this))
_stderrBuffer = new LineBuffer(this._stderr.bind(this))
_stdout(value) {
// string or char code
if (this._suppressOutputs) return // used for Ctrl+C
// numbers are interpreted as char codes -> convert to string
if (typeof value == "number") value = String.fromCharCode(value)
// avoid offsets with line breaks
value = value.replace(/\n/g, "\r\n")
// buffer time for synchronity
this._outputBuffer += value
this._lastOutputTime = Date.now()
// write to terminal
this._xterm.write(value)
}
_stderr(value) {
// check if it's a javascript error
if (value instanceof Error) {
// log error to the console
console.error("stderr error:", value)
// convert error object to string
value = value.toString() + "\n"
}
// print to terminal
this._stdout(value)
}
async printWelcomeMessage() {
let message = `\x1b[1;32m
_ _ _ _ _ _ _ _____ \r
| | | |___ ___ _____ | | | |___| |_ |_ _|___ ___ _____ \r
| | | | .'|_ -| | | | | | -_| . | | | | -_| _| |\r
|_____|__,|___|_|_|_| |_____|___|___| |_| |___|_| |_|_|_|\r
\x1b[37m\r\n`
message +=
"Run WebAssembly binaries compiled with Emscripten or Wasmer.\r\n"
message +=
"You can also define and run custom JavaScript functions.\r\n\r\n"
message += "Version: " + __VERSION__ + ". "
message +=
"Backend: " + (this._worker ? "WebWorker" : "Prompts Fallback") + ".\r\n"
message +=
"Commands: " +
[...this._jsCommands]
.map((commandObj) => commandObj[0])
.sort()
.join(", ") +
". "
return message
}
// helper function to cleanly print the welcome message
async printWelcomeMessagePlusControlSequences() {
// clear terminal, reset color, print welcome message, reset color, add empty line
return (
"\x1bc" +
"\x1b[0;37m" +
(await this.printWelcomeMessage()) +
"\x1b[0;37m\r\n"
)
}
_onXtermData(data) {
if (data == "\x03") {
// custom handler for Ctrl+C (webworker only)
if (this._worker) {