@opensearch-project/opensearch 2.2.1 → 2.3.1
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/CHANGELOG.md +27 -1
- package/USER_GUIDE.md +16 -0
- package/index.js +2 -0
- package/lib/Serializer.d.ts +1 -0
- package/lib/Serializer.js +246 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,10 +3,35 @@
|
|
|
3
3
|
Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
|
+
### Added
|
|
7
|
+
### Dependencies
|
|
8
|
+
### Changed
|
|
9
|
+
### Deprecated
|
|
10
|
+
### Removed
|
|
11
|
+
### Fixed
|
|
12
|
+
### Security
|
|
13
|
+
|
|
14
|
+
## [2.3.1]
|
|
15
|
+
### Added
|
|
16
|
+
### Dependencies
|
|
17
|
+
### Changed
|
|
18
|
+
- Made the new Long Number logic added in 2.3.0 optional and disabled by default ([561](https://github.com/opensearch-project/opensearch-js/pull/561)
|
|
19
|
+
### Deprecated
|
|
20
|
+
### Removed
|
|
21
|
+
### Fixed
|
|
22
|
+
### Security
|
|
23
|
+
|
|
24
|
+
## [2.3.0]
|
|
6
25
|
|
|
7
26
|
### Added
|
|
27
|
+
|
|
28
|
+
- Add serialization and deserialization of numerals larger than `Number.MAX_SAFE_INTEGER` ([#544](https://github.com/opensearch-project/opensearch-js/pull/544))
|
|
29
|
+
|
|
8
30
|
### Dependencies
|
|
9
31
|
### Changed
|
|
32
|
+
|
|
33
|
+
- Add upgrading NPM to all workflows running older Node.js versions ([#545](https://github.com/opensearch-project/opensearch-js/issues/545))
|
|
34
|
+
|
|
10
35
|
### Deprecated
|
|
11
36
|
### Removed
|
|
12
37
|
### Fixed
|
|
@@ -140,4 +165,5 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
|
140
165
|
|
|
141
166
|
[2.1.0]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.1.0
|
|
142
167
|
[2.2.0]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.2.0
|
|
143
|
-
[
|
|
168
|
+
[2.3.0]: https://github.com/opensearch-project/opensearch-js/releases/tag/2.3.0
|
|
169
|
+
[Unreleased]: https://github.com/opensearch-project/opensearch-js/compare/2.3.0...HEAD
|
package/USER_GUIDE.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
- [Authenticate with Amazon OpenSearch Service](#authenticate-with-amazon-opensearch-service)
|
|
6
6
|
- [Using AWS V2 SDK](#using-aws-v2-sdk)
|
|
7
7
|
- [Using AWS V3 SDK](#using-aws-v3-sdk)
|
|
8
|
+
- [Enable Handling of Long Numerals](#enable-handling-of-long-numerals)
|
|
8
9
|
- [Create an Index](#create-an-index)
|
|
9
10
|
- [Add a Document to the Index](#add-a-document-to-the-index)
|
|
10
11
|
- [Search for the Document](#search-for-the-document)
|
|
@@ -107,6 +108,21 @@ const client = new Client({
|
|
|
107
108
|
});
|
|
108
109
|
```
|
|
109
110
|
|
|
111
|
+
### Enable Handling of Long Numerals
|
|
112
|
+
|
|
113
|
+
JavaScript can safely work with integers from -(2<sup>53</sup> - 1) to 2<sup>53</sup> - 1. However,
|
|
114
|
+
serialized JSON texts from other languages can potentially have numeric values beyond that range and the native
|
|
115
|
+
serialization and deserialization methods of JavaScript's JSON, incapable of parsing them with precision; these
|
|
116
|
+
values get rounded to fit the IEEE-754 representation.
|
|
117
|
+
|
|
118
|
+
The `Client` can be configured to appropriately deserialize long numerals as `BigInt` values and vice versa:
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
const client = new Client({
|
|
122
|
+
enableLongNumeralSupport: true,
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
110
126
|
## Create an Index
|
|
111
127
|
|
|
112
128
|
```javascript
|
package/index.js
CHANGED
|
@@ -128,6 +128,7 @@ class Client extends OpenSearchAPI {
|
|
|
128
128
|
proxy: null,
|
|
129
129
|
enableMetaHeader: true,
|
|
130
130
|
disablePrototypePoisoningProtection: false,
|
|
131
|
+
enableLongNumeralSupport: false,
|
|
131
132
|
},
|
|
132
133
|
opts
|
|
133
134
|
);
|
|
@@ -151,6 +152,7 @@ class Client extends OpenSearchAPI {
|
|
|
151
152
|
this[kEventEmitter] = new EventEmitter();
|
|
152
153
|
this.serializer = new options.Serializer({
|
|
153
154
|
disablePrototypePoisoningProtection: options.disablePrototypePoisoningProtection,
|
|
155
|
+
enableLongNumeralSupport: options.enableLongNumeralSupport,
|
|
154
156
|
});
|
|
155
157
|
this.connectionPool = new options.ConnectionPool({
|
|
156
158
|
pingTimeout: options.pingTimeout,
|
package/lib/Serializer.d.ts
CHANGED
package/lib/Serializer.js
CHANGED
|
@@ -35,20 +35,239 @@ const sjson = require('secure-json-parse');
|
|
|
35
35
|
const { SerializationError, DeserializationError } = require('./errors');
|
|
36
36
|
const kJsonOptions = Symbol('secure json parse options');
|
|
37
37
|
|
|
38
|
+
/* In JavaScript, a `Number` is a 64-bit floating-point value which can store 16 digits. However, the
|
|
39
|
+
* serializer and deserializer will need to cater to numeric values generated by other languages which
|
|
40
|
+
* can have up to 19 digits. Native JSON parser and stringifier, incapable of handling the extra
|
|
41
|
+
* digits, corrupt the values, making them unusable.
|
|
42
|
+
*
|
|
43
|
+
* To work around this limitation, the deserializer converts long sequences of digits into strings and
|
|
44
|
+
* marks them before applying the parser. During the parsing, string values that begin with the mark
|
|
45
|
+
* are converted to `BigInt` values.
|
|
46
|
+
* Similarly, during stringification, the serializer converts `BigInt` values to marked strings and
|
|
47
|
+
* when done, it replaces them with plain numerals.
|
|
48
|
+
*
|
|
49
|
+
* `Number.MAX_SAFE_INTEGER`, 9,007,199,254,740,991, is the largest number that the native methods can
|
|
50
|
+
* parse and stringify, and any numeral greater than that would need to be translated using the
|
|
51
|
+
* workaround; all 17-digits or longer and only tail-end of the 16-digits need translation. It would
|
|
52
|
+
* be unfair to all the 16-digit numbers if the translation applied to `\d{16,}` only to cover the
|
|
53
|
+
* less than 10%. Hence, a RegExp is created to only match numerals too long to be a number.
|
|
54
|
+
*
|
|
55
|
+
* To make the explanation simpler, let's assume that MAX_SAFE_INTEGER is 8921 which has 4 digits.
|
|
56
|
+
* Starting from the right, we take each digit onwards, `[<start>-9]`:
|
|
57
|
+
* 1) 7922 - 7929: 792[2-9]\d{0}
|
|
58
|
+
* 2) 7930 - 7999: 79[3-9]\d{1}
|
|
59
|
+
* 9) 9 + 1 = 10 which results in a rollover; no need to do anything.
|
|
60
|
+
* 8) 9000 - 9999: [9-9]\d{3}
|
|
61
|
+
* Finally we add anything 5 digits or longer: `\d{5,}
|
|
62
|
+
*
|
|
63
|
+
* PS, a better solution would use AST but considering its performance penalty, RegExp is the next
|
|
64
|
+
* the best solution.
|
|
65
|
+
*/
|
|
66
|
+
const isBigIntSupported = typeof BigInt !== 'undefined';
|
|
67
|
+
const maxIntAsString = String(Number.MAX_SAFE_INTEGER);
|
|
68
|
+
const maxIntLength = maxIntAsString.length;
|
|
69
|
+
// Sub-patterns for each digit
|
|
70
|
+
const bigIntMatcherTokens = [`\\d{${maxIntAsString.length + 1},}`];
|
|
71
|
+
for (let i = 0; i < maxIntLength; i++) {
|
|
72
|
+
if (maxIntAsString[i] !== '9') {
|
|
73
|
+
bigIntMatcherTokens.push(
|
|
74
|
+
maxIntAsString.substring(0, i) +
|
|
75
|
+
`[${parseInt(maxIntAsString[i], 10) + 1}-9]` +
|
|
76
|
+
`\\d{${maxIntLength - i - 1}}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/* The matcher that looks for `": <numerals>, ...}` and `[..., <numeral>, ...]`
|
|
82
|
+
*
|
|
83
|
+
* The pattern starts by looking for `":` not immediately preceded by a `\`. That should be
|
|
84
|
+
* followed by any of the numeric sub-patterns. A comma, end of an array, end of an object, or
|
|
85
|
+
* the end of the input are the only acceptable elements after it.
|
|
86
|
+
*/
|
|
87
|
+
const bigIntMatcher = new RegExp(
|
|
88
|
+
`((?:\\[|,|(?<!\\\\)"\\s*:)\\s*)(-?(?:${bigIntMatcherTokens.join('|')}))(\\s*)(?=,|}|]|$)`,
|
|
89
|
+
'g'
|
|
90
|
+
);
|
|
91
|
+
|
|
38
92
|
class Serializer {
|
|
39
93
|
constructor(opts = {}) {
|
|
40
94
|
const disable = opts.disablePrototypePoisoningProtection;
|
|
41
95
|
this[kJsonOptions] = {
|
|
42
96
|
protoAction: disable === true || disable === 'proto' ? 'ignore' : 'error',
|
|
43
97
|
constructorAction: disable === true || disable === 'constructor' ? 'ignore' : 'error',
|
|
98
|
+
enableLongNumeralSupport: opts.enableLongNumeralSupport === true,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* The characters with a highly unlikely chance of occurrence in strings, alone or in combination.
|
|
103
|
+
*
|
|
104
|
+
* ToDo: When support for ancient versions of Node.js are dropped, replace with
|
|
105
|
+
* _bigIntMarkChars = ['෴', '߷', '֍'];
|
|
106
|
+
*/
|
|
107
|
+
get _bigIntMarkChars() {
|
|
108
|
+
return ['෴', '߷', '֍'];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* Generates an array of all combinations of `_bigIntMarkChars` with the requested length. */
|
|
112
|
+
_bigIntMarkerCombinations(length = 3) {
|
|
113
|
+
const results = [];
|
|
114
|
+
const arr = this._bigIntMarkChars;
|
|
115
|
+
const arrLength = arr.length;
|
|
116
|
+
const temp = Array(length);
|
|
117
|
+
|
|
118
|
+
(function fill(pos, start) {
|
|
119
|
+
if (pos === length) return results.push(temp.join(''));
|
|
120
|
+
|
|
121
|
+
for (let i = start; i < arrLength; i++) {
|
|
122
|
+
temp[pos] = arr[i];
|
|
123
|
+
fill(pos + 1, i);
|
|
124
|
+
}
|
|
125
|
+
})(0, 0);
|
|
126
|
+
|
|
127
|
+
return results;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* Experiments with different combinations of various lengths, until one is found to not be in
|
|
131
|
+
* the input string.
|
|
132
|
+
*/
|
|
133
|
+
_getSuitableBigIntMarker(json) {
|
|
134
|
+
let bigIntMarker;
|
|
135
|
+
let length = 0;
|
|
136
|
+
do {
|
|
137
|
+
length++;
|
|
138
|
+
this._bigIntMarkerCombinations(length).some((marker) => {
|
|
139
|
+
if (json.indexOf(marker) === -1) {
|
|
140
|
+
bigIntMarker = marker;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
} while (!bigIntMarker);
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
bigIntMarker,
|
|
148
|
+
length,
|
|
44
149
|
};
|
|
45
150
|
}
|
|
46
151
|
|
|
152
|
+
_parseWithBigInt(json) {
|
|
153
|
+
const { bigIntMarker, length } = this._getSuitableBigIntMarker(json);
|
|
154
|
+
|
|
155
|
+
let hadException;
|
|
156
|
+
let markedJSON = json.replace(bigIntMatcher, `$1"${bigIntMarker}$2"$3`);
|
|
157
|
+
|
|
158
|
+
/* RegExp cannot replace AST and the process of marking adds quotes. So, any false-positive hit
|
|
159
|
+
* will make the JSON string unparseable.
|
|
160
|
+
*
|
|
161
|
+
* To find those instances, we try to parse and watch for the location of any errors. If an error
|
|
162
|
+
* is caused by the marking, we remove that single marking and try again.
|
|
163
|
+
*/
|
|
164
|
+
do {
|
|
165
|
+
try {
|
|
166
|
+
hadException = false;
|
|
167
|
+
JSON.parse(markedJSON);
|
|
168
|
+
} catch (e) {
|
|
169
|
+
hadException = true;
|
|
170
|
+
/* There are two types of exception objects that can be raised:
|
|
171
|
+
* 1) a proper object with lineNumber and columnNumber which we can use
|
|
172
|
+
* 2) a textual message with the position that we need to parse
|
|
173
|
+
*/
|
|
174
|
+
let { lineNumber, columnNumber } = e;
|
|
175
|
+
if (!lineNumber || !columnNumber) {
|
|
176
|
+
const match =
|
|
177
|
+
// ToDo: When support for ancient versions of Node.js are dropped, replace with
|
|
178
|
+
// e?.message?.match?.()
|
|
179
|
+
e &&
|
|
180
|
+
e.message &&
|
|
181
|
+
typeof e.message.match === 'function' &&
|
|
182
|
+
e.message.match(/^Unexpected token.*at position (\d+)$/);
|
|
183
|
+
if (match) {
|
|
184
|
+
lineNumber = 1;
|
|
185
|
+
// The position is zero-indexed; adding 1 to normalize it for the -2 that comes later
|
|
186
|
+
columnNumber = parseInt(match[1], 10) + 1;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (lineNumber < 1 || columnNumber < 2) {
|
|
191
|
+
// The problem is not with this replacement; just return a failure.
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/* We need to skip e.lineNumber - 1 number of `\n` occurrences.
|
|
196
|
+
* Then, we need to go to e.columnNumber - 2 to look for `"<mark>\d+"`; we need to `-1` to
|
|
197
|
+
* account for the quote but an additional `-1` is needed because columnNumber starts from 1.
|
|
198
|
+
*/
|
|
199
|
+
const re = new RegExp(
|
|
200
|
+
`^((?:.*\\n){${lineNumber - 1}}[^\\n]{${columnNumber - 2}})"${bigIntMarker}(-?\\d+)"`
|
|
201
|
+
);
|
|
202
|
+
if (!re.test(markedJSON)) {
|
|
203
|
+
// The exception is not caused by adding the marker
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// We have found a bad replacement; let's remove it.
|
|
208
|
+
markedJSON = markedJSON.replace(re, '$1$2');
|
|
209
|
+
}
|
|
210
|
+
} while (hadException);
|
|
211
|
+
|
|
212
|
+
const bigIntMarkFinder = new RegExp(`^${bigIntMarker}-?\\d+$`);
|
|
213
|
+
|
|
214
|
+
// Exceptions will trickle up to the caller
|
|
215
|
+
return sjson.parse(
|
|
216
|
+
markedJSON,
|
|
217
|
+
(key, val) =>
|
|
218
|
+
/* Convert marked values to BigInt values.
|
|
219
|
+
* The `startsWith` is purely for performance, to avoid running `test` if not needed.
|
|
220
|
+
*/
|
|
221
|
+
typeof val === 'string' && val.startsWith(bigIntMarker) && bigIntMarkFinder.test(val)
|
|
222
|
+
? BigInt(val.substring(length)) // eslint-disable-line no-undef
|
|
223
|
+
: val,
|
|
224
|
+
this[kJsonOptions]
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_stringifyWithBigInt(object, candidate) {
|
|
229
|
+
const { bigIntMarker } = this._getSuitableBigIntMarker(candidate);
|
|
230
|
+
|
|
231
|
+
/* The matcher that looks for "<marker><numerals>"
|
|
232
|
+
* Because we have made sure that `bigIntMarker` was never present in the original object, we can
|
|
233
|
+
* carelessly assume every "<marker><numerals>" is due to our marking.
|
|
234
|
+
*/
|
|
235
|
+
const markedBigIntMatcher = new RegExp(`"${bigIntMarker}(-?\\d+)"`, 'g');
|
|
236
|
+
|
|
237
|
+
return (
|
|
238
|
+
JSON.stringify(
|
|
239
|
+
object,
|
|
240
|
+
/* Convert BigInt values to a string and mark them.
|
|
241
|
+
* Can't be bothered with Number values beyond safe values because they are already corrupted.
|
|
242
|
+
*/
|
|
243
|
+
(key, val) => (typeof val === 'bigint' ? `${bigIntMarker}${val.toString()}` : val)
|
|
244
|
+
)
|
|
245
|
+
// Replace marked substrings with just the numerals
|
|
246
|
+
.replace(markedBigIntMatcher, '$1')
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
47
250
|
serialize(object) {
|
|
48
251
|
debug('Serializing', object);
|
|
49
252
|
let json;
|
|
253
|
+
let numeralsAreNumbers = true;
|
|
254
|
+
const checkForBigInts = (key, val) => {
|
|
255
|
+
if (typeof val === 'bigint') {
|
|
256
|
+
numeralsAreNumbers = false;
|
|
257
|
+
// Number() is much faster than parseInt() on BigInt values
|
|
258
|
+
return Number(val);
|
|
259
|
+
}
|
|
260
|
+
return val;
|
|
261
|
+
};
|
|
262
|
+
const shouldHandleLongNumerals =
|
|
263
|
+
isBigIntSupported && this[kJsonOptions].enableLongNumeralSupport;
|
|
50
264
|
try {
|
|
51
|
-
json = JSON.stringify(object);
|
|
265
|
+
json = JSON.stringify(object, shouldHandleLongNumerals ? checkForBigInts : null);
|
|
266
|
+
|
|
267
|
+
if (shouldHandleLongNumerals && !numeralsAreNumbers) {
|
|
268
|
+
const temp = this._stringifyWithBigInt(object, json);
|
|
269
|
+
if (temp) json = temp;
|
|
270
|
+
}
|
|
52
271
|
} catch (err) {
|
|
53
272
|
throw new SerializationError(err.message, object);
|
|
54
273
|
}
|
|
@@ -58,8 +277,33 @@ class Serializer {
|
|
|
58
277
|
deserialize(json) {
|
|
59
278
|
debug('Deserializing', json);
|
|
60
279
|
let object;
|
|
280
|
+
let numeralsAreNumbers = true;
|
|
281
|
+
const checkForLargeNumerals = (key, val) => {
|
|
282
|
+
if (
|
|
283
|
+
numeralsAreNumbers &&
|
|
284
|
+
typeof val === 'number' &&
|
|
285
|
+
(val < Number.MAX_SAFE_INTEGER || val > Number.MAX_SAFE_INTEGER)
|
|
286
|
+
) {
|
|
287
|
+
numeralsAreNumbers = false;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return val;
|
|
291
|
+
};
|
|
292
|
+
const shouldHandleLongNumerals =
|
|
293
|
+
isBigIntSupported && this[kJsonOptions].enableLongNumeralSupport;
|
|
61
294
|
try {
|
|
62
|
-
object = sjson.parse(
|
|
295
|
+
object = sjson.parse(
|
|
296
|
+
json,
|
|
297
|
+
shouldHandleLongNumerals ? checkForLargeNumerals : null,
|
|
298
|
+
this[kJsonOptions]
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
if (shouldHandleLongNumerals && !numeralsAreNumbers) {
|
|
302
|
+
const temp = this._parseWithBigInt(json);
|
|
303
|
+
if (temp) {
|
|
304
|
+
object = temp;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
63
307
|
} catch (err) {
|
|
64
308
|
throw new DeserializationError(err.message, json);
|
|
65
309
|
}
|