Skip to content

Commit

Permalink
refactor(server): stream binary data into raw response
Browse files Browse the repository at this point in the history
Way more memory efficient and now supports binary data!
  • Loading branch information
Pkmmte committed Jun 17, 2024
1 parent 694a680 commit dfc4180
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-monkeys-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@robojs/server': patch
---

refactor: stream binary data into raw response
19 changes: 17 additions & 2 deletions packages/plugin-api/src/core/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,23 @@ export function createServerHandler(router: Router, vite?: ViteDevServer): Serve
this.raw.setHeader(key, value)
})
this.raw.statusCode = data.status
data.text().then((text) => {
this.raw.end(text)

// Stream the response body
const reader = data.body.getReader()
const read = async () => {
while (true) {
const { done, value } = await reader.read()

if (done) {
break
} else {
this.raw.write(value)
}
}
}

read().then(() => {
this.raw.end()
})
} else {
this.raw.end(data)
Expand Down

0 comments on commit dfc4180

Please sign in to comment.