Ei kuvausta

index.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var Writable = require('readable-stream').Writable
  2. var inherits = require('inherits')
  3. if (typeof Uint8Array === 'undefined') {
  4. var U8 = require('typedarray').Uint8Array
  5. } else {
  6. var U8 = Uint8Array
  7. }
  8. function ConcatStream(opts, cb) {
  9. if (!(this instanceof ConcatStream)) return new ConcatStream(opts, cb)
  10. if (typeof opts === 'function') {
  11. cb = opts
  12. opts = {}
  13. }
  14. if (!opts) opts = {}
  15. var encoding = opts.encoding
  16. var shouldInferEncoding = false
  17. if (!encoding) {
  18. shouldInferEncoding = true
  19. } else {
  20. encoding = String(encoding).toLowerCase()
  21. if (encoding === 'u8' || encoding === 'uint8') {
  22. encoding = 'uint8array'
  23. }
  24. }
  25. Writable.call(this, { objectMode: true })
  26. this.encoding = encoding
  27. this.shouldInferEncoding = shouldInferEncoding
  28. if (cb) this.on('finish', function () { cb(this.getBody()) })
  29. this.body = []
  30. }
  31. module.exports = ConcatStream
  32. inherits(ConcatStream, Writable)
  33. ConcatStream.prototype._write = function(chunk, enc, next) {
  34. this.body.push(chunk)
  35. next()
  36. }
  37. ConcatStream.prototype.inferEncoding = function (buff) {
  38. var firstBuffer = buff === undefined ? this.body[0] : buff;
  39. if (Buffer.isBuffer(firstBuffer)) return 'buffer'
  40. if (typeof Uint8Array !== 'undefined' && firstBuffer instanceof Uint8Array) return 'uint8array'
  41. if (Array.isArray(firstBuffer)) return 'array'
  42. if (typeof firstBuffer === 'string') return 'string'
  43. if (Object.prototype.toString.call(firstBuffer) === "[object Object]") return 'object'
  44. return 'buffer'
  45. }
  46. ConcatStream.prototype.getBody = function () {
  47. if (!this.encoding && this.body.length === 0) return []
  48. if (this.shouldInferEncoding) this.encoding = this.inferEncoding()
  49. if (this.encoding === 'array') return arrayConcat(this.body)
  50. if (this.encoding === 'string') return stringConcat(this.body)
  51. if (this.encoding === 'buffer') return bufferConcat(this.body)
  52. if (this.encoding === 'uint8array') return u8Concat(this.body)
  53. return this.body
  54. }
  55. var isArray = Array.isArray || function (arr) {
  56. return Object.prototype.toString.call(arr) == '[object Array]'
  57. }
  58. function isArrayish (arr) {
  59. return /Array\]$/.test(Object.prototype.toString.call(arr))
  60. }
  61. function isBufferish (p) {
  62. return typeof p === 'string' || isArrayish(p) || (p && typeof p.subarray === 'function')
  63. }
  64. function stringConcat (parts) {
  65. var strings = []
  66. var needsToString = false
  67. for (var i = 0; i < parts.length; i++) {
  68. var p = parts[i]
  69. if (typeof p === 'string') {
  70. strings.push(p)
  71. } else if (Buffer.isBuffer(p)) {
  72. strings.push(p)
  73. } else if (isBufferish(p)) {
  74. strings.push(new Buffer(p))
  75. } else {
  76. strings.push(new Buffer(String(p)))
  77. }
  78. }
  79. if (Buffer.isBuffer(parts[0])) {
  80. strings = Buffer.concat(strings)
  81. strings = strings.toString('utf8')
  82. } else {
  83. strings = strings.join('')
  84. }
  85. return strings
  86. }
  87. function bufferConcat (parts) {
  88. var bufs = []
  89. for (var i = 0; i < parts.length; i++) {
  90. var p = parts[i]
  91. if (Buffer.isBuffer(p)) {
  92. bufs.push(p)
  93. } else if (isBufferish(p)) {
  94. bufs.push(new Buffer(p))
  95. } else {
  96. bufs.push(new Buffer(String(p)))
  97. }
  98. }
  99. return Buffer.concat(bufs)
  100. }
  101. function arrayConcat (parts) {
  102. var res = []
  103. for (var i = 0; i < parts.length; i++) {
  104. res.push.apply(res, parts[i])
  105. }
  106. return res
  107. }
  108. function u8Concat (parts) {
  109. var len = 0
  110. for (var i = 0; i < parts.length; i++) {
  111. if (typeof parts[i] === 'string') {
  112. parts[i] = new Buffer(parts[i])
  113. }
  114. len += parts[i].length
  115. }
  116. var u8 = new U8(len)
  117. for (var i = 0, offset = 0; i < parts.length; i++) {
  118. var part = parts[i]
  119. for (var j = 0; j < part.length; j++) {
  120. u8[offset++] = part[j]
  121. }
  122. }
  123. return u8
  124. }