MCPcopy Create free account
hub / github.com/denoland/std / #handle

Method #handle

http/unstable_formdata_encoder_stream.ts:139–196  ·  view source on GitHub ↗
(
    readable: ReadableStream<FormDataInput>,
  )

Source from the content-addressed store, hash-verified

137 }
138
139 async *#handle(
140 readable: ReadableStream<FormDataInput>,
141 ): AsyncGenerator<Uint8Array, void, Uint8Array> {
142 const fixed = [
143 this.#encoder.encode('Content-Disposition: form-data; name="'),
144 this.#encoder.encode('"; filename="'),
145 this.#encoder.encode('"\r\nContent-Type: '),
146 this.#encoder.encode('text/plain; charset="UTF-8"\r\n\r\n'),
147 this.#encoder.encode("application/octet-stream\r\n\r\n"),
148 this.#encoder.encode("\r\n\r\n"),
149 this.#encoder.encode("\r\n"),
150 ];
151 let buffer = yield new Uint8Array();
152 for await (const input of readable) {
153 buffer = yield this.#setBuffer(buffer, this.#boundary);
154
155 buffer = yield this.#setBuffer(buffer, fixed[0]!);
156 buffer = yield this.#setString(buffer, input.name);
157 if (input.filename != undefined || typeof input.value !== "string") {
158 buffer = yield this.#setBuffer(buffer, fixed[1]!);
159 buffer = yield this.#setString(buffer, input.filename ?? "blob");
160 }
161 buffer = yield this.#setBuffer(buffer, fixed[2]!);
162
163 if (input.contentType != undefined) {
164 buffer = yield this.#setString(buffer, input.contentType);
165 buffer = yield this.#setBuffer(buffer, fixed[5]!);
166 } else if (typeof input.value === "string") {
167 buffer = yield this.#setBuffer(buffer, fixed[3]!);
168 } else if (input.value instanceof Blob && input.value.type.length) {
169 buffer = yield this.#setString(buffer, input.value.type);
170 buffer = yield this.#setBuffer(buffer, fixed[5]!);
171 } else {
172 buffer = yield this.#setBuffer(buffer, fixed[4]!);
173 }
174
175 if (typeof input.value === "string") {
176 buffer = yield this.#setString(buffer, input.value);
177 } else {
178 if (input.value instanceof Blob) input.value = input.value.stream();
179 const reader = toByteStream(input.value).getReader({ mode: "byob" });
180 try {
181 while (true) {
182 const { done, value } = await reader
183 .read(buffer, { min: buffer.length });
184 buffer = yield value!;
185 if (done) break;
186 }
187 } catch (reason) {
188 await reader.cancel(reason);
189 throw reason;
190 }
191 }
192 buffer = yield this.#setBuffer(buffer, fixed[6]!);
193 }
194 this.#boundary.set([45, 45], this.#boundary.length - 2);
195 yield this.#setBuffer(buffer, this.#boundary);
196 }

Callers 1

constructorMethod · 0.95

Calls 6

#setBufferMethod · 0.95
#setStringMethod · 0.95
toByteStreamFunction · 0.85
readMethod · 0.65
setMethod · 0.65
cancelMethod · 0.45

Tested by

no test coverage detected