@trpc/client 11.3.1 → 11.3.2-canary.4
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/TRPCClientError-CGgRtttT.cjs +67 -0
- package/dist/TRPCClientError-COkhCKf3.mjs +56 -0
- package/dist/TRPCClientError-COkhCKf3.mjs.map +1 -0
- package/dist/chunk-DWy1uDak.cjs +39 -0
- package/dist/httpBatchLink-CIC9PZ_Z.cjs +267 -0
- package/dist/httpBatchLink-DIfnmmcu.mjs +238 -0
- package/dist/httpBatchLink-DIfnmmcu.mjs.map +1 -0
- package/dist/httpBatchLink.d-BN2Iw_cK.d.mts +33 -0
- package/dist/httpBatchLink.d-BN2Iw_cK.d.mts.map +1 -0
- package/dist/httpBatchLink.d-CLAuJKNV.d.cts +33 -0
- package/dist/httpBatchLink.d-CLAuJKNV.d.cts.map +1 -0
- package/dist/httpLink-9bpwQ15c.mjs +90 -0
- package/dist/httpLink-9bpwQ15c.mjs.map +1 -0
- package/dist/httpLink-BbVj_TgM.cjs +113 -0
- package/dist/httpLink.d-CTSOARfE.d.mts +23 -0
- package/dist/httpLink.d-CTSOARfE.d.mts.map +1 -0
- package/dist/httpLink.d-yMKfOlTU.d.cts +23 -0
- package/dist/httpLink.d-yMKfOlTU.d.cts.map +1 -0
- package/dist/httpUtils-3pbQzhUL.mjs +123 -0
- package/dist/httpUtils-3pbQzhUL.mjs.map +1 -0
- package/dist/httpUtils-Buf6jCgh.cjs +163 -0
- package/dist/httpUtils.d-C8wPxU79.d.mts +25 -0
- package/dist/httpUtils.d-C8wPxU79.d.mts.map +1 -0
- package/dist/httpUtils.d-DNRVGNHs.d.cts +25 -0
- package/dist/httpUtils.d-DNRVGNHs.d.cts.map +1 -0
- package/dist/index.cjs +736 -0
- package/dist/index.d.cts +201 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +201 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +737 -16
- package/dist/index.mjs.map +1 -0
- package/dist/links/httpBatchLink.cjs +6 -0
- package/dist/links/httpBatchLink.d.cts +6 -0
- package/dist/links/httpBatchLink.d.mts +6 -0
- package/dist/links/httpBatchLink.mjs +5 -107
- package/dist/links/httpLink.cjs +6 -0
- package/dist/links/httpLink.d.cts +6 -0
- package/dist/links/httpLink.d.mts +6 -0
- package/dist/links/httpLink.mjs +5 -92
- package/dist/links/loggerLink.cjs +3 -0
- package/dist/links/loggerLink.d.cts +4 -0
- package/dist/links/loggerLink.d.mts +4 -0
- package/dist/links/loggerLink.mjs +2 -175
- package/dist/links/splitLink.cjs +3 -0
- package/dist/links/splitLink.d.cts +4 -0
- package/dist/links/splitLink.d.mts +4 -0
- package/dist/links/splitLink.mjs +2 -24
- package/dist/links/wsLink/wsLink.cjs +6 -0
- package/dist/links/wsLink/wsLink.d.cts +5 -0
- package/dist/links/wsLink/wsLink.d.mts +5 -0
- package/dist/links/wsLink/wsLink.mjs +4 -31
- package/dist/loggerLink-CsDfg5Bb.cjs +130 -0
- package/dist/loggerLink-PGBSQFcR.mjs +125 -0
- package/dist/loggerLink-PGBSQFcR.mjs.map +1 -0
- package/dist/loggerLink.d-CZjK1CXm.d.mts +54 -0
- package/dist/loggerLink.d-CZjK1CXm.d.mts.map +1 -0
- package/dist/loggerLink.d-DU855urG.d.cts +54 -0
- package/dist/loggerLink.d-DU855urG.d.cts.map +1 -0
- package/dist/splitLink-BMgxggng.cjs +58 -0
- package/dist/splitLink-BVblHq4n.mjs +47 -0
- package/dist/splitLink-BVblHq4n.mjs.map +1 -0
- package/dist/splitLink.d-BAqgq0NG.d.mts +20 -0
- package/dist/splitLink.d-BAqgq0NG.d.mts.map +1 -0
- package/dist/splitLink.d-GdevSkGW.d.cts +20 -0
- package/dist/splitLink.d-GdevSkGW.d.cts.map +1 -0
- package/dist/subscriptions.d-Ciljg_dH.d.cts +19 -0
- package/dist/subscriptions.d-Ciljg_dH.d.cts.map +1 -0
- package/dist/subscriptions.d-U92STdZl.d.mts +19 -0
- package/dist/subscriptions.d-U92STdZl.d.mts.map +1 -0
- package/dist/types.d-DXbqQLCC.d.mts +182 -0
- package/dist/types.d-DXbqQLCC.d.mts.map +1 -0
- package/dist/types.d-DqGYG6S_.d.cts +182 -0
- package/dist/types.d-DqGYG6S_.d.cts.map +1 -0
- package/dist/unstable-internals-CYBH4jbF.cjs +34 -0
- package/dist/unstable-internals-DU4WecoG.mjs +29 -0
- package/dist/unstable-internals-DU4WecoG.mjs.map +1 -0
- package/dist/unstable-internals.cjs +3 -0
- package/dist/unstable-internals.d-DyLd-B0J.d.mts +46 -0
- package/dist/unstable-internals.d-DyLd-B0J.d.mts.map +1 -0
- package/dist/unstable-internals.d-kWsZTlQq.d.cts +46 -0
- package/dist/unstable-internals.d-kWsZTlQq.d.cts.map +1 -0
- package/dist/unstable-internals.d.cts +3 -0
- package/dist/unstable-internals.d.mts +3 -0
- package/dist/unstable-internals.mjs +3 -1
- package/dist/wsLink-DbSHOzlB.mjs +698 -0
- package/dist/wsLink-DbSHOzlB.mjs.map +1 -0
- package/dist/wsLink-DhJ50EC5.cjs +715 -0
- package/dist/wsLink.d-BqO7ltN_.d.mts +194 -0
- package/dist/wsLink.d-BqO7ltN_.d.mts.map +1 -0
- package/dist/wsLink.d-DtZjg09g.d.cts +194 -0
- package/dist/wsLink.d-DtZjg09g.d.cts.map +1 -0
- package/links/httpBatchLink/package.json +1 -0
- package/links/httpLink/package.json +1 -0
- package/links/loggerLink/package.json +1 -0
- package/links/splitLink/package.json +1 -0
- package/links/wsLink/wsLink/package.json +1 -0
- package/package.json +67 -33
- package/src/links/localLink.ts +6 -1
- package/unstable-internals/package.json +1 -0
- package/dist/TRPCClientError.d.ts +0 -30
- package/dist/TRPCClientError.d.ts.map +0 -1
- package/dist/TRPCClientError.js +0 -79
- package/dist/TRPCClientError.mjs +0 -76
- package/dist/createTRPCClient.d.ts +0 -69
- package/dist/createTRPCClient.d.ts.map +0 -1
- package/dist/createTRPCClient.js +0 -48
- package/dist/createTRPCClient.mjs +0 -43
- package/dist/createTRPCUntypedClient.d.ts +0 -7
- package/dist/createTRPCUntypedClient.d.ts.map +0 -1
- package/dist/createTRPCUntypedClient.js +0 -10
- package/dist/createTRPCUntypedClient.mjs +0 -7
- package/dist/getFetch.d.ts +0 -3
- package/dist/getFetch.d.ts.map +0 -1
- package/dist/getFetch.js +0 -17
- package/dist/getFetch.mjs +0 -15
- package/dist/index.d.ts +0 -16
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -46
- package/dist/internals/TRPCUntypedClient.d.ts +0 -39
- package/dist/internals/TRPCUntypedClient.d.ts.map +0 -1
- package/dist/internals/TRPCUntypedClient.js +0 -115
- package/dist/internals/TRPCUntypedClient.mjs +0 -113
- package/dist/internals/dataLoader.d.ts +0 -13
- package/dist/internals/dataLoader.d.ts.map +0 -1
- package/dist/internals/dataLoader.js +0 -117
- package/dist/internals/dataLoader.mjs +0 -115
- package/dist/internals/inputWithTrackedEventId.d.ts +0 -2
- package/dist/internals/inputWithTrackedEventId.d.ts.map +0 -1
- package/dist/internals/inputWithTrackedEventId.js +0 -16
- package/dist/internals/inputWithTrackedEventId.mjs +0 -14
- package/dist/internals/signals.d.ts +0 -16
- package/dist/internals/signals.d.ts.map +0 -1
- package/dist/internals/signals.js +0 -61
- package/dist/internals/signals.mjs +0 -57
- package/dist/internals/transformer.d.ts +0 -42
- package/dist/internals/transformer.d.ts.map +0 -1
- package/dist/internals/transformer.js +0 -30
- package/dist/internals/transformer.mjs +0 -28
- package/dist/internals/types.d.ts +0 -82
- package/dist/internals/types.d.ts.map +0 -1
- package/dist/links/HTTPBatchLinkOptions.d.ts +0 -20
- package/dist/links/HTTPBatchLinkOptions.d.ts.map +0 -1
- package/dist/links/httpBatchLink.d.ts +0 -8
- package/dist/links/httpBatchLink.d.ts.map +0 -1
- package/dist/links/httpBatchLink.js +0 -110
- package/dist/links/httpBatchStreamLink.d.ts +0 -12
- package/dist/links/httpBatchStreamLink.d.ts.map +0 -1
- package/dist/links/httpBatchStreamLink.js +0 -150
- package/dist/links/httpBatchStreamLink.mjs +0 -147
- package/dist/links/httpLink.d.ts +0 -17
- package/dist/links/httpLink.d.ts.map +0 -1
- package/dist/links/httpLink.js +0 -95
- package/dist/links/httpSubscriptionLink.d.ts +0 -26
- package/dist/links/httpSubscriptionLink.d.ts.map +0 -1
- package/dist/links/httpSubscriptionLink.js +0 -177
- package/dist/links/httpSubscriptionLink.mjs +0 -174
- package/dist/links/internals/contentTypes.d.ts +0 -4
- package/dist/links/internals/contentTypes.d.ts.map +0 -1
- package/dist/links/internals/contentTypes.js +0 -16
- package/dist/links/internals/contentTypes.mjs +0 -12
- package/dist/links/internals/createChain.d.ts +0 -8
- package/dist/links/internals/createChain.d.ts.map +0 -1
- package/dist/links/internals/createChain.js +0 -26
- package/dist/links/internals/createChain.mjs +0 -24
- package/dist/links/internals/dedupeLink.d.ts +0 -7
- package/dist/links/internals/dedupeLink.d.ts.map +0 -1
- package/dist/links/internals/httpUtils.d.ts +0 -68
- package/dist/links/internals/httpUtils.d.ts.map +0 -1
- package/dist/links/internals/httpUtils.js +0 -141
- package/dist/links/internals/httpUtils.mjs +0 -133
- package/dist/links/internals/subscriptions.d.ts +0 -17
- package/dist/links/internals/subscriptions.d.ts.map +0 -1
- package/dist/links/internals/urlWithConnectionParams.d.ts +0 -23
- package/dist/links/internals/urlWithConnectionParams.d.ts.map +0 -1
- package/dist/links/internals/urlWithConnectionParams.js +0 -10
- package/dist/links/internals/urlWithConnectionParams.mjs +0 -8
- package/dist/links/localLink.d.ts +0 -15
- package/dist/links/localLink.d.ts.map +0 -1
- package/dist/links/localLink.js +0 -333
- package/dist/links/localLink.mjs +0 -331
- package/dist/links/loggerLink.d.ts +0 -52
- package/dist/links/loggerLink.d.ts.map +0 -1
- package/dist/links/loggerLink.js +0 -178
- package/dist/links/retryLink.d.ts +0 -33
- package/dist/links/retryLink.d.ts.map +0 -1
- package/dist/links/retryLink.js +0 -72
- package/dist/links/retryLink.mjs +0 -70
- package/dist/links/splitLink.d.ts +0 -14
- package/dist/links/splitLink.d.ts.map +0 -1
- package/dist/links/splitLink.js +0 -27
- package/dist/links/types.d.ts +0 -63
- package/dist/links/types.d.ts.map +0 -1
- package/dist/links/wsLink/createWsClient.d.ts +0 -6
- package/dist/links/wsLink/createWsClient.d.ts.map +0 -1
- package/dist/links/wsLink/createWsClient.js +0 -9
- package/dist/links/wsLink/createWsClient.mjs +0 -7
- package/dist/links/wsLink/wsClient/options.d.ts +0 -79
- package/dist/links/wsLink/wsClient/options.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/options.js +0 -22
- package/dist/links/wsLink/wsClient/options.mjs +0 -18
- package/dist/links/wsLink/wsClient/requestManager.d.ts +0 -106
- package/dist/links/wsLink/wsClient/requestManager.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/requestManager.js +0 -143
- package/dist/links/wsLink/wsClient/requestManager.mjs +0 -141
- package/dist/links/wsLink/wsClient/utils.d.ts +0 -38
- package/dist/links/wsLink/wsClient/utils.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/utils.js +0 -94
- package/dist/links/wsLink/wsClient/utils.mjs +0 -88
- package/dist/links/wsLink/wsClient/wsClient.d.ts +0 -84
- package/dist/links/wsLink/wsClient/wsClient.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/wsClient.js +0 -325
- package/dist/links/wsLink/wsClient/wsClient.mjs +0 -323
- package/dist/links/wsLink/wsClient/wsConnection.d.ts +0 -79
- package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +0 -1
- package/dist/links/wsLink/wsClient/wsConnection.js +0 -188
- package/dist/links/wsLink/wsClient/wsConnection.mjs +0 -185
- package/dist/links/wsLink/wsLink.d.ts +0 -11
- package/dist/links/wsLink/wsLink.d.ts.map +0 -1
- package/dist/links/wsLink/wsLink.js +0 -35
- package/dist/links.d.ts +0 -12
- package/dist/links.d.ts.map +0 -1
- package/dist/unstable-internals.d.ts +0 -3
- package/dist/unstable-internals.d.ts.map +0 -1
- package/dist/unstable-internals.js +0 -7
- package/links/httpBatchLink/index.d.ts +0 -1
- package/links/httpBatchLink/index.js +0 -1
- package/links/httpLink/index.d.ts +0 -1
- package/links/httpLink/index.js +0 -1
- package/links/loggerLink/index.d.ts +0 -1
- package/links/loggerLink/index.js +0 -1
- package/links/splitLink/index.d.ts +0 -1
- package/links/splitLink/index.js +0 -1
- package/links/wsLink/index.d.ts +0 -1
- package/links/wsLink/index.js +0 -1
- package/links/wsLink/wsLink/index.d.ts +0 -1
- package/links/wsLink/wsLink/index.js +0 -1
- package/unstable-internals/index.d.ts +0 -1
- package/unstable-internals/index.js +0 -1
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
import { observable, behaviorSubject } from '@trpc/server/observable';
|
|
2
|
-
import { transformResult, run, sleep } from '@trpc/server/unstable-core-do-not-import';
|
|
3
|
-
import { TRPCClientError } from '../../../TRPCClientError.mjs';
|
|
4
|
-
import { lazyDefaults, keepAliveDefaults, exponentialBackoff } from './options.mjs';
|
|
5
|
-
import { RequestManager } from './requestManager.mjs';
|
|
6
|
-
import { TRPCWebSocketClosedError, ResettableTimeout } from './utils.mjs';
|
|
7
|
-
import { backwardCompatibility, WsConnection } from './wsConnection.mjs';
|
|
8
|
-
|
|
9
|
-
function _define_property(obj, key, value) {
|
|
10
|
-
if (key in obj) {
|
|
11
|
-
Object.defineProperty(obj, key, {
|
|
12
|
-
value: value,
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
writable: true
|
|
16
|
-
});
|
|
17
|
-
} else {
|
|
18
|
-
obj[key] = value;
|
|
19
|
-
}
|
|
20
|
-
return obj;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* A WebSocket client for managing TRPC operations, supporting lazy initialization,
|
|
24
|
-
* reconnection, keep-alive, and request management.
|
|
25
|
-
*/ class WsClient {
|
|
26
|
-
/**
|
|
27
|
-
* Opens the WebSocket connection. Handles reconnection attempts and updates
|
|
28
|
-
* the connection state accordingly.
|
|
29
|
-
*/ async open() {
|
|
30
|
-
this.allowReconnect = true;
|
|
31
|
-
if (this.connectionState.get().state !== 'connecting') {
|
|
32
|
-
this.connectionState.next({
|
|
33
|
-
type: 'state',
|
|
34
|
-
state: 'connecting',
|
|
35
|
-
error: null
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
try {
|
|
39
|
-
await this.activeConnection.open();
|
|
40
|
-
} catch (error) {
|
|
41
|
-
this.reconnect(new TRPCWebSocketClosedError({
|
|
42
|
-
message: 'Initialization error',
|
|
43
|
-
cause: error
|
|
44
|
-
}));
|
|
45
|
-
return this.reconnecting;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Closes the WebSocket connection and stops managing requests.
|
|
50
|
-
* Ensures all outgoing and pending requests are properly finalized.
|
|
51
|
-
*/ async close() {
|
|
52
|
-
this.allowReconnect = false;
|
|
53
|
-
this.inactivityTimeout.stop();
|
|
54
|
-
const requestsToAwait = [];
|
|
55
|
-
for (const request of this.requestManager.getRequests()){
|
|
56
|
-
if (request.message.method === 'subscription') {
|
|
57
|
-
request.callbacks.complete();
|
|
58
|
-
} else if (request.state === 'outgoing') {
|
|
59
|
-
request.callbacks.error(TRPCClientError.from(new TRPCWebSocketClosedError({
|
|
60
|
-
message: 'Closed before connection was established'
|
|
61
|
-
})));
|
|
62
|
-
} else {
|
|
63
|
-
requestsToAwait.push(request.end);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
await Promise.all(requestsToAwait).catch(()=>null);
|
|
67
|
-
await this.activeConnection.close().catch(()=>null);
|
|
68
|
-
this.connectionState.next({
|
|
69
|
-
type: 'state',
|
|
70
|
-
state: 'idle',
|
|
71
|
-
error: null
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Method to request the server.
|
|
76
|
-
* Handles data transformation, batching of requests, and subscription lifecycle.
|
|
77
|
-
*
|
|
78
|
-
* @param op - The operation details including id, type, path, input and signal
|
|
79
|
-
* @param transformer - Data transformer for serializing requests and deserializing responses
|
|
80
|
-
* @param lastEventId - Optional ID of the last received event for subscriptions
|
|
81
|
-
*
|
|
82
|
-
* @returns An observable that emits operation results and handles cleanup
|
|
83
|
-
*/ request({ op: { id, type, path, input, signal }, transformer, lastEventId }) {
|
|
84
|
-
return observable((observer)=>{
|
|
85
|
-
const abort = this.batchSend({
|
|
86
|
-
id,
|
|
87
|
-
method: type,
|
|
88
|
-
params: {
|
|
89
|
-
input: transformer.input.serialize(input),
|
|
90
|
-
path,
|
|
91
|
-
lastEventId
|
|
92
|
-
}
|
|
93
|
-
}, {
|
|
94
|
-
...observer,
|
|
95
|
-
next (event) {
|
|
96
|
-
const transformed = transformResult(event, transformer.output);
|
|
97
|
-
if (!transformed.ok) {
|
|
98
|
-
observer.error(TRPCClientError.from(transformed.error));
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
observer.next({
|
|
102
|
-
result: transformed.result
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
return ()=>{
|
|
107
|
-
abort();
|
|
108
|
-
if (type === 'subscription' && this.activeConnection.isOpen()) {
|
|
109
|
-
this.send({
|
|
110
|
-
id,
|
|
111
|
-
method: 'subscription.stop'
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
signal?.removeEventListener('abort', abort);
|
|
115
|
-
};
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
get connection() {
|
|
119
|
-
return backwardCompatibility(this.activeConnection);
|
|
120
|
-
}
|
|
121
|
-
reconnect(closedError) {
|
|
122
|
-
this.connectionState.next({
|
|
123
|
-
type: 'state',
|
|
124
|
-
state: 'connecting',
|
|
125
|
-
error: TRPCClientError.from(closedError)
|
|
126
|
-
});
|
|
127
|
-
if (this.reconnecting) return;
|
|
128
|
-
const tryReconnect = async (attemptIndex)=>{
|
|
129
|
-
try {
|
|
130
|
-
await sleep(this.reconnectRetryDelay(attemptIndex));
|
|
131
|
-
if (this.allowReconnect) {
|
|
132
|
-
await this.activeConnection.close();
|
|
133
|
-
await this.activeConnection.open();
|
|
134
|
-
if (this.requestManager.hasPendingRequests()) {
|
|
135
|
-
this.send(this.requestManager.getPendingRequests().map(({ message })=>message));
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
this.reconnecting = null;
|
|
139
|
-
} catch {
|
|
140
|
-
await tryReconnect(attemptIndex + 1);
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
this.reconnecting = tryReconnect(0);
|
|
144
|
-
}
|
|
145
|
-
setupWebSocketListeners(ws) {
|
|
146
|
-
const handleCloseOrError = (cause)=>{
|
|
147
|
-
const reqs = this.requestManager.getPendingRequests();
|
|
148
|
-
for (const { message, callbacks } of reqs){
|
|
149
|
-
if (message.method === 'subscription') continue;
|
|
150
|
-
callbacks.error(TRPCClientError.from(cause ?? new TRPCWebSocketClosedError({
|
|
151
|
-
message: 'WebSocket closed',
|
|
152
|
-
cause
|
|
153
|
-
})));
|
|
154
|
-
this.requestManager.delete(message.id);
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
ws.addEventListener('open', ()=>{
|
|
158
|
-
run(async ()=>{
|
|
159
|
-
if (this.lazyMode) {
|
|
160
|
-
this.inactivityTimeout.start();
|
|
161
|
-
}
|
|
162
|
-
this.callbacks.onOpen?.();
|
|
163
|
-
this.connectionState.next({
|
|
164
|
-
type: 'state',
|
|
165
|
-
state: 'pending',
|
|
166
|
-
error: null
|
|
167
|
-
});
|
|
168
|
-
}).catch((error)=>{
|
|
169
|
-
ws.close(3000);
|
|
170
|
-
handleCloseOrError(error);
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
ws.addEventListener('message', ({ data })=>{
|
|
174
|
-
this.inactivityTimeout.reset();
|
|
175
|
-
if (typeof data !== 'string' || [
|
|
176
|
-
'PING',
|
|
177
|
-
'PONG'
|
|
178
|
-
].includes(data)) return;
|
|
179
|
-
const incomingMessage = JSON.parse(data);
|
|
180
|
-
if ('method' in incomingMessage) {
|
|
181
|
-
this.handleIncomingRequest(incomingMessage);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
this.handleResponseMessage(incomingMessage);
|
|
185
|
-
});
|
|
186
|
-
ws.addEventListener('close', (event)=>{
|
|
187
|
-
handleCloseOrError(event);
|
|
188
|
-
this.callbacks.onClose?.(event);
|
|
189
|
-
if (!this.lazyMode || this.requestManager.hasPendingSubscriptions()) {
|
|
190
|
-
this.reconnect(new TRPCWebSocketClosedError({
|
|
191
|
-
message: 'WebSocket closed',
|
|
192
|
-
cause: event
|
|
193
|
-
}));
|
|
194
|
-
}
|
|
195
|
-
});
|
|
196
|
-
ws.addEventListener('error', (event)=>{
|
|
197
|
-
handleCloseOrError(event);
|
|
198
|
-
this.callbacks.onError?.(event);
|
|
199
|
-
this.reconnect(new TRPCWebSocketClosedError({
|
|
200
|
-
message: 'WebSocket closed',
|
|
201
|
-
cause: event
|
|
202
|
-
}));
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
handleResponseMessage(message) {
|
|
206
|
-
const request = this.requestManager.getPendingRequest(message.id);
|
|
207
|
-
if (!request) return;
|
|
208
|
-
request.callbacks.next(message);
|
|
209
|
-
let completed = true;
|
|
210
|
-
if ('result' in message && request.message.method === 'subscription') {
|
|
211
|
-
if (message.result.type === 'data') {
|
|
212
|
-
request.message.params.lastEventId = message.result.id;
|
|
213
|
-
}
|
|
214
|
-
if (message.result.type !== 'stopped') {
|
|
215
|
-
completed = false;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (completed) {
|
|
219
|
-
request.callbacks.complete();
|
|
220
|
-
this.requestManager.delete(message.id);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
handleIncomingRequest(message) {
|
|
224
|
-
if (message.method === 'reconnect') {
|
|
225
|
-
this.reconnect(new TRPCWebSocketClosedError({
|
|
226
|
-
message: 'Server requested reconnect'
|
|
227
|
-
}));
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Sends a message or batch of messages directly to the server.
|
|
232
|
-
*/ send(messageOrMessages) {
|
|
233
|
-
if (!this.activeConnection.isOpen()) {
|
|
234
|
-
throw new Error('Active connection is not open');
|
|
235
|
-
}
|
|
236
|
-
const messages = messageOrMessages instanceof Array ? messageOrMessages : [
|
|
237
|
-
messageOrMessages
|
|
238
|
-
];
|
|
239
|
-
this.activeConnection.ws.send(JSON.stringify(messages.length === 1 ? messages[0] : messages));
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Groups requests for batch sending.
|
|
243
|
-
*
|
|
244
|
-
* @returns A function to abort the batched request.
|
|
245
|
-
*/ batchSend(message, callbacks) {
|
|
246
|
-
this.inactivityTimeout.reset();
|
|
247
|
-
run(async ()=>{
|
|
248
|
-
if (!this.activeConnection.isOpen()) {
|
|
249
|
-
await this.open();
|
|
250
|
-
}
|
|
251
|
-
await sleep(0);
|
|
252
|
-
if (!this.requestManager.hasOutgoingRequests()) return;
|
|
253
|
-
this.send(this.requestManager.flush().map(({ message })=>message));
|
|
254
|
-
}).catch((err)=>{
|
|
255
|
-
this.requestManager.delete(message.id);
|
|
256
|
-
callbacks.error(TRPCClientError.from(err));
|
|
257
|
-
});
|
|
258
|
-
return this.requestManager.register(message, callbacks);
|
|
259
|
-
}
|
|
260
|
-
constructor(opts){
|
|
261
|
-
/**
|
|
262
|
-
* Observable tracking the current connection state, including errors.
|
|
263
|
-
*/ _define_property(this, "connectionState", void 0);
|
|
264
|
-
_define_property(this, "allowReconnect", false);
|
|
265
|
-
_define_property(this, "requestManager", new RequestManager());
|
|
266
|
-
_define_property(this, "activeConnection", void 0);
|
|
267
|
-
_define_property(this, "reconnectRetryDelay", void 0);
|
|
268
|
-
_define_property(this, "inactivityTimeout", void 0);
|
|
269
|
-
_define_property(this, "callbacks", void 0);
|
|
270
|
-
_define_property(this, "lazyMode", void 0);
|
|
271
|
-
/**
|
|
272
|
-
* Manages the reconnection process for the WebSocket using retry logic.
|
|
273
|
-
* Ensures that only one reconnection attempt is active at a time by tracking the current
|
|
274
|
-
* reconnection state in the `reconnecting` promise.
|
|
275
|
-
*/ _define_property(this, "reconnecting", null);
|
|
276
|
-
// Initialize callbacks, connection parameters, and options.
|
|
277
|
-
this.callbacks = {
|
|
278
|
-
onOpen: opts.onOpen,
|
|
279
|
-
onClose: opts.onClose,
|
|
280
|
-
onError: opts.onError
|
|
281
|
-
};
|
|
282
|
-
const lazyOptions = {
|
|
283
|
-
...lazyDefaults,
|
|
284
|
-
...opts.lazy
|
|
285
|
-
};
|
|
286
|
-
// Set up inactivity timeout for lazy connections.
|
|
287
|
-
this.inactivityTimeout = new ResettableTimeout(()=>{
|
|
288
|
-
if (this.requestManager.hasOutgoingRequests() || this.requestManager.hasPendingRequests()) {
|
|
289
|
-
this.inactivityTimeout.reset();
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
this.close().catch(()=>null);
|
|
293
|
-
}, lazyOptions.closeMs);
|
|
294
|
-
// Initialize the WebSocket connection.
|
|
295
|
-
this.activeConnection = new WsConnection({
|
|
296
|
-
WebSocketPonyfill: opts.WebSocket,
|
|
297
|
-
urlOptions: opts,
|
|
298
|
-
keepAlive: {
|
|
299
|
-
...keepAliveDefaults,
|
|
300
|
-
...opts.keepAlive
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
this.activeConnection.wsObservable.subscribe({
|
|
304
|
-
next: (ws)=>{
|
|
305
|
-
if (!ws) return;
|
|
306
|
-
this.setupWebSocketListeners(ws);
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
this.reconnectRetryDelay = opts.retryDelayMs ?? exponentialBackoff;
|
|
310
|
-
this.lazyMode = lazyOptions.enabled;
|
|
311
|
-
this.connectionState = behaviorSubject({
|
|
312
|
-
type: 'state',
|
|
313
|
-
state: lazyOptions.enabled ? 'idle' : 'connecting',
|
|
314
|
-
error: null
|
|
315
|
-
});
|
|
316
|
-
// Automatically open the connection if lazy mode is disabled.
|
|
317
|
-
if (!this.lazyMode) {
|
|
318
|
-
this.open().catch(()=>null);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
export { WsClient };
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import type { UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';
|
|
2
|
-
interface PingPongOptions {
|
|
3
|
-
/**
|
|
4
|
-
* The interval (in milliseconds) between "PING" messages.
|
|
5
|
-
*/
|
|
6
|
-
intervalMs: number;
|
|
7
|
-
/**
|
|
8
|
-
* The timeout (in milliseconds) to wait for a "PONG" response before closing the connection.
|
|
9
|
-
*/
|
|
10
|
-
pongTimeoutMs: number;
|
|
11
|
-
}
|
|
12
|
-
export interface WebSocketConnectionOptions {
|
|
13
|
-
WebSocketPonyfill?: typeof WebSocket;
|
|
14
|
-
urlOptions: UrlOptionsWithConnectionParams;
|
|
15
|
-
keepAlive: PingPongOptions & {
|
|
16
|
-
enabled: boolean;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,
|
|
21
|
-
* and observable state tracking.
|
|
22
|
-
*/
|
|
23
|
-
export declare class WsConnection {
|
|
24
|
-
static connectCount: number;
|
|
25
|
-
id: number;
|
|
26
|
-
private readonly WebSocketPonyfill;
|
|
27
|
-
private readonly urlOptions;
|
|
28
|
-
private readonly keepAliveOpts;
|
|
29
|
-
readonly wsObservable: import("@trpc/server/observable").BehaviorSubject<WebSocket | null>;
|
|
30
|
-
constructor(opts: WebSocketConnectionOptions);
|
|
31
|
-
get ws(): WebSocket | null;
|
|
32
|
-
private set ws(value);
|
|
33
|
-
/**
|
|
34
|
-
* Checks if the WebSocket connection is open and ready to communicate.
|
|
35
|
-
*/
|
|
36
|
-
isOpen(): this is {
|
|
37
|
-
ws: WebSocket;
|
|
38
|
-
};
|
|
39
|
-
/**
|
|
40
|
-
* Checks if the WebSocket connection is closed or in the process of closing.
|
|
41
|
-
*/
|
|
42
|
-
isClosed(): this is {
|
|
43
|
-
ws: WebSocket;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Manages the WebSocket opening process, ensuring that only one open operation
|
|
47
|
-
* occurs at a time. Tracks the ongoing operation with `openPromise` to avoid
|
|
48
|
-
* redundant calls and ensure proper synchronization.
|
|
49
|
-
*
|
|
50
|
-
* Sets up the keep-alive mechanism and necessary event listeners for the connection.
|
|
51
|
-
*
|
|
52
|
-
* @returns A promise that resolves once the WebSocket connection is successfully opened.
|
|
53
|
-
*/
|
|
54
|
-
private openPromise;
|
|
55
|
-
open(): Promise<void>;
|
|
56
|
-
/**
|
|
57
|
-
* Closes the WebSocket connection gracefully.
|
|
58
|
-
* Waits for any ongoing open operation to complete before closing.
|
|
59
|
-
*/
|
|
60
|
-
close(): Promise<void>;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Provides a backward-compatible representation of the connection state.
|
|
64
|
-
*/
|
|
65
|
-
export declare function backwardCompatibility(connection: WsConnection): {
|
|
66
|
-
readonly id: number;
|
|
67
|
-
readonly state: "open";
|
|
68
|
-
readonly ws: WebSocket;
|
|
69
|
-
} | {
|
|
70
|
-
readonly id: number;
|
|
71
|
-
readonly state: "closed";
|
|
72
|
-
readonly ws: WebSocket;
|
|
73
|
-
} | {
|
|
74
|
-
readonly id: number;
|
|
75
|
-
readonly state: "connecting";
|
|
76
|
-
readonly ws: WebSocket;
|
|
77
|
-
} | null;
|
|
78
|
-
export {};
|
|
79
|
-
//# sourceMappingURL=wsConnection.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wsConnection.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/wsConnection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,yCAAyC,CAAC;AAoB9F,UAAU,eAAe;IACvB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,aAAa,EAAE,MAAM,CAAC;CACvB;AAuDD,MAAM,WAAW,0BAA0B;IACzC,iBAAiB,CAAC,EAAE,OAAO,SAAS,CAAC;IACrC,UAAU,EAAE,8BAA8B,CAAC;IAC3C,SAAS,EAAE,eAAe,GAAG;QAC3B,OAAO,EAAE,OAAO,CAAC;KAClB,CAAC;CACH;AAED;;;GAGG;AACH,qBAAa,YAAY;IACvB,MAAM,CAAC,YAAY,SAAK;IACjB,EAAE,SAA+B;IAExC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiC;IAC5D,OAAO,CAAC,QAAQ,CAAC,aAAa,CAA0C;IACxE,SAAgB,YAAY,sEAA2C;gBAE3D,IAAI,EAAE,0BAA0B;IAY5C,IAAW,EAAE,qBAEZ;IAED,OAAO,KAAK,EAAE,QAEb;IAED;;OAEG;IACI,MAAM,IAAI,IAAI,IAAI;QAAE,EAAE,EAAE,SAAS,CAAA;KAAE;IAQ1C;;OAEG;IACI,QAAQ,IAAI,IAAI,IAAI;QAAE,EAAE,EAAE,SAAS,CAAA;KAAE;IAQ5C;;;;;;;;OAQG;IACH,OAAO,CAAC,WAAW,CAA8B;IACpC,IAAI;IAyCjB;;;OAGG;IACU,KAAK;CAOnB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,YAAY;;;;;;;;;;;;SA0B7D"}
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var observable = require('@trpc/server/observable');
|
|
4
|
-
var utils = require('./utils.js');
|
|
5
|
-
|
|
6
|
-
function _define_property(obj, key, value) {
|
|
7
|
-
if (key in obj) {
|
|
8
|
-
Object.defineProperty(obj, key, {
|
|
9
|
-
value: value,
|
|
10
|
-
enumerable: true,
|
|
11
|
-
configurable: true,
|
|
12
|
-
writable: true
|
|
13
|
-
});
|
|
14
|
-
} else {
|
|
15
|
-
obj[key] = value;
|
|
16
|
-
}
|
|
17
|
-
return obj;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Opens a WebSocket connection asynchronously and returns a promise
|
|
21
|
-
* that resolves when the connection is successfully established.
|
|
22
|
-
* The promise rejects if an error occurs during the connection attempt.
|
|
23
|
-
*/ function asyncWsOpen(ws) {
|
|
24
|
-
const { promise, resolve, reject } = utils.withResolvers();
|
|
25
|
-
ws.addEventListener('open', ()=>{
|
|
26
|
-
ws.removeEventListener('error', reject);
|
|
27
|
-
resolve();
|
|
28
|
-
});
|
|
29
|
-
ws.addEventListener('error', reject);
|
|
30
|
-
return promise;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.
|
|
34
|
-
*
|
|
35
|
-
* - Sends "PING" messages at regular intervals defined by `intervalMs`.
|
|
36
|
-
* - If a "PONG" response is not received within the `pongTimeoutMs`, the WebSocket is closed.
|
|
37
|
-
* - The ping timer resets upon receiving any message to maintain activity.
|
|
38
|
-
* - Automatically starts the ping process when the WebSocket connection is opened.
|
|
39
|
-
* - Cleans up timers when the WebSocket is closed.
|
|
40
|
-
*
|
|
41
|
-
* @param ws - The WebSocket instance to manage.
|
|
42
|
-
* @param options - Configuration options for ping-pong intervals and timeouts.
|
|
43
|
-
*/ function setupPingInterval(ws, { intervalMs, pongTimeoutMs }) {
|
|
44
|
-
let pingTimeout;
|
|
45
|
-
let pongTimeout;
|
|
46
|
-
function start() {
|
|
47
|
-
pingTimeout = setTimeout(()=>{
|
|
48
|
-
ws.send('PING');
|
|
49
|
-
pongTimeout = setTimeout(()=>{
|
|
50
|
-
ws.close();
|
|
51
|
-
}, pongTimeoutMs);
|
|
52
|
-
}, intervalMs);
|
|
53
|
-
}
|
|
54
|
-
function reset() {
|
|
55
|
-
clearTimeout(pingTimeout);
|
|
56
|
-
start();
|
|
57
|
-
}
|
|
58
|
-
function pong() {
|
|
59
|
-
clearTimeout(pongTimeout);
|
|
60
|
-
reset();
|
|
61
|
-
}
|
|
62
|
-
ws.addEventListener('open', start);
|
|
63
|
-
ws.addEventListener('message', ({ data })=>{
|
|
64
|
-
clearTimeout(pingTimeout);
|
|
65
|
-
start();
|
|
66
|
-
if (data === 'PONG') {
|
|
67
|
-
pong();
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
ws.addEventListener('close', ()=>{
|
|
71
|
-
clearTimeout(pingTimeout);
|
|
72
|
-
clearTimeout(pongTimeout);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,
|
|
77
|
-
* and observable state tracking.
|
|
78
|
-
*/ class WsConnection {
|
|
79
|
-
get ws() {
|
|
80
|
-
return this.wsObservable.get();
|
|
81
|
-
}
|
|
82
|
-
set ws(ws) {
|
|
83
|
-
this.wsObservable.next(ws);
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Checks if the WebSocket connection is open and ready to communicate.
|
|
87
|
-
*/ isOpen() {
|
|
88
|
-
return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN && !this.openPromise;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Checks if the WebSocket connection is closed or in the process of closing.
|
|
92
|
-
*/ isClosed() {
|
|
93
|
-
return !!this.ws && (this.ws.readyState === this.WebSocketPonyfill.CLOSING || this.ws.readyState === this.WebSocketPonyfill.CLOSED);
|
|
94
|
-
}
|
|
95
|
-
async open() {
|
|
96
|
-
if (this.openPromise) return this.openPromise;
|
|
97
|
-
this.id = ++WsConnection.connectCount;
|
|
98
|
-
const wsPromise = utils.prepareUrl(this.urlOptions).then((url)=>new this.WebSocketPonyfill(url));
|
|
99
|
-
this.openPromise = wsPromise.then(async (ws)=>{
|
|
100
|
-
this.ws = ws;
|
|
101
|
-
// Setup ping listener
|
|
102
|
-
ws.addEventListener('message', function({ data }) {
|
|
103
|
-
if (data === 'PING') {
|
|
104
|
-
this.send('PONG');
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
if (this.keepAliveOpts.enabled) {
|
|
108
|
-
setupPingInterval(ws, this.keepAliveOpts);
|
|
109
|
-
}
|
|
110
|
-
ws.addEventListener('close', ()=>{
|
|
111
|
-
if (this.ws === ws) {
|
|
112
|
-
this.ws = null;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
await asyncWsOpen(ws);
|
|
116
|
-
if (this.urlOptions.connectionParams) {
|
|
117
|
-
ws.send(await utils.buildConnectionMessage(this.urlOptions.connectionParams));
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
try {
|
|
121
|
-
await this.openPromise;
|
|
122
|
-
} finally{
|
|
123
|
-
this.openPromise = null;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Closes the WebSocket connection gracefully.
|
|
128
|
-
* Waits for any ongoing open operation to complete before closing.
|
|
129
|
-
*/ async close() {
|
|
130
|
-
try {
|
|
131
|
-
await this.openPromise;
|
|
132
|
-
} finally{
|
|
133
|
-
this.ws?.close();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
constructor(opts){
|
|
137
|
-
_define_property(this, "id", ++WsConnection.connectCount);
|
|
138
|
-
_define_property(this, "WebSocketPonyfill", void 0);
|
|
139
|
-
_define_property(this, "urlOptions", void 0);
|
|
140
|
-
_define_property(this, "keepAliveOpts", void 0);
|
|
141
|
-
_define_property(this, "wsObservable", observable.behaviorSubject(null));
|
|
142
|
-
/**
|
|
143
|
-
* Manages the WebSocket opening process, ensuring that only one open operation
|
|
144
|
-
* occurs at a time. Tracks the ongoing operation with `openPromise` to avoid
|
|
145
|
-
* redundant calls and ensure proper synchronization.
|
|
146
|
-
*
|
|
147
|
-
* Sets up the keep-alive mechanism and necessary event listeners for the connection.
|
|
148
|
-
*
|
|
149
|
-
* @returns A promise that resolves once the WebSocket connection is successfully opened.
|
|
150
|
-
*/ _define_property(this, "openPromise", null);
|
|
151
|
-
this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;
|
|
152
|
-
if (!this.WebSocketPonyfill) {
|
|
153
|
-
throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
|
|
154
|
-
}
|
|
155
|
-
this.urlOptions = opts.urlOptions;
|
|
156
|
-
this.keepAliveOpts = opts.keepAlive;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
_define_property(WsConnection, "connectCount", 0);
|
|
160
|
-
/**
|
|
161
|
-
* Provides a backward-compatible representation of the connection state.
|
|
162
|
-
*/ function backwardCompatibility(connection) {
|
|
163
|
-
if (connection.isOpen()) {
|
|
164
|
-
return {
|
|
165
|
-
id: connection.id,
|
|
166
|
-
state: 'open',
|
|
167
|
-
ws: connection.ws
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
if (connection.isClosed()) {
|
|
171
|
-
return {
|
|
172
|
-
id: connection.id,
|
|
173
|
-
state: 'closed',
|
|
174
|
-
ws: connection.ws
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
if (!connection.ws) {
|
|
178
|
-
return null;
|
|
179
|
-
}
|
|
180
|
-
return {
|
|
181
|
-
id: connection.id,
|
|
182
|
-
state: 'connecting',
|
|
183
|
-
ws: connection.ws
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
exports.WsConnection = WsConnection;
|
|
188
|
-
exports.backwardCompatibility = backwardCompatibility;
|