FormDataEncoderStream
import { FormDataEncoderStream } from "jsr:@ende/http/unstable-formdata-encoder-stream";class ยท experimental
class FormDataEncoderStreamOverview
FormDataEncoderStream is a class based off the
RFC 7578 spec and offers a
way to create a FormData in a streaming manner. Enabling one to
send large amounts of information without having it all locally in memory
first.
Limitations
Due to the structure of the multipart media type and the constraints of a
streaming implementation, it is not possible to guarantee that a chosen
boundary string will never occur within the payload itself. As a result, the
appearance of a boundary sequence within the content must be considered a
valid and unavoidable edge case inherent to this API. Each instance of this
class will result in a different boundary being generated.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const response = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]))
.toResponse();
const formData = await response.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));
Constructors
constructor
new FormDataEncoderStream(readable: ReadableStream<FormDataInput>)Constructs a new instance.
readable: ReadableStream<FormDataInput>The readable stream of form data inputs.
Static methods
from
static from(readable: ReadableStream<FormDataInput>): FormDataEncoderStreamCreate a FormDataEncoderStream from a
ReadableStream.
readable: ReadableStream<FormDataInput>The
ReadableStreamto encode.
Returns
The FormDataEncoderStream containing the encoded form
data.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const response = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]))
.toResponse();
const formData = await response.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));
Methods
toResponse
toResponse(init?: ResponseInit): ResponseConvert the FormDataEncoderStream to a Response to send to a
client.
init?: ResponseInitOptional response initialization options.
Returns
The Response containing the encoded form data.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const response = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]))
.toResponse();
const formData = await response.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));
toRequest
toRequest(input: RequestInfo | URL, init?: RequestInit): RequestConvert the FormDataEncoderStream to a Request to send to a server.
input: RequestInfo | URLThe URL or RequestInfo to send the request to.
init?: RequestInitOptional request initialization options.
Returns
The Request containing the encoded form data.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const request = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]))
.toRequest("https://example.com", { method: "POST" });
const formData = await request.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));
contentType
get contentType(): stringThe content type of the ReadableStream. Contains the boundary
required for decoding.
Returns
The content type of the ReadableStream.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const encoder = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]));
const response = new Response(encoder.readable, {
headers: { "Content-Type": encoder.contentType },
});
const formData = await response.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));
readable
get readable(): ReadableStream<Uint8Array>The ReadableStream containing the encoded content.
Returns
The ReadableStream containing the encoded form data.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { FormDataEncoderStream } from "@ende/http/unstable-formdata-encoder-stream";
const encoder = FormDataEncoderStream.from(ReadableStream.from([
{
name: "file",
value: (await Ende.open("ende.json")).readable,
contentType: "application/json",
}
]));
const response = new Response(encoder.readable, {
headers: { "Content-Type": encoder.contentType },
});
const formData = await response.formData();
const file = formData.get("file");
assert(typeof file !== "string");
assertEquals(await file?.text(), await Ende.readTextFile("ende.json"));