pluviouse commited on
Commit
f3a31b3
·
verified ·
1 Parent(s): b9a1c37

Update app.ts

Browse files
Files changed (1) hide show
  1. app.ts +13 -7
app.ts CHANGED
@@ -13,15 +13,21 @@ const app = new Elysia()
13
  }
14
  }))
15
  .get("/", () => "Hello, Elysia!")
16
- .get("/items/:id", ({ params }) => `Item ID: ${params.id}`)
17
  .get('/exec', async function* ({ query }) {
18
- const proc = Bun.spawn({
19
- cmd: ["bash", "-c", query.cmd]
20
- });
 
21
 
22
- for await (const chunk of proc.stdout) {
23
- yield(new TextDecoder().decode(chunk)) + "\n";
24
- }
 
 
 
 
 
25
  })
26
  .ws('/realtime', {
27
  message(ws, message) {
 
13
  }
14
  }))
15
  .get("/", () => "Hello, Elysia!")
16
+ .get("/items/:id", ({ params, error }) => `Item ID: ${params.id}`)
17
  .get('/exec', async function* ({ query }) {
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) {