@opentap/runner-client 2.3.2-alpha.1.6 → 2.3.2-alpha.1.8
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/lib/BaseClient.js +40 -34
- 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(
|
|
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.
|
|
@@ -148,39 +149,44 @@ var BaseClient = /** @class */ (function () {
|
|
|
148
149
|
}
|
|
149
150
|
// Put all the response chunks in an array in the order they are received
|
|
150
151
|
messages.push(message);
|
|
152
|
+
// If the chunk has a size equal to the chunkSize, we should expect another message
|
|
153
|
+
if (message.data.length === chunkSize) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
151
156
|
// If the chunk has a length smaller than the chunkSize, the message is complete
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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);
|
|
157
|
+
// If the final message was received, we can safely unsubscribe
|
|
158
|
+
subscription.unsubscribe();
|
|
159
|
+
// Check if the number of the final message is equal to the number of
|
|
160
|
+
// messages we received. If this is not the case, we dropped a chunk,
|
|
161
|
+
// likely due to a network error. In this case, the entire message is invalid.
|
|
162
|
+
var finalMessage = message;
|
|
163
|
+
var finalMessageNumber = (_a = finalMessage.headers) === null || _a === void 0 ? void 0 : _a.get('ChunkNumber');
|
|
164
|
+
if (!finalMessageNumber) {
|
|
165
|
+
return reject('Response is not a valid chunk.');
|
|
180
166
|
}
|
|
167
|
+
if (parseInt(finalMessageNumber) !== messages.length) {
|
|
168
|
+
return reject("Expected {finalMessageNumber} chunks, but received ".concat(messages.length, ". ") +
|
|
169
|
+
"The connection may have been interrupted.");
|
|
170
|
+
}
|
|
171
|
+
// Concatenate the payloads
|
|
172
|
+
// When there are many chunks, doing a single allocation
|
|
173
|
+
// is significantly faster than concatenating arrays in sequence
|
|
174
|
+
var dataArrays = messages.map(function (m) { return m.data; });
|
|
175
|
+
var flattenedSize = dataArrays.reduce(function (sum, array) { return sum + array.length; }, 0);
|
|
176
|
+
var flattenedArray = new Uint8Array(flattenedSize);
|
|
177
|
+
var k = 0;
|
|
178
|
+
dataArrays.map(function (m) {
|
|
179
|
+
for (var i = 0; i < m.length; i++) {
|
|
180
|
+
flattenedArray[k++] = m[i];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return resolve(flattenedArray);
|
|
181
184
|
};
|
|
182
185
|
})
|
|
183
186
|
.then(function (byteArray) {
|
|
187
|
+
if (byteArray.length === 0) {
|
|
188
|
+
return Promise.resolve(undefined);
|
|
189
|
+
}
|
|
184
190
|
var jsonCodec = JSONCodec();
|
|
185
191
|
// If a raw response is requested, we should avoid decoding the bytes.
|
|
186
192
|
var response = (options === null || options === void 0 ? void 0 : options.rawResponse) ? byteArray : jsonCodec.decode(byteArray);
|
|
@@ -205,10 +211,10 @@ var BaseClient = /** @class */ (function () {
|
|
|
205
211
|
this.connection.publish(subject, Empty, opts);
|
|
206
212
|
}
|
|
207
213
|
// Now that we have sent the terminating chunk, the result should arrive on our promise.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
214
|
+
return [2 /*return*/, this.withTimeout(responsePromise, timeout).catch(function (err) {
|
|
215
|
+
subscription.unsubscribe();
|
|
216
|
+
return Promise.reject(_this.natsErrorHandler(err, subject));
|
|
217
|
+
})];
|
|
212
218
|
});
|
|
213
219
|
});
|
|
214
220
|
};
|