Skip to content

Commit b827ff8

Browse files
committed
Pass the agent parameter to WebSocket to support accessing the target service through a proxy
1 parent 75a0b38 commit b827ff8

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
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: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import { AxiosDefaults } from 'axios';
2+
13
export interface HttpInstance {
24
request<T = any, R = T, D = any>(opts: HttpRequestOptions<D>): Promise<R>;
3-
get<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
4-
delete<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
5-
head<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
6-
options<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
7-
post<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
8-
put<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
9-
patch<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
5+
get<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
6+
delete<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
7+
head<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
8+
options<T = any, R = T, D = any>(url: string, opts?: HttpRequestOptions<D>): Promise<R>;
9+
post<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
10+
put<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
11+
patch<T = any, R = T, D = any>(url: string, data?: D, opts?: HttpRequestOptions<D>): Promise<R>;
12+
defaults: AxiosDefaults;
1013
}
1114

1215
export type ResponseType =
@@ -18,13 +21,13 @@ export type ResponseType =
1821
| 'stream';
1922

2023
export interface HttpRequestOptions<D> {
21-
url?: string;
22-
method?: string;
23-
headers?: Record<string, any>;
24-
params?: Record<string, any>;
25-
data?: D;
26-
responseType?: ResponseType;
27-
paramsSerializer?: (params: Record<string, any>) => string;
28-
timeout?: number;
29-
$return_headers?: boolean;
24+
url?: string;
25+
method?: string;
26+
headers?: Record<string, any>;
27+
params?: Record<string, any>;
28+
data?: D;
29+
responseType?: ResponseType;
30+
paramsSerializer?: (params: Record<string, any>) => string;
31+
timeout?: number;
32+
$return_headers?: boolean;
3033
}

ws-client/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ export class WSClient {
4646
}
4747

4848
constructor(params: IConstructorParams) {
49-
const {
50-
appId,
51-
appSecret,
52-
domain = Domain.Feishu,
49+
const {
50+
appId,
51+
appSecret,
52+
domain = Domain.Feishu,
5353
httpInstance = defaultHttpInstance,
5454
loggerLevel = LoggerLevel.info,
5555
logger = defaultLogger,
5656
autoReconnect = true
5757
} = params;
5858

5959
this.logger = new LoggerProxy(loggerLevel, logger);
60-
60+
6161
assert(!appId, () => this.logger.error('appId is needed'));
6262
assert(!appSecret, () => this.logger.error('appSecret is needed'));
63-
63+
6464
this.dataCache = new DataCache({logger: this.logger});
6565
this.httpInstance = httpInstance;
6666
this.wsConfig.updateClient({
@@ -72,10 +72,10 @@ export class WSClient {
7272
this.wsConfig.updateWs({
7373
autoReconnect
7474
})
75-
}
75+
}
7676

7777
private async pullConnectConfig() {
78-
const {
78+
const {
7979
appId,
8080
appSecret
8181
} = this.wsConfig.getClient();
@@ -116,7 +116,7 @@ export class WSClient {
116116

117117
this.wsConfig.updateWs({
118118
connectUrl: URL,
119-
119+
120120
deviceId: device_id as string,
121121
serviceId: service_id as string,
122122

@@ -141,7 +141,8 @@ export class WSClient {
141141
let wsInstance;
142142

143143
try {
144-
wsInstance = new WebSocket(connectUrl);
144+
const { httpsAgent, httpAgent } = this.httpInstance.defaults;
145+
wsInstance = new WebSocket(connectUrl, { agent: httpsAgent || httpAgent });
145146
} catch(e) {
146147
this.logger.error('[ws]', 'new WebSocket error');
147148
}
@@ -214,17 +215,17 @@ export class WSClient {
214215
}
215216

216217
const { autoReconnect, reconnectNonce, reconnectCount, reconnectInterval } = this.wsConfig.getWS();
217-
218+
218219
if (!autoReconnect) {
219220
return;
220221
}
221222

222223
this.logger.info('[ws]', 'reconnect');
223-
224+
224225
if (wsInstance) {
225226
wsInstance?.terminate();
226227
}
227-
228+
228229
this.wsConfig.setWSInstance(null);
229230

230231
const reconnectNonceTime = reconnectNonce ? reconnectNonce * Math.random() : 0
@@ -256,7 +257,7 @@ export class WSClient {
256257
}
257258

258259
private pingLoop() {
259-
const {
260+
const {
260261
serviceId,
261262
pingInterval
262263
} = this.wsConfig.getWS();
@@ -310,7 +311,7 @@ export class WSClient {
310311
private async handleControlData(data: pbbp2.Frame) {
311312
const type = data.headers.find(item => item.key === HeaderKey.type)?.value;
312313
const payload = data.payload;
313-
314+
314315
if (type === MessageType.ping) {
315316
return;
316317
}

0 commit comments

Comments
 (0)