infinispan 0.13.0 → 0.15.0
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/.githooks/commit-msg +9 -0
- package/.githooks/configure-hooks +7 -0
- package/.githooks/configure-hooks.bat +15 -0
- package/.gitmessage +7 -0
- package/AI-CODE.md +86 -0
- package/README.md +52 -80
- package/lib/codec.js +396 -28
- package/lib/functional.js +65 -7
- package/lib/infinispan.js +591 -69
- package/lib/io.js +402 -44
- package/lib/listeners.js +166 -5
- package/lib/near-cache.js +99 -0
- package/lib/protocols.js +580 -82
- package/lib/sasl/digest.js +22 -17
- package/lib/sasl/external.js +7 -4
- package/lib/sasl/factory.js +6 -5
- package/lib/sasl/oauthbearer.js +7 -5
- package/lib/sasl/plain.js +6 -4
- package/lib/sasl/scram.js +19 -11
- package/lib/uri.js +206 -0
- package/lib/utils.js +89 -24
- package/package.json +10 -4
- package/server/.keep +0 -0
- package/types/index.d.ts +236 -1
package/lib/codec.js
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
exports.encodeBytesWithLength = f.lift(doEncodeBytesWithLength, _.identity);
|
|
23
23
|
exports.encodeString = f.lift(doEncodeString, _.identity);
|
|
24
24
|
exports.encodeSignedInt = f.lift(doEncodeSignedInt, _.identity);
|
|
25
|
+
exports.encodeLong = f.lift(doEncodeLong, _.identity);
|
|
25
26
|
exports.encodeProtobuf = f.lift(doEncodeProtobuf, _.identity);
|
|
26
27
|
exports.encodeQuery = f.lift(doEncodeQuery,_.identity);
|
|
27
28
|
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
exports.allDecoded = function(expectedNumEntries) {
|
|
46
47
|
return function(values) {
|
|
47
48
|
if (values.length < expectedNumEntries) {
|
|
48
|
-
logger.tracef(
|
|
49
|
+
logger.tracef('Not enough to read (not array): %s', values);
|
|
49
50
|
return undefined;
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -63,56 +64,151 @@
|
|
|
63
64
|
return bytes;
|
|
64
65
|
};
|
|
65
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Encode an unsigned byte into the buffer.
|
|
69
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
70
|
+
* @param {number} num - The unsigned byte value to encode.
|
|
71
|
+
* @returns {number} The new buffer offset after writing.
|
|
72
|
+
*/
|
|
66
73
|
function doEncodeUByte(bytebuf, num) {
|
|
67
74
|
return _.compose(updateEncOffset(bytebuf), checkedWriteUByte(bytebuf))(num);
|
|
68
75
|
}
|
|
69
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Encode a variable-length integer into the buffer.
|
|
79
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
80
|
+
* @param {number} num - The integer value to encode.
|
|
81
|
+
* @returns {number} The new buffer offset after writing.
|
|
82
|
+
*/
|
|
70
83
|
function doEncodeVInt(bytebuf, num) {
|
|
71
84
|
return _.compose(updateEncOffset(bytebuf), checkedWriteVInt(bytebuf))(num);
|
|
72
85
|
}
|
|
73
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Encode a variable-length long into the buffer.
|
|
89
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
90
|
+
* @param {number} num - The long value to encode.
|
|
91
|
+
* @returns {number} The new buffer offset after writing.
|
|
92
|
+
*/
|
|
74
93
|
function doEncodeVLong(bytebuf, num) {
|
|
75
94
|
return _.compose(updateEncOffset(bytebuf), checkedWriteVLong(bytebuf))(num);
|
|
76
95
|
}
|
|
77
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Encode a JSON object into the buffer.
|
|
99
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
100
|
+
* @param {Object} obj - The object to JSON-encode.
|
|
101
|
+
* @param {Function} typeId - The type identifier resolver function.
|
|
102
|
+
* @returns {number} The new buffer offset after writing.
|
|
103
|
+
*/
|
|
78
104
|
function doEncodeJSON(bytebuf, obj, typeId) {
|
|
79
105
|
return _.compose(updateEncOffset(bytebuf), checkedWriteJSON(bytebuf, typeId))(obj);
|
|
80
106
|
}
|
|
81
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Encode a string into the buffer with a length prefix.
|
|
110
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
111
|
+
* @param {string} obj - The string to encode.
|
|
112
|
+
* @returns {number} The new buffer offset after writing.
|
|
113
|
+
*/
|
|
82
114
|
function doEncodeString(bytebuf, obj) {
|
|
83
115
|
return _.compose(updateEncOffset(bytebuf), checkedWriteString(bytebuf))(obj);
|
|
84
116
|
}
|
|
85
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Encode raw bytes into the buffer without a length prefix.
|
|
120
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
121
|
+
* @param {Buffer} bytes - The bytes to encode.
|
|
122
|
+
* @returns {number} The new buffer offset after writing.
|
|
123
|
+
*/
|
|
86
124
|
function doEncodeBytes(bytebuf, bytes) {
|
|
87
125
|
return _.compose(updateEncOffset(bytebuf), checkedWriteBytes(bytebuf))(bytes);
|
|
88
126
|
}
|
|
89
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Encode raw bytes into the buffer with a variable-length integer length prefix.
|
|
130
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
131
|
+
* @param {Buffer} bytes - The bytes to encode.
|
|
132
|
+
* @returns {number} The new buffer offset after writing.
|
|
133
|
+
*/
|
|
90
134
|
function doEncodeBytesWithLength(bytebuf, bytes) {
|
|
91
135
|
return _.compose(updateEncOffset(bytebuf), checkedWriteBytesWithLength(bytebuf))(bytes);
|
|
92
136
|
}
|
|
93
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Encode a signed integer using zigzag encoding into the buffer.
|
|
140
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
141
|
+
* @param {number} num - The signed integer to encode.
|
|
142
|
+
* @returns {number} The new buffer offset after writing.
|
|
143
|
+
*/
|
|
94
144
|
function doEncodeSignedInt(bytebuf, num) {
|
|
95
145
|
return _.compose(updateEncOffset(bytebuf), checkedWriteSignedInt(bytebuf), zigZag)(num);
|
|
96
146
|
}
|
|
97
147
|
|
|
148
|
+
/**
|
|
149
|
+
* Encode a signed 8-byte long integer into the buffer (big-endian).
|
|
150
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
151
|
+
* @param {number} num - The long value to encode (must be within safe integer range).
|
|
152
|
+
* @returns {number} The new buffer offset after writing.
|
|
153
|
+
*/
|
|
154
|
+
function doEncodeLong(bytebuf, num) {
|
|
155
|
+
bytebuf = bytebufOverflowProtect(bytebuf, 8);
|
|
156
|
+
var high = Math.floor(num / 4294967296);
|
|
157
|
+
var low = num >>> 0;
|
|
158
|
+
bytebuf.buf.writeInt32BE(high, bytebuf.offset);
|
|
159
|
+
bytebuf.buf.writeUInt32BE(low, bytebuf.offset + 4);
|
|
160
|
+
bytebuf.offset += 8;
|
|
161
|
+
return bytebuf.offset;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Encode a Protobuf message as length-prefixed bytes into the buffer.
|
|
166
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
167
|
+
* @param {Object} message - The Protobuf message to encode.
|
|
168
|
+
* @param {Function} typeId - The type identifier resolver function.
|
|
169
|
+
* @returns {number} The new buffer offset after writing.
|
|
170
|
+
*/
|
|
98
171
|
function doEncodeProtobuf(bytebuf, message, typeId) {
|
|
99
172
|
return doEncodeBytesWithLength(bytebuf,encodeProtobuf(message,typeId));
|
|
100
173
|
}
|
|
101
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Create a function that encodes an object using the given Protobuf root type.
|
|
177
|
+
* @param {Object} root - The Protobuf type used for encoding.
|
|
178
|
+
* @returns {Function} A function that encodes an object to a Protobuf byte array.
|
|
179
|
+
*/
|
|
102
180
|
function encodeProtobufInstance(root){
|
|
103
181
|
return function(obj){
|
|
104
182
|
return root.encode(obj).finish();
|
|
105
|
-
}
|
|
183
|
+
};
|
|
106
184
|
}
|
|
107
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Encode a message as a Protobuf WrappedMessage.
|
|
188
|
+
* @param {*} message - The message value to wrap and encode.
|
|
189
|
+
* @param {Function} typeId - The type identifier resolver function.
|
|
190
|
+
* @returns {Uint8Array} The encoded Protobuf bytes.
|
|
191
|
+
*/
|
|
108
192
|
function encodeProtobuf(message,typeId){
|
|
109
193
|
return _.compose(encodeProtobufInstance(WrappedMessage), createWrappedMessage)(message,typeId);
|
|
110
194
|
}
|
|
111
195
|
|
|
196
|
+
/**
|
|
197
|
+
* Encode a Protobuf QueryRequest into the buffer with a length prefix.
|
|
198
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
199
|
+
* @param {Object} query - The query request object to encode.
|
|
200
|
+
* @returns {number} The new buffer offset after writing.
|
|
201
|
+
*/
|
|
112
202
|
function doEncodeQuery(bytebuf,query){
|
|
113
203
|
return doEncodeBytesWithLength(bytebuf,encodeProtobufInstance(Query.QueryRequest)(query));
|
|
114
204
|
}
|
|
115
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Create a WrappedMessage object from a value based on its type.
|
|
208
|
+
* @param {*} message - The value to wrap (number, string, boolean, ArrayBuffer, or typed message).
|
|
209
|
+
* @param {Function} typeId - The type identifier resolver for typed messages.
|
|
210
|
+
* @returns {Object} The WrappedMessage descriptor object.
|
|
211
|
+
*/
|
|
116
212
|
function createWrappedMessage(message,typeId){
|
|
117
213
|
var wrappedMessage={};
|
|
118
214
|
if(_.isNumber(message)){
|
|
@@ -132,9 +228,14 @@
|
|
|
132
228
|
var messageTypeId = typeId(message.$type.fullName);
|
|
133
229
|
return f.merge(wrappedMessage,{'wrappedMessage':encodedMessage},{'wrappedTypeId':messageTypeId});
|
|
134
230
|
}
|
|
135
|
-
throw new Error(
|
|
231
|
+
throw new Error('Provide valid data types.');
|
|
136
232
|
}
|
|
137
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Apply zigzag encoding to a signed integer for variable-length encoding.
|
|
236
|
+
* @param {number} num - The signed integer to zigzag-encode.
|
|
237
|
+
* @returns {number} The zigzag-encoded value.
|
|
238
|
+
*/
|
|
138
239
|
function zigZag(num) {
|
|
139
240
|
return (num << 1) ^ (num >> 31);
|
|
140
241
|
}
|
|
@@ -143,64 +244,125 @@
|
|
|
143
244
|
var number = f.validator('must be a number', _.isNumber);
|
|
144
245
|
var positiveOrZero = f.validator('must be >= 0', f.greaterThan(-1));
|
|
145
246
|
var intTooBig = f.validator('must be less than 2^32', f.lessThan(Math.pow(2, 32)));
|
|
146
|
-
var shortTooBig = f.validator('must be less than 2^16', f.lessThan(Math.pow(2, 16)));
|
|
147
247
|
var longTooBig = f.validator('must be less than 2^53 (javascript safe integer limitation)',
|
|
148
248
|
f.lessThan(Math.pow(2, 53)));
|
|
149
249
|
var stringOrNullCheck = f.validator('must be a String or null', stringOrNull);
|
|
150
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Check if a value is a string or null/undefined.
|
|
253
|
+
* @param {*} x - The value to check.
|
|
254
|
+
* @returns {boolean} True if the value is a string or null/undefined.
|
|
255
|
+
*/
|
|
151
256
|
function stringOrNull(x) {
|
|
152
257
|
return !f.existy(x) || _.isString(x);
|
|
153
258
|
}
|
|
154
259
|
|
|
260
|
+
/**
|
|
261
|
+
* Create a validated unsigned byte writer for the buffer.
|
|
262
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
263
|
+
* @returns {Function} A function that validates and writes an unsigned byte.
|
|
264
|
+
*/
|
|
155
265
|
function checkedWriteUByte(bytebuf) {
|
|
156
266
|
return f.partial1(f.condition1(number, positiveOrZero), uncheckedWriteUByte(bytebuf));
|
|
157
267
|
}
|
|
158
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Create a validated variable-length integer writer for the buffer.
|
|
271
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
272
|
+
* @returns {Function} A function that validates and writes a variable-length integer.
|
|
273
|
+
*/
|
|
159
274
|
function checkedWriteVInt(bytebuf) {
|
|
160
275
|
return f.partial1(f.condition1(number, positiveOrZero, intTooBig), uncheckedWriteVNum(bytebuf));
|
|
161
276
|
}
|
|
162
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Create a validated variable-length long writer for the buffer.
|
|
280
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
281
|
+
* @returns {Function} A function that validates and writes a variable-length long.
|
|
282
|
+
*/
|
|
163
283
|
function checkedWriteVLong(bytebuf) {
|
|
164
284
|
return f.partial1(f.condition1(number, positiveOrZero, longTooBig), uncheckedWriteVNum(bytebuf));
|
|
165
285
|
}
|
|
166
286
|
|
|
287
|
+
/**
|
|
288
|
+
* Create a validated JSON writer for the buffer.
|
|
289
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
290
|
+
* @param {Function} typeId - The type identifier resolver function.
|
|
291
|
+
* @returns {Function} A function that validates and writes a JSON object.
|
|
292
|
+
*/
|
|
167
293
|
function checkedWriteJSON(bytebuf, typeId) {
|
|
168
294
|
return f.partial1(f.condition1(nullCheck), uncheckedWriteJSON(bytebuf, typeId));
|
|
169
295
|
}
|
|
170
296
|
|
|
297
|
+
/**
|
|
298
|
+
* Create a validated string writer for the buffer.
|
|
299
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
300
|
+
* @returns {Function} A function that validates and writes a string.
|
|
301
|
+
*/
|
|
171
302
|
function checkedWriteString(bytebuf) {
|
|
172
303
|
return f.partial1(f.condition1(stringOrNullCheck), uncheckedWriteString(bytebuf));
|
|
173
304
|
}
|
|
174
305
|
|
|
306
|
+
/**
|
|
307
|
+
* Create a validated raw bytes writer for the buffer.
|
|
308
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
309
|
+
* @returns {Function} A function that validates and writes raw bytes.
|
|
310
|
+
*/
|
|
175
311
|
function checkedWriteBytes(bytebuf) {
|
|
176
312
|
return f.partial1(f.condition1(nullCheck), uncheckedWriteBytes(bytebuf));
|
|
177
313
|
}
|
|
178
314
|
|
|
315
|
+
/**
|
|
316
|
+
* Create a validated length-prefixed bytes writer for the buffer.
|
|
317
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
318
|
+
* @returns {Function} A function that validates and writes length-prefixed bytes.
|
|
319
|
+
*/
|
|
179
320
|
function checkedWriteBytesWithLength(bytebuf) {
|
|
180
321
|
return f.partial1(f.condition1(nullCheck), uncheckedWriteBytesWithLength(bytebuf));
|
|
181
322
|
}
|
|
182
323
|
|
|
324
|
+
/**
|
|
325
|
+
* Create a validated signed integer writer for the buffer.
|
|
326
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
327
|
+
* @returns {Function} A function that validates and writes a signed integer.
|
|
328
|
+
*/
|
|
183
329
|
function checkedWriteSignedInt(bytebuf) {
|
|
184
330
|
return f.partial1(f.condition1(number, intTooBig), uncheckedWriteVNum(bytebuf));
|
|
185
331
|
}
|
|
186
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Create a function that updates the buffer's encoding offset.
|
|
335
|
+
* @param {Object} bytebuf - The byte buffer whose offset to update.
|
|
336
|
+
* @returns {Function} A function that sets the buffer offset and returns it.
|
|
337
|
+
*/
|
|
187
338
|
function updateEncOffset(bytebuf) {
|
|
188
339
|
return function(offset) {
|
|
189
340
|
bytebuf.offset = offset;
|
|
190
341
|
return offset;
|
|
191
|
-
}
|
|
342
|
+
};
|
|
192
343
|
}
|
|
193
344
|
|
|
345
|
+
/**
|
|
346
|
+
* Create an unchecked unsigned byte writer for the buffer.
|
|
347
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
348
|
+
* @returns {Function} A function that writes a single byte and returns the new offset.
|
|
349
|
+
*/
|
|
194
350
|
function uncheckedWriteUByte(bytebuf) {
|
|
195
351
|
return function(byte) {
|
|
196
352
|
bytebuf = bytebufOverflowProtect(bytebuf, 1);
|
|
197
353
|
// TODO: We can better control validation, e.g. no boundary check but double size, so try passing noAssert=true
|
|
198
354
|
bytebuf.buf.writeUInt8(byte, bytebuf.offset);
|
|
199
355
|
return bytebuf.offset + 1;
|
|
200
|
-
}
|
|
356
|
+
};
|
|
201
357
|
}
|
|
202
358
|
|
|
203
359
|
// TODO: Should create an ByteBuf type and move this logic there
|
|
360
|
+
/**
|
|
361
|
+
* Grow the byte buffer if there is not enough space for the given number of bytes.
|
|
362
|
+
* @param {Object} bytebuf - The byte buffer to protect from overflow.
|
|
363
|
+
* @param {number} maxSize - The maximum number of bytes needed.
|
|
364
|
+
* @returns {Object} The byte buffer, potentially with a larger underlying buffer.
|
|
365
|
+
*/
|
|
204
366
|
function bytebufOverflowProtect(bytebuf, maxSize) {
|
|
205
367
|
if (bytebuf.offset + maxSize > bytebuf.buf.length) {
|
|
206
368
|
var length = bytebuf.buf.length * 2;
|
|
@@ -215,6 +377,11 @@
|
|
|
215
377
|
return bytebuf;
|
|
216
378
|
}
|
|
217
379
|
|
|
380
|
+
/**
|
|
381
|
+
* Create an unchecked variable-length number writer for the buffer.
|
|
382
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
383
|
+
* @returns {Function} A function that writes a variable-length number and returns the new offset.
|
|
384
|
+
*/
|
|
218
385
|
function uncheckedWriteVNum(bytebuf) {
|
|
219
386
|
return function(num) {
|
|
220
387
|
// Resize if not enough space to fit the biggest of var nums.
|
|
@@ -226,24 +393,34 @@
|
|
|
226
393
|
|
|
227
394
|
while(num >= INT) {
|
|
228
395
|
bytebuf.buf.writeUInt8((num & 0xFF) | MSB, localOffset++);
|
|
229
|
-
num /= 128
|
|
396
|
+
num /= 128;
|
|
230
397
|
}
|
|
231
398
|
while(num & MSBALL) {
|
|
232
399
|
bytebuf.buf.writeUInt8((num & 0xFF) | MSB, localOffset++);
|
|
233
|
-
num >>>= 7
|
|
400
|
+
num >>>= 7;
|
|
234
401
|
}
|
|
235
402
|
bytebuf.buf.writeUInt8(num | 0, localOffset);
|
|
236
403
|
|
|
237
404
|
return localOffset + 1;
|
|
238
|
-
}
|
|
405
|
+
};
|
|
239
406
|
}
|
|
240
407
|
|
|
408
|
+
/**
|
|
409
|
+
* Create an unchecked JSON writer that serializes an object as a string into the buffer.
|
|
410
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
411
|
+
* @returns {Function} A function that writes a JSON-serialized object and returns the new offset.
|
|
412
|
+
*/
|
|
241
413
|
function uncheckedWriteJSON(bytebuf) {
|
|
242
414
|
return function(obj) {
|
|
243
415
|
return uncheckedWriteString(bytebuf)(JSON.stringify(obj));
|
|
244
|
-
}
|
|
416
|
+
};
|
|
245
417
|
}
|
|
246
418
|
|
|
419
|
+
/**
|
|
420
|
+
* Create an unchecked string writer that writes a length-prefixed string into the buffer.
|
|
421
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
422
|
+
* @returns {Function} A function that writes a string and returns the new offset.
|
|
423
|
+
*/
|
|
247
424
|
function uncheckedWriteString(bytebuf) {
|
|
248
425
|
return function(obj) {
|
|
249
426
|
var stringNumBytes = f.existy(obj) ? Buffer.byteLength(obj) : 0;
|
|
@@ -253,59 +430,110 @@
|
|
|
253
430
|
return bytebuf.buf.write(obj, offsetAfterBytes) + offsetAfterBytes;
|
|
254
431
|
}
|
|
255
432
|
return offsetAfterBytes;
|
|
256
|
-
}
|
|
433
|
+
};
|
|
257
434
|
}
|
|
258
435
|
|
|
436
|
+
/**
|
|
437
|
+
* Create an unchecked raw bytes writer for the buffer.
|
|
438
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
439
|
+
* @returns {Function} A function that writes raw bytes and returns the new offset.
|
|
440
|
+
*/
|
|
259
441
|
function uncheckedWriteBytes(bytebuf) {
|
|
260
442
|
return function(bytes) {
|
|
261
443
|
bytebuf = bytebufOverflowProtect(bytebuf, bytes.length);
|
|
262
444
|
var targetStart = bytebuf.offset;
|
|
263
445
|
bytes.copy(bytebuf.buf, targetStart);
|
|
264
446
|
return targetStart + bytes.length;
|
|
265
|
-
}
|
|
447
|
+
};
|
|
266
448
|
}
|
|
267
449
|
|
|
450
|
+
/**
|
|
451
|
+
* Create an unchecked length-prefixed bytes writer for the buffer.
|
|
452
|
+
* @param {Object} bytebuf - The byte buffer to write to.
|
|
453
|
+
* @returns {Function} A function that writes length-prefixed bytes and returns the new offset.
|
|
454
|
+
*/
|
|
268
455
|
function uncheckedWriteBytesWithLength(bytebuf) {
|
|
269
456
|
return function(bytes) {
|
|
270
457
|
var buffNumBytes = f.existy(bytes) ? Buffer.byteLength(bytes) : 0;
|
|
271
|
-
|
|
458
|
+
doEncodeVInt(bytebuf, buffNumBytes);
|
|
272
459
|
if (buffNumBytes > 0) {
|
|
273
460
|
bytebuf = bytebufOverflowProtect(bytebuf, bytes.length);
|
|
274
461
|
var targetStart = bytebuf.offset;
|
|
275
462
|
bytes.copy(bytebuf.buf, targetStart);
|
|
276
463
|
}
|
|
277
464
|
return targetStart + buffNumBytes;
|
|
278
|
-
}
|
|
465
|
+
};
|
|
279
466
|
}
|
|
280
467
|
|
|
468
|
+
/**
|
|
469
|
+
* Decode an unsigned byte from the buffer.
|
|
470
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
471
|
+
* @returns {number|undefined} The decoded unsigned byte, or undefined if not enough data.
|
|
472
|
+
*/
|
|
281
473
|
function doDecodeUByte(bytebuf) {
|
|
282
474
|
return uncheckedReadUByte(bytebuf)();
|
|
283
475
|
}
|
|
284
476
|
|
|
477
|
+
/**
|
|
478
|
+
* Decode a variable-length integer from the buffer.
|
|
479
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
480
|
+
* @returns {number|undefined} The decoded integer, or undefined if not enough data.
|
|
481
|
+
*/
|
|
285
482
|
function doDecodeVInt(bytebuf) {
|
|
286
483
|
return uncheckedReadVNum(bytebuf)();
|
|
287
484
|
}
|
|
288
485
|
|
|
486
|
+
/**
|
|
487
|
+
* Decode a variable-length long from the buffer.
|
|
488
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
489
|
+
* @returns {number|undefined} The decoded long, or undefined if not enough data.
|
|
490
|
+
*/
|
|
289
491
|
function doDecodeVLong(bytebuf) {
|
|
290
492
|
return uncheckedReadVNum(bytebuf)();
|
|
291
493
|
}
|
|
292
494
|
|
|
495
|
+
/**
|
|
496
|
+
* Decode a fixed-width 8-byte long from the buffer.
|
|
497
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
498
|
+
* @returns {number|undefined} The decoded long, or undefined if not enough data.
|
|
499
|
+
*/
|
|
293
500
|
function doDecodeLong(bytebuf) {
|
|
294
501
|
return uncheckedReadLong(bytebuf)();
|
|
295
502
|
}
|
|
296
503
|
|
|
504
|
+
/**
|
|
505
|
+
* Decode a JSON object from the buffer.
|
|
506
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
507
|
+
* @returns {Object} The decoded JSON object.
|
|
508
|
+
*/
|
|
297
509
|
function doDecodeJSON(bytebuf) {
|
|
298
510
|
return uncheckedReadJSON(bytebuf);
|
|
299
511
|
}
|
|
300
512
|
|
|
513
|
+
/**
|
|
514
|
+
* Decode a fixed number of bytes from the buffer.
|
|
515
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
516
|
+
* @param {number} num - The number of bytes to read.
|
|
517
|
+
* @returns {Buffer} The decoded bytes.
|
|
518
|
+
*/
|
|
301
519
|
function doDecodeFixedBytes(bytebuf, num) {
|
|
302
520
|
return uncheckedReadBytes(bytebuf)(num);
|
|
303
521
|
}
|
|
304
522
|
|
|
523
|
+
/**
|
|
524
|
+
* Decode a length-prefixed string from the buffer.
|
|
525
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
526
|
+
* @returns {string|undefined} The decoded string, or undefined if not enough data.
|
|
527
|
+
*/
|
|
305
528
|
function doDecodeString(bytebuf) {
|
|
306
529
|
return uncheckedReadString(bytebuf)();
|
|
307
530
|
}
|
|
308
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Decode a zigzag-encoded signed integer from the buffer.
|
|
534
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
535
|
+
* @returns {number|undefined} The decoded signed integer, or undefined if not enough data.
|
|
536
|
+
*/
|
|
309
537
|
function doDecodeSignedInt(bytebuf) {
|
|
310
538
|
var num = uncheckedReadVNum(bytebuf)();
|
|
311
539
|
if (!f.existy(num))
|
|
@@ -314,19 +542,43 @@
|
|
|
314
542
|
return (num & 1) == 0 ? num >>> 1 : ~(num >>> 1);
|
|
315
543
|
}
|
|
316
544
|
|
|
545
|
+
/**
|
|
546
|
+
* Decode a variable-length prefixed byte array from the buffer.
|
|
547
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
548
|
+
* @returns {Buffer} The decoded bytes.
|
|
549
|
+
*/
|
|
317
550
|
function doDecodeVariableBytes(bytebuf) {
|
|
318
551
|
return _.compose(uncheckedReadBytes(bytebuf), uncheckedReadVNum(bytebuf))();
|
|
319
552
|
}
|
|
320
553
|
|
|
554
|
+
/**
|
|
555
|
+
* Decode an unsigned 16-bit short from the buffer.
|
|
556
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
557
|
+
* @returns {number} The decoded short value.
|
|
558
|
+
*/
|
|
321
559
|
function doDecodeShort(bytebuf) {
|
|
322
560
|
return uncheckedReadShort(bytebuf)();
|
|
323
561
|
}
|
|
324
562
|
|
|
563
|
+
/**
|
|
564
|
+
* Decode a Protobuf WrappedMessage from the buffer and unwrap it.
|
|
565
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
566
|
+
* @param {Function} protostreamTypename - Resolver from type ID to type name.
|
|
567
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
568
|
+
* @returns {*} The unwrapped decoded value.
|
|
569
|
+
*/
|
|
325
570
|
function doDecodeProtobuf(bytebuf,protostreamTypename,root){
|
|
326
571
|
var wrappedObject = _.compose(decodeProtobufMessage(WrappedMessage),trimBytebuf)(bytebuf);
|
|
327
572
|
return unwrapWrappedMessage(wrappedObject,protostreamTypename,root);
|
|
328
573
|
}
|
|
329
574
|
|
|
575
|
+
/**
|
|
576
|
+
* Unwrap a WrappedMessage object into its underlying value based on the wrapped field type.
|
|
577
|
+
* @param {Object} wrappedObject - The decoded WrappedMessage Protobuf object.
|
|
578
|
+
* @param {Function} protostreamTypename - Resolver from type ID to type name.
|
|
579
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
580
|
+
* @returns {*} The unwrapped value (number, string, boolean, bytes, or decoded message).
|
|
581
|
+
*/
|
|
330
582
|
function unwrapWrappedMessage(wrappedObject,protostreamTypename,root){
|
|
331
583
|
switch(true){
|
|
332
584
|
case _.has(wrappedObject,'wrappedDouble'):
|
|
@@ -344,18 +596,35 @@
|
|
|
344
596
|
}
|
|
345
597
|
}
|
|
346
598
|
|
|
599
|
+
/**
|
|
600
|
+
* Create a function that decodes a Protobuf message using the given root type.
|
|
601
|
+
* @param {Object} root - The Protobuf type used for decoding.
|
|
602
|
+
* @returns {Function} A function that decodes bytes into a Protobuf message.
|
|
603
|
+
*/
|
|
347
604
|
function decodeProtobufMessage(root){
|
|
348
605
|
return function(bytebuf){
|
|
349
606
|
return root.decode(bytebuf);
|
|
350
|
-
}
|
|
607
|
+
};
|
|
351
608
|
}
|
|
352
609
|
|
|
610
|
+
/**
|
|
611
|
+
* Create a function that resolves a Protobuf root type from a wrapped type ID.
|
|
612
|
+
* @param {Function} protostreamTypename - Resolver from type ID to type name.
|
|
613
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
614
|
+
* @returns {Function} A function that takes a type ID and returns the Protobuf root type.
|
|
615
|
+
*/
|
|
353
616
|
function findRootByTypeId(protostreamTypename,root){
|
|
354
617
|
return function(wrappedTypeId){
|
|
355
618
|
return _.compose(root,protostreamTypename)(wrappedTypeId);
|
|
356
|
-
}
|
|
619
|
+
};
|
|
357
620
|
}
|
|
358
621
|
|
|
622
|
+
/**
|
|
623
|
+
* Extract and return a slice of bytes from the buffer, advancing the offset.
|
|
624
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
625
|
+
* @param {number} [length] - The number of bytes to extract; decoded as a VInt if omitted.
|
|
626
|
+
* @returns {Buffer} The extracted byte slice.
|
|
627
|
+
*/
|
|
359
628
|
function trimBytebuf(bytebuf,length){
|
|
360
629
|
if(!f.existy(length)) length = doDecodeVInt(bytebuf);
|
|
361
630
|
var retBuf = bytebuf.buf.slice(bytebuf.offset, bytebuf.offset + length);
|
|
@@ -363,6 +632,13 @@
|
|
|
363
632
|
return retBuf;
|
|
364
633
|
}
|
|
365
634
|
|
|
635
|
+
/**
|
|
636
|
+
* Decode a Protobuf QueryResponse from the buffer and unwrap the results.
|
|
637
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
638
|
+
* @param {Function} protostreamTypename - Resolver from type ID to type name.
|
|
639
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
640
|
+
* @returns {Array} The decoded query results, with or without projections.
|
|
641
|
+
*/
|
|
366
642
|
function doDecodeQuery(bytebuf,protostreamTypename,root){
|
|
367
643
|
var queryResponse = decodeProtobufMessage(Query.QueryResponse)(trimBytebuf(bytebuf));
|
|
368
644
|
var queryResults = queryResponse.results;
|
|
@@ -370,20 +646,45 @@
|
|
|
370
646
|
return f.greaterThan(0)(projectionSize) ? unwrapQueryWithProj(queryResults,projectionSize) : unwrapQueryWithoutProj(queryResults,protostreamTypename,root);
|
|
371
647
|
}
|
|
372
648
|
|
|
649
|
+
/**
|
|
650
|
+
* Decode and unwrap the wrappedBytes field from a query result entry.
|
|
651
|
+
* @param {Object} result - A query result containing a wrappedBytes field.
|
|
652
|
+
* @param {Function} protostreamTypename - Resolver from type ID to type name.
|
|
653
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
654
|
+
* @returns {*} The unwrapped decoded value.
|
|
655
|
+
*/
|
|
373
656
|
function decodeWrappedBytes(result,protostreamTypename,root){
|
|
374
657
|
return _.compose(f.curry3(unwrapWrappedMessage)(root)(protostreamTypename),decodeProtobufMessage(WrappedMessage))(result.wrappedBytes);
|
|
375
658
|
}
|
|
376
659
|
|
|
660
|
+
/**
|
|
661
|
+
* Unwrap query results that have no projections by decoding each result's wrapped bytes.
|
|
662
|
+
* @param {Array} queryResults - The array of raw query result entries.
|
|
663
|
+
* @param {Function} protostreamTypeName - Resolver from type ID to type name.
|
|
664
|
+
* @param {Function} root - Resolver from type name to Protobuf root type.
|
|
665
|
+
* @returns {Array} The array of decoded result values.
|
|
666
|
+
*/
|
|
377
667
|
function unwrapQueryWithoutProj(queryResults,protostreamTypeName,root){
|
|
378
668
|
return queryResults.map(f.curry3(decodeWrappedBytes)(root)(protostreamTypeName));
|
|
379
669
|
}
|
|
380
670
|
|
|
671
|
+
/**
|
|
672
|
+
* Unwrap query results with projections by grouping values into arrays of projection size.
|
|
673
|
+
* @param {Array} queryResults - The array of raw query result entries.
|
|
674
|
+
* @param {number} projectionSize - The number of projection columns per result row.
|
|
675
|
+
* @returns {Array<Array>} The array of result rows, each containing projection values.
|
|
676
|
+
*/
|
|
381
677
|
function unwrapQueryWithProj(queryResults,projectionSize){
|
|
382
678
|
return _.reduce(queryResults,function(acc,cur,i){
|
|
383
679
|
return ((i%projectionSize) ? acc[acc.length-1].push(_.values(cur)[0]) : acc.push([_.values(cur)[0]])) && acc;
|
|
384
680
|
},[]);
|
|
385
681
|
}
|
|
386
682
|
|
|
683
|
+
/**
|
|
684
|
+
* Create an unchecked unsigned byte reader for the buffer.
|
|
685
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
686
|
+
* @returns {Function} A function that reads a single unsigned byte, or undefined if not enough data.
|
|
687
|
+
*/
|
|
387
688
|
function uncheckedReadUByte(bytebuf) {
|
|
388
689
|
return function() {
|
|
389
690
|
if (1 > bytebuf.buf.length - bytebuf.offset) {
|
|
@@ -393,9 +694,14 @@
|
|
|
393
694
|
}
|
|
394
695
|
|
|
395
696
|
return bytebuf.buf.readUInt8(bytebuf.offset++);
|
|
396
|
-
}
|
|
697
|
+
};
|
|
397
698
|
}
|
|
398
699
|
|
|
700
|
+
/**
|
|
701
|
+
* Create an unchecked variable-length number reader for the buffer.
|
|
702
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
703
|
+
* @returns {Function} A function that reads a variable-length number, or undefined if not enough data.
|
|
704
|
+
*/
|
|
399
705
|
function uncheckedReadVNum(bytebuf) {
|
|
400
706
|
return function() {
|
|
401
707
|
var res = 0, shift = 0, b;
|
|
@@ -414,10 +720,15 @@
|
|
|
414
720
|
shift += 7;
|
|
415
721
|
} while (b >= MSB);
|
|
416
722
|
|
|
417
|
-
return res
|
|
418
|
-
}
|
|
723
|
+
return res;
|
|
724
|
+
};
|
|
419
725
|
}
|
|
420
726
|
|
|
727
|
+
/**
|
|
728
|
+
* Create an unchecked fixed-width 8-byte long reader for the buffer.
|
|
729
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
730
|
+
* @returns {Function} A function that reads a long, or undefined if not enough data.
|
|
731
|
+
*/
|
|
421
732
|
function uncheckedReadLong(bytebuf) {
|
|
422
733
|
return function() {
|
|
423
734
|
if (8 > bytebuf.buf.length - bytebuf.offset) {
|
|
@@ -431,26 +742,41 @@
|
|
|
431
742
|
if (low < 0) n += 4294967296;
|
|
432
743
|
bytebuf.offset = bytebuf.offset + 8;
|
|
433
744
|
return n;
|
|
434
|
-
}
|
|
745
|
+
};
|
|
435
746
|
}
|
|
436
747
|
|
|
748
|
+
/**
|
|
749
|
+
* Create an unchecked fixed-length byte reader for the buffer.
|
|
750
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
751
|
+
* @returns {Function} A function that reads a given number of bytes as a Buffer.
|
|
752
|
+
*/
|
|
437
753
|
function uncheckedReadBytes(bytebuf) {
|
|
438
754
|
return function(num) {
|
|
439
755
|
var end = bytebuf.offset + num;
|
|
440
756
|
var bytes = bytebuf.buf.slice(bytebuf.offset, end);
|
|
441
757
|
bytebuf.offset = end;
|
|
442
758
|
return bytes;
|
|
443
|
-
}
|
|
759
|
+
};
|
|
444
760
|
}
|
|
445
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Read and parse a JSON object from the buffer.
|
|
764
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
765
|
+
* @returns {Object} The parsed JSON object, or an empty object if the string is empty.
|
|
766
|
+
*/
|
|
446
767
|
function uncheckedReadJSON(bytebuf) {
|
|
447
768
|
var str = uncheckedReadString(bytebuf)();
|
|
448
|
-
logger.tracef(
|
|
769
|
+
logger.tracef('Read object as string: \'%s\'', str);
|
|
449
770
|
var obj = _.isEmpty(str) ? {} : JSON.parse(str);
|
|
450
771
|
logger.tracef('Returning JSON object: %s', JSON.stringify(obj));
|
|
451
772
|
return obj;
|
|
452
773
|
}
|
|
453
774
|
|
|
775
|
+
/**
|
|
776
|
+
* Create an unchecked length-prefixed string reader for the buffer.
|
|
777
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
778
|
+
* @returns {Function} A function that reads a string, or undefined if not enough data.
|
|
779
|
+
*/
|
|
454
780
|
function uncheckedReadString(bytebuf) {
|
|
455
781
|
return function() {
|
|
456
782
|
var numBytes = uncheckedReadVNum(bytebuf)();
|
|
@@ -466,19 +792,24 @@
|
|
|
466
792
|
var obj = bytebuf.buf.toString(undefined, bytebuf.offset, bytebuf.offset + numBytes);
|
|
467
793
|
bytebuf.offset = bytebuf.offset + numBytes;
|
|
468
794
|
return obj;
|
|
469
|
-
}
|
|
795
|
+
};
|
|
470
796
|
}
|
|
471
797
|
|
|
798
|
+
/**
|
|
799
|
+
* Create an unchecked unsigned 16-bit short reader for the buffer.
|
|
800
|
+
* @param {Object} bytebuf - The byte buffer to read from.
|
|
801
|
+
* @returns {Function} A function that reads a big-endian unsigned short.
|
|
802
|
+
*/
|
|
472
803
|
function uncheckedReadShort(bytebuf) {
|
|
473
804
|
return function() {
|
|
474
805
|
var numBytes = bytebuf.buf.readUInt16BE(bytebuf.offset);
|
|
475
806
|
bytebuf.offset = bytebuf.offset + 2;
|
|
476
807
|
return numBytes;
|
|
477
|
-
}
|
|
808
|
+
};
|
|
478
809
|
}
|
|
479
810
|
|
|
480
811
|
var WrappedMessage = (function() {
|
|
481
|
-
var root=protobuf.loadSync(path.join(__dirname
|
|
812
|
+
var root=protobuf.loadSync(path.join(`${__dirname}/protostream/message-wrapping.proto`));
|
|
482
813
|
|
|
483
814
|
var wrappedMessage = root.lookupType('org.infinispan.protostream.WrappedMessage');
|
|
484
815
|
|
|
@@ -486,15 +817,52 @@
|
|
|
486
817
|
}());
|
|
487
818
|
|
|
488
819
|
var Query = (function() {
|
|
489
|
-
var root=protobuf.loadSync(path.join(__dirname
|
|
490
|
-
protobuf.loadSync(path.join(__dirname
|
|
820
|
+
var root=protobuf.loadSync(path.join(`${__dirname}/protostream/query.proto`));
|
|
821
|
+
protobuf.loadSync(path.join(`${__dirname}/protostream/message-wrapping.proto`),root); //loaded the wrappedMessage.proto to the root
|
|
491
822
|
var QueryRequest = root.lookupType('org.infinispan.query.remote.client.QueryRequest');
|
|
492
823
|
var QueryResponse = root.lookupType('org.infinispan.query.remote.client.QueryResponse');
|
|
824
|
+
var ContinuousQueryResult = root.lookupType('org.infinispan.query.remote.client.ContinuousQueryResult');
|
|
493
825
|
|
|
494
826
|
return {
|
|
495
827
|
QueryRequest,
|
|
496
|
-
QueryResponse
|
|
828
|
+
QueryResponse,
|
|
829
|
+
ContinuousQueryResult
|
|
497
830
|
};
|
|
498
831
|
}());
|
|
499
832
|
|
|
833
|
+
var CQ_RESULT_TYPES = ['leaving', 'joining', 'updated'];
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* Wrap a scalar value as a WrappedMessage byte array.
|
|
837
|
+
* @param {string|number|boolean} value - The value to wrap.
|
|
838
|
+
* @returns {Buffer} The encoded WrappedMessage bytes.
|
|
839
|
+
*/
|
|
840
|
+
exports.wrapScalar = function(value) {
|
|
841
|
+
return WrappedMessage.encode(createWrappedMessage(value, _.identity)).finish();
|
|
842
|
+
};
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Decode a ContinuousQueryResult from raw protobuf bytes.
|
|
846
|
+
* @param {Buffer} bytes - The raw ContinuousQueryResult bytes.
|
|
847
|
+
* @returns {Object} Decoded result with resultType, key, value, projection.
|
|
848
|
+
*/
|
|
849
|
+
exports.decodeContinuousQueryResult = function(bytes) {
|
|
850
|
+
var msg = Query.ContinuousQueryResult.decode(bytes);
|
|
851
|
+
return {
|
|
852
|
+
resultType: CQ_RESULT_TYPES[msg.resultType] || 'leaving',
|
|
853
|
+
key: msg.key,
|
|
854
|
+
value: msg.value,
|
|
855
|
+
projection: msg.projection
|
|
856
|
+
};
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* Decode a WrappedMessage from raw bytes.
|
|
861
|
+
* @param {Buffer} bytes - The raw WrappedMessage bytes.
|
|
862
|
+
* @returns {Object} The decoded WrappedMessage object.
|
|
863
|
+
*/
|
|
864
|
+
exports.decodeWrappedMessage = function(bytes) {
|
|
865
|
+
return WrappedMessage.decode(bytes);
|
|
866
|
+
};
|
|
867
|
+
|
|
500
868
|
}.call(this));
|