GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: fix disparity between buffer and the count · nodejs/node@b6ce918

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • _stream_writable.js
  • test/sequential
    • test-stream-writable-clear-buffer.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/_stream_writable.js‎

Copy file name to clipboardExpand all lines: lib/_stream_writable.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ function clearBuffer(stream, state) {
429429
} else {
430430
state.corkedRequestsFree = new CorkedRequest(state);
431431
}
432+
state.bufferedRequestCount = 0;
432433
} else {
433434
// Slow case, write chunks one-by-one
434435
while (entry) {
@@ -439,6 +440,7 @@ function clearBuffer(stream, state) {
439440

440441
doWrite(stream, state, false, len, chunk, encoding, cb);
441442
entry = entry.next;
443+
state.bufferedRequestCount--;
442444
// if we didn't call the onwrite immediately, then
443445
// it means that we need to wait until it does.
444446
// also, that means that the chunk and cb are currently
@@ -452,7 +454,6 @@ function clearBuffer(stream, state) {
452454
state.lastBufferedRequest = null;
453455
}
454456

455-
state.bufferedRequestCount = 0;
456457
state.bufferedRequest = entry;
457458
state.bufferProcessing = false;
458459
}
Collapse file

‎test/sequential/test-stream-writable-clear-buffer.js‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const common = require('../common');
3+
const Stream = require('stream');
4+
// This test ensures that the _writeableState.bufferedRequestCount and
5+
// the actual buffered request count are the same
6+
const assert = require('assert');
7+
8+
class StreamWritable extends Stream.Writable {
9+
constructor() {
10+
super({ objectMode: true });
11+
}
12+
13+
// We need a timeout like on the original issue thread
14+
// otherwise the code will never reach our test case
15+
// this means this should go on the sequential folder.
16+
_write(chunk, encoding, cb) {
17+
setTimeout(cb, common.platformTimeout(10));
18+
}
19+
}
20+
21+
const testStream = new StreamWritable();
22+
testStream.cork();
23+
24+
for (let i = 1; i <= 5; i++) {
25+
testStream.write(i, function() {
26+
assert.strictEqual(
27+
testStream._writableState.bufferedRequestCount,
28+
testStream._writableState.getBuffer().length,
29+
'bufferedRequestCount variable is different from the actual length of' +
30+
' the buffer');
31+
});
32+
}
33+
34+
testStream.end();

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.