I'm still trying to grok my way through streams
in general. I have been able to stream a large file using multiparty from within form.on('part')
. But I need to defer the invocation and resolve the stream before it's read. I have tried PassThrough
, through
. through2
, but have gotten different results, which it mainly hangs, and I can't figure out what to do, nor steps to debug. I'm open to all alternatives. Thanks for all insights.
import multiparty from 'multiparty'
import {
PassThrough
} from 'stream';
import through from 'through'
import through2 from 'through2'
export function promisedMultiparty(req) {
return new Promise((resolve, reject) => {
const form = new multiparty.Form()
const form_files = []
let q_str = ''
form.on('field', (fieldname, value) => {
if (value) q_str = appendQStr(fieldname, value, q_str)
})
form.on('part', async (part) => {
if (part.filename) {
const pass1 = new PassThrough() // this hangs at 10%
const pass2 = through(function write(data) { // this hangs from the beginning
this.queue(data)
},
function end() {
this.queue(null)
})
const pass3 = through2() // this hangs at 10%
/*
// This way works for large files, but I want to defer
// invocation
const form_data = new FormData()
form_data.append(savepath, part, {
filename,
})
const r = request.post(url, {
headers: {
'transfer-encoding': 'chunked'
}
}, responseCallback(resolve))
r._form = form
*/
form_files.push({
part: part.pipe(pass1),
// part: part.pipe(pass2),
// part: part.pipe(pass3),
})
} else {
part.resume()
}
})
form.on('close', () => {
resolve({
fields: qs.parse(q_str),
forms: form_files,
})
})
form.parse(req)
})
}
p.s. For sure the title could be better, if someone could use the proper terms please. Thanks.
PassThrough
andthrough2
works with smaller files. DoesPassThrough
andthrough2
behave the same way (hangs at 10%) because they are based onStream2
? – Cyclamenform.on('part')
, I want to resolve with the unread stream, and in the function where the promise resolves, start streaming with additional context. – Cyclamenmultiparty
would be great otherwise. – Cyclamen