question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function like `tmp.open` to create write stream.

See original GitHub issue

tmp.file has some nice advantages (implicit mode and exclusive) but code using the file descriptor from fs.open is rare nowadays.

An alternate version that would return a stream made with fs.createWriteStream would be nice.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
silkentrancecommented, Aug 7, 2015

@dantman I see where you are getting at, would the following API extension suffice?

function tmp.createReadStream(tmpObject) {
  return fs.createReadStream(tmpObject.name, {fd : tmpObject.fd, flags : "r"});
}
function tmp.createWriteStream(tmpObject) {
  return fs.createWriteStream(tmpObject.name, {fd : tmpObject.fd, flags : "a+"});
}

I think that registering event listeners should be left to the client, e.g. error event listener that will automatically cleanup the tmp file and so on…

Problems here might be:

'wx+' - Like 'w+' but fails if path exists.
'ax+' - Like 'a+' but fails if path exists.

Calling the above API multiple times will likely cause an error with the default flags… Would it not suffice that tmp.fileSync() used these flags and possibly failed if the path did already exist, and use createWriteStream as it is, with only the “w+”?

In addition w+ will still truncate the tmp file, do you want this to happen? I think that a+ is the better choice here, is it not?

1reaction
dantmancommented, Aug 6, 2015

Huh? No, I want to be able to create a write stream for a temporary file the same way I would create an open fd for a temporary file. With the exact same behaviour. ie: Without suddenly being forced to declare _c.O_CREAT | _c.O_EXCL | _c.O_RDWR and the default mode myself on a nodejs fs call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use fs.createWriteStream? - Node.js
createWriteStream () function opens a file for writing. The file is created (if it does not exist) or truncated (if it exists). Since...
Read more >
event associated with fs.createWriteStream in node.js
What event is triggered when EOF is reached while writing to a stream ? My code is as follows. and it ...
Read more >
Understanding Streams in Node.js - NodeSource
The function call fs.createReadStream() gives you a readable stream. Initially, the stream is in a static state. As soon as you listen to...
Read more >
Write Data to File Using fs.createWriteStream() - YouTube
In this Video, we looked at how we can write data to a file, pipe data from one file to another using a...
Read more >
fs.rename(path1, path2, [callback]) : nodejs API - GitHub Pages
WriteStream is a Writable Stream. Event: 'open' #. function (fd) { }. fd is the file descriptor used by the WriteStream.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found