在上传文件或在网络上写入文件之前查找流的内容长度

tim*_*tim 1 javascript pipeline stream node.js needle.js

我正在读取文件,对其进行压缩和加密,然后在网络上上传/写入。但我需要知道最终流的内容长度(通过读取、压缩、加密后返回的流)才能发出发布请求。

let zlib = zlib.createGzip(),
   encrypt = crypto.cipherIV(....),
    input = fs.createReadStream('file.jpg');
function zipAndEncrypt(){
   let stream = readStream.pipe( zlib).pipe( encrypt );
   let options = {
     "stream_length":0,
     headers: { "content-type": 'image/jpeg',
                "content-length": '123456', // need to get this length 
          .....
     }
    }

// post the stream
needle( 'post', url, stream, options )
   .then( resp => { console.log( "file length", resp.body.length);})
   .catch( err => {})
}
Run Code Online (Sandbox Code Playgroud)

如果我在标题中输入正确的内容长度(在这种情况下我知道长度),上面的代码就可以工作。所以我需要找到流的长度。

到目前为止,我通过以下方式达到了长度:

  let chunk = [], conLength;
  stream.on( 'data', ( data ) => {
            chunk.push( data );
        } )
        .on( 'end', () => {
         conLength = Buffer.concat( chunk ).length; 
        } );
Run Code Online (Sandbox Code Playgroud)

但是post请求失败,SOCKET挂起错误。

看起来流已被耗尽或消耗,因为在使用上面的代码找到长度后它不会发出“数据”事件。

尝试了stream.resume()。但没有任何作用。您能否建议如何在不消耗流的情况下找到流的长度。

Mar*_*nde 5

如果您需要发送内容长度,了解它的唯一方法是在文件被压缩和加密之后。

因此,您的解决方案有效,但前提是您发送缓冲区而不是流,因为您已经消耗了流中的所有数据。既然你已经在内存中拥有了所有的块,你不妨发送它。

let chunk = [];

stream.on('data', data => chunk.push(data))
.on('end', () => {
    const buffer = Buffer.concat(chunk);
    const conLength = buffer.length;
    // Execute the request here, sending the whole buffer, not the stream
    needle(/*...*/)
});
Run Code Online (Sandbox Code Playgroud)

但是,如果您的文件太大,则需要对其进行流式传输,否则您将耗尽内存,一个简单的解决方法(只需一点开销)是将其通过管道传输到临时文件,然后发送该文件。这样您就可以在执行请求、访问属性stream.bytesWritten或使用fs.lstat.

function zipAndEncrypt(input) {
    const gzip = zlib.createGzip();
    const encrypt = crypto.createCipheriv(algo, key, iv),

    const stream = input.pipe(gzip).pipe(encrypt);


    const fileName = tmpFileName();
    const file = fs.createWriteStream(fileName)
    stream
        .pipe(file)
        .on('finish', () => {
            let options = {
                "stream_length": 0,
                headers: {
                    "content-type": 'image/jpeg',
                    "content-length": file.bytesWritten
                }
            }

            const readStream = fs.createReadStream(fileName);

            // post the stream
            needle('post', url, readStream, options)
                .then(resp => {
                    console.log("file length", resp.body.length);
                })
                .catch(err => {})
                .finally(() => {
                    // Remove the file from disk
                });
        })

}
Run Code Online (Sandbox Code Playgroud)