Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Add backword compatibility for ease of migration #101

Merged
merged 1 commit into from
Oct 6, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
*
* @see [[https://nodejs.org/api/buffer.html#buffer_buf_entries]]
*/
def entries(): js.Iterator[js.Array[Int]] = js.native
def entries(): io.scalajs.collection.Iterator[js.Array[Int]] = js.native

/**
* Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise.
Expand Down Expand Up @@ -200,7 +200,7 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
* @return an [[Iterator]]
* @example buf.keys()
*/
def keys(): js.Iterator[Int] = js.native
def keys(): io.scalajs.collection.Iterator[Int] = js.native

/**
* The largest size allowed for a single Buffer instance.
Expand Down Expand Up @@ -547,7 +547,7 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
* @return an iterator for buf values (bytes)
* @example buf.values()
*/
def values(): js.Iterator[Int] = js.native
def values(): io.scalajs.collection.Iterator[Int] = js.native

/**
* Writes string to buf at offset according to the character encoding in encoding. The length parameter is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ package object buffer {
* @return the hex-formatted string
*/
@inline
def toHexString: String = buffer.entries().toIterator.flatMap(_.lastOption).map(n => f"$n%02x").mkString
def toHexString: String =
js.Iterator.IteratorOps(buffer.entries()).toIterator.flatMap(_.lastOption).map(n => f"$n%02x").mkString
}

/**
Expand Down
88 changes: 48 additions & 40 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,83 +34,87 @@ package object fs {

/**
* File System Extensions
* @param fs the given [[Fs file system]] instance
* @param instance the given [[Fs file system]] instance
*/
implicit final class FsExtensions(private val fs: Fs) extends AnyVal {
implicit final class FsExtensions(private val instance: Fs) extends AnyVal {
@deprecated("Use Fs directly instead of Fs.fs", "0.9.0")
@inline
def fs: Fs = instance

@inline
def accessFuture(path: Buffer | String): Future[Unit] = {
promiseWithError0[FileIOError](fs.access(path, _))
promiseWithError0[FileIOError](instance.access(path, _))
}

@inline
def accessFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
promiseWithError0[FileIOError](fs.access(path, mode, _))
promiseWithError0[FileIOError](instance.access(path, mode, _))
}

@inline
def appendFileFuture(file: Buffer | FileDescriptor | String,
data: Buffer | String,
options: FileAppendOptions = null): Future[Unit] = {
promiseWithError0[FileIOError](fs.appendFile(file, data, options, _))
promiseWithError0[FileIOError](instance.appendFile(file, data, options, _))
}

@inline
def chmodFuture(path: Buffer | String, mode: FileMode, callback: js.Function1[FileIOError, Any]): Future[Unit] = {
promiseWithError0[FileIOError](fs.chmod(path, mode, _))
promiseWithError0[FileIOError](instance.chmod(path, mode, _))
}

@inline
def closeFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](fs.close(fd, _))
def closeFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.close(fd, _))

@inline
def existsFuture(path: String): Future[Boolean] = promiseWithErrorAsBoolean[FileIOError](fs.access(path, _))
def existsFuture(path: String): Future[Boolean] = promiseWithErrorAsBoolean[FileIOError](instance.access(path, _))

@inline
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](fs.fdatasync(fd, _))
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.fdatasync(fd, _))

@inline
def futimesFuture(fd: FileDescriptor, atime: Time, mtime: Time): Future[Unit] = {
promiseWithError0[FileIOError](fs.futimes(fd, atime, mtime, _))
promiseWithError0[FileIOError](instance.futimes(fd, atime, mtime, _))
}

@inline
def lchmodFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
promiseWithError0[FileIOError](fs.lchmod(path, mode, _))
promiseWithError0[FileIOError](instance.lchmod(path, mode, _))
}

@inline
def lchownFuture(path: Buffer | String, uid: UID, gid: GID): Future[Unit] = {
promiseWithError0[FileIOError](fs.lchown(path, uid, gid, _))
promiseWithError0[FileIOError](instance.lchown(path, uid, gid, _))
}

@inline
def linkFuture(srcpath: Buffer | String, dstpath: Buffer | String): Future[Unit] = {
promiseWithError0[FileIOError](fs.link(srcpath, dstpath, _))
promiseWithError0[FileIOError](instance.link(srcpath, dstpath, _))
}

@inline
def mkdirFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
promiseWithError0[FileIOError](fs.mkdir(path, mode, _))
promiseWithError0[FileIOError](instance.mkdir(path, mode, _))
}

@inline
def mkdirFuture(path: Buffer | String): Future[Unit] = {
promiseWithError0[FileIOError](fs.mkdir(path, _))
promiseWithError0[FileIOError](instance.mkdir(path, _))
}

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
@inline
def mkdirFuture(path: Buffer | String, options: MkdirOptions): Future[Unit] = {
promiseWithError0[FileIOError](fs.mkdir(path, options, _))
promiseWithError0[FileIOError](instance.mkdir(path, options, _))
}

@inline
def openFuture(path: Buffer | String, flags: Flags, mode: FileMode): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](fs.open(path, flags, mode, _))
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, mode, _))
}
@inline
def openFuture(path: Buffer | String, flags: Flags): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](fs.open(path, flags, _))
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, _))
}

@inline
Expand All @@ -125,15 +129,15 @@ package object fs {
@inline
def readdirFuture(path: Buffer | String, options: String = "utf8"): Future[js.Array[String]] = {
val callback: FsCallback1[js.Array[String]] => Unit = { callback =>
fs.readdir(path, options, callback.asInstanceOf[FsCallback1[ReaddirArrays]])
instance.readdir(path, options, callback.asInstanceOf[FsCallback1[ReaddirArrays]])
}
promiseWithError1[FileIOError, js.Array[String]](callback)
}

@inline
def readdirBufferFuture(path: Buffer | String): Future[js.Array[Buffer]] = {
val callback: FsCallback1[js.Array[Buffer]] => Unit = { callback =>
fs.readdir(
instance.readdir(
path,
new FileEncodingOptions(encoding = "buffer"),
callback.asInstanceOf[FsCallback1[ReaddirArrays]]
Expand All @@ -146,61 +150,65 @@ package object fs {
@inline
def readdirDirentFuture(path: Buffer | String): Future[js.Array[Dirent]] = {
val callback: FsCallback1[js.Array[Dirent]] => Unit = { callback =>
fs.readdir(path, new ReaddirOptions(withFileTypes = true), callback.asInstanceOf[FsCallback1[ReaddirArrays2]])
instance.readdir(
path,
new ReaddirOptions(withFileTypes = true),
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
)
}
promiseWithError1[FileIOError, js.Array[Dirent]](callback)
}

@inline
def readFileFuture(file: String, options: ReadFileOptions = null): Future[Output] = {
promiseWithError1[FileIOError, Output](fs.readFile(file, options, _))
promiseWithError1[FileIOError, Output](instance.readFile(file, options, _))
}

@inline
def renameFuture(oldPath: String, newPath: String): Future[Unit] = {
promiseWithError0[FileIOError](fs.rename(oldPath, newPath, _))
promiseWithError0[FileIOError](instance.rename(oldPath, newPath, _))
}

@inline
def realpathFuture(path: String): Future[String] = {
promiseWithError1[FileIOError, String](fs.realpath(path, _))
promiseWithError1[FileIOError, String](instance.realpath(path, _))
}

@inline
def realpathFuture(path: String, options: FileEncodingOptions): Future[Output] = {
promiseWithError1[FileIOError, Output](fs.realpath(path, options, _))
promiseWithError1[FileIOError, Output](instance.realpath(path, options, _))
}

@inline
def rmdirFuture(path: Buffer | String): Future[Unit] = promiseWithError0[FileIOError](fs.rmdir(path, _))
def rmdirFuture(path: Buffer | String): Future[Unit] = promiseWithError0[FileIOError](instance.rmdir(path, _))

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
@inline
def rmdirFuture(path: Buffer | String, options: RmdirOptions): Future[Unit] =
promiseWithError0[FileIOError](fs.rmdir(path, options, _))
promiseWithError0[FileIOError](instance.rmdir(path, options, _))

@inline
def statFuture(path: String): Future[Stats] = promiseWithError1[FileIOError, Stats](fs.stat(path, _))
def statFuture(path: String): Future[Stats] = promiseWithError1[FileIOError, Stats](instance.stat(path, _))

@inline
def symlinkFuture(target: Buffer | String, path: Buffer | String, `type`: String = null): Future[Unit] = {
promiseWithError0[FileIOError](fs.symlink(target, path, `type`, _))
promiseWithError0[FileIOError](instance.symlink(target, path, `type`, _))
}

@inline
def unlinkFuture(path: Buffer | String): Future[Unit] = promiseWithError0[FileIOError](fs.unlink(path, _))
def unlinkFuture(path: Buffer | String): Future[Unit] = promiseWithError0[FileIOError](instance.unlink(path, _))

@inline
def unwatchFileFuture(filename: Buffer | String): Future[Unit] =
promiseWithError0[FileIOError](fs.unwatchFile(filename, _))
promiseWithError0[FileIOError](instance.unwatchFile(filename, _))

@inline
def utimesFuture(path: Buffer | String, atime: Int, mtime: Int): Future[Unit] =
promiseWithError0[FileIOError](fs.utimes(path, atime, mtime, _))
promiseWithError0[FileIOError](instance.utimes(path, atime, mtime, _))

@inline
def watchFuture(filename: String, options: FSWatcherOptions = null): Future[(String, String)] = {
promiseCallback2[String, String](fs.watch(filename, options, _))
promiseCallback2[String, String](instance.watch(filename, options, _))
}

@inline
Expand All @@ -209,37 +217,37 @@ package object fs {
offset: Int | Null = null,
length: Int | Null = null,
position: Int | Null = null): Future[(FileType, Buffer)] = {
promiseWithError2[FileIOError, Int, Buffer](fs.write(fd, buffer, offset, length, position, _))
promiseWithError2[FileIOError, Int, Buffer](instance.write(fd, buffer, offset, length, position, _))
}

@inline
def writeFuture(fd: FileDescriptor, string: String, position: Int, encoding: String): Future[(FileType, String)] = {
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, position, encoding, _))
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, position, encoding, _))
}

@inline
def writeFuture(fd: FileDescriptor, string: String, position: Int): Future[(FileType, String)] = {
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, position, null, _))
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, position, null, _))
}

@inline
def writeFuture(fd: FileDescriptor, string: String): Future[(FileType, String)] = {
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, _))
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, _))
}

@inline
def writeFileFuture(file: String, data: Buffer, options: FileWriteOptions = null): Future[Unit] = {
promiseWithError0[FileIOError](fs.writeFile(file, data, options, _))
promiseWithError0[FileIOError](instance.writeFile(file, data, options, _))
}

@inline
def writeFileFuture(file: String, data: String, options: FileWriteOptions): Future[Unit] = {
promiseWithError0[FileIOError](fs.writeFile(file, data, options, _))
promiseWithError0[FileIOError](instance.writeFile(file, data, options, _))
}

@inline
def writeFileFuture(file: String, data: String): Future[Unit] = {
promiseWithError0[FileIOError](fs.writeFile(file, data, _))
promiseWithError0[FileIOError](instance.writeFile(file, data, _))
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/current/src/main/scala/io/scalajs/nodejs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ package object nodejs {

type FileIOError = SystemError

type FileMode = Int
type FileMode = Int | Null

type FileType = Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ trait Environment extends js.Object {
@JSBracketAccess
def update(key: String, value: String): Unit = js.native
}

object Environment {
implicit final class EnvironmentExtension(private val env: Environment) extends AnyVal {
@deprecated("Use env(key)", "0.9.0")
@inline def get(key: String): Option[String] = env(key).toOption

@deprecated("Use env(key)", "0.9.0")
@inline def contains(key: String): Boolean = env(key).isDefined
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class URLSearchParams() extends js.Object {
* a JavaScript Array. The first item of the Array is the name, the second item of the Array is the value.
* @return an iterable of an array of results
*/
def entries(): js.Iterator[js.Tuple2[String, String]] = js.native
def entries(): io.scalajs.collection.Iterator[js.Tuple2[String, String]] = js.native

/**
* Iterates over each name-value pair in the query and invokes the given function.
Expand Down Expand Up @@ -96,11 +96,7 @@ class URLSearchParams() extends js.Object {
*/
def has(name: String): Boolean = js.native

/**
* Returns an ES6 Iterator over the names of each name-value pair.
* @return an [[js.Iterator Iterator]] over the names of each name-value pair.
*/
def keys(): js.Iterator[String] = js.native
def keys(): io.scalajs.collection.Iterator[String] = js.native

/**
* Sets the value in the URLSearchParams object associated with name to value. If there are any pre-existing
Expand All @@ -119,8 +115,7 @@ class URLSearchParams() extends js.Object {

/**
* Returns an ES6 Iterator over the values of each name-value pair.
* @return an [[js.Iterator Iterator]] over the values of each name-value pair.
*/
def values(): js.Iterator[String] = js.native
def values(): io.scalajs.collection.Iterator[String] = js.native

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class BufferTest extends FunSpec {
val buf = Buffer.from("Hello!")
val it = buf.entries()
assert(
it.toIterator.toSeq.map(_.toSeq) === Seq(
// TODO: Use it.toIterator once io.scalajs.colletion.Iterator removed.
js.Iterator.IteratorOps(it).toIterator.toSeq.map(_.toSeq) === Seq(
Seq(0, 72),
Seq(1, 101),
Seq(2, 108),
Expand Down
Loading