fs: warn on non-portable mkdtemp() templates · nodejs/node@b925379 (original) (raw)

4 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -73,7 +73,8 @@ const {
73 73 toUnixTimestamp,
74 74 validateOffsetLengthRead,
75 75 validateOffsetLengthWrite,
76 - validatePath
76 + validatePath,
77 + warnOnNonPortableTemplate
77 78 } = require('internal/fs/utils');
78 79 const {
79 80 CHAR_FORWARD_SLASH,
@@ -1721,6 +1722,7 @@ function mkdtemp(prefix, options, callback) {
1721 1722 throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
1722 1723 }
1723 1724 nullCheck(prefix, 'prefix');
1725 +warnOnNonPortableTemplate(prefix);
1724 1726 const req = new FSReqCallback();
1725 1727 req.oncomplete = callback;
1726 1728 binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, req);
@@ -1733,6 +1735,7 @@ function mkdtempSync(prefix, options) {
1733 1735 throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
1734 1736 }
1735 1737 nullCheck(prefix, 'prefix');
1738 +warnOnNonPortableTemplate(prefix);
1736 1739 const path = `${prefix}XXXXXX`;
1737 1740 const ctx = { path };
1738 1741 const result = binding.mkdtemp(path, options.encoding,
Original file line number Diff line number Diff line change
@@ -31,7 +31,8 @@ const {
31 31 toUnixTimestamp,
32 32 validateOffsetLengthRead,
33 33 validateOffsetLengthWrite,
34 - validatePath
34 + validatePath,
35 + warnOnNonPortableTemplate
35 36 } = require('internal/fs/utils');
36 37 const {
37 38 parseMode,
@@ -461,6 +462,7 @@ async function mkdtemp(prefix, options) {
461 462 throw new ERR_INVALID_ARG_TYPE('prefix', 'string', prefix);
462 463 }
463 464 nullCheck(prefix);
465 +warnOnNonPortableTemplate(prefix);
464 466 return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises);
465 467 }
466 468
Original file line number Diff line number Diff line change
@@ -432,6 +432,18 @@ const validatePath = hideStackFrames((path, propName = 'path') => {
432 432 }
433 433 });
434 434
435 +let nonPortableTemplateWarn = true;
436 +
437 +function warnOnNonPortableTemplate(template) {
438 +// Template strings passed to the mkdtemp() family of functions should not
439 +// end with 'X' because they are handled inconsistently across platforms.
440 +if (nonPortableTemplateWarn && template.endsWith('X')) {
441 +process.emitWarning('mkdtemp() templates ending with X are not portable. ' +
442 +'For details see: https://nodejs.org/api/fs.html');
443 +nonPortableTemplateWarn = false;
444 +}
445 +}
446 +
435 447 module.exports = {
436 448 assertEncoding,
437 449 copyObject,
@@ -448,5 +460,6 @@ module.exports = {
448 460 toUnixTimestamp,
449 461 validateOffsetLengthRead,
450 462 validateOffsetLengthWrite,
451 - validatePath
463 + validatePath,
464 + warnOnNonPortableTemplate
452 465 };
Original file line number Diff line number Diff line change
@@ -29,3 +29,8 @@ fs.mkdtemp(path.join(tmpdir.path, 'bar.'), common.mustCall(handler));
29 29 // Same test as above, but making sure that passing an options object doesn't
30 30 // affect the way the callback function is handled.
31 31 fs.mkdtemp(path.join(tmpdir.path, 'bar.'), {}, common.mustCall(handler));
32 +
33 +const warningMsg = 'mkdtemp() templates ending with X are not portable. ' +
34 +'For details see: https://nodejs.org/api/fs.html';
35 +common.expectWarning('Warning', warningMsg);
36 +fs.mkdtemp(path.join(tmpdir.path, 'bar.X'), common.mustCall(handler));