@opentap/runner-client 2.3.2-alpha.1.6 → 2.3.2-alpha.1.7

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 +40 -35
  2. package/package.json +1 -1
package/lib/BaseClient.js CHANGED
@@ -99,7 +99,7 @@ var BaseClient = /** @class */ (function () {
99
99
  configurable: true
100
100
  });
101
101
  BaseClient.prototype.withTimeout = function (promise, timeout) {
102
- return Promise.race([promise, new Promise(function (_, reject) { return setTimeout(function () { return reject(new Error('Request timed out.')); }, timeout); })]);
102
+ return Promise.race([promise, new Promise(function (_, reject) { return setTimeout(function () { return reject(new Error(ErrorCode.Timeout)); }, timeout); })]);
103
103
  };
104
104
  /**
105
105
  * Send a request to the nats server.
@@ -111,7 +111,7 @@ var BaseClient = /** @class */ (function () {
111
111
  BaseClient.prototype.request = function (subject, payload, options) {
112
112
  var _a, _b, _c;
113
113
  return __awaiter(this, void 0, void 0, function () {
114
- var data, headers, replySubject, chunkSize, requestId, opts, fileDescriptor, chunkNumber, getChunk, subscription, responsePromise, chunk, i;
114
+ var data, headers, timeout, replySubject, chunkSize, requestId, opts, fileDescriptor, chunkNumber, getChunk, subscription, responsePromise, chunk, i;
115
115
  var _this = this;
116
116
  return __generator(this, function (_d) {
117
117
  // Prepend the base subject if the given subject does not start with that
@@ -124,6 +124,7 @@ var BaseClient = /** @class */ (function () {
124
124
  return [2 /*return*/, Promise.reject("".concat(subject, ": Connection has been closed! Please reconnect!"))];
125
125
  data = this.encode(payload);
126
126
  headers = this.buildHeaders();
127
+ timeout = (options === null || options === void 0 ? void 0 : options.timeout) || this.timeout;
127
128
  replySubject = crypto.randomUUID();
128
129
  chunkSize = ((_c = (_b = (_a = this.connection) === null || _a === void 0 ? void 0 : _a.info) === null || _b === void 0 ? void 0 : _b.max_payload) !== null && _c !== void 0 ? _c : 512000) - 2000;
129
130
  // The Session and the Client need to agree on the chunk size. Put it in a header.
@@ -138,49 +139,56 @@ var BaseClient = /** @class */ (function () {
138
139
  var offset = chunk * fileDescriptor.chunkSize;
139
140
  return data.slice(offset, offset + fileDescriptor.chunkSize);
140
141
  };
141
- subscription = this.connection.subscribe(replySubject);
142
+ subscription = this.connection.subscribe(replySubject, { timeout: timeout });
142
143
  responsePromise = new Promise(function (resolve, reject) {
143
144
  var messages = [];
144
145
  subscription.callback = function (error, message) {
145
146
  var _a;
146
147
  if (error) {
148
+ subscription.unsubscribe();
149
+ console.log('error:', error);
147
150
  reject(error);
148
151
  }
149
152
  // Put all the response chunks in an array in the order they are received
150
153
  messages.push(message);
154
+ // If the chunk has a size equal to the chunkSize, we should expect another message
155
+ if (message.data.length === chunkSize) {
156
+ return;
157
+ }
151
158
  // If the chunk has a length smaller than the chunkSize, the message is complete
152
- if (message.data.length < chunkSize) {
153
- // If the final message was received, we can safely unsubscribe
154
- subscription.unsubscribe();
155
- // Check if the number of the final message is equal to the number of
156
- // messages we received. If this is not the case, we dropped a chunk,
157
- // likely due to a network error. In this case, the entire message is invalid.
158
- var finalMessage = messages[messages.length - 1];
159
- var finalMessageNumber = (_a = finalMessage.headers) === null || _a === void 0 ? void 0 : _a.get('ChunkNumber');
160
- if (!finalMessageNumber) {
161
- return reject('Response is not a valid chunk.');
162
- }
163
- if (parseInt(finalMessageNumber) !== messages.length) {
164
- return reject("Expected {finalMessageNumber} chunks, but received ".concat(messages.length, ". ") +
165
- "The connection may have been interrupted.");
166
- }
167
- // Concatenate the payloads
168
- // When there are many chunks, doing a single allocation
169
- // is significantly faster than concatenating arrays in sequence
170
- var dataArrays = messages.map(function (m) { return m.data; });
171
- var flattenedSize = dataArrays.reduce(function (sum, array) { return sum + array.length; }, 0);
172
- var flattenedArray_1 = new Uint8Array(flattenedSize);
173
- var k_1 = 0;
174
- dataArrays.map(function (m) {
175
- for (var i = 0; i < m.length; i++) {
176
- flattenedArray_1[k_1++] = m[i];
177
- }
178
- });
179
- return resolve(flattenedArray_1);
159
+ // If the final message was received, we can safely unsubscribe
160
+ subscription.unsubscribe();
161
+ // Check if the number of the final message is equal to the number of
162
+ // messages we received. If this is not the case, we dropped a chunk,
163
+ // likely due to a network error. In this case, the entire message is invalid.
164
+ var finalMessage = message;
165
+ var finalMessageNumber = (_a = finalMessage.headers) === null || _a === void 0 ? void 0 : _a.get('ChunkNumber');
166
+ if (!finalMessageNumber) {
167
+ return reject('Response is not a valid chunk.');
168
+ }
169
+ if (parseInt(finalMessageNumber) !== messages.length) {
170
+ return reject("Expected {finalMessageNumber} chunks, but received ".concat(messages.length, ". ") +
171
+ "The connection may have been interrupted.");
180
172
  }
173
+ // Concatenate the payloads
174
+ // When there are many chunks, doing a single allocation
175
+ // is significantly faster than concatenating arrays in sequence
176
+ var dataArrays = messages.map(function (m) { return m.data; });
177
+ var flattenedSize = dataArrays.reduce(function (sum, array) { return sum + array.length; }, 0);
178
+ var flattenedArray = new Uint8Array(flattenedSize);
179
+ var k = 0;
180
+ dataArrays.map(function (m) {
181
+ for (var i = 0; i < m.length; i++) {
182
+ flattenedArray[k++] = m[i];
183
+ }
184
+ });
185
+ return resolve(flattenedArray);
181
186
  };
182
187
  })
183
188
  .then(function (byteArray) {
189
+ if (byteArray.length === 0) {
190
+ return Promise.resolve(undefined);
191
+ }
184
192
  var jsonCodec = JSONCodec();
185
193
  // If a raw response is requested, we should avoid decoding the bytes.
186
194
  var response = (options === null || options === void 0 ? void 0 : options.rawResponse) ? byteArray : jsonCodec.decode(byteArray);
@@ -205,10 +213,7 @@ var BaseClient = /** @class */ (function () {
205
213
  this.connection.publish(subject, Empty, opts);
206
214
  }
207
215
  // Now that we have sent the terminating chunk, the result should arrive on our promise.
208
- if (options === null || options === void 0 ? void 0 : options.timeout) {
209
- return [2 /*return*/, this.withTimeout(responsePromise, options.timeout)];
210
- }
211
- return [2 /*return*/, responsePromise];
216
+ return [2 /*return*/, this.withTimeout(responsePromise, timeout)];
212
217
  });
213
218
  });
214
219
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentap/runner-client",
3
- "version": "2.3.2-alpha.1.6",
3
+ "version": "2.3.2-alpha.1.7",
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",