-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(workerd): remote eval api (#23)
* feat(workerd): remote eval api * chore: workerd-repl demo * chore: lockfile * chore: readme * chore: rename * chore: readme * chore: tweak * test: e2e
- Loading branch information
Showing
13 changed files
with
252 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# workerd-cli | ||
|
||
```sh | ||
$ pnpm cli | ||
[mf:inf] Ready on http://127.0.0.1:44031 | ||
|
||
> env.kv.list() | ||
{ keys: [], list_complete: true, cacheStatus: null } | ||
|
||
> env.kv.put("hello", "world") | ||
|
||
> env.kv.list() | ||
{ keys: [ { name: 'hello' } ], list_complete: true, cacheStatus: null } | ||
|
||
> env.kv.get("hello") | ||
world | ||
|
||
> (await import("/wrangler.toml?raw")).default | ||
compatibility_date = "2024-01-01" | ||
compatibility_flags = ["nodejs_compat"] | ||
kv_namespaces = [ | ||
{ binding = "kv", id = "test-namespace" } | ||
] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import test from "node:test"; | ||
import childProcess from "node:child_process"; | ||
|
||
test("basic", async () => { | ||
const proc = childProcess.spawn("pnpm", ["cli"]); | ||
using _ = proc; | ||
const helper = createProcessHelper(proc); | ||
await helper.waitFor((out) => out.includes("[mf:inf] Ready")); | ||
proc.stdin.write(`env.kv.list()\n`); | ||
await helper.waitFor((out) => out.includes("{ keys: []")); | ||
}); | ||
|
||
function createProcessHelper( | ||
proc: childProcess.ChildProcessWithoutNullStreams, | ||
) { | ||
let stdout = ""; | ||
const listeners = new Set<() => void>(); | ||
proc.stdout.on("data", (data) => { | ||
stdout += String(data); | ||
for (const f of listeners) { | ||
f(); | ||
} | ||
}); | ||
|
||
async function waitFor(predicate: (stdout: string) => boolean) { | ||
return new Promise<void>((resolve) => { | ||
const listener = () => { | ||
if (predicate(stdout)) { | ||
resolve(); | ||
listeners.delete(listener); | ||
} | ||
}; | ||
listeners.add(listener); | ||
}); | ||
} | ||
|
||
return { | ||
get stdout() { | ||
return stdout; | ||
}, | ||
waitFor, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@hiogawa/vite-environment-examples-workerd-cli", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"cli": "tsx src/cli.ts", | ||
"test": "node --import tsx/esm --test e2e/*.test.ts" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@hiogawa/vite-plugin-workerd": "workspace:*" | ||
}, | ||
"volta": { | ||
"extends": "../../package.json" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { createServer } from "vite"; | ||
import { | ||
createWorkerdDevEnvironment, | ||
type WorkerdDevEnvironment, | ||
} from "@hiogawa/vite-plugin-workerd"; | ||
import { Log } from "miniflare"; | ||
import repl from "node:repl"; | ||
|
||
async function main() { | ||
const server = await createServer({ | ||
clearScreen: false, | ||
plugins: [ | ||
{ | ||
name: "virtual-repl", | ||
resolveId(source, _importer, _options) { | ||
if (source.startsWith("virtual:repl/")) { | ||
return "\0" + source; | ||
} | ||
return; | ||
}, | ||
load(id, _options) { | ||
if (id.startsWith("\0virtual:repl/")) { | ||
const cmd = id.slice("\0virtual:repl/".length); | ||
return decodeURI(cmd); | ||
} | ||
return; | ||
}, | ||
}, | ||
], | ||
environments: { | ||
workerd: { | ||
resolve: { | ||
noExternal: true, | ||
}, | ||
dev: { | ||
createEnvironment: (server, name) => | ||
createWorkerdDevEnvironment(server, name, { | ||
miniflare: { | ||
log: new Log(), | ||
}, | ||
wrangler: { | ||
configPath: "./wrangler.toml", | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
const devEnv = server.environments["workerd"] as WorkerdDevEnvironment; | ||
|
||
async function evaluate(cmd: string) { | ||
if (!cmd.includes("return")) { | ||
cmd = `return ${cmd}`; | ||
} | ||
const entrySource = `export default async function({ env }) { ${cmd} }`; | ||
const entry = "virtual:repl/" + encodeURI(entrySource); | ||
await devEnv.api.eval( | ||
entry, | ||
async function (this: any, mod: any) { | ||
const result = await mod.default({ env: this.env }); | ||
if (typeof result !== "undefined") { | ||
console.log(result); | ||
} | ||
}, | ||
[], | ||
); | ||
} | ||
|
||
const replServer = repl.start({ | ||
eval: async (cmd, _context, _filename, callback) => { | ||
try { | ||
await evaluate(cmd); | ||
(callback as any)(null); | ||
} catch (e) { | ||
callback(e as Error, null); | ||
} | ||
}, | ||
}); | ||
|
||
replServer.on("close", () => { | ||
server.close(); | ||
}); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "@tsconfig/strictest/tsconfig.json", | ||
"include": ["src", "vite.config.ts"], | ||
"compilerOptions": { | ||
"exactOptionalPropertyTypes": false, | ||
"verbatimModuleSyntax": true, | ||
"noEmit": true, | ||
"moduleResolution": "Bundler", | ||
"module": "ESNext", | ||
"target": "ESNext", | ||
"lib": ["ESNext", "DOM"], | ||
"types": ["vite/client"], | ||
"jsx": "react-jsx" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { defineConfig } from "vite"; | ||
|
||
export default defineConfig({}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
compatibility_date = "2024-01-01" | ||
compatibility_flags = ["nodejs_compat"] | ||
kv_namespaces = [ | ||
{ binding = "kv", id = "test-namespace" } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.