UNPKG

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