@opensearch-project/opensearch 2.7.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 CHANGED
@@ -9,19 +9,42 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
9
9
  ### Deprecated
10
10
  ### Removed
11
11
  ### Fixed
12
+ ### Security
12
13
 
13
- ## [2.7.0]
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
+
29
+ ## [2.8.0]
14
30
  ### Added
31
+ - Added `@opensearch-project/opensearch/aws-v3` import ([758](https://github.com/opensearch-project/opensearch-js/pull/758)).
32
+ ### Dependencies
33
+ - Bumps `@types/node` from 20.12.5 to 20.12.9
34
+ - Bumps `semver` from 5.7.1 to 7.6.0
35
+ - Bumps `dedent` from 1.5.1 to 1.5.3
36
+ - Bumps `jsdoc` from 4.0.2 to 4.0.3
37
+ ### Changed
38
+ - Disabled fail_ci_if_error for upload coverage step in coverage workflow ([#755](https://github.com/opensearch-project/opensearch-js/issues/755)
39
+
40
+ ## [2.7.0]
15
41
  ### Dependencies
16
42
  - Bumps `@aws-sdk/types` from 3.523.0 to 3.535.0
17
43
  - Bumps `simple-git` from 3.22.0 to 3.24.0
18
44
  - Bumps `@types/node` from 20.11.25 to 20.12.5
19
45
  - Bumps `@babel/eslint-parser` from 7.23.10 to 7.24.1
20
46
  ### Changed
21
- - Added `search_pipeline` ([716](https://github.com/opensearch-project/opensearch-js/pull/716))
22
- ### Deprecated
23
- ### Removed
24
- ### Fixed
47
+ - Added `search_pipeline` ([#716](https://github.com/opensearch-project/opensearch-js/pull/716))
25
48
 
26
49
  ## [2.6.0]
27
50
  ### Added
@@ -39,12 +62,10 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
39
62
  - Updated integration tests to use strong password in OS 2.12 and up ([#707](https://github.com/opensearch-project/opensearch-js/pull/707))
40
63
  - Simplified client creation in the guides ([#707](https://github.com/opensearch-project/opensearch-js/pull/707))
41
64
  - Corrected `guides/bulk.md` ([#616](https://github.com/opensearch-project/opensearch-js/issues/616))
42
- ### Deprecated
43
65
  ### Removed
44
66
  - Removed AutoHeal([#707](https://github.com/opensearch-project/opensearch-js/pull/707))
45
67
  ### Fixed
46
68
  - Make optional fields optional in `MappingGenericProperty` ([708](https://github.com/opensearch-project/opensearch-js/pull/708))
47
- ### Security
48
69
 
49
70
  ## [2.5.0]
50
71
  ### Added
@@ -72,8 +93,6 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
72
93
  - Bumps `eslint-config-prettier` from 9.0.0 to 9.1.0
73
94
  - Bumps `ora` from 7.0.1 to 8.0.1
74
95
  - Bumps `simple-git` from 3.21.0 to 3.22.0
75
- ### Changed
76
- ### Deprecated
77
96
  ### Removed
78
97
  - Removed fixture lines that git-secrets wrongly flagged as passwords/secrets ([654](https://github.com/opensearch-project/opensearch-js/pull/645))
79
98
  ### Fixed
@@ -101,11 +120,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
101
120
  ### Changed
102
121
  - Make handling of long numerals an option that is disabled by default ([#557](https://github.com/opensearch-project/opensearch-js/pull/557))
103
122
  - Add `enableLongNumeralSupport` to the type definition for `ClientOptions` ([#598](https://github.com/opensearch-project/opensearch-js/pull/598))
104
- ### Deprecated
105
- ### Removed
106
123
  ### Fixed
107
124
  - Fix wrong documentation for file serach.md regarding `search_after` param ([#584](https://github.com/opensearch-project/opensearch-js/pull/584))
108
- ### Security
109
125
 
110
126
  ## [2.3.0]
111
127
 
@@ -126,24 +142,18 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
126
142
  - Implemented Docker Image caching for `integration-unreleased` workflow ([#387](https://github.com/opensearch-project/opensearch-js/issues/387))
127
143
  - Add upgrading NPM to all workflows running older Node.js versions ([#545](https://github.com/opensearch-project/opensearch-js/issues/545))
128
144
  - Upgrade and secure the backport workflow ([#547](https://github.com/opensearch-project/opensearch-js/issues/547))
129
- ### Deprecated
130
- ### Removed
131
145
  ### Fixed
132
146
  - Fixed malformed `bundler.yml` ([#497](https://github.com/opensearch-project/opensearch-js/pull/497))
133
- ### Security
134
147
 
135
148
  ## [2.2.1]
136
149
 
137
150
  ### Added
138
-
139
151
  - Add release details to releasing.md ([319](https://github.com/opensearch-project/opensearch-js/pull/319))
140
152
  - Added Amazon OpenSearch Serverless in user_guide ([#372](https://github.com/opensearch-project/opensearch-js/issues/372))
141
153
  - Add guidelines on installing yarn, dependencies; instructions on running ESLint in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435))
142
154
  - Added pull_request_template ([440](https://github.com/opensearch-project/opensearch-js/pull/440))
143
155
  - Added guide for Search ([#473](https://github.com/opensearch-project/opensearch-js/pull/489))
144
-
145
156
  ### Dependencies
146
-
147
157
  - Bumps `aws4` from 1.11.0 to 1.12.0
148
158
  - Bumps `minimist` from 1.2.6 to 1.2.8
149
159
  - Bumps `tsd` from 0.24.1 to 0.27.0
@@ -165,37 +175,26 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
165
175
  - Bumps `prettier` from 2.8.4 to 2.8.7
166
176
  - Bumps `jsdoc` from 4.0.0 to 4.0.2
167
177
  - Bumps `@babel/eslint-parser` from 7.19.1 to 7.21.3
168
-
169
178
  ### Changed
170
-
171
179
  - Remove test artifacts from gh_pages workflow ([#335](https://github.com/opensearch-project/opensearch-js/issues/335))
172
180
  - Make fields in `BulkOperation` optional to match OpenSearch Bulk API requirements ([#378](https://github.com/opensearch-project/opensearch-js/pull/378))
173
181
  - Remove guidance on using npm and switch completely to yarn in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435))
174
182
  - Change coverage, compatability, integration, integration with unreleased Open Search, node ci, bundler tests not to run on documentation change ([441](https://github.com/opensearch-project/opensearch-js/pull/441))
175
183
  - Change the Windows yarn installation troubleshoot steps ([455](https://github.com/opensearch-project/opensearch-js/issues/455))
176
184
  - Make `callback` arg in `BaseConnectionPool`, `CloudConnectionPool` and `ConnectionPool` optional ([#451](https://github.com/opensearch-project/opensearch-js/pull/451))
177
-
178
185
  ### Deprecated
179
-
180
186
  - Remove deprecation warnings in bulk.test.js ([#434](https://github.com/opensearch-project/opensearch-js/issues/434))
181
-
182
187
  ### Removed
183
-
184
188
  - Remove waitCluster in Integration Tests ([#422](https://github.com/opensearch-project/opensearch-js/issues/422))
185
-
186
189
  ### Fixed
187
-
188
190
  - Added missing types for AwsSigv4SignerOptions.service ([#377](https://github.com/opensearch-project/opensearch-js/pull/377))
189
191
  - Fixed deprecated folder mapping "./" in the "exports" field module resolution ([#416](https://github.com/opensearch-project/opensearch-js/pull/416))
190
192
 
191
- ### Security
192
-
193
193
  - [CVE-2022-25912] Bumps simple-git from 3.4.0 to 3.15.0 ([#341](https://github.com/opensearch-project/opensearch-js/pull/341))
194
194
 
195
195
  ## [2.2.0]
196
196
 
197
197
  ### Added
198
-
199
198
  - Add release details to releasing.md ([319](https://github.com/opensearch-project/opensearch-js/pull/319))
200
199
  - Allow overriding the aws service identifier in AwsSigv4Signer ([333](https://github.com/opensearch-project/opensearch-js/pull/333))
201
200
  - Added skip-changelog label ([339](https://github.com/opensearch-project/opensearch-js/pull/339))
@@ -209,9 +208,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
209
208
  - Fixed typos in `ConnectionPool` ([#427](https://github.com/opensearch-project/opensearch-js/pull/427))
210
209
  - Added the solution for the possible error during yarn installation on Windows OS ([#435](https://github.com/opensearch-project/opensearch-js/issues/435))
211
210
  - Added missing `createConnection` method type definition in `BaseConnectionPool` )([#490](https://github.com/opensearch-project/opensearch-js/pull/490))
212
-
213
211
  ### Dependencies
214
-
215
212
  - Bumps `xmlbuilder2` from 2.4.1 to 3.0.2
216
213
  - Bumps `minimatch` from 3.0.4 to 3.1.2
217
214
  - Bumps `eslint` from 7.32.0 to 8.32.0
@@ -224,36 +221,18 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
224
221
  - Bumps `hpagent` from 0.1.2 to 1.2.0
225
222
  - Bumps `eslint-config-prettier` from 8.5.0 to 8.6.0
226
223
  - Bumps `rimraf` from 3.0.2 to 4.1.1
227
-
228
- ### Dependencies
229
-
230
224
  ### Changed
231
-
232
225
  - Remove test artifacts from gh_pages workflow ([#335](https://github.com/opensearch-project/opensearch-js/issues/335))
233
226
  - Make `TimeoutError` and `RequestAbortedError` classes' `meta` argument optional ([#478](https://github.com/opensearch-project/opensearch-js/pull/478/))
234
-
235
- ### Deprecated
236
-
237
- ### Removed
238
-
239
- ### Fixed
240
-
241
227
  ### Security
242
-
243
228
  - [CVE-2022-25912] Bumps simple-git from 3.4.0 to 3.15.0 ([#341](https://github.com/opensearch-project/opensearch-js/pull/341))
244
229
 
245
230
  ## [2.1.0]
246
-
247
231
  ### Added
248
-
249
232
  - Github workflow for changelog verification ([#306](https://github.com/opensearch-project/opensearch-js/pull/306))
250
233
  - Add GitHub and Jenkins release workflow ([#317](https://github.com/opensearch-project/opensearch-js/pull/317))
251
-
252
234
  ### Dependencies
253
-
254
235
  - Bumps `tsd` from 0.22.0 to 0.24.1
255
236
  - Bumps `semver` from 7.3.7 to 7.3.8
256
-
257
237
  ### Fixed
258
-
259
238
  - Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291))
package/USER_GUIDE.md CHANGED
@@ -90,7 +90,9 @@ const client = new Client({
90
90
  ```javascript
91
91
  const { defaultProvider } = require('@aws-sdk/credential-provider-node'); // V3 SDK.
92
92
  const { Client } = require('@opensearch-project/opensearch');
93
- const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws');
93
+ const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws-v3'); // use aws-v3 import path if you are using aws-sdk v3
94
+ // Unlike the import path in the v2 example above that lazy loads both aws-sdk v3 credential providers & entire aws-sdk v2 if available
95
+ // This will only lazy load the aws-sdk v3 credential providers
94
96
 
95
97
  const client = new Client({
96
98
  ...AwsSigv4Signer({
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
- const temp = this._stringifyWithBigInt(object, json);
269
- if (temp) json = temp;
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
- const temp = this._parseWithBigInt(json);
303
- if (temp) {
304
- object = temp;
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,32 @@
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
+ 'use strict';
12
+ const AwsSigv4SignerError = require('./errors');
13
+ const { giveAwsV4Signer, giveAwsCredentialProviderLoader } = require('./shared');
14
+
15
+ const getAwsSDKCredentialsProvider = async () => {
16
+ try {
17
+ const awsV3 = await import('@aws-sdk/credential-provider-node');
18
+ if (typeof awsV3.defaultProvider === 'function') {
19
+ return awsV3.defaultProvider();
20
+ }
21
+ } catch (err) {
22
+ throw new AwsSigv4SignerError(
23
+ "Missing '@aws-sdk/credential-provider-node' module. Install it as a dependency."
24
+ );
25
+ }
26
+ };
27
+
28
+ const AwsSigv4Signer = giveAwsV4Signer(
29
+ giveAwsCredentialProviderLoader(getAwsSDKCredentialsProvider)
30
+ );
31
+
32
+ module.exports = AwsSigv4Signer;
@@ -9,12 +9,8 @@
9
9
  */
10
10
 
11
11
  'use strict';
12
- const Connection = require('../Connection');
13
- const Transport = require('../Transport');
14
- const aws4 = require('aws4');
15
12
  const AwsSigv4SignerError = require('./errors');
16
- const crypto = require('crypto');
17
- const { toMs } = Transport.internals;
13
+ const { giveAwsV4Signer, giveAwsCredentialProviderLoader } = require('./shared');
18
14
 
19
15
  const getAwsSDKCredentialsProvider = async () => {
20
16
  // First try V3
@@ -49,150 +45,8 @@ const getAwsSDKCredentialsProvider = async () => {
49
45
  );
50
46
  };
51
47
 
52
- const awsDefaultCredentialsProvider = () =>
53
- new Promise((resolve, reject) => {
54
- getAwsSDKCredentialsProvider()
55
- .then((provider) => {
56
- provider().then(resolve).catch(reject);
57
- })
58
- .catch((err) => {
59
- reject(err);
60
- });
61
- });
48
+ const AwsSigv4Signer = giveAwsV4Signer(
49
+ giveAwsCredentialProviderLoader(getAwsSDKCredentialsProvider)
50
+ );
62
51
 
63
- function AwsSigv4Signer(opts = {}) {
64
- const credentialsState = {
65
- credentials: null,
66
- };
67
- if (!opts.region) {
68
- throw new AwsSigv4SignerError('Region cannot be empty');
69
- }
70
- if (!opts.service) {
71
- opts.service = 'es';
72
- }
73
- if (typeof opts.getCredentials !== 'function') {
74
- opts.getCredentials = awsDefaultCredentialsProvider;
75
- }
76
-
77
- function buildSignedRequestObject(request = {}) {
78
- request.service = opts.service;
79
- request.region = opts.region;
80
- request.headers = request.headers || {};
81
- request.headers['host'] = request.hostname;
82
-
83
- if (request['auth']) {
84
- const awssigv4Cred = request['auth'];
85
- credentialsState.credentials = {
86
- accessKeyId: awssigv4Cred.credentials.accessKeyId,
87
- secretAccessKey: awssigv4Cred.credentials.secretAccessKey,
88
- sessionToken: awssigv4Cred.credentials.sessionToken,
89
- };
90
- request.region = awssigv4Cred.region;
91
- request.service = awssigv4Cred.service;
92
- delete request['auth'];
93
- }
94
- const signed = aws4.sign(request, credentialsState.credentials);
95
- signed.headers['x-amz-content-sha256'] = crypto
96
- .createHash('sha256')
97
- .update(request.body || '', 'utf8')
98
- .digest('hex');
99
- return signed;
100
- }
101
-
102
- class AwsSigv4SignerConnection extends Connection {
103
- buildRequestObject(params) {
104
- const request = super.buildRequestObject(params);
105
- return buildSignedRequestObject(request);
106
- }
107
- }
108
-
109
- class AwsSigv4SignerTransport extends Transport {
110
- request(params, options = {}, callback = undefined) {
111
- // options is optional so if options is a function, it's the callback.
112
- if (typeof options === 'function') {
113
- callback = options;
114
- options = {};
115
- }
116
-
117
- const currentCredentials = credentialsState.credentials;
118
- /**
119
- * For AWS SDK V3
120
- * Make sure token will expire no earlier than `expiryBufferMs` milliseconds in the future.
121
- */
122
- const expiryBufferMs = toMs(options.requestTimeout || this.requestTimeout);
123
-
124
- let expired = false;
125
- if (!currentCredentials) {
126
- // Credentials haven't been acquired yet.
127
- expired = true;
128
- }
129
- // AWS SDK V2, needsRefresh should be available.
130
- else if (typeof currentCredentials.needsRefresh === 'function') {
131
- expired = currentCredentials.needsRefresh();
132
- }
133
- // AWS SDK V2, alternative to needsRefresh.
134
- else if (currentCredentials.expired === true) {
135
- expired = true;
136
- }
137
- // AWS SDK V2, alternative to needsRefresh and expired.
138
- else if (currentCredentials.expireTime && currentCredentials.expireTime < new Date()) {
139
- expired = true;
140
- }
141
- // AWS SDK V3, Credentials.expiration is a Date object
142
- else if (
143
- currentCredentials.expiration &&
144
- currentCredentials.expiration.getTime() - Date.now() < expiryBufferMs
145
- ) {
146
- expired = true;
147
- }
148
-
149
- if (!expired) {
150
- if (typeof callback === 'undefined') {
151
- return super.request(params, options);
152
- }
153
- super.request(params, options, callback);
154
- return;
155
- }
156
-
157
- // In AWS SDK V2 Credentials.refreshPromise should be available.
158
- if (currentCredentials && typeof currentCredentials.refreshPromise === 'function') {
159
- if (typeof callback === 'undefined') {
160
- return currentCredentials.refreshPromise().then(() => {
161
- return super.request(params, options);
162
- });
163
- } else {
164
- currentCredentials
165
- .refreshPromise()
166
- .then(() => {
167
- super.request(params, options, callback);
168
- })
169
- .catch(callback);
170
- return;
171
- }
172
- }
173
-
174
- // For AWS SDK V3 or when the client has not acquired credentials yet.
175
- if (typeof callback === 'undefined') {
176
- return opts.getCredentials().then((credentials) => {
177
- credentialsState.credentials = credentials;
178
- return super.request(params, options);
179
- });
180
- } else {
181
- opts
182
- .getCredentials()
183
- .then((credentials) => {
184
- credentialsState.credentials = credentials;
185
- super.request(params, options, callback);
186
- })
187
- .catch(callback);
188
- }
189
- }
190
- }
191
-
192
- return {
193
- Transport: AwsSigv4SignerTransport,
194
- Connection: AwsSigv4SignerConnection,
195
- buildSignedRequestObject,
196
- };
197
- }
198
52
  module.exports = AwsSigv4Signer;
@@ -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 };
@@ -0,0 +1,19 @@
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
+ 'use strict';
12
+
13
+ const AwsSigv4Signer = require('./AwsSigv4Signer-sdk-v3');
14
+ const AwsSigv4SignerError = require('./errors');
15
+
16
+ module.exports = {
17
+ AwsSigv4Signer,
18
+ AwsSigv4SignerError,
19
+ };
@@ -0,0 +1,172 @@
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
+ 'use strict';
12
+ const Connection = require('../Connection');
13
+ const Transport = require('../Transport');
14
+ const aws4 = require('aws4');
15
+ const AwsSigv4SignerError = require('./errors');
16
+ const crypto = require('crypto');
17
+ const { toMs } = Transport.internals;
18
+
19
+ function giveAwsCredentialProviderLoader(getAwsSDKCredentialsProvider) {
20
+ return function loadAwsCredentialProvider() {
21
+ return new Promise((resolve, reject) => {
22
+ getAwsSDKCredentialsProvider()
23
+ .then((provider) => {
24
+ provider().then(resolve).catch(reject);
25
+ })
26
+ .catch((err) => {
27
+ reject(err);
28
+ });
29
+ });
30
+ };
31
+ }
32
+
33
+ function giveAwsV4Signer(awsDefaultCredentialsProvider) {
34
+ return function AwsSigv4Signer(opts = {}) {
35
+ const credentialsState = {
36
+ credentials: null,
37
+ };
38
+ if (!opts.region) {
39
+ throw new AwsSigv4SignerError('Region cannot be empty');
40
+ }
41
+ if (!opts.service) {
42
+ opts.service = 'es';
43
+ }
44
+ if (typeof opts.getCredentials !== 'function') {
45
+ opts.getCredentials = awsDefaultCredentialsProvider;
46
+ }
47
+
48
+ function buildSignedRequestObject(request = {}) {
49
+ request.service = opts.service;
50
+ request.region = opts.region;
51
+ request.headers = request.headers || {};
52
+ request.headers['host'] = request.hostname;
53
+
54
+ if (request['auth']) {
55
+ const awssigv4Cred = request['auth'];
56
+ credentialsState.credentials = {
57
+ accessKeyId: awssigv4Cred.credentials.accessKeyId,
58
+ secretAccessKey: awssigv4Cred.credentials.secretAccessKey,
59
+ sessionToken: awssigv4Cred.credentials.sessionToken,
60
+ };
61
+ request.region = awssigv4Cred.region;
62
+ request.service = awssigv4Cred.service;
63
+ delete request['auth'];
64
+ }
65
+ const signed = aws4.sign(request, credentialsState.credentials);
66
+ signed.headers['x-amz-content-sha256'] = crypto
67
+ .createHash('sha256')
68
+ .update(request.body || '', 'utf8')
69
+ .digest('hex');
70
+ return signed;
71
+ }
72
+
73
+ class AwsSigv4SignerConnection extends Connection {
74
+ buildRequestObject(params) {
75
+ const request = super.buildRequestObject(params);
76
+ return buildSignedRequestObject(request);
77
+ }
78
+ }
79
+
80
+ class AwsSigv4SignerTransport extends Transport {
81
+ request(params, options = {}, callback = undefined) {
82
+ // options is optional so if options is a function, it's the callback.
83
+ if (typeof options === 'function') {
84
+ callback = options;
85
+ options = {};
86
+ }
87
+
88
+ const currentCredentials = credentialsState.credentials;
89
+ /**
90
+ * For AWS SDK V3
91
+ * Make sure token will expire no earlier than `expiryBufferMs` milliseconds in the future.
92
+ */
93
+ const expiryBufferMs = toMs(options.requestTimeout || this.requestTimeout);
94
+
95
+ let expired = false;
96
+ if (!currentCredentials) {
97
+ // Credentials haven't been acquired yet.
98
+ expired = true;
99
+ }
100
+ // AWS SDK V2, needsRefresh should be available.
101
+ else if (typeof currentCredentials.needsRefresh === 'function') {
102
+ expired = currentCredentials.needsRefresh();
103
+ }
104
+ // AWS SDK V2, alternative to needsRefresh.
105
+ else if (currentCredentials.expired === true) {
106
+ expired = true;
107
+ }
108
+ // AWS SDK V2, alternative to needsRefresh and expired.
109
+ else if (currentCredentials.expireTime && currentCredentials.expireTime < new Date()) {
110
+ expired = true;
111
+ }
112
+ // AWS SDK V3, Credentials.expiration is a Date object
113
+ else if (
114
+ currentCredentials.expiration &&
115
+ currentCredentials.expiration.getTime() - Date.now() < expiryBufferMs
116
+ ) {
117
+ expired = true;
118
+ }
119
+
120
+ if (!expired) {
121
+ if (typeof callback === 'undefined') {
122
+ return super.request(params, options);
123
+ }
124
+ super.request(params, options, callback);
125
+ return;
126
+ }
127
+
128
+ // In AWS SDK V2 Credentials.refreshPromise should be available.
129
+ if (currentCredentials && typeof currentCredentials.refreshPromise === 'function') {
130
+ if (typeof callback === 'undefined') {
131
+ return currentCredentials.refreshPromise().then(() => {
132
+ return super.request(params, options);
133
+ });
134
+ } else {
135
+ currentCredentials
136
+ .refreshPromise()
137
+ .then(() => {
138
+ super.request(params, options, callback);
139
+ })
140
+ .catch(callback);
141
+ return;
142
+ }
143
+ }
144
+
145
+ // For AWS SDK V3 or when the client has not acquired credentials yet.
146
+ if (typeof callback === 'undefined') {
147
+ return opts.getCredentials().then((credentials) => {
148
+ credentialsState.credentials = credentials;
149
+ return super.request(params, options);
150
+ });
151
+ } else {
152
+ opts
153
+ .getCredentials()
154
+ .then((credentials) => {
155
+ credentialsState.credentials = credentials;
156
+ super.request(params, options, callback);
157
+ })
158
+ .catch(callback);
159
+ }
160
+ }
161
+ }
162
+
163
+ return {
164
+ Transport: AwsSigv4SignerTransport,
165
+ Connection: AwsSigv4SignerConnection,
166
+ buildSignedRequestObject,
167
+ };
168
+ };
169
+ }
170
+
171
+ module.exports.giveAwsCredentialProviderLoader = giveAwsCredentialProviderLoader;
172
+ module.exports.giveAwsV4Signer = giveAwsV4Signer;
package/package.json CHANGED
@@ -16,6 +16,7 @@
16
16
  "types": "./api/types.d.ts"
17
17
  },
18
18
  "./aws": "./lib/aws/index.js",
19
+ "./aws-v3": "./lib/aws/index-v3.js",
19
20
  "./*": "./*"
20
21
  },
21
22
  "typesVersions": {
@@ -29,7 +30,7 @@
29
30
  }
30
31
  },
31
32
  "homepage": "https://www.opensearch.org/",
32
- "version": "2.7.0",
33
+ "version": "2.9.0",
33
34
  "versionCanary": "7.10.0-canary.6",
34
35
  "keywords": [
35
36
  "opensearch",
@@ -53,7 +54,7 @@
53
54
  "test:coverage-ui": "tap test/{unit,acceptance}/{*,**/*,**/**/*}.test.js --coverage --coverage-report=html --nyc-arg=\"--exclude=api\"",
54
55
  "lint": "eslint .",
55
56
  "lint:fix": "eslint . --fix",
56
- "license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;ISC;BSD-3-Clause;BSD-2-Clause'",
57
+ "license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;0BSD;ISC;BSD-3-Clause;BSD-2-Clause'",
57
58
  "build-esm": "npx gen-esm-wrapper . index.mjs && eslint --fix index.mjs"
58
59
  },
59
60
  "author": "opensearch-project",
@@ -97,6 +98,7 @@
97
98
  "xmlbuilder2": "^3.0.2"
98
99
  },
99
100
  "dependencies": {
101
+ "json11": "^1.0.4",
100
102
  "aws4": "^1.11.0",
101
103
  "debug": "^4.3.1",
102
104
  "hpagent": "^1.2.0",