22 Star 218 Fork 230

记得要让着本宝宝/ckjcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.idea
_includes
_layouts
_posts
css
fonts
img
js
less
node_modules
.bin
abbrev
align-text
amdefine
ansi-regex
ansi-styles
argparse
asn1
assert-plus
async
aws-sign2
boom
browserify-zlib
buffer-from
camelcase
center-align
chalk
clean-css
cliui
coffee-script
colors
combined-stream
commander
concat-stream
LICENSE
index.js
package.json
readme.md
core-util-is
cryptiles
ctype
dateformat
debug
decamelize
deep-equal
defined
delayed-stream
escape-string-regexp
esprima
eventemitter2
exit
faye-websocket
figures
findup-sync
forever-agent
form-data
gaze
getobject
glob
globule
graceful-fs
grunt-contrib-less/node_modules
grunt-contrib-uglify/node_modules
grunt-contrib-watch/node_modules
grunt-legacy-log-utils
grunt-legacy-log
grunt-legacy-util
gzip-size
has-ansi
has-color
hawk
hoek
hooker
http-signature
iconv-lite
inherits
ip-regex
is-buffer
isarray
js-yaml
json-stringify-safe
jsonify
kind-of
lazy-cache
less
lodash
longest
lru-cache
maxmin
mime-types
mime
minimatch
minimist
mkdirp
natives
node-uuid
nopt
noptify
oauth-sign
object-assign
pako
pretty-bytes
process-nextick-args
psl
punycode
qs
readable-stream
repeat-string
request
right-align
rimraf
safe-buffer
sigmund
sntp
source-map
string_decoder
stringstream
strip-ansi
supports-color
tape
tiny-lr-fork
tough-cookie
tunnel-agent
typedarray
uglify-js
uglify-to-browserify
underscore.string
underscore
util-deprecate
which
window-size
wordwrap
yargs
zlib-browserify
pwa
.gitignore
404.html
ChengKeJ.github.io.iml
Graph Bed.md
Gruntfile.js
LICENSE
README.md
_config.yml
about.html
chengkej.io.iml
codecov.yml
feed.xml
index.html
offline.html
package-lock.json
package.json
sw.js
tags.html
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

concat-stream

Writable stream that concatenates all the data from a stream and calls a callback with the result. Use this when you want to collect all the data from a stream into a single buffer.

Build Status

NPM

description

Streams emit many buffers. If you want to collect all of the buffers, and when the stream ends concatenate all of the buffers together and receive a single buffer then this is the module for you.

Only use this if you know you can fit all of the output of your stream into a single Buffer (e.g. in RAM).

There are also objectMode streams that emit things other than Buffers, and you can concatenate these too. See below for details.

Related

concat-stream is part of the mississippi stream utility collection which includes more useful stream modules similar to this one.

examples

Buffers

var fs = require('fs')
var concat = require('concat-stream')

var readStream = fs.createReadStream('cat.png')
var concatStream = concat(gotPicture)

readStream.on('error', handleError)
readStream.pipe(concatStream)

function gotPicture(imageBuffer) {
  // imageBuffer is all of `cat.png` as a node.js Buffer
}

function handleError(err) {
  // handle your error appropriately here, e.g.:
  console.error(err) // print the error to STDERR
  process.exit(1) // exit program with non-zero exit code
}

Arrays

var write = concat(function(data) {})
write.write([1,2,3])
write.write([4,5,6])
write.end()
// data will be [1,2,3,4,5,6] in the above callback

Uint8Arrays

var write = concat(function(data) {})
var a = new Uint8Array(3)
a[0] = 97; a[1] = 98; a[2] = 99
write.write(a)
write.write('!')
write.end(Buffer.from('!!1'))

See test/ for more examples

methods

var concat = require('concat-stream')

var writable = concat(opts={}, cb)

Return a writable stream that will fire cb(data) with all of the data that was written to the stream. Data can be written to writable as strings, Buffers, arrays of byte integers, and Uint8Arrays.

By default concat-stream will give you back the same data type as the type of the first buffer written to the stream. Use opts.encoding to set what format data should be returned as, e.g. if you if you don't want to rely on the built-in type checking or for some other reason.

  • string - get a string
  • buffer - get back a Buffer
  • array - get an array of byte integers
  • uint8array, u8, uint8 - get back a Uint8Array
  • object, get back an array of Objects

If you don't specify an encoding, and the types can't be inferred (e.g. you write things that aren't in the list above), it will try to convert concat them into a Buffer.

If nothing is written to writable then data will be an empty array [].

error handling

concat-stream does not handle errors for you, so you must handle errors on whatever streams you pipe into concat-stream. This is a general rule when programming with node.js streams: always handle errors on each and every stream. Since concat-stream is not itself a stream it does not emit errors.

We recommend using end-of-stream or pump for writing error tolerant stream code.

license

MIT LICENSE

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ckjcode/ckjcode.git
git@gitee.com:ckjcode/ckjcode.git
ckjcode
ckjcode
ckjcode
master

搜索帮助