ovr 6.2.4 → 6.2.5
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/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +103 -61
- package/dist/render/index.d.ts.map +1 -1
- package/dist/render/index.js +12 -4
- package/package.json +1 -1
- package/src/multipart/index.ts +125 -85
- package/src/render/index.ts +8 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/multipart/index.ts"],"names":[],"mappings":"AAgCA,qBAAqB;AACrB,cAAM,IAAK,SAAQ,OAAO;IACzB;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAGjC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAG7B,gBAAgB;IAChB,SAAiB,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;IAE/D;;;;;;OAMG;IACH,YAAY,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EA4BrE;IAED;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAQzC;CACD;AAED,yBAAiB,SAAS,CAAC;IAC1B,wBAAwB;IACxB,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;;;;WAeG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAEhB;;;;;;;;;;;;;;;;WAgBG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QAEjB;;;;;;;;;;WAUG;QACH,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAC3B,CAAC;IAEF,8CAA8C;IAC9C,KAAY,IAAI,GAAG,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC;CAC7C;AAED,wBAAwB;AACxB,qBAAa,SAAU,SAAQ,OAAO;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/multipart/index.ts"],"names":[],"mappings":"AAgCA,qBAAqB;AACrB,cAAM,IAAK,SAAQ,OAAO;IACzB;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;;;OAIG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IAGjC;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAG7B,gBAAgB;IAChB,SAAiB,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC;IAE/D;;;;;;OAMG;IACH,YAAY,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,UAAU,EAAE,UAAU,EA4BrE;IAED;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,kBAAkB,CAAC,CAQzC;CACD;AAED,yBAAiB,SAAS,CAAC;IAC1B,wBAAwB;IACxB,KAAY,OAAO,GAAG;QACrB;;;;;;;;;;;;;;;WAeG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAEhB;;;;;;;;;;;;;;;;WAgBG;QACH,OAAO,CAAC,EAAE,MAAM,CAAC;QAEjB;;;;;;;;;;WAUG;QACH,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAC3B,CAAC;IAEF,8CAA8C;IAC9C,KAAY,IAAI,GAAG,YAAY,CAAC,OAAO,IAAI,CAAC,CAAC;CAC7C;AAED,wBAAwB;AACxB,qBAAa,SAAU,SAAQ,OAAO;;IAyCrC;;;;;;;;;;;;;;OAcG;IACH,YAAY,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,SAAS,CAAC,OAAO,EA0BpD;IAyMD,4CAA4C;IACrC,CAAC,MAAM,CAAC,aAAa,CAAC,wCA4C5B;IAED;;;;;;;OAOG;IACG,IAAI,sBAQT;CACD"}
|
package/dist/multipart/index.js
CHANGED
|
@@ -102,7 +102,9 @@ export class Multipart extends Request {
|
|
|
102
102
|
/** Request body reader */
|
|
103
103
|
#reader;
|
|
104
104
|
/** Current values being buffered in memory */
|
|
105
|
-
#memory;
|
|
105
|
+
#memory = new Uint8Array(0);
|
|
106
|
+
/** Owned storage for headers and boundaries spanning input chunks. */
|
|
107
|
+
#scratch;
|
|
106
108
|
/** Opening boundary needle */
|
|
107
109
|
#opening;
|
|
108
110
|
/** Part boundary needle */
|
|
@@ -111,6 +113,8 @@ export class Multipart extends Request {
|
|
|
111
113
|
#valid = 0;
|
|
112
114
|
/** Start index of the found needle */
|
|
113
115
|
#start = 0;
|
|
116
|
+
/** Start of unread data within the buffer. */
|
|
117
|
+
#offset = 0;
|
|
114
118
|
/** End index of the found needle */
|
|
115
119
|
#end = 0;
|
|
116
120
|
/** Total bytes read from the stream */
|
|
@@ -144,22 +148,15 @@ export class Multipart extends Request {
|
|
|
144
148
|
this.#reader = this.body.getReader();
|
|
145
149
|
this.#opening = new Needle(`--${boundary}\r\n`);
|
|
146
150
|
this.#boundary = new Needle(`\r\n--${boundary}`);
|
|
147
|
-
this.#
|
|
151
|
+
this.#scratch = new Uint8Array(new ArrayBuffer(
|
|
148
152
|
// slightly larger than common chunk size/high water mark 64kb for leftover boundary
|
|
149
153
|
// prevents having to resize memory for most requests
|
|
150
154
|
65 * Size.kb,
|
|
151
155
|
// cap max chunk size + leftover
|
|
152
156
|
{ maxByteLength: this.#options.memory }));
|
|
153
157
|
}
|
|
154
|
-
/**
|
|
155
|
-
|
|
156
|
-
* onto the buffer.
|
|
157
|
-
*
|
|
158
|
-
* @param append
|
|
159
|
-
* @returns `true` if done
|
|
160
|
-
*/
|
|
161
|
-
async #read(append = true) {
|
|
162
|
-
const next = await this.#reader.read();
|
|
158
|
+
/** Account for a source read and append or temporarily borrow its bytes. */
|
|
159
|
+
#read(next, append = true, borrow = false) {
|
|
163
160
|
if (next.done)
|
|
164
161
|
return true;
|
|
165
162
|
const nextLength = next.value.length;
|
|
@@ -167,35 +164,48 @@ export class Multipart extends Request {
|
|
|
167
164
|
throw new RangeError("Payload Too Large");
|
|
168
165
|
}
|
|
169
166
|
if (append) {
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this.#memory.buffer.resize(Math.min(this.#options.memory, // in case double is larger than the max
|
|
175
|
-
Math.max(required, size * 2)));
|
|
167
|
+
const length = this.#valid - this.#offset;
|
|
168
|
+
const required = length + nextLength;
|
|
169
|
+
if (required > this.#scratch.buffer.maxByteLength) {
|
|
170
|
+
throw new RangeError("Multipart buffer limit exceeded");
|
|
176
171
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
172
|
+
if (length || !borrow) {
|
|
173
|
+
this.#retain();
|
|
174
|
+
if (required > this.#scratch.length) {
|
|
175
|
+
this.#scratch.buffer.resize(Math.min(this.#scratch.buffer.maxByteLength, Math.max(required, this.#scratch.length * 2)));
|
|
176
|
+
}
|
|
177
|
+
this.#scratch.set(next.value, length);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
// The caller must consume or retain these bytes before yielding.
|
|
181
|
+
this.#memory = next.value;
|
|
182
|
+
}
|
|
183
|
+
this.#valid = required;
|
|
184
|
+
this.#start -= this.#offset;
|
|
185
|
+
this.#end -= this.#offset;
|
|
186
|
+
this.#offset = 0;
|
|
180
187
|
}
|
|
181
188
|
}
|
|
189
|
+
/** Preserve unread bytes before the source can reuse its input storage. */
|
|
190
|
+
#retain() {
|
|
191
|
+
const length = this.#valid - this.#offset;
|
|
192
|
+
if (length > this.#scratch.length)
|
|
193
|
+
this.#scratch.buffer.resize(length);
|
|
194
|
+
this.#scratch.set(this.#memory.subarray(this.#offset, this.#valid));
|
|
195
|
+
this.#memory = this.#scratch;
|
|
196
|
+
this.#valid = length;
|
|
197
|
+
this.#start -= this.#offset;
|
|
198
|
+
this.#end -= this.#offset;
|
|
199
|
+
this.#offset = 0;
|
|
200
|
+
}
|
|
182
201
|
/**
|
|
183
202
|
* Cuts off the buffer < end index.
|
|
184
203
|
*
|
|
185
204
|
* @returns Shifted off buffer < start index
|
|
186
205
|
*/
|
|
187
206
|
#shift() {
|
|
188
|
-
const before = this.#memory.
|
|
189
|
-
|
|
190
|
-
const remainder = this.#valid - this.#end;
|
|
191
|
-
if (remainder) {
|
|
192
|
-
// copy remainder to the start of the buffer
|
|
193
|
-
// this doesn't move the chunks, just copies to the start
|
|
194
|
-
// old will be overwritten
|
|
195
|
-
this.#memory.copyWithin(0, this.#end, this.#valid);
|
|
196
|
-
}
|
|
197
|
-
this.#valid = remainder;
|
|
198
|
-
this.#start = this.#end = 0;
|
|
207
|
+
const before = this.#memory.subarray(this.#offset, this.#start);
|
|
208
|
+
this.#offset = this.#start = this.#end;
|
|
199
209
|
return before;
|
|
200
210
|
}
|
|
201
211
|
/**
|
|
@@ -223,8 +233,7 @@ export class Multipart extends Request {
|
|
|
223
233
|
}
|
|
224
234
|
// where to safely start the next search in the concatenated chunk result
|
|
225
235
|
// go back len - 1 in case it was partially at the end
|
|
226
|
-
this.#start = this.#end =
|
|
227
|
-
needle.length < haystackLength ? haystackLength - (needle.length - 1) : 0;
|
|
236
|
+
this.#start = this.#end = Math.max(this.#offset, haystackLength - (needle.length - 1));
|
|
228
237
|
}
|
|
229
238
|
/**
|
|
230
239
|
* Tries to find the needle in the buffer, if not found, the next
|
|
@@ -235,7 +244,8 @@ export class Multipart extends Request {
|
|
|
235
244
|
*/
|
|
236
245
|
async #findConcat(needle) {
|
|
237
246
|
let found;
|
|
238
|
-
while (!(found = this.#find(needle)) &&
|
|
247
|
+
while (!(found = this.#find(needle)) &&
|
|
248
|
+
!this.#read(await this.#reader.read()))
|
|
239
249
|
;
|
|
240
250
|
return found;
|
|
241
251
|
}
|
|
@@ -244,38 +254,70 @@ export class Multipart extends Request {
|
|
|
244
254
|
* @returns Stream that streams the content until the next find
|
|
245
255
|
*/
|
|
246
256
|
#findStream(needle) {
|
|
257
|
+
let pending;
|
|
258
|
+
let ended = false;
|
|
247
259
|
return new ReadableStream({
|
|
248
260
|
type: "bytes",
|
|
249
261
|
pull: async (c) => {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
262
|
+
if (!pending?.length) {
|
|
263
|
+
let found;
|
|
264
|
+
for (let done; !(found = this.#find(needle)) && !done; done = this.#read(await this.#reader.read(), true, true)) {
|
|
265
|
+
// Retain only a suffix that could begin a boundary.
|
|
266
|
+
this.#start = this.#end = this.#valid;
|
|
267
|
+
const last = this.#valid - 1;
|
|
268
|
+
const indices = this.#valid > this.#offset
|
|
269
|
+
? needle.loc[this.#memory[last]]
|
|
270
|
+
: undefined;
|
|
271
|
+
if (indices) {
|
|
272
|
+
search: for (let i = indices.length - 1; i >= 0; i--) {
|
|
273
|
+
for (let match = indices[i], cursor = last; match >= 0 &&
|
|
274
|
+
cursor >= this.#offset &&
|
|
275
|
+
needle[match] === this.#memory[cursor]; match--, cursor--) {
|
|
276
|
+
if (match === 0) {
|
|
277
|
+
this.#start = this.#end = cursor;
|
|
278
|
+
break search;
|
|
279
|
+
}
|
|
266
280
|
}
|
|
267
281
|
}
|
|
268
282
|
}
|
|
283
|
+
pending = this.#shift();
|
|
284
|
+
if (pending.length)
|
|
285
|
+
break;
|
|
286
|
+
if (this.#memory !== this.#scratch)
|
|
287
|
+
this.#retain();
|
|
288
|
+
}
|
|
289
|
+
if (!pending?.length) {
|
|
290
|
+
pending = found;
|
|
291
|
+
ended = true;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (pending?.length) {
|
|
295
|
+
const request = c.byobRequest;
|
|
296
|
+
const view = request?.view;
|
|
297
|
+
if (view) {
|
|
298
|
+
const length = Math.min(view.byteLength, pending.length);
|
|
299
|
+
new Uint8Array(view.buffer, view.byteOffset, length).set(pending.subarray(0, length));
|
|
300
|
+
pending = pending.subarray(length);
|
|
301
|
+
request.respond(length);
|
|
269
302
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
c.enqueue(
|
|
273
|
-
|
|
303
|
+
else {
|
|
304
|
+
// Byte-stream enqueue transfers this independent allocation.
|
|
305
|
+
c.enqueue(new Uint8Array(pending));
|
|
306
|
+
pending = undefined;
|
|
274
307
|
}
|
|
275
308
|
}
|
|
276
|
-
if (
|
|
277
|
-
|
|
278
|
-
|
|
309
|
+
if (this.#memory !== this.#scratch) {
|
|
310
|
+
// Source buffers may be reused on the next pull. Borrow only
|
|
311
|
+
// within this synchronous continuation, including small BYOB reads.
|
|
312
|
+
if (pending?.length)
|
|
313
|
+
pending = new Uint8Array(pending);
|
|
314
|
+
this.#retain();
|
|
315
|
+
}
|
|
316
|
+
if (ended && !pending?.length) {
|
|
317
|
+
c.close();
|
|
318
|
+
// An empty part or final short BYOB read still needs a response.
|
|
319
|
+
c.byobRequest?.respond(0);
|
|
320
|
+
}
|
|
279
321
|
},
|
|
280
322
|
});
|
|
281
323
|
}
|
|
@@ -303,13 +345,13 @@ export class Multipart extends Request {
|
|
|
303
345
|
}
|
|
304
346
|
if (
|
|
305
347
|
// ensures invalid characters aren't used to determine if it's done
|
|
306
|
-
this.#valid > 1 &&
|
|
348
|
+
this.#valid - this.#offset > 1 &&
|
|
307
349
|
// check for --
|
|
308
350
|
// if not done it would be \r\nNEXT-HEADER
|
|
309
|
-
this.#memory[
|
|
310
|
-
this.#memory[1] === 45) {
|
|
351
|
+
this.#memory[this.#offset] === 45 &&
|
|
352
|
+
this.#memory[this.#offset + 1] === 45) {
|
|
311
353
|
// done, drain any epilogue
|
|
312
|
-
while (!(await this.#read(false))
|
|
354
|
+
while (!this.#read(await this.#reader.read(), false))
|
|
313
355
|
;
|
|
314
356
|
break;
|
|
315
357
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAY3C,wDAAwD;AACxD,cAAM,KAAK;;IAIV;;;;;OAKG;IACH,YAAY,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO,EAGxC;IAED;;OAEG;IACH,QAAQ,WAEP;IAED;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,KAAK,QAElB;CACD;AAED,yBAAiB,MAAM,CAAC;IACvB,qBAAqB;IACrB,KAAY,OAAO,GAAG;QACrB,yDAAyD;QACzD,IAAI,CAAC,EAAE,OAAO,CAAC;KACf,CAAC;IAEF,gCAAgC;IAChC,KAAY,KAAK,GAAG,YAAY,CAAC,OAAO,KAAK,CAAC,CAAC;CAC/C;AAED,qBAAa,MAAM;;IAgBlB;;;;;OAKG;IACH,YAAY,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,EAGzD;IAED,iDAAiD;IAC1C,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/render/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAY3C,wDAAwD;AACxD,cAAM,KAAK;;IAIV;;;;;OAKG;IACH,YAAY,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO,EAGxC;IAED;;OAEG;IACH,QAAQ,WAEP;IAED;;OAEG;IACH,MAAM,CAAC,KAAK,EAAE,KAAK,QAElB;CACD;AAED,yBAAiB,MAAM,CAAC;IACvB,qBAAqB;IACrB,KAAY,OAAO,GAAG;QACrB,yDAAyD;QACzD,IAAI,CAAC,EAAE,OAAO,CAAC;KACf,CAAC;IAEF,gCAAgC;IAChC,KAAY,KAAK,GAAG,YAAY,CAAC,OAAO,KAAK,CAAC,CAAC;CAC/C;AAED,qBAAa,MAAM;;IAgBlB;;;;;OAKG;IACH,YAAY,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,EAGzD;IAED,iDAAiD;IAC1C,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAuFpE;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,+CAuC3D;IAED;;;;;;;;;;OAUG;IACG,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,OAAO,mBAE1D;IAED;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,SAExB;IAED;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,UAOzC;CA0DD"}
|
package/dist/render/index.js
CHANGED
|
@@ -70,16 +70,24 @@ export class Render {
|
|
|
70
70
|
if (typeof element === "object") {
|
|
71
71
|
if (Symbol.asyncIterator in element) {
|
|
72
72
|
// any async iterable - lazily resolve
|
|
73
|
-
for await (const children of element)
|
|
74
|
-
|
|
73
|
+
for await (const children of element) {
|
|
74
|
+
if (children instanceof Chunk)
|
|
75
|
+
yield children;
|
|
76
|
+
else
|
|
77
|
+
yield* new Render(children, this.#options);
|
|
78
|
+
}
|
|
75
79
|
return;
|
|
76
80
|
}
|
|
77
81
|
if (Symbol.iterator in element) {
|
|
78
82
|
// sync iterable
|
|
79
83
|
if ("next" in element) {
|
|
80
84
|
// sync generator - lazily resolve, avoids loading all in memory
|
|
81
|
-
for (const children of element)
|
|
82
|
-
|
|
85
|
+
for (const children of element) {
|
|
86
|
+
if (children instanceof Chunk)
|
|
87
|
+
yield children;
|
|
88
|
+
else
|
|
89
|
+
yield* new Render(children, this.#options);
|
|
90
|
+
}
|
|
83
91
|
return;
|
|
84
92
|
}
|
|
85
93
|
// other iterable - array, set, etc.
|
package/package.json
CHANGED
package/src/multipart/index.ts
CHANGED
|
@@ -188,7 +188,10 @@ export class Multipart extends Request {
|
|
|
188
188
|
readonly #reader: ReadableStreamDefaultReader;
|
|
189
189
|
|
|
190
190
|
/** Current values being buffered in memory */
|
|
191
|
-
|
|
191
|
+
#memory = new Uint8Array(0);
|
|
192
|
+
|
|
193
|
+
/** Owned storage for headers and boundaries spanning input chunks. */
|
|
194
|
+
readonly #scratch: Uint8Array<ArrayBuffer>;
|
|
192
195
|
|
|
193
196
|
/** Opening boundary needle */
|
|
194
197
|
readonly #opening: Needle;
|
|
@@ -202,6 +205,9 @@ export class Multipart extends Request {
|
|
|
202
205
|
/** Start index of the found needle */
|
|
203
206
|
#start = 0;
|
|
204
207
|
|
|
208
|
+
/** Start of unread data within the buffer. */
|
|
209
|
+
#offset = 0;
|
|
210
|
+
|
|
205
211
|
/** End index of the found needle */
|
|
206
212
|
#end = 0;
|
|
207
213
|
|
|
@@ -240,7 +246,7 @@ export class Multipart extends Request {
|
|
|
240
246
|
this.#opening = new Needle(`--${boundary}\r\n`);
|
|
241
247
|
this.#boundary = new Needle(`\r\n--${boundary}`);
|
|
242
248
|
|
|
243
|
-
this.#
|
|
249
|
+
this.#scratch = new Uint8Array(
|
|
244
250
|
new ArrayBuffer(
|
|
245
251
|
// slightly larger than common chunk size/high water mark 64kb for leftover boundary
|
|
246
252
|
// prevents having to resize memory for most requests
|
|
@@ -251,64 +257,65 @@ export class Multipart extends Request {
|
|
|
251
257
|
);
|
|
252
258
|
}
|
|
253
259
|
|
|
254
|
-
/**
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
*/
|
|
261
|
-
async #read(append = true) {
|
|
262
|
-
const next = await this.#reader.read();
|
|
263
|
-
|
|
260
|
+
/** Account for a source read and append or temporarily borrow its bytes. */
|
|
261
|
+
#read(
|
|
262
|
+
next: ReadableStreamReadResult<Uint8Array<ArrayBuffer>>,
|
|
263
|
+
append = true,
|
|
264
|
+
borrow = false,
|
|
265
|
+
) {
|
|
264
266
|
if (next.done) return true;
|
|
265
|
-
|
|
266
267
|
const nextLength = next.value.length;
|
|
267
|
-
|
|
268
268
|
if ((this.#payloadSize += nextLength) > this.#options.payload) {
|
|
269
269
|
throw new RangeError("Payload Too Large");
|
|
270
270
|
}
|
|
271
|
-
|
|
272
271
|
if (append) {
|
|
273
|
-
const
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (required > size) {
|
|
278
|
-
this.#memory.buffer.resize(
|
|
279
|
-
Math.min(
|
|
280
|
-
this.#options.memory, // in case double is larger than the max
|
|
281
|
-
Math.max(required, size * 2),
|
|
282
|
-
),
|
|
283
|
-
);
|
|
272
|
+
const length = this.#valid - this.#offset;
|
|
273
|
+
const required = length + nextLength;
|
|
274
|
+
if (required > this.#scratch.buffer.maxByteLength) {
|
|
275
|
+
throw new RangeError("Multipart buffer limit exceeded");
|
|
284
276
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
277
|
+
if (length || !borrow) {
|
|
278
|
+
this.#retain();
|
|
279
|
+
if (required > this.#scratch.length) {
|
|
280
|
+
this.#scratch.buffer.resize(
|
|
281
|
+
Math.min(
|
|
282
|
+
this.#scratch.buffer.maxByteLength,
|
|
283
|
+
Math.max(required, this.#scratch.length * 2),
|
|
284
|
+
),
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
this.#scratch.set(next.value, length);
|
|
288
|
+
} else {
|
|
289
|
+
// The caller must consume or retain these bytes before yielding.
|
|
290
|
+
this.#memory = next.value;
|
|
291
|
+
}
|
|
292
|
+
this.#valid = required;
|
|
293
|
+
this.#start -= this.#offset;
|
|
294
|
+
this.#end -= this.#offset;
|
|
295
|
+
this.#offset = 0;
|
|
289
296
|
}
|
|
290
297
|
}
|
|
291
298
|
|
|
299
|
+
/** Preserve unread bytes before the source can reuse its input storage. */
|
|
300
|
+
#retain() {
|
|
301
|
+
const length = this.#valid - this.#offset;
|
|
302
|
+
if (length > this.#scratch.length) this.#scratch.buffer.resize(length);
|
|
303
|
+
this.#scratch.set(this.#memory.subarray(this.#offset, this.#valid));
|
|
304
|
+
this.#memory = this.#scratch;
|
|
305
|
+
this.#valid = length;
|
|
306
|
+
this.#start -= this.#offset;
|
|
307
|
+
this.#end -= this.#offset;
|
|
308
|
+
this.#offset = 0;
|
|
309
|
+
}
|
|
310
|
+
|
|
292
311
|
/**
|
|
293
312
|
* Cuts off the buffer < end index.
|
|
294
313
|
*
|
|
295
314
|
* @returns Shifted off buffer < start index
|
|
296
315
|
*/
|
|
297
316
|
#shift() {
|
|
298
|
-
const before = this.#memory.
|
|
299
|
-
|
|
300
|
-
// how much data is left after the found needle
|
|
301
|
-
const remainder = this.#valid - this.#end;
|
|
302
|
-
|
|
303
|
-
if (remainder) {
|
|
304
|
-
// copy remainder to the start of the buffer
|
|
305
|
-
// this doesn't move the chunks, just copies to the start
|
|
306
|
-
// old will be overwritten
|
|
307
|
-
this.#memory.copyWithin(0, this.#end, this.#valid);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
this.#valid = remainder;
|
|
311
|
-
this.#start = this.#end = 0;
|
|
317
|
+
const before = this.#memory.subarray(this.#offset, this.#start);
|
|
318
|
+
this.#offset = this.#start = this.#end;
|
|
312
319
|
|
|
313
320
|
return before;
|
|
314
321
|
}
|
|
@@ -346,8 +353,10 @@ export class Multipart extends Request {
|
|
|
346
353
|
|
|
347
354
|
// where to safely start the next search in the concatenated chunk result
|
|
348
355
|
// go back len - 1 in case it was partially at the end
|
|
349
|
-
this.#start = this.#end =
|
|
350
|
-
|
|
356
|
+
this.#start = this.#end = Math.max(
|
|
357
|
+
this.#offset,
|
|
358
|
+
haystackLength - (needle.length - 1),
|
|
359
|
+
);
|
|
351
360
|
}
|
|
352
361
|
|
|
353
362
|
/**
|
|
@@ -359,7 +368,10 @@ export class Multipart extends Request {
|
|
|
359
368
|
*/
|
|
360
369
|
async #findConcat(needle: Needle) {
|
|
361
370
|
let found: Uint8Array | undefined;
|
|
362
|
-
while (
|
|
371
|
+
while (
|
|
372
|
+
!(found = this.#find(needle)) &&
|
|
373
|
+
!this.#read(await this.#reader.read())
|
|
374
|
+
);
|
|
363
375
|
return found;
|
|
364
376
|
}
|
|
365
377
|
|
|
@@ -368,50 +380,78 @@ export class Multipart extends Request {
|
|
|
368
380
|
* @returns Stream that streams the content until the next find
|
|
369
381
|
*/
|
|
370
382
|
#findStream(needle: Needle) {
|
|
383
|
+
let pending: Uint8Array<ArrayBuffer> | undefined;
|
|
384
|
+
let ended = false;
|
|
371
385
|
return new ReadableStream({
|
|
372
386
|
type: "bytes",
|
|
373
387
|
pull: async (c) => {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
for (
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
388
|
+
if (!pending?.length) {
|
|
389
|
+
let found: Uint8Array<ArrayBuffer> | undefined;
|
|
390
|
+
for (
|
|
391
|
+
let done: boolean | undefined;
|
|
392
|
+
!(found = this.#find(needle)) && !done;
|
|
393
|
+
done = this.#read(await this.#reader.read(), true, true)
|
|
394
|
+
) {
|
|
395
|
+
// Retain only a suffix that could begin a boundary.
|
|
396
|
+
this.#start = this.#end = this.#valid;
|
|
397
|
+
const last = this.#valid - 1;
|
|
398
|
+
const indices =
|
|
399
|
+
this.#valid > this.#offset
|
|
400
|
+
? needle.loc[this.#memory[last]!]
|
|
401
|
+
: undefined;
|
|
402
|
+
if (indices) {
|
|
403
|
+
search: for (let i = indices.length - 1; i >= 0; i--) {
|
|
404
|
+
for (
|
|
405
|
+
let match = indices[i]!, cursor = last;
|
|
406
|
+
match >= 0 &&
|
|
407
|
+
cursor >= this.#offset &&
|
|
408
|
+
needle[match] === this.#memory[cursor];
|
|
409
|
+
match--, cursor--
|
|
410
|
+
) {
|
|
411
|
+
if (match === 0) {
|
|
412
|
+
this.#start = this.#end = cursor;
|
|
413
|
+
break search;
|
|
414
|
+
}
|
|
400
415
|
}
|
|
401
416
|
}
|
|
402
417
|
}
|
|
418
|
+
pending = this.#shift();
|
|
419
|
+
if (pending.length) break;
|
|
420
|
+
if (this.#memory !== this.#scratch) this.#retain();
|
|
403
421
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
c.enqueue(before);
|
|
408
|
-
return; // wait for next pull
|
|
422
|
+
if (!pending?.length) {
|
|
423
|
+
pending = found;
|
|
424
|
+
ended = true;
|
|
409
425
|
}
|
|
410
426
|
}
|
|
411
427
|
|
|
412
|
-
if (
|
|
413
|
-
|
|
414
|
-
|
|
428
|
+
if (pending?.length) {
|
|
429
|
+
const request = c.byobRequest;
|
|
430
|
+
const view = request?.view;
|
|
431
|
+
if (view) {
|
|
432
|
+
const length = Math.min(view.byteLength, pending.length);
|
|
433
|
+
new Uint8Array(view.buffer, view.byteOffset, length).set(
|
|
434
|
+
pending.subarray(0, length),
|
|
435
|
+
);
|
|
436
|
+
pending = pending.subarray(length);
|
|
437
|
+
request.respond(length);
|
|
438
|
+
} else {
|
|
439
|
+
// Byte-stream enqueue transfers this independent allocation.
|
|
440
|
+
c.enqueue(new Uint8Array(pending));
|
|
441
|
+
pending = undefined;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (this.#memory !== this.#scratch) {
|
|
445
|
+
// Source buffers may be reused on the next pull. Borrow only
|
|
446
|
+
// within this synchronous continuation, including small BYOB reads.
|
|
447
|
+
if (pending?.length) pending = new Uint8Array(pending);
|
|
448
|
+
this.#retain();
|
|
449
|
+
}
|
|
450
|
+
if (ended && !pending?.length) {
|
|
451
|
+
c.close();
|
|
452
|
+
// An empty part or final short BYOB read still needs a response.
|
|
453
|
+
c.byobRequest?.respond(0);
|
|
454
|
+
}
|
|
415
455
|
},
|
|
416
456
|
});
|
|
417
457
|
}
|
|
@@ -447,14 +487,14 @@ export class Multipart extends Request {
|
|
|
447
487
|
|
|
448
488
|
if (
|
|
449
489
|
// ensures invalid characters aren't used to determine if it's done
|
|
450
|
-
this.#valid > 1 &&
|
|
490
|
+
this.#valid - this.#offset > 1 &&
|
|
451
491
|
// check for --
|
|
452
492
|
// if not done it would be \r\nNEXT-HEADER
|
|
453
|
-
this.#memory[
|
|
454
|
-
this.#memory[1] === 45
|
|
493
|
+
this.#memory[this.#offset] === 45 &&
|
|
494
|
+
this.#memory[this.#offset + 1] === 45
|
|
455
495
|
) {
|
|
456
496
|
// done, drain any epilogue
|
|
457
|
-
while (!(await this.#read(false))
|
|
497
|
+
while (!this.#read(await this.#reader.read(), false));
|
|
458
498
|
break;
|
|
459
499
|
}
|
|
460
500
|
}
|
package/src/render/index.ts
CHANGED
|
@@ -105,8 +105,10 @@ export class Render {
|
|
|
105
105
|
if (typeof element === "object") {
|
|
106
106
|
if (Symbol.asyncIterator in element) {
|
|
107
107
|
// any async iterable - lazily resolve
|
|
108
|
-
for await (const children of element)
|
|
109
|
-
|
|
108
|
+
for await (const children of element) {
|
|
109
|
+
if (children instanceof Chunk) yield children;
|
|
110
|
+
else yield* new Render(children, this.#options);
|
|
111
|
+
}
|
|
110
112
|
return;
|
|
111
113
|
}
|
|
112
114
|
|
|
@@ -114,8 +116,10 @@ export class Render {
|
|
|
114
116
|
// sync iterable
|
|
115
117
|
if ("next" in element) {
|
|
116
118
|
// sync generator - lazily resolve, avoids loading all in memory
|
|
117
|
-
for (const children of element)
|
|
118
|
-
|
|
119
|
+
for (const children of element) {
|
|
120
|
+
if (children instanceof Chunk) yield children;
|
|
121
|
+
else yield* new Render(children, this.#options);
|
|
122
|
+
}
|
|
119
123
|
return;
|
|
120
124
|
}
|
|
121
125
|
|