@pyscript/core 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.js +2 -2
- package/dist/core.js.map +1 -1
- package/package.json +1 -1
- package/src/config.js +61 -54
- package/src/core.js +11 -7
- package/src/types.js +4 -0
- package/types/config.d.ts +2 -4
- package/types/core.d.ts +2 -1
- package/types/types.d.ts +2 -0
package/package.json
CHANGED
package/src/config.js
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
*/
|
6
6
|
import { $ } from "basic-devtools";
|
7
7
|
|
8
|
+
import TYPES from "./types.js";
|
8
9
|
import allPlugins from "./plugins.js";
|
9
10
|
import { robustFetch as fetch, getText } from "./fetch.js";
|
10
11
|
import { ErrorCode } from "./exceptions.js";
|
@@ -45,66 +46,72 @@ const syntaxError = (type, url, { message }) => {
|
|
45
46
|
return new SyntaxError(`${str}\n${message}`);
|
46
47
|
};
|
47
48
|
|
48
|
-
|
49
|
-
let config, plugins, parsed, error, type;
|
50
|
-
let pyConfig = $("py-config");
|
51
|
-
if (pyConfig) {
|
52
|
-
config = pyConfig.getAttribute("src") || pyConfig.textContent;
|
53
|
-
type = pyConfig.getAttribute("type");
|
54
|
-
} else {
|
55
|
-
pyConfig = $(
|
56
|
-
[
|
57
|
-
'script[type="py"][config]:not([worker])',
|
58
|
-
"py-script[config]:not([worker])",
|
59
|
-
].join(","),
|
60
|
-
);
|
61
|
-
if (pyConfig) config = pyConfig.getAttribute("config");
|
62
|
-
}
|
49
|
+
const configs = new Map();
|
63
50
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
51
|
+
for (const [TYPE] of TYPES) {
|
52
|
+
// find the shared config for all py-script elements
|
53
|
+
let config, plugins, parsed, error, type;
|
54
|
+
let pyConfig = $(`${TYPE}-config`);
|
55
|
+
if (pyConfig) {
|
56
|
+
config = pyConfig.getAttribute("src") || pyConfig.textContent;
|
57
|
+
type = pyConfig.getAttribute("type");
|
58
|
+
} else {
|
59
|
+
pyConfig = $(
|
60
|
+
[
|
61
|
+
`script[type="${TYPE}"][config]:not([worker])`,
|
62
|
+
`${TYPE}-script[config]:not([worker])`,
|
63
|
+
].join(","),
|
64
|
+
);
|
65
|
+
if (pyConfig) config = pyConfig.getAttribute("config");
|
66
|
+
}
|
67
|
+
|
68
|
+
// catch possible fetch errors
|
69
|
+
if (config) {
|
70
|
+
try {
|
71
|
+
const { json, toml, text, url } = await configDetails(config);
|
72
|
+
config = text;
|
73
|
+
if (json || type === "json") {
|
74
|
+
try {
|
75
|
+
parsed = JSON.parse(text);
|
76
|
+
} catch (e) {
|
77
|
+
error = syntaxError("JSON", url, e);
|
78
|
+
}
|
79
|
+
} else if (toml || type === "toml") {
|
80
|
+
try {
|
81
|
+
const { parse } = await import(
|
82
|
+
/* webpackIgnore: true */
|
83
|
+
"https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
|
84
|
+
);
|
85
|
+
parsed = parse(text);
|
86
|
+
} catch (e) {
|
87
|
+
error = syntaxError("TOML", url, e);
|
88
|
+
}
|
84
89
|
}
|
90
|
+
} catch (e) {
|
91
|
+
error = e;
|
85
92
|
}
|
86
|
-
} catch (e) {
|
87
|
-
error = e;
|
88
93
|
}
|
89
|
-
}
|
90
94
|
|
91
|
-
// parse all plugins and optionally ignore only
|
92
|
-
// those flagged as "undesired" via `!` prefix
|
93
|
-
const toBeAwaited = [];
|
94
|
-
for (const [key, value] of Object.entries(allPlugins)) {
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
95
|
+
// parse all plugins and optionally ignore only
|
96
|
+
// those flagged as "undesired" via `!` prefix
|
97
|
+
const toBeAwaited = [];
|
98
|
+
for (const [key, value] of Object.entries(allPlugins)) {
|
99
|
+
if (error) {
|
100
|
+
if (key === "error") {
|
101
|
+
// show on page the config is broken, meaning that
|
102
|
+
// it was not possible to disable error plugin neither
|
103
|
+
// as that part wasn't correctly parsed anyway
|
104
|
+
value().then(({ notify }) => notify(error.message));
|
105
|
+
}
|
106
|
+
} else if (!parsed?.plugins?.includes(`!${key}`)) {
|
107
|
+
toBeAwaited.push(value());
|
101
108
|
}
|
102
|
-
} else if (!parsed?.plugins?.includes(`!${key}`)) {
|
103
|
-
toBeAwaited.push(value());
|
104
109
|
}
|
105
|
-
}
|
106
110
|
|
107
|
-
// assign plugins as Promise.all only if needed
|
108
|
-
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
|
111
|
+
// assign plugins as Promise.all only if needed
|
112
|
+
if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
|
113
|
+
|
114
|
+
configs.set(TYPE, { config: parsed, plugins, error });
|
115
|
+
}
|
109
116
|
|
110
|
-
export
|
117
|
+
export default configs;
|
package/src/core.js
CHANGED
@@ -9,10 +9,11 @@ import { queryTarget } from "../node_modules/polyscript/esm/script-handler.js";
|
|
9
9
|
import { dedent, dispatch } from "../node_modules/polyscript/esm/utils.js";
|
10
10
|
import { Hook } from "../node_modules/polyscript/esm/worker/hooks.js";
|
11
11
|
|
12
|
-
import
|
12
|
+
import TYPES from "./types.js";
|
13
|
+
import configs from "./config.js";
|
13
14
|
import sync from "./sync.js";
|
14
15
|
import stdlib from "./stdlib.js";
|
15
|
-
import {
|
16
|
+
import { ErrorCode } from "./exceptions.js";
|
16
17
|
import { robustFetch as fetch, getText } from "./fetch.js";
|
17
18
|
|
18
19
|
const { assign, defineProperty } = Object;
|
@@ -20,11 +21,6 @@ const { assign, defineProperty } = Object;
|
|
20
21
|
// allows lazy element features on code evaluation
|
21
22
|
let currentElement;
|
22
23
|
|
23
|
-
const TYPES = new Map([
|
24
|
-
["py", "pyodide"],
|
25
|
-
["mpy", "micropython"],
|
26
|
-
]);
|
27
|
-
|
28
24
|
// generic helper to disambiguate between custom element and script
|
29
25
|
const isScript = ({ tagName }) => tagName === "SCRIPT";
|
30
26
|
|
@@ -103,7 +99,12 @@ const workerHooks = {
|
|
103
99
|
[...hooks.codeAfterRunWorkerAsync].map(dedent).join("\n"),
|
104
100
|
};
|
105
101
|
|
102
|
+
const exportedConfig = {};
|
103
|
+
export { exportedConfig as config };
|
104
|
+
|
106
105
|
for (const [TYPE, interpreter] of TYPES) {
|
106
|
+
const { config, plugins, error } = configs.get(TYPE);
|
107
|
+
|
107
108
|
// create a unique identifier when/if needed
|
108
109
|
let id = 0;
|
109
110
|
const getID = (prefix = TYPE) => `${prefix}-${id++}`;
|
@@ -273,6 +274,9 @@ for (const [TYPE, interpreter] of TYPES) {
|
|
273
274
|
|
274
275
|
// define py-script only if the config didn't throw an error
|
275
276
|
if (!error) customElements.define(`${TYPE}-script`, PyScriptElement);
|
277
|
+
|
278
|
+
// export the used config without allowing leaks through it
|
279
|
+
exportedConfig[TYPE] = structuredClone(config);
|
276
280
|
}
|
277
281
|
|
278
282
|
// TBD: I think manual worker cases are interesting in pyodide only
|
package/src/types.js
ADDED
package/types/config.d.ts
CHANGED
package/types/core.d.ts
CHANGED
@@ -21,5 +21,6 @@ export namespace hooks {
|
|
21
21
|
let codeAfterRunWorker: Set<string>;
|
22
22
|
let codeAfterRunWorkerAsync: Set<string>;
|
23
23
|
}
|
24
|
-
|
24
|
+
export { exportedConfig as config };
|
25
25
|
import sync from "./sync.js";
|
26
|
+
declare const exportedConfig: {};
|
package/types/types.d.ts
ADDED