1 | 'use strict'
|
---|
2 |
|
---|
3 | // Node has always utf-8
|
---|
4 | const utf8Decoder = new TextDecoder('utf-8')
|
---|
5 | const textDecoders = new Map([
|
---|
6 | ['utf-8', utf8Decoder],
|
---|
7 | ['utf8', utf8Decoder]
|
---|
8 | ])
|
---|
9 |
|
---|
10 | function getDecoder (charset) {
|
---|
11 | let lc
|
---|
12 | while (true) {
|
---|
13 | switch (charset) {
|
---|
14 | case 'utf-8':
|
---|
15 | case 'utf8':
|
---|
16 | return decoders.utf8
|
---|
17 | case 'latin1':
|
---|
18 | case 'ascii': // TODO: Make these a separate, strict decoder?
|
---|
19 | case 'us-ascii':
|
---|
20 | case 'iso-8859-1':
|
---|
21 | case 'iso8859-1':
|
---|
22 | case 'iso88591':
|
---|
23 | case 'iso_8859-1':
|
---|
24 | case 'windows-1252':
|
---|
25 | case 'iso_8859-1:1987':
|
---|
26 | case 'cp1252':
|
---|
27 | case 'x-cp1252':
|
---|
28 | return decoders.latin1
|
---|
29 | case 'utf16le':
|
---|
30 | case 'utf-16le':
|
---|
31 | case 'ucs2':
|
---|
32 | case 'ucs-2':
|
---|
33 | return decoders.utf16le
|
---|
34 | case 'base64':
|
---|
35 | return decoders.base64
|
---|
36 | default:
|
---|
37 | if (lc === undefined) {
|
---|
38 | lc = true
|
---|
39 | charset = charset.toLowerCase()
|
---|
40 | continue
|
---|
41 | }
|
---|
42 | return decoders.other.bind(charset)
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | const decoders = {
|
---|
48 | utf8: (data, sourceEncoding) => {
|
---|
49 | if (data.length === 0) {
|
---|
50 | return ''
|
---|
51 | }
|
---|
52 | if (typeof data === 'string') {
|
---|
53 | data = Buffer.from(data, sourceEncoding)
|
---|
54 | }
|
---|
55 | return data.utf8Slice(0, data.length)
|
---|
56 | },
|
---|
57 |
|
---|
58 | latin1: (data, sourceEncoding) => {
|
---|
59 | if (data.length === 0) {
|
---|
60 | return ''
|
---|
61 | }
|
---|
62 | if (typeof data === 'string') {
|
---|
63 | return data
|
---|
64 | }
|
---|
65 | return data.latin1Slice(0, data.length)
|
---|
66 | },
|
---|
67 |
|
---|
68 | utf16le: (data, sourceEncoding) => {
|
---|
69 | if (data.length === 0) {
|
---|
70 | return ''
|
---|
71 | }
|
---|
72 | if (typeof data === 'string') {
|
---|
73 | data = Buffer.from(data, sourceEncoding)
|
---|
74 | }
|
---|
75 | return data.ucs2Slice(0, data.length)
|
---|
76 | },
|
---|
77 |
|
---|
78 | base64: (data, sourceEncoding) => {
|
---|
79 | if (data.length === 0) {
|
---|
80 | return ''
|
---|
81 | }
|
---|
82 | if (typeof data === 'string') {
|
---|
83 | data = Buffer.from(data, sourceEncoding)
|
---|
84 | }
|
---|
85 | return data.base64Slice(0, data.length)
|
---|
86 | },
|
---|
87 |
|
---|
88 | other: (data, sourceEncoding) => {
|
---|
89 | if (data.length === 0) {
|
---|
90 | return ''
|
---|
91 | }
|
---|
92 | if (typeof data === 'string') {
|
---|
93 | data = Buffer.from(data, sourceEncoding)
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (textDecoders.has(this.toString())) {
|
---|
97 | try {
|
---|
98 | return textDecoders.get(this).decode(data)
|
---|
99 | } catch (e) { }
|
---|
100 | }
|
---|
101 | return typeof data === 'string'
|
---|
102 | ? data
|
---|
103 | : data.toString()
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | function decodeText (text, sourceEncoding, destEncoding) {
|
---|
108 | if (text) {
|
---|
109 | return getDecoder(destEncoding)(text, sourceEncoding)
|
---|
110 | }
|
---|
111 | return text
|
---|
112 | }
|
---|
113 |
|
---|
114 | module.exports = decodeText
|
---|