pluviouse commited on
Commit
e620dbd
·
verified ·
1 Parent(s): e60f600

Update app.ts

Browse files
Files changed (1) hide show
  1. app.ts +8 -10
app.ts CHANGED
@@ -15,19 +15,17 @@ const app = new Elysia()
15
  .get("/", () => "Hello, Elysia!")
16
  .get("/items/:id", ({ params }) => `Item ID: ${params.id}`)
17
  .get('/exec', async function* ({ query, error }) {
18
- try {
19
  const proc = Bun.spawn({
20
- cmd: ["bash", "-c", query.cmd]
 
21
  });
22
 
23
- for await (let chunk of proc.stdout) {
24
- chunk = new TextDecoder().decode(chunk)
25
- const result = chunk.endsWith("\n") ? chunk : chunk + "\n"
26
- yield result
27
- }
28
- } catch(e) {
29
- error(400, String(e))
30
- }
31
  })
32
  .ws('/realtime', {
33
  message(ws, message) {
 
15
  .get("/", () => "Hello, Elysia!")
16
  .get("/items/:id", ({ params }) => `Item ID: ${params.id}`)
17
  .get('/exec', async function* ({ query, error }) {
18
+ if (!query.cmd) return error(400, "required query 'cmd'")
19
  const proc = Bun.spawn({
20
+ cmd: ["bash", "-c", query.cmd],
21
+ stderr: "pipe",
22
  });
23
 
24
+ const err = await new Response(proc.stderr).text();
25
+ if (err) yield err
26
+ const out = await new Response(proc.stdout).text();
27
+ if (out) yield out
28
+ yield "done."
 
 
 
29
  })
30
  .ws('/realtime', {
31
  message(ws, message) {