extend-node.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. var Buffer = require("buffer").Buffer;
  3. // Note: not polyfilled with safer-buffer on a purpose, as overrides Buffer
  4. // == Extend Node primitives to use iconv-lite =================================
  5. module.exports = function (iconv) {
  6. var original = undefined; // Place to keep original methods.
  7. // Node authors rewrote Buffer internals to make it compatible with
  8. // Uint8Array and we cannot patch key functions since then.
  9. // Note: this does use older Buffer API on a purpose
  10. iconv.supportsNodeEncodingsExtension = !(Buffer.from || new Buffer(0) instanceof Uint8Array);
  11. iconv.extendNodeEncodings = function extendNodeEncodings() {
  12. if (original) return;
  13. original = {};
  14. if (!iconv.supportsNodeEncodingsExtension) {
  15. console.error("ACTION NEEDED: require('iconv-lite').extendNodeEncodings() is not supported in your version of Node");
  16. console.error("See more info at https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility");
  17. return;
  18. }
  19. var nodeNativeEncodings = {
  20. 'hex': true, 'utf8': true, 'utf-8': true, 'ascii': true, 'binary': true,
  21. 'base64': true, 'ucs2': true, 'ucs-2': true, 'utf16le': true, 'utf-16le': true,
  22. };
  23. Buffer.isNativeEncoding = function(enc) {
  24. return enc && nodeNativeEncodings[enc.toLowerCase()];
  25. }
  26. // -- SlowBuffer -----------------------------------------------------------
  27. var SlowBuffer = require('buffer').SlowBuffer;
  28. original.SlowBufferToString = SlowBuffer.prototype.toString;
  29. SlowBuffer.prototype.toString = function(encoding, start, end) {
  30. encoding = String(encoding || 'utf8').toLowerCase();
  31. // Use native conversion when possible
  32. if (Buffer.isNativeEncoding(encoding))
  33. return original.SlowBufferToString.call(this, encoding, start, end);
  34. // Otherwise, use our decoding method.
  35. if (typeof start == 'undefined') start = 0;
  36. if (typeof end == 'undefined') end = this.length;
  37. return iconv.decode(this.slice(start, end), encoding);
  38. }
  39. original.SlowBufferWrite = SlowBuffer.prototype.write;
  40. SlowBuffer.prototype.write = function(string, offset, length, encoding) {
  41. // Support both (string, offset, length, encoding)
  42. // and the legacy (string, encoding, offset, length)
  43. if (isFinite(offset)) {
  44. if (!isFinite(length)) {
  45. encoding = length;
  46. length = undefined;
  47. }
  48. } else { // legacy
  49. var swap = encoding;
  50. encoding = offset;
  51. offset = length;
  52. length = swap;
  53. }
  54. offset = +offset || 0;
  55. var remaining = this.length - offset;
  56. if (!length) {
  57. length = remaining;
  58. } else {
  59. length = +length;
  60. if (length > remaining) {
  61. length = remaining;
  62. }
  63. }
  64. encoding = String(encoding || 'utf8').toLowerCase();
  65. // Use native conversion when possible
  66. if (Buffer.isNativeEncoding(encoding))
  67. return original.SlowBufferWrite.call(this, string, offset, length, encoding);
  68. if (string.length > 0 && (length < 0 || offset < 0))
  69. throw new RangeError('attempt to write beyond buffer bounds');
  70. // Otherwise, use our encoding method.
  71. var buf = iconv.encode(string, encoding);
  72. if (buf.length < length) length = buf.length;
  73. buf.copy(this, offset, 0, length);
  74. return length;
  75. }
  76. // -- Buffer ---------------------------------------------------------------
  77. original.BufferIsEncoding = Buffer.isEncoding;
  78. Buffer.isEncoding = function(encoding) {
  79. return Buffer.isNativeEncoding(encoding) || iconv.encodingExists(encoding);
  80. }
  81. original.BufferByteLength = Buffer.byteLength;
  82. Buffer.byteLength = SlowBuffer.byteLength = function(str, encoding) {
  83. encoding = String(encoding || 'utf8').toLowerCase();
  84. // Use native conversion when possible
  85. if (Buffer.isNativeEncoding(encoding))
  86. return original.BufferByteLength.call(this, str, encoding);
  87. // Slow, I know, but we don't have a better way yet.
  88. return iconv.encode(str, encoding).length;
  89. }
  90. original.BufferToString = Buffer.prototype.toString;
  91. Buffer.prototype.toString = function(encoding, start, end) {
  92. encoding = String(encoding || 'utf8').toLowerCase();
  93. // Use native conversion when possible
  94. if (Buffer.isNativeEncoding(encoding))
  95. return original.BufferToString.call(this, encoding, start, end);
  96. // Otherwise, use our decoding method.
  97. if (typeof start == 'undefined') start = 0;
  98. if (typeof end == 'undefined') end = this.length;
  99. return iconv.decode(this.slice(start, end), encoding);
  100. }
  101. original.BufferWrite = Buffer.prototype.write;
  102. Buffer.prototype.write = function(string, offset, length, encoding) {
  103. var _offset = offset, _length = length, _encoding = encoding;
  104. // Support both (string, offset, length, encoding)
  105. // and the legacy (string, encoding, offset, length)
  106. if (isFinite(offset)) {
  107. if (!isFinite(length)) {
  108. encoding = length;
  109. length = undefined;
  110. }
  111. } else { // legacy
  112. var swap = encoding;
  113. encoding = offset;
  114. offset = length;
  115. length = swap;
  116. }
  117. encoding = String(encoding || 'utf8').toLowerCase();
  118. // Use native conversion when possible
  119. if (Buffer.isNativeEncoding(encoding))
  120. return original.BufferWrite.call(this, string, _offset, _length, _encoding);
  121. offset = +offset || 0;
  122. var remaining = this.length - offset;
  123. if (!length) {
  124. length = remaining;
  125. } else {
  126. length = +length;
  127. if (length > remaining) {
  128. length = remaining;
  129. }
  130. }
  131. if (string.length > 0 && (length < 0 || offset < 0))
  132. throw new RangeError('attempt to write beyond buffer bounds');
  133. // Otherwise, use our encoding method.
  134. var buf = iconv.encode(string, encoding);
  135. if (buf.length < length) length = buf.length;
  136. buf.copy(this, offset, 0, length);
  137. return length;
  138. // TODO: Set _charsWritten.
  139. }
  140. // -- Readable -------------------------------------------------------------
  141. if (iconv.supportsStreams) {
  142. var Readable = require('stream').Readable;
  143. original.ReadableSetEncoding = Readable.prototype.setEncoding;
  144. Readable.prototype.setEncoding = function setEncoding(enc, options) {
  145. // Use our own decoder, it has the same interface.
  146. // We cannot use original function as it doesn't handle BOM-s.
  147. this._readableState.decoder = iconv.getDecoder(enc, options);
  148. this._readableState.encoding = enc;
  149. }
  150. Readable.prototype.collect = iconv._collect;
  151. }
  152. }
  153. // Remove iconv-lite Node primitive extensions.
  154. iconv.undoExtendNodeEncodings = function undoExtendNodeEncodings() {
  155. if (!iconv.supportsNodeEncodingsExtension)
  156. return;
  157. if (!original)
  158. throw new Error("require('iconv-lite').undoExtendNodeEncodings(): Nothing to undo; extendNodeEncodings() is not called.")
  159. delete Buffer.isNativeEncoding;
  160. var SlowBuffer = require('buffer').SlowBuffer;
  161. SlowBuffer.prototype.toString = original.SlowBufferToString;
  162. SlowBuffer.prototype.write = original.SlowBufferWrite;
  163. Buffer.isEncoding = original.BufferIsEncoding;
  164. Buffer.byteLength = original.BufferByteLength;
  165. Buffer.prototype.toString = original.BufferToString;
  166. Buffer.prototype.write = original.BufferWrite;
  167. if (iconv.supportsStreams) {
  168. var Readable = require('stream').Readable;
  169. Readable.prototype.setEncoding = original.ReadableSetEncoding;
  170. delete Readable.prototype.collect;
  171. }
  172. original = undefined;
  173. }
  174. }