codekingpro/portable-devtools
114k
1"use strict"
2
3var Buffer = require("safer-buffer").Buffer
4
5var bomHandling = require("./bom-handling")
6var mergeModules = require("./helpers/merge-exports")
7
8// All codecs and aliases are kept here, keyed by encoding name/alias.
9// They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
10// Cannot initialize with { __proto__: null } because Boolean({ __proto__: null }) === true
11module.exports.encodings = null
12
13// Characters emitted in case of error.
14module.exports.defaultCharUnicode = "�"
15module.exports.defaultCharSingleByte = "?"
16
17// Public API.
18module.exports.encode = function encode (str, encoding, options) {
19 str = "" + (str || "") // Ensure string.
20
21 var encoder = module.exports.getEncoder(encoding, options)
22
23 var res = encoder.write(str)
24 var trail = encoder.end()
25
26 return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res
27}
28
29module.exports.decode = function decode (buf, encoding, options) {
30 if (typeof buf === "string") {
31 if (!module.exports.skipDecodeWarning) {
32 console.error("Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding")
33 module.exports.skipDecodeWarning = true
34 }
35
36 buf = Buffer.from("" + (buf || ""), "binary") // Ensure buffer.
37 }
38
39 var decoder = module.exports.getDecoder(encoding, options)
40
41 var res = decoder.write(buf)
42 var trail = decoder.end()
43
44 return trail ? (res + trail) : res
45}
46
47module.exports.encodingExists = function encodingExists (enc) {
48 try {
49 module.exports.getCodec(enc)
50 return true
51 } catch (e) {
52 return false
53 }
54}
55
56// Legacy aliases to convert functions
57module.exports.toEncoding = module.exports.encode
58module.exports.fromEncoding = module.exports.decode
59
60// Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
61module.exports._codecDataCache = { __proto__: null }
62
63module.exports.getCodec = function getCodec (encoding) {
64 if (!module.exports.encodings) {
65 var raw = require("../encodings")
66 // TODO: In future versions when old nodejs support is removed can use object.assign
67 module.exports.encodings = { __proto__: null } // Initialize as empty object.
68 mergeModules(module.exports.encodings, raw)
69 }
70
71 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
72 var enc = module.exports._canonicalizeEncoding(encoding)
73
74 // Traverse iconv.encodings to find actual codec.
75 var codecOptions = {}
76 while (true) {
77 var codec = module.exports._codecDataCache[enc]
78
79 if (codec) { return codec }
80
81 var codecDef = module.exports.encodings[enc]
82
83 switch (typeof codecDef) {
84 case "string": // Direct alias to other encoding.
85 enc = codecDef
86 break
87
88 case "object": // Alias with options. Can be layered.
89 for (var key in codecDef) { codecOptions[key] = codecDef[key] }
90
91 if (!codecOptions.encodingName) { codecOptions.encodingName = enc }
92
93 enc = codecDef.type
94 break
95
96 case "function": // Codec itself.
97 if (!codecOptions.encodingName) { codecOptions.encodingName = enc }
98
99 // The codec function must load all tables and return object with .encoder and .decoder methods.
100 // It'll be called only once (for each different options object).
101 //
102 codec = new codecDef(codecOptions, module.exports)
103
104 module.exports._codecDataCache[codecOptions.encodingName] = codec // Save it to be reused later.
105 return codec
106
107 default:
108 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '" + enc + "')")
109 }
110 }
111}
112
113module.exports._canonicalizeEncoding = function (encoding) {
114 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
115 return ("" + encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, "")
116}
117
118module.exports.getEncoder = function getEncoder (encoding, options) {
119 var codec = module.exports.getCodec(encoding)
120 var encoder = new codec.encoder(options, codec)
121
122 if (codec.bomAware && options && options.addBOM) { encoder = new bomHandling.PrependBOM(encoder, options) }
123
124 return encoder
125}
126
127module.exports.getDecoder = function getDecoder (encoding, options) {
128 var codec = module.exports.getCodec(encoding)
129 var decoder = new codec.decoder(options, codec)
130
131 if (codec.bomAware && !(options && options.stripBOM === false)) { decoder = new bomHandling.StripBOM(decoder, options) }
132
133 return decoder
134}
135
136// Streaming API
137// NOTE: Streaming API naturally depends on 'stream' module from Node.js. Unfortunately in browser environments this module can add
138// up to 100Kb to the output bundle. To avoid unnecessary code bloat, we don't enable Streaming API in browser by default.
139// If you would like to enable it explicitly, please add the following code to your app:
140// > iconv.enableStreamingAPI(require('stream'));
141module.exports.enableStreamingAPI = function enableStreamingAPI (streamModule) {
142 if (module.exports.supportsStreams) { return }
143
144 // Dependency-inject stream module to create IconvLite stream classes.
145 var streams = require("./streams")(streamModule)
146
147 // Not public API yet, but expose the stream classes.
148 module.exports.IconvLiteEncoderStream = streams.IconvLiteEncoderStream
149 module.exports.IconvLiteDecoderStream = streams.IconvLiteDecoderStream
150
151 // Streaming API.
152 module.exports.encodeStream = function encodeStream (encoding, options) {
153 return new module.exports.IconvLiteEncoderStream(module.exports.getEncoder(encoding, options), options)
154 }
155
156 module.exports.decodeStream = function decodeStream (encoding, options) {
157 return new module.exports.IconvLiteDecoderStream(module.exports.getDecoder(encoding, options), options)
158 }
159
160 module.exports.supportsStreams = true
161}
162
163// Enable Streaming API automatically if 'stream' module is available and non-empty (the majority of environments).
164var streamModule
165try {
166 streamModule = require("stream")
167} catch (e) {}
168
169if (streamModule && streamModule.Transform) {
170 module.exports.enableStreamingAPI(streamModule)
171} else {
172 // In rare cases where 'stream' module is not available by default, throw a helpful exception.
173 module.exports.encodeStream = module.exports.decodeStream = function () {
174 throw new Error("iconv-lite Streaming API is not enabled. Use iconv.enableStreamingAPI(require('stream')); to enable it.")
175 }
176}
177
178// Some environments, such as browsers, may not load JavaScript files as UTF-8
179// eslint-disable-next-line no-constant-condition
180if ("Ā" !== "\u0100") {
181 console.error("iconv-lite warning: js files use non-utf8 encoding. See https://github.com/ashtuchkin/iconv-lite/wiki/Javascript-source-file-encodings for more info.")
182}
183 