FastlyBody
A low-level API for constructing and manipulating HTTP message bodies outside of the standard Fetch API.
Note: Can only be used when processing requests, not during build-time initialization.
123456789import { FastlyBody } from 'fastly:body';
addEventListener('fetch', (event) => { const body = new FastlyBody(); body.append('Hello, '); body.append('world!'); const chunk = body.read(1024); event.respondWith(new Response(chunk, { status: 200 }));});Constructor
Methods
abandon
View sourceabandon(): voidAbandons the body, discarding its contents without finalizing. Unlike close, this signals that the body is being discarded rather than completed.
Returns
append
View sourceAppends data to the end of this body.
Parameters
- data: BodyInit
The data to append.
Returns
Throws
TypeErrorifdatais a guest-backedReadableStream(not yet supported), or if theReadableStreamis unusable (locked or already read from).
close
View sourceclose(): voidCloses the body, signaling that no more data will be written.
Returns
concat
View sourceconcat(dest: FastlyBody): voidAppends the contents of another FastlyBody to this body.
The dest body is consumed by this operation and should not be reused.
Parameters
- dest: FastlyBody
The
FastlyBodyto append to this body.
Returns
Throws
Errorifdestis not aFastlyBodyinstance.
prepend
View sourcePrepends data to the beginning of this body.
Parameters
- data: BodyInit
The data to prepend.
Returns
Throws
TypeErrorifdatais a guest-backedReadableStream(not yet supported), or if theReadableStreamis unusable (locked or already read from).
read
View sourceReads up to chunkSize bytes from the body, advancing the read position.
Returns an empty ArrayBuffer when no more data is available.
Parameters
- chunkSize: number
The maximum number of bytes to read. Must be a positive number.
Returns
Throws
ErrorifchunkSizeis not a positive number, isNaN, or isInfinity.