18
18
*/
19
19
import utf8 from './utf8' ;
20
20
import Integer , { int , isInt } from '../integer' ;
21
- import { newError } from './../error' ;
21
+ import { newError , PROTOCOL_ERROR } from './../error' ;
22
22
import { Chunker } from './chunking' ;
23
23
import { Node , Path , PathSegment , Relationship , UnboundRelationship } from '../graph-types' ;
24
24
@@ -50,9 +50,16 @@ const STRUCT_8 = 0xDC;
50
50
const STRUCT_16 = 0xDD ;
51
51
52
52
const NODE = 0x4E ;
53
+ const NODE_STRUCT_SIZE = 3 ;
54
+
53
55
const RELATIONSHIP = 0x52 ;
56
+ const RELATIONSHIP_STRUCT_SIZE = 5 ;
57
+
54
58
const UNBOUND_RELATIONSHIP = 0x72 ;
59
+ const UNBOUND_RELATIONSHIP_STRUCT_SIZE = 3 ;
60
+
55
61
const PATH = 0x50 ;
62
+ const PATH_STRUCT_SIZE = 3 ;
56
63
57
64
/**
58
65
* A Structure have a signature and fields.
@@ -505,30 +512,34 @@ class Unpacker {
505
512
}
506
513
}
507
514
508
- _unpackStructWithSize ( size , buffer ) {
515
+ _unpackStructWithSize ( structSize , buffer ) {
509
516
const signature = buffer . readUInt8 ( ) ;
510
517
if ( signature == NODE ) {
511
- return this . _unpackNode ( buffer ) ;
518
+ return this . _unpackNode ( structSize , buffer ) ;
512
519
} else if ( signature == RELATIONSHIP ) {
513
- return this . _unpackRelationship ( buffer ) ;
520
+ return this . _unpackRelationship ( structSize , buffer ) ;
514
521
} else if ( signature == UNBOUND_RELATIONSHIP ) {
515
- return this . _unpackUnboundRelationship ( buffer ) ;
522
+ return this . _unpackUnboundRelationship ( structSize , buffer ) ;
516
523
} else if ( signature == PATH ) {
517
- return this . _unpackPath ( buffer ) ;
524
+ return this . _unpackPath ( structSize , buffer ) ;
518
525
} else {
519
- return this . _unpackUnknownStruct ( signature , size , buffer ) ;
526
+ return this . _unpackUnknownStruct ( signature , structSize , buffer ) ;
520
527
}
521
528
}
522
529
523
- _unpackNode ( buffer ) {
530
+ _unpackNode ( structSize , buffer ) {
531
+ this . _verifyStructSize ( 'Node' , NODE_STRUCT_SIZE , structSize ) ;
532
+
524
533
return new Node (
525
534
this . unpack ( buffer ) , // Identity
526
535
this . unpack ( buffer ) , // Labels
527
536
this . unpack ( buffer ) // Properties
528
537
) ;
529
538
}
530
539
531
- _unpackRelationship ( buffer ) {
540
+ _unpackRelationship ( structSize , buffer ) {
541
+ this . _verifyStructSize ( 'Relationship' , RELATIONSHIP_STRUCT_SIZE , structSize ) ;
542
+
532
543
return new Relationship (
533
544
this . unpack ( buffer ) , // Identity
534
545
this . unpack ( buffer ) , // Start Node Identity
@@ -538,15 +549,19 @@ class Unpacker {
538
549
) ;
539
550
}
540
551
541
- _unpackUnboundRelationship ( buffer ) {
552
+ _unpackUnboundRelationship ( structSize , buffer ) {
553
+ this . _verifyStructSize ( 'UnboundRelationship' , UNBOUND_RELATIONSHIP_STRUCT_SIZE , structSize ) ;
554
+
542
555
return new UnboundRelationship (
543
556
this . unpack ( buffer ) , // Identity
544
557
this . unpack ( buffer ) , // Type
545
558
this . unpack ( buffer ) // Properties
546
559
) ;
547
560
}
548
561
549
- _unpackPath ( buffer ) {
562
+ _unpackPath ( structSize , buffer ) {
563
+ this . _verifyStructSize ( 'Path' , PATH_STRUCT_SIZE , structSize ) ;
564
+
550
565
const nodes = this . unpack ( buffer ) ;
551
566
const rels = this . unpack ( buffer ) ;
552
567
const sequence = this . unpack ( buffer ) ;
@@ -582,14 +597,19 @@ class Unpacker {
582
597
return new Path ( nodes [ 0 ] , nodes [ nodes . length - 1 ] , segments ) ;
583
598
}
584
599
585
- _unpackUnknownStruct ( signature , size , buffer ) {
600
+ _unpackUnknownStruct ( signature , structSize , buffer ) {
586
601
const result = new Structure ( signature , [ ] ) ;
587
- for ( let i = 0 ; i < size ; i ++ ) {
602
+ for ( let i = 0 ; i < structSize ; i ++ ) {
588
603
result . fields . push ( this . unpack ( buffer ) ) ;
589
604
}
590
605
return result ;
591
606
}
592
607
608
+ _verifyStructSize ( structName , expectedSize , actualSize ) {
609
+ if ( expectedSize !== actualSize ) {
610
+ throw newError ( `Wrong struct size for ${ structName } , expected ${ expectedSize } but was ${ actualSize } ` , PROTOCOL_ERROR ) ;
611
+ }
612
+ }
593
613
}
594
614
595
615
export {
0 commit comments