GeezerVison is showing github.com — fs: make `mkdtemp` accept buffers and URL · nodejs/node@48c25b1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +12-3Lines changed: 12 additions & 3 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1153,14 +1153,17 @@ makeDirectory().catch(console.error); | 1153 | 1153 | <!-- YAML | 1154 | 1154 | added: v10.0.0 | 1155 | 1155 | changes: | | 1156 | + - version: REPLACEME | | 1157 | + pr-url: https://github.com/nodejs/node/pull/48828 | | 1158 | + description: The `prefix` parameter now accepts buffers and URL. | 1156 | 1159 | - version: | 1157 | 1160 | - v16.5.0 | 1158 | 1161 | - v14.18.0 | 1159 | 1162 | pr-url: https://github.com/nodejs/node/pull/39028 | 1160 | 1163 | description: The `prefix` parameter now accepts an empty string. | 1161 | 1164 | --> | 1162 | 1165 |
| 1163 | | -* `prefix` {string} | | 1166 | +* `prefix` {string|Buffer|URL} | 1164 | 1167 | * `options` {string|Object} | 1165 | 1168 | * `encoding` {string} **Default:** `'utf8'` | 1166 | 1169 | * Returns: {Promise} Fulfills with a string containing the file system path | @@ -3225,6 +3228,9 @@ See the POSIX mkdir(2) documentation for more details. | 3225 | 3228 | <!-- YAML | 3226 | 3229 | added: v5.10.0 | 3227 | 3230 | changes: | | 3231 | + - version: REPLACEME | | 3232 | + pr-url: https://github.com/nodejs/node/pull/48828 | | 3233 | + description: The `prefix` parameter now accepts buffers and URL. | 3228 | 3234 | - version: v18.0.0 | 3229 | 3235 | pr-url: https://github.com/nodejs/node/pull/41678 | 3230 | 3236 | description: Passing an invalid callback to the `callback` argument | @@ -3248,7 +3254,7 @@ changes: | 3248 | 3254 | description: The `callback` parameter is optional now. | 3249 | 3255 | --> | 3250 | 3256 |
| 3251 | | -* `prefix` {string} | | 3257 | +* `prefix` {string|Buffer|URL} | 3252 | 3258 | * `options` {string|Object} | 3253 | 3259 | * `encoding` {string} **Default:** `'utf8'` | 3254 | 3260 | * `callback` {Function} | @@ -5478,14 +5484,17 @@ See the POSIX mkdir(2) documentation for more details. | 5478 | 5484 | <!-- YAML | 5479 | 5485 | added: v5.10.0 | 5480 | 5486 | changes: | | 5487 | + - version: REPLACEME | | 5488 | + pr-url: https://github.com/nodejs/node/pull/48828 | | 5489 | + description: The `prefix` parameter now accepts buffers and URL. | 5481 | 5490 | - version: | 5482 | 5491 | - v16.5.0 | 5483 | 5492 | - v14.18.0 | 5484 | 5493 | pr-url: https://github.com/nodejs/node/pull/39028 | 5485 | 5494 | description: The `prefix` parameter now accepts an empty string. | 5486 | 5495 | --> | 5487 | 5496 |
| 5488 | | -* `prefix` {string} | | 5497 | +* `prefix` {string|Buffer|URL} | 5489 | 5498 | * `options` {string|Object} | 5490 | 5499 | * `encoding` {string} **Default:** `'utf8'` | 5491 | 5500 | * Returns: {string} | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +21-9Lines changed: 21 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -139,7 +139,6 @@ const { | 139 | 139 | validateFunction, | 140 | 140 | validateInteger, | 141 | 141 | validateObject, | 142 | | - validateString, | 143 | 142 | } = require('internal/validators'); | 144 | 143 |
| 145 | 144 | let truncateWarn = true; | @@ -2884,7 +2883,7 @@ realpath.native = (path, options, callback) => { | 2884 | 2883 |
| 2885 | 2884 | /** | 2886 | 2885 | * Creates a unique temporary directory. | 2887 | | - * @param {string} prefix | | 2886 | + * @param {string | Buffer | URL} prefix | 2888 | 2887 | * @param {string | { encoding?: string; }} [options] | 2889 | 2888 | * @param {( | 2890 | 2889 | * err?: Error, | @@ -2896,27 +2895,40 @@ function mkdtemp(prefix, options, callback) { | 2896 | 2895 | callback = makeCallback(typeof options === 'function' ? options : callback); | 2897 | 2896 | options = getOptions(options); | 2898 | 2897 |
| 2899 | | - validateString(prefix, 'prefix'); | 2900 | | - nullCheck(prefix, 'prefix'); | | 2898 | + prefix = getValidatedPath(prefix, 'prefix'); | 2901 | 2899 | warnOnNonPortableTemplate(prefix); | | 2900 | + | | 2901 | + let path; | | 2902 | + if (typeof prefix === 'string') { | | 2903 | + path = `${prefix}XXXXXX`; | | 2904 | + } else { | | 2905 | + path = Buffer.concat([prefix, Buffer.from('XXXXXX')]); | | 2906 | + } | | 2907 | + | 2902 | 2908 | const req = new FSReqCallback(); | 2903 | 2909 | req.oncomplete = callback; | 2904 | | - binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, req); | | 2910 | + binding.mkdtemp(path, options.encoding, req); | 2905 | 2911 | } | 2906 | 2912 |
| 2907 | 2913 | /** | 2908 | 2914 | * Synchronously creates a unique temporary directory. | 2909 | | - * @param {string} prefix | | 2915 | + * @param {string | Buffer | URL} prefix | 2910 | 2916 | * @param {string | { encoding?: string; }} [options] | 2911 | 2917 | * @returns {string} | 2912 | 2918 | */ | 2913 | 2919 | function mkdtempSync(prefix, options) { | 2914 | 2920 | options = getOptions(options); | 2915 | 2921 |
| 2916 | | - validateString(prefix, 'prefix'); | 2917 | | - nullCheck(prefix, 'prefix'); | | 2922 | + prefix = getValidatedPath(prefix, 'prefix'); | 2918 | 2923 | warnOnNonPortableTemplate(prefix); | 2919 | | - const path = `${prefix}XXXXXX`; | | 2924 | + | | 2925 | + let path; | | 2926 | + if (typeof prefix === 'string') { | | 2927 | + path = `${prefix}XXXXXX`; | | 2928 | + } else { | | 2929 | + path = Buffer.concat([prefix, Buffer.from('XXXXXX')]); | | 2930 | + } | | 2931 | + | 2920 | 2932 | const ctx = { path }; | 2921 | 2933 | const result = binding.mkdtemp(path, options.encoding, | 2922 | 2934 | undefined, ctx); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,6 @@ const { | 59 | 59 | getStatsFromBinding, | 60 | 60 | getValidatedPath, | 61 | 61 | getValidMode, | 62 | | - nullCheck, | 63 | 62 | preprocessSymlinkDestination, | 64 | 63 | stringToFlags, | 65 | 64 | stringToSymlinkType, | @@ -976,10 +975,17 @@ async function realpath(path, options) { | 976 | 975 | async function mkdtemp(prefix, options) { | 977 | 976 | options = getOptions(options); | 978 | 977 |
| 979 | | - validateString(prefix, 'prefix'); | 980 | | - nullCheck(prefix); | | 978 | + prefix = getValidatedPath(prefix, 'prefix'); | 981 | 979 | warnOnNonPortableTemplate(prefix); | 982 | | - return binding.mkdtemp(`${prefix}XXXXXX`, options.encoding, kUsePromises); | | 980 | + | | 981 | + let path; | | 982 | + if (typeof prefix === 'string') { | | 983 | + path = `${prefix}XXXXXX`; | | 984 | + } else { | | 985 | + path = Buffer.concat([prefix, Buffer.from('XXXXXX')]); | | 986 | + } | | 987 | + | | 988 | + return binding.mkdtemp(path, options.encoding, kUsePromises); | 983 | 989 | } | 984 | 990 |
| 985 | 991 | async function writeFile(path, data, options) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/utils.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ const { | 21 | 21 | StringPrototypeEndsWith, | 22 | 22 | StringPrototypeIncludes, | 23 | 23 | Symbol, | | 24 | + TypedArrayPrototypeAt, | 24 | 25 | TypedArrayPrototypeIncludes, | 25 | 26 | } = primordials; | 26 | 27 |
| @@ -736,7 +737,9 @@ let nonPortableTemplateWarn = true; | 736 | 737 | function warnOnNonPortableTemplate(template) { | 737 | 738 | // Template strings passed to the mkdtemp() family of functions should not | 738 | 739 | // end with 'X' because they are handled inconsistently across platforms. | 739 | | - if (nonPortableTemplateWarn && StringPrototypeEndsWith(template, 'X')) { | | 740 | + if (nonPortableTemplateWarn && | | 741 | + ((typeof template === 'string' && StringPrototypeEndsWith(template, 'X')) || | | 742 | + (typeof template !== 'string' && TypedArrayPrototypeAt(template, -1) === 0x58))) { | 740 | 743 | process.emitWarning('mkdtemp() templates ending with X are not portable. ' + | 741 | 744 | 'For details see: https://nodejs.org/api/fs.html'); | 742 | 745 | nonPortableTemplateWarn = false; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-mkdtemp.js +92-18Lines changed: 92 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,29 +8,103 @@ const path = require('path'); | 8 | 8 | const tmpdir = require('../common/tmpdir'); | 9 | 9 | tmpdir.refresh(); | 10 | 10 |
| 11 | | -const tmpFolder = fs.mkdtempSync(path.join(tmpdir.path, 'foo.')); | 12 | | - | 13 | | -assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | 14 | | -assert(fs.existsSync(tmpFolder)); | 15 | | - | 16 | | -const utf8 = fs.mkdtempSync(path.join(tmpdir.path, '\u0222abc.')); | 17 | | -assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | 18 | | - Buffer.byteLength('\u0222abc.XXXXXX')); | 19 | | -assert(fs.existsSync(utf8)); | 20 | | - | 21 | 11 | function handler(err, folder) { | 22 | 12 | assert.ifError(err); | 23 | 13 | assert(fs.existsSync(folder)); | 24 | 14 | assert.strictEqual(this, undefined); | 25 | 15 | } | 26 | 16 |
| 27 | | -fs.mkdtemp(path.join(tmpdir.path, 'bar.'), common.mustCall(handler)); | | 17 | +// Test with plain string | | 18 | +{ | | 19 | + const tmpFolder = fs.mkdtempSync(path.join(tmpdir.path, 'foo.')); | | 20 | + | | 21 | + assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | | 22 | + assert(fs.existsSync(tmpFolder)); | | 23 | + | | 24 | + const utf8 = fs.mkdtempSync(path.join(tmpdir.path, '\u0222abc.')); | | 25 | + assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | | 26 | + Buffer.byteLength('\u0222abc.XXXXXX')); | | 27 | + assert(fs.existsSync(utf8)); | | 28 | + | | 29 | + fs.mkdtemp(path.join(tmpdir.path, 'bar.'), common.mustCall(handler)); | | 30 | + | | 31 | + // Same test as above, but making sure that passing an options object doesn't | | 32 | + // affect the way the callback function is handled. | | 33 | + fs.mkdtemp(path.join(tmpdir.path, 'bar.'), {}, common.mustCall(handler)); | | 34 | + | | 35 | + const warningMsg = 'mkdtemp() templates ending with X are not portable. ' + | | 36 | + 'For details see: https://nodejs.org/api/fs.html'; | | 37 | + common.expectWarning('Warning', warningMsg); | | 38 | + fs.mkdtemp(path.join(tmpdir.path, 'bar.X'), common.mustCall(handler)); | | 39 | +} | | 40 | + | | 41 | +// Test with URL object | | 42 | +{ | | 43 | + tmpdir.url = new URL(`file://${tmpdir.path}`); | | 44 | + const urljoin = (base, path) => new URL(path, base); | | 45 | + | | 46 | + const tmpFolder = fs.mkdtempSync(urljoin(tmpdir.url, 'foo.')); | | 47 | + | | 48 | + assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | | 49 | + assert(fs.existsSync(tmpFolder)); | | 50 | + | | 51 | + const utf8 = fs.mkdtempSync(urljoin(tmpdir.url, '\u0222abc.')); | | 52 | + assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | | 53 | + Buffer.byteLength('\u0222abc.XXXXXX')); | | 54 | + assert(fs.existsSync(utf8)); | | 55 | + | | 56 | + fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), common.mustCall(handler)); | | 57 | + | | 58 | + // Same test as above, but making sure that passing an options object doesn't | | 59 | + // affect the way the callback function is handled. | | 60 | + fs.mkdtemp(urljoin(tmpdir.url, 'bar.'), {}, common.mustCall(handler)); | | 61 | + | | 62 | + // Warning fires only once | | 63 | + fs.mkdtemp(urljoin(tmpdir.url, 'bar.X'), common.mustCall(handler)); | | 64 | +} | | 65 | + | | 66 | +// Test with Buffer | | 67 | +{ | | 68 | + const tmpFolder = fs.mkdtempSync(Buffer.from(path.join(tmpdir.path, 'foo.'))); | | 69 | + | | 70 | + assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | | 71 | + assert(fs.existsSync(tmpFolder)); | 28 | 72 |
| 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(tmpdir.path, 'bar.'), {}, common.mustCall(handler)); | | 73 | + const utf8 = fs.mkdtempSync(Buffer.from(path.join(tmpdir.path, '\u0222abc.'))); | | 74 | + assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | | 75 | + Buffer.byteLength('\u0222abc.XXXXXX')); | | 76 | + assert(fs.existsSync(utf8)); | | 77 | + | | 78 | + fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.')), common.mustCall(handler)); | | 79 | + | | 80 | + // Same test as above, but making sure that passing an options object doesn't | | 81 | + // affect the way the callback function is handled. | | 82 | + fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.')), {}, common.mustCall(handler)); | | 83 | + | | 84 | + // Warning fires only once | | 85 | + fs.mkdtemp(Buffer.from(path.join(tmpdir.path, 'bar.X')), common.mustCall(handler)); | | 86 | +} | 32 | 87 |
| 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)); | | 88 | +// Test with Uint8Array | | 89 | +{ | | 90 | + const encoder = new TextEncoder(); | | 91 | + | | 92 | + const tmpFolder = fs.mkdtempSync(encoder.encode(path.join(tmpdir.path, 'foo.'))); | | 93 | + | | 94 | + assert.strictEqual(path.basename(tmpFolder).length, 'foo.XXXXXX'.length); | | 95 | + assert(fs.existsSync(tmpFolder)); | | 96 | + | | 97 | + const utf8 = fs.mkdtempSync(encoder.encode(path.join(tmpdir.path, '\u0222abc.'))); | | 98 | + assert.strictEqual(Buffer.byteLength(path.basename(utf8)), | | 99 | + Buffer.byteLength('\u0222abc.XXXXXX')); | | 100 | + assert(fs.existsSync(utf8)); | | 101 | + | | 102 | + fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.')), common.mustCall(handler)); | | 103 | + | | 104 | + // Same test as above, but making sure that passing an options object doesn't | | 105 | + // affect the way the callback function is handled. | | 106 | + fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.')), {}, common.mustCall(handler)); | | 107 | + | | 108 | + // Warning fires only once | | 109 | + fs.mkdtemp(encoder.encode(path.join(tmpdir.path, 'bar.X')), common.mustCall(handler)); | | 110 | +} |
|