@@ -2,6 +2,7 @@ import {Writer} from '../../../util/buffers/Writer';
22import { CborEncoderDag } from '../CborEncoderDag' ;
33import { CborDecoder } from '../CborDecoder' ;
44import { JsonPackExtension } from '../../JsonPackExtension' ;
5+ import { CborDecoderDag } from '../CborDecoderDag' ;
56
67const writer = new Writer ( 1 ) ;
78const encoder = new CborEncoderDag ( writer ) ;
@@ -50,37 +51,50 @@ describe('only extension = 42 is permitted', () => {
5051 expect ( val ) . toStrictEqual ( { a : 'a' , b : 'b' } ) ;
5152 } ) ;
5253
53- test ( 'can encode CID using inlined custom class' , ( ) => {
54- class CID {
55- constructor ( public readonly value : string ) { }
54+ class CID {
55+ constructor ( public readonly value : string ) { }
56+ }
57+ class NotCID {
58+ constructor ( public readonly value : string ) { }
59+ }
60+
61+ class IpfsCborEncoder extends CborEncoderDag {
62+ public writeUnknown ( val : unknown ) : void {
63+ if ( val instanceof CID ) this . writeTag ( 42 , val . value ) ;
64+ else throw new Error ( 'Unknown value type' ) ;
5665 }
57- const encoder = new ( class extends CborEncoderDag {
58- public writeUnknown ( val : unknown ) : void {
59- if ( val instanceof CID ) encoder . writeTag ( 42 , val . value ) ;
60- else throw new Error ( 'Unknown value type' ) ;
61- }
62- } ) ( ) ;
66+ }
67+
68+ class IpfsCborDecoder extends CborDecoderDag {
69+ public readTagRaw ( tag : number ) : CID | unknown {
70+ if ( tag === 42 ) return new CID ( this . val ( ) as any ) ;
71+ throw new Error ( 'UNKNOWN_TAG' ) ;
72+ }
73+ }
74+
75+ test ( 'can encode CID using inlined custom class' , ( ) => {
76+ const encoder = new IpfsCborEncoder ( ) ;
6377 const encoded = encoder . encode ( { a : 'a' , b : new JsonPackExtension ( 42 , 'b' ) } ) ;
6478 const val = decoder . read ( encoded ) ;
6579 expect ( val ) . toStrictEqual ( { a : 'a' , b : new JsonPackExtension ( 42 , 'b' ) } ) ;
6680 const encoded2 = encoder . encode ( { a : 'a' , b : new CID ( 'b' ) } ) ;
67- const val2 = decoder . read ( encoded2 ) ;
81+ const val2 = decoder . decode ( encoded2 ) ;
6882 expect ( val ) . toStrictEqual ( { a : 'a' , b : new JsonPackExtension ( 42 , 'b' ) } ) ;
83+ expect ( val2 ) . toStrictEqual ( { a : 'a' , b : new JsonPackExtension ( 42 , 'b' ) } ) ;
84+ } ) ;
85+
86+ test ( 'can encode CID inside a nested array' , ( ) => {
87+ const encoder = new IpfsCborEncoder ( ) ;
88+ const decoder = new IpfsCborDecoder ( ) ;
89+ const cid = new CID ( 'my-cid' ) ;
90+ const data = [ 1 , [ 2 , [ 3 , cid , 4 ] , 5 ] , 6 ] ;
91+ const encoded = encoder . encode ( data ) ;
92+ const decoded = decoder . decode ( encoded ) ;
93+ expect ( decoded ) . toStrictEqual ( data ) ;
6994 } ) ;
7095
7196 test ( 'can throw on unknown custom class' , ( ) => {
72- class CID {
73- constructor ( public readonly value : string ) { }
74- }
75- class NotCID {
76- constructor ( public readonly value : string ) { }
77- }
78- const encoder = new ( class extends CborEncoderDag {
79- public writeUnknown ( val : unknown ) : void {
80- if ( val instanceof CID ) encoder . writeTag ( 42 , val . value ) ;
81- else throw new Error ( 'Unknown value type' ) ;
82- }
83- } ) ( ) ;
97+ const encoder = new IpfsCborEncoder ( ) ;
8498 const encoded1 = encoder . encode ( { a : 'a' , b : new CID ( 'b' ) } ) ;
8599 expect ( ( ) => encoder . encode ( { a : 'a' , b : new NotCID ( 'b' ) } ) ) . toThrowError ( new Error ( 'Unknown value type' ) ) ;
86100 } ) ;
0 commit comments