@trpc/server 10.29.0 → 10.29.1
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/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/index.mjs +1 -1
- package/dist/adapters/express.js +2 -2
- package/dist/adapters/express.mjs +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +17 -16
- package/dist/adapters/fastify/index.mjs +17 -16
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +24 -23
- package/dist/adapters/fetch/index.mjs +24 -23
- package/dist/adapters/next.js +2 -2
- package/dist/adapters/next.mjs +2 -2
- package/dist/adapters/node-http/index.js +2 -2
- package/dist/adapters/node-http/index.mjs +2 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +11 -1
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.js +2 -2
- package/dist/adapters/standalone.mjs +2 -2
- package/dist/http/index.js +1 -1
- package/dist/http/index.mjs +1 -1
- package/dist/http/internals/types.d.ts +1 -1
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +10 -9
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-e637755a.js → nodeHTTPRequestHandler-071d36b5.js} +21 -19
- package/dist/{nodeHTTPRequestHandler-53b9fc2d.js → nodeHTTPRequestHandler-51d58a23.js} +21 -18
- package/dist/{nodeHTTPRequestHandler-bdad2781.mjs → nodeHTTPRequestHandler-bb12529f.mjs} +21 -19
- package/dist/{resolveHTTPResponse-2c307e04.js → resolveHTTPResponse-1f03fdfd.js} +11 -11
- package/dist/{resolveHTTPResponse-a9be3d96.mjs → resolveHTTPResponse-dd3677b3.mjs} +11 -11
- package/dist/{resolveHTTPResponse-2bdf2cd7.js → resolveHTTPResponse-e0047ea2.js} +12 -12
- package/package.json +2 -2
- package/src/adapters/fastify/fastifyRequestHandler.ts +18 -17
- package/src/adapters/fetch/fetchRequestHandler.ts +23 -22
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +23 -21
- package/src/adapters/node-http/types.ts +11 -1
- package/src/http/internals/types.ts +1 -1
- package/src/http/resolveHTTPResponse.ts +26 -22
|
@@ -64,7 +64,9 @@ export async function nodeHTTPRequestHandler<
|
|
|
64
64
|
body: bodyResult.ok ? bodyResult.data : undefined,
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
let isStream = false;
|
|
68
|
+
let formatter: ReturnType<typeof getBatchStreamFormatter>;
|
|
69
|
+
const unstable_onHead = (head: HTTPResponse, isStreaming: boolean) => {
|
|
68
70
|
if (
|
|
69
71
|
'status' in head &&
|
|
70
72
|
(!opts.res.statusCode || opts.res.statusCode === 200)
|
|
@@ -78,11 +80,20 @@ export async function nodeHTTPRequestHandler<
|
|
|
78
80
|
}
|
|
79
81
|
opts.res.setHeader(key, value);
|
|
80
82
|
}
|
|
83
|
+
if (isStreaming) {
|
|
84
|
+
opts.res.setHeader('Transfer-Encoding', 'chunked');
|
|
85
|
+
const vary = opts.res.getHeader('Vary');
|
|
86
|
+
opts.res.setHeader(
|
|
87
|
+
'Vary',
|
|
88
|
+
vary ? 'trpc-batch-mode, ' + vary : 'trpc-batch-mode',
|
|
89
|
+
);
|
|
90
|
+
isStream = true;
|
|
91
|
+
formatter = getBatchStreamFormatter();
|
|
92
|
+
opts.res.flushHeaders();
|
|
93
|
+
}
|
|
81
94
|
};
|
|
82
95
|
|
|
83
|
-
const
|
|
84
|
-
let isStream = false;
|
|
85
|
-
const onChunk = ([index, string]: ResponseChunk) => {
|
|
96
|
+
const unstable_onChunk = ([index, string]: ResponseChunk) => {
|
|
86
97
|
if (index === -1) {
|
|
87
98
|
/**
|
|
88
99
|
* Full response, no streaming. This can happen
|
|
@@ -90,18 +101,10 @@ export async function nodeHTTPRequestHandler<
|
|
|
90
101
|
* - if response is empty (HEAD request)
|
|
91
102
|
*/
|
|
92
103
|
opts.res.end(string);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
opts.res.setHeader('Transfer-Encoding', 'chunked');
|
|
97
|
-
const vary = opts.res.getHeader('Vary');
|
|
98
|
-
opts.res.setHeader(
|
|
99
|
-
'Vary',
|
|
100
|
-
vary ? 'trpc-batch-mode, ' + vary : 'trpc-batch-mode',
|
|
101
|
-
);
|
|
102
|
-
isStream = true;
|
|
104
|
+
} else {
|
|
105
|
+
opts.res.write(formatter!(index, string));
|
|
106
|
+
opts.res.flush?.();
|
|
103
107
|
}
|
|
104
|
-
opts.res.write(formatter(index, string));
|
|
105
108
|
};
|
|
106
109
|
|
|
107
110
|
await resolveHTTPResponse({
|
|
@@ -120,16 +123,15 @@ export async function nodeHTTPRequestHandler<
|
|
|
120
123
|
});
|
|
121
124
|
},
|
|
122
125
|
contentTypeHandler,
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
unstable_onHead,
|
|
127
|
+
unstable_onChunk,
|
|
125
128
|
});
|
|
126
129
|
|
|
127
|
-
if (
|
|
128
|
-
|
|
130
|
+
if (isStream) {
|
|
131
|
+
opts.res.write(formatter!.end());
|
|
132
|
+
opts.res.end();
|
|
129
133
|
}
|
|
130
134
|
|
|
131
|
-
opts.res.write(formatter.end());
|
|
132
|
-
opts.res.end();
|
|
133
135
|
return opts.res;
|
|
134
136
|
});
|
|
135
137
|
}
|
|
@@ -12,7 +12,17 @@ export type NodeHTTPRequest = IncomingMessage & {
|
|
|
12
12
|
query?: ParsedQs;
|
|
13
13
|
body?: unknown;
|
|
14
14
|
};
|
|
15
|
-
export type NodeHTTPResponse = ServerResponse
|
|
15
|
+
export type NodeHTTPResponse = ServerResponse & {
|
|
16
|
+
/**
|
|
17
|
+
* Force the partially-compressed response to be flushed to the client.
|
|
18
|
+
*
|
|
19
|
+
* Added by compression middleware
|
|
20
|
+
* (depending on the environment,
|
|
21
|
+
* e.g. Next <= 12,
|
|
22
|
+
* e.g. Express w/ `compression()`)
|
|
23
|
+
*/
|
|
24
|
+
flush?: () => void;
|
|
25
|
+
};
|
|
16
26
|
|
|
17
27
|
export type NodeHTTPCreateContextOption<
|
|
18
28
|
TRouter extends AnyRouter,
|
|
@@ -47,12 +47,16 @@ interface ResolveHTTPRequestOptions<
|
|
|
47
47
|
contentTypeHandler?: BaseContentTypeHandler<any>;
|
|
48
48
|
preprocessedBody?: boolean;
|
|
49
49
|
/**
|
|
50
|
-
* Called as soon as the response head is known
|
|
51
|
-
*
|
|
50
|
+
* Called as soon as the response head is known.
|
|
51
|
+
* When streaming, headers will have been generated
|
|
52
|
+
* **without** knowing the response body.
|
|
52
53
|
*
|
|
53
54
|
* Without this callback, streaming is disabled.
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
unstable_onHead: (
|
|
57
|
+
headResponse: Omit<HTTPResponse, 'body'>,
|
|
58
|
+
isStreaming: boolean,
|
|
59
|
+
) => void;
|
|
56
60
|
/**
|
|
57
61
|
* Called for every procedure with `[index, result]`.
|
|
58
62
|
*
|
|
@@ -62,7 +66,7 @@ interface ResolveHTTPRequestOptions<
|
|
|
62
66
|
*
|
|
63
67
|
* Without this callback, streaming is disabled.
|
|
64
68
|
*/
|
|
65
|
-
|
|
69
|
+
unstable_onChunk: (chunk: ResponseChunk) => void;
|
|
66
70
|
}
|
|
67
71
|
|
|
68
72
|
function initResponse<
|
|
@@ -223,8 +227,8 @@ function caughtErrorToData<
|
|
|
223
227
|
|
|
224
228
|
/**
|
|
225
229
|
* Non-streaming signature for `resolveHTTPResponse`:
|
|
226
|
-
* @param opts.
|
|
227
|
-
* @param opts.
|
|
230
|
+
* @param opts.unstable_onHead `undefined`
|
|
231
|
+
* @param opts.unstable_onChunk `undefined`
|
|
228
232
|
* @returns `Promise<HTTPResponse>`
|
|
229
233
|
*/
|
|
230
234
|
export async function resolveHTTPResponse<
|
|
@@ -233,13 +237,13 @@ export async function resolveHTTPResponse<
|
|
|
233
237
|
>(
|
|
234
238
|
opts: Omit<
|
|
235
239
|
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
236
|
-
'
|
|
240
|
+
'unstable_onHead' | 'unstable_onChunk'
|
|
237
241
|
>,
|
|
238
242
|
): Promise<HTTPResponse>;
|
|
239
243
|
/**
|
|
240
244
|
* Streaming signature for `resolveHTTPResponse`:
|
|
241
|
-
* @param opts.
|
|
242
|
-
* @param opts.
|
|
245
|
+
* @param opts.unstable_onHead called as soon as the response head is known
|
|
246
|
+
* @param opts.unstable_onChunk called for every procedure with `[index, result]`
|
|
243
247
|
* @returns `Promise<void>` since the response is streamed
|
|
244
248
|
*/
|
|
245
249
|
export async function resolveHTTPResponse<
|
|
@@ -253,18 +257,18 @@ export async function resolveHTTPResponse<
|
|
|
253
257
|
>(
|
|
254
258
|
opts: PartialBy<
|
|
255
259
|
ResolveHTTPRequestOptions<TRouter, TRequest>,
|
|
256
|
-
'
|
|
260
|
+
'unstable_onHead' | 'unstable_onChunk'
|
|
257
261
|
>,
|
|
258
262
|
): Promise<HTTPResponse | void> {
|
|
259
|
-
const { router, req,
|
|
263
|
+
const { router, req, unstable_onHead, unstable_onChunk } = opts;
|
|
260
264
|
|
|
261
265
|
if (req.method === 'HEAD') {
|
|
262
266
|
// can be used for lambda warmup
|
|
263
267
|
const headResponse: HTTPResponse = {
|
|
264
268
|
status: 204,
|
|
265
269
|
};
|
|
266
|
-
|
|
267
|
-
|
|
270
|
+
unstable_onHead?.(headResponse, false);
|
|
271
|
+
unstable_onChunk?.([-1, '']);
|
|
268
272
|
return headResponse;
|
|
269
273
|
}
|
|
270
274
|
const contentTypeHandler =
|
|
@@ -278,8 +282,8 @@ export async function resolveHTTPResponse<
|
|
|
278
282
|
const isBatchCall = !!req.query.get('batch');
|
|
279
283
|
const isStreamCall =
|
|
280
284
|
isBatchCall &&
|
|
281
|
-
|
|
282
|
-
|
|
285
|
+
unstable_onHead &&
|
|
286
|
+
unstable_onChunk &&
|
|
283
287
|
req.headers['trpc-batch-mode'] === 'stream';
|
|
284
288
|
|
|
285
289
|
try {
|
|
@@ -337,7 +341,7 @@ export async function resolveHTTPResponse<
|
|
|
337
341
|
untransformedJSON,
|
|
338
342
|
errors,
|
|
339
343
|
});
|
|
340
|
-
|
|
344
|
+
unstable_onHead?.(headResponse, false);
|
|
341
345
|
|
|
342
346
|
// return body stuff
|
|
343
347
|
const result = isBatchCall ? untransformedJSON : untransformedJSON[0]!; // eslint-disable-line @typescript-eslint/no-non-null-assertion -- `untransformedJSON` should be the length of `paths` which should be at least 1 otherwise there wouldn't be a request at all
|
|
@@ -346,7 +350,7 @@ export async function resolveHTTPResponse<
|
|
|
346
350
|
result,
|
|
347
351
|
);
|
|
348
352
|
const body = JSON.stringify(transformedJSON);
|
|
349
|
-
|
|
353
|
+
unstable_onChunk?.([-1, body]);
|
|
350
354
|
|
|
351
355
|
return {
|
|
352
356
|
status: headResponse.status,
|
|
@@ -367,7 +371,7 @@ export async function resolveHTTPResponse<
|
|
|
367
371
|
type,
|
|
368
372
|
responseMeta: opts.responseMeta,
|
|
369
373
|
});
|
|
370
|
-
|
|
374
|
+
unstable_onHead(headResponse, true);
|
|
371
375
|
|
|
372
376
|
const indexedPromises = new Map(
|
|
373
377
|
promises.map((promise, index) => [
|
|
@@ -388,7 +392,7 @@ export async function resolveHTTPResponse<
|
|
|
388
392
|
);
|
|
389
393
|
const body = JSON.stringify(transformedJSON);
|
|
390
394
|
|
|
391
|
-
|
|
395
|
+
unstable_onChunk([index, body]);
|
|
392
396
|
} catch (cause) {
|
|
393
397
|
const path = paths[index];
|
|
394
398
|
const input = inputs[index];
|
|
@@ -400,7 +404,7 @@ export async function resolveHTTPResponse<
|
|
|
400
404
|
input,
|
|
401
405
|
});
|
|
402
406
|
|
|
403
|
-
|
|
407
|
+
unstable_onChunk([index, body]);
|
|
404
408
|
}
|
|
405
409
|
}
|
|
406
410
|
return;
|
|
@@ -426,9 +430,9 @@ export async function resolveHTTPResponse<
|
|
|
426
430
|
untransformedJSON,
|
|
427
431
|
errors: [error],
|
|
428
432
|
});
|
|
429
|
-
|
|
433
|
+
unstable_onHead?.(headResponse, false);
|
|
430
434
|
|
|
431
|
-
|
|
435
|
+
unstable_onChunk?.([-1, body]);
|
|
432
436
|
|
|
433
437
|
return {
|
|
434
438
|
status: headResponse.status,
|