Skip to content
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
36 changes: 36 additions & 0 deletions io/js/src/main/scala/fs2/io/NodeStream.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package fs2.io

import scala.scalajs.js

/** A facade for Node.js `stream.Readable`. Cast to/from your own bindings.
* @see [[https://nodejs.org/api/stream.html]]
*/
@js.native
sealed trait Readable extends js.Object

/** A facade for Node.js `stream.Writable`. Cast to/from your own bindings.
* @see [[https://nodejs.org/api/stream.html]]
*/
@js.native
sealed trait Writable extends js.Object
25 changes: 13 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 @@ -23,12 +23,11 @@ package fs2
package io
package file

import fs2.internal.jsdeps.node.fsPromisesMod
import cats.effect.kernel.Async
import cats.syntax.all._
import scala.scalajs.js.typedarray.ArrayBuffer
import scala.scalajs.js.typedarray.TypedArrayBuffer
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
import fs2.internal.jsdeps.node.fsPromisesMod

import scala.scalajs.js.typedarray.Uint8Array

private[file] trait FileHandlePlatform[F[_]]

Expand All @@ -42,12 +41,13 @@ private[file] trait FileHandleCompanionPlatform {
F.fromPromise(F.delay(fsPromisesMod.fdatasync(fd)))

override def read(numBytes: Int, offset: Long): F[Option[Chunk[Byte]]] =
F.fromPromise(F.delay(fd.read(new ArrayBuffer(numBytes), offset.toDouble, numBytes))).map {
res =>
if (res.bytesRead < 0) None
else if (res.bytesRead == 0) Some(Chunk.empty)
else
Some(Chunk.byteBuffer(TypedArrayBuffer.wrap(res.buffer).limit(res.bytesRead.toInt)))
F.fromPromise(
F.delay(fd.read(new Uint8Array(numBytes), 0, numBytes.toDouble, offset.toDouble))
).map { res =>
if (res.bytesRead < 0) None
else if (res.bytesRead == 0) Some(Chunk.empty)
else
Some(Chunk.uint8Array(res.buffer).take(res.bytesRead.toInt))
}

override def size: F[Long] =
Expand All @@ -57,7 +57,8 @@ private[file] trait FileHandleCompanionPlatform {
F.fromPromise(F.delay(fd.truncate(size.toDouble)))

override def write(bytes: Chunk[Byte], offset: Long): F[Int] =
F.fromPromise(F.delay(fd.write(bytes.toByteBuffer.arrayBuffer(), offset.toDouble)))
.map(_.bytesWritten.toInt)
F.fromPromise(
F.delay(fd.write(bytes.toUint8Array, 0, bytes.size.toDouble, offset.toDouble))
).map(_.bytesWritten.toInt)
}
}
57 changes: 44 additions & 13 deletions io/js/src/main/scala/fs2/io/file/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,57 @@
*/

package fs2
package io.file
package io
package file

import fs2.internal.jsdeps.node.fsMod.PathLike
import fs2.io.internal.ByteChunkOps._
import cats.kernel.Hash
import cats.kernel.Monoid
import cats.kernel.Order
import fs2.internal.jsdeps.node.fsMod
import fs2.internal.jsdeps.node.osMod
import fs2.internal.jsdeps.node.pathMod
import CollectionCompat._

sealed abstract class Path {
private[file] def toPathLike: PathLike
final class Path(private val path: String) extends AnyVal {
def basename: Path = Path(pathMod.basename(path))
def basename(ext: String): Path = Path(pathMod.basename(path, ext))
def dirname: Path = Path(pathMod.dirname(path))
def extname: String = pathMod.extname(path)
def isAbsolute: Boolean = pathMod.isAbsolute(path)
def normalize: Path = Path(pathMod.normalize(path))
def relativeTo(that: Path): Path = Path(pathMod.relative(this.path, that.path))

def /(that: Path): Path = Path.join(this, that)

override def toString: String = path

private[file] def toJS: fsMod.PathLike = path.asInstanceOf[fsMod.PathLike]
}

object Path {
def apply(path: String): Path = new Path(path)

def apply(path: String): Path = StringPath(path)
def apply(path: Chunk[Byte]): Path = BufferPath(path)
def join(paths: Path*): Path = Path(pathMod.join(paths.map(_.path): _*))
def resolve(paths: Path*): Path = Path(pathMod.resolve(paths.map(_.path): _*))

private final case class StringPath(path: String) extends Path {
override private[file] def toPathLike: PathLike = path.asInstanceOf[PathLike]
}
val empty = Path("")
val tmpdir = Path(osMod.tmpdir())

private final case class BufferPath(path: Chunk[Byte]) extends Path {
override private[file] def toPathLike: PathLike = path.toBuffer.asInstanceOf[PathLike]
}
implicit def instances: Monoid[Path] with Order[Path] with Hash[Path] = algebra

private object algebra extends Monoid[Path] with Order[Path] with Hash[Path] {

override def empty: Path = Path.empty

override def combine(x: Path, y: Path): Path = x / y

override def combineAll(as: IterableOnce[Path]): Path = Path.join(as.toSeq: _*)

override def eqv(x: Path, y: Path): Boolean = x.path == y.path

override def compare(x: Path, y: Path): Int = x.path.compare(y.path)

override def hash(x: Path): Int = x.path.hashCode()

}
}
Loading