node:string_decoder
Node-compatible incremental multi-byte character decoder (node:string_decoder).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed.
import ... from "node:string_decoder"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature:
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import { Buffer } from "node:buffer";import { StringDecoder } from "node:string_decoder";
const decoder = new StringDecoder("utf8");console.log(decoder.write(Buffer.from([0xe2, 0x82]))); // ""console.log(decoder.write(Buffer.from([0xac]))); // "€"API Symbols
Type Aliases (1)
Classes
Section titled “Classes”StringDecoder
Section titled “StringDecoder”export class StringDecoder {
readonly encoding: string;
constructor(encoding?: StringDecoderEncoding | string);
write(buffer: Uint8Array | ArrayBufferView): string;
end(buffer?: Uint8Array | ArrayBufferView): string;
}
Stateful byte-to-string decoder preserving incomplete multi-byte sequences
across write() calls.
Constructor
constructor(encoding?: StringDecoderEncoding | string);
Construct a StringDecoder for encoding (default "utf8").
Parameters
| Name | Type | Description |
|---|---|---|
encoding optional | StringDecoderEncoding | string | Character encoding name. |
Exceptions
TypeError— withcode = "ERR_UNKNOWN_ENCODING"whenencodingis unsupported.
Properties
| Property | Type | Description | Since |
|---|---|---|---|
encoding readonly | string | Normalized encoding name used by this decoder. | v0.0.0-alpha.2 |
Methods
StringDecoder.write
Section titled “StringDecoder.write”write(buffer: Uint8Array | ArrayBufferView): string;
Decode buffer to a string, retaining any incomplete trailing sequence
until the next write() or end() call.
Parameters
| Name | Type | Description |
|---|---|---|
buffer | Uint8Array | ArrayBufferView | Byte buffer or view to decode. |
Returns
string — Decoded string for all complete characters so far.StringDecoder.end
Section titled “StringDecoder.end”end(buffer?: Uint8Array | ArrayBufferView): string;
Flush any remaining buffered bytes (and optionally decode a final buffer),
returning replacement characters for incomplete trailing sequences.
Parameters
| Name | Type | Description |
|---|---|---|
buffer optional | Uint8Array | ArrayBufferView | Optional final byte buffer or view. |
Returns
string — Remaining decoded string.Type Aliases
Section titled “Type Aliases”StringDecoderEncoding
Section titled “StringDecoderEncoding”export type StringDecoderEncoding =
| "utf8"
| "utf-8"
| "utf16le"
| "ucs2"
| "ucs-2"
| "base64"
| "base64url"
| "latin1"
| "binary"
| "hex"
| "ascii";
Supported character encodings for StringDecoder.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** Stateful multi-byte `StringDecoder` class. @since 0.0.0-alpha.2 */
StringDecoder: typeof StringDecoder;
};
export default _default;
Default export object providing { StringDecoder }.
