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