2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2021-06-05 21:10:07 +00:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
2021-07-06 12:38:12 +00:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-06-05 21:10:07 +00:00
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
2021-06-06 01:23:16 +00:00
|
|
|
/// <reference path="../fetch/lib.deno_fetch.d.ts" />
|
2021-06-05 21:10:07 +00:00
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
|
2023-12-07 13:21:01 +00:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 22:37:25 +00:00
|
|
|
const {
|
|
|
|
isDataView,
|
|
|
|
isSharedArrayBuffer,
|
|
|
|
isTypedArray,
|
|
|
|
} = core;
|
2024-01-26 22:46:46 +00:00
|
|
|
import {
|
2024-01-10 22:37:25 +00: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 22:46:46 +00:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 19:22:46 +00:00
|
|
|
const {
|
2023-04-02 17:41:41 +00:00
|
|
|
DataViewPrototypeGetBuffer,
|
|
|
|
DataViewPrototypeGetByteLength,
|
|
|
|
DataViewPrototypeGetByteOffset,
|
2024-01-04 04:12:38 +00:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2023-02-07 19:22:46 +00:00
|
|
|
PromiseReject,
|
|
|
|
PromiseResolve,
|
|
|
|
// TODO(lucacasonato): add SharedArrayBuffer to primordials
|
2024-01-04 04:12:38 +00:00
|
|
|
// SharedArrayBufferPrototype,
|
2023-02-07 19:22:46 +00:00
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
StringPrototypeSlice,
|
2023-11-19 08:13:38 +00:00
|
|
|
SymbolFor,
|
2023-04-02 17:41:41 +00:00
|
|
|
TypedArrayPrototypeGetBuffer,
|
|
|
|
TypedArrayPrototypeGetByteLength,
|
|
|
|
TypedArrayPrototypeGetByteOffset,
|
2024-01-04 04:12:38 +00:00
|
|
|
TypedArrayPrototypeSubarray,
|
2023-02-07 19:22:46 +00:00
|
|
|
Uint32Array,
|
2024-01-04 04:12:38 +00:00
|
|
|
Uint8Array,
|
2023-02-07 19:22:46 +00:00
|
|
|
} = primordials;
|
2024-01-10 22:37:25 +00:00
|
|
|
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
2023-02-07 19:22:46 +00:00
|
|
|
|
|
|
|
class TextDecoder {
|
|
|
|
/** @type {string} */
|
|
|
|
#encoding;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#fatal;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#ignoreBOM;
|
|
|
|
/** @type {boolean} */
|
|
|
|
#utf8SinglePass;
|
|
|
|
|
|
|
|
/** @type {number | null} */
|
|
|
|
#rid = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} label
|
|
|
|
* @param {TextDecoderOptions} options
|
|
|
|
*/
|
|
|
|
constructor(label = "utf-8", options = {}) {
|
|
|
|
const prefix = "Failed to construct 'TextDecoder'";
|
2023-05-01 10:47:13 +00:00
|
|
|
label = webidl.converters.DOMString(label, prefix, "Argument 1");
|
|
|
|
options = webidl.converters.TextDecoderOptions(
|
|
|
|
options,
|
2023-02-07 19:22:46 +00:00
|
|
|
prefix,
|
2023-05-01 10:47:13 +00:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2024-01-10 22:37:25 +00:00
|
|
|
const encoding = op_encoding_normalize_label(label);
|
2023-02-07 19:22:46 +00: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 10:47:13 +00:00
|
|
|
input = webidl.converters.BufferSource(input, prefix, "Argument 1", {
|
2023-02-07 19:22:46 +00:00
|
|
|
allowShared: true,
|
2021-06-05 21:10:07 +00:00
|
|
|
});
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
|
|
|
let stream = false;
|
|
|
|
if (options !== undefined) {
|
2023-05-01 10:47:13 +00:00
|
|
|
options = webidl.converters.TextDecodeOptions(
|
|
|
|
options,
|
2021-06-05 21:10:07 +00:00
|
|
|
prefix,
|
2023-05-01 10:47:13 +00:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 19:22:46 +00:00
|
|
|
stream = options.stream;
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
try {
|
2023-04-02 17:41:41 +00:00
|
|
|
/** @type {ArrayBufferLike} */
|
|
|
|
let buffer = input;
|
2024-01-04 04:12:38 +00:00
|
|
|
if (isTypedArray(input)) {
|
|
|
|
buffer = TypedArrayPrototypeGetBuffer(
|
|
|
|
/** @type {Uint8Array} */ (input),
|
|
|
|
);
|
|
|
|
} else if (isDataView(input)) {
|
|
|
|
buffer = DataViewPrototypeGetBuffer(/** @type {DataView} */ (input));
|
2023-04-02 17:41:41 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00: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-04 04:12:38 +00:00
|
|
|
if (isSharedArrayBuffer(buffer)) {
|
2023-02-07 19:22:46 +00: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-04 04:12:38 +00: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 19:22:46 +00:00
|
|
|
} else {
|
2023-04-02 17:41:41 +00:00
|
|
|
input = new Uint8Array(buffer);
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
2022-11-11 14:37:18 +00:00
|
|
|
}
|
2021-01-24 19:08:01 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
// Fast path for single pass encoding.
|
|
|
|
if (!stream && this.#rid === null) {
|
|
|
|
// Fast path for utf8 single pass encoding.
|
|
|
|
if (this.#utf8SinglePass) {
|
2024-01-10 22:37:25 +00:00
|
|
|
return op_encoding_decode_utf8(input, this.#ignoreBOM);
|
2021-10-28 20:32:58 +00:00
|
|
|
}
|
2022-05-17 13:52:48 +00:00
|
|
|
|
2024-01-10 22:37:25 +00:00
|
|
|
return op_encoding_decode_single(
|
2023-02-07 19:22:46 +00:00
|
|
|
input,
|
|
|
|
this.#encoding,
|
|
|
|
this.#fatal,
|
|
|
|
this.#ignoreBOM,
|
|
|
|
);
|
|
|
|
}
|
2022-05-17 13:52:48 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
if (this.#rid === null) {
|
2024-01-10 22:37:25 +00:00
|
|
|
this.#rid = op_encoding_new_decoder(
|
2023-02-07 19:22:46 +00:00
|
|
|
this.#encoding,
|
|
|
|
this.#fatal,
|
|
|
|
this.#ignoreBOM,
|
|
|
|
);
|
|
|
|
}
|
2024-01-10 22:37:25 +00:00
|
|
|
return op_encoding_decode(input, this.#rid, stream);
|
2023-02-07 19:22:46 +00:00
|
|
|
} finally {
|
|
|
|
if (!stream && this.#rid !== null) {
|
|
|
|
core.close(this.#rid);
|
|
|
|
this.#rid = null;
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-19 08:13:38 +00:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(TextDecoderPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"encoding",
|
|
|
|
"fatal",
|
|
|
|
"ignoreBOM",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-10-10 03:01:01 +00:00
|
|
|
webidl.configureInterface(TextDecoder);
|
2023-02-07 19:22:46 +00:00
|
|
|
const TextDecoderPrototype = TextDecoder.prototype;
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
class TextEncoder {
|
|
|
|
constructor() {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextEncoderPrototype);
|
|
|
|
return "utf-8";
|
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00: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 10:47:13 +00:00
|
|
|
input = webidl.converters.DOMString(
|
|
|
|
input,
|
|
|
|
"Failed to execute 'encode' on 'TextEncoder'",
|
|
|
|
"Argument 1",
|
|
|
|
);
|
2023-02-07 19:22:46 +00:00
|
|
|
return core.encode(input);
|
|
|
|
}
|
2021-06-05 21:10:07 +00:00
|
|
|
|
2023-02-07 19:22:46 +00: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 10:47:13 +00:00
|
|
|
source = webidl.converters.DOMString(source, prefix, "Argument 1");
|
|
|
|
destination = webidl.converters.Uint8Array(
|
|
|
|
destination,
|
2023-02-07 19:22:46 +00:00
|
|
|
prefix,
|
2023-05-01 10:47:13 +00:00
|
|
|
"Argument 2",
|
|
|
|
{
|
|
|
|
allowShared: true,
|
|
|
|
},
|
|
|
|
);
|
2024-01-10 22:37:25 +00:00
|
|
|
op_encoding_encode_into(source, destination, encodeIntoBuf);
|
2023-02-07 19:22:46 +00:00
|
|
|
return {
|
|
|
|
read: encodeIntoBuf[0],
|
|
|
|
written: encodeIntoBuf[1],
|
|
|
|
};
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
2023-11-19 08:13:38 +00:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(TextEncoderPrototype, this),
|
|
|
|
keys: ["encoding"],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
const encodeIntoBuf = new Uint32Array(2);
|
2022-09-17 11:18:15 +00:00
|
|
|
|
2023-10-10 03:01:01 +00:00
|
|
|
webidl.configureInterface(TextEncoder);
|
2023-02-07 19:22:46 +00:00
|
|
|
const TextEncoderPrototype = TextEncoder.prototype;
|
2021-06-05 21:10:07 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
class TextDecoderStream {
|
|
|
|
/** @type {TextDecoder} */
|
|
|
|
#decoder;
|
|
|
|
/** @type {TransformStream<BufferSource, string>} */
|
|
|
|
#transform;
|
2021-06-06 01:23:16 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/**
|
|
|
|
* @param {string} label
|
|
|
|
* @param {TextDecoderOptions} options
|
|
|
|
*/
|
|
|
|
constructor(label = "utf-8", options = {}) {
|
|
|
|
const prefix = "Failed to construct 'TextDecoderStream'";
|
2023-05-01 10:47:13 +00:00
|
|
|
label = webidl.converters.DOMString(label, prefix, "Argument 1");
|
|
|
|
options = webidl.converters.TextDecoderOptions(
|
|
|
|
options,
|
2023-02-07 19:22:46 +00:00
|
|
|
prefix,
|
2023-05-01 10:47:13 +00:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 19:22:46 +00: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 10:47:13 +00:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk", {
|
2023-02-07 19:22:46 +00:00
|
|
|
allowShared: true,
|
|
|
|
});
|
|
|
|
const decoded = this.#decoder.decode(chunk, { stream: true });
|
|
|
|
if (decoded) {
|
|
|
|
controller.enqueue(decoded);
|
2021-06-06 01:23:16 +00:00
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
flush: (controller) => {
|
|
|
|
try {
|
|
|
|
const final = this.#decoder.decode();
|
|
|
|
if (final) {
|
|
|
|
controller.enqueue(final);
|
2021-06-06 01:23:16 +00:00
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
2023-11-12 19:47:03 +00:00
|
|
|
cancel: (_reason) => {
|
|
|
|
try {
|
|
|
|
const _ = this.#decoder.decode();
|
|
|
|
return PromiseResolve();
|
|
|
|
} catch (err) {
|
|
|
|
return PromiseReject(err);
|
|
|
|
}
|
|
|
|
},
|
2023-02-07 19:22:46 +00:00
|
|
|
});
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2021-06-06 01:23:16 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.encoding;
|
|
|
|
}
|
2021-06-06 01:23:16 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {boolean} */
|
|
|
|
get fatal() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.fatal;
|
|
|
|
}
|
2021-06-06 01:23:16 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {boolean} */
|
|
|
|
get ignoreBOM() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#decoder.ignoreBOM;
|
|
|
|
}
|
2021-06-06 01:23:16 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {ReadableStream<string>} */
|
|
|
|
get readable() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#transform.readable;
|
2021-06-06 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {WritableStream<BufferSource>} */
|
|
|
|
get writable() {
|
|
|
|
webidl.assertBranded(this, TextDecoderStreamPrototype);
|
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
2023-11-19 08:13:38 +00: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 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 03:01:01 +00:00
|
|
|
webidl.configureInterface(TextDecoderStream);
|
2023-02-07 19:22:46 +00: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 12:38:12 +00:00
|
|
|
return PromiseResolve();
|
2021-06-06 01:23:16 +00:00
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
if (this.#pendingHighSurrogate !== null) {
|
|
|
|
chunk = this.#pendingHighSurrogate + chunk;
|
2021-06-06 01:23:16 +00:00
|
|
|
}
|
2023-02-07 19:22:46 +00: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 21:10:07 +00:00
|
|
|
},
|
2023-02-07 19:22:46 +00: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 21:10:07 +00:00
|
|
|
},
|
2023-02-07 19:22:46 +00:00
|
|
|
});
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
2020-08-07 14:55:02 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {string} */
|
|
|
|
get encoding() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return "utf-8";
|
2021-04-08 13:05:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {ReadableStream<Uint8Array>} */
|
|
|
|
get readable() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return this.#transform.readable;
|
2021-04-08 13:05:08 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
/** @returns {WritableStream<string>} */
|
|
|
|
get writable() {
|
|
|
|
webidl.assertBranded(this, TextEncoderStreamPrototype);
|
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
2023-11-19 08:13:38 +00:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
TextEncoderStreamPrototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
"encoding",
|
|
|
|
"readable",
|
|
|
|
"writable",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 03:01:01 +00:00
|
|
|
webidl.configureInterface(TextEncoderStream);
|
2023-02-07 19:22:46 +00: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,
|
|
|
|
};
|