From 0bac5478eb9d242719f7e19f44db03d36c3647d8 Mon Sep 17 00:00:00 2001 From: Livia Medeiros <74449973+LiviaMedeiros@users.noreply.github.com> Date: Fri, 8 Apr 2022 18:16:08 +0800 Subject: [PATCH] fs: runtime deprecate string coercion in `fs.write`, `fs.writeFileSync` This also affects `fs.writeFile`, `fs.appendFile`, and `fs.appendFileSync` PR-URL: https://github.com/nodejs/node/pull/42607 Reviewed-By: Antoine du Hamel Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- doc/api/deprecations.md | 5 ++++- lib/fs.js | 14 ++++++++++++++ test/parallel/test-fs-write-file-sync.js | 4 ++++ test/parallel/test-fs-write.js | 6 ++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 32b25ee9b87f88..b154c96c9eb277 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3114,12 +3114,15 @@ resources and not the actual references. -Type: Documentation-only +Type: Runtime Implicit coercion of objects with own `toString` property, passed as second parameter in [`fs.write()`][], [`fs.writeFile()`][], [`fs.appendFile()`][], diff --git a/lib/fs.js b/lib/fs.js index 417fff0dedcac3..405c206d56685c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -163,6 +163,11 @@ const isWindows = process.platform === 'win32'; const isOSX = process.platform === 'darwin'; +const showStringCoercionDeprecation = internalUtil.deprecate( + () => {}, + 'Implicit coercion of objects with own toString property is deprecated.', + 'DEP0162' +); function showTruncateDeprecation() { if (truncateWarn) { process.emitWarning( @@ -826,6 +831,9 @@ function write(fd, buffer, offset, length, position, callback) { } validateStringAfterArrayBufferView(buffer, 'buffer'); + if (typeof buffer !== 'string') { + showStringCoercionDeprecation(); + } if (typeof position !== 'function') { if (typeof offset === 'function') { @@ -2121,6 +2129,9 @@ function writeFile(path, data, options, callback) { if (!isArrayBufferView(data)) { validateStringAfterArrayBufferView(data, 'data'); + if (typeof data !== 'string') { + showStringCoercionDeprecation(); + } data = Buffer.from(String(data), options.encoding || 'utf8'); } @@ -2161,6 +2172,9 @@ function writeFileSync(path, data, options) { if (!isArrayBufferView(data)) { validateStringAfterArrayBufferView(data, 'data'); + if (typeof data !== 'string') { + showStringCoercionDeprecation(); + } data = Buffer.from(String(data), options.encoding || 'utf8'); } diff --git a/test/parallel/test-fs-write-file-sync.js b/test/parallel/test-fs-write-file-sync.js index 2ef7d14365b781..950b571c79eb39 100644 --- a/test/parallel/test-fs-write-file-sync.js +++ b/test/parallel/test-fs-write-file-sync.js @@ -104,6 +104,10 @@ tmpdir.refresh(); // Test writeFileSync with an object with an own toString function { + // Runtime deprecated by DEP0162 + common.expectWarning('DeprecationWarning', + 'Implicit coercion of objects with own toString property is deprecated.', + 'DEP0162'); const file = path.join(tmpdir.path, 'testWriteFileSyncStringify.txt'); const data = { toString() { diff --git a/test/parallel/test-fs-write.js b/test/parallel/test-fs-write.js index c30eba28bd95a0..a321f1b27adaa3 100644 --- a/test/parallel/test-fs-write.js +++ b/test/parallel/test-fs-write.js @@ -126,6 +126,12 @@ fs.open(fn3, 'w', 0o644, common.mustSucceed((fd) => { fs.write(fd, expected, done); })); + +// Test write with an object with an own toString function +// Runtime deprecated by DEP0162 +common.expectWarning('DeprecationWarning', + 'Implicit coercion of objects with own toString property is deprecated.', + 'DEP0162'); fs.open(fn4, 'w', 0o644, common.mustSucceed((fd) => { const done = common.mustSucceed((written) => { assert.strictEqual(written, Buffer.byteLength(expected));