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,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
+ };
@@ -0,0 +1,124 @@
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 BulkSetSerializer {
26
+ constructor(ioc) {
27
+ this.ioc = ioc;
28
+ this.ioc.serializers[ioc.DataType.BULKSET] = this;
29
+ }
30
+
31
+ deserialize(buffer, fullyQualifiedFormat = true) {
32
+ let len = 0;
33
+ let cursor = buffer;
34
+
35
+ try {
36
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
37
+ throw new Error('buffer is missing');
38
+ }
39
+ if (buffer.length < 1) {
40
+ throw new Error('buffer is empty');
41
+ }
42
+
43
+ if (fullyQualifiedFormat) {
44
+ const type_code = cursor.readUInt8();
45
+ len++;
46
+ if (type_code !== this.ioc.DataType.BULKSET) {
47
+ throw new Error('unexpected {type_code}');
48
+ }
49
+ cursor = cursor.slice(1);
50
+
51
+ if (cursor.length < 1) {
52
+ throw new Error('{value_flag} is missing');
53
+ }
54
+ const value_flag = cursor.readUInt8();
55
+ len++;
56
+ if (value_flag === 1) {
57
+ return { v: null, len };
58
+ }
59
+ if (value_flag !== 0) {
60
+ throw new Error('unexpected {value_flag}');
61
+ }
62
+ cursor = cursor.slice(1);
63
+ }
64
+
65
+ // {length}
66
+ let length, length_len;
67
+ try {
68
+ ({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
69
+ len += length_len;
70
+ } catch (err) {
71
+ err.message = '{length}: ' + err.message;
72
+ throw err;
73
+ }
74
+ if (length < 0) {
75
+ throw new Error('{length} is less than zero');
76
+ }
77
+ cursor = cursor.slice(length_len);
78
+
79
+ // Official GraphBinary 1.0 spec:
80
+ // If the implementing language does not have a BulkSet object to deserialize into,
81
+ // this format can be coerced to a List and still be considered compliant with Gremlin.
82
+ // Simply "expand the bulk" by adding the item to the List the number of times specified by the bulk.
83
+
84
+ // {item_0}...{item_n}
85
+ let v = new Array();
86
+ for (let i = 0; i < length; i++) {
87
+ let value, value_len;
88
+ try {
89
+ ({ v: value, len: value_len } = this.ioc.anySerializer.deserialize(cursor));
90
+ len += value_len;
91
+ } catch (err) {
92
+ err.message = `{item_${i}} value: ` + err.message;
93
+ throw err;
94
+ }
95
+ cursor = cursor.slice(value_len);
96
+
97
+ let bulk, bulk_len;
98
+ try {
99
+ ({ v: bulk, len: bulk_len } = this.ioc.longSerializer.deserialize(cursor, false));
100
+ len += bulk_len;
101
+ } catch (err) {
102
+ err.message = `{item_${i}} bulk: ` + err.message;
103
+ throw err;
104
+ }
105
+ if (bulk < 0) {
106
+ throw new Error(`{item_${i}}: bulk is less than zero`);
107
+ }
108
+ if (bulk > 4294967295) {
109
+ // arrayLength of Array() constructor is expected to be an integer between 0 and 2^32 - 1
110
+ throw new Error(`{item_${i}}: bulk is greater than 2^32-1`);
111
+ }
112
+ cursor = cursor.slice(bulk_len);
113
+
114
+ bulk = Number(bulk); // coersion to Number is required for BigInt if LongSerializerNg is used
115
+ const item = new Array(bulk).fill(value);
116
+ v = v.concat(item);
117
+ }
118
+
119
+ return { v, len };
120
+ } catch (err) {
121
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
122
+ }
123
+ }
124
+ };
@@ -0,0 +1,124 @@
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 ByteBufferSerializer {
26
+ constructor(ioc) {
27
+ this.ioc = ioc;
28
+ this.ioc.serializers[ioc.DataType.BYTEBUFFER] = this;
29
+ }
30
+
31
+ canBeUsedFor(value) {
32
+ return value instanceof Buffer;
33
+ }
34
+
35
+ serialize(item, fullyQualifiedFormat = true) {
36
+ if (item === undefined || item === null) {
37
+ if (fullyQualifiedFormat) {
38
+ return Buffer.from([this.ioc.DataType.BYTEBUFFER, 0x01]);
39
+ }
40
+ return Buffer.from([0x00, 0x00, 0x00, 0x00]); // {length} = 0
41
+ }
42
+
43
+ const bufs = [];
44
+ if (fullyQualifiedFormat) {
45
+ bufs.push(Buffer.from([this.ioc.DataType.BYTEBUFFER, 0x00]));
46
+ }
47
+
48
+ // {length}
49
+ let length = item.length;
50
+ if (length < 0) {
51
+ length = 0; // TODO: test it
52
+ }
53
+ if (length > this.ioc.intSerializer.INT32_MAX) {
54
+ throw new Error(
55
+ `Buffer length=${length} is greater than supported max_length=${this.ioc.intSerializer.INT32_MAX}.`,
56
+ );
57
+ }
58
+ bufs.push(this.ioc.intSerializer.serialize(length, false));
59
+
60
+ // {value} sequence of bytes
61
+ bufs.push(item);
62
+
63
+ return Buffer.concat(bufs);
64
+ }
65
+
66
+ deserialize(buffer, fullyQualifiedFormat = true) {
67
+ let len = 0;
68
+ let cursor = buffer;
69
+
70
+ try {
71
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
72
+ throw new Error('buffer is missing');
73
+ }
74
+ if (buffer.length < 1) {
75
+ throw new Error('buffer is empty');
76
+ }
77
+
78
+ if (fullyQualifiedFormat) {
79
+ const type_code = cursor.readUInt8();
80
+ len++;
81
+ if (type_code !== this.ioc.DataType.BYTEBUFFER) {
82
+ throw new Error('unexpected {type_code}');
83
+ }
84
+ cursor = cursor.slice(1);
85
+
86
+ if (cursor.length < 1) {
87
+ throw new Error('{value_flag} is missing');
88
+ }
89
+ const value_flag = cursor.readUInt8();
90
+ len++;
91
+ if (value_flag === 1) {
92
+ return { v: null, len };
93
+ }
94
+ if (value_flag !== 0) {
95
+ throw new Error('unexpected {value_flag}');
96
+ }
97
+ cursor = cursor.slice(1);
98
+ }
99
+
100
+ let length, length_len;
101
+ try {
102
+ ({ v: length, len: length_len } = this.ioc.intSerializer.deserialize(cursor, false));
103
+ len += length_len;
104
+ } catch (err) {
105
+ err.message = '{length}: ' + err.message;
106
+ throw err;
107
+ }
108
+ if (length < 0) {
109
+ throw new Error('{length} is less than zero');
110
+ }
111
+ cursor = cursor.slice(length_len);
112
+
113
+ if (length !== cursor.length) {
114
+ throw new Error(`{value}: unexpected actual {value} length=${cursor.length} when {length}=${length}`);
115
+ }
116
+ const v = cursor.slice(0, length);
117
+ len += length;
118
+
119
+ return { v, len };
120
+ } catch (err) {
121
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
122
+ }
123
+ }
124
+ };
@@ -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 ByteSerializer {
26
+ constructor(ioc) {
27
+ this.ioc = ioc;
28
+ this.ioc.serializers[ioc.DataType.BYTE] = this;
29
+ }
30
+
31
+ serialize(item, fullyQualifiedFormat = true) {
32
+ if (item === undefined || item === null) {
33
+ if (fullyQualifiedFormat) {
34
+ return Buffer.from([this.ioc.DataType.BYTE, 0x01]);
35
+ }
36
+ return Buffer.from([0x00]);
37
+ }
38
+
39
+ const bufs = [];
40
+ if (fullyQualifiedFormat) {
41
+ bufs.push(Buffer.from([this.ioc.DataType.BYTE, 0x00]));
42
+ }
43
+ const v = Buffer.alloc(1);
44
+ v.writeUInt8(item); // if item is not within uint8 limits writeUInt8 will error
45
+ bufs.push(v);
46
+
47
+ return Buffer.concat(bufs);
48
+ }
49
+
50
+ deserialize(buffer, fullyQualifiedFormat = true) {
51
+ let len = 0;
52
+ let cursor = buffer;
53
+
54
+ try {
55
+ if (buffer === undefined || buffer === null || !(buffer instanceof Buffer)) {
56
+ throw new Error('buffer is missing');
57
+ }
58
+ if (buffer.length < 1) {
59
+ throw new Error('buffer is empty');
60
+ }
61
+
62
+ if (fullyQualifiedFormat) {
63
+ const type_code = cursor.readUInt8();
64
+ len++;
65
+ if (type_code !== this.ioc.DataType.BYTE) {
66
+ throw new Error('unexpected {type_code}');
67
+ }
68
+ cursor = cursor.slice(1);
69
+
70
+ if (cursor.length < 1) {
71
+ throw new Error('{value_flag} is missing');
72
+ }
73
+ const value_flag = cursor.readUInt8();
74
+ len++;
75
+ if (value_flag === 1) {
76
+ return { v: null, len };
77
+ }
78
+ if (value_flag !== 0) {
79
+ throw new Error('unexpected {value_flag}');
80
+ }
81
+ cursor = cursor.slice(1);
82
+ }
83
+
84
+ if (cursor.length < 1) {
85
+ throw new Error('unexpected {value} length');
86
+ }
87
+ len += 1;
88
+
89
+ const v = cursor.readUInt8();
90
+ return { v, len };
91
+ } catch (err) {
92
+ throw this.ioc.utils.des_error({ serializer: this, args: arguments, cursor, err });
93
+ }
94
+ }
95
+ };