@trpc/server 11.0.0-rc.630 → 11.0.0-rc.632
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/@trpc/server/http.d.ts +1 -1
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +1 -0
- package/dist/adapters/aws-lambda/index.mjs +1 -0
- package/dist/adapters/express.js +1 -0
- package/dist/adapters/express.mjs +1 -0
- package/dist/adapters/fastify/fastifyRequestHandler.js +2 -1
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +2 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +1 -0
- package/dist/adapters/fetch/fetchRequestHandler.mjs +1 -0
- package/dist/adapters/next-app-dir/nextAppDirCaller.js +1 -0
- package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +1 -0
- package/dist/adapters/next-app-dir/notFound.js +1 -0
- package/dist/adapters/next-app-dir/notFound.mjs +1 -0
- package/dist/adapters/next-app-dir/redirect.js +1 -0
- package/dist/adapters/next-app-dir/redirect.mjs +1 -0
- package/dist/adapters/next.js +1 -0
- package/dist/adapters/next.mjs +1 -0
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +7 -5
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +7 -5
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +9 -42
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +9 -42
- package/dist/adapters/node-http/writeResponse.d.ts +18 -0
- package/dist/adapters/node-http/writeResponse.d.ts.map +1 -0
- package/dist/adapters/node-http/writeResponse.js +85 -0
- package/dist/adapters/node-http/writeResponse.mjs +82 -0
- package/dist/adapters/standalone.js +1 -0
- package/dist/adapters/standalone.mjs +1 -0
- package/dist/adapters/ws.js +2 -1
- package/dist/adapters/ws.mjs +2 -1
- package/dist/bundle-analysis.json +146 -119
- package/dist/http.js +1 -2
- package/dist/http.mjs +1 -1
- package/dist/index.js +2 -1
- package/dist/index.mjs +2 -1
- package/dist/rpc.js +1 -0
- package/dist/rpc.mjs +1 -0
- package/dist/shared.js +1 -0
- package/dist/shared.mjs +1 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts +4 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.js +9 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.mjs +7 -0
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +6 -9
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +75 -124
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +76 -125
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/sse.js +25 -21
- package/dist/unstable-core-do-not-import/stream/sse.mjs +26 -22
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +10 -10
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +47 -34
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +47 -34
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts +0 -4
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.js +0 -11
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.mjs +1 -11
- package/dist/unstable-core-do-not-import/stream/utils/disposablePromiseTimer.d.ts +6 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposablePromiseTimer.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposablePromiseTimer.js +28 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposablePromiseTimer.mjs +25 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/withPing.js +17 -17
- package/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +17 -17
- package/dist/unstable-core-do-not-import/stream/utils/withRefCount.d.ts +17 -0
- package/dist/unstable-core-do-not-import/stream/utils/withRefCount.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/withRefCount.js +59 -0
- package/dist/unstable-core-do-not-import/stream/utils/withRefCount.mjs +57 -0
- package/dist/unstable-core-do-not-import.d.ts +2 -2
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +2 -2
- package/dist/unstable-core-do-not-import.mjs +1 -1
- package/package.json +3 -3
- package/src/@trpc/server/http.ts +0 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
- package/src/adapters/node-http/incomingMessageToRequest.ts +8 -4
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +8 -46
- package/src/adapters/node-http/writeResponse.ts +96 -0
- package/src/unstable-core-do-not-import/http/isAbortError.ts +7 -0
- package/src/unstable-core-do-not-import/stream/jsonl.ts +85 -154
- package/src/unstable-core-do-not-import/stream/sse.ts +24 -25
- package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +58 -37
- package/src/unstable-core-do-not-import/stream/utils/createReadableStream.ts +0 -13
- package/src/unstable-core-do-not-import/stream/utils/disposablePromiseTimer.ts +27 -0
- package/src/unstable-core-do-not-import/stream/utils/withPing.ts +31 -19
- package/src/unstable-core-do-not-import/stream/utils/withRefCount.ts +93 -0
- package/src/unstable-core-do-not-import.ts +2 -2
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +0 -32
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +0 -30
- package/dist/unstable-core-do-not-import/stream/utils/promiseTimer.d.ts +0 -8
- package/dist/unstable-core-do-not-import/stream/utils/promiseTimer.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/promiseTimer.js +0 -38
- package/dist/unstable-core-do-not-import/stream/utils/promiseTimer.mjs +0 -36
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
- package/src/unstable-core-do-not-import/stream/utils/promiseTimer.ts +0 -40
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { _ as _define_property } from '../../node_modules/.pnpm/@swc_helpers@0.5.13/node_modules/@swc/helpers/esm/_define_property.mjs';
|
|
2
|
-
import { Unpromise } from '../../vendor/unpromise/unpromise.mjs';
|
|
3
|
-
import { getTRPCErrorFromUnknown } from '../error/TRPCError.mjs';
|
|
4
2
|
import { isObject, isFunction, isAsyncIterable, run } from '../utils.mjs';
|
|
5
3
|
import { createDeferred } from './utils/createDeferred.mjs';
|
|
6
|
-
import { createReadableStream
|
|
4
|
+
import { createReadableStream } from './utils/createReadableStream.mjs';
|
|
5
|
+
import { withRefCount } from './utils/withRefCount.mjs';
|
|
7
6
|
|
|
8
7
|
// ---------- types
|
|
9
8
|
const CHUNK_VALUE_TYPE_PROMISE = 0;
|
|
@@ -26,41 +25,43 @@ function createBatchStreamProducer(opts) {
|
|
|
26
25
|
let counter = 0;
|
|
27
26
|
const placeholder = 0;
|
|
28
27
|
const stream = createReadableStream();
|
|
29
|
-
const pending = new Set()
|
|
30
|
-
|
|
31
|
-
if (pending.size === 0 && !stream.cancelled()) {
|
|
28
|
+
const pending = withRefCount(new Set(), ()=>{
|
|
29
|
+
if (!stream.cancelled()) {
|
|
32
30
|
stream.controller.close();
|
|
33
31
|
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
});
|
|
33
|
+
const maybeEnqueue = (chunk)=>{
|
|
34
|
+
if (!stream.cancelled()) {
|
|
35
|
+
stream.controller.enqueue(chunk);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
function encodePromise(promise, path) {
|
|
37
39
|
const error = checkMaxDepth(path);
|
|
38
40
|
if (error) {
|
|
39
|
-
promise
|
|
40
|
-
|
|
41
|
+
// Catch any errors from the original promise to ensure they're reported
|
|
42
|
+
promise.catch((cause)=>{
|
|
43
|
+
opts.onError?.({
|
|
44
|
+
error: cause,
|
|
45
|
+
path
|
|
46
|
+
});
|
|
41
47
|
});
|
|
48
|
+
// Replace the promise with a rejected one containing the max depth error
|
|
42
49
|
promise = Promise.reject(error);
|
|
43
50
|
}
|
|
44
51
|
const idx = counter++;
|
|
45
52
|
pending.add(idx);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
stream.cancelledPromise
|
|
49
|
-
]).then((it)=>{
|
|
50
|
-
if (isCancelledStreamResult(it)) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
stream.controller.enqueue([
|
|
53
|
+
promise.then((it)=>{
|
|
54
|
+
maybeEnqueue([
|
|
54
55
|
idx,
|
|
55
56
|
PROMISE_STATUS_FULFILLED,
|
|
56
|
-
|
|
57
|
+
encode(it, path)
|
|
57
58
|
]);
|
|
58
59
|
}).catch((cause)=>{
|
|
59
60
|
opts.onError?.({
|
|
60
61
|
error: cause,
|
|
61
62
|
path
|
|
62
63
|
});
|
|
63
|
-
|
|
64
|
+
maybeEnqueue([
|
|
64
65
|
idx,
|
|
65
66
|
PROMISE_STATUS_REJECTED,
|
|
66
67
|
opts.formatError?.({
|
|
@@ -70,73 +71,53 @@ function createBatchStreamProducer(opts) {
|
|
|
70
71
|
]);
|
|
71
72
|
}).finally(()=>{
|
|
72
73
|
pending.delete(idx);
|
|
73
|
-
maybeClose();
|
|
74
74
|
});
|
|
75
75
|
return idx;
|
|
76
76
|
}
|
|
77
|
-
function
|
|
78
|
-
const error = checkMaxDepth(path);
|
|
79
|
-
if (error) {
|
|
80
|
-
iterable = {
|
|
81
|
-
[Symbol.asyncIterator] () {
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|
|
77
|
+
function encodeAsyncIterable(iterable, path) {
|
|
86
78
|
const idx = counter++;
|
|
87
79
|
pending.add(idx);
|
|
88
80
|
run(async ()=>{
|
|
81
|
+
const error = checkMaxDepth(path);
|
|
82
|
+
if (error) {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
89
85
|
const iterator = iterable[Symbol.asyncIterator]();
|
|
90
86
|
while(true){
|
|
91
|
-
|
|
92
|
-
iterator.
|
|
93
|
-
|
|
94
|
-
]);
|
|
95
|
-
if (next instanceof Error) {
|
|
96
|
-
opts.onError?.({
|
|
97
|
-
error: next,
|
|
98
|
-
path
|
|
99
|
-
});
|
|
100
|
-
stream.controller.enqueue([
|
|
101
|
-
idx,
|
|
102
|
-
ASYNC_ITERABLE_STATUS_ERROR,
|
|
103
|
-
opts.formatError?.({
|
|
104
|
-
error: next,
|
|
105
|
-
path
|
|
106
|
-
})
|
|
107
|
-
]);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (isCancelledStreamResult(next)) {
|
|
111
|
-
await iterator.return?.();
|
|
112
|
-
break;
|
|
87
|
+
if (stream.cancelled()) {
|
|
88
|
+
const res = await iterator.return?.();
|
|
89
|
+
return res?.value;
|
|
113
90
|
}
|
|
91
|
+
const next = await iterator.next();
|
|
114
92
|
if (next.done) {
|
|
115
|
-
|
|
93
|
+
maybeEnqueue([
|
|
116
94
|
idx,
|
|
117
95
|
ASYNC_ITERABLE_STATUS_RETURN,
|
|
118
|
-
|
|
96
|
+
encode(next.value, path)
|
|
119
97
|
]);
|
|
120
98
|
break;
|
|
121
99
|
}
|
|
122
|
-
|
|
100
|
+
maybeEnqueue([
|
|
123
101
|
idx,
|
|
124
102
|
ASYNC_ITERABLE_STATUS_VALUE,
|
|
125
|
-
|
|
103
|
+
encode(next.value, path)
|
|
126
104
|
]);
|
|
127
105
|
}
|
|
128
|
-
pending.delete(idx);
|
|
129
|
-
maybeClose();
|
|
130
106
|
}).catch((cause)=>{
|
|
131
|
-
// this shouldn't happen, but node crashes if we don't catch it
|
|
132
107
|
opts.onError?.({
|
|
133
|
-
error:
|
|
134
|
-
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
135
|
-
{
|
|
136
|
-
cause
|
|
137
|
-
}),
|
|
108
|
+
error: cause,
|
|
138
109
|
path
|
|
139
110
|
});
|
|
111
|
+
maybeEnqueue([
|
|
112
|
+
idx,
|
|
113
|
+
ASYNC_ITERABLE_STATUS_ERROR,
|
|
114
|
+
opts.formatError?.({
|
|
115
|
+
error: cause,
|
|
116
|
+
path
|
|
117
|
+
})
|
|
118
|
+
]);
|
|
119
|
+
}).finally(()=>{
|
|
120
|
+
pending.delete(idx);
|
|
140
121
|
});
|
|
141
122
|
return idx;
|
|
142
123
|
}
|
|
@@ -146,11 +127,11 @@ function createBatchStreamProducer(opts) {
|
|
|
146
127
|
}
|
|
147
128
|
return null;
|
|
148
129
|
}
|
|
149
|
-
function
|
|
130
|
+
function encodeAsync(value, path) {
|
|
150
131
|
if (isPromise(value)) {
|
|
151
132
|
return [
|
|
152
133
|
CHUNK_VALUE_TYPE_PROMISE,
|
|
153
|
-
|
|
134
|
+
encodePromise(value, path)
|
|
154
135
|
];
|
|
155
136
|
}
|
|
156
137
|
if (isAsyncIterable(value)) {
|
|
@@ -159,12 +140,12 @@ function createBatchStreamProducer(opts) {
|
|
|
159
140
|
}
|
|
160
141
|
return [
|
|
161
142
|
CHUNK_VALUE_TYPE_ASYNC_ITERABLE,
|
|
162
|
-
|
|
143
|
+
encodeAsyncIterable(value, path)
|
|
163
144
|
];
|
|
164
145
|
}
|
|
165
146
|
return null;
|
|
166
147
|
}
|
|
167
|
-
function
|
|
148
|
+
function encode(value, path) {
|
|
168
149
|
if (value === undefined) {
|
|
169
150
|
return [
|
|
170
151
|
[]
|
|
@@ -177,7 +158,7 @@ function createBatchStreamProducer(opts) {
|
|
|
177
158
|
]
|
|
178
159
|
];
|
|
179
160
|
}
|
|
180
|
-
const reg =
|
|
161
|
+
const reg = encodeAsync(value, path);
|
|
181
162
|
if (reg) {
|
|
182
163
|
return [
|
|
183
164
|
[
|
|
@@ -192,7 +173,7 @@ function createBatchStreamProducer(opts) {
|
|
|
192
173
|
const newObj = {};
|
|
193
174
|
const asyncValues = [];
|
|
194
175
|
for (const [key, item] of Object.entries(value)){
|
|
195
|
-
const transformed =
|
|
176
|
+
const transformed = encodeAsync(item, [
|
|
196
177
|
...path,
|
|
197
178
|
key
|
|
198
179
|
]);
|
|
@@ -215,10 +196,11 @@ function createBatchStreamProducer(opts) {
|
|
|
215
196
|
}
|
|
216
197
|
const newHead = {};
|
|
217
198
|
for (const [key, item] of Object.entries(data)){
|
|
218
|
-
newHead[key] =
|
|
199
|
+
newHead[key] = encode(item, [
|
|
219
200
|
key
|
|
220
201
|
]);
|
|
221
202
|
}
|
|
203
|
+
pending.activate();
|
|
222
204
|
return [
|
|
223
205
|
newHead,
|
|
224
206
|
stream.readable
|
|
@@ -334,34 +316,19 @@ function createConsumerStream(from) {
|
|
|
334
316
|
}));
|
|
335
317
|
}
|
|
336
318
|
let headDeferred = createDeferred();
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
// nothing is listening to the stream anymore
|
|
342
|
-
opts.abortController?.abort();
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
function hydrateChunkDefinition(value) {
|
|
319
|
+
const controllers = withRefCount(new Map(), ()=>{
|
|
320
|
+
opts.abortController?.abort();
|
|
321
|
+
});
|
|
322
|
+
function decodeChunkDefinition(value) {
|
|
346
323
|
const [_path, type, chunkId] = value;
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
controller,
|
|
350
|
-
returned: false
|
|
351
|
-
};
|
|
352
|
-
controllers.set(chunkId, wrapper);
|
|
353
|
-
// resolve chunk deferred if it exists
|
|
354
|
-
const deferred = chunkDeferred.get(chunkId);
|
|
355
|
-
if (deferred) {
|
|
356
|
-
deferred.resolve(wrapper);
|
|
357
|
-
chunkDeferred.delete(chunkId);
|
|
358
|
-
}
|
|
324
|
+
const stream = createReadableStream();
|
|
325
|
+
controllers.set(chunkId, stream.controller);
|
|
359
326
|
switch(type){
|
|
360
327
|
case CHUNK_VALUE_TYPE_PROMISE:
|
|
361
328
|
{
|
|
362
329
|
return new Promise((resolve, reject)=>{
|
|
363
330
|
// listen for next value in the stream
|
|
364
|
-
const reader = readable.getReader();
|
|
331
|
+
const reader = stream.readable.getReader();
|
|
365
332
|
reader.read().then((it)=>{
|
|
366
333
|
if (it.done) {
|
|
367
334
|
reject(new Error('Promise chunk ended without value'));
|
|
@@ -375,7 +342,7 @@ function createConsumerStream(from) {
|
|
|
375
342
|
const [_chunkId, status, data] = value;
|
|
376
343
|
switch(status){
|
|
377
344
|
case PROMISE_STATUS_FULFILLED:
|
|
378
|
-
resolve(
|
|
345
|
+
resolve(decode(data));
|
|
379
346
|
break;
|
|
380
347
|
case PROMISE_STATUS_REJECTED:
|
|
381
348
|
reject(opts.formatError?.({
|
|
@@ -384,9 +351,7 @@ function createConsumerStream(from) {
|
|
|
384
351
|
break;
|
|
385
352
|
}
|
|
386
353
|
}).catch(reject).finally(()=>{
|
|
387
|
-
// reader.releaseLock();
|
|
388
354
|
controllers.delete(chunkId);
|
|
389
|
-
maybeAbort();
|
|
390
355
|
});
|
|
391
356
|
});
|
|
392
357
|
}
|
|
@@ -394,7 +359,7 @@ function createConsumerStream(from) {
|
|
|
394
359
|
{
|
|
395
360
|
return {
|
|
396
361
|
[Symbol.asyncIterator]: ()=>{
|
|
397
|
-
const reader = readable.getReader();
|
|
362
|
+
const reader = stream.readable.getReader();
|
|
398
363
|
const iterator = {
|
|
399
364
|
next: async ()=>{
|
|
400
365
|
const { done, value } = await reader.read();
|
|
@@ -403,7 +368,6 @@ function createConsumerStream(from) {
|
|
|
403
368
|
}
|
|
404
369
|
if (done) {
|
|
405
370
|
controllers.delete(chunkId);
|
|
406
|
-
maybeAbort();
|
|
407
371
|
return {
|
|
408
372
|
done: true,
|
|
409
373
|
value: undefined
|
|
@@ -414,26 +378,23 @@ function createConsumerStream(from) {
|
|
|
414
378
|
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
415
379
|
return {
|
|
416
380
|
done: false,
|
|
417
|
-
value:
|
|
381
|
+
value: decode(data)
|
|
418
382
|
};
|
|
419
383
|
case ASYNC_ITERABLE_STATUS_RETURN:
|
|
420
384
|
controllers.delete(chunkId);
|
|
421
|
-
maybeAbort();
|
|
422
385
|
return {
|
|
423
386
|
done: true,
|
|
424
|
-
value:
|
|
387
|
+
value: decode(data)
|
|
425
388
|
};
|
|
426
389
|
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
427
390
|
controllers.delete(chunkId);
|
|
428
|
-
maybeAbort();
|
|
429
391
|
throw opts.formatError?.({
|
|
430
392
|
error: data
|
|
431
393
|
}) ?? new AsyncError(data);
|
|
432
394
|
}
|
|
433
395
|
},
|
|
434
396
|
return: async ()=>{
|
|
435
|
-
|
|
436
|
-
maybeAbort();
|
|
397
|
+
controllers.delete(chunkId);
|
|
437
398
|
return {
|
|
438
399
|
done: true,
|
|
439
400
|
value: undefined
|
|
@@ -446,26 +407,22 @@ function createConsumerStream(from) {
|
|
|
446
407
|
}
|
|
447
408
|
}
|
|
448
409
|
}
|
|
449
|
-
function
|
|
410
|
+
function decode(value) {
|
|
450
411
|
const [[data], ...asyncProps] = value;
|
|
451
412
|
for (const value of asyncProps){
|
|
452
413
|
const [key] = value;
|
|
453
|
-
const
|
|
414
|
+
const decoded = decodeChunkDefinition(value);
|
|
454
415
|
if (key === null) {
|
|
455
|
-
return
|
|
416
|
+
return decoded;
|
|
456
417
|
}
|
|
457
|
-
data[key] =
|
|
418
|
+
data[key] = decoded;
|
|
458
419
|
}
|
|
459
420
|
return data;
|
|
460
421
|
}
|
|
461
422
|
const closeOrAbort = (reason)=>{
|
|
462
423
|
const error = new StreamInterruptedError(reason);
|
|
463
424
|
headDeferred?.reject(error);
|
|
464
|
-
for (const
|
|
465
|
-
deferred.reject(error);
|
|
466
|
-
}
|
|
467
|
-
chunkDeferred.clear();
|
|
468
|
-
for (const { controller } of controllers.values()){
|
|
425
|
+
for (const controller of controllers.values()){
|
|
469
426
|
controller.enqueue(error);
|
|
470
427
|
controller.close();
|
|
471
428
|
}
|
|
@@ -476,25 +433,19 @@ function createConsumerStream(from) {
|
|
|
476
433
|
if (headDeferred) {
|
|
477
434
|
const head = chunkOrHead;
|
|
478
435
|
for (const [key, value] of Object.entries(chunkOrHead)){
|
|
479
|
-
const parsed =
|
|
436
|
+
const parsed = decode(value);
|
|
480
437
|
head[key] = parsed;
|
|
481
438
|
}
|
|
482
439
|
headDeferred.resolve(head);
|
|
483
440
|
headDeferred = null;
|
|
441
|
+
controllers.activate();
|
|
484
442
|
return;
|
|
485
443
|
}
|
|
486
444
|
const chunk = chunkOrHead;
|
|
487
445
|
const [idx] = chunk;
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
if (!deferred) {
|
|
492
|
-
deferred = createDeferred();
|
|
493
|
-
chunkDeferred.set(idx, deferred);
|
|
494
|
-
}
|
|
495
|
-
wrapper = await deferred.promise;
|
|
496
|
-
}
|
|
497
|
-
wrapper.controller.enqueue(chunk);
|
|
446
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
447
|
+
const controller = controllers.get(idx);
|
|
448
|
+
controller.enqueue(chunk);
|
|
498
449
|
},
|
|
499
450
|
close: closeOrAbort,
|
|
500
451
|
abort: closeOrAbort
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/stream/sse.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/stream/sse.ts"],"names":[],"mappings":"AAEA,OAAO,KAA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AAC,OAAO,SAAS,cAAc,EAC9D,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,GACtC,aAAa,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,CA6G9C;AAED,eAAO,MAAM,UAAU;;;;;CAKb,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var TRPCError = require('../error/TRPCError.js');
|
|
4
|
+
var isAbortError = require('../http/isAbortError.js');
|
|
4
5
|
var utils = require('../utils.js');
|
|
5
6
|
var tracked = require('./tracked.js');
|
|
6
7
|
var asyncIterable = require('./utils/asyncIterable.js');
|
|
7
8
|
var createReadableStream = require('./utils/createReadableStream.js');
|
|
8
|
-
var promiseTimer = require('./utils/promiseTimer.js');
|
|
9
9
|
var withPing = require('./utils/withPing.js');
|
|
10
10
|
|
|
11
11
|
const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
@@ -24,18 +24,18 @@ const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
|
24
24
|
};
|
|
25
25
|
utils.run(async ()=>{
|
|
26
26
|
let iterable = opts.data;
|
|
27
|
-
iterable = asyncIterable.withCancel(iterable, stream.cancelledPromise);
|
|
28
27
|
if (opts.emitAndEndImmediately) {
|
|
29
28
|
iterable = asyncIterable.takeWithGrace(iterable, {
|
|
30
29
|
count: 1,
|
|
31
30
|
gracePeriodMs: 1,
|
|
32
|
-
|
|
31
|
+
abortCtrl: opts.abortCtrl
|
|
33
32
|
});
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
if (opts.maxDurationMs && opts.maxDurationMs > 0 && opts.maxDurationMs !== Infinity) {
|
|
35
|
+
iterable = asyncIterable.withMaxDuration(iterable, {
|
|
36
|
+
maxDurationMs: opts.maxDurationMs,
|
|
37
|
+
abortCtrl: opts.abortCtrl
|
|
38
|
+
});
|
|
39
39
|
}
|
|
40
40
|
if (ping.enabled && ping.intervalMs !== Infinity && ping.intervalMs > 0) {
|
|
41
41
|
iterable = withPing.withPing(iterable, ping.intervalMs);
|
|
@@ -67,22 +67,26 @@ const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
|
67
67
|
chunk = null;
|
|
68
68
|
}
|
|
69
69
|
} catch (err) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
// So, a user error in any case.
|
|
74
|
-
const error = TRPCError.getTRPCErrorFromUnknown(err);
|
|
75
|
-
const data = opts.formatError?.({
|
|
76
|
-
error
|
|
77
|
-
}) ?? null;
|
|
78
|
-
stream.controller.enqueue({
|
|
79
|
-
event: SERIALIZED_ERROR_EVENT,
|
|
80
|
-
data: JSON.stringify(serialize(data))
|
|
81
|
-
});
|
|
70
|
+
if (isAbortError.isAbortError(err)) {
|
|
71
|
+
// ignore abort errors, send any other errors
|
|
72
|
+
return;
|
|
82
73
|
}
|
|
74
|
+
// `err` must be caused by `opts.data`, `JSON.stringify` or `serialize`.
|
|
75
|
+
// So, a user error in any case.
|
|
76
|
+
const error = TRPCError.getTRPCErrorFromUnknown(err);
|
|
77
|
+
const data = opts.formatError?.({
|
|
78
|
+
error
|
|
79
|
+
}) ?? null;
|
|
80
|
+
stream.controller.enqueue({
|
|
81
|
+
event: SERIALIZED_ERROR_EVENT,
|
|
82
|
+
data: JSON.stringify(serialize(data))
|
|
83
|
+
});
|
|
83
84
|
} finally{
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
try {
|
|
86
|
+
stream.controller.close();
|
|
87
|
+
} catch {
|
|
88
|
+
// ignore
|
|
89
|
+
}
|
|
86
90
|
}
|
|
87
91
|
}).catch((err)=>{
|
|
88
92
|
// should not be reached; just in case...
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { getTRPCErrorFromUnknown } from '../error/TRPCError.mjs';
|
|
2
|
+
import { isAbortError } from '../http/isAbortError.mjs';
|
|
2
3
|
import { run, identity } from '../utils.mjs';
|
|
3
4
|
import { isTrackedEnvelope } from './tracked.mjs';
|
|
4
|
-
import {
|
|
5
|
+
import { takeWithGrace, withMaxDuration } from './utils/asyncIterable.mjs';
|
|
5
6
|
import { createReadableStream } from './utils/createReadableStream.mjs';
|
|
6
|
-
import { createPromiseTimer } from './utils/promiseTimer.mjs';
|
|
7
7
|
import { withPing, PING_SYM } from './utils/withPing.mjs';
|
|
8
8
|
|
|
9
9
|
const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
@@ -22,18 +22,18 @@ const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
|
22
22
|
};
|
|
23
23
|
run(async ()=>{
|
|
24
24
|
let iterable = opts.data;
|
|
25
|
-
iterable = withCancel(iterable, stream.cancelledPromise);
|
|
26
25
|
if (opts.emitAndEndImmediately) {
|
|
27
26
|
iterable = takeWithGrace(iterable, {
|
|
28
27
|
count: 1,
|
|
29
28
|
gracePeriodMs: 1,
|
|
30
|
-
|
|
29
|
+
abortCtrl: opts.abortCtrl
|
|
31
30
|
});
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
if (opts.maxDurationMs && opts.maxDurationMs > 0 && opts.maxDurationMs !== Infinity) {
|
|
33
|
+
iterable = withMaxDuration(iterable, {
|
|
34
|
+
maxDurationMs: opts.maxDurationMs,
|
|
35
|
+
abortCtrl: opts.abortCtrl
|
|
36
|
+
});
|
|
37
37
|
}
|
|
38
38
|
if (ping.enabled && ping.intervalMs !== Infinity && ping.intervalMs > 0) {
|
|
39
39
|
iterable = withPing(iterable, ping.intervalMs);
|
|
@@ -65,22 +65,26 @@ const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
|
65
65
|
chunk = null;
|
|
66
66
|
}
|
|
67
67
|
} catch (err) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// So, a user error in any case.
|
|
72
|
-
const error = getTRPCErrorFromUnknown(err);
|
|
73
|
-
const data = opts.formatError?.({
|
|
74
|
-
error
|
|
75
|
-
}) ?? null;
|
|
76
|
-
stream.controller.enqueue({
|
|
77
|
-
event: SERIALIZED_ERROR_EVENT,
|
|
78
|
-
data: JSON.stringify(serialize(data))
|
|
79
|
-
});
|
|
68
|
+
if (isAbortError(err)) {
|
|
69
|
+
// ignore abort errors, send any other errors
|
|
70
|
+
return;
|
|
80
71
|
}
|
|
72
|
+
// `err` must be caused by `opts.data`, `JSON.stringify` or `serialize`.
|
|
73
|
+
// So, a user error in any case.
|
|
74
|
+
const error = getTRPCErrorFromUnknown(err);
|
|
75
|
+
const data = opts.formatError?.({
|
|
76
|
+
error
|
|
77
|
+
}) ?? null;
|
|
78
|
+
stream.controller.enqueue({
|
|
79
|
+
event: SERIALIZED_ERROR_EVENT,
|
|
80
|
+
data: JSON.stringify(serialize(data))
|
|
81
|
+
});
|
|
81
82
|
} finally{
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
try {
|
|
84
|
+
stream.controller.close();
|
|
85
|
+
} catch {
|
|
86
|
+
// ignore
|
|
87
|
+
}
|
|
84
88
|
}
|
|
85
89
|
}).catch((err)=>{
|
|
86
90
|
// should not be reached; just in case...
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Derives a new {@link AsyncGenerator} based
|
|
3
|
-
* passed {@link cancel} promise.
|
|
2
|
+
* Derives a new {@link AsyncGenerator} based on {@link iterable}, that automatically stops after the specified duration.
|
|
4
3
|
*/
|
|
5
|
-
export declare function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
onCancel: () => void;
|
|
10
|
-
}
|
|
4
|
+
export declare function withMaxDuration<T>(iterable: AsyncIterable<T>, opts: {
|
|
5
|
+
maxDurationMs: number;
|
|
6
|
+
abortCtrl: AbortController;
|
|
7
|
+
}): AsyncGenerator<T>;
|
|
11
8
|
/**
|
|
12
9
|
* Derives a new {@link AsyncGenerator} based of {@link iterable}, that yields its first
|
|
13
10
|
* {@link count} values. Then, a grace period of {@link gracePeriodMs} is started in which further
|
|
14
11
|
* values may still come through. After this period, the generator stops.
|
|
15
12
|
*/
|
|
16
|
-
export declare function takeWithGrace<T>(iterable: AsyncIterable<T>,
|
|
17
|
-
|
|
13
|
+
export declare function takeWithGrace<T>(iterable: AsyncIterable<T>, opts: {
|
|
14
|
+
count: number;
|
|
15
|
+
gracePeriodMs: number;
|
|
16
|
+
abortCtrl: AbortController;
|
|
17
|
+
}): AsyncGenerator<T>;
|
|
18
18
|
//# sourceMappingURL=asyncIterable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncIterable.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/asyncIterable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"asyncIterable.d.ts","sourceRoot":"","sources":["../../../../src/unstable-core-do-not-import/stream/utils/asyncIterable.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,wBAAuB,eAAe,CAAC,CAAC,EACtC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,EAC1B,IAAI,EAAE;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,eAAe,CAAA;CAAE,GAC1D,cAAc,CAAC,CAAC,CAAC,CA8BnB;AAED;;;;GAIG;AACH,wBAAuB,aAAa,CAAC,CAAC,EACpC,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,EAC1B,IAAI,EAAE;IACJ,KAAK,EAAE,MAAM,CAAC;IACd,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,eAAe,CAAC;CAC5B,GACA,cAAc,CAAC,CAAC,CAAC,CAoCnB"}
|