Skip to content

Commit 0bac547

Browse files
fs: runtime deprecate string coercion in fs.write, fs.writeFileSync
This also affects `fs.writeFile`, `fs.appendFile`, and `fs.appendFileSync` PR-URL: #42607 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent c08a361 commit 0bac547

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

doc/api/deprecations.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3114,12 +3114,15 @@ resources and not the actual references.
31143114

31153115
<!-- YAML
31163116
changes:
3117+
- version: REPLACEME
3118+
pr-url: https://github.com/nodejs/node/pull/42607
3119+
description: Runtime deprecation.
31173120
- version: v17.8.0
31183121
pr-url: https://github.com/nodejs/node/pull/42149
31193122
description: Documentation-only deprecation.
31203123
-->
31213124

3122-
Type: Documentation-only
3125+
Type: Runtime
31233126

31243127
Implicit coercion of objects with own `toString` property, passed as second
31253128
parameter in [`fs.write()`][], [`fs.writeFile()`][], [`fs.appendFile()`][],

lib/fs.js

+14
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ const isWindows = process.platform === 'win32';
163163
const isOSX = process.platform === 'darwin';
164164

165165

166+
const showStringCoercionDeprecation = internalUtil.deprecate(
167+
() => {},
168+
'Implicit coercion of objects with own toString property is deprecated.',
169+
'DEP0162'
170+
);
166171
function showTruncateDeprecation() {
167172
if (truncateWarn) {
168173
process.emitWarning(
@@ -826,6 +831,9 @@ function write(fd, buffer, offset, length, position, callback) {
826831
}
827832

828833
validateStringAfterArrayBufferView(buffer, 'buffer');
834+
if (typeof buffer !== 'string') {
835+
showStringCoercionDeprecation();
836+
}
829837

830838
if (typeof position !== 'function') {
831839
if (typeof offset === 'function') {
@@ -2121,6 +2129,9 @@ function writeFile(path, data, options, callback) {
21212129

21222130
if (!isArrayBufferView(data)) {
21232131
validateStringAfterArrayBufferView(data, 'data');
2132+
if (typeof data !== 'string') {
2133+
showStringCoercionDeprecation();
2134+
}
21242135
data = Buffer.from(String(data), options.encoding || 'utf8');
21252136
}
21262137

@@ -2161,6 +2172,9 @@ function writeFileSync(path, data, options) {
21612172

21622173
if (!isArrayBufferView(data)) {
21632174
validateStringAfterArrayBufferView(data, 'data');
2175+
if (typeof data !== 'string') {
2176+
showStringCoercionDeprecation();
2177+
}
21642178
data = Buffer.from(String(data), options.encoding || 'utf8');
21652179
}
21662180

test/parallel/test-fs-write-file-sync.js

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ tmpdir.refresh();
104104

105105
// Test writeFileSync with an object with an own toString function
106106
{
107+
// Runtime deprecated by DEP0162
108+
common.expectWarning('DeprecationWarning',
109+
'Implicit coercion of objects with own toString property is deprecated.',
110+
'DEP0162');
107111
const file = path.join(tmpdir.path, 'testWriteFileSyncStringify.txt');
108112
const data = {
109113
toString() {

test/parallel/test-fs-write.js

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ fs.open(fn3, 'w', 0o644, common.mustSucceed((fd) => {
126126
fs.write(fd, expected, done);
127127
}));
128128

129+
130+
// Test write with an object with an own toString function
131+
// Runtime deprecated by DEP0162
132+
common.expectWarning('DeprecationWarning',
133+
'Implicit coercion of objects with own toString property is deprecated.',
134+
'DEP0162');
129135
fs.open(fn4, 'w', 0o644, common.mustSucceed((fd) => {
130136
const done = common.mustSucceed((written) => {
131137
assert.strictEqual(written, Buffer.byteLength(expected));

0 commit comments

Comments
 (0)