@trpc/server 11.0.0-next-beta.206 → 11.0.0-next-beta.216
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/adapters/aws-lambda/index.js +14 -105
- package/dist/adapters/aws-lambda/index.mjs +1 -90
- package/dist/adapters/aws-lambda/utils.js +100 -0
- package/dist/adapters/aws-lambda/utils.mjs +93 -0
- package/dist/adapters/express.js +1 -7
- package/dist/adapters/express.mjs +1 -5
- package/dist/adapters/fastify/fastifyRequestHandler.js +81 -0
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +79 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +51 -0
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +49 -0
- package/dist/adapters/fastify/index.js +4 -128
- package/dist/adapters/fastify/index.mjs +2 -128
- package/dist/adapters/fetch/fetchRequestHandler.js +118 -0
- package/dist/adapters/fetch/fetchRequestHandler.mjs +116 -0
- package/dist/adapters/fetch/index.js +2 -115
- package/dist/adapters/fetch/index.mjs +1 -116
- package/dist/adapters/next.js +1 -6
- package/dist/adapters/next.mjs +1 -4
- 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.js +42 -0
- package/dist/adapters/node-http/content-type/json/getPostBody.mjs +40 -0
- package/dist/adapters/node-http/content-type/json/index.js +3 -42
- package/dist/adapters/node-http/content-type/json/index.mjs +2 -39
- package/dist/adapters/node-http/index.js +1 -7
- package/dist/adapters/node-http/index.mjs +1 -5
- package/dist/{contentType-72ed9df5.mjs → adapters/node-http/internals/contentType.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-83441c73.js → adapters/node-http/nodeHTTPRequestHandler.js} +2 -2
- package/dist/{nodeHTTPRequestHandler-0223fac5.mjs → adapters/node-http/nodeHTTPRequestHandler.mjs} +2 -2
- package/dist/adapters/standalone.js +2 -12
- package/dist/adapters/standalone.mjs +1 -5
- package/dist/adapters/ws.js +0 -2
- package/dist/bundle-analysis.json +97 -97
- package/dist/http.js +1 -3
- package/dist/index.js +10 -12
- 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.js +1 -3
- package/dist/rpc.js +1 -3
- package/dist/shared.js +2 -4
- package/package.json +4 -4
- package/dist/contentType-24c44bba.js +0 -5
- package/dist/nodeHTTPRequestHandler-aa0dce4e.js +0 -105
- /package/dist/{contentType-d9d22104.js → adapters/node-http/internals/contentType.js} +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var search = require('./search.js');
|
|
4
|
+
var utils = require('./utils.js');
|
|
5
|
+
|
|
6
|
+
const mergeArrays2 = Function.prototype.apply.bind(utils.mergeArrays, undefined);
|
|
7
|
+
const dash = utils.stringToArray('--');
|
|
8
|
+
const CRLF = utils.stringToArray('\r\n');
|
|
9
|
+
function parseContentDisposition(header) {
|
|
10
|
+
const parts = header.split(';').map(part => part.trim());
|
|
11
|
+
if (parts.shift() !== 'form-data') {
|
|
12
|
+
throw new Error('malformed content-disposition header: missing "form-data" in `' + JSON.stringify(parts) + '`');
|
|
13
|
+
}
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const part of parts) {
|
|
16
|
+
const kv = part.split('=', 2);
|
|
17
|
+
if (kv.length !== 2) {
|
|
18
|
+
throw new Error('malformed content-disposition header: key-value pair not found - ' + part + ' in `' + header + '`');
|
|
19
|
+
}
|
|
20
|
+
const [name, value] = kv;
|
|
21
|
+
if (value[0] === '"' && value[value.length - 1] === '"') {
|
|
22
|
+
out[name] = value.slice(1, -1).replace(/\\"/g, '"');
|
|
23
|
+
} else if (value[0] !== '"' && value[value.length - 1] !== '"') {
|
|
24
|
+
out[name] = value;
|
|
25
|
+
} else if (value[0] === '"' && value[value.length - 1] !== '"' || value[0] !== '"' && value[value.length - 1] === '"') {
|
|
26
|
+
throw new Error('malformed content-disposition header: mismatched quotations in `' + header + '`');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!out.name) {
|
|
30
|
+
throw new Error('malformed content-disposition header: missing field name in `' + header + '`');
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
function parsePartHeaders(lines) {
|
|
35
|
+
const entries = [];
|
|
36
|
+
let disposition = false;
|
|
37
|
+
let line;
|
|
38
|
+
while (typeof (line = lines.shift()) !== 'undefined') {
|
|
39
|
+
const colon = line.indexOf(':');
|
|
40
|
+
if (colon === -1) {
|
|
41
|
+
throw new Error('malformed multipart-form header: missing colon');
|
|
42
|
+
}
|
|
43
|
+
const header = line.slice(0, colon).trim().toLowerCase();
|
|
44
|
+
const value = line.slice(colon + 1).trim();
|
|
45
|
+
switch (header) {
|
|
46
|
+
case 'content-disposition':
|
|
47
|
+
disposition = true;
|
|
48
|
+
entries.push(...Object.entries(parseContentDisposition(value)));
|
|
49
|
+
break;
|
|
50
|
+
case 'content-type':
|
|
51
|
+
entries.push([
|
|
52
|
+
'contentType',
|
|
53
|
+
value
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!disposition) {
|
|
58
|
+
throw new Error('malformed multipart-form header: missing content-disposition');
|
|
59
|
+
}
|
|
60
|
+
return Object.fromEntries(entries);
|
|
61
|
+
}
|
|
62
|
+
async function readHeaderLines(it, needle) {
|
|
63
|
+
let firstChunk = true;
|
|
64
|
+
let lastTokenWasMatch = false;
|
|
65
|
+
const headerLines = [[]];
|
|
66
|
+
const crlfSearch = new search.StreamSearch(CRLF);
|
|
67
|
+
for (;;) {
|
|
68
|
+
const result = await it.next();
|
|
69
|
+
if (result.done) {
|
|
70
|
+
throw new Error('malformed multipart-form data: unexpected end of stream');
|
|
71
|
+
}
|
|
72
|
+
if (firstChunk && result.value !== search.MATCH && utils.arraysEqual(result.value.slice(0, 2), dash)) {
|
|
73
|
+
return [
|
|
74
|
+
undefined,
|
|
75
|
+
new Uint8Array()
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
let chunk;
|
|
79
|
+
if (result.value !== search.MATCH) {
|
|
80
|
+
chunk = result.value;
|
|
81
|
+
} else if (!lastTokenWasMatch) {
|
|
82
|
+
chunk = needle;
|
|
83
|
+
} else {
|
|
84
|
+
throw new Error('malformed multipart-form data: unexpected boundary');
|
|
85
|
+
}
|
|
86
|
+
if (!chunk.length) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (firstChunk) {
|
|
90
|
+
firstChunk = false;
|
|
91
|
+
}
|
|
92
|
+
const tokens = crlfSearch.feed(chunk);
|
|
93
|
+
for (const [i, token] of tokens.entries()) {
|
|
94
|
+
const isMatch = token === search.MATCH;
|
|
95
|
+
if (!isMatch && !token.length) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (lastTokenWasMatch && isMatch) {
|
|
99
|
+
tokens.push(crlfSearch.end());
|
|
100
|
+
return [
|
|
101
|
+
headerLines.filter(chunks => chunks.length).map(mergeArrays2).map(utils.arrayToString),
|
|
102
|
+
utils.mergeArrays(...tokens.slice(i + 1).map(token => token === search.MATCH ? CRLF : token))
|
|
103
|
+
];
|
|
104
|
+
}
|
|
105
|
+
if (lastTokenWasMatch = isMatch) {
|
|
106
|
+
headerLines.push([]);
|
|
107
|
+
} else {
|
|
108
|
+
headerLines[headerLines.length - 1].push(token);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async function* streamMultipart(body, boundary) {
|
|
114
|
+
const needle = utils.mergeArrays(dash, utils.stringToArray(boundary));
|
|
115
|
+
const it = new search.ReadableStreamSearch(needle, body)[Symbol.asyncIterator]();
|
|
116
|
+
for (;;) {
|
|
117
|
+
const result = await it.next();
|
|
118
|
+
if (result.done) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (result.value === search.MATCH) {
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const crlfSearch = new search.StreamSearch(CRLF);
|
|
126
|
+
for (;;) {
|
|
127
|
+
const [headerLines, tail] = await readHeaderLines(it, needle);
|
|
128
|
+
if (!headerLines) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
async function nextToken() {
|
|
132
|
+
const result = await it.next();
|
|
133
|
+
if (result.done) {
|
|
134
|
+
throw new Error('malformed multipart-form data: unexpected end of stream');
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
let trailingCRLF = false;
|
|
139
|
+
function feedChunk(chunk) {
|
|
140
|
+
const chunks = [];
|
|
141
|
+
for (const token of crlfSearch.feed(chunk)) {
|
|
142
|
+
if (trailingCRLF) {
|
|
143
|
+
chunks.push(CRLF);
|
|
144
|
+
}
|
|
145
|
+
if (!(trailingCRLF = token === search.MATCH)) {
|
|
146
|
+
chunks.push(token);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return utils.mergeArrays(...chunks);
|
|
150
|
+
}
|
|
151
|
+
let done = false;
|
|
152
|
+
async function nextChunk() {
|
|
153
|
+
const result = await nextToken();
|
|
154
|
+
let chunk;
|
|
155
|
+
if (result.value !== search.MATCH) {
|
|
156
|
+
chunk = result.value;
|
|
157
|
+
} else if (!trailingCRLF) {
|
|
158
|
+
chunk = CRLF;
|
|
159
|
+
} else {
|
|
160
|
+
done = true;
|
|
161
|
+
return { value: crlfSearch.end() };
|
|
162
|
+
}
|
|
163
|
+
return { value: feedChunk(chunk) };
|
|
164
|
+
}
|
|
165
|
+
const bufferedChunks = [{ value: feedChunk(tail) }];
|
|
166
|
+
yield {
|
|
167
|
+
...parsePartHeaders(headerLines),
|
|
168
|
+
data: {
|
|
169
|
+
[Symbol.asyncIterator]() {
|
|
170
|
+
return this;
|
|
171
|
+
},
|
|
172
|
+
async next() {
|
|
173
|
+
for (;;) {
|
|
174
|
+
const result = bufferedChunks.shift();
|
|
175
|
+
if (!result) {
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
if (result.value.length > 0) {
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (;;) {
|
|
183
|
+
if (done) {
|
|
184
|
+
return {
|
|
185
|
+
done,
|
|
186
|
+
value: undefined
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
const result = await nextChunk();
|
|
190
|
+
if (result.value.length > 0) {
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
while (!done) {
|
|
198
|
+
bufferedChunks.push(await nextChunk());
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
exports.streamMultipart = streamMultipart;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ReadableStreamSearch, MATCH, StreamSearch } from './search.mjs';
|
|
2
|
+
import { mergeArrays, stringToArray, arraysEqual, arrayToString } from './utils.mjs';
|
|
3
|
+
|
|
4
|
+
const mergeArrays2 = Function.prototype.apply.bind(mergeArrays, undefined);
|
|
5
|
+
const dash = stringToArray('--');
|
|
6
|
+
const CRLF = stringToArray('\r\n');
|
|
7
|
+
function parseContentDisposition(header) {
|
|
8
|
+
const parts = header.split(';').map(part => part.trim());
|
|
9
|
+
if (parts.shift() !== 'form-data') {
|
|
10
|
+
throw new Error('malformed content-disposition header: missing "form-data" in `' + JSON.stringify(parts) + '`');
|
|
11
|
+
}
|
|
12
|
+
const out = {};
|
|
13
|
+
for (const part of parts) {
|
|
14
|
+
const kv = part.split('=', 2);
|
|
15
|
+
if (kv.length !== 2) {
|
|
16
|
+
throw new Error('malformed content-disposition header: key-value pair not found - ' + part + ' in `' + header + '`');
|
|
17
|
+
}
|
|
18
|
+
const [name, value] = kv;
|
|
19
|
+
if (value[0] === '"' && value[value.length - 1] === '"') {
|
|
20
|
+
out[name] = value.slice(1, -1).replace(/\\"/g, '"');
|
|
21
|
+
} else if (value[0] !== '"' && value[value.length - 1] !== '"') {
|
|
22
|
+
out[name] = value;
|
|
23
|
+
} else if (value[0] === '"' && value[value.length - 1] !== '"' || value[0] !== '"' && value[value.length - 1] === '"') {
|
|
24
|
+
throw new Error('malformed content-disposition header: mismatched quotations in `' + header + '`');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (!out.name) {
|
|
28
|
+
throw new Error('malformed content-disposition header: missing field name in `' + header + '`');
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
function parsePartHeaders(lines) {
|
|
33
|
+
const entries = [];
|
|
34
|
+
let disposition = false;
|
|
35
|
+
let line;
|
|
36
|
+
while (typeof (line = lines.shift()) !== 'undefined') {
|
|
37
|
+
const colon = line.indexOf(':');
|
|
38
|
+
if (colon === -1) {
|
|
39
|
+
throw new Error('malformed multipart-form header: missing colon');
|
|
40
|
+
}
|
|
41
|
+
const header = line.slice(0, colon).trim().toLowerCase();
|
|
42
|
+
const value = line.slice(colon + 1).trim();
|
|
43
|
+
switch (header) {
|
|
44
|
+
case 'content-disposition':
|
|
45
|
+
disposition = true;
|
|
46
|
+
entries.push(...Object.entries(parseContentDisposition(value)));
|
|
47
|
+
break;
|
|
48
|
+
case 'content-type':
|
|
49
|
+
entries.push([
|
|
50
|
+
'contentType',
|
|
51
|
+
value
|
|
52
|
+
]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!disposition) {
|
|
56
|
+
throw new Error('malformed multipart-form header: missing content-disposition');
|
|
57
|
+
}
|
|
58
|
+
return Object.fromEntries(entries);
|
|
59
|
+
}
|
|
60
|
+
async function readHeaderLines(it, needle) {
|
|
61
|
+
let firstChunk = true;
|
|
62
|
+
let lastTokenWasMatch = false;
|
|
63
|
+
const headerLines = [[]];
|
|
64
|
+
const crlfSearch = new StreamSearch(CRLF);
|
|
65
|
+
for (;;) {
|
|
66
|
+
const result = await it.next();
|
|
67
|
+
if (result.done) {
|
|
68
|
+
throw new Error('malformed multipart-form data: unexpected end of stream');
|
|
69
|
+
}
|
|
70
|
+
if (firstChunk && result.value !== MATCH && arraysEqual(result.value.slice(0, 2), dash)) {
|
|
71
|
+
return [
|
|
72
|
+
undefined,
|
|
73
|
+
new Uint8Array()
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
let chunk;
|
|
77
|
+
if (result.value !== MATCH) {
|
|
78
|
+
chunk = result.value;
|
|
79
|
+
} else if (!lastTokenWasMatch) {
|
|
80
|
+
chunk = needle;
|
|
81
|
+
} else {
|
|
82
|
+
throw new Error('malformed multipart-form data: unexpected boundary');
|
|
83
|
+
}
|
|
84
|
+
if (!chunk.length) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (firstChunk) {
|
|
88
|
+
firstChunk = false;
|
|
89
|
+
}
|
|
90
|
+
const tokens = crlfSearch.feed(chunk);
|
|
91
|
+
for (const [i, token] of tokens.entries()) {
|
|
92
|
+
const isMatch = token === MATCH;
|
|
93
|
+
if (!isMatch && !token.length) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (lastTokenWasMatch && isMatch) {
|
|
97
|
+
tokens.push(crlfSearch.end());
|
|
98
|
+
return [
|
|
99
|
+
headerLines.filter(chunks => chunks.length).map(mergeArrays2).map(arrayToString),
|
|
100
|
+
mergeArrays(...tokens.slice(i + 1).map(token => token === MATCH ? CRLF : token))
|
|
101
|
+
];
|
|
102
|
+
}
|
|
103
|
+
if (lastTokenWasMatch = isMatch) {
|
|
104
|
+
headerLines.push([]);
|
|
105
|
+
} else {
|
|
106
|
+
headerLines[headerLines.length - 1].push(token);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function* streamMultipart(body, boundary) {
|
|
112
|
+
const needle = mergeArrays(dash, stringToArray(boundary));
|
|
113
|
+
const it = new ReadableStreamSearch(needle, body)[Symbol.asyncIterator]();
|
|
114
|
+
for (;;) {
|
|
115
|
+
const result = await it.next();
|
|
116
|
+
if (result.done) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (result.value === MATCH) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const crlfSearch = new StreamSearch(CRLF);
|
|
124
|
+
for (;;) {
|
|
125
|
+
const [headerLines, tail] = await readHeaderLines(it, needle);
|
|
126
|
+
if (!headerLines) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
async function nextToken() {
|
|
130
|
+
const result = await it.next();
|
|
131
|
+
if (result.done) {
|
|
132
|
+
throw new Error('malformed multipart-form data: unexpected end of stream');
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
let trailingCRLF = false;
|
|
137
|
+
function feedChunk(chunk) {
|
|
138
|
+
const chunks = [];
|
|
139
|
+
for (const token of crlfSearch.feed(chunk)) {
|
|
140
|
+
if (trailingCRLF) {
|
|
141
|
+
chunks.push(CRLF);
|
|
142
|
+
}
|
|
143
|
+
if (!(trailingCRLF = token === MATCH)) {
|
|
144
|
+
chunks.push(token);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return mergeArrays(...chunks);
|
|
148
|
+
}
|
|
149
|
+
let done = false;
|
|
150
|
+
async function nextChunk() {
|
|
151
|
+
const result = await nextToken();
|
|
152
|
+
let chunk;
|
|
153
|
+
if (result.value !== MATCH) {
|
|
154
|
+
chunk = result.value;
|
|
155
|
+
} else if (!trailingCRLF) {
|
|
156
|
+
chunk = CRLF;
|
|
157
|
+
} else {
|
|
158
|
+
done = true;
|
|
159
|
+
return { value: crlfSearch.end() };
|
|
160
|
+
}
|
|
161
|
+
return { value: feedChunk(chunk) };
|
|
162
|
+
}
|
|
163
|
+
const bufferedChunks = [{ value: feedChunk(tail) }];
|
|
164
|
+
yield {
|
|
165
|
+
...parsePartHeaders(headerLines),
|
|
166
|
+
data: {
|
|
167
|
+
[Symbol.asyncIterator]() {
|
|
168
|
+
return this;
|
|
169
|
+
},
|
|
170
|
+
async next() {
|
|
171
|
+
for (;;) {
|
|
172
|
+
const result = bufferedChunks.shift();
|
|
173
|
+
if (!result) {
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
if (result.value.length > 0) {
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
for (;;) {
|
|
181
|
+
if (done) {
|
|
182
|
+
return {
|
|
183
|
+
done,
|
|
184
|
+
value: undefined
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
const result = await nextChunk();
|
|
188
|
+
if (result.value.length > 0) {
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
while (!done) {
|
|
196
|
+
bufferedChunks.push(await nextChunk());
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export { streamMultipart };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('./utils.js');
|
|
4
|
+
|
|
5
|
+
function coerce(a) {
|
|
6
|
+
if (a instanceof Uint8Array) {
|
|
7
|
+
return index => a[index];
|
|
8
|
+
}
|
|
9
|
+
return a;
|
|
10
|
+
}
|
|
11
|
+
function jsmemcmp(buf1, pos1, buf2, pos2, len) {
|
|
12
|
+
const fn1 = coerce(buf1);
|
|
13
|
+
const fn2 = coerce(buf2);
|
|
14
|
+
for (let i = 0; i < len; ++i) {
|
|
15
|
+
if (fn1(pos1 + i) !== fn2(pos2 + i)) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
function createOccurenceTable(s) {
|
|
22
|
+
const table = new Array(256).fill(s.length);
|
|
23
|
+
if (s.length > 1) {
|
|
24
|
+
for (let i = 0; i < s.length - 1; i++) {
|
|
25
|
+
table[s[i]] = s.length - 1 - i;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return table;
|
|
29
|
+
}
|
|
30
|
+
const MATCH = Symbol('Match');
|
|
31
|
+
class StreamSearch {
|
|
32
|
+
constructor(needle) {
|
|
33
|
+
this._lookbehind = new Uint8Array();
|
|
34
|
+
if (typeof needle === 'string') {
|
|
35
|
+
this._needle = needle = utils.stringToArray(needle);
|
|
36
|
+
} else {
|
|
37
|
+
this._needle = needle;
|
|
38
|
+
}
|
|
39
|
+
this._lastChar = needle[needle.length - 1];
|
|
40
|
+
this._occ = createOccurenceTable(needle);
|
|
41
|
+
}
|
|
42
|
+
feed(chunk) {
|
|
43
|
+
let pos = 0;
|
|
44
|
+
let tokens;
|
|
45
|
+
const allTokens = [];
|
|
46
|
+
while (pos !== chunk.length) {
|
|
47
|
+
[pos, ...tokens] = this._feed(chunk, pos);
|
|
48
|
+
allTokens.push(...tokens);
|
|
49
|
+
}
|
|
50
|
+
return allTokens;
|
|
51
|
+
}
|
|
52
|
+
end() {
|
|
53
|
+
const tail = this._lookbehind;
|
|
54
|
+
this._lookbehind = new Uint8Array();
|
|
55
|
+
return tail;
|
|
56
|
+
}
|
|
57
|
+
_feed(data, bufPos) {
|
|
58
|
+
const tokens = [];
|
|
59
|
+
let pos = -this._lookbehind.length;
|
|
60
|
+
if (pos < 0) {
|
|
61
|
+
while (pos < 0 && pos <= data.length - this._needle.length) {
|
|
62
|
+
const ch = this._charAt(data, pos + this._needle.length - 1);
|
|
63
|
+
if (ch === this._lastChar && this._memcmp(data, pos, this._needle.length - 1)) {
|
|
64
|
+
if (pos > -this._lookbehind.length) {
|
|
65
|
+
tokens.push(this._lookbehind.slice(0, this._lookbehind.length + pos));
|
|
66
|
+
}
|
|
67
|
+
tokens.push(MATCH);
|
|
68
|
+
this._lookbehind = new Uint8Array();
|
|
69
|
+
return [
|
|
70
|
+
pos + this._needle.length,
|
|
71
|
+
...tokens
|
|
72
|
+
];
|
|
73
|
+
} else {
|
|
74
|
+
pos += this._occ[ch];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (pos < 0) {
|
|
78
|
+
while (pos < 0 && !this._memcmp(data, pos, data.length - pos)) {
|
|
79
|
+
pos++;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (pos >= 0) {
|
|
83
|
+
tokens.push(this._lookbehind);
|
|
84
|
+
this._lookbehind = new Uint8Array();
|
|
85
|
+
} else {
|
|
86
|
+
const bytesToCutOff = this._lookbehind.length + pos;
|
|
87
|
+
if (bytesToCutOff > 0) {
|
|
88
|
+
tokens.push(this._lookbehind.slice(0, bytesToCutOff));
|
|
89
|
+
this._lookbehind = this._lookbehind.slice(bytesToCutOff);
|
|
90
|
+
}
|
|
91
|
+
this._lookbehind = Uint8Array.from(new Array(this._lookbehind.length + data.length), (_, i) => this._charAt(data, i - this._lookbehind.length));
|
|
92
|
+
return [
|
|
93
|
+
data.length,
|
|
94
|
+
...tokens
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
pos += bufPos;
|
|
99
|
+
while (pos <= data.length - this._needle.length) {
|
|
100
|
+
const ch = data[pos + this._needle.length - 1];
|
|
101
|
+
if (ch === this._lastChar && data[pos] === this._needle[0] && jsmemcmp(this._needle, 0, data, pos, this._needle.length - 1)) {
|
|
102
|
+
if (pos > bufPos) {
|
|
103
|
+
tokens.push(data.slice(bufPos, pos));
|
|
104
|
+
}
|
|
105
|
+
tokens.push(MATCH);
|
|
106
|
+
return [
|
|
107
|
+
pos + this._needle.length,
|
|
108
|
+
...tokens
|
|
109
|
+
];
|
|
110
|
+
} else {
|
|
111
|
+
pos += this._occ[ch];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (pos < data.length) {
|
|
115
|
+
while (pos < data.length && (data[pos] !== this._needle[0] || !jsmemcmp(data, pos, this._needle, 0, data.length - pos))) {
|
|
116
|
+
++pos;
|
|
117
|
+
}
|
|
118
|
+
if (pos < data.length) {
|
|
119
|
+
this._lookbehind = data.slice(pos);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (pos > 0) {
|
|
123
|
+
tokens.push(data.slice(bufPos, pos < data.length ? pos : data.length));
|
|
124
|
+
}
|
|
125
|
+
return [
|
|
126
|
+
data.length,
|
|
127
|
+
...tokens
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
_charAt(data, pos) {
|
|
131
|
+
if (pos < 0) {
|
|
132
|
+
return this._lookbehind[this._lookbehind.length + pos];
|
|
133
|
+
}
|
|
134
|
+
return data[pos];
|
|
135
|
+
}
|
|
136
|
+
_memcmp(data, pos, len) {
|
|
137
|
+
return jsmemcmp(this._charAt.bind(this, data), pos, this._needle, 0, len);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
class ReadableStreamSearch {
|
|
141
|
+
constructor(needle, _readableStream) {
|
|
142
|
+
this._readableStream = _readableStream;
|
|
143
|
+
this._search = new StreamSearch(needle);
|
|
144
|
+
}
|
|
145
|
+
async *[Symbol.asyncIterator]() {
|
|
146
|
+
const reader = this._readableStream.getReader();
|
|
147
|
+
try {
|
|
148
|
+
while (true) {
|
|
149
|
+
const result = await reader.read();
|
|
150
|
+
if (result.done) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
yield* this._search.feed(result.value);
|
|
154
|
+
}
|
|
155
|
+
const tail = this._search.end();
|
|
156
|
+
if (tail.length) {
|
|
157
|
+
yield tail;
|
|
158
|
+
}
|
|
159
|
+
} finally {
|
|
160
|
+
reader.releaseLock();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
exports.MATCH = MATCH;
|
|
166
|
+
exports.ReadableStreamSearch = ReadableStreamSearch;
|
|
167
|
+
exports.StreamSearch = StreamSearch;
|