fs: runtime deprecate string coercion in `fs.write`, `fs.writeFileSync` · nodejs/node@0bac547 · GitHub
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 <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent c08a361 commit 0bac547

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion

lib/fs.js

Lines changed: 14 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions

0 commit comments

Comments
 (0)