-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
streams: add cork option to pipe #2020
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -467,6 +467,9 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |
dest !== process.stdout && | ||
dest !== process.stderr; | ||
|
||
var autoCork = pipeOpts && pipeOpts.cork && (typeof dest.cork === 'function'); | ||
var corked = false; | ||
|
||
var endFn = doEnd ? onend : cleanup; | ||
if (state.endEmitted) | ||
process.nextTick(endFn); | ||
|
@@ -515,9 +518,26 @@ Readable.prototype.pipe = function(dest, pipeOpts) { | |
ondrain(); | ||
} | ||
|
||
function maybeCork() { | ||
if (!autoCork || corked) { | ||
debug('already corked'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not technically correct in the case of autoCork being false. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True should be more like, 'no need to cork' |
||
return; | ||
} | ||
debug('corking'); | ||
corked = true; | ||
dest.cork(); | ||
process.nextTick(uncork); | ||
} | ||
function uncork() { | ||
debug('uncorking'); | ||
corked = false; | ||
dest.uncork(); | ||
} | ||
src.on('data', ondata); | ||
function ondata(chunk) { | ||
debug('ondata'); | ||
maybeCork(); | ||
debug('corks=%d', dest._writableState.corked); | ||
var ret = dest.write(chunk); | ||
if (false === ret) { | ||
debug('false write response, pause', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
'use strict'; | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
var stream = require('stream'); | ||
// tiny node-tap lookalike. | ||
var tests = []; | ||
var count = 0; | ||
|
||
function test(name, fn) { | ||
count++; | ||
tests.push([name, fn]); | ||
} | ||
|
||
function run() { | ||
var next = tests.shift(); | ||
if (!next) | ||
return console.error('ok'); | ||
|
||
var name = next[0]; | ||
var fn = next[1]; | ||
console.log('# %s', name); | ||
fn({ | ||
same: assert.deepEqual, | ||
equal: assert.equal, | ||
end: function() { | ||
count--; | ||
run(); | ||
} | ||
}); | ||
} | ||
|
||
// ensure all tests have run | ||
process.on('exit', function() { | ||
assert.equal(count, 0); | ||
}); | ||
|
||
process.nextTick(run); | ||
test('all sync', function (t) { | ||
var counter = 0; | ||
var expectCount = 0; | ||
function cnt(msg) { | ||
expectCount++; | ||
var expect = expectCount; | ||
var called = false; | ||
return function(er) { | ||
if (er) | ||
throw er; | ||
called = true; | ||
counter++; | ||
t.equal(counter, expect); | ||
}; | ||
} | ||
var p = new stream.PassThrough(); | ||
var w = new stream.Writable(); | ||
w._write = function(chunk, e, cb) { | ||
assert(false, 'Should not call _write'); | ||
}; | ||
p.pipe(w, { | ||
cork: true | ||
}); | ||
var expectChunks = | ||
[ | ||
{ encoding: 'buffer', | ||
chunk: [104, 101, 108, 108, 111, 44, 32] }, | ||
{ encoding: 'buffer', | ||
chunk: [119, 111, 114, 108, 100] }, | ||
{ encoding: 'buffer', | ||
chunk: [33] }, | ||
{ encoding: 'buffer', | ||
chunk: [10, 97, 110, 100, 32, 116, 104, 101, 110, 46, 46, 46] }, | ||
{ encoding: 'buffer', | ||
chunk: [250, 206, 190, 167, 222, 173, 190, 239, 222, 202, 251, 173]} | ||
]; | ||
|
||
var actualChunks; | ||
w._writev = function(chunks, cb) { | ||
actualChunks = chunks.map(function(chunk) { | ||
return { | ||
encoding: chunk.encoding, | ||
chunk: Buffer.isBuffer(chunk.chunk) ? | ||
Array.prototype.slice.call(chunk.chunk) : chunk.chunk | ||
}; | ||
}); | ||
cb(); | ||
}; | ||
p.write('hello, ', 'ascii', cnt('hello')); | ||
p.write('world', 'utf8', cnt('world')); | ||
|
||
p.write(new Buffer('!'), 'buffer', cnt('!')); | ||
|
||
p.write('\nand then...', 'binary', cnt('and then')); | ||
p.write('facebea7deadbeefdecafbad', 'hex', cnt('hex')); | ||
|
||
p.end(cnt('end')); | ||
|
||
|
||
w.on('finish', function() { | ||
// make sure finish comes after all the write cb | ||
cnt('finish')(); | ||
t.same(expectChunks, actualChunks); | ||
t.end(); | ||
}); | ||
}); | ||
test('2 groups', function (t) { | ||
var counter = 0; | ||
var expectCount = 0; | ||
function cnt(msg) { | ||
expectCount++; | ||
var expect = expectCount; | ||
var called = false; | ||
return function(er) { | ||
if (er) | ||
throw er; | ||
called = true; | ||
counter++; | ||
t.equal(counter, expect); | ||
}; | ||
} | ||
var p = new stream.PassThrough(); | ||
var w = new stream.Writable(); | ||
w._write = function(chunk, e, cb) { | ||
assert(false, 'Should not call _write'); | ||
}; | ||
p.pipe(w, { | ||
cork: true | ||
}); | ||
var expectChunks = [ | ||
[ | ||
{ encoding: 'buffer', | ||
chunk: [104, 101, 108, 108, 111, 44, 32] }, | ||
{ encoding: 'buffer', | ||
chunk: [119, 111, 114, 108, 100] } | ||
],[ | ||
{ encoding: 'buffer', | ||
chunk: [33] }, | ||
{ encoding: 'buffer', | ||
chunk: [10, 97, 110, 100, 32, 116, 104, 101, 110, 46, 46, 46] }, | ||
{ encoding: 'buffer', | ||
chunk: [250, 206, 190, 167, 222, 173, 190, 239, 222, 202, 251, 173]} | ||
]]; | ||
|
||
var actualChunks = []; | ||
var called = 0; | ||
w._writev = function(chunks, cb) { | ||
actualChunks.push(chunks.map(function(chunk) { | ||
return { | ||
encoding: chunk.encoding, | ||
chunk: Buffer.isBuffer(chunk.chunk) ? | ||
Array.prototype.slice.call(chunk.chunk) : chunk.chunk | ||
}; | ||
})); | ||
cb(); | ||
}; | ||
p.write('hello, ', 'ascii', cnt('hello')); | ||
p.write('world', 'utf8', cnt('world')); | ||
process.nextTick(function () { | ||
p.write(new Buffer('!'), 'buffer', cnt('!')); | ||
|
||
p.write('\nand then...', 'binary', cnt('and then')); | ||
p.write('facebea7deadbeefdecafbad', 'hex', cnt('hex')); | ||
|
||
p.end(cnt('end')); | ||
}); | ||
|
||
w.on('finish', function() { | ||
// make sure finish comes after all the write cb | ||
cnt('finish')(); | ||
console.log('expected'); | ||
console.log(expectChunks); | ||
console.log('actual'); | ||
console.log(actualChunks); | ||
t.same(expectChunks, actualChunks); | ||
t.end(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a time when
dest.cork
isn't a function? Won't it error anyways if it isn't a writable stream?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Old streams?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense! Thanks!