open-webui/static/pyscript/py-terminal-CgcHH2nx.js.map

1 line
16 KiB
Plaintext
Raw Normal View History

2024-05-17 03:49:28 +00:00
{"version":3,"file":"py-terminal-CgcHH2nx.js","sources":["../src/plugins/py-terminal.js"],"sourcesContent":["// PyScript py-terminal plugin\nimport { TYPES, hooks } from \"../core.js\";\nimport { notify } from \"./error.js\";\nimport { customObserver, defineProperties } from \"polyscript/exports\";\n\n// will contain all valid selectors\nconst SELECTORS = [];\n\n// show the error on main and\n// stops the module from keep executing\nconst notifyAndThrow = (message) => {\n notify(message);\n throw new Error(message);\n};\n\nconst onceOnMain = ({ attributes: { worker } }) => !worker;\n\nconst bootstrapped = new WeakSet();\n\nlet addStyle = true;\n\n// this callback will be serialized as string and it never needs\n// to be invoked multiple times. Each xworker here is bootstrapped\n// only once thanks to the `sync.is_pyterminal()` check.\nconst workerReady = ({ interpreter, io, run, type }, { sync }) => {\n if (!sync.is_pyterminal()) return;\n\n // in workers it's always safe to grab the polyscript currentScript\n // the ugly `_` dance is due MicroPython not able to import via:\n // `from polyscript.currentScript import terminal as __terminal__`\n run(\n \"from polyscript import currentScript as _; __terminal__ = _.terminal; del _\",\n );\n\n let data = \"\";\n const { pyterminal_read, pyterminal_write } = sync;\n const decoder = new TextDecoder();\n const generic = {\n isatty: false,\n write(buffer) {\n data = decoder.decode(buffer);\n pyterminal_write(data);\n return buffer.length;\n },\n };\n\n // This part works already in both Pyodide and MicroPython\n io.stderr = (error) => {\n pyterminal_write(String(error.message || error));\n };\n\n // MicroPython has no code or code.interact()\n // This part patches it in a way that simulates\n // the code.interact() module in Pyodide.\n if (type === \"mpy\") {\n // monkey patch global input otherwise broken in MicroPython\n interpreter.registerJsModule(\"_pyscript_input\", {\n input: pyterminal_read,\n });\n run(\"from _pyscript_input import input\");\n\n // this is needed to avoid truncated unicode in MicroPython\n // the reason is that `linebuffer` false just send one byte\n // per time and readline here doesn't like it much.\n // MicroPython also has issues with code-points and\n // replProcessChar(byte) but that function accepts only\n // one byte per time so ... we have an issue!\n // @see https://github.com/pyscript/pyscript/pull/2018\n // @see https://github.com/WebReflection/buffer-points\n const bufferPoints = (stdio) => {\n const bytes = [];\n let needed = 0;\n return (buffer) => {\n let written = 0;\n for (const byte of buffer) {\n bytes.push(byte);\n // @see https://encoding.spec.whatwg.org/#utf-8-bytes-needed\n if (needed) needed--;\n else if (0xc2 <= byte && byte <= 0xdf) needed = 1;\n else if (0xe0 <= byte && byte <= 0xef) needed = 2;\n else if (0xf0 <= byte && byte <= 0xf4) needed = 3;\n if (!needed) {\n written += bytes.length;\n stdio(new Uint8Array(bytes.splice(0)));\n }\n }\n return written;\n };\n };\n\n io.stdout = bufferPoints(generic.write);\n\n // tiny shim of the code module with only interact\n // to bootstrap a REPL like environment\n interpreter.registerJsModule(\"code\", {\n interact() {\n let input = \"\";\n let length = 1;\n\n const encoder = new TextEncoder();\n const acc = [];\n const handlePoints = bufferPoints((buffer) => {\n acc.push(...buffer);\n pyterminal_write(d