Skip to content
Open
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.typesafe.tools.mima.core._

Global / onChangedBuildSource := ReloadOnSourceChanges

ThisBuild / tlBaseVersion := "3.12"
ThisBuild / tlBaseVersion := "3.13"

ThisBuild / organization := "co.fs2"
ThisBuild / organizationName := "Functional Streams for Scala"
Expand Down
62 changes: 35 additions & 27 deletions io/js/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -416,36 +416,44 @@ private[fs2] trait FilesCompanionPlatform {
.chunkN(options.chunkSize)
.flatMap(Stream.chunk)
}

override def writeAll(path: Path, _flags: Flags): Pipe[F, Byte, Nothing] =
in =>
in.through {
writeWritable(
F.async_[Writable] { cb =>
val ws = facade.fs
.createWriteStream(
path.toString,
new facade.fs.WriteStreamOptions {
flags = combineFlags(_flags)
in.pull.stepLeg.flatMap {
case None => Pull.done
case Some(leg) =>
Stream
.eval(F.async_[Writable] { cb =>
val ws = facade.fs
.createWriteStream(
path.toString,
new facade.fs.WriteStreamOptions {
flags = combineFlags(_flags)
}
)
ws.once[Unit](
"ready",
_ => {
ws.removeAllListeners()
cb(Right(ws))
}
)
ws.once[Unit](
"ready",
_ => {
ws.removeAllListeners()
cb(Right(ws))
}
)
ws.once[js.Error](
"error",
error => {
ws.removeAllListeners()
cb(Left(js.JavaScriptException(error)))
}
)
()
}
)
}
ws.once[js.Error](
"error",
error => {
ws.removeAllListeners()
cb(Left(js.JavaScriptException(error)))
}
)
()
})
.flatMap { ws =>
leg.stream
.cons(leg.head)
.through(writeWritable(F.pure(ws)))
}
.pull
.echo
}.stream

}
}
14 changes: 11 additions & 3 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,17 @@ object Files extends FilesCompanionPlatform with FilesLowPriority {
flags: Flags
): Pipe[F, Byte, Nothing] =
in =>
Stream
.resource(writeCursor(path, flags))
.flatMap(_.writeAll(in).void.stream)
in.pull.stepLeg.flatMap {
case None => Pull.done
case Some(leg) =>
Stream
.resource(writeCursor(path, flags))
.flatMap { cursor =>
cursor.writeAll(leg.stream.cons(leg.head)).void.stream
}
.pull
.echo
}.stream

def writeCursor(
path: Path,
Expand Down
30 changes: 28 additions & 2 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ class FilesSuite extends Fs2Suite with BaseFileSuite {
.compile
.foldMonoid
.assertEquals("""|foo
|bar
|""".stripMargin)
|bar
|""".stripMargin)
}

test("writeUtf8Lines - side effect") {
Expand Down Expand Up @@ -200,6 +200,32 @@ class FilesSuite extends Fs2Suite with BaseFileSuite {
.foldMonoid
.assertEquals("")
}
test("empty stream does not create file") {
Files[IO].tempDirectory.use { dir =>
val path = dir / "should-not-exist.txt"
Stream.empty
.covary[IO]
.through(Files[IO].writeAll(path))
.compile
.drain
.flatMap(_ => Files[IO].exists(path))
.assertEquals(false)
}
}

test("error stream does not create file") {
Files[IO].tempDirectory.use { dir =>
val path = dir / "should-not-exist.txt"
Stream
.raiseError[IO](new RuntimeException("boom"))
.through(Files[IO].writeAll(path))
.compile
.drain
.attempt
.flatMap(_ => Files[IO].exists(path))
.assertEquals(false)
}
}
}

group("tail") {
Expand Down
Loading