如何推迟流读取调用

gar*_*ajo 10 javascript through2 node-streams multiparty

streams一般来说,我仍然试图改变自己的方式.我已经能够从内部使用multiparty流式传输大型文件form.on('part').但我需要推迟调用并在读取之前解析流.我曾尝试PassThrough,through.through2,但已经得到了不同的结果,它主要挂起,我无法弄清楚该怎么做,也没有步骤调试.我对所有选择都持开放态度.感谢您的所有见解.

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)
  })
}
Run Code Online (Sandbox Code Playgroud)

ps如果有人可以使用正确的条款,请确保标题可能更好.谢谢.

Gab*_*oia 1

我相信这是因为您没有through2正确使用 - 即一旦缓冲区已满,实际上并没有清空缓冲区(这就是为什么它在较大文件上挂在 10% 处,但适用于较小文件)。

我相信这样的实现应该可以做到:

const pass2 = through2(function(chunk, encoding, next) {

   // do something with the data


   // Use this only if you want to send the data further to another stream reader 
   // Note - From your implementation you don't seem to need it
   // this.push(data)

   // This is what tells through2 it's ready to empty the 
   //  buffer and read more data
   next();
})
Run Code Online (Sandbox Code Playgroud)