Skip to content

Commit 4e782d0

Browse files
committed
examples: thread env and stdin through the worker-javascript exec route
The exec handler now forwards env and stdin from the request body into runtime.exec, so a posted module can read process.env and process.stdin. Document the extended request shape and add a smoke-test recipe that pipes stdin and reads an env value.
1 parent a5f07ce commit 4e782d0

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

examples/worker-javascript/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ npm run seed:r2 --workspace @example/computer-worker-javascript
8686
PUT /c/<name>/file/workspace/<path> raw body → writeFile at /workspace/<path>
8787
GET /c/<name>/file/workspace/<path> octet-stream of /workspace/<path>
8888
(any path outside /workspace returns 400)
89-
POST /c/<name>/exec { source, input?, cwd? }
89+
POST /c/<name>/exec { source, input?, cwd?, env?, stdin? }
9090
cwd defaults to /workspace
9191
→ JSON { status, exitCode, stdout, stderr, value }
9292
```
@@ -120,8 +120,18 @@ curl -X POST http://127.0.0.1:8787/c/demo/exec \
120120
curl -X POST http://127.0.0.1:8787/c/demo/exec \
121121
-H 'content-type: application/json' \
122122
-d '{"source":"export default (input) => input.n * 2;","input":{"n":21}}'
123+
124+
curl -X POST http://127.0.0.1:8787/c/demo/exec \
125+
-H 'content-type: application/json' \
126+
-d '{"source":"export default async () => { let s = \"\"; for await (const c of process.stdin) s += new TextDecoder().decode(c); return process.env.WHO + \":\" + s; };","env":{"WHO":"demo"},"stdin":"piped"}'
123127
```
124128

129+
`env` populates `process.env` (only the values you pass; the host
130+
environment is never exposed), and `stdin` is readable through
131+
`process.stdin`. `console.log` / `console.error` and
132+
`process.stdout` / `process.stderr` writes come back as the result's
133+
`stdout` and `stderr`.
134+
125135
## Layout
126136

127137
```

examples/worker-javascript/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface ExecRequest {
2525
source?: string;
2626
input?: WorkspaceRuntimeValue;
2727
cwd?: string;
28+
env?: Record<string, string>;
29+
stdin?: string;
2830
}
2931

3032
const MOUNT_ROOT = "/workspace";
@@ -130,6 +132,8 @@ async function handleExec(request: Request, env: Env, name: string): Promise<Res
130132
backend: "worker-javascript",
131133
cwd: body.cwd,
132134
input: body.input,
135+
env: body.env,
136+
stdin: body.stdin,
133137
encoding: "utf8",
134138
});
135139
const result = await handle.result();

0 commit comments

Comments
 (0)