diff --git a/src/pyodide.js b/src/pyodide.js index 9077af658..2a898b45d 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -45,19 +45,10 @@ globalThis.loadPyodide = async function(config = {}) { const DEFAULT_CHANNEL = "default channel"; // Regexp for validating package name and URI - const package_uri_regexp = - new RegExp('^https?://.*?([a-z0-9_][a-z0-9_\-]*).js$', 'i'); - - let _uri_to_package_name = (package_uri) => { - if (package_uri_regexp.test(package_uri)) { - let match = package_uri_regexp.exec(package_uri); - // Get the regexp group corresponding to the package name - return match[1]; - } else { - return null; - } - }; + const package_uri_regexp = /^.*?([^/]*)\.js$/ + let _uri_to_package_name = + (package_uri) => package_uri_regexp.exec(package_uri) ?.[1]; let loadScript; if (self.document) { // browser loadScript = (url) => new Promise((res, rej) => { @@ -99,7 +90,7 @@ globalThis.loadPyodide = async function(config = {}) { }; for (let name of names) { const pkgname = _uri_to_package_name(name); - if (pkgname !== null) { + if (pkgname !== undefined) { if (toLoad.has(pkgname) && toLoad.get(pkgname) !== name) { errorCallback(`Loading same package ${pkgname} from ${name} and ${ toLoad.get(pkgname)}`); @@ -279,12 +270,14 @@ globalThis.loadPyodide = async function(config = {}) { * Load a package or a list of packages over the network. This makes the files * for the package available in the virtual filesystem. The package needs to * be imported from Python before it can be used. - * @param {String | Array} names package name, or URL. Can be either a single - * element, or an array + * @param {String | Array} names Package name or URL. Can be either a single + * element, or an array. URLs can be absolute or relative. URLs must have + * file name `.js` and there must be a file called + * `.data` in the same directory. * @param {function} messageCallback A callback, called with progress messages - * (optional) + * (optional) * @param {function} errorCallback A callback, called with error/warning - * messages (optional) + * messages (optional) * @returns {Promise} Resolves to ``undefined`` when loading is complete */ Module.loadPackage = async function(names, messageCallback, errorCallback) { diff --git a/src/tests/test_package_loading.py b/src/tests/test_package_loading.py index 1b8bc6b89..731cc889e 100644 --- a/src/tests/test_package_loading.py +++ b/src/tests/test_package_loading.py @@ -45,6 +45,11 @@ def test_load_from_url(selenium_standalone, web_server_secondary, active_server) selenium_standalone.run("import pytz") +def test_load_relative_url(selenium_standalone): + selenium_standalone.load_package("./pytz.js") + selenium_standalone.run("import pytz") + + def test_list_loaded_urls(selenium_standalone): selenium = selenium_standalone