@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
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _TextDecoder_encoding, _TextDecoder_fatal, _TextDecoder_ignoreBOM, _TextDecoder_buffer;
|
|
13
|
+
/**
|
|
14
|
+
* This module provides a high performance dependency-free ponyfill for the
|
|
15
|
+
* `TextDecoder` Web API, allowing you to decode UTF-8 encoded `BufferSource`
|
|
16
|
+
* buffers into strings in any ES2015+ environment.
|
|
17
|
+
*
|
|
18
|
+
* Currently only UTF-8 encoding is supported, but additional encodings will be
|
|
19
|
+
* added in the near future. Encodings that are currently being implemented are
|
|
20
|
+
* UTF-16, ISO-8859-1, Windows-1252, and ASCII.
|
|
21
|
+
*
|
|
22
|
+
* @module text-decoder
|
|
23
|
+
*/
|
|
24
|
+
import { normalizeEncoding, StringFromCharCode, toUint8Array, TypeError, Uint8Array, Uint8ArrayPrototypeSubarray, undefined, } from "./_internal.js";
|
|
25
|
+
/**
|
|
26
|
+
* Decodes an encoded sequence of bytes into a string, using the specified
|
|
27
|
+
* encoding standard. Currently, only UTF-8 encoding is supported.
|
|
28
|
+
*
|
|
29
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
|
|
30
|
+
* @category Encoding
|
|
31
|
+
* @tags utf-8, decoder
|
|
32
|
+
*/
|
|
33
|
+
export class TextDecoder {
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new TextDecoder instance.
|
|
36
|
+
* @param label The encoding to use. Currently, only "utf-8" is supported.
|
|
37
|
+
* @param options Configuration options.
|
|
38
|
+
*/
|
|
39
|
+
constructor(label = "utf-8", options = {}) {
|
|
40
|
+
_TextDecoder_encoding.set(this, void 0);
|
|
41
|
+
_TextDecoder_fatal.set(this, void 0);
|
|
42
|
+
_TextDecoder_ignoreBOM.set(this, void 0);
|
|
43
|
+
_TextDecoder_buffer.set(this, new Uint8Array(0));
|
|
44
|
+
__classPrivateFieldSet(this, _TextDecoder_encoding, normalizeEncoding(label), "f");
|
|
45
|
+
__classPrivateFieldSet(this, _TextDecoder_fatal, !!options.fatal, "f");
|
|
46
|
+
__classPrivateFieldSet(this, _TextDecoder_ignoreBOM, !!options.ignoreBOM, "f");
|
|
47
|
+
if (__classPrivateFieldGet(this, _TextDecoder_encoding, "f") !== "utf-8") {
|
|
48
|
+
throw new TypeError(`The encoding "${label}" is not supported.`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** The encoding standard to use. */
|
|
52
|
+
get encoding() {
|
|
53
|
+
return __classPrivateFieldGet(this, _TextDecoder_encoding, "f");
|
|
54
|
+
}
|
|
55
|
+
/** If true, invalid bytes will throw a TypeError. */
|
|
56
|
+
get fatal() {
|
|
57
|
+
return __classPrivateFieldGet(this, _TextDecoder_fatal, "f");
|
|
58
|
+
}
|
|
59
|
+
/** If true, the BOM (Byte Order Mark) will be ignored. */
|
|
60
|
+
get ignoreBOM() {
|
|
61
|
+
return __classPrivateFieldGet(this, _TextDecoder_ignoreBOM, "f");
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Decodes a BufferSource into a string using UTF-8 decoding.
|
|
65
|
+
*
|
|
66
|
+
* @param input The bytes to decode. Defaults to an empty Uint8Array.
|
|
67
|
+
* @param [options] Decoding options.
|
|
68
|
+
* @returns The decoded string.
|
|
69
|
+
* @throws if the input is not a BufferSource.
|
|
70
|
+
* @throws if fatal is true and an invalid byte sequence is encountered.
|
|
71
|
+
*/
|
|
72
|
+
decode(input, options) {
|
|
73
|
+
const stream = options?.stream ?? false;
|
|
74
|
+
let bytes = toUint8Array(input);
|
|
75
|
+
// Concatenate any leftover bytes from the previous decode call
|
|
76
|
+
if (__classPrivateFieldGet(this, _TextDecoder_buffer, "f").length > 0) {
|
|
77
|
+
const combined = new Uint8Array(__classPrivateFieldGet(this, _TextDecoder_buffer, "f").length + bytes.length);
|
|
78
|
+
combined.set(__classPrivateFieldGet(this, _TextDecoder_buffer, "f"), 0);
|
|
79
|
+
combined.set(bytes, __classPrivateFieldGet(this, _TextDecoder_buffer, "f").length);
|
|
80
|
+
bytes = combined;
|
|
81
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, new Uint8Array(), "f");
|
|
82
|
+
}
|
|
83
|
+
let string = "", i = 0;
|
|
84
|
+
// Handle BOM
|
|
85
|
+
if (!this.ignoreBOM &&
|
|
86
|
+
bytes.length >= 3 &&
|
|
87
|
+
bytes[0] === 0xef &&
|
|
88
|
+
bytes[1] === 0xbb &&
|
|
89
|
+
bytes[2] === 0xbf)
|
|
90
|
+
i += 3;
|
|
91
|
+
while (i < bytes.length) {
|
|
92
|
+
const startIndex = i;
|
|
93
|
+
const byte1 = bytes[i++];
|
|
94
|
+
if (byte1 <= 0x7f) {
|
|
95
|
+
string += StringFromCharCode(byte1);
|
|
96
|
+
}
|
|
97
|
+
else if (byte1 >= 0xc0 && byte1 <= 0xdf) {
|
|
98
|
+
// 2-byte sequence
|
|
99
|
+
const byte2 = bytes[i++];
|
|
100
|
+
if (byte2 === undefined) {
|
|
101
|
+
// Incomplete sequence
|
|
102
|
+
if (stream) {
|
|
103
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (this.fatal)
|
|
108
|
+
throw new TypeError("Incomplete byte sequence");
|
|
109
|
+
string += "\uFFFD";
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if ((byte2 & 0xc0) !== 0x80) {
|
|
114
|
+
if (this.fatal)
|
|
115
|
+
throw new TypeError("Invalid continuation byte");
|
|
116
|
+
string += "\uFFFD";
|
|
117
|
+
i = startIndex + 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const codePoint = ((byte1 & 0x1f) << 6) | (byte2 & 0x3f);
|
|
121
|
+
string += StringFromCharCode(codePoint);
|
|
122
|
+
}
|
|
123
|
+
else if (byte1 >= 0xe0 && byte1 <= 0xef) {
|
|
124
|
+
// 3-byte sequence
|
|
125
|
+
const byte2 = bytes[i++];
|
|
126
|
+
if (byte2 === undefined) {
|
|
127
|
+
// Incomplete sequence
|
|
128
|
+
if (stream) {
|
|
129
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
if (this.fatal)
|
|
134
|
+
throw new TypeError("Incomplete byte sequence");
|
|
135
|
+
string += "\uFFFD";
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const byte3 = bytes[i++];
|
|
140
|
+
if (byte3 === undefined) {
|
|
141
|
+
// Incomplete sequence
|
|
142
|
+
if (stream) {
|
|
143
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
if (this.fatal)
|
|
148
|
+
throw new TypeError("Incomplete byte sequence");
|
|
149
|
+
string += "\uFFFD";
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if ((byte2 & 0xc0) !== 0x80 || (byte3 & 0xc0) !== 0x80) {
|
|
154
|
+
if (this.fatal)
|
|
155
|
+
throw new TypeError("Invalid continuation bytes");
|
|
156
|
+
string += "\uFFFD";
|
|
157
|
+
i = startIndex + 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const codePoint = ((byte1 & 0x0f) << 12) |
|
|
161
|
+
((byte2 & 0x3f) << 6) |
|
|
162
|
+
(byte3 & 0x3f);
|
|
163
|
+
string += StringFromCharCode(codePoint);
|
|
164
|
+
}
|
|
165
|
+
else if (byte1 >= 0xf0 && byte1 <= 0xf7) {
|
|
166
|
+
// 4-byte sequence
|
|
167
|
+
const byte2 = bytes[i++];
|
|
168
|
+
if (byte2 === undefined) {
|
|
169
|
+
// Incomplete sequence
|
|
170
|
+
if (stream) {
|
|
171
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
if (this.fatal)
|
|
176
|
+
throw new TypeError("Incomplete byte sequence");
|
|
177
|
+
string += "\uFFFD";
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const byte3 = bytes[i++];
|
|
182
|
+
if (byte3 === undefined) {
|
|
183
|
+
// Incomplete sequence
|
|
184
|
+
if (stream) {
|
|
185
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
if (this.fatal)
|
|
190
|
+
throw new TypeError("Incomplete byte sequence");
|
|
191
|
+
string += "\uFFFD";
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const byte4 = bytes[i++];
|
|
196
|
+
if (byte4 === undefined) {
|
|
197
|
+
// Incomplete sequence
|
|
198
|
+
if (stream) {
|
|
199
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, Uint8ArrayPrototypeSubarray(bytes, startIndex), "f");
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
if (this.fatal)
|
|
204
|
+
throw new TypeError("Incomplete byte sequence");
|
|
205
|
+
string += "\uFFFD";
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if ((byte2 & 0xc0) !== 0x80 ||
|
|
210
|
+
(byte3 & 0xc0) !== 0x80 ||
|
|
211
|
+
(byte4 & 0xc0) !== 0x80) {
|
|
212
|
+
if (this.fatal)
|
|
213
|
+
throw new TypeError("Invalid continuation bytes");
|
|
214
|
+
string += "\uFFFD";
|
|
215
|
+
i = startIndex + 1;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
let codePoint = ((byte1 & 0x07) << 18) |
|
|
219
|
+
((byte2 & 0x3f) << 12) |
|
|
220
|
+
((byte3 & 0x3f) << 6) |
|
|
221
|
+
(byte4 & 0x3f);
|
|
222
|
+
codePoint -= 0x10000;
|
|
223
|
+
string += StringFromCharCode(0xd800 + ((codePoint >> 10) & 0x3ff), 0xdc00 + (codePoint & 0x3ff));
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
if (this.fatal)
|
|
227
|
+
throw new TypeError("Invalid byte");
|
|
228
|
+
string += "\uFFFD";
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// If not streaming, reset the buffer
|
|
232
|
+
if (!stream)
|
|
233
|
+
__classPrivateFieldSet(this, _TextDecoder_buffer, new Uint8Array(), "f");
|
|
234
|
+
return string;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
_TextDecoder_encoding = new WeakMap(), _TextDecoder_fatal = new WeakMap(), _TextDecoder_ignoreBOM = new WeakMap(), _TextDecoder_buffer = new WeakMap();
|
|
238
|
+
//# 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,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,EACZ,SAAS,EACT,UAAU,EACV,2BAA2B,EAC3B,SAAS,GACV,MAAM,gBAAgB,CAAC;AA6BxB;;;;;;;GAOG;AACH,MAAM,OAAO,WAAW;IAOtB;;;;OAIG;IACH,YAAY,KAAK,GAAG,OAAO,EAAE,UAA8B,EAAE;QAXpD,wCAAkB;QAClB,qCAAgB;QAChB,yCAAoB;QAE7B,8BAAU,IAAI,UAAU,CAAC,CAAC,CAAC,EAAC;QAQ1B,uBAAA,IAAI,yBAAa,iBAAiB,CAAC,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,SAAS,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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,KAAK,IAAI;oBACvB,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI;oBACvB,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,EACvB,CAAC;oBACD,IAAI,IAAI,CAAC,KAAK;wBAAE,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAC;oBAClE,MAAM,IAAI,QAAQ,CAAC;oBACnB,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC;oBACnB,SAAS;gBACX,CAAC;gBACD,IAAI,SAAS,GAAG,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;oBACpC,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;oBACtB,CAAC,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;oBACrB,CAAC,KAAK,GAAG,IAAI,CAAC,CAAC;gBACjB,SAAS,IAAI,OAAO,CAAC;gBACrB,MAAM,IAAI,kBAAkB,CAC1B,MAAM,GAAG,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,GAAG,KAAK,CAAC,EACpC,MAAM,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,CAC7B,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,IAAI,IAAI,CAAC,KAAK;oBAAE,MAAM,IAAI,SAAS,CAAC,cAAc,CAAC,CAAC;gBACpD,MAAM,IAAI,QAAQ,CAAC;YACrB,CAAC;QACH,CAAC;QAED,qCAAqC;QACrC,IAAI,CAAC,MAAM;YAAE,uBAAA,IAAI,uBAAW,IAAI,UAAU,EAAE,MAAA,CAAC;QAE7C,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type TextDecoderOptions } from "./text_decoder.js";
|
|
2
|
+
/**
|
|
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,116 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _TextDecoderStream_decoder, _TextDecoderStream_transform;
|
|
13
|
+
/**
|
|
14
|
+
* This module provides a streaming text decoder implementation, as a ponyfill
|
|
15
|
+
* for the native `TextDecoderStream` Web API.
|
|
16
|
+
*
|
|
17
|
+
* Under the hood, it uses the `TextDecoder` ponyfill from this package to
|
|
18
|
+
* decode UTF-8 bytes into strings, and the native `TransformStream` API to
|
|
19
|
+
* handle the streaming process. It requires the `TransformStream` API to be
|
|
20
|
+
* available in the current environment.
|
|
21
|
+
*
|
|
22
|
+
* **Note**: This was directly adapted from the Deno `TextDecoderStream`
|
|
23
|
+
* implementation (MIT License), which is based on the WHATWG Streams standard.
|
|
24
|
+
*
|
|
25
|
+
* @module text-decoder-stream
|
|
26
|
+
*/
|
|
27
|
+
import { PromiseReject, PromiseResolve, TransformStream } from "./_internal.js";
|
|
28
|
+
import { TextDecoder } from "./text_decoder.js";
|
|
29
|
+
/**
|
|
30
|
+
* Zero-dependency ponyfill for the native `TextDecoderStream` Web API.
|
|
31
|
+
*
|
|
32
|
+
* Uses the {@linkcode TextDecoder} ponyfill to decode UTF-8 bytes into strings
|
|
33
|
+
* in a streaming fashion. Requires the `TransformStream` API to be available
|
|
34
|
+
* in the current environment.
|
|
35
|
+
*
|
|
36
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream
|
|
37
|
+
* @category Streams
|
|
38
|
+
* @tags utf-8, decoder
|
|
39
|
+
*/
|
|
40
|
+
export class TextDecoderStream {
|
|
41
|
+
constructor(label = "utf-8", options = { __proto__: null }) {
|
|
42
|
+
_TextDecoderStream_decoder.set(this, void 0);
|
|
43
|
+
_TextDecoderStream_transform.set(this, void 0);
|
|
44
|
+
__classPrivateFieldSet(this, _TextDecoderStream_decoder, new TextDecoder(label, options), "f");
|
|
45
|
+
__classPrivateFieldSet(this, _TextDecoderStream_transform, new TransformStream({
|
|
46
|
+
transform: (chunk, controller) => {
|
|
47
|
+
try {
|
|
48
|
+
const decoded = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode(chunk, { stream: true });
|
|
49
|
+
if (decoded)
|
|
50
|
+
controller.enqueue(decoded);
|
|
51
|
+
return PromiseResolve();
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
return PromiseReject(e);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
flush: (controller) => {
|
|
58
|
+
try {
|
|
59
|
+
const flushed = __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode();
|
|
60
|
+
if (flushed)
|
|
61
|
+
controller.enqueue(flushed);
|
|
62
|
+
return PromiseResolve();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
return PromiseReject(e);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
cancel: () => {
|
|
69
|
+
try {
|
|
70
|
+
__classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").decode();
|
|
71
|
+
return PromiseResolve();
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
return PromiseReject(e);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
}), "f");
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @returns the encoding standard being used by the underlying `TextDecoder`.
|
|
81
|
+
*/
|
|
82
|
+
get encoding() {
|
|
83
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").encoding;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* If true, invalid bytes will throw a TypeError. This reflects the value of
|
|
87
|
+
* the `fatal` option passed to the `TextDecoderStream` constructor.
|
|
88
|
+
*/
|
|
89
|
+
get fatal() {
|
|
90
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").fatal;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* If true, the BOM (Byte Order Mark) will be ignored. This reflects the
|
|
94
|
+
* value of the `ignoreBOM` option passed to the `TextDecoderStream`
|
|
95
|
+
* constructor.
|
|
96
|
+
*/
|
|
97
|
+
get ignoreBOM() {
|
|
98
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_decoder, "f").ignoreBOM;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @returns the readable stream side of the `TextDecoderStream`, which can be
|
|
102
|
+
* used to read the decoded strings as they are produced by the decoder.
|
|
103
|
+
*/
|
|
104
|
+
get readable() {
|
|
105
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").readable;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* @returns the writable stream side of the `TextDecoderStream`, which can be
|
|
109
|
+
* used to write UTF-8 bytes to be decoded by the underlying `TextDecoder`.
|
|
110
|
+
*/
|
|
111
|
+
get writable() {
|
|
112
|
+
return __classPrivateFieldGet(this, _TextDecoderStream_transform, "f").writable;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
_TextDecoderStream_decoder = new WeakMap(), _TextDecoderStream_transform = new WeakMap();
|
|
116
|
+
//# 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;;;;;;;;;;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,CAAC,MAAM,EAAE,CAAC;oBACvB,OAAO,cAAc,EAAE,CAAC;gBAC1B,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,OAAO,aAAa,CAAC,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"}
|
|
@@ -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,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This module provides a high performance dependency-free ponyfill for the
|
|
3
|
+
* native `TextEncoder` Web API, allowing you to encode strings into UTF-8
|
|
4
|
+
* encoded `Uint8Array` buffers in any ES2015+ environment.
|
|
5
|
+
*
|
|
6
|
+
* @module text-encoder
|
|
7
|
+
*/
|
|
8
|
+
import { getCodePoint, Uint8Array, Uint8ArrayPrototypeSubarray, utf8BytesNeeded, } from "./_internal.js";
|
|
9
|
+
/**
|
|
10
|
+
* Zero-dependency ponyfill for the native `TextEncoder` Web API.
|
|
11
|
+
*
|
|
12
|
+
* @category Encoding
|
|
13
|
+
* @tags utf-8, encoder
|
|
14
|
+
*/
|
|
15
|
+
export class TextEncoder {
|
|
16
|
+
/**
|
|
17
|
+
* The encoding standard to use. This is always `"utf-8"`.
|
|
18
|
+
* @returns "utf-8"
|
|
19
|
+
*/
|
|
20
|
+
get encoding() {
|
|
21
|
+
return "utf-8";
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Encodes a string into a `Uint8Array` with UTF-8 encoding.
|
|
25
|
+
*
|
|
26
|
+
* @param input The string to encode. Defaults to an empty string.
|
|
27
|
+
* @returns A `Uint8Array` containing the UTF-8 encoded bytes.
|
|
28
|
+
*/
|
|
29
|
+
encode(input = "") {
|
|
30
|
+
// speculatively allocate 4 B per character and trim the result later
|
|
31
|
+
const buffer = new Uint8Array(input.length * 4);
|
|
32
|
+
const result = this.encodeInto(input, buffer);
|
|
33
|
+
return Uint8ArrayPrototypeSubarray(buffer, 0, result.written);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Encodes a string into a provided Uint8Array using UTF-8 encoding.
|
|
37
|
+
*
|
|
38
|
+
* @param input The string to encode.
|
|
39
|
+
* @param output The Uint8Array to write the encoded bytes into.
|
|
40
|
+
* @returns Object containing the number of characters read and bytes written
|
|
41
|
+
*/
|
|
42
|
+
encodeInto(input, output) {
|
|
43
|
+
let read = 0, written = 0;
|
|
44
|
+
for (let i = 0; i < input.length; i++) {
|
|
45
|
+
const codePoint = getCodePoint(input, i);
|
|
46
|
+
if (codePoint > 0xffff)
|
|
47
|
+
i++; // handle surrogate pairs
|
|
48
|
+
const bytesNeeded = utf8BytesNeeded(codePoint);
|
|
49
|
+
if (written + bytesNeeded > output.length)
|
|
50
|
+
break;
|
|
51
|
+
if (codePoint <= 0x7f) {
|
|
52
|
+
output[written++] = codePoint;
|
|
53
|
+
}
|
|
54
|
+
else if (codePoint <= 0x7ff) {
|
|
55
|
+
output[written++] = 0xc0 | (codePoint >> 6);
|
|
56
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
57
|
+
}
|
|
58
|
+
else if (codePoint <= 0xffff) {
|
|
59
|
+
output[written++] = 0xe0 | (codePoint >> 12);
|
|
60
|
+
output[written++] = 0x80 | ((codePoint >> 6) & 0x3f);
|
|
61
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
output[written++] = 0xf0 | (codePoint >> 18);
|
|
65
|
+
output[written++] = 0x80 | ((codePoint >> 12) & 0x3f);
|
|
66
|
+
output[written++] = 0x80 | ((codePoint >> 6) & 0x3f);
|
|
67
|
+
output[written++] = 0x80 | (codePoint & 0x3f);
|
|
68
|
+
}
|
|
69
|
+
read++;
|
|
70
|
+
}
|
|
71
|
+
return { read, written };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# 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,OAAO,EACL,YAAY,EACZ,UAAU,EACV,2BAA2B,EAC3B,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAcxB;;;;;GAKG;AACH,MAAM,OAAO,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,UAAU,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QAC9C,OAAO,2BAA2B,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;IAChE,CAAC;IAED;;;;;;OAMG;IACH,UAAU,CAAC,KAAa,EAAE,MAAkB;QAC1C,IAAI,IAAI,GAAG,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC;QAC1B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACtC,MAAM,SAAS,GAAG,YAAY,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAEzC,IAAI,SAAS,GAAG,MAAM;gBAAE,CAAC,EAAE,CAAC,CAAC,yBAA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AS,GAAG,IAAI,CAAC,CAAC;YAChD,CAAC;YACD,IAAI,EAAE,CAAC;QACT,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency ponyfill for the native `TextEncoderStream` Web API.
|
|
3
|
+
*
|
|
4
|
+
* Uses the {@linkcode TextEncoder} ponyfill to encode strings into UTF-8
|
|
5
|
+
* bytes in a streaming fashion. Requires the `TransformStream` API to be
|
|
6
|
+
* available in the current environment.
|
|
7
|
+
*
|
|
8
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoderStream
|
|
9
|
+
* @category Streams
|
|
10
|
+
* @tags utf-8, encoder
|
|
11
|
+
*/
|
|
12
|
+
export declare class TextEncoderStream {
|
|
13
|
+
#private;
|
|
14
|
+
constructor();
|
|
15
|
+
/** The encoding standard to use. This is always `"utf-8"`. */
|
|
16
|
+
get encoding(): string;
|
|
17
|
+
/**
|
|
18
|
+
* @returns the readable stream side of the `TextEncoderStream`, which can be
|
|
19
|
+
* used to read the encoded bytes as they are produced by the encoder.
|
|
20
|
+
*/
|
|
21
|
+
get readable(): ReadableStream<Uint8Array>;
|
|
22
|
+
/**
|
|
23
|
+
* @returns the writable stream side of the `TextEncoderStream`, which can be
|
|
24
|
+
* used to write strings to be encoded by the underlying `TextEncoder`.
|
|
25
|
+
*/
|
|
26
|
+
get writable(): WritableStream<string>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
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");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
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");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _TextEncoderStream_encoder, _TextEncoderStream_transform;
|
|
13
|
+
/**
|
|
14
|
+
* This module provides a streaming encoder for UTF-8 text, which is based on
|
|
15
|
+
* the `TextEncoder` API.
|
|
16
|
+
*
|
|
17
|
+
* This is a zero-dependency ponyfill for the native `TextEncoderStream` Web
|
|
18
|
+
* API, which can be used in any ES2015+ environment with support for the
|
|
19
|
+
* `TransformStream` API.
|
|
20
|
+
*
|
|
21
|
+
* **Note**: This was directly adapted from the Deno `TextEncoderStream`
|
|
22
|
+
* implementation (MIT License), which is based on the WHATWG Streams standard.
|
|
23
|
+
*
|
|
24
|
+
* @module text-encoder-stream
|
|
25
|
+
*/
|
|
26
|
+
import { TransformStream } from "./_internal.js";
|
|
27
|
+
import { TextEncoder } from "./text_encoder.js";
|
|
28
|
+
/**
|
|
29
|
+
* Zero-dependency ponyfill for the native `TextEncoderStream` Web API.
|
|
30
|
+
*
|
|
31
|
+
* Uses the {@linkcode TextEncoder} ponyfill to encode strings into UTF-8
|
|
32
|
+
* bytes in a streaming fashion. Requires the `TransformStream` API to be
|
|
33
|
+
* available in the current environment.
|
|
34
|
+
*
|
|
35
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoderStream
|
|
36
|
+
* @category Streams
|
|
37
|
+
* @tags utf-8, encoder
|
|
38
|
+
*/
|
|
39
|
+
export class TextEncoderStream {
|
|
40
|
+
constructor() {
|
|
41
|
+
_TextEncoderStream_encoder.set(this, void 0);
|
|
42
|
+
_TextEncoderStream_transform.set(this, void 0);
|
|
43
|
+
__classPrivateFieldSet(this, _TextEncoderStream_encoder, new TextEncoder(), "f");
|
|
44
|
+
__classPrivateFieldSet(this, _TextEncoderStream_transform, new TransformStream({
|
|
45
|
+
transform: (chunk, controller) => {
|
|
46
|
+
const encoded = __classPrivateFieldGet(this, _TextEncoderStream_encoder, "f").encode(chunk);
|
|
47
|
+
controller.enqueue(encoded);
|
|
48
|
+
},
|
|
49
|
+
}), "f");
|
|
50
|
+
}
|
|
51
|
+
/** The encoding standard to use. This is always `"utf-8"`. */
|
|
52
|
+
get encoding() {
|
|
53
|
+
return "utf-8";
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* @returns the readable stream side of the `TextEncoderStream`, which can be
|
|
57
|
+
* used to read the encoded bytes as they are produced by the encoder.
|
|
58
|
+
*/
|
|
59
|
+
get readable() {
|
|
60
|
+
return __classPrivateFieldGet(this, _TextEncoderStream_transform, "f").readable;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @returns the writable stream side of the `TextEncoderStream`, which can be
|
|
64
|
+
* used to write strings to be encoded by the underlying `TextEncoder`.
|
|
65
|
+
*/
|
|
66
|
+
get writable() {
|
|
67
|
+
return __classPrivateFieldGet(this, _TextEncoderStream_transform, "f").writable;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
_TextEncoderStream_encoder = new WeakMap(), _TextEncoderStream_transform = new WeakMap();
|
|
71
|
+
//# sourceMappingURL=text_encoder_stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text_encoder_stream.js","sourceRoot":"","sources":["../src/text_encoder_stream.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;;;;;;;;;GAUG;AACH,MAAM,OAAO,iBAAiB;IAI5B;QAHA,6CAAsB;QACtB,+CAAgD;QAG9C,uBAAA,IAAI,8BAAY,IAAI,WAAW,EAAE,MAAA,CAAC;QAClC,uBAAA,IAAI,gCAAc,IAAI,eAAe,CAAC;YACpC,SAAS,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE;gBAC/B,MAAM,OAAO,GAAG,uBAAA,IAAI,kCAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAC5C,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;YAC9B,CAAC;SACF,CAAC,MAAA,CAAC;IACL,CAAC;IAED,8DAA8D;IAC9D,IAAI,QAAQ;QACV,OAAO,OAAO,CAAC;IACjB,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"}
|