@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,79 @@
1
+ import type { UrlOptionsWithConnectionParams } from '@trpc/client/links/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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wsConnection.d.ts","sourceRoot":"","sources":["../../../../src/links/wsLink/wsClient/wsConnection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,sDAAsD,CAAC;AAqB3G,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;IAI1C;;OAEG;IACI,QAAQ,IAAI,IAAI,IAAI;QAAE,EAAE,EAAE,SAAS,CAAA;KAAE;IAQ5C;;;;;;;;OAQG;IACH,OAAO,CAAC,WAAW,CAA8B;IACpC,IAAI;IAgCjB;;;OAGG;IACU,KAAK;CAOnB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,YAAY;;;;;;;;;;;;SA0B7D"}
@@ -0,0 +1,181 @@
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;
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(asyncWsOpen);
100
+ this.ws = await wsPromise;
101
+ // Setup ping listener
102
+ this.ws.addEventListener('message', function({ data }) {
103
+ if (data === 'PING') {
104
+ this.send('PONG');
105
+ }
106
+ });
107
+ if (this.keepAliveOpts.enabled) {
108
+ setupPingInterval(this.ws, this.keepAliveOpts);
109
+ }
110
+ this.ws.addEventListener('close', ()=>{
111
+ this.ws = null;
112
+ });
113
+ try {
114
+ await this.openPromise;
115
+ } finally{
116
+ this.openPromise = null;
117
+ }
118
+ }
119
+ /**
120
+ * Closes the WebSocket connection gracefully.
121
+ * Waits for any ongoing open operation to complete before closing.
122
+ */ async close() {
123
+ try {
124
+ await this.openPromise;
125
+ } finally{
126
+ this.ws?.close();
127
+ }
128
+ }
129
+ constructor(opts){
130
+ _define_property(this, "id", ++WsConnection.connectCount);
131
+ _define_property(this, "WebSocketPonyfill", void 0);
132
+ _define_property(this, "urlOptions", void 0);
133
+ _define_property(this, "keepAliveOpts", void 0);
134
+ _define_property(this, "wsObservable", observable.behaviorSubject(null));
135
+ /**
136
+ * Manages the WebSocket opening process, ensuring that only one open operation
137
+ * occurs at a time. Tracks the ongoing operation with `openPromise` to avoid
138
+ * redundant calls and ensure proper synchronization.
139
+ *
140
+ * Sets up the keep-alive mechanism and necessary event listeners for the connection.
141
+ *
142
+ * @returns A promise that resolves once the WebSocket connection is successfully opened.
143
+ */ _define_property(this, "openPromise", null);
144
+ this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;
145
+ if (!this.WebSocketPonyfill) {
146
+ 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");
147
+ }
148
+ this.urlOptions = opts.urlOptions;
149
+ this.keepAliveOpts = opts.keepAlive;
150
+ }
151
+ }
152
+ _define_property(WsConnection, "connectCount", 0);
153
+ /**
154
+ * Provides a backward-compatible representation of the connection state.
155
+ */ function backwardCompatibility(connection) {
156
+ if (connection.isOpen()) {
157
+ return {
158
+ id: connection.id,
159
+ state: 'open',
160
+ ws: connection.ws
161
+ };
162
+ }
163
+ if (connection.isClosed()) {
164
+ return {
165
+ id: connection.id,
166
+ state: 'closed',
167
+ ws: connection.ws
168
+ };
169
+ }
170
+ if (!connection.ws) {
171
+ return null;
172
+ }
173
+ return {
174
+ id: connection.id,
175
+ state: 'connecting',
176
+ ws: connection.ws
177
+ };
178
+ }
179
+
180
+ exports.WsConnection = WsConnection;
181
+ exports.backwardCompatibility = backwardCompatibility;
@@ -0,0 +1,178 @@
1
+ import { behaviorSubject } from '@trpc/server/observable';
2
+ import { prepareUrl, withResolvers } from './utils.mjs';
3
+
4
+ function _define_property(obj, key, value) {
5
+ if (key in obj) {
6
+ Object.defineProperty(obj, key, {
7
+ value: value,
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true
11
+ });
12
+ } else {
13
+ obj[key] = value;
14
+ }
15
+ return obj;
16
+ }
17
+ /**
18
+ * Opens a WebSocket connection asynchronously and returns a promise
19
+ * that resolves when the connection is successfully established.
20
+ * The promise rejects if an error occurs during the connection attempt.
21
+ */ function asyncWsOpen(ws) {
22
+ const { promise, resolve, reject } = withResolvers();
23
+ ws.addEventListener('open', ()=>{
24
+ ws.removeEventListener('error', reject);
25
+ resolve();
26
+ });
27
+ ws.addEventListener('error', reject);
28
+ return promise;
29
+ }
30
+ /**
31
+ * Sets up a periodic ping-pong mechanism to keep the WebSocket connection alive.
32
+ *
33
+ * - Sends "PING" messages at regular intervals defined by `intervalMs`.
34
+ * - If a "PONG" response is not received within the `pongTimeoutMs`, the WebSocket is closed.
35
+ * - The ping timer resets upon receiving any message to maintain activity.
36
+ * - Automatically starts the ping process when the WebSocket connection is opened.
37
+ * - Cleans up timers when the WebSocket is closed.
38
+ *
39
+ * @param ws - The WebSocket instance to manage.
40
+ * @param options - Configuration options for ping-pong intervals and timeouts.
41
+ */ function setupPingInterval(ws, { intervalMs, pongTimeoutMs }) {
42
+ let pingTimeout;
43
+ let pongTimeout;
44
+ function start() {
45
+ pingTimeout = setTimeout(()=>{
46
+ ws.send('PING');
47
+ pongTimeout = setTimeout(()=>{
48
+ ws.close();
49
+ }, pongTimeoutMs);
50
+ }, intervalMs);
51
+ }
52
+ function reset() {
53
+ clearTimeout(pingTimeout);
54
+ start();
55
+ }
56
+ function pong() {
57
+ clearTimeout(pongTimeout);
58
+ reset();
59
+ }
60
+ ws.addEventListener('open', start);
61
+ ws.addEventListener('message', ({ data })=>{
62
+ clearTimeout(pingTimeout);
63
+ start();
64
+ if (data === 'PONG') {
65
+ pong();
66
+ }
67
+ });
68
+ ws.addEventListener('close', ()=>{
69
+ clearTimeout(pingTimeout);
70
+ clearTimeout(pongTimeout);
71
+ });
72
+ }
73
+ /**
74
+ * Manages a WebSocket connection with support for reconnection, keep-alive mechanisms,
75
+ * and observable state tracking.
76
+ */ class WsConnection {
77
+ get ws() {
78
+ return this.wsObservable.get();
79
+ }
80
+ set ws(ws) {
81
+ this.wsObservable.next(ws);
82
+ }
83
+ /**
84
+ * Checks if the WebSocket connection is open and ready to communicate.
85
+ */ isOpen() {
86
+ return !!this.ws && this.ws.readyState === this.WebSocketPonyfill.OPEN;
87
+ }
88
+ /**
89
+ * Checks if the WebSocket connection is closed or in the process of closing.
90
+ */ isClosed() {
91
+ return !!this.ws && (this.ws.readyState === this.WebSocketPonyfill.CLOSING || this.ws.readyState === this.WebSocketPonyfill.CLOSED);
92
+ }
93
+ async open() {
94
+ if (this.openPromise) return this.openPromise;
95
+ this.id = ++WsConnection.connectCount;
96
+ const wsPromise = prepareUrl(this.urlOptions).then((url)=>new this.WebSocketPonyfill(url));
97
+ this.openPromise = wsPromise.then(asyncWsOpen);
98
+ this.ws = await wsPromise;
99
+ // Setup ping listener
100
+ this.ws.addEventListener('message', function({ data }) {
101
+ if (data === 'PING') {
102
+ this.send('PONG');
103
+ }
104
+ });
105
+ if (this.keepAliveOpts.enabled) {
106
+ setupPingInterval(this.ws, this.keepAliveOpts);
107
+ }
108
+ this.ws.addEventListener('close', ()=>{
109
+ this.ws = null;
110
+ });
111
+ try {
112
+ await this.openPromise;
113
+ } finally{
114
+ this.openPromise = null;
115
+ }
116
+ }
117
+ /**
118
+ * Closes the WebSocket connection gracefully.
119
+ * Waits for any ongoing open operation to complete before closing.
120
+ */ async close() {
121
+ try {
122
+ await this.openPromise;
123
+ } finally{
124
+ this.ws?.close();
125
+ }
126
+ }
127
+ constructor(opts){
128
+ _define_property(this, "id", ++WsConnection.connectCount);
129
+ _define_property(this, "WebSocketPonyfill", void 0);
130
+ _define_property(this, "urlOptions", void 0);
131
+ _define_property(this, "keepAliveOpts", void 0);
132
+ _define_property(this, "wsObservable", behaviorSubject(null));
133
+ /**
134
+ * Manages the WebSocket opening process, ensuring that only one open operation
135
+ * occurs at a time. Tracks the ongoing operation with `openPromise` to avoid
136
+ * redundant calls and ensure proper synchronization.
137
+ *
138
+ * Sets up the keep-alive mechanism and necessary event listeners for the connection.
139
+ *
140
+ * @returns A promise that resolves once the WebSocket connection is successfully opened.
141
+ */ _define_property(this, "openPromise", null);
142
+ this.WebSocketPonyfill = opts.WebSocketPonyfill ?? WebSocket;
143
+ if (!this.WebSocketPonyfill) {
144
+ 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");
145
+ }
146
+ this.urlOptions = opts.urlOptions;
147
+ this.keepAliveOpts = opts.keepAlive;
148
+ }
149
+ }
150
+ _define_property(WsConnection, "connectCount", 0);
151
+ /**
152
+ * Provides a backward-compatible representation of the connection state.
153
+ */ function backwardCompatibility(connection) {
154
+ if (connection.isOpen()) {
155
+ return {
156
+ id: connection.id,
157
+ state: 'open',
158
+ ws: connection.ws
159
+ };
160
+ }
161
+ if (connection.isClosed()) {
162
+ return {
163
+ id: connection.id,
164
+ state: 'closed',
165
+ ws: connection.ws
166
+ };
167
+ }
168
+ if (!connection.ws) {
169
+ return null;
170
+ }
171
+ return {
172
+ id: connection.id,
173
+ state: 'connecting',
174
+ ws: connection.ws
175
+ };
176
+ }
177
+
178
+ export { WsConnection, backwardCompatibility };
@@ -0,0 +1,11 @@
1
+ import type { AnyRouter, inferClientTypes } from '@trpc/server/unstable-core-do-not-import';
2
+ import type { TransformerOptions } from '../../unstable-internals';
3
+ import type { TRPCLink } from '../types';
4
+ import type { TRPCWebSocketClient, WebSocketClientOptions } from './createWsClient';
5
+ import { createWSClient } from './createWsClient';
6
+ export type WebSocketLinkOptions<TRouter extends AnyRouter> = {
7
+ client: TRPCWebSocketClient;
8
+ } & TransformerOptions<inferClientTypes<TRouter>>;
9
+ export declare function wsLink<TRouter extends AnyRouter>(opts: WebSocketLinkOptions<TRouter>): TRPCLink<TRouter>;
10
+ export { TRPCWebSocketClient, WebSocketClientOptions, createWSClient };
11
+ //# sourceMappingURL=wsLink.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wsLink.d.ts","sourceRoot":"","sources":["../../../src/links/wsLink/wsLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EACjB,MAAM,0CAA0C,CAAC;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,EACV,mBAAmB,EACnB,sBAAsB,EACvB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD,MAAM,MAAM,oBAAoB,CAAC,OAAO,SAAS,SAAS,IAAI;IAC5D,MAAM,EAAE,mBAAmB,CAAC;CAC7B,GAAG,kBAAkB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC;AAElD,wBAAgB,MAAM,CAAC,OAAO,SAAS,SAAS,EAC9C,IAAI,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAClC,QAAQ,CAAC,OAAO,CAAC,CAgCnB;AAED,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,cAAc,EAAE,CAAC"}
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var observable = require('@trpc/server/observable');
4
+ var transformer = require('../../internals/transformer.js');
5
+ var createWsClient = require('./createWsClient.js');
6
+
7
+ function wsLink(opts) {
8
+ const { client } = opts;
9
+ const transformer$1 = transformer.getTransformer(opts.transformer);
10
+ return ()=>{
11
+ return ({ op })=>{
12
+ return observable.observable((observer)=>{
13
+ const connStateSubscription = op.type === 'subscription' ? client.connectionState.subscribe({
14
+ next (result) {
15
+ observer.next({
16
+ result,
17
+ context: op.context
18
+ });
19
+ }
20
+ }) : null;
21
+ const requestSubscription = client.request({
22
+ op,
23
+ transformer: transformer$1
24
+ }).subscribe(observer);
25
+ return ()=>{
26
+ requestSubscription.unsubscribe();
27
+ connStateSubscription?.unsubscribe();
28
+ };
29
+ });
30
+ };
31
+ };
32
+ }
33
+
34
+ exports.createWSClient = createWsClient.createWSClient;
35
+ exports.wsLink = wsLink;
@@ -0,0 +1,32 @@
1
+ import { observable } from '@trpc/server/observable';
2
+ import { getTransformer } from '../../internals/transformer.mjs';
3
+ export { createWSClient } from './createWsClient.mjs';
4
+
5
+ function wsLink(opts) {
6
+ const { client } = opts;
7
+ const transformer = getTransformer(opts.transformer);
8
+ return ()=>{
9
+ return ({ op })=>{
10
+ return observable((observer)=>{
11
+ const connStateSubscription = op.type === 'subscription' ? client.connectionState.subscribe({
12
+ next (result) {
13
+ observer.next({
14
+ result,
15
+ context: op.context
16
+ });
17
+ }
18
+ }) : null;
19
+ const requestSubscription = client.request({
20
+ op,
21
+ transformer
22
+ }).subscribe(observer);
23
+ return ()=>{
24
+ requestSubscription.unsubscribe();
25
+ connStateSubscription?.unsubscribe();
26
+ };
27
+ });
28
+ };
29
+ };
30
+ }
31
+
32
+ export { wsLink };
package/dist/links.d.ts CHANGED
@@ -5,7 +5,7 @@ export * from './links/httpBatchStreamLink';
5
5
  export * from './links/httpLink';
6
6
  export * from './links/loggerLink';
7
7
  export * from './links/splitLink';
8
- export * from './links/wsLink';
8
+ export * from './links/wsLink/wsLink';
9
9
  export * from './links/httpSubscriptionLink';
10
10
  export * from './links/retryLink';
11
11
  //# sourceMappingURL=links.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../src/links.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAE9B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mBAAmB,CAAC"}
1
+ {"version":3,"file":"links.d.ts","sourceRoot":"","sources":["../src/links.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAE9B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC;AACtC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from '../../../dist/links/wsLink/wsLink';
@@ -0,0 +1 @@
1
+ module.exports = require('../../../dist/links/wsLink/wsLink');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "11.0.0-rc.772+6627c5a96",
3
+ "version": "11.0.0-rc.781+df4d4ede3",
4
4
  "description": "The tRPC client library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -55,10 +55,10 @@
55
55
  "require": "./dist/links/splitLink.js",
56
56
  "default": "./dist/links/splitLink.js"
57
57
  },
58
- "./links/wsLink": {
59
- "import": "./dist/links/wsLink.mjs",
60
- "require": "./dist/links/wsLink.js",
61
- "default": "./dist/links/wsLink.js"
58
+ "./links/wsLink/wsLink": {
59
+ "import": "./dist/links/wsLink/wsLink.mjs",
60
+ "require": "./dist/links/wsLink/wsLink.js",
61
+ "default": "./dist/links/wsLink/wsLink.js"
62
62
  },
63
63
  "./unstable-internals": {
64
64
  "import": "./dist/unstable-internals.mjs",
@@ -77,11 +77,11 @@
77
77
  "!**/__tests__"
78
78
  ],
79
79
  "peerDependencies": {
80
- "@trpc/server": "11.0.0-rc.772+6627c5a96",
80
+ "@trpc/server": "11.0.0-rc.781+df4d4ede3",
81
81
  "typescript": ">=5.7.2"
82
82
  },
83
83
  "devDependencies": {
84
- "@trpc/server": "11.0.0-rc.772+6627c5a96",
84
+ "@trpc/server": "11.0.0-rc.781+df4d4ede3",
85
85
  "@types/isomorphic-fetch": "^0.0.39",
86
86
  "@types/node": "^22.9.0",
87
87
  "eslint": "^9.13.0",
@@ -99,5 +99,5 @@
99
99
  "funding": [
100
100
  "https://trpc.io/sponsor"
101
101
  ],
102
- "gitHead": "6627c5a965a2081cd28cfad24d6712e985f0ad5f"
102
+ "gitHead": "df4d4ede3492c91a0846f683ef2f1d0ffeed3b09"
103
103
  }
@@ -0,0 +1,10 @@
1
+ import type { WebSocketClientOptions } from './wsClient/options';
2
+ import { WsClient } from './wsClient/wsClient';
3
+
4
+ export function createWSClient(opts: WebSocketClientOptions) {
5
+ return new WsClient(opts);
6
+ }
7
+
8
+ export type TRPCWebSocketClient = ReturnType<typeof createWSClient>;
9
+
10
+ export { WebSocketClientOptions };