@nberlette/utf8 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +298 -0
- package/cjs/_dnt.shims.d.ts +1 -0
- package/cjs/_dnt.shims.js +61 -0
- package/cjs/_dnt.shims.js.map +1 -0
- package/cjs/_internal.d.ts +54 -0
- package/cjs/_internal.js +173 -0
- package/cjs/_internal.js.map +1 -0
- package/cjs/index.d.ts +28 -0
- package/cjs/index.js +45 -0
- package/cjs/index.js.map +1 -0
- package/cjs/package.json +3 -0
- package/cjs/text_decoder.d.ts +58 -0
- package/cjs/text_decoder.js +242 -0
- package/cjs/text_decoder.js.map +1 -0
- package/cjs/text_decoder_stream.d.ts +41 -0
- package/cjs/text_decoder_stream.js +120 -0
- package/cjs/text_decoder_stream.js.map +1 -0
- package/cjs/text_encoder.d.ts +39 -0
- package/cjs/text_encoder.js +78 -0
- package/cjs/text_encoder.js.map +1 -0
- package/cjs/text_encoder_stream.d.ts +27 -0
- package/cjs/text_encoder_stream.js +75 -0
- package/cjs/text_encoder_stream.js.map +1 -0
- package/esm/_dnt.shims.d.ts +1 -0
- package/esm/_dnt.shims.js +58 -0
- package/esm/_dnt.shims.js.map +1 -0
- package/esm/_internal.d.ts +54 -0
- package/esm/_internal.js +143 -0
- package/esm/_internal.js.map +1 -0
- package/esm/index.d.ts +28 -0
- package/esm/index.js +29 -0
- package/esm/index.js.map +1 -0
- package/esm/package.json +3 -0
- package/esm/text_decoder.d.ts +58 -0
- package/esm/text_decoder.js +238 -0
- package/esm/text_decoder.js.map +1 -0
- package/esm/text_decoder_stream.d.ts +41 -0
- package/esm/text_decoder_stream.js +116 -0
- package/esm/text_decoder_stream.js.map +1 -0
- package/esm/text_encoder.d.ts +39 -0
- package/esm/text_encoder.js +74 -0
- package/esm/text_encoder.js.map +1 -0
- package/esm/text_encoder_stream.d.ts +27 -0
- package/esm/text_encoder_stream.js +71 -0
- package/esm/text_encoder_stream.js.map +1 -0
- package/package.json +187 -0
package/cjs/index.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
/**
|
|
18
|
+
* # `@nick/utf8`
|
|
19
|
+
*
|
|
20
|
+
* This package provides blazing-fast, zero-dependency ponyfills for the native
|
|
21
|
+
* `TextEncoder` and `TextDecoder` APIs, as well as the streaming counterparts,
|
|
22
|
+
* `TextEncoderStream` and `TextDecoderStream`.
|
|
23
|
+
*
|
|
24
|
+
* The ponyfills are based on the WHATWG Encoding standard, and are designed to
|
|
25
|
+
* work in any ES2015+ environment, including Deno, Node, Bun, and the browser.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { TextEncoder, TextDecoder } from "@nick/utf8";
|
|
30
|
+
*
|
|
31
|
+
* const encoder = new TextEncoder();
|
|
32
|
+
* const decoder = new TextDecoder();
|
|
33
|
+
*
|
|
34
|
+
* const buffer = encoder.encode("Hello, world!");
|
|
35
|
+
* const text = decoder.decode(buffer);
|
|
36
|
+
*
|
|
37
|
+
* console.log(text); // Outputs: "Hello, world!"
|
|
38
|
+
* ```
|
|
39
|
+
* @module utf8
|
|
40
|
+
*/
|
|
41
|
+
__exportStar(require("./text_encoder.js"), exports);
|
|
42
|
+
__exportStar(require("./text_decoder.js"), exports);
|
|
43
|
+
__exportStar(require("./text_encoder_stream.js"), exports);
|
|
44
|
+
__exportStar(require("./text_decoder_stream.js"), exports);
|
|
45
|
+
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,oDAAkC;AAClC,oDAAkC;AAClC,2DAAyC;AACzC,2DAAyC"}
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for the {@linkcode TextDecoder} constructor.
|
|
3
|
+
*/
|
|
4
|
+
export interface TextDecoderOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If true, invalid bytes will throw a TypeError. Otherwise, they will be
|
|
7
|
+
* replaced with the Unicode replacement character.
|
|
8
|
+
* @default {false}
|
|
9
|
+
*/
|
|
10
|
+
fatal?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If true, the BOM (Byte Order Mark) will be ignored.
|
|
13
|
+
* @default {false}
|
|
14
|
+
*/
|
|
15
|
+
ignoreBOM?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Options for the {@linkcode TextDecoder.decode} method. */
|
|
18
|
+
export interface TextDecodeOptions {
|
|
19
|
+
/**
|
|
20
|
+
* If true, indicates that the data being decoded is part of a larger stream.
|
|
21
|
+
* This allows the decoder to handle incomplete byte sequences appropriately.
|
|
22
|
+
* @default {false}
|
|
23
|
+
*/
|
|
24
|
+
stream?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Decodes an encoded sequence of bytes into a string, using the specified
|
|
28
|
+
* encoding standard. Currently, only UTF-8 encoding is supported.
|
|
29
|
+
*
|
|
30
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
|
|
31
|
+
* @category Encoding
|
|
32
|
+
* @tags utf-8, decoder
|
|
33
|
+
*/
|
|
34
|
+
export declare class TextDecoder {
|
|
35
|
+
#private;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new TextDecoder instance.
|
|
38
|
+
* @param label The encoding to use. Currently, only "utf-8" is supported.
|
|
39
|
+
* @param options Configuration options.
|
|
40
|
+
*/
|
|
41
|
+
constructor(label?: string, options?: TextDecoderOptions);
|
|
42
|
+
/** The encoding standard to use. */
|
|
43
|
+
get encoding(): string;
|
|
44
|
+
/** If true, invalid bytes will throw a TypeError. */
|
|
45
|
+
get fatal(): boolean;
|
|
46
|
+
/** If true, the BOM (Byte Order Mark) will be ignored. */
|
|
47
|
+
get ignoreBOM(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Decodes a BufferSource into a string using UTF-8 decoding.
|
|
50
|
+
*
|
|
51
|
+
* @param input The bytes to decode. Defaults to an empty Uint8Array.
|
|
52
|
+
* @param [options] Decoding options.
|
|
53
|
+
* @returns The decoded string.
|
|
54
|
+
* @throws if the input is not a BufferSource.
|
|
55
|
+
* @throws if fatal is true and an invalid byte sequence is encountered.
|
|
56
|
+
*/
|
|
57
|
+
decode(input?: BufferSource, options?: TextDecodeOptions): string;
|
|
58
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _TextDecoder_encoding, _TextDecoder_fatal, _TextDecoder_ignoreBOM, _TextDecoder_buffer;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.TextDecoder = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* This module provides a high performance dependency-free ponyfill for the
|
|
18
|
+
* `TextDecoder` Web API, allowing you to decode UTF-8 encoded `BufferSource`
|
|
19
|
+
* buffers into strings in any ES2015+ environment.
|
|
20
|
+
*
|
|
21
|
+
* Currently only UTF-8 encoding is supported, but additional encodings will be
|
|
22
|
+
* added in the near future. Encodings that are currently being implemented are
|
|
23
|
+
* UTF-16, ISO-8859-1, Windows-1252, and ASCII.
|
|
24
|
+
*
|
|
25
|
+
* @module text-decoder
|
|
26
|
+
*/
|
|
27
|
+
const _internal_js_1 = require("./_internal.js");
|
|
28
|
+
/**
|
|
29
|
+
* Decodes an encoded sequence of bytes into a string, using the specified
|
|
30
|
+
* encoding standard. Currently, only UTF-8 encoding is supported.
|
|
31
|
+
*
|
|
32
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
|
|
33
|
+
* @category Encoding
|
|
34
|
+
* @tags utf-8, decoder
|
|
35
|
+
*/
|
|
36
|
+
class TextDecoder {
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new TextDecoder instance.
|
|
39
|
+
* @param label The encoding to use. Currently, only "utf-8" is supported.
|
|
40
|
+
* @param options Configuration options.
|
|
41
|
+
*/
|
|
42
|
+
constructor(label = "utf-8", options = {}) {
|
|
43
|
+
_TextDecoder_encoding.set(this, void 0);
|
|
44
|
+
_TextDecoder_fatal.set(this, void 0);
|
|
45
|
+
_TextDecoder_ignoreBOM.set(this, void 0);
|
|
46
|
+
_TextDecoder_buffer.set(this, new _internal_js_1.Uint8Array(0));
|
|
47
|
+
__classPrivateFieldSet(this, _TextDecoder_encoding, (0, _internal_js_1.normalizeEncoding)(label), "f");
|
|
48
|
+
__classPrivateFieldSet(this, _TextDecoder_fatal, !!options.fatal, "f");
|
|
49
|
+
__classPrivateFieldSet(this, _TextDecoder_ignoreBOM, !!options.ignoreBOM, "f");
|
|
50
|
+
if (__classPrivateFieldGet(this, _TextDecoder_encoding, "f") !== "utf-8") {
|
|
51
|
+
throw new _internal_js_1.TypeError(`The encoding "${label}" is not supported.`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The encoding standard to use. */
|
|
55
|
+
get encoding() {
|
|
56
|
+
return __classPrivateFieldGet(this, _TextDecoder_encoding, "f");
|
|
57
|
+
}
|
|
58
|
+
/** If true, invalid bytes will throw a TypeError. */
|
|
59
|
+
get fatal() {
|
|
60
|
+
return __classPrivateFieldGet(this, _TextDecoder_fatal, "f");
|
|
61
|
+
}
|
|
62
|
+
/** If true, the BOM (Byte Order Mark) will be ignored. */
|
|
63
|
+
get ignoreBOM() {
|
|
64
|
+
return __classPrivateFieldGet(this, _TextDecoder_ignoreBOM, "f");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Decodes a BufferSource into a string using UTF-8 decoding.
|
|
68
|
+
*
|
|
69
|
+
* @param input The bytes to decode. Defaults to an empty Uint8Array.
|
|
70
|
+
* @param [options] Decoding options.
|
|
71
|
+
* @returns The decoded string.
|
|
72
|
+
* @throws if the input is not a BufferSource.
|
|
73
|
+
* @throws if fatal is true and an invalid byte sequence is encountered.
|
|
74
|
+
*/
|
|
75
|
+
decode(input, options) {
|
|
76
|
+
const stream = options?.stream ?? false;
|
|
77
|
+
let bytes = (0, _internal_js_1.toUint8Array)(input);
|
|
78
|
+
// Concatenate any leftover bytes from the previous decode call
|
|
79
|
+
if (__classPrivateFieldGet(this, _TextDecoder_buffer, "f").length > 0) {
|
|
80
|
+
const combined = new _internal_js_1.Uint8Array(__classPrivateFieldGet(this, _TextDecoder_buffer, "f").length + bytes.length);
|
|
81
|
+
combined.set(__classPrivateFieldGet(this, _TextDecoder_buffer, "f"), 0);
|
|
82
|
+
combined.set(bytes, __classPrivateFieldGet(this, _TextDecoder_buffer, "f").length);
|
|
83
|
+
bytes = combined;
|
|
84
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, new _internal_js_1.Uint8Array(), "f");
|
|
85
|
+
}
|
|
86
|
+
let string = "", i = 0;
|
|
87
|
+
// Handle BOM
|
|
88
|
+
if (!this.ignoreBOM &&
|
|
89
|
+
bytes.length >= 3 &&
|
|
90
|
+
bytes[0] === 0xef &&
|
|
91
|
+
bytes[1] === 0xbb &&
|
|
92
|
+
bytes[2] === 0xbf)
|
|
93
|
+
i += 3;
|
|
94
|
+
while (i < bytes.length) {
|
|
95
|
+
const startIndex = i;
|
|
96
|
+
const byte1 = bytes[i++];
|
|
97
|
+
if (byte1 <= 0x7f) {
|
|
98
|
+
string += (0, _internal_js_1.StringFromCharCode)(byte1);
|
|
99
|
+
}
|
|
100
|
+
else if (byte1 >= 0xc0 && byte1 <= 0xdf) {
|
|
101
|
+
// 2-byte sequence
|
|
102
|
+
const byte2 = bytes[i++];
|
|
103
|
+
if (byte2 === _internal_js_1.undefined) {
|
|
104
|
+
// Incomplete sequence
|
|
105
|
+
if (stream) {
|
|
106
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
if (this.fatal)
|
|
111
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
112
|
+
string += "\uFFFD";
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if ((byte2 & 0xc0) !== 0x80) {
|
|
117
|
+
if (this.fatal)
|
|
118
|
+
throw new _internal_js_1.TypeError("Invalid continuation byte");
|
|
119
|
+
string += "\uFFFD";
|
|
120
|
+
i = startIndex + 1;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const codePoint = ((byte1 & 0x1f) << 6) | (byte2 & 0x3f);
|
|
124
|
+
string += (0, _internal_js_1.StringFromCharCode)(codePoint);
|
|
125
|
+
}
|
|
126
|
+
else if (byte1 >= 0xe0 && byte1 <= 0xef) {
|
|
127
|
+
// 3-byte sequence
|
|
128
|
+
const byte2 = bytes[i++];
|
|
129
|
+
if (byte2 === _internal_js_1.undefined) {
|
|
130
|
+
// Incomplete sequence
|
|
131
|
+
if (stream) {
|
|
132
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (this.fatal)
|
|
137
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
138
|
+
string += "\uFFFD";
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const byte3 = bytes[i++];
|
|
143
|
+
if (byte3 === _internal_js_1.undefined) {
|
|
144
|
+
// Incomplete sequence
|
|
145
|
+
if (stream) {
|
|
146
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
if (this.fatal)
|
|
151
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
152
|
+
string += "\uFFFD";
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if ((byte2 & 0xc0) !== 0x80 || (byte3 & 0xc0) !== 0x80) {
|
|
157
|
+
if (this.fatal)
|
|
158
|
+
throw new _internal_js_1.TypeError("Invalid continuation bytes");
|
|
159
|
+
string += "\uFFFD";
|
|
160
|
+
i = startIndex + 1;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const codePoint = ((byte1 & 0x0f) << 12) |
|
|
164
|
+
((byte2 & 0x3f) << 6) |
|
|
165
|
+
(byte3 & 0x3f);
|
|
166
|
+
string += (0, _internal_js_1.StringFromCharCode)(codePoint);
|
|
167
|
+
}
|
|
168
|
+
else if (byte1 >= 0xf0 && byte1 <= 0xf7) {
|
|
169
|
+
// 4-byte sequence
|
|
170
|
+
const byte2 = bytes[i++];
|
|
171
|
+
if (byte2 === _internal_js_1.undefined) {
|
|
172
|
+
// Incomplete sequence
|
|
173
|
+
if (stream) {
|
|
174
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
if (this.fatal)
|
|
179
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
180
|
+
string += "\uFFFD";
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const byte3 = bytes[i++];
|
|
185
|
+
if (byte3 === _internal_js_1.undefined) {
|
|
186
|
+
// Incomplete sequence
|
|
187
|
+
if (stream) {
|
|
188
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
if (this.fatal)
|
|
193
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
194
|
+
string += "\uFFFD";
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const byte4 = bytes[i++];
|
|
199
|
+
if (byte4 === _internal_js_1.undefined) {
|
|
200
|
+
// Incomplete sequence
|
|
201
|
+
if (stream) {
|
|
202
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(bytes, startIndex), "f");
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
if (this.fatal)
|
|
207
|
+
throw new _internal_js_1.TypeError("Incomplete byte sequence");
|
|
208
|
+
string += "\uFFFD";
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if ((byte2 & 0xc0) !== 0x80 ||
|
|
213
|
+
(byte3 & 0xc0) !== 0x80 ||
|
|
214
|
+
(byte4 & 0xc0) !== 0x80) {
|
|
215
|
+
if (this.fatal)
|
|
216
|
+
throw new _internal_js_1.TypeError("Invalid continuation bytes");
|
|
217
|
+
string += "\uFFFD";
|
|
218
|
+
i = startIndex + 1;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
let codePoint = ((byte1 & 0x07) << 18) |
|
|
222
|
+
((byte2 & 0x3f) << 12) |
|
|
223
|
+
((byte3 & 0x3f) << 6) |
|
|
224
|
+
(byte4 & 0x3f);
|
|
225
|
+
codePoint -= 0x10000;
|
|
226
|
+
string += (0, _internal_js_1.StringFromCharCode)(0xd800 + ((codePoint >> 10) & 0x3ff), 0xdc00 + (codePoint & 0x3ff));
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
if (this.fatal)
|
|
230
|
+
throw new _internal_js_1.TypeError("Invalid byte");
|
|
231
|
+
string += "\uFFFD";
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// If not streaming, reset the buffer
|
|
235
|
+
if (!stream)
|
|
236
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, new _internal_js_1.Uint8Array(), "f");
|
|
237
|
+
return string;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.TextDecoder = TextDecoder;
|
|
241
|
+
_TextDecoder_encoding = new WeakMap(), _TextDecoder_fatal = new WeakMap(), _TextDecoder_ignoreBOM = new WeakMap(), _TextDecoder_buffer = new WeakMap();
|
|
242
|
+
//# sourceMappingURL=text_decoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text_decoder.js","sourceRoot":"","sources":["../src/text_decoder.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;;;;;;;;;;GAUG;AACH,iDAQwB;AA6BxB;;;;;;;GAOG;AACH,MAAa,WAAW;IAOtB;;;;OAIG;IACH,YAAY,KAAK,GAAG,OAAO,EAAE,UAA8B,EAAE;QAXpD,wCAAkB;QAClB,qCAAgB;QAChB,yCAAoB;QAE7B,8BAAU,IAAI,yBAAU,CAAC,CAAC,CAAC,EAAC;QAQ1B,uBAAA,IAAI,yBAAa,IAAA,gCAAiB,EAAC,KAAK,CAAC,MAAA,CAAC;QAC1C,uBAAA,IAAI,sBAAU,CAAC,CAAC,OAAO,CAAC,KAAK,MAAA,CAAC;QAC9B,uBAAA,IAAI,0BAAc,CAAC,CAAC,OAAO,CAAC,SAAS,MAAA,CAAC;QAEtC,IAAI,uBAAA,IAAI,6BAAU,KAAK,OAAO,EAAE,CAAC;YAC/B,MAAM,IAAI,wBAAS,CAAC,iBAAiB,KAAK,qBAAqB,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;IAED,oCAAoC;IACpC,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,6BAAU,CAAC;IACxB,CAAC;IAED,qDAAqD;IACrD,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,0BAAO,CAAC;IACrB,CAAC;IAED,0DAA0D;IAC1D,IAAI,SAAS;QACX,OAAO,uBAAA,IAAI,8BAAW,CAAC;IACzB,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,KAAoB,EAAE,OAA2B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
|
|
3
|
+
* Zero-dependency ponyfill for the native `TextDecoderStream` Web API.
|
|
4
|
+
*
|
|
5
|
+
* Uses the {@linkcode TextDecoder} ponyfill to decode UTF-8 bytes into strings
|
|
6
|
+
* in a streaming fashion. Requires the `TransformStream` API to be available
|
|
7
|
+
* in the current environment.
|
|
8
|
+
*
|
|
9
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream
|
|
10
|
+
* @category Streams
|
|
11
|
+
* @tags utf-8, decoder
|
|
12
|
+
*/
|
|
13
|
+
export declare class TextDecoderStream {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(label?: string, options?: TextDecoderOptions);
|
|
16
|
+
/**
|
|
17
|
+
* @returns the encoding standard being used by the underlying `TextDecoder`.
|
|
18
|
+
*/
|
|
19
|
+
get encoding(): string;
|
|
20
|
+
/**
|
|
21
|
+
* If true, invalid bytes will throw a TypeError. This reflects the value of
|
|
22
|
+
* the `fatal` option passed to the `TextDecoderStream` constructor.
|
|
23
|
+
*/
|
|
24
|
+
get fatal(): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* If true, the BOM (Byte Order Mark) will be ignored. This reflects the
|
|
27
|
+
* value of the `ignoreBOM` option passed to the `TextDecoderStream`
|
|
28
|
+
* constructor.
|
|
29
|
+
*/
|
|
30
|
+
get ignoreBOM(): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* @returns the readable stream side of the `TextDecoderStream`, which can be
|
|
33
|
+
* used to read the decoded strings as they are produced by the decoder.
|
|
34
|
+
*/
|
|
35
|
+
get readable(): ReadableStream<string>;
|
|
36
|
+
/**
|
|
37
|
+
* @returns the writable stream side of the `TextDecoderStream`, which can be
|
|
38
|
+
* used to write UTF-8 bytes to be decoded by the underlying `TextDecoder`.
|
|
39
|
+
*/
|
|
40
|
+
get writable(): WritableStream<BufferSource>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
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");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _TextDecoderStream_decoder, _TextDecoderStream_transform;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.TextDecoderStream = void 0;
|
|
16
|
+
/**
|
|
17
|
+
* This module provides a streaming text decoder implementation, as a ponyfill
|
|
18
|
+
* for the native `TextDecoderStream` Web API.
|
|
19
|
+
*
|
|
20
|
+
* Under the hood, it uses the `TextDecoder` ponyfill from this package to
|
|
21
|
+
* decode UTF-8 bytes into strings, and the native `TransformStream` API to
|
|
22
|
+
* handle the streaming process. It requires the `TransformStream` API to be
|
|
23
|
+
* available in the current environment.
|
|
24
|
+
*
|
|
25
|
+
* **Note**: This was directly adapted from the Deno `TextDecoderStream`
|
|
26
|
+
* implementation (MIT License), which is based on the WHATWG Streams standard.
|
|
27
|
+
*
|
|
28
|
+
* @module text-decoder-stream
|
|
29
|
+
*/
|
|
30
|
+
const _internal_js_1 = require("./_internal.js");
|
|
31
|
+
const text_decoder_js_1 = require("./text_decoder.js");
|
|
32
|
+
/**
|
|
33
|
+
* Zero-dependency ponyfill for the native `TextDecoderStream` Web API.
|
|
34
|
+
*
|
|
35
|
+
* Uses the {@linkcode TextDecoder} ponyfill to decode UTF-8 bytes into strings
|
|
36
|
+
* in a streaming fashion. Requires the `TransformStream` API to be available
|
|
37
|
+
* in the current environment.
|
|
38
|
+
*
|
|
39
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream
|
|
40
|
+
* @category Streams
|
|
41
|
+
* @tags utf-8, decoder
|
|
42
|
+
*/
|
|
43
|
+
class TextDecoderStream {
|
|
44
|
+
constructor(label = "utf-8", options = { __proto__: null }) {
|
|
45
|
+
_TextDecoderStream_decoder.set(this, void 0);
|
|
46
|
+
_TextDecoderStream_transform.set(this, void 0);
|
|
47
|
+
__classPrivateFieldSet(this, _TextDecoderStream_decoder, new text_decoder_js_1.TextDecoder(label, options), "f");
|
|
48
|
+
__classPrivateFieldSet(this, _TextDecoderStream_transform, new _internal_js_1.TransformStream({
|
|
49
|
+
transform: (chunk, controller) => {
|
|
50
|
+
try {
|
|
51
|
+
const decoded = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode(chunk, { stream: true });
|
|
52
|
+
if (decoded)
|
|
53
|
+
controller.enqueue(decoded);
|
|
54
|
+
return (0, _internal_js_1.PromiseResolve)();
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
return (0, _internal_js_1.PromiseReject)(e);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
flush: (controller) => {
|
|
61
|
+
try {
|
|
62
|
+
const flushed = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode();
|
|
63
|
+
if (flushed)
|
|
64
|
+
controller.enqueue(flushed);
|
|
65
|
+
return (0, _internal_js_1.PromiseResolve)();
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
return (0, _internal_js_1.PromiseReject)(e);
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
cancel: () => {
|
|
72
|
+
try {
|
|
73
|
+
__classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode();
|
|
74
|
+
return (0, _internal_js_1.PromiseResolve)();
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
return (0, _internal_js_1.PromiseReject)(e);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
}), "f");
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* @returns the encoding standard being used by the underlying `TextDecoder`.
|
|
84
|
+
*/
|
|
85
|
+
get encoding() {
|
|
86
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").encoding;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* If true, invalid bytes will throw a TypeError. This reflects the value of
|
|
90
|
+
* the `fatal` option passed to the `TextDecoderStream` constructor.
|
|
91
|
+
*/
|
|
92
|
+
get fatal() {
|
|
93
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").fatal;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* If true, the BOM (Byte Order Mark) will be ignored. This reflects the
|
|
97
|
+
* value of the `ignoreBOM` option passed to the `TextDecoderStream`
|
|
98
|
+
* constructor.
|
|
99
|
+
*/
|
|
100
|
+
get ignoreBOM() {
|
|
101
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").ignoreBOM;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @returns the readable stream side of the `TextDecoderStream`, which can be
|
|
105
|
+
* used to read the decoded strings as they are produced by the decoder.
|
|
106
|
+
*/
|
|
107
|
+
get readable() {
|
|
108
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").readable;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @returns the writable stream side of the `TextDecoderStream`, which can be
|
|
112
|
+
* used to write UTF-8 bytes to be decoded by the underlying `TextDecoder`.
|
|
113
|
+
*/
|
|
114
|
+
get writable() {
|
|
115
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").writable;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.TextDecoderStream = TextDecoderStream;
|
|
119
|
+
_TextDecoderStream_decoder = new WeakMap(), _TextDecoderStream_transform = new WeakMap();
|
|
120
|
+
//# sourceMappingURL=text_decoder_stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text_decoder_stream.js","sourceRoot":"","sources":["../src/text_decoder_stream.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;GAaG;AACH,iDAAgF;AAChF,uDAAyE;AAEzE;;;;;;;;;;GAUG;AACH,MAAa,iBAAiB;IAI5B,YACE,KAAK,GAAG,OAAO,EACf,UAA8B,EAAE,SAAS,EAAE,IAAI,EAAwB;QALzE,6CAAsB;QACtB,+CAAkD;QAMhD,uBAAA,IAAI,8BAAY,IAAI,6BAAW,CAAC,KAAK,EAAE,OAAO,CAAC,MAAA,CAAC;QAChD,uBAAA,IAAI,gCAAc,IAAI,8BAAe,CAAC;YACpC,SAAS,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBAC/B,IAAI,CAAC;oBACH,MAAM,OAAO,GAAG,uBAAA,IAAI,kCAAS,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;oBAC9D,IAAI,OAAO;wBAAE,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;oBACzC,OAAO,IAAA,6BAAc,GAAE,CAAC;gBAC1B,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,OAAO,IAAA,4BAAa,EAAC,CAAC,CAAC,CAAC;gBAC1B,CAAC;YACH,CAAC;YACD,KAAK,EAAE,CAAC,UAAU,EAAE,EAAE;gBACpB,IAAI,CAAC;oBACH,MAAM,OAAO,GAAG,uBAAA,IAAI,kCAAS,CAAC,MAAM,EAAE,CAAC;oBACvC,IAAI,OAAO;wBAAE,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;oBACzC,OAAO,IAAA,6BAAc,GAAE,CAAC;gBAC1B,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,OAAO,IAAA,4BAAa,EAAC,CAAC,CAAC,CAAC;gBAC1B,CAAC;YACH,CAAC;YACD,MAAM,EAAE,GAAG,EAAE;gBACX,IAAI,CAAC;oBACH,uBAAA,IAAI,kCAAS,CAAC,MAAM,EAAE,CAAC;oBACvB,OAAO,IAAA,6BAAc,GAAE,CAAC;gBAC1B,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,OAAO,IAAA,4BAAa,EAAC,CAAC,CAAC,CAAC;gBAC1B,CAAC;YACH,CAAC;SACF,CAAC,MAAA,CAAC;IACL,CAAC;IAED;;OAEG;IACH,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,kCAAS,CAAC,QAAQ,CAAC;IAChC,CAAC;IAED;;;OAGG;IACH,IAAI,KAAK;QACP,OAAO,uBAAA,IAAI,kCAAS,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,IAAI,SAAS;QACX,OAAO,uBAAA,IAAI,kCAAS,CAAC,SAAS,CAAC;IACjC,CAAC;IAED;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,oCAAW,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,uBAAA,IAAI,oCAAW,CAAC,QAAQ,CAAC;IAClC,CAAC;CACF;AA9ED,8CA8EC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the result of encoding a string into a `Uint8Array` using the
|
|
3
|
+
* {@linkcode TextEncoder.encodeInto} method, with the number of characters
|
|
4
|
+
* read from the source and number of bytes written to the destination.
|
|
5
|
+
*/
|
|
6
|
+
export interface TextEncoderEncodeIntoResult {
|
|
7
|
+
/** The number of characters read from the input string. */
|
|
8
|
+
read: number;
|
|
9
|
+
/** The number of bytes written to the output buffer. */
|
|
10
|
+
written: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Zero-dependency ponyfill for the native `TextEncoder` Web API.
|
|
14
|
+
*
|
|
15
|
+
* @category Encoding
|
|
16
|
+
* @tags utf-8, encoder
|
|
17
|
+
*/
|
|
18
|
+
export declare class TextEncoder {
|
|
19
|
+
/**
|
|
20
|
+
* The encoding standard to use. This is always `"utf-8"`.
|
|
21
|
+
* @returns "utf-8"
|
|
22
|
+
*/
|
|
23
|
+
get encoding(): string;
|
|
24
|
+
/**
|
|
25
|
+
* Encodes a string into a `Uint8Array` with UTF-8 encoding.
|
|
26
|
+
*
|
|
27
|
+
* @param input The string to encode. Defaults to an empty string.
|
|
28
|
+
* @returns A `Uint8Array` containing the UTF-8 encoded bytes.
|
|
29
|
+
*/
|
|
30
|
+
encode(input?: string): Uint8Array;
|
|
31
|
+
/**
|
|
32
|
+
* Encodes a string into a provided Uint8Array using UTF-8 encoding.
|
|
33
|
+
*
|
|
34
|
+
* @param input The string to encode.
|
|
35
|
+
* @param output The Uint8Array to write the encoded bytes into.
|
|
36
|
+
* @returns Object containing the number of characters read and bytes written
|
|
37
|
+
*/
|
|
38
|
+
encodeInto(input: string, output: Uint8Array): TextEncoderEncodeIntoResult;
|
|
39
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TextEncoder = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This module provides a high performance dependency-free ponyfill for the
|
|
6
|
+
* native `TextEncoder` Web API, allowing you to encode strings into UTF-8
|
|
7
|
+
* encoded `Uint8Array` buffers in any ES2015+ environment.
|
|
8
|
+
*
|
|
9
|
+
* @module text-encoder
|
|
10
|
+
*/
|
|
11
|
+
const _internal_js_1 = require("./_internal.js");
|
|
12
|
+
/**
|
|
13
|
+
* Zero-dependency ponyfill for the native `TextEncoder` Web API.
|
|
14
|
+
*
|
|
15
|
+
* @category Encoding
|
|
16
|
+
* @tags utf-8, encoder
|
|
17
|
+
*/
|
|
18
|
+
class TextEncoder {
|
|
19
|
+
/**
|
|
20
|
+
* The encoding standard to use. This is always `"utf-8"`.
|
|
21
|
+
* @returns "utf-8"
|
|
22
|
+
*/
|
|
23
|
+
get encoding() {
|
|
24
|
+
return "utf-8";
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Encodes a string into a `Uint8Array` with UTF-8 encoding.
|
|
28
|
+
*
|
|
29
|
+
* @param input The string to encode. Defaults to an empty string.
|
|
30
|
+
* @returns A `Uint8Array` containing the UTF-8 encoded bytes.
|
|
31
|
+
*/
|
|
32
|
+
encode(input = "") {
|
|
33
|
+
// speculatively allocate 4 B per character and trim the result later
|
|
34
|
+
const buffer = new _internal_js_1.Uint8Array(input.length * 4);
|
|
35
|
+
const result = this.encodeInto(input, buffer);
|
|
36
|
+
return (0, _internal_js_1.Uint8ArrayPrototypeSubarray)(buffer, 0, result.written);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Encodes a string into a provided Uint8Array using UTF-8 encoding.
|
|
40
|
+
*
|
|
41
|
+
* @param input The string to encode.
|
|
42
|
+
* @param output The Uint8Array to write the encoded bytes into.
|
|
43
|
+
* @returns Object containing the number of characters read and bytes written
|
|
44
|
+
*/
|
|
45
|
+
encodeInto(input, output) {
|
|
46
|
+
let read = 0, written = 0;
|
|
47
|
+
for (let i = 0; i < input.length; i++) {
|
|
48
|
+
const codePoint = (0, _internal_js_1.getCodePoint)(input, i);
|
|
49
|
+
if (codePoint > 0xffff)
|
|
50
|
+
i++; // handle surrogate pairs
|
|
51
|
+
const bytesNeeded = (0, _internal_js_1.utf8BytesNeeded)(codePoint);
|
|
52
|
+
if (written + bytesNeeded > output.length)
|
|
53
|
+
break;
|
|
54
|
+
if (codePoint <= 0x7f) {
|
|
55
|
+
output[written++] = codePoint;
|
|
56
|
+
}
|
|
57
|
+
else if (codePoint <= 0x7ff) {
|
|
58
|
+
output[written++] = 0xc0 | (codePoint >> 6);
|
|
59
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
60
|
+
}
|
|
61
|
+
else if (codePoint <= 0xffff) {
|
|
62
|
+
output[written++] = 0xe0 | (codePoint >> 12);
|
|
63
|
+
output[written++] = 0x80 | ((codePoint >> 6) & 0x3f);
|
|
64
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
output[written++] = 0xf0 | (codePoint >> 18);
|
|
68
|
+
output[written++] = 0x80 | ((codePoint >> 12) & 0x3f);
|
|
69
|
+
output[written++] = 0x80 | ((codePoint >> 6) & 0x3f);
|
|
70
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
71
|
+
}
|
|
72
|
+
read++;
|
|
73
|
+
}
|
|
74
|
+
return { read, written };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.TextEncoder = TextEncoder;
|
|
78
|
+
//# sourceMappingURL=text_encoder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text_encoder.js","sourceRoot":"","sources":["../src/text_encoder.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,iDAKwB;AAcxB;;;;;GAKG;AACH,MAAa,WAAW;IACtB;;;OAGG;IACH,IAAI,QAAQ;QACV,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,KAAK,GAAG,EAAE;QACf,qEAAqE;QACrE,MAAM,MAAM,GAAG,IAAI,yBAAU,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC9C,OAAO,IAAA,0CAA2B,EAAC,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;IAChE,C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