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.
Files changed (105) hide show
  1. package/.eslintrc.js +3 -2
  2. package/doc/AnonymousTraversalSource.html +2 -2
  3. package/doc/Authenticator.html +2 -2
  4. package/doc/Bytecode.html +2 -2
  5. package/doc/Client.html +2 -2
  6. package/doc/Connection.html +10 -10
  7. package/doc/DriverRemoteConnection.html +2 -2
  8. package/doc/EdgeLabelVerificationStrategy.html +2 -2
  9. package/doc/Graph.html +2 -2
  10. package/doc/GraphSON2Reader.html +2 -2
  11. package/doc/GraphSON2Writer.html +2 -2
  12. package/doc/GraphSON3Reader.html +2 -2
  13. package/doc/GraphSON3Writer.html +2 -2
  14. package/doc/GraphTraversal.html +2 -2
  15. package/doc/GraphTraversalSource.html +2 -2
  16. package/doc/HaltedTraverserStrategy.html +2 -2
  17. package/doc/MatchAlgorithmStrategy.html +2 -2
  18. package/doc/P.html +2 -2
  19. package/doc/PartitionStrategy.html +2 -2
  20. package/doc/Path.html +2 -2
  21. package/doc/PlainTextSaslAuthenticator.html +2 -2
  22. package/doc/ProductiveByStrategy.html +2 -2
  23. package/doc/RemoteConnection.html +2 -2
  24. package/doc/RemoteStrategy.html +2 -2
  25. package/doc/RemoteTraversal.html +2 -2
  26. package/doc/ReservedKeysVerificationStrategy.html +2 -2
  27. package/doc/ResponseError.html +2 -2
  28. package/doc/ResultSet.html +2 -2
  29. package/doc/SaslAuthenticator.html +2 -2
  30. package/doc/SaslMechanismBase.html +2 -2
  31. package/doc/SaslMechanismPlain.html +2 -2
  32. package/doc/SeedStrategy.html +2 -2
  33. package/doc/SubgraphStrategy.html +2 -2
  34. package/doc/TextP.html +2 -2
  35. package/doc/Transaction.html +2 -2
  36. package/doc/Translator.html +2 -2
  37. package/doc/TraversalStrategies.html +2 -2
  38. package/doc/TraversalStrategy.html +2 -2
  39. package/doc/TypeSerializer.html +2 -2
  40. package/doc/driver_auth_authenticator.js.html +2 -2
  41. package/doc/driver_auth_mechanisms_sasl-mechanism-base.js.html +2 -2
  42. package/doc/driver_auth_mechanisms_sasl-mechanism-plain.js.html +2 -2
  43. package/doc/driver_auth_plain-text-sasl-authenticator.js.html +2 -2
  44. package/doc/driver_auth_sasl-authenticator.js.html +2 -2
  45. package/doc/driver_client.js.html +2 -2
  46. package/doc/driver_connection.js.html +32 -9
  47. package/doc/driver_driver-remote-connection.js.html +2 -2
  48. package/doc/driver_remote-connection.js.html +2 -2
  49. package/doc/driver_response-error.js.html +2 -2
  50. package/doc/driver_result-set.js.html +2 -2
  51. package/doc/global.html +66 -2
  52. package/doc/index.html +2 -2
  53. package/doc/module.exports.html +301 -0
  54. package/doc/process_anonymous-traversal.js.html +2 -2
  55. package/doc/process_bytecode.js.html +2 -2
  56. package/doc/process_graph-traversal.js.html +2 -2
  57. package/doc/process_transaction.js.html +2 -2
  58. package/doc/process_translator.js.html +2 -2
  59. package/doc/process_traversal-strategy.js.html +3 -3
  60. package/doc/process_traversal.js.html +2 -2
  61. package/doc/structure_graph.js.html +2 -2
  62. package/doc/structure_io_binary_internals_DataType.js.html +148 -0
  63. package/doc/structure_io_binary_internals_GraphBinaryReader.js.html +131 -0
  64. package/doc/structure_io_binary_internals_GraphBinaryWriter.js.html +134 -0
  65. package/doc/structure_io_graph-serializer.js.html +2 -2
  66. package/doc/structure_io_type-serializers.js.html +2 -2
  67. package/lib/driver/connection.js +30 -7
  68. package/lib/process/traversal-strategy.js +1 -1
  69. package/lib/structure/io/binary/GraphBinary.js +109 -0
  70. package/lib/structure/io/binary/internals/AnySerializer.js +95 -0
  71. package/lib/structure/io/binary/internals/ArraySerializer.js +135 -0
  72. package/lib/structure/io/binary/internals/BigIntegerSerializer.js +151 -0
  73. package/lib/structure/io/binary/internals/BooleanSerializer.js +103 -0
  74. package/lib/structure/io/binary/internals/BulkSetSerializer.js +124 -0
  75. package/lib/structure/io/binary/internals/ByteBufferSerializer.js +124 -0
  76. package/lib/structure/io/binary/internals/ByteSerializer.js +95 -0
  77. package/lib/structure/io/binary/internals/BytecodeSerializer.js +249 -0
  78. package/lib/structure/io/binary/internals/DataType.js +97 -0
  79. package/lib/structure/io/binary/internals/DateSerializer.js +103 -0
  80. package/lib/structure/io/binary/internals/DoubleSerializer.js +99 -0
  81. package/lib/structure/io/binary/internals/EdgeSerializer.js +215 -0
  82. package/lib/structure/io/binary/internals/EnumSerializer.js +148 -0
  83. package/lib/structure/io/binary/internals/FloatSerializer.js +99 -0
  84. package/lib/structure/io/binary/internals/GraphBinaryReader.js +80 -0
  85. package/lib/structure/io/binary/internals/GraphBinaryWriter.js +83 -0
  86. package/lib/structure/io/binary/internals/IntSerializer.js +112 -0
  87. package/lib/structure/io/binary/internals/LambdaSerializer.js +70 -0
  88. package/lib/structure/io/binary/internals/LongSerializer.js +107 -0
  89. package/lib/structure/io/binary/internals/LongSerializerNg.js +102 -0
  90. package/lib/structure/io/binary/internals/MapSerializer.js +153 -0
  91. package/lib/structure/io/binary/internals/NumberSerializationStrategy.js +68 -0
  92. package/lib/structure/io/binary/internals/PSerializer.js +151 -0
  93. package/lib/structure/io/binary/internals/PathSerializer.js +123 -0
  94. package/lib/structure/io/binary/internals/PropertySerializer.js +141 -0
  95. package/lib/structure/io/binary/internals/ShortSerializer.js +99 -0
  96. package/lib/structure/io/binary/internals/StringSerializer.js +114 -0
  97. package/lib/structure/io/binary/internals/TextPSerializer.js +145 -0
  98. package/lib/structure/io/binary/internals/TraversalStrategySerializer.js +72 -0
  99. package/lib/structure/io/binary/internals/TraverserSerializer.js +123 -0
  100. package/lib/structure/io/binary/internals/UnspecifiedNullSerializer.js +75 -0
  101. package/lib/structure/io/binary/internals/UuidSerializer.js +119 -0
  102. package/lib/structure/io/binary/internals/VertexPropertySerializer.js +172 -0
  103. package/lib/structure/io/binary/internals/VertexSerializer.js +135 -0
  104. package/lib/structure/io/binary/internals/utils.js +59 -0
  105. package/package.json +14 -7
@@ -0,0 +1,80 @@
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
+ /**
26
+ * GraphBinary reader.
27
+ */
28
+ module.exports = class GraphBinaryReader {
29
+ constructor(ioc) {
30
+ this.ioc = ioc;
31
+ }
32
+
33
+ readResponse(buffer) {
34
+ if (buffer === undefined || buffer === null) {
35
+ throw new Error('Buffer is missing.');
36
+ }
37
+ if (!(buffer instanceof Buffer)) {
38
+ throw new Error('Not an instance of Buffer.');
39
+ }
40
+ if (buffer.length < 1) {
41
+ throw new Error('Buffer is empty.');
42
+ }
43
+
44
+ const response = { status: {}, result: {} };
45
+ let cursor = buffer;
46
+ let len;
47
+
48
+ // {version} is a Byte representing the protocol version
49
+ const version = cursor[0];
50
+ if (version !== 0x81) {
51
+ throw new Error(`Unsupported version '${version}'.`);
52
+ }
53
+ cursor = cursor.slice(1); // skip version
54
+
55
+ // {request_id} is a nullable UUID
56
+ ({ v: response.requestId, len } = this.ioc.uuidSerializer.deserialize(cursor, false, true));
57
+ cursor = cursor.slice(len);
58
+
59
+ // {status_code} is an Int
60
+ ({ v: response.status.code, len } = this.ioc.intSerializer.deserialize(cursor, false));
61
+ cursor = cursor.slice(len);
62
+
63
+ // {status_message} is a nullable String
64
+ ({ v: response.status.message, len } = this.ioc.stringSerializer.deserialize(cursor, false, true));
65
+ cursor = cursor.slice(len);
66
+
67
+ // {status_attributes} is a Map
68
+ ({ v: response.status.attributes, len } = this.ioc.mapSerializer.deserialize(cursor, false));
69
+ cursor = cursor.slice(len);
70
+
71
+ // {result_meta} is a Map
72
+ ({ v: response.result.meta, len } = this.ioc.mapSerializer.deserialize(cursor, false));
73
+ cursor = cursor.slice(len);
74
+
75
+ // {result_data} is a fully qualified typed value composed of {type_code}{type_info}{value_flag}{value}
76
+ ({ v: response.result.data } = this.ioc.anySerializer.deserialize(cursor));
77
+
78
+ return response;
79
+ }
80
+ };
@@ -0,0 +1,83 @@
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
+ /**
26
+ * GraphBinary writer.
27
+ */
28
+ module.exports = class GraphBinaryWriter {
29
+ constructor(ioc) {
30
+ this.ioc = ioc;
31
+ }
32
+
33
+ writeRequest({ requestId, op, processor, args }) {
34
+ const bufs = [
35
+ // {version} 1 byte
36
+ Buffer.from([0x81]),
37
+ // {request_id} UUID
38
+ this.ioc.uuidSerializer.serialize(requestId, false),
39
+ // {op} String
40
+ this.ioc.stringSerializer.serialize(op, false),
41
+ // {processor} String
42
+ this.ioc.stringSerializer.serialize(processor, false),
43
+ // {args} Map
44
+ this.ioc.mapSerializer.serialize(args, false),
45
+ ];
46
+ return Buffer.concat(bufs);
47
+
48
+ /*// Detailed example for a quick reference:
49
+ // {version}
50
+ 0x81,
51
+ // {request_id} UUID
52
+ 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
53
+ // {op} String
54
+ 0x00,0x00,0x00,0x08, ...Buffer.from('bytecode'),
55
+ // {processor} String
56
+ 0x00,0x00,0x00,0x09, ...Buffer.from('traversal'),
57
+ // {args} Map
58
+ 0x00,0x00,0x00,0x02,
59
+ // 1.
60
+ // args.aliases key String
61
+ 0x03,0x00, 0x00,0x00,0x00,0x07, ...Buffer.from('aliases'),
62
+ // args.aliases value Map
63
+ 0x0A,0x00, 0x00,0x00,0x00,0x01,
64
+ // aliases.g key String
65
+ 0x03,0x00, 0x00,0x00,0x00,0x01, ...Buffer.from('g'),
66
+ // aliases.g value String
67
+ 0x03,0x00, 0x00,0x00,0x00,0x01, ...Buffer.from('g'),
68
+ // 2.
69
+ // args.gremlin key String
70
+ 0x03,0x00, 0x00,0x00,0x00,0x07, ...Buffer.from('gremlin'),
71
+ // args.gremlin value Bytecode
72
+ 0x15,0x00,
73
+ // {steps_length}
74
+ 0x00,0x00,0x00,0x01,
75
+ // step 1 - {name} String
76
+ 0x00,0x00,0x00,0x01, ...Buffer.from('V'),
77
+ // step 1 - {values_length} Int
78
+ 0x00,0x00,0x00,0x00,
79
+ // {sources_length}
80
+ 0x00,0x00,0x00,0x00,
81
+ */
82
+ }
83
+ };
@@ -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
+ };