• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const fs = require('fs');
5const util = require('util');
6const { Writable } = require('stream');
7const { Worker, isMainThread } = require('worker_threads');
8
9class BufferingWritable extends Writable {
10  constructor() {
11    super();
12    this.chunks = [];
13  }
14
15  _write(chunk, enc, cb) {
16    this.chunks.push(chunk);
17    cb();
18  }
19
20  get buffer() {
21    return Buffer.concat(this.chunks);
22  }
23}
24
25if (isMainThread) {
26  const original = new BufferingWritable();
27  const passed = new BufferingWritable();
28
29  const w = new Worker(__filename, { stdin: true, stdout: true });
30  const source = fs.createReadStream(process.execPath);
31  source.pipe(w.stdin);
32  source.pipe(original);
33  w.stdout.pipe(passed);
34
35  passed.on('finish', common.mustCall(() => {
36    assert.strictEqual(original.buffer.compare(passed.buffer), 0,
37                       `Original: ${util.inspect(original.buffer)}, ` +
38                       `Actual: ${util.inspect(passed.buffer)}`);
39  }));
40} else {
41  process.stdin.pipe(process.stdout);
42}
43