@orpc/node 2.0.0-beta.48 → 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/README.md +4 -4
- package/dist/batch-response-compression-handler-plugin.d.ts +47 -0
- package/dist/batch-response-compression-handler-plugin.js +108 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/multipart.js +170 -0
- package/dist/static-file-handler-plugin.d.ts +84 -0
- package/dist/static-file-handler-plugin.js +190 -0
- package/dist/tmp-file-upload-handler-plugin.d.ts +133 -0
- package/dist/{tmp-file-upload-handler-plugin.mjs → tmp-file-upload-handler-plugin.js} +153 -124
- package/package.json +27 -29
- package/dist/batch-response-compression-handler-plugin.d.mts +0 -46
- package/dist/batch-response-compression-handler-plugin.mjs +0 -88
- package/dist/index.d.mts +0 -3
- package/dist/index.mjs +0 -3
- package/dist/multipart.d.mts +0 -60
- package/dist/multipart.mjs +0 -179
- package/dist/static-file-handler-plugin.d.mts +0 -133
- package/dist/static-file-handler-plugin.mjs +0 -354
- package/dist/tmp-file-upload-handler-plugin.d.mts +0 -133
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Promisable, Value } from "@orpc/shared";
|
|
2
|
+
import { FilePropertyBag } from "node:buffer";
|
|
3
|
+
import { Context } from "@orpc/server";
|
|
4
|
+
import { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from "@orpc/server/standard";
|
|
5
|
+
export interface TmpFileUploadHandlerPluginMaxBodySize {
|
|
6
|
+
/**
|
|
7
|
+
* The maximum size in bytes of request body content buffered into memory:
|
|
8
|
+
* JSON, URL-encoded forms, and the fields, part headers, and in-memory files
|
|
9
|
+
* of a multipart body in total, and each event of an event stream on its
|
|
10
|
+
* own. Exceeding it rejects with `PAYLOAD_TOO_LARGE`, except for uploads,
|
|
11
|
+
* which spool to disk instead once they no longer fit. Usually the lowest of
|
|
12
|
+
* the three limits, because this content cannot stream anywhere.
|
|
13
|
+
*/
|
|
14
|
+
memory: number;
|
|
15
|
+
/**
|
|
16
|
+
* The maximum total size in bytes of uploaded file content: file bodies and
|
|
17
|
+
* the file parts of a multipart body combined, whether held in memory or
|
|
18
|
+
* spooled to temporary files. A larger body rejects the request with
|
|
19
|
+
* `PAYLOAD_TOO_LARGE`.
|
|
20
|
+
*/
|
|
21
|
+
file: number;
|
|
22
|
+
/**
|
|
23
|
+
* The maximum total size in bytes of a request body that is consumed as a
|
|
24
|
+
* stream: event streams and raw binary streams. Enforced while the stream is
|
|
25
|
+
* consumed, so an oversized stream fails at the reader. Usually the highest
|
|
26
|
+
* of the three limits, but keep it finite so no client can stream
|
|
27
|
+
* indefinitely.
|
|
28
|
+
*/
|
|
29
|
+
stream: number;
|
|
30
|
+
}
|
|
31
|
+
export interface TmpFileUploadHandlerPluginOptions<T extends Context> {
|
|
32
|
+
/**
|
|
33
|
+
* The directory temporary files are created under. Each request that spools
|
|
34
|
+
* an upload gets its own subdirectory inside it, removed when the request
|
|
35
|
+
* finishes.
|
|
36
|
+
*
|
|
37
|
+
* @default os.tmpdir()
|
|
38
|
+
*/
|
|
39
|
+
tmpDir?: string | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* The size limit for each kind of request body, either fixed or resolved
|
|
42
|
+
* per request, so limits can follow the request itself, such as a larger
|
|
43
|
+
* allowance for an authenticated user than for a guest. Every kind is
|
|
44
|
+
* required when the option is given, so none is left unbounded by accident;
|
|
45
|
+
* set a kind to `Number.POSITIVE_INFINITY` to deliberately leave it
|
|
46
|
+
* unlimited.
|
|
47
|
+
*
|
|
48
|
+
* A resolver runs only when a body is parsed, so a request without one never
|
|
49
|
+
* pays for it, and it receives the request as it arrived, before this plugin
|
|
50
|
+
* wraps its body resolution.
|
|
51
|
+
*
|
|
52
|
+
* @default unlimited for every kind
|
|
53
|
+
*/
|
|
54
|
+
maxBodySize?: Value<Promisable<TmpFileUploadHandlerPluginMaxBodySize>, [options: StandardHandlerRoutingInterceptorOptions<T>]> | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The size in bytes an uploaded file must exceed before it is spooled to a
|
|
57
|
+
* temporary file. Smaller files stay in memory as plain `File`s, counted
|
|
58
|
+
* against `maxBodySize.memory`, and spool anyway once they no longer fit.
|
|
59
|
+
* Set to `0` to spool every non-empty upload.
|
|
60
|
+
*
|
|
61
|
+
* @default 64 * 1024
|
|
62
|
+
*/
|
|
63
|
+
spoolThreshold?: number | undefined;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A `File` whose content lives in a temporary file on disk and is read lazily,
|
|
67
|
+
* so it can represent an upload far larger than available memory.
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* Node's `fs.openAsBlob`, which backs this class, has known bugs with files
|
|
71
|
+
* over 2 GiB ({@link https://github.com/nodejs/node/issues/52585}): `size` is
|
|
72
|
+
* truncated above 4 GiB and `slice()` is unreliable above 2 GiB, while
|
|
73
|
+
* whole-file reads like `stream()` stay correct. When size or random access
|
|
74
|
+
* matters for such a file, stat or read {@link path} directly.
|
|
75
|
+
*
|
|
76
|
+
* @see {@link https://orpc.dev/docs/plugins/tmp-file-upload | Tmp File Upload Plugin}
|
|
77
|
+
*/
|
|
78
|
+
export declare class TmpFile extends File {
|
|
79
|
+
/**
|
|
80
|
+
* Absolute path of the temporary file holding this file's content.
|
|
81
|
+
* The file is removed when the request finishes, so move or copy it
|
|
82
|
+
* to keep the content. The `File` reads from this path, so it must
|
|
83
|
+
* not be read after the file is moved or removed.
|
|
84
|
+
*/
|
|
85
|
+
readonly path: string;
|
|
86
|
+
constructor(blob: Blob, path: string, name: string, options?: FilePropertyBag);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Parses file uploads into temporary files instead of memory. Bodies the standard
|
|
90
|
+
* body parser would buffer into an in-memory `File`, and multipart file parts, are
|
|
91
|
+
* streamed to disk once they outgrow the spool threshold and surfaced as lazily read
|
|
92
|
+
* {@link TmpFile} instances, so requests far larger than available memory parse in
|
|
93
|
+
* constant memory. Smaller uploads stay in memory as plain `File`s. Every other body is
|
|
94
|
+
* left to the standard parser.
|
|
95
|
+
*
|
|
96
|
+
* Request body sizes are limited per content category: buffered in memory, uploaded
|
|
97
|
+
* files, and streamed, each limit fixed or resolved per request. This subsumes the
|
|
98
|
+
* request limit plugin while sizing each kind of body to what it actually costs.
|
|
99
|
+
*
|
|
100
|
+
* @remarks
|
|
101
|
+
* Temporary files are removed when the request finishes. A streaming response body,
|
|
102
|
+
* an event iterator or a raw stream, keeps them alive until it completes, so
|
|
103
|
+
* responses that read the upload while streaming work. Any other response is
|
|
104
|
+
* transmitted after removal, so one that embeds the upload itself, as a `File` or
|
|
105
|
+
* inside `FormData`, needs the content copied or the file moved first.
|
|
106
|
+
*
|
|
107
|
+
* @see {@link https://orpc.dev/docs/plugins/tmp-file-upload | Tmp File Upload Plugin}
|
|
108
|
+
*/
|
|
109
|
+
export declare class TmpFileUploadHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
110
|
+
name: string;
|
|
111
|
+
/**
|
|
112
|
+
* Should parse the original request body instead of batched sub-requests.
|
|
113
|
+
*/
|
|
114
|
+
after: string[];
|
|
115
|
+
/**
|
|
116
|
+
* Should run inside the limit and compression plugins, so spooling receives
|
|
117
|
+
* the size-checked, decompressed bytes.
|
|
118
|
+
*/
|
|
119
|
+
before: string[];
|
|
120
|
+
private readonly tmpDir;
|
|
121
|
+
private readonly maxBodySize;
|
|
122
|
+
private readonly spoolThreshold;
|
|
123
|
+
constructor(options?: TmpFileUploadHandlerPluginOptions<T>);
|
|
124
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
125
|
+
private resolveBody;
|
|
126
|
+
/**
|
|
127
|
+
* Enforces size limits on a body the standard parser handles, counting the
|
|
128
|
+
* raw bytes before handing them back for regular parsing.
|
|
129
|
+
*/
|
|
130
|
+
private parseLimitedBody;
|
|
131
|
+
private parseFileBody;
|
|
132
|
+
private parseMultipartBody;
|
|
133
|
+
}
|
|
@@ -1,27 +1,71 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseHeaderParameters, parseMultipart } from "./multipart.js";
|
|
2
|
+
import { isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream } from "@orpc/shared";
|
|
3
|
+
import { flattenStandardHeader, getFilenameFromContentDisposition, resolveStandardBodyHint } from "@standard-server/core";
|
|
4
|
+
import { toFetchHeaders, toStandardBody } from "@standard-server/fetch";
|
|
2
5
|
import { openAsBlob } from "node:fs";
|
|
3
6
|
import { appendFile, mkdtemp, rm } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
7
|
import path from "node:path";
|
|
8
|
+
import { Buffer } from "node:buffer";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
6
10
|
import { ORPCError } from "@orpc/server";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
// src/tmp-file-upload-handler-plugin.ts
|
|
12
|
+
/**
|
|
13
|
+
* A `File` whose content lives in a temporary file on disk and is read lazily,
|
|
14
|
+
* so it can represent an upload far larger than available memory.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* Node's `fs.openAsBlob`, which backs this class, has known bugs with files
|
|
18
|
+
* over 2 GiB ({@link https://github.com/nodejs/node/issues/52585}): `size` is
|
|
19
|
+
* truncated above 4 GiB and `slice()` is unreliable above 2 GiB, while
|
|
20
|
+
* whole-file reads like `stream()` stay correct. When size or random access
|
|
21
|
+
* matters for such a file, stat or read {@link path} directly.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://orpc.dev/docs/plugins/tmp-file-upload | Tmp File Upload Plugin}
|
|
24
|
+
*/
|
|
25
|
+
var TmpFile = class extends File {
|
|
26
|
+
/**
|
|
27
|
+
* Absolute path of the temporary file holding this file's content.
|
|
28
|
+
* The file is removed when the request finishes, so move or copy it
|
|
29
|
+
* to keep the content. The `File` reads from this path, so it must
|
|
30
|
+
* not be read after the file is moved or removed.
|
|
31
|
+
*/
|
|
13
32
|
path;
|
|
14
33
|
constructor(blob, path, name, options) {
|
|
15
34
|
super([blob], name, options);
|
|
16
35
|
this.path = path;
|
|
17
36
|
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Parses file uploads into temporary files instead of memory. Bodies the standard
|
|
40
|
+
* body parser would buffer into an in-memory `File`, and multipart file parts, are
|
|
41
|
+
* streamed to disk once they outgrow the spool threshold and surfaced as lazily read
|
|
42
|
+
* {@link TmpFile} instances, so requests far larger than available memory parse in
|
|
43
|
+
* constant memory. Smaller uploads stay in memory as plain `File`s. Every other body is
|
|
44
|
+
* left to the standard parser.
|
|
45
|
+
*
|
|
46
|
+
* Request body sizes are limited per content category: buffered in memory, uploaded
|
|
47
|
+
* files, and streamed, each limit fixed or resolved per request. This subsumes the
|
|
48
|
+
* request limit plugin while sizing each kind of body to what it actually costs.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* Temporary files are removed when the request finishes. A streaming response body,
|
|
52
|
+
* an event iterator or a raw stream, keeps them alive until it completes, so
|
|
53
|
+
* responses that read the upload while streaming work. Any other response is
|
|
54
|
+
* transmitted after removal, so one that embeds the upload itself, as a `File` or
|
|
55
|
+
* inside `FormData`, needs the content copied or the file moved first.
|
|
56
|
+
*
|
|
57
|
+
* @see {@link https://orpc.dev/docs/plugins/tmp-file-upload | Tmp File Upload Plugin}
|
|
58
|
+
*/
|
|
59
|
+
var TmpFileUploadHandlerPlugin = class {
|
|
21
60
|
name = "~tmp-file-upload";
|
|
22
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Should parse the original request body instead of batched sub-requests.
|
|
63
|
+
*/
|
|
23
64
|
after = ["~batch"];
|
|
24
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Should run inside the limit and compression plugins, so spooling receives
|
|
67
|
+
* the size-checked, decompressed bytes.
|
|
68
|
+
*/
|
|
25
69
|
before = ["~request-limit", "~request-compression"];
|
|
26
70
|
tmpDir;
|
|
27
71
|
maxBodySize;
|
|
@@ -33,7 +77,7 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
33
77
|
file: Number.POSITIVE_INFINITY,
|
|
34
78
|
stream: Number.POSITIVE_INFINITY
|
|
35
79
|
};
|
|
36
|
-
this.spoolThreshold = options.spoolThreshold ??
|
|
80
|
+
this.spoolThreshold = options.spoolThreshold ?? 65536;
|
|
37
81
|
}
|
|
38
82
|
init(options) {
|
|
39
83
|
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
@@ -47,7 +91,13 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
47
91
|
resolveBody: (hint) => this.resolveBody(interceptorOptions, hint, tmpFiles)
|
|
48
92
|
}
|
|
49
93
|
});
|
|
50
|
-
|
|
94
|
+
/**
|
|
95
|
+
* A streaming response body transmits after this interceptor returns and
|
|
96
|
+
* may read the uploaded tmp files while doing so, so their removal rides
|
|
97
|
+
* on the body finishing instead. That covers abandoned transfers too: the
|
|
98
|
+
* adapter cancels the body, which reaches the same finish hook only after
|
|
99
|
+
* any in-flight procedure logic has completed.
|
|
100
|
+
*/
|
|
51
101
|
if (result.matched && tmpFiles.inUse) {
|
|
52
102
|
const body = result.response.body;
|
|
53
103
|
if (isAsyncIteratorObject(body)) {
|
|
@@ -56,7 +106,10 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
56
106
|
matched: true,
|
|
57
107
|
response: {
|
|
58
108
|
...result.response,
|
|
59
|
-
|
|
109
|
+
/**
|
|
110
|
+
* @warning
|
|
111
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
112
|
+
*/
|
|
60
113
|
body: override(body, wrapAsyncIterator(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
61
114
|
}
|
|
62
115
|
};
|
|
@@ -67,7 +120,10 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
67
120
|
matched: true,
|
|
68
121
|
response: {
|
|
69
122
|
...result.response,
|
|
70
|
-
|
|
123
|
+
/**
|
|
124
|
+
* @warning
|
|
125
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
126
|
+
*/
|
|
71
127
|
body: override(body, wrapReadableStream(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
72
128
|
}
|
|
73
129
|
};
|
|
@@ -75,9 +131,7 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
75
131
|
}
|
|
76
132
|
return result;
|
|
77
133
|
} finally {
|
|
78
|
-
if (!cleanupDeferred)
|
|
79
|
-
await tmpFiles.cleanup();
|
|
80
|
-
}
|
|
134
|
+
if (!cleanupDeferred) await tmpFiles.cleanup();
|
|
81
135
|
}
|
|
82
136
|
};
|
|
83
137
|
return {
|
|
@@ -87,73 +141,57 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
87
141
|
}
|
|
88
142
|
async resolveBody(interceptorOptions, hint, tmpFiles) {
|
|
89
143
|
const { request } = interceptorOptions;
|
|
90
|
-
|
|
91
144
|
const resolvedHint = hint ?? resolveStandardBodyHint(request.headers);
|
|
92
|
-
|
|
93
|
-
if (resolvedHint === "none") {
|
|
94
|
-
return request.resolveBody(hint);
|
|
95
|
-
}
|
|
145
|
+
if (resolvedHint === "none") return request.resolveBody(hint);
|
|
96
146
|
const maxBodySize = await value(this.maxBodySize, interceptorOptions);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (resolvedHint === "
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
147
|
+
/**
|
|
148
|
+
* A form-data hint always means multipart here, even though it can also
|
|
149
|
+
* arrive explicitly or through the standard-server header on a body that
|
|
150
|
+
* is not. Such a body fails the multipart parse, unlike the standard
|
|
151
|
+
* parser, which would fall back to urlencoded form data.
|
|
152
|
+
*/
|
|
153
|
+
if (resolvedHint === "form-data") return this.parseMultipartBody(request, tmpFiles, maxBodySize);
|
|
154
|
+
if (resolvedHint === "file") return this.parseFileBody(request, tmpFiles, maxBodySize);
|
|
155
|
+
if (resolvedHint === "json" || resolvedHint === "url-search-params") return this.parseLimitedBody(request, hint, resolvedHint, maxBodySize.memory);
|
|
108
156
|
const eventLimit = resolvedHint === "event-stream" ? maxBodySize.memory : Number.POSITIVE_INFINITY;
|
|
109
157
|
return this.parseLimitedBody(request, hint, resolvedHint, maxBodySize.stream, eventLimit);
|
|
110
158
|
}
|
|
111
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Enforces size limits on a body the standard parser handles, counting the
|
|
161
|
+
* raw bytes before handing them back for regular parsing.
|
|
162
|
+
*/
|
|
112
163
|
async parseLimitedBody(request, hint, resolvedHint, limit, eventLimit = Number.POSITIVE_INFINITY) {
|
|
113
|
-
if (limit === Number.POSITIVE_INFINITY && eventLimit === Number.POSITIVE_INFINITY)
|
|
114
|
-
return request.resolveBody(hint);
|
|
115
|
-
}
|
|
164
|
+
if (limit === Number.POSITIVE_INFINITY && eventLimit === Number.POSITIVE_INFINITY) return request.resolveBody(hint);
|
|
116
165
|
assertContentLengthWithin(request.headers, limit);
|
|
117
166
|
const stream = await request.resolveBody("octet-stream");
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return stream;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const fits = eventLimit < limit ? createEventSizeCheck(eventLimit) : undefined;
|
|
167
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
168
|
+
const fits = eventLimit < limit ? createEventSizeCheck(eventLimit) : void 0;
|
|
124
169
|
const response = new Response(limitStream(stream, limit, fits), { headers: toFetchHeaders(request.headers) });
|
|
125
170
|
return toStandardBody(response, { hint: resolvedHint });
|
|
126
171
|
}
|
|
127
172
|
async parseFileBody(request, tmpFiles, maxBodySize) {
|
|
128
173
|
assertContentLengthWithin(request.headers, maxBodySize.file);
|
|
129
174
|
const stream = await request.resolveBody("octet-stream");
|
|
130
|
-
|
|
131
|
-
if (!(stream instanceof ReadableStream)) {
|
|
132
|
-
return stream;
|
|
133
|
-
}
|
|
175
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
134
176
|
const contentDisposition = flattenStandardHeader(request.headers["content-disposition"]);
|
|
135
|
-
const fileName = contentDisposition !==
|
|
177
|
+
const fileName = contentDisposition !== void 0 ? getFilenameFromContentDisposition(contentDisposition) : void 0;
|
|
136
178
|
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
137
179
|
const upload = new UploadWriter(tmpFiles, new Budget(maxBodySize.memory), new Budget(maxBodySize.file), this.spoolThreshold);
|
|
138
|
-
for await (const chunk of stream)
|
|
139
|
-
await upload.write(chunk);
|
|
140
|
-
}
|
|
180
|
+
for await (const chunk of stream) await upload.write(chunk);
|
|
141
181
|
return upload.end(fileName ?? "blob", contentType ?? "");
|
|
142
182
|
}
|
|
143
183
|
async parseMultipartBody(request, tmpFiles, maxBodySize) {
|
|
144
184
|
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
145
|
-
const boundary = contentType ===
|
|
146
|
-
if (boundary ===
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
185
|
+
const boundary = contentType === void 0 ? void 0 : parseHeaderParameters(contentType).get("boundary");
|
|
186
|
+
if (boundary === void 0 || boundary === "") throw new TypeError("Invalid multipart body: content-type header is missing the boundary parameter");
|
|
187
|
+
/**
|
|
188
|
+
* The whole multipart body, framing included, can never exceed what its two
|
|
189
|
+
* content categories allow together.
|
|
190
|
+
*/
|
|
150
191
|
const totalLimit = maxBodySize.memory + maxBodySize.file;
|
|
151
192
|
assertContentLengthWithin(request.headers, totalLimit);
|
|
152
193
|
const stream = await request.resolveBody("octet-stream");
|
|
153
|
-
|
|
154
|
-
if (!(stream instanceof ReadableStream)) {
|
|
155
|
-
return stream;
|
|
156
|
-
}
|
|
194
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
157
195
|
const limited = totalLimit === Number.POSITIVE_INFINITY ? stream : limitStream(stream, totalLimit);
|
|
158
196
|
const form = new FormData();
|
|
159
197
|
const memory = new Budget(maxBodySize.memory);
|
|
@@ -161,12 +199,11 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
161
199
|
await parseMultipart(limited, boundary, (part) => {
|
|
162
200
|
const name = part.name;
|
|
163
201
|
memory.charge(part.headerSize);
|
|
164
|
-
if (part.filename ===
|
|
202
|
+
if (part.filename === void 0) {
|
|
165
203
|
const chunks = [];
|
|
166
204
|
return {
|
|
167
205
|
write: (chunk) => {
|
|
168
206
|
memory.charge(chunk.length);
|
|
169
|
-
|
|
170
207
|
chunks.push(Buffer.from(chunk));
|
|
171
208
|
},
|
|
172
209
|
end: () => {
|
|
@@ -175,7 +212,6 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
175
212
|
};
|
|
176
213
|
}
|
|
177
214
|
const filename = part.filename;
|
|
178
|
-
|
|
179
215
|
const type = part.type ?? "text/plain";
|
|
180
216
|
const upload = new UploadWriter(tmpFiles, memory, file, this.spoolThreshold);
|
|
181
217
|
return {
|
|
@@ -187,15 +223,19 @@ export class TmpFileUploadHandlerPlugin {
|
|
|
187
223
|
});
|
|
188
224
|
return form;
|
|
189
225
|
}
|
|
190
|
-
}
|
|
191
|
-
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Rejects early when the declared length already exceeds the limit. The length
|
|
229
|
+
* can lie, so the streaming enforcement stays either way.
|
|
230
|
+
*/
|
|
192
231
|
function assertContentLengthWithin(headers, limit) {
|
|
193
232
|
const contentLength = Number(flattenStandardHeader(headers["content-length"]));
|
|
194
|
-
if (Number.isFinite(contentLength) && contentLength > limit)
|
|
195
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
196
|
-
}
|
|
233
|
+
if (Number.isFinite(contentLength) && contentLength > limit) throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
197
234
|
}
|
|
198
|
-
|
|
235
|
+
/**
|
|
236
|
+
* Limits a stream's total size, also rejecting once `fits` refuses a chunk.
|
|
237
|
+
* `fits` sees every chunk in order, so it can keep state across them.
|
|
238
|
+
*/
|
|
199
239
|
function limitStream(stream, limit, fits = () => true) {
|
|
200
240
|
let total = 0;
|
|
201
241
|
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
@@ -209,13 +249,15 @@ function limitStream(stream, limit, fits = () => true) {
|
|
|
209
249
|
}
|
|
210
250
|
const CR = 13;
|
|
211
251
|
const LF = 10;
|
|
212
|
-
|
|
252
|
+
/**
|
|
253
|
+
* Counts each event's bytes across chunks until its blank line, since the
|
|
254
|
+
* decoder buffers the whole event, and fails once one outgrows the limit. Scans
|
|
255
|
+
* raw bytes, as UTF-8 never puts CR or LF in a multi-byte character.
|
|
256
|
+
*/
|
|
213
257
|
function createEventSizeCheck(eventLimit) {
|
|
214
258
|
let eventSize = 0;
|
|
215
|
-
|
|
216
259
|
let trailing = "none";
|
|
217
260
|
return (chunk) => {
|
|
218
|
-
|
|
219
261
|
const bytes = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
220
262
|
let nextCR = indexOrEnd(bytes, CR, 0);
|
|
221
263
|
let nextLF = indexOrEnd(bytes, LF, 0);
|
|
@@ -226,30 +268,17 @@ function createEventSizeCheck(eventLimit) {
|
|
|
226
268
|
eventSize += lineEnding - offset;
|
|
227
269
|
trailing = "none";
|
|
228
270
|
}
|
|
229
|
-
if (lineEnding === bytes.length)
|
|
230
|
-
return eventSize <= eventLimit;
|
|
231
|
-
}
|
|
271
|
+
if (lineEnding === bytes.length) return eventSize <= eventLimit;
|
|
232
272
|
const byte = bytes[lineEnding];
|
|
233
273
|
offset = lineEnding + 1;
|
|
234
|
-
if (byte === CR)
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (eventSize === 0) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
if (++eventSize > eventLimit) {
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
274
|
+
if (byte === CR) nextCR = indexOrEnd(bytes, CR, offset);
|
|
275
|
+
else nextLF = indexOrEnd(bytes, LF, offset);
|
|
276
|
+
if (eventSize === 0) continue;
|
|
277
|
+
if (++eventSize > eventLimit) return false;
|
|
246
278
|
if (trailing === "line" || trailing === "cr" && byte === CR) {
|
|
247
|
-
|
|
248
279
|
eventSize = 0;
|
|
249
280
|
trailing = "none";
|
|
250
|
-
} else
|
|
251
|
-
trailing = byte === CR ? "cr" : "line";
|
|
252
|
-
}
|
|
281
|
+
} else trailing = byte === CR ? "cr" : "line";
|
|
253
282
|
}
|
|
254
283
|
};
|
|
255
284
|
}
|
|
@@ -257,29 +286,25 @@ function indexOrEnd(bytes, byte, from) {
|
|
|
257
286
|
const index = bytes.indexOf(byte, from);
|
|
258
287
|
return index === -1 ? bytes.length : index;
|
|
259
288
|
}
|
|
260
|
-
|
|
289
|
+
var Budget = class {
|
|
261
290
|
limit;
|
|
262
291
|
used = 0;
|
|
263
292
|
constructor(limit) {
|
|
264
293
|
this.limit = limit;
|
|
265
294
|
}
|
|
266
295
|
charge(size) {
|
|
267
|
-
if (!this.tryCharge(size))
|
|
268
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
269
|
-
}
|
|
296
|
+
if (!this.tryCharge(size)) throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
270
297
|
}
|
|
271
298
|
tryCharge(size) {
|
|
272
|
-
if (this.used + size > this.limit)
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
299
|
+
if (this.used + size > this.limit) return false;
|
|
275
300
|
this.used += size;
|
|
276
301
|
return true;
|
|
277
302
|
}
|
|
278
303
|
refund(size) {
|
|
279
304
|
this.used -= size;
|
|
280
305
|
}
|
|
281
|
-
}
|
|
282
|
-
|
|
306
|
+
};
|
|
307
|
+
var UploadWriter = class {
|
|
283
308
|
tmpFiles;
|
|
284
309
|
memory;
|
|
285
310
|
file;
|
|
@@ -295,11 +320,8 @@ class UploadWriter {
|
|
|
295
320
|
}
|
|
296
321
|
async write(chunk) {
|
|
297
322
|
this.file.charge(chunk.length);
|
|
298
|
-
if (this.tmpPath !==
|
|
299
|
-
return this.tmpFiles.append(this.tmpPath, chunk);
|
|
300
|
-
}
|
|
323
|
+
if (this.tmpPath !== void 0) return this.tmpFiles.append(this.tmpPath, chunk);
|
|
301
324
|
if (this.heldSize + chunk.length <= this.spoolThreshold && this.memory.tryCharge(chunk.length)) {
|
|
302
|
-
|
|
303
325
|
this.held.push(Buffer.from(chunk));
|
|
304
326
|
this.heldSize += chunk.length;
|
|
305
327
|
return;
|
|
@@ -309,15 +331,23 @@ class UploadWriter {
|
|
|
309
331
|
this.held = [];
|
|
310
332
|
}
|
|
311
333
|
async end(name, type) {
|
|
312
|
-
if (this.tmpPath !==
|
|
313
|
-
return this.tmpFiles.seal(this.tmpPath, name, type);
|
|
314
|
-
}
|
|
315
|
-
|
|
334
|
+
if (this.tmpPath !== void 0) return this.tmpFiles.seal(this.tmpPath, name, type);
|
|
316
335
|
return new File([Buffer.concat(this.held, this.heldSize)], name, { type });
|
|
317
336
|
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* Owns every temporary file a request creates: creates them inside one lazily
|
|
340
|
+
* created per-request directory and guarantees removal. Content lands through
|
|
341
|
+
* self-contained appends rather than long-lived handles, so a request holds at
|
|
342
|
+
* most one transient file descriptor per in-flight write no matter how many
|
|
343
|
+
* files it carries, and removal never contends with an open handle on platforms
|
|
344
|
+
* that cannot delete open files.
|
|
345
|
+
*
|
|
346
|
+
* Filesystem failures surface as `INTERNAL_SERVER_ERROR`, keeping them distinct
|
|
347
|
+
* from the malformed-body errors the handler reports as `BAD_REQUEST`. Their
|
|
348
|
+
* details ride only on `cause`, which is never serialized to the client.
|
|
349
|
+
*/
|
|
350
|
+
var RequestTmpFiles = class {
|
|
321
351
|
tmpDir;
|
|
322
352
|
dir;
|
|
323
353
|
count = 0;
|
|
@@ -325,7 +355,7 @@ class RequestTmpFiles {
|
|
|
325
355
|
this.tmpDir = tmpDir;
|
|
326
356
|
}
|
|
327
357
|
get inUse() {
|
|
328
|
-
return this.dir !==
|
|
358
|
+
return this.dir !== void 0;
|
|
329
359
|
}
|
|
330
360
|
async create(content) {
|
|
331
361
|
try {
|
|
@@ -352,13 +382,12 @@ class RequestTmpFiles {
|
|
|
352
382
|
}
|
|
353
383
|
}
|
|
354
384
|
async cleanup() {
|
|
355
|
-
if (this.dir !==
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
} catch {}
|
|
362
|
-
}
|
|
385
|
+
if (this.dir !== void 0) try {
|
|
386
|
+
await rm(await this.dir, {
|
|
387
|
+
recursive: true,
|
|
388
|
+
force: true
|
|
389
|
+
});
|
|
390
|
+
} catch {}
|
|
363
391
|
}
|
|
364
|
-
}
|
|
392
|
+
};
|
|
393
|
+
export { TmpFile, TmpFileUploadHandlerPlugin };
|
package/package.json
CHANGED
|
@@ -1,53 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/node",
|
|
3
|
-
"
|
|
4
|
-
"version": "2.0.0-beta.48",
|
|
3
|
+
"version": "2.0.0-beta.50",
|
|
5
4
|
"description": "Node.js plugins for oRPC: static file serving, temporary file uploads, and batch response compression",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"compression",
|
|
7
|
+
"file-upload",
|
|
8
|
+
"node",
|
|
9
|
+
"nodejs",
|
|
10
|
+
"orpc",
|
|
11
|
+
"static-files",
|
|
12
|
+
"typescript"
|
|
10
13
|
],
|
|
11
14
|
"homepage": "https://orpc.dev",
|
|
15
|
+
"license": "MIT",
|
|
12
16
|
"repository": {
|
|
13
17
|
"type": "git",
|
|
14
18
|
"url": "git+https://github.com/middleapi/orpc.git",
|
|
15
19
|
"directory": "packages/node"
|
|
16
20
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"compression",
|
|
24
|
-
"typescript"
|
|
21
|
+
"funding": [
|
|
22
|
+
"https://github.com/sponsors/dinwwwh",
|
|
23
|
+
"https://opencollective.com/middleapi"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
25
27
|
],
|
|
28
|
+
"type": "module",
|
|
26
29
|
"sideEffects": false,
|
|
27
30
|
"exports": {
|
|
28
31
|
"./package.json": "./package.json",
|
|
29
32
|
".": {
|
|
30
|
-
"types": "./dist/index.d.
|
|
31
|
-
"import": "./dist/index.
|
|
32
|
-
"default": "./dist/index.
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.js",
|
|
35
|
+
"default": "./dist/index.js"
|
|
33
36
|
}
|
|
34
37
|
},
|
|
35
|
-
"files": [
|
|
36
|
-
"dist"
|
|
37
|
-
],
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@orpc/server": "2.0.0-beta.
|
|
40
|
-
"@orpc/shared": "2.0.0-beta.
|
|
41
|
-
"@standard-server/core": "~0.
|
|
42
|
-
"@standard-server/fetch": "~0.
|
|
43
|
-
"@standard-server/node": "~0.
|
|
39
|
+
"@orpc/server": "2.0.0-beta.50",
|
|
40
|
+
"@orpc/shared": "2.0.0-beta.50",
|
|
41
|
+
"@standard-server/core": "~0.16.0",
|
|
42
|
+
"@standard-server/fetch": "~0.16.0",
|
|
43
|
+
"@standard-server/node": "~0.16.0",
|
|
44
44
|
"mime": "^4.1.2"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@orpc/client": "2.0.0-beta.
|
|
47
|
+
"@orpc/client": "2.0.0-beta.50",
|
|
48
48
|
"supertest": "^7.3.1"
|
|
49
49
|
},
|
|
50
|
-
"scripts": {
|
|
51
|
-
"type:check": "tsc -b"
|
|
52
|
-
}
|
|
50
|
+
"scripts": {}
|
|
53
51
|
}
|