Commit 4f665e0a authored by Roman Yurchak's avatar Roman Yurchak

Validate package URI

parent f5027533
......@@ -2,6 +2,13 @@
* The main bootstrap script for loading pyodide.
*/
// Regexp for validating package name and URI
var package_name_regexp = '[a-zA-Z0-9_\-]+'
var package_uri_regexp = new RegExp(
'^(?:https?|file)://.*?(' + package_name_regexp + ').js$');
var package_name_regexp = new RegExp('^' + package_name_regexp + '$');
var languagePluginLoader = new Promise((resolve, reject) => {
// This is filled in by the Makefile to be either a local file or the
// deployed location. TODO: This should be done in a less hacky
......@@ -13,20 +20,38 @@ var languagePluginLoader = new Promise((resolve, reject) => {
var packages = undefined;
let loadedPackages = new Set();
let _uri_to_package_name = (package_uri) => {
// Generate a unique package name from URI
if (package_name_regexp.test(package_uri)) {
return package_uri;
} else if (package_uri_regexp.test(package_uri)) {
var match = package_uri_regexp.exec(package_uri);
// Get the regexp group corresponding to the package name
return match[1];
} else {
return null;
}
};
let loadPackage = (names) => {
// DFS to find all dependencies of the requested packages
let packages = window.pyodide.packages.dependencies;
let queue = new Array(names);
let toLoad = new Set();
while (queue.length) {
const package = queue.pop();
var valid_package_name_regexp = new RegExp('^[a-zA-Z0-9_\-]+$');
console.log(package + valid_package_name_regexp.test(package));
if (!valid_package_name_regexp.test(package)) {
console.log(`Invalid package name '${package}'`);
const package_uri = queue.pop();
const package = _uri_to_package_name(package_uri);
if (package == null) {
console.log(`Invalid package name or URI '${package_uri}'`);
break;
}
console.log(`Loading ${package} from ${package_uri}`);
if (!loadedPackages.has(package)) {
toLoad.add(package);
if (packages.hasOwnProperty(package)) {
......
......@@ -53,6 +53,9 @@ class SeleniumWrapper:
logs = self.driver.execute_script("return window.logs")
return '\n'.join(str(x) for x in logs)
def clean_logs(self):
self.driver.execute_script("window.logs = []")
def run(self, code):
return self.run_js(
'return pyodide.runPython({!r})'.format(code))
......@@ -145,11 +148,10 @@ if pytest is not None:
def selenium(_selenium_cached):
# selenium instance cached at the module level
try:
# clean selenium logs for each test run
_selenium_cached.driver.execute_script("window.logs = []")
_selenium_cached.clean_logs()
yield _selenium_cached
finally:
print('\n'.join(str(x) for x in _selenium_cached.logs))
print(_selenium_cached.logs)
PORT = 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment