@pyscript/core 0.1.21 → 0.1.22
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 +3 -3
- package/dist/core.js.map +1 -1
- package/package.json +2 -2
- package/src/config.js +1 -1
- package/src/core.js +33 -0
- package/src/exceptions.js +7 -6
- package/types/config.d.ts +2 -1
- package/types/exceptions.d.ts +6 -5
    
        package/package.json
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            {
         | 
| 2 2 | 
             
                "name": "@pyscript/core",
         | 
| 3 | 
            -
                "version": "0.1. | 
| 3 | 
            +
                "version": "0.1.22",
         | 
| 4 4 | 
             
                "type": "module",
         | 
| 5 5 | 
             
                "description": "PyScript",
         | 
| 6 6 | 
             
                "module": "./index.js",
         | 
| @@ -33,7 +33,7 @@ | |
| 33 33 | 
             
                "dependencies": {
         | 
| 34 34 | 
             
                    "@ungap/with-resolvers": "^0.1.0",
         | 
| 35 35 | 
             
                    "basic-devtools": "^0.1.6",
         | 
| 36 | 
            -
                    "polyscript": "^0.3. | 
| 36 | 
            +
                    "polyscript": "^0.3.10"
         | 
| 37 37 | 
             
                },
         | 
| 38 38 | 
             
                "devDependencies": {
         | 
| 39 39 | 
             
                    "@rollup/plugin-node-resolve": "^15.2.1",
         | 
    
        package/src/config.js
    CHANGED
    
    | @@ -107,4 +107,4 @@ for (const [key, value] of Object.entries(allPlugins)) { | |
| 107 107 | 
             
            // assign plugins as Promise.all only if needed
         | 
| 108 108 | 
             
            if (toBeAwaited.length) plugins = Promise.all(toBeAwaited);
         | 
| 109 109 |  | 
| 110 | 
            -
            export { config, plugins, error };
         | 
| 110 | 
            +
            export { parsed as config, plugins, error };
         | 
    
        package/src/core.js
    CHANGED
    
    | @@ -9,6 +9,7 @@ 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 13 | 
             
            import sync from "./sync.js";
         | 
| 13 14 | 
             
            import stdlib from "./stdlib.js";
         | 
| 14 15 | 
             
            import { config, plugins, error } from "./config.js";
         | 
| @@ -40,6 +41,36 @@ const after = () => { | |
| 40 41 | 
             
                delete document.currentScript;
         | 
| 41 42 | 
             
            };
         | 
| 42 43 |  | 
| 44 | 
            +
            /**
         | 
| 45 | 
            +
             * Some content that might contain Python/JS comments only.
         | 
| 46 | 
            +
             * @param {string} text some content to evaluate
         | 
| 47 | 
            +
             * @returns {boolean}
         | 
| 48 | 
            +
             */
         | 
| 49 | 
            +
            const hasCommentsOnly = (text) =>
         | 
| 50 | 
            +
                !text
         | 
| 51 | 
            +
                    .replace(/\/\*[\s\S]*?\*\//g, "")
         | 
| 52 | 
            +
                    .replace(/^\s*(?:\/\/|#).*/gm, "")
         | 
| 53 | 
            +
                    .trim();
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            /**
         | 
| 56 | 
            +
             *
         | 
| 57 | 
            +
             * @param {Element} scriptOrPyScript the element with possible `src` or `worker` content
         | 
| 58 | 
            +
             * @returns {boolean}
         | 
| 59 | 
            +
             */
         | 
| 60 | 
            +
            const hasAmbiguousContent = (
         | 
| 61 | 
            +
                io,
         | 
| 62 | 
            +
                { localName, textContent, attributes: { src, worker } },
         | 
| 63 | 
            +
            ) => {
         | 
| 64 | 
            +
                // any `src` or a non-empty `worker` attribute + not just comments
         | 
| 65 | 
            +
                if ((src || worker?.value) && !hasCommentsOnly(textContent)) {
         | 
| 66 | 
            +
                    io.stderr(
         | 
| 67 | 
            +
                        `(${ErrorCode.CONFLICTING_CODE}) a ${localName} tag has content shadowed by attributes`,
         | 
| 68 | 
            +
                    );
         | 
| 69 | 
            +
                    return true;
         | 
| 70 | 
            +
                }
         | 
| 71 | 
            +
                return false;
         | 
| 72 | 
            +
            };
         | 
| 73 | 
            +
             | 
| 43 74 | 
             
            /**
         | 
| 44 75 | 
             
             * Given a generic DOM Element, tries to fetch the 'src' attribute, if present.
         | 
| 45 76 | 
             
             * It either throws an error if the 'src' can't be fetched or it returns a fallback
         | 
| @@ -172,6 +203,7 @@ error || | |
| 172 203 | 
             
                            callback(pyodide, element);
         | 
| 173 204 |  | 
| 174 205 | 
             
                        if (isScript(element)) {
         | 
| 206 | 
            +
                            if (hasAmbiguousContent(pyodide.io, element)) return;
         | 
| 175 207 | 
             
                            const {
         | 
| 176 208 | 
             
                                attributes: { async: isAsync, target },
         | 
| 177 209 | 
             
                            } = element;
         | 
| @@ -222,6 +254,7 @@ class PyScriptElement extends HTMLElement { | |
| 222 254 | 
             
                    if (!this.executed) {
         | 
| 223 255 | 
             
                        this.executed = true;
         | 
| 224 256 | 
             
                        const { io, run, runAsync } = await this._pyodide.promise;
         | 
| 257 | 
            +
                        if (hasAmbiguousContent(io, this)) return;
         | 
| 225 258 | 
             
                        const runner = this.hasAttribute("async") ? runAsync : run;
         | 
| 226 259 | 
             
                        this.srcCode = await fetchSource(this, io, !this.childElementCount);
         | 
| 227 260 | 
             
                        this.replaceChildren();
         | 
    
        package/src/exceptions.js
    CHANGED
    
    | @@ -7,19 +7,20 @@ const CLOSEBUTTON = | |
| 7 7 | 
             
             */
         | 
| 8 8 | 
             
            export const ErrorCode = {
         | 
| 9 9 | 
             
                GENERIC: "PY0000", // Use this only for development then change to a more specific error code
         | 
| 10 | 
            +
                CONFLICTING_CODE: "PY0409",
         | 
| 11 | 
            +
                BAD_CONFIG: "PY1000",
         | 
| 12 | 
            +
                MICROPIP_INSTALL_ERROR: "PY1001",
         | 
| 13 | 
            +
                BAD_PLUGIN_FILE_EXTENSION: "PY2000",
         | 
| 14 | 
            +
                NO_DEFAULT_EXPORT: "PY2001",
         | 
| 15 | 
            +
                TOP_LEVEL_AWAIT: "PY9000",
         | 
| 16 | 
            +
                // Currently these are created depending on error code received from fetching
         | 
| 10 17 | 
             
                FETCH_ERROR: "PY0001",
         | 
| 11 18 | 
             
                FETCH_NAME_ERROR: "PY0002",
         | 
| 12 | 
            -
                // Currently these are created depending on error code received from fetching
         | 
| 13 19 | 
             
                FETCH_UNAUTHORIZED_ERROR: "PY0401",
         | 
| 14 20 | 
             
                FETCH_FORBIDDEN_ERROR: "PY0403",
         | 
| 15 21 | 
             
                FETCH_NOT_FOUND_ERROR: "PY0404",
         | 
| 16 22 | 
             
                FETCH_SERVER_ERROR: "PY0500",
         | 
| 17 23 | 
             
                FETCH_UNAVAILABLE_ERROR: "PY0503",
         | 
| 18 | 
            -
                BAD_CONFIG: "PY1000",
         | 
| 19 | 
            -
                MICROPIP_INSTALL_ERROR: "PY1001",
         | 
| 20 | 
            -
                BAD_PLUGIN_FILE_EXTENSION: "PY2000",
         | 
| 21 | 
            -
                NO_DEFAULT_EXPORT: "PY2001",
         | 
| 22 | 
            -
                TOP_LEVEL_AWAIT: "PY9000",
         | 
| 23 24 | 
             
            };
         | 
| 24 25 |  | 
| 25 26 | 
             
            export class UserError extends Error {
         | 
    
        package/types/config.d.ts
    CHANGED
    
    
    
        package/types/exceptions.d.ts
    CHANGED
    
    | @@ -1,6 +1,12 @@ | |
| 1 1 | 
             
            export function _createAlertBanner(message: any, level: any, messageType?: string, logMessage?: boolean): void;
         | 
| 2 2 | 
             
            export namespace ErrorCode {
         | 
| 3 3 | 
             
                let GENERIC: string;
         | 
| 4 | 
            +
                let CONFLICTING_CODE: string;
         | 
| 5 | 
            +
                let BAD_CONFIG: string;
         | 
| 6 | 
            +
                let MICROPIP_INSTALL_ERROR: string;
         | 
| 7 | 
            +
                let BAD_PLUGIN_FILE_EXTENSION: string;
         | 
| 8 | 
            +
                let NO_DEFAULT_EXPORT: string;
         | 
| 9 | 
            +
                let TOP_LEVEL_AWAIT: string;
         | 
| 4 10 | 
             
                let FETCH_ERROR: string;
         | 
| 5 11 | 
             
                let FETCH_NAME_ERROR: string;
         | 
| 6 12 | 
             
                let FETCH_UNAUTHORIZED_ERROR: string;
         | 
| @@ -8,11 +14,6 @@ export namespace ErrorCode { | |
| 8 14 | 
             
                let FETCH_NOT_FOUND_ERROR: string;
         | 
| 9 15 | 
             
                let FETCH_SERVER_ERROR: string;
         | 
| 10 16 | 
             
                let FETCH_UNAVAILABLE_ERROR: string;
         | 
| 11 | 
            -
                let BAD_CONFIG: string;
         | 
| 12 | 
            -
                let MICROPIP_INSTALL_ERROR: string;
         | 
| 13 | 
            -
                let BAD_PLUGIN_FILE_EXTENSION: string;
         | 
| 14 | 
            -
                let NO_DEFAULT_EXPORT: string;
         | 
| 15 | 
            -
                let TOP_LEVEL_AWAIT: string;
         | 
| 16 17 | 
             
            }
         | 
| 17 18 | 
             
            export class UserError extends Error {
         | 
| 18 19 | 
             
                constructor(errorCode: any, message?: string, messageType?: string);
         |