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

Commit d3661a9

Browse files
authored
Merge pull request #101 from exoego/add-migration-path
Add backword compatibility for ease of migration
2 parents 08959ed + 9ad45d7 commit d3661a9

File tree

8 files changed

+75
-89
lines changed

8 files changed

+75
-89
lines changed

app/current/src/main/scala/io/scalajs/nodejs/buffer/Buffer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Buffer protected () extends Uint8Array( /* dummy to trick constructor */ -
147147
*
148148
* @see [[https://nodejs.org/api/buffer.html#buffer_buf_entries]]
149149
*/
150-
def entries(): js.Iterator[js.Array[Int]] = js.native
150+
def entries(): io.scalajs.collection.Iterator[js.Array[Int]] = js.native
151151

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

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

552552
/**
553553
* Writes string to buf at offset according to the character encoding in encoding. The length parameter is

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ package object buffer {
4242
* @return the hex-formatted string
4343
*/
4444
@inline
45-
def toHexString: String = buffer.entries().toIterator.flatMap(_.lastOption).map(n => f"$n%02x").mkString
45+
def toHexString: String =
46+
js.Iterator.IteratorOps(buffer.entries()).toIterator.flatMap(_.lastOption).map(n => f"$n%02x").mkString
4647
}
4748

4849
/**

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

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,83 +34,87 @@ package object fs {
3434

3535
/**
3636
* File System Extensions
37-
* @param fs the given [[Fs file system]] instance
37+
* @param instance the given [[Fs file system]] instance
3838
*/
39-
implicit final class FsExtensions(private val fs: Fs) extends AnyVal {
39+
implicit final class FsExtensions(private val instance: Fs) extends AnyVal {
40+
@deprecated("Use Fs directly instead of Fs.fs", "0.9.0")
41+
@inline
42+
def fs: Fs = instance
43+
4044
@inline
4145
def accessFuture(path: Buffer | String): Future[Unit] = {
42-
promiseWithError0[FileIOError](fs.access(path, _))
46+
promiseWithError0[FileIOError](instance.access(path, _))
4347
}
4448

4549
@inline
4650
def accessFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
47-
promiseWithError0[FileIOError](fs.access(path, mode, _))
51+
promiseWithError0[FileIOError](instance.access(path, mode, _))
4852
}
4953

5054
@inline
5155
def appendFileFuture(file: Buffer | FileDescriptor | String,
5256
data: Buffer | String,
5357
options: FileAppendOptions = null): Future[Unit] = {
54-
promiseWithError0[FileIOError](fs.appendFile(file, data, options, _))
58+
promiseWithError0[FileIOError](instance.appendFile(file, data, options, _))
5559
}
5660

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

6266
@inline
63-
def closeFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](fs.close(fd, _))
67+
def closeFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.close(fd, _))
6468

6569
@inline
66-
def existsFuture(path: String): Future[Boolean] = promiseWithErrorAsBoolean[FileIOError](fs.access(path, _))
70+
def existsFuture(path: String): Future[Boolean] = promiseWithErrorAsBoolean[FileIOError](instance.access(path, _))
6771

6872
@inline
69-
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](fs.fdatasync(fd, _))
73+
def fdatasyncFuture(fd: FileDescriptor): Future[Unit] = promiseWithError0[FileIOError](instance.fdatasync(fd, _))
7074

7175
@inline
7276
def futimesFuture(fd: FileDescriptor, atime: Time, mtime: Time): Future[Unit] = {
73-
promiseWithError0[FileIOError](fs.futimes(fd, atime, mtime, _))
77+
promiseWithError0[FileIOError](instance.futimes(fd, atime, mtime, _))
7478
}
7579

7680
@inline
7781
def lchmodFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
78-
promiseWithError0[FileIOError](fs.lchmod(path, mode, _))
82+
promiseWithError0[FileIOError](instance.lchmod(path, mode, _))
7983
}
8084

8185
@inline
8286
def lchownFuture(path: Buffer | String, uid: UID, gid: GID): Future[Unit] = {
83-
promiseWithError0[FileIOError](fs.lchown(path, uid, gid, _))
87+
promiseWithError0[FileIOError](instance.lchown(path, uid, gid, _))
8488
}
8589

8690
@inline
8791
def linkFuture(srcpath: Buffer | String, dstpath: Buffer | String): Future[Unit] = {
88-
promiseWithError0[FileIOError](fs.link(srcpath, dstpath, _))
92+
promiseWithError0[FileIOError](instance.link(srcpath, dstpath, _))
8993
}
9094

9195
@inline
9296
def mkdirFuture(path: Buffer | String, mode: FileMode): Future[Unit] = {
93-
promiseWithError0[FileIOError](fs.mkdir(path, mode, _))
97+
promiseWithError0[FileIOError](instance.mkdir(path, mode, _))
9498
}
9599

96100
@inline
97101
def mkdirFuture(path: Buffer | String): Future[Unit] = {
98-
promiseWithError0[FileIOError](fs.mkdir(path, _))
102+
promiseWithError0[FileIOError](instance.mkdir(path, _))
99103
}
100104

101105
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
102106
@inline
103107
def mkdirFuture(path: Buffer | String, options: MkdirOptions): Future[Unit] = {
104-
promiseWithError0[FileIOError](fs.mkdir(path, options, _))
108+
promiseWithError0[FileIOError](instance.mkdir(path, options, _))
105109
}
106110

107111
@inline
108112
def openFuture(path: Buffer | String, flags: Flags, mode: FileMode): Future[FileDescriptor] = {
109-
promiseWithError1[FileIOError, FileDescriptor](fs.open(path, flags, mode, _))
113+
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, mode, _))
110114
}
111115
@inline
112116
def openFuture(path: Buffer | String, flags: Flags): Future[FileDescriptor] = {
113-
promiseWithError1[FileIOError, FileDescriptor](fs.open(path, flags, _))
117+
promiseWithError1[FileIOError, FileDescriptor](instance.open(path, flags, _))
114118
}
115119

116120
@inline
@@ -125,15 +129,15 @@ package object fs {
125129
@inline
126130
def readdirFuture(path: Buffer | String, options: String = "utf8"): Future[js.Array[String]] = {
127131
val callback: FsCallback1[js.Array[String]] => Unit = { callback =>
128-
fs.readdir(path, options, callback.asInstanceOf[FsCallback1[ReaddirArrays]])
132+
instance.readdir(path, options, callback.asInstanceOf[FsCallback1[ReaddirArrays]])
129133
}
130134
promiseWithError1[FileIOError, js.Array[String]](callback)
131135
}
132136

133137
@inline
134138
def readdirBufferFuture(path: Buffer | String): Future[js.Array[Buffer]] = {
135139
val callback: FsCallback1[js.Array[Buffer]] => Unit = { callback =>
136-
fs.readdir(
140+
instance.readdir(
137141
path,
138142
new FileEncodingOptions(encoding = "buffer"),
139143
callback.asInstanceOf[FsCallback1[ReaddirArrays]]
@@ -146,61 +150,65 @@ package object fs {
146150
@inline
147151
def readdirDirentFuture(path: Buffer | String): Future[js.Array[Dirent]] = {
148152
val callback: FsCallback1[js.Array[Dirent]] => Unit = { callback =>
149-
fs.readdir(path, new ReaddirOptions(withFileTypes = true), callback.asInstanceOf[FsCallback1[ReaddirArrays2]])
153+
instance.readdir(
154+
path,
155+
new ReaddirOptions(withFileTypes = true),
156+
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
157+
)
150158
}
151159
promiseWithError1[FileIOError, js.Array[Dirent]](callback)
152160
}
153161

154162
@inline
155163
def readFileFuture(file: String, options: ReadFileOptions = null): Future[Output] = {
156-
promiseWithError1[FileIOError, Output](fs.readFile(file, options, _))
164+
promiseWithError1[FileIOError, Output](instance.readFile(file, options, _))
157165
}
158166

159167
@inline
160168
def renameFuture(oldPath: String, newPath: String): Future[Unit] = {
161-
promiseWithError0[FileIOError](fs.rename(oldPath, newPath, _))
169+
promiseWithError0[FileIOError](instance.rename(oldPath, newPath, _))
162170
}
163171

164172
@inline
165173
def realpathFuture(path: String): Future[String] = {
166-
promiseWithError1[FileIOError, String](fs.realpath(path, _))
174+
promiseWithError1[FileIOError, String](instance.realpath(path, _))
167175
}
168176

169177
@inline
170178
def realpathFuture(path: String, options: FileEncodingOptions): Future[Output] = {
171-
promiseWithError1[FileIOError, Output](fs.realpath(path, options, _))
179+
promiseWithError1[FileIOError, Output](instance.realpath(path, options, _))
172180
}
173181

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

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

182190
@inline
183-
def statFuture(path: String): Future[Stats] = promiseWithError1[FileIOError, Stats](fs.stat(path, _))
191+
def statFuture(path: String): Future[Stats] = promiseWithError1[FileIOError, Stats](instance.stat(path, _))
184192

185193
@inline
186194
def symlinkFuture(target: Buffer | String, path: Buffer | String, `type`: String = null): Future[Unit] = {
187-
promiseWithError0[FileIOError](fs.symlink(target, path, `type`, _))
195+
promiseWithError0[FileIOError](instance.symlink(target, path, `type`, _))
188196
}
189197

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

193201
@inline
194202
def unwatchFileFuture(filename: Buffer | String): Future[Unit] =
195-
promiseWithError0[FileIOError](fs.unwatchFile(filename, _))
203+
promiseWithError0[FileIOError](instance.unwatchFile(filename, _))
196204

197205
@inline
198206
def utimesFuture(path: Buffer | String, atime: Int, mtime: Int): Future[Unit] =
199-
promiseWithError0[FileIOError](fs.utimes(path, atime, mtime, _))
207+
promiseWithError0[FileIOError](instance.utimes(path, atime, mtime, _))
200208

201209
@inline
202210
def watchFuture(filename: String, options: FSWatcherOptions = null): Future[(String, String)] = {
203-
promiseCallback2[String, String](fs.watch(filename, options, _))
211+
promiseCallback2[String, String](instance.watch(filename, options, _))
204212
}
205213

206214
@inline
@@ -209,37 +217,37 @@ package object fs {
209217
offset: Int | Null = null,
210218
length: Int | Null = null,
211219
position: Int | Null = null): Future[(FileType, Buffer)] = {
212-
promiseWithError2[FileIOError, Int, Buffer](fs.write(fd, buffer, offset, length, position, _))
220+
promiseWithError2[FileIOError, Int, Buffer](instance.write(fd, buffer, offset, length, position, _))
213221
}
214222

215223
@inline
216224
def writeFuture(fd: FileDescriptor, string: String, position: Int, encoding: String): Future[(FileType, String)] = {
217-
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, position, encoding, _))
225+
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, position, encoding, _))
218226
}
219227

220228
@inline
221229
def writeFuture(fd: FileDescriptor, string: String, position: Int): Future[(FileType, String)] = {
222-
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, position, null, _))
230+
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, position, null, _))
223231
}
224232

225233
@inline
226234
def writeFuture(fd: FileDescriptor, string: String): Future[(FileType, String)] = {
227-
promiseWithError2[FileIOError, Int, String](fs.write(fd, string, _))
235+
promiseWithError2[FileIOError, Int, String](instance.write(fd, string, _))
228236
}
229237

230238
@inline
231239
def writeFileFuture(file: String, data: Buffer, options: FileWriteOptions = null): Future[Unit] = {
232-
promiseWithError0[FileIOError](fs.writeFile(file, data, options, _))
240+
promiseWithError0[FileIOError](instance.writeFile(file, data, options, _))
233241
}
234242

235243
@inline
236244
def writeFileFuture(file: String, data: String, options: FileWriteOptions): Future[Unit] = {
237-
promiseWithError0[FileIOError](fs.writeFile(file, data, options, _))
245+
promiseWithError0[FileIOError](instance.writeFile(file, data, options, _))
238246
}
239247

240248
@inline
241249
def writeFileFuture(file: String, data: String): Future[Unit] = {
242-
promiseWithError0[FileIOError](fs.writeFile(file, data, _))
250+
promiseWithError0[FileIOError](instance.writeFile(file, data, _))
243251
}
244252
}
245253

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ package object nodejs {
3232

3333
type FileIOError = SystemError
3434

35-
type FileMode = Int
35+
type FileMode = Int | Null
3636

3737
type FileType = Int
3838

app/current/src/main/scala/io/scalajs/nodejs/process/Environment.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,13 @@ trait Environment extends js.Object {
3333
@JSBracketAccess
3434
def update(key: String, value: String): Unit = js.native
3535
}
36+
37+
object Environment {
38+
implicit final class EnvironmentExtension(private val env: Environment) extends AnyVal {
39+
@deprecated("Use env(key)", "0.9.0")
40+
@inline def get(key: String): Option[String] = env(key).toOption
41+
42+
@deprecated("Use env(key)", "0.9.0")
43+
@inline def contains(key: String): Boolean = env(key).isDefined
44+
}
45+
}

app/current/src/main/scala/io/scalajs/nodejs/url/URLSearchParams.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class URLSearchParams() extends js.Object {
6767
* a JavaScript Array. The first item of the Array is the name, the second item of the Array is the value.
6868
* @return an iterable of an array of results
6969
*/
70-
def entries(): js.Iterator[js.Tuple2[String, String]] = js.native
70+
def entries(): io.scalajs.collection.Iterator[js.Tuple2[String, String]] = js.native
7171

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

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

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

120116
/**
121117
* Returns an ES6 Iterator over the values of each name-value pair.
122-
* @return an [[js.Iterator Iterator]] over the values of each name-value pair.
123118
*/
124-
def values(): js.Iterator[String] = js.native
119+
def values(): io.scalajs.collection.Iterator[String] = js.native
125120

126121
}

app/nodejs-v8/src/test/scala/io/scalajs/nodejs/buffer/BufferTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class BufferTest extends FunSpec {
2626
val buf = Buffer.from("Hello!")
2727
val it = buf.entries()
2828
assert(
29-
it.toIterator.toSeq.map(_.toSeq) === Seq(
29+
// TODO: Use it.toIterator once io.scalajs.colletion.Iterator removed.
30+
js.Iterator.IteratorOps(it).toIterator.toSeq.map(_.toSeq) === Seq(
3031
Seq(0, 72),
3132
Seq(1, 101),
3233
Seq(2, 108),

0 commit comments

Comments
 (0)