| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c81f894 commit 0b6af91
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,10 @@ const kMode = Symbol('kMode'); | |||
| 28 | 28 | const kPosition = Symbol('kPosition'); | |
| 29 | 29 | const kClosed = Symbol('kClosed'); | |
| 30 | 30 | ||
| 31 | + function isCurrentPosition(position) { | ||
| 32 | + return position === null || position === undefined || position === -1; | ||
| 33 | + } | ||
| 34 | + | ||
| 31 | 35 | /** | |
| 32 | 36 | * Base class for virtual file handles. | |
| 33 | 37 | * Provides the interface that file handles must implement. | |
@@ -467,8 +471,8 @@ class MemoryFileHandle extends VirtualFileHandle { | |||
| 467 | 471 | ||
| 468 | 472 | // Get content (resolves dynamic content providers) | |
| 469 | 473 | const content = this.content; | |
| 470 | - const readPos = position !== null && position !== undefined ? | ||
| 471 | - Number(position) : this.position; | ||
| 474 | + const useCurrentPosition = isCurrentPosition(position); | ||
| 475 | + const readPos = useCurrentPosition ? this.position : Number(position); | ||
| 472 | 476 | const available = content.length - readPos; | |
| 473 | 477 | ||
| 474 | 478 | if (available <= 0) { | |
@@ -479,7 +483,7 @@ class MemoryFileHandle extends VirtualFileHandle { | |||
| 479 | 483 | content.copy(buffer, offset, readPos, readPos + bytesToRead); | |
| 480 | 484 | ||
| 481 | 485 | // Update position if not using explicit position | |
| 482 | - if (position === null || position === undefined) { | ||
| 486 | + if (useCurrentPosition) { | ||
| 483 | 487 | this.position = readPos + bytesToRead; | |
| 484 | 488 | } | |
| 485 | 489 | ||
@@ -512,10 +516,10 @@ class MemoryFileHandle extends VirtualFileHandle { | |||
| 512 | 516 | this.#checkWritable(); | |
| 513 | 517 | ||
| 514 | 518 | // In append mode, always write at the end | |
| 519 | + const useCurrentPosition = isCurrentPosition(position); | ||
| 515 | 520 | const writePos = this.#isAppend() ? | |
| 516 | 521 | this.#size : | |
| 517 | - (position !== null && position !== undefined ? | ||
| 518 | - Number(position) : this.position); | ||
| 522 | + (useCurrentPosition ? this.position : Number(position)); | ||
| 519 | 523 | const data = buffer.subarray(offset, offset + length); | |
| 520 | 524 | ||
| 521 | 525 | // Expand buffer if needed (geometric doubling for amortized O(1) appends) | |
@@ -544,7 +548,7 @@ class MemoryFileHandle extends VirtualFileHandle { | |||
| 544 | 548 | } | |
| 545 | 549 | ||
| 546 | 550 | // Update position if not using explicit position | |
| 547 | - if (position === null || position === undefined) { | ||
| 551 | + if (useCurrentPosition) { | ||
| 548 | 552 | this.position = writePos + length; | |
| 549 | 553 | } | |
| 550 | 554 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,6 +80,26 @@ const vfs = require('node:vfs'); | |||
| 80 | 80 | myVfs.closeSync(fd); | |
| 81 | 81 | } | |
| 82 | 82 | ||
| 83 | + // Test readSync with Node's current-position sentinel | ||
| 84 | + { | ||
| 85 | + const myVfs = vfs.create(); | ||
| 86 | + myVfs.writeFileSync('/file.txt', 'hello world'); | ||
| 87 | + | ||
| 88 | + const fd = myVfs.openSync('/file.txt'); | ||
| 89 | + const buffer1 = Buffer.alloc(5); | ||
| 90 | + const buffer2 = Buffer.alloc(6); | ||
| 91 | + | ||
| 92 | + let bytesRead = myVfs.readSync(fd, buffer1, 0, 5, -1); | ||
| 93 | + assert.strictEqual(bytesRead, 5); | ||
| 94 | + assert.strictEqual(buffer1.toString(), 'hello'); | ||
| 95 | + | ||
| 96 | + bytesRead = myVfs.readSync(fd, buffer2, 0, 6, -1); | ||
| 97 | + assert.strictEqual(bytesRead, 6); | ||
| 98 | + assert.strictEqual(buffer2.toString(), ' world'); | ||
| 99 | + | ||
| 100 | + myVfs.closeSync(fd); | ||
| 101 | + } | ||
| 102 | + | ||
| 83 | 103 | // Test readSync with explicit position | |
| 84 | 104 | { | |
| 85 | 105 | const myVfs = vfs.create(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,6 +82,12 @@ myVfs.writeFileSync('/file.txt', 'hello world'); | |||
| 82 | 82 | myVfs.writeSync(fd, buf, 0, 3, 0); | |
| 83 | 83 | myVfs.closeSync(fd); | |
| 84 | 84 | ||
| 85 | + const fd3 = myVfs.openSync('/sync-current.txt', 'w'); | ||
| 86 | + myVfs.writeSync(fd3, Buffer.from('abc'), 0, 3, -1); | ||
| 87 | + myVfs.writeSync(fd3, Buffer.from('def'), 0, 3, -1); | ||
| 88 | + myVfs.closeSync(fd3); | ||
| 89 | + assert.strictEqual(myVfs.readFileSync('/sync-current.txt', 'utf8'), 'abcdef'); | ||
| 90 | + | ||
| 85 | 91 | const fd2 = myVfs.openSync('/sync.txt', 'r'); | |
| 86 | 92 | const out = Buffer.alloc(3); | |
| 87 | 93 | myVfs.readSync(fd2, out, 0, 3, 0); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,6 +30,19 @@ myVfs.mount(mountPoint); | |||
| 30 | 30 | fs.closeSync(fd); | |
| 31 | 31 | } | |
| 32 | 32 | ||
| 33 | + // readSync with position null uses and advances the current file position | ||
| 34 | + { | ||
| 35 | + const fd = fs.openSync(path.join(mountPoint, 'src/hello.txt'), 'r'); | ||
| 36 | + const b1 = Buffer.alloc(5); | ||
| 37 | + const b2 = Buffer.alloc(6); | ||
| 38 | + | ||
| 39 | + assert.strictEqual(fs.readSync(fd, b1, 0, 5, null), 5); | ||
| 40 | + assert.strictEqual(fs.readSync(fd, b2, 0, 6, null), 6); | ||
| 41 | + assert.strictEqual(b1.toString(), 'hello'); | ||
| 42 | + assert.strictEqual(b2.toString(), ' world'); | ||
| 43 | + fs.closeSync(fd); | ||
| 44 | + } | ||
| 45 | + | ||
| 33 | 46 | // openSync + writeSync (buffer) + closeSync | |
| 34 | 47 | { | |
| 35 | 48 | const fd = fs.openSync(path.join(mountPoint, 'src/wfd.txt'), 'w'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments