1.0.0 • Published 7 years ago
k-stream v1.0.0
Transform File With Stream
samples
basic
const stream = require('k-stream')
stream
// [ glob | Array | http[s] ] available
.read([
'./dev/*.html',
'http://cdn.com/sample.js'
// arg2 is globOption
], globOption)
// output directory [not file]
.write('./out')
// return promise
.then(() => {
console.log('finished!!')
})
change filename
const stream = require('k-stream')
stream
.read('./dev/SAMPLE.html')
// ./out/sample.out.html
.write('./out', (filename) => {
return filename.toLowerCase() + '.out'
})
transform
const stream = require('k-stream')
stream
.read('http://cdn.com/sample.js')
// transform pattern 1 [ arg: object ]
.transform({
// called each chunk [optional]
_transform: function(file, cb){
let path = file.path,
chunk = file.buffer.toString(),
upper = chunk.toUpperCase()
// push to stream
this.push(Buffer.from(upper))
// callback
cb()
},
// called finally [optional]
_flush: function(file, cb) {
let path = file.path,
content = file.buffer.toString(),
hyphened = file.split('_').join('-')
// push to stream
this.push(Buffer.from(hyphened))
// callback
cb()
}
})
// transform pattern 2 [ arg: function ]
.transform(function(file, cb) {
// flush
cb()
})
// do nothing without error
.transform(null)
.transform(undefined)
.transform("")
.write('./out')
bundle
const stream = require('k-stream')
stream
.read([
'./dev/*.html',
'http://cdn.com/sample.js'
])
.transform(...)
// bundle file
.bundle('./out/bundle.html', {
// files separator
sep: ';\n'
})
from Stream
const stream = require('k-stream'),
http = require('http')
http.get('http://cdn.com/sample.js', (res) => {
stream
// [ ReadableStream | Array ] available
.from([
fs.createReadStream('./dev/sample.html'),
res
])
.transform(...)
.bundle('./out/bundle.html')
})