Skip to content

Commit d099d96

Browse files
committed
Fix hydration/de-hydration
1 parent 57cd952 commit d099d96

File tree

6 files changed

+182
-88
lines changed

6 files changed

+182
-88
lines changed

packages/bolt-connection/src/bolt/bolt-protocol-v1.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from './bolt-protocol-util'
2424
// eslint-disable-next-line no-unused-vars
2525
import { Chunker } from '../channel'
26-
import { v1 } from '../packstream'
26+
import { structure, v1 } from '../packstream'
2727
import RequestMessage from './request-message'
2828
import {
2929
LoginObserver,
@@ -386,11 +386,9 @@ export default class BoltProtocol {
386386
}
387387

388388
this._lastMessageSignature = message.signature
389+
const messageStruct = new structure.Structure(message.signature, message.fields)
389390

390-
this.packer().packStruct(
391-
message.signature,
392-
message.fields.map(field => this.packable(field))
393-
)
391+
this.packable(messageStruct)()
394392

395393
this._chunker.messageBoundary()
396394

packages/bolt-connection/src/bolt/bolt-protocol-v5x0.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default class BoltProtocol extends BoltProtocolV44 {
3434

3535
get transformer () {
3636
if (this._transformer === undefined) {
37-
this._transformer = new Transformer([...transformersFactories].map(create => create(this._config)))
37+
this._transformer = new Transformer(Object.values(transformersFactories).map(create => create(this._config)))
3838
}
3939
return this._transformer
4040
}

packages/bolt-connection/src/bolt/bolt-protocol-v5x0.transformer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const UNBOUND_RELATIONSHIP_STRUCT_SIZE = 4
2828
function createNodeTransformer (config) {
2929
const node4x4Transformer = v4x4.createNodeTransformer(config)
3030
return node4x4Transformer.extendsWith({
31-
toStructure: struct => {
32-
structure.verifyStructSize('Node', NODE_STRUCT_SIZE, struct)
31+
fromStructure: struct => {
32+
structure.verifyStructSize('Node', NODE_STRUCT_SIZE, struct.size)
3333

3434
const [identity, lables, properties, elementId] = struct.fields
3535

@@ -46,8 +46,8 @@ function createNodeTransformer (config) {
4646
function createRelationshipTransformer (config) {
4747
const relationship4x4Transformer = v4x4.createRelationshipTransformer(config)
4848
return relationship4x4Transformer.extendsWith({
49-
toStructure: struct => {
50-
structure.verifyStructSize('Relationship', RELATIONSHIP_STRUCT_SIZE, structure.size)
49+
fromStructure: struct => {
50+
structure.verifyStructSize('Relationship', RELATIONSHIP_STRUCT_SIZE, struct.size)
5151

5252
const [
5353
identity,
@@ -77,7 +77,7 @@ function createRelationshipTransformer (config) {
7777
function createUnboundRelationshipTransformer (config) {
7878
const unboundRelationshipTransformer = v4x4.createUnboundRelationshipTransformer(config)
7979
return unboundRelationshipTransformer.extendsWith({
80-
toStructure: struct => {
80+
fromStructure: struct => {
8181
this._verifyStructSize(
8282
'UnboundRelationship',
8383
UNBOUND_RELATIONSHIP_STRUCT_SIZE,

packages/bolt-connection/src/bolt/transformer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class Transformer {
3030

3131
fromStructure (struct) {
3232
if (struct instanceof structure.Structure && this._transformersPerSignature.has(struct.signature)) {
33-
const { fromStructure } = this._transformersPerSignature[struct.signature]
33+
const { fromStructure } = this._transformersPerSignature.get(struct.signature)
3434
return fromStructure(struct)
3535
}
3636
return struct

packages/bolt-connection/src/packstream/packstream-v1.js

Lines changed: 72 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ class Packer {
7575
* @param x the value to pack
7676
* @returns Function
7777
*/
78-
packable (x, dehydrate = functional.identity) {
78+
packable (x, dehydrateStruct = functional.identity) {
7979
try {
80-
x = dehydrate(x)
80+
x = dehydrateStruct(x)
8181
} catch (ex) {
8282
return () => ex
8383
}
@@ -102,15 +102,15 @@ class Packer {
102102
return () => {
103103
this.packListHeader(x.length)
104104
for (let i = 0; i < x.length; i++) {
105-
this.packable(x[i] === undefined ? null : x[i])()
105+
this.packable(x[i] === undefined ? null : x[i], dehydrateStruct)()
106106
}
107107
}
108108
} else if (isIterable(x)) {
109-
return this.packableIterable(x)
109+
return this.packableIterable(x, dehydrateStruct)
110110
} else if (x instanceof Structure) {
111111
const packableFields = []
112112
for (let i = 0; i < x.fields.length; i++) {
113-
packableFields[i] = this.packable(x.fields[i])
113+
packableFields[i] = this.packable(x.fields[i], dehydrateStruct)
114114
}
115115
return () => this.packStruct(x.signature, packableFields)
116116
} else if (typeof x === 'object') {
@@ -128,7 +128,7 @@ class Packer {
128128
const key = keys[i]
129129
if (x[key] !== undefined) {
130130
this.packString(key)
131-
this.packable(x[key])()
131+
this.packable(x[key], dehydrateStruct)()
132132
}
133133
}
134134
}
@@ -137,10 +137,10 @@ class Packer {
137137
}
138138
}
139139

140-
packableIterable (iterable) {
140+
packableIterable (iterable, dehydrateStruct) {
141141
try {
142142
const array = Array.from(iterable)
143-
return this.packable(array)
143+
return this.packable(array, dehydrateStruct)
144144
} catch (e) {
145145
// handle errors from iterable to array conversion
146146
throw newError(`Cannot pack given iterable, ${e.message}: ${iterable}`)
@@ -331,63 +331,58 @@ class Unpacker {
331331
this._useBigInt = useBigInt
332332
}
333333

334-
unpack (buffer, hydrate = functional.identity) {
335-
const deserialize = () => {
336-
const marker = buffer.readUInt8()
337-
const markerHigh = marker & 0xf0
338-
const markerLow = marker & 0x0f
334+
unpack (buffer, hydrateStructure = functional.identity) {
335+
const marker = buffer.readUInt8()
336+
const markerHigh = marker & 0xf0
337+
const markerLow = marker & 0x0f
339338

340-
if (marker === NULL) {
341-
return null
342-
}
339+
if (marker === NULL) {
340+
return null
341+
}
343342

344-
const boolean = this._unpackBoolean(marker)
345-
if (boolean !== null) {
346-
return boolean
347-
}
343+
const boolean = this._unpackBoolean(marker)
344+
if (boolean !== null) {
345+
return boolean
346+
}
348347

349-
const numberOrInteger = this._unpackNumberOrInteger(marker, buffer)
350-
if (numberOrInteger !== null) {
351-
if (isInt(numberOrInteger)) {
352-
if (this._useBigInt) {
353-
return numberOrInteger.toBigInt()
354-
} else if (this._disableLosslessIntegers) {
355-
return numberOrInteger.toNumberOrInfinity()
356-
}
348+
const numberOrInteger = this._unpackNumberOrInteger(marker, buffer)
349+
if (numberOrInteger !== null) {
350+
if (isInt(numberOrInteger)) {
351+
if (this._useBigInt) {
352+
return numberOrInteger.toBigInt()
353+
} else if (this._disableLosslessIntegers) {
354+
return numberOrInteger.toNumberOrInfinity()
357355
}
358-
return numberOrInteger
359-
}
360-
361-
const string = this._unpackString(marker, markerHigh, markerLow, buffer)
362-
if (string !== null) {
363-
return string
364356
}
357+
return numberOrInteger
358+
}
365359

366-
const list = this._unpackList(marker, markerHigh, markerLow, buffer)
367-
if (list !== null) {
368-
return list
369-
}
360+
const string = this._unpackString(marker, markerHigh, markerLow, buffer)
361+
if (string !== null) {
362+
return string
363+
}
370364

371-
const byteArray = this._unpackByteArray(marker, buffer)
372-
if (byteArray !== null) {
373-
return byteArray
374-
}
365+
const list = this._unpackList(marker, markerHigh, markerLow, buffer, hydrateStructure)
366+
if (list !== null) {
367+
return list
368+
}
375369

376-
const map = this._unpackMap(marker, markerHigh, markerLow, buffer)
377-
if (map !== null) {
378-
return map
379-
}
370+
const byteArray = this._unpackByteArray(marker, buffer)
371+
if (byteArray !== null) {
372+
return byteArray
373+
}
380374

381-
const struct = this._unpackStruct(marker, markerHigh, markerLow, buffer)
382-
if (struct !== null) {
383-
return struct
384-
}
375+
const map = this._unpackMap(marker, markerHigh, markerLow, buffer, hydrateStructure)
376+
if (map !== null) {
377+
return map
378+
}
385379

386-
throw newError('Unknown packed value with marker ' + marker.toString(16))
380+
const struct = this._unpackStruct(marker, markerHigh, markerLow, buffer, hydrateStructure)
381+
if (struct !== null) {
382+
return struct
387383
}
388384

389-
const deserialized = deserialize()
390-
return hydrate(deserialized)
385+
throw newError('Unknown packed value with marker ' + marker.toString(16))
391386
}
392387

393388
unpackInteger (buffer) {
@@ -454,24 +449,24 @@ class Unpacker {
454449
}
455450
}
456451

457-
_unpackList (marker, markerHigh, markerLow, buffer) {
452+
_unpackList (marker, markerHigh, markerLow, buffer, hydrateStructure) {
458453
if (markerHigh === TINY_LIST) {
459-
return this._unpackListWithSize(markerLow, buffer)
454+
return this._unpackListWithSize(markerLow, buffer, hydrateStructure)
460455
} else if (marker === LIST_8) {
461-
return this._unpackListWithSize(buffer.readUInt8(), buffer)
456+
return this._unpackListWithSize(buffer.readUInt8(), buffer, hydrateStructure)
462457
} else if (marker === LIST_16) {
463-
return this._unpackListWithSize(buffer.readUInt16(), buffer)
458+
return this._unpackListWithSize(buffer.readUInt16(), buffer, hydrateStructure)
464459
} else if (marker === LIST_32) {
465-
return this._unpackListWithSize(buffer.readUInt32(), buffer)
460+
return this._unpackListWithSize(buffer.readUInt32(), buffer, hydrateStructure)
466461
} else {
467462
return null
468463
}
469464
}
470465

471-
_unpackListWithSize (size, buffer) {
466+
_unpackListWithSize (size, buffer, hydrateStructure) {
472467
const value = []
473468
for (let i = 0; i < size; i++) {
474-
value.push(this.unpack(buffer))
469+
value.push(this.unpack(buffer, hydrateStructure))
475470
}
476471
return value
477472
}
@@ -496,48 +491,49 @@ class Unpacker {
496491
return value
497492
}
498493

499-
_unpackMap (marker, markerHigh, markerLow, buffer) {
494+
_unpackMap (marker, markerHigh, markerLow, buffer, hydrateStructure) {
500495
if (markerHigh === TINY_MAP) {
501-
return this._unpackMapWithSize(markerLow, buffer)
496+
return this._unpackMapWithSize(markerLow, buffer, hydrateStructure)
502497
} else if (marker === MAP_8) {
503-
return this._unpackMapWithSize(buffer.readUInt8(), buffer)
498+
return this._unpackMapWithSize(buffer.readUInt8(), buffer, hydrateStructure)
504499
} else if (marker === MAP_16) {
505-
return this._unpackMapWithSize(buffer.readUInt16(), buffer)
500+
return this._unpackMapWithSize(buffer.readUInt16(), buffer, hydrateStructure)
506501
} else if (marker === MAP_32) {
507-
return this._unpackMapWithSize(buffer.readUInt32(), buffer)
502+
return this._unpackMapWithSize(buffer.readUInt32(), buffer, hydrateStructure)
508503
} else {
509504
return null
510505
}
511506
}
512507

513-
_unpackMapWithSize (size, buffer) {
508+
_unpackMapWithSize (size, buffer, hydrateStructure) {
514509
const value = {}
515510
for (let i = 0; i < size; i++) {
516-
const key = this.unpack(buffer)
517-
value[key] = this.unpack(buffer)
511+
const key = this.unpack(buffer, hydrateStructure)
512+
value[key] = this.unpack(buffer, hydrateStructure)
518513
}
519514
return value
520515
}
521516

522-
_unpackStruct (marker, markerHigh, markerLow, buffer) {
517+
_unpackStruct (marker, markerHigh, markerLow, buffer, hydrateStructure) {
523518
if (markerHigh === TINY_STRUCT) {
524-
return this._unpackStructWithSize(markerLow, buffer)
519+
return this._unpackStructWithSize(markerLow, buffer, hydrateStructure)
525520
} else if (marker === STRUCT_8) {
526-
return this._unpackStructWithSize(buffer.readUInt8(), buffer)
521+
return this._unpackStructWithSize(buffer.readUInt8(), buffer, hydrateStructure)
527522
} else if (marker === STRUCT_16) {
528-
return this._unpackStructWithSize(buffer.readUInt16(), buffer)
523+
return this._unpackStructWithSize(buffer.readUInt16(), buffer, hydrateStructure)
529524
} else {
530525
return null
531526
}
532527
}
533528

534-
_unpackStructWithSize (structSize, buffer) {
529+
_unpackStructWithSize (structSize, buffer, hydrateStructure) {
535530
const signature = buffer.readUInt8()
536531
const structure = new Structure(signature, [])
537532
for (let i = 0; i < structSize; i++) {
538-
structure.fields.push(this.unpack(buffer))
533+
structure.fields.push(this.unpack(buffer, hydrateStructure))
539534
}
540-
return structure
535+
536+
return hydrateStructure(structure)
541537
}
542538
}
543539

@@ -548,4 +544,4 @@ function isIterable (obj) {
548544
return typeof obj[Symbol.iterator] === 'function'
549545
}
550546

551-
export { Packer, Unpacker, Structure }
547+
export { Packer, Unpacker }

0 commit comments

Comments
 (0)