code-interpreter
code-interpreter copied to clipboard
Updated JavaScript kernel with typescript, top-level async/await, es module import, promise resolution
changelog
- replaces ijavascript kernel with a fork that includes a compiler step and promise resolution
- removes typescript compiler step as this is now built-in in the kernel
- tests/docs will follow in a separate PR
kernel changes:
- https://github.com/n-riesco/ijavascript/compare/master...e2b-dev:ijavascript:master
- https://github.com/n-riesco/nel/compare/master...e2b-dev:nel:master
one note: currently Node will issue a warning to stderr when trying to import ESM module - we can disable that in the kernel.
Examples
snippet:
import got from "got"
before:
Execution {
results: [],
logs: {
stdout: [],
stderr: [],
},
error: ExecutionError {
name: "SyntaxError",
value: "Cannot use import statement outside a module",
traceback: "evalmachine.<anonymous>:2 import got from \"got\" ^^^^^^SyntaxError: Cannot use import statement outside a module at new Script (node:vm:117:7) at createScript (node:vm:269:10) at Object.runInThisContext (node:vm:317:10) at run ([eval]:1020:15) at onRunRequest ([eval]:864:18) at onMessage ([eval]:828:13) at process.emit (node:events:524:28) at emit (node:internal/child_process:950:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21)",
},
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}
after:
Execution {
results: [],
logs: {
stdout: [],
stderr: [ "(node:322) ExperimentalWarning: vm.USE_MAIN_CONTEXT_DEFAULT_LOADER is an experimental feature and might change at any time\n(Use `node --trace-warnings ...` to show where the warning was created)\n" ],
},
error: undefined,
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}
(see my note regarding the warning)
snippet:
const s: string = "Hello, world!"
s
before:
Execution {
results: [],
logs: {
stdout: [],
stderr: [],
},
error: ExecutionError {
name: "SyntaxError",
value: "Missing initializer in const declaration",
traceback: "evalmachine.<anonymous>:2 const s: string = \"Hello, world!\" ^SyntaxError: Missing initializer in const declaration at new Script (node:vm:117:7) at createScript (node:vm:269:10) at Object.runInThisContext (node:vm:317:10) at run ([eval]:1020:15) at onRunRequest ([eval]:864:18) at onMessage ([eval]:828:13) at process.emit (node:events:524:28) at emit (node:internal/child_process:950:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21)",
},
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}
after:
Execution {
results: [
Result {
isMainResult: true,
text: "Hello, world!",
html: undefined,
markdown: undefined,
svg: undefined,
png: undefined,
jpeg: undefined,
pdf: undefined,
latex: undefined,
json: undefined,
javascript: undefined,
raw: [Object ...],
data: undefined,
chart: undefined,
extra: [Object ...],
formats: [Function: formats],
toJSON: [Function: toJSON],
}
],
logs: {
stdout: [],
stderr: [],
},
error: undefined,
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}
snippet:
const res = await fetch("https://example.com")
await res.text()
before:
Execution {
results: [],
logs: {
stdout: [],
stderr: [],
},
error: ExecutionError {
name: "SyntaxError",
value: "await is only valid in async functions and the top level bodies of modules",
traceback: "evalmachine.<anonymous>:2 const res = await fetch(\"https://example.com\") ^^^^^SyntaxError: await is only valid in async functions and the top level bodies of modules at new Script (node:vm:117:7) at createScript (node:vm:269:10) at Object.runInThisContext (node:vm:317:10) at run ([eval]:1020:15) at onRunRequest ([eval]:864:18) at onMessage ([eval]:828:13) at process.emit (node:events:524:28) at emit (node:internal/child_process:950:14) at process.processTicksAndRejections (node:internal/process/task_queues:83:21)",
},
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}
after:
Execution {
results: [
Result {
isMainResult: true,
text: "<!doctype html>\\n' +\n '<html>\\n' +\n '<head>\\n' +\n ' <title>Example Domain</title>\\n' +\n '\\n' +\n ' <meta charset=\"utf-8\" />\\n' +\n ' <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\\n' +\n ' <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\\n' +\n ' <style type=\"text/css\">\\n' +\n ' body {\\n' +\n ' background-color: #f0f0f2;\\n' +\n ' margin: 0;\\n' +\n ' padding: 0;\\n' +\n ' font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\\n' +\n ' \\n' +\n ' }\\n' +\n ' div {\\n' +\n ' width: 600px;\\n' +\n ' margin: 5em auto;\\n' +\n ' padding: 2em;\\n' +\n ' background-color: #fdfdff;\\n' +\n ' border-radius: 0.5em;\\n' +\n ' box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\\n' +\n ' }\\n' +\n ' a:link, a:visited {\\n' +\n ' color: #38488f;\\n' +\n ' text-decoration: none;\\n' +\n ' }\\n' +\n ' @media (max-width: 700px) {\\n' +\n ' div {\\n' +\n ' margin: 0 auto;\\n' +\n ' width: auto;\\n' +\n ' }\\n' +\n ' }\\n' +\n ' </style> \\n' +\n '</head>\\n' +\n '\\n' +\n '<body>\\n' +\n '<div>\\n' +\n ' <h1>Example Domain</h1>\\n' +\n ' <p>This domain is for use in illustrative examples in documents. You may use this\\n' +\n ' domain in literature without prior coordination or asking for permission.</p>\\n' +\n ' <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\\n' +\n '</div>\\n' +\n '</body>\\n' +\n '</html>\\n",
html: undefined,
markdown: undefined,
svg: undefined,
png: undefined,
jpeg: undefined,
pdf: undefined,
latex: undefined,
json: undefined,
javascript: undefined,
raw: [Object ...],
data: undefined,
chart: undefined,
extra: [Object ...],
formats: [Function: formats],
toJSON: [Function: toJSON],
}
],
logs: {
stdout: [],
stderr: [],
},
error: undefined,
executionCount: 1,
text: [Getter],
toJSON: [Function: toJSON],
}