Skip to content

Commit a8c8797

Browse files
committed
refactor: updated the text encoder/ decoder types use node types
1 parent 3141bd7 commit a8c8797

1 file changed

Lines changed: 11 additions & 90 deletions

File tree

encoding.d.ts

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/// <reference types="node" />
22

3-
import type { Uint8ArrayBuffer } from './array.js';
4-
53
/**
64
* Converts an encoding label to its name, as an ASCII-lowercased string
75
* @param label - The encoding label to normalize
@@ -14,15 +12,20 @@ export function normalizeEncoding(label: string): string | null;
1412
* @param input - The bytes to check for BOM
1513
* @returns The encoding ('utf-8', 'utf-16le', 'utf-16be'), or null if no BOM found
1614
*/
17-
export function getBOMEncoding(input: ArrayBufferLike | ArrayBufferView): 'utf-8' | 'utf-16le' | 'utf-16be' | null;
15+
export function getBOMEncoding(
16+
input: ArrayBufferLike | ArrayBufferView
17+
): 'utf-8' | 'utf-16le' | 'utf-16be' | null;
1818

1919
/**
2020
* Implements decode (https://encoding.spec.whatwg.org/#decode) legacy hook.
2121
* @param input - The bytes to decode
2222
* @param fallbackEncoding - The encoding to use if no BOM detected (default: 'utf-8')
2323
* @returns The decoded string
2424
*/
25-
export function legacyHookDecode(input: ArrayBufferLike | ArrayBufferView, fallbackEncoding?: string): string;
25+
export function legacyHookDecode(
26+
input: ArrayBufferLike | ArrayBufferView,
27+
fallbackEncoding?: string
28+
): string;
2629

2730
/**
2831
* Converts an encoding label to its name, as a case-sensitive string.
@@ -35,103 +38,21 @@ export function labelToName(label: string): string | null;
3538
* Text decoder for decoding bytes to strings in various encodings
3639
* Supports strict and lossy modes
3740
*/
38-
export class TextDecoder {
39-
/** The encoding used by this decoder */
40-
readonly encoding: string;
41-
/** Whether the decoder throws on invalid sequences */
42-
readonly fatal: boolean;
43-
/** Whether the decoder ignores byte order marks */
44-
readonly ignoreBOM: boolean;
45-
46-
/**
47-
* Creates a new TextDecoder
48-
* @param encoding - The encoding to use (default: 'utf-8')
49-
* @param options - Decoder options
50-
* @param options.fatal - Throw on invalid sequences (default: false)
51-
* @param options.ignoreBOM - Ignore byte order mark (default: false)
52-
*/
53-
constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean });
54-
55-
/**
56-
* Decodes bytes to a string
57-
* @param input - The bytes to decode
58-
* @param options - Decode options
59-
* @param options.stream - Whether more data will follow (default: false)
60-
* @returns The decoded string
61-
*/
62-
decode(input?: ArrayBufferLike | ArrayBufferView, options?: { stream?: boolean }): string;
63-
64-
get [Symbol.toStringTag](): 'TextDecoder';
65-
}
41+
export const TextDecoder: typeof globalThis.TextDecoder;
6642

6743
/**
6844
* Text encoder for encoding strings to UTF-8 bytes
6945
*/
70-
export class TextEncoder {
71-
/** The encoding used by this encoder (always 'utf-8') */
72-
readonly encoding: 'utf-8';
73-
74-
constructor();
75-
76-
/**
77-
* Encodes a string to UTF-8 bytes
78-
* @param str - The string to encode
79-
* @returns The encoded bytes
80-
*/
81-
encode(str?: string): Uint8ArrayBuffer;
82-
83-
/**
84-
* Encodes a string into a target Uint8Array
85-
* @param str - The string to encode
86-
* @param target - The target Uint8Array to write to
87-
* @returns Object with read count and written count
88-
*/
89-
encodeInto(str: string, target: Uint8Array): { read: number; written: number };
90-
91-
get [Symbol.toStringTag](): 'TextEncoder';
92-
}
46+
export const TextEncoder: typeof globalThis.TextEncoder;
9347

9448
/**
9549
* Transform stream wrapper for TextDecoder
9650
* Decodes chunks of bytes to strings
9751
*/
98-
export class TextDecoderStream {
99-
/** The encoding used by this decoder */
100-
readonly encoding: string;
101-
/** Whether the decoder throws on invalid sequences */
102-
readonly fatal: boolean;
103-
/** Whether the decoder ignores byte order marks */
104-
readonly ignoreBOM: boolean;
105-
/** Readable side of the transform stream */
106-
readonly readable: ReadableStream<string>;
107-
/** Writable side of the transform stream */
108-
readonly writable: WritableStream<ArrayBufferLike | ArrayBufferView>;
109-
110-
/**
111-
* Creates a new TextDecoderStream
112-
* @param encoding - The encoding to use (default: 'utf-8')
113-
* @param options - Decoder options
114-
* @param options.fatal - Throw on invalid sequences (default: false)
115-
* @param options.ignoreBOM - Ignore byte order mark (default: false)
116-
*/
117-
constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean });
118-
119-
get [Symbol.toStringTag](): 'TextDecoderStream';
120-
}
52+
export const TextDecoderStream: typeof globalThis.TextDecoderStream;
12153

12254
/**
12355
* Transform stream wrapper for TextEncoder
12456
* Encodes chunks of strings to UTF-8 bytes
12557
*/
126-
export class TextEncoderStream {
127-
/** The encoding used by this encoder (always 'utf-8') */
128-
readonly encoding: 'utf-8';
129-
/** Readable side of the transform stream */
130-
readonly readable: ReadableStream<Uint8Array>;
131-
/** Writable side of the transform stream */
132-
readonly writable: WritableStream<string>;
133-
134-
constructor();
135-
136-
get [Symbol.toStringTag](): 'TextEncoderStream';
137-
}
58+
export const TextEncoderStream: typeof globalThis.TextEncoderStream;

0 commit comments

Comments
 (0)