UNPKG

@nberlette/utf8

Version:

Blazing fast universal ponyfills for TextEncoder and TextDecoder.

116 lines 5.39 kB
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 _TextDecoderStream_decoder, _TextDecoderStream_transform; /** * This module provides a streaming text decoder implementation, as a ponyfill * for the native `TextDecoderStream` Web API. * * Under the hood, it uses the `TextDecoder` ponyfill from this package to * decode UTF-8 bytes into strings, and the native `TransformStream` API to * handle the streaming process. It requires the `TransformStream` API to be * available in the current environment. * * **Note**: This was directly adapted from the Deno `TextDecoderStream` * implementation (MIT License), which is based on the WHATWG Streams standard. * * @module text-decoder-stream */ import { PromiseReject, PromiseResolve, TransformStream } from "./_internal.js"; import { TextDecoder, } from "./text_decoder.js"; /** * Zero-dependency ponyfill for the native `TextDecoderStream` Web API. * * Uses the {@linkcode TextDecoder} ponyfill to decode UTF-8 bytes into strings * 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/TextDecoderStream * @category Streams * @tags utf-8, decoder */ export class TextDecoderStream { constructor(label = "utf-8", options = { __proto__: null }) { _TextDecoderStream_decoder.set(this, void 0); _TextDecoderStream_transform.set(this, void 0); __classPrivateFieldSet(this, _TextDecoderStream_decoder, new TextDecoder(label, options), "f"); __classPrivateFieldSet(this, _TextDecoderStream_transform, new TransformStream({ transform: (chunk, controller) => { try { const decoded = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode(chunk, { stream: true }); if (decoded) controller.enqueue(decoded); return PromiseResolve(); } catch (e) { return PromiseReject(e); } }, flush: (controller) => { try { const flushed = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode(); if (flushed) controller.enqueue(flushed); return PromiseResolve(); } catch (e) { return PromiseReject(e); } }, cancel: () => { try { __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode(); return PromiseResolve(); } catch (e) { return PromiseReject(e); } }, }), "f"); } /** * @returns the encoding standard being used by the underlying `TextDecoder`. */ get encoding() { return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").encoding; } /** * If true, invalid bytes will throw a TypeError. This reflects the value of * the `fatal` option passed to the `TextDecoderStream` constructor. */ get fatal() { return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").fatal; } /** * If true, the BOM (Byte Order Mark) will be ignored. This reflects the * value of the `ignoreBOM` option passed to the `TextDecoderStream` * constructor. */ get ignoreBOM() { return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").ignoreBOM; } /** * @returns the readable stream side of the `TextDecoderStream`, which can be * used to read the decoded strings as they are produced by the decoder. */ get readable() { return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").readable; } /** * @returns the writable stream side of the `TextDecoderStream`, which can be * used to write UTF-8 bytes to be decoded by the underlying `TextDecoder`. */ get writable() { return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").writable; } } _TextDecoderStream_decoder = new WeakMap(), _TextDecoderStream_transform = new WeakMap(); //# sourceMappingURL=text_decoder_stream.js.map