@trpc/client 11.0.0-rc.772 → 11.0.0-rc.781
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/dist/bundle-analysis.json +132 -39
- package/dist/index.js +3 -2
- package/dist/index.mjs +2 -1
- package/dist/links/wsLink/createWsClient.d.ts +6 -0
- package/dist/links/wsLink/createWsClient.d.ts.map +1 -0
- package/dist/links/wsLink/createWsClient.js +9 -0
- package/dist/links/wsLink/createWsClient.mjs +7 -0
- package/dist/links/wsLink/wsClient/options.d.ts +79 -0
- package/dist/links/wsLink/wsClient/options.d.ts.map +1 -0
- package/dist/links/wsLink/wsClient/options.js +22 -0
- package/dist/links/wsLink/wsClient/options.mjs +18 -0
- package/dist/links/wsLink/wsClient/requestManager.d.ts +102 -0
- package/dist/links/wsLink/wsClient/requestManager.d.ts.map +1 -0
- package/dist/links/wsLink/wsClient/requestManager.js +138 -0
- package/dist/links/wsLink/wsClient/requestManager.mjs +136 -0
- package/dist/links/wsLink/wsClient/utils.d.ts +38 -0
- package/dist/links/wsLink/wsClient/utils.d.ts.map +1 -0
- package/dist/links/wsLink/wsClient/utils.js +94 -0
- package/dist/links/wsLink/wsClient/utils.mjs +88 -0
- package/dist/links/wsLink/wsClient/wsClient.d.ts +85 -0
- package/dist/links/wsLink/wsClient/wsClient.d.ts.map +1 -0
- package/dist/links/wsLink/wsClient/wsClient.js +331 -0
- package/dist/links/wsLink/wsClient/wsClient.mjs +329 -0
- package/dist/links/wsLink/wsClient/wsConnection.d.ts +79 -0
- package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +1 -0
- package/dist/links/wsLink/wsClient/wsConnection.js +181 -0
- package/dist/links/wsLink/wsClient/wsConnection.mjs +178 -0
- package/dist/links/wsLink/wsLink.d.ts +11 -0
- package/dist/links/wsLink/wsLink.d.ts.map +1 -0
- package/dist/links/wsLink/wsLink.js +35 -0
- package/dist/links/wsLink/wsLink.mjs +32 -0
- package/dist/links.d.ts +1 -1
- package/dist/links.d.ts.map +1 -1
- package/links/wsLink/wsLink/index.d.ts +1 -0
- package/links/wsLink/wsLink/index.js +1 -0
- package/package.json +8 -8
- package/src/links/wsLink/createWsClient.ts +10 -0
- package/src/links/wsLink/wsClient/options.ts +91 -0
- package/src/links/wsLink/wsClient/requestManager.ts +174 -0
- package/src/links/wsLink/wsClient/utils.ts +94 -0
- package/src/links/wsLink/wsClient/wsClient.ts +441 -0
- package/src/links/wsLink/wsClient/wsConnection.ts +230 -0
- package/src/links/wsLink/wsLink.ts +55 -0
- package/src/links.ts +1 -1
- package/dist/links/wsLink.d.ts +0 -125
- package/dist/links/wsLink.d.ts.map +0 -1
- package/dist/links/wsLink.js +0 -498
- package/dist/links/wsLink.mjs +0 -495
- package/links/wsLink/index.d.ts +0 -1
- package/links/wsLink/index.js +0 -1
- package/src/links/wsLink.ts +0 -737
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';
|
|
2
|
+
|
|
3
|
+
export interface WebSocketClientOptions extends UrlOptionsWithConnectionParams {
|
|
4
|
+
/**
|
|
5
|
+
* Ponyfill which WebSocket implementation to use
|
|
6
|
+
*/
|
|
7
|
+
WebSocket?: typeof WebSocket;
|
|
8
|
+
/**
|
|
9
|
+
* The number of milliseconds before a reconnect is attempted.
|
|
10
|
+
* @default {@link exponentialBackoff}
|
|
11
|
+
*/
|
|
12
|
+
retryDelayMs?: (attemptIndex: number) => number;
|
|
13
|
+
/**
|
|
14
|
+
* Triggered when a WebSocket connection is established
|
|
15
|
+
*/
|
|
16
|
+
onOpen?: () => void;
|
|
17
|
+
/**
|
|
18
|
+
* Triggered when a WebSocket connection encounters an error
|
|
19
|
+
*/
|
|
20
|
+
onError?: (evt?: Event) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Triggered when a WebSocket connection is closed
|
|
23
|
+
*/
|
|
24
|
+
onClose?: (cause?: { code?: number }) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)
|
|
27
|
+
*/
|
|
28
|
+
lazy?: {
|
|
29
|
+
/**
|
|
30
|
+
* Enable lazy mode
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Close the WebSocket after this many milliseconds
|
|
36
|
+
* @default 0
|
|
37
|
+
*/
|
|
38
|
+
closeMs: number;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Send ping messages to the server and kill the connection if no pong message is returned
|
|
42
|
+
*/
|
|
43
|
+
keepAlive?: {
|
|
44
|
+
/**
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
enabled: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Send a ping message every this many milliseconds
|
|
50
|
+
* @default 5_000
|
|
51
|
+
*/
|
|
52
|
+
intervalMs?: number;
|
|
53
|
+
/**
|
|
54
|
+
* Close the WebSocket after this many milliseconds if the server does not respond
|
|
55
|
+
* @default 1_000
|
|
56
|
+
*/
|
|
57
|
+
pongTimeoutMs?: number;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Default options for lazy WebSocket connections.
|
|
63
|
+
* Determines whether the connection should be established lazily and defines the delay before closure.
|
|
64
|
+
*/
|
|
65
|
+
export type LazyOptions = Required<NonNullable<WebSocketClientOptions['lazy']>>;
|
|
66
|
+
export const lazyDefaults: LazyOptions = {
|
|
67
|
+
enabled: false,
|
|
68
|
+
closeMs: 0,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Default options for the WebSocket keep-alive mechanism.
|
|
73
|
+
* Configures whether keep-alive is enabled and specifies the timeout and interval for ping-pong messages.
|
|
74
|
+
*/
|
|
75
|
+
export type KeepAliveOptions = Required<
|
|
76
|
+
NonNullable<WebSocketClientOptions['keepAlive']>
|
|
77
|
+
>;
|
|
78
|
+
export const keepAliveDefaults: KeepAliveOptions = {
|
|
79
|
+
enabled: false,
|
|
80
|
+
pongTimeoutMs: 1_000,
|
|
81
|
+
intervalMs: 5_000,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Calculates a delay for exponential backoff based on the retry attempt index.
|
|
86
|
+
* The delay starts at 0 for the first attempt and doubles for each subsequent attempt,
|
|
87
|
+
* capped at 30 seconds.
|
|
88
|
+
*/
|
|
89
|
+
export const exponentialBackoff = (attemptIndex: number) => {
|
|
90
|
+
return attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
|
|
91
|
+
};
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type { AnyTRPCRouter, inferRouterError } from '@trpc/server';
|
|
2
|
+
import type { Observer } from '@trpc/server/observable';
|
|
3
|
+
import type {
|
|
4
|
+
TRPCClientOutgoingMessage,
|
|
5
|
+
TRPCResponseMessage,
|
|
6
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
7
|
+
import type { TRPCClientError } from '../../../TRPCClientError';
|
|
8
|
+
import { withResolvers } from './utils';
|
|
9
|
+
|
|
10
|
+
export type TCallbacks = Observer<
|
|
11
|
+
TRPCResponseMessage<unknown, inferRouterError<AnyTRPCRouter>>,
|
|
12
|
+
TRPCClientError<AnyTRPCRouter>
|
|
13
|
+
>;
|
|
14
|
+
|
|
15
|
+
type MessageId = string;
|
|
16
|
+
type MessageIdLike = string | number | null;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Represents a WebSocket request managed by the RequestManager.
|
|
20
|
+
* Combines the network message, a utility promise (`end`) that mirrors the lifecycle
|
|
21
|
+
* handled by `callbacks`, and a set of state monitoring callbacks.
|
|
22
|
+
*/
|
|
23
|
+
interface Request {
|
|
24
|
+
message: TRPCClientOutgoingMessage;
|
|
25
|
+
end: Promise<void>;
|
|
26
|
+
callbacks: TCallbacks;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Manages WebSocket requests, tracking their lifecycle and providing utility methods
|
|
31
|
+
* for handling outgoing and pending requests.
|
|
32
|
+
*
|
|
33
|
+
* - **Outgoing requests**: Requests that are queued and waiting to be sent.
|
|
34
|
+
* - **Pending requests**: Requests that have been sent and are in flight awaiting a response.
|
|
35
|
+
* For subscriptions, multiple responses may be received until the subscription is closed.
|
|
36
|
+
*/
|
|
37
|
+
export class RequestManager {
|
|
38
|
+
/**
|
|
39
|
+
* Stores requests that are outgoing, meaning they are registered but not yet sent over the WebSocket.
|
|
40
|
+
*/
|
|
41
|
+
private outgoingRequests = new Array<Request & { id: MessageId }>();
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Stores requests that are pending (in flight), meaning they have been sent over the WebSocket
|
|
45
|
+
* and are awaiting responses. For subscriptions, this includes requests
|
|
46
|
+
* that may receive multiple responses.
|
|
47
|
+
*/
|
|
48
|
+
private pendingRequests: Record<MessageId, Request> = {};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Registers a new request by adding it to the outgoing queue and setting up
|
|
52
|
+
* callbacks for lifecycle events such as completion or error.
|
|
53
|
+
*
|
|
54
|
+
* @param message - The outgoing message to be sent.
|
|
55
|
+
* @param callbacks - Callback functions to observe the request's state.
|
|
56
|
+
* @returns A cleanup function to manually remove the request.
|
|
57
|
+
*/
|
|
58
|
+
public register(message: TRPCClientOutgoingMessage, callbacks: TCallbacks) {
|
|
59
|
+
const { promise: end, resolve } = withResolvers<void>();
|
|
60
|
+
|
|
61
|
+
this.outgoingRequests.push({
|
|
62
|
+
id: String(message.id),
|
|
63
|
+
message,
|
|
64
|
+
end,
|
|
65
|
+
callbacks: {
|
|
66
|
+
next: callbacks.next,
|
|
67
|
+
complete: () => {
|
|
68
|
+
callbacks.complete();
|
|
69
|
+
resolve();
|
|
70
|
+
},
|
|
71
|
+
error: (e) => {
|
|
72
|
+
callbacks.error(e);
|
|
73
|
+
resolve();
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return () => {
|
|
79
|
+
this.delete(message.id);
|
|
80
|
+
callbacks.complete();
|
|
81
|
+
resolve();
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Deletes a request from both the outgoing and pending collections, if it exists.
|
|
87
|
+
*/
|
|
88
|
+
public delete(messageId: MessageIdLike) {
|
|
89
|
+
if (messageId === null) return;
|
|
90
|
+
|
|
91
|
+
this.outgoingRequests = this.outgoingRequests.filter(
|
|
92
|
+
({ id }) => id !== String(messageId),
|
|
93
|
+
);
|
|
94
|
+
delete this.pendingRequests[String(messageId)];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Moves all outgoing requests to the pending state and clears the outgoing queue.
|
|
99
|
+
*
|
|
100
|
+
* The caller is expected to handle the actual sending of the requests
|
|
101
|
+
* (e.g., sending them over the network) after this method is called.
|
|
102
|
+
*
|
|
103
|
+
* @returns The list of requests that were transitioned to the pending state.
|
|
104
|
+
*/
|
|
105
|
+
public flush() {
|
|
106
|
+
const requests = this.outgoingRequests;
|
|
107
|
+
this.outgoingRequests = [];
|
|
108
|
+
|
|
109
|
+
for (const request of requests) {
|
|
110
|
+
this.pendingRequests[request.id] = request;
|
|
111
|
+
}
|
|
112
|
+
return requests;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Retrieves all currently pending requests, which are in flight awaiting responses
|
|
117
|
+
* or handling ongoing subscriptions.
|
|
118
|
+
*/
|
|
119
|
+
public getPendingRequests() {
|
|
120
|
+
return Object.values(this.pendingRequests);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves a specific pending request by its message ID.
|
|
125
|
+
*/
|
|
126
|
+
public getPendingRequest(messageId: MessageIdLike) {
|
|
127
|
+
if (messageId === null) return null;
|
|
128
|
+
|
|
129
|
+
return this.pendingRequests[String(messageId)];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Retrieves all outgoing requests, which are waiting to be sent.
|
|
134
|
+
*/
|
|
135
|
+
public getOutgoingRequests() {
|
|
136
|
+
return this.outgoingRequests;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Retrieves all requests, both outgoing and pending, with their respective states.
|
|
141
|
+
*
|
|
142
|
+
* @returns An array of all requests with their state ("outgoing" or "pending").
|
|
143
|
+
*/
|
|
144
|
+
public getRequests() {
|
|
145
|
+
return [
|
|
146
|
+
...this.getOutgoingRequests().map((request) => ({
|
|
147
|
+
state: 'outgoing' as const,
|
|
148
|
+
message: request.message,
|
|
149
|
+
end: request.end,
|
|
150
|
+
callbacks: request.callbacks,
|
|
151
|
+
})),
|
|
152
|
+
...this.getPendingRequests().map((request) => ({
|
|
153
|
+
state: 'pending' as const,
|
|
154
|
+
message: request.message,
|
|
155
|
+
end: request.end,
|
|
156
|
+
callbacks: request.callbacks,
|
|
157
|
+
})),
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Checks if there are any pending requests, including ongoing subscriptions.
|
|
163
|
+
*/
|
|
164
|
+
public hasPendingRequests() {
|
|
165
|
+
return this.getPendingRequests().length > 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Checks if there are any outgoing requests waiting to be sent.
|
|
170
|
+
*/
|
|
171
|
+
public hasOutgoingRequests() {
|
|
172
|
+
return this.outgoingRequests.length > 0;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
TRPCConnectionParamsMessage,
|
|
3
|
+
TRPCRequestInfo,
|
|
4
|
+
} from '@trpc/server/unstable-core-do-not-import';
|
|
5
|
+
import type {
|
|
6
|
+
CallbackOrValue,
|
|
7
|
+
UrlOptionsWithConnectionParams,
|
|
8
|
+
} from '../../internals/urlWithConnectionParams';
|
|
9
|
+
import { resultOf } from '../../internals/urlWithConnectionParams';
|
|
10
|
+
|
|
11
|
+
export class TRPCWebSocketClosedError extends Error {
|
|
12
|
+
constructor(opts: { message: string; cause?: unknown }) {
|
|
13
|
+
super(opts.message, {
|
|
14
|
+
cause: opts.cause,
|
|
15
|
+
});
|
|
16
|
+
this.name = 'TRPCWebSocketClosedError';
|
|
17
|
+
Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Utility class for managing a timeout that can be started, stopped, and reset.
|
|
23
|
+
* Useful for scenarios where the timeout duration is reset dynamically based on events.
|
|
24
|
+
*/
|
|
25
|
+
export class ResettableTimeout {
|
|
26
|
+
private timeout: ReturnType<typeof setTimeout> | undefined;
|
|
27
|
+
|
|
28
|
+
constructor(
|
|
29
|
+
private readonly onTimeout: () => void,
|
|
30
|
+
private readonly timeoutMs: number,
|
|
31
|
+
) {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resets the current timeout, restarting it with the same duration.
|
|
35
|
+
* Does nothing if no timeout is active.
|
|
36
|
+
*/
|
|
37
|
+
public reset() {
|
|
38
|
+
if (!this.timeout) return;
|
|
39
|
+
|
|
40
|
+
clearTimeout(this.timeout);
|
|
41
|
+
this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public start() {
|
|
45
|
+
clearTimeout(this.timeout);
|
|
46
|
+
this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public stop() {
|
|
50
|
+
clearTimeout(this.timeout);
|
|
51
|
+
this.timeout = undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Ponyfill for Promise.withResolvers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
|
|
56
|
+
export function withResolvers<T>() {
|
|
57
|
+
let resolve: (value: T | PromiseLike<T>) => void;
|
|
58
|
+
let reject: (reason?: any) => void;
|
|
59
|
+
const promise = new Promise<T>((res, rej) => {
|
|
60
|
+
resolve = res;
|
|
61
|
+
reject = rej;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
65
|
+
return { promise, resolve: resolve!, reject: reject! };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Resolves a WebSocket URL and optionally appends connection parameters.
|
|
70
|
+
*
|
|
71
|
+
* If connectionParams are provided, appends 'connectionParams=1' query parameter.
|
|
72
|
+
*/
|
|
73
|
+
export async function prepareUrl(urlOptions: UrlOptionsWithConnectionParams) {
|
|
74
|
+
const url = await resultOf(urlOptions.url);
|
|
75
|
+
|
|
76
|
+
if (!urlOptions.connectionParams) return url;
|
|
77
|
+
|
|
78
|
+
// append `?connectionParams=1` when connection params are used
|
|
79
|
+
const prefix = url.includes('?') ? '&' : '?';
|
|
80
|
+
const connectionParams = `${prefix}connectionParams=1`;
|
|
81
|
+
|
|
82
|
+
return url + connectionParams;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function buildConnectionMessage(
|
|
86
|
+
connectionParams: CallbackOrValue<TRPCRequestInfo['connectionParams']>,
|
|
87
|
+
) {
|
|
88
|
+
const message: TRPCConnectionParamsMessage = {
|
|
89
|
+
method: 'connectionParams',
|
|
90
|
+
data: await resultOf(connectionParams),
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return JSON.stringify(message);
|
|
94
|
+
}
|