How can I take a writable stream and return a readable stream from a buffer?
I have the following to write the data that comes from an ftp server to an array of chunks:
let chunks = []let writable = new Writablewritable._write = (chunk, encoding, callback) => { chunks.push(chunk) callback()}
I am then creating a new readstream:
let readable = new ReadStream()
I then tried to pipe the writable to the readable but that doesn't seem to work:
Argument of type 'ReadStream' is not assignable to parameter of type 'WritableStream'.
writable.pipe(readable)
Here is the entire method:
export class FTP { readStream(filePath, options = {}) { let conn = this.getConnection(this.name) if (!conn) return Buffer.from('') filePath = this.forceRoot(filePath) let chunks = [] let writable = new Writable writable._write = (chunk, encoding, callback) => { chunks.push(chunk) callback() } let readable = new ReadStream() conn.client.download(writable, filePath, options.start || undefined) writable.pipe(readable) return readable }}
I then read from the stream and pipe the output to the response object created from http.createServer()
like this:
let stream = store.readStream(file, { start, end }) .on('open', () => stream.pipe(res)) .on('close', () => res.end()) .on('error', err => res.end(err))