@trpc/server 11.0.0-rc.452 → 11.0.0-rc.456
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/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +3 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +3 -1
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +4 -1
- package/dist/adapters/ws.mjs +4 -1
- package/dist/bundle-analysis.json +107 -106
- package/dist/observable/observable.d.ts +1 -4
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/observable.js +6 -18
- package/dist/observable/observable.mjs +6 -18
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +1 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/contentType.js +6 -3
- package/dist/unstable-core-do-not-import/http/contentType.mjs +6 -3
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +5 -2
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +5 -2
- package/dist/unstable-core-do-not-import/http/types.d.ts +5 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +4 -1
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/codes.js +4 -1
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +4 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +8 -3
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +66 -32
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +66 -32
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.d.ts +1 -0
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +3 -0
- package/dist/unstable-core-do-not-import.mjs +1 -0
- package/package.json +2 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +3 -1
- package/src/adapters/ws.ts +3 -0
- package/src/observable/observable.ts +6 -19
- package/src/unstable-core-do-not-import/http/contentType.ts +3 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +4 -1
- package/src/unstable-core-do-not-import/http/types.ts +5 -0
- package/src/unstable-core-do-not-import/rpc/codes.ts +6 -3
- package/src/unstable-core-do-not-import/stream/jsonl.ts +88 -43
- package/src/unstable-core-do-not-import/stream/utils/createServer.ts +1 -3
- package/src/unstable-core-do-not-import.ts +1 -0
|
@@ -34,6 +34,7 @@ export * from './unstable-core-do-not-import/router';
|
|
|
34
34
|
export * from './unstable-core-do-not-import/rpc';
|
|
35
35
|
export * from './unstable-core-do-not-import/stream/jsonl';
|
|
36
36
|
export * from './unstable-core-do-not-import/stream/sse';
|
|
37
|
+
export * from './unstable-core-do-not-import/stream/utils/createDeferred';
|
|
37
38
|
export * from './unstable-core-do-not-import/transformer';
|
|
38
39
|
export * from './unstable-core-do-not-import/types';
|
|
39
40
|
export * from './unstable-core-do-not-import/utils';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unstable-core-do-not-import.d.ts","sourceRoot":"","sources":["../src/unstable-core-do-not-import.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,cAAc,mDAAmD,CAAC;AAClE,cAAc,oDAAoD,CAAC;AACnE,cAAc,mDAAmD,CAAC;AAClE,cAAc,2CAA2C,CAAC;AAC1D,cAAc,+CAA+C,CAAC;AAC9D,cAAc,mDAAmD,CAAC;AAClE,cAAc,+CAA+C,CAAC;AAC9D,cAAc,yDAAyD,CAAC;AACxE,cAAc,gDAAgD,CAAC;AAC/D,cAAc,uDAAuD,CAAC;AACtE,cAAc,qDAAqD,CAAC;AACpE,cAAc,sDAAsD,CAAC;AACrE,cAAc,0DAA0D,CAAC;AACzE,cAAc,oDAAoD,CAAC;AACnE,cAAc,0CAA0C,CAAC;AACzD,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC;AACvD,cAAc,0CAA0C,CAAC;AACzD,cAAc,sCAAsC,CAAC;AACrD,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,0CAA0C,CAAC;AACzD,cAAc,sCAAsC,CAAC;AACrD,cAAc,mCAAmC,CAAC;AAClD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,0CAA0C,CAAC;AACzD,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qCAAqC,CAAC;AACpD,cAAc,qCAAqC,CAAC"}
|
|
1
|
+
{"version":3,"file":"unstable-core-do-not-import.d.ts","sourceRoot":"","sources":["../src/unstable-core-do-not-import.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,cAAc,mDAAmD,CAAC;AAClE,cAAc,oDAAoD,CAAC;AACnE,cAAc,mDAAmD,CAAC;AAClE,cAAc,2CAA2C,CAAC;AAC1D,cAAc,+CAA+C,CAAC;AAC9D,cAAc,mDAAmD,CAAC;AAClE,cAAc,+CAA+C,CAAC;AAC9D,cAAc,yDAAyD,CAAC;AACxE,cAAc,gDAAgD,CAAC;AAC/D,cAAc,uDAAuD,CAAC;AACtE,cAAc,qDAAqD,CAAC;AACpE,cAAc,sDAAsD,CAAC;AACrE,cAAc,0DAA0D,CAAC;AACzE,cAAc,oDAAoD,CAAC;AACnE,cAAc,0CAA0C,CAAC;AACzD,cAAc,0CAA0C,CAAC;AACzD,cAAc,wCAAwC,CAAC;AACvD,cAAc,0CAA0C,CAAC;AACzD,cAAc,sCAAsC,CAAC;AACrD,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC;AAC/D,cAAc,0CAA0C,CAAC;AACzD,cAAc,sCAAsC,CAAC;AACrD,cAAc,mCAAmC,CAAC;AAClD,cAAc,4CAA4C,CAAC;AAC3D,cAAc,0CAA0C,CAAC;AACzD,cAAc,2DAA2D,CAAC;AAC1E,cAAc,2CAA2C,CAAC;AAC1D,cAAc,qCAAqC,CAAC;AACpD,cAAc,qCAAqC,CAAC"}
|
|
@@ -23,6 +23,7 @@ var codes = require('./unstable-core-do-not-import/rpc/codes.js');
|
|
|
23
23
|
var parseTRPCMessage = require('./unstable-core-do-not-import/rpc/parseTRPCMessage.js');
|
|
24
24
|
var jsonl = require('./unstable-core-do-not-import/stream/jsonl.js');
|
|
25
25
|
var sse = require('./unstable-core-do-not-import/stream/sse.js');
|
|
26
|
+
var createDeferred = require('./unstable-core-do-not-import/stream/utils/createDeferred.js');
|
|
26
27
|
var transformer = require('./unstable-core-do-not-import/transformer.js');
|
|
27
28
|
var types = require('./unstable-core-do-not-import/types.js');
|
|
28
29
|
var utils = require('./unstable-core-do-not-import/utils.js');
|
|
@@ -71,6 +72,8 @@ exports.sse = sse.sse;
|
|
|
71
72
|
exports.sseHeaders = sse.sseHeaders;
|
|
72
73
|
exports.sseStreamConsumer = sse.sseStreamConsumer;
|
|
73
74
|
exports.sseStreamProducer = sse.sseStreamProducer;
|
|
75
|
+
exports.createDeferred = createDeferred.createDeferred;
|
|
76
|
+
exports.createTimeoutPromise = createDeferred.createTimeoutPromise;
|
|
74
77
|
exports.defaultTransformer = transformer.defaultTransformer;
|
|
75
78
|
exports.getDataTransformer = transformer.getDataTransformer;
|
|
76
79
|
exports.transformResult = transformer.transformResult;
|
|
@@ -21,6 +21,7 @@ export { TRPC_ERROR_CODES_BY_KEY, TRPC_ERROR_CODES_BY_NUMBER } from './unstable-
|
|
|
21
21
|
export { parseTRPCMessage } from './unstable-core-do-not-import/rpc/parseTRPCMessage.mjs';
|
|
22
22
|
export { isPromise, jsonlStreamConsumer, jsonlStreamProducer } from './unstable-core-do-not-import/stream/jsonl.mjs';
|
|
23
23
|
export { isSSEMessageEnvelope, sse, sseHeaders, sseStreamConsumer, sseStreamProducer } from './unstable-core-do-not-import/stream/sse.mjs';
|
|
24
|
+
export { createDeferred, createTimeoutPromise } from './unstable-core-do-not-import/stream/utils/createDeferred.mjs';
|
|
24
25
|
export { defaultTransformer, getDataTransformer, transformResult, transformTRPCResponse } from './unstable-core-do-not-import/transformer.mjs';
|
|
25
26
|
export { ERROR_SYMBOL } from './unstable-core-do-not-import/types.mjs';
|
|
26
27
|
export { isAsyncIterable, isFunction, isObject, mergeWithoutOverrides, omitPrototype, run, unsetMarker } from './unstable-core-do-not-import/utils.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trpc/server",
|
|
3
|
-
"version": "11.0.0-rc.
|
|
3
|
+
"version": "11.0.0-rc.456+a9a2c25b1",
|
|
4
4
|
"description": "The tRPC server library",
|
|
5
5
|
"author": "KATT",
|
|
6
6
|
"license": "MIT",
|
|
@@ -149,5 +149,5 @@
|
|
|
149
149
|
"funding": [
|
|
150
150
|
"https://trpc.io/sponsor"
|
|
151
151
|
],
|
|
152
|
-
"gitHead": "
|
|
152
|
+
"gitHead": "a9a2c25b1ead6113bde45bb26c627e3696fb61a8"
|
|
153
153
|
}
|
|
@@ -71,7 +71,9 @@ export async function nodeHTTPRequestHandler<
|
|
|
71
71
|
// console.error('reader.cancel() error', err);
|
|
72
72
|
});
|
|
73
73
|
};
|
|
74
|
-
req.signal.addEventListener('abort', onAbort
|
|
74
|
+
req.signal.addEventListener('abort', onAbort, {
|
|
75
|
+
once: true,
|
|
76
|
+
});
|
|
75
77
|
|
|
76
78
|
while (true) {
|
|
77
79
|
const { done, value } = await reader.read();
|
package/src/adapters/ws.ts
CHANGED
|
@@ -97,6 +97,7 @@ export function getWSConnectionHandler<TRouter extends AnyRouter>(
|
|
|
97
97
|
|
|
98
98
|
return async (client: ws.WebSocket, req: IncomingMessage) => {
|
|
99
99
|
const clientSubscriptions = new Map<number | string, AbortController>();
|
|
100
|
+
const abortController = new AbortController();
|
|
100
101
|
|
|
101
102
|
function respond(untransformedJSON: TRPCResponseMessage) {
|
|
102
103
|
client.send(
|
|
@@ -119,6 +120,7 @@ export function getWSConnectionHandler<TRouter extends AnyRouter>(
|
|
|
119
120
|
isBatchCall: false,
|
|
120
121
|
accept: null,
|
|
121
122
|
type: 'unknown',
|
|
123
|
+
signal: abortController.signal,
|
|
122
124
|
},
|
|
123
125
|
});
|
|
124
126
|
|
|
@@ -410,6 +412,7 @@ export function getWSConnectionHandler<TRouter extends AnyRouter>(
|
|
|
410
412
|
sub.abort();
|
|
411
413
|
}
|
|
412
414
|
clientSubscriptions.clear();
|
|
415
|
+
abortController.abort();
|
|
413
416
|
});
|
|
414
417
|
|
|
415
418
|
if (ctxPromise !== unsetContextSymbol) {
|
|
@@ -90,19 +90,11 @@ function pipeReducer(prev: any, fn: UnaryFunction<any, any>) {
|
|
|
90
90
|
return fn(prev);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
class ObservableAbortError extends Error {
|
|
94
|
-
constructor(message: string) {
|
|
95
|
-
super(message);
|
|
96
|
-
this.name = 'ObservableAbortError';
|
|
97
|
-
Object.setPrototypeOf(this, ObservableAbortError.prototype);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
93
|
/** @internal */
|
|
102
94
|
export function observableToPromise<TValue>(
|
|
103
95
|
observable: Observable<TValue, unknown>,
|
|
104
96
|
) {
|
|
105
|
-
|
|
97
|
+
const ac = new AbortController();
|
|
106
98
|
const promise = new Promise<TValue>((resolve, reject) => {
|
|
107
99
|
let isDone = false;
|
|
108
100
|
function onDone() {
|
|
@@ -110,9 +102,11 @@ export function observableToPromise<TValue>(
|
|
|
110
102
|
return;
|
|
111
103
|
}
|
|
112
104
|
isDone = true;
|
|
113
|
-
reject(new ObservableAbortError('This operation was aborted.'));
|
|
114
105
|
obs$.unsubscribe();
|
|
115
106
|
}
|
|
107
|
+
ac.signal.addEventListener('abort', () => {
|
|
108
|
+
reject(ac.signal.reason);
|
|
109
|
+
});
|
|
116
110
|
const obs$ = observable.subscribe({
|
|
117
111
|
next(data) {
|
|
118
112
|
isDone = true;
|
|
@@ -120,22 +114,15 @@ export function observableToPromise<TValue>(
|
|
|
120
114
|
onDone();
|
|
121
115
|
},
|
|
122
116
|
error(data) {
|
|
123
|
-
isDone = true;
|
|
124
117
|
reject(data);
|
|
125
|
-
onDone();
|
|
126
118
|
},
|
|
127
119
|
complete() {
|
|
128
|
-
|
|
120
|
+
ac.abort();
|
|
129
121
|
onDone();
|
|
130
122
|
},
|
|
131
123
|
});
|
|
132
|
-
abort = onDone;
|
|
133
124
|
});
|
|
134
|
-
return
|
|
135
|
-
promise,
|
|
136
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
137
|
-
abort: abort!,
|
|
138
|
-
};
|
|
125
|
+
return promise;
|
|
139
126
|
}
|
|
140
127
|
|
|
141
128
|
/**
|
|
@@ -173,6 +173,7 @@ const jsonContentTypeHandler: ContentTypeHandler = {
|
|
|
173
173
|
connectionParamsStr === null
|
|
174
174
|
? null
|
|
175
175
|
: parseConnectionParamsFromString(connectionParamsStr),
|
|
176
|
+
signal: req.signal,
|
|
176
177
|
};
|
|
177
178
|
return info;
|
|
178
179
|
},
|
|
@@ -208,6 +209,7 @@ const formDataContentTypeHandler: ContentTypeHandler = {
|
|
|
208
209
|
isBatchCall: false,
|
|
209
210
|
type: 'mutation',
|
|
210
211
|
connectionParams: null,
|
|
212
|
+
signal: req.signal,
|
|
211
213
|
};
|
|
212
214
|
},
|
|
213
215
|
};
|
|
@@ -243,6 +245,7 @@ const octetStreamContentTypeHandler: ContentTypeHandler = {
|
|
|
243
245
|
accept: null,
|
|
244
246
|
type: 'mutation',
|
|
245
247
|
connectionParams: null,
|
|
248
|
+
signal: req.signal,
|
|
246
249
|
};
|
|
247
250
|
},
|
|
248
251
|
};
|
|
@@ -10,8 +10,8 @@ const JSONRPC2_TO_HTTP_CODE: Record<
|
|
|
10
10
|
PARSE_ERROR: 400,
|
|
11
11
|
BAD_REQUEST: 400,
|
|
12
12
|
UNAUTHORIZED: 401,
|
|
13
|
-
NOT_FOUND: 404,
|
|
14
13
|
FORBIDDEN: 403,
|
|
14
|
+
NOT_FOUND: 404,
|
|
15
15
|
METHOD_NOT_SUPPORTED: 405,
|
|
16
16
|
TIMEOUT: 408,
|
|
17
17
|
CONFLICT: 409,
|
|
@@ -23,6 +23,9 @@ const JSONRPC2_TO_HTTP_CODE: Record<
|
|
|
23
23
|
CLIENT_CLOSED_REQUEST: 499,
|
|
24
24
|
INTERNAL_SERVER_ERROR: 500,
|
|
25
25
|
NOT_IMPLEMENTED: 501,
|
|
26
|
+
BAD_GATEWAY: 502,
|
|
27
|
+
SERVICE_UNAVAILABLE: 503,
|
|
28
|
+
GATEWAY_TIMEOUT: 504,
|
|
26
29
|
};
|
|
27
30
|
|
|
28
31
|
function getStatusCodeFromKey(code: keyof typeof TRPC_ERROR_CODES_BY_KEY) {
|
|
@@ -99,6 +99,11 @@ export interface TRPCRequestInfo {
|
|
|
99
99
|
* Connection params when using `httpSubscriptionLink` or `createWSClient`
|
|
100
100
|
*/
|
|
101
101
|
connectionParams: Dict<string> | null;
|
|
102
|
+
/**
|
|
103
|
+
* Signal when the request is aborted
|
|
104
|
+
* Can be used to abort async operations during the request, e.g. `fetch()`-requests
|
|
105
|
+
*/
|
|
106
|
+
signal: AbortSignal;
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
/**
|
|
@@ -20,8 +20,11 @@ export const TRPC_ERROR_CODES_BY_KEY = {
|
|
|
20
20
|
BAD_REQUEST: -32600, // 400
|
|
21
21
|
|
|
22
22
|
// Internal JSON-RPC error
|
|
23
|
-
INTERNAL_SERVER_ERROR: -32603,
|
|
24
|
-
NOT_IMPLEMENTED: -32603,
|
|
23
|
+
INTERNAL_SERVER_ERROR: -32603, // 500
|
|
24
|
+
NOT_IMPLEMENTED: -32603, // 501
|
|
25
|
+
BAD_GATEWAY: -32603, // 502
|
|
26
|
+
SERVICE_UNAVAILABLE: -32603, // 503
|
|
27
|
+
GATEWAY_TIMEOUT: -32603, // 504
|
|
25
28
|
|
|
26
29
|
// Implementation specific errors
|
|
27
30
|
UNAUTHORIZED: -32001, // 401
|
|
@@ -31,8 +34,8 @@ export const TRPC_ERROR_CODES_BY_KEY = {
|
|
|
31
34
|
TIMEOUT: -32008, // 408
|
|
32
35
|
CONFLICT: -32009, // 409
|
|
33
36
|
PRECONDITION_FAILED: -32012, // 412
|
|
34
|
-
UNSUPPORTED_MEDIA_TYPE: -32015, // 415
|
|
35
37
|
PAYLOAD_TOO_LARGE: -32013, // 413
|
|
38
|
+
UNSUPPORTED_MEDIA_TYPE: -32015, // 415
|
|
36
39
|
UNPROCESSABLE_CONTENT: -32022, // 422
|
|
37
40
|
TOO_MANY_REQUESTS: -32029, // 429
|
|
38
41
|
CLIENT_CLOSED_REQUEST: -32099, // 499
|
|
@@ -94,18 +94,16 @@ export function isPromise(value: unknown): value is Promise<unknown> {
|
|
|
94
94
|
type Serialize = (value: any) => any;
|
|
95
95
|
type Deserialize = (value: any) => any;
|
|
96
96
|
|
|
97
|
+
type PathArray = readonly (string | number)[];
|
|
97
98
|
export type ProducerOnError = (opts: {
|
|
98
99
|
error: unknown;
|
|
99
|
-
path:
|
|
100
|
+
path: PathArray;
|
|
100
101
|
}) => void;
|
|
101
102
|
export interface ProducerOptions {
|
|
102
103
|
serialize?: Serialize;
|
|
103
104
|
data: Record<string, unknown> | unknown[];
|
|
104
105
|
onError?: ProducerOnError;
|
|
105
|
-
formatError?: (opts: {
|
|
106
|
-
error: unknown;
|
|
107
|
-
path: (string | number)[];
|
|
108
|
-
}) => unknown;
|
|
106
|
+
formatError?: (opts: { error: unknown; path: PathArray }) => unknown;
|
|
109
107
|
maxDepth?: number;
|
|
110
108
|
}
|
|
111
109
|
|
|
@@ -424,6 +422,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
424
422
|
deserialize?: Deserialize;
|
|
425
423
|
onError?: ConsumerOnError;
|
|
426
424
|
formatError?: (opts: { error: unknown }) => Error;
|
|
425
|
+
abortController: AbortController | null;
|
|
427
426
|
}) {
|
|
428
427
|
const { deserialize = (v) => v } = opts;
|
|
429
428
|
|
|
@@ -441,19 +440,38 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
441
440
|
|
|
442
441
|
type ControllerChunk = ChunkData | StreamInterruptedError;
|
|
443
442
|
type ChunkController = ReadableStreamDefaultController<ControllerChunk>;
|
|
444
|
-
|
|
445
|
-
|
|
443
|
+
type ControllerWrapper = {
|
|
444
|
+
controller: ChunkController;
|
|
445
|
+
returned: boolean;
|
|
446
|
+
};
|
|
447
|
+
const chunkDeferred = new Map<ChunkIndex, Deferred<ControllerWrapper>>();
|
|
448
|
+
const controllers = new Map<ChunkIndex, ControllerWrapper>();
|
|
449
|
+
|
|
450
|
+
const maybeAbort = () => {
|
|
451
|
+
if (
|
|
452
|
+
chunkDeferred.size === 0 &&
|
|
453
|
+
Array.from(controllers.values()).every((it) => it.returned)
|
|
454
|
+
) {
|
|
455
|
+
// nothing is listening to the stream anymore
|
|
456
|
+
opts.abortController?.abort();
|
|
457
|
+
}
|
|
458
|
+
};
|
|
446
459
|
|
|
447
460
|
function hydrateChunkDefinition(value: ChunkDefinition) {
|
|
448
461
|
const [_path, type, chunkId] = value;
|
|
449
462
|
|
|
450
463
|
const { readable, controller } = createReadableStream<ChunkData>();
|
|
451
|
-
|
|
464
|
+
|
|
465
|
+
const wrapper: ControllerWrapper = {
|
|
466
|
+
controller,
|
|
467
|
+
returned: false,
|
|
468
|
+
};
|
|
469
|
+
controllers.set(chunkId, wrapper);
|
|
452
470
|
|
|
453
471
|
// resolve chunk deferred if it exists
|
|
454
472
|
const deferred = chunkDeferred.get(chunkId);
|
|
455
473
|
if (deferred) {
|
|
456
|
-
deferred.resolve(
|
|
474
|
+
deferred.resolve(wrapper);
|
|
457
475
|
chunkDeferred.delete(chunkId);
|
|
458
476
|
}
|
|
459
477
|
|
|
@@ -478,6 +496,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
478
496
|
switch (status) {
|
|
479
497
|
case PROMISE_STATUS_FULFILLED:
|
|
480
498
|
resolve(hydrate(data));
|
|
499
|
+
|
|
481
500
|
break;
|
|
482
501
|
case PROMISE_STATUS_REJECTED:
|
|
483
502
|
reject(
|
|
@@ -490,38 +509,64 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
490
509
|
.finally(() => {
|
|
491
510
|
// reader.releaseLock();
|
|
492
511
|
controllers.delete(chunkId);
|
|
512
|
+
|
|
513
|
+
maybeAbort();
|
|
493
514
|
});
|
|
494
515
|
});
|
|
495
516
|
}
|
|
496
517
|
case CHUNK_VALUE_TYPE_ASYNC_ITERABLE: {
|
|
497
518
|
return {
|
|
498
|
-
[Symbol.asyncIterator]:
|
|
519
|
+
[Symbol.asyncIterator]: () => {
|
|
499
520
|
const reader = readable.getReader();
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const [_chunkId, status, data] = value as IterableChunk;
|
|
510
|
-
|
|
511
|
-
switch (status) {
|
|
512
|
-
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
513
|
-
yield hydrate(data);
|
|
514
|
-
break;
|
|
515
|
-
case ASYNC_ITERABLE_STATUS_DONE:
|
|
516
|
-
controllers.delete(chunkId);
|
|
517
|
-
return;
|
|
518
|
-
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
521
|
+
const iterator: AsyncIterator<unknown> = {
|
|
522
|
+
next: async () => {
|
|
523
|
+
const { done, value } = await reader.read();
|
|
524
|
+
if (value instanceof StreamInterruptedError) {
|
|
525
|
+
throw value;
|
|
526
|
+
}
|
|
527
|
+
if (done) {
|
|
519
528
|
controllers.delete(chunkId);
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
529
|
+
maybeAbort();
|
|
530
|
+
return {
|
|
531
|
+
done: true,
|
|
532
|
+
value: undefined,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const [_chunkId, status, data] = value as IterableChunk;
|
|
537
|
+
|
|
538
|
+
switch (status) {
|
|
539
|
+
case ASYNC_ITERABLE_STATUS_VALUE:
|
|
540
|
+
return {
|
|
541
|
+
done: false,
|
|
542
|
+
value: hydrate(data),
|
|
543
|
+
};
|
|
544
|
+
case ASYNC_ITERABLE_STATUS_DONE:
|
|
545
|
+
controllers.delete(chunkId);
|
|
546
|
+
maybeAbort();
|
|
547
|
+
return {
|
|
548
|
+
done: true,
|
|
549
|
+
value: undefined,
|
|
550
|
+
};
|
|
551
|
+
case ASYNC_ITERABLE_STATUS_ERROR:
|
|
552
|
+
controllers.delete(chunkId);
|
|
553
|
+
maybeAbort();
|
|
554
|
+
throw (
|
|
555
|
+
opts.formatError?.({ error: data }) ??
|
|
556
|
+
new AsyncError(data)
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
return: async () => {
|
|
561
|
+
wrapper.returned = true;
|
|
562
|
+
maybeAbort();
|
|
563
|
+
return {
|
|
564
|
+
done: true,
|
|
565
|
+
value: undefined,
|
|
566
|
+
};
|
|
567
|
+
},
|
|
568
|
+
};
|
|
569
|
+
return iterator;
|
|
525
570
|
},
|
|
526
571
|
};
|
|
527
572
|
}
|
|
@@ -532,14 +577,14 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
532
577
|
const [[data], ...asyncProps] = value;
|
|
533
578
|
|
|
534
579
|
for (const value of asyncProps) {
|
|
580
|
+
const [key] = value;
|
|
535
581
|
const hydrated = hydrateChunkDefinition(value);
|
|
536
582
|
|
|
537
|
-
|
|
538
|
-
if (path === null) {
|
|
583
|
+
if (key === null) {
|
|
539
584
|
return hydrated;
|
|
540
585
|
}
|
|
541
586
|
|
|
542
|
-
(data as any)[
|
|
587
|
+
(data as any)[key] = hydrated;
|
|
543
588
|
}
|
|
544
589
|
return data;
|
|
545
590
|
}
|
|
@@ -552,7 +597,7 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
552
597
|
deferred.reject(error);
|
|
553
598
|
}
|
|
554
599
|
chunkDeferred.clear();
|
|
555
|
-
for (const controller of controllers.values()) {
|
|
600
|
+
for (const { controller } of controllers.values()) {
|
|
556
601
|
controller.enqueue(error);
|
|
557
602
|
controller.close();
|
|
558
603
|
}
|
|
@@ -575,17 +620,17 @@ export async function jsonlStreamConsumer<THead>(opts: {
|
|
|
575
620
|
}
|
|
576
621
|
const chunk = chunkOrHead as ChunkData;
|
|
577
622
|
const [idx] = chunk;
|
|
578
|
-
let
|
|
579
|
-
if (!
|
|
623
|
+
let wrapper = controllers.get(idx);
|
|
624
|
+
if (!wrapper) {
|
|
580
625
|
let deferred = chunkDeferred.get(idx);
|
|
581
626
|
if (!deferred) {
|
|
582
|
-
deferred = createDeferred
|
|
627
|
+
deferred = createDeferred();
|
|
583
628
|
chunkDeferred.set(idx, deferred);
|
|
584
629
|
}
|
|
585
630
|
|
|
586
|
-
|
|
631
|
+
wrapper = await deferred.promise;
|
|
587
632
|
}
|
|
588
|
-
controller.enqueue(chunk);
|
|
633
|
+
wrapper.controller.enqueue(chunk);
|
|
589
634
|
},
|
|
590
635
|
close: closeOrAbort,
|
|
591
636
|
abort: closeOrAbort,
|
|
@@ -34,6 +34,7 @@ export * from './unstable-core-do-not-import/router';
|
|
|
34
34
|
export * from './unstable-core-do-not-import/rpc';
|
|
35
35
|
export * from './unstable-core-do-not-import/stream/jsonl';
|
|
36
36
|
export * from './unstable-core-do-not-import/stream/sse';
|
|
37
|
+
export * from './unstable-core-do-not-import/stream/utils/createDeferred';
|
|
37
38
|
export * from './unstable-core-do-not-import/transformer';
|
|
38
39
|
export * from './unstable-core-do-not-import/types';
|
|
39
40
|
export * from './unstable-core-do-not-import/utils';
|