@opensearch-project/opensearch 2.8.0 → 2.9.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/CHANGELOG.md +17 -1
- package/lib/Serializer.js +36 -205
- package/lib/aws/index-v3.d.ts +20 -0
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -11,13 +11,29 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
|
11
11
|
### Fixed
|
|
12
12
|
### Security
|
|
13
13
|
|
|
14
|
+
## [2.9.0]
|
|
15
|
+
### Dependencies
|
|
16
|
+
- Bumps `@aws-sdk/types` from 3.535.0 to 3.577.0
|
|
17
|
+
- Bumps `@babel/eslint-parser` from 7.24.1 to 7.24.6
|
|
18
|
+
- Bumps `@types/node` from 20.12.9 to 20.14.0
|
|
19
|
+
- Bumps `semver` from 7.6.0 to 7.6.2
|
|
20
|
+
- Bumps `rimraf` from 5.0.5 to 5.0.7
|
|
21
|
+
- Bumps `aws4` from 1.12.0 to 1.13.0
|
|
22
|
+
- Bumps `prettier` from 3.2.5 to 3.3.0
|
|
23
|
+
- Bumps `debug` from 4.3.4 to 4.3.5
|
|
24
|
+
### Changed
|
|
25
|
+
- Upgraded the parsing and serialization of long numerals to employ JSON11 ([784](https://github.com/opensearch-project/opensearch-js/pull/784)).
|
|
26
|
+
### Fixed
|
|
27
|
+
- Missing type definitions for `@opensearch-project/opensearch/aws-v3` ([776](https://github.com/opensearch-project/opensearch-js/pull/776)).
|
|
28
|
+
|
|
14
29
|
## [2.8.0]
|
|
15
30
|
### Added
|
|
16
31
|
- Added `@opensearch-project/opensearch/aws-v3` import ([758](https://github.com/opensearch-project/opensearch-js/pull/758)).
|
|
17
32
|
### Dependencies
|
|
18
|
-
- Bumps `@types/node` from 20.12.5 to 20.12.
|
|
33
|
+
- Bumps `@types/node` from 20.12.5 to 20.12.9
|
|
19
34
|
- Bumps `semver` from 5.7.1 to 7.6.0
|
|
20
35
|
- Bumps `dedent` from 1.5.1 to 1.5.3
|
|
36
|
+
- Bumps `jsdoc` from 4.0.2 to 4.0.3
|
|
21
37
|
### Changed
|
|
22
38
|
- Disabled fail_ci_if_error for upload coverage step in coverage workflow ([#755](https://github.com/opensearch-project/opensearch-js/issues/755)
|
|
23
39
|
|
package/lib/Serializer.js
CHANGED
|
@@ -34,60 +34,9 @@ const debug = require('debug')('opensearch');
|
|
|
34
34
|
const sjson = require('secure-json-parse');
|
|
35
35
|
const { SerializationError, DeserializationError } = require('./errors');
|
|
36
36
|
const kJsonOptions = Symbol('secure json parse options');
|
|
37
|
+
const JSON11 = require('json11');
|
|
37
38
|
|
|
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
39
|
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
40
|
|
|
92
41
|
class Serializer {
|
|
93
42
|
constructor(opts = {}) {
|
|
@@ -99,154 +48,6 @@ class Serializer {
|
|
|
99
48
|
};
|
|
100
49
|
}
|
|
101
50
|
|
|
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,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
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
|
-
|
|
250
51
|
serialize(object) {
|
|
251
52
|
debug('Serializing', object);
|
|
252
53
|
let json;
|
|
@@ -262,11 +63,29 @@ class Serializer {
|
|
|
262
63
|
const shouldHandleLongNumerals =
|
|
263
64
|
isBigIntSupported && this[kJsonOptions].enableLongNumeralSupport;
|
|
264
65
|
try {
|
|
66
|
+
/* When handling long numerals is not requested or the platform doesn't support BigInt, the
|
|
67
|
+
* result of JSON.stringify are returned.
|
|
68
|
+
*
|
|
69
|
+
* When long numerals should be handled:
|
|
70
|
+
* Use JSON.stringify to check if any value is a BigInt:
|
|
71
|
+
* * If no BigInt values are found, the result of JSON.stringify is good enough to be returned.
|
|
72
|
+
* * Only If a BigInt value is found, JSON11.stringify is employed and its result is returned.
|
|
73
|
+
*/
|
|
265
74
|
json = JSON.stringify(object, shouldHandleLongNumerals ? checkForBigInts : null);
|
|
266
75
|
|
|
267
76
|
if (shouldHandleLongNumerals && !numeralsAreNumbers) {
|
|
268
|
-
|
|
269
|
-
|
|
77
|
+
try {
|
|
78
|
+
// With `withBigInt: false`, valid JSON is produced while maintaining accuracy
|
|
79
|
+
const temp = JSON11.stringify(object, {
|
|
80
|
+
withBigInt: false,
|
|
81
|
+
quote: '"',
|
|
82
|
+
quoteNames: true,
|
|
83
|
+
});
|
|
84
|
+
if (temp) json = temp;
|
|
85
|
+
} catch (ex) {
|
|
86
|
+
// Do nothing: JSON.stringify succeeded but JSON11.stringify failed; return the
|
|
87
|
+
// JSON.stringify result.
|
|
88
|
+
}
|
|
270
89
|
}
|
|
271
90
|
} catch (err) {
|
|
272
91
|
throw new SerializationError(err.message, object);
|
|
@@ -292,6 +111,14 @@ class Serializer {
|
|
|
292
111
|
const shouldHandleLongNumerals =
|
|
293
112
|
isBigIntSupported && this[kJsonOptions].enableLongNumeralSupport;
|
|
294
113
|
try {
|
|
114
|
+
/* When handling long numerals is not requested or the platform doesn't support BigInt, the
|
|
115
|
+
* result of sjson.parse are returned.
|
|
116
|
+
*
|
|
117
|
+
* When long numerals should be handled:
|
|
118
|
+
* Use sjson.parse to check if any value is outside the range of safe integers:
|
|
119
|
+
* * If no long numerals are found, the result of sjson.parse is good enough to be returned.
|
|
120
|
+
* * Only If long numerals are found, JSON11.parse is employed and its result is returned.
|
|
121
|
+
*/
|
|
295
122
|
object = sjson.parse(
|
|
296
123
|
json,
|
|
297
124
|
shouldHandleLongNumerals ? checkForLargeNumerals : null,
|
|
@@ -299,9 +126,13 @@ class Serializer {
|
|
|
299
126
|
);
|
|
300
127
|
|
|
301
128
|
if (shouldHandleLongNumerals && !numeralsAreNumbers) {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
129
|
+
try {
|
|
130
|
+
const temp = JSON11.parse(json, null, { withLongNumerals: true });
|
|
131
|
+
if (temp) {
|
|
132
|
+
object = temp;
|
|
133
|
+
}
|
|
134
|
+
} catch (ex) {
|
|
135
|
+
// Do nothing: sjson.parse succeeded but JSON11.parse failed; return the sjson.parse result
|
|
305
136
|
}
|
|
306
137
|
}
|
|
307
138
|
} catch (err) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright OpenSearch Contributors
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*
|
|
5
|
+
* The OpenSearch Contributors require contributions made to
|
|
6
|
+
* this file be licensed under the Apache-2.0 license or a
|
|
7
|
+
* compatible open source license.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/// <reference types="node" />
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
AwsSigv4Signer,
|
|
15
|
+
AwsSigv4SignerError,
|
|
16
|
+
AwsSigv4SignerOptions,
|
|
17
|
+
AwsSigv4SignerResponse,
|
|
18
|
+
} from './index';
|
|
19
|
+
|
|
20
|
+
export { AwsSigv4Signer, AwsSigv4SignerOptions, AwsSigv4SignerResponse, AwsSigv4SignerError };
|
package/package.json
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://www.opensearch.org/",
|
|
33
|
-
"version": "2.
|
|
33
|
+
"version": "2.9.0",
|
|
34
34
|
"versionCanary": "7.10.0-canary.6",
|
|
35
35
|
"keywords": [
|
|
36
36
|
"opensearch",
|
|
@@ -98,6 +98,7 @@
|
|
|
98
98
|
"xmlbuilder2": "^3.0.2"
|
|
99
99
|
},
|
|
100
100
|
"dependencies": {
|
|
101
|
+
"json11": "^1.0.4",
|
|
101
102
|
"aws4": "^1.11.0",
|
|
102
103
|
"debug": "^4.3.1",
|
|
103
104
|
"hpagent": "^1.2.0",
|