@trpc/server 10.29.0 → 10.30.0

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.
Files changed (39) hide show
  1. package/dist/adapters/aws-lambda/index.js +1 -1
  2. package/dist/adapters/aws-lambda/index.mjs +1 -1
  3. package/dist/adapters/express.js +2 -2
  4. package/dist/adapters/express.mjs +2 -2
  5. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  6. package/dist/adapters/fastify/index.js +17 -16
  7. package/dist/adapters/fastify/index.mjs +17 -16
  8. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  9. package/dist/adapters/fetch/index.js +24 -23
  10. package/dist/adapters/fetch/index.mjs +24 -23
  11. package/dist/adapters/next.js +2 -2
  12. package/dist/adapters/next.mjs +2 -2
  13. package/dist/adapters/node-http/index.js +2 -2
  14. package/dist/adapters/node-http/index.mjs +2 -2
  15. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  16. package/dist/adapters/node-http/types.d.ts +11 -1
  17. package/dist/adapters/node-http/types.d.ts.map +1 -1
  18. package/dist/adapters/standalone.js +2 -2
  19. package/dist/adapters/standalone.mjs +2 -2
  20. package/dist/http/index.js +1 -1
  21. package/dist/http/index.mjs +1 -1
  22. package/dist/http/internals/types.d.ts +1 -1
  23. package/dist/http/internals/types.d.ts.map +1 -1
  24. package/dist/http/resolveHTTPResponse.d.ts +10 -9
  25. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  26. package/dist/{nodeHTTPRequestHandler-e637755a.js → nodeHTTPRequestHandler-071d36b5.js} +21 -19
  27. package/dist/{nodeHTTPRequestHandler-53b9fc2d.js → nodeHTTPRequestHandler-51d58a23.js} +21 -18
  28. package/dist/{nodeHTTPRequestHandler-bdad2781.mjs → nodeHTTPRequestHandler-bb12529f.mjs} +21 -19
  29. package/dist/{resolveHTTPResponse-2c307e04.js → resolveHTTPResponse-1f03fdfd.js} +11 -11
  30. package/dist/{resolveHTTPResponse-a9be3d96.mjs → resolveHTTPResponse-dd3677b3.mjs} +11 -11
  31. package/dist/{resolveHTTPResponse-2bdf2cd7.js → resolveHTTPResponse-e0047ea2.js} +12 -12
  32. package/package.json +3 -3
  33. package/src/adapters/fastify/fastifyRequestHandler.ts +18 -17
  34. package/src/adapters/fetch/fetchRequestHandler.ts +23 -22
  35. package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
  36. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +23 -21
  37. package/src/adapters/node-http/types.ts +11 -1
  38. package/src/http/internals/types.ts +1 -1
  39. 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
- const onHead = (head: HTTPResponse) => {
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 formatter = getBatchStreamFormatter();
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
- return;
94
- }
95
- if (!isStream) {
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
- onHead,
124
- onChunk,
126
+ unstable_onHead,
127
+ unstable_onChunk,
125
128
  });
126
129
 
127
- if (!isStream) {
128
- return opts.res;
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,
@@ -36,5 +36,5 @@ export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
36
36
  * generated without knowing the response data
37
37
  * (e.g. for streaming requests).
38
38
  */
39
- eagerGeneration: boolean;
39
+ eagerGeneration?: boolean;
40
40
  }) => ResponseMeta;
@@ -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
- * with headers that were generated **without** knowing the response body.
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
- onHead: (headResponse: Omit<HTTPResponse, 'body'>) => void;
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
- onChunk: (chunk: ResponseChunk) => void;
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.onHead `undefined`
227
- * @param opts.onChunk `undefined`
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
- 'onHead' | 'onChunk'
240
+ 'unstable_onHead' | 'unstable_onChunk'
237
241
  >,
238
242
  ): Promise<HTTPResponse>;
239
243
  /**
240
244
  * Streaming signature for `resolveHTTPResponse`:
241
- * @param opts.onHead called as soon as the response head is known
242
- * @param opts.onChunk called for every procedure with `[index, result]`
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
- 'onHead' | 'onChunk'
260
+ 'unstable_onHead' | 'unstable_onChunk'
257
261
  >,
258
262
  ): Promise<HTTPResponse | void> {
259
- const { router, req, onHead, onChunk } = opts;
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
- onHead?.(headResponse);
267
- onChunk?.([-1, '']);
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
- onHead &&
282
- onChunk &&
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
- onHead?.(headResponse);
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
- onChunk?.([-1, body]);
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
- onHead(headResponse);
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
- onChunk([index, body]);
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
- onChunk([index, body]);
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
- onHead?.(headResponse);
433
+ unstable_onHead?.(headResponse, false);
430
434
 
431
- onChunk?.([-1, body]);
435
+ unstable_onChunk?.([-1, body]);
432
436
 
433
437
  return {
434
438
  status: headResponse.status,