Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getDecoder (encoding) {
if (!encoding) return null
try {
return iconv.getDecoder(encoding)
} catch (e) {
// error getting decoder
if (!ICONV_ENCODING_MESSAGE_REGEXP.test(e.message)) throw e
// the encoding was not found
throw createError(415, 'specified encoding unsupported', {
encoding: encoding,
type: 'encoding.unsupported'
})
}
}
function getDecoder (encoding) {
if (!encoding) return null
try {
return iconv.getDecoder(encoding)
} catch (e) {
// error getting decoder
if (!ICONV_ENCODING_MESSAGE_REGEXP.test(e.message)) throw e
// the encoding was not found
throw createError(415, 'specified encoding unsupported', {
encoding: encoding,
type: 'encoding.unsupported'
})
}
}
function getDecoder (encoding) {
if (!encoding) return null
try {
return iconv.getDecoder(encoding)
} catch (e) {
// error getting decoder
if (!iconvEncodingMessageRegExp.test(e.message)) throw e
// the encoding was not found
throw createError(415, 'specified encoding unsupported', 'encoding.unsupported', {
encoding: encoding
})
}
}
exports.decode = function(buffer, encoding, options) {
if (Buffer.isEncoding(encoding)) {
return buffer.toString(encoding);
}
const decoder = Iconv.getDecoder(encoding, options || {});
const res = decoder.write(buffer);
const trail = decoder.end();
return trail ? res + trail : res;
};