@trpc/client 10.0.0-alpha.8 → 10.0.0-proxy-alpha.53
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/README.md +58 -1
- package/dist/TRPCClientError.d.ts +16 -20
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +2 -3
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/createTRPCClientProxy.d.ts +37 -0
- package/dist/createTRPCClientProxy.d.ts.map +1 -0
- package/dist/httpUtils-44781613.mjs +76 -0
- package/dist/httpUtils-801112a6.js +79 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +260 -280
- package/dist/index.mjs +253 -271
- package/dist/internals/TRPCClient.d.ts +39 -21
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +7 -6
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/internals/fetchHelpers.d.ts +1 -1
- package/dist/internals/fetchHelpers.d.ts.map +1 -1
- package/dist/internals/retryDelay.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +2 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +189 -172
- package/dist/links/httpBatchLink.mjs +189 -172
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +31 -35
- package/dist/links/httpLink.mjs +31 -35
- package/dist/links/index.d.ts +9 -0
- package/dist/links/index.d.ts.map +1 -0
- package/dist/links/internals/createChain.d.ts.map +1 -1
- package/dist/links/internals/httpUtils.d.ts +31 -0
- package/dist/links/internals/httpUtils.d.ts.map +1 -0
- package/dist/links/internals/transformResult.d.ts +28 -0
- package/dist/links/internals/transformResult.d.ts.map +1 -0
- package/dist/links/loggerLink.d.ts +3 -3
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +90 -96
- package/dist/links/loggerLink.mjs +90 -91
- package/dist/links/retryLink.d.ts.map +1 -1
- package/dist/links/splitLink.d.ts +1 -1
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +3 -25
- package/dist/links/splitLink.mjs +2 -28
- package/dist/links/types.d.ts +13 -12
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +12 -5
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +250 -358
- package/dist/links/wsLink.mjs +250 -348
- package/dist/splitLink-695ae288.js +48 -0
- package/dist/splitLink-ad44a55d.mjs +45 -0
- package/dist/transformResult-106791d7.mjs +62 -0
- package/dist/transformResult-e15ccdf6.js +65 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +15 -13
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +12 -10
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +4 -4
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
- package/package.json +23 -34
- package/src/TRPCClientError.ts +38 -28
- package/src/createTRPCClient.ts +8 -3
- package/src/createTRPCClientProxy.ts +113 -0
- package/src/index.ts +2 -1
- package/src/internals/TRPCClient.ts +118 -77
- package/src/internals/dataLoader.ts +116 -53
- package/src/internals/fetchHelpers.ts +2 -2
- package/src/links/dedupeLink.test.ts +9 -15
- package/src/links/dedupeLink.ts +6 -7
- package/src/links/httpBatchLink.ts +73 -36
- package/src/links/httpLink.ts +18 -6
- package/src/links/index.ts +8 -0
- package/src/links/internals/createChain.test.ts +23 -38
- package/src/links/internals/createChain.ts +8 -3
- package/src/links/internals/httpUtils.ts +117 -0
- package/src/links/internals/transformResult.ts +40 -0
- package/src/links/loggerLink.ts +6 -7
- package/src/links/retryLink.ts +2 -3
- package/src/links/splitLink.test.ts +3 -3
- package/src/links/splitLink.ts +5 -5
- package/src/links/types.ts +15 -12
- package/src/links/wsLink.ts +57 -23
- package/dist/TRPCClientError-17b117e1.mjs +0 -81
- package/dist/TRPCClientError-ec6bd2ca.js +0 -94
- package/dist/createChain-389cc116.mjs +0 -25
- package/dist/createChain-864dbb30.js +0 -27
- package/dist/httpUtils-1f9e4e3c.js +0 -97
- package/dist/httpUtils-c09d7b79.mjs +0 -91
- package/dist/index.ts.js +0 -1
- package/dist/internals/transformRPCResponse.d.ts +0 -15
- package/dist/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/links/httpBatchLink.ts.js +0 -1
- package/dist/links/httpLink.ts.js +0 -1
- package/dist/links/httpUtils.d.ts +0 -22
- package/dist/links/httpUtils.d.ts.map +0 -1
- package/dist/links/internals/transformOperationResult.d.ts +0 -16
- package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
- package/dist/links/loggerLink.ts.js +0 -1
- package/dist/links/splitLink.ts.js +0 -1
- package/dist/links/transformerLink.d.ts +0 -4
- package/dist/links/transformerLink.d.ts.map +0 -1
- package/dist/links/transformerLink.js +0 -75
- package/dist/links/transformerLink.mjs +0 -67
- package/dist/links/transformerLink.ts.js +0 -1
- package/dist/links/wsLink.ts.js +0 -1
- package/dist/observable-7e66cb56.mjs +0 -102
- package/dist/observable-85669100.js +0 -104
- package/dist/rx/index.d.ts +0 -3
- package/dist/rx/index.d.ts.map +0 -1
- package/dist/rx/index.js +0 -9
- package/dist/rx/index.mjs +0 -1
- package/dist/rx/index.ts.js +0 -1
- package/dist/rx/observable.d.ts +0 -4
- package/dist/rx/observable.d.ts.map +0 -1
- package/dist/rx/operators/index.d.ts +0 -4
- package/dist/rx/operators/index.d.ts.map +0 -1
- package/dist/rx/operators/index.js +0 -38
- package/dist/rx/operators/index.mjs +0 -32
- package/dist/rx/operators/index.ts.js +0 -1
- package/dist/rx/operators/map.d.ts +0 -3
- package/dist/rx/operators/map.d.ts.map +0 -1
- package/dist/rx/operators/share.d.ts +0 -6
- package/dist/rx/operators/share.d.ts.map +0 -1
- package/dist/rx/operators/tap.d.ts +0 -3
- package/dist/rx/operators/tap.d.ts.map +0 -1
- package/dist/rx/types.d.ts +0 -29
- package/dist/rx/types.d.ts.map +0 -1
- package/dist/rx/util/identity.d.ts +0 -2
- package/dist/rx/util/identity.d.ts.map +0 -1
- package/dist/rx/util/observableToPromise.d.ts +0 -10
- package/dist/rx/util/observableToPromise.d.ts.map +0 -1
- package/dist/rx/util/pipe.d.ts +0 -4
- package/dist/rx/util/pipe.d.ts.map +0 -1
- package/dist/share-4d79dc49.js +0 -110
- package/dist/share-671b63fa.mjs +0 -108
- package/dist/tap-538fa07f.mjs +0 -30
- package/dist/tap-799a2d33.js +0 -32
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
- package/links/transformerLink/index.d.ts +0 -1
- package/links/transformerLink/index.js +0 -1
- package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
- package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
- package/rx/dist/trpc-client-rx.cjs.js +0 -7
- package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
- package/rx/dist/trpc-client-rx.esm.js +0 -1
- package/rx/index.d.ts +0 -1
- package/rx/index.js +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
- package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
- package/rx/operators/index.d.ts +0 -1
- package/rx/operators/index.js +0 -1
- package/src/internals/transformRPCResponse.ts +0 -25
- package/src/links/httpUtils.ts +0 -94
- package/src/links/internals/transformOperationResult.ts +0 -25
- package/src/links/transformerLink.ts +0 -70
- package/src/rx/index.ts +0 -2
- package/src/rx/observable.test.ts +0 -83
- package/src/rx/observable.ts +0 -70
- package/src/rx/operators/index.ts +0 -3
- package/src/rx/operators/map.test.ts +0 -92
- package/src/rx/operators/map.ts +0 -25
- package/src/rx/operators/share.test.ts +0 -60
- package/src/rx/operators/share.ts +0 -67
- package/src/rx/operators/tap.ts +0 -26
- package/src/rx/types.ts +0 -69
- package/src/rx/util/identity.ts +0 -3
- package/src/rx/util/observableToPromise.ts +0 -49
- package/src/rx/util/pipe.ts +0 -22
package/dist/links/wsLink.mjs
CHANGED
|
@@ -1,370 +1,272 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
|
4
|
-
import _inherits from '@babel/runtime/helpers/inherits';
|
|
5
|
-
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
6
|
-
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
|
|
7
|
-
import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
|
|
8
|
-
import { o as observable } from '../observable-7e66cb56.mjs';
|
|
9
|
-
import { T as TRPCClientError } from '../TRPCClientError-17b117e1.mjs';
|
|
10
|
-
import '@babel/runtime/helpers/defineProperty';
|
|
1
|
+
import { observable } from '@trpc/server/observable';
|
|
2
|
+
import { T as TRPCClientError, t as transformResult } from '../transformResult-106791d7.mjs';
|
|
11
3
|
|
|
12
|
-
/* istanbul ignore next */
|
|
13
|
-
var retryDelay = function retryDelay(attemptIndex) {
|
|
14
|
-
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
|
|
15
|
-
};
|
|
4
|
+
/* istanbul ignore next */ const retryDelay = (attemptIndex)=>attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
|
|
16
5
|
|
|
17
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
18
|
-
|
|
19
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
20
6
|
function createWSClient(opts) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
/* istanbul ignore next */
|
|
27
|
-
|
|
28
|
-
if (!WebSocketImpl) {
|
|
29
|
-
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");
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
7
|
+
const { url , WebSocket: WebSocketImpl = WebSocket , retryDelayMs: retryDelayFn = retryDelay , onOpen , onClose , } = opts;
|
|
8
|
+
/* istanbul ignore next */ if (!WebSocketImpl) {
|
|
9
|
+
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");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
32
12
|
* outgoing messages buffer whilst not open
|
|
33
|
-
*/
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var pendingRequests = Object.create(null);
|
|
42
|
-
var connectAttempt = 0;
|
|
43
|
-
var dispatchTimer = null;
|
|
44
|
-
var connectTimer = null;
|
|
45
|
-
var activeConnection = createWS();
|
|
46
|
-
var state = 'connecting';
|
|
47
|
-
/**
|
|
13
|
+
*/ let outgoing = [];
|
|
14
|
+
const pendingRequests = Object.create(null);
|
|
15
|
+
let connectAttempt = 0;
|
|
16
|
+
let dispatchTimer = null;
|
|
17
|
+
let connectTimer = null;
|
|
18
|
+
let activeConnection = createWS();
|
|
19
|
+
let state = 'connecting';
|
|
20
|
+
/**
|
|
48
21
|
* tries to send the list of messages
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
} // clear
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
outgoing = [];
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function tryReconnect() {
|
|
73
|
-
if (connectTimer || state === 'closed') {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
var timeout = retryDelayFn(connectAttempt++);
|
|
78
|
-
reconnectInMs(timeout);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function reconnect() {
|
|
82
|
-
state = 'connecting';
|
|
83
|
-
var oldConnection = activeConnection;
|
|
84
|
-
activeConnection = createWS();
|
|
85
|
-
closeIfNoPending(oldConnection);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function reconnectInMs(ms) {
|
|
89
|
-
if (connectTimer) {
|
|
90
|
-
return;
|
|
22
|
+
*/ function dispatch() {
|
|
23
|
+
if (state !== 'open' || dispatchTimer) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
dispatchTimer = setTimeout(()=>{
|
|
27
|
+
dispatchTimer = null;
|
|
28
|
+
if (outgoing.length === 1) {
|
|
29
|
+
// single send
|
|
30
|
+
activeConnection.send(JSON.stringify(outgoing.pop()));
|
|
31
|
+
} else {
|
|
32
|
+
// batch send
|
|
33
|
+
activeConnection.send(JSON.stringify(outgoing));
|
|
34
|
+
}
|
|
35
|
+
// clear
|
|
36
|
+
outgoing = [];
|
|
37
|
+
});
|
|
91
38
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// disconnect as soon as there are are no pending result
|
|
99
|
-
var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
|
|
100
|
-
return p.ws === conn;
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
if (!hasPendingRequests) {
|
|
104
|
-
conn.close();
|
|
39
|
+
function tryReconnect() {
|
|
40
|
+
if (connectTimer || state === 'closed') {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const timeout = retryDelayFn(connectAttempt++);
|
|
44
|
+
reconnectInMs(timeout);
|
|
105
45
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
})) {
|
|
112
|
-
return;
|
|
46
|
+
function reconnect() {
|
|
47
|
+
state = 'connecting';
|
|
48
|
+
const oldConnection = activeConnection;
|
|
49
|
+
activeConnection = createWS();
|
|
50
|
+
closeIfNoPending(oldConnection);
|
|
113
51
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
function createWS() {
|
|
119
|
-
var conn = new WebSocket(url);
|
|
120
|
-
clearTimeout(connectTimer);
|
|
121
|
-
connectTimer = null;
|
|
122
|
-
conn.addEventListener('open', function () {
|
|
123
|
-
/* istanbul ignore next */
|
|
124
|
-
if (conn !== activeConnection) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
connectAttempt = 0;
|
|
129
|
-
state = 'open';
|
|
130
|
-
dispatch();
|
|
131
|
-
});
|
|
132
|
-
conn.addEventListener('error', function () {
|
|
133
|
-
if (conn === activeConnection) {
|
|
134
|
-
tryReconnect();
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
var handleIncomingRequest = function handleIncomingRequest(req) {
|
|
139
|
-
if (req.method === 'reconnect' && conn === activeConnection) {
|
|
140
|
-
reconnect(); // notify subscribers
|
|
141
|
-
|
|
142
|
-
for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
|
|
143
|
-
var pendingReq = _Object$values[_i];
|
|
144
|
-
|
|
145
|
-
if (pendingReq.type === 'subscription') {
|
|
146
|
-
resumeSubscriptionOnReconnect(pendingReq);
|
|
147
|
-
}
|
|
52
|
+
function reconnectInMs(ms) {
|
|
53
|
+
if (connectTimer) {
|
|
54
|
+
return;
|
|
148
55
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if (!req) {
|
|
158
|
-
// do something?
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
(_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, {
|
|
163
|
-
data: data
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
if (req.ws !== activeConnection && conn === activeConnection) {
|
|
167
|
-
var oldWs = req.ws; // gracefully replace old connection with this
|
|
168
|
-
|
|
169
|
-
req.ws = activeConnection;
|
|
170
|
-
closeIfNoPending(oldWs);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
|
|
174
|
-
req.callbacks.complete();
|
|
175
|
-
}
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
conn.addEventListener('message', function (_ref) {
|
|
179
|
-
var data = _ref.data;
|
|
180
|
-
var msg = JSON.parse(data);
|
|
181
|
-
|
|
182
|
-
if ('method' in msg) {
|
|
183
|
-
handleIncomingRequest(msg);
|
|
184
|
-
} else {
|
|
185
|
-
handleIncomingResponse(msg);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (conn !== activeConnection || state === 'closed') {
|
|
189
|
-
// when receiving a message, we close old connection that has no pending requests
|
|
190
|
-
closeIfNoPending(conn);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
conn.addEventListener('close', function () {
|
|
194
|
-
if (activeConnection === conn) {
|
|
195
|
-
// connection might have been replaced already
|
|
196
|
-
tryReconnect();
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
for (var key in pendingRequests) {
|
|
200
|
-
var _req$callbacks$error, _req$callbacks2;
|
|
201
|
-
|
|
202
|
-
var req = pendingRequests[key];
|
|
203
|
-
|
|
204
|
-
if (req.ws !== conn) {
|
|
205
|
-
continue;
|
|
56
|
+
state = 'connecting';
|
|
57
|
+
connectTimer = setTimeout(reconnect, ms);
|
|
58
|
+
}
|
|
59
|
+
function closeIfNoPending(conn) {
|
|
60
|
+
// disconnect as soon as there are are no pending result
|
|
61
|
+
const hasPendingRequests = Object.values(pendingRequests).some((p)=>p.ws === conn);
|
|
62
|
+
if (!hasPendingRequests) {
|
|
63
|
+
conn.close();
|
|
206
64
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
var _req$callbacks$comple, _req$callbacks3;
|
|
212
|
-
|
|
213
|
-
delete pendingRequests[key];
|
|
214
|
-
(_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
|
|
215
|
-
} else if (state !== 'closed') {
|
|
216
|
-
// request restart of sub with next connection
|
|
217
|
-
resumeSubscriptionOnReconnect(req);
|
|
65
|
+
}
|
|
66
|
+
function resumeSubscriptionOnReconnect(req) {
|
|
67
|
+
if (outgoing.some((r)=>r.id === req.op.id)) {
|
|
68
|
+
return;
|
|
218
69
|
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
70
|
+
request(req.op, req.callbacks);
|
|
71
|
+
}
|
|
72
|
+
function createWS() {
|
|
73
|
+
const conn = new WebSocketImpl(url);
|
|
74
|
+
clearTimeout(connectTimer);
|
|
75
|
+
connectTimer = null;
|
|
76
|
+
conn.addEventListener('open', ()=>{
|
|
77
|
+
/* istanbul ignore next */ if (conn !== activeConnection) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
connectAttempt = 0;
|
|
81
|
+
state = 'open';
|
|
82
|
+
onOpen?.();
|
|
83
|
+
dispatch();
|
|
84
|
+
});
|
|
85
|
+
conn.addEventListener('error', ()=>{
|
|
86
|
+
if (conn === activeConnection) {
|
|
87
|
+
tryReconnect();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
const handleIncomingRequest = (req)=>{
|
|
91
|
+
if (req.method === 'reconnect' && conn === activeConnection) {
|
|
92
|
+
if (state === 'open') {
|
|
93
|
+
onClose?.();
|
|
94
|
+
}
|
|
95
|
+
reconnect();
|
|
96
|
+
// notify subscribers
|
|
97
|
+
for (const pendingReq of Object.values(pendingRequests)){
|
|
98
|
+
if (pendingReq.type === 'subscription') {
|
|
99
|
+
resumeSubscriptionOnReconnect(pendingReq);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const handleIncomingResponse = (data)=>{
|
|
105
|
+
const req = data.id !== null && pendingRequests[data.id];
|
|
106
|
+
if (!req) {
|
|
107
|
+
// do something?
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
req.callbacks.next?.(data);
|
|
111
|
+
if (req.ws !== activeConnection && conn === activeConnection) {
|
|
112
|
+
const oldWs = req.ws;
|
|
113
|
+
// gracefully replace old connection with this
|
|
114
|
+
req.ws = activeConnection;
|
|
115
|
+
closeIfNoPending(oldWs);
|
|
116
|
+
}
|
|
117
|
+
if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
|
|
118
|
+
req.callbacks.complete();
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
conn.addEventListener('message', ({ data })=>{
|
|
122
|
+
const msg = JSON.parse(data);
|
|
123
|
+
if ('method' in msg) {
|
|
124
|
+
handleIncomingRequest(msg);
|
|
125
|
+
} else {
|
|
126
|
+
handleIncomingResponse(msg);
|
|
127
|
+
}
|
|
128
|
+
if (conn !== activeConnection || state === 'closed') {
|
|
129
|
+
// when receiving a message, we close old connection that has no pending requests
|
|
130
|
+
closeIfNoPending(conn);
|
|
131
|
+
}
|
|
262
132
|
});
|
|
133
|
+
conn.addEventListener('close', ()=>{
|
|
134
|
+
if (state === 'open') {
|
|
135
|
+
onClose?.();
|
|
136
|
+
}
|
|
137
|
+
if (activeConnection === conn) {
|
|
138
|
+
// connection might have been replaced already
|
|
139
|
+
tryReconnect();
|
|
140
|
+
}
|
|
141
|
+
for (const [key, req] of Object.entries(pendingRequests)){
|
|
142
|
+
if (req.ws !== conn) {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
req.callbacks.error?.(TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
|
|
146
|
+
if (req.type !== 'subscription') {
|
|
147
|
+
delete pendingRequests[key];
|
|
148
|
+
req.callbacks.complete?.();
|
|
149
|
+
} else if (state !== 'closed') {
|
|
150
|
+
// request restart of sub with next connection
|
|
151
|
+
resumeSubscriptionOnReconnect(req);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
return conn;
|
|
156
|
+
}
|
|
157
|
+
function request(op, callbacks) {
|
|
158
|
+
const { type , input , path , id } = op;
|
|
159
|
+
const envelope = {
|
|
160
|
+
id,
|
|
161
|
+
method: type,
|
|
162
|
+
params: {
|
|
163
|
+
input,
|
|
164
|
+
path
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
pendingRequests[id] = {
|
|
168
|
+
ws: activeConnection,
|
|
169
|
+
type,
|
|
170
|
+
callbacks,
|
|
171
|
+
op
|
|
172
|
+
};
|
|
173
|
+
// enqueue message
|
|
174
|
+
outgoing.push(envelope);
|
|
263
175
|
dispatch();
|
|
264
|
-
|
|
176
|
+
return ()=>{
|
|
177
|
+
const callbacks = pendingRequests[id]?.callbacks;
|
|
178
|
+
delete pendingRequests[id];
|
|
179
|
+
outgoing = outgoing.filter((msg)=>msg.id !== id);
|
|
180
|
+
callbacks?.complete?.();
|
|
181
|
+
if (op.type === 'subscription') {
|
|
182
|
+
outgoing.push({
|
|
183
|
+
id,
|
|
184
|
+
method: 'subscription.stop'
|
|
185
|
+
});
|
|
186
|
+
dispatch();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
close: ()=>{
|
|
192
|
+
state = 'closed';
|
|
193
|
+
onClose?.();
|
|
194
|
+
closeIfNoPending(activeConnection);
|
|
195
|
+
clearTimeout(connectTimer);
|
|
196
|
+
connectTimer = null;
|
|
197
|
+
},
|
|
198
|
+
request,
|
|
199
|
+
getConnection () {
|
|
200
|
+
return activeConnection;
|
|
201
|
+
}
|
|
265
202
|
};
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
203
|
+
}
|
|
204
|
+
class TRPCWebSocketClosedError extends Error {
|
|
205
|
+
constructor(message){
|
|
206
|
+
super(message);
|
|
207
|
+
this.name = 'TRPCWebSocketClosedError';
|
|
208
|
+
Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
class TRPCSubscriptionEndedError extends Error {
|
|
212
|
+
constructor(message){
|
|
213
|
+
super(message);
|
|
214
|
+
this.name = 'TRPCSubscriptionEndedError';
|
|
215
|
+
Object.setPrototypeOf(this, TRPCSubscriptionEndedError.prototype);
|
|
276
216
|
}
|
|
277
|
-
};
|
|
278
217
|
}
|
|
279
|
-
|
|
280
|
-
var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
281
|
-
_inherits(TRPCWebSocketClosedError, _Error);
|
|
282
|
-
|
|
283
|
-
var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
|
|
284
|
-
|
|
285
|
-
function TRPCWebSocketClosedError(message) {
|
|
286
|
-
var _this;
|
|
287
|
-
|
|
288
|
-
_classCallCheck(this, TRPCWebSocketClosedError);
|
|
289
|
-
|
|
290
|
-
_this = _super.call(this, message);
|
|
291
|
-
_this.name = 'TRPCWebSocketClosedError';
|
|
292
|
-
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
|
|
293
|
-
return _this;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
return _createClass(TRPCWebSocketClosedError);
|
|
297
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
298
|
-
|
|
299
|
-
var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
|
|
300
|
-
_inherits(TRPCSubscriptionEndedError, _Error2);
|
|
301
|
-
|
|
302
|
-
var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
|
|
303
|
-
|
|
304
|
-
function TRPCSubscriptionEndedError(message) {
|
|
305
|
-
var _this2;
|
|
306
|
-
|
|
307
|
-
_classCallCheck(this, TRPCSubscriptionEndedError);
|
|
308
|
-
|
|
309
|
-
_this2 = _super2.call(this, message);
|
|
310
|
-
_this2.name = 'TRPCSubscriptionEndedError';
|
|
311
|
-
Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
|
|
312
|
-
return _this2;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return _createClass(TRPCSubscriptionEndedError);
|
|
316
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
317
|
-
|
|
318
218
|
function wsLink(opts) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
219
|
+
return (runtime)=>{
|
|
220
|
+
const { client } = opts;
|
|
221
|
+
return ({ op })=>{
|
|
222
|
+
return observable((observer)=>{
|
|
223
|
+
const { type , path , id , context } = op;
|
|
224
|
+
const input = runtime.transformer.serialize(op.input);
|
|
225
|
+
let isDone = false;
|
|
226
|
+
const unsub = client.request({
|
|
227
|
+
type,
|
|
228
|
+
path,
|
|
229
|
+
input,
|
|
230
|
+
id,
|
|
231
|
+
context
|
|
232
|
+
}, {
|
|
233
|
+
error (err) {
|
|
234
|
+
isDone = true;
|
|
235
|
+
observer.error(err);
|
|
236
|
+
unsub();
|
|
237
|
+
},
|
|
238
|
+
complete () {
|
|
239
|
+
if (!isDone) {
|
|
240
|
+
isDone = true;
|
|
241
|
+
observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
|
|
242
|
+
} else {
|
|
243
|
+
observer.complete();
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
next (message) {
|
|
247
|
+
const transformed = transformResult(message, runtime);
|
|
248
|
+
if (!transformed.ok) {
|
|
249
|
+
observer.error(TRPCClientError.from(transformed.error));
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
observer.next({
|
|
253
|
+
result: transformed.result
|
|
254
|
+
});
|
|
255
|
+
if (op.type !== 'subscription') {
|
|
256
|
+
// if it isn't a subscription we don't care about next response
|
|
257
|
+
isDone = true;
|
|
258
|
+
unsub();
|
|
259
|
+
observer.complete();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
return ()=>{
|
|
264
|
+
isDone = true;
|
|
265
|
+
unsub();
|
|
266
|
+
};
|
|
267
|
+
});
|
|
364
268
|
};
|
|
365
|
-
});
|
|
366
269
|
};
|
|
367
|
-
};
|
|
368
270
|
}
|
|
369
271
|
|
|
370
272
|
export { createWSClient, wsLink };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var observable = require('@trpc/server/observable');
|
|
4
|
+
|
|
5
|
+
/** @internal */ function createChain(opts) {
|
|
6
|
+
return observable.observable((observer)=>{
|
|
7
|
+
function execute(index = 0, op = opts.op) {
|
|
8
|
+
const next = opts.links[index];
|
|
9
|
+
if (!next) {
|
|
10
|
+
throw new Error('No more links to execute - did you forget to add an ending link?');
|
|
11
|
+
}
|
|
12
|
+
const subscription = next({
|
|
13
|
+
op,
|
|
14
|
+
next (nextOp) {
|
|
15
|
+
const nextObserver = execute(index + 1, nextOp);
|
|
16
|
+
return nextObserver;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return subscription;
|
|
20
|
+
}
|
|
21
|
+
const obs$ = execute();
|
|
22
|
+
return obs$.subscribe(observer);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function asArray(value) {
|
|
27
|
+
return Array.isArray(value) ? value : [
|
|
28
|
+
value
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
function splitLink(opts) {
|
|
32
|
+
return (runtime)=>{
|
|
33
|
+
const yes = asArray(opts.true).map((link)=>link(runtime));
|
|
34
|
+
const no = asArray(opts.false).map((link)=>link(runtime));
|
|
35
|
+
return (props)=>{
|
|
36
|
+
return observable.observable((observer)=>{
|
|
37
|
+
const links = opts.condition(props.op) ? yes : no;
|
|
38
|
+
return createChain({
|
|
39
|
+
op: props.op,
|
|
40
|
+
links
|
|
41
|
+
}).subscribe(observer);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exports.createChain = createChain;
|
|
48
|
+
exports.splitLink = splitLink;
|