@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
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
- // find the shared config for all py-script elements
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
- // catch possible fetch errors
65
- if (config) {
66
- try {
67
- const { json, toml, text, url } = await configDetails(config);
68
- config = text;
69
- if (json || type === "json") {
70
- try {
71
- parsed = JSON.parse(text);
72
- } catch (e) {
73
- error = syntaxError("JSON", url, e);
74
- }
75
- } else if (toml || type === "toml") {
76
- try {
77
- const { parse } = await import(
78
- /* webpackIgnore: true */
79
- "https://cdn.jsdelivr.net/npm/@webreflection/toml-j0.4/toml.js"
80
- );
81
- parsed = parse(text);
82
- } catch (e) {
83
- error = syntaxError("TOML", url, e);
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
- if (error) {
96
- if (key === "error") {
97
- // show on page the config is broken, meaning that
98
- // it was not possible to disable error plugin neither
99
- // as that part wasn't correctly parsed anyway
100
- value().then(({ notify }) => notify(error.message));
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 { parsed as config, plugins, error };
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 { ErrorCode } from "./exceptions.js";
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 { config, plugins, error } from "./config.js";
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
@@ -0,0 +1,4 @@
1
+ export default new Map([
2
+ ["py", "pyodide"],
3
+ ["mpy", "micropython"],
4
+ ]);
package/types/config.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- declare let parsed: any;
2
- export let plugins: any;
3
- export let error: any;
4
- export { parsed as config };
1
+ export default configs;
2
+ declare const configs: Map<any, any>;
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
- import { config } from "./config.js";
24
+ export { exportedConfig as config };
25
25
  import sync from "./sync.js";
26
+ declare const exportedConfig: {};
@@ -0,0 +1,2 @@
1
+ declare const _default: Map<string, string>;
2
+ export default _default;