Allow relative URLs in loadPackage (#1421)

This commit is contained in:
Hood Chatham 2021-04-02 14:40:58 -04:00 committed by GitHub
parent ce3f80ac7c
commit 2ba598c2d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 17 deletions

View File

@ -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 `<package-name>.js` and there must be a file called
* `<package-name>.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) {

View File

@ -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