Load .data files from the same base URL as .js

This commit is contained in:
Roman Yurchak 2018-09-19 18:12:05 +02:00
parent f7aec29209
commit 73e3898b72
3 changed files with 35 additions and 4 deletions

View File

@ -35,8 +35,10 @@ var languagePluginLoader = new Promise((resolve, reject) => {
let _loadPackage = (names) => { let _loadPackage = (names) => {
// DFS to find all dependencies of the requested packages // DFS to find all dependencies of the requested packages
let packages = window.pyodide._module.packages.dependencies; let packages = window.pyodide._module.packages.dependencies;
let loadedPackages = window.pyodide.loadedPackages;
let queue = [].concat(names || []); let queue = [].concat(names || []);
let toLoad = new Array(); let toLoad = new Array();
window.pyodide._toLoadPackages = toLoad;
while (queue.length) { while (queue.length) {
let package_uri = queue.pop(); let package_uri = queue.pop();
@ -87,7 +89,7 @@ var languagePluginLoader = new Promise((resolve, reject) => {
window.pyodide._module.monitorRunDependencies = (n) => { window.pyodide._module.monitorRunDependencies = (n) => {
if (n === 0) { if (n === 0) {
for (let package in toLoad) { for (let package in toLoad) {
loadedPackages[package] = toLoad[package]; window.pyodide.loadedPackages[package] = toLoad[package];
} }
delete window.pyodide._module.monitorRunDependencies; delete window.pyodide._module.monitorRunDependencies;
const packageList = Array.from(Object.keys(toLoad)).join(', '); const packageList = Array.from(Object.keys(toLoad)).join(', ');
@ -195,7 +197,20 @@ var languagePluginLoader = new Promise((resolve, reject) => {
return {}; return {};
}; };
Module.locateFile = (path) => baseURL + path; Module.locateFile = (path) => {
if ((window.hasOwnProperty('pyodide')) &&
(window.pyodide.hasOwnProperty('_toLoadPackages'))) {
// handle packages loaded from custom URLs
let package = path.replace(/\.data$/, "");
if (package in window.pyodide._toLoadPackages) {
let package_uri = window.pyodide._toLoadPackages[package];
if (package_uri != 'default channel') {
return package_uri.replace(/\.js$/, ".data");
};
};
};
return baseURL + path;
};
var postRunPromise = new Promise((resolve, reject) => { var postRunPromise = new Promise((resolve, reject) => {
Module.postRun = () => { Module.postRun = () => {
delete window.Module; delete window.Module;
@ -232,6 +247,7 @@ var languagePluginLoader = new Promise((resolve, reject) => {
// filesystem to install itself. Once that's complete, it will be replaced // filesystem to install itself. Once that's complete, it will be replaced
// by the call to `makePublicAPI` with a more limited public API. // by the call to `makePublicAPI` with a more limited public API.
window.pyodide = pyodide(Module); window.pyodide = pyodide(Module);
window.pyodide.loadedPackages = new Array();
window.pyodide.loadPackage = loadPackage; window.pyodide.loadPackage = loadPackage;
}; };
document.head.appendChild(script); document.head.appendChild(script);

View File

@ -272,6 +272,11 @@ def run_web_server(q, log_filepath):
*self.client_address, *self.client_address,
format_ % args)) format_ % args))
def end_headers(self):
# Enable Cross-Origin Resource Sharing (CORS)
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
Handler.extensions_map['.wasm'] = 'application/wasm' Handler.extensions_map['.wasm'] = 'application/wasm'
with socketserver.TCPServer(("", 0), Handler) as httpd: with socketserver.TCPServer(("", 0), Handler) as httpd:

View File

@ -11,8 +11,8 @@ def test_load_from_url(selenium_standalone, web_server_secondary):
log_main.open('r') as fh_main: log_main.open('r') as fh_main:
# skip existing log lines # skip existing log lines
fh_main.read() fh_main.seek(0, 2)
fh_secondary.read() fh_secondary.seek(0, 2)
selenium_standalone.load_package(f"http://{url}:{port}/pyparsing.js") selenium_standalone.load_package(f"http://{url}:{port}/pyparsing.js")
assert "Invalid package name or URI" not in selenium_standalone.logs assert "Invalid package name or URI" not in selenium_standalone.logs
@ -32,6 +32,16 @@ def test_load_from_url(selenium_standalone, web_server_secondary):
selenium_standalone.run("import numpy as np") selenium_standalone.run("import numpy as np")
def test_list_loaded_urls(selenium_standalone):
selenium = selenium_standalone
selenium.load_package('pyparsing')
assert selenium.run_js(
'return Object.keys(pyodide.loadedPackages)') == ['pyparsing']
assert selenium.run_js(
"return pyodide.loadedPackages['pyparsing']") == "default channel"
def test_uri_mismatch(selenium_standalone): def test_uri_mismatch(selenium_standalone):
selenium_standalone.load_package('pyparsing') selenium_standalone.load_package('pyparsing')
selenium_standalone.load_package('http://some_url/pyparsing.js') selenium_standalone.load_package('http://some_url/pyparsing.js')