@@ -1195,15 +1195,36 @@ export function createChunk(data: string): Uint8Array;
11951195// streams frame their chunks identically — see frame-transport.js) so there
11961196// is exactly one framing implementation.
11971197export function createChunk ( data ) {
1198- const encoder = new TextEncoder ( ) ;
1199- const encodeData = encoder . encode ( data ) ;
1198+ const encodeData = CHUNK_ENCODER . encode ( data ) ;
12001199 const bytes = encodeData . length ;
1200+ // The header carries exactly 8 hex digits. A longer length would widen it
1201+ // past 12 bytes and every reader would misframe the rest of the stream.
1202+ if ( bytes > MAX_CHUNK_BYTES ) {
1203+ throw new RangeError ( "Server function chunk is too large to frame." ) ;
1204+ }
12011205 const chunk = new Uint8Array ( 12 + bytes ) ;
1202- chunk . set ( encoder . encode ( `;0x${ bytes . toString ( 16 ) . padStart ( 8 , "0" ) } ;` ) ) ; // 32-bit
1206+ chunk . set ( CHUNK_ENCODER . encode ( `;0x${ bytes . toString ( 16 ) . padStart ( 8 , "0" ) } ;` ) ) ; // 32-bit
12031207 chunk . set ( encodeData , 12 ) ;
12041208 return chunk ;
12051209}
12061210
1211+ const CHUNK_ENCODER = /* @__PURE__ */ new TextEncoder ( ) ;
1212+ // Fatal, so a payload that is not valid UTF-8 is refused instead of being
1213+ // decoded with U+FFFD substituted. `createChunk` always writes valid UTF-8,
1214+ // so only a corrupted or hand-built stream can trip it. Stateless use only:
1215+ // every call decodes one whole payload, so one instance serves every reader.
1216+ const CHUNK_DECODER = /* @__PURE__ */ new TextDecoder ( "utf-8" , { fatal : true } ) ;
1217+ const MAX_CHUNK_BYTES = 0xffffffff ;
1218+ // A store over this size is shrunk to its unread bytes after each frame (see
1219+ // the end of `ChunkReader.next`). A store at or under it is kept as is.
1220+ const RETAINED_STORE_BYTES = 64 * 1024 ;
1221+
1222+ const DONE = { done : true , value : undefined } ;
1223+
1224+ function malformedStream ( ) {
1225+ return new Error ( "Malformed server function stream." ) ;
1226+ }
1227+
12071228export class ChunkReader {
12081229 constructor ( stream ) {
12091230 this . reader = stream . getReader ( ) ;
@@ -1212,13 +1233,17 @@ export class ChunkReader {
12121233 this . store = new Uint8Array ( 0 ) ;
12131234 this . buffer = this . store ;
12141235 this . done = false ;
1236+ // Set by `cancel()`. A cancelled read ends cleanly even when it stops
1237+ // partway through a frame.
1238+ this . cancelled = false ;
12151239 }
12161240
1241+ /** Reads once into the buffer; answers whether the reader was cancelled. */
12171242 async readChunk ( ) {
12181243 const chunk = await this . reader . read ( ) ;
12191244 if ( chunk . done ) {
12201245 this . done = true ;
1221- return ;
1246+ return this . cancelled ;
12221247 }
12231248 // Amortized growth (#3154). Reallocating the whole buffer per network
12241249 // read made one frame O(reads²): a 1 MiB argument payload delivered at
@@ -1250,51 +1275,89 @@ export class ChunkReader {
12501275 this . store = grown ;
12511276 this . buffer = grown . subarray ( 0 , needed ) ;
12521277 }
1278+ return this . cancelled ;
12531279 }
12541280
12551281 async next ( ) {
1282+ // A cancelled read is over. Frames still buffered are not delivered:
1283+ // the caller asked to stop, and frames cancels a superseded response
1284+ // precisely so its later chunks are not applied. The check repeats after
1285+ // every read below, because `cancel()` can run while a read is pending,
1286+ // or after one has already resolved with data.
1287+ if ( this . cancelled ) return DONE ;
12561288 // A network read boundary can land anywhere — inside the 12-byte header
12571289 // just as easily as inside a payload — so buffer until the whole header
12581290 // is present before parsing it. Parsing a truncated header used to
12591291 // mis-read the length (or throw) whenever a proxy/TLS record split a
12601292 // frame, which no localhost test ever produces.
12611293 while ( this . buffer . length < 12 ) {
12621294 if ( this . done ) {
1263- if ( this . buffer . length === 0 ) return { done : true , value : undefined } ;
1264- throw new Error ( "Malformed server function stream." ) ;
1295+ if ( this . buffer . length === 0 ) return DONE ;
1296+ throw malformedStream ( ) ;
12651297 }
1266- await this . readChunk ( ) ;
1267- }
1268- // `;0x00000000;` — the hex length names how many payload bytes to wait for
1269- const decoder = new TextDecoder ( ) ;
1270- const bytes = Number . parseInt ( decoder . decode ( this . buffer . subarray ( 1 , 11 ) ) , 16 ) ;
1271- if ( Number . isNaN ( bytes ) ) {
1272- throw new Error ( "Malformed server function stream." ) ;
1298+ if ( await this . readChunk ( ) ) return DONE ;
12731299 }
1300+ // `;0x00000000;`, exactly: the delimiters, the `0x`, then 8 hex digits
1301+ // naming how many payload bytes to wait for. `parseInt` used to accept
1302+ // any 10 bytes it could read a number from, so a header with the wrong
1303+ // delimiters or trailing junk (`;0x5zzzzzzz;`) decoded as a real frame.
1304+ const header = String . fromCharCode ( ...this . buffer . subarray ( 0 , 12 ) ) ;
1305+ if ( ! / ^ ; 0 x [ \d A - F a - f ] { 8 } ; $ / . test ( header ) ) throw malformedStream ( ) ;
1306+ const bytes = parseInt ( header . slice ( 3 , 11 ) , 16 ) ;
12741307 while ( bytes > this . buffer . length - 12 ) {
12751308 if ( this . done ) {
1276- throw new Error ( "Malformed server function stream." ) ;
1309+ throw malformedStream ( ) ;
12771310 }
1278- await this . readChunk ( ) ;
1311+ if ( await this . readChunk ( ) ) return DONE ;
1312+ }
1313+ let partial ;
1314+ try {
1315+ partial = CHUNK_DECODER . decode ( this . buffer . subarray ( 12 , 12 + bytes ) ) ;
1316+ } catch {
1317+ throw malformedStream ( ) ;
12791318 }
1280- const partial = decoder . decode ( this . buffer . subarray ( 12 , 12 + bytes ) ) ;
12811319 this . buffer = this . buffer . subarray ( 12 + bytes ) ;
1320+ // Shrink an oversized store to the bytes still unread, after each frame.
1321+ // Without this, the doubled allocation from one large frame was kept
1322+ // until the stream ended. For a live source or a frames connection, that
1323+ // can be as long as the page is open.
1324+ //
1325+ // The rule looks only at what is left to read, so a connection that goes
1326+ // idle right after a large frame releases it at once. The cost is that a
1327+ // steady stream of frames over 64 KiB regrows its store for each one.
1328+ // That is a few doubling allocations per frame, small next to decoding
1329+ // the frame. Frames under 64 KiB never trigger it, so the #3154 steady
1330+ // state for small frames is unchanged. Unread bytes (the next frame's,
1331+ // arriving with this one) are copied over only up to 64 KiB, which is
1332+ // still far cheaper than keeping the large store.
1333+ if ( this . store . length > RETAINED_STORE_BYTES && this . buffer . length <= RETAINED_STORE_BYTES ) {
1334+ this . store = this . buffer = this . buffer . slice ( ) ;
1335+ }
12821336 return { done : false , value : partial } ;
12831337 }
12841338
12851339 async drain ( interpret ) {
1286- while ( true ) {
1287- const result = await this . next ( ) ;
1288- if ( result . done ) {
1289- break ;
1340+ try {
1341+ while ( true ) {
1342+ const result = await this . next ( ) ;
1343+ if ( result . done ) {
1344+ break ;
1345+ }
1346+ interpret ( result . value ) ;
12901347 }
1291- interpret ( result . value ) ;
1348+ } catch ( error ) {
1349+ // Nothing will read the rest of the body, so release it instead of
1350+ // leaving it locked and unread until it is collected.
1351+ this . cancel ( error ) . catch ( ( ) => { } ) ;
1352+ throw error ;
12921353 }
12931354 }
12941355
12951356 /** End the read: the body is cancelled through the lock this reader
1296- * holds, and a pending `next()` resolves done. */
1357+ * holds, and a pending `next()` resolves done, even partway through a
1358+ * frame. */
12971359 cancel ( reason ) {
1360+ this . cancelled = true ;
12981361 return this . reader . cancel ( reason ) ;
12991362 }
13001363}
@@ -1323,7 +1386,7 @@ export function createEventChunk(data, id) {
13231386 // end of its line, and the payload's lines were split above it.
13241387 let event = id !== undefined && ! id . includes ( "\0" ) ? `id: ${ id } \n` : "" ;
13251388 for ( const line of data . split ( / \r \n | \r | \n / ) ) event += `data: ${ line } \n` ;
1326- return new TextEncoder ( ) . encode ( event + "\n" ) ;
1389+ return CHUNK_ENCODER . encode ( event + "\n" ) ;
13271390}
13281391
13291392/**
@@ -1625,72 +1688,80 @@ export async function deserializeStream(source, codecOptions, wire) {
16251688 }
16261689 const reader =
16271690 wire && isEventStream ( source ) ? wire . open ( source . body ) : new ChunkReader ( source . body ) ;
1628- const result = await reader . next ( ) ;
1629- if ( ! result . done ) {
1630- // An error trailer as the FIRST frame is the whole answer: encoding
1631- // failed before any value was delivered, and the failure is the result
1632- // (#3117). Thrown here so the caller sees a failed call, never a void
1633- // success.
1634- if ( result . value . startsWith ( ERROR_TRAILER_PREFIX ) ) {
1635- throw errorFromTrailer ( result . value ) ;
1636- }
1637- // The codec's decode half loads here — when a Serialized body has
1638- // actually arrived — so a client whose responses all ride the JSON fast
1639- // path never pays for it (see the loading notes at the top). The
1640- // decode-only module: reading a payload never needs the encode half
1641- // (that loads separately, when rich arguments serialize).
1642- const { createJSONDeserializer } = await import ( "../../serialization/src/serializer-decode.js" ) ;
1643- // Cross-references between chunks resolve through state inside the
1644- // deserializer, so one instance handles the whole stream.
1645- const deserializeChunk = createJSONDeserializer ( codecOptions ) ;
1646-
1647- function interpretChunk ( chunk ) {
1648- // Mid-stream, the trailer means a LATER value's encoding failed: the
1649- // resolved head keeps its data, and the throw below rejects the drain,
1650- // whose abort sweep fails every still-pending async value with the
1651- // carried reason instead of a generic truncation error (#3117).
1652- if ( chunk . startsWith ( ERROR_TRAILER_PREFIX ) ) {
1653- throw errorFromTrailer ( chunk ) ;
1691+ // When the first frame fails, nothing will read the rest of the body, so
1692+ // it is cancelled rather than left locked and unread. Once the drain below
1693+ // is running, the same cancel also ends it.
1694+ try {
1695+ const result = await reader . next ( ) ;
1696+ if ( ! result . done ) {
1697+ // An error trailer as the FIRST frame is the whole answer: encoding
1698+ // failed before any value was delivered, and the failure is the result
1699+ // (#3117). Thrown here so the caller sees a failed call, never a void
1700+ // success.
1701+ if ( result . value . startsWith ( ERROR_TRAILER_PREFIX ) ) {
1702+ throw errorFromTrailer ( result . value ) ;
1703+ }
1704+ // The codec's decode half loads here — when a Serialized body has
1705+ // actually arrived — so a client whose responses all ride the JSON fast
1706+ // path never pays for it (see the loading notes at the top). The
1707+ // decode-only module: reading a payload never needs the encode half
1708+ // (that loads separately, when rich arguments serialize).
1709+ const { createJSONDeserializer } =
1710+ await import ( "../../serialization/src/serializer-decode.js" ) ;
1711+ // Cross-references between chunks resolve through state inside the
1712+ // deserializer, so one instance handles the whole stream.
1713+ const deserializeChunk = createJSONDeserializer ( codecOptions ) ;
1714+
1715+ function interpretChunk ( chunk ) {
1716+ // Mid-stream, the trailer means a LATER value's encoding failed: the
1717+ // resolved head keeps its data, and the throw below rejects the drain,
1718+ // whose abort sweep fails every still-pending async value with the
1719+ // carried reason instead of a generic truncation error (#3117).
1720+ if ( chunk . startsWith ( ERROR_TRAILER_PREFIX ) ) {
1721+ throw errorFromTrailer ( chunk ) ;
1722+ }
1723+ return deserializeChunk ( JSON . parse ( chunk ) ) ;
16541724 }
1655- return deserializeChunk ( JSON . parse ( chunk ) ) ;
1656- }
16571725
1658- // Failure wiring for the drain: a network drop or malformed frame must
1659- // fail every value still waiting on later chunks — otherwise their
1660- // promises hang forever and open streams never terminate (and the drain
1661- // rejection itself goes unhandled). Normal completion runs the same
1662- // sweep: on a well-formed stream every value has already settled and the
1663- // sweep no-ops, while a truncation that lands exactly on a frame
1664- // boundary — indistinguishable from completion — leaves stranded values
1665- // that can never settle once the body is done.
1666- //
1667- // A live loop (`wire`) is told instead of swept: the body's end is the
1668- // connection's lifetime signal — a death when deferreds are still open,
1669- // a completion when none are — and the loop decides what happens to the
1670- // open ones. A death it will reconnect from leaves them pending (the
1671- // re-yielded answer supersedes them; throwing into them would surface
1672- // the death the loop exists to erase). An iteration ending for good
1673- // settles them by how it ended: `sweep` fails them (ended by error),
1674- // `close` completes the streams and leaves promises pending (ended by
1675- // the consumer or by completion) — see the loop's emitClosed.
1676- const connection = wire && wire . connection ;
1677- let endConnection ;
1678- if ( connection ) connection . ended = new Promise ( resolve => ( endConnection = resolve ) ) ;
1679- const end = error => {
1680- const sweep = ( ) => deserializeChunk . abort ( error ) ;
1681- if ( connection ) {
1682- const close = ( ) => deserializeChunk . close ( ) ;
1683- endConnection ( { open : deserializeChunk . open ( ) , error, sweep, close } ) ;
1684- } else sweep ( ) ;
1685- } ;
1686- reader . drain ( interpretChunk ) . then (
1687- ( ) => end ( new Error ( "Server function stream ended unexpectedly." ) ) ,
1688- error => end ( error )
1689- ) ;
1726+ // Failure wiring for the drain: a network drop or malformed frame must
1727+ // fail every value still waiting on later chunks — otherwise their
1728+ // promises hang forever and open streams never terminate (and the drain
1729+ // rejection itself goes unhandled). Normal completion runs the same
1730+ // sweep: on a well-formed stream every value has already settled and the
1731+ // sweep no-ops, while a truncation that lands exactly on a frame
1732+ // boundary — indistinguishable from completion — leaves stranded values
1733+ // that can never settle once the body is done.
1734+ //
1735+ // A live loop (`wire`) is told instead of swept: the body's end is the
1736+ // connection's lifetime signal — a death when deferreds are still open,
1737+ // a completion when none are — and the loop decides what happens to the
1738+ // open ones. A death it will reconnect from leaves them pending (the
1739+ // re-yielded answer supersedes them; throwing into them would surface
1740+ // the death the loop exists to erase). An iteration ending for good
1741+ // settles them by how it ended: `sweep` fails them (ended by error),
1742+ // `close` completes the streams and leaves promises pending (ended by
1743+ // the consumer or by completion) — see the loop's emitClosed.
1744+ const connection = wire && wire . connection ;
1745+ let endConnection ;
1746+ if ( connection ) connection . ended = new Promise ( resolve => ( endConnection = resolve ) ) ;
1747+ const end = error => {
1748+ const sweep = ( ) => deserializeChunk . abort ( error ) ;
1749+ if ( connection ) {
1750+ const close = ( ) => deserializeChunk . close ( ) ;
1751+ endConnection ( { open : deserializeChunk . open ( ) , error, sweep, close } ) ;
1752+ } else sweep ( ) ;
1753+ } ;
1754+ reader . drain ( interpretChunk ) . then (
1755+ ( ) => end ( new Error ( "Server function stream ended unexpectedly." ) ) ,
1756+ error => end ( error )
1757+ ) ;
16901758
1691- return interpretChunk ( result . value ) ;
1759+ return interpretChunk ( result . value ) ;
1760+ }
1761+ } catch ( error ) {
1762+ reader . cancel ( error ) . catch ( ( ) => { } ) ;
1763+ throw error ;
16921764 }
1693- return undefined ;
16941765} /**
16951766 * `deserializeStream` for an already-buffered string.
16961767 *
0 commit comments