2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-06-05 17:10:07 -04:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
2021-07-06 08:38:12 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-06-05 17:10:07 -04:00
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
2021-06-05 21:23:16 -04:00
|
|
|
/// <reference path="../fetch/lib.deno_fetch.d.ts" />
|
2021-06-05 17:10:07 -04:00
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
isDataView,
|
|
|
|
isSharedArrayBuffer,
|
|
|
|
isTypedArray,
|
|
|
|
} = core;
|
2024-01-26 17:46:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_encoding_decode,
|
|
|
|
op_encoding_decode_single,
|
|
|
|
op_encoding_decode_utf8,
|
|
|
|
op_encoding_encode_into,
|
|
|
|
op_encoding_new_decoder,
|
|
|
|
op_encoding_normalize_label,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
2023-04-02 13:41:41 -04:00
|
|
|
DataViewPrototypeGetBuffer,
|
|
|
|
DataViewPrototypeGetByteLength,
|
|
|
|
DataViewPrototypeGetByteOffset,
|
2024-01-03 23:12:38 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2023-02-07 14:22:46 -05:00
|
|
|
PromiseReject,
|
|
|
|
PromiseResolve,
|
|
|
|
// TODO(lucacasonato): add SharedArrayBuffer to primordials
|
2024-01-03 23:12:38 -05:00
|
|
|
// SharedArrayBufferPrototype,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
StringPrototypeSlice,
|
2023-11-19 03:13:38 -05:00
|
|
|
SymbolFor,
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetBuffer,
|
|
|
|
TypedArrayPrototypeGetByteLength,
|
|
|
|
TypedArrayPrototypeGetByteOffset,
|
2024-01-03 23:12:38 -05:00
|
|
|
TypedArrayPrototypeSubarray,
|
2023-02-07 14:22:46 -05:00
|
|
|
Uint32Array,
|
2024-01-03 23:12:38 -05:00
|
|
|
Uint8Array,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
class TextDecoder {
|
|
|
|
/** @type {string} */
|
|
|
|
#encoding;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#fatal;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#ignoreBOM;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#utf8SinglePass;
|
|
|
|
|
2024-08-06 03:40:17 -04:00
|
|
|
/** @type {object | null} */
|
|
|
|
#handle = null;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} label
|
|
|
|
* @param {TextDecoderOptions} options
|
|
|
|
*/
|
2024-05-22 18:03:35 -04:00
|
|
|
constructor(label = "utf-8", options = { __proto__: null }) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const prefix = "Failed to construct 'TextDecoder'";
|
2023-05-01 06:47:13 -04:00
|
|
|
label = webidl.converters.DOMString(label, prefix, "Argument 1");
|
|
|
|
options = webidl.converters.TextDecoderOptions(
|
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2024-01-10 17:37:25 -05:00
|
|
|
const encoding = op_encoding_normalize_label(label);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#encoding = encoding;
|
|
|
|
this.#fatal = options.fatal;
|
|
|
|
this.#ignoreBOM = options.ignoreBOM;
|
|
|
|
this.#utf8SinglePass = encoding === "utf-8" && !options.fatal;
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextDecoderPrototype);
|
|
|
|
return this.#encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {boolean} */
|
|
|
|
get fatal() {
|
|
|
|
webidl.assertBranded(this, TextDecoderPrototype);
|
|
|
|
return this.#fatal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @returns {boolean} */
|
|
|
|
get ignoreBOM() {
|
|
|
|
webidl.assertBranded(this, TextDecoderPrototype);
|
|
|
|
return this.#ignoreBOM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {BufferSource} [input]
|
|
|
|
* @param {TextDecodeOptions} options
|
|
|
|
*/
|
|
|
|
decode(input = new Uint8Array(), options = undefined) {
|
|
|
|
webidl.assertBranded(this, TextDecoderPrototype);
|
|
|
|
const prefix = "Failed to execute 'decode' on 'TextDecoder'";
|
|
|
|
if (input !== undefined) {
|
2023-05-01 06:47:13 -04:00
|
|
|
input = webidl.converters.BufferSource(input, prefix, "Argument 1", {
|
2023-02-07 14:22:46 -05:00
|
|
|
allowShared: true,
|
2021-06-05 17:10:07 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
let stream = false;
|
|
|
|
if (options !== undefined) {
|
2023-05-01 06:47:13 -04:00
|
|
|
options = webidl.converters.TextDecodeOptions(
|
|
|
|
options,
|
2021-06-05 17:10:07 -04:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
stream = options.stream;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
2023-04-02 13:41:41 -04:00
|
|
|
/** @type {ArrayBufferLike} */
|
|
|
|
let buffer = input;
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isTypedArray(input)) {
|
|
|
|
buffer = TypedArrayPrototypeGetBuffer(
|
|
|
|
/** @type {Uint8Array} */ (input),
|
|
|
|
);
|
|
|
|
} else if (isDataView(input)) {
|
|
|
|
buffer = DataViewPrototypeGetBuffer(/** @type {DataView} */ (input));
|
2023-04-02 13:41:41 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Note from spec: implementations are strongly encouraged to use an implementation strategy that avoids this copy.
|
|
|
|
// When doing so they will have to make sure that changes to input do not affect future calls to decode().
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isSharedArrayBuffer(buffer)) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// We clone the data into a non-shared ArrayBuffer so we can pass it
|
|
|
|
// to Rust.
|
|
|
|
// `input` is now a Uint8Array, and calling the TypedArray constructor
|
|
|
|
// with a TypedArray argument copies the data.
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isTypedArray(input)) {
|
|
|
|
input = new Uint8Array(
|
|
|
|
buffer,
|
|
|
|
TypedArrayPrototypeGetByteOffset(
|
|
|
|
/** @type {Uint8Array} */ (input),
|
|
|
|
),
|
|
|
|
TypedArrayPrototypeGetByteLength(
|
|
|
|
/** @type {Uint8Array} */ (input),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if (isDataView(input)) {
|
|
|
|
input = new Uint8Array(
|
|
|
|
buffer,
|
|
|
|
DataViewPrototypeGetByteOffset(/** @type {DataView} */ (input)),
|
|
|
|
DataViewPrototypeGetByteLength(/** @type {DataView} */ (input)),
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-02 13:41:41 -04:00
|
|
|
input = new Uint8Array(buffer);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-11-11 09:37:18 -05:00
|
|
|
}
|
2021-01-24 14:08:01 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Fast path for single pass encoding.
|
2024-08-06 03:40:17 -04:00
|
|
|
if (!stream && this.#handle === null) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// Fast path for utf8 single pass encoding.
|
|
|
|
if (this.#utf8SinglePass) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_encoding_decode_utf8(input, this.#ignoreBOM);
|
2021-10-28 16:32:58 -04:00
|
|
|
}
|
2022-05-17 09:52:48 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_encoding_decode_single(
|
2023-02-07 14:22:46 -05:00
|
|
|
input,
|
|
|
|
this.#encoding,
|
|
|
|
this.#fatal,
|
|
|
|
this.#ignoreBOM,
|
|
|
|
);
|
|
|
|
}
|
2022-05-17 09:52:48 -04:00
|
|
|
|
2024-08-06 03:40:17 -04:00
|
|
|
if (this.#handle === null) {
|
|
|
|
this.#handle = op_encoding_new_decoder(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#encoding,
|
|
|
|
this.#fatal,
|
|
|
|
this.#ignoreBOM,
|
|
|
|
);
|
|
|
|
}
|
2024-08-06 03:40:17 -04:00
|
|
|
return op_encoding_decode(input, this.#handle, stream);
|
2023-02-07 14:22:46 -05:00
|
|
|
} finally {
|
2024-08-06 03:40:17 -04:00
|
|
|
if (!stream && this.#handle !== null) {
|
|
|
|
this.#handle = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(TextDecoderPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"encoding",
|
|
|
|
"fatal",
|
|
|
|
"ignoreBOM",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(TextDecoder);
|
2023-02-07 14:22:46 -05:00
|
|
|
const TextDecoderPrototype = TextDecoder.prototype;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TextEncoder {
|
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextEncoderPrototype);
|
|
|
|
return "utf-8";
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
*/
|
|
|
|
encode(input = "") {
|
|
|
|
webidl.assertBranded(this, TextEncoderPrototype);
|
|
|
|
// The WebIDL type of `input` is `USVString`, but `core.encode` already
|
|
|
|
// converts lone surrogates to the replacement character.
|
2023-05-01 06:47:13 -04:00
|
|
|
input = webidl.converters.DOMString(
|
|
|
|
input,
|
|
|
|
"Failed to execute 'encode' on 'TextEncoder'",
|
|
|
|
"Argument 1",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
return core.encode(input);
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} source
|
|
|
|
* @param {Uint8Array} destination
|
|
|
|
* @returns {TextEncoderEncodeIntoResult}
|
|
|
|
*/
|
|
|
|
encodeInto(source, destination) {
|
|
|
|
webidl.assertBranded(this, TextEncoderPrototype);
|
|
|
|
const prefix = "Failed to execute 'encodeInto' on 'TextEncoder'";
|
|
|
|
// The WebIDL type of `source` is `USVString`, but the ops bindings
|
|
|
|
// already convert lone surrogates to the replacement character.
|
2023-05-01 06:47:13 -04:00
|
|
|
source = webidl.converters.DOMString(source, prefix, "Argument 1");
|
|
|
|
destination = webidl.converters.Uint8Array(
|
|
|
|
destination,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
{
|
|
|
|
allowShared: true,
|
|
|
|
},
|
|
|
|
);
|
2024-01-10 17:37:25 -05:00
|
|
|
op_encoding_encode_into(source, destination, encodeIntoBuf);
|
2023-02-07 14:22:46 -05:00
|
|
|
return {
|
|
|
|
read: encodeIntoBuf[0],
|
|
|
|
written: encodeIntoBuf[1],
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(TextEncoderPrototype, this),
|
|
|
|
keys: ["encoding"],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const encodeIntoBuf = new Uint32Array(2);
|
2022-09-17 07:18:15 -04:00
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(TextEncoder);
|
2023-02-07 14:22:46 -05:00
|
|
|
const TextEncoderPrototype = TextEncoder.prototype;
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TextDecoderStream {
|
|
|
|
/** @type {TextDecoder} */
|
|
|
|
#decoder;
|
|
|
|
/** @type {TransformStream<BufferSource, string>} */
|
|
|
|
#transform;
|
2021-06-05 21:23:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} label
|
|
|
|
* @param {TextDecoderOptions} options
|
|
|
|
*/
|
2024-05-22 18:03:35 -04:00
|
|
|
constructor(label = "utf-8", options = { __proto__: null }) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const prefix = "Failed to construct 'TextDecoderStream'";
|
2023-05-01 06:47:13 -04:00
|
|
|
label = webidl.converters.DOMString(label, prefix, "Argument 1");
|
|
|
|
options = webidl.converters.TextDecoderOptions(
|
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#decoder = new TextDecoder(label, options);
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
// The transform and flush functions need access to TextDecoderStream's
|
|
|
|
// `this`, so they are defined as functions rather than methods.
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
try {
|
2023-05-01 06:47:13 -04:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk", {
|
2023-02-07 14:22:46 -05:00
|
|
|
allowShared: true,
|
|
|
|
});
|
|
|
|
const decoded = this.#decoder.decode(chunk, { stream: true });
|
|
|
|
if (decoded) {
|
|
|
|
controller.enqueue(decoded);
|
2021-06-05 21:23:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
flush: (controller) => {
|
|
|
|
try {
|
|
|
|
const final = this.#decoder.decode();
|
|
|
|
if (final) {
|
|
|
|
controller.enqueue(final);
|
2021-06-05 21:23:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
2023-11-12 14:47:03 -05:00
|
|
|
cancel: (_reason) => {
|
|
|
|
try {
|
|
|
|
const _ = this.#decoder.decode();
|
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2021-06-05 21:23:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.encoding;
|
|
|
|
}
|
2021-06-05 21:23:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {boolean} */
|
|
|
|
get fatal() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.fatal;
|
|
|
|
}
|
2021-06-05 21:23:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {boolean} */
|
|
|
|
get ignoreBOM() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.ignoreBOM;
|
|
|
|
}
|
2021-06-05 21:23:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {ReadableStream<string>} */
|
|
|
|
get readable() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#transform.readable;
|
2021-06-05 21:23:16 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {WritableStream<BufferSource>} */
|
|
|
|
get writable() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
TextDecoderStreamPrototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
"encoding",
|
|
|
|
"fatal",
|
|
|
|
"ignoreBOM",
|
|
|
|
"readable",
|
|
|
|
"writable",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(TextDecoderStream);
|
2023-02-07 14:22:46 -05:00
|
|
|
const TextDecoderStreamPrototype = TextDecoderStream.prototype;
|
|
|
|
|
|
|
|
class TextEncoderStream {
|
|
|
|
/** @type {string | null} */
|
|
|
|
#pendingHighSurrogate = null;
|
|
|
|
/** @type {TransformStream<string, Uint8Array>} */
|
|
|
|
#transform;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
// The transform and flush functions need access to TextEncoderStream's
|
|
|
|
// `this`, so they are defined as functions rather than methods.
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
try {
|
|
|
|
chunk = webidl.converters.DOMString(chunk);
|
|
|
|
if (chunk === "") {
|
2021-07-06 08:38:12 -04:00
|
|
|
return PromiseResolve();
|
2021-06-05 21:23:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this.#pendingHighSurrogate !== null) {
|
|
|
|
chunk = this.#pendingHighSurrogate + chunk;
|
2021-06-05 21:23:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const lastCodeUnit = StringPrototypeCharCodeAt(
|
|
|
|
chunk,
|
|
|
|
chunk.length - 1,
|
|
|
|
);
|
|
|
|
if (0xD800 <= lastCodeUnit && lastCodeUnit <= 0xDBFF) {
|
|
|
|
this.#pendingHighSurrogate = StringPrototypeSlice(chunk, -1);
|
|
|
|
chunk = StringPrototypeSlice(chunk, 0, -1);
|
|
|
|
} else {
|
|
|
|
this.#pendingHighSurrogate = null;
|
|
|
|
}
|
|
|
|
if (chunk) {
|
|
|
|
controller.enqueue(core.encode(chunk));
|
|
|
|
}
|
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
flush: (controller) => {
|
|
|
|
try {
|
|
|
|
if (this.#pendingHighSurrogate !== null) {
|
|
|
|
controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD]));
|
|
|
|
}
|
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-08-07 10:55:02 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return "utf-8";
|
2021-04-08 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {ReadableStream<Uint8Array>} */
|
|
|
|
get readable() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return this.#transform.readable;
|
2021-04-08 09:05:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @returns {WritableStream<string>} */
|
|
|
|
get writable() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
TextEncoderStreamPrototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
"encoding",
|
|
|
|
"readable",
|
|
|
|
"writable",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(TextEncoderStream);
|
2023-02-07 14:22:46 -05:00
|
|
|
const TextEncoderStreamPrototype = TextEncoderStream.prototype;
|
|
|
|
|
|
|
|
webidl.converters.TextDecoderOptions = webidl.createDictionaryConverter(
|
|
|
|
"TextDecoderOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "fatal",
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "ignoreBOM",
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
webidl.converters.TextDecodeOptions = webidl.createDictionaryConverter(
|
|
|
|
"TextDecodeOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "stream",
|
|
|
|
converter: webidl.converters.boolean,
|
|
|
|
defaultValue: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Uint8Array} bytes
|
|
|
|
*/
|
|
|
|
function decode(bytes, encoding) {
|
|
|
|
const BOMEncoding = BOMSniff(bytes);
|
|
|
|
if (BOMEncoding !== null) {
|
|
|
|
encoding = BOMEncoding;
|
|
|
|
const start = BOMEncoding === "UTF-8" ? 3 : 2;
|
|
|
|
bytes = TypedArrayPrototypeSubarray(bytes, start);
|
|
|
|
}
|
|
|
|
return new TextDecoder(encoding).decode(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Uint8Array} bytes
|
|
|
|
*/
|
|
|
|
function BOMSniff(bytes) {
|
|
|
|
if (bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF) {
|
|
|
|
return "UTF-8";
|
|
|
|
}
|
|
|
|
if (bytes[0] === 0xFE && bytes[1] === 0xFF) return "UTF-16BE";
|
|
|
|
if (bytes[0] === 0xFF && bytes[1] === 0xFE) return "UTF-16LE";
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
decode,
|
|
|
|
TextDecoder,
|
|
|
|
TextDecoderStream,
|
|
|
|
TextEncoder,
|
|
|
|
TextEncoderStream,
|
|
|
|
};
|