mirror of https://github.com/pyodide/pyodide.git
Allow relative URLs in loadPackage (#1421)
This commit is contained in:
parent
ce3f80ac7c
commit
2ba598c2d2
|
@ -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,8 +270,10 @@ 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)
|
||||
* @param {function} errorCallback A callback, called with error/warning
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue