| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b7dc651 commit 59fff92
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1887,6 +1887,12 @@ behavior is similar to `cp dir1/ dir2/`. | |||
| 1887 | 1887 | <!-- YAML | |
| 1888 | 1888 | added: v0.1.31 | |
| 1889 | 1889 | changes: | |
| 1890 | + - version: REPLACEME | ||
| 1891 | + pr-url: https://github.com/nodejs/node/pull/40013 | ||
| 1892 | + description: The `fs` option does not need `open` method if an `fd` was provided. | ||
| 1893 | + - version: REPLACEME | ||
| 1894 | + pr-url: https://github.com/nodejs/node/pull/40013 | ||
| 1895 | + description: The `fs` option does not need `close` method if `autoClose` is `false`. | ||
| 1890 | 1896 | - version: | |
| 1891 | 1897 | - v15.4.0 | |
| 1892 | 1898 | pr-url: https://github.com/nodejs/node/pull/35922 | |
@@ -1962,7 +1968,9 @@ destroyed, like most `Readable` streams. Set the `emitClose` option to | |||
| 1962 | 1968 | ||
| 1963 | 1969 | By providing the `fs` option, it is possible to override the corresponding `fs` | |
| 1964 | 1970 | implementations for `open`, `read`, and `close`. When providing the `fs` option, | |
| 1965 | - overrides for `open`, `read`, and `close` are required. | ||
| 1971 | + an override for `read` is required. If no `fd` is provided, an override for | ||
| 1972 | + `open` is also required. If `autoClose` is `true`, an override for `close` is | ||
| 1973 | + also required. | ||
| 1966 | 1974 | ||
| 1967 | 1975 | ```mjs | |
| 1968 | 1976 | import { createReadStream } from 'fs'; | |
@@ -2004,6 +2012,12 @@ If `options` is a string, then it specifies the encoding. | |||
| 2004 | 2012 | <!-- YAML | |
| 2005 | 2013 | added: v0.1.31 | |
| 2006 | 2014 | changes: | |
| 2015 | + - version: REPLACEME | ||
| 2016 | + pr-url: https://github.com/nodejs/node/pull/40013 | ||
| 2017 | + description: The `fs` option does not need `open` method if an `fd` was provided. | ||
| 2018 | + - version: REPLACEME | ||
| 2019 | + pr-url: https://github.com/nodejs/node/pull/40013 | ||
| 2020 | + description: The `fs` option does not need `close` method if `autoClose` is `false`. | ||
| 2007 | 2021 | - version: | |
| 2008 | 2022 | - v15.4.0 | |
| 2009 | 2023 | pr-url: https://github.com/nodejs/node/pull/35922 | |
@@ -2067,8 +2081,10 @@ destroyed, like most `Writable` streams. Set the `emitClose` option to | |||
| 2067 | 2081 | By providing the `fs` option it is possible to override the corresponding `fs` | |
| 2068 | 2082 | implementations for `open`, `write`, `writev` and `close`. Overriding `write()` | |
| 2069 | 2083 | without `writev()` can reduce performance as some optimizations (`_writev()`) | |
| 2070 | - will be disabled. When providing the `fs` option, overrides for `open`, | ||
| 2071 | - `close`, and at least one of `write` and `writev` are required. | ||
| 2084 | + will be disabled. When providing the `fs` option, overrides for at least one of | ||
| 2085 | + `write` and `writev` are required. If no `fd` option is supplied, an override | ||
| 2086 | + for `open` is also required. If `autoClose` is `true`, an override for `close` | ||
| 2087 | + is also required. | ||
| 2072 | 2088 | ||
| 2073 | 2089 | Like {fs.ReadStream}, if `fd` is specified, {fs.WriteStream} will ignore the | |
| 2074 | 2090 | `path` argument and will use the specified file descriptor. This means that no | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -120,29 +120,29 @@ function close(stream, err, cb) { | |||
| 120 | 120 | } | |
| 121 | 121 | ||
| 122 | 122 | function importFd(stream, options) { | |
| 123 | - stream.fd = null; | ||
| 124 | - if (options.fd != null) { | ||
| 125 | - if (typeof options.fd === 'number') { | ||
| 126 | - // When fd is a raw descriptor, we must keep our fingers crossed | ||
| 127 | - // that the descriptor won't get closed, or worse, replaced with | ||
| 128 | - // another one | ||
| 129 | - // https://github.com/nodejs/node/issues/35862 | ||
| 130 | - stream.fd = options.fd; | ||
| 131 | - } else if (typeof options.fd === 'object' && | ||
| 132 | - options.fd instanceof FileHandle) { | ||
| 133 | - // When fd is a FileHandle we can listen for 'close' events | ||
| 134 | - if (options.fs) | ||
| 135 | - // FileHandle is not supported with custom fs operations | ||
| 136 | - throw new ERR_METHOD_NOT_IMPLEMENTED('FileHandle with fs'); | ||
| 137 | - stream[kHandle] = options.fd; | ||
| 138 | - stream.fd = options.fd.fd; | ||
| 139 | - stream[kFs] = FileHandleOperations(stream[kHandle]); | ||
| 140 | - stream[kHandle][kRef](); | ||
| 141 | - options.fd.on('close', FunctionPrototypeBind(stream.close, stream)); | ||
| 142 | - } else | ||
| 143 | - throw ERR_INVALID_ARG_TYPE('options.fd', | ||
| 144 | - ['number', 'FileHandle'], options.fd); | ||
| 123 | + if (typeof options.fd === 'number') { | ||
| 124 | + // When fd is a raw descriptor, we must keep our fingers crossed | ||
| 125 | + // that the descriptor won't get closed, or worse, replaced with | ||
| 126 | + // another one | ||
| 127 | + // https://github.com/nodejs/node/issues/35862 | ||
| 128 | + stream[kFs] = options.fs || fs; | ||
| 129 | + return options.fd; | ||
| 130 | + } else if (typeof options.fd === 'object' && | ||
| 131 | + options.fd instanceof FileHandle) { | ||
| 132 | + // When fd is a FileHandle we can listen for 'close' events | ||
| 133 | + if (options.fs) { | ||
| 134 | + // FileHandle is not supported with custom fs operations | ||
| 135 | + throw new ERR_METHOD_NOT_IMPLEMENTED('FileHandle with fs'); | ||
| 136 | + } | ||
| 137 | + stream[kHandle] = options.fd; | ||
| 138 | + stream[kFs] = FileHandleOperations(stream[kHandle]); | ||
| 139 | + stream[kHandle][kRef](); | ||
| 140 | + options.fd.on('close', FunctionPrototypeBind(stream.close, stream)); | ||
| 141 | + return options.fd.fd; | ||
| 145 | 142 | } | |
| 143 | + | ||
| 144 | + throw ERR_INVALID_ARG_TYPE('options.fd', | ||
| 145 | + ['number', 'FileHandle'], options.fd); | ||
| 146 | 146 | } | |
| 147 | 147 | ||
| 148 | 148 | function ReadStream(path, options) { | |
@@ -158,21 +158,29 @@ function ReadStream(path, options) { | |||
| 158 | 158 | options.autoDestroy = false; | |
| 159 | 159 | } | |
| 160 | 160 | ||
| 161 | - this[kFs] = options.fs || fs; | ||
| 161 | + if (options.fd == null) { | ||
| 162 | + this.fd = null; | ||
| 163 | + this[kFs] = options.fs || fs; | ||
| 164 | + validateFunction(this[kFs].open, 'options.fs.open'); | ||
| 162 | 165 | ||
| 163 | - validateFunction(this[kFs].open, 'options.fs.open'); | ||
| 164 | - validateFunction(this[kFs].read, 'options.fs.read'); | ||
| 165 | - validateFunction(this[kFs].close, 'options.fs.close'); | ||
| 166 | + // Path will be ignored when fd is specified, so it can be falsy | ||
| 167 | + this.path = toPathIfFileURL(path); | ||
| 168 | + this.flags = options.flags === undefined ? 'r' : options.flags; | ||
| 169 | + this.mode = options.mode === undefined ? 0o666 : options.mode; | ||
| 170 | + | ||
| 171 | + validatePath(this.path); | ||
| 172 | + } else { | ||
| 173 | + this.fd = getValidatedFd(importFd(this, options)); | ||
| 174 | + } | ||
| 166 | 175 | ||
| 167 | 176 | options.autoDestroy = options.autoClose === undefined ? | |
| 168 | 177 | true : options.autoClose; | |
| 169 | 178 | ||
| 170 | - // Path will be ignored when fd is specified, so it can be falsy | ||
| 171 | - this.path = toPathIfFileURL(path); | ||
| 172 | - this.flags = options.flags === undefined ? 'r' : options.flags; | ||
| 173 | - this.mode = options.mode === undefined ? 0o666 : options.mode; | ||
| 179 | + validateFunction(this[kFs].read, 'options.fs.read'); | ||
| 174 | 180 | ||
| 175 | - importFd(this, options); | ||
| 181 | + if (options.autoDestroy) { | ||
| 182 | + validateFunction(this[kFs].close, 'options.fs.close'); | ||
| 183 | + } | ||
| 176 | 184 | ||
| 177 | 185 | this.start = options.start; | |
| 178 | 186 | this.end = options.end; | |
@@ -187,12 +195,6 @@ function ReadStream(path, options) { | |||
| 187 | 195 | this.pos = this.start; | |
| 188 | 196 | } | |
| 189 | 197 | ||
| 190 | - // If fd has been set, validate, otherwise validate path. | ||
| 191 | - if (this.fd != null) { | ||
| 192 | - this.fd = getValidatedFd(this.fd); | ||
| 193 | - } else { | ||
| 194 | - validatePath(this.path); | ||
| 195 | - } | ||
| 196 | 198 | ||
| 197 | 199 | if (this.end === undefined) { | |
| 198 | 200 | this.end = Infinity; | |
@@ -310,9 +312,23 @@ function WriteStream(path, options) { | |||
| 310 | 312 | // Only buffers are supported. | |
| 311 | 313 | options.decodeStrings = true; | |
| 312 | 314 | ||
| 313 | - this[kFs] = options.fs || fs; | ||
| 315 | + if (options.fd == null) { | ||
| 316 | + this.fd = null; | ||
| 317 | + this[kFs] = options.fs || fs; | ||
| 318 | + validateFunction(this[kFs].open, 'options.fs.open'); | ||
| 319 | + | ||
| 320 | + // Path will be ignored when fd is specified, so it can be falsy | ||
| 321 | + this.path = toPathIfFileURL(path); | ||
| 322 | + this.flags = options.flags === undefined ? 'w' : options.flags; | ||
| 323 | + this.mode = options.mode === undefined ? 0o666 : options.mode; | ||
| 324 | + | ||
| 325 | + validatePath(this.path); | ||
| 326 | + } else { | ||
| 327 | + this.fd = getValidatedFd(importFd(this, options)); | ||
| 328 | + } | ||
| 314 | 329 | ||
| 315 | - validateFunction(this[kFs].open, 'options.fs.open'); | ||
| 330 | + options.autoDestroy = options.autoClose === undefined ? | ||
| 331 | + true : options.autoClose; | ||
| 316 | 332 | ||
| 317 | 333 | if (!this[kFs].write && !this[kFs].writev) { | |
| 318 | 334 | throw new ERR_INVALID_ARG_TYPE('options.fs.write', 'function', | |
@@ -327,7 +343,9 @@ function WriteStream(path, options) { | |||
| 327 | 343 | validateFunction(this[kFs].writev, 'options.fs.writev'); | |
| 328 | 344 | } | |
| 329 | 345 | ||
| 330 | - validateFunction(this[kFs].close, 'options.fs.close'); | ||
| 346 | + if (options.autoDestroy) { | ||
| 347 | + validateFunction(this[kFs].close, 'options.fs.close'); | ||
| 348 | + } | ||
| 331 | 349 | ||
| 332 | 350 | // It's enough to override either, in which case only one will be used. | |
| 333 | 351 | if (!this[kFs].write) { | |
@@ -337,28 +355,12 @@ function WriteStream(path, options) { | |||
| 337 | 355 | this._writev = null; | |
| 338 | 356 | } | |
| 339 | 357 | ||
| 340 | - options.autoDestroy = options.autoClose === undefined ? | ||
| 341 | - true : options.autoClose; | ||
| 342 | - | ||
| 343 | - // Path will be ignored when fd is specified, so it can be falsy | ||
| 344 | - this.path = toPathIfFileURL(path); | ||
| 345 | - this.flags = options.flags === undefined ? 'w' : options.flags; | ||
| 346 | - this.mode = options.mode === undefined ? 0o666 : options.mode; | ||
| 347 | - | ||
| 348 | - importFd(this, options); | ||
| 349 | - | ||
| 350 | 358 | this.start = options.start; | |
| 351 | 359 | this.pos = undefined; | |
| 352 | 360 | this.bytesWritten = 0; | |
| 353 | 361 | this.closed = false; | |
| 354 | 362 | this[kIsPerformingIO] = false; | |
| 355 | 363 | ||
| 356 | - // If fd has been set, validate, otherwise validate path. | ||
| 357 | - if (this.fd != null) { | ||
| 358 | - this.fd = getValidatedFd(this.fd); | ||
| 359 | - } else { | ||
| 360 | - validatePath(this.path); | ||
| 361 | - } | ||
| 362 | 364 | ||
| 363 | 365 | if (this.start !== undefined) { | |
| 364 | 366 | validateInteger(this.start, 'start', 0); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments