Skip to content

Commit 126362e

Browse files
committed
feat: set max connections to 100, allow integrators to configure Fetch API
1 parent eb1f9a8 commit 126362e

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"@types/jsonwebtoken": "^9.0.3",
7474
"@types/node": "^20.11.24",
7575
"jsonwebtoken": "^9.0.2",
76+
"undici": "^5.6.0",
7677
"uuid": "^9.0.1"
7778
},
7879
"peerDependencies": {

src/BaseApi.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { v4 as uuidv4 } from 'uuid';
22
import { ApiConfig, RequestMetadata, StreamError } from './types';
33
import { APIError } from './gen/models';
44
import { getRateLimitFromResponseHeader } from './utils/rate-limit';
5+
import { Agent } from 'undici';
56

67
export class BaseApi {
7-
constructor(protected readonly apiConfig: ApiConfig) {}
8+
private readonly dispatcher: Agent;
9+
10+
constructor(protected readonly apiConfig: ApiConfig) {
11+
this.dispatcher = this.apiConfig.agent;
12+
}
813

914
protected sendRequest = async <T>(
1015
method: string,
@@ -21,6 +26,7 @@ export class BaseApi {
2126
url = url.replace(`{${paramName}}`, pathParams[paramName]);
2227
});
2328
}
29+
2430
url += `?${encodedParams}`;
2531
const clientRequestId = uuidv4();
2632
const headers = {
@@ -40,6 +46,8 @@ export class BaseApi {
4046
method,
4147
body: JSON.stringify(body),
4248
headers,
49+
/** @ts-expect-error we get types from DOM here, but we should use node types */
50+
dispatcher: this.dispatcher,
4351
});
4452

4553
const responseHeaders = response.headers;

src/StreamClient.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { StreamChatClient } from './StreamChatClient';
66
import { CallTokenPayload, UserTokenPayload } from './types';
77
import { QueryBannedUsersPayload, UserRequest } from './gen/models';
88
import { StreamModerationClient } from './StreamModerationClient';
9+
import { Agent } from 'undici';
910

1011
export interface StreamClientOptions {
1112
timeout?: number;
1213
basePath?: string;
14+
/** The max number of clients to create. `null` if no limit. Default is 100. Has no effect if `agent` is provided. */
15+
maxConnections?: number | null;
16+
/** The [Agent](https://undici.nodejs.org/#/docs/api/Agent.md) to use. */
17+
agent?: Agent;
1318
}
1419

1520
export class StreamClient extends CommonApi {
@@ -19,6 +24,7 @@ export class StreamClient extends CommonApi {
1924
public readonly options: StreamClientOptions = {};
2025

2126
private static readonly DEFAULT_TIMEOUT = 3000;
27+
private static readonly MAX_CONNECTIONS = 100;
2228

2329
/**
2430
*
@@ -33,28 +39,39 @@ export class StreamClient extends CommonApi {
3339
) {
3440
const token = JWTServerToken(secret);
3541
const timeout = config?.timeout ?? StreamClient.DEFAULT_TIMEOUT;
42+
const agent =
43+
config?.agent ??
44+
new Agent({
45+
connections:
46+
config?.maxConnections === undefined
47+
? StreamClient.MAX_CONNECTIONS
48+
: config.maxConnections,
49+
});
3650
const chatBaseUrl = config?.basePath ?? 'https://chat.stream-io-api.com';
3751
const videoBaseUrl = config?.basePath ?? 'https://video.stream-io-api.com';
38-
super({ apiKey, token, timeout, baseUrl: chatBaseUrl });
52+
super({ apiKey, token, timeout, baseUrl: chatBaseUrl, agent });
3953

4054
this.video = new StreamVideoClient({
4155
streamClient: this,
4256
apiKey,
4357
token,
4458
timeout,
4559
baseUrl: videoBaseUrl,
60+
agent,
4661
});
4762
this.chat = new StreamChatClient({
4863
apiKey,
4964
token,
5065
timeout,
5166
baseUrl: chatBaseUrl,
67+
agent,
5268
});
5369
this.moderation = new StreamModerationClient({
5470
apiKey,
5571
token,
5672
timeout,
5773
baseUrl: chatBaseUrl,
74+
agent,
5875
});
5976
}
6077

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { Agent } from 'undici';
2+
13
export type OmitTypeId<T> = Omit<T, 'type' | 'id' | 'connection_id'>;
24

35
export interface ApiConfig {
46
apiKey: string;
57
token: string;
68
baseUrl: string;
9+
/** The timeout for requests in milliseconds. The default is 3000. */
710
timeout: number;
11+
agent: Agent;
812
}
913

1014
export interface RequestMetadata {

yarn.lock

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@
271271
resolved "https://registry.npmjs.org/@eslint/js/-/js-8.54.0.tgz"
272272
integrity sha512-ut5V+D+fOoWPgGGNj83GGjnntO39xDy6DWxO0wb7Jp3DcMX0TfIqdzHF85VTQkerdyGmuuMD9AKAo5KiNlf/AQ==
273273

274+
"@fastify/busboy@^2.0.0":
275+
version "2.1.1"
276+
resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.1.1.tgz#b9da6a878a371829a0502c9b6c1c143ef6663f4d"
277+
integrity sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==
278+
274279
"@humanwhocodes/config-array@^0.11.13":
275280
version "0.11.13"
276281
resolved "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.13.tgz"
@@ -365,7 +370,7 @@
365370
consola "^2.15.0"
366371
node-fetch "^2.6.1"
367372

368-
"@openai/realtime-api-beta@github:openai/openai-realtime-api-beta#a5cb94824f625423858ebacb9f769226ca98945f":
373+
"@openai/realtime-api-beta@openai/openai-realtime-api-beta#a5cb94824f625423858ebacb9f769226ca98945f":
369374
version "0.0.0"
370375
resolved "https://codeload.github.com/openai/openai-realtime-api-beta/tar.gz/a5cb94824f625423858ebacb9f769226ca98945f"
371376
dependencies:
@@ -524,11 +529,11 @@
524529
integrity sha512-LT+OIXpp2kj4E2S/p91BMe+VgGX2+lfO+XTpfXhh+bCk2LkQtHZSub8ewFBMGP5ClysPjTDFa4sMI8Q3n4T0wg==
525530

526531
"@types/node@^20.11.24":
527-
version "20.11.24"
528-
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.11.24.tgz#cc207511104694e84e9fb17f9a0c4c42d4517792"
529-
integrity sha512-Kza43ewS3xoLgCEpQrsT+xRo/EJej1y0kVYGiLFE1NEODXGzTfwiC6tXTLMQskn1X4/Rjlh0MQUvx9W+L9long==
532+
version "20.17.30"
533+
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.17.30.tgz#1d93f656d3b869dbef7b796568ac457606ba58d0"
534+
integrity sha512-7zf4YyHA+jvBNfVrk2Gtvs6x7E8V+YDW05bNfG2XkWDJfYRXrTiP/DsB2zSYTaHX0bGIujTBQdMVAhb+j7mwpg==
530535
dependencies:
531-
undici-types "~5.26.4"
536+
undici-types "~6.19.2"
532537

533538
"@types/semver@^7.5.0":
534539
version "7.5.6"
@@ -3311,10 +3316,17 @@ unbox-primitive@^1.0.2:
33113316
has-symbols "^1.0.3"
33123317
which-boxed-primitive "^1.0.2"
33133318

3314-
undici-types@~5.26.4:
3315-
version "5.26.5"
3316-
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
3317-
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==
3319+
undici-types@~6.19.2:
3320+
version "6.19.8"
3321+
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02"
3322+
integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==
3323+
3324+
undici@^5.6.0:
3325+
version "5.29.0"
3326+
resolved "https://registry.yarnpkg.com/undici/-/undici-5.29.0.tgz#419595449ae3f2cdcba3580a2e8903399bd1f5a3"
3327+
integrity sha512-raqeBD6NQK4SkWhQzeYKd1KmIG6dllBOTt55Rmkt4HtI9mwdWtJljnrXjAFUBLTSN67HWrOIZ3EPF4kjUw80Bg==
3328+
dependencies:
3329+
"@fastify/busboy" "^2.0.0"
33183330

33193331
universalify@^2.0.0:
33203332
version "2.0.0"

0 commit comments

Comments
 (0)