From 15c720584a147f8c3e1c60ee0579d8c98684470e Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Tue, 5 Apr 2022 02:21:07 +0800 Subject: [PATCH 1/2] fs: runtime deprecate string coercion in `fs.write`, `fs.writeFileSync` This also affects `fs.writeFile`, `fs.appendFile`, and `fs.appendFileSync` --- 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..2d66039ef0e5b1 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)); From e5a6d62a78df0d0c354aa7c9e676382481145c2b Mon Sep 17 00:00:00 2001 From: Livia Medeiros <74449973+LiviaMedeiros@users.noreply.github.com> Date: Tue, 5 Apr 2022 03:20:59 +0800 Subject: [PATCH 2/2] squash: set PR url Co-authored-by: Antoine du Hamel --- doc/api/deprecations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 2d66039ef0e5b1..b154c96c9eb277 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3115,7 +3115,7 @@ resources and not the actual references.