-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathpackstream-v2.js
636 lines (577 loc) · 17.2 KB
/
packstream-v2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/**
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as v1 from './packstream-v1'
import {
int,
isInt,
isPoint,
Point,
DateTime,
Duration,
isDate,
isDateTime,
isDuration,
isLocalDateTime,
isLocalTime,
isTime,
Time,
internal
} from 'neo4j-driver-core'
import {
epochDayToDate,
epochSecondAndNanoToLocalDateTime,
nanoOfDayToLocalTime
} from './temporal-factory'
const {
temporalUtil: {
dateToEpochDay,
localDateTimeToEpochSecond,
localTimeToNanoOfDay
}
} = internal
const POINT_2D = 0x58
const POINT_2D_STRUCT_SIZE = 3
const POINT_3D = 0x59
const POINT_3D_STRUCT_SIZE = 4
const DURATION = 0x45
const DURATION_STRUCT_SIZE = 4
const LOCAL_TIME = 0x74
const LOCAL_TIME_STRUCT_SIZE = 1
const TIME = 0x54
const TIME_STRUCT_SIZE = 2
const DATE = 0x44
const DATE_STRUCT_SIZE = 1
const LOCAL_DATE_TIME = 0x64
const LOCAL_DATE_TIME_STRUCT_SIZE = 2
const DATE_TIME_WITH_ZONE_OFFSET = 0x46
const DATE_TIME_WITH_ZONE_OFFSET_STRUCT_SIZE = 3
const DATE_TIME_WITH_ZONE_ID = 0x66
const DATE_TIME_WITH_ZONE_ID_STRUCT_SIZE = 3
export class Packer extends v1.Packer {
disableByteArrays () {
throw new Error('Bolt V2 should always support byte arrays')
}
packable (obj) {
if (isPoint(obj)) {
return () => packPoint(obj, this)
} else if (isDuration(obj)) {
return () => packDuration(obj, this)
} else if (isLocalTime(obj)) {
return () => packLocalTime(obj, this)
} else if (isTime(obj)) {
return () => packTime(obj, this)
} else if (isDate(obj)) {
return () => packDate(obj, this)
} else if (isLocalDateTime(obj)) {
return () => packLocalDateTime(obj, this)
} else if (isDateTime(obj)) {
return () => packDateTime(obj, this)
} else {
return super.packable(obj)
}
}
}
export class Unpacker extends v1.Unpacker {
/**
* @constructor
* @param {boolean} disableLosslessIntegers if this unpacker should convert all received integers to native JS numbers.
* @param {boolean} useBigInt if this unpacker should convert all received integers to Bigint
*/
constructor (disableLosslessIntegers = false, useBigInt = false) {
super(disableLosslessIntegers, useBigInt)
}
_unpackUnknownStruct (signature, structSize, buffer) {
if (signature === POINT_2D) {
return unpackPoint2D(this, structSize, buffer)
} else if (signature === POINT_3D) {
return unpackPoint3D(this, structSize, buffer)
} else if (signature === DURATION) {
return unpackDuration(this, structSize, buffer)
} else if (signature === LOCAL_TIME) {
return unpackLocalTime(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else if (signature === TIME) {
return unpackTime(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else if (signature === DATE) {
return unpackDate(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else if (signature === LOCAL_DATE_TIME) {
return unpackLocalDateTime(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else if (signature === DATE_TIME_WITH_ZONE_OFFSET) {
return unpackDateTimeWithZoneOffset(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else if (signature === DATE_TIME_WITH_ZONE_ID) {
return unpackDateTimeWithZoneId(
this,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
} else {
return super._unpackUnknownStruct(
signature,
structSize,
buffer,
this._disableLosslessIntegers,
this._useBigInt
)
}
}
}
/**
* Pack given 2D or 3D point.
* @param {Point} point the point value to pack.
* @param {Packer} packer the packer to use.
*/
function packPoint (point, packer) {
const is2DPoint = point.z === null || point.z === undefined
if (is2DPoint) {
packPoint2D(point, packer)
} else {
packPoint3D(point, packer)
}
}
/**
* Pack given 2D point.
* @param {Point} point the point value to pack.
* @param {Packer} packer the packer to use.
*/
function packPoint2D (point, packer) {
const packableStructFields = [
packer.packable(int(point.srid)),
packer.packable(point.x),
packer.packable(point.y)
]
packer.packStruct(POINT_2D, packableStructFields)
}
/**
* Pack given 3D point.
* @param {Point} point the point value to pack.
* @param {Packer} packer the packer to use.
*/
function packPoint3D (point, packer) {
const packableStructFields = [
packer.packable(int(point.srid)),
packer.packable(point.x),
packer.packable(point.y),
packer.packable(point.z)
]
packer.packStruct(POINT_3D, packableStructFields)
}
/**
* Unpack 2D point value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @return {Point} the unpacked 2D point value.
*/
function unpackPoint2D (unpacker, structSize, buffer) {
unpacker._verifyStructSize('Point2D', POINT_2D_STRUCT_SIZE, structSize)
return new Point(
unpacker.unpack(buffer), // srid
unpacker.unpack(buffer), // x
unpacker.unpack(buffer), // y
undefined // z
)
}
/**
* Unpack 3D point value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @return {Point} the unpacked 3D point value.
*/
function unpackPoint3D (unpacker, structSize, buffer) {
unpacker._verifyStructSize('Point3D', POINT_3D_STRUCT_SIZE, structSize)
return new Point(
unpacker.unpack(buffer), // srid
unpacker.unpack(buffer), // x
unpacker.unpack(buffer), // y
unpacker.unpack(buffer) // z
)
}
/**
* Pack given duration.
* @param {Duration} value the duration value to pack.
* @param {Packer} packer the packer to use.
*/
function packDuration (value, packer) {
const months = int(value.months)
const days = int(value.days)
const seconds = int(value.seconds)
const nanoseconds = int(value.nanoseconds)
const packableStructFields = [
packer.packable(months),
packer.packable(days),
packer.packable(seconds),
packer.packable(nanoseconds)
]
packer.packStruct(DURATION, packableStructFields)
}
/**
* Unpack duration value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @return {Duration} the unpacked duration value.
*/
function unpackDuration (unpacker, structSize, buffer) {
unpacker._verifyStructSize('Duration', DURATION_STRUCT_SIZE, structSize)
const months = unpacker.unpack(buffer)
const days = unpacker.unpack(buffer)
const seconds = unpacker.unpack(buffer)
const nanoseconds = unpacker.unpack(buffer)
return new Duration(months, days, seconds, nanoseconds)
}
/**
* Pack given local time.
* @param {LocalTime} value the local time value to pack.
* @param {Packer} packer the packer to use.
*/
function packLocalTime (value, packer) {
const nanoOfDay = localTimeToNanoOfDay(
value.hour,
value.minute,
value.second,
value.nanosecond
)
const packableStructFields = [packer.packable(nanoOfDay)]
packer.packStruct(LOCAL_TIME, packableStructFields)
}
/**
* Unpack local time value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result local time should be native JS numbers.
* @return {LocalTime} the unpacked local time value.
*/
function unpackLocalTime (
unpacker,
structSize,
buffer,
disableLosslessIntegers
) {
unpacker._verifyStructSize('LocalTime', LOCAL_TIME_STRUCT_SIZE, structSize)
const nanoOfDay = unpacker.unpackInteger(buffer)
const result = nanoOfDayToLocalTime(nanoOfDay)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers)
}
/**
* Pack given time.
* @param {Time} value the time value to pack.
* @param {Packer} packer the packer to use.
*/
function packTime (value, packer) {
const nanoOfDay = localTimeToNanoOfDay(
value.hour,
value.minute,
value.second,
value.nanosecond
)
const offsetSeconds = int(value.timeZoneOffsetSeconds)
const packableStructFields = [
packer.packable(nanoOfDay),
packer.packable(offsetSeconds)
]
packer.packStruct(TIME, packableStructFields)
}
/**
* Unpack time value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result time should be native JS numbers.
* @return {Time} the unpacked time value.
*/
function unpackTime (
unpacker,
structSize,
buffer,
disableLosslessIntegers,
useBigInt
) {
unpacker._verifyStructSize('Time', TIME_STRUCT_SIZE, structSize)
const nanoOfDay = unpacker.unpackInteger(buffer)
const offsetSeconds = unpacker.unpackInteger(buffer)
const localTime = nanoOfDayToLocalTime(nanoOfDay)
const result = new Time(
localTime.hour,
localTime.minute,
localTime.second,
localTime.nanosecond,
offsetSeconds
)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers, useBigInt)
}
/**
* Pack given neo4j date.
* @param {Date} value the date value to pack.
* @param {Packer} packer the packer to use.
*/
function packDate (value, packer) {
const epochDay = dateToEpochDay(value.year, value.month, value.day)
const packableStructFields = [packer.packable(epochDay)]
packer.packStruct(DATE, packableStructFields)
}
/**
* Unpack neo4j date value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result date should be native JS numbers.
* @return {Date} the unpacked neo4j date value.
*/
function unpackDate (
unpacker,
structSize,
buffer,
disableLosslessIntegers,
useBigInt
) {
unpacker._verifyStructSize('Date', DATE_STRUCT_SIZE, structSize)
const epochDay = unpacker.unpackInteger(buffer)
const result = epochDayToDate(epochDay)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers, useBigInt)
}
/**
* Pack given local date time.
* @param {LocalDateTime} value the local date time value to pack.
* @param {Packer} packer the packer to use.
*/
function packLocalDateTime (value, packer) {
const epochSecond = localDateTimeToEpochSecond(
value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.nanosecond
)
const nano = int(value.nanosecond)
const packableStructFields = [
packer.packable(epochSecond),
packer.packable(nano)
]
packer.packStruct(LOCAL_DATE_TIME, packableStructFields)
}
/**
* Unpack local date time value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result local date-time should be native JS numbers.
* @return {LocalDateTime} the unpacked local date time value.
*/
function unpackLocalDateTime (
unpacker,
structSize,
buffer,
disableLosslessIntegers,
useBigInt
) {
unpacker._verifyStructSize(
'LocalDateTime',
LOCAL_DATE_TIME_STRUCT_SIZE,
structSize
)
const epochSecond = unpacker.unpackInteger(buffer)
const nano = unpacker.unpackInteger(buffer)
const result = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers, useBigInt)
}
/**
* Pack given date time.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
*/
function packDateTime (value, packer) {
if (value.timeZoneId) {
packDateTimeWithZoneId(value, packer)
} else {
packDateTimeWithZoneOffset(value, packer)
}
}
/**
* Pack given date time with zone offset.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
*/
function packDateTimeWithZoneOffset (value, packer) {
const epochSecond = localDateTimeToEpochSecond(
value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.nanosecond
)
const nano = int(value.nanosecond)
const timeZoneOffsetSeconds = int(value.timeZoneOffsetSeconds)
const packableStructFields = [
packer.packable(epochSecond),
packer.packable(nano),
packer.packable(timeZoneOffsetSeconds)
]
packer.packStruct(DATE_TIME_WITH_ZONE_OFFSET, packableStructFields)
}
/**
* Unpack date time with zone offset value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
* @return {DateTime} the unpacked date time with zone offset value.
*/
function unpackDateTimeWithZoneOffset (
unpacker,
structSize,
buffer,
disableLosslessIntegers,
useBigInt
) {
unpacker._verifyStructSize(
'DateTimeWithZoneOffset',
DATE_TIME_WITH_ZONE_OFFSET_STRUCT_SIZE,
structSize
)
const epochSecond = unpacker.unpackInteger(buffer)
const nano = unpacker.unpackInteger(buffer)
const timeZoneOffsetSeconds = unpacker.unpackInteger(buffer)
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
const result = new DateTime(
localDateTime.year,
localDateTime.month,
localDateTime.day,
localDateTime.hour,
localDateTime.minute,
localDateTime.second,
localDateTime.nanosecond,
timeZoneOffsetSeconds,
null
)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers, useBigInt)
}
/**
* Pack given date time with zone id.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
*/
function packDateTimeWithZoneId (value, packer) {
const epochSecond = localDateTimeToEpochSecond(
value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.nanosecond
)
const nano = int(value.nanosecond)
const timeZoneId = value.timeZoneId
const packableStructFields = [
packer.packable(epochSecond),
packer.packable(nano),
packer.packable(timeZoneId)
]
packer.packStruct(DATE_TIME_WITH_ZONE_ID, packableStructFields)
}
/**
* Unpack date time with zone id value using the given unpacker.
* @param {Unpacker} unpacker the unpacker to use.
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
* @return {DateTime} the unpacked date time with zone id value.
*/
function unpackDateTimeWithZoneId (
unpacker,
structSize,
buffer,
disableLosslessIntegers,
useBigInt
) {
unpacker._verifyStructSize(
'DateTimeWithZoneId',
DATE_TIME_WITH_ZONE_ID_STRUCT_SIZE,
structSize
)
const epochSecond = unpacker.unpackInteger(buffer)
const nano = unpacker.unpackInteger(buffer)
const timeZoneId = unpacker.unpack(buffer)
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano)
const result = new DateTime(
localDateTime.year,
localDateTime.month,
localDateTime.day,
localDateTime.hour,
localDateTime.minute,
localDateTime.second,
localDateTime.nanosecond,
null,
timeZoneId
)
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers, useBigInt)
}
function convertIntegerPropsIfNeeded (obj, disableLosslessIntegers, useBigInt) {
if (!disableLosslessIntegers && !useBigInt) {
return obj
}
const convert = value =>
useBigInt ? value.toBigInt() : value.toNumberOrInfinity()
const clone = Object.create(Object.getPrototypeOf(obj))
for (const prop in obj) {
if (obj.hasOwnProperty(prop)) {
const value = obj[prop]
clone[prop] = isInt(value) ? convert(value) : value
}
}
Object.freeze(clone)
return clone
}