@@ -20,8 +20,8 @@ import {
2020 SerovalMalformedBinaryError ,
2121 SerovalMissingPluginError ,
2222} from '../core/errors' ;
23- import { createSequence , type Sequence } from '../core/sequence' ;
24- import { createStream , type Stream } from '../core/stream' ;
23+ import { createSequence , sequenceToIterator , type Sequence } from '../core/sequence' ;
24+ import { createStream , streamToAsyncIterable , type Stream } from '../core/stream' ;
2525import {
2626 SYM_ASYNC_ITERATOR ,
2727 SYM_IS_CONCAT_SPREADABLE ,
@@ -30,9 +30,10 @@ import {
3030} from '../core/symbols' ;
3131import {
3232 decodeBigint ,
33- decodeInteger ,
33+ decodeInt ,
3434 decodeNumber ,
3535 decodeString ,
36+ decodeUint ,
3637} from './encoder' ;
3738import { SerovalEndianness , SerovalNodeType } from './nodes' ;
3839import type { Plugin } from './plugin' ;
@@ -134,9 +135,14 @@ async function deserializeByte(ctx: DeserializerContext): Promise<number> {
134135 return bytes [ 0 ] ;
135136}
136137
137- async function deserializeInteger ( ctx : DeserializerContext ) : Promise < number > {
138+ async function deserializeUint ( ctx : DeserializerContext ) : Promise < number > {
138139 const bytes = await ensureChunk ( ctx , 4 ) ;
139- return decodeInteger ( bytes , ctx . endianness === SerovalEndianness . LE ) ;
140+ return decodeUint ( bytes , ctx . endianness === SerovalEndianness . LE ) ;
141+ }
142+
143+ async function deserializeInt ( ctx : DeserializerContext ) : Promise < number > {
144+ const bytes = await ensureChunk ( ctx , 4 ) ;
145+ return decodeInt ( bytes , ctx . endianness === SerovalEndianness . LE ) ;
140146}
141147
142148async function deserializeNumberValue (
@@ -155,14 +161,14 @@ async function deserializeId(
155161 type : SerovalNodeType ,
156162) : Promise < number > {
157163 // parse ID
158- const id = await deserializeInteger ( ctx ) ;
164+ const id = await deserializeUint ( ctx ) ;
159165 // Mark id
160166 ctx . marker . set ( id , type ) ;
161167 return id ;
162168}
163169
164170async function deserializeRef ( ctx : DeserializerContext ) {
165- const ref = await deserializeInteger ( ctx ) ;
171+ const ref = await deserializeUint ( ctx ) ;
166172 if ( ctx . refs . has ( ref ) ) {
167173 return ctx . refs . get ( ref ) ! ;
168174 }
@@ -183,7 +189,7 @@ async function deserializeNumber(ctx: DeserializerContext) {
183189async function deserializeString ( ctx : DeserializerContext ) {
184190 const id = await deserializeId ( ctx , SerovalNodeType . String ) ;
185191 // First, ensure that there's an encoded length
186- const length = await deserializeInteger ( ctx ) ;
192+ const length = await deserializeUint ( ctx ) ;
187193 // Ensure the next chunk is based on encoded length
188194 const encodedData = await ensureChunk ( ctx , length ) ;
189195 upsert ( ctx , id , decodeString ( encodedData ) ) ;
@@ -264,7 +270,7 @@ async function deserializeObjectAssign(ctx: DeserializerContext) {
264270
265271async function deserializeArrayAssign ( ctx : DeserializerContext ) {
266272 const object = ( await deserializeRef ( ctx ) ) . value as unknown [ ] ;
267- const key = ( await deserializeInteger ( ctx ) ) as number ;
273+ const key = ( await deserializeUint ( ctx ) ) as number ;
268274 const value = ( await deserializeRef ( ctx ) ) . value ;
269275
270276 object [ key ] = value ;
@@ -289,7 +295,7 @@ async function deserializeObjectFlag(ctx: DeserializerContext) {
289295
290296async function deserializeArray ( ctx : DeserializerContext ) {
291297 const id = await deserializeId ( ctx , SerovalNodeType . Array ) ;
292- const length = await deserializeInteger ( ctx ) ;
298+ const length = await deserializeUint ( ctx ) ;
293299 upsert ( ctx , id , new Array ( length ) ) ;
294300}
295301
@@ -318,8 +324,8 @@ async function deserializeStreamReturn(ctx: DeserializerContext) {
318324
319325async function deserializeSequence ( ctx : DeserializerContext ) {
320326 const id = await deserializeId ( ctx , SerovalNodeType . Sequence ) ;
321- const throwAt = await deserializeNumberValue ( ctx ) ;
322- const doneAt = await deserializeNumberValue ( ctx ) ;
327+ const throwAt = await deserializeInt ( ctx ) ;
328+ const doneAt = await deserializeInt ( ctx ) ;
323329 upsert ( ctx , id , createSequence ( [ ] , throwAt , doneAt ) ) ;
324330}
325331
@@ -341,7 +347,7 @@ async function deserializeNullConstructor(ctx: DeserializerContext) {
341347
342348async function deserializeDate ( ctx : DeserializerContext ) {
343349 const id = await deserializeId ( ctx , SerovalNodeType . Date ) ;
344- const timestamp = await deserializeInteger ( ctx ) ;
350+ const timestamp = await deserializeUint ( ctx ) ;
345351 upsert ( ctx , id , new Date ( timestamp ) ) ;
346352}
347353
@@ -364,7 +370,7 @@ async function deserializeBoxed(ctx: DeserializerContext) {
364370
365371async function deserializeArrayBuffer ( ctx : DeserializerContext ) {
366372 const id = await deserializeId ( ctx , SerovalNodeType . ArrayBuffer ) ;
367- const length = await deserializeInteger ( ctx ) ;
373+ const length = await deserializeUint ( ctx ) ;
368374 const bytes = await ensureChunk ( ctx , length ) ;
369375 upsert ( ctx , id , bytes . buffer ) ;
370376}
@@ -375,8 +381,8 @@ async function deserializeTypedArray(ctx: DeserializerContext) {
375381 TYPED_ARRAY_CONSTRUCTOR ,
376382 ( await deserializeByte ( ctx ) ) as TypedArrayTag ,
377383 ) ;
378- const offset = await deserializeInteger ( ctx ) ;
379- const length = await deserializeInteger ( ctx ) ;
384+ const offset = await deserializeUint ( ctx ) ;
385+ const length = await deserializeUint ( ctx ) ;
380386 const buffer = ( await deserializeRef ( ctx ) ) . value as ArrayBuffer ;
381387 upsert ( ctx , id , new construct ( buffer , offset , length ) ) ;
382388}
@@ -387,16 +393,16 @@ async function deserializeBigIntTypedArray(ctx: DeserializerContext) {
387393 BIG_INT_TYPED_ARRAY_CONSTRUCTOR ,
388394 ( await deserializeByte ( ctx ) ) as BigIntTypedArrayTag ,
389395 ) ;
390- const offset = await deserializeInteger ( ctx ) ;
391- const length = await deserializeInteger ( ctx ) ;
396+ const offset = await deserializeUint ( ctx ) ;
397+ const length = await deserializeUint ( ctx ) ;
392398 const buffer = ( await deserializeRef ( ctx ) ) . value as ArrayBuffer ;
393399 upsert ( ctx , id , new construct ( buffer , offset , length ) ) ;
394400}
395401
396402async function deserializeDataView ( ctx : DeserializerContext ) {
397403 const id = await deserializeId ( ctx , SerovalNodeType . DataView ) ;
398- const offset = await deserializeInteger ( ctx ) ;
399- const length = await deserializeInteger ( ctx ) ;
404+ const offset = await deserializeUint ( ctx ) ;
405+ const length = await deserializeUint ( ctx ) ;
400406 const buffer = ( await deserializeRef ( ctx ) ) . value as ArrayBuffer ;
401407 upsert ( ctx , id , new DataView ( buffer , offset , length ) ) ;
402408}
@@ -433,7 +439,7 @@ async function deserializePromise(ctx: DeserializerContext) {
433439}
434440
435441async function deserializePromiseSuccess ( ctx : DeserializerContext ) {
436- const resolverIndex = await deserializeInteger ( ctx ) ;
442+ const resolverIndex = await deserializeUint ( ctx ) ;
437443 const currentResolver = ctx . resolvers . get ( resolverIndex ) ;
438444 if ( currentResolver == null ) {
439445 throw new SerovalMalformedBinaryError ( ) ;
@@ -443,7 +449,7 @@ async function deserializePromiseSuccess(ctx: DeserializerContext) {
443449}
444450
445451async function deserializePromiseFailure ( ctx : DeserializerContext ) {
446- const resolverIndex = await deserializeInteger ( ctx ) ;
452+ const resolverIndex = await deserializeUint ( ctx ) ;
447453 const currentResolver = ctx . resolvers . get ( resolverIndex ) ;
448454 if ( currentResolver == null ) {
449455 throw new SerovalMalformedBinaryError ( ) ;
@@ -493,6 +499,18 @@ async function deserializeRoot(ctx: DeserializerContext) {
493499 ctx . root . s ( reference ) ;
494500}
495501
502+ async function deserializeIterator ( ctx : DeserializerContext ) {
503+ const id = await deserializeId ( ctx , SerovalNodeType . Iterator ) ;
504+ const sequence = ( await deserializeRef ( ctx ) ) . value as Sequence ;
505+ upsert ( ctx , id , sequenceToIterator ( sequence ) ) ;
506+ }
507+
508+ async function deserializeAsyncIterator ( ctx : DeserializerContext ) {
509+ const id = await deserializeId ( ctx , SerovalNodeType . AsyncIterator ) ;
510+ const stream = ( await deserializeRef ( ctx ) ) . value as Stream < unknown > ;
511+ upsert ( ctx , id , streamToAsyncIterable ( stream ) ) ;
512+ }
513+
496514async function deserializeChunk ( ctx : DeserializerContext ) {
497515 // Read first byte
498516 const firstByte = ( await deserializeByte ( ctx ) ) as SerovalNodeType ;
@@ -606,6 +624,12 @@ async function deserializeChunk(ctx: DeserializerContext) {
606624 case SerovalNodeType . Root :
607625 await deserializeRoot ( ctx ) ;
608626 break ;
627+ case SerovalNodeType . Iterator :
628+ await deserializeIterator ( ctx ) ;
629+ break ;
630+ case SerovalNodeType . AsyncIterator :
631+ await deserializeAsyncIterator ( ctx ) ;
632+ break ;
609633 default :
610634 throw new SerovalMalformedBinaryError ( ) ;
611635 }
0 commit comments