gremlin 3.5.3 → 3.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +3 -2
- package/doc/AnonymousTraversalSource.html +2 -2
- package/doc/Authenticator.html +2 -2
- package/doc/Bytecode.html +2 -2
- package/doc/Client.html +2 -2
- package/doc/Connection.html +10 -10
- package/doc/DriverRemoteConnection.html +2 -2
- package/doc/EdgeLabelVerificationStrategy.html +2 -2
- package/doc/Graph.html +2 -2
- package/doc/GraphSON2Reader.html +2 -2
- package/doc/GraphSON2Writer.html +2 -2
- package/doc/GraphSON3Reader.html +2 -2
- package/doc/GraphSON3Writer.html +2 -2
- package/doc/GraphTraversal.html +2 -2
- package/doc/GraphTraversalSource.html +2 -2
- package/doc/HaltedTraverserStrategy.html +2 -2
- package/doc/MatchAlgorithmStrategy.html +2 -2
- package/doc/P.html +2 -2
- package/doc/PartitionStrategy.html +2 -2
- package/doc/Path.html +2 -2
- package/doc/PlainTextSaslAuthenticator.html +2 -2
- package/doc/ProductiveByStrategy.html +2 -2
- package/doc/RemoteConnection.html +2 -2
- package/doc/RemoteStrategy.html +2 -2
- package/doc/RemoteTraversal.html +2 -2
- package/doc/ReservedKeysVerificationStrategy.html +2 -2
- package/doc/ResponseError.html +2 -2
- package/doc/ResultSet.html +2 -2
- package/doc/SaslAuthenticator.html +2 -2
- package/doc/SaslMechanismBase.html +2 -2
- package/doc/SaslMechanismPlain.html +2 -2
- package/doc/SeedStrategy.html +2 -2
- package/doc/SubgraphStrategy.html +2 -2
- package/doc/TextP.html +2 -2
- package/doc/Transaction.html +2 -2
- package/doc/Translator.html +2 -2
- package/doc/TraversalStrategies.html +2 -2
- package/doc/TraversalStrategy.html +2 -2
- package/doc/TypeSerializer.html +2 -2
- package/doc/driver_auth_authenticator.js.html +2 -2
- package/doc/driver_auth_mechanisms_sasl-mechanism-base.js.html +2 -2
- package/doc/driver_auth_mechanisms_sasl-mechanism-plain.js.html +2 -2
- package/doc/driver_auth_plain-text-sasl-authenticator.js.html +2 -2
- package/doc/driver_auth_sasl-authenticator.js.html +2 -2
- package/doc/driver_client.js.html +2 -2
- package/doc/driver_connection.js.html +32 -9
- package/doc/driver_driver-remote-connection.js.html +2 -2
- package/doc/driver_remote-connection.js.html +2 -2
- package/doc/driver_response-error.js.html +2 -2
- package/doc/driver_result-set.js.html +2 -2
- package/doc/global.html +66 -2
- package/doc/index.html +2 -2
- package/doc/module.exports.html +301 -0
- package/doc/process_anonymous-traversal.js.html +2 -2
- package/doc/process_bytecode.js.html +2 -2
- package/doc/process_graph-traversal.js.html +2 -2
- package/doc/process_transaction.js.html +2 -2
- package/doc/process_translator.js.html +2 -2
- package/doc/process_traversal-strategy.js.html +3 -3
- package/doc/process_traversal.js.html +2 -2
- package/doc/structure_graph.js.html +2 -2
- package/doc/structure_io_binary_internals_DataType.js.html +148 -0
- package/doc/structure_io_binary_internals_GraphBinaryReader.js.html +131 -0
- package/doc/structure_io_binary_internals_GraphBinaryWriter.js.html +134 -0
- package/doc/structure_io_graph-serializer.js.html +2 -2
- package/doc/structure_io_type-serializers.js.html +2 -2
- package/lib/driver/connection.js +30 -7
- package/lib/process/traversal-strategy.js +1 -1
- package/lib/structure/io/binary/GraphBinary.js +109 -0
- package/lib/structure/io/binary/internals/AnySerializer.js +95 -0
- package/lib/structure/io/binary/internals/ArraySerializer.js +135 -0
- package/lib/structure/io/binary/internals/BigIntegerSerializer.js +151 -0
- package/lib/structure/io/binary/internals/BooleanSerializer.js +103 -0
- package/lib/structure/io/binary/internals/BulkSetSerializer.js +124 -0
- package/lib/structure/io/binary/internals/ByteBufferSerializer.js +124 -0
- package/lib/structure/io/binary/internals/ByteSerializer.js +95 -0
- package/lib/structure/io/binary/internals/BytecodeSerializer.js +249 -0
- package/lib/structure/io/binary/internals/DataType.js +97 -0
- package/lib/structure/io/binary/internals/DateSerializer.js +103 -0
- package/lib/structure/io/binary/internals/DoubleSerializer.js +99 -0
- package/lib/structure/io/binary/internals/EdgeSerializer.js +215 -0
- package/lib/structure/io/binary/internals/EnumSerializer.js +148 -0
- package/lib/structure/io/binary/internals/FloatSerializer.js +99 -0
- package/lib/structure/io/binary/internals/GraphBinaryReader.js +80 -0
- package/lib/structure/io/binary/internals/GraphBinaryWriter.js +83 -0
- package/lib/structure/io/binary/internals/IntSerializer.js +112 -0
- package/lib/structure/io/binary/internals/LambdaSerializer.js +70 -0
- package/lib/structure/io/binary/internals/LongSerializer.js +107 -0
- package/lib/structure/io/binary/internals/LongSerializerNg.js +102 -0
- package/lib/structure/io/binary/internals/MapSerializer.js +153 -0
- package/lib/structure/io/binary/internals/NumberSerializationStrategy.js +68 -0
- package/lib/structure/io/binary/internals/PSerializer.js +151 -0
- package/lib/structure/io/binary/internals/PathSerializer.js +123 -0
- package/lib/structure/io/binary/internals/PropertySerializer.js +141 -0
- package/lib/structure/io/binary/internals/ShortSerializer.js +99 -0
- package/lib/structure/io/binary/internals/StringSerializer.js +114 -0
- package/lib/structure/io/binary/internals/TextPSerializer.js +145 -0
- package/lib/structure/io/binary/internals/TraversalStrategySerializer.js +72 -0
- package/lib/structure/io/binary/internals/TraverserSerializer.js +123 -0
- package/lib/structure/io/binary/internals/UnspecifiedNullSerializer.js +75 -0
- package/lib/structure/io/binary/internals/UuidSerializer.js +119 -0
- package/lib/structure/io/binary/internals/VertexPropertySerializer.js +172 -0
- package/lib/structure/io/binary/internals/VertexSerializer.js +135 -0
- package/lib/structure/io/binary/internals/utils.js +59 -0
- package/package.json +14 -7
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* GraphBinary 1.0 support implementation.
|
|
22
|
+
*
|
|
23
|
+
* The officially expected entrypoint is GraphBinaryReader/GraphBinaryWriter pair of classes,
|
|
24
|
+
* examine lib/driver/** for use cases.
|
|
25
|
+
*
|
|
26
|
+
* See AnySerializer.serialize() for the mechanism of serializer selection for a given JavaScript value,
|
|
27
|
+
* also consider AnySerializer.serialize() unit tests for real examples.
|
|
28
|
+
* See NumberSerializationStrategy to understand how it deals with JavaScript numbers' serialization.
|
|
29
|
+
*
|
|
30
|
+
* Consider AnySerializer.serialize()/deserialize() unit tests to see what is not implemented,
|
|
31
|
+
* what is ignored, what is not expected to be (de)serialized, etc.
|
|
32
|
+
*
|
|
33
|
+
* TODO: it has the following open topics:
|
|
34
|
+
* - [] Should we do anything for application/vnd.graphbinary-v1.0-stringd mime type support?
|
|
35
|
+
* Core Data Types support:
|
|
36
|
+
* - [] 0x22: BigDecimal
|
|
37
|
+
* - [] 0x2b: Tree
|
|
38
|
+
* - [] 0x2c: Metrics
|
|
39
|
+
* - [] 0x2d: TraversalMetrics
|
|
40
|
+
* - [] 0x00: Custom
|
|
41
|
+
* Extended Types support:
|
|
42
|
+
* - [] 0x80: Char
|
|
43
|
+
* - [] 0x81: Duration
|
|
44
|
+
* - [] 0x82: InetAddress
|
|
45
|
+
* - [] 0x83: Instant
|
|
46
|
+
* - [] 0x84: LocalDate
|
|
47
|
+
* - [] 0x85: LocalDateTime
|
|
48
|
+
* - [] 0x86: LocalTime
|
|
49
|
+
* - [] 0x87: MonthDay
|
|
50
|
+
* - [] 0x88: OffsetDateTime
|
|
51
|
+
* - [] 0x89: OffsetTime
|
|
52
|
+
* - [] 0x8a: Period
|
|
53
|
+
* - [] 0x8b: Year
|
|
54
|
+
* - [] 0x8c: YearMonth
|
|
55
|
+
* - [] 0x8d: ZonedDateTime
|
|
56
|
+
* - [] 0x8e: ZoneOffset
|
|
57
|
+
*
|
|
58
|
+
* @author Igor Ostapenko
|
|
59
|
+
*/
|
|
60
|
+
/*eslint-disable*/
|
|
61
|
+
'use strict';
|
|
62
|
+
|
|
63
|
+
const ioc = {};
|
|
64
|
+
|
|
65
|
+
ioc.DataType = require('./internals/DataType');
|
|
66
|
+
ioc.utils = require('./internals/utils');
|
|
67
|
+
|
|
68
|
+
ioc.serializers = {};
|
|
69
|
+
|
|
70
|
+
ioc.intSerializer = new (require('./internals/IntSerializer'))(ioc);
|
|
71
|
+
ioc.longSerializer = new (require('./internals/LongSerializer'))(ioc);
|
|
72
|
+
ioc.longSerializerNg = new (require('./internals/LongSerializerNg'))(ioc);
|
|
73
|
+
ioc.stringSerializer = new (require('./internals/StringSerializer'))(ioc, ioc.DataType.STRING);
|
|
74
|
+
ioc.dateSerializer = new (require('./internals/DateSerializer'))(ioc, ioc.DataType.DATE);
|
|
75
|
+
ioc.timestampSerializer = new (require('./internals/DateSerializer'))(ioc, ioc.DataType.TIMESTAMP);
|
|
76
|
+
ioc.classSerializer = new (require('./internals/StringSerializer'))(ioc, ioc.DataType.CLASS);
|
|
77
|
+
ioc.doubleSerializer = new (require('./internals/DoubleSerializer'))(ioc);
|
|
78
|
+
ioc.floatSerializer = new (require('./internals/FloatSerializer'))(ioc);
|
|
79
|
+
ioc.listSerializer = new (require('./internals/ArraySerializer'))(ioc, ioc.DataType.LIST);
|
|
80
|
+
ioc.mapSerializer = new (require('./internals/MapSerializer'))(ioc);
|
|
81
|
+
ioc.setSerializer = new (require('./internals/ArraySerializer'))(ioc, ioc.DataType.SET);
|
|
82
|
+
ioc.uuidSerializer = new (require('./internals/UuidSerializer'))(ioc);
|
|
83
|
+
ioc.edgeSerializer = new (require('./internals/EdgeSerializer'))(ioc);
|
|
84
|
+
ioc.pathSerializer = new (require('./internals/PathSerializer'))(ioc);
|
|
85
|
+
ioc.propertySerializer = new (require('./internals/PropertySerializer'))(ioc);
|
|
86
|
+
ioc.vertexSerializer = new (require('./internals/VertexSerializer'))(ioc);
|
|
87
|
+
ioc.vertexPropertySerializer = new (require('./internals/VertexPropertySerializer'))(ioc);
|
|
88
|
+
ioc.bytecodeSerializer = new (require('./internals/BytecodeSerializer'))(ioc);
|
|
89
|
+
ioc.pSerializer = new (require('./internals/PSerializer'))(ioc);
|
|
90
|
+
ioc.traverserSerializer = new (require('./internals/TraverserSerializer'))(ioc);
|
|
91
|
+
ioc.enumSerializer = new (require('./internals/EnumSerializer'))(ioc);
|
|
92
|
+
ioc.lambdaSerializer = new (require('./internals/LambdaSerializer'))(ioc);
|
|
93
|
+
ioc.bigIntegerSerializer = new (require('./internals/BigIntegerSerializer'))(ioc);
|
|
94
|
+
ioc.byteSerializer = new (require('./internals/ByteSerializer'))(ioc);
|
|
95
|
+
ioc.byteBufferSerializer = new (require('./internals/ByteBufferSerializer'))(ioc);
|
|
96
|
+
ioc.shortSerializer = new (require('./internals/ShortSerializer'))(ioc);
|
|
97
|
+
ioc.booleanSerializer = new (require('./internals/BooleanSerializer'))(ioc);
|
|
98
|
+
ioc.textPSerializer = new (require('./internals/TextPSerializer'))(ioc);
|
|
99
|
+
ioc.traversalStrategySerializer = new (require('./internals/TraversalStrategySerializer'))(ioc);
|
|
100
|
+
ioc.bulkSetSerializer = new (require('./internals/BulkSetSerializer'))(ioc);
|
|
101
|
+
ioc.unspecifiedNullSerializer = new (require('./internals/UnspecifiedNullSerializer'))(ioc);
|
|
102
|
+
|
|
103
|
+
ioc.numberSerializationStrategy = new (require('./internals/NumberSerializationStrategy'))(ioc);
|
|
104
|
+
ioc.anySerializer = new (require('./internals/AnySerializer'))(ioc);
|
|
105
|
+
|
|
106
|
+
ioc.graphBinaryReader = new (require('./internals/GraphBinaryReader.js'))(ioc);
|
|
107
|
+
ioc.graphBinaryWriter = new (require('./internals/GraphBinaryWriter'))(ioc);
|
|
108
|
+
|
|
109
|
+
module.exports = ioc;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @author Igor Ostapenko
|
|
22
|
+
*/
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
module.exports = class AnySerializer {
|
|
26
|
+
constructor(ioc) {
|
|
27
|
+
this.ioc = ioc;
|
|
28
|
+
|
|
29
|
+
// specifically ordered, the first canBeUsedFor=true wins
|
|
30
|
+
this.serializers = [
|
|
31
|
+
ioc.unspecifiedNullSerializer,
|
|
32
|
+
|
|
33
|
+
ioc.numberSerializationStrategy,
|
|
34
|
+
|
|
35
|
+
ioc.booleanSerializer,
|
|
36
|
+
ioc.dateSerializer,
|
|
37
|
+
ioc.bytecodeSerializer,
|
|
38
|
+
ioc.pSerializer,
|
|
39
|
+
ioc.traverserSerializer,
|
|
40
|
+
ioc.enumSerializer,
|
|
41
|
+
ioc.listSerializer,
|
|
42
|
+
ioc.uuidSerializer,
|
|
43
|
+
ioc.edgeSerializer,
|
|
44
|
+
ioc.pathSerializer,
|
|
45
|
+
ioc.propertySerializer,
|
|
46
|
+
ioc.vertexSerializer,
|
|
47
|
+
ioc.vertexPropertySerializer,
|
|
48
|
+
ioc.stringSerializer,
|
|
49
|
+
ioc.textPSerializer,
|
|
50
|
+
ioc.traversalStrategySerializer,
|
|
51
|
+
|
|
52
|
+
ioc.byteBufferSerializer, // Buffer instance
|
|
53
|
+
ioc.lambdaSerializer, // any function
|
|
54
|
+
ioc.mapSerializer, // Map or any Object
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getSerializerCanBeUsedFor(item) {
|
|
59
|
+
for (let i = 0; i < this.serializers.length; i++) {
|
|
60
|
+
if (this.serializers[i].canBeUsedFor(item)) {
|
|
61
|
+
return this.serializers[i];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw new Error(
|
|
66
|
+
`No serializer found to support item where typeof(item)='${typeof item}' and String(item)='${String(item)}'.`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
serialize(item, fullyQualifiedFormat = true) {
|
|
71
|
+
return this.getSerializerCanBeUsedFor(item).serialize(item, fullyQualifiedFormat);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
deserialize(buffer) {
|
|
75
|
+
// obviously, fullyQualifiedFormat always is true
|
|
76
|
+
try {
|
|
77
|
+
if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
|
|
78
|
+
throw new Error('buffer is missing');
|
|
79
|
+
}
|
|
80
|
+
if (buffer.length < 1) {
|
|
81
|
+
throw new Error('buffer is empty');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const type_code = buffer.readUInt8();
|
|
85
|
+
const serializer = this.ioc.serializers[type_code];
|
|
86
|
+
if (!serializer) {
|
|
87
|
+
throw new Error('unknown {type_code}');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return serializer.deserialize(buffer);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
throw this.ioc.utils.des_error({ serializer: this, args: arguments, err });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @author Igor Ostapenko
|
|
22
|
+
*/
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
module.exports = class ArraySerializer {
|
|
26
|
+
constructor(ioc, ID) {
|
|
27
|
+
this.ioc = ioc;
|
|
28
|
+
this.ID = ID;
|
|
29
|
+
this.ioc.serializers[ID] = this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
canBeUsedFor(value) {
|
|
33
|
+
return Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
serialize(item, fullyQualifiedFormat = true) {
|
|
37
|
+
if (item === undefined || item === null) {
|
|
38
|
+
if (fullyQualifiedFormat) {
|
|
39
|
+
return Buffer.from([this.ID, 0x01]);
|
|
40
|
+
}
|
|
41
|
+
return Buffer.from([0x00, 0x00, 0x00, 0x00]); // {length} = 0
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const bufs = [];
|
|
45
|
+
if (fullyQualifiedFormat) {
|
|
46
|
+
bufs.push(Buffer.from([this.ID, 0x00]));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// {length}
|
|
50
|
+
let length = item.length;
|
|
51
|
+
if (length < 0) {
|
|
52
|
+
length = 0; // TODO: test it
|
|
53
|
+
}
|
|
54
|
+
if (length > this.ioc.intSerializer.INT32_MAX) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Array length=${length} is greater than supported max_length=${this.ioc.intSerializer.INT32_MAX}.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
bufs.push(this.ioc.intSerializer.serialize(length, false));
|
|
60
|
+
|
|
61
|
+
// {item_0}...{item_n}
|
|
62
|
+
for (let i = 0; i < length; i++) {
|
|
63
|
+
bufs.push(this.ioc.anySerializer.serialize(item[i]));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return Buffer.concat(bufs);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
deserialize(buffer, fullyQualifiedFormat = true) {
|
|
70
|
+
let len = 0;
|
|
71
|
+
let cursor = buffer;
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
|
|
75
|
+
throw new Error('buffer is missing');
|
|
76
|
+
}
|
|
77
|
+
if (buffer.length < 1) {
|
|
78
|
+
throw new Error('buffer is empty');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (fullyQualifiedFormat) {
|
|
82
|
+
const type_code = cursor.readUInt8();
|
|
83
|
+
len++;
|
|
84
|
+
if (type_code !== this.ID) {
|
|
85
|
+
throw new Error('unexpected {type_code}');
|
|
86
|
+
}
|
|
87
|
+
cursor = cursor.slice(1);
|
|
88
|
+
|
|
89
|
+
if (cursor.length < 1) {
|
|
90
|
+
throw new Error('{value_flag} is missing');
|
|
91
|
+
}
|
|
92
|
+
const value_flag = cursor.readUInt8();
|
|
93
|
+
len++;
|
|
94
|
+
if (value_flag === 1) {
|
|
95
|
+
return { v: null, len };
|
|
96
|
+
}
|
|
97
|
+
if (value_flag !== 0) {
|
|
98
|
+
throw new Error('unexpected {value_flag}');
|
|
99
|
+
}
|
|
100
|
+
cursor = cursor.slice(1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let length, length_len;
|
|
104
|
+
try {
|
|
105
|
+
({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
|
|
106
|
+
len += length_len;
|
|
107
|
+
} catch (err) {
|
|
108
|
+
err.message = '{length}: ' + err.message;
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
if (length < 0) {
|
|
112
|
+
throw new Error('{length} is less than zero');
|
|
113
|
+
}
|
|
114
|
+
cursor = cursor.slice(length_len);
|
|
115
|
+
|
|
116
|
+
const v = [];
|
|
117
|
+
for (let i = 0; i < length; i++) {
|
|
118
|
+
let value, value_len;
|
|
119
|
+
try {
|
|
120
|
+
({ v: value, len: value_len } = this.ioc.anySerializer.deserialize(cursor));
|
|
121
|
+
len += value_len;
|
|
122
|
+
} catch (err) {
|
|
123
|
+
err.message = `{item_${i}}: ` + err.message;
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
cursor = cursor.slice(value_len);
|
|
127
|
+
v.push(value);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return { v, len };
|
|
131
|
+
} catch (err) {
|
|
132
|
+
throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @author Igor Ostapenko
|
|
22
|
+
*/
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
// TODO: it has room for performance improvements
|
|
26
|
+
module.exports = class BigIntegerSerializer {
|
|
27
|
+
constructor(ioc) {
|
|
28
|
+
this.ioc = ioc;
|
|
29
|
+
this.ioc.serializers[ioc.DataType.BIGINTEGER] = this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
canBeUsedFor(value) {
|
|
33
|
+
// it's not expected to be used directly, see NumberSerializationStrategy
|
|
34
|
+
return typeof value === 'bigint';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
serialize(item, fullyQualifiedFormat = true) {
|
|
38
|
+
if (item === undefined || item === null) {
|
|
39
|
+
if (fullyQualifiedFormat) {
|
|
40
|
+
return Buffer.from([this.ioc.DataType.BIGINTEGER, 0x01]);
|
|
41
|
+
}
|
|
42
|
+
return Buffer.from([0x00, 0x00, 0x00, 0x01, 0x00]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const bufs = [];
|
|
46
|
+
if (fullyQualifiedFormat) {
|
|
47
|
+
bufs.push(Buffer.from([this.ioc.DataType.BIGINTEGER, 0x00]));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let v;
|
|
51
|
+
|
|
52
|
+
// positive number
|
|
53
|
+
if (item >= 0) {
|
|
54
|
+
let hex_str = item.toString(16);
|
|
55
|
+
if (hex_str.length % 2 !== 0) {
|
|
56
|
+
hex_str = '0' + hex_str; // Buffer.from() expects exactly two hex digits per byte
|
|
57
|
+
}
|
|
58
|
+
if (Number.parseInt(hex_str[0], 16) > 7) {
|
|
59
|
+
hex_str = '00' + hex_str; // to keep sign bit unset for positive number
|
|
60
|
+
}
|
|
61
|
+
v = Buffer.from(hex_str, 'hex');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// negative number
|
|
65
|
+
else {
|
|
66
|
+
let hex_str = (-item).toString(16);
|
|
67
|
+
|
|
68
|
+
const bytes = (hex_str.length + (hex_str.length % 2)) / 2; // number's size in bytes
|
|
69
|
+
let N = BigInt(bytes) * BigInt(8); // number's size in bits
|
|
70
|
+
const INTN_MIN = -(BigInt(2) ** (N - BigInt(1))); // minimum negative number we can keep within N bits
|
|
71
|
+
if (item < INTN_MIN) {
|
|
72
|
+
N += BigInt(8); // to provide room for smaller negative number
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const twos_complement = BigInt(2) ** N + item;
|
|
76
|
+
hex_str = twos_complement.toString(16);
|
|
77
|
+
if (hex_str.length % 2 !== 0) {
|
|
78
|
+
hex_str = '0' + hex_str; // Buffer.from() expects exactly two hex digits per byte
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
v = Buffer.from(hex_str, 'hex');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
bufs.push(this.ioc.intSerializer.serialize(v.length, false));
|
|
85
|
+
bufs.push(v);
|
|
86
|
+
|
|
87
|
+
return Buffer.concat(bufs);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
deserialize(buffer, fullyQualifiedFormat = true) {
|
|
91
|
+
let len = 0;
|
|
92
|
+
let cursor = buffer;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
|
|
96
|
+
throw new Error('buffer is missing');
|
|
97
|
+
}
|
|
98
|
+
if (buffer.length < 1) {
|
|
99
|
+
throw new Error('buffer is empty');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (fullyQualifiedFormat) {
|
|
103
|
+
const type_code = cursor.readUInt8();
|
|
104
|
+
len++;
|
|
105
|
+
if (type_code !== this.ioc.DataType.BIGINTEGER) {
|
|
106
|
+
throw new Error('unexpected {type_code}');
|
|
107
|
+
}
|
|
108
|
+
cursor = cursor.slice(1);
|
|
109
|
+
|
|
110
|
+
if (cursor.length < 1) {
|
|
111
|
+
throw new Error('{value_flag} is missing');
|
|
112
|
+
}
|
|
113
|
+
const value_flag = cursor.readUInt8();
|
|
114
|
+
len++;
|
|
115
|
+
if (value_flag === 1) {
|
|
116
|
+
return { v: null, len };
|
|
117
|
+
}
|
|
118
|
+
if (value_flag !== 0) {
|
|
119
|
+
throw new Error('unexpected {value_flag}');
|
|
120
|
+
}
|
|
121
|
+
cursor = cursor.slice(1);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// {length}
|
|
125
|
+
let length, length_len;
|
|
126
|
+
try {
|
|
127
|
+
({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
|
|
128
|
+
len += length_len;
|
|
129
|
+
} catch (err) {
|
|
130
|
+
err.message = '{length}: ' + err.message;
|
|
131
|
+
throw err;
|
|
132
|
+
}
|
|
133
|
+
if (length < 1) {
|
|
134
|
+
throw new Error(`{length}=${length} is less than one`);
|
|
135
|
+
}
|
|
136
|
+
cursor = cursor.slice(length_len);
|
|
137
|
+
|
|
138
|
+
len += length;
|
|
139
|
+
cursor = cursor.slice(0, length);
|
|
140
|
+
let v = BigInt(`0x${cursor.toString('hex')}`);
|
|
141
|
+
const is_sign_bit_set = (cursor[0] & 0x80) === 0x80;
|
|
142
|
+
if (is_sign_bit_set) {
|
|
143
|
+
v = BigInt.asIntN(length * 8, v); // now we get expected negative number
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { v, len };
|
|
147
|
+
} catch (err) {
|
|
148
|
+
throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @author Igor Ostapenko
|
|
22
|
+
*/
|
|
23
|
+
'use strict';
|
|
24
|
+
|
|
25
|
+
module.exports = class BooleanSerializer {
|
|
26
|
+
constructor(ioc) {
|
|
27
|
+
this.ioc = ioc;
|
|
28
|
+
this.ioc.serializers[ioc.DataType.BOOLEAN] = this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
canBeUsedFor(value) {
|
|
32
|
+
return typeof value === 'boolean';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
serialize(item, fullyQualifiedFormat = true) {
|
|
36
|
+
if (item === undefined || item === null) {
|
|
37
|
+
if (fullyQualifiedFormat) {
|
|
38
|
+
return Buffer.from([this.ioc.DataType.BOOLEAN, 0x01]);
|
|
39
|
+
}
|
|
40
|
+
return Buffer.from([0x00]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const bufs = [];
|
|
44
|
+
if (fullyQualifiedFormat) {
|
|
45
|
+
bufs.push(Buffer.from([this.ioc.DataType.BOOLEAN, 0x00]));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bufs.push(Buffer.from([item ? 0x01 : 0x00]));
|
|
49
|
+
|
|
50
|
+
return Buffer.concat(bufs);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
deserialize(buffer, fullyQualifiedFormat = true) {
|
|
54
|
+
let len = 0;
|
|
55
|
+
let cursor = buffer;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
|
|
59
|
+
throw new Error('buffer is missing');
|
|
60
|
+
}
|
|
61
|
+
if (buffer.length < 1) {
|
|
62
|
+
throw new Error('buffer is empty');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (fullyQualifiedFormat) {
|
|
66
|
+
const type_code = cursor.readUInt8();
|
|
67
|
+
len++;
|
|
68
|
+
if (type_code !== this.ioc.DataType.BOOLEAN) {
|
|
69
|
+
throw new Error('unexpected {type_code}');
|
|
70
|
+
}
|
|
71
|
+
cursor = cursor.slice(1);
|
|
72
|
+
|
|
73
|
+
if (cursor.length < 1) {
|
|
74
|
+
throw new Error('{value_flag} is missing');
|
|
75
|
+
}
|
|
76
|
+
const value_flag = cursor.readUInt8();
|
|
77
|
+
len++;
|
|
78
|
+
if (value_flag === 1) {
|
|
79
|
+
return { v: null, len };
|
|
80
|
+
}
|
|
81
|
+
if (value_flag !== 0) {
|
|
82
|
+
throw new Error('unexpected {value_flag}');
|
|
83
|
+
}
|
|
84
|
+
cursor = cursor.slice(1);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (cursor.length < 1) {
|
|
88
|
+
throw new Error('unexpected {value} length');
|
|
89
|
+
}
|
|
90
|
+
len += 1;
|
|
91
|
+
|
|
92
|
+
let v = cursor.readUInt8();
|
|
93
|
+
if (v !== 0x00 && v !== 0x01) {
|
|
94
|
+
throw new Error(`unexpected boolean byte=${v}`);
|
|
95
|
+
}
|
|
96
|
+
v = v === 0x01;
|
|
97
|
+
|
|
98
|
+
return { v, len };
|
|
99
|
+
} catch (err) {
|
|
100
|
+
throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|