@trpc/server 11.0.0-alpha-tmp-export-from-main.221 → 11.0.0-alpha-tmp-export-from-main.222

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 (136) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +68 -0
  2. package/dist/adapters/aws-lambda/index.d.ts +54 -6
  3. package/dist/adapters/aws-lambda/index.js +110 -20
  4. package/dist/adapters/aws-lambda/index.mjs +94 -4
  5. package/dist/adapters/express.d.mts +19 -0
  6. package/dist/adapters/express.d.ts +9 -6
  7. package/dist/adapters/express.js +3 -3
  8. package/dist/adapters/express.mjs +1 -1
  9. package/dist/adapters/fastify/index.d.mts +33 -0
  10. package/dist/adapters/fastify/index.d.ts +33 -3
  11. package/dist/adapters/fastify/index.js +124 -5
  12. package/dist/adapters/fastify/index.mjs +125 -2
  13. package/dist/adapters/fetch/index.d.mts +51 -0
  14. package/dist/adapters/fetch/index.d.ts +51 -3
  15. package/dist/adapters/fetch/index.js +115 -4
  16. package/dist/adapters/fetch/index.mjs +116 -1
  17. package/dist/adapters/next.d.mts +21 -0
  18. package/dist/adapters/next.d.ts +11 -7
  19. package/dist/adapters/next.js +6 -6
  20. package/dist/adapters/next.mjs +2 -2
  21. package/dist/adapters/node-http/content-type/form-data/index.d.mts +219 -0
  22. package/dist/adapters/node-http/content-type/form-data/index.d.ts +201 -10
  23. package/dist/adapters/node-http/content-type/form-data/index.js +662 -23
  24. package/dist/adapters/node-http/content-type/form-data/index.mjs +646 -10
  25. package/dist/adapters/node-http/{types.d.ts → content-type/json/index.d.mts} +21 -15
  26. package/dist/adapters/node-http/content-type/json/index.d.ts +89 -2
  27. package/dist/adapters/node-http/content-type/json/index.js +48 -7
  28. package/dist/adapters/node-http/content-type/json/index.mjs +44 -3
  29. package/dist/adapters/node-http/index.d.mts +100 -0
  30. package/dist/adapters/node-http/index.d.ts +100 -3
  31. package/dist/adapters/node-http/index.js +106 -4
  32. package/dist/adapters/node-http/index.mjs +107 -1
  33. package/dist/adapters/standalone.d.mts +21 -0
  34. package/dist/adapters/standalone.d.ts +11 -9
  35. package/dist/adapters/standalone.js +9 -5
  36. package/dist/adapters/standalone.mjs +2 -2
  37. package/dist/adapters/ws.d.mts +36 -0
  38. package/dist/adapters/ws.d.ts +12 -13
  39. package/dist/adapters/ws.js +30 -28
  40. package/dist/adapters/ws.mjs +12 -10
  41. package/dist/http.d.mts +1 -0
  42. package/dist/http.d.ts +1 -7
  43. package/dist/http.js +7 -7
  44. package/dist/http.mjs +1 -1
  45. package/dist/index.d.mts +9 -0
  46. package/dist/index.d.ts +5 -65
  47. package/dist/index.js +809 -46
  48. package/dist/index.mjs +798 -1
  49. package/dist/observable.d.mts +1 -0
  50. package/dist/observable.d.ts +1 -3
  51. package/dist/observable.js +8 -8
  52. package/dist/observable.mjs +1 -1
  53. package/dist/rpc.d.mts +1 -0
  54. package/dist/rpc.d.ts +1 -3
  55. package/dist/rpc.js +5 -5
  56. package/dist/rpc.mjs +1 -1
  57. package/dist/shared.d.mts +1 -0
  58. package/dist/shared.d.ts +1 -26
  59. package/dist/shared.js +4 -4
  60. package/dist/shared.mjs +1 -1
  61. package/dist/unstableDontImportMe.d.mts +1 -0
  62. package/dist/unstableDontImportMe.d.ts +1 -10
  63. package/dist/unstableDontImportMe.js +1549 -142
  64. package/dist/unstableDontImportMe.mjs +1514 -1
  65. package/package.json +6 -4
  66. package/dist/@trpc-server/http.d.ts +0 -2
  67. package/dist/@trpc-server/http.d.ts.map +0 -1
  68. package/dist/adapters/aws-lambda/index.d.ts.map +0 -1
  69. package/dist/adapters/aws-lambda/utils.d.ts +0 -48
  70. package/dist/adapters/aws-lambda/utils.d.ts.map +0 -1
  71. package/dist/adapters/aws-lambda/utils.js +0 -100
  72. package/dist/adapters/aws-lambda/utils.mjs +0 -93
  73. package/dist/adapters/express.d.ts.map +0 -1
  74. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +0 -13
  75. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +0 -1
  76. package/dist/adapters/fastify/fastifyRequestHandler.js +0 -81
  77. package/dist/adapters/fastify/fastifyRequestHandler.mjs +0 -79
  78. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +0 -22
  79. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +0 -1
  80. package/dist/adapters/fastify/fastifyTRPCPlugin.js +0 -51
  81. package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +0 -49
  82. package/dist/adapters/fastify/index.d.ts.map +0 -1
  83. package/dist/adapters/fetch/fetchRequestHandler.d.ts +0 -18
  84. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +0 -1
  85. package/dist/adapters/fetch/fetchRequestHandler.js +0 -118
  86. package/dist/adapters/fetch/fetchRequestHandler.mjs +0 -116
  87. package/dist/adapters/fetch/index.d.ts.map +0 -1
  88. package/dist/adapters/fetch/types.d.ts +0 -31
  89. package/dist/adapters/fetch/types.d.ts.map +0 -1
  90. package/dist/adapters/next.d.ts.map +0 -1
  91. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +0 -70
  92. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +0 -1
  93. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.js +0 -161
  94. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.mjs +0 -157
  95. package/dist/adapters/node-http/content-type/form-data/index.d.ts.map +0 -1
  96. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +0 -31
  97. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +0 -1
  98. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.js +0 -29
  99. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.mjs +0 -27
  100. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts +0 -16
  101. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +0 -1
  102. package/dist/adapters/node-http/content-type/form-data/streamSlice.js +0 -46
  103. package/dist/adapters/node-http/content-type/form-data/streamSlice.mjs +0 -44
  104. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +0 -45
  105. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +0 -1
  106. package/dist/adapters/node-http/content-type/form-data/uploadHandler.js +0 -30
  107. package/dist/adapters/node-http/content-type/form-data/uploadHandler.mjs +0 -26
  108. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts +0 -7
  109. package/dist/adapters/node-http/content-type/json/getPostBody.d.ts.map +0 -1
  110. package/dist/adapters/node-http/content-type/json/getPostBody.js +0 -42
  111. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +0 -40
  112. package/dist/adapters/node-http/content-type/json/index.d.ts.map +0 -1
  113. package/dist/adapters/node-http/index.d.ts.map +0 -1
  114. package/dist/adapters/node-http/internals/contentType.d.ts +0 -9
  115. package/dist/adapters/node-http/internals/contentType.d.ts.map +0 -1
  116. package/dist/adapters/node-http/internals/contentType.js +0 -8
  117. package/dist/adapters/node-http/internals/contentType.mjs +0 -6
  118. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +0 -14
  119. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +0 -1
  120. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +0 -109
  121. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +0 -107
  122. package/dist/adapters/node-http/types.d.ts.map +0 -1
  123. package/dist/adapters/standalone.d.ts.map +0 -1
  124. package/dist/adapters/ws.d.ts.map +0 -1
  125. package/dist/http.d.ts.map +0 -1
  126. package/dist/index.d.ts.map +0 -1
  127. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.js +0 -203
  128. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs +0 -201
  129. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.js +0 -167
  130. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/search.mjs +0 -163
  131. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.js +0 -35
  132. package/dist/node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/utils.mjs +0 -30
  133. package/dist/observable.d.ts.map +0 -1
  134. package/dist/rpc.d.ts.map +0 -1
  135. package/dist/shared.d.ts.map +0 -1
  136. package/dist/unstableDontImportMe.d.ts.map +0 -1
@@ -1,12 +1,648 @@
1
- import * as fs from 'node:fs/promises';
2
- import { Readable } from 'node:stream';
3
- import { streamMultipart } from '../../../../node_modules/.pnpm/@web3-storage_multipart-parser@1.0.0/node_modules/@web3-storage/multipart-parser/esm/src/index.mjs';
4
- import { createNodeHTTPContentTypeHandler } from '../../internals/contentType.mjs';
5
- import { NodeOnDiskFile } from './fileUploadHandler.mjs';
6
- export { createFileUploadHandler as experimental_createFileUploadHandler } from './fileUploadHandler.mjs';
7
- import { MaxBodySizeExceededError } from './uploadHandler.mjs';
8
- export { MaxPartSizeExceededError, composeUploadHandlers as experimental_composeUploadHandlers } from './uploadHandler.mjs';
9
- export { createMemoryUploadHandler as experimental_createMemoryUploadHandler } from './memoryUploadHandler.mjs';
1
+ import * as fs from 'fs/promises';
2
+ import { Transform, finished, Readable } from 'node:stream';
3
+ import { randomBytes } from 'node:crypto';
4
+ import { createWriteStream, statSync, createReadStream } from 'node:fs';
5
+ import { mkdir, rm, unlink, stat } from 'node:fs/promises';
6
+ import { tmpdir } from 'node:os';
7
+ import { resolve, dirname, basename, extname } from 'node:path';
8
+ import { promisify } from 'node:util';
9
+
10
+ function stringToArray(s) {
11
+ const utf8 = unescape(encodeURIComponent(s));
12
+ return Uint8Array.from(utf8, (_, i)=>utf8.charCodeAt(i));
13
+ }
14
+ function arrayToString(a) {
15
+ const utf8 = String.fromCharCode.apply(null, a);
16
+ return decodeURIComponent(escape(utf8));
17
+ }
18
+ function mergeArrays(...arrays) {
19
+ const out = new Uint8Array(arrays.reduce((total, arr)=>total + arr.length, 0));
20
+ let offset = 0;
21
+ for (const arr of arrays){
22
+ out.set(arr, offset);
23
+ offset += arr.length;
24
+ }
25
+ return out;
26
+ }
27
+ function arraysEqual(a, b) {
28
+ if (a.length !== b.length) {
29
+ return false;
30
+ }
31
+ for(let i = 0; i < a.length; i++){
32
+ if (a[i] !== b[i]) {
33
+ return false;
34
+ }
35
+ }
36
+ return true;
37
+ }
38
+
39
+ function coerce(a) {
40
+ if (a instanceof Uint8Array) {
41
+ return (index)=>a[index];
42
+ }
43
+ return a;
44
+ }
45
+ function jsmemcmp(buf1, pos1, buf2, pos2, len) {
46
+ const fn1 = coerce(buf1);
47
+ const fn2 = coerce(buf2);
48
+ for(let i = 0; i < len; ++i){
49
+ if (fn1(pos1 + i) !== fn2(pos2 + i)) {
50
+ return false;
51
+ }
52
+ }
53
+ return true;
54
+ }
55
+ function createOccurenceTable(s) {
56
+ const table = new Array(256).fill(s.length);
57
+ if (s.length > 1) {
58
+ for(let i = 0; i < s.length - 1; i++){
59
+ table[s[i]] = s.length - 1 - i;
60
+ }
61
+ }
62
+ return table;
63
+ }
64
+ const MATCH = Symbol('Match');
65
+ class StreamSearch {
66
+ constructor(needle){
67
+ this._lookbehind = new Uint8Array();
68
+ if (typeof needle === 'string') {
69
+ this._needle = needle = stringToArray(needle);
70
+ } else {
71
+ this._needle = needle;
72
+ }
73
+ this._lastChar = needle[needle.length - 1];
74
+ this._occ = createOccurenceTable(needle);
75
+ }
76
+ feed(chunk) {
77
+ let pos = 0;
78
+ let tokens;
79
+ const allTokens = [];
80
+ while(pos !== chunk.length){
81
+ [pos, ...tokens] = this._feed(chunk, pos);
82
+ allTokens.push(...tokens);
83
+ }
84
+ return allTokens;
85
+ }
86
+ end() {
87
+ const tail = this._lookbehind;
88
+ this._lookbehind = new Uint8Array();
89
+ return tail;
90
+ }
91
+ _feed(data, bufPos) {
92
+ const tokens = [];
93
+ let pos = -this._lookbehind.length;
94
+ if (pos < 0) {
95
+ while(pos < 0 && pos <= data.length - this._needle.length){
96
+ const ch = this._charAt(data, pos + this._needle.length - 1);
97
+ if (ch === this._lastChar && this._memcmp(data, pos, this._needle.length - 1)) {
98
+ if (pos > -this._lookbehind.length) {
99
+ tokens.push(this._lookbehind.slice(0, this._lookbehind.length + pos));
100
+ }
101
+ tokens.push(MATCH);
102
+ this._lookbehind = new Uint8Array();
103
+ return [
104
+ pos + this._needle.length,
105
+ ...tokens
106
+ ];
107
+ } else {
108
+ pos += this._occ[ch];
109
+ }
110
+ }
111
+ if (pos < 0) {
112
+ while(pos < 0 && !this._memcmp(data, pos, data.length - pos)){
113
+ pos++;
114
+ }
115
+ }
116
+ if (pos >= 0) {
117
+ tokens.push(this._lookbehind);
118
+ this._lookbehind = new Uint8Array();
119
+ } else {
120
+ const bytesToCutOff = this._lookbehind.length + pos;
121
+ if (bytesToCutOff > 0) {
122
+ tokens.push(this._lookbehind.slice(0, bytesToCutOff));
123
+ this._lookbehind = this._lookbehind.slice(bytesToCutOff);
124
+ }
125
+ this._lookbehind = Uint8Array.from(new Array(this._lookbehind.length + data.length), (_, i)=>this._charAt(data, i - this._lookbehind.length));
126
+ return [
127
+ data.length,
128
+ ...tokens
129
+ ];
130
+ }
131
+ }
132
+ pos += bufPos;
133
+ while(pos <= data.length - this._needle.length){
134
+ const ch = data[pos + this._needle.length - 1];
135
+ if (ch === this._lastChar && data[pos] === this._needle[0] && jsmemcmp(this._needle, 0, data, pos, this._needle.length - 1)) {
136
+ if (pos > bufPos) {
137
+ tokens.push(data.slice(bufPos, pos));
138
+ }
139
+ tokens.push(MATCH);
140
+ return [
141
+ pos + this._needle.length,
142
+ ...tokens
143
+ ];
144
+ } else {
145
+ pos += this._occ[ch];
146
+ }
147
+ }
148
+ if (pos < data.length) {
149
+ while(pos < data.length && (data[pos] !== this._needle[0] || !jsmemcmp(data, pos, this._needle, 0, data.length - pos))){
150
+ ++pos;
151
+ }
152
+ if (pos < data.length) {
153
+ this._lookbehind = data.slice(pos);
154
+ }
155
+ }
156
+ if (pos > 0) {
157
+ tokens.push(data.slice(bufPos, pos < data.length ? pos : data.length));
158
+ }
159
+ return [
160
+ data.length,
161
+ ...tokens
162
+ ];
163
+ }
164
+ _charAt(data, pos) {
165
+ if (pos < 0) {
166
+ return this._lookbehind[this._lookbehind.length + pos];
167
+ }
168
+ return data[pos];
169
+ }
170
+ _memcmp(data, pos, len) {
171
+ return jsmemcmp(this._charAt.bind(this, data), pos, this._needle, 0, len);
172
+ }
173
+ }
174
+ class ReadableStreamSearch {
175
+ constructor(needle, _readableStream){
176
+ this._readableStream = _readableStream;
177
+ this._search = new StreamSearch(needle);
178
+ }
179
+ async *[Symbol.asyncIterator]() {
180
+ const reader = this._readableStream.getReader();
181
+ try {
182
+ while(true){
183
+ const result = await reader.read();
184
+ if (result.done) {
185
+ break;
186
+ }
187
+ yield* this._search.feed(result.value);
188
+ }
189
+ const tail = this._search.end();
190
+ if (tail.length) {
191
+ yield tail;
192
+ }
193
+ } finally{
194
+ reader.releaseLock();
195
+ }
196
+ }
197
+ }
198
+
199
+ const mergeArrays2 = Function.prototype.apply.bind(mergeArrays, undefined);
200
+ const dash = stringToArray('--');
201
+ const CRLF = stringToArray('\r\n');
202
+ function parseContentDisposition(header) {
203
+ const parts = header.split(';').map((part)=>part.trim());
204
+ if (parts.shift() !== 'form-data') {
205
+ throw new Error('malformed content-disposition header: missing "form-data" in `' + JSON.stringify(parts) + '`');
206
+ }
207
+ const out = {};
208
+ for (const part of parts){
209
+ const kv = part.split('=', 2);
210
+ if (kv.length !== 2) {
211
+ throw new Error('malformed content-disposition header: key-value pair not found - ' + part + ' in `' + header + '`');
212
+ }
213
+ const [name, value] = kv;
214
+ if (value[0] === '"' && value[value.length - 1] === '"') {
215
+ out[name] = value.slice(1, -1).replace(/\\"/g, '"');
216
+ } else if (value[0] !== '"' && value[value.length - 1] !== '"') {
217
+ out[name] = value;
218
+ } else if (value[0] === '"' && value[value.length - 1] !== '"' || value[0] !== '"' && value[value.length - 1] === '"') {
219
+ throw new Error('malformed content-disposition header: mismatched quotations in `' + header + '`');
220
+ }
221
+ }
222
+ if (!out.name) {
223
+ throw new Error('malformed content-disposition header: missing field name in `' + header + '`');
224
+ }
225
+ return out;
226
+ }
227
+ function parsePartHeaders(lines) {
228
+ const entries = [];
229
+ let disposition = false;
230
+ let line;
231
+ while(typeof (line = lines.shift()) !== 'undefined'){
232
+ const colon = line.indexOf(':');
233
+ if (colon === -1) {
234
+ throw new Error('malformed multipart-form header: missing colon');
235
+ }
236
+ const header = line.slice(0, colon).trim().toLowerCase();
237
+ const value = line.slice(colon + 1).trim();
238
+ switch(header){
239
+ case 'content-disposition':
240
+ disposition = true;
241
+ entries.push(...Object.entries(parseContentDisposition(value)));
242
+ break;
243
+ case 'content-type':
244
+ entries.push([
245
+ 'contentType',
246
+ value
247
+ ]);
248
+ }
249
+ }
250
+ if (!disposition) {
251
+ throw new Error('malformed multipart-form header: missing content-disposition');
252
+ }
253
+ return Object.fromEntries(entries);
254
+ }
255
+ async function readHeaderLines(it, needle) {
256
+ let firstChunk = true;
257
+ let lastTokenWasMatch = false;
258
+ const headerLines = [
259
+ []
260
+ ];
261
+ const crlfSearch = new StreamSearch(CRLF);
262
+ for(;;){
263
+ const result = await it.next();
264
+ if (result.done) {
265
+ throw new Error('malformed multipart-form data: unexpected end of stream');
266
+ }
267
+ if (firstChunk && result.value !== MATCH && arraysEqual(result.value.slice(0, 2), dash)) {
268
+ return [
269
+ undefined,
270
+ new Uint8Array()
271
+ ];
272
+ }
273
+ let chunk;
274
+ if (result.value !== MATCH) {
275
+ chunk = result.value;
276
+ } else if (!lastTokenWasMatch) {
277
+ chunk = needle;
278
+ } else {
279
+ throw new Error('malformed multipart-form data: unexpected boundary');
280
+ }
281
+ if (!chunk.length) {
282
+ continue;
283
+ }
284
+ if (firstChunk) {
285
+ firstChunk = false;
286
+ }
287
+ const tokens = crlfSearch.feed(chunk);
288
+ for (const [i, token] of tokens.entries()){
289
+ const isMatch = token === MATCH;
290
+ if (!isMatch && !token.length) {
291
+ continue;
292
+ }
293
+ if (lastTokenWasMatch && isMatch) {
294
+ tokens.push(crlfSearch.end());
295
+ return [
296
+ headerLines.filter((chunks)=>chunks.length).map(mergeArrays2).map(arrayToString),
297
+ mergeArrays(...tokens.slice(i + 1).map((token)=>token === MATCH ? CRLF : token))
298
+ ];
299
+ }
300
+ if (lastTokenWasMatch = isMatch) {
301
+ headerLines.push([]);
302
+ } else {
303
+ headerLines[headerLines.length - 1].push(token);
304
+ }
305
+ }
306
+ }
307
+ }
308
+ async function* streamMultipart(body, boundary) {
309
+ const needle = mergeArrays(dash, stringToArray(boundary));
310
+ const it = new ReadableStreamSearch(needle, body)[Symbol.asyncIterator]();
311
+ for(;;){
312
+ const result = await it.next();
313
+ if (result.done) {
314
+ return;
315
+ }
316
+ if (result.value === MATCH) {
317
+ break;
318
+ }
319
+ }
320
+ const crlfSearch = new StreamSearch(CRLF);
321
+ for(;;){
322
+ const [headerLines, tail] = await readHeaderLines(it, needle);
323
+ if (!headerLines) {
324
+ return;
325
+ }
326
+ async function nextToken() {
327
+ const result = await it.next();
328
+ if (result.done) {
329
+ throw new Error('malformed multipart-form data: unexpected end of stream');
330
+ }
331
+ return result;
332
+ }
333
+ let trailingCRLF = false;
334
+ function feedChunk(chunk) {
335
+ const chunks = [];
336
+ for (const token of crlfSearch.feed(chunk)){
337
+ if (trailingCRLF) {
338
+ chunks.push(CRLF);
339
+ }
340
+ if (!(trailingCRLF = token === MATCH)) {
341
+ chunks.push(token);
342
+ }
343
+ }
344
+ return mergeArrays(...chunks);
345
+ }
346
+ let done = false;
347
+ async function nextChunk() {
348
+ const result = await nextToken();
349
+ let chunk;
350
+ if (result.value !== MATCH) {
351
+ chunk = result.value;
352
+ } else if (!trailingCRLF) {
353
+ chunk = CRLF;
354
+ } else {
355
+ done = true;
356
+ return {
357
+ value: crlfSearch.end()
358
+ };
359
+ }
360
+ return {
361
+ value: feedChunk(chunk)
362
+ };
363
+ }
364
+ const bufferedChunks = [
365
+ {
366
+ value: feedChunk(tail)
367
+ }
368
+ ];
369
+ yield {
370
+ ...parsePartHeaders(headerLines),
371
+ data: {
372
+ [Symbol.asyncIterator] () {
373
+ return this;
374
+ },
375
+ async next () {
376
+ for(;;){
377
+ const result = bufferedChunks.shift();
378
+ if (!result) {
379
+ break;
380
+ }
381
+ if (result.value.length > 0) {
382
+ return result;
383
+ }
384
+ }
385
+ for(;;){
386
+ if (done) {
387
+ return {
388
+ done,
389
+ value: undefined
390
+ };
391
+ }
392
+ const result = await nextChunk();
393
+ if (result.value.length > 0) {
394
+ return result;
395
+ }
396
+ }
397
+ }
398
+ }
399
+ };
400
+ while(!done){
401
+ bufferedChunks.push(await nextChunk());
402
+ }
403
+ }
404
+ }
405
+
406
+ // @trpc/server
407
+ function createNodeHTTPContentTypeHandler(contentTypeHandler) {
408
+ return ()=>contentTypeHandler;
409
+ }
410
+
411
+ class SliceStream extends Transform {
412
+ constructor(startIndex = 0, endIndex = Infinity){
413
+ super();
414
+ this.startIndex = startIndex;
415
+ this.endIndex = endIndex;
416
+ this.indexOffset = 0;
417
+ this.emitUp = false;
418
+ this.emitDown = false;
419
+ }
420
+ _transform(chunk, _, done) {
421
+ this.indexOffset += chunk.length;
422
+ if (!this.emitUp && this.indexOffset >= this.startIndex) {
423
+ this.emitUp = true;
424
+ const start = chunk.length - (this.indexOffset - this.startIndex);
425
+ if (this.indexOffset > this.endIndex) {
426
+ const end = chunk.length - (this.indexOffset - this.endIndex);
427
+ this.emitDown = true;
428
+ this.push(chunk.slice(start, end));
429
+ } else {
430
+ this.push(chunk.slice(start, chunk.length));
431
+ }
432
+ done();
433
+ return;
434
+ }
435
+ if (this.emitUp && !this.emitDown) {
436
+ if (this.indexOffset >= this.endIndex) {
437
+ this.emitDown = true;
438
+ this.push(chunk.slice(0, chunk.length - (this.indexOffset - this.endIndex)));
439
+ } else {
440
+ this.push(chunk);
441
+ }
442
+ done();
443
+ return;
444
+ }
445
+ done();
446
+ }
447
+ }
448
+ function streamSlice(startIndex = 0, endIndex = Infinity) {
449
+ return new SliceStream(startIndex, endIndex);
450
+ }
451
+
452
+ function composeUploadHandlers(...handlers) {
453
+ return async (part)=>{
454
+ for (const handler of handlers){
455
+ const value = await handler(part);
456
+ if (typeof value !== 'undefined' && value !== null) {
457
+ return value;
458
+ }
459
+ }
460
+ return undefined;
461
+ };
462
+ }
463
+ class MaxPartSizeExceededError extends Error {
464
+ constructor(field, maxBytes){
465
+ super(`Field "${field}" exceeded upload size of ${maxBytes} bytes.`);
466
+ this.field = field;
467
+ this.maxBytes = maxBytes;
468
+ }
469
+ }
470
+ class MaxBodySizeExceededError extends Error {
471
+ constructor(maxBytes){
472
+ super(`Body exceeded upload size of ${maxBytes} bytes.`);
473
+ this.maxBytes = maxBytes;
474
+ }
475
+ }
476
+
477
+ async function readableStreamToString(stream, encoding) {
478
+ const reader = stream.getReader();
479
+ const chunks = [];
480
+ async function read() {
481
+ const { done, value } = await reader.read();
482
+ if (done) {
483
+ return;
484
+ } else if (value) {
485
+ chunks.push(value);
486
+ }
487
+ await read();
488
+ }
489
+ await read();
490
+ return Buffer.concat(chunks).toString(encoding);
491
+ }
492
+ const defaultFilePathResolver = ({ filename })=>{
493
+ const ext = filename ? extname(filename) : '';
494
+ return 'upload_' + randomBytes(4).readUInt32LE(0) + ext;
495
+ };
496
+ async function uniqueFile(filepath) {
497
+ const ext = extname(filepath);
498
+ let uniqueFilepath = filepath;
499
+ for(let i = 1; await stat(uniqueFilepath).then(()=>true).catch(()=>false); i++){
500
+ uniqueFilepath = (ext ? filepath.slice(0, -ext.length) : filepath) + `-${new Date().getTime()}${ext}`;
501
+ }
502
+ return uniqueFilepath;
503
+ }
504
+ function createFileUploadHandler({ directory = tmpdir(), avoidFileConflicts = true, file = defaultFilePathResolver, filter, maxPartSize = 3000000 } = {}) {
505
+ return async ({ name, filename, contentType, data })=>{
506
+ if (!filename || filter && !await filter({
507
+ name,
508
+ filename,
509
+ contentType
510
+ })) {
511
+ return undefined;
512
+ }
513
+ const dir = typeof directory === 'string' ? directory : directory({
514
+ name,
515
+ filename,
516
+ contentType
517
+ });
518
+ if (!dir) {
519
+ return undefined;
520
+ }
521
+ const filedir = resolve(dir);
522
+ const path = typeof file === 'string' ? file : file({
523
+ name,
524
+ filename,
525
+ contentType
526
+ });
527
+ if (!path) {
528
+ return undefined;
529
+ }
530
+ let filepath = resolve(filedir, path);
531
+ if (avoidFileConflicts) {
532
+ filepath = await uniqueFile(filepath);
533
+ }
534
+ await mkdir(dirname(filepath), {
535
+ recursive: true
536
+ }).catch(()=>{});
537
+ const writeFileStream = createWriteStream(filepath);
538
+ let size = 0;
539
+ let deleteFile = false;
540
+ try {
541
+ for await (const chunk of data){
542
+ size += chunk.byteLength;
543
+ if (size > maxPartSize) {
544
+ deleteFile = true;
545
+ throw new MaxPartSizeExceededError(name, maxPartSize);
546
+ }
547
+ writeFileStream.write(chunk);
548
+ }
549
+ } finally{
550
+ writeFileStream.end();
551
+ await promisify(finished)(writeFileStream);
552
+ if (deleteFile) {
553
+ await rm(filepath).catch(()=>{});
554
+ }
555
+ }
556
+ return new NodeOnDiskFile(filepath, contentType);
557
+ };
558
+ }
559
+ let _Symbol_toStringTag = Symbol.toStringTag;
560
+ class NodeOnDiskFile {
561
+ constructor(filepath, type, slicer){
562
+ this.filepath = filepath;
563
+ this.type = type;
564
+ this.slicer = slicer;
565
+ this.lastModified = 0;
566
+ this.webkitRelativePath = '';
567
+ this[_Symbol_toStringTag] = 'File';
568
+ this.name = basename(filepath);
569
+ }
570
+ get size() {
571
+ const stats = statSync(this.filepath);
572
+ if (this.slicer) {
573
+ const slice = this.slicer.end - this.slicer.start;
574
+ return slice < 0 ? 0 : slice > stats.size ? stats.size : slice;
575
+ }
576
+ return stats.size;
577
+ }
578
+ slice(start, end, type) {
579
+ if (typeof start === 'number' && start < 0) start = this.size + start;
580
+ if (typeof end === 'number' && end < 0) end = this.size + end;
581
+ const startOffset = this.slicer?.start ?? 0;
582
+ start = startOffset + (start ?? 0);
583
+ end = startOffset + (end ?? this.size);
584
+ return new NodeOnDiskFile(this.filepath, typeof type === 'string' ? type : this.type, {
585
+ start,
586
+ end
587
+ });
588
+ }
589
+ async arrayBuffer() {
590
+ let stream = createReadStream(this.filepath);
591
+ if (this.slicer) {
592
+ stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
593
+ }
594
+ return new Promise((resolve, reject)=>{
595
+ const buf = [];
596
+ stream.on('data', (chunk)=>buf.push(chunk));
597
+ stream.on('end', ()=>{
598
+ resolve(Buffer.concat(buf));
599
+ });
600
+ stream.on('error', (err)=>{
601
+ reject(err);
602
+ });
603
+ });
604
+ }
605
+ stream() {
606
+ let stream = createReadStream(this.filepath);
607
+ if (this.slicer) {
608
+ stream = stream.pipe(streamSlice(this.slicer.start, this.slicer.end));
609
+ }
610
+ return Readable.toWeb(stream);
611
+ }
612
+ async text() {
613
+ return readableStreamToString(this.stream());
614
+ }
615
+ remove() {
616
+ return unlink(this.filepath);
617
+ }
618
+ getFilePath() {
619
+ return this.filepath;
620
+ }
621
+ }
622
+
623
+ function createMemoryUploadHandler({ filter, maxPartSize = 3000000 } = {}) {
624
+ return async ({ filename, contentType, name, data })=>{
625
+ if (filter && !await filter({
626
+ filename,
627
+ contentType,
628
+ name
629
+ })) {
630
+ return undefined;
631
+ }
632
+ let size = 0;
633
+ const chunks = [];
634
+ for await (const chunk of data){
635
+ size += chunk.byteLength;
636
+ if (size > maxPartSize) {
637
+ throw new MaxPartSizeExceededError(name, maxPartSize);
638
+ }
639
+ chunks.push(chunk);
640
+ }
641
+ return new File(chunks, filename, {
642
+ type: contentType
643
+ });
644
+ };
645
+ }
10
646
 
11
647
  const utfTextDecoder = new TextDecoder('utf-8');
12
648
  /**
@@ -105,4 +741,4 @@ const nodeHTTPFormDataContentTypeHandler = createNodeHTTPContentTypeHandler({
105
741
  }
106
742
  });
107
743
 
108
- export { MaxBodySizeExceededError, NodeOnDiskFile as experimental_NodeOnDiskFile, isMultipartFormDataRequest as experimental_isMultipartFormDataRequest, parseMultipartFormData as experimental_parseMultipartFormData, nodeHTTPFormDataContentTypeHandler };
744
+ export { MaxBodySizeExceededError, MaxPartSizeExceededError, NodeOnDiskFile as experimental_NodeOnDiskFile, composeUploadHandlers as experimental_composeUploadHandlers, createFileUploadHandler as experimental_createFileUploadHandler, createMemoryUploadHandler as experimental_createMemoryUploadHandler, isMultipartFormDataRequest as experimental_isMultipartFormDataRequest, parseMultipartFormData as experimental_parseMultipartFormData, nodeHTTPFormDataContentTypeHandler };