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";
|
const DEFAULT_CHANNEL = "default channel";
|
||||||
|
|
||||||
// Regexp for validating package name and URI
|
// Regexp for validating package name and URI
|
||||||
const package_uri_regexp =
|
const package_uri_regexp = /^.*?([^/]*)\.js$/
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
let _uri_to_package_name =
|
||||||
|
(package_uri) => package_uri_regexp.exec(package_uri) ?.[1];
|
||||||
let loadScript;
|
let loadScript;
|
||||||
if (self.document) { // browser
|
if (self.document) { // browser
|
||||||
loadScript = (url) => new Promise((res, rej) => {
|
loadScript = (url) => new Promise((res, rej) => {
|
||||||
|
@ -99,7 +90,7 @@ globalThis.loadPyodide = async function(config = {}) {
|
||||||
};
|
};
|
||||||
for (let name of names) {
|
for (let name of names) {
|
||||||
const pkgname = _uri_to_package_name(name);
|
const pkgname = _uri_to_package_name(name);
|
||||||
if (pkgname !== null) {
|
if (pkgname !== undefined) {
|
||||||
if (toLoad.has(pkgname) && toLoad.get(pkgname) !== name) {
|
if (toLoad.has(pkgname) && toLoad.get(pkgname) !== name) {
|
||||||
errorCallback(`Loading same package ${pkgname} from ${name} and ${
|
errorCallback(`Loading same package ${pkgname} from ${name} and ${
|
||||||
toLoad.get(pkgname)}`);
|
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
|
* 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
|
* for the package available in the virtual filesystem. The package needs to
|
||||||
* be imported from Python before it can be used.
|
* be imported from Python before it can be used.
|
||||||
* @param {String | Array} names package name, or URL. Can be either a single
|
* @param {String | Array} names Package name or URL. Can be either a single
|
||||||
* element, or an array
|
* 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
|
* @param {function} messageCallback A callback, called with progress messages
|
||||||
* (optional)
|
* (optional)
|
||||||
* @param {function} errorCallback A callback, called with error/warning
|
* @param {function} errorCallback A callback, called with error/warning
|
||||||
* messages (optional)
|
* messages (optional)
|
||||||
* @returns {Promise} Resolves to ``undefined`` when loading is complete
|
* @returns {Promise} Resolves to ``undefined`` when loading is complete
|
||||||
*/
|
*/
|
||||||
Module.loadPackage = async function(names, messageCallback, errorCallback) {
|
Module.loadPackage = async function(names, messageCallback, errorCallback) {
|
||||||
|
|
|
@ -45,6 +45,11 @@ def test_load_from_url(selenium_standalone, web_server_secondary, active_server)
|
||||||
selenium_standalone.run("import pytz")
|
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):
|
def test_list_loaded_urls(selenium_standalone):
|
||||||
selenium = selenium_standalone
|
selenium = selenium_standalone
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue