【问题标题】:node.js tar module, 'entry' readable streamnode.js tar 模块,“入口”可读流
【发布时间】:2013-12-03 16:53:37
【问题描述】:

我应该如何使用从 tar 模块到 pipe - npmsj.org 其内容的“入口”可读流而不在管道中出现流错误?

这是为了获得stream-adventure - github最后练习的提示。

我不是在寻找答案。而是一个提示或建议。

这是我的代码:

var zlib = require('zlib');
var tar = require('tar');
var crypto = require('crypto');
var through = require('through');

var unzip = zlib.createGunzip();
var parser = tar.Parse();
var stream = process.stdin.pipe(crypto.createDecipher(process.argv[2], process.argv[3])).pipe(unzip);
var md5 = crypto.createHash('md5', { encoding: 'hex' });

parser.on('entry', function(entry) {
  if (entry.type === 'File') {
    entry.pipe(md5).pipe(process.stdout);
    console.log(entry.path);
  }
});

unzip.pipe(parser);

这是输出:

$> stream-adventure run app
97911dcc607865d621029f6f927c7851
stream.js:94
      throw er; // Unhandled stream error in pipe.
            ^
Error: write after end
    at writeAfterEnd (_stream_writable.js:130:12)
    at Hash.Writable.write (_stream_writable.js:178:5)
    at Entry.ondata (stream.js:51:26)
    at Entry.EventEmitter.emit (events.js:117:20)
    at Entry._read (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/entry.js:111:10)
    at Entry.write (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/entry.js:68:8)
    at Parse._process (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/parse.js:104:11)
    at BlockStream.<anonymous> (/home/n0t/stream-adventure/secretz/node_modules/tar/lib/parse.js:46:8)
    at BlockStream.EventEmitter.emit (events.js:95:17)
    at BlockStream._emitChunk (/home/n0t/stream-adventure/secretz/node_modules/tar/node_modules/block-stream/block-stream.js:145:10)

verify:

$> stream-adventure verify app

ACTUAL:   "97911dcc607865d621029f6f927c7851"
EXPECTED: "97911dcc607865d621029f6f927c7851 secretz/METADATA.TXT"

ACTUAL:   null
EXPECTED: "2cdcfa9f8bbefb82fb7a894964b5c199 secretz/SPYING.TXT"

ACTUAL:   null
EXPECTED: ""

# FAIL

【问题讨论】:

    标签: node.js pipe tar


    【解决方案1】:

    您收到此错误是因为 entry 在 is 关闭后写入 md5 流。一旦一个流被关闭,你就不能再次写入它:对于md5,这很容易理解,因为你必须重置内部缓冲区,否则哈希会倾斜。

    在您的示例中,在 tar 模块中的每个文件上,您将文件流通过管道传输到 same md5 流中。您只需要将文件流通过管道传输到新的 MD5 流中;以下是如何正确地做到这一点:

    parser.on('entry', function(entry) {
      if (entry.type === 'File') {
        var md5 = crypto.createHash('md5', { encoding: 'hex' });
        entry.pipe(md5).pipe(process.stdout);
        console.log(entry.path);
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 2014-03-13
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      相关资源
      最近更新 更多