Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1600966

Browse filesBrowse files
thefourtheyeevanlucas
authored andcommitted
fs: execute mkdtemp's callback with no context
All the callback functions in `fs` module are supposed to be executed with no context (`this` value should not be a valid object). But `mkdtemp`'s callback will have the `FSReqWrap` object as the context. Sample code to reproduce the problem 'use strict'; const fs = require('fs'); fs.mkdtemp('/tmp/abcd', null, function() { console.log(this); }); This would print FSReqWrap { oncomplete: [Function] } But that should have printed `null` and this patch fixes that. PR-URL: #7068 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 2961f06 commit 1600966
Copy full SHA for 1600966

File tree

Expand file treeCollapse file tree

2 files changed

+16
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-9
lines changed
Open diff view settings
Collapse file

‎lib/fs.js‎

Copy file name to clipboardExpand all lines: lib/fs.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,8 +1590,7 @@ fs.realpath = function realpath(path, options, callback) {
15901590
};
15911591

15921592

1593-
fs.mkdtemp = function(prefix, options, callback_) {
1594-
var callback = maybeCallback(callback_);
1593+
fs.mkdtemp = function(prefix, options, callback) {
15951594
if (!prefix || typeof prefix !== 'string')
15961595
throw new TypeError('filename prefix is required');
15971596

@@ -1605,6 +1604,7 @@ fs.mkdtemp = function(prefix, options, callback_) {
16051604
if (typeof options !== 'object')
16061605
throw new TypeError('"options" must be a string or an object');
16071606

1607+
callback = makeCallback(callback);
16081608
if (!nullCheck(prefix, callback)) {
16091609
return;
16101610
}
Collapse file

‎test/parallel/test-fs-mkdtemp.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-mkdtemp.js
+14-7Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ assert.equal(Buffer.byteLength(path.basename(utf8)),
1818
Buffer.byteLength('\u0222abc.XXXXXX'));
1919
assert(common.fileExists(utf8));
2020

21-
fs.mkdtemp(
22-
path.join(common.tmpDir, 'bar.'),
23-
common.mustCall(function(err, folder) {
24-
assert.ifError(err);
25-
assert(common.fileExists(folder));
26-
})
27-
);
21+
function handler(err, folder) {
22+
assert.ifError(err);
23+
assert(common.fileExists(folder));
24+
assert.strictEqual(this, null);
25+
}
2826

27+
fs.mkdtemp(path.join(common.tmpDir, 'bar.'), common.mustCall(handler));
28+
29+
// Same test as above, but making sure that passing an options object doesn't
30+
// affect the way the callback function is handled.
31+
fs.mkdtemp(path.join(common.tmpDir, 'bar.'), {}, common.mustCall(handler));
32+
33+
// Making sure that not passing a callback doesn't crash, as a default function
34+
// is passed internally.
2935
assert.doesNotThrow(() => fs.mkdtemp(path.join(common.tmpDir, 'bar-')));
36+
assert.doesNotThrow(() => fs.mkdtemp(path.join(common.tmpDir, 'bar-'), {}));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.