Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Node.js readRange bug #3062

Merged
merged 4 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions io/js/src/main/scala/fs2/io/file/FileHandlePlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,48 @@ private[file] trait FileHandleCompanionPlatform {
new FileHandle[F] {

override def force(metaData: Boolean): F[Unit] =
F.fromPromise(F.delay(fd.datasync()))
F.fromPromise(F.delay(fd.datasync())).adaptError { case IOException(ex) => ex }

override def read(numBytes: Int, offset: Long): F[Option[Chunk[Byte]]] =
F.fromPromise(
F.delay(fd.read(new Uint8Array(numBytes), 0, numBytes, js.BigInt(offset.toString)))
).map { res =>
if (res.bytesRead == 0)
if (numBytes > 0) None else Some(Chunk.empty)
else
Some(Chunk.uint8Array(res.buffer).take(res.bytesRead))
}
F.async_[Option[Chunk[Byte]]] { cb =>
facade.fs.read(
fd.fd,
new Uint8Array(numBytes),
0,
numBytes,
js.BigInt(offset.toString),
(err, bytesRead, buffer) =>
cb {
Option(err).map(js.JavaScriptException(_)).toLeft {
if (bytesRead == 0)
if (numBytes > 0) None else Some(Chunk.empty)
else
Some(Chunk.uint8Array(buffer).take(bytesRead))
}
}
)
}.adaptError { case IOException(ex) => ex }

override def size: F[Long] =
F.fromPromise(F.delay(fd.stat(new facade.fs.StatOptions { bigint = true })))
.map(_.size.toString.toLong)
.adaptError { case IOException(ex) => ex }

override def truncate(size: Long): F[Unit] =
F.fromPromise(F.delay(fd.truncate(size.toDouble)))
.adaptError { case IOException(ex) => ex }

override def write(bytes: Chunk[Byte], offset: Long): F[Int] =
F.fromPromise(
F.delay(fd.write(bytes.toUint8Array, 0, bytes.size, js.BigInt(offset.toString)))
).map(_.bytesWritten.toInt)
F.async_[Int] { cb =>
facade.fs.write(
fd.fd,
bytes.toUint8Array,
0,
bytes.size,
js.BigInt(offset.toString),
(err, bytesWritten, _) =>
cb(Option(err).map(js.JavaScriptException(_)).toLeft(bytesWritten))
)
}.adaptError { case IOException(ex) => ex }
}
}
38 changes: 24 additions & 14 deletions io/js/src/main/scala/fs2/io/internal/facade/fs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ package object fs {
private[io] def createWriteStream(path: String, options: WriteStreamOptions): fs2.io.Writable =
js.native

@js.native
@JSImport("fs", "read")
private[io] def read(
fd: Int,
buffer: Uint8Array,
offset: Int,
length: Int,
position: js.BigInt,
cb: js.Function3[js.Error, Int, Uint8Array, Unit]
): Unit = js.native

@js.native
@JSImport("fs", "write")
private[io] def write(
fd: Int,
buffer: Uint8Array,
offset: Int,
length: Int,
position: js.BigInt,
cb: js.Function3[js.Error, Int, Uint8Array, Unit]
): Unit = js.native

}

package fs {
Expand Down Expand Up @@ -213,21 +235,9 @@ package fs {
@js.native
private[io] trait FileHandle extends js.Object {

def datasync(): js.Promise[Unit] = js.native
def fd: Int = js.native

def read(
buffer: Uint8Array,
offset: Int,
length: Int,
position: js.BigInt
): js.Promise[FileHandleReadResult] = js.native

def write(
buffer: Uint8Array,
offset: Int,
length: Int,
position: js.BigInt
): js.Promise[FileHandleWriteResult] = js.native
def datasync(): js.Promise[Unit] = js.native

def stat(options: StatOptions): js.Promise[BigIntStats] = js.native

Expand Down
7 changes: 3 additions & 4 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
test("reads half of a file") {
Stream
.resource(tempFile.evalMap(modify))
.flatMap(path => Files[IO].readRange(path, 4096, 0, 2))
.map(_ => 1)
.flatMap(path => Files[IO].readRange(path, 4096, 2, 4))
.compile
.foldMonoid
.assertEquals(2)
.toList
.assertEquals(List[Byte](2, 3))
}
test("reads full file if end is bigger than file size") {
Stream
Expand Down