koa
koa copied to clipboard
Add custom stream support
Description
There are situations when we need to send response stream that is not an instance of node Readable stream and current way of checking for stream value instanceof Stream fails.
e.g. when using alternative module like readable-stream or some other cases
My usecase
import archiver from 'archiver'
const archive = archiver('zip')
const stream = ...
archive.append(stream, { name: 'archive.zip'})
res.body = archive
Solution
- Added tests to reproduce issue with mocked custom stream object that represents possible
Readablestream implementation. - Addopted code from is-stream
Tested also with this code
const archiver = require('archiver')
const Stream = require('stream')
const CustomStream = require('./test-helpers/stream.js')
const isReadableStream = require('./lib/is-readable-stream.js')
const stream = new Stream()
const archive = archiver('zip')
const customstream = new CustomStream.Readable()
console.log(stream instanceof Stream) // true
console.log(customstream instanceof Stream) // false
console.log(archive instanceof Stream) // false
console.log(isReadableStream(stream)) // true
console.log(isReadableStream(customstream)) // true
console.log(isReadableStream(archive)) // true
why not use is-stream directly? because it is in es6?
why not use is-stream directly? because it is in es6?
I wasn't sure it is acceptable to introduce additional dependencies.
an external dependency is preferred if maintained from a reliable author, I can try to look later
@jonathanong I reviewed the library in question. Considering we're planning to move into the TS world, while I don't tend to prefer libraries that use d.ts instead of being natively written in TS, the way they have added a d.ts test file ensures that it isn't lying about its types. I'd move for inclusion if that's the preference.
that's a good point, thanks! cc @kevinpeno