@trpc/client 11.0.0-rc.553 → 11.0.0-rc.560
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle-analysis.json +33 -33
- package/dist/links/httpSubscriptionLink.d.ts +15 -0
- package/dist/links/httpSubscriptionLink.d.ts.map +1 -1
- package/dist/links/httpSubscriptionLink.js +93 -7
- package/dist/links/httpSubscriptionLink.mjs +93 -7
- package/package.json +4 -4
- package/src/links/httpSubscriptionLink.ts +154 -8
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
|
-
"bundleSize":
|
|
3
|
-
"bundleOrigSize":
|
|
4
|
-
"bundleReduction": 24.
|
|
2
|
+
"bundleSize": 57426,
|
|
3
|
+
"bundleOrigSize": 75759,
|
|
4
|
+
"bundleReduction": 24.2,
|
|
5
5
|
"modules": [
|
|
6
6
|
{
|
|
7
7
|
"id": "/src/links/wsLink.ts",
|
|
@@ -13,9 +13,21 @@
|
|
|
13
13
|
],
|
|
14
14
|
"removedExports": [],
|
|
15
15
|
"dependents": [],
|
|
16
|
-
"percent":
|
|
16
|
+
"percent": 26.6,
|
|
17
17
|
"reduction": 10.13
|
|
18
18
|
},
|
|
19
|
+
{
|
|
20
|
+
"id": "/src/links/httpSubscriptionLink.ts",
|
|
21
|
+
"size": 7004,
|
|
22
|
+
"origSize": 8051,
|
|
23
|
+
"renderedExports": [
|
|
24
|
+
"unstable_httpSubscriptionLink"
|
|
25
|
+
],
|
|
26
|
+
"removedExports": [],
|
|
27
|
+
"dependents": [],
|
|
28
|
+
"percent": 12.2,
|
|
29
|
+
"reduction": 13
|
|
30
|
+
},
|
|
19
31
|
{
|
|
20
32
|
"id": "/src/links/httpBatchStreamLink.ts",
|
|
21
33
|
"size": 5861,
|
|
@@ -25,7 +37,7 @@
|
|
|
25
37
|
],
|
|
26
38
|
"removedExports": [],
|
|
27
39
|
"dependents": [],
|
|
28
|
-
"percent": 10.
|
|
40
|
+
"percent": 10.21,
|
|
29
41
|
"reduction": 3.51
|
|
30
42
|
},
|
|
31
43
|
{
|
|
@@ -37,7 +49,7 @@
|
|
|
37
49
|
],
|
|
38
50
|
"removedExports": [],
|
|
39
51
|
"dependents": [],
|
|
40
|
-
"percent":
|
|
52
|
+
"percent": 9.5,
|
|
41
53
|
"reduction": 18.48
|
|
42
54
|
},
|
|
43
55
|
{
|
|
@@ -61,7 +73,7 @@
|
|
|
61
73
|
"/src/links/httpBatchStreamLink.ts",
|
|
62
74
|
"/src/links/httpSubscriptionLink.ts"
|
|
63
75
|
],
|
|
64
|
-
"percent":
|
|
76
|
+
"percent": 7.78,
|
|
65
77
|
"reduction": 32.81
|
|
66
78
|
},
|
|
67
79
|
{
|
|
@@ -76,7 +88,7 @@
|
|
|
76
88
|
"/src/links/httpBatchLink.ts",
|
|
77
89
|
"/src/links/httpBatchStreamLink.ts"
|
|
78
90
|
],
|
|
79
|
-
"percent": 7.
|
|
91
|
+
"percent": 7.11,
|
|
80
92
|
"reduction": 5.64
|
|
81
93
|
},
|
|
82
94
|
{
|
|
@@ -88,21 +100,9 @@
|
|
|
88
100
|
],
|
|
89
101
|
"removedExports": [],
|
|
90
102
|
"dependents": [],
|
|
91
|
-
"percent":
|
|
103
|
+
"percent": 6.83,
|
|
92
104
|
"reduction": 4.69
|
|
93
105
|
},
|
|
94
|
-
{
|
|
95
|
-
"id": "/src/links/httpSubscriptionLink.ts",
|
|
96
|
-
"size": 3805,
|
|
97
|
-
"origSize": 4221,
|
|
98
|
-
"renderedExports": [
|
|
99
|
-
"unstable_httpSubscriptionLink"
|
|
100
|
-
],
|
|
101
|
-
"removedExports": [],
|
|
102
|
-
"dependents": [],
|
|
103
|
-
"percent": 7.02,
|
|
104
|
-
"reduction": 9.86
|
|
105
|
-
},
|
|
106
106
|
{
|
|
107
107
|
"id": "/src/links/httpLink.ts",
|
|
108
108
|
"size": 3183,
|
|
@@ -112,7 +112,7 @@
|
|
|
112
112
|
],
|
|
113
113
|
"removedExports": [],
|
|
114
114
|
"dependents": [],
|
|
115
|
-
"percent": 5.
|
|
115
|
+
"percent": 5.54,
|
|
116
116
|
"reduction": 14.14
|
|
117
117
|
},
|
|
118
118
|
{
|
|
@@ -127,7 +127,7 @@
|
|
|
127
127
|
"/src/createTRPCUntypedClient.ts",
|
|
128
128
|
"/src/createTRPCClient.ts"
|
|
129
129
|
],
|
|
130
|
-
"percent": 3.
|
|
130
|
+
"percent": 3.76,
|
|
131
131
|
"reduction": 48.86
|
|
132
132
|
},
|
|
133
133
|
{
|
|
@@ -141,13 +141,13 @@
|
|
|
141
141
|
"dependents": [
|
|
142
142
|
"/src/index.ts",
|
|
143
143
|
"/src/links/httpBatchLink.ts",
|
|
144
|
-
"/src/links/wsLink.ts",
|
|
145
144
|
"/src/links/httpLink.ts",
|
|
145
|
+
"/src/links/wsLink.ts",
|
|
146
146
|
"/src/links/httpBatchStreamLink.ts",
|
|
147
147
|
"/src/links/httpSubscriptionLink.ts",
|
|
148
148
|
"/src/internals/TRPCUntypedClient.ts"
|
|
149
149
|
],
|
|
150
|
-
"percent": 3.
|
|
150
|
+
"percent": 3.38,
|
|
151
151
|
"reduction": 45.43
|
|
152
152
|
},
|
|
153
153
|
{
|
|
@@ -164,7 +164,7 @@
|
|
|
164
164
|
"dependents": [
|
|
165
165
|
"/src/index.ts"
|
|
166
166
|
],
|
|
167
|
-
"percent": 2.
|
|
167
|
+
"percent": 2.07,
|
|
168
168
|
"reduction": 73.19
|
|
169
169
|
},
|
|
170
170
|
{
|
|
@@ -179,7 +179,7 @@
|
|
|
179
179
|
"/src/links/splitLink.ts",
|
|
180
180
|
"/src/internals/TRPCUntypedClient.ts"
|
|
181
181
|
],
|
|
182
|
-
"percent": 1.
|
|
182
|
+
"percent": 1.2,
|
|
183
183
|
"reduction": 32.75
|
|
184
184
|
},
|
|
185
185
|
{
|
|
@@ -191,7 +191,7 @@
|
|
|
191
191
|
],
|
|
192
192
|
"removedExports": [],
|
|
193
193
|
"dependents": [],
|
|
194
|
-
"percent": 1.
|
|
194
|
+
"percent": 1.06,
|
|
195
195
|
"reduction": 44.95
|
|
196
196
|
},
|
|
197
197
|
{
|
|
@@ -205,7 +205,7 @@
|
|
|
205
205
|
"dependents": [
|
|
206
206
|
"/src/unstable-internals.ts"
|
|
207
207
|
],
|
|
208
|
-
"percent":
|
|
208
|
+
"percent": 0.98,
|
|
209
209
|
"reduction": 66.71
|
|
210
210
|
},
|
|
211
211
|
{
|
|
@@ -220,7 +220,7 @@
|
|
|
220
220
|
"/src/index.ts",
|
|
221
221
|
"/src/links/internals/httpUtils.ts"
|
|
222
222
|
],
|
|
223
|
-
"percent": 0.
|
|
223
|
+
"percent": 0.75,
|
|
224
224
|
"reduction": 33.54
|
|
225
225
|
},
|
|
226
226
|
{
|
|
@@ -234,7 +234,7 @@
|
|
|
234
234
|
],
|
|
235
235
|
"removedExports": [],
|
|
236
236
|
"dependents": [],
|
|
237
|
-
"percent": 0.
|
|
237
|
+
"percent": 0.57,
|
|
238
238
|
"reduction": 15.17
|
|
239
239
|
},
|
|
240
240
|
{
|
|
@@ -249,7 +249,7 @@
|
|
|
249
249
|
"/src/links/wsLink.ts",
|
|
250
250
|
"/src/links/httpSubscriptionLink.ts"
|
|
251
251
|
],
|
|
252
|
-
"percent": 0.
|
|
252
|
+
"percent": 0.28,
|
|
253
253
|
"reduction": 81.71
|
|
254
254
|
},
|
|
255
255
|
{
|
|
@@ -263,7 +263,7 @@
|
|
|
263
263
|
"dependents": [
|
|
264
264
|
"/src/index.ts"
|
|
265
265
|
],
|
|
266
|
-
"percent": 0.
|
|
266
|
+
"percent": 0.17,
|
|
267
267
|
"reduction": 82.58
|
|
268
268
|
},
|
|
269
269
|
{
|
|
@@ -3,11 +3,26 @@ import { type TransformerOptions } from '../unstable-internals';
|
|
|
3
3
|
import type { CallbackOrValue } from './internals/urlWithConnectionParams';
|
|
4
4
|
import { type UrlOptionsWithConnectionParams } from './internals/urlWithConnectionParams';
|
|
5
5
|
import type { TRPCLink } from './types';
|
|
6
|
+
type RecreateOnErrorOpt = {
|
|
7
|
+
type: 'raw';
|
|
8
|
+
event: Event;
|
|
9
|
+
} | {
|
|
10
|
+
type: 'http-error';
|
|
11
|
+
status: number;
|
|
12
|
+
event: Event;
|
|
13
|
+
};
|
|
6
14
|
type HTTPSubscriptionLinkOptions<TRoot extends AnyClientTypes> = {
|
|
7
15
|
/**
|
|
8
16
|
* EventSource options or a callback that returns them
|
|
9
17
|
*/
|
|
10
18
|
eventSourceOptions?: CallbackOrValue<EventSourceInit>;
|
|
19
|
+
/**
|
|
20
|
+
* For a given error, should we reinitialize the underlying EventSource?
|
|
21
|
+
*
|
|
22
|
+
* This is useful where a long running subscription might be interrupted by a recoverable network error,
|
|
23
|
+
* but the existing authorization in a header or URI has expired in the mean-time
|
|
24
|
+
*/
|
|
25
|
+
shouldRecreateOnError?: (opt: RecreateOnErrorOpt) => boolean | Promise<boolean>;
|
|
11
26
|
} & TransformerOptions<TRoot> & UrlOptionsWithConnectionParams;
|
|
12
27
|
/**
|
|
13
28
|
* @see https://trpc.io/docs/client/links/httpSubscriptionLink
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"httpSubscriptionLink.d.ts","sourceRoot":"","sources":["../../src/links/httpSubscriptionLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,0CAA0C,CAAC;AAMlD,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAiBxC,KAAK,2BAA2B,CAAC,KAAK,SAAS,cAAc,IAAI;IAC/D;;OAEG;IACH,kBAAkB,CAAC,EAAE,eAAe,CAAC,eAAe,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"httpSubscriptionLink.d.ts","sourceRoot":"","sources":["../../src/links/httpSubscriptionLink.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,0CAA0C,CAAC;AAMlD,OAAO,EAAkB,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAEhF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAC3E,OAAO,EAEL,KAAK,8BAA8B,EACpC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAiBxC,KAAK,kBAAkB,GACnB;IACE,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE,KAAK,CAAC;CACd,GACD;IACE,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEN,KAAK,2BAA2B,CAAC,KAAK,SAAS,cAAc,IAAI;IAC/D;;OAEG;IACH,kBAAkB,CAAC,EAAE,eAAe,CAAC,eAAe,CAAC,CAAC;IACtD;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,CACtB,GAAG,EAAE,kBAAkB,KACpB,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACjC,GAAG,kBAAkB,CAAC,KAAK,CAAC,GAC3B,8BAA8B,CAAC;AAEjC;;GAEG;AACH,wBAAgB,6BAA6B,CAC3C,WAAW,SAAS,qBAAqB,EAEzC,IAAI,EAAE,2BAA2B,CAAC,gBAAgB,CAAC,WAAW,CAAC,CAAC,GAC/D,QAAQ,CAAC,WAAW,CAAC,CA2HvB"}
|
|
@@ -7,6 +7,7 @@ var transformer = require('../internals/transformer.js');
|
|
|
7
7
|
var httpUtils = require('./internals/httpUtils.js');
|
|
8
8
|
var urlWithConnectionParams$1 = require('./internals/urlWithConnectionParams.js');
|
|
9
9
|
|
|
10
|
+
var _listeners, _type, _listeners1, _type1;
|
|
10
11
|
async function urlWithConnectionParams(opts) {
|
|
11
12
|
let url = await urlWithConnectionParams$1.resultOf(opts.url);
|
|
12
13
|
if (opts.connectionParams) {
|
|
@@ -43,24 +44,43 @@ async function urlWithConnectionParams(opts) {
|
|
|
43
44
|
// already unsubscribed - rare race condition
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
|
-
eventSource = new
|
|
47
|
+
eventSource = new EventSourceWrapper(url, eventSourceOptions);
|
|
47
48
|
const onStarted = ()=>{
|
|
48
49
|
observer.next({
|
|
49
50
|
result: {
|
|
50
51
|
type: 'started'
|
|
51
52
|
},
|
|
52
53
|
context: {
|
|
53
|
-
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
55
|
+
eventSource: eventSource.getEventSource()
|
|
54
56
|
}
|
|
55
57
|
});
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
57
|
-
eventSource.removeEventListener('open', onStarted);
|
|
58
58
|
};
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
eventSource.addEventListener('open', onStarted, {
|
|
60
|
+
once: true
|
|
61
|
+
});
|
|
61
62
|
const iterable = unstableCoreDoNotImport.sseStreamConsumer({
|
|
62
63
|
from: eventSource,
|
|
63
|
-
deserialize: transformer$1.output.deserialize
|
|
64
|
+
deserialize: transformer$1.output.deserialize,
|
|
65
|
+
tryHandleError: async (ev)=>{
|
|
66
|
+
if (typeof opts.shouldRecreateOnError !== 'function' || !eventSource) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const recreateOnErrorOpts = createRecreateOnErrorOpts(ev);
|
|
70
|
+
const shouldRestart = await opts.shouldRecreateOnError(recreateOnErrorOpts);
|
|
71
|
+
if (!shouldRestart) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
eventSource.restart(httpUtils.getUrl({
|
|
75
|
+
transformer: transformer$1,
|
|
76
|
+
url: await urlWithConnectionParams(opts),
|
|
77
|
+
input,
|
|
78
|
+
path,
|
|
79
|
+
type,
|
|
80
|
+
signal: null
|
|
81
|
+
}), await urlWithConnectionParams$1.resultOf(opts.eventSourceOptions));
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
64
84
|
});
|
|
65
85
|
for await (const chunk of iterable){
|
|
66
86
|
if (!chunk.ok) {
|
|
@@ -93,5 +113,71 @@ async function urlWithConnectionParams(opts) {
|
|
|
93
113
|
};
|
|
94
114
|
};
|
|
95
115
|
}
|
|
116
|
+
function createRecreateOnErrorOpts(ev) {
|
|
117
|
+
if ('status' in ev && typeof ev.status === 'number') {
|
|
118
|
+
return {
|
|
119
|
+
type: 'http-error',
|
|
120
|
+
status: ev.status,
|
|
121
|
+
event: ev
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
type: 'raw',
|
|
126
|
+
event: ev
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* We wrap EventSource so that is can be reinitialized with new options
|
|
131
|
+
*/ class EventSourceWrapper {
|
|
132
|
+
*getAllEventListeners() {
|
|
133
|
+
for(const _type in this.listeners){
|
|
134
|
+
const type = _type;
|
|
135
|
+
for (const listener of this.listeners[type] ?? []){
|
|
136
|
+
yield listener;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
restart(url, options) {
|
|
141
|
+
for (const [type, callback, options1] of this.getAllEventListeners()){
|
|
142
|
+
this.es.removeEventListener(type, callback, options1);
|
|
143
|
+
}
|
|
144
|
+
this.es.close();
|
|
145
|
+
this.es = new EventSource(url, options);
|
|
146
|
+
for (const [type1, callback1, options2] of this.getAllEventListeners()){
|
|
147
|
+
this.es.addEventListener(type1, callback1, options2);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
close() {
|
|
151
|
+
this.listeners = {};
|
|
152
|
+
this.es.close();
|
|
153
|
+
}
|
|
154
|
+
getEventSource() {
|
|
155
|
+
return this.es;
|
|
156
|
+
}
|
|
157
|
+
get readyState() {
|
|
158
|
+
return this.es.readyState;
|
|
159
|
+
}
|
|
160
|
+
addEventListener(type, listener, options) {
|
|
161
|
+
(_listeners = this.listeners)[_type = type] ?? (_listeners[_type] = []);
|
|
162
|
+
this.listeners[type].push([
|
|
163
|
+
type,
|
|
164
|
+
listener,
|
|
165
|
+
options
|
|
166
|
+
]);
|
|
167
|
+
this.es.addEventListener(type, listener, options);
|
|
168
|
+
}
|
|
169
|
+
removeEventListener(type, listener, options) {
|
|
170
|
+
(_listeners1 = this.listeners)[_type1 = type] ?? (_listeners1[_type1] = []);
|
|
171
|
+
const indexToRemove = this.listeners[type]?.findIndex(([_type, thisListener])=>thisListener === listener);
|
|
172
|
+
if (typeof indexToRemove === 'number' && indexToRemove >= 0) {
|
|
173
|
+
this.listeners[type].splice(indexToRemove, 1);
|
|
174
|
+
}
|
|
175
|
+
this.es.removeEventListener(type, listener, options);
|
|
176
|
+
}
|
|
177
|
+
constructor(url, options){
|
|
178
|
+
this.listeners = {};
|
|
179
|
+
this.es = new EventSource(url, options);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
96
182
|
|
|
97
183
|
exports.unstable_httpSubscriptionLink = unstable_httpSubscriptionLink;
|
|
@@ -5,6 +5,7 @@ import { getTransformer } from '../internals/transformer.mjs';
|
|
|
5
5
|
import { getUrl } from './internals/httpUtils.mjs';
|
|
6
6
|
import { resultOf } from './internals/urlWithConnectionParams.mjs';
|
|
7
7
|
|
|
8
|
+
var _listeners, _type, _listeners1, _type1;
|
|
8
9
|
async function urlWithConnectionParams(opts) {
|
|
9
10
|
let url = await resultOf(opts.url);
|
|
10
11
|
if (opts.connectionParams) {
|
|
@@ -41,24 +42,43 @@ async function urlWithConnectionParams(opts) {
|
|
|
41
42
|
// already unsubscribed - rare race condition
|
|
42
43
|
return;
|
|
43
44
|
}
|
|
44
|
-
eventSource = new
|
|
45
|
+
eventSource = new EventSourceWrapper(url, eventSourceOptions);
|
|
45
46
|
const onStarted = ()=>{
|
|
46
47
|
observer.next({
|
|
47
48
|
result: {
|
|
48
49
|
type: 'started'
|
|
49
50
|
},
|
|
50
51
|
context: {
|
|
51
|
-
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
53
|
+
eventSource: eventSource.getEventSource()
|
|
52
54
|
}
|
|
53
55
|
});
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
55
|
-
eventSource.removeEventListener('open', onStarted);
|
|
56
56
|
};
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
eventSource.addEventListener('open', onStarted, {
|
|
58
|
+
once: true
|
|
59
|
+
});
|
|
59
60
|
const iterable = sseStreamConsumer({
|
|
60
61
|
from: eventSource,
|
|
61
|
-
deserialize: transformer.output.deserialize
|
|
62
|
+
deserialize: transformer.output.deserialize,
|
|
63
|
+
tryHandleError: async (ev)=>{
|
|
64
|
+
if (typeof opts.shouldRecreateOnError !== 'function' || !eventSource) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const recreateOnErrorOpts = createRecreateOnErrorOpts(ev);
|
|
68
|
+
const shouldRestart = await opts.shouldRecreateOnError(recreateOnErrorOpts);
|
|
69
|
+
if (!shouldRestart) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
eventSource.restart(getUrl({
|
|
73
|
+
transformer,
|
|
74
|
+
url: await urlWithConnectionParams(opts),
|
|
75
|
+
input,
|
|
76
|
+
path,
|
|
77
|
+
type,
|
|
78
|
+
signal: null
|
|
79
|
+
}), await resultOf(opts.eventSourceOptions));
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
62
82
|
});
|
|
63
83
|
for await (const chunk of iterable){
|
|
64
84
|
if (!chunk.ok) {
|
|
@@ -91,5 +111,71 @@ async function urlWithConnectionParams(opts) {
|
|
|
91
111
|
};
|
|
92
112
|
};
|
|
93
113
|
}
|
|
114
|
+
function createRecreateOnErrorOpts(ev) {
|
|
115
|
+
if ('status' in ev && typeof ev.status === 'number') {
|
|
116
|
+
return {
|
|
117
|
+
type: 'http-error',
|
|
118
|
+
status: ev.status,
|
|
119
|
+
event: ev
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
type: 'raw',
|
|
124
|
+
event: ev
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* We wrap EventSource so that is can be reinitialized with new options
|
|
129
|
+
*/ class EventSourceWrapper {
|
|
130
|
+
*getAllEventListeners() {
|
|
131
|
+
for(const _type in this.listeners){
|
|
132
|
+
const type = _type;
|
|
133
|
+
for (const listener of this.listeners[type] ?? []){
|
|
134
|
+
yield listener;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
restart(url, options) {
|
|
139
|
+
for (const [type, callback, options1] of this.getAllEventListeners()){
|
|
140
|
+
this.es.removeEventListener(type, callback, options1);
|
|
141
|
+
}
|
|
142
|
+
this.es.close();
|
|
143
|
+
this.es = new EventSource(url, options);
|
|
144
|
+
for (const [type1, callback1, options2] of this.getAllEventListeners()){
|
|
145
|
+
this.es.addEventListener(type1, callback1, options2);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
close() {
|
|
149
|
+
this.listeners = {};
|
|
150
|
+
this.es.close();
|
|
151
|
+
}
|
|
152
|
+
getEventSource() {
|
|
153
|
+
return this.es;
|
|
154
|
+
}
|
|
155
|
+
get readyState() {
|
|
156
|
+
return this.es.readyState;
|
|
157
|
+
}
|
|
158
|
+
addEventListener(type, listener, options) {
|
|
159
|
+
(_listeners = this.listeners)[_type = type] ?? (_listeners[_type] = []);
|
|
160
|
+
this.listeners[type].push([
|
|
161
|
+
type,
|
|
162
|
+
listener,
|
|
163
|
+
options
|
|
164
|
+
]);
|
|
165
|
+
this.es.addEventListener(type, listener, options);
|
|
166
|
+
}
|
|
167
|
+
removeEventListener(type, listener, options) {
|
|
168
|
+
(_listeners1 = this.listeners)[_type1 = type] ?? (_listeners1[_type1] = []);
|
|
169
|
+
const indexToRemove = this.listeners[type]?.findIndex(([_type, thisListener])=>thisListener === listener);
|
|
170
|
+
if (typeof indexToRemove === 'number' && indexToRemove >= 0) {
|
|
171
|
+
this.listeners[type].splice(indexToRemove, 1);
|
|
172
|
+
}
|
|
173
|
+
this.es.removeEventListener(type, listener, options);
|
|
174
|
+
}
|
|
175
|
+
constructor(url, options){
|
|
176
|
+
this.listeners = {};
|
|
177
|
+
this.es = new EventSource(url, options);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
94
180
|
|
|
95
181
|
export { unstable_httpSubscriptionLink };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/client",
|
|
3
|
-
"version": "11.0.0-rc.
|
|
3
|
+
"version": "11.0.0-rc.560+137b085f8",
|
|
4
4
|
"description": "The tRPC client library",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,10 +76,10 @@
|
|
|
76
76
|
"!**/*.test.*"
|
|
77
77
|
],
|
|
78
78
|
"peerDependencies": {
|
|
79
|
-
"@trpc/server": "11.0.0-rc.
|
|
79
|
+
"@trpc/server": "11.0.0-rc.560+137b085f8"
|
|
80
80
|
},
|
|
81
81
|
"devDependencies": {
|
|
82
|
-
"@trpc/server": "11.0.0-rc.
|
|
82
|
+
"@trpc/server": "11.0.0-rc.560+137b085f8",
|
|
83
83
|
"@types/isomorphic-fetch": "^0.0.39",
|
|
84
84
|
"@types/node": "^20.10.0",
|
|
85
85
|
"eslint": "^8.57.0",
|
|
@@ -96,5 +96,5 @@
|
|
|
96
96
|
"funding": [
|
|
97
97
|
"https://trpc.io/sponsor"
|
|
98
98
|
],
|
|
99
|
-
"gitHead": "
|
|
99
|
+
"gitHead": "137b085f86cb4ceb397b29c69ccbe38a93dbe79a"
|
|
100
100
|
}
|
|
@@ -33,11 +33,31 @@ async function urlWithConnectionParams(
|
|
|
33
33
|
return url;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
type RecreateOnErrorOpt =
|
|
37
|
+
| {
|
|
38
|
+
type: 'raw';
|
|
39
|
+
event: Event;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
type: 'http-error';
|
|
43
|
+
status: number;
|
|
44
|
+
event: Event;
|
|
45
|
+
};
|
|
46
|
+
|
|
36
47
|
type HTTPSubscriptionLinkOptions<TRoot extends AnyClientTypes> = {
|
|
37
48
|
/**
|
|
38
49
|
* EventSource options or a callback that returns them
|
|
39
50
|
*/
|
|
40
51
|
eventSourceOptions?: CallbackOrValue<EventSourceInit>;
|
|
52
|
+
/**
|
|
53
|
+
* For a given error, should we reinitialize the underlying EventSource?
|
|
54
|
+
*
|
|
55
|
+
* This is useful where a long running subscription might be interrupted by a recoverable network error,
|
|
56
|
+
* but the existing authorization in a header or URI has expired in the mean-time
|
|
57
|
+
*/
|
|
58
|
+
shouldRecreateOnError?: (
|
|
59
|
+
opt: RecreateOnErrorOpt,
|
|
60
|
+
) => boolean | Promise<boolean>;
|
|
41
61
|
} & TransformerOptions<TRoot> &
|
|
42
62
|
UrlOptionsWithConnectionParams;
|
|
43
63
|
|
|
@@ -60,7 +80,7 @@ export function unstable_httpSubscriptionLink<
|
|
|
60
80
|
throw new Error('httpSubscriptionLink only supports subscriptions');
|
|
61
81
|
}
|
|
62
82
|
|
|
63
|
-
let eventSource:
|
|
83
|
+
let eventSource: EventSourceWrapper | null = null;
|
|
64
84
|
let unsubscribed = false;
|
|
65
85
|
|
|
66
86
|
run(async () => {
|
|
@@ -79,22 +99,22 @@ export function unstable_httpSubscriptionLink<
|
|
|
79
99
|
// already unsubscribed - rare race condition
|
|
80
100
|
return;
|
|
81
101
|
}
|
|
82
|
-
|
|
102
|
+
|
|
103
|
+
eventSource = new EventSourceWrapper(url, eventSourceOptions);
|
|
104
|
+
|
|
83
105
|
const onStarted = () => {
|
|
84
106
|
observer.next({
|
|
85
107
|
result: {
|
|
86
108
|
type: 'started',
|
|
87
109
|
},
|
|
88
110
|
context: {
|
|
89
|
-
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
112
|
+
eventSource: eventSource!.getEventSource(),
|
|
90
113
|
},
|
|
91
114
|
});
|
|
92
|
-
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
94
|
-
eventSource!.removeEventListener('open', onStarted);
|
|
95
115
|
};
|
|
96
|
-
|
|
97
|
-
|
|
116
|
+
eventSource.addEventListener('open', onStarted, { once: true });
|
|
117
|
+
|
|
98
118
|
const iterable = sseStreamConsumer<
|
|
99
119
|
Partial<{
|
|
100
120
|
id?: string;
|
|
@@ -103,6 +123,38 @@ export function unstable_httpSubscriptionLink<
|
|
|
103
123
|
>({
|
|
104
124
|
from: eventSource,
|
|
105
125
|
deserialize: transformer.output.deserialize,
|
|
126
|
+
tryHandleError: async (ev) => {
|
|
127
|
+
if (
|
|
128
|
+
typeof opts.shouldRecreateOnError !== 'function' ||
|
|
129
|
+
!eventSource
|
|
130
|
+
) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const recreateOnErrorOpts = createRecreateOnErrorOpts(ev);
|
|
135
|
+
|
|
136
|
+
const shouldRestart = await opts.shouldRecreateOnError(
|
|
137
|
+
recreateOnErrorOpts,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
if (!shouldRestart) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
eventSource.restart(
|
|
145
|
+
getUrl({
|
|
146
|
+
transformer,
|
|
147
|
+
url: await urlWithConnectionParams(opts),
|
|
148
|
+
input,
|
|
149
|
+
path,
|
|
150
|
+
type,
|
|
151
|
+
signal: null,
|
|
152
|
+
}),
|
|
153
|
+
await resultOf(opts.eventSourceOptions),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return true;
|
|
157
|
+
},
|
|
106
158
|
});
|
|
107
159
|
|
|
108
160
|
for await (const chunk of iterable) {
|
|
@@ -140,3 +192,97 @@ export function unstable_httpSubscriptionLink<
|
|
|
140
192
|
};
|
|
141
193
|
};
|
|
142
194
|
}
|
|
195
|
+
|
|
196
|
+
function createRecreateOnErrorOpts(ev: Event): RecreateOnErrorOpt {
|
|
197
|
+
if ('status' in ev && typeof ev.status === 'number') {
|
|
198
|
+
return {
|
|
199
|
+
type: 'http-error',
|
|
200
|
+
status: ev.status,
|
|
201
|
+
event: ev,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
type: 'raw',
|
|
207
|
+
event: ev,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* We wrap EventSource so that is can be reinitialized with new options
|
|
213
|
+
*/
|
|
214
|
+
class EventSourceWrapper implements Partial<EventSource> {
|
|
215
|
+
private es: EventSource;
|
|
216
|
+
|
|
217
|
+
private listeners: Partial<
|
|
218
|
+
Record<
|
|
219
|
+
keyof EventSourceEventMap,
|
|
220
|
+
Parameters<EventSource['addEventListener']>[]
|
|
221
|
+
>
|
|
222
|
+
> = {};
|
|
223
|
+
private *getAllEventListeners() {
|
|
224
|
+
for (const _type in this.listeners) {
|
|
225
|
+
const type = _type as keyof typeof this.listeners;
|
|
226
|
+
for (const listener of this.listeners[type] ?? []) {
|
|
227
|
+
yield listener;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
constructor(url: string, options: EventSourceInit | undefined) {
|
|
233
|
+
this.es = new EventSource(url, options);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
restart(url: string, options: EventSourceInit | undefined) {
|
|
237
|
+
for (const [type, callback, options] of this.getAllEventListeners()) {
|
|
238
|
+
this.es.removeEventListener(type, callback, options);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
this.es.close();
|
|
242
|
+
this.es = new EventSource(url, options);
|
|
243
|
+
|
|
244
|
+
for (const [type, callback, options] of this.getAllEventListeners()) {
|
|
245
|
+
this.es.addEventListener(type, callback, options);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
close() {
|
|
250
|
+
this.listeners = {};
|
|
251
|
+
this.es.close();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
getEventSource() {
|
|
255
|
+
return this.es;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
get readyState() {
|
|
259
|
+
return this.es.readyState;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
addEventListener<TEvent extends keyof EventSourceEventMap>(
|
|
263
|
+
type: TEvent,
|
|
264
|
+
listener: (this: EventSource, ev: EventSourceEventMap[TEvent]) => any,
|
|
265
|
+
options?: boolean | AddEventListenerOptions,
|
|
266
|
+
) {
|
|
267
|
+
this.listeners[type] ??= [];
|
|
268
|
+
this.listeners[type].push([type, listener as any, options]);
|
|
269
|
+
|
|
270
|
+
this.es.addEventListener(type, listener, options);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
removeEventListener<TEvent extends keyof EventSourceEventMap>(
|
|
274
|
+
type: TEvent,
|
|
275
|
+
listener: (this: EventSource, ev: EventSourceEventMap[TEvent]) => any,
|
|
276
|
+
options?: boolean | EventListenerOptions,
|
|
277
|
+
) {
|
|
278
|
+
this.listeners[type] ??= [];
|
|
279
|
+
|
|
280
|
+
const indexToRemove = this.listeners[type]?.findIndex(
|
|
281
|
+
([_type, thisListener]) => thisListener === listener,
|
|
282
|
+
);
|
|
283
|
+
if (typeof indexToRemove === 'number' && indexToRemove >= 0) {
|
|
284
|
+
this.listeners[type].splice(indexToRemove, 1);
|
|
285
|
+
}
|
|
286
|
+
this.es.removeEventListener(type, listener, options);
|
|
287
|
+
}
|
|
288
|
+
}
|