-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.ts
More file actions
143 lines (140 loc) · 3.96 KB
/
encoding.ts
File metadata and controls
143 lines (140 loc) · 3.96 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @fileoverview Encoding-name normalization. Maps user-supplied encoding
* strings (case- and dash-variant) to the canonical Node Buffer
* encoding tokens. Mirrors `internal/util.js#normalizeEncoding` so the
* common-case fast path is identical to Node's, and slow cases delegate
* to a separate function so v8 will inline the hot one.
*/
import type { BufferEncoding } from './types'
/**
* Normalize encoding string to canonical form.
* Handles common encodings inline for performance, delegates to slowCases for others.
*
* Based on Node.js internal/util.js normalizeEncoding implementation.
* @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
*
* @param enc - Encoding to normalize (can be null/undefined)
* @returns Normalized encoding string, defaults to 'utf8'
*
* @example
* ```ts
* normalizeEncoding('UTF-8') // Returns 'utf8'
* normalizeEncoding('binary') // Returns 'latin1'
* normalizeEncoding('ucs-2') // Returns 'utf16le'
* normalizeEncoding(null) // Returns 'utf8'
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function normalizeEncoding(
enc: BufferEncoding | string | null | undefined,
): BufferEncoding {
return enc == null || enc === 'utf8' || enc === 'utf-8'
? 'utf8'
: normalizeEncodingSlow(enc)
}
/**
* Move the "slow cases" to a separate function to make sure this function gets
* inlined properly. That prioritizes the common case.
*
* Based on Node.js internal/util.js normalizeEncoding implementation.
* @see https://github.com/nodejs/node/blob/ae62b36d442b7bf987e85ae6e0df0f02cc1bb17f/lib/internal/util.js#L247-L310
*
* @param enc - Encoding to normalize
* @returns Normalized encoding string, defaults to 'utf8' for unknown encodings
*
* @example
* ```typescript
* normalizeEncodingSlow('ucs2') // 'utf16le'
* normalizeEncodingSlow('LATIN1') // 'latin1'
* normalizeEncodingSlow('binary') // 'latin1'
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function normalizeEncodingSlow(enc: string): BufferEncoding {
const { length } = enc
if (length === 4) {
if (enc === 'ucs2' || enc === 'UCS2') {
return 'utf16le'
}
if (enc.toLowerCase() === 'ucs2') {
return 'utf16le'
}
} else if (
(length === 3 && enc === 'hex') ||
enc === 'HEX' ||
enc.toLowerCase() === 'hex'
) {
return 'hex'
} else if (length === 5) {
if (enc === 'ascii') {
return 'ascii'
}
if (enc === 'ucs-2') {
return 'utf16le'
}
if (enc === 'ASCII') {
return 'ascii'
}
if (enc === 'UCS-2') {
return 'utf16le'
}
enc = enc.toLowerCase()
if (enc === 'ascii') {
return 'ascii'
}
if (enc === 'ucs-2') {
return 'utf16le'
}
} else if (length === 6) {
if (enc === 'base64') {
return 'base64'
}
if (enc === 'latin1' || enc === 'binary') {
return 'latin1'
}
if (enc === 'BASE64') {
return 'base64'
}
if (enc === 'LATIN1' || enc === 'BINARY') {
return 'latin1'
}
enc = enc.toLowerCase()
if (enc === 'base64') {
return 'base64'
}
if (enc === 'latin1' || enc === 'binary') {
return 'latin1'
}
// Length 7/8/9 branches handle utf16le, utf-16le, base64url. Each
// length needs a specific encoding; tests cover the canonical forms
// but the inner case-coercion sub-arms (the second/third operand
// of each `||`) fire only on mixed-case inputs.
/* c8 ignore start */
} else if (length === 7) {
if (
enc === 'utf16le' ||
enc === 'UTF16LE' ||
enc.toLowerCase() === 'utf16le'
) {
return 'utf16le'
}
} else if (length === 8) {
if (
enc === 'utf-16le' ||
enc === 'UTF-16LE' ||
enc.toLowerCase() === 'utf-16le'
) {
return 'utf16le'
}
} else if (length === 9) {
if (
enc === 'base64url' ||
enc === 'BASE64URL' ||
enc.toLowerCase() === 'base64url'
) {
return 'base64url'
}
}
/* c8 ignore stop */
return 'utf8'
}