Skip to content

Commit

Permalink
fix: prefer async iteration vs callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Oct 14, 2023
1 parent 97a2954 commit cb33108
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
19 changes: 8 additions & 11 deletions packages/next/src/experimental/testmode/proxy/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import { UNHANDLED } from './types'
import type { FetchHandler } from './fetch-api'
import { handleFetch } from './fetch-api'

function readBody(req: IncomingMessage): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
const acc: Buffer[] = []
req.on('data', (chunk) => {
acc.push(chunk)
})
req.on('end', () => {
resolve(Buffer.concat(acc))
})
req.on('error', reject)
})
async function readBody(req: IncomingMessage): Promise<Buffer> {
const acc: Buffer[] = []

for await (const chunk of req) {
acc.push(chunk)
}

return Buffer.concat(acc)
}

export async function createProxyServer({
Expand Down
15 changes: 4 additions & 11 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,11 @@ function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
}

return new ReadableStream({
start(controller) {
nodeReadable.on('data', (chunk) => {
start: async (controller) => {
for await (const chunk of nodeReadable) {
controller.enqueue(chunk)
})

nodeReadable.on('end', () => {
controller.close()
})

nodeReadable.on('error', (error) => {
controller.error(error)
})
}
controller.close()
},
})
} else {
Expand Down
9 changes: 4 additions & 5 deletions packages/next/src/server/body-streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ export function requestToBodyStream(
stream: Readable
) {
return new context.ReadableStream({
start(controller) {
stream.on('data', (chunk) =>
start: async (controller) => {
for await (const chunk of stream) {
controller.enqueue(new KUint8Array([...new Uint8Array(chunk)]))
)
stream.on('end', () => controller.close())
stream.on('error', (err) => controller.error(err))
}
controller.close()
},
})
}
Expand Down

0 comments on commit cb33108

Please sign in to comment.