• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const stream = require('stream');
6
7const writable = new stream.Writable();
8
9writable._writev = common.mustCall((chunks, cb) => {
10  assert.strictEqual(chunks.length, 2);
11  cb();
12}, 1);
13
14writable._write = common.mustCall((chunk, encoding, cb) => {
15  cb();
16}, 1);
17
18// first cork
19writable.cork();
20assert.strictEqual(writable._writableState.corked, 1);
21assert.strictEqual(writable._writableState.bufferedRequestCount, 0);
22
23// cork again
24writable.cork();
25assert.strictEqual(writable._writableState.corked, 2);
26
27// The first chunk is buffered
28writable.write('first chunk');
29assert.strictEqual(writable._writableState.bufferedRequestCount, 1);
30
31// First uncork does nothing
32writable.uncork();
33assert.strictEqual(writable._writableState.corked, 1);
34assert.strictEqual(writable._writableState.bufferedRequestCount, 1);
35
36process.nextTick(uncork);
37
38// The second chunk is buffered, because we uncork at the end of tick
39writable.write('second chunk');
40assert.strictEqual(writable._writableState.corked, 1);
41assert.strictEqual(writable._writableState.bufferedRequestCount, 2);
42
43function uncork() {
44  // Second uncork flushes the buffer
45  writable.uncork();
46  assert.strictEqual(writable._writableState.corked, 0);
47  assert.strictEqual(writable._writableState.bufferedRequestCount, 0);
48
49  // Verify that end() uncorks correctly
50  writable.cork();
51  writable.write('third chunk');
52  writable.end();
53
54  // End causes an uncork() as well
55  assert.strictEqual(writable._writableState.corked, 0);
56  assert.strictEqual(writable._writableState.bufferedRequestCount, 0);
57}
58