@nberlette/utf8
Version:
Blazing fast universal ponyfills for TextEncoder and TextDecoder.
75 lines • 3.73 kB
JavaScript
;
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _TextEncoderStream_encoder, _TextEncoderStream_transform;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextEncoderStream = void 0;
/**
* This module provides a streaming encoder for UTF-8 text, which is based on
* the `TextEncoder` API.
*
* This is a zero-dependency ponyfill for the native `TextEncoderStream` Web
* API, which can be used in any ES2015+ environment with support for the
* `TransformStream` API.
*
* **Note**: This was directly adapted from the Deno `TextEncoderStream`
* implementation (MIT License), which is based on the WHATWG Streams standard.
*
* @module text-encoder-stream
*/
const _internal_js_1 = require("./_internal.js");
const text_encoder_js_1 = require("./text_encoder.js");
/**
* Zero-dependency ponyfill for the native `TextEncoderStream` Web API.
*
* Uses the {@linkcode TextEncoder} ponyfill to encode strings into UTF-8
* bytes in a streaming fashion. Requires the `TransformStream` API to be
* available in the current environment.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoderStream
* @category Streams
* @tags utf-8, encoder
*/
class TextEncoderStream {
constructor() {
_TextEncoderStream_encoder.set(this, void 0);
_TextEncoderStream_transform.set(this, void 0);
__classPrivateFieldSet(this, _TextEncoderStream_encoder, new text_encoder_js_1.TextEncoder(), "f");
__classPrivateFieldSet(this, _TextEncoderStream_transform, new _internal_js_1.TransformStream({
transform: (chunk, controller) => {
const encoded = __classPrivateFieldGet(this, _TextEncoderStream_encoder, "f").encode(chunk);
controller.enqueue(encoded);
},
}), "f");
}
/** The encoding standard to use. This is always `"utf-8"`. */
get encoding() {
return "utf-8";
}
/**
* @returns the readable stream side of the `TextEncoderStream`, which can be
* used to read the encoded bytes as they are produced by the encoder.
*/
get readable() {
return __classPrivateFieldGet(this, _TextEncoderStream_transform, "f").readable;
}
/**
* @returns the writable stream side of the `TextEncoderStream`, which can be
* used to write strings to be encoded by the underlying `TextEncoder`.
*/
get writable() {
return __classPrivateFieldGet(this, _TextEncoderStream_transform, "f").writable;
}
}
exports.TextEncoderStream = TextEncoderStream;
_TextEncoderStream_encoder = new WeakMap(), _TextEncoderStream_transform = new WeakMap();
//# sourceMappingURL=text_encoder_stream.js.map