-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathpyodideWebWorker.ts
More file actions
218 lines (204 loc) · 7.15 KB
/
Copy pathpyodideWebWorker.ts
File metadata and controls
218 lines (204 loc) · 7.15 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
import {DEFAULT_FOLDER_ID} from '@codebridge/constants';
import {loadPyodide, PyodideInterface, version} from 'pyodide';
import {MAIN_PYTHON_FILE} from '@cdo/apps/lab2/constants';
import {HOME_FOLDER} from './pythonHelpers/constants';
import {
patchInputCode,
pythonlabInputModule,
SETUP_CODE,
} from './pythonHelpers/patches';
import {
getCleanupCode,
getUpdatedSourceAndDeleteFiles,
importPackagesFromFiles,
resetGlobals,
writeSource,
} from './pythonHelpers/pythonScriptUtils';
import {MessageType} from './types';
let pyodide: PyodideInterface;
async function loadPyodideAndPackages() {
pyodide = await loadPyodide({
// /assets does not serve unhashed files, so we load from /blockly instead,
// which does serve the unhashed files. We need to serve the unhashed files because
// pyodide controls adding the filenames to the url we provide here.
indexURL: `/blockly/js/pyodide/${version}/`,
env: {
HOME: `/${HOME_FOLDER}/`,
},
// Remove all JS globals so Python can’t call browser APIs (like fetch) unless we
// explicitly add them.
jsglobals: {},
});
pyodide.setStdout(getStreamHandlerOptions('sysout'));
pyodide.setStderr(getStreamHandlerOptions('syserr'));
// Freeze the module object and its properties
Object.freeze(pythonlabInputModule);
Object.defineProperty(pythonlabInputModule.getInput, 'constructor', {
writable: false,
configurable: false,
enumerable: false,
});
Object.freeze(pythonlabInputModule.getInput);
pyodide.registerJsModule('pythonlab_input', pythonlabInputModule);
// Pre-load our custom packages (unittest_runner and pythonlab_setup), as well as
// matplotlib, which pythonlab_setup depends on, and numpy,
// which will frequently be used. We have seen occasional issues with loading
// packages, so we retry loading if we see any errors.
let loadErrors = await loadPackages();
if (loadErrors.length > 0) {
// Retry loading packages once. Any packages that were successfully loaded
// will be skipped in the retry.
loadErrors = await loadPackages();
if (loadErrors.length > 0) {
postMessage({
type: 'load_failed',
message: `Error(s) loading python packages: ${loadErrors.join('\n')}`,
id: 'startup',
});
}
}
// Warm up the pyodide environment by running setup code.
await runInternalCode(SETUP_CODE, -1);
}
let pyodideReadyPromise: Promise<void> | null = null;
// Pyodide defines the globals object as any.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let pyodideGlobals: any | null = null;
let loadStartTime: number | undefined;
async function initializePyodide() {
const promiseWasNull = pyodideReadyPromise === null;
if (promiseWasNull) {
loadStartTime = Date.now();
pyodideReadyPromise = loadPyodideAndPackages();
postMessage({type: 'loading_pyodide'});
}
await pyodideReadyPromise;
if (promiseWasNull) {
const loadTime = loadStartTime ? Date.now() - loadStartTime : undefined;
postMessage({
type: 'loaded_pyodide',
message: loadTime,
});
}
pyodideGlobals = pyodide.globals.toJs();
}
// Get pyodide ready as soon as possible.
initializePyodide();
onmessage = async event => {
// make sure loading is done
await initializePyodide();
const {id, python, source, validationFile} = event.data;
let results = undefined;
let sourceToWrite = source;
// Add the validation file to the source if it exists. Use the id "validation"
// so the validation file does not overwrite a user file (user files have stringified numeric ids).
if (validationFile) {
sourceToWrite = {
...source,
files: {
...source.files,
validation: {...validationFile, id: 'validation'},
},
};
}
try {
writeSource(sourceToWrite, DEFAULT_FOLDER_ID, '', pyodide);
postMessage({type: 'loading_packages'});
await importPackagesFromFiles(sourceToWrite, pyodide);
postMessage({type: 'loaded_packages'});
await patchInput(id);
results = await pyodide.runPythonAsync(python, {
filename: `/${HOME_FOLDER}/${MAIN_PYTHON_FILE}`,
});
} catch (error) {
postMessage({type: 'error', message: (error as Error).message, id});
}
// Clean up environment.
await runInternalCode(getCleanupCode(sourceToWrite), id);
// We run setup code at the end to prepare the environment for the next run.
await runInternalCode(SETUP_CODE, id);
// We don't want to send back the validation file as part of the sources,
// so we skip adding it to updatedSource.
const filenamesToSkipSaving = validationFile ? [validationFile.name] : [];
const updatedSource = getUpdatedSourceAndDeleteFiles(
source,
id,
pyodide,
postMessage,
filenamesToSkipSaving
);
postMessage({type: 'updated_source', message: updatedSource, id});
resetGlobals(pyodide, pyodideGlobals);
// If there is a results response, convert it to a JS object.
// Documentation on this method:
// https://pyodide.org/en/stable/usage/api/js-api.html#pyodide.ffi.PyProxy.toJs
const resultsObject = results?.toJs();
try {
postMessage({
type: 'run_complete',
message: JSON.stringify(resultsObject),
id,
});
} catch (e) {
// Likely we hit a DataCloneError trying to send the resultsObject.
// In this case, don't try to send the results object, as if it can't be
// sent, it wasn't going to be parsed by us anyway.
postMessage({type: 'run_complete', id});
}
};
// Run code owned by us (not the user). If there is an error, post a
// system_error message.
async function runInternalCode(code: string, id: number) {
try {
await pyodide.runPythonAsync(code);
} catch (error) {
postMessage({type: 'system_error', message: (error as Error).message, id});
}
}
// Return the options for sysout or syserr stream handler.
function getStreamHandlerOptions(type: MessageType) {
return {
batched: (msg: string) => {
postMessage({type: type, message: msg, id: 'none'});
},
};
}
async function patchInput(id: number) {
await runInternalCode(patchInputCode(id), id);
}
async function loadPackages() {
const loadErrors: string[] = [];
// We explicitly load all dependencies of the packages we want to be available to users,
// matplotlib and numpy, as well as our custom packages. We do this so that on retry
// if the top-level package was loaded successfully, but a dependency
// failed, we will still try to reload the dependency.
await pyodide.loadPackage(
[
// Main packages
'matplotlib',
'numpy',
// Dependencies of main packages
'contourpy',
'cycler',
'fonttools',
'kiwisolver',
'packaging',
'pillow',
'pyparsing',
'python-dateutil',
'pytz',
'six',
// Custom packages that we have built. They are defined in the
// python/pythonlab/ folder in the codebase.
`/blockly/js/pyodide/${version}/unittest_runner-0.3.0-py3-none-any.whl`,
`/blockly/js/pyodide/${version}/pythonlab_setup-0.2.0-py3-none-any.whl`,
`/blockly/js/pyodide/${version}/neighborhood-0.4.0-py3-none-any.whl`,
],
{
errorCallback: (message: string) => {
loadErrors.push(message);
},
}
);
return loadErrors;
}