Skip to content

Commit 5b6bc81

Browse files
authored
Pass the agent parameter to WebSocket to support accessing the target service through a proxy (#145)
1 parent 75a0b38 commit 5b6bc81

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@larksuiteoapi/node-sdk",
3-
"version": "1.51.0",
3+
"version": "1.52.0",
44
"description": "larksuite open sdk for nodejs",
55
"keywords": [
66
"feishu",

typings/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface HttpInstance {
2-
request<T = any, R = T, D = any>(opts: HttpRequestOptions<D>): Promise<R>;
2+
request<T = any, R = T, D = any>(opts: HttpRequestOptions<D>): Promise<R>;
33
get<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
44
delete<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
55
head<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;

ws-client/index.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface IConstructorParams {
2121
loggerLevel?: LoggerLevel;
2222
httpInstance?: HttpInstance;
2323
autoReconnect?: boolean;
24+
agent?: any;
2425
}
2526

2627
export class WSClient {
@@ -46,21 +47,23 @@ export class WSClient {
4647
}
4748

4849
constructor(params: IConstructorParams) {
49-
const {
50-
appId,
51-
appSecret,
52-
domain = Domain.Feishu,
50+
const {
51+
appId,
52+
appSecret,
53+
agent,
54+
domain = Domain.Feishu,
5355
httpInstance = defaultHttpInstance,
5456
loggerLevel = LoggerLevel.info,
5557
logger = defaultLogger,
5658
autoReconnect = true
5759
} = params;
5860

5961
this.logger = new LoggerProxy(loggerLevel, logger);
60-
62+
6163
assert(!appId, () => this.logger.error('appId is needed'));
6264
assert(!appSecret, () => this.logger.error('appSecret is needed'));
63-
65+
66+
this.agent = agent;
6467
this.dataCache = new DataCache({logger: this.logger});
6568
this.httpInstance = httpInstance;
6669
this.wsConfig.updateClient({
@@ -72,10 +75,10 @@ export class WSClient {
7275
this.wsConfig.updateWs({
7376
autoReconnect
7477
})
75-
}
78+
}
7679

7780
private async pullConnectConfig() {
78-
const {
81+
const {
7982
appId,
8083
appSecret
8184
} = this.wsConfig.getClient();
@@ -116,7 +119,7 @@ export class WSClient {
116119

117120
this.wsConfig.updateWs({
118121
connectUrl: URL,
119-
122+
120123
deviceId: device_id as string,
121124
serviceId: service_id as string,
122125

@@ -141,7 +144,8 @@ export class WSClient {
141144
let wsInstance;
142145

143146
try {
144-
wsInstance = new WebSocket(connectUrl);
147+
const { agent } = this;
148+
wsInstance = new WebSocket(connectUrl, { agent });
145149
} catch(e) {
146150
this.logger.error('[ws]', 'new WebSocket error');
147151
}
@@ -214,17 +218,17 @@ export class WSClient {
214218
}
215219

216220
const { autoReconnect, reconnectNonce, reconnectCount, reconnectInterval } = this.wsConfig.getWS();
217-
221+
218222
if (!autoReconnect) {
219223
return;
220224
}
221225

222226
this.logger.info('[ws]', 'reconnect');
223-
227+
224228
if (wsInstance) {
225229
wsInstance?.terminate();
226230
}
227-
231+
228232
this.wsConfig.setWSInstance(null);
229233

230234
const reconnectNonceTime = reconnectNonce ? reconnectNonce * Math.random() : 0
@@ -256,7 +260,7 @@ export class WSClient {
256260
}
257261

258262
private pingLoop() {
259-
const {
263+
const {
260264
serviceId,
261265
pingInterval
262266
} = this.wsConfig.getWS();
@@ -310,7 +314,7 @@ export class WSClient {
310314
private async handleControlData(data: pbbp2.Frame) {
311315
const type = data.headers.find(item => item.key === HeaderKey.type)?.value;
312316
const payload = data.payload;
313-
317+
314318
if (type === MessageType.ping) {
315319
return;
316320
}

0 commit comments

Comments
 (0)