react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / iconv-lite / lib / extend-node.js
81169 views"use strict"12// == Extend Node primitives to use iconv-lite =================================34module.exports = function (iconv) {5var original = undefined; // Place to keep original methods.67iconv.extendNodeEncodings = function extendNodeEncodings() {8if (original) return;9original = {};1011var nodeNativeEncodings = {12'hex': true, 'utf8': true, 'utf-8': true, 'ascii': true, 'binary': true,13'base64': true, 'ucs2': true, 'ucs-2': true, 'utf16le': true, 'utf-16le': true,14};1516Buffer.isNativeEncoding = function(enc) {17return enc && nodeNativeEncodings[enc.toLowerCase()];18}1920// -- SlowBuffer -----------------------------------------------------------21var SlowBuffer = require('buffer').SlowBuffer;2223original.SlowBufferToString = SlowBuffer.prototype.toString;24SlowBuffer.prototype.toString = function(encoding, start, end) {25encoding = String(encoding || 'utf8').toLowerCase();2627// Use native conversion when possible28if (Buffer.isNativeEncoding(encoding))29return original.SlowBufferToString.call(this, encoding, start, end);3031// Otherwise, use our decoding method.32if (typeof start == 'undefined') start = 0;33if (typeof end == 'undefined') end = this.length;34return iconv.decode(this.slice(start, end), encoding);35}3637original.SlowBufferWrite = SlowBuffer.prototype.write;38SlowBuffer.prototype.write = function(string, offset, length, encoding) {39// Support both (string, offset, length, encoding)40// and the legacy (string, encoding, offset, length)41if (isFinite(offset)) {42if (!isFinite(length)) {43encoding = length;44length = undefined;45}46} else { // legacy47var swap = encoding;48encoding = offset;49offset = length;50length = swap;51}5253offset = +offset || 0;54var remaining = this.length - offset;55if (!length) {56length = remaining;57} else {58length = +length;59if (length > remaining) {60length = remaining;61}62}63encoding = String(encoding || 'utf8').toLowerCase();6465// Use native conversion when possible66if (Buffer.isNativeEncoding(encoding))67return original.SlowBufferWrite.call(this, string, offset, length, encoding);6869if (string.length > 0 && (length < 0 || offset < 0))70throw new RangeError('attempt to write beyond buffer bounds');7172// Otherwise, use our encoding method.73var buf = iconv.encode(string, encoding);74if (buf.length < length) length = buf.length;75buf.copy(this, offset, 0, length);76return length;77}7879// -- Buffer ---------------------------------------------------------------8081original.BufferIsEncoding = Buffer.isEncoding;82Buffer.isEncoding = function(encoding) {83return Buffer.isNativeEncoding(encoding) || iconv.encodingExists(encoding);84}8586original.BufferByteLength = Buffer.byteLength;87Buffer.byteLength = SlowBuffer.byteLength = function(str, encoding) {88encoding = String(encoding || 'utf8').toLowerCase();8990// Use native conversion when possible91if (Buffer.isNativeEncoding(encoding))92return original.BufferByteLength.call(this, str, encoding);9394// Slow, I know, but we don't have a better way yet.95return iconv.encode(str, encoding).length;96}9798original.BufferToString = Buffer.prototype.toString;99Buffer.prototype.toString = function(encoding, start, end) {100encoding = String(encoding || 'utf8').toLowerCase();101102// Use native conversion when possible103if (Buffer.isNativeEncoding(encoding))104return original.BufferToString.call(this, encoding, start, end);105106// Otherwise, use our decoding method.107if (typeof start == 'undefined') start = 0;108if (typeof end == 'undefined') end = this.length;109return iconv.decode(this.slice(start, end), encoding);110}111112original.BufferWrite = Buffer.prototype.write;113Buffer.prototype.write = function(string, offset, length, encoding) {114var _offset = offset, _length = length, _encoding = encoding;115// Support both (string, offset, length, encoding)116// and the legacy (string, encoding, offset, length)117if (isFinite(offset)) {118if (!isFinite(length)) {119encoding = length;120length = undefined;121}122} else { // legacy123var swap = encoding;124encoding = offset;125offset = length;126length = swap;127}128129encoding = String(encoding || 'utf8').toLowerCase();130131// Use native conversion when possible132if (Buffer.isNativeEncoding(encoding))133return original.BufferWrite.call(this, string, _offset, _length, _encoding);134135offset = +offset || 0;136var remaining = this.length - offset;137if (!length) {138length = remaining;139} else {140length = +length;141if (length > remaining) {142length = remaining;143}144}145146if (string.length > 0 && (length < 0 || offset < 0))147throw new RangeError('attempt to write beyond buffer bounds');148149// Otherwise, use our encoding method.150var buf = iconv.encode(string, encoding);151if (buf.length < length) length = buf.length;152buf.copy(this, offset, 0, length);153return length;154155// TODO: Set _charsWritten.156}157158159// -- Readable -------------------------------------------------------------160if (iconv.supportsStreams) {161var Readable = require('stream').Readable;162163original.ReadableSetEncoding = Readable.prototype.setEncoding;164Readable.prototype.setEncoding = function setEncoding(enc, options) {165// Use our own decoder, it has the same interface.166// We cannot use original function as it doesn't handle BOM-s.167this._readableState.decoder = iconv.getDecoder(enc, options);168this._readableState.encoding = enc;169}170171Readable.prototype.collect = iconv._collect;172}173}174175// Remove iconv-lite Node primitive extensions.176iconv.undoExtendNodeEncodings = function undoExtendNodeEncodings() {177if (!original)178throw new Error("require('iconv-lite').undoExtendNodeEncodings(): Nothing to undo; extendNodeEncodings() is not called.")179180delete Buffer.isNativeEncoding;181182var SlowBuffer = require('buffer').SlowBuffer;183184SlowBuffer.prototype.toString = original.SlowBufferToString;185SlowBuffer.prototype.write = original.SlowBufferWrite;186187Buffer.isEncoding = original.BufferIsEncoding;188Buffer.byteLength = original.BufferByteLength;189Buffer.prototype.toString = original.BufferToString;190Buffer.prototype.write = original.BufferWrite;191192if (iconv.supportsStreams) {193var Readable = require('stream').Readable;194195Readable.prototype.setEncoding = original.ReadableSetEncoding;196delete Readable.prototype.collect;197}198199original = undefined;200}201}202203204