@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,465 +1,393 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
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";
|
|
5
|
+
import { openAsBlob } from "node:fs";
|
|
6
|
+
import { appendFile, mkdtemp, rm } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { Buffer } from "node:buffer";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { ORPCError } from "@orpc/server";
|
|
11
|
+
// src/tmp-file-upload-handler-plugin.ts
|
|
11
12
|
/**
|
|
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
|
-
}
|
|
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
|
+
*/
|
|
32
|
+
path;
|
|
33
|
+
constructor(blob, path, name, options) {
|
|
34
|
+
super([blob], name, options);
|
|
35
|
+
this.path = path;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return stream;
|
|
228
|
-
}
|
|
229
|
-
const limited = totalLimit === Number.POSITIVE_INFINITY ? stream : limitStream(stream, totalLimit);
|
|
230
|
-
const form = new FormData();
|
|
231
|
-
const memory = new Budget(maxBodySize.memory);
|
|
232
|
-
const file = new Budget(maxBodySize.file);
|
|
233
|
-
await parseMultipart(limited, boundary, (part) => {
|
|
234
|
-
const name = part.name;
|
|
235
|
-
memory.charge(part.headerSize);
|
|
236
|
-
if (part.filename === undefined) {
|
|
237
|
-
const chunks = [];
|
|
238
|
-
return {
|
|
239
|
-
write: (chunk) => {
|
|
240
|
-
memory.charge(chunk.length);
|
|
241
|
-
// The parser only guarantees the chunk until write returns, so retaining it requires a copy
|
|
242
|
-
chunks.push(Buffer.from(chunk));
|
|
243
|
-
},
|
|
244
|
-
end: () => {
|
|
245
|
-
form.append(name, Buffer.concat(chunks).toString());
|
|
246
|
-
},
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
const filename = part.filename;
|
|
250
|
-
// A part without a content-type defaults to text/plain, matching the standard parser
|
|
251
|
-
const type = part.type ?? 'text/plain';
|
|
252
|
-
const upload = new UploadWriter(tmpFiles, memory, file, this.spoolThreshold);
|
|
253
|
-
return {
|
|
254
|
-
write: (chunk) => upload.write(chunk),
|
|
255
|
-
end: async () => {
|
|
256
|
-
form.append(name, await upload.end(filename, type));
|
|
257
|
-
},
|
|
258
|
-
};
|
|
259
|
-
});
|
|
260
|
-
return form;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
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 {
|
|
60
|
+
name = "~tmp-file-upload";
|
|
61
|
+
/**
|
|
62
|
+
* Should parse the original request body instead of batched sub-requests.
|
|
63
|
+
*/
|
|
64
|
+
after = ["~batch"];
|
|
65
|
+
/**
|
|
66
|
+
* Should run inside the limit and compression plugins, so spooling receives
|
|
67
|
+
* the size-checked, decompressed bytes.
|
|
68
|
+
*/
|
|
69
|
+
before = ["~request-limit", "~request-compression"];
|
|
70
|
+
tmpDir;
|
|
71
|
+
maxBodySize;
|
|
72
|
+
spoolThreshold;
|
|
73
|
+
constructor(options = {}) {
|
|
74
|
+
this.tmpDir = options.tmpDir ?? tmpdir();
|
|
75
|
+
this.maxBodySize = options.maxBodySize ?? {
|
|
76
|
+
memory: Number.POSITIVE_INFINITY,
|
|
77
|
+
file: Number.POSITIVE_INFINITY,
|
|
78
|
+
stream: Number.POSITIVE_INFINITY
|
|
79
|
+
};
|
|
80
|
+
this.spoolThreshold = options.spoolThreshold ?? 65536;
|
|
81
|
+
}
|
|
82
|
+
init(options) {
|
|
83
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
84
|
+
const tmpFiles = new RequestTmpFiles(this.tmpDir);
|
|
85
|
+
let cleanupDeferred = false;
|
|
86
|
+
try {
|
|
87
|
+
const result = await next({
|
|
88
|
+
...interceptorOptions,
|
|
89
|
+
request: {
|
|
90
|
+
...interceptorOptions.request,
|
|
91
|
+
resolveBody: (hint) => this.resolveBody(interceptorOptions, hint, tmpFiles)
|
|
92
|
+
}
|
|
93
|
+
});
|
|
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
|
+
*/
|
|
101
|
+
if (result.matched && tmpFiles.inUse) {
|
|
102
|
+
const body = result.response.body;
|
|
103
|
+
if (isAsyncIteratorObject(body)) {
|
|
104
|
+
cleanupDeferred = true;
|
|
105
|
+
return {
|
|
106
|
+
matched: true,
|
|
107
|
+
response: {
|
|
108
|
+
...result.response,
|
|
109
|
+
/**
|
|
110
|
+
* @warning
|
|
111
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
112
|
+
*/
|
|
113
|
+
body: override(body, wrapAsyncIterator(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (body instanceof ReadableStream) {
|
|
118
|
+
cleanupDeferred = true;
|
|
119
|
+
return {
|
|
120
|
+
matched: true,
|
|
121
|
+
response: {
|
|
122
|
+
...result.response,
|
|
123
|
+
/**
|
|
124
|
+
* @warning
|
|
125
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
126
|
+
*/
|
|
127
|
+
body: override(body, wrapReadableStream(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return result;
|
|
133
|
+
} finally {
|
|
134
|
+
if (!cleanupDeferred) await tmpFiles.cleanup();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
...options,
|
|
139
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)]
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async resolveBody(interceptorOptions, hint, tmpFiles) {
|
|
143
|
+
const { request } = interceptorOptions;
|
|
144
|
+
const resolvedHint = hint ?? resolveStandardBodyHint(request.headers);
|
|
145
|
+
if (resolvedHint === "none") return request.resolveBody(hint);
|
|
146
|
+
const maxBodySize = await value(this.maxBodySize, interceptorOptions);
|
|
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);
|
|
156
|
+
const eventLimit = resolvedHint === "event-stream" ? maxBodySize.memory : Number.POSITIVE_INFINITY;
|
|
157
|
+
return this.parseLimitedBody(request, hint, resolvedHint, maxBodySize.stream, eventLimit);
|
|
158
|
+
}
|
|
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
|
+
*/
|
|
163
|
+
async parseLimitedBody(request, hint, resolvedHint, limit, eventLimit = Number.POSITIVE_INFINITY) {
|
|
164
|
+
if (limit === Number.POSITIVE_INFINITY && eventLimit === Number.POSITIVE_INFINITY) return request.resolveBody(hint);
|
|
165
|
+
assertContentLengthWithin(request.headers, limit);
|
|
166
|
+
const stream = await request.resolveBody("octet-stream");
|
|
167
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
168
|
+
const fits = eventLimit < limit ? createEventSizeCheck(eventLimit) : void 0;
|
|
169
|
+
const response = new Response(limitStream(stream, limit, fits), { headers: toFetchHeaders(request.headers) });
|
|
170
|
+
return toStandardBody(response, { hint: resolvedHint });
|
|
171
|
+
}
|
|
172
|
+
async parseFileBody(request, tmpFiles, maxBodySize) {
|
|
173
|
+
assertContentLengthWithin(request.headers, maxBodySize.file);
|
|
174
|
+
const stream = await request.resolveBody("octet-stream");
|
|
175
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
176
|
+
const contentDisposition = flattenStandardHeader(request.headers["content-disposition"]);
|
|
177
|
+
const fileName = contentDisposition !== void 0 ? getFilenameFromContentDisposition(contentDisposition) : void 0;
|
|
178
|
+
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
179
|
+
const upload = new UploadWriter(tmpFiles, new Budget(maxBodySize.memory), new Budget(maxBodySize.file), this.spoolThreshold);
|
|
180
|
+
for await (const chunk of stream) await upload.write(chunk);
|
|
181
|
+
return upload.end(fileName ?? "blob", contentType ?? "");
|
|
182
|
+
}
|
|
183
|
+
async parseMultipartBody(request, tmpFiles, maxBodySize) {
|
|
184
|
+
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
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
|
+
*/
|
|
191
|
+
const totalLimit = maxBodySize.memory + maxBodySize.file;
|
|
192
|
+
assertContentLengthWithin(request.headers, totalLimit);
|
|
193
|
+
const stream = await request.resolveBody("octet-stream");
|
|
194
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
195
|
+
const limited = totalLimit === Number.POSITIVE_INFINITY ? stream : limitStream(stream, totalLimit);
|
|
196
|
+
const form = new FormData();
|
|
197
|
+
const memory = new Budget(maxBodySize.memory);
|
|
198
|
+
const file = new Budget(maxBodySize.file);
|
|
199
|
+
await parseMultipart(limited, boundary, (part) => {
|
|
200
|
+
const name = part.name;
|
|
201
|
+
memory.charge(part.headerSize);
|
|
202
|
+
if (part.filename === void 0) {
|
|
203
|
+
const chunks = [];
|
|
204
|
+
return {
|
|
205
|
+
write: (chunk) => {
|
|
206
|
+
memory.charge(chunk.length);
|
|
207
|
+
chunks.push(Buffer.from(chunk));
|
|
208
|
+
},
|
|
209
|
+
end: () => {
|
|
210
|
+
form.append(name, Buffer.concat(chunks).toString());
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const filename = part.filename;
|
|
215
|
+
const type = part.type ?? "text/plain";
|
|
216
|
+
const upload = new UploadWriter(tmpFiles, memory, file, this.spoolThreshold);
|
|
217
|
+
return {
|
|
218
|
+
write: (chunk) => upload.write(chunk),
|
|
219
|
+
end: async () => {
|
|
220
|
+
form.append(name, await upload.end(filename, type));
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
});
|
|
224
|
+
return form;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
263
227
|
/**
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
228
|
+
* Rejects early when the declared length already exceeds the limit. The length
|
|
229
|
+
* can lie, so the streaming enforcement stays either way.
|
|
230
|
+
*/
|
|
267
231
|
function assertContentLengthWithin(headers, limit) {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
throw new ORPCError('PAYLOAD_TOO_LARGE');
|
|
271
|
-
}
|
|
232
|
+
const contentLength = Number(flattenStandardHeader(headers["content-length"]));
|
|
233
|
+
if (Number.isFinite(contentLength) && contentLength > limit) throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
272
234
|
}
|
|
273
235
|
/**
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
+
*/
|
|
277
239
|
function limitStream(stream, limit, fits = () => true) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
},
|
|
288
|
-
}));
|
|
240
|
+
let total = 0;
|
|
241
|
+
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
242
|
+
total += chunk.byteLength;
|
|
243
|
+
if (total > limit || !fits(chunk)) {
|
|
244
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
controller.enqueue(chunk);
|
|
248
|
+
} }));
|
|
289
249
|
}
|
|
290
|
-
const CR =
|
|
291
|
-
const LF =
|
|
250
|
+
const CR = 13;
|
|
251
|
+
const LF = 10;
|
|
292
252
|
/**
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
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
|
+
*/
|
|
297
257
|
function createEventSizeCheck(eventLimit) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
// The decoder skips line endings before an event
|
|
325
|
-
if (eventSize === 0) {
|
|
326
|
-
continue;
|
|
327
|
-
}
|
|
328
|
-
if (++eventSize > eventLimit) {
|
|
329
|
-
return false;
|
|
330
|
-
}
|
|
331
|
-
if (trailing === 'line' || (trailing === 'cr' && byte === CR)) {
|
|
332
|
-
// A blank line: the decoder releases the event
|
|
333
|
-
eventSize = 0;
|
|
334
|
-
trailing = 'none';
|
|
335
|
-
}
|
|
336
|
-
else {
|
|
337
|
-
trailing = byte === CR ? 'cr' : 'line';
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
};
|
|
258
|
+
let eventSize = 0;
|
|
259
|
+
let trailing = "none";
|
|
260
|
+
return (chunk) => {
|
|
261
|
+
const bytes = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
262
|
+
let nextCR = indexOrEnd(bytes, CR, 0);
|
|
263
|
+
let nextLF = indexOrEnd(bytes, LF, 0);
|
|
264
|
+
let offset = 0;
|
|
265
|
+
while (true) {
|
|
266
|
+
const lineEnding = Math.min(nextCR, nextLF);
|
|
267
|
+
if (lineEnding > offset) {
|
|
268
|
+
eventSize += lineEnding - offset;
|
|
269
|
+
trailing = "none";
|
|
270
|
+
}
|
|
271
|
+
if (lineEnding === bytes.length) return eventSize <= eventLimit;
|
|
272
|
+
const byte = bytes[lineEnding];
|
|
273
|
+
offset = lineEnding + 1;
|
|
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;
|
|
278
|
+
if (trailing === "line" || trailing === "cr" && byte === CR) {
|
|
279
|
+
eventSize = 0;
|
|
280
|
+
trailing = "none";
|
|
281
|
+
} else trailing = byte === CR ? "cr" : "line";
|
|
282
|
+
}
|
|
283
|
+
};
|
|
341
284
|
}
|
|
342
285
|
function indexOrEnd(bytes, byte, from) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
346
|
-
class Budget {
|
|
347
|
-
limit;
|
|
348
|
-
used = 0;
|
|
349
|
-
constructor(limit) {
|
|
350
|
-
this.limit = limit;
|
|
351
|
-
}
|
|
352
|
-
charge(size) {
|
|
353
|
-
if (!this.tryCharge(size)) {
|
|
354
|
-
throw new ORPCError('PAYLOAD_TOO_LARGE');
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
tryCharge(size) {
|
|
358
|
-
if (this.used + size > this.limit) {
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
this.used += size;
|
|
362
|
-
return true;
|
|
363
|
-
}
|
|
364
|
-
refund(size) {
|
|
365
|
-
this.used -= size;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
class UploadWriter {
|
|
369
|
-
tmpFiles;
|
|
370
|
-
memory;
|
|
371
|
-
file;
|
|
372
|
-
spoolThreshold;
|
|
373
|
-
held = [];
|
|
374
|
-
heldSize = 0;
|
|
375
|
-
tmpPath;
|
|
376
|
-
constructor(tmpFiles, memory, file, spoolThreshold) {
|
|
377
|
-
this.tmpFiles = tmpFiles;
|
|
378
|
-
this.memory = memory;
|
|
379
|
-
this.file = file;
|
|
380
|
-
this.spoolThreshold = spoolThreshold;
|
|
381
|
-
}
|
|
382
|
-
async write(chunk) {
|
|
383
|
-
this.file.charge(chunk.length);
|
|
384
|
-
if (this.tmpPath !== undefined) {
|
|
385
|
-
return this.tmpFiles.append(this.tmpPath, chunk);
|
|
386
|
-
}
|
|
387
|
-
if (this.heldSize + chunk.length <= this.spoolThreshold &&
|
|
388
|
-
this.memory.tryCharge(chunk.length)) {
|
|
389
|
-
// Chunks are only guaranteed until write returns, so holding one requires a copy
|
|
390
|
-
this.held.push(Buffer.from(chunk));
|
|
391
|
-
this.heldSize += chunk.length;
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
this.tmpPath = await this.tmpFiles.create(Buffer.concat([...this.held, chunk]));
|
|
395
|
-
this.memory.refund(this.heldSize);
|
|
396
|
-
this.held = [];
|
|
397
|
-
}
|
|
398
|
-
async end(name, type) {
|
|
399
|
-
if (this.tmpPath !== undefined) {
|
|
400
|
-
return this.tmpFiles.seal(this.tmpPath, name, type);
|
|
401
|
-
}
|
|
402
|
-
// A File keeps one part per chunk, so merge them first
|
|
403
|
-
return new File([Buffer.concat(this.held, this.heldSize)], name, { type });
|
|
404
|
-
}
|
|
286
|
+
const index = bytes.indexOf(byte, from);
|
|
287
|
+
return index === -1 ? bytes.length : index;
|
|
405
288
|
}
|
|
289
|
+
var Budget = class {
|
|
290
|
+
limit;
|
|
291
|
+
used = 0;
|
|
292
|
+
constructor(limit) {
|
|
293
|
+
this.limit = limit;
|
|
294
|
+
}
|
|
295
|
+
charge(size) {
|
|
296
|
+
if (!this.tryCharge(size)) throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
297
|
+
}
|
|
298
|
+
tryCharge(size) {
|
|
299
|
+
if (this.used + size > this.limit) return false;
|
|
300
|
+
this.used += size;
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
refund(size) {
|
|
304
|
+
this.used -= size;
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
var UploadWriter = class {
|
|
308
|
+
tmpFiles;
|
|
309
|
+
memory;
|
|
310
|
+
file;
|
|
311
|
+
spoolThreshold;
|
|
312
|
+
held = [];
|
|
313
|
+
heldSize = 0;
|
|
314
|
+
tmpPath;
|
|
315
|
+
constructor(tmpFiles, memory, file, spoolThreshold) {
|
|
316
|
+
this.tmpFiles = tmpFiles;
|
|
317
|
+
this.memory = memory;
|
|
318
|
+
this.file = file;
|
|
319
|
+
this.spoolThreshold = spoolThreshold;
|
|
320
|
+
}
|
|
321
|
+
async write(chunk) {
|
|
322
|
+
this.file.charge(chunk.length);
|
|
323
|
+
if (this.tmpPath !== void 0) return this.tmpFiles.append(this.tmpPath, chunk);
|
|
324
|
+
if (this.heldSize + chunk.length <= this.spoolThreshold && this.memory.tryCharge(chunk.length)) {
|
|
325
|
+
this.held.push(Buffer.from(chunk));
|
|
326
|
+
this.heldSize += chunk.length;
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
this.tmpPath = await this.tmpFiles.create(Buffer.concat([...this.held, chunk]));
|
|
330
|
+
this.memory.refund(this.heldSize);
|
|
331
|
+
this.held = [];
|
|
332
|
+
}
|
|
333
|
+
async end(name, type) {
|
|
334
|
+
if (this.tmpPath !== void 0) return this.tmpFiles.seal(this.tmpPath, name, type);
|
|
335
|
+
return new File([Buffer.concat(this.held, this.heldSize)], name, { type });
|
|
336
|
+
}
|
|
337
|
+
};
|
|
406
338
|
/**
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
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 {
|
|
351
|
+
tmpDir;
|
|
352
|
+
dir;
|
|
353
|
+
count = 0;
|
|
354
|
+
constructor(tmpDir) {
|
|
355
|
+
this.tmpDir = tmpDir;
|
|
356
|
+
}
|
|
357
|
+
get inUse() {
|
|
358
|
+
return this.dir !== void 0;
|
|
359
|
+
}
|
|
360
|
+
async create(content) {
|
|
361
|
+
try {
|
|
362
|
+
this.dir ??= mkdtemp(path.join(this.tmpDir, "orpc-upload-"));
|
|
363
|
+
const tmpPath = path.join(await this.dir, String(this.count++));
|
|
364
|
+
await appendFile(tmpPath, content);
|
|
365
|
+
return tmpPath;
|
|
366
|
+
} catch (cause) {
|
|
367
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
async append(tmpPath, chunk) {
|
|
371
|
+
try {
|
|
372
|
+
await appendFile(tmpPath, chunk);
|
|
373
|
+
} catch (cause) {
|
|
374
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async seal(tmpPath, name, type) {
|
|
378
|
+
try {
|
|
379
|
+
return new TmpFile(await openAsBlob(tmpPath), tmpPath, name, { type });
|
|
380
|
+
} catch (cause) {
|
|
381
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async cleanup() {
|
|
385
|
+
if (this.dir !== void 0) try {
|
|
386
|
+
await rm(await this.dir, {
|
|
387
|
+
recursive: true,
|
|
388
|
+
force: true
|
|
389
|
+
});
|
|
390
|
+
} catch {}
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
export { TmpFile, TmpFileUploadHandlerPlugin };
|