-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathbody-streams.ts
196 lines (176 loc) · 5.17 KB
/
body-streams.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import type { IncomingMessage } from 'http'
import type { Writable } from 'stream'
import { Readable } from 'stream'
type BodyStream = ReadableStream<Uint8Array>
/**
* An interface that encapsulates body stream cloning
* of an incoming request.
*/
export function getClonableBodyStream<T extends IncomingMessage>(
incomingMessage: T,
KUint8Array: typeof Uint8Array,
KTransformStream: typeof TransformStream,
) {
let bufferedBodyStream: BodyStream | null = null
return {
/**
* Replaces the original request body if necessary.
* This is done because once we read the body from the original request,
* we can't read it again.
*/
finalize(): void {
if (bufferedBodyStream) {
replaceRequestBody(
incomingMessage,
bodyStreamToNodeStream(bufferedBodyStream),
)
}
},
/**
* Clones the body stream
* to pass into a middleware
*/
cloneBodyStream(): BodyStream {
const originalStream =
bufferedBodyStream ??
requestToBodyStream(incomingMessage, KUint8Array, KTransformStream)
const [stream1, stream2] = originalStream.tee()
bufferedBodyStream = stream1
return stream2
},
}
}
/**
* Creates a ReadableStream from a Node.js HTTP request
*/
function requestToBodyStream(
request: IncomingMessage,
KUint8Array: typeof Uint8Array,
KTransformStream: typeof TransformStream,
): BodyStream {
const transform = new KTransformStream<Uint8Array, Uint8Array>({
start(controller) {
request.on('data', (chunk) =>
controller.enqueue(new KUint8Array([...new Uint8Array(chunk)])),
)
request.on('end', () => controller.terminate())
request.on('error', (err) => controller.error(err))
},
})
return transform.readable as unknown as ReadableStream<Uint8Array>
}
function bodyStreamToNodeStream(bodyStream: BodyStream): Readable {
const reader = bodyStream.getReader()
return Readable.from(
(async function* () {
while (true) {
const { done, value } = await reader.read()
if (done) {
return
}
yield value
}
})(),
)
}
function replaceRequestBody<T extends IncomingMessage>(
base: T,
stream: Readable,
): T {
for (const key in stream) {
let v = stream[key as keyof Readable] as any
if (typeof v === 'function') {
v = v.bind(stream)
}
base[key as keyof T] = v
}
return base
}
function isUint8ArrayChunk(value: any): value is Uint8Array {
return value?.constructor?.name == 'Uint8Array'
}
/**
* Creates an async iterator from a ReadableStream that ensures that every
* emitted chunk is a `Uint8Array`. If there is some invalid chunk it will
* throw.
*/
export async function* consumeUint8ArrayReadableStream(
body?: ReadableStream,
): AsyncGenerator<Uint8Array, void, unknown> {
const reader = body?.getReader()
if (reader) {
let error
try {
while (true) {
const { done, value } = await reader.read()
if (done) {
return
}
if (!isUint8ArrayChunk(value)) {
error = new TypeError('This ReadableStream did not return bytes.')
break
}
yield value
}
} finally {
if (error) {
reader.cancel(error)
throw error
} else {
reader.cancel()
}
}
}
}
/**
* Pipes the chunks of a BodyStream into a Response. This optimizes for
* laziness, pauses reading if we experience back-pressure, and handles early
* disconnects by the client on the other end of the server response.
*/
export async function pipeBodyStreamToResponse(
body: BodyStream | null,
res: Writable,
) {
if (!body) return
// If the client has already disconnected, then we don't need to pipe anything.
if (res.destroyed) return body.cancel()
// When the server pushes more data than the client reads, then we need to
// wait for the client to catch up before writing more data. We register this
// generic handler once so that we don't incur constant register/unregister
// calls.
let drainResolve: () => void
res.on('drain', () => drainResolve?.())
// If the user aborts, then we'll receive a close event before the
// body closes. In that case, we want to end the streaming.
let open = true
res.on('close', () => {
open = false
drainResolve?.()
})
const reader = body.getReader()
while (open) {
const { done, value } = await reader.read()
if (done) break
if (!isUint8ArrayChunk(value)) {
const error = new TypeError('This ReadableStream did not return bytes.')
reader.cancel(error)
throw error
}
if (open) {
const bufferSpaceAvailable = res.write(value)
// If there's no more space in the buffer, then we need to wait on the
// client to read data before pushing again.
if (!bufferSpaceAvailable) {
await new Promise<void>((res) => {
drainResolve = res
})
}
}
// If the client disconnected early, then we need to cleanup the stream.
// This cannot be joined with the above if-statement, because the client may
// have disconnected while waiting for a drain signal.
if (!open) {
return reader.cancel()
}
}
}