@pyscript/core 0.2.2 → 0.2.3

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.
@@ -1 +1 @@
1
- {"version":3,"file":"error-e4fe78fd.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onInterpreterReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onInterpreterReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onInterpreterReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA0BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CAtCAS,EAAMC,mBAAmBC,KAAI,SAASC,EAASC,GAE3CJ,EAAMC,mBAAmBI,OAAOF,GAGhC,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
1
+ {"version":3,"file":"error-e4fe78fd.js","sources":["../src/plugins/error.js"],"sourcesContent":["// PyScript Error Plugin\nimport { hooks } from \"../core.js\";\n\nhooks.onInterpreterReady.add(function override(pyScript) {\n // be sure this override happens only once\n hooks.onInterpreterReady.delete(override);\n\n // trap generic `stderr` to propagate to it regardless\n const { stderr } = pyScript.io;\n\n // override it with our own logic\n pyScript.io.stderr = (error, ...rest) => {\n notify(error.message || error);\n // let other plugins or stderr hook, if any, do the rest\n return stderr(error, ...rest);\n };\n\n // be sure uncaught Python errors are also visible\n addEventListener(\"error\", ({ message }) => {\n if (message.startsWith(\"Uncaught PythonError\")) notify(message);\n });\n});\n\n// Error hook utilities\n\n// Custom function to show notifications\n\n/**\n * Add a banner to the top of the page, notifying the user of an error\n * @param {string} message\n */\nexport function notify(message) {\n const div = document.createElement(\"div\");\n div.className = \"py-error\";\n div.textContent = message;\n div.style.cssText = `\n border: 1px solid red;\n background: #ffdddd;\n color: black;\n font-family: courier, monospace;\n white-space: pre;\n overflow-x: auto;\n padding: 8px;\n margin-top: 8px;\n `;\n document.body.append(div);\n}\n"],"names":["notify","message","div","document","createElement","className","textContent","style","cssText","body","append","hooks","onInterpreterReady","add","override","pyScript","delete","stderr","io","error","rest","addEventListener","startsWith"],"mappings":"kCA+BO,SAASA,EAAOC,GACnB,MAAMC,EAAMC,SAASC,cAAc,OACnCF,EAAIG,UAAY,WAChBH,EAAII,YAAcL,EAClBC,EAAIK,MAAMC,QAAU,6MAUpBL,SAASM,KAAKC,OAAOR,EACzB,CA3CAS,EAAMC,mBAAmBC,KAAI,SAASC,EAASC,GAE3CJ,EAAMC,mBAAmBI,OAAOF,GAGhC,MAAMG,OAAEA,GAAWF,EAASG,GAG5BH,EAASG,GAAGD,OAAS,CAACE,KAAUC,KAC5BpB,EAAOmB,EAAMlB,SAAWkB,GAEjBF,EAAOE,KAAUC,IAI5BC,iBAAiB,SAAS,EAAGpB,cACrBA,EAAQqB,WAAW,yBAAyBtB,EAAOC,EAAQ,GAEvE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pyscript/core",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "description": "PyScript",
6
6
  "module": "./index.js",
@@ -38,7 +38,7 @@
38
38
  "devDependencies": {
39
39
  "@rollup/plugin-node-resolve": "^15.2.1",
40
40
  "@rollup/plugin-terser": "^0.4.3",
41
- "rollup": "^3.29.2",
41
+ "rollup": "^3.29.3",
42
42
  "rollup-plugin-postcss": "^4.0.2",
43
43
  "rollup-plugin-string": "^3.0.0",
44
44
  "static-handler": "^0.4.2",
package/src/config.js CHANGED
@@ -20,9 +20,10 @@ const badURL = (url, expected = "") => {
20
20
  * Given a string, returns its trimmed content as text,
21
21
  * fetching it from a file if the content is a URL.
22
22
  * @param {string} config either JSON, TOML, or a file to fetch
23
+ * @param {string?} type the optional type to enforce
23
24
  * @returns {{json: boolean, toml: boolean, text: string}}
24
25
  */
25
- const configDetails = async (config) => {
26
+ const configDetails = async (config, type) => {
26
27
  let text = config?.trim();
27
28
  // we only support an object as root config
28
29
  let url = "",
@@ -49,9 +50,18 @@ const syntaxError = (type, url, { message }) => {
49
50
  const configs = new Map();
50
51
 
51
52
  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`);
53
+ /** @type {Promise<any> | undefined} A Promise wrapping any plugins which should be loaded. */
54
+ let plugins;
55
+
56
+ /** @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} ) */
57
+ let parsed;
58
+
59
+ /** @type {SyntaxError | undefined} The error thrown when parsing the PyScript config, if any.*/
60
+ let error;
61
+
62
+ let config,
63
+ type,
64
+ pyConfig = $(`${TYPE}-config`);
55
65
  if (pyConfig) {
56
66
  config = pyConfig.getAttribute("src") || pyConfig.textContent;
57
67
  type = pyConfig.getAttribute("type");
@@ -68,7 +78,7 @@ for (const [TYPE] of TYPES) {
68
78
  // catch possible fetch errors
69
79
  if (config) {
70
80
  try {
71
- const { json, toml, text, url } = await configDetails(config);
81
+ const { json, toml, text, url } = await configDetails(config, type);
72
82
  config = text;
73
83
  if (json || type === "json") {
74
84
  try {
package/src/exceptions.js CHANGED
@@ -23,7 +23,17 @@ export const ErrorCode = {
23
23
  FETCH_UNAVAILABLE_ERROR: "PY0503",
24
24
  };
25
25
 
26
+ /**
27
+ * Keys of the ErrorCode object
28
+ * @typedef {keyof ErrorCode} ErrorCodes
29
+ * */
30
+
26
31
  export class UserError extends Error {
32
+ /**
33
+ * @param {ErrorCodes} errorCode
34
+ * @param {string} message
35
+ * @param {string} messageType
36
+ * */
27
37
  constructor(errorCode, message = "", messageType = "text") {
28
38
  super(`(${errorCode}): ${message}`);
29
39
  this.errorCode = errorCode;
@@ -33,6 +43,10 @@ export class UserError extends Error {
33
43
  }
34
44
 
35
45
  export class FetchError extends UserError {
46
+ /**
47
+ * @param {ErrorCodes} errorCode
48
+ * @param {string} message
49
+ * */
36
50
  constructor(errorCode, message) {
37
51
  super(errorCode, message);
38
52
  this.name = "FetchError";
@@ -40,12 +54,23 @@ export class FetchError extends UserError {
40
54
  }
41
55
 
42
56
  export class InstallError extends UserError {
57
+ /**
58
+ * @param {ErrorCodes} errorCode
59
+ * @param {string} message
60
+ * */
43
61
  constructor(errorCode, message) {
44
62
  super(errorCode, message);
45
63
  this.name = "InstallError";
46
64
  }
47
65
  }
48
66
 
67
+ /**
68
+ * Internal function for creating alert banners on the page
69
+ * @param {string} message The message to be displayed to the user
70
+ * @param {string} level The alert level of the message. Can be any string; 'error' or 'warning' cause matching messages to be emitted to the console
71
+ * @param {string} [messageType="text"] If set to "html", the message content will be assigned to the banner's innerHTML directly, instead of its textContent
72
+ * @param {any} [logMessage=true] An additional flag for whether the message should be sent to the console log.
73
+ */
49
74
  export function _createAlertBanner(
50
75
  message,
51
76
  level,
@@ -24,6 +24,11 @@ hooks.onInterpreterReady.add(function override(pyScript) {
24
24
  // Error hook utilities
25
25
 
26
26
  // Custom function to show notifications
27
+
28
+ /**
29
+ * Add a banner to the top of the page, notifying the user of an error
30
+ * @param {string} message
31
+ */
27
32
  export function notify(message) {
28
33
  const div = document.createElement("div");
29
34
  div.className = "py-error";
package/src/sync.js CHANGED
@@ -1,4 +1,8 @@
1
1
  export default {
2
+ /**
3
+ * 'Sleep' for the given number of seconds. Used to implement Python's time.sleep in Worker threads.
4
+ * @param {number} seconds The number of seconds to sleep.
5
+ */
2
6
  sleep(seconds) {
3
7
  return new Promise(($) => setTimeout($, seconds * 1000));
4
8
  },
@@ -1,4 +1,11 @@
1
- export function _createAlertBanner(message: any, level: any, messageType?: string, logMessage?: boolean): void;
1
+ /**
2
+ * Internal function for creating alert banners on the page
3
+ * @param {string} message The message to be displayed to the user
4
+ * @param {string} level The alert level of the message. Can be any string; 'error' or 'warning' cause matching messages to be emitted to the console
5
+ * @param {string} [messageType="text"] If set to "html", the message content will be assigned to the banner's innerHTML directly, instead of its textContent
6
+ * @param {any} [logMessage=true] An additional flag for whether the message should be sent to the console log.
7
+ */
8
+ export function _createAlertBanner(message: string, level: string, messageType?: string, logMessage?: any): void;
2
9
  export namespace ErrorCode {
3
10
  let GENERIC: string;
4
11
  let CONFLICTING_CODE: string;
@@ -15,14 +22,50 @@ export namespace ErrorCode {
15
22
  let FETCH_SERVER_ERROR: string;
16
23
  let FETCH_UNAVAILABLE_ERROR: string;
17
24
  }
25
+ /**
26
+ * Keys of the ErrorCode object
27
+ * @typedef {keyof ErrorCode} ErrorCodes
28
+ * */
18
29
  export class UserError extends Error {
19
- constructor(errorCode: any, message?: string, messageType?: string);
20
- errorCode: any;
30
+ /**
31
+ * @param {ErrorCodes} errorCode
32
+ * @param {string} message
33
+ * @param {string} messageType
34
+ * */
35
+ constructor(errorCode: ErrorCodes, message?: string, messageType?: string);
36
+ errorCode: "GENERIC" | "CONFLICTING_CODE" | "BAD_CONFIG" | "MICROPIP_INSTALL_ERROR" | "BAD_PLUGIN_FILE_EXTENSION" | "NO_DEFAULT_EXPORT" | "TOP_LEVEL_AWAIT" | "FETCH_ERROR" | "FETCH_NAME_ERROR" | "FETCH_UNAUTHORIZED_ERROR" | "FETCH_FORBIDDEN_ERROR" | "FETCH_NOT_FOUND_ERROR" | "FETCH_SERVER_ERROR" | "FETCH_UNAVAILABLE_ERROR";
21
37
  messageType: string;
22
38
  }
23
39
  export class FetchError extends UserError {
24
- constructor(errorCode: any, message: any);
40
+ /**
41
+ * @param {ErrorCodes} errorCode
42
+ * @param {string} message
43
+ * */
44
+ constructor(errorCode: ErrorCodes, message: string);
25
45
  }
26
46
  export class InstallError extends UserError {
27
- constructor(errorCode: any, message: any);
47
+ /**
48
+ * @param {ErrorCodes} errorCode
49
+ * @param {string} message
50
+ * */
51
+ constructor(errorCode: ErrorCodes, message: string);
28
52
  }
53
+ /**
54
+ * Keys of the ErrorCode object
55
+ */
56
+ export type ErrorCodes = keyof {
57
+ GENERIC: string;
58
+ CONFLICTING_CODE: string;
59
+ BAD_CONFIG: string;
60
+ MICROPIP_INSTALL_ERROR: string;
61
+ BAD_PLUGIN_FILE_EXTENSION: string;
62
+ NO_DEFAULT_EXPORT: string;
63
+ TOP_LEVEL_AWAIT: string;
64
+ FETCH_ERROR: string;
65
+ FETCH_NAME_ERROR: string;
66
+ FETCH_UNAUTHORIZED_ERROR: string;
67
+ FETCH_FORBIDDEN_ERROR: string;
68
+ FETCH_NOT_FOUND_ERROR: string;
69
+ FETCH_SERVER_ERROR: string;
70
+ FETCH_UNAVAILABLE_ERROR: string;
71
+ };
@@ -1 +1,5 @@
1
- export function notify(message: any): void;
1
+ /**
2
+ * Add a banner to the top of the page, notifying the user of an error
3
+ * @param {string} message
4
+ */
5
+ export function notify(message: string): void;
package/types/sync.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  declare namespace _default {
2
- function sleep(seconds: any): Promise<any>;
2
+ /**
3
+ * 'Sleep' for the given number of seconds. Used to implement Python's time.sleep in Worker threads.
4
+ * @param {number} seconds The number of seconds to sleep.
5
+ */
6
+ function sleep(seconds: number): Promise<any>;
3
7
  }
4
8
  export default _default;