GeezerVison is showing github.com — fs: stop lazy loading stream constructors · nodejs/node@484140e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +7-53Lines changed: 7 additions & 53 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -52,6 +52,7 @@ const { | 52 | 52 | } = errors.codes; | 53 | 53 |
| 54 | 54 | const { FSReqWrap, statValues } = binding; | | 55 | +const { ReadStream, WriteStream } = require('internal/fs/streams'); | 55 | 56 | const internalFS = require('internal/fs/utils'); | 56 | 57 | const { getPathFromURL } = require('internal/url'); | 57 | 58 | const internalUtil = require('internal/util'); | @@ -91,13 +92,6 @@ let fs; | 91 | 92 | let promises; | 92 | 93 | let watchers; | 93 | 94 | let ReadFileContext; | 94 | | -let ReadStream; | 95 | | -let WriteStream; | 96 | | - | 97 | | -// These have to be separate because of how graceful-fs happens to do it's | 98 | | -// monkeypatching. | 99 | | -let FileReadStream; | 100 | | -let FileWriteStream; | 101 | 95 |
| 102 | 96 | const isWindows = process.platform === 'win32'; | 103 | 97 |
| @@ -1697,20 +1691,11 @@ function copyFileSync(src, dest, flags) { | 1697 | 1691 | handleErrorFromBinding(ctx); | 1698 | 1692 | } | 1699 | 1693 |
| 1700 | | -function lazyLoadStreams() { | 1701 | | - if (!ReadStream) { | 1702 | | - ({ ReadStream, WriteStream } = require('internal/fs/streams')); | 1703 | | - [ FileReadStream, FileWriteStream ] = [ ReadStream, WriteStream ]; | 1704 | | - } | 1705 | | -} | 1706 | | - | 1707 | 1694 | function createReadStream(path, options) { | 1708 | | - lazyLoadStreams(); | 1709 | 1695 | return new ReadStream(path, options); | 1710 | 1696 | } | 1711 | 1697 |
| 1712 | 1698 | function createWriteStream(path, options) { | 1713 | | - lazyLoadStreams(); | 1714 | 1699 | return new WriteStream(path, options); | 1715 | 1700 | } | 1716 | 1701 |
| @@ -1793,43 +1778,12 @@ module.exports = fs = { | 1793 | 1778 | writeSync, | 1794 | 1779 | Stats, | 1795 | 1780 |
| 1796 | | - get ReadStream() { | 1797 | | - lazyLoadStreams(); | 1798 | | - return ReadStream; | 1799 | | - }, | 1800 | | - | 1801 | | - set ReadStream(val) { | 1802 | | - ReadStream = val; | 1803 | | - }, | 1804 | | - | 1805 | | - get WriteStream() { | 1806 | | - lazyLoadStreams(); | 1807 | | - return WriteStream; | 1808 | | - }, | 1809 | | - | 1810 | | - set WriteStream(val) { | 1811 | | - WriteStream = val; | 1812 | | - }, | 1813 | | - | 1814 | | - // Legacy names... these have to be separate because of how graceful-fs | 1815 | | - // (and possibly other) modules monkey patch the values. | 1816 | | - get FileReadStream() { | 1817 | | - lazyLoadStreams(); | 1818 | | - return FileReadStream; | 1819 | | - }, | 1820 | | - | 1821 | | - set FileReadStream(val) { | 1822 | | - FileReadStream = val; | 1823 | | - }, | 1824 | | - | 1825 | | - get FileWriteStream() { | 1826 | | - lazyLoadStreams(); | 1827 | | - return FileWriteStream; | 1828 | | - }, | 1829 | | - | 1830 | | - set FileWriteStream(val) { | 1831 | | - FileWriteStream = val; | 1832 | | - }, | | 1781 | + // Stream constructors | | 1782 | + ReadStream, | | 1783 | + WriteStream, | | 1784 | + // Legacy names... | | 1785 | + FileReadStream: ReadStream, | | 1786 | + FileWriteStream: WriteStream, | 1833 | 1787 |
| 1834 | 1788 | // For tests | 1835 | 1789 | _toUnixTimestamp: toUnixTimestamp | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/streams.js +12-6Lines changed: 12 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | ERR_INVALID_ARG_TYPE, | 9 | 9 | ERR_OUT_OF_RANGE | 10 | 10 | } = require('internal/errors').codes; | 11 | | -const fs = require('fs'); | 12 | 11 | const { Buffer } = require('buffer'); | 13 | 12 | const { | 14 | 13 | copyObject, | @@ -18,6 +17,13 @@ const { Readable, Writable } = require('stream'); | 18 | 17 | const { getPathFromURL } = require('internal/url'); | 19 | 18 | const util = require('util'); | 20 | 19 |
| | 20 | +let fs; | | 21 | +function lazyFs() { | | 22 | + if (fs === undefined) | | 23 | + fs = require('fs'); | | 24 | + return fs; | | 25 | +} | | 26 | + | 21 | 27 | const kMinPoolSpace = 128; | 22 | 28 |
| 23 | 29 | let pool; | @@ -92,7 +98,7 @@ function ReadStream(path, options) { | 92 | 98 | util.inherits(ReadStream, Readable); | 93 | 99 |
| 94 | 100 | ReadStream.prototype.open = function() { | 95 | | - fs.open(this.path, this.flags, this.mode, (er, fd) => { | | 101 | + lazyFs().open(this.path, this.flags, this.mode, (er, fd) => { | 96 | 102 | if (er) { | 97 | 103 | if (this.autoClose) { | 98 | 104 | this.destroy(); | @@ -142,7 +148,7 @@ ReadStream.prototype._read = function(n) { | 142 | 148 | return this.push(null); | 143 | 149 |
| 144 | 150 | // the actual read. | 145 | | - fs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { | | 151 | + lazyFs().read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => { | 146 | 152 | if (er) { | 147 | 153 | if (this.autoClose) { | 148 | 154 | this.destroy(); | @@ -177,7 +183,7 @@ ReadStream.prototype._destroy = function(err, cb) { | 177 | 183 | }; | 178 | 184 |
| 179 | 185 | function closeFsStream(stream, cb, err) { | 180 | | - fs.close(stream.fd, (er) => { | | 186 | + lazyFs().close(stream.fd, (er) => { | 181 | 187 | er = er || err; | 182 | 188 | cb(er); | 183 | 189 | stream.closed = true; | @@ -242,7 +248,7 @@ WriteStream.prototype._final = function(callback) { | 242 | 248 | }; | 243 | 249 |
| 244 | 250 | WriteStream.prototype.open = function() { | 245 | | - fs.open(this.path, this.flags, this.mode, (er, fd) => { | | 251 | + lazyFs().open(this.path, this.flags, this.mode, (er, fd) => { | 246 | 252 | if (er) { | 247 | 253 | if (this.autoClose) { | 248 | 254 | this.destroy(); | @@ -270,7 +276,7 @@ WriteStream.prototype._write = function(data, encoding, cb) { | 270 | 276 | }); | 271 | 277 | } | 272 | 278 |
| 273 | | - fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { | | 279 | + lazyFs().write(this.fd, data, 0, data.length, this.pos, (er, bytes) => { | 274 | 280 | if (er) { | 275 | 281 | if (this.autoClose) { | 276 | 282 | this.destroy(); | |
|