|
"use strict"; |
|
var Buffer = require("buffer").Buffer; |
|
|
|
|
|
|
|
|
|
module.exports = function (iconv) { |
|
var original = undefined; |
|
|
|
|
|
|
|
|
|
iconv.supportsNodeEncodingsExtension = !(Buffer.from || new Buffer(0) instanceof Uint8Array); |
|
|
|
iconv.extendNodeEncodings = function extendNodeEncodings() { |
|
if (original) return; |
|
original = {}; |
|
|
|
if (!iconv.supportsNodeEncodingsExtension) { |
|
console.error("ACTION NEEDED: require('iconv-lite').extendNodeEncodings() is not supported in your version of Node"); |
|
console.error("See more info at https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility"); |
|
return; |
|
} |
|
|
|
var nodeNativeEncodings = { |
|
'hex': true, 'utf8': true, 'utf-8': true, 'ascii': true, 'binary': true, |
|
'base64': true, 'ucs2': true, 'ucs-2': true, 'utf16le': true, 'utf-16le': true, |
|
}; |
|
|
|
Buffer.isNativeEncoding = function(enc) { |
|
return enc && nodeNativeEncodings[enc.toLowerCase()]; |
|
} |
|
|
|
|
|
var SlowBuffer = require('buffer').SlowBuffer; |
|
|
|
original.SlowBufferToString = SlowBuffer.prototype.toString; |
|
SlowBuffer.prototype.toString = function(encoding, start, end) { |
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
if (Buffer.isNativeEncoding(encoding)) |
|
return original.SlowBufferToString.call(this, encoding, start, end); |
|
|
|
|
|
if (typeof start == 'undefined') start = 0; |
|
if (typeof end == 'undefined') end = this.length; |
|
return iconv.decode(this.slice(start, end), encoding); |
|
} |
|
|
|
original.SlowBufferWrite = SlowBuffer.prototype.write; |
|
SlowBuffer.prototype.write = function(string, offset, length, encoding) { |
|
|
|
|
|
if (isFinite(offset)) { |
|
if (!isFinite(length)) { |
|
encoding = length; |
|
length = undefined; |
|
} |
|
} else { |
|
var swap = encoding; |
|
encoding = offset; |
|
offset = length; |
|
length = swap; |
|
} |
|
|
|
offset = +offset || 0; |
|
var remaining = this.length - offset; |
|
if (!length) { |
|
length = remaining; |
|
} else { |
|
length = +length; |
|
if (length > remaining) { |
|
length = remaining; |
|
} |
|
} |
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
if (Buffer.isNativeEncoding(encoding)) |
|
return original.SlowBufferWrite.call(this, string, offset, length, encoding); |
|
|
|
if (string.length > 0 && (length < 0 || offset < 0)) |
|
throw new RangeError('attempt to write beyond buffer bounds'); |
|
|
|
|
|
var buf = iconv.encode(string, encoding); |
|
if (buf.length < length) length = buf.length; |
|
buf.copy(this, offset, 0, length); |
|
return length; |
|
} |
|
|
|
|
|
|
|
original.BufferIsEncoding = Buffer.isEncoding; |
|
Buffer.isEncoding = function(encoding) { |
|
return Buffer.isNativeEncoding(encoding) || iconv.encodingExists(encoding); |
|
} |
|
|
|
original.BufferByteLength = Buffer.byteLength; |
|
Buffer.byteLength = SlowBuffer.byteLength = function(str, encoding) { |
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
if (Buffer.isNativeEncoding(encoding)) |
|
return original.BufferByteLength.call(this, str, encoding); |
|
|
|
|
|
return iconv.encode(str, encoding).length; |
|
} |
|
|
|
original.BufferToString = Buffer.prototype.toString; |
|
Buffer.prototype.toString = function(encoding, start, end) { |
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
if (Buffer.isNativeEncoding(encoding)) |
|
return original.BufferToString.call(this, encoding, start, end); |
|
|
|
|
|
if (typeof start == 'undefined') start = 0; |
|
if (typeof end == 'undefined') end = this.length; |
|
return iconv.decode(this.slice(start, end), encoding); |
|
} |
|
|
|
original.BufferWrite = Buffer.prototype.write; |
|
Buffer.prototype.write = function(string, offset, length, encoding) { |
|
var _offset = offset, _length = length, _encoding = encoding; |
|
|
|
|
|
if (isFinite(offset)) { |
|
if (!isFinite(length)) { |
|
encoding = length; |
|
length = undefined; |
|
} |
|
} else { |
|
var swap = encoding; |
|
encoding = offset; |
|
offset = length; |
|
length = swap; |
|
} |
|
|
|
encoding = String(encoding || 'utf8').toLowerCase(); |
|
|
|
|
|
if (Buffer.isNativeEncoding(encoding)) |
|
return original.BufferWrite.call(this, string, _offset, _length, _encoding); |
|
|
|
offset = +offset || 0; |
|
var remaining = this.length - offset; |
|
if (!length) { |
|
length = remaining; |
|
} else { |
|
length = +length; |
|
if (length > remaining) { |
|
length = remaining; |
|
} |
|
} |
|
|
|
if (string.length > 0 && (length < 0 || offset < 0)) |
|
throw new RangeError('attempt to write beyond buffer bounds'); |
|
|
|
|
|
var buf = iconv.encode(string, encoding); |
|
if (buf.length < length) length = buf.length; |
|
buf.copy(this, offset, 0, length); |
|
return length; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (iconv.supportsStreams) { |
|
var Readable = require('stream').Readable; |
|
|
|
original.ReadableSetEncoding = Readable.prototype.setEncoding; |
|
Readable.prototype.setEncoding = function setEncoding(enc, options) { |
|
|
|
|
|
this._readableState.decoder = iconv.getDecoder(enc, options); |
|
this._readableState.encoding = enc; |
|
} |
|
|
|
Readable.prototype.collect = iconv._collect; |
|
} |
|
} |
|
|
|
|
|
iconv.undoExtendNodeEncodings = function undoExtendNodeEncodings() { |
|
if (!iconv.supportsNodeEncodingsExtension) |
|
return; |
|
if (!original) |
|
throw new Error("require('iconv-lite').undoExtendNodeEncodings(): Nothing to undo; extendNodeEncodings() is not called.") |
|
|
|
delete Buffer.isNativeEncoding; |
|
|
|
var SlowBuffer = require('buffer').SlowBuffer; |
|
|
|
SlowBuffer.prototype.toString = original.SlowBufferToString; |
|
SlowBuffer.prototype.write = original.SlowBufferWrite; |
|
|
|
Buffer.isEncoding = original.BufferIsEncoding; |
|
Buffer.byteLength = original.BufferByteLength; |
|
Buffer.prototype.toString = original.BufferToString; |
|
Buffer.prototype.write = original.BufferWrite; |
|
|
|
if (iconv.supportsStreams) { |
|
var Readable = require('stream').Readable; |
|
|
|
Readable.prototype.setEncoding = original.ReadableSetEncoding; |
|
delete Readable.prototype.collect; |
|
} |
|
|
|
original = undefined; |
|
} |
|
} |
|
|