Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: runtime deprecate string coercion in fs.write, fs.writeFileSync #42607

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
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
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
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
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