@opentap/runner-client 2.14.0-alpha.1.4 → 2.15.0-alpha.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/BaseClient.js +14 -13
  2. package/package.json +1 -1
package/lib/BaseClient.js CHANGED
@@ -145,13 +145,20 @@ var BaseClient = /** @class */ (function () {
145
145
  responsePromise = new Promise(function (resolve, reject) {
146
146
  var messages = [];
147
147
  subscription.callback = function (error, message) {
148
- var _a, _b, _c;
148
+ var _a, _b, _c, _d;
149
149
  if (error) {
150
150
  return reject(error);
151
151
  }
152
152
  if (((_a = message.headers) === null || _a === void 0 ? void 0 : _a.code) === 503) {
153
153
  return reject(Error('No responders.'));
154
154
  }
155
+ // If the message isn't chunked, we can assume the message is finished after a single message has been received.
156
+ var chunkNumber = (_b = message.headers) === null || _b === void 0 ? void 0 : _b.get('ChunkNumber');
157
+ // ChunkNumber starts from 1, so this check should be correct
158
+ if (!chunkNumber) {
159
+ subscription.unsubscribe();
160
+ return resolve({ byteArray: message.data, isErrorResponse: ((_c = message.headers) === null || _c === void 0 ? void 0 : _c.get('OpenTapNatsError')) === 'true' });
161
+ }
155
162
  // Put all the response chunks in an array in the order they are received
156
163
  messages.push(message);
157
164
  // If the chunk has a size equal to the chunkSize, we should expect another message
@@ -164,28 +171,22 @@ var BaseClient = /** @class */ (function () {
164
171
  // Check if the number of the final message is equal to the number of
165
172
  // messages we received. If this is not the case, we dropped a chunk,
166
173
  // likely due to a network error. In this case, the entire message is invalid.
167
- var finalMessage = message;
168
- var finalMessageNumber = (_b = finalMessage.headers) === null || _b === void 0 ? void 0 : _b.get('ChunkNumber');
169
- if (!finalMessageNumber) {
170
- return reject(Error('Response is not a valid chunk.'));
171
- }
172
- if (parseInt(finalMessageNumber) !== messages.length) {
174
+ if (parseInt(chunkNumber) !== messages.length) {
173
175
  return reject(Error("Expected {finalMessageNumber} chunks, but received ".concat(messages.length, ". ") +
174
176
  "The connection may have been interrupted."));
175
177
  }
176
178
  // Concatenate the payloads
177
179
  // When there are many chunks, doing a single allocation
178
180
  // is significantly faster than concatenating arrays in sequence
179
- var dataArrays = messages.map(function (m) { return m.data; });
180
- var flattenedSize = dataArrays.reduce(function (sum, array) { return sum + array.length; }, 0);
181
+ var flattenedSize = messages.reduce(function (sum, array) { return sum + array.data.length; }, 0);
181
182
  var flattenedArray = new Uint8Array(flattenedSize);
182
183
  var k = 0;
183
- dataArrays.map(function (m) {
184
- for (var i = 0; i < m.length; i++) {
185
- flattenedArray[k++] = m[i];
184
+ messages.forEach(function (m) {
185
+ for (var i = 0; i < m.data.length; i++) {
186
+ flattenedArray[k++] = m.data[i];
186
187
  }
187
188
  });
188
- return resolve({ byteArray: flattenedArray, isErrorResponse: ((_c = finalMessage.headers) === null || _c === void 0 ? void 0 : _c.get('OpenTapNatsError')) === 'true' });
189
+ return resolve({ byteArray: flattenedArray, isErrorResponse: ((_d = message.headers) === null || _d === void 0 ? void 0 : _d.get('OpenTapNatsError')) === 'true' });
189
190
  };
190
191
  }).then(function (_a) {
191
192
  var byteArray = _a.byteArray, isErrorResponse = _a.isErrorResponse;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentap/runner-client",
3
- "version": "2.14.0-alpha.1.4",
3
+ "version": "2.15.0-alpha.1.2",
4
4
  "description": "This is the web client for the OpenTAP Runner.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",