* Constructs a new instance. * * @param readable The readable stream of form data inputs.
(readable: ReadableStream<FormDataInput>)
| 86 | * @param readable The readable stream of form data inputs. |
| 87 | */ |
| 88 | constructor(readable: ReadableStream<FormDataInput>) { |
| 89 | const boundary = "--deno-std-" + |
| 90 | encodeBase64(crypto.getRandomValues(new Uint8Array(30))) + |
| 91 | "\r\n"; |
| 92 | this.#encoder |
| 93 | .encodeInto(boundary, this.#boundary = new Uint8Array(boundary.length)); |
| 94 | this.#contentType = 'multipart/form-data; boundary="' + |
| 95 | boundary.slice(2, -2) + '"'; |
| 96 | |
| 97 | const gen = this.#handle(readable); |
| 98 | this.#readable = new ReadableStream({ |
| 99 | type: "bytes", |
| 100 | autoAllocateChunkSize: 1024, |
| 101 | async start() { |
| 102 | await gen.next(); |
| 103 | }, |
| 104 | async pull(controller) { |
| 105 | const length = controller.byobRequest!.view!.byteLength; |
| 106 | const buffer = new Uint8Array( |
| 107 | controller.byobRequest!.view!.buffer, |
| 108 | controller.byobRequest!.view!.byteOffset, |
| 109 | length, |
| 110 | ); |
| 111 | |
| 112 | try { |
| 113 | const { done, value } = await gen.next(buffer); |
| 114 | if (done) { |
| 115 | controller.close(); |
| 116 | return controller.byobRequest!.respond(0); |
| 117 | } |
| 118 | |
| 119 | // deno-lint-ignore no-explicit-any |
| 120 | if ((buffer.buffer as any).detached) { |
| 121 | controller.byobRequest!.respondWithNewView(value); |
| 122 | } else if (buffer.buffer === value.buffer) { |
| 123 | controller.byobRequest!.respond(value.length); |
| 124 | } else { |
| 125 | buffer.set(value.subarray(0, length)); |
| 126 | controller.byobRequest!.respond(length); |
| 127 | controller.enqueue(value.subarray(length)); |
| 128 | } |
| 129 | } catch (error) { |
| 130 | controller.error(error); |
| 131 | } |
| 132 | }, |
| 133 | async cancel(reason) { |
| 134 | await gen.throw(reason).catch(() => undefined); |
| 135 | }, |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | async *#handle( |
| 140 | readable: ReadableStream<FormDataInput>, |
nothing calls this directly
no test coverage detected