Skip to content

Commit

Permalink
fs: runtime deprecate string coercion in fs.write, fs.writeFileSync
Browse files Browse the repository at this point in the history
This also affects `fs.writeFile`, `fs.appendFile`, and
`fs.appendFileSync`

PR-URL: #42607
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
LiviaMedeiros authored Apr 8, 2022
1 parent c08a361 commit 0bac547
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3114,12 +3114,15 @@ resources and not the actual references.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/42607
description: Runtime deprecation.
- version: v17.8.0
pr-url: https://github.com/nodejs/node/pull/42149
description: Documentation-only deprecation.
-->

Type: Documentation-only
Type: Runtime

Implicit coercion of objects with own `toString` property, passed as second
parameter in [`fs.write()`][], [`fs.writeFile()`][], [`fs.appendFile()`][],
Expand Down
14 changes: 14 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -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');
}

Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-fs-write-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-fs-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 0bac547

Please sign in to comment.