FastlyBody

Classin body@fastly/js-compute 4.0.0since 3.9.0View source

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.

1
2
3
4
5
6
7
8
9
import { 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

new FastlyBody()

Creates a new, empty FastlyBody instance.

Methods

abandon(): void

Abandons the body, discarding its contents without finalizing. Unlike close, this signals that the body is being discarded rather than completed.

Returns

void
append(data: BodyInit): void

Appends data to the end of this body.

Parameters

data: BodyInit

The data to append.

Returns

void

Throws

  • TypeError if data is a guest-backed ReadableStream (not yet supported), or if the ReadableStream is unusable (locked or already read from).
close(): void

Closes the body, signaling that no more data will be written.

Returns

void
concat(dest: FastlyBody): void

Appends 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 FastlyBody to append to this body.

Returns

void

Throws

  • Error if dest is not a FastlyBody instance.
prepend(data: BodyInit): void

Prepends data to the beginning of this body.

Parameters

data: BodyInit

The data to prepend.

Returns

void

Throws

  • TypeError if data is a guest-backed ReadableStream (not yet supported), or if the ReadableStream is unusable (locked or already read from).
read(chunkSize: number): ArrayBuffer

Reads 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

  • Error if chunkSize is not a positive number, is NaN, or is Infinity.