-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Add autoClose=true option to fs.createWrireStream #20880 #25275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -822,13 +822,20 @@ Returns a new WriteStream object (See `Writable Stream`). | |
{ flags: 'w', | ||
encoding: null, | ||
fd: null, | ||
mode: 0666 } | ||
mode: 0666, | ||
autoClose: true } | ||
|
||
`options` may also include a `start` option to allow writing data at | ||
some position past the beginning of the file. Modifying a file rather | ||
than replacing it may require a `flags` mode of `r+` rather than the | ||
default mode `w`. | ||
|
||
If `autoClose` is false, then the file descriptor won't be closed, even if | ||
there's an error. It is your responsiblity to close it and make sure | ||
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. Let's change this sentence to 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. I can change this line but i copied this text from createReadStream autoclose paragraph. Shall i still change it? As of now both createReadStream and createWriteStream para are consistent. 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. OK, keep it in that case. 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. okie. Are we good to merge this PR now? This will be my first contribution to Node :-) 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. @saquibkhan Thank you very much for your first contribution! There's no rush to merge a PR. Taking the extra time to make sure changes are as good as they can be and well understood always pays off. We don't want to rush merging changes and introduce new problems that will take more time to fix. In this case however I'm confident we'll be able to merge it very soon, so there's no need to worry :) |
||
there's no file descriptor leak. If `autoClose` is set to true (default | ||
behavior) on `error` or `end` the file descriptor will be closed | ||
automatically. | ||
|
||
Like `ReadStream` above, if `fd` is specified, `WriteStream` will ignore the | ||
`path` argument and will use the specified file descriptor. This means that no | ||
`open` event will be emitted. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright Joyent, Inc. and other Node contributors. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to permit | ||
// persons to whom the Software is furnished to do so, subject to the | ||
// following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included | ||
// in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN | ||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | ||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
|
||
var file = path.join(common.tmpDir, 'write-autoclose-opt1.txt'); | ||
var stream = fs.createWriteStream(file, {flags: 'w+', autoClose: false}); | ||
stream.write('Test1'); | ||
stream.end(); | ||
stream.on('finish', function() { | ||
process.nextTick(function() { | ||
assert(!stream.closed); | ||
assert(stream.fd); | ||
next(); | ||
}); | ||
}); | ||
|
||
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. Minor style issue: unnecessary blank line. |
||
function next() { | ||
// This will tell us if the fd is usable again or not | ||
stream = fs.createWriteStream(null, {fd: stream.fd, start:0 }); | ||
stream.write('Test2'); | ||
stream.end(); | ||
stream.on('finish', function() { | ||
assert(stream.closed); | ||
assert(!stream.fd); | ||
process.nextTick(next2); | ||
}); | ||
} | ||
|
||
function next2() { | ||
//This will test if after reusing the fd data is written properly | ||
fs.readFile(file, function(err, data) { | ||
assert(!err); | ||
assert.equal(data, 'Test2'); | ||
process.nextTick(next3); | ||
}); | ||
} | ||
|
||
function next3() { | ||
//This is to test success scenario where autoClose is true | ||
var stream = fs.createWriteStream(file, {autoClose: true}); | ||
stream.write('Test3'); | ||
stream.end(); | ||
stream.on('finish', function() { | ||
process.nextTick(function() { | ||
assert(stream.closed); | ||
assert(!stream.fd); | ||
}); | ||
}); | ||
} |
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.
Can we change the end of this sentence to
..., even if an error occurs.