forked from Maaarcocr/pyes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
58 lines (51 loc) · 1.44 KB
/
worker.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
import {parentPort} from "worker_threads"
import crypto from "crypto";
import StdinBuffer from './stdinBuffer.js'
import Python from './python.js';
const stdout = (charCode) => {
if (charCode) {
parentPort.postMessage({
type: 'stdout',
stdout: String.fromCharCode(charCode),
})
} else {
console.log(typeof charCode, charCode)
}
}
const stderr = (charCode) => {
if (charCode) {
parentPort.postMessage({
type: 'stderr',
stderr: String.fromCharCode(charCode),
})
} else {
console.log(typeof charCode, charCode)
}
}
let stdinBuffer = new StdinBuffer();
function getRandomValues(array) {
if (!ArrayBuffer.isView(array)) {
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
}
const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength);
crypto.randomFillSync(buffer);
return array;
}
var Module = {
stdout: stdout,
stderr: stderr,
stdin: stdinBuffer.stdin,
arguments: ["-i", "-q", "-"],
onRuntimeInitialized: () => {
parentPort.postMessage({type: 'ready', stdinBuffer: stdinBuffer.sab})
},
}
if (typeof fetch === 'undefined') {
globalThis.self = {}
globalThis.self.location = {}
globalThis.self.location.href = import.meta.url
globalThis.importScripts = () => {}
globalThis.crypto = {}
globalThis.crypto.getRandomValues = getRandomValues
}
Python(Module);