@orpc/node 2.0.0-beta.49 → 2.0.0-beta.50

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.
@@ -1,22 +1,23 @@
1
- import type { Context } from '@orpc/server';
2
- import type { StandardHandlerOptions, StandardHandlerPlugin } from '@orpc/server/standard';
1
+ import { CompressionEncoding } from "@orpc/shared";
2
+ import { Context } from "@orpc/server";
3
+ import { StandardHandlerOptions, StandardHandlerPlugin } from "@orpc/server/standard";
3
4
  export interface BatchResponseCompressionHandlerPluginOptions {
4
- /**
5
- * The compression schemes to use for batch responses.
6
- * Schemes are prioritized by their order in this array and
7
- * only applied if the client supports them (via Accept-Encoding).
8
- *
9
- * @default ['gzip', 'deflate']
10
- */
11
- encodings?: readonly ('gzip' | 'deflate' | 'deflate-raw')[] | undefined;
12
- /**
13
- * The minimum response size in bytes required to trigger compression.
14
- * Responses smaller than this threshold will not be compressed to avoid overhead.
15
- * A streaming batch response has no size until it ends, so it is always compressed.
16
- *
17
- * @default 1024 (1KB)
18
- */
19
- threshold?: number | undefined;
5
+ /**
6
+ * The compression schemes to use for batch responses.
7
+ * Schemes are prioritized by their order in this array and
8
+ * only applied if the client supports them (via Accept-Encoding).
9
+ *
10
+ * @default ['gzip', 'deflate']
11
+ */
12
+ encodings?: readonly CompressionEncoding[] | undefined;
13
+ /**
14
+ * The minimum response size in bytes required to trigger compression.
15
+ * Responses smaller than this threshold will not be compressed to avoid overhead.
16
+ * A streaming batch response has no size until it ends, so it is always compressed.
17
+ *
18
+ * @default 1024 (1KB)
19
+ */
20
+ threshold?: number | undefined;
20
21
  }
21
22
  /**
22
23
  * Compresses every successful [batch](https://orpc.dev/docs/plugins/batch) response, which the
@@ -33,14 +34,14 @@ export interface BatchResponseCompressionHandlerPluginOptions {
33
34
  * @see {@link https://orpc.dev/docs/plugins/batch-response-compression | Batch Response Compression Plugin}
34
35
  */
35
36
  export declare class BatchResponseCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
36
- name: string;
37
- /**
38
- * Compression should be done after batching, to compress the assembled batch response
39
- * rather than each subresponse inside it.
40
- */
41
- after: string[];
42
- private readonly encodings;
43
- private readonly threshold;
44
- constructor(options?: BatchResponseCompressionHandlerPluginOptions);
45
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
46
- }
37
+ name: string;
38
+ /**
39
+ * Compression should be done after batching, to compress the assembled batch response
40
+ * rather than each subresponse inside it.
41
+ */
42
+ after: string[];
43
+ private readonly encodings;
44
+ private readonly threshold;
45
+ constructor(options?: BatchResponseCompressionHandlerPluginOptions);
46
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
47
+ }
@@ -1,129 +1,108 @@
1
- import { Duplex } from 'node:stream';
2
- import { constants, createDeflate, createDeflateRaw, createGzip } from 'node:zlib';
3
- import { BATCH_HEADER, negotiateResponseCompressionEncoding, toCompressedHeaders, } from '@orpc/server/plugins';
4
- import { toArray } from '@orpc/shared';
5
- import { flattenStandardHeader } from '@standard-server/core';
6
- import { toFetchBody } from '@standard-server/fetch';
1
+ import { Duplex } from "node:stream";
2
+ import { constants, createDeflate, createDeflateRaw, createGzip } from "node:zlib";
3
+ import { BATCH_HEADER, negotiateResponseCompressionEncoding, toCompressedHeaders } from "@orpc/server/plugins";
4
+ import { toArray } from "@orpc/shared";
5
+ import { flattenStandardHeader } from "@standard-server/core";
6
+ import { toFetchBody } from "@standard-server/fetch";
7
+ // src/batch-response-compression-handler-plugin.ts
7
8
  /**
8
- * Compresses every successful [batch](https://orpc.dev/docs/plugins/batch) response, which the
9
- * runtime-agnostic {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
10
- * leaves untouched when framed: a batch frames subresponses of mixed content types, so its envelope
11
- * has none of its own and nothing says whether the contents compress. Registering this plugin
12
- * states that they do.
13
- *
14
- * @remarks
15
- * Each message is flushed as soon as it is produced, so a streaming batch stays streaming.
16
- * This is why the plugin lives in the Node.js package: `zlib` can flush mid-stream, while the
17
- * web `CompressionStream` cannot, and would hold every early message until the batch ends.
18
- *
19
- * @see {@link https://orpc.dev/docs/plugins/batch-response-compression | Batch Response Compression Plugin}
20
- */
21
- export class BatchResponseCompressionHandlerPlugin {
22
- name = '~batch-response-compression';
23
- /**
24
- * Compression should be done after batching, to compress the assembled batch response
25
- * rather than each subresponse inside it.
26
- */
27
- after = ['~batch'];
28
- encodings;
29
- threshold;
30
- constructor(options = {}) {
31
- this.encodings = options.encodings ?? ['gzip', 'deflate'];
32
- this.threshold = options.threshold ?? 1024;
33
- }
34
- init(options) {
35
- const routingInterceptor = async ({ next, ...interceptorOptions }) => {
36
- const result = await next();
37
- if (!result.matched) {
38
- return result;
39
- }
40
- /**
41
- * The batch plugin only engages on a request carrying this header, so a response framed like
42
- * a batch without it belongs to a procedure serving that content type itself, whose own file
43
- * semantics must survive untouched.
44
- */
45
- if (flattenStandardHeader(interceptorOptions.request.headers[BATCH_HEADER]) === undefined) {
46
- return result;
47
- }
48
- const response = result.response;
49
- const headers = response.headers;
50
- const body = response.body;
51
- /**
52
- * A batch that failed as a whole answers with a plain message instead of subresponses,
53
- * so only a successful envelope is a batch payload worth compressing.
54
- */
55
- if (response.status >= 400) {
56
- return result;
57
- }
58
- const encoding = negotiateResponseCompressionEncoding(response, interceptorOptions.request.headers, this.encodings);
59
- if (encoding === undefined) {
60
- return result;
61
- }
62
- let stream;
63
- /**
64
- * A framed body is opaque bytes, while a json one still has to be parsed as json,
65
- * so each keeps the hint that tells the client how to read it.
66
- */
67
- let bodyHeaders;
68
- /**
69
- * A buffered batch with no binary or stream subresponse stays a plain array of messages, serialized
70
- * as json by the adapter. The header and status above already say this is a batch envelope,
71
- * so the array itself is the only thing left to recognise.
72
- */
73
- if (Array.isArray(body)) {
74
- // Serialized here rather than by the adapter, which no longer sees the array
75
- const [string, jsonHeaders] = toFetchBody(body, headers);
76
- const json = new Blob([string]);
77
- if (json.size < this.threshold) {
78
- return result;
79
- }
80
- stream = json.stream();
81
- bodyHeaders = {
82
- 'content-type': jsonHeaders['content-type'],
83
- // An empty hint rather than none, so the adapter does not hint the compressed stream as `octet-stream`
84
- 'standard-server': jsonHeaders['standard-server'] ?? [],
85
- };
86
- }
87
- // Every other batch body is the length-prefixed framing, buffered into a blob or streamed
88
- else if (body instanceof Blob || body instanceof ReadableStream) {
89
- if (body instanceof Blob && Number.isFinite(body.size) && body.size < this.threshold) {
90
- return result;
91
- }
92
- stream = body instanceof Blob ? body.stream() : body;
93
- bodyHeaders = { 'standard-server': 'octet-stream' };
94
- }
95
- else {
96
- return result;
97
- }
98
- return {
99
- ...result,
100
- response: {
101
- ...response,
102
- body: stream.pipeThrough(createFlushingCompressionStream(encoding)),
103
- headers: {
104
- ...toCompressedHeaders(headers, encoding),
105
- ...bodyHeaders,
106
- },
107
- },
108
- };
109
- };
110
- return {
111
- ...options,
112
- routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
113
- };
114
- }
115
- }
9
+ * Compresses every successful [batch](https://orpc.dev/docs/plugins/batch) response, which the
10
+ * runtime-agnostic {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
11
+ * leaves untouched when framed: a batch frames subresponses of mixed content types, so its envelope
12
+ * has none of its own and nothing says whether the contents compress. Registering this plugin
13
+ * states that they do.
14
+ *
15
+ * @remarks
16
+ * Each message is flushed as soon as it is produced, so a streaming batch stays streaming.
17
+ * This is why the plugin lives in the Node.js package: `zlib` can flush mid-stream, while the
18
+ * web `CompressionStream` cannot, and would hold every early message until the batch ends.
19
+ *
20
+ * @see {@link https://orpc.dev/docs/plugins/batch-response-compression | Batch Response Compression Plugin}
21
+ */
22
+ var BatchResponseCompressionHandlerPlugin = class {
23
+ name = "~batch-response-compression";
24
+ /**
25
+ * Compression should be done after batching, to compress the assembled batch response
26
+ * rather than each subresponse inside it.
27
+ */
28
+ after = ["~batch"];
29
+ encodings;
30
+ threshold;
31
+ constructor(options = {}) {
32
+ this.encodings = options.encodings ?? ["gzip", "deflate"];
33
+ this.threshold = options.threshold ?? 1024;
34
+ }
35
+ init(options) {
36
+ const routingInterceptor = async ({ next, ...interceptorOptions }) => {
37
+ const result = await next();
38
+ if (!result.matched) return result;
39
+ /**
40
+ * The batch plugin only engages on a request carrying this header, so a response framed like
41
+ * a batch without it belongs to a procedure serving that content type itself, whose own file
42
+ * semantics must survive untouched.
43
+ */
44
+ if (flattenStandardHeader(interceptorOptions.request.headers[BATCH_HEADER]) === void 0) return result;
45
+ const response = result.response;
46
+ const headers = response.headers;
47
+ const body = response.body;
48
+ /**
49
+ * A batch that failed as a whole answers with a plain message instead of subresponses,
50
+ * so only a successful envelope is a batch payload worth compressing.
51
+ */
52
+ if (response.status >= 400) return result;
53
+ const encoding = negotiateResponseCompressionEncoding(response, interceptorOptions.request.headers, this.encodings);
54
+ if (encoding === void 0) return result;
55
+ let stream;
56
+ /**
57
+ * A framed body is opaque bytes, while a json one still has to be parsed as json,
58
+ * so each keeps the hint that tells the client how to read it.
59
+ */
60
+ let bodyHeaders;
61
+ /**
62
+ * A buffered batch with no binary or stream subresponse stays a plain array of messages, serialized
63
+ * as json by the adapter. The header and status above already say this is a batch envelope,
64
+ * so the array itself is the only thing left to recognise.
65
+ */
66
+ if (Array.isArray(body)) {
67
+ const [string, jsonHeaders] = toFetchBody(body, headers);
68
+ const json = new Blob([string]);
69
+ if (json.size < this.threshold) return result;
70
+ stream = json.stream();
71
+ bodyHeaders = {
72
+ "content-type": jsonHeaders["content-type"],
73
+ "standard-server": jsonHeaders["standard-server"] ?? []
74
+ };
75
+ } else if (body instanceof Blob || body instanceof ReadableStream) {
76
+ if (body instanceof Blob && Number.isFinite(body.size) && body.size < this.threshold) return result;
77
+ stream = body instanceof Blob ? body.stream() : body;
78
+ bodyHeaders = { "standard-server": "octet-stream" };
79
+ } else return result;
80
+ return {
81
+ ...result,
82
+ response: {
83
+ ...response,
84
+ body: stream.pipeThrough(createFlushingCompressionStream(encoding)),
85
+ headers: {
86
+ ...toCompressedHeaders(headers, encoding),
87
+ ...bodyHeaders
88
+ }
89
+ }
90
+ };
91
+ };
92
+ return {
93
+ ...options,
94
+ routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)]
95
+ };
96
+ }
97
+ };
116
98
  /**
117
- * A compressor that ends every write with a sync flush, so each batch message reaches the client
118
- * as soon as the batch produces it instead of waiting in the compressor for the batch to end.
119
- * The few bytes each flush costs are what keep a streaming batch streaming.
120
- */
99
+ * A compressor that ends every write with a sync flush, so each batch message reaches the client
100
+ * as soon as the batch produces it instead of waiting in the compressor for the batch to end.
101
+ * The few bytes each flush costs are what keep a streaming batch streaming.
102
+ */
121
103
  function createFlushingCompressionStream(encoding) {
122
- const zlibOptions = { flush: constants.Z_SYNC_FLUSH };
123
- const compressor = encoding === 'gzip'
124
- ? createGzip(zlibOptions)
125
- : encoding === 'deflate'
126
- ? createDeflate(zlibOptions)
127
- : createDeflateRaw(zlibOptions);
128
- return Duplex.toWeb(compressor);
104
+ const zlibOptions = { flush: constants.Z_SYNC_FLUSH };
105
+ const compressor = encoding === "gzip" ? createGzip(zlibOptions) : encoding === "deflate" ? createDeflate(zlibOptions) : createDeflateRaw(zlibOptions);
106
+ return Duplex.toWeb(compressor);
129
107
  }
108
+ export { BatchResponseCompressionHandlerPlugin };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export * from './batch-response-compression-handler-plugin.ts';
2
- export * from './static-file-handler-plugin.ts';
3
- export * from './tmp-file-upload-handler-plugin.ts';
1
+ import { BatchResponseCompressionHandlerPlugin, BatchResponseCompressionHandlerPluginOptions } from "./batch-response-compression-handler-plugin.js";
2
+ import { StaticFileHandlerPlugin, StaticFileHandlerPluginOptions } from "./static-file-handler-plugin.js";
3
+ import { TmpFile, TmpFileUploadHandlerPlugin, TmpFileUploadHandlerPluginMaxBodySize, TmpFileUploadHandlerPluginOptions } from "./tmp-file-upload-handler-plugin.js";
4
+ export { BatchResponseCompressionHandlerPlugin, BatchResponseCompressionHandlerPluginOptions, StaticFileHandlerPlugin, StaticFileHandlerPluginOptions, TmpFile, TmpFileUploadHandlerPlugin, TmpFileUploadHandlerPluginMaxBodySize, TmpFileUploadHandlerPluginOptions };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from './batch-response-compression-handler-plugin.js';
2
- export * from './static-file-handler-plugin.js';
3
- export * from './tmp-file-upload-handler-plugin.js';
1
+ export { BatchResponseCompressionHandlerPlugin } from "./batch-response-compression-handler-plugin.js";
2
+ export { StaticFileHandlerPlugin } from "./static-file-handler-plugin.js";
3
+ export { TmpFile, TmpFileUploadHandlerPlugin } from "./tmp-file-upload-handler-plugin.js";