@opentap/runner-client 2.3.2-alpha.1.5 → 2.3.2-alpha.1.6
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.d.ts +1 -1
- package/lib/BaseClient.js +22 -11
- package/package.json +1 -1
package/lib/BaseClient.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ComponentSettingsBase, ComponentSettingsIdentifier, ComponentSettingsListItem, DataGridControl, ErrorResponse, FileParameter, FileResponse, ListItemType, ProfileGroup, RepositoryPackageReference, RepositorySettingsPackageDefinition, SettingsTapPackage } from './DTOs';
|
|
2
2
|
import { ConnectionOptions, Subscription, SubscriptionOptions, PublishOptions } from 'nats.ws';
|
|
3
3
|
interface BaseClientRequestOptions {
|
|
4
|
-
|
|
4
|
+
publishOptions?: PublishOptions;
|
|
5
5
|
rawResponse?: boolean;
|
|
6
6
|
fullSubject?: boolean;
|
|
7
7
|
timeout?: number;
|
package/lib/BaseClient.js
CHANGED
|
@@ -109,10 +109,11 @@ var BaseClient = /** @class */ (function () {
|
|
|
109
109
|
* @returns Promise of an object
|
|
110
110
|
*/
|
|
111
111
|
BaseClient.prototype.request = function (subject, payload, options) {
|
|
112
|
+
var _a, _b, _c;
|
|
112
113
|
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
-
var data, headers, replySubject,
|
|
114
|
+
var data, headers, replySubject, chunkSize, requestId, opts, fileDescriptor, chunkNumber, getChunk, subscription, responsePromise, chunk, i;
|
|
114
115
|
var _this = this;
|
|
115
|
-
return __generator(this, function (
|
|
116
|
+
return __generator(this, function (_d) {
|
|
116
117
|
// Prepend the base subject if the given subject does not start with that
|
|
117
118
|
if (!(options === null || options === void 0 ? void 0 : options.fullSubject)) {
|
|
118
119
|
subject = "".concat(this.baseSubject, ".Request.").concat(subject);
|
|
@@ -124,20 +125,20 @@ var BaseClient = /** @class */ (function () {
|
|
|
124
125
|
data = this.encode(payload);
|
|
125
126
|
headers = this.buildHeaders();
|
|
126
127
|
replySubject = crypto.randomUUID();
|
|
127
|
-
|
|
128
|
-
|
|
128
|
+
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
|
+
// The Session and the Client need to agree on the chunk size. Put it in a header.
|
|
130
|
+
headers.append('ChunkSize', chunkSize.toString());
|
|
129
131
|
requestId = crypto.randomUUID();
|
|
130
132
|
headers.append('RequestId', requestId);
|
|
131
|
-
opts = __assign(__assign({}, options === null || options === void 0 ? void 0 : options.
|
|
132
|
-
fileDescriptor = new FileDescriptor(data.length,
|
|
133
|
+
opts = __assign(__assign({}, options === null || options === void 0 ? void 0 : options.publishOptions), { headers: headers, reply: replySubject });
|
|
134
|
+
fileDescriptor = new FileDescriptor(data.length, chunkSize);
|
|
133
135
|
chunkNumber = 1;
|
|
134
136
|
headers.set('ChunkNumber', chunkNumber.toString());
|
|
135
|
-
subscription = this.connection.subscribe(replySubject);
|
|
136
137
|
getChunk = function (chunk) {
|
|
137
138
|
var offset = chunk * fileDescriptor.chunkSize;
|
|
138
139
|
return data.slice(offset, offset + fileDescriptor.chunkSize);
|
|
139
140
|
};
|
|
140
|
-
|
|
141
|
+
subscription = this.connection.subscribe(replySubject);
|
|
141
142
|
responsePromise = new Promise(function (resolve, reject) {
|
|
142
143
|
var messages = [];
|
|
143
144
|
subscription.callback = function (error, message) {
|
|
@@ -145,9 +146,15 @@ var BaseClient = /** @class */ (function () {
|
|
|
145
146
|
if (error) {
|
|
146
147
|
reject(error);
|
|
147
148
|
}
|
|
149
|
+
// Put all the response chunks in an array in the order they are received
|
|
148
150
|
messages.push(message);
|
|
149
|
-
|
|
151
|
+
// 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
|
|
150
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.
|
|
151
158
|
var finalMessage = messages[messages.length - 1];
|
|
152
159
|
var finalMessageNumber = (_a = finalMessage.headers) === null || _a === void 0 ? void 0 : _a.get('ChunkNumber');
|
|
153
160
|
if (!finalMessageNumber) {
|
|
@@ -158,9 +165,11 @@ var BaseClient = /** @class */ (function () {
|
|
|
158
165
|
"The connection may have been interrupted.");
|
|
159
166
|
}
|
|
160
167
|
// Concatenate the payloads
|
|
168
|
+
// When there are many chunks, doing a single allocation
|
|
169
|
+
// is significantly faster than concatenating arrays in sequence
|
|
161
170
|
var dataArrays = messages.map(function (m) { return m.data; });
|
|
162
|
-
var
|
|
163
|
-
var flattenedArray_1 = new Uint8Array(
|
|
171
|
+
var flattenedSize = dataArrays.reduce(function (sum, array) { return sum + array.length; }, 0);
|
|
172
|
+
var flattenedArray_1 = new Uint8Array(flattenedSize);
|
|
164
173
|
var k_1 = 0;
|
|
165
174
|
dataArrays.map(function (m) {
|
|
166
175
|
for (var i = 0; i < m.length; i++) {
|
|
@@ -172,6 +181,8 @@ var BaseClient = /** @class */ (function () {
|
|
|
172
181
|
};
|
|
173
182
|
})
|
|
174
183
|
.then(function (byteArray) {
|
|
184
|
+
var jsonCodec = JSONCodec();
|
|
185
|
+
// If a raw response is requested, we should avoid decoding the bytes.
|
|
175
186
|
var response = (options === null || options === void 0 ? void 0 : options.rawResponse) ? byteArray : jsonCodec.decode(byteArray);
|
|
176
187
|
return _this.isErrorResponse(response) ? Promise.reject(ErrorResponse.fromJS(response)) : Promise.resolve(response);
|
|
177
188
|
})
|