@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.
- package/dist/batch-response-compression-handler-plugin.d.ts +30 -29
- package/dist/batch-response-compression-handler-plugin.js +104 -125
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -3
- package/dist/multipart.js +160 -211
- package/dist/static-file-handler-plugin.d.ts +74 -74
- package/dist/static-file-handler-plugin.js +175 -200
- package/dist/tmp-file-upload-handler-plugin.d.ts +92 -101
- package/dist/tmp-file-upload-handler-plugin.js +379 -451
- package/package.json +7 -7
- package/dist/multipart.d.ts +0 -60
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
|
2
|
-
import { constants, createDeflate, createDeflateRaw, createGzip } from
|
|
3
|
-
import { BATCH_HEADER, negotiateResponseCompressionEncoding, toCompressedHeaders
|
|
4
|
-
import { toArray } from
|
|
5
|
-
import { flattenStandardHeader } from
|
|
6
|
-
import { toFetchBody } from
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
2
|
-
export
|
|
3
|
-
export
|
|
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";
|