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

Commit 84b2992

Browse files
authored
Merge pull request #238 from exoego/fs
[fs] Various updates for Node.js v14
2 parents a3b8817 + 2aa0137 commit 84b2992

File tree

4 files changed

+92
-22
lines changed

4 files changed

+92
-22
lines changed

app/current/src/main/scala/io/scalajs/nodejs/fs/FSWatcher.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.scalajs.nodejs
22
package fs
33

44
import _root_.net.exoego.scalajs.types.util.Factory
5+
import com.thoughtworks.enableIf
56
import io.scalajs.nodejs.events.IEventEmitter
67

78
import scala.scalajs.js
@@ -18,6 +19,24 @@ trait FSWatcher extends IEventEmitter {
1819
* @since 0.5.8
1920
*/
2021
def close(): Unit = js.native
22+
23+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
24+
def ref(): FSWatcher = js.native
25+
26+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
27+
def unref(): FSWatcher = js.native
28+
}
29+
30+
/**
31+
* A successful call to fs.watchFile() method will return a new fs.StatWatcher object.
32+
*/
33+
@js.native
34+
trait FSStatWatcher extends IEventEmitter {
35+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
36+
def ref(): FSStatWatcher = js.native
37+
38+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
39+
def unref(): FSStatWatcher = js.native
2140
}
2241

2342
@Factory

app/current/src/main/scala/io/scalajs/nodejs/fs/Fs.scala

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import _root_.net.exoego.scalajs.types.util.Factory
77

88
import scala.scalajs.js
99
import scala.scalajs.js.annotation.JSImport
10-
import scala.scalajs.js.typedarray
1110
import scala.scalajs.js.|
1211

1312
/**
@@ -430,9 +429,11 @@ trait Fs extends js.Object with FSConstants {
430429
/**
431430
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
432431
* mode defaults to 0o777.
432+
*
433+
* After v13.11.0, in recursive mode, the `callback`` now receives the first created path as an 2nd argument.
433434
* @example fs.mkdir(path[, mode], callback)
434435
*/
435-
def mkdir(path: Path, mode: MkdirOptions, callback: FsCallback0): Unit = js.native
436+
def mkdir(path: Path, mode: MkdirOptions, callback: FsCallback0 | FsRecursiveCallback0): Unit = js.native
436437

437438
/**
438439
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
@@ -448,7 +449,10 @@ trait Fs extends js.Object with FSConstants {
448449
*/
449450
def mkdirSync(path: Path, mode: FileMode = js.native): Unit = js.native
450451

451-
def mkdirSync(path: Path, mode: MkdirOptions): Unit = js.native
452+
/**
453+
* @return After Node.js v13.11.0, in recursive mode, the first created path is returned now. Otherwise undefined
454+
*/
455+
def mkdirSync(path: Path, mode: MkdirOptions): js.UndefOr[Path] = js.native
452456

453457
/**
454458
* Creates a unique temporary directory.
@@ -595,6 +599,9 @@ trait Fs extends js.Object with FSConstants {
595599
callback: FsCallback2[Int, Buffer]
596600
): Unit = js.native
597601

602+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
603+
def read(fd: FileDescriptor, options: ReadOptions, callback: FsCallback2[Int, Buffer]): Unit = js.native
604+
598605
/**
599606
* Synchronous version of fs.read().
600607
* @param fd is the file descriptor
@@ -609,6 +616,17 @@ trait Fs extends js.Object with FSConstants {
609616

610617
def readSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int, position: Int): Int = js.native
611618

619+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
620+
def readv(fd: FileDescriptor,
621+
buffers: js.Array[js.typedarray.ArrayBufferView],
622+
options: ReadOptions,
623+
callback: FsCallback2[Int, js.Array[js.typedarray.ArrayBufferView]]
624+
): Unit = js.native
625+
626+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
627+
def readvSync(fd: FileDescriptor, buffers: js.Array[js.typedarray.ArrayBufferView], options: ReadOptions): Unit =
628+
js.native
629+
612630
/**
613631
* Asynchronous readdir(3). Reads the contents of a directory.
614632
* @param path the path (Buffer | String)
@@ -974,7 +992,7 @@ trait Fs extends js.Object with FSConstants {
974992
* @param options the [[FSWatcherOptions optional settings]]
975993
* @param listener the callback
976994
*/
977-
def watchFile(filename: Path, options: FileWatcherOptions, listener: js.Function2[Stats, Stats, Any]): Unit =
995+
def watchFile(filename: Path, options: FileWatcherOptions, listener: js.Function2[Stats, Stats, Any]): FSStatWatcher =
978996
js.native
979997

980998
/**
@@ -987,7 +1005,7 @@ trait Fs extends js.Object with FSConstants {
9871005
* @param filename the filename (Buffer | String)
9881006
* @param listener the callback
9891007
*/
990-
def watchFile(filename: Path, listener: js.Function2[Stats, Stats, Any]): Unit = js.native
1008+
def watchFile(filename: Path, listener: js.Function2[Stats, Stats, Any]): FSStatWatcher = js.native
9911009

9921010
/**
9931011
* Write buffer to the file specified by fd.
@@ -1007,7 +1025,7 @@ trait Fs extends js.Object with FSConstants {
10071025
* @example {{{ fs.write(fd, buffer[, offset[, length[, position]]], callback) }}}
10081026
**/
10091027
def write(fd: FileDescriptor,
1010-
buffer: typedarray.Uint8Array,
1028+
buffer: js.typedarray.Uint8Array,
10111029
offset: Int | Null,
10121030
length: Int | Null,
10131031
position: Int | Null,
@@ -1054,9 +1072,9 @@ trait Fs extends js.Object with FSConstants {
10541072
* The encoding option is ignored if data is a buffer. It defaults to 'utf8'
10551073
* @example fs.writeFile(file, data[, options], callback)
10561074
*/
1057-
def writeFile(file: String, data: typedarray.Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
1075+
def writeFile(file: String, data: js.typedarray.Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
10581076
js.native
1059-
def writeFile(file: String, data: typedarray.Uint8Array, callback: FsCallback0): Unit = js.native
1077+
def writeFile(file: String, data: js.typedarray.Uint8Array, callback: FsCallback0): Unit = js.native
10601078
def writeFile(file: String, data: String, options: FileWriteOptions, callback: FsCallback0): Unit =
10611079
js.native
10621080
def writeFile(file: String, data: String, callback: FsCallback0): Unit = js.native
@@ -1070,7 +1088,7 @@ trait Fs extends js.Object with FSConstants {
10701088
* @example fs.writeFileSync(file, data[, options])
10711089
*/
10721090
def writeFileSync(file: Path | FileDescriptor,
1073-
data: typedarray.Uint8Array,
1091+
data: js.typedarray.Uint8Array,
10741092
options: FileWriteOptions = js.native
10751093
): Unit =
10761094
js.native
@@ -1092,14 +1110,14 @@ trait Fs extends js.Object with FSConstants {
10921110
* @example {{{ fs.writeSync(fd, buffer[, offset[, length[, position]]]) }}}
10931111
*/
10941112
def writeSync(fd: FileDescriptor,
1095-
buffer: typedarray.Uint8Array,
1113+
buffer: js.typedarray.Uint8Array,
10961114
offset: Int,
10971115
length: Int,
10981116
position: Int = js.native
10991117
): Unit =
11001118
js.native
1101-
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array, offset: Int): Unit = js.native
1102-
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array): Unit = js.native
1119+
def writeSync(fd: FileDescriptor, buffer: js.typedarray.Uint8Array, offset: Int): Unit = js.native
1120+
def writeSync(fd: FileDescriptor, buffer: js.typedarray.Uint8Array): Unit = js.native
11031121
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int, position: Int): Unit = js.native
11041122
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int): Unit = js.native
11051123
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int): Unit = js.native
@@ -1120,13 +1138,15 @@ trait Fs extends js.Object with FSConstants {
11201138

11211139
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
11221140
def writev(fd: FileDescriptor,
1123-
buffers: js.Array[typedarray.ArrayBufferView],
1141+
buffers: js.Array[js.typedarray.ArrayBufferView],
11241142
position: Int,
1125-
fsCallback2: FsCallback2[Int, js.Array[typedarray.ArrayBufferView]]
1143+
fsCallback2: FsCallback2[Int, js.Array[js.typedarray.ArrayBufferView]]
11261144
): Unit = js.native
11271145
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
1128-
def writevSync(fd: FileDescriptor, buffers: js.Array[typedarray.ArrayBufferView], position: Int = js.native): Unit =
1129-
js.native
1146+
def writevSync(fd: FileDescriptor,
1147+
buffers: js.Array[js.typedarray.ArrayBufferView],
1148+
position: Int = js.native
1149+
): Unit = js.native
11301150
}
11311151

11321152
/**
@@ -1190,7 +1210,10 @@ object Fs extends Fs {
11901210
offset: Int | Null,
11911211
length: Int | Null,
11921212
position: Int | Null
1193-
): js.Promise[BufferIOResult[T]] = js.native
1213+
): js.Promise[BufferIOResult[T]] = js.native
1214+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
1215+
def readd[T <: js.typedarray.TypedArray[_, _]](options: ReadOptions): js.Promise[BufferIOResult[T]] = js.native
1216+
11941217
def readFile(): js.Promise[Buffer] = js.native
11951218
def readFile(encoding: String): js.Promise[String] = js.native
11961219
def readFile(options: ReadFileOptions): js.Promise[Output] = js.native
@@ -1264,6 +1287,14 @@ trait FileEncodingOptions {
12641287
var encoding: js.UndefOr[String] = js.undefined
12651288
}
12661289

1290+
@Factory
1291+
trait ReadOptions extends js.Object {
1292+
var buffer: js.UndefOr[BufferLike] = js.undefined
1293+
var offset: js.UndefOr[Int] = js.undefined
1294+
var length: js.UndefOr[Int] = js.undefined
1295+
var position: js.UndefOr[Int] = js.undefined
1296+
}
1297+
12671298
@Factory
12681299
trait ReaddirOptions extends js.Object {
12691300
var encoding: js.UndefOr[String] = js.undefined
@@ -1272,7 +1303,8 @@ trait ReaddirOptions extends js.Object {
12721303

12731304
@Factory
12741305
trait OpendirOptions extends js.Object {
1275-
var encoding: js.UndefOr[String] = js.undefined
1306+
var encoding: js.UndefOr[String] = js.undefined
1307+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
12761308
var bufferSize: js.UndefOr[Double] = js.undefined
12771309
}
12781310

@@ -1292,6 +1324,9 @@ trait FileInputOptions extends js.Object {
12921324
var start: js.UndefOr[Int] = js.undefined
12931325
var end: js.UndefOr[Int] = js.undefined
12941326
var highWaterMark: js.UndefOr[Int] = js.undefined
1327+
1328+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
1329+
var fs: js.UndefOr[js.Object] = js.undefined
12951330
}
12961331

12971332
@Factory
@@ -1303,6 +1338,9 @@ trait FileOutputOptions extends js.Object {
13031338
var autoClose: js.UndefOr[Boolean] = js.undefined
13041339
var emitClose: js.UndefOr[Boolean] = js.undefined
13051340
var start: js.UndefOr[Int] = js.undefined
1341+
1342+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
1343+
var fs: js.UndefOr[js.Object] = js.undefined
13061344
}
13071345

13081346
@Factory
@@ -1324,8 +1362,19 @@ trait MkdirOptions extends js.Object {
13241362

13251363
@Factory
13261364
trait RmdirOptions extends js.Object {
1327-
var emfileWait: js.UndefOr[Int] = js.undefined
1328-
var maxBusyTries: js.UndefOr[Int] = js.undefined
1365+
@deprecated(
1366+
"Tha option has been removed, and EMFILE errors use the same retry logic as other errors.",
1367+
"Node.js v13.3.0, v12.16.0"
1368+
)
1369+
var emfileWait: js.UndefOr[Int] = js.undefined
1370+
@deprecated("Use maxRetries", "Node.js v13.3.0, v12.16.0")
1371+
var maxBusyTries: js.UndefOr[Int] = js.undefined
1372+
var maxRetries: js.UndefOr[Int] = js.undefined
1373+
@deprecated(
1374+
"Tha option has been removed, and EMFILE errors use the same retry logic as other errors.",
1375+
"Node.js v13.3.0, v12.16.0"
1376+
)
1377+
var retryDelay: js.UndefOr[Int] = js.undefined
13291378
var recursive: js.UndefOr[Boolean] = js.undefined
13301379
}
13311380

app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ package object fs {
324324
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
325325
@inline
326326
def rmdirRecursiveFuture(path: Path, options: RmdirOptions): Future[Unit] = {
327-
val recursiveEnabled =
328-
RmdirOptions(recursive = true, maxBusyTries = options.maxBusyTries, emfileWait = options.emfileWait)
327+
val recursiveEnabled = js.Object.assign(js.Object(), options).asInstanceOf[RmdirOptions]
328+
recursiveEnabled.recursive = true
329329
promiseWithError0[FileIOError](instance.rmdir(path, recursiveEnabled, _))
330330
}
331331

app/current/src/main/scala/io/scalajs/nodejs/package.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ package object nodejs {
3838

3939
type FsCallback3[A, B, C] = js.Function4[FileIOError, A, B, C, Any]
4040

41+
type FsRecursiveCallback0 = js.Function2[FileIOError, fs.Path, Any]
42+
4143
type GID = Int
4244

4345
type UID = Int

0 commit comments

Comments
 (0)