@orpc/node 2.0.0-beta.48 → 2.0.0-beta.49
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 +46 -0
- package/dist/batch-response-compression-handler-plugin.js +129 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/multipart.d.ts +60 -0
- package/dist/multipart.js +221 -0
- package/dist/static-file-handler-plugin.d.ts +84 -0
- package/dist/static-file-handler-plugin.js +215 -0
- package/dist/tmp-file-upload-handler-plugin.d.ts +142 -0
- package/dist/tmp-file-upload-handler-plugin.js +465 -0
- 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
- package/dist/tmp-file-upload-handler-plugin.mjs +0 -364
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { openAsBlob } from "node:fs";
|
|
3
|
-
import { appendFile, mkdtemp, rm } from "node:fs/promises";
|
|
4
|
-
import { tmpdir } from "node:os";
|
|
5
|
-
import path from "node:path";
|
|
6
|
-
import { ORPCError } from "@orpc/server";
|
|
7
|
-
import { isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream } from "@orpc/shared";
|
|
8
|
-
import { flattenStandardHeader, getFilenameFromContentDisposition, resolveStandardBodyHint } from "@standard-server/core";
|
|
9
|
-
import { toFetchHeaders, toStandardBody } from "@standard-server/fetch";
|
|
10
|
-
import { parseHeaderParameters, parseMultipart } from "./multipart.mjs";
|
|
11
|
-
|
|
12
|
-
export class TmpFile extends File {
|
|
13
|
-
path;
|
|
14
|
-
constructor(blob, path, name, options) {
|
|
15
|
-
super([blob], name, options);
|
|
16
|
-
this.path = path;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class TmpFileUploadHandlerPlugin {
|
|
21
|
-
name = "~tmp-file-upload";
|
|
22
|
-
|
|
23
|
-
after = ["~batch"];
|
|
24
|
-
|
|
25
|
-
before = ["~request-limit", "~request-compression"];
|
|
26
|
-
tmpDir;
|
|
27
|
-
maxBodySize;
|
|
28
|
-
spoolThreshold;
|
|
29
|
-
constructor(options = {}) {
|
|
30
|
-
this.tmpDir = options.tmpDir ?? tmpdir();
|
|
31
|
-
this.maxBodySize = options.maxBodySize ?? {
|
|
32
|
-
memory: Number.POSITIVE_INFINITY,
|
|
33
|
-
file: Number.POSITIVE_INFINITY,
|
|
34
|
-
stream: Number.POSITIVE_INFINITY
|
|
35
|
-
};
|
|
36
|
-
this.spoolThreshold = options.spoolThreshold ?? 64 * 1024;
|
|
37
|
-
}
|
|
38
|
-
init(options) {
|
|
39
|
-
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
40
|
-
const tmpFiles = new RequestTmpFiles(this.tmpDir);
|
|
41
|
-
let cleanupDeferred = false;
|
|
42
|
-
try {
|
|
43
|
-
const result = await next({
|
|
44
|
-
...interceptorOptions,
|
|
45
|
-
request: {
|
|
46
|
-
...interceptorOptions.request,
|
|
47
|
-
resolveBody: (hint) => this.resolveBody(interceptorOptions, hint, tmpFiles)
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
if (result.matched && tmpFiles.inUse) {
|
|
52
|
-
const body = result.response.body;
|
|
53
|
-
if (isAsyncIteratorObject(body)) {
|
|
54
|
-
cleanupDeferred = true;
|
|
55
|
-
return {
|
|
56
|
-
matched: true,
|
|
57
|
-
response: {
|
|
58
|
-
...result.response,
|
|
59
|
-
|
|
60
|
-
body: override(body, wrapAsyncIterator(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
if (body instanceof ReadableStream) {
|
|
65
|
-
cleanupDeferred = true;
|
|
66
|
-
return {
|
|
67
|
-
matched: true,
|
|
68
|
-
response: {
|
|
69
|
-
...result.response,
|
|
70
|
-
|
|
71
|
-
body: override(body, wrapReadableStream(body, { onFinish: () => tmpFiles.cleanup() }))
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
77
|
-
} finally {
|
|
78
|
-
if (!cleanupDeferred) {
|
|
79
|
-
await tmpFiles.cleanup();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
return {
|
|
84
|
-
...options,
|
|
85
|
-
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)]
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
async resolveBody(interceptorOptions, hint, tmpFiles) {
|
|
89
|
-
const { request } = interceptorOptions;
|
|
90
|
-
|
|
91
|
-
const resolvedHint = hint ?? resolveStandardBodyHint(request.headers);
|
|
92
|
-
|
|
93
|
-
if (resolvedHint === "none") {
|
|
94
|
-
return request.resolveBody(hint);
|
|
95
|
-
}
|
|
96
|
-
const maxBodySize = await value(this.maxBodySize, interceptorOptions);
|
|
97
|
-
|
|
98
|
-
if (resolvedHint === "form-data") {
|
|
99
|
-
return this.parseMultipartBody(request, tmpFiles, maxBodySize);
|
|
100
|
-
}
|
|
101
|
-
if (resolvedHint === "file") {
|
|
102
|
-
return this.parseFileBody(request, tmpFiles, maxBodySize);
|
|
103
|
-
}
|
|
104
|
-
if (resolvedHint === "json" || resolvedHint === "url-search-params") {
|
|
105
|
-
return this.parseLimitedBody(request, hint, resolvedHint, maxBodySize.memory);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const eventLimit = resolvedHint === "event-stream" ? maxBodySize.memory : Number.POSITIVE_INFINITY;
|
|
109
|
-
return this.parseLimitedBody(request, hint, resolvedHint, maxBodySize.stream, eventLimit);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
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
|
-
}
|
|
116
|
-
assertContentLengthWithin(request.headers, limit);
|
|
117
|
-
const stream = await request.resolveBody("octet-stream");
|
|
118
|
-
|
|
119
|
-
if (!(stream instanceof ReadableStream)) {
|
|
120
|
-
return stream;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const fits = eventLimit < limit ? createEventSizeCheck(eventLimit) : undefined;
|
|
124
|
-
const response = new Response(limitStream(stream, limit, fits), { headers: toFetchHeaders(request.headers) });
|
|
125
|
-
return toStandardBody(response, { hint: resolvedHint });
|
|
126
|
-
}
|
|
127
|
-
async parseFileBody(request, tmpFiles, maxBodySize) {
|
|
128
|
-
assertContentLengthWithin(request.headers, maxBodySize.file);
|
|
129
|
-
const stream = await request.resolveBody("octet-stream");
|
|
130
|
-
|
|
131
|
-
if (!(stream instanceof ReadableStream)) {
|
|
132
|
-
return stream;
|
|
133
|
-
}
|
|
134
|
-
const contentDisposition = flattenStandardHeader(request.headers["content-disposition"]);
|
|
135
|
-
const fileName = contentDisposition !== undefined ? getFilenameFromContentDisposition(contentDisposition) : undefined;
|
|
136
|
-
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
137
|
-
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
|
-
}
|
|
141
|
-
return upload.end(fileName ?? "blob", contentType ?? "");
|
|
142
|
-
}
|
|
143
|
-
async parseMultipartBody(request, tmpFiles, maxBodySize) {
|
|
144
|
-
const contentType = flattenStandardHeader(request.headers["content-type"]);
|
|
145
|
-
const boundary = contentType === undefined ? undefined : parseHeaderParameters(contentType).get("boundary");
|
|
146
|
-
if (boundary === undefined || boundary === "") {
|
|
147
|
-
throw new TypeError("Invalid multipart body: content-type header is missing the boundary parameter");
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const totalLimit = maxBodySize.memory + maxBodySize.file;
|
|
151
|
-
assertContentLengthWithin(request.headers, totalLimit);
|
|
152
|
-
const stream = await request.resolveBody("octet-stream");
|
|
153
|
-
|
|
154
|
-
if (!(stream instanceof ReadableStream)) {
|
|
155
|
-
return stream;
|
|
156
|
-
}
|
|
157
|
-
const limited = totalLimit === Number.POSITIVE_INFINITY ? stream : limitStream(stream, totalLimit);
|
|
158
|
-
const form = new FormData();
|
|
159
|
-
const memory = new Budget(maxBodySize.memory);
|
|
160
|
-
const file = new Budget(maxBodySize.file);
|
|
161
|
-
await parseMultipart(limited, boundary, (part) => {
|
|
162
|
-
const name = part.name;
|
|
163
|
-
memory.charge(part.headerSize);
|
|
164
|
-
if (part.filename === undefined) {
|
|
165
|
-
const chunks = [];
|
|
166
|
-
return {
|
|
167
|
-
write: (chunk) => {
|
|
168
|
-
memory.charge(chunk.length);
|
|
169
|
-
|
|
170
|
-
chunks.push(Buffer.from(chunk));
|
|
171
|
-
},
|
|
172
|
-
end: () => {
|
|
173
|
-
form.append(name, Buffer.concat(chunks).toString());
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
const filename = part.filename;
|
|
178
|
-
|
|
179
|
-
const type = part.type ?? "text/plain";
|
|
180
|
-
const upload = new UploadWriter(tmpFiles, memory, file, this.spoolThreshold);
|
|
181
|
-
return {
|
|
182
|
-
write: (chunk) => upload.write(chunk),
|
|
183
|
-
end: async () => {
|
|
184
|
-
form.append(name, await upload.end(filename, type));
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
});
|
|
188
|
-
return form;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function assertContentLengthWithin(headers, limit) {
|
|
193
|
-
const contentLength = Number(flattenStandardHeader(headers["content-length"]));
|
|
194
|
-
if (Number.isFinite(contentLength) && contentLength > limit) {
|
|
195
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function limitStream(stream, limit, fits = () => true) {
|
|
200
|
-
let total = 0;
|
|
201
|
-
return stream.pipeThrough(new TransformStream({ transform(chunk, controller) {
|
|
202
|
-
total += chunk.byteLength;
|
|
203
|
-
if (total > limit || !fits(chunk)) {
|
|
204
|
-
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
controller.enqueue(chunk);
|
|
208
|
-
} }));
|
|
209
|
-
}
|
|
210
|
-
const CR = 13;
|
|
211
|
-
const LF = 10;
|
|
212
|
-
|
|
213
|
-
function createEventSizeCheck(eventLimit) {
|
|
214
|
-
let eventSize = 0;
|
|
215
|
-
|
|
216
|
-
let trailing = "none";
|
|
217
|
-
return (chunk) => {
|
|
218
|
-
|
|
219
|
-
const bytes = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
220
|
-
let nextCR = indexOrEnd(bytes, CR, 0);
|
|
221
|
-
let nextLF = indexOrEnd(bytes, LF, 0);
|
|
222
|
-
let offset = 0;
|
|
223
|
-
while (true) {
|
|
224
|
-
const lineEnding = Math.min(nextCR, nextLF);
|
|
225
|
-
if (lineEnding > offset) {
|
|
226
|
-
eventSize += lineEnding - offset;
|
|
227
|
-
trailing = "none";
|
|
228
|
-
}
|
|
229
|
-
if (lineEnding === bytes.length) {
|
|
230
|
-
return eventSize <= eventLimit;
|
|
231
|
-
}
|
|
232
|
-
const byte = bytes[lineEnding];
|
|
233
|
-
offset = lineEnding + 1;
|
|
234
|
-
if (byte === CR) {
|
|
235
|
-
nextCR = indexOrEnd(bytes, CR, offset);
|
|
236
|
-
} else {
|
|
237
|
-
nextLF = indexOrEnd(bytes, LF, offset);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
if (eventSize === 0) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
if (++eventSize > eventLimit) {
|
|
244
|
-
return false;
|
|
245
|
-
}
|
|
246
|
-
if (trailing === "line" || trailing === "cr" && byte === CR) {
|
|
247
|
-
|
|
248
|
-
eventSize = 0;
|
|
249
|
-
trailing = "none";
|
|
250
|
-
} else {
|
|
251
|
-
trailing = byte === CR ? "cr" : "line";
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function indexOrEnd(bytes, byte, from) {
|
|
257
|
-
const index = bytes.indexOf(byte, from);
|
|
258
|
-
return index === -1 ? bytes.length : index;
|
|
259
|
-
}
|
|
260
|
-
class Budget {
|
|
261
|
-
limit;
|
|
262
|
-
used = 0;
|
|
263
|
-
constructor(limit) {
|
|
264
|
-
this.limit = limit;
|
|
265
|
-
}
|
|
266
|
-
charge(size) {
|
|
267
|
-
if (!this.tryCharge(size)) {
|
|
268
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
tryCharge(size) {
|
|
272
|
-
if (this.used + size > this.limit) {
|
|
273
|
-
return false;
|
|
274
|
-
}
|
|
275
|
-
this.used += size;
|
|
276
|
-
return true;
|
|
277
|
-
}
|
|
278
|
-
refund(size) {
|
|
279
|
-
this.used -= size;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
class UploadWriter {
|
|
283
|
-
tmpFiles;
|
|
284
|
-
memory;
|
|
285
|
-
file;
|
|
286
|
-
spoolThreshold;
|
|
287
|
-
held = [];
|
|
288
|
-
heldSize = 0;
|
|
289
|
-
tmpPath;
|
|
290
|
-
constructor(tmpFiles, memory, file, spoolThreshold) {
|
|
291
|
-
this.tmpFiles = tmpFiles;
|
|
292
|
-
this.memory = memory;
|
|
293
|
-
this.file = file;
|
|
294
|
-
this.spoolThreshold = spoolThreshold;
|
|
295
|
-
}
|
|
296
|
-
async write(chunk) {
|
|
297
|
-
this.file.charge(chunk.length);
|
|
298
|
-
if (this.tmpPath !== undefined) {
|
|
299
|
-
return this.tmpFiles.append(this.tmpPath, chunk);
|
|
300
|
-
}
|
|
301
|
-
if (this.heldSize + chunk.length <= this.spoolThreshold && this.memory.tryCharge(chunk.length)) {
|
|
302
|
-
|
|
303
|
-
this.held.push(Buffer.from(chunk));
|
|
304
|
-
this.heldSize += chunk.length;
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
this.tmpPath = await this.tmpFiles.create(Buffer.concat([...this.held, chunk]));
|
|
308
|
-
this.memory.refund(this.heldSize);
|
|
309
|
-
this.held = [];
|
|
310
|
-
}
|
|
311
|
-
async end(name, type) {
|
|
312
|
-
if (this.tmpPath !== undefined) {
|
|
313
|
-
return this.tmpFiles.seal(this.tmpPath, name, type);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return new File([Buffer.concat(this.held, this.heldSize)], name, { type });
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
class RequestTmpFiles {
|
|
321
|
-
tmpDir;
|
|
322
|
-
dir;
|
|
323
|
-
count = 0;
|
|
324
|
-
constructor(tmpDir) {
|
|
325
|
-
this.tmpDir = tmpDir;
|
|
326
|
-
}
|
|
327
|
-
get inUse() {
|
|
328
|
-
return this.dir !== undefined;
|
|
329
|
-
}
|
|
330
|
-
async create(content) {
|
|
331
|
-
try {
|
|
332
|
-
this.dir ??= mkdtemp(path.join(this.tmpDir, "orpc-upload-"));
|
|
333
|
-
const tmpPath = path.join(await this.dir, String(this.count++));
|
|
334
|
-
await appendFile(tmpPath, content);
|
|
335
|
-
return tmpPath;
|
|
336
|
-
} catch (cause) {
|
|
337
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
async append(tmpPath, chunk) {
|
|
341
|
-
try {
|
|
342
|
-
await appendFile(tmpPath, chunk);
|
|
343
|
-
} catch (cause) {
|
|
344
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
async seal(tmpPath, name, type) {
|
|
348
|
-
try {
|
|
349
|
-
return new TmpFile(await openAsBlob(tmpPath), tmpPath, name, { type });
|
|
350
|
-
} catch (cause) {
|
|
351
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", { cause });
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
async cleanup() {
|
|
355
|
-
if (this.dir !== undefined) {
|
|
356
|
-
try {
|
|
357
|
-
await rm(await this.dir, {
|
|
358
|
-
recursive: true,
|
|
359
|
-
force: true
|
|
360
|
-
});
|
|
361
|
-
} catch {}
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|