-
Notifications
You must be signed in to change notification settings - Fork 25
/
streamingClient.ts
312 lines (278 loc) · 8.98 KB
/
streamingClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Client } from 'faye';
import { Connection } from '@salesforce/core';
import {
RetrieveResultsInterval,
StreamMessage,
StreamingErrors,
TestResultMessage
} from './types';
import { Progress } from '../common';
import { nls } from '../i18n';
import { refreshAuth } from '../utils';
import {
ApexTestProgressValue,
ApexTestQueueItem,
ApexTestQueueItemRecord,
ApexTestQueueItemStatus
} from '../tests/types';
import { elapsedTime } from '../utils/elapsedTime';
const TEST_RESULT_CHANNEL = '/systemTopic/TestResult';
const DEFAULT_STREAMING_TIMEOUT_MS = 14400;
export interface AsyncTestRun {
runId: string;
queueItem: ApexTestQueueItem;
}
export class Deferred<T> {
public promise: Promise<T>;
public resolve: Function;
constructor() {
this.promise = new Promise((resolve) => (this.resolve = resolve));
}
}
export class StreamingClient {
// This should be a Client from Faye, but I'm not sure how to get around the type
// that is exported from jsforce.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private client: any;
private conn: Connection;
private progress?: Progress<ApexTestProgressValue>;
private apiVersion = '36.0';
public subscribedTestRunId: string;
private subscribedTestRunIdDeferred = new Deferred<string>();
public get subscribedTestRunIdPromise(): Promise<string> {
return this.subscribedTestRunIdDeferred.promise;
}
private removeTrailingSlashURL(instanceUrl?: string): string {
return instanceUrl ? instanceUrl.replace(/\/+$/, '') : '';
}
public getStreamURL(instanceUrl: string): string {
const urlElements = [
this.removeTrailingSlashURL(instanceUrl),
'cometd',
this.apiVersion
];
return urlElements.join('/');
}
public constructor(
connection: Connection,
progress?: Progress<ApexTestProgressValue>
) {
this.conn = connection;
this.progress = progress;
const streamUrl = this.getStreamURL(this.conn.instanceUrl);
this.client = new Client(streamUrl, {
timeout: DEFAULT_STREAMING_TIMEOUT_MS
});
this.client.on('transport:up', () => {
this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingTransportUp',
message: nls.localize('streamingTransportUp')
});
});
this.client.on('transport:down', () => {
this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingTransportDown',
message: nls.localize('streamingTransportDown')
});
});
this.client.addExtension({
incoming: async (
message: StreamMessage,
callback: (message: StreamMessage) => void
) => {
if (message && message.error) {
// throw errors on handshake errors
if (message.channel === '/meta/handshake') {
this.disconnect();
throw new Error(
nls.localize('streamingHandshakeFail', message.error)
);
}
// refresh auth on 401 errors
if (message.error === StreamingErrors.ERROR_AUTH_INVALID) {
await this.init();
callback(message);
return;
}
// call faye callback on handshake advice
if (message.advice && message.advice.reconnect === 'handshake') {
callback(message);
return;
}
// call faye callback on 403 unknown client errors
if (message.error === StreamingErrors.ERROR_UNKNOWN_CLIENT_ID) {
callback(message);
return;
}
// default: disconnect and throw error
this.disconnect();
throw new Error(message.error);
}
callback(message);
}
});
}
// NOTE: There's an intermittent auth issue with Streaming API that requires the connection to be refreshed
// The builtin org.refreshAuth() util only refreshes the connection associated with the instance of the org you provide, not all connections associated with that username's orgs
public async init(): Promise<void> {
await refreshAuth(this.conn);
const accessToken = this.conn.getConnectionOptions().accessToken;
if (accessToken) {
this.client.setHeader('Authorization', `OAuth ${accessToken}`);
} else {
throw new Error(nls.localize('noAccessTokenFound'));
}
}
public handshake(): Promise<void> {
return new Promise((resolve) => {
this.client.handshake(() => {
resolve();
});
});
}
public disconnect(): void {
this.client.disconnect();
this.hasDisconnected = true;
}
public hasDisconnected = false;
@elapsedTime()
public async subscribe(
action?: () => Promise<string>,
testRunId?: string
): Promise<AsyncTestRun> {
return new Promise((subscriptionResolve, subscriptionReject) => {
let intervalId: NodeJS.Timeout;
try {
this.client.subscribe(
TEST_RESULT_CHANNEL,
async (message: TestResultMessage) => {
const result = await this.handler(message);
if (result) {
this.disconnect();
clearInterval(intervalId);
subscriptionResolve({
runId: this.subscribedTestRunId,
queueItem: result
});
}
}
);
if (action) {
action()
.then((id) => {
this.subscribedTestRunId = id;
this.subscribedTestRunIdDeferred.resolve(id);
if (!this.hasDisconnected) {
intervalId = setInterval(async () => {
const result = await this.getCompletedTestRun(id);
if (result) {
this.disconnect();
clearInterval(intervalId);
subscriptionResolve({
runId: this.subscribedTestRunId,
queueItem: result
});
}
}, RetrieveResultsInterval);
}
})
.catch((e) => {
this.disconnect();
clearInterval(intervalId);
subscriptionReject(e);
});
} else {
this.subscribedTestRunId = testRunId;
this.subscribedTestRunIdDeferred.resolve(testRunId);
if (!this.hasDisconnected) {
intervalId = setInterval(async () => {
const result = await this.getCompletedTestRun(testRunId);
if (result) {
this.disconnect();
clearInterval(intervalId);
subscriptionResolve({
runId: this.subscribedTestRunId,
queueItem: result
});
}
}, RetrieveResultsInterval);
}
}
} catch (e) {
this.disconnect();
clearInterval(intervalId);
subscriptionReject(e);
}
});
}
private isValidTestRunID(testRunId: string, subscribedId?: string): boolean {
if (testRunId.length !== 15 && testRunId.length !== 18) {
return false;
}
const testRunId15char = testRunId.substring(0, 14);
if (subscribedId) {
const subscribedTestRunId15char = subscribedId.substring(0, 14);
return subscribedTestRunId15char === testRunId15char;
}
return true;
}
@elapsedTime()
public async handler(
message?: TestResultMessage,
runId?: string
): Promise<ApexTestQueueItem> {
const testRunId = runId || message.sobject.Id;
if (!this.isValidTestRunID(testRunId, this.subscribedTestRunId)) {
return null;
}
const result = await this.getCompletedTestRun(testRunId);
if (result) {
return result;
}
this.progress?.report({
type: 'StreamingClientProgress',
value: 'streamingProcessingTestRun',
message: nls.localize('streamingProcessingTestRun', testRunId),
testRunId
});
return null;
}
private async getCompletedTestRun(
testRunId: string
): Promise<ApexTestQueueItem> {
const queryApexTestQueueItem = `SELECT Id, Status, ApexClassId, TestRunResultId FROM ApexTestQueueItem WHERE ParentJobId = '${testRunId}'`;
const result = await this.conn.tooling.query<ApexTestQueueItemRecord>(
queryApexTestQueueItem,
{
autoFetch: true
}
);
if (result.records.length === 0) {
throw new Error(nls.localize('noTestQueueResults', testRunId));
}
this.progress?.report({
type: 'TestQueueProgress',
value: result
});
if (
result.records.some(
(item) =>
item.Status === ApexTestQueueItemStatus.Queued ||
item.Status === ApexTestQueueItemStatus.Holding ||
item.Status === ApexTestQueueItemStatus.Preparing ||
item.Status === ApexTestQueueItemStatus.Processing
)
) {
return null;
}
return result;
}
}