UNPKG

4.52 kBJavaScriptView Raw
1"use strict"
2
3var bomHandling = require('./bom-handling'),
4 iconv = module.exports;
5
6// All codecs and aliases are kept here, keyed by encoding name/alias.
7// They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
8iconv.encodings = null;
9
10// Characters emitted in case of error.
11iconv.defaultCharUnicode = '�';
12iconv.defaultCharSingleByte = '?';
13
14// Public API.
15iconv.encode = function encode(str, encoding, options) {
16 str = "" + (str || ""); // Ensure string.
17
18 var encoder = iconv.getEncoder(encoding, options);
19
20 var res = encoder.write(str);
21 var trail = encoder.end();
22
23 return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
24}
25
26iconv.decode = function decode(buf, encoding, options) {
27 if (typeof buf === 'string') {
28 if (!iconv.skipDecodeWarning) {
29 console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
30 iconv.skipDecodeWarning = true;
31 }
32
33 buf = new Buffer("" + (buf || ""), "binary"); // Ensure buffer.
34 }
35
36 var decoder = iconv.getDecoder(encoding, options);
37
38 var res = decoder.write(buf);
39 var trail = decoder.end();
40
41 return trail ? (res + trail) : res;
42}
43
44iconv.encodingExists = function encodingExists(enc) {
45 try {
46 iconv.getCodec(enc);
47 return true;
48 } catch (e) {
49 return false;
50 }
51}
52
53// Legacy aliases to convert functions
54iconv.toEncoding = iconv.encode;
55iconv.fromEncoding = iconv.decode;
56
57// Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
58iconv._codecDataCache = {};
59iconv.getCodec = function getCodec(encoding) {
60 if (!iconv.encodings)
61 iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
62
63 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
64 var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
65
66 // Traverse iconv.encodings to find actual codec.
67 var codecOptions = {};
68 while (true) {
69 var codec = iconv._codecDataCache[enc];
70 if (codec)
71 return codec;
72
73 var codecDef = iconv.encodings[enc];
74
75 switch (typeof codecDef) {
76 case "string": // Direct alias to other encoding.
77 enc = codecDef;
78 break;
79
80 case "object": // Alias with options. Can be layered.
81 for (var key in codecDef)
82 codecOptions[key] = codecDef[key];
83
84 if (!codecOptions.encodingName)
85 codecOptions.encodingName = enc;
86
87 enc = codecDef.type;
88 break;
89
90 case "function": // Codec itself.
91 if (!codecOptions.encodingName)
92 codecOptions.encodingName = enc;
93
94 // The codec function must load all tables and return object with .encoder and .decoder methods.
95 // It'll be called only once (for each different options object).
96 codec = new codecDef(codecOptions, iconv);
97
98 iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later.
99 return codec;
100
101 default:
102 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
103 }
104 }
105}
106
107iconv.getEncoder = function getEncoder(encoding, options) {
108 var codec = iconv.getCodec(encoding),
109 encoder = new codec.encoder(options, codec);
110
111 if (codec.bomAware && options && options.addBOM)
112 encoder = new bomHandling.PrependBOM(encoder, options);
113
114 return encoder;
115}
116
117iconv.getDecoder = function getDecoder(encoding, options) {
118 var codec = iconv.getCodec(encoding),
119 decoder = new codec.decoder(options, codec);
120
121 if (codec.bomAware && !(options && options.stripBOM === false))
122 decoder = new bomHandling.StripBOM(decoder, options);
123
124 return decoder;
125}
126
127
128// Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
129var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
130if (nodeVer) {
131
132 // Load streaming support in Node v0.10+
133 var nodeVerArr = nodeVer.split(".").map(Number);
134 if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
135 require("./streams")(iconv);
136 }
137
138 // Load Node primitive extensions.
139 require("./extend-node")(iconv);
140}
141