@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.
Files changed (51) hide show
  1. package/dist/bundle-analysis.json +132 -39
  2. package/dist/index.js +3 -2
  3. package/dist/index.mjs +2 -1
  4. package/dist/links/wsLink/createWsClient.d.ts +6 -0
  5. package/dist/links/wsLink/createWsClient.d.ts.map +1 -0
  6. package/dist/links/wsLink/createWsClient.js +9 -0
  7. package/dist/links/wsLink/createWsClient.mjs +7 -0
  8. package/dist/links/wsLink/wsClient/options.d.ts +79 -0
  9. package/dist/links/wsLink/wsClient/options.d.ts.map +1 -0
  10. package/dist/links/wsLink/wsClient/options.js +22 -0
  11. package/dist/links/wsLink/wsClient/options.mjs +18 -0
  12. package/dist/links/wsLink/wsClient/requestManager.d.ts +102 -0
  13. package/dist/links/wsLink/wsClient/requestManager.d.ts.map +1 -0
  14. package/dist/links/wsLink/wsClient/requestManager.js +138 -0
  15. package/dist/links/wsLink/wsClient/requestManager.mjs +136 -0
  16. package/dist/links/wsLink/wsClient/utils.d.ts +38 -0
  17. package/dist/links/wsLink/wsClient/utils.d.ts.map +1 -0
  18. package/dist/links/wsLink/wsClient/utils.js +94 -0
  19. package/dist/links/wsLink/wsClient/utils.mjs +88 -0
  20. package/dist/links/wsLink/wsClient/wsClient.d.ts +85 -0
  21. package/dist/links/wsLink/wsClient/wsClient.d.ts.map +1 -0
  22. package/dist/links/wsLink/wsClient/wsClient.js +331 -0
  23. package/dist/links/wsLink/wsClient/wsClient.mjs +329 -0
  24. package/dist/links/wsLink/wsClient/wsConnection.d.ts +79 -0
  25. package/dist/links/wsLink/wsClient/wsConnection.d.ts.map +1 -0
  26. package/dist/links/wsLink/wsClient/wsConnection.js +181 -0
  27. package/dist/links/wsLink/wsClient/wsConnection.mjs +178 -0
  28. package/dist/links/wsLink/wsLink.d.ts +11 -0
  29. package/dist/links/wsLink/wsLink.d.ts.map +1 -0
  30. package/dist/links/wsLink/wsLink.js +35 -0
  31. package/dist/links/wsLink/wsLink.mjs +32 -0
  32. package/dist/links.d.ts +1 -1
  33. package/dist/links.d.ts.map +1 -1
  34. package/links/wsLink/wsLink/index.d.ts +1 -0
  35. package/links/wsLink/wsLink/index.js +1 -0
  36. package/package.json +8 -8
  37. package/src/links/wsLink/createWsClient.ts +10 -0
  38. package/src/links/wsLink/wsClient/options.ts +91 -0
  39. package/src/links/wsLink/wsClient/requestManager.ts +174 -0
  40. package/src/links/wsLink/wsClient/utils.ts +94 -0
  41. package/src/links/wsLink/wsClient/wsClient.ts +441 -0
  42. package/src/links/wsLink/wsClient/wsConnection.ts +230 -0
  43. package/src/links/wsLink/wsLink.ts +55 -0
  44. package/src/links.ts +1 -1
  45. package/dist/links/wsLink.d.ts +0 -125
  46. package/dist/links/wsLink.d.ts.map +0 -1
  47. package/dist/links/wsLink.js +0 -498
  48. package/dist/links/wsLink.mjs +0 -495
  49. package/links/wsLink/index.d.ts +0 -1
  50. package/links/wsLink/index.js +0 -1
  51. package/src/links/wsLink.ts +0 -737
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ var utils = require('./utils.js');
4
+
5
+ function _define_property(obj, key, value) {
6
+ if (key in obj) {
7
+ Object.defineProperty(obj, key, {
8
+ value: value,
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true
12
+ });
13
+ } else {
14
+ obj[key] = value;
15
+ }
16
+ return obj;
17
+ }
18
+ /**
19
+ * Manages WebSocket requests, tracking their lifecycle and providing utility methods
20
+ * for handling outgoing and pending requests.
21
+ *
22
+ * - **Outgoing requests**: Requests that are queued and waiting to be sent.
23
+ * - **Pending requests**: Requests that have been sent and are in flight awaiting a response.
24
+ * For subscriptions, multiple responses may be received until the subscription is closed.
25
+ */ class RequestManager {
26
+ /**
27
+ * Registers a new request by adding it to the outgoing queue and setting up
28
+ * callbacks for lifecycle events such as completion or error.
29
+ *
30
+ * @param message - The outgoing message to be sent.
31
+ * @param callbacks - Callback functions to observe the request's state.
32
+ * @returns A cleanup function to manually remove the request.
33
+ */ register(message, callbacks) {
34
+ const { promise: end, resolve } = utils.withResolvers();
35
+ this.outgoingRequests.push({
36
+ id: String(message.id),
37
+ message,
38
+ end,
39
+ callbacks: {
40
+ next: callbacks.next,
41
+ complete: ()=>{
42
+ callbacks.complete();
43
+ resolve();
44
+ },
45
+ error: (e)=>{
46
+ callbacks.error(e);
47
+ resolve();
48
+ }
49
+ }
50
+ });
51
+ return ()=>{
52
+ this.delete(message.id);
53
+ callbacks.complete();
54
+ resolve();
55
+ };
56
+ }
57
+ /**
58
+ * Deletes a request from both the outgoing and pending collections, if it exists.
59
+ */ delete(messageId) {
60
+ if (messageId === null) return;
61
+ this.outgoingRequests = this.outgoingRequests.filter(({ id })=>id !== String(messageId));
62
+ delete this.pendingRequests[String(messageId)];
63
+ }
64
+ /**
65
+ * Moves all outgoing requests to the pending state and clears the outgoing queue.
66
+ *
67
+ * The caller is expected to handle the actual sending of the requests
68
+ * (e.g., sending them over the network) after this method is called.
69
+ *
70
+ * @returns The list of requests that were transitioned to the pending state.
71
+ */ flush() {
72
+ const requests = this.outgoingRequests;
73
+ this.outgoingRequests = [];
74
+ for (const request of requests){
75
+ this.pendingRequests[request.id] = request;
76
+ }
77
+ return requests;
78
+ }
79
+ /**
80
+ * Retrieves all currently pending requests, which are in flight awaiting responses
81
+ * or handling ongoing subscriptions.
82
+ */ getPendingRequests() {
83
+ return Object.values(this.pendingRequests);
84
+ }
85
+ /**
86
+ * Retrieves a specific pending request by its message ID.
87
+ */ getPendingRequest(messageId) {
88
+ if (messageId === null) return null;
89
+ return this.pendingRequests[String(messageId)];
90
+ }
91
+ /**
92
+ * Retrieves all outgoing requests, which are waiting to be sent.
93
+ */ getOutgoingRequests() {
94
+ return this.outgoingRequests;
95
+ }
96
+ /**
97
+ * Retrieves all requests, both outgoing and pending, with their respective states.
98
+ *
99
+ * @returns An array of all requests with their state ("outgoing" or "pending").
100
+ */ getRequests() {
101
+ return [
102
+ ...this.getOutgoingRequests().map((request)=>({
103
+ state: 'outgoing',
104
+ message: request.message,
105
+ end: request.end,
106
+ callbacks: request.callbacks
107
+ })),
108
+ ...this.getPendingRequests().map((request)=>({
109
+ state: 'pending',
110
+ message: request.message,
111
+ end: request.end,
112
+ callbacks: request.callbacks
113
+ }))
114
+ ];
115
+ }
116
+ /**
117
+ * Checks if there are any pending requests, including ongoing subscriptions.
118
+ */ hasPendingRequests() {
119
+ return this.getPendingRequests().length > 0;
120
+ }
121
+ /**
122
+ * Checks if there are any outgoing requests waiting to be sent.
123
+ */ hasOutgoingRequests() {
124
+ return this.outgoingRequests.length > 0;
125
+ }
126
+ constructor(){
127
+ /**
128
+ * Stores requests that are outgoing, meaning they are registered but not yet sent over the WebSocket.
129
+ */ _define_property(this, "outgoingRequests", new Array());
130
+ /**
131
+ * Stores requests that are pending (in flight), meaning they have been sent over the WebSocket
132
+ * and are awaiting responses. For subscriptions, this includes requests
133
+ * that may receive multiple responses.
134
+ */ _define_property(this, "pendingRequests", {});
135
+ }
136
+ }
137
+
138
+ exports.RequestManager = RequestManager;
@@ -0,0 +1,136 @@
1
+ import { withResolvers } from './utils.mjs';
2
+
3
+ function _define_property(obj, key, value) {
4
+ if (key in obj) {
5
+ Object.defineProperty(obj, key, {
6
+ value: value,
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true
10
+ });
11
+ } else {
12
+ obj[key] = value;
13
+ }
14
+ return obj;
15
+ }
16
+ /**
17
+ * Manages WebSocket requests, tracking their lifecycle and providing utility methods
18
+ * for handling outgoing and pending requests.
19
+ *
20
+ * - **Outgoing requests**: Requests that are queued and waiting to be sent.
21
+ * - **Pending requests**: Requests that have been sent and are in flight awaiting a response.
22
+ * For subscriptions, multiple responses may be received until the subscription is closed.
23
+ */ class RequestManager {
24
+ /**
25
+ * Registers a new request by adding it to the outgoing queue and setting up
26
+ * callbacks for lifecycle events such as completion or error.
27
+ *
28
+ * @param message - The outgoing message to be sent.
29
+ * @param callbacks - Callback functions to observe the request's state.
30
+ * @returns A cleanup function to manually remove the request.
31
+ */ register(message, callbacks) {
32
+ const { promise: end, resolve } = withResolvers();
33
+ this.outgoingRequests.push({
34
+ id: String(message.id),
35
+ message,
36
+ end,
37
+ callbacks: {
38
+ next: callbacks.next,
39
+ complete: ()=>{
40
+ callbacks.complete();
41
+ resolve();
42
+ },
43
+ error: (e)=>{
44
+ callbacks.error(e);
45
+ resolve();
46
+ }
47
+ }
48
+ });
49
+ return ()=>{
50
+ this.delete(message.id);
51
+ callbacks.complete();
52
+ resolve();
53
+ };
54
+ }
55
+ /**
56
+ * Deletes a request from both the outgoing and pending collections, if it exists.
57
+ */ delete(messageId) {
58
+ if (messageId === null) return;
59
+ this.outgoingRequests = this.outgoingRequests.filter(({ id })=>id !== String(messageId));
60
+ delete this.pendingRequests[String(messageId)];
61
+ }
62
+ /**
63
+ * Moves all outgoing requests to the pending state and clears the outgoing queue.
64
+ *
65
+ * The caller is expected to handle the actual sending of the requests
66
+ * (e.g., sending them over the network) after this method is called.
67
+ *
68
+ * @returns The list of requests that were transitioned to the pending state.
69
+ */ flush() {
70
+ const requests = this.outgoingRequests;
71
+ this.outgoingRequests = [];
72
+ for (const request of requests){
73
+ this.pendingRequests[request.id] = request;
74
+ }
75
+ return requests;
76
+ }
77
+ /**
78
+ * Retrieves all currently pending requests, which are in flight awaiting responses
79
+ * or handling ongoing subscriptions.
80
+ */ getPendingRequests() {
81
+ return Object.values(this.pendingRequests);
82
+ }
83
+ /**
84
+ * Retrieves a specific pending request by its message ID.
85
+ */ getPendingRequest(messageId) {
86
+ if (messageId === null) return null;
87
+ return this.pendingRequests[String(messageId)];
88
+ }
89
+ /**
90
+ * Retrieves all outgoing requests, which are waiting to be sent.
91
+ */ getOutgoingRequests() {
92
+ return this.outgoingRequests;
93
+ }
94
+ /**
95
+ * Retrieves all requests, both outgoing and pending, with their respective states.
96
+ *
97
+ * @returns An array of all requests with their state ("outgoing" or "pending").
98
+ */ getRequests() {
99
+ return [
100
+ ...this.getOutgoingRequests().map((request)=>({
101
+ state: 'outgoing',
102
+ message: request.message,
103
+ end: request.end,
104
+ callbacks: request.callbacks
105
+ })),
106
+ ...this.getPendingRequests().map((request)=>({
107
+ state: 'pending',
108
+ message: request.message,
109
+ end: request.end,
110
+ callbacks: request.callbacks
111
+ }))
112
+ ];
113
+ }
114
+ /**
115
+ * Checks if there are any pending requests, including ongoing subscriptions.
116
+ */ hasPendingRequests() {
117
+ return this.getPendingRequests().length > 0;
118
+ }
119
+ /**
120
+ * Checks if there are any outgoing requests waiting to be sent.
121
+ */ hasOutgoingRequests() {
122
+ return this.outgoingRequests.length > 0;
123
+ }
124
+ constructor(){
125
+ /**
126
+ * Stores requests that are outgoing, meaning they are registered but not yet sent over the WebSocket.
127
+ */ _define_property(this, "outgoingRequests", new Array());
128
+ /**
129
+ * Stores requests that are pending (in flight), meaning they have been sent over the WebSocket
130
+ * and are awaiting responses. For subscriptions, this includes requests
131
+ * that may receive multiple responses.
132
+ */ _define_property(this, "pendingRequests", {});
133
+ }
134
+ }
135
+
136
+ export { RequestManager };
@@ -0,0 +1,38 @@
1
+ import type { TRPCRequestInfo } from '@trpc/server/unstable-core-do-not-import';
2
+ import type { CallbackOrValue, UrlOptionsWithConnectionParams } from '../../internals/urlWithConnectionParams';
3
+ export declare class TRPCWebSocketClosedError extends Error {
4
+ constructor(opts: {
5
+ message: string;
6
+ cause?: unknown;
7
+ });
8
+ }
9
+ /**
10
+ * Utility class for managing a timeout that can be started, stopped, and reset.
11
+ * Useful for scenarios where the timeout duration is reset dynamically based on events.
12
+ */
13
+ export declare class ResettableTimeout {
14
+ private readonly onTimeout;
15
+ private readonly timeoutMs;
16
+ private timeout;
17
+ constructor(onTimeout: () => void, timeoutMs: number);
18
+ /**
19
+ * Resets the current timeout, restarting it with the same duration.
20
+ * Does nothing if no timeout is active.
21
+ */
22
+ reset(): void;
23
+ start(): void;
24
+ stop(): void;
25
+ }
26
+ export declare function withResolvers<T>(): {
27
+ promise: Promise<T>;
28
+ resolve: (value: T | PromiseLike<T>) => void;
29
+ reject: (reason?: any) => void;
30
+ };
31
+ /**
32
+ * Resolves a WebSocket URL and optionally appends connection parameters.
33
+ *
34
+ * If connectionParams are provided, appends 'connectionParams=1' query parameter.
35
+ */
36
+ export declare function prepareUrl(urlOptions: UrlOptionsWithConnectionParams): Promise<string>;
37
+ export declare function buildConnectionMessage(connectionParams: CallbackOrValue<TRPCRequestInfo['connectionParams']>): Promise<string>;
38
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,eAAe,EAChB,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EACV,eAAe,EACf,8BAA8B,EAC/B,MAAM,yCAAyC,CAAC;AAGjD,qBAAa,wBAAyB,SAAQ,KAAK;gBACrC,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE;CAOvD;AAED;;;GAGG;AACH,qBAAa,iBAAiB;IAI1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAJ5B,OAAO,CAAC,OAAO,CAA4C;gBAGxC,SAAS,EAAE,MAAM,IAAI,EACrB,SAAS,EAAE,MAAM;IAGpC;;;OAGG;IACI,KAAK;IAOL,KAAK;IAKL,IAAI;CAIZ;AAGD,wBAAgB,aAAa,CAAC,CAAC;;qBACR,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI;sBAC1B,GAAG,KAAK,IAAI;EAQnC;AAED;;;;GAIG;AACH,wBAAsB,UAAU,CAAC,UAAU,EAAE,8BAA8B,mBAU1E;AAED,wBAAsB,sBAAsB,CAC1C,gBAAgB,EAAE,eAAe,CAAC,eAAe,CAAC,kBAAkB,CAAC,CAAC,mBAQvE"}
@@ -0,0 +1,94 @@
1
+ 'use strict';
2
+
3
+ var urlWithConnectionParams = require('../../internals/urlWithConnectionParams.js');
4
+
5
+ function _define_property(obj, key, value) {
6
+ if (key in obj) {
7
+ Object.defineProperty(obj, key, {
8
+ value: value,
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true
12
+ });
13
+ } else {
14
+ obj[key] = value;
15
+ }
16
+ return obj;
17
+ }
18
+ class TRPCWebSocketClosedError extends Error {
19
+ constructor(opts){
20
+ super(opts.message, {
21
+ cause: opts.cause
22
+ });
23
+ this.name = 'TRPCWebSocketClosedError';
24
+ Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
25
+ }
26
+ }
27
+ /**
28
+ * Utility class for managing a timeout that can be started, stopped, and reset.
29
+ * Useful for scenarios where the timeout duration is reset dynamically based on events.
30
+ */ class ResettableTimeout {
31
+ /**
32
+ * Resets the current timeout, restarting it with the same duration.
33
+ * Does nothing if no timeout is active.
34
+ */ reset() {
35
+ if (!this.timeout) return;
36
+ clearTimeout(this.timeout);
37
+ this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
38
+ }
39
+ start() {
40
+ clearTimeout(this.timeout);
41
+ this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
42
+ }
43
+ stop() {
44
+ clearTimeout(this.timeout);
45
+ this.timeout = undefined;
46
+ }
47
+ constructor(onTimeout, timeoutMs){
48
+ _define_property(this, "onTimeout", void 0);
49
+ _define_property(this, "timeoutMs", void 0);
50
+ _define_property(this, "timeout", void 0);
51
+ this.onTimeout = onTimeout;
52
+ this.timeoutMs = timeoutMs;
53
+ }
54
+ }
55
+ // Ponyfill for Promise.withResolvers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
56
+ function withResolvers() {
57
+ let resolve;
58
+ let reject;
59
+ const promise = new Promise((res, rej)=>{
60
+ resolve = res;
61
+ reject = rej;
62
+ });
63
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
64
+ return {
65
+ promise,
66
+ resolve: resolve,
67
+ reject: reject
68
+ };
69
+ }
70
+ /**
71
+ * Resolves a WebSocket URL and optionally appends connection parameters.
72
+ *
73
+ * If connectionParams are provided, appends 'connectionParams=1' query parameter.
74
+ */ async function prepareUrl(urlOptions) {
75
+ const url = await urlWithConnectionParams.resultOf(urlOptions.url);
76
+ if (!urlOptions.connectionParams) return url;
77
+ // append `?connectionParams=1` when connection params are used
78
+ const prefix = url.includes('?') ? '&' : '?';
79
+ const connectionParams = `${prefix}connectionParams=1`;
80
+ return url + connectionParams;
81
+ }
82
+ async function buildConnectionMessage(connectionParams) {
83
+ const message = {
84
+ method: 'connectionParams',
85
+ data: await urlWithConnectionParams.resultOf(connectionParams)
86
+ };
87
+ return JSON.stringify(message);
88
+ }
89
+
90
+ exports.ResettableTimeout = ResettableTimeout;
91
+ exports.TRPCWebSocketClosedError = TRPCWebSocketClosedError;
92
+ exports.buildConnectionMessage = buildConnectionMessage;
93
+ exports.prepareUrl = prepareUrl;
94
+ exports.withResolvers = withResolvers;
@@ -0,0 +1,88 @@
1
+ import { resultOf } from '../../internals/urlWithConnectionParams.mjs';
2
+
3
+ function _define_property(obj, key, value) {
4
+ if (key in obj) {
5
+ Object.defineProperty(obj, key, {
6
+ value: value,
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true
10
+ });
11
+ } else {
12
+ obj[key] = value;
13
+ }
14
+ return obj;
15
+ }
16
+ class TRPCWebSocketClosedError extends Error {
17
+ constructor(opts){
18
+ super(opts.message, {
19
+ cause: opts.cause
20
+ });
21
+ this.name = 'TRPCWebSocketClosedError';
22
+ Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
23
+ }
24
+ }
25
+ /**
26
+ * Utility class for managing a timeout that can be started, stopped, and reset.
27
+ * Useful for scenarios where the timeout duration is reset dynamically based on events.
28
+ */ class ResettableTimeout {
29
+ /**
30
+ * Resets the current timeout, restarting it with the same duration.
31
+ * Does nothing if no timeout is active.
32
+ */ reset() {
33
+ if (!this.timeout) return;
34
+ clearTimeout(this.timeout);
35
+ this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
36
+ }
37
+ start() {
38
+ clearTimeout(this.timeout);
39
+ this.timeout = setTimeout(this.onTimeout, this.timeoutMs);
40
+ }
41
+ stop() {
42
+ clearTimeout(this.timeout);
43
+ this.timeout = undefined;
44
+ }
45
+ constructor(onTimeout, timeoutMs){
46
+ _define_property(this, "onTimeout", void 0);
47
+ _define_property(this, "timeoutMs", void 0);
48
+ _define_property(this, "timeout", void 0);
49
+ this.onTimeout = onTimeout;
50
+ this.timeoutMs = timeoutMs;
51
+ }
52
+ }
53
+ // Ponyfill for Promise.withResolvers https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
54
+ function withResolvers() {
55
+ let resolve;
56
+ let reject;
57
+ const promise = new Promise((res, rej)=>{
58
+ resolve = res;
59
+ reject = rej;
60
+ });
61
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
62
+ return {
63
+ promise,
64
+ resolve: resolve,
65
+ 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
+ */ async function prepareUrl(urlOptions) {
73
+ const url = await resultOf(urlOptions.url);
74
+ if (!urlOptions.connectionParams) return url;
75
+ // append `?connectionParams=1` when connection params are used
76
+ const prefix = url.includes('?') ? '&' : '?';
77
+ const connectionParams = `${prefix}connectionParams=1`;
78
+ return url + connectionParams;
79
+ }
80
+ async function buildConnectionMessage(connectionParams) {
81
+ const message = {
82
+ method: 'connectionParams',
83
+ data: await resultOf(connectionParams)
84
+ };
85
+ return JSON.stringify(message);
86
+ }
87
+
88
+ export { ResettableTimeout, TRPCWebSocketClosedError, buildConnectionMessage, prepareUrl, withResolvers };
@@ -0,0 +1,85 @@
1
+ import type { AnyTRPCRouter } from '@trpc/server';
2
+ import type { BehaviorSubject } from '@trpc/server/observable';
3
+ import type { CombinedDataTransformer } from '@trpc/server/unstable-core-do-not-import';
4
+ import { TRPCClientError } from '../../../TRPCClientError';
5
+ import type { TRPCConnectionState } from '../../internals/subscriptions';
6
+ import type { Operation, OperationResultEnvelope } from '../../types';
7
+ import type { WebSocketClientOptions } from './options';
8
+ /**
9
+ * A WebSocket client for managing TRPC operations, supporting lazy initialization,
10
+ * reconnection, keep-alive, and request management.
11
+ */
12
+ export declare class WsClient {
13
+ /**
14
+ * Observable tracking the current connection state, including errors.
15
+ */
16
+ readonly connectionState: BehaviorSubject<TRPCConnectionState<TRPCClientError<AnyTRPCRouter>>>;
17
+ private allowReconnect;
18
+ private requestManager;
19
+ private readonly activeConnection;
20
+ private readonly reconnectRetryDelay;
21
+ private inactivityTimeout;
22
+ private readonly callbacks;
23
+ private readonly connectionParams;
24
+ private readonly lazyMode;
25
+ constructor(opts: WebSocketClientOptions);
26
+ /**
27
+ * Opens the WebSocket connection. Handles reconnection attempts and updates
28
+ * the connection state accordingly.
29
+ */
30
+ private open;
31
+ /**
32
+ * Closes the WebSocket connection and stops managing requests.
33
+ * Ensures all outgoing and pending requests are properly finalized.
34
+ */
35
+ close(): Promise<void>;
36
+ /**
37
+ * Method to request the server.
38
+ * Handles data transformation, batching of requests, and subscription lifecycle.
39
+ *
40
+ * @param op - The operation details including id, type, path, input and signal
41
+ * @param transformer - Data transformer for serializing requests and deserializing responses
42
+ * @param lastEventId - Optional ID of the last received event for subscriptions
43
+ *
44
+ * @returns An observable that emits operation results and handles cleanup
45
+ */
46
+ request({ op: { id, type, path, input, signal }, transformer, lastEventId, }: {
47
+ op: Pick<Operation, 'id' | 'type' | 'path' | 'input' | 'signal'>;
48
+ transformer: CombinedDataTransformer;
49
+ lastEventId?: string;
50
+ }): import("@trpc/server/observable").Observable<OperationResultEnvelope<unknown, TRPCClientError<AnyTRPCRouter>>, TRPCClientError<AnyTRPCRouter>>;
51
+ get connection(): {
52
+ readonly id: number;
53
+ readonly state: "open";
54
+ readonly ws: WebSocket;
55
+ } | {
56
+ readonly id: number;
57
+ readonly state: "closed";
58
+ readonly ws: WebSocket;
59
+ } | {
60
+ readonly id: number;
61
+ readonly state: "connecting";
62
+ readonly ws: WebSocket;
63
+ } | null;
64
+ /**
65
+ * Manages the reconnection process for the WebSocket using retry logic.
66
+ * Ensures that only one reconnection attempt is active at a time by tracking the current
67
+ * reconnection state in the `reconnecting` promise.
68
+ */
69
+ private reconnecting;
70
+ private reconnect;
71
+ private setupWebSocketListeners;
72
+ private handleResponseMessage;
73
+ private handleIncomingRequest;
74
+ /**
75
+ * Sends a message or batch of messages directly to the server.
76
+ */
77
+ private send;
78
+ /**
79
+ * Groups requests for batch sending.
80
+ *
81
+ * @returns A function to abort the batched request.
82
+ */
83
+ private batchSend;
84
+ }
85
+ //# sourceMappingURL=wsClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wsClient.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/wsClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAE/D,OAAO,KAAK,EACV,uBAAuB,EAKxB,MAAM,0CAA0C,CAAC;AAMlD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACzE,OAAO,KAAK,EAAE,SAAS,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AAWxD;;;GAGG;AACH,qBAAa,QAAQ;IACnB;;OAEG;IACH,SAAgB,eAAe,EAAE,eAAe,CAC9C,mBAAmB,CAAC,eAAe,CAAC,aAAa,CAAC,CAAC,CACpD,CAAC;IAEF,OAAO,CAAC,cAAc,CAAS;IAC/B,OAAO,CAAC,cAAc,CAAwB;IAC9C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAe;IAChD,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAmC;IACvE,OAAO,CAAC,iBAAiB,CAAoB;IAC7C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAGxB;IACF,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA6C;IAC9E,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;gBAEvB,IAAI,EAAE,sBAAsB;IA4DxC;;;OAGG;YACW,IAAI;IAuBlB;;;OAGG;IACU,KAAK;IA+BlB;;;;;;;;;OASG;IACI,OAAO,CAAC,EACb,EAAE,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EACrC,WAAW,EACX,WAAW,GACZ,EAAE;QACD,EAAE,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC,CAAC;QACjE,WAAW,EAAE,uBAAuB,CAAC;QACrC,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB;IA+CD,IAAW,UAAU;;;;;;;;;;;;aAEpB;IAED;;;;OAIG;IACH,OAAO,CAAC,YAAY,CAA8B;IAClD,OAAO,CAAC,SAAS;IAwBjB,OAAO,CAAC,uBAAuB;IA0F/B,OAAO,CAAC,qBAAqB;IAuB7B,OAAO,CAAC,qBAAqB;IAU7B;;OAEG;IACH,OAAO,CAAC,IAAI;IAgBZ;;;;OAIG;IACH,OAAO,CAAC,SAAS;CAmBlB"}