Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

frame-decoder.ts

281 lines7.7 KB

1import {
2 FRAME_HEADER_SIZE,
3 FRAME_TYPE_CHUNK,
4 FRAME_TYPE_END,
5 FRAME_TYPE_ERROR,
6 FRAME_TYPE_JSON,
7 MAX_FRAMED_STREAMS,
8 MAX_FRAME_PAYLOAD_SIZE,
9 MAX_UNREAD_RAW_STREAM_BYTES,
10} from '../constants'
11​
12const decoder = new TextDecoder()
13const empty: Uint8Array = new Uint8Array()
14// With a zero high-water mark, `-desiredSize` is the unread byte count.
15const rawStreamQueue = new ByteLengthQueuingStrategy({ highWaterMark: 0 })
16​
17type Closed = 1
18type Failed = readonly [unknown]
19type State = 0 | Closed | Failed
20type RawController =
21 | ReadableStreamDefaultController<Uint8Array>
22 | null // canceled by its consumer
23 | false // ended by the wire or decoder
24type RawEntry = [ReadableStream<Uint8Array>, RawController]
25​
26export function createFrameDecoder(input: ReadableStream<Uint8Array>) {
27 const reader = input.getReader()
28 const rawStreams = new Map<number, RawEntry>()
29 let state = 0 as State
30 let resume: (() => void) | undefined
31 let jsonController!: ReadableStreamDefaultController<string>
32​
33 const wake = () => {
34 resume?.()
35 resume = undefined
36 }
37​
38 const settleRaw = (entry: RawEntry, terminal: Closed | Failed) => {
39 const controller = entry[1]
40 entry[1] = false
41 if (controller) {
42 if (terminal === 1) {
43 controller.close()
44 } else {
45 controller.error(terminal[0])
46 }
47 }
48 }
49​
50 const chunks = new ReadableStream<string>({
51 start(controller) {
52 jsonController = controller
53 },
54 pull: wake,
55 cancel(reason) {
56 // Every raw stream, including one requested later, ends with this reason.
57 const failed: Failed = [
58 reason === undefined ? new Error('Framed response cancelled') : reason,
59 ]
60 state = failed
61 wake()
62 void reader.cancel(reason).catch(() => {})
63 for (const entry of rawStreams.values()) {
64 settleRaw(entry, failed)
65 }
66 },
67 })
68​
69 function getRaw(id: number): RawEntry {
70 const existing = rawStreams.get(id)
71 if (existing) {
72 return existing
73 }
74 if (rawStreams.size >= MAX_FRAMED_STREAMS) {
75 throw new Error('Too many raw streams')
76 }
77​
78 let controller!: ReadableStreamDefaultController<Uint8Array>
79 const stream = new ReadableStream<Uint8Array>(
80 {
81 start(value) {
82 controller = value
83 },
84 cancel() {
85 if (entry[1] !== false) {
86 entry[1] = null
87 }
88 },
89 },
90 rawStreamQueue,
91 )
92 const entry: RawEntry = [stream, controller]
93 rawStreams.set(id, entry)
94 if (state !== 0) {
95 settleRaw(entry, state)
96 }
97 return entry
98 }
99​
100 function getStream(id: number): ReadableStream<Uint8Array> {
101 if (id === 0 || id >>> 0 !== id) {
102 throw new RangeError('Invalid raw stream ID')
103 }
104 return getRaw(id)[0]
105 }
106​
107 void (async () => {
108 let inputChunk = empty
109 let inputOffset = 0
110​
111 async function more(): Promise<boolean> {
112 while (inputOffset === inputChunk.byteLength) {
113 inputChunk = empty
114 inputOffset = 0
115 const next = await reader.read()
116 if (state !== 0 || next.done) {
117 return false
118 }
119 inputChunk = next.value
120 }
121 return true
122 }
123​
124 async function read(
125 length: number,
126 cleanEof?: boolean,
127 ): Promise<Uint8Array | undefined> {
128 if (length === 0) {
129 return empty
130 }
131 if (!(await more())) {
132 if (cleanEof) {
133 return
134 }
135 throw new Error('Incomplete frame')
136 }
137​
138 const available = inputChunk.byteLength - inputOffset
139 if (available >= length) {
140 const result = inputChunk.subarray(inputOffset, inputOffset + length)
141 inputOffset += length
142 if (inputOffset === inputChunk.byteLength) {
143 inputChunk = empty
144 inputOffset = 0
145 }
146 return result
147 }
148​
149 const result = new Uint8Array(length)
150 let offset = 0
151 while (offset < length) {
152 if (!(await more())) {
153 throw new Error('Incomplete frame')
154 }
155 const size = Math.min(
156 length - offset,
157 inputChunk.byteLength - inputOffset,
158 )
159 result.set(inputChunk.subarray(inputOffset, inputOffset + size), offset)
160 inputOffset += size
161 offset += size
162 }
163 if (inputOffset === inputChunk.byteLength) {
164 inputChunk = empty
165 inputOffset = 0
166 }
167 return result
168 }
169​
170 try {
171 while (state === 0) {
172 let header = await read(FRAME_HEADER_SIZE, true)
173 // Cancellation can run while the read is suspended.
174 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
175 if (state !== 0) {
176 return
177 }
178 if (!header) {
179 for (const entry of rawStreams.values()) {
180 if (entry[1]) {
181 throw new Error('Incomplete raw stream')
182 }
183 }
184 state = 1
185 jsonController.close()
186 return
187 }
188​
189 const type = header[0]!
190 const streamId =
191 ((header[1]! << 24) |
192 (header[2]! << 16) |
193 (header[3]! << 8) |
194 header[4]!) >>>
195 0
196 const length =
197 ((header[5]! << 24) |
198 (header[6]! << 16) |
199 (header[7]! << 8) |
200 header[8]!) >>>
201 0
202 header = empty
203​
204 if (
205 type > FRAME_TYPE_ERROR ||
206 (type === FRAME_TYPE_JSON) !== (streamId === 0) ||
207 length > MAX_FRAME_PAYLOAD_SIZE ||
208 (type === FRAME_TYPE_END && length !== 0)
209 ) {
210 throw new Error('Invalid frame')
211 }
212​
213 const entry = type === FRAME_TYPE_JSON ? undefined : getRaw(streamId)
214 if (entry?.[1] === false) {
215 throw new Error('Raw stream already ended')
216 }
217​
218 let payload = (await read(length))!
219 // Cancellation can run while the read is suspended.
220 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
221 if (state !== 0) {
222 return
223 }
224 if (!entry) {
225 const value = decoder.decode(payload)
226 payload = empty
227 jsonController.enqueue(value)
228 // Cancellation wakes this wait even when JSON demand stays at zero.
229 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
230 while (state === 0 && jsonController.desiredSize! <= 0) {
231 await new Promise<void>((resolve) => {
232 resume = resolve
233 })
234 }
235 continue
236 }
237​
238 if (type === FRAME_TYPE_CHUNK) {
239 const controller = entry[1]
240 if (controller) {
241 if (-controller.desiredSize! > MAX_UNREAD_RAW_STREAM_BYTES) {
242 controller.error(
243 new Error(`Raw stream ${streamId} has too many unread bytes`),
244 )
245 entry[1] = null
246 payload = empty
247 continue
248 }
249 // A small view would pin its whole network buffer; copy those.
250 const chunk =
251 payload.byteLength * 4 < payload.buffer.byteLength
252 ? payload.slice()
253 : payload
254 payload = empty
255 controller.enqueue(chunk)
256 }
257 } else {
258 settleRaw(
259 entry,
260 type === FRAME_TYPE_END ? 1 : [new Error(decoder.decode(payload))],
261 )
262 }
263 }
264 } catch (error) {
265 if (state === 0) {
266 const failed: Failed = [error]
267 state = failed
268 void reader.cancel(error).catch(() => {})
269 jsonController.error(error)
270 for (const entry of rawStreams.values()) {
271 settleRaw(entry, failed)
272 }
273 }
274 } finally {
275 inputChunk = empty
276 reader.releaseLock()
277 }
278 })()
279​
280 return [chunks, getStream] as const