@@ -107,21 +107,6 @@ Technically any value can be used.
107107 * @property {(context: ObjectDeserializerContext<EXPECTED_ANY>) => EXPECTED_ANY } deserialize
108108 */
109109
110- /**
111- * Updates set size using the provided set.
112- * @template T
113- * @param {Set<T> } set set
114- * @param {number } size count of items to keep
115- */
116- const setSetSize = ( set , size ) => {
117- let i = 0 ;
118- for ( const item of set ) {
119- if ( i ++ >= size ) {
120- set . delete ( item ) ;
121- }
122- }
123- } ;
124-
125110/**
126111 * Updates map size using the provided map.
127112 * @template K, X
@@ -149,6 +134,9 @@ const toHash = (buffer, hashFunction) => {
149134 return hash . digest ( "latin1" ) ;
150135} ;
151136
137+ /** Marks an object whose children are still being written; never a real position. */
138+ const IN_PROGRESS = - 1 ;
139+
152140const ESCAPE = null ;
153141const ESCAPE_ESCAPE_VALUE = null ;
154142const ESCAPE_END_OBJECT = true ;
@@ -463,16 +451,22 @@ class ObjectMiddleware extends SerializerMiddleware {
463451 let currentPosTypeLookup = 0 ;
464452 /** @type {Map<SerializerConfigWithSerializer, number> } */
465453 let objectTypeLookup = new Map ( ) ;
466- /** @type {Set<ComplexSerializableType> } */
467- const cycleStack = new Set ( ) ;
454+ /**
455+ * One-entry cache in front of `objectTypeLookup`, since runs of one class are common.
456+ * @type {SerializerConfigWithSerializer | undefined }
457+ */
458+ let lastConfig ;
459+ /** @type {number } */
460+ let lastConfigIndex = 0 ;
461+ /** @type {ComplexSerializableType[] } */
462+ const cycleStack = [ ] ;
468463 /**
469464 * Returns stack.
470465 * @param {ComplexSerializableType } item item to stack
471466 * @returns {string } stack
472467 */
473468 const stackToString = ( item ) => {
474- const arr = [ ...cycleStack ] ;
475- arr . push ( item ) ;
469+ const arr = [ ...cycleStack , item ] ;
476470 return arr
477471 . map ( ( item ) => {
478472 if ( typeof item === "string" ) {
@@ -528,63 +522,35 @@ class ObjectMiddleware extends SerializerMiddleware {
528522 } ;
529523 /** @type {undefined | WeakSet<Error> } */
530524 let hasDebugInfoAttached ;
531- /** @type {ObjectSerializerContext<EXPECTED_ANY> } */
532- let ctx = {
533- write ( value ) {
534- try {
535- process ( /** @type {ComplexSerializableType } */ ( value ) ) ;
536- } catch ( err ) {
537- if ( err !== NOT_SERIALIZABLE ) {
538- if ( hasDebugInfoAttached === undefined ) {
539- hasDebugInfoAttached = new WeakSet ( ) ;
540- }
541- if ( ! hasDebugInfoAttached . has ( /** @type {Error } */ ( err ) ) ) {
542- /** @type {Error } */
543- ( err ) . message +=
544- `\nwhile serializing ${ stackToString ( /** @type {ComplexSerializableType } */ ( value ) ) } ` ;
545- hasDebugInfoAttached . add ( /** @type {Error } */ ( err ) ) ;
546- }
547- }
548- throw err ;
549- }
550- return ctx ;
551- } ,
552- setCircularReference ( ref ) {
553- addReferenceable ( ref ) ;
554- } ,
555- snapshot ( ) {
556- return {
557- length : result . length ,
558- cycleStackSize : cycleStack . size ,
559- referenceableSize : referenceable . size ,
560- currentPos,
561- objectTypeLookupSize : objectTypeLookup . size ,
562- currentPosTypeLookup
563- } ;
564- } ,
565- rollback ( snapshot ) {
566- result . length = snapshot . length ;
567- setSetSize ( cycleStack , snapshot . cycleStackSize ) ;
568- setMapSize ( referenceable , snapshot . referenceableSize ) ;
569- currentPos = snapshot . currentPos ;
570- setMapSize ( objectTypeLookup , snapshot . objectTypeLookupSize ) ;
571- currentPosTypeLookup = snapshot . currentPosTypeLookup ;
572- } ,
573- ...context
525+ /**
526+ * Appends the path from the root to `item`, once per error.
527+ * @param {Error } err error thrown while writing `item`
528+ * @param {ComplexSerializableType } item item being written
529+ * @returns {Error } err
530+ */
531+ const attachSerializationPath = ( err , item ) => {
532+ if ( hasDebugInfoAttached === undefined ) {
533+ hasDebugInfoAttached = new WeakSet ( ) ;
534+ }
535+ if ( ! hasDebugInfoAttached . has ( err ) ) {
536+ err . message += `\nwhile serializing ${ stackToString ( item ) } ` ;
537+ hasDebugInfoAttached . add ( err ) ;
538+ }
539+ return err ;
574540 } ;
575- this . extendContext ( ctx ) ;
576541 // Dispatched on `typeof` first: two thirds of the items are numbers,
577542 // booleans and `undefined`, and only 3% are buffers.
578543 /**
579- * Processes the provided item .
544+ * Writes one item into the output; the context is returned so writes chain .
580545 * @param {ComplexSerializableType } item item to serialize
546+ * @returns {ObjectSerializerContext<EXPECTED_ANY> } context
581547 */
582548 const process = ( item ) => {
583549 switch ( typeof item ) {
584550 case "number" :
585551 case "boolean" :
586552 result . push ( item ) ;
587- return ;
553+ return ctx ;
588554 case "string" : {
589555 if ( item . length > 1 ) {
590556 // short strings are shorter when not emitting a reference (this saves 1 byte per empty string)
@@ -602,10 +568,10 @@ class ObjectMiddleware extends SerializerMiddleware {
602568 ) {
603569 // A back-reference is `null` + the offset.
604570 result . push ( ESCAPE , offset ) ;
605- return ;
571+ return ctx ;
606572 }
607573 }
608- addReferenceable ( item ) ;
574+ referenceable . set ( item , currentPos ++ ) ;
609575 }
610576
611577 if ( item . length > 102400 && context . logger ) {
@@ -617,87 +583,115 @@ class ObjectMiddleware extends SerializerMiddleware {
617583 }
618584
619585 result . push ( item ) ;
620- return ;
586+ return ctx ;
621587 }
622588 case "undefined" :
623589 result . push ( ESCAPE , ESCAPE_UNDEFINED ) ;
624- return ;
590+ return ctx ;
625591 case "object" : {
626592 // ESCAPE is null, so this covers null too
627593 if ( item === ESCAPE ) {
628594 result . push ( ESCAPE , ESCAPE_ESCAPE_VALUE ) ;
629- return ;
595+ return ctx ;
630596 }
631597
632- if ( Buffer . isBuffer ( item ) ) {
633- // check if we can emit a reference
634- const ref = referenceable . get ( item ) ;
635- if ( ref !== undefined ) {
636- result . push ( ESCAPE , ref - currentPos ) ;
637- return ;
598+ // buffers and objects share one reference table, so a single
599+ // lookup answers both before the costlier buffer test
600+ const ref = referenceable . get ( item ) ;
601+ if ( ref !== undefined ) {
602+ if ( ref === IN_PROGRESS ) {
603+ throw attachSerializationPath (
604+ new Error (
605+ "This is a circular references. To serialize circular references use 'setCircularReference' somewhere in the circle during serialize and deserialize."
606+ ) ,
607+ item
608+ ) ;
638609 }
610+ result . push ( ESCAPE , ref - currentPos ) ;
611+ return ctx ;
612+ }
613+
614+ if ( Buffer . isBuffer ( item ) ) {
639615 const alreadyUsedBuffer = dedupeBuffer ( item ) ;
640616 if ( alreadyUsedBuffer !== item ) {
641617 const ref = referenceable . get ( alreadyUsedBuffer ) ;
642618 if ( ref !== undefined ) {
643619 referenceable . set ( item , ref ) ;
644620 result . push ( ESCAPE , ref - currentPos ) ;
645- return ;
621+ return ctx ;
646622 }
647623 item = alreadyUsedBuffer ;
648624 }
649- addReferenceable ( item ) ;
625+ referenceable . set ( item , currentPos ++ ) ;
650626
651627 result . push ( /** @type {Buffer } */ ( item ) ) ;
652- return ;
653- }
654-
655- // check if we can emit a reference
656- const ref = referenceable . get ( item ) ;
657- if ( ref !== undefined ) {
658- result . push ( ESCAPE , ref - currentPos ) ;
659- return ;
660- }
661-
662- if ( cycleStack . has ( item ) ) {
663- throw new Error (
664- "This is a circular references. To serialize circular references use 'setCircularReference' somewhere in the circle during serialize and deserialize."
665- ) ;
628+ return ctx ;
666629 }
667630
668631 // Keyed by the registration itself: one per class, so it stands in for
669632 // `${request}/${name}` without building that string per object.
670- const config = ObjectMiddleware . getSerializerFor (
671- /** @type { Constructor } */
672- ( item )
633+ const proto = Object . getPrototypeOf ( item ) ;
634+ const registered = serializers . get (
635+ proto === null ? null : proto . constructor
673636 ) ;
637+ /** @type {SerializerConfigWithSerializer } */
638+ let config ;
639+ if ( registered !== undefined && registered . serializer !== undefined ) {
640+ config = /** @type {SerializerConfigWithSerializer } */ ( registered ) ;
641+ } else {
642+ try {
643+ // re-resolved so the miss reports which class is unregistered
644+ config = ObjectMiddleware . getSerializerFor (
645+ /** @type {Constructor } */
646+ ( item )
647+ ) ;
648+ } catch ( err ) {
649+ if ( err === NOT_SERIALIZABLE ) throw err ;
650+ throw attachSerializationPath ( /** @type {Error } */ ( err ) , item ) ;
651+ }
652+ }
674653 const { request, name, serializer } = config ;
675- const lastIndex = objectTypeLookup . get ( config ) ;
654+ const lastIndex =
655+ config === lastConfig
656+ ? lastConfigIndex
657+ : objectTypeLookup . get ( config ) ;
676658
677659 if ( lastIndex === undefined ) {
678- objectTypeLookup . set ( config , currentPosTypeLookup ++ ) ;
660+ lastConfigIndex = currentPosTypeLookup ++ ;
661+ objectTypeLookup . set ( config , lastConfigIndex ) ;
679662
680663 result . push ( ESCAPE , request , name ) ;
681664 } else {
665+ lastConfigIndex = lastIndex ;
682666 result . push ( ESCAPE , currentPosTypeLookup - lastIndex ) ;
683667 }
668+ lastConfig = config ;
684669
685- cycleStack . add ( item ) ;
670+ referenceable . set ( item , IN_PROGRESS ) ;
671+ cycleStack . push ( item ) ;
686672
687673 try {
688674 serializer . serialize ( item , ctx ) ;
689- } finally {
690- cycleStack . delete ( item ) ;
675+ } catch ( err ) {
676+ cycleStack . pop ( ) ;
677+ if ( err !== NOT_SERIALIZABLE ) {
678+ attachSerializationPath ( /** @type {Error } */ ( err ) , item ) ;
679+ }
680+ throw err ;
691681 }
682+ cycleStack . pop ( ) ;
692683
693684 result . push ( ESCAPE , ESCAPE_END_OBJECT ) ;
694685
695- addReferenceable ( item ) ;
696- return ;
686+ referenceable . set ( item , currentPos ++ ) ;
687+ return ctx ;
697688 }
698689 case "function" : {
699690 if ( ! SerializerMiddleware . isLazy ( item ) ) {
700- throw new Error ( `Unexpected function ${ item } ` ) ;
691+ throw attachSerializationPath (
692+ new Error ( `Unexpected function ${ item } ` ) ,
693+ item
694+ ) ;
701695 }
702696
703697 /** @type {SerializedType | undefined } */
@@ -708,10 +702,10 @@ class ObjectMiddleware extends SerializerMiddleware {
708702 if ( typeof serializedData === "function" ) {
709703 result . push ( serializedData ) ;
710704 } else {
711- throw new Error ( "Not implemented" ) ;
705+ throw attachSerializationPath ( new Error ( "Not implemented" ) , item ) ;
712706 }
713707 } else if ( SerializerMiddleware . isLazy ( item , this ) ) {
714- throw new Error ( "Not implemented" ) ;
708+ throw attachSerializationPath ( new Error ( "Not implemented" ) , item ) ;
715709 } else {
716710 const data =
717711 /** @type {() => PrimitiveSerializableType[] | Promise<PrimitiveSerializableType[]> } */
@@ -723,12 +717,50 @@ class ObjectMiddleware extends SerializerMiddleware {
723717 SerializerMiddleware . setLazySerializedValue ( item , data ) ;
724718 result . push ( data ) ;
725719 }
726- return ;
720+ return ctx ;
727721 }
728722 default :
729723 result . push ( item ) ;
730724 }
725+ return ctx ;
731726 } ;
727+ /** @type {ObjectSerializerContext<EXPECTED_ANY> } */
728+ let ctx = {
729+ write : process ,
730+ setCircularReference ( ref ) {
731+ addReferenceable ( ref ) ;
732+ } ,
733+ snapshot ( ) {
734+ return {
735+ length : result . length ,
736+ cycleStackSize : cycleStack . length ,
737+ referenceableSize : referenceable . size ,
738+ currentPos,
739+ objectTypeLookupSize : objectTypeLookup . size ,
740+ currentPosTypeLookup
741+ } ;
742+ } ,
743+ rollback ( snapshot ) {
744+ result . length = snapshot . length ;
745+ cycleStack . length = snapshot . cycleStackSize ;
746+ // a `setCircularReference` since the snapshot replaced a marker on an
747+ // item already keyed here, which `setMapSize` cannot undo
748+ for ( let i = 0 ; i < cycleStack . length ; i ++ ) {
749+ const item = /** @type {ReferenceableItem } */ ( cycleStack [ i ] ) ;
750+ const ref = referenceable . get ( item ) ;
751+ if ( ref !== undefined && ref >= snapshot . currentPos ) {
752+ referenceable . set ( item , IN_PROGRESS ) ;
753+ }
754+ }
755+ setMapSize ( referenceable , snapshot . referenceableSize ) ;
756+ currentPos = snapshot . currentPos ;
757+ setMapSize ( objectTypeLookup , snapshot . objectTypeLookupSize ) ;
758+ lastConfig = undefined ;
759+ currentPosTypeLookup = snapshot . currentPosTypeLookup ;
760+ } ,
761+ ...context
762+ } ;
763+ this . extendContext ( ctx ) ;
732764
733765 try {
734766 for ( const item of data ) {
0 commit comments