| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a4d6f78 commit 2b0e270
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const kWriteFileMaxChunkSize = 2 ** 14; | |
| 4 | 4 | ||
| 5 | 5 | const { | |
| 6 | + ArrayPrototypePush, | ||
| 6 | 7 | Error, | |
| 7 | 8 | MathMax, | |
| 8 | 9 | MathMin, | |
@@ -293,24 +294,46 @@ async function readFileHandle(filehandle, options) { | |||
| 293 | 294 | if (size > kIoMaxLength) | |
| 294 | 295 | throw new ERR_FS_FILE_TOO_LARGE(size); | |
| 295 | 296 | ||
| 296 | - const chunks = []; | ||
| 297 | - const chunkSize = size === 0 ? | ||
| 298 | - kReadFileMaxChunkSize : | ||
| 299 | - MathMin(size, kReadFileMaxChunkSize); | ||
| 300 | 297 | let endOfFile = false; | |
| 298 | + let totalRead = 0; | ||
| 299 | + const noSize = size === 0; | ||
| 300 | + const buffers = []; | ||
| 301 | + const fullBuffer = noSize ? undefined : Buffer.allocUnsafeSlow(size); | ||
| 301 | 302 | do { | |
| 302 | 303 | if (signal && signal.aborted) { | |
| 303 | 304 | throw lazyDOMException('The operation was aborted', 'AbortError'); | |
| 304 | 305 | } | |
| 305 | - const buf = Buffer.alloc(chunkSize); | ||
| 306 | - const { bytesRead, buffer } = | ||
| 307 | - await read(filehandle, buf, 0, chunkSize, -1); | ||
| 308 | - endOfFile = bytesRead === 0; | ||
| 309 | - if (bytesRead > 0) | ||
| 310 | - chunks.push(buffer.slice(0, bytesRead)); | ||
| 306 | + let buffer; | ||
| 307 | + let offset; | ||
| 308 | + let length; | ||
| 309 | + if (noSize) { | ||
| 310 | + buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength); | ||
| 311 | + offset = 0; | ||
| 312 | + length = kReadFileUnknownBufferLength; | ||
| 313 | + } else { | ||
| 314 | + buffer = fullBuffer; | ||
| 315 | + offset = totalRead; | ||
| 316 | + length = MathMin(size - totalRead, kReadFileBufferLength); | ||
| 317 | + } | ||
| 318 | + | ||
| 319 | + const bytesRead = (await binding.read(filehandle.fd, buffer, offset, | ||
| 320 | + length, -1, kUsePromises)) || 0; | ||
| 321 | + totalRead += bytesRead; | ||
| 322 | + endOfFile = bytesRead === 0 || totalRead === size; | ||
| 323 | + if (noSize && bytesRead > 0) { | ||
| 324 | + const isBufferFull = bytesRead === kReadFileUnknownBufferLength; | ||
| 325 | + const chunkBuffer = isBufferFull ? buffer : buffer.slice(0, bytesRead); | ||
| 326 | + ArrayPrototypePush(buffers, chunkBuffer); | ||
| 327 | + } | ||
| 311 | 328 | } while (!endOfFile); | |
| 312 | 329 | ||
| 313 | - const result = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks); | ||
| 330 | + let result; | ||
| 331 | + if (size > 0) { | ||
| 332 | + result = totalRead === size ? fullBuffer : fullBuffer.slice(0, totalRead); | ||
| 333 | + } else { | ||
| 334 | + result = buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, | ||
| 335 | + totalRead); | ||
| 336 | + } | ||
| 314 | 337 | ||
| 315 | 338 | return options.encoding ? result.toString(options.encoding) : result; | |
| 316 | 339 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,7 @@ const { | |||
| 11 | 11 | open, | |
| 12 | 12 | readFile, | |
| 13 | 13 | writeFile, | |
| 14 | - truncate | ||
| 14 | + truncate, | ||
| 15 | 15 | } = fs.promises; | |
| 16 | 16 | const path = require('path'); | |
| 17 | 17 | const tmpdir = require('../common/tmpdir'); | |
@@ -65,6 +65,7 @@ async function doReadAndCancel() { | |||
| 65 | 65 | await assert.rejects(readFile(fileHandle, { signal }), { | |
| 66 | 66 | name: 'AbortError' | |
| 67 | 67 | }); | |
| 68 | + await fileHandle.close(); | ||
| 68 | 69 | } | |
| 69 | 70 | ||
| 70 | 71 | // Signal aborted on first tick | |
@@ -75,10 +76,11 @@ async function doReadAndCancel() { | |||
| 75 | 76 | fs.writeFileSync(filePathForHandle, buffer); | |
| 76 | 77 | const controller = new AbortController(); | |
| 77 | 78 | const { signal } = controller; | |
| 78 | - tick(1, () => controller.abort()); | ||
| 79 | + process.nextTick(() => controller.abort()); | ||
| 79 | 80 | await assert.rejects(readFile(fileHandle, { signal }), { | |
| 80 | 81 | name: 'AbortError' | |
| 81 | - }); | ||
| 82 | + }, 'tick-0'); | ||
| 83 | + await fileHandle.close(); | ||
| 82 | 84 | } | |
| 83 | 85 | ||
| 84 | 86 | // Signal aborted right before buffer read | |
@@ -91,10 +93,12 @@ async function doReadAndCancel() { | |||
| 91 | 93 | ||
| 92 | 94 | const controller = new AbortController(); | |
| 93 | 95 | const { signal } = controller; | |
| 94 | - tick(2, () => controller.abort()); | ||
| 96 | + tick(1, () => controller.abort()); | ||
| 95 | 97 | await assert.rejects(fileHandle.readFile({ signal, encoding: 'utf8' }), { | |
| 96 | 98 | name: 'AbortError' | |
| 97 | - }); | ||
| 99 | + }, 'tick-1'); | ||
| 100 | + | ||
| 101 | + await fileHandle.close(); | ||
| 98 | 102 | } | |
| 99 | 103 | ||
| 100 | 104 | // Validate file size is within range for reading | |
@@ -112,6 +116,7 @@ async function doReadAndCancel() { | |||
| 112 | 116 | name: 'RangeError', | |
| 113 | 117 | code: 'ERR_FS_FILE_TOO_LARGE' | |
| 114 | 118 | }); | |
| 119 | + await fileHandle.close(); | ||
| 115 | 120 | } | |
| 116 | 121 | } | |
| 117 | 122 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments