gremlin 3.6.0 → 3.6.2

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.
Files changed (113) hide show
  1. package/.eslintrc.js +3 -2
  2. package/NOTICE +1 -1
  3. package/doc/AnonymousTraversalSource.html +2 -2
  4. package/doc/Authenticator.html +2 -2
  5. package/doc/Bytecode.html +2 -2
  6. package/doc/Client.html +174 -9
  7. package/doc/Connection.html +43 -10
  8. package/doc/DriverRemoteConnection.html +144 -12
  9. package/doc/EdgeLabelVerificationStrategy.html +2 -2
  10. package/doc/Graph.html +2 -2
  11. package/doc/GraphSON2Reader.html +2 -2
  12. package/doc/GraphSON2Writer.html +2 -2
  13. package/doc/GraphSON3Reader.html +2 -2
  14. package/doc/GraphSON3Writer.html +2 -2
  15. package/doc/GraphTraversal.html +112 -112
  16. package/doc/GraphTraversalSource.html +19 -19
  17. package/doc/HaltedTraverserStrategy.html +2 -2
  18. package/doc/MatchAlgorithmStrategy.html +2 -2
  19. package/doc/P.html +2 -2
  20. package/doc/PartitionStrategy.html +2 -2
  21. package/doc/Path.html +2 -2
  22. package/doc/PlainTextSaslAuthenticator.html +2 -2
  23. package/doc/ProductiveByStrategy.html +2 -2
  24. package/doc/RemoteConnection.html +2 -2
  25. package/doc/RemoteStrategy.html +2 -2
  26. package/doc/RemoteTraversal.html +2 -2
  27. package/doc/ReservedKeysVerificationStrategy.html +2 -2
  28. package/doc/ResponseError.html +2 -2
  29. package/doc/ResultSet.html +2 -2
  30. package/doc/SaslAuthenticator.html +2 -2
  31. package/doc/SaslMechanismBase.html +2 -2
  32. package/doc/SaslMechanismPlain.html +2 -2
  33. package/doc/SeedStrategy.html +2 -2
  34. package/doc/SubgraphStrategy.html +2 -2
  35. package/doc/TextP.html +2 -2
  36. package/doc/Transaction.html +2 -2
  37. package/doc/Translator.html +2 -2
  38. package/doc/TraversalStrategies.html +2 -2
  39. package/doc/TraversalStrategy.html +2 -2
  40. package/doc/TypeSerializer.html +2 -2
  41. package/doc/driver_auth_authenticator.js.html +2 -2
  42. package/doc/driver_auth_mechanisms_sasl-mechanism-base.js.html +2 -2
  43. package/doc/driver_auth_mechanisms_sasl-mechanism-plain.js.html +2 -2
  44. package/doc/driver_auth_plain-text-sasl-authenticator.js.html +2 -2
  45. package/doc/driver_auth_sasl-authenticator.js.html +2 -2
  46. package/doc/driver_client.js.html +7 -2
  47. package/doc/driver_connection.js.html +49 -13
  48. package/doc/driver_driver-remote-connection.js.html +6 -2
  49. package/doc/driver_remote-connection.js.html +2 -2
  50. package/doc/driver_response-error.js.html +2 -2
  51. package/doc/driver_result-set.js.html +2 -2
  52. package/doc/global.html +68 -4
  53. package/doc/index.html +2 -2
  54. package/doc/module.exports.html +301 -0
  55. package/doc/process_anonymous-traversal.js.html +2 -2
  56. package/doc/process_bytecode.js.html +2 -2
  57. package/doc/process_graph-traversal.js.html +25 -13
  58. package/doc/process_transaction.js.html +2 -2
  59. package/doc/process_translator.js.html +2 -2
  60. package/doc/process_traversal-strategy.js.html +3 -3
  61. package/doc/process_traversal.js.html +4 -4
  62. package/doc/structure_graph.js.html +2 -2
  63. package/doc/structure_io_binary_internals_DataType.js.html +149 -0
  64. package/doc/structure_io_binary_internals_GraphBinaryReader.js.html +131 -0
  65. package/doc/structure_io_binary_internals_GraphBinaryWriter.js.html +134 -0
  66. package/doc/structure_io_graph-serializer.js.html +2 -2
  67. package/doc/structure_io_type-serializers.js.html +2 -2
  68. package/docker-compose.yml +62 -0
  69. package/index.js +1 -0
  70. package/lib/driver/client.js +5 -0
  71. package/lib/driver/connection.js +47 -11
  72. package/lib/driver/driver-remote-connection.js +4 -0
  73. package/lib/process/graph-traversal.js +23 -11
  74. package/lib/process/traversal-strategy.js +1 -1
  75. package/lib/process/traversal.js +2 -2
  76. package/lib/structure/io/binary/GraphBinary.js +109 -0
  77. package/lib/structure/io/binary/internals/AnySerializer.js +95 -0
  78. package/lib/structure/io/binary/internals/ArraySerializer.js +135 -0
  79. package/lib/structure/io/binary/internals/BigIntegerSerializer.js +151 -0
  80. package/lib/structure/io/binary/internals/BooleanSerializer.js +103 -0
  81. package/lib/structure/io/binary/internals/BulkSetSerializer.js +124 -0
  82. package/lib/structure/io/binary/internals/ByteBufferSerializer.js +124 -0
  83. package/lib/structure/io/binary/internals/ByteSerializer.js +95 -0
  84. package/lib/structure/io/binary/internals/BytecodeSerializer.js +249 -0
  85. package/lib/structure/io/binary/internals/DataType.js +98 -0
  86. package/lib/structure/io/binary/internals/DateSerializer.js +103 -0
  87. package/lib/structure/io/binary/internals/DoubleSerializer.js +99 -0
  88. package/lib/structure/io/binary/internals/EdgeSerializer.js +215 -0
  89. package/lib/structure/io/binary/internals/EnumSerializer.js +149 -0
  90. package/lib/structure/io/binary/internals/FloatSerializer.js +99 -0
  91. package/lib/structure/io/binary/internals/GraphBinaryReader.js +80 -0
  92. package/lib/structure/io/binary/internals/GraphBinaryWriter.js +83 -0
  93. package/lib/structure/io/binary/internals/IntSerializer.js +112 -0
  94. package/lib/structure/io/binary/internals/LambdaSerializer.js +70 -0
  95. package/lib/structure/io/binary/internals/LongSerializer.js +107 -0
  96. package/lib/structure/io/binary/internals/LongSerializerNg.js +102 -0
  97. package/lib/structure/io/binary/internals/MapSerializer.js +153 -0
  98. package/lib/structure/io/binary/internals/NumberSerializationStrategy.js +68 -0
  99. package/lib/structure/io/binary/internals/PSerializer.js +151 -0
  100. package/lib/structure/io/binary/internals/PathSerializer.js +123 -0
  101. package/lib/structure/io/binary/internals/PropertySerializer.js +141 -0
  102. package/lib/structure/io/binary/internals/ShortSerializer.js +99 -0
  103. package/lib/structure/io/binary/internals/StringSerializer.js +114 -0
  104. package/lib/structure/io/binary/internals/TextPSerializer.js +145 -0
  105. package/lib/structure/io/binary/internals/TraversalStrategySerializer.js +72 -0
  106. package/lib/structure/io/binary/internals/TraverserSerializer.js +123 -0
  107. package/lib/structure/io/binary/internals/UnspecifiedNullSerializer.js +75 -0
  108. package/lib/structure/io/binary/internals/UuidSerializer.js +119 -0
  109. package/lib/structure/io/binary/internals/VertexPropertySerializer.js +172 -0
  110. package/lib/structure/io/binary/internals/VertexSerializer.js +135 -0
  111. package/lib/structure/io/binary/internals/utils.js +59 -0
  112. package/lib/utils.js +29 -0
  113. package/package.json +22 -12
@@ -0,0 +1,112 @@
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 IntSerializer {
26
+ get INT32_MIN() {
27
+ return -2147483648;
28
+ }
29
+ get INT32_MAX() {
30
+ return 2147483647;
31
+ }
32
+
33
+ constructor(ioc) {
34
+ this.ioc = ioc;
35
+ this.ioc.serializers[ioc.DataType.INT] = this;
36
+ }
37
+
38
+ canBeUsedFor(value) {
39
+ if (typeof value !== 'number') {
40
+ return false;
41
+ }
42
+ if (value < this.INT32_MIN || value > this.INT32_MAX) {
43
+ return false;
44
+ }
45
+ return true;
46
+ }
47
+
48
+ serialize(item, fullyQualifiedFormat = true) {
49
+ if (item === undefined || item === null) {
50
+ if (fullyQualifiedFormat) {
51
+ return Buffer.from([this.ioc.DataType.INT, 0x01]);
52
+ }
53
+ return Buffer.from([0x00, 0x00, 0x00, 0x00]);
54
+ }
55
+
56
+ const bufs = [];
57
+ if (fullyQualifiedFormat) {
58
+ bufs.push(Buffer.from([this.ioc.DataType.INT, 0x00]));
59
+ }
60
+ const v = Buffer.alloc(4);
61
+ v.writeInt32BE(item); // if item is not within int32 limits writeInt32BE will error
62
+ bufs.push(v);
63
+
64
+ return Buffer.concat(bufs);
65
+ }
66
+
67
+ deserialize(buffer, fullyQualifiedFormat = true) {
68
+ let len = 0;
69
+ let cursor = buffer;
70
+
71
+ try {
72
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
73
+ throw new Error('buffer is missing');
74
+ }
75
+ if (buffer.length < 1) {
76
+ throw new Error('buffer is empty');
77
+ }
78
+
79
+ if (fullyQualifiedFormat) {
80
+ const type_code = cursor.readUInt8();
81
+ len++;
82
+ if (type_code !== this.ioc.DataType.INT) {
83
+ throw new Error('unexpected {type_code}');
84
+ }
85
+ cursor = cursor.slice(1);
86
+
87
+ if (cursor.length < 1) {
88
+ throw new Error('{value_flag} is missing');
89
+ }
90
+ const value_flag = cursor.readUInt8();
91
+ len++;
92
+ if (value_flag === 1) {
93
+ return { v: null, len };
94
+ }
95
+ if (value_flag !== 0) {
96
+ throw new Error('unexpected {value_flag}');
97
+ }
98
+ cursor = cursor.slice(1);
99
+ }
100
+
101
+ if (cursor.length < 4) {
102
+ throw new Error('unexpected {value} length');
103
+ }
104
+ len += 4;
105
+
106
+ const v = cursor.readInt32BE();
107
+ return { v, len };
108
+ } catch (err) {
109
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
110
+ }
111
+ }
112
+ };
@@ -0,0 +1,70 @@
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
+ const { valueKey, LambdaSerializer: GraphsonLambdaSerializer } = require('../../type-serializers');
26
+
27
+ module.exports = class LambdaSerializer {
28
+ constructor(ioc) {
29
+ this.ioc = ioc;
30
+ // this.ioc.serializers[ioc.DataType.LAMBDA] = this; // it's not expected to be deserialized
31
+ this.graphsonLambdaSerializer = new GraphsonLambdaSerializer();
32
+ }
33
+
34
+ canBeUsedFor(value) {
35
+ return this.graphsonLambdaSerializer.canBeUsedFor(value);
36
+ }
37
+
38
+ serialize(item, fullyQualifiedFormat = true) {
39
+ if (item === undefined || item === null) {
40
+ if (fullyQualifiedFormat) {
41
+ return Buffer.from([this.ioc.DataType.LAMBDA, 0x01]);
42
+ }
43
+ const language = [0x00, 0x00, 0x00, 0x00]; // ''
44
+ const script = [0x00, 0x00, 0x00, 0x00]; // ''
45
+ const arguments_length = [0x00, 0x00, 0x00, 0x00]; // 0
46
+ return Buffer.from([...language, ...script, ...arguments_length]);
47
+ }
48
+
49
+ const bufs = [];
50
+ if (fullyQualifiedFormat) {
51
+ bufs.push(Buffer.from([this.ioc.DataType.LAMBDA, 0x00]));
52
+ }
53
+
54
+ const graphson = this.graphsonLambdaSerializer.serialize(item);
55
+ const language = graphson[valueKey].language;
56
+ const script = graphson[valueKey].script;
57
+ const arguments_ = graphson[valueKey]['arguments'];
58
+
59
+ // {language}
60
+ bufs.push(this.ioc.stringSerializer.serialize(language, false));
61
+
62
+ // {script}
63
+ bufs.push(this.ioc.stringSerializer.serialize(script, false));
64
+
65
+ // {arguments_length}
66
+ bufs.push(this.ioc.intSerializer.serialize(arguments_, false));
67
+
68
+ return Buffer.concat(bufs);
69
+ }
70
+ };
@@ -0,0 +1,107 @@
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 LongSerializer {
26
+ constructor(ioc) {
27
+ this.ioc = ioc;
28
+ this.ioc.serializers[ioc.DataType.LONG] = this;
29
+ }
30
+
31
+ canBeUsedFor() {
32
+ // it's not expected to be used, see NumberSerializationStrategy
33
+ }
34
+
35
+ serialize(item, fullyQualifiedFormat = true) {
36
+ if (item === undefined || item === null) {
37
+ if (fullyQualifiedFormat) {
38
+ return Buffer.from([this.ioc.DataType.LONG, 0x01]);
39
+ }
40
+ return Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
41
+ }
42
+
43
+ const bufs = [];
44
+ if (fullyQualifiedFormat) {
45
+ bufs.push(Buffer.from([this.ioc.DataType.LONG, 0x00]));
46
+ }
47
+ const v = Buffer.alloc(8);
48
+ v.writeBigInt64BE(BigInt(item));
49
+ bufs.push(v);
50
+
51
+ return Buffer.concat(bufs);
52
+ }
53
+
54
+ deserialize(buffer, fullyQualifiedFormat = true) {
55
+ let len = 0;
56
+ let cursor = buffer;
57
+
58
+ try {
59
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
60
+ throw new Error('buffer is missing');
61
+ }
62
+ if (buffer.length < 1) {
63
+ throw new Error('buffer is empty');
64
+ }
65
+
66
+ if (fullyQualifiedFormat) {
67
+ const type_code = cursor.readUInt8();
68
+ len++;
69
+ if (type_code !== this.ioc.DataType.LONG) {
70
+ throw new Error('unexpected {type_code}');
71
+ }
72
+ cursor = cursor.slice(1);
73
+
74
+ if (cursor.length < 1) {
75
+ throw new Error('{value_flag} is missing');
76
+ }
77
+ const value_flag = cursor.readUInt8();
78
+ len++;
79
+ if (value_flag === 1) {
80
+ return { v: null, len };
81
+ }
82
+ if (value_flag !== 0) {
83
+ throw new Error('unexpected {value_flag}');
84
+ }
85
+ cursor = cursor.slice(1);
86
+ }
87
+
88
+ if (cursor.length < 8) {
89
+ throw new Error('unexpected {value} length');
90
+ }
91
+ len += 8;
92
+
93
+ let v = cursor.readBigInt64BE();
94
+ if (v < Number.MIN_SAFE_INTEGER || v > Number.MAX_SAFE_INTEGER) {
95
+ // TODO: It tries to keep the same contract as GraphSON LongSerializer provides.
96
+ // Is it still appropriate to do so? Should we consider LongSerializerNg?
97
+ v = parseFloat(v.toString());
98
+ } else {
99
+ v = Number(v);
100
+ }
101
+
102
+ return { v, len };
103
+ } catch (err) {
104
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
105
+ }
106
+ }
107
+ };
@@ -0,0 +1,102 @@
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's based on BigInt native JavaScript type to completely support
26
+ // 64 bits integers, but for backward compatibility with current GraphSON
27
+ // implementation we do not use this for now. Consider its removal.
28
+ module.exports = class LongSerializerNg {
29
+ constructor(ioc) {
30
+ this.ioc = ioc;
31
+ // this.ioc.serializers[ioc.DataType.LONG] = this; // disabled, see AnySerializer.deserialize()
32
+ }
33
+
34
+ canBeUsedFor() {
35
+ // it's not expected to be used
36
+ }
37
+
38
+ serialize(item, fullyQualifiedFormat = true) {
39
+ if (item === undefined || item === null) {
40
+ if (fullyQualifiedFormat) {
41
+ return Buffer.from([this.ioc.DataType.LONG, 0x01]);
42
+ }
43
+ return Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
44
+ }
45
+
46
+ const bufs = [];
47
+ if (fullyQualifiedFormat) {
48
+ bufs.push(Buffer.from([this.ioc.DataType.LONG, 0x00]));
49
+ }
50
+ const v = Buffer.alloc(8);
51
+ v.writeBigInt64BE(BigInt(item));
52
+ bufs.push(v);
53
+
54
+ return Buffer.concat(bufs);
55
+ }
56
+
57
+ deserialize(buffer, fullyQualifiedFormat = true) {
58
+ let len = 0;
59
+ let cursor = buffer;
60
+
61
+ try {
62
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
63
+ throw new Error('buffer is missing');
64
+ }
65
+ if (buffer.length < 1) {
66
+ throw new Error('buffer is empty');
67
+ }
68
+
69
+ if (fullyQualifiedFormat) {
70
+ const type_code = cursor.readUInt8();
71
+ len++;
72
+ if (type_code !== this.ioc.DataType.LONG) {
73
+ throw new Error('unexpected {type_code}');
74
+ }
75
+ cursor = cursor.slice(1);
76
+
77
+ if (cursor.length < 1) {
78
+ throw new Error('{value_flag} is missing');
79
+ }
80
+ const value_flag = cursor.readUInt8();
81
+ len++;
82
+ if (value_flag === 1) {
83
+ return { v: null, len };
84
+ }
85
+ if (value_flag !== 0) {
86
+ throw new Error('unexpected {value_flag}');
87
+ }
88
+ cursor = cursor.slice(1);
89
+ }
90
+
91
+ if (cursor.length < 8) {
92
+ throw new Error('unexpected {value} length');
93
+ }
94
+ len += 8;
95
+
96
+ const v = cursor.readBigInt64BE();
97
+ return { v, len };
98
+ } catch (err) {
99
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
100
+ }
101
+ }
102
+ };
@@ -0,0 +1,153 @@
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 MapSerializer {
26
+ constructor(ioc) {
27
+ this.ioc = ioc;
28
+ this.ioc.serializers[ioc.DataType.MAP] = this;
29
+ }
30
+
31
+ canBeUsedFor(value) {
32
+ if (value === null || value === undefined) {
33
+ return false;
34
+ }
35
+ if (value instanceof Map) {
36
+ return true;
37
+ }
38
+ if (Array.isArray(value)) {
39
+ return false;
40
+ }
41
+
42
+ return typeof value === 'object';
43
+ }
44
+
45
+ serialize(item, fullyQualifiedFormat = true) {
46
+ if (item === undefined || item === null) {
47
+ if (fullyQualifiedFormat) {
48
+ return Buffer.from([this.ioc.DataType.MAP, 0x01]);
49
+ }
50
+ return this.ioc.intSerializer.serialize(0, false);
51
+ }
52
+
53
+ const isMap = item instanceof Map;
54
+
55
+ const keys = isMap ? Array.from(item.keys()) : Object.keys(item);
56
+ let map_length = keys.length;
57
+ if (map_length < 0) {
58
+ map_length = 0;
59
+ } else if (map_length > this.ioc.intSerializer.INT32_MAX) {
60
+ map_length = this.ioc.intSerializer.INT32_MAX; // TODO: is it expected to be silenced? or it's better to error instead?
61
+ }
62
+
63
+ const bufs = [];
64
+ if (fullyQualifiedFormat) {
65
+ bufs.push(Buffer.from([this.ioc.DataType.MAP, 0x00]));
66
+ }
67
+ bufs.push(this.ioc.intSerializer.serialize(map_length, false));
68
+ for (let i = 0; i < map_length; i++) {
69
+ const key = keys[i];
70
+ const value = isMap ? item.get(key) : item[key];
71
+ bufs.push(this.ioc.anySerializer.serialize(key), this.ioc.anySerializer.serialize(value));
72
+ }
73
+ return Buffer.concat(bufs);
74
+ }
75
+
76
+ deserialize(buffer, fullyQualifiedFormat = true) {
77
+ let len = 0;
78
+ let cursor = buffer;
79
+
80
+ try {
81
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
82
+ throw new Error('buffer is missing');
83
+ }
84
+ if (buffer.length < 1) {
85
+ throw new Error('buffer is empty');
86
+ }
87
+
88
+ if (fullyQualifiedFormat) {
89
+ const type_code = cursor.readUInt8();
90
+ len++;
91
+ if (type_code !== this.ioc.DataType.MAP) {
92
+ throw new Error('unexpected {type_code}');
93
+ }
94
+ cursor = cursor.slice(1);
95
+
96
+ if (cursor.length < 1) {
97
+ throw new Error('{value_flag} is missing');
98
+ }
99
+ const value_flag = cursor.readUInt8();
100
+ len++;
101
+ if (value_flag === 1) {
102
+ return { v: null, len };
103
+ }
104
+ if (value_flag !== 0) {
105
+ throw new Error('unexpected {value_flag}');
106
+ }
107
+ cursor = cursor.slice(1);
108
+ }
109
+
110
+ let length, length_len;
111
+ try {
112
+ ({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
113
+ len += length_len;
114
+ } catch (err) {
115
+ err.message = '{length}: ' + err.message;
116
+ throw err;
117
+ }
118
+ if (length < 0) {
119
+ throw new Error('{length} is less than zero');
120
+ }
121
+ cursor = cursor.slice(length_len);
122
+
123
+ const v = new Map();
124
+ for (let i = 0; i < length; i++) {
125
+ let key, key_len;
126
+ try {
127
+ ({ v: key, len: key_len } = this.ioc.anySerializer.deserialize(cursor));
128
+ len += key_len;
129
+ } catch (err) {
130
+ err.message = `{item_${i}} key: ` + err.message;
131
+ throw err;
132
+ }
133
+ cursor = cursor.slice(key_len);
134
+
135
+ let value, value_len;
136
+ try {
137
+ ({ v: value, len: value_len } = this.ioc.anySerializer.deserialize(cursor));
138
+ len += value_len;
139
+ } catch (err) {
140
+ err.message = `{item_${i}} value: ` + err.message;
141
+ throw err;
142
+ }
143
+ cursor = cursor.slice(value_len);
144
+
145
+ v.set(key, value);
146
+ }
147
+
148
+ return { v, len };
149
+ } catch (err) {
150
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
151
+ }
152
+ }
153
+ };
@@ -0,0 +1,68 @@
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
+ // Based on GraphSON NumberSerializer.serialize().
26
+ // It's tested by AnySerializer.serialize() tests.
27
+ module.exports = class NumberSerializationStrategy {
28
+ constructor(ioc) {
29
+ this.ioc = ioc;
30
+ }
31
+
32
+ canBeUsedFor(value) {
33
+ if (Number.isNaN(value) || value === Number.POSITIVE_INFINITY || value === Number.NEGATIVE_INFINITY) {
34
+ return true;
35
+ }
36
+ if (typeof value === 'number') {
37
+ return true;
38
+ }
39
+ if (typeof value === 'bigint') {
40
+ return true;
41
+ }
42
+
43
+ return false;
44
+ }
45
+
46
+ serialize(item, fullyQualifiedFormat = true) {
47
+ if (typeof item === 'number') {
48
+ if (
49
+ Number.isNaN(item) ||
50
+ item === Number.POSITIVE_INFINITY ||
51
+ item === Number.NEGATIVE_INFINITY ||
52
+ !Number.isInteger(item)
53
+ ) {
54
+ return this.ioc.doubleSerializer.serialize(item, fullyQualifiedFormat);
55
+ }
56
+
57
+ if (item >= -2147483648 && item <= 2147483647) {
58
+ // INT32_MIN/MAX
59
+ return this.ioc.intSerializer.serialize(item, fullyQualifiedFormat);
60
+ }
61
+ return this.ioc.longSerializer.serialize(item, fullyQualifiedFormat);
62
+ }
63
+
64
+ if (typeof item === 'bigint') {
65
+ return this.ioc.bigIntegerSerializer.serialize(item, fullyQualifiedFormat);
66
+ }
67
+ }
68
+ };