10000 [RC] Work on the reverted changes by WebReflection · Pull Request #1753 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

[RC] Work on the reverted changes #1753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyscript.core/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pytest_cache/
node_modules/
rollup/
test/
Expand Down
78 changes: 39 additions & 39 deletions pyscript.core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyscript.core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pyscript/core",
"version": "0.2.0",
"version": "0.2.2",
"type": "module",
"description": "PyScript",
"module": "./index.js",
Expand Down Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.1",
"@rollup/plugin-terser": "^0.4.3",
"rollup": "^3.29.2",
"rollup": "^3.29.3",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-string": "^3.0.0",
"static-handler": "^0.4.2",
Expand Down
131 changes: 70 additions & 61 deletions pyscript.core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { $ } from "basic-devtools";

import TYPES from "./types.js";
import allPlugins from "./plugins.js";
import { robustFetch as fetch, getText } from "./fetch.js";
import { ErrorCode } from "./exceptions.js";
Expand All @@ -19,9 +20,10 @@ const badURL = (url, expected = "") => {
* Given a string, returns its trimmed content as text,
* fetching it from a file if the content is a URL.
* @param {string} config either JSON, TOML, or a file to fetch
* @param {string?} type the optional type to enforce
* @returns {{json: boolean, toml: boolean, text: string}}
*/
const configDetails = async (config) => {
const configDetails = async (config, type) => {
let text = config?.trim();
// we only support an object as root config
let url = "",
Expand All @@ -45,74 +47,81 @@ const syntaxError = (type, url, { message }) => {
return new SyntaxError(`${str}\n${message}`);
};

// find the shared config for all py-script elements
let config, type;
const configs = new Map();

/** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
let plugins;
/** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
let parsed;
/** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
let error;
for (const [TYPE] of TYPES) {
/** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
let plugins;

let pyConfig = $("py-config");
if (pyConfig) {
config = pyConfig.getAttribute("src") || pyConfig.textContent;
type = pyConfig.getAttribute("type");
} else {
pyConfig = $(
[
'script[type="py"][config]:not([worker])',
"py-script[config]:not([worker])",
].join(","),
);
if (pyConfig) config = pyConfig.getAttribute("config");
}
/** @type {any} The PyScript configuration parsed from the JSON or TOML object*. May be any of the return types of JSON.parse() or toml-j0.4's parse() ( {number | string | boolean | null | object | Array} ) */
let parsed;

// catch possible fetch errors
if (config) {
try {
const { json, toml, text, url } = await configDetails(config);
config = text;
if (json || type === "json") {
try {
parsed = JSON.parse(text);
} catch (e) {
error = syntaxError("JSON", url, e);
}
} else if (toml || type === "toml") {
try {
const { parse } = await import(
/* webpackIgnore: true */
"https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
);
parsed = parse(text);
} catch (e) {
error = syntaxError("TOML", url, e);
/** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
let error;

let config,
type,
pyConfig = $(`${TYPE}-config`);
if (pyConfig) {
config = pyConfig.getAttribute("src") || pyConfig.textContent;
type = pyConfig.getAttribute("type");
} else {
pyConfig = $(
[
`script[type="${TYPE}"][config]:not([worker])`,
`${TYPE}-script[config]:not([worker])`,
].join(","),
);
if (pyConfig) config = pyConfig.getAttribute("config");
}

// catch possible fetch errors
if (config) {
try {
const { json, toml, text, url } = await configDetails(config, type);
config = text;
if (json || type === "json") {
try {
parsed = JSON.parse(text);
} catch (e) {
error = syntaxError("JSON", url, e);
}
} else if (toml || type === "toml") {
try {
const { parse } = await import(
/* webpackIgnore: true */
"https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
);
parsed = parse(text);
} catch (e) {
error = syntaxError("TOML", url, e);
}
}
} catch (e) {
error = e;
}
} catch (e) {
error = e;
}
}

// parse all plugins and optionally ignore only
// those flagged as "undesired" via `!` prefix
const toBeAwaited = [];
for (const [key, value] of Object.entries(allPlugins)) {
if (error) {
if (key === "error") {
// show on page the config is broken, meaning that
// it was not possible to disable error plugin neither
// as that part wasn't correctly parsed anyway
value().then(({ notify }) => notify(error.message));
// parse all plugins and optionally ignore only
// those flagged as "undesired" via `!` prefix
const toBeAwaited = [];
for (const [key, value] of Object.entries(allPlugins)) {
if (error) {
if (key === "error") {
// show on page the config is broken, meaning that
// it was not possible to disable error plugin neither
// as that part wasn't correctly parsed anyway
value().then(({ notify }) => notify(error.message));
}
} else if (!parsed?.plugins?.includes(`!${key}`)) {
toBeAwaited.push(value());
}
} else if (!parsed?.plugins?.includes(`!${key}`)) {
toBeAwaited.push(value());
}
}

// assign plugins as Promise.all only if needed
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
// assign plugins as Promise.all only if needed
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);

configs.set(TYPE, { config: parsed, plugins, error });
}

export { parsed as config, plugins, error };
export default configs;
Loading
0