Skip to content

Commit a8f43b8

Browse files
committed
Continued refinement of the “fs” module
1 parent ede83ca commit a8f43b8

File tree

4 files changed

+111
-58
lines changed

4 files changed

+111
-58
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,25 @@ import scala.scalajs.js.|
2323
* @author lawrence.daniels@gmail.com
2424
*/
2525
@js.native
26-
@JSImport("buffer", JSImport.Namespace)
27-
class Buffer extends js.Object {
26+
@JSImport("buffer", "Buffer")
27+
class Buffer() extends js.Object {
2828

2929
/////////////////////////////////////////////////////////////////////////////////
3030
// Constructors
3131
/////////////////////////////////////////////////////////////////////////////////
3232

3333
/**
34-
* new Buffer(size)
34+
* @example new Buffer(size)
3535
*/
3636
@inline
3737
def this(size: Int) = this()
3838

3939
/**
40-
* new Buffer(str, [encoding])
40+
* @example new Buffer(str, [encoding])
4141
*/
4242
@inline
4343
@deprecated("Use Buffer.from(str[, encoding]) instead.", since = "6.0.0")
44-
def this(str: String, encoding: String) = this()
45-
46-
/**
47-
* new Buffer(str, [encoding])
48-
*/
49-
@inline
50-
@deprecated("Use Buffer.from(str[, encoding]) instead.", since = "6.0.0")
51-
def this(str: String) = this()
44+
def this(str: String, encoding: String = js.native) = this()
5245

5346
/**
5447
* @example new Buffer(array)
@@ -204,6 +197,15 @@ class Buffer extends js.Object {
204197
*/
205198
def keys(): Iterator[Int] = js.native
206199

200+
/**
201+
* The largest size allowed for a single Buffer instance.
202+
* On 32-bit architectures, this value is (2^30)-1 (~1GB). On 64-bit architectures, this value is (2^31)-1 (~2GB).
203+
* Note that this is a property on the buffer module returned by require('buffer'), not on the Buffer global or
204+
* a Buffer instance.
205+
* @return the largest size allowed
206+
*/
207+
def kMaxLength: Int = js.native
208+
207209
/**
208210
* Identical to buf.indexOf(), except buf is searched from back to front instead of front to back.
209211
* @param value What to search for
@@ -523,6 +525,17 @@ class Buffer extends js.Object {
523525
*/
524526
def toString(encoding: String = js.native, start: Int = js.native, end: Int = js.native): String = js.native
525527

528+
/**
529+
* Re-encodes the given Buffer instance from one character encoding to another. Returns a new Buffer instance.
530+
* Throws if the fromEnc or toEnc specify invalid character encodings or if conversion from fromEnc to toEnc
531+
* is not permitted.
532+
* @param source A Buffer instance
533+
* @param fromEnc The current encoding
534+
* @param toEnc To target encoding
535+
* @return a new [[Buffer]]
536+
*/
537+
def transcode(source: Buffer, fromEnc: String, toEnc: String): Buffer = js.native
538+
526539
/**
527540
* Creates and returns an iterator for buf values (bytes). This function is called automatically when a [[Buffer]]
528541
* is used in a for..of statement.

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ trait Fs extends IEventEmitter {
4949
val W_OK: FileMode = js.native
5050

5151
/**
52-
* File can be executed by the calling process. This has no effect on Windows (will behave like fs.[[F_OK]]).
52+
* File can be executed by the calling process. This has no effect on Windows (will behave like [[F_OK]]).
5353
*/
5454
val X_OK: FileMode = js.native
5555

@@ -195,9 +195,9 @@ trait Fs extends IEventEmitter {
195195
* <ul>
196196
* <li>fs.F_OK - File is visible to the calling process. This is useful for determining if a file exists,
197197
* but says nothing about rwx permissions. Default if no mode is specified.</li>
198-
* <li>fs.[[R_OK]] - File can be read by the calling process.</li>
199-
* <li>fs.[[W_OK]] - File can be written by the calling process.</li>
200-
* <li>fs.[[X_OK]] - File can be executed by the calling process. This has no effect on Windows (will behave like fs.[[F_OK]]).</li>
198+
* <li>[[R_OK]] - File can be read by the calling process.</li>
199+
* <li>[[W_OK]] - File can be written by the calling process.</li>
200+
* <li>[[X_OK]] - File can be executed by the calling process. This has no effect on Windows (will behave like [[F_OK]]).</li>
201201
* </ul>
202202
* @param path the path (Buffer | String)
203203
* @param mode the optional mode
@@ -214,9 +214,9 @@ trait Fs extends IEventEmitter {
214214
* <ul>
215215
* <li>fs.F_OK - File is visible to the calling process. This is useful for determining if a file exists,
216216
* but says nothing about rwx permissions. Default if no mode is specified.</li>
217-
* <li>fs.[[R_OK]] - File can be read by the calling process.</li>
218-
* <li>fs.[[W_OK]] - File can be written by the calling process.</li>
219-
* <li>fs.[[X_OK]] - File can be executed by the calling process. This has no effect on Windows (will behave like fs.[[F_OK]]).</li>
217+
* <li>[[R_OK]] - File can be read by the calling process.</li>
218+
* <li>[[W_OK]] - File can be written by the calling process.</li>
219+
* <li>[[X_OK]] - File can be executed by the calling process. This has no effect on Windows (will behave like [[F_OK]]).</li>
220220
* </ul>
221221
* @param path the path (Buffer | String)
222222
* @param callback is a callback function that is invoked with a possible error argument. If any of the accessibility
@@ -400,13 +400,14 @@ trait Fs extends IEventEmitter {
400400
* @param fd the file descriptor
401401
* @param callback the completion callback.
402402
*/
403-
def fstat(fd: FileDescriptor, callback: FsCallback0): Unit = js.native
403+
def fstat(fd: FileDescriptor, callback: FsCallback1[Stats]): Unit = js.native
404404

405405
/**
406-
* Synchronous fstat(2). Returns an instance of fs.Stats.
406+
* Synchronous fstat(2).
407407
* @param fd the file descriptor
408+
* @return an instance of [[fs.Stats]].
408409
*/
409-
def fstatSync(fd: FileDescriptor): Unit = js.native
410+
def fstatSync(fd: FileDescriptor): Stats = js.native
410411

411412
/**
412413
* Asynchronous fsync(2). No arguments other than a possible exception are given to the completion callback.
@@ -424,21 +425,21 @@ trait Fs extends IEventEmitter {
424425

425426
/**
426427
* Asynchronous ftruncate(2). No arguments other than a possible exception are given to the completion callback.
427-
* If the file referred to by the file descriptor was larger than len bytes, only the first len bytes will be
428+
* If the file referred to by the file descriptor was larger than length bytes, only the first length bytes will be
428429
* retained in the file.
429430
* @param fd the file descriptor
430-
* @param len the desired length
431+
* @param length the desired length
431432
* @param callback the completion callback.
432433
*/
433-
def ftruncate(fd: FileDescriptor, len: Double, callback: FsCallback0): Unit = js.native
434+
def ftruncate(fd: FileDescriptor, length: Double, callback: FsCallback0): Unit = js.native
434435

435436
/**
436437
* Synchronous ftruncate(2).
437-
* @param fd the file descriptor
438-
* @param len the desired length
438+
* @param fd the file descriptor
439+
* @param length the desired length
439440
* @return undefined.
440441
*/
441-
def ftruncateSync(fd: FileDescriptor, len: Double): Unit = js.native
442+
def ftruncateSync(fd: FileDescriptor, length: Double): Unit = js.native
442443

443444
/**
444445
* Change the file timestamps of a file referenced by the supplied file descriptor.
@@ -516,7 +517,7 @@ trait Fs extends IEventEmitter {
516517
/**
517518
* Synchronous lstat(2).
518519
* @param path the path (Buffer | String)
519-
* @return an instance of fs.Stats.
520+
* @return an instance of [[fs.Stats]].
520521
*/
521522
def lstatSync(path: Buffer | String): Stats = js.native
522523

@@ -525,7 +526,7 @@ trait Fs extends IEventEmitter {
525526
* mode defaults to 0o777.
526527
* @example fs.mkdir(path[, mode], callback)
527528
*/
528-
def mkdir(path: Buffer | String, mode: FileMode | js.Any, callback: FsCallback0): Unit = js.native
529+
def mkdir(path: Buffer | String, mode: FileMode, callback: FsCallback0): Unit = js.native
529530

530531
/**
531532
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
@@ -785,7 +786,7 @@ trait Fs extends IEventEmitter {
785786
*/
786787
def readlink(path: Buffer | String,
787788
options: String | FileEncodingOptions | RawOptions,
788-
callback: js.Function2[FileIOError, String, Any]): Unit = js.native
789+
callback: FsCallback1[String]): Unit = js.native
789790

790791
/**
791792
* Synchronous readlink(2).
@@ -864,14 +865,14 @@ trait Fs extends IEventEmitter {
864865
def rmdirSync(path: Buffer | String): Unit = js.native
865866

866867
/**
867-
* Asynchronous stat(2). The callback gets two arguments (err, stats) where stats is a fs.[[Stats]] object.
868+
* Asynchronous stat(2). The callback gets two arguments (err, stats) where stats is a [[fs.Stats]] object.
868869
* See the fs.Stats section for more information.
869870
* @example fs.stat(path, callback)
870871
*/
871872
def stat(path: Buffer | String, callback: FsCallback1[Stats]): Stats = js.native
872873

873874
/**
874-
* Synchronous stat(2). Returns an instance of fs.[[Stats]].
875+
* Synchronous stat(2). Returns an instance of [[fs.Stats]].
875876
* @example fs.statSync(path)
876877
*/
877878
def statSync(path: Buffer | String): Stats = js.native
@@ -912,7 +913,7 @@ trait Fs extends IEventEmitter {
912913
* @param path the path <String> | <Buffer>
913914
* @param length the length
914915
* @param callback the completion callback.
915-
* @example fs.truncate(path, len, callback)
916+
* @example fs.truncate(path, length, callback)
916917
*/
917918
def truncate(path: Buffer | FileDescriptor | String, length: Int, callback: FsCallback0): Unit = js.native
918919

@@ -922,7 +923,7 @@ trait Fs extends IEventEmitter {
922923
* @param path the path or file descriptor - <String> | <Buffer> | <Integer>
923924
* @param length the length
924925
* @return undefined.
925-
* @example fs.truncateSync(path, len)
926+
* @example fs.truncateSync(path, length)
926927
*/
927928
def truncateSync(path: Buffer | FileDescriptor | String, length: Int): Unit = js.native
928929

@@ -971,7 +972,7 @@ trait Fs extends IEventEmitter {
971972

972973
/**
973974
* Watch for changes on filename, where filename is either a file or a directory.
974-
* The returned object is a fs.[[FSWatcher]].
975+
* The returned object is a [[fs.FSWatcher]].
975976
*
976977
* The second argument is optional. If options is provided as a string, it specifies the encoding.
977978
* Otherwise options should be passed as an object.
@@ -990,7 +991,7 @@ trait Fs extends IEventEmitter {
990991

991992
/**
992993
* Watch for changes on filename, where filename is either a file or a directory.
993-
* The returned object is a fs.[[FSWatcher]].
994+
* The returned object is a [[fs.FSWatcher]].
994995
*
995996
* The second argument is optional. If options is provided as a string, it specifies the encoding.
996997
* Otherwise options should be passed as an object.
@@ -1008,7 +1009,7 @@ trait Fs extends IEventEmitter {
10081009

10091010
/**
10101011
* Watch for changes on filename, where filename is either a file or a directory.
1011-
* The returned object is a fs.[[FSWatcher]].
1012+
* The returned object is a [[fs.FSWatcher]].
10121013
*
10131014
* The second argument is optional. If options is provided as a string, it specifies the encoding.
10141015
* Otherwise options should be passed as an object.

src/main/scala/io/scalajs/nodejs/fs/Stats.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ trait Stats extends js.Object {
1616

1717
def dev: Int = js.native
1818

19-
def mode: Int = js.native
19+
def mode: FileMode = js.native
2020

2121
def nlink: Int = js.native
2222

23-
def uid: Int = js.native
23+
def uid: UID = js.native
2424

25-
def gid: Int = js.native
25+
def gid: GID = js.native
2626

2727
def rdev: Int = js.native
2828

0 commit comments

Comments
 (0)