From 773769df60ac4f2448fa88b2ece035de2512928f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 9 Aug 2019 09:01:43 +0200 Subject: fs: add runtime deprecate for file stream open() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WriteStream.open() and ReadStream.open() are undocumented internal APIs that do not make sense to use in userland. File streams should always be opened through their corresponding factory methods (fs.createWriteStream() and fs.createReadStream()) or by passing a file descriptor in options. PR-URL: https://github.com/nodejs/node/pull/29061 Reviewed-By: Ben Noordhuis Reviewed-By: Matteo Collina Reviewed-By: Jeremiah Senkpiel Reviewed-By: João Reis Reviewed-By: Ruben Bridgewater --- test/parallel/test-fs-read-stream-patch-open.js | 15 +++++++++++ test/parallel/test-fs-write-stream-patch-open.js | 34 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/parallel/test-fs-read-stream-patch-open.js create mode 100644 test/parallel/test-fs-write-stream-patch-open.js (limited to 'test') diff --git a/test/parallel/test-fs-read-stream-patch-open.js b/test/parallel/test-fs-read-stream-patch-open.js new file mode 100644 index 0000000000..80f8888de3 --- /dev/null +++ b/test/parallel/test-fs-read-stream-patch-open.js @@ -0,0 +1,15 @@ +'use strict'; +const common = require('../common'); +const fs = require('fs'); + +common.expectWarning( + 'DeprecationWarning', + 'ReadStream.prototype.open() is deprecated', 'DEP0XXX'); +const s = fs.createReadStream('asd') + // We don't care about errors in this test. + .on('error', () => {}); +s.open(); + +// Allow overriding open(). +fs.ReadStream.prototype.open = common.mustCall(); +fs.createReadStream('asd'); diff --git a/test/parallel/test-fs-write-stream-patch-open.js b/test/parallel/test-fs-write-stream-patch-open.js new file mode 100644 index 0000000000..1c82e80213 --- /dev/null +++ b/test/parallel/test-fs-write-stream-patch-open.js @@ -0,0 +1,34 @@ +'use strict'; +const common = require('../common'); +const fs = require('fs'); + +const tmpdir = require('../common/tmpdir'); + +// Run in a child process because 'out' is opened twice, blocking the tmpdir +// and preventing cleanup. +if (process.argv[2] !== 'child') { + // Parent + const assert = require('assert'); + const { fork } = require('child_process'); + tmpdir.refresh(); + + // Run test + const child = fork(__filename, ['child'], { stdio: 'inherit' }); + child.on('exit', common.mustCall(function(code) { + assert.strictEqual(code, 0); + })); + + return; +} + +// Child + +common.expectWarning( + 'DeprecationWarning', + 'WriteStream.prototype.open() is deprecated', 'DEP0XXX'); +const s = fs.createWriteStream(`${tmpdir.path}/out`); +s.open(); + +// Allow overriding open(). +fs.WriteStream.prototype.open = common.mustCall(); +fs.createWriteStream('asd'); -- cgit v1.2.3