@trpc/server 11.0.0-alpha-tmp-app-router-example.388 → 11.0.0-alpha-tmp-issues-5851-take-two.448
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 -2
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/@trpc/server/rpc.d.ts +1 -1
- package/dist/@trpc/server/rpc.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.js +19 -2
- package/dist/adapters/aws-lambda/getPlanner.mjs +19 -2
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.js +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +1 -1
- package/dist/adapters/next-app-dir/redirect.d.ts.map +1 -1
- package/dist/adapters/next.js +1 -1
- package/dist/adapters/next.mjs +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +0 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +3 -1
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +3 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +30 -7
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +30 -7
- package/dist/adapters/node-http/types.d.ts +0 -1
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +0 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/ws.d.ts +3 -4
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +172 -114
- package/dist/adapters/ws.mjs +172 -114
- package/dist/bundle-analysis.json +236 -155
- package/dist/http.js +3 -0
- package/dist/http.mjs +1 -0
- package/dist/index.js +7 -5
- package/dist/index.mjs +3 -2
- package/dist/observable/observable.d.ts +1 -0
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/observable.js +55 -0
- package/dist/observable/observable.mjs +55 -1
- package/dist/unstable-core-do-not-import/createProxy.d.ts +3 -3
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/createProxy.js +15 -6
- package/dist/unstable-core-do-not-import/createProxy.mjs +15 -6
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +7 -4
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +60 -17
- package/dist/unstable-core-do-not-import/http/contentType.mjs +61 -18
- package/dist/unstable-core-do-not-import/http/formDataToObject.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/formDataToObject.js +40 -0
- package/dist/unstable-core-do-not-import/http/formDataToObject.mjs +38 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +4 -4
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +4 -4
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.d.ts +4 -0
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.js +42 -0
- package/dist/unstable-core-do-not-import/http/parseConnectionParams.mjs +39 -0
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveResponse.js +302 -149
- package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +301 -148
- package/dist/unstable-core-do-not-import/http/types.d.ts +26 -2
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +12 -12
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/middleware.d.ts +3 -3
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +3 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +12 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.d.ts +2 -2
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.js +7 -2
- package/dist/unstable-core-do-not-import/router.mjs +7 -2
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +7 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +1 -1
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/{stream.d.ts → jsonl.d.ts} +5 -5
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/{stream.js → jsonl.js} +148 -111
- package/dist/unstable-core-do-not-import/stream/{stream.mjs → jsonl.mjs} +147 -110
- package/dist/unstable-core-do-not-import/stream/sse.d.ts +86 -0
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/sse.js +178 -0
- package/dist/unstable-core-do-not-import/stream/sse.mjs +172 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts +18 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.js +46 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.mjs +43 -0
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts +10 -0
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.js +31 -0
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.mjs +29 -0
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts +7 -0
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +5 -5
- package/dist/unstable-core-do-not-import/utils.d.ts +4 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +4 -0
- package/dist/unstable-core-do-not-import/utils.mjs +4 -1
- package/dist/unstable-core-do-not-import.d.ts +5 -2
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +19 -7
- package/dist/unstable-core-do-not-import.mjs +6 -3
- package/package.json +6 -6
- package/src/@trpc/server/http.ts +7 -2
- package/src/@trpc/server/index.ts +1 -0
- package/src/@trpc/server/rpc.ts +1 -0
- package/src/adapters/aws-lambda/getPlanner.ts +21 -2
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +2 -1
- package/src/adapters/node-http/incomingMessageToRequest.ts +3 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +32 -7
- package/src/adapters/ws.ts +193 -107
- package/src/observable/observable.ts +63 -0
- package/src/unstable-core-do-not-import/createProxy.ts +23 -8
- package/src/unstable-core-do-not-import/http/contentType.ts +83 -21
- package/src/{adapters/next-app-dir → unstable-core-do-not-import/http}/formDataToObject.ts +18 -10
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +4 -7
- package/src/unstable-core-do-not-import/http/parseConnectionParams.ts +49 -0
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +333 -164
- package/src/unstable-core-do-not-import/http/types.ts +31 -2
- package/src/unstable-core-do-not-import/procedureBuilder.ts +8 -1
- package/src/unstable-core-do-not-import/rootConfig.ts +12 -0
- package/src/unstable-core-do-not-import/router.ts +47 -35
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +9 -0
- package/src/unstable-core-do-not-import/rpc/index.ts +1 -0
- package/src/unstable-core-do-not-import/stream/{stream.ts → jsonl.ts} +163 -110
- package/src/unstable-core-do-not-import/stream/sse.ts +288 -0
- package/src/unstable-core-do-not-import/stream/utils/createDeferred.ts +48 -0
- package/src/unstable-core-do-not-import/stream/utils/createReadableStream.ts +31 -0
- package/src/unstable-core-do-not-import/stream/utils/createServer.ts +44 -0
- package/src/unstable-core-do-not-import/utils.ts +5 -0
- package/src/unstable-core-do-not-import.ts +5 -2
- package/dist/adapters/next-app-dir/formDataToObject.d.ts.map +0 -1
- package/dist/adapters/next-app-dir/formDataToObject.js +0 -34
- package/dist/adapters/next-app-dir/formDataToObject.mjs +0 -32
- package/dist/unstable-core-do-not-import/stream/stream.d.ts.map +0 -1
- /package/dist/{adapters/next-app-dir → unstable-core-do-not-import/http}/formDataToObject.d.ts +0 -0
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getTRPCErrorFromUnknown } from '../error/TRPCError.mjs';
|
|
2
|
+
import { isObject, isFunction, isAsyncIterable, run } from '../utils.mjs';
|
|
3
|
+
import { createDeferred } from './utils/createDeferred.mjs';
|
|
4
|
+
import { createReadableStream } from './utils/createReadableStream.mjs';
|
|
2
5
|
|
|
3
|
-
// ---------- utils
|
|
4
|
-
function createReadableStream() {
|
|
5
|
-
let controller = null;
|
|
6
|
-
const stream = new ReadableStream({
|
|
7
|
-
start (c) {
|
|
8
|
-
controller = c;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
return [
|
|
12
|
-
stream,
|
|
13
|
-
controller
|
|
14
|
-
];
|
|
15
|
-
}
|
|
16
6
|
// ---------- types
|
|
17
7
|
const CHUNK_VALUE_TYPE_PROMISE = 0;
|
|
18
8
|
const CHUNK_VALUE_TYPE_ASYNC_ITERABLE = 1;
|
|
@@ -34,14 +24,14 @@ function createBatchStreamProducer(opts) {
|
|
|
34
24
|
const { data } = opts;
|
|
35
25
|
let counter = 0;
|
|
36
26
|
const placeholder = 0;
|
|
37
|
-
const
|
|
27
|
+
const stream = createReadableStream();
|
|
38
28
|
const pending = new Set();
|
|
39
29
|
function maybeClose() {
|
|
40
|
-
if (pending.size === 0) {
|
|
41
|
-
controller.close();
|
|
30
|
+
if (pending.size === 0 && !stream.cancelled()) {
|
|
31
|
+
stream.controller.close();
|
|
42
32
|
}
|
|
43
33
|
}
|
|
44
|
-
function
|
|
34
|
+
function dehydratePromise(promise, path) {
|
|
45
35
|
//
|
|
46
36
|
const error = checkMaxDepth(path);
|
|
47
37
|
if (error) {
|
|
@@ -52,25 +42,28 @@ function createBatchStreamProducer(opts) {
|
|
|
52
42
|
}
|
|
53
43
|
const idx = counter++;
|
|
54
44
|
pending.add(idx);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
45
|
+
Promise.race([
|
|
46
|
+
promise,
|
|
47
|
+
stream.cancelledPromise
|
|
48
|
+
]).then((it)=>{
|
|
49
|
+
if (it === null) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
stream.controller.enqueue([
|
|
60
53
|
idx,
|
|
61
54
|
PROMISE_STATUS_FULFILLED,
|
|
62
|
-
|
|
55
|
+
dehydrate(it, path)
|
|
63
56
|
]);
|
|
64
|
-
}).catch((
|
|
57
|
+
}).catch((cause)=>{
|
|
65
58
|
opts.onError?.({
|
|
66
|
-
error,
|
|
59
|
+
error: cause,
|
|
67
60
|
path
|
|
68
61
|
});
|
|
69
|
-
enqueue([
|
|
62
|
+
stream.controller.enqueue([
|
|
70
63
|
idx,
|
|
71
64
|
PROMISE_STATUS_REJECTED,
|
|
72
65
|
opts.formatError?.({
|
|
73
|
-
error,
|
|
66
|
+
error: cause,
|
|
74
67
|
path
|
|
75
68
|
})
|
|
76
69
|
]);
|
|
@@ -80,7 +73,7 @@ function createBatchStreamProducer(opts) {
|
|
|
80
73
|
});
|
|
81
74
|
return idx;
|
|
82
75
|
}
|
|
83
|
-
function
|
|
76
|
+
function dehydrateAsyncIterable(iterable, path) {
|
|
84
77
|
const error = checkMaxDepth(path);
|
|
85
78
|
if (error) {
|
|
86
79
|
iterable = {
|
|
@@ -91,37 +84,58 @@ function createBatchStreamProducer(opts) {
|
|
|
91
84
|
}
|
|
92
85
|
const idx = counter++;
|
|
93
86
|
pending.add(idx);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
87
|
+
run(async ()=>{
|
|
88
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
89
|
+
while(true){
|
|
90
|
+
const next = await Promise.race([
|
|
91
|
+
iterator.next().catch(getTRPCErrorFromUnknown),
|
|
92
|
+
stream.cancelledPromise
|
|
93
|
+
]);
|
|
94
|
+
if (next instanceof Error) {
|
|
95
|
+
opts.onError?.({
|
|
96
|
+
error: next,
|
|
97
|
+
path
|
|
98
|
+
});
|
|
99
|
+
stream.controller.enqueue([
|
|
98
100
|
idx,
|
|
99
|
-
|
|
100
|
-
|
|
101
|
+
ASYNC_ITERABLE_STATUS_ERROR,
|
|
102
|
+
opts.formatError?.({
|
|
103
|
+
error: next,
|
|
104
|
+
path
|
|
105
|
+
})
|
|
101
106
|
]);
|
|
107
|
+
return;
|
|
102
108
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
if (next === 'cancelled') {
|
|
110
|
+
await iterator.return?.();
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
if (next.done) {
|
|
114
|
+
stream.controller.enqueue([
|
|
115
|
+
idx,
|
|
116
|
+
ASYNC_ITERABLE_STATUS_DONE
|
|
117
|
+
]);
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
stream.controller.enqueue([
|
|
113
121
|
idx,
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
error,
|
|
117
|
-
path
|
|
118
|
-
})
|
|
122
|
+
ASYNC_ITERABLE_STATUS_VALUE,
|
|
123
|
+
dehydrate(next.value, path)
|
|
119
124
|
]);
|
|
120
|
-
} finally{
|
|
121
|
-
pending.delete(idx);
|
|
122
|
-
maybeClose();
|
|
123
125
|
}
|
|
124
|
-
|
|
126
|
+
pending.delete(idx);
|
|
127
|
+
maybeClose();
|
|
128
|
+
}).catch((cause)=>{
|
|
129
|
+
// this shouldn't happen, but node crashes if we don't catch it
|
|
130
|
+
opts.onError?.({
|
|
131
|
+
error: new Error('You found a bug - please report it on https://github.com/trpc/trpc', // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
132
|
+
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
133
|
+
{
|
|
134
|
+
cause
|
|
135
|
+
}),
|
|
136
|
+
path
|
|
137
|
+
});
|
|
138
|
+
});
|
|
125
139
|
return idx;
|
|
126
140
|
}
|
|
127
141
|
function checkMaxDepth(path) {
|
|
@@ -130,11 +144,11 @@ function createBatchStreamProducer(opts) {
|
|
|
130
144
|
}
|
|
131
145
|
return null;
|
|
132
146
|
}
|
|
133
|
-
function
|
|
147
|
+
function dehydrateChunk(value, path) {
|
|
134
148
|
if (isPromise(value)) {
|
|
135
149
|
return [
|
|
136
150
|
CHUNK_VALUE_TYPE_PROMISE,
|
|
137
|
-
|
|
151
|
+
dehydratePromise(value, path)
|
|
138
152
|
];
|
|
139
153
|
}
|
|
140
154
|
if (isAsyncIterable(value)) {
|
|
@@ -143,13 +157,13 @@ function createBatchStreamProducer(opts) {
|
|
|
143
157
|
}
|
|
144
158
|
return [
|
|
145
159
|
CHUNK_VALUE_TYPE_ASYNC_ITERABLE,
|
|
146
|
-
|
|
160
|
+
dehydrateAsyncIterable(value, path)
|
|
147
161
|
];
|
|
148
162
|
}
|
|
149
163
|
return null;
|
|
150
164
|
}
|
|
151
|
-
function
|
|
152
|
-
const reg =
|
|
165
|
+
function dehydrate(value, path) {
|
|
166
|
+
const reg = dehydrateChunk(value, path);
|
|
153
167
|
if (reg) {
|
|
154
168
|
return [
|
|
155
169
|
[
|
|
@@ -171,7 +185,7 @@ function createBatchStreamProducer(opts) {
|
|
|
171
185
|
const newObj = {};
|
|
172
186
|
const asyncValues = [];
|
|
173
187
|
for (const [key, item] of Object.entries(value)){
|
|
174
|
-
const transformed =
|
|
188
|
+
const transformed = dehydrateChunk(item, [
|
|
175
189
|
...path,
|
|
176
190
|
key
|
|
177
191
|
]);
|
|
@@ -194,13 +208,13 @@ function createBatchStreamProducer(opts) {
|
|
|
194
208
|
}
|
|
195
209
|
const newHead = {};
|
|
196
210
|
for (const [key, item] of Object.entries(data)){
|
|
197
|
-
newHead[key] =
|
|
211
|
+
newHead[key] = dehydrate(item, [
|
|
198
212
|
key
|
|
199
213
|
]);
|
|
200
214
|
}
|
|
201
215
|
return [
|
|
202
216
|
newHead,
|
|
203
|
-
stream
|
|
217
|
+
stream.readable
|
|
204
218
|
];
|
|
205
219
|
}
|
|
206
220
|
/**
|
|
@@ -244,7 +258,7 @@ class AsyncError extends Error {
|
|
|
244
258
|
const nodeJsStreamToReaderEsque = (source)=>{
|
|
245
259
|
return {
|
|
246
260
|
getReader () {
|
|
247
|
-
const
|
|
261
|
+
const { readable , controller } = createReadableStream();
|
|
248
262
|
source.on('data', (chunk)=>{
|
|
249
263
|
controller.enqueue(chunk);
|
|
250
264
|
});
|
|
@@ -254,7 +268,7 @@ const nodeJsStreamToReaderEsque = (source)=>{
|
|
|
254
268
|
source.on('error', (error)=>{
|
|
255
269
|
controller.error(error);
|
|
256
270
|
});
|
|
257
|
-
return
|
|
271
|
+
return readable.getReader();
|
|
258
272
|
}
|
|
259
273
|
};
|
|
260
274
|
};
|
|
@@ -300,24 +314,12 @@ function createConsumerStream(from) {
|
|
|
300
314
|
}
|
|
301
315
|
}));
|
|
302
316
|
}
|
|
303
|
-
function createDeferred() {
|
|
304
|
-
let resolve;
|
|
305
|
-
let reject;
|
|
306
|
-
const promise = new Promise((res, rej)=>{
|
|
307
|
-
resolve = res;
|
|
308
|
-
reject = rej;
|
|
309
|
-
});
|
|
310
|
-
return {
|
|
311
|
-
promise,
|
|
312
|
-
resolve: resolve,
|
|
313
|
-
reject: reject
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
317
|
/**
|
|
317
318
|
* JSON Lines stream consumer
|
|
318
319
|
* @see https://jsonlines.org/
|
|
319
320
|
*/ async function jsonlStreamConsumer(opts) {
|
|
320
321
|
const { deserialize =(v)=>v } = opts;
|
|
322
|
+
const abortController = new AbortController();
|
|
321
323
|
let source = createConsumerStream(opts.from);
|
|
322
324
|
if (deserialize) {
|
|
323
325
|
source = source.pipeThrough(new TransformStream({
|
|
@@ -329,9 +331,16 @@ function createDeferred() {
|
|
|
329
331
|
let headDeferred = createDeferred();
|
|
330
332
|
const chunkDeferred = new Map();
|
|
331
333
|
const controllers = new Map();
|
|
332
|
-
|
|
334
|
+
const deleteController = (idx)=>{
|
|
335
|
+
controllers.delete(idx);
|
|
336
|
+
if (controllers.size === 0 && chunkDeferred.size === 0) {
|
|
337
|
+
// nothing can be listening to the stream anymore
|
|
338
|
+
abortController.abort();
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
function hydrateChunkDefinition(value) {
|
|
333
342
|
const [_path, type, chunkId] = value;
|
|
334
|
-
const
|
|
343
|
+
const { readable , controller } = createReadableStream();
|
|
335
344
|
controllers.set(chunkId, controller);
|
|
336
345
|
// resolve chunk deferred if it exists
|
|
337
346
|
const deferred = chunkDeferred.get(chunkId);
|
|
@@ -344,7 +353,7 @@ function createDeferred() {
|
|
|
344
353
|
{
|
|
345
354
|
return new Promise((resolve, reject)=>{
|
|
346
355
|
// listen for next value in the stream
|
|
347
|
-
const reader =
|
|
356
|
+
const reader = readable.getReader();
|
|
348
357
|
reader.read().then((it)=>{
|
|
349
358
|
if (it.done) {
|
|
350
359
|
reject(new Error('Promise chunk ended without value'));
|
|
@@ -358,7 +367,7 @@ function createDeferred() {
|
|
|
358
367
|
const [_chunkId, status, data] = value;
|
|
359
368
|
switch(status){
|
|
360
369
|
case PROMISE_STATUS_FULFILLED:
|
|
361
|
-
resolve(
|
|
370
|
+
resolve(hydrate(data));
|
|
362
371
|
break;
|
|
363
372
|
case PROMISE_STATUS_REJECTED:
|
|
364
373
|
reject(opts.formatError?.({
|
|
@@ -375,45 +384,68 @@ function createDeferred() {
|
|
|
375
384
|
case CHUNK_VALUE_TYPE_ASYNC_ITERABLE:
|
|
376
385
|
{
|
|
377
386
|
return {
|
|
378
|
-
[Symbol.asyncIterator]:
|
|
379
|
-
const reader =
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
387
|
+
[Symbol.asyncIterator]: ()=>{
|
|
388
|
+
const reader = readable.getReader();
|
|
389
|
+
const iterator = {
|
|
390
|
+
next: async ()=>{
|
|
391
|
+
const { done , value } = await reader.read();
|
|
392
|
+
if (value instanceof StreamInterruptedError) {
|
|
393
|
+
throw value;
|
|
394
|
+
}
|
|
395
|
+
if (done) {
|
|
396
|
+
deleteController(chunkId);
|
|
397
|
+
return {
|
|
398
|
+
done: true,
|
|
399
|
+
value: undefined
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
const [_chunkId, status, data] = value;
|
|
403
|
+
switch(status){
|
|
404
|
+
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
405
|
+
return {
|
|
406
|
+
done: false,
|
|
407
|
+
value: hydrate(data)
|
|
408
|
+
};
|
|
409
|
+
case ASYNC_ITERABLE_STATUS_DONE:
|
|
410
|
+
deleteController(chunkId);
|
|
411
|
+
return {
|
|
412
|
+
done: true,
|
|
413
|
+
value: undefined
|
|
414
|
+
};
|
|
415
|
+
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
416
|
+
deleteController(chunkId);
|
|
417
|
+
throw opts.formatError?.({
|
|
418
|
+
error: data
|
|
419
|
+
}) ?? new AsyncError(data);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
return: async ()=>{
|
|
423
|
+
deleteController(chunkId);
|
|
424
|
+
if (controllers.size === 0) {
|
|
425
|
+
// nothing can be listening to the stream anymore
|
|
426
|
+
abortController.abort();
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
done: true,
|
|
430
|
+
value: undefined
|
|
431
|
+
};
|
|
401
432
|
}
|
|
402
|
-
}
|
|
433
|
+
};
|
|
434
|
+
return iterator;
|
|
403
435
|
}
|
|
404
436
|
};
|
|
405
437
|
}
|
|
406
438
|
}
|
|
407
439
|
}
|
|
408
|
-
function
|
|
440
|
+
function hydrate(value) {
|
|
409
441
|
const [[data], ...asyncProps] = value;
|
|
410
442
|
for (const value1 of asyncProps){
|
|
411
|
-
const
|
|
443
|
+
const hydrated = hydrateChunkDefinition(value1);
|
|
412
444
|
const [path] = value1;
|
|
413
445
|
if (path === null) {
|
|
414
|
-
return
|
|
446
|
+
return hydrated;
|
|
415
447
|
}
|
|
416
|
-
data[path] =
|
|
448
|
+
data[path] = hydrated;
|
|
417
449
|
}
|
|
418
450
|
return data;
|
|
419
451
|
}
|
|
@@ -431,11 +463,16 @@ function createDeferred() {
|
|
|
431
463
|
controllers.clear();
|
|
432
464
|
};
|
|
433
465
|
source.pipeTo(new WritableStream({
|
|
466
|
+
start (controller) {
|
|
467
|
+
abortController.signal.addEventListener('abort', ()=>{
|
|
468
|
+
controller.error(abortController.signal.reason);
|
|
469
|
+
});
|
|
470
|
+
},
|
|
434
471
|
async write (chunkOrHead) {
|
|
435
472
|
if (headDeferred) {
|
|
436
473
|
const head = chunkOrHead;
|
|
437
474
|
for (const [key, value] of Object.entries(chunkOrHead)){
|
|
438
|
-
const parsed =
|
|
475
|
+
const parsed = hydrate(value);
|
|
439
476
|
head[key] = parsed;
|
|
440
477
|
}
|
|
441
478
|
headDeferred.resolve(head);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ValidateShape } from '../types';
|
|
2
|
+
import type { ConsumerOnError } from './jsonl';
|
|
3
|
+
type Serialize = (value: any) => any;
|
|
4
|
+
type Deserialize = (value: any) => any;
|
|
5
|
+
/**
|
|
6
|
+
* Server-sent Event Message
|
|
7
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export interface SSEMessage {
|
|
11
|
+
/**
|
|
12
|
+
* The data field of the message - this can be anything
|
|
13
|
+
*/
|
|
14
|
+
data: unknown;
|
|
15
|
+
/**
|
|
16
|
+
* The id for this message
|
|
17
|
+
* Passing this id will allow the client to resume the connection from this point if the connection is lost
|
|
18
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-last-event-id-header
|
|
19
|
+
*/
|
|
20
|
+
id: string;
|
|
21
|
+
}
|
|
22
|
+
declare const sseSymbol: unique symbol;
|
|
23
|
+
export type SSEMessageEnvelope<TData> = [typeof sseSymbol, TData];
|
|
24
|
+
/**
|
|
25
|
+
* Produce a typed server-sent event message
|
|
26
|
+
*/
|
|
27
|
+
export declare function sse<TData extends SSEMessage>(event: ValidateShape<TData, SSEMessage>): SSEMessageEnvelope<TData>;
|
|
28
|
+
export declare function isSSEMessageEnvelope<TData extends SSEMessage>(value: unknown): value is SSEMessageEnvelope<TData>;
|
|
29
|
+
export type SerializedSSEvent = Omit<SSEMessage, 'data'> & {
|
|
30
|
+
data?: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export interface PingOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Enable ping comments sent from the server
|
|
38
|
+
* @default false
|
|
39
|
+
*/
|
|
40
|
+
enabled: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Interval in milliseconds
|
|
43
|
+
* @default 1000
|
|
44
|
+
*/
|
|
45
|
+
intervalMs?: number;
|
|
46
|
+
}
|
|
47
|
+
export interface SSEStreamProducerOptions {
|
|
48
|
+
serialize?: Serialize;
|
|
49
|
+
data: AsyncIterable<unknown>;
|
|
50
|
+
maxDepth?: number;
|
|
51
|
+
ping?: PingOptions;
|
|
52
|
+
/**
|
|
53
|
+
* Maximum duration in milliseconds for the request before ending the stream
|
|
54
|
+
* Only useful for serverless runtimes
|
|
55
|
+
* @default undefined
|
|
56
|
+
*/
|
|
57
|
+
maxDurationMs?: number;
|
|
58
|
+
/**
|
|
59
|
+
* End the request immediately after data is sent
|
|
60
|
+
* Only useful for serverless runtimes that do not support streaming responses
|
|
61
|
+
* @default false
|
|
62
|
+
*/
|
|
63
|
+
emitAndEndImmediately?: boolean;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
68
|
+
*/
|
|
69
|
+
export declare function sseStreamProducer(opts: SSEStreamProducerOptions): ReadableStream<string>;
|
|
70
|
+
export type inferSSEOutput<TData> = TData extends SSEMessageEnvelope<infer $Data> ? $Data : TData;
|
|
71
|
+
/**
|
|
72
|
+
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
73
|
+
*/
|
|
74
|
+
export declare function sseStreamConsumer<TData>(opts: {
|
|
75
|
+
from: EventSource;
|
|
76
|
+
onError?: ConsumerOnError;
|
|
77
|
+
deserialize?: Deserialize;
|
|
78
|
+
}): AsyncIterable<inferSSEOutput<TData>>;
|
|
79
|
+
export declare const sseHeaders: {
|
|
80
|
+
readonly 'Content-Type': "text/event-stream";
|
|
81
|
+
readonly 'Cache-Control': "no-cache, no-transform";
|
|
82
|
+
readonly 'X-Accel-Buffering': "no";
|
|
83
|
+
readonly Connection: "keep-alive";
|
|
84
|
+
};
|
|
85
|
+
export {};
|
|
86
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/stream/sse.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAI/C,KAAK,SAAS,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;AACrC,KAAK,WAAW,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;AAEvC;;;;GAIG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,IAAI,EAAE,OAAO,CAAC;IACd;;;;OAIG;IACH,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,QAAA,MAAM,SAAS,eAA+B,CAAC;AAC/C,MAAM,MAAM,kBAAkB,CAAC,KAAK,IAAI,CAAC,OAAO,SAAS,EAAE,KAAK,CAAC,CAAC;AAElE;;GAEG;AACH,wBAAgB,GAAG,CAAC,KAAK,SAAS,UAAU,EAC1C,KAAK,EAAE,aAAa,CAAC,KAAK,EAAE,UAAU,CAAC,GACtC,kBAAkB,CAAC,KAAK,CAAC,CAQ3B;AAED,wBAAgB,oBAAoB,CAAC,KAAK,SAAS,UAAU,EAC3D,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,kBAAkB,CAAC,KAAK,CAAC,CAEpC;AAED,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,wBAAwB;IACvC,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAQD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,wBAAwB,0BA2G/D;AACD,MAAM,MAAM,cAAc,CAAC,KAAK,IAAI,KAAK,SAAS,kBAAkB,CAClE,MAAM,KAAK,CACZ,GACG,KAAK,GACL,KAAK,CAAC;AACV;;GAEG;AAEH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,IAAI,EAAE;IAC7C,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,CAAC,EAAE,eAAe,CAAC;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,GAAG,aAAa,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CA0DvC;AAED,eAAO,MAAM,UAAU;;;;;CAKb,CAAC"}
|