pyodide 0.19.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/load-pyodide.js DELETED
@@ -1,426 +0,0 @@
1
- import { Module } from "./module.js";
2
-
3
- const IN_NODE =
4
- typeof process !== "undefined" &&
5
- process.release &&
6
- process.release.name === "node" &&
7
- typeof process.browser ===
8
- "undefined"; /* This last condition checks if we run the browser shim of process */
9
-
10
- /** @typedef {import('./pyproxy.js').PyProxy} PyProxy */
11
- /** @private */
12
- let baseURL;
13
- /**
14
- * @param {string} indexURL
15
- * @private
16
- */
17
- export async function initializePackageIndex(indexURL) {
18
- baseURL = indexURL;
19
- let package_json;
20
- if (IN_NODE) {
21
- const fsPromises = await import(/* webpackIgnore: true */ "fs/promises");
22
- const package_string = await fsPromises.readFile(
23
- `${indexURL}packages.json`
24
- );
25
- package_json = JSON.parse(package_string);
26
- } else {
27
- let response = await fetch(`${indexURL}packages.json`);
28
- package_json = await response.json();
29
- }
30
- if (!package_json.packages) {
31
- throw new Error(
32
- "Loaded packages.json does not contain the expected key 'packages'."
33
- );
34
- }
35
- Module.packages = package_json.packages;
36
-
37
- // compute the inverted index for imports to package names
38
- Module._import_name_to_package_name = new Map();
39
- for (let name of Object.keys(Module.packages)) {
40
- for (let import_name of Module.packages[name].imports) {
41
- Module._import_name_to_package_name.set(import_name, name);
42
- }
43
- }
44
- }
45
-
46
- export async function _fetchBinaryFile(indexURL, path) {
47
- if (IN_NODE) {
48
- const fsPromises = await import(/* webpackIgnore: true */ "fs/promises");
49
- const tar_buffer = await fsPromises.readFile(`${indexURL}${path}`);
50
- return tar_buffer.buffer;
51
- } else {
52
- let response = await fetch(`${indexURL}${path}`);
53
- return await response.arrayBuffer();
54
- }
55
- }
56
-
57
- ////////////////////////////////////////////////////////////
58
- // Package loading
59
- const DEFAULT_CHANNEL = "default channel";
60
-
61
- // Regexp for validating package name and URI
62
- const package_uri_regexp = /^.*?([^\/]*)\.js$/;
63
-
64
- function _uri_to_package_name(package_uri) {
65
- let match = package_uri_regexp.exec(package_uri);
66
- if (match) {
67
- return match[1].toLowerCase();
68
- }
69
- }
70
-
71
- /**
72
- * @param {string) url
73
- * @async
74
- * @private
75
- */
76
- export let loadScript;
77
- if (globalThis.document) {
78
- // browser
79
- loadScript = async (url) => await import(/* webpackIgnore: true */ url);
80
- } else if (globalThis.importScripts) {
81
- // webworker
82
- loadScript = async (url) => {
83
- // This is async only for consistency
84
- globalThis.importScripts(url);
85
- };
86
- } else if (IN_NODE) {
87
- const pathPromise = import(/* webpackIgnore: true */ "path").then(
88
- (M) => M.default
89
- );
90
- const fetchPromise = import("node-fetch").then((M) => M.default);
91
- const vmPromise = import(/* webpackIgnore: true */ "vm").then(
92
- (M) => M.default
93
- );
94
- loadScript = async (url) => {
95
- if (url.includes("://")) {
96
- // If it's a url, have to load it with fetch and then eval it.
97
- const fetch = await fetchPromise;
98
- const vm = await vmPromise;
99
- vm.runInThisContext(await (await fetch(url)).text());
100
- } else {
101
- // Otherwise, hopefully it is a relative path we can load from the file
102
- // system.
103
- const path = await pathPromise;
104
- await import(path.resolve(url));
105
- }
106
- };
107
- } else {
108
- throw new Error("Cannot determine runtime environment");
109
- }
110
-
111
- function addPackageToLoad(name, toLoad) {
112
- name = name.toLowerCase();
113
- if (toLoad.has(name)) {
114
- return;
115
- }
116
- toLoad.set(name, DEFAULT_CHANNEL);
117
- // If the package is already loaded, we don't add dependencies, but warn
118
- // the user later. This is especially important if the loaded package is
119
- // from a custom url, in which case adding dependencies is wrong.
120
- if (loadedPackages[name] !== undefined) {
121
- return;
122
- }
123
- for (let dep_name of Module.packages[name].depends) {
124
- addPackageToLoad(dep_name, toLoad);
125
- }
126
- }
127
-
128
- function recursiveDependencies(
129
- names,
130
- _messageCallback,
131
- errorCallback,
132
- sharedLibsOnly
133
- ) {
134
- const toLoad = new Map();
135
- for (let name of names) {
136
- const pkgname = _uri_to_package_name(name);
137
- if (toLoad.has(pkgname) && toLoad.get(pkgname) !== name) {
138
- errorCallback(
139
- `Loading same package ${pkgname} from ${name} and ${toLoad.get(
140
- pkgname
141
- )}`
142
- );
143
- continue;
144
- }
145
- if (pkgname !== undefined) {
146
- toLoad.set(pkgname, name);
147
- continue;
148
- }
149
- name = name.toLowerCase();
150
- if (name in Module.packages) {
151
- addPackageToLoad(name, toLoad);
152
- continue;
153
- }
154
- errorCallback(`Skipping unknown package '${name}'`);
155
- }
156
- if (sharedLibsOnly) {
157
- let onlySharedLibs = new Map();
158
- for (let c of toLoad) {
159
- let name = c[0];
160
- if (Module.packages[name].shared_library) {
161
- onlySharedLibs.set(name, toLoad.get(name));
162
- }
163
- }
164
- return onlySharedLibs;
165
- }
166
- return toLoad;
167
- }
168
-
169
- // locateFile is the function used by the .js file to locate the .data file
170
- // given the filename
171
- Module.locateFile = function (path) {
172
- // handle packages loaded from custom URLs
173
- let pkg = path.replace(/\.data$/, "");
174
- const toLoad = Module.locateFile_packagesToLoad;
175
- if (toLoad && toLoad.has(pkg)) {
176
- let package_uri = toLoad.get(pkg);
177
- if (package_uri != DEFAULT_CHANNEL) {
178
- return package_uri.replace(/\.js$/, ".data");
179
- }
180
- }
181
- return baseURL + path;
182
- };
183
-
184
- // When the JS loads, it synchronously adds a runDependency to emscripten. It
185
- // then loads the data file, and removes the runDependency from emscripten.
186
- // This function returns a promise that resolves when there are no pending
187
- // runDependencies.
188
- function waitRunDependency() {
189
- const promise = new Promise((r) => {
190
- Module.monitorRunDependencies = (n) => {
191
- if (n === 0) {
192
- r();
193
- }
194
- };
195
- });
196
- // If there are no pending dependencies left, monitorRunDependencies will
197
- // never be called. Since we can't check the number of dependencies,
198
- // manually trigger a call.
199
- Module.addRunDependency("dummy");
200
- Module.removeRunDependency("dummy");
201
- return promise;
202
- }
203
-
204
- async function _loadPackage(names, messageCallback, errorCallback) {
205
- // toLoad is a map pkg_name => pkg_uri
206
- let toLoad = recursiveDependencies(names, messageCallback, errorCallback);
207
- // Tell Module.locateFile about the packages we're loading
208
- Module.locateFile_packagesToLoad = toLoad;
209
- if (toLoad.size === 0) {
210
- return Promise.resolve("No new packages to load");
211
- } else {
212
- let packageNames = Array.from(toLoad.keys()).join(", ");
213
- messageCallback(`Loading ${packageNames}`);
214
- }
215
-
216
- // This is a collection of promises that resolve when the package's JS file is
217
- // loaded. The promises already handle error and never fail.
218
- let scriptPromises = [];
219
-
220
- for (let [pkg, uri] of toLoad) {
221
- let loaded = loadedPackages[pkg];
222
- if (loaded !== undefined) {
223
- // If uri is from the DEFAULT_CHANNEL, we assume it was added as a
224
- // depedency, which was previously overridden.
225
- if (loaded === uri || uri === DEFAULT_CHANNEL) {
226
- messageCallback(`${pkg} already loaded from ${loaded}`);
227
- continue;
228
- } else {
229
- errorCallback(
230
- `URI mismatch, attempting to load package ${pkg} from ${uri} ` +
231
- `while it is already loaded from ${loaded}. To override a dependency, ` +
232
- `load the custom package first.`
233
- );
234
- continue;
235
- }
236
- }
237
- let pkgname = (Module.packages[pkg] && Module.packages[pkg].name) || pkg;
238
- let scriptSrc = uri === DEFAULT_CHANNEL ? `${baseURL}${pkgname}.js` : uri;
239
- messageCallback(`Loading ${pkg} from ${scriptSrc}`);
240
- scriptPromises.push(
241
- loadScript(scriptSrc).catch((e) => {
242
- errorCallback(`Couldn't load package from URL ${scriptSrc}`, e);
243
- toLoad.delete(pkg);
244
- })
245
- );
246
- }
247
-
248
- // We must start waiting for runDependencies *after* all the JS files are
249
- // loaded, since the number of runDependencies may happen to equal zero
250
- // between package files loading.
251
- try {
252
- await Promise.all(scriptPromises).then(waitRunDependency);
253
- } finally {
254
- delete Module.monitorRunDependencies;
255
- }
256
-
257
- let packageList = [];
258
- for (let [pkg, uri] of toLoad) {
259
- loadedPackages[pkg] = uri;
260
- packageList.push(pkg);
261
- }
262
-
263
- let resolveMsg;
264
- if (packageList.length > 0) {
265
- let packageNames = packageList.join(", ");
266
- resolveMsg = `Loaded ${packageNames}`;
267
- } else {
268
- resolveMsg = "No packages loaded";
269
- }
270
-
271
- Module.reportUndefinedSymbols();
272
-
273
- messageCallback(resolveMsg);
274
-
275
- // We have to invalidate Python's import caches, or it won't
276
- // see the new files.
277
- Module.importlib.invalidate_caches();
278
- }
279
-
280
- // This is a promise that is resolved iff there are no pending package loads. It
281
- // never fails.
282
- let _package_lock = Promise.resolve();
283
-
284
- /**
285
- * An async lock for package loading. Prevents race conditions in loadPackage.
286
- * @returns A zero argument function that releases the lock.
287
- * @private
288
- */
289
- async function acquirePackageLock() {
290
- let old_lock = _package_lock;
291
- let releaseLock;
292
- _package_lock = new Promise((resolve) => (releaseLock = resolve));
293
- await old_lock;
294
- return releaseLock;
295
- }
296
-
297
- /**
298
- *
299
- * The list of packages that Pyodide has loaded.
300
- * Use ``Object.keys(pyodide.loadedPackages)`` to get the list of names of
301
- * loaded packages, and ``pyodide.loadedPackages[package_name]`` to access
302
- * install location for a particular ``package_name``.
303
- *
304
- * @type {object}
305
- */
306
- export let loadedPackages = {};
307
-
308
- let sharedLibraryWasmPlugin;
309
- let origWasmPlugin;
310
- let wasmPluginIndex;
311
- function initSharedLibraryWasmPlugin() {
312
- for (let p in Module.preloadPlugins) {
313
- if (Module.preloadPlugins[p].canHandle("test.so")) {
314
- origWasmPlugin = Module.preloadPlugins[p];
315
- wasmPluginIndex = p;
316
- break;
317
- }
318
- }
319
- sharedLibraryWasmPlugin = {
320
- canHandle: origWasmPlugin.canHandle,
321
- handle(byteArray, name, onload, onerror) {
322
- origWasmPlugin.handle(byteArray, name, onload, onerror);
323
- origWasmPlugin.asyncWasmLoadPromise = (async () => {
324
- await origWasmPlugin.asyncWasmLoadPromise;
325
- Module.loadDynamicLibrary(name, {
326
- global: true,
327
- nodelete: true,
328
- });
329
- })();
330
- },
331
- };
332
- }
333
-
334
- // override the load plugin so that it calls "Module.loadDynamicLibrary" on any
335
- // .so files.
336
- // this only needs to be done for shared library packages because we assume that
337
- // if a package depends on a shared library it needs to have access to it. not
338
- // needed for .so in standard module because those are linked together
339
- // correctly, it is only where linking goes across modules that it needs to be
340
- // done. Hence, we only put this extra preload plugin in during the shared
341
- // library load
342
- function useSharedLibraryWasmPlugin() {
343
- if (!sharedLibraryWasmPlugin) {
344
- initSharedLibraryWasmPlugin();
345
- }
346
- Module.preloadPlugins[wasmPluginIndex] = sharedLibraryWasmPlugin;
347
- }
348
-
349
- function restoreOrigWasmPlugin() {
350
- Module.preloadPlugins[wasmPluginIndex] = origWasmPlugin;
351
- }
352
-
353
- /**
354
- * @callback LogFn
355
- * @param {string} msg
356
- * @returns {void}
357
- * @private
358
- */
359
-
360
- /**
361
- * Load a package or a list of packages over the network. This installs the
362
- * package in the virtual filesystem. The package needs to be imported from
363
- * Python before it can be used.
364
- *
365
- * @param {string | string[] | PyProxy} names Either a single package name or
366
- * URL or a list of them. URLs can be absolute or relative. The URLs must have
367
- * file name ``<package-name>.js`` and there must be a file called
368
- * ``<package-name>.data`` in the same directory. The argument can be a
369
- * ``PyProxy`` of a list, in which case the list will be converted to JavaScript
370
- * and the ``PyProxy`` will be destroyed.
371
- * @param {LogFn=} messageCallback A callback, called with progress messages
372
- * (optional)
373
- * @param {LogFn=} errorCallback A callback, called with error/warning messages
374
- * (optional)
375
- * @async
376
- */
377
- export async function loadPackage(names, messageCallback, errorCallback) {
378
- if (Module.isPyProxy(names)) {
379
- let temp;
380
- try {
381
- temp = names.toJs();
382
- } finally {
383
- names.destroy();
384
- }
385
- names = temp;
386
- }
387
-
388
- if (!Array.isArray(names)) {
389
- names = [names];
390
- }
391
- // get shared library packages and load those first
392
- // otherwise bad things happen with linking them in firefox.
393
- let sharedLibraryNames = [];
394
- try {
395
- let sharedLibraryPackagesToLoad = recursiveDependencies(
396
- names,
397
- messageCallback,
398
- errorCallback,
399
- true
400
- );
401
- for (let pkg of sharedLibraryPackagesToLoad) {
402
- sharedLibraryNames.push(pkg[0]);
403
- }
404
- } catch (e) {
405
- // do nothing - let the main load throw any errors
406
- }
407
-
408
- let releaseLock = await acquirePackageLock();
409
- try {
410
- useSharedLibraryWasmPlugin();
411
- await _loadPackage(
412
- sharedLibraryNames,
413
- messageCallback || console.log,
414
- errorCallback || console.error
415
- );
416
- restoreOrigWasmPlugin();
417
- await _loadPackage(
418
- names,
419
- messageCallback || console.log,
420
- errorCallback || console.error
421
- );
422
- } finally {
423
- restoreOrigWasmPlugin();
424
- releaseLock();
425
- }
426
- }