@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.
Files changed (277) hide show
  1. package/dist/@trpc/server/http.d.ts +7 -0
  2. package/dist/@trpc/server/http.d.ts.map +1 -0
  3. package/dist/@trpc/server/index.d.ts +69 -0
  4. package/dist/@trpc/server/index.d.ts.map +1 -0
  5. package/dist/@trpc/server/rpc.d.ts +3 -0
  6. package/dist/@trpc/server/rpc.d.ts.map +1 -0
  7. package/dist/adapters/aws-lambda/index.d.ts +1 -2
  8. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  9. package/dist/adapters/aws-lambda/index.js +20 -109
  10. package/dist/adapters/aws-lambda/index.mjs +5 -92
  11. package/dist/adapters/aws-lambda/utils.d.ts +2 -3
  12. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  13. package/dist/adapters/aws-lambda/utils.js +101 -0
  14. package/dist/adapters/aws-lambda/utils.mjs +94 -0
  15. package/dist/adapters/express.d.ts +1 -2
  16. package/dist/adapters/express.d.ts.map +1 -1
  17. package/dist/adapters/express.js +1 -6
  18. package/dist/adapters/express.mjs +1 -4
  19. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
  20. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  21. package/dist/adapters/fastify/fastifyRequestHandler.js +84 -0
  22. package/dist/adapters/fastify/fastifyRequestHandler.mjs +82 -0
  23. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -2
  24. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  25. package/dist/adapters/fastify/fastifyTRPCPlugin.js +50 -0
  26. package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +48 -0
  27. package/dist/adapters/fastify/index.js +4 -125
  28. package/dist/adapters/fastify/index.mjs +2 -125
  29. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -2
  30. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  31. package/dist/adapters/fetch/fetchRequestHandler.js +120 -0
  32. package/dist/adapters/fetch/fetchRequestHandler.mjs +118 -0
  33. package/dist/adapters/fetch/index.js +2 -115
  34. package/dist/adapters/fetch/index.mjs +1 -116
  35. package/dist/adapters/fetch/types.d.ts +2 -3
  36. package/dist/adapters/fetch/types.d.ts.map +1 -1
  37. package/dist/adapters/next.d.ts +1 -2
  38. package/dist/adapters/next.d.ts.map +1 -1
  39. package/dist/adapters/next.js +7 -9
  40. package/dist/adapters/next.mjs +5 -5
  41. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +161 -0
  42. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +157 -0
  43. package/dist/adapters/node-http/content-type/form-data/index.js +20 -646
  44. package/dist/adapters/node-http/content-type/form-data/index.mjs +9 -631
  45. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +29 -0
  46. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +27 -0
  47. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +46 -0
  48. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +44 -0
  49. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +30 -0
  50. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +26 -0
  51. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +1 -1
  52. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +1 -1
  53. package/dist/adapters/node-http/content-type/json/getPostBody.js +44 -0
  54. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +42 -0
  55. package/dist/adapters/node-http/content-type/json/index.js +7 -43
  56. package/dist/adapters/node-http/content-type/json/index.mjs +5 -39
  57. package/dist/adapters/node-http/index.js +1 -6
  58. package/dist/adapters/node-http/index.mjs +1 -4
  59. package/dist/adapters/node-http/internals/contentType.d.ts +2 -2
  60. package/dist/adapters/node-http/internals/contentType.d.ts.map +1 -1
  61. package/dist/{contentType-72ed9df5.mjs → adapters/node-http/internals/contentType.mjs} +1 -1
  62. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -2
  63. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  64. package/dist/{nodeHTTPRequestHandler-55f05150.js → adapters/node-http/nodeHTTPRequestHandler.js} +8 -6
  65. package/dist/{nodeHTTPRequestHandler-2d5c8791.mjs → adapters/node-http/nodeHTTPRequestHandler.mjs} +6 -4
  66. package/dist/adapters/node-http/types.d.ts +3 -4
  67. package/dist/adapters/node-http/types.d.ts.map +1 -1
  68. package/dist/adapters/standalone.d.ts +1 -2
  69. package/dist/adapters/standalone.d.ts.map +1 -1
  70. package/dist/adapters/standalone.js +3 -11
  71. package/dist/adapters/standalone.mjs +3 -5
  72. package/dist/adapters/ws.d.ts +3 -2
  73. package/dist/adapters/ws.d.ts.map +1 -1
  74. package/dist/adapters/ws.js +33 -28
  75. package/dist/adapters/ws.mjs +8 -1
  76. package/dist/bundle-analysis.json +757 -0
  77. package/dist/http.d.ts +1 -6
  78. package/dist/http.d.ts.map +1 -1
  79. package/dist/http.js +11 -23
  80. package/dist/http.mjs +6 -1
  81. package/dist/index.d.ts +1 -68
  82. package/dist/index.d.ts.map +1 -1
  83. package/dist/index.js +18 -43
  84. package/dist/index.mjs +8 -1
  85. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +203 -0
  86. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +201 -0
  87. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +167 -0
  88. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +163 -0
  89. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +35 -0
  90. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +30 -0
  91. package/dist/observable/index.d.ts +5 -0
  92. package/dist/observable/index.d.ts.map +1 -0
  93. package/dist/observable/index.js +13 -0
  94. package/dist/observable/index.mjs +2 -0
  95. package/dist/observable/observable.d.ts +13 -0
  96. package/dist/observable/observable.d.ts.map +1 -0
  97. package/dist/observable/observable.js +126 -0
  98. package/dist/observable/observable.mjs +122 -0
  99. package/dist/observable/operators.d.ts +8 -0
  100. package/dist/observable/operators.d.ts.map +1 -0
  101. package/dist/observable/operators.js +103 -0
  102. package/dist/observable/operators.mjs +99 -0
  103. package/dist/observable/types.d.ts +26 -0
  104. package/dist/observable/types.d.ts.map +1 -0
  105. package/dist/rpc.d.ts +1 -2
  106. package/dist/rpc.d.ts.map +1 -1
  107. package/dist/rpc.js +6 -15
  108. package/dist/rpc.mjs +3 -1
  109. package/dist/shared.d.ts +1 -1
  110. package/dist/shared.d.ts.map +1 -1
  111. package/dist/shared.js +6 -11
  112. package/dist/shared.mjs +4 -1
  113. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts +6 -0
  114. package/dist/unstable-core-do-not-import/TRPCInferrable.d.ts.map +1 -0
  115. package/dist/unstable-core-do-not-import/createProxy.d.ts +19 -0
  116. package/dist/unstable-core-do-not-import/createProxy.d.ts.map +1 -0
  117. package/dist/unstable-core-do-not-import/createProxy.js +52 -0
  118. package/dist/unstable-core-do-not-import/createProxy.mjs +49 -0
  119. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +13 -0
  120. package/dist/unstable-core-do-not-import/error/TRPCError.d.ts.map +1 -0
  121. package/dist/unstable-core-do-not-import/error/TRPCError.js +67 -0
  122. package/dist/unstable-core-do-not-import/error/TRPCError.mjs +63 -0
  123. package/dist/unstable-core-do-not-import/error/formatter.d.ts +33 -0
  124. package/dist/unstable-core-do-not-import/error/formatter.d.ts.map +1 -0
  125. package/dist/unstable-core-do-not-import/error/formatter.js +7 -0
  126. package/dist/unstable-core-do-not-import/error/formatter.mjs +5 -0
  127. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts +15 -0
  128. package/dist/unstable-core-do-not-import/error/getErrorShape.d.ts.map +1 -0
  129. package/dist/unstable-core-do-not-import/error/getErrorShape.js +31 -0
  130. package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +29 -0
  131. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +24 -0
  132. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +1 -0
  133. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +32 -0
  134. package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +30 -0
  135. package/dist/unstable-core-do-not-import/http/contentType.d.ts +29 -0
  136. package/dist/unstable-core-do-not-import/http/contentType.d.ts.map +1 -0
  137. package/dist/unstable-core-do-not-import/http/contentType.js +54 -0
  138. package/dist/unstable-core-do-not-import/http/contentType.mjs +52 -0
  139. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +5 -0
  140. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -0
  141. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +51 -0
  142. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +48 -0
  143. package/dist/unstable-core-do-not-import/http/index.d.ts +7 -0
  144. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -0
  145. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts +51 -0
  146. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.d.ts.map +1 -0
  147. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.js +295 -0
  148. package/dist/unstable-core-do-not-import/http/resolveHTTPResponse.mjs +293 -0
  149. package/dist/unstable-core-do-not-import/http/types.d.ts +96 -0
  150. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -0
  151. package/dist/unstable-core-do-not-import/index.d.ts +37 -0
  152. package/dist/unstable-core-do-not-import/index.d.ts.map +1 -0
  153. package/dist/unstable-core-do-not-import/index.js +48 -0
  154. package/dist/unstable-core-do-not-import/index.mjs +16 -0
  155. package/dist/unstable-core-do-not-import/initTRPC.d.ts +96 -0
  156. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -0
  157. package/dist/unstable-core-do-not-import/initTRPC.js +95 -0
  158. package/dist/unstable-core-do-not-import/initTRPC.mjs +93 -0
  159. package/dist/unstable-core-do-not-import/middleware.d.ts +105 -0
  160. package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -0
  161. package/dist/unstable-core-do-not-import/middleware.js +95 -0
  162. package/dist/unstable-core-do-not-import/middleware.mjs +89 -0
  163. package/dist/unstable-core-do-not-import/parser.d.ts +30 -0
  164. package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -0
  165. package/dist/unstable-core-do-not-import/parser.js +37 -0
  166. package/dist/unstable-core-do-not-import/parser.mjs +35 -0
  167. package/dist/unstable-core-do-not-import/procedure.d.ts +72 -0
  168. package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -0
  169. package/dist/unstable-core-do-not-import/procedure.js +9 -0
  170. package/dist/unstable-core-do-not-import/procedure.mjs +7 -0
  171. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +101 -0
  172. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -0
  173. package/dist/unstable-core-do-not-import/procedureBuilder.js +176 -0
  174. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +173 -0
  175. package/dist/unstable-core-do-not-import/rootConfig.d.ts +75 -0
  176. package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -0
  177. package/dist/unstable-core-do-not-import/rootConfig.js +8 -0
  178. package/dist/unstable-core-do-not-import/rootConfig.mjs +6 -0
  179. package/dist/unstable-core-do-not-import/router.d.ts +85 -0
  180. package/dist/unstable-core-do-not-import/router.d.ts.map +1 -0
  181. package/dist/unstable-core-do-not-import/router.js +156 -0
  182. package/dist/unstable-core-do-not-import/router.mjs +151 -0
  183. package/dist/unstable-core-do-not-import/rpc/codes.d.ts +51 -0
  184. package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -0
  185. package/dist/unstable-core-do-not-import/rpc/codes.js +38 -0
  186. package/dist/unstable-core-do-not-import/rpc/codes.mjs +35 -0
  187. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +97 -0
  188. package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -0
  189. package/dist/unstable-core-do-not-import/rpc/index.d.ts +5 -0
  190. package/dist/unstable-core-do-not-import/rpc/index.d.ts.map +1 -0
  191. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts +5 -0
  192. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.d.ts.map +1 -0
  193. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +59 -0
  194. package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +57 -0
  195. package/dist/unstable-core-do-not-import/serialize.d.ts +62 -0
  196. package/dist/unstable-core-do-not-import/serialize.d.ts.map +1 -0
  197. package/dist/unstable-core-do-not-import/transformer.d.ts +107 -0
  198. package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -0
  199. package/dist/unstable-core-do-not-import/transformer.js +107 -0
  200. package/dist/unstable-core-do-not-import/transformer.mjs +102 -0
  201. package/dist/unstable-core-do-not-import/types.d.ts +98 -0
  202. package/dist/unstable-core-do-not-import/types.d.ts.map +1 -0
  203. package/dist/unstable-core-do-not-import/utils.d.ts +27 -0
  204. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -0
  205. package/dist/unstable-core-do-not-import/utils.js +44 -0
  206. package/dist/unstable-core-do-not-import/utils.mjs +39 -0
  207. package/package.json +54 -126
  208. package/src/@trpc/server/http.ts +26 -0
  209. package/src/@trpc/server/index.ts +105 -0
  210. package/src/@trpc/server/rpc.ts +26 -0
  211. package/src/adapters/aws-lambda/index.ts +4 -5
  212. package/src/adapters/aws-lambda/utils.ts +3 -4
  213. package/src/adapters/express.ts +1 -3
  214. package/src/adapters/fastify/fastifyRequestHandler.ts +6 -4
  215. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -2
  216. package/src/adapters/fetch/fetchRequestHandler.ts +6 -4
  217. package/src/adapters/fetch/types.ts +2 -3
  218. package/src/adapters/next.ts +2 -3
  219. package/src/adapters/node-http/content-type/form-data/index.ts +1 -1
  220. package/src/adapters/node-http/content-type/json/getPostBody.ts +2 -2
  221. package/src/adapters/node-http/content-type/json/index.ts +1 -1
  222. package/src/adapters/node-http/internals/contentType.ts +2 -2
  223. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +6 -4
  224. package/src/adapters/node-http/types.ts +4 -5
  225. package/src/adapters/standalone.ts +3 -3
  226. package/src/adapters/ws.ts +14 -13
  227. package/src/http.ts +1 -26
  228. package/src/index.ts +1 -105
  229. package/src/observable/index.ts +10 -0
  230. package/src/observable/observable.ts +158 -0
  231. package/src/observable/operators.ts +119 -0
  232. package/src/observable/types.ts +76 -0
  233. package/src/rpc.ts +1 -26
  234. package/src/shared.ts +1 -1
  235. package/src/unstable-core-do-not-import/TRPCInferrable.ts +9 -0
  236. package/src/unstable-core-do-not-import/createProxy.ts +59 -0
  237. package/src/unstable-core-do-not-import/error/TRPCError.ts +82 -0
  238. package/src/unstable-core-do-not-import/error/formatter.ts +51 -0
  239. package/src/unstable-core-do-not-import/error/getErrorShape.ts +36 -0
  240. package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +29 -0
  241. package/src/unstable-core-do-not-import/http/contentType.ts +99 -0
  242. package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +57 -0
  243. package/src/unstable-core-do-not-import/http/index.ts +23 -0
  244. package/src/unstable-core-do-not-import/http/resolveHTTPResponse.ts +458 -0
  245. package/src/unstable-core-do-not-import/http/types.ts +111 -0
  246. package/src/unstable-core-do-not-import/index.ts +110 -0
  247. package/src/unstable-core-do-not-import/initTRPC.ts +206 -0
  248. package/src/unstable-core-do-not-import/middleware.ts +233 -0
  249. package/src/unstable-core-do-not-import/parser.ts +94 -0
  250. package/src/unstable-core-do-not-import/procedure.ts +108 -0
  251. package/src/unstable-core-do-not-import/procedureBuilder.ts +458 -0
  252. package/src/unstable-core-do-not-import/rootConfig.ts +90 -0
  253. package/src/unstable-core-do-not-import/router.ts +370 -0
  254. package/src/unstable-core-do-not-import/rpc/codes.ts +44 -0
  255. package/src/unstable-core-do-not-import/rpc/envelopes.ts +136 -0
  256. package/src/unstable-core-do-not-import/rpc/index.ts +21 -0
  257. package/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts +85 -0
  258. package/src/unstable-core-do-not-import/serialize.ts +122 -0
  259. package/src/unstable-core-do-not-import/transformer.ts +202 -0
  260. package/src/unstable-core-do-not-import/types.ts +151 -0
  261. package/src/unstable-core-do-not-import/utils.ts +59 -0
  262. package/unstable-core-do-not-import/index.d.ts +1 -0
  263. package/unstable-core-do-not-import/index.js +1 -0
  264. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts +0 -10
  265. package/dist/@trpc-core-unstable-do-not-import-this-please.d.ts.map +0 -1
  266. package/dist/@trpc-server/http.d.ts +0 -2
  267. package/dist/@trpc-server/http.d.ts.map +0 -1
  268. package/dist/contentType-24c44bba.js +0 -5
  269. package/dist/nodeHTTPRequestHandler-7691fc79.js +0 -105
  270. package/dist/observable.d.ts +0 -3
  271. package/dist/observable.d.ts.map +0 -1
  272. package/dist/observable.js +0 -32
  273. package/dist/observable.mjs +0 -1
  274. package/src/@trpc-core-unstable-do-not-import-this-please.ts +0 -146
  275. package/src/@trpc-server/http.ts +0 -2
  276. package/src/observable.ts +0 -16
  277. /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 };