-
Notifications
You must be signed in to change notification settings - Fork 4
/
text-encoder-stream-polyfill.ts
63 lines (57 loc) · 1.72 KB
/
text-encoder-stream-polyfill.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
if (!('TextEncoderStream' in self)) {
class TES {
encoder!: TextEncoder;
start() { this.encoder = new TextEncoder() }
transform(chunk: string, controller: TransformStreamDefaultController<Uint8Array>) {
controller.enqueue(this.encoder.encode(chunk))
}
}
class JSTextEncoderStream extends TransformStream<string, Uint8Array> {
#t: TES;
constructor() {
const t = new TES();
super(t);
this.#t = t;
}
get encoding() { return this.#t.encoder.encoding }
}
Object.defineProperty(self, 'TextEncoderStream', {
configurable: false,
enumerable: false,
writable: false,
value: JSTextEncoderStream,
});
}
if (!('TextDecoderStream' in self)) {
class TDS {
decoder!: TextDecoder;
encoding: string;
options: TextDecoderOptions;
constructor(encoding: string, options: TextDecoderOptions) {
this.encoding = encoding;
this.options = options;
}
start() { this.decoder = new TextDecoder(this.encoding, this.options) }
transform(chunk: Uint8Array, controller: TransformStreamDefaultController<string>) {
controller.enqueue(this.decoder.decode(chunk, { stream: true }))
}
}
class JSTextDecoderStream extends TransformStream<Uint8Array, string> {
#t: TDS;
constructor(encoding = 'utf-8', { ...options } = {}) {
const t = new TDS(encoding, options);
super(t);
this.#t = t;
}
get encoding() { return this.#t.decoder.encoding }
get fatal() { return this.#t.decoder.fatal }
get ignoreBOM() { return this.#t.decoder.ignoreBOM }
}
Object.defineProperty(self, 'TextDecoderStream', {
configurable: false,
enumerable: false,
writable: false,
value: JSTextDecoderStream,
});
}
export {}