| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -158,7 +158,19 @@ function importFd(stream, options) { | |||
| 158 | 158 | stream[kHandle] = options.fd; | |
| 159 | 159 | stream[kFs] = FileHandleOperations(stream[kHandle]); | |
| 160 | 160 | stream[kHandle][kRef](); | |
| 161 | - options.fd.on('close', FunctionPrototypeBind(stream.close, stream)); | ||
| 161 | + | ||
| 162 | + const onclose = FunctionPrototypeBind(stream.close, stream); | ||
| 163 | + options.fd.on('close', onclose); | ||
| 164 | + if (options.autoClose === false) { | ||
| 165 | + function cleanup() { | ||
| 166 | + options.fd.removeListener('close', onclose); | ||
| 167 | + options.fd[kUnref](); | ||
| 168 | + } | ||
| 169 | + stream.once('end', cleanup); | ||
| 170 | + stream.once('finish', cleanup); | ||
| 171 | + stream.once('error', cleanup); | ||
| 172 | + } | ||
| 173 | + | ||
| 162 | 174 | return options.fd.fd; | |
| 163 | 175 | } | |
| 164 | 176 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,46 @@ async function validateRead() { | |||
| 42 | 42 | ); | |
| 43 | 43 | } | |
| 44 | 44 | ||
| 45 | + async function validateReusedCreateReadStream() { | ||
| 46 | + const filePath = path.resolve(tmpDir, 'tmp-reused-stream.txt'); | ||
| 47 | + fs.writeFileSync(filePath, Buffer.from('ab', 'utf8')); | ||
| 48 | + | ||
| 49 | + const fileHandle = await open(filePath, 'r'); | ||
| 50 | + try { | ||
| 51 | + await buffer(fileHandle.createReadStream({ | ||
| 52 | + start: 0, | ||
| 53 | + end: 0, | ||
| 54 | + autoClose: false, | ||
| 55 | + })); | ||
| 56 | + assert.strictEqual(fileHandle.listenerCount('close'), 0); | ||
| 57 | + | ||
| 58 | + await buffer(fileHandle.createReadStream({ | ||
| 59 | + start: 1, | ||
| 60 | + end: 1, | ||
| 61 | + autoClose: false, | ||
| 62 | + })); | ||
| 63 | + assert.strictEqual(fileHandle.listenerCount('close'), 0); | ||
| 64 | + } finally { | ||
| 65 | + await fileHandle.close(); | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + async function validateReusedCreateWriteStream() { | ||
| 70 | + const filePath = path.resolve(tmpDir, 'tmp-reused-write-stream.txt'); | ||
| 71 | + const fileHandle = await open(filePath, 'w'); | ||
| 72 | + try { | ||
| 73 | + const stream = fileHandle.createWriteStream({ autoClose: false }); | ||
| 74 | + stream.end('a'); | ||
| 75 | + await finished(stream); | ||
| 76 | + assert.strictEqual(fileHandle.listenerCount('close'), 0); | ||
| 77 | + } finally { | ||
| 78 | + await fileHandle.close(); | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + | ||
| 45 | 82 | Promise.all([ | |
| 46 | 83 | validateWrite(), | |
| 47 | 84 | validateRead(), | |
| 85 | + validateReusedCreateReadStream(), | ||
| 86 | + validateReusedCreateWriteStream(), | ||
| 48 | 87 | ]).then(common.mustCall()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments