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