@trpc/server 11.0.0-alpha-tmp-export-from-main.213 → 11.0.0-alpha-tmp-export-from-main-nuke-core.236
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/@trpc/server/http.d.ts +7 -0
- package/dist/@trpc/server/http.d.ts.map +1 -0
- package/dist/@trpc/server/index.d.ts +69 -0
- package/dist/@trpc/server/index.d.ts.map +1 -0
- package/dist/@trpc/server/rpc.d.ts +3 -0
- package/dist/@trpc/server/rpc.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/index.d.ts +1 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +20 -109
- package/dist/adapters/aws-lambda/index.mjs +5 -92
- package/dist/adapters/aws-lambda/utils.d.ts +2 -3
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/utils.js +101 -0
- package/dist/adapters/aws-lambda/utils.mjs +94 -0
- package/dist/adapters/express.d.ts +1 -2
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +1 -6
- package/dist/adapters/express.mjs +1 -4
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.js +84 -0
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +82 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +50 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +48 -0
- package/dist/adapters/fastify/index.js +4 -125
- package/dist/adapters/fastify/index.mjs +2 -125
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.js +120 -0
- package/dist/adapters/fetch/fetchRequestHandler.mjs +118 -0
- package/dist/adapters/fetch/index.js +2 -115
- package/dist/adapters/fetch/index.mjs +1 -116
- package/dist/adapters/fetch/types.d.ts +2 -3
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/next.d.ts +1 -2
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +7 -9
- package/dist/adapters/next.mjs +5 -5
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +161 -0
- package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +157 -0
- package/dist/adapters/node-http/content-type/form-data/index.js +20 -646
- package/dist/adapters/node-http/content-type/form-data/index.mjs +9 -631
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +29 -0
- package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +27 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.js +46 -0
- package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +44 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +30 -0
- package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +26 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
- package/dist/adapters/node-http/content-type/json/getPostBody.js +44 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +42 -0
- package/dist/adapters/node-http/content-type/json/index.js +7 -43
- package/dist/adapters/node-http/content-type/json/index.mjs +5 -39
- package/dist/adapters/node-http/index.js +1 -6
- package/dist/adapters/node-http/index.mjs +1 -4
- package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
- package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
- package/dist/{contentType-72ed9df5.mjs → adapters/node-http/internals/contentType.mjs} +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-55f05150.js → adapters/node-http/nodeHTTPRequestHandler.js} +8 -6
- package/dist/{nodeHTTPRequestHandler-2d5c8791.mjs → adapters/node-http/nodeHTTPRequestHandler.mjs} +6 -4
- package/dist/adapters/node-http/types.d.ts +3 -4
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +1 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +3 -11
- package/dist/adapters/standalone.mjs +3 -5
- package/dist/adapters/ws.d.ts +3 -2
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +33 -28
- package/dist/adapters/ws.mjs +8 -1
- package/dist/bundle-analysis.json +757 -0
- package/dist/http.d.ts +1 -6
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +11 -23
- package/dist/http.mjs +6 -1
- package/dist/index.d.ts +1 -68
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -43
- package/dist/index.mjs +8 -1
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +203 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +201 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +167 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +163 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +35 -0
- package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +30 -0
- package/dist/observable/index.d.ts +5 -0
- package/dist/observable/index.d.ts.map +1 -0
- package/dist/observable/index.js +13 -0
- package/dist/observable/index.mjs +2 -0
- package/dist/observable/observable.d.ts +13 -0
- package/dist/observable/observable.d.ts.map +1 -0
- package/dist/observable/observable.js +126 -0
- package/dist/observable/observable.mjs +122 -0
- package/dist/observable/operators.d.ts +8 -0
- package/dist/observable/operators.d.ts.map +1 -0
- package/dist/observable/operators.js +103 -0
- package/dist/observable/operators.mjs +99 -0
- package/dist/observable/types.d.ts +26 -0
- package/dist/observable/types.d.ts.map +1 -0
- package/dist/rpc.d.ts +1 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +6 -15
- package/dist/rpc.mjs +3 -1
- package/dist/shared.d.ts +1 -1
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +6 -11
- package/dist/shared.mjs +4 -1
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +6 -0
- package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts +19 -0
- package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/createProxy.js +52 -0
- package/dist/unstable-core-do-not-import/createProxy.mjs +49 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +13 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.js +67 -0
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +63 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts +33 -0
- package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/formatter.js +7 -0
- package/dist/unstable-core-do-not-import/error/formatter.mjs +5 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +15 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +31 -0
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +29 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +24 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +32 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +30 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts +29 -0
- package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/contentType.js +54 -0
- package/dist/unstable-core-do-not-import/http/contentType.mjs +52 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +5 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +51 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +48 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts +7 -0
- package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +51 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +295 -0
- package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +293 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts +96 -0
- package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.d.ts +37 -0
- package/dist/unstable-core-do-not-import/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/index.js +48 -0
- package/dist/unstable-core-do-not-import/index.mjs +16 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +96 -0
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/initTRPC.js +95 -0
- package/dist/unstable-core-do-not-import/initTRPC.mjs +93 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts +105 -0
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/middleware.js +95 -0
- package/dist/unstable-core-do-not-import/middleware.mjs +89 -0
- package/dist/unstable-core-do-not-import/parser.d.ts +30 -0
- package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/parser.js +37 -0
- package/dist/unstable-core-do-not-import/parser.mjs +35 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts +72 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedure.js +9 -0
- package/dist/unstable-core-do-not-import/procedure.mjs +7 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +101 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.js +176 -0
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +173 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +75 -0
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rootConfig.js +8 -0
- package/dist/unstable-core-do-not-import/rootConfig.mjs +6 -0
- package/dist/unstable-core-do-not-import/router.d.ts +85 -0
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/router.js +156 -0
- package/dist/unstable-core-do-not-import/router.mjs +151 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +51 -0
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/codes.js +38 -0
- package/dist/unstable-core-do-not-import/rpc/codes.mjs +35 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +97 -0
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +5 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +59 -0
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +57 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts +62 -0
- package/dist/unstable-core-do-not-import/serialize.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +107 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/transformer.js +107 -0
- package/dist/unstable-core-do-not-import/transformer.mjs +102 -0
- package/dist/unstable-core-do-not-import/types.d.ts +98 -0
- package/dist/unstable-core-do-not-import/types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.d.ts +27 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/utils.js +44 -0
- package/dist/unstable-core-do-not-import/utils.mjs +39 -0
- package/package.json +54 -126
- package/src/@trpc/server/http.ts +26 -0
- package/src/@trpc/server/index.ts +105 -0
- package/src/@trpc/server/rpc.ts +26 -0
- package/src/adapters/aws-lambda/index.ts +4 -5
- package/src/adapters/aws-lambda/utils.ts +3 -4
- package/src/adapters/express.ts +1 -3
- package/src/adapters/fastify/fastifyRequestHandler.ts +6 -4
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -2
- package/src/adapters/fetch/fetchRequestHandler.ts +6 -4
- package/src/adapters/fetch/types.ts +2 -3
- package/src/adapters/next.ts +2 -3
- package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
- package/src/adapters/node-http/content-type/json/getPostBody.ts +2 -2
- package/src/adapters/node-http/content-type/json/index.ts +1 -1
- package/src/adapters/node-http/internals/contentType.ts +2 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +6 -4
- package/src/adapters/node-http/types.ts +4 -5
- package/src/adapters/standalone.ts +3 -3
- package/src/adapters/ws.ts +14 -13
- package/src/http.ts +1 -26
- package/src/index.ts +1 -105
- package/src/observable/index.ts +10 -0
- package/src/observable/observable.ts +158 -0
- package/src/observable/operators.ts +119 -0
- package/src/observable/types.ts +76 -0
- package/src/rpc.ts +1 -26
- package/src/shared.ts +1 -1
- package/src/unstable-core-do-not-import/TRPCInferrable.ts +9 -0
- package/src/unstable-core-do-not-import/createProxy.ts +59 -0
- package/src/unstable-core-do-not-import/error/TRPCError.ts +82 -0
- package/src/unstable-core-do-not-import/error/formatter.ts +51 -0
- package/src/unstable-core-do-not-import/error/getErrorShape.ts +36 -0
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +29 -0
- package/src/unstable-core-do-not-import/http/contentType.ts +99 -0
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +57 -0
- package/src/unstable-core-do-not-import/http/index.ts +23 -0
- package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +458 -0
- package/src/unstable-core-do-not-import/http/types.ts +111 -0
- package/src/unstable-core-do-not-import/index.ts +110 -0
- package/src/unstable-core-do-not-import/initTRPC.ts +206 -0
- package/src/unstable-core-do-not-import/middleware.ts +233 -0
- package/src/unstable-core-do-not-import/parser.ts +94 -0
- package/src/unstable-core-do-not-import/procedure.ts +108 -0
- package/src/unstable-core-do-not-import/procedureBuilder.ts +458 -0
- package/src/unstable-core-do-not-import/rootConfig.ts +90 -0
- package/src/unstable-core-do-not-import/router.ts +370 -0
- package/src/unstable-core-do-not-import/rpc/codes.ts +44 -0
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +136 -0
- package/src/unstable-core-do-not-import/rpc/index.ts +21 -0
- package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +85 -0
- package/src/unstable-core-do-not-import/serialize.ts +122 -0
- package/src/unstable-core-do-not-import/transformer.ts +202 -0
- package/src/unstable-core-do-not-import/types.ts +151 -0
- package/src/unstable-core-do-not-import/utils.ts +59 -0
- package/unstable-core-do-not-import/index.d.ts +1 -0
- package/unstable-core-do-not-import/index.js +1 -0
- package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts +0 -10
- package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts.map +0 -1
- package/dist/@trpc-server/http.d.ts +0 -2
- package/dist/@trpc-server/http.d.ts.map +0 -1
- package/dist/contentType-24c44bba.js +0 -5
- package/dist/nodeHTTPRequestHandler-7691fc79.js +0 -105
- package/dist/observable.d.ts +0 -3
- package/dist/observable.d.ts.map +0 -1
- package/dist/observable.js +0 -32
- package/dist/observable.mjs +0 -1
- package/src/@trpc-core-unstable-do-not-import-this-please.ts +0 -146
- package/src/@trpc-server/http.ts +0 -2
- package/src/observable.ts +0 -16
- /package/dist/{contentType-d9d22104.js → adapters/node-http/internals/contentType.js} +0 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var node_crypto = require('node:crypto');
|
|
4
|
+
var node_fs = require('node:fs');
|
|
5
|
+
var fs = require('node:fs/promises');
|
|
6
|
+
var node_os = require('node:os');
|
|
7
|
+
var node_path = require('node:path');
|
|
8
|
+
var node_stream = require('node:stream');
|
|
9
|
+
var node_util = require('node:util');
|
|
10
|
+
var streamSlice = require('./streamSlice.js');
|
|
11
|
+
var uploadHandler = require('./uploadHandler.js');
|
|
12
|
+
|
|
13
|
+
async function readableStreamToString(stream, encoding) {
|
|
14
|
+
const reader = stream.getReader();
|
|
15
|
+
const chunks = [];
|
|
16
|
+
async function read() {
|
|
17
|
+
const { done , value } = await reader.read();
|
|
18
|
+
if (done) {
|
|
19
|
+
return;
|
|
20
|
+
} else if (value) {
|
|
21
|
+
chunks.push(value);
|
|
22
|
+
}
|
|
23
|
+
await read();
|
|
24
|
+
}
|
|
25
|
+
await read();
|
|
26
|
+
return Buffer.concat(chunks).toString(encoding);
|
|
27
|
+
}
|
|
28
|
+
const defaultFilePathResolver = ({ filename , })=>{
|
|
29
|
+
const ext = filename ? node_path.extname(filename) : '';
|
|
30
|
+
return 'upload_' + node_crypto.randomBytes(4).readUInt32LE(0) + ext;
|
|
31
|
+
};
|
|
32
|
+
async function uniqueFile(filepath) {
|
|
33
|
+
const ext = node_path.extname(filepath);
|
|
34
|
+
let uniqueFilepath = filepath;
|
|
35
|
+
for(let i = 1; await fs.stat(uniqueFilepath).then(()=>true).catch(()=>false); i++){
|
|
36
|
+
uniqueFilepath = (ext ? filepath.slice(0, -ext.length) : filepath) + `-${new Date().getTime()}${ext}`;
|
|
37
|
+
}
|
|
38
|
+
return uniqueFilepath;
|
|
39
|
+
}
|
|
40
|
+
function createFileUploadHandler({ directory =node_os.tmpdir() , avoidFileConflicts =true , file =defaultFilePathResolver , filter , maxPartSize =3000000 } = {}) {
|
|
41
|
+
return async ({ name , filename , contentType , data })=>{
|
|
42
|
+
if (!filename || filter && !await filter({
|
|
43
|
+
name,
|
|
44
|
+
filename,
|
|
45
|
+
contentType
|
|
46
|
+
})) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
const dir = typeof directory === 'string' ? directory : directory({
|
|
50
|
+
name,
|
|
51
|
+
filename,
|
|
52
|
+
contentType
|
|
53
|
+
});
|
|
54
|
+
if (!dir) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const filedir = node_path.resolve(dir);
|
|
58
|
+
const path = typeof file === 'string' ? file : file({
|
|
59
|
+
name,
|
|
60
|
+
filename,
|
|
61
|
+
contentType
|
|
62
|
+
});
|
|
63
|
+
if (!path) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
let filepath = node_path.resolve(filedir, path);
|
|
67
|
+
if (avoidFileConflicts) {
|
|
68
|
+
filepath = await uniqueFile(filepath);
|
|
69
|
+
}
|
|
70
|
+
await fs.mkdir(node_path.dirname(filepath), {
|
|
71
|
+
recursive: true
|
|
72
|
+
}).catch(()=>{});
|
|
73
|
+
const writeFileStream = node_fs.createWriteStream(filepath);
|
|
74
|
+
let size = 0;
|
|
75
|
+
let deleteFile = false;
|
|
76
|
+
try {
|
|
77
|
+
for await (const chunk of data){
|
|
78
|
+
size += chunk.byteLength;
|
|
79
|
+
if (size > maxPartSize) {
|
|
80
|
+
deleteFile = true;
|
|
81
|
+
throw new uploadHandler.MaxPartSizeExceededError(name, maxPartSize);
|
|
82
|
+
}
|
|
83
|
+
writeFileStream.write(chunk);
|
|
84
|
+
}
|
|
85
|
+
} finally{
|
|
86
|
+
writeFileStream.end();
|
|
87
|
+
await node_util.promisify(node_stream.finished)(writeFileStream);
|
|
88
|
+
if (deleteFile) {
|
|
89
|
+
await fs.rm(filepath).catch(()=>{});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return new NodeOnDiskFile(filepath, contentType);
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
let _toStringTag = Symbol.toStringTag;
|
|
96
|
+
class NodeOnDiskFile {
|
|
97
|
+
get size() {
|
|
98
|
+
const stats = node_fs.statSync(this.filepath);
|
|
99
|
+
if (this.slicer) {
|
|
100
|
+
const slice = this.slicer.end - this.slicer.start;
|
|
101
|
+
return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
|
|
102
|
+
}
|
|
103
|
+
return stats.size;
|
|
104
|
+
}
|
|
105
|
+
slice(start, end, type) {
|
|
106
|
+
if (typeof start === 'number' && start < 0) start = this.size + start;
|
|
107
|
+
if (typeof end === 'number' && end < 0) end = this.size + end;
|
|
108
|
+
const startOffset = this.slicer?.start ?? 0;
|
|
109
|
+
start = startOffset + (start ?? 0);
|
|
110
|
+
end = startOffset + (end ?? this.size);
|
|
111
|
+
return new NodeOnDiskFile(this.filepath, typeof type === 'string' ? type : this.type, {
|
|
112
|
+
start,
|
|
113
|
+
end
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async arrayBuffer() {
|
|
117
|
+
let stream = node_fs.createReadStream(this.filepath);
|
|
118
|
+
if (this.slicer) {
|
|
119
|
+
stream = stream.pipe(streamSlice.streamSlice(this.slicer.start, this.slicer.end));
|
|
120
|
+
}
|
|
121
|
+
return new Promise((resolve, reject)=>{
|
|
122
|
+
const buf = [];
|
|
123
|
+
stream.on('data', (chunk)=>buf.push(chunk));
|
|
124
|
+
stream.on('end', ()=>{
|
|
125
|
+
resolve(Buffer.concat(buf));
|
|
126
|
+
});
|
|
127
|
+
stream.on('error', (err)=>{
|
|
128
|
+
reject(err);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
stream() {
|
|
133
|
+
let stream = node_fs.createReadStream(this.filepath);
|
|
134
|
+
if (this.slicer) {
|
|
135
|
+
stream = stream.pipe(streamSlice.streamSlice(this.slicer.start, this.slicer.end));
|
|
136
|
+
}
|
|
137
|
+
return node_stream.Readable.toWeb(stream);
|
|
138
|
+
}
|
|
139
|
+
async text() {
|
|
140
|
+
return readableStreamToString(this.stream());
|
|
141
|
+
}
|
|
142
|
+
remove() {
|
|
143
|
+
return fs.unlink(this.filepath);
|
|
144
|
+
}
|
|
145
|
+
getFilePath() {
|
|
146
|
+
return this.filepath;
|
|
147
|
+
}
|
|
148
|
+
constructor(filepath, type, slicer){
|
|
149
|
+
this.filepath = filepath;
|
|
150
|
+
this.type = type;
|
|
151
|
+
this.slicer = slicer;
|
|
152
|
+
this.lastModified = 0;
|
|
153
|
+
this.webkitRelativePath = '';
|
|
154
|
+
this[_toStringTag] = 'File';
|
|
155
|
+
this.name = node_path.basename(filepath);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
exports.NodeOnDiskFile = NodeOnDiskFile;
|
|
160
|
+
exports.createFileUploadHandler = createFileUploadHandler;
|
|
161
|
+
exports.readableStreamToString = readableStreamToString;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { createWriteStream, statSync, createReadStream } from 'node:fs';
|
|
3
|
+
import { mkdir, rm, unlink, stat } from 'node:fs/promises';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { resolve, dirname, basename, extname } from 'node:path';
|
|
6
|
+
import { finished, Readable } from 'node:stream';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { streamSlice } from './streamSlice.mjs';
|
|
9
|
+
import { MaxPartSizeExceededError } from './uploadHandler.mjs';
|
|
10
|
+
|
|
11
|
+
async function readableStreamToString(stream, encoding) {
|
|
12
|
+
const reader = stream.getReader();
|
|
13
|
+
const chunks = [];
|
|
14
|
+
async function read() {
|
|
15
|
+
const { done , value } = await reader.read();
|
|
16
|
+
if (done) {
|
|
17
|
+
return;
|
|
18
|
+
} else if (value) {
|
|
19
|
+
chunks.push(value);
|
|
20
|
+
}
|
|
21
|
+
await read();
|
|
22
|
+
}
|
|
23
|
+
await read();
|
|
24
|
+
return Buffer.concat(chunks).toString(encoding);
|
|
25
|
+
}
|
|
26
|
+
const defaultFilePathResolver = ({ filename , })=>{
|
|
27
|
+
const ext = filename ? extname(filename) : '';
|
|
28
|
+
return 'upload_' + randomBytes(4).readUInt32LE(0) + ext;
|
|
29
|
+
};
|
|
30
|
+
async function uniqueFile(filepath) {
|
|
31
|
+
const ext = extname(filepath);
|
|
32
|
+
let uniqueFilepath = filepath;
|
|
33
|
+
for(let i = 1; await stat(uniqueFilepath).then(()=>true).catch(()=>false); i++){
|
|
34
|
+
uniqueFilepath = (ext ? filepath.slice(0, -ext.length) : filepath) + `-${new Date().getTime()}${ext}`;
|
|
35
|
+
}
|
|
36
|
+
return uniqueFilepath;
|
|
37
|
+
}
|
|
38
|
+
function createFileUploadHandler({ directory =tmpdir() , avoidFileConflicts =true , file =defaultFilePathResolver , filter , maxPartSize =3000000 } = {}) {
|
|
39
|
+
return async ({ name , filename , contentType , data })=>{
|
|
40
|
+
if (!filename || filter && !await filter({
|
|
41
|
+
name,
|
|
42
|
+
filename,
|
|
43
|
+
contentType
|
|
44
|
+
})) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
const dir = typeof directory === 'string' ? directory : directory({
|
|
48
|
+
name,
|
|
49
|
+
filename,
|
|
50
|
+
contentType
|
|
51
|
+
});
|
|
52
|
+
if (!dir) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const filedir = resolve(dir);
|
|
56
|
+
const path = typeof file === 'string' ? file : file({
|
|
57
|
+
name,
|
|
58
|
+
filename,
|
|
59
|
+
contentType
|
|
60
|
+
});
|
|
61
|
+
if (!path) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
let filepath = resolve(filedir, path);
|
|
65
|
+
if (avoidFileConflicts) {
|
|
66
|
+
filepath = await uniqueFile(filepath);
|
|
67
|
+
}
|
|
68
|
+
await mkdir(dirname(filepath), {
|
|
69
|
+
recursive: true
|
|
70
|
+
}).catch(()=>{});
|
|
71
|
+
const writeFileStream = createWriteStream(filepath);
|
|
72
|
+
let size = 0;
|
|
73
|
+
let deleteFile = false;
|
|
74
|
+
try {
|
|
75
|
+
for await (const chunk of data){
|
|
76
|
+
size += chunk.byteLength;
|
|
77
|
+
if (size > maxPartSize) {
|
|
78
|
+
deleteFile = true;
|
|
79
|
+
throw new MaxPartSizeExceededError(name, maxPartSize);
|
|
80
|
+
}
|
|
81
|
+
writeFileStream.write(chunk);
|
|
82
|
+
}
|
|
83
|
+
} finally{
|
|
84
|
+
writeFileStream.end();
|
|
85
|
+
await promisify(finished)(writeFileStream);
|
|
86
|
+
if (deleteFile) {
|
|
87
|
+
await rm(filepath).catch(()=>{});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return new NodeOnDiskFile(filepath, contentType);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
let _toStringTag = Symbol.toStringTag;
|
|
94
|
+
class NodeOnDiskFile {
|
|
95
|
+
get size() {
|
|
96
|
+
const stats = statSync(this.filepath);
|
|
97
|
+
if (this.slicer) {
|
|
98
|
+
const slice = this.slicer.end - this.slicer.start;
|
|
99
|
+
return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
|
|
100
|
+
}
|
|
101
|
+
return stats.size;
|
|
102
|
+
}
|
|
103
|
+
slice(start, end, type) {
|
|
104
|
+
if (typeof start === 'number' && start < 0) start = this.size + start;
|
|
105
|
+
if (typeof end === 'number' && end < 0) end = this.size + end;
|
|
106
|
+
const startOffset = this.slicer?.start ?? 0;
|
|
107
|
+
start = startOffset + (start ?? 0);
|
|
108
|
+
end = startOffset + (end ?? this.size);
|
|
109
|
+
return new NodeOnDiskFile(this.filepath, typeof type === 'string' ? type : this.type, {
|
|
110
|
+
start,
|
|
111
|
+
end
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async arrayBuffer() {
|
|
115
|
+
let stream = createReadStream(this.filepath);
|
|
116
|
+
if (this.slicer) {
|
|
117
|
+
stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
|
|
118
|
+
}
|
|
119
|
+
return new Promise((resolve, reject)=>{
|
|
120
|
+
const buf = [];
|
|
121
|
+
stream.on('data', (chunk)=>buf.push(chunk));
|
|
122
|
+
stream.on('end', ()=>{
|
|
123
|
+
resolve(Buffer.concat(buf));
|
|
124
|
+
});
|
|
125
|
+
stream.on('error', (err)=>{
|
|
126
|
+
reject(err);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
stream() {
|
|
131
|
+
let stream = createReadStream(this.filepath);
|
|
132
|
+
if (this.slicer) {
|
|
133
|
+
stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
|
|
134
|
+
}
|
|
135
|
+
return Readable.toWeb(stream);
|
|
136
|
+
}
|
|
137
|
+
async text() {
|
|
138
|
+
return readableStreamToString(this.stream());
|
|
139
|
+
}
|
|
140
|
+
remove() {
|
|
141
|
+
return unlink(this.filepath);
|
|
142
|
+
}
|
|
143
|
+
getFilePath() {
|
|
144
|
+
return this.filepath;
|
|
145
|
+
}
|
|
146
|
+
constructor(filepath, type, slicer){
|
|
147
|
+
this.filepath = filepath;
|
|
148
|
+
this.type = type;
|
|
149
|
+
this.slicer = slicer;
|
|
150
|
+
this.lastModified = 0;
|
|
151
|
+
this.webkitRelativePath = '';
|
|
152
|
+
this[_toStringTag] = 'File';
|
|
153
|
+
this.name = basename(filepath);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export { NodeOnDiskFile, createFileUploadHandler, readableStreamToString };
|