Skip to main content

Class: Encoder

@divine/uri.Encoder

The base class for all encoder subclasses. Encoders can be constructed manually, but usually aren't. Instead, this class provides the static methods encode and decode.

Encoders transform byte streams and are used, among other things, to handle the content-encoding, content-transfer-encoding and transfer-encoding headers in MIME and HTTP.

Below is a list of all known encoders:

EncodingEncoder class
7bitIdentityEncoder
8bitIdentityEncoder
base64Base64Encoder
base64urlBase64Encoder
binaryIdentityEncoder
brZlibEncoder
deflateZlibEncoder
gzipZlibEncoder
identityIdentityEncoder
quoted-printableQuotedPrintableEncoder
x-gzipZlibEncoder

Hierarchy

Constructors

constructor

Protected new Encoder(type)

Constructs a new Encoder instance.

Parameters

NameTypeDescription
typestringThe encoding format this encoder object was instanciated for.

Defined in

uri/src/encoders.ts:114

Properties

type

Readonly type: string

The encoding format this encoder object was instanciated for.

Defined in

uri/src/encoders.ts:114

Methods

decode

Abstract decode(stream): AsyncIterable<Buffer>

Decodes the provided byte stream into an new byte stream.

This method must be implemented by the actual subclass.

Throws

EncoderError On decoding errors.

Parameters

NameTypeDescription
streamAsyncIterable<Buffer>The stream to decode.

Returns

AsyncIterable<Buffer>

The decoded stream.

Defined in

uri/src/encoders.ts:138


encode

Abstract encode(stream): AsyncIterable<Buffer>

Encodes the provided byte stream into an new byte stream.

This method must be implemented by the actual subclass.

Throws

EncoderError On encoding errors.

Parameters

NameTypeDescription
streamAsyncIterable<Buffer>The stream to encode.

Returns

AsyncIterable<Buffer>

The encoded stream.

Defined in

uri/src/encoders.ts:127


decode

Static decode(stream, types): Readable & AsyncIterable<Buffer>

Decodes the provided stream using one or more encoders.

Throws

EncoderError On decoding errors or if the encoding format is not recognized.

Parameters

NameTypeDescription
streamstring | Buffer | AsyncIterable<Buffer>The data to encode. If a string, it will first converted to UTF-8.
typesstring | string[]An encoding format or an ordered list of encoding formats to apply (in reverse!) to the stream. A list may either be a comma-separated string or an array of strings.

Returns

Readable & AsyncIterable<Buffer>

An encoded byte stream.

Defined in

uri/src/encoders.ts:80


encode

Static encode(stream, types): Readable & AsyncIterable<Buffer>

Encodes the provided stream using one or more encoders.

Throws

EncoderError On encoding errors or if the encoding format is not recognized.

Parameters

NameTypeDescription
streamstring | Buffer | AsyncIterable<Buffer>The data to encode. If a string, it will first converted to UTF-8.
typesstring | string[]An encoding format or an ordered list of encoding formats to apply to the stream. A list may either be a comma-separated string or an array of strings.

Returns

Readable & AsyncIterable<Buffer>

An encoded byte stream.

Defined in

uri/src/encoders.ts:55


register

Static register(type, encoder): typeof Encoder

Registers a new encoder. All subclasses must register their encoding type support with this method.

Parameters

NameTypeDescription
typestringThe encoding format the encoder can handle.
encodertypeof EncoderThe Encoder subclass to register.

Returns

typeof Encoder

The Encoder base class (for method chaining).

Defined in

uri/src/encoders.ts:41