Node: RFE: node should have a mktemp()

Created on 18 Feb 2016  路  13Comments  路  Source: nodejs/node

Today it is more and more important being able to create uniqe files and directories, initially only read/writable to the creating user. Node should have such a function and as a node special, supporting something like a "deleteOnExit" flag would be very sweet.

feature request fs libuv

Most helpful comment

I've seen that since 5.10.0 there is a fs.mkdtemp(prefix, callback). So aligning with it makes sense IMHO ...

All 13 comments

Is there anything wrong with npm modules, such as https://www.npmjs.com/package/tmp ?

Sounds kinda out of the scope of core, is there any reason this would need to be in core? As @vkurchatkin pointed out there already seems to be npm alternatives.

Yes, I consider it to be core. Other commonly used languages think probably the same and provide such a function. Because they are so important, IIRC almost every Unix/Linux has an implementation in its libc (i.e. mkdtemp,mkstemp as well as tmpfile). So at least for those it would be more or less just a matter of wrapping it into node.

The need to install npm and even open a system so that it can just pull a more or less untrusted version of it for such very basic/essential stuff is IMHO kind of ...

mktemp() support would likely have to be implemented in libuv (which is what Node.js uses to make system calls) rather than in Node.js directly. There's an open issue for it in the libuv repo.

libuv already has uv_os_tmpdir that gives you where is it: https://github.com/libuv/libuv/pull/672

This should be exposed (if it's not already)

And leave the rest for userland.

This should be exposed (if it's not already)

It's not exposed in Node. Once the version of libuv is upgraded in Node, I plan to open a PR.

Ehmm, nope. AFAICS libuv/libuv#672 is only about getting just _the_ directory, which one may use for temporary files, i.e. returns always the same name, if related env entries do not change.

But mktemp() is about creating a uniqe tmp file or directory with mode 0700 & ~umask! So it is just the part, which can fullfill the -t options part compared to most OS/GNU implementations of this utility.

Also if properly done, it has to be thread safe, otherwise it is of little use.

Please have a look at the provided links for mkdtemp,mkstemp as well as tmpfile (the specs should make clear, what at min. is needed) or if you have access to a Linux/Unix box, just read its mktemp(1) man page.

@cjihrig https://nodejs.org/api/os.html#os_os_tmpdir , is that sufficient or are you going to PR against the libuv API?

I've seen that since 5.10.0 there is a fs.mkdtemp(prefix, callback). So aligning with it makes sense IMHO ...

@bmeck I'm not sure I understand the question. Node and libuv both have functions for getting the temp directory. They are just subtly different enough that I decided against replacing Node's with a call to libuv.

@cjihrig was mostly asking about if this change is necessary / what great benefits are to be gained vs the existing JS API. If things need to be added / there is improvement to be gained I was wondering if you still planned on making a PR (looks like you planned on doing so in Feb?)

@cjihrig I guess this could be closed? The wanted functionality is implemented with mkdtemp or is there anything else still that should still be done?

Let's close. We can reopen if necessary.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vsemozhetbyt picture vsemozhetbyt  路  3Comments

srl295 picture srl295  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments

loretoparisi picture loretoparisi  路  3Comments