koa icon indicating copy to clipboard operation
koa copied to clipboard

Add custom stream support

Open KristapsR opened this issue 1 year ago • 3 comments

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 Readable stream 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

KristapsR avatar Jun 21 '24 14:06 KristapsR

why not use is-stream directly? because it is in es6?

jonathanong avatar Aug 18 '24 20:08 jonathanong

why not use is-stream directly? because it is in es6?

I wasn't sure it is acceptable to introduce additional dependencies.

KristapsR avatar Aug 19 '24 16:08 KristapsR

an external dependency is preferred if maintained from a reliable author, I can try to look later

jonathanong avatar Aug 31 '24 04:08 jonathanong

@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.

kevinpeno avatar Oct 22 '24 18:10 kevinpeno

that's a good point, thanks! cc @kevinpeno

jonathanong avatar Oct 28 '24 02:10 jonathanong