| "use strict"; |
|
|
| var Buffer = require("safer-buffer").Buffer; |
|
|
| var bomHandling = require("./bom-handling"), |
| iconv = module.exports; |
|
|
| |
| |
| iconv.encodings = null; |
|
|
| |
| iconv.defaultCharUnicode = '�'; |
| iconv.defaultCharSingleByte = '?'; |
|
|
| |
| iconv.encode = function encode(str, encoding, options) { |
| str = "" + (str || ""); |
|
|
| var encoder = iconv.getEncoder(encoding, options); |
|
|
| var res = encoder.write(str); |
| var trail = encoder.end(); |
| |
| return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res; |
| } |
|
|
| iconv.decode = function decode(buf, encoding, options) { |
| if (typeof buf === 'string') { |
| if (!iconv.skipDecodeWarning) { |
| console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding'); |
| iconv.skipDecodeWarning = true; |
| } |
|
|
| buf = Buffer.from("" + (buf || ""), "binary"); |
| } |
|
|
| var decoder = iconv.getDecoder(encoding, options); |
|
|
| var res = decoder.write(buf); |
| var trail = decoder.end(); |
|
|
| return trail ? (res + trail) : res; |
| } |
|
|
| iconv.encodingExists = function encodingExists(enc) { |
| try { |
| iconv.getCodec(enc); |
| return true; |
| } catch (e) { |
| return false; |
| } |
| } |
|
|
| |
| iconv.toEncoding = iconv.encode; |
| iconv.fromEncoding = iconv.decode; |
|
|
| |
| iconv._codecDataCache = {}; |
| iconv.getCodec = function getCodec(encoding) { |
| if (!iconv.encodings) |
| iconv.encodings = require("../encodings"); |
| |
| |
| var enc = iconv._canonicalizeEncoding(encoding); |
|
|
| |
| var codecOptions = {}; |
| while (true) { |
| var codec = iconv._codecDataCache[enc]; |
| if (codec) |
| return codec; |
|
|
| var codecDef = iconv.encodings[enc]; |
|
|
| switch (typeof codecDef) { |
| case "string": |
| enc = codecDef; |
| break; |
|
|
| case "object": |
| for (var key in codecDef) |
| codecOptions[key] = codecDef[key]; |
|
|
| if (!codecOptions.encodingName) |
| codecOptions.encodingName = enc; |
| |
| enc = codecDef.type; |
| break; |
|
|
| case "function": |
| if (!codecOptions.encodingName) |
| codecOptions.encodingName = enc; |
|
|
| |
| |
| codec = new codecDef(codecOptions, iconv); |
|
|
| iconv._codecDataCache[codecOptions.encodingName] = codec; |
| return codec; |
|
|
| default: |
| throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')"); |
| } |
| } |
| } |
|
|
| iconv._canonicalizeEncoding = function(encoding) { |
| |
| return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, ""); |
| } |
|
|
| iconv.getEncoder = function getEncoder(encoding, options) { |
| var codec = iconv.getCodec(encoding), |
| encoder = new codec.encoder(options, codec); |
|
|
| if (codec.bomAware && options && options.addBOM) |
| encoder = new bomHandling.PrependBOM(encoder, options); |
|
|
| return encoder; |
| } |
|
|
| iconv.getDecoder = function getDecoder(encoding, options) { |
| var codec = iconv.getCodec(encoding), |
| decoder = new codec.decoder(options, codec); |
|
|
| if (codec.bomAware && !(options && options.stripBOM === false)) |
| decoder = new bomHandling.StripBOM(decoder, options); |
|
|
| return decoder; |
| } |
|
|
| |
| |
| |
| |
| |
| iconv.enableStreamingAPI = function enableStreamingAPI(stream_module) { |
| if (iconv.supportsStreams) |
| return; |
|
|
| |
| var streams = require("./streams")(stream_module); |
|
|
| |
| iconv.IconvLiteEncoderStream = streams.IconvLiteEncoderStream; |
| iconv.IconvLiteDecoderStream = streams.IconvLiteDecoderStream; |
|
|
| |
| iconv.encodeStream = function encodeStream(encoding, options) { |
| return new iconv.IconvLiteEncoderStream(iconv.getEncoder(encoding, options), options); |
| } |
|
|
| iconv.decodeStream = function decodeStream(encoding, options) { |
| return new iconv.IconvLiteDecoderStream(iconv.getDecoder(encoding, options), options); |
| } |
|
|
| iconv.supportsStreams = true; |
| } |
|
|
| |
| var stream_module; |
| try { |
| stream_module = require("stream"); |
| } catch (e) {} |
|
|
| if (stream_module && stream_module.Transform) { |
| iconv.enableStreamingAPI(stream_module); |
|
|
| } else { |
| |
| iconv.encodeStream = iconv.decodeStream = function() { |
| throw new Error("iconv-lite Streaming API is not enabled. Use iconv.enableStreamingAPI(require('stream')); to enable it."); |
| }; |
| } |
|
|
| if ("Ā" != "\u0100") { |
| 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."); |
| } |
|
|