FormDataDecoderStream
import { FormDataDecoderStream } from "jsr:@ende/http/unstable-formdata-decoder-stream";class ยท experimental
class FormDataDecoderStreamOverview
FormDataDecoderStream is a class based off the
RFC 7578 spec and offers a
way to decode a FormData in a streaming manner. Enabling one to
receive large amounts of information and start working with it, without
having it all locally in memory first.
Limitations
Due to the nature of streaming implementations the next entry of this stream
won't be yielded until the ReadableStream value on the entry has been
either fully consumed or cancelled. To not handle it will result in a
hanging effect. Be aware that the client may send you a
multipart/form-data with entries in an undesirable order.
Example: Usage
import { assertEquals } from "@ende/assert";
import { FormDataDecoderStream } from "@ende/http/unstable-formdata-decoder-stream";
const request = new Request("https://example.com", {
method: "POST",
body: function() {
const formData = new FormData();
formData.append("file", "Hello World");
return formData;
}(),
});
for await (const entry of FormDataDecoderStream.from(request)) {
assertEquals(entry.name, "file");
assertEquals(await new Response(entry.value).text(), "Hello World");
}
Constructors
constructor
new FormDataDecoderStream(contentType: string, readable: ReadableStream<Uint8Array>)Constructs a new instance.
contentType: stringThe content type of the form data.
readable: ReadableStream<Uint8Array>The readable stream of the form data.
Static methods
from
static from(request: Request | Response): ReadableStream<FormDataEntry>Creates a ReadableStream from a Request or
Response.
Returns
A ReadableStream containing the decoded form data
entries.
Example: Usage
import { assertEquals } from "@ende/assert";
import { FormDataDecoderStream } from "@ende/http/unstable-formdata-decoder-stream";
const request = new Request("https://example.com", {
method: "POST",
body: function() {
const formData = new FormData();
formData.append("file", "Hello World");
return formData;
}(),
});
for await (const entry of FormDataDecoderStream.from(request)) {
assertEquals(entry.name, "file");
assertEquals(await new Response(entry.value).text(), "Hello World");
}
Methods
readable
get readable(): ReadableStream<FormDataEntry>The ReadableStream containing the decoded form data entries.
Returns
The ReadableStream containing the decoded form data
entries.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataDecoderStream } from "@ende/http/unstable-formdata-decoder-stream";
const request = new Request("https://example.com", {
method: "POST",
body: function() {
const formData = new FormData();
formData.append("file", "Hello World");
return formData;
}(),
});
const contentType = request.headers.get("Content-Type");
assert(typeof contentType === "string");
const readable = request.body;
assert(readable != undefined);
for await (
const entry of new FormDataDecoderStream(contentType, readable).readable
) {
assertEquals(entry.name, "file");
assertEquals(await new Response(entry.value).text(), "Hello World");
}