@gjsify/fs 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -8
- package/lib/cjs/callback.js +112 -0
- package/lib/cjs/dirent.js +98 -0
- package/lib/cjs/encoding.js +34 -0
- package/lib/cjs/file-handle.js +444 -0
- package/lib/cjs/fs-watcher.js +50 -0
- package/lib/cjs/index.js +95 -0
- package/lib/cjs/promises.js +160 -0
- package/lib/cjs/read-stream.js +78 -0
- package/lib/cjs/stats.js +45 -0
- package/lib/cjs/sync.js +126 -0
- package/lib/cjs/types/encoding-option.js +0 -0
- package/lib/cjs/types/file-read-options.js +0 -0
- package/lib/cjs/types/file-read-result.js +0 -0
- package/lib/cjs/types/flag-and-open-mode.js +0 -0
- package/lib/cjs/types/index.js +6 -0
- package/lib/cjs/types/open-flags.js +0 -0
- package/lib/cjs/types/read-options.js +0 -0
- package/lib/cjs/utils.js +18 -0
- package/lib/cjs/write-stream.js +116 -0
- package/lib/esm/callback.js +112 -0
- package/lib/esm/dirent.js +98 -0
- package/lib/esm/encoding.js +34 -0
- package/lib/esm/file-handle.js +444 -0
- package/lib/esm/fs-watcher.js +50 -0
- package/lib/esm/index.js +95 -0
- package/lib/esm/promises.js +160 -0
- package/lib/esm/read-stream.js +78 -0
- package/lib/esm/stats.js +45 -0
- package/lib/esm/sync.js +126 -0
- package/lib/esm/types/encoding-option.js +0 -0
- package/lib/esm/types/file-read-options.js +0 -0
- package/lib/esm/types/file-read-result.js +0 -0
- package/lib/esm/types/flag-and-open-mode.js +0 -0
- package/lib/esm/types/index.js +6 -0
- package/lib/esm/types/open-flags.js +0 -0
- package/lib/esm/types/read-options.js +0 -0
- package/lib/esm/utils.js +18 -0
- package/lib/esm/write-stream.js +116 -0
- package/package.json +52 -17
- package/src/callback.spec.ts +42 -0
- package/src/callback.ts +362 -0
- package/src/dirent.ts +117 -0
- package/src/encoding.ts +40 -0
- package/src/file-handle.spec.ts +34 -0
- package/src/file-handle.ts +606 -0
- package/{fs-watcher.js → src/fs-watcher.ts} +10 -9
- package/src/index.ts +95 -0
- package/src/promises.spec.ts +172 -0
- package/src/promises.ts +349 -0
- package/src/read-stream.ts +98 -0
- package/src/stat.spec.ts +79 -0
- package/src/stats.ts +106 -0
- package/src/symlink.spec.ts +38 -0
- package/src/sync.spec.ts +205 -0
- package/src/sync.ts +239 -0
- package/src/test.mts +11 -0
- package/src/types/encoding-option.ts +3 -0
- package/src/types/file-read-options.ts +15 -0
- package/src/types/file-read-result.ts +4 -0
- package/src/types/flag-and-open-mode.ts +6 -0
- package/src/types/index.ts +6 -0
- package/src/types/open-flags.ts +14 -0
- package/src/types/read-options.ts +9 -0
- package/src/utils.ts +19 -0
- package/src/write-stream.ts +143 -0
- package/test/file.txt +1 -0
- package/test/watch.js +1 -0
- package/test.gjs.js +35359 -0
- package/test.gjs.js.map +7 -0
- package/test.gjs.mjs +40570 -0
- package/test.gjs.mjs.meta.json +1 -0
- package/test.node.js +1479 -0
- package/test.node.js.map +7 -0
- package/test.node.mjs +710 -0
- package/tsconfig.json +18 -0
- package/tsconfig.types.json +8 -0
- package/index.js +0 -114
- package/test/index.js +0 -90
- package/test/resources/file.txt +0 -1
package/README.md
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
# @gjsify/fs
|
|
1
|
+
# @gjsify/fs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* [x] mkdirSync
|
|
9
|
-
* [x] rmdirSync
|
|
3
|
+
## Inspirations
|
|
4
|
+
- https://github.com/cgjs/cgjs/blob/master/packages/fs/index.js
|
|
5
|
+
- https://github.com/geut/brode/blob/main/packages/browser-node-core/src/fs.js
|
|
6
|
+
- https://github.com/mafintosh/level-filesystem/blob/master/index.js
|
|
7
|
+
- https://github.com/denoland/deno_std/blob/main/node/fs.ts
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { open as openP, rm as rmP } from "./promises.js";
|
|
2
|
+
import { warnNotImplemented } from "@gjsify/utils";
|
|
3
|
+
import { FileHandle } from "./file-handle.js";
|
|
4
|
+
import { Buffer } from "buffer";
|
|
5
|
+
import { realpath } from "@gjsify/deno_std/node/_fs/_fs_realpath";
|
|
6
|
+
import { readdir } from "@gjsify/deno_std/node/_fs/_fs_readdir";
|
|
7
|
+
import { symlink } from "@gjsify/deno_std/node/_fs/_fs_symlink";
|
|
8
|
+
import { lstat } from "@gjsify/deno_std/node/_fs/_fs_lstat";
|
|
9
|
+
import { stat } from "@gjsify/deno_std/node/_fs/_fs_stat";
|
|
10
|
+
function open(path, ...args) {
|
|
11
|
+
let flags;
|
|
12
|
+
let mode;
|
|
13
|
+
let callback;
|
|
14
|
+
switch (args.length) {
|
|
15
|
+
case 1:
|
|
16
|
+
callback = args[0];
|
|
17
|
+
break;
|
|
18
|
+
case 2:
|
|
19
|
+
flags = args[0];
|
|
20
|
+
callback = args[1];
|
|
21
|
+
break;
|
|
22
|
+
case 3:
|
|
23
|
+
flags = args[0];
|
|
24
|
+
mode = args[1];
|
|
25
|
+
callback = args[2];
|
|
26
|
+
break;
|
|
27
|
+
default:
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
openP(path, flags, mode).then((fileHandle) => {
|
|
31
|
+
callback(null, fileHandle.fd);
|
|
32
|
+
}).catch((err) => {
|
|
33
|
+
callback(err, -1);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function write(fd, data, ...args) {
|
|
37
|
+
const fileHandle = FileHandle.getInstance(fd);
|
|
38
|
+
if (typeof data === "string") {
|
|
39
|
+
const callback2 = args[args.length - 1];
|
|
40
|
+
fileHandle.write(data, ...args.pop()).then((res) => {
|
|
41
|
+
callback2(null, res.bytesWritten, res.buffer);
|
|
42
|
+
}).catch((err) => {
|
|
43
|
+
callback2(err, 0, "");
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const callback = args[args.length - 1];
|
|
48
|
+
const offset = args[0];
|
|
49
|
+
const length = args[1];
|
|
50
|
+
const position = args[2];
|
|
51
|
+
fileHandle.write(data, offset, length, position).then((res) => {
|
|
52
|
+
callback(null, res.bytesWritten, res.buffer);
|
|
53
|
+
}).catch((err) => {
|
|
54
|
+
callback(err, 0, Buffer.from([]));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
function read(fd, ...args) {
|
|
58
|
+
const fileHandle = FileHandle.getInstance(fd);
|
|
59
|
+
const callback = args[args.length - 1];
|
|
60
|
+
const err = new Error(warnNotImplemented("fs.read"));
|
|
61
|
+
callback(err, 0, Buffer.from(""));
|
|
62
|
+
let buffer;
|
|
63
|
+
let offset;
|
|
64
|
+
let length;
|
|
65
|
+
let position;
|
|
66
|
+
if (typeof args[0] === "object") {
|
|
67
|
+
const options = args[0];
|
|
68
|
+
buffer = options.buffer;
|
|
69
|
+
offset = options.offset;
|
|
70
|
+
length = options.length;
|
|
71
|
+
position = options.position;
|
|
72
|
+
} else {
|
|
73
|
+
buffer = args[0];
|
|
74
|
+
offset = args[1];
|
|
75
|
+
length = args[2];
|
|
76
|
+
position = args[3];
|
|
77
|
+
}
|
|
78
|
+
fileHandle.read(buffer, offset, length, position).then((res) => {
|
|
79
|
+
callback(null, res.bytesRead, res.buffer);
|
|
80
|
+
}).catch((err2) => {
|
|
81
|
+
callback(err2, 0, Buffer.from([]));
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function close(fd, callback) {
|
|
85
|
+
FileHandle.getInstance(fd).close().then(() => {
|
|
86
|
+
callback(null);
|
|
87
|
+
}).catch((err) => callback(err));
|
|
88
|
+
}
|
|
89
|
+
function rm(path, ...args) {
|
|
90
|
+
let options = {};
|
|
91
|
+
let callback = args[args.length - 1];
|
|
92
|
+
if (args.length >= 2) {
|
|
93
|
+
options = args[0];
|
|
94
|
+
}
|
|
95
|
+
rmP(path, options).then(() => {
|
|
96
|
+
callback(null);
|
|
97
|
+
}).catch((err) => {
|
|
98
|
+
callback(err);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
close,
|
|
103
|
+
lstat,
|
|
104
|
+
open,
|
|
105
|
+
read,
|
|
106
|
+
readdir,
|
|
107
|
+
realpath,
|
|
108
|
+
rm,
|
|
109
|
+
stat,
|
|
110
|
+
symlink,
|
|
111
|
+
write
|
|
112
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import Gio from "@girs/gio-2.0";
|
|
2
|
+
import { basename } from "path";
|
|
3
|
+
class Dirent {
|
|
4
|
+
/**
|
|
5
|
+
* The file name that this `fs.Dirent` object refers to. The type of this
|
|
6
|
+
* value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
|
|
7
|
+
* @since v10.10.0
|
|
8
|
+
*/
|
|
9
|
+
name;
|
|
10
|
+
_isFile = false;
|
|
11
|
+
_isDirectory = false;
|
|
12
|
+
_isBlockDevice = false;
|
|
13
|
+
_isCharacterDevice = false;
|
|
14
|
+
_isSymbolicLink = false;
|
|
15
|
+
_isFIFO = false;
|
|
16
|
+
_isSocket = false;
|
|
17
|
+
/** This is not part of node.fs and is used internal by gjsify */
|
|
18
|
+
_file;
|
|
19
|
+
/** This is not part of node.fs and is used internal by gjsify */
|
|
20
|
+
constructor(path, filename) {
|
|
21
|
+
if (!filename)
|
|
22
|
+
filename = basename(path);
|
|
23
|
+
this.name = filename;
|
|
24
|
+
this._file = Gio.File.new_for_path(path);
|
|
25
|
+
const type = this._file.query_file_type(Gio.FileQueryInfoFlags.NONE, null);
|
|
26
|
+
switch (type) {
|
|
27
|
+
case Gio.FileType.DIRECTORY:
|
|
28
|
+
this._isDirectory = true;
|
|
29
|
+
break;
|
|
30
|
+
case Gio.FileType.MOUNTABLE:
|
|
31
|
+
break;
|
|
32
|
+
case Gio.FileType.REGULAR:
|
|
33
|
+
this._isFile = true;
|
|
34
|
+
break;
|
|
35
|
+
case Gio.FileType.SYMBOLIC_LINK:
|
|
36
|
+
case Gio.FileType.SHORTCUT:
|
|
37
|
+
this._isSymbolicLink = true;
|
|
38
|
+
break;
|
|
39
|
+
case Gio.FileType.SPECIAL:
|
|
40
|
+
this._isBlockDevice = Gio.unix_is_system_device_path(path);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Returns `true` if the `fs.Dirent` object describes a regular file.
|
|
46
|
+
* @since v10.10.0
|
|
47
|
+
*/
|
|
48
|
+
isFile() {
|
|
49
|
+
return this._isFile;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns `true` if the `fs.Dirent` object describes a file system
|
|
53
|
+
* directory.
|
|
54
|
+
* @since v10.10.0
|
|
55
|
+
*/
|
|
56
|
+
isDirectory() {
|
|
57
|
+
return this._isDirectory;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns `true` if the `fs.Dirent` object describes a block device.
|
|
61
|
+
* @since v10.10.0
|
|
62
|
+
*/
|
|
63
|
+
isBlockDevice() {
|
|
64
|
+
return this._isBlockDevice;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Returns `true` if the `fs.Dirent` object describes a character device.
|
|
68
|
+
* @since v10.10.0
|
|
69
|
+
*/
|
|
70
|
+
isCharacterDevice() {
|
|
71
|
+
return this._isCharacterDevice;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns `true` if the `fs.Dirent` object describes a symbolic link.
|
|
75
|
+
* @since v10.10.0
|
|
76
|
+
*/
|
|
77
|
+
isSymbolicLink() {
|
|
78
|
+
return this._isSymbolicLink;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Returns `true` if the `fs.Dirent` object describes a first-in-first-out
|
|
82
|
+
* (FIFO) pipe.
|
|
83
|
+
* @since v10.10.0
|
|
84
|
+
*/
|
|
85
|
+
isFIFO() {
|
|
86
|
+
return this._isFIFO;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns `true` if the `fs.Dirent` object describes a socket.
|
|
90
|
+
* @since v10.10.0
|
|
91
|
+
*/
|
|
92
|
+
isSocket() {
|
|
93
|
+
return this._isSocket;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
Dirent
|
|
98
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
function getEncodingFromOptions(options = { encoding: null, flag: "r" }, defaultEncoding = "utf8") {
|
|
3
|
+
if (options === null) {
|
|
4
|
+
return defaultEncoding;
|
|
5
|
+
}
|
|
6
|
+
if (typeof options === "string") {
|
|
7
|
+
return options;
|
|
8
|
+
}
|
|
9
|
+
if (typeof options === "object" && typeof options.encoding === "string") {
|
|
10
|
+
return options.encoding;
|
|
11
|
+
}
|
|
12
|
+
return defaultEncoding;
|
|
13
|
+
}
|
|
14
|
+
function encodeUint8Array(encoding, data) {
|
|
15
|
+
if (encoding === "buffer") {
|
|
16
|
+
return Buffer.from(data);
|
|
17
|
+
}
|
|
18
|
+
const decoder = new TextDecoder(encoding);
|
|
19
|
+
return decoder.decode(data);
|
|
20
|
+
}
|
|
21
|
+
function decode(str, encoding) {
|
|
22
|
+
if (!encoding)
|
|
23
|
+
return str;
|
|
24
|
+
else {
|
|
25
|
+
const decoder = new TextDecoder(encoding);
|
|
26
|
+
const encoder = new TextEncoder();
|
|
27
|
+
return decoder.decode(encoder.encode(str));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
decode,
|
|
32
|
+
encodeUint8Array,
|
|
33
|
+
getEncodingFromOptions
|
|
34
|
+
};
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import { warnNotImplemented, notImplemented } from "@gjsify/utils";
|
|
2
|
+
import { ReadStream } from "./read-stream.js";
|
|
3
|
+
import { WriteStream } from "./write-stream.js";
|
|
4
|
+
import { Stats } from "./stats.js";
|
|
5
|
+
import { getEncodingFromOptions, encodeUint8Array } from "./encoding.js";
|
|
6
|
+
import GLib from "@girs/glib-2.0";
|
|
7
|
+
import { ReadableStream } from "stream/web";
|
|
8
|
+
import { Buffer } from "buffer";
|
|
9
|
+
class FileHandle {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.options.flags ||= "r";
|
|
13
|
+
this.options.mode ||= 438;
|
|
14
|
+
this._file = GLib.IOChannel.new_file(options.path.toString(), this.options.flags);
|
|
15
|
+
this.fd = this._file.unix_get_fd();
|
|
16
|
+
FileHandle.instances[this.fd] = this;
|
|
17
|
+
return FileHandle.getInstance(this.fd);
|
|
18
|
+
}
|
|
19
|
+
/** Not part of the default implementation, used internal by gjsify */
|
|
20
|
+
_file;
|
|
21
|
+
/** Not part of the default implementation, used internal by gjsify */
|
|
22
|
+
static instances = {};
|
|
23
|
+
/**
|
|
24
|
+
* The numeric file descriptor managed by the {FileHandle} object.
|
|
25
|
+
* @since v10.0.0
|
|
26
|
+
*/
|
|
27
|
+
fd;
|
|
28
|
+
/** Not part of the default implementation, used internal by gjsify */
|
|
29
|
+
static getInstance(fd) {
|
|
30
|
+
const instance = FileHandle.instances[fd];
|
|
31
|
+
if (!instance) {
|
|
32
|
+
throw new Error("No instance found for fd!");
|
|
33
|
+
}
|
|
34
|
+
return FileHandle.instances[fd];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Alias of `filehandle.writeFile()`.
|
|
38
|
+
*
|
|
39
|
+
* When operating on file handles, the mode cannot be changed from what it was set
|
|
40
|
+
* to with `fsPromises.open()`. Therefore, this is equivalent to `filehandle.writeFile()`.
|
|
41
|
+
* @since v10.0.0
|
|
42
|
+
* @return Fulfills with `undefined` upon success.
|
|
43
|
+
*/
|
|
44
|
+
async appendFile(data, options) {
|
|
45
|
+
const encoding = getEncodingFromOptions(options);
|
|
46
|
+
if (typeof data === "string") {
|
|
47
|
+
data = Buffer.from(data);
|
|
48
|
+
}
|
|
49
|
+
if (encoding)
|
|
50
|
+
this._file.set_encoding(encoding);
|
|
51
|
+
const [status, written] = this._file.write_chars(data, data.length);
|
|
52
|
+
if (status === GLib.IOStatus.ERROR) {
|
|
53
|
+
throw new Error("Error on append to file!");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Changes the ownership of the file. A wrapper for [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html).
|
|
58
|
+
* @since v10.0.0
|
|
59
|
+
* @param uid The file's new owner's user id.
|
|
60
|
+
* @param gid The file's new group's group id.
|
|
61
|
+
* @return Fulfills with `undefined` upon success.
|
|
62
|
+
*/
|
|
63
|
+
async chown(uid, gid) {
|
|
64
|
+
warnNotImplemented("fs.FileHandle.chown");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Modifies the permissions on the file. See [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html).
|
|
68
|
+
* @since v10.0.0
|
|
69
|
+
* @param mode the file mode bit mask.
|
|
70
|
+
* @return Fulfills with `undefined` upon success.
|
|
71
|
+
*/
|
|
72
|
+
async chmod(mode) {
|
|
73
|
+
warnNotImplemented("fs.FileHandle.chmod");
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Unlike the 16 kb default `highWaterMark` for a `stream.Readable`, the stream
|
|
77
|
+
* returned by this method has a default `highWaterMark` of 64 kb.
|
|
78
|
+
*
|
|
79
|
+
* `options` can include `start` and `end` values to read a range of bytes from
|
|
80
|
+
* the file instead of the entire file. Both `start` and `end` are inclusive and
|
|
81
|
+
* start counting at 0, allowed values are in the
|
|
82
|
+
* \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `start` is
|
|
83
|
+
* omitted or `undefined`, `filehandle.createReadStream()` reads sequentially from
|
|
84
|
+
* the current file position. The `encoding` can be any one of those accepted by `Buffer`.
|
|
85
|
+
*
|
|
86
|
+
* If the `FileHandle` points to a character device that only supports blocking
|
|
87
|
+
* reads (such as keyboard or sound card), read operations do not finish until data
|
|
88
|
+
* is available. This can prevent the process from exiting and the stream from
|
|
89
|
+
* closing naturally.
|
|
90
|
+
*
|
|
91
|
+
* By default, the stream will emit a `'close'` event after it has been
|
|
92
|
+
* destroyed. Set the `emitClose` option to `false` to change this behavior.
|
|
93
|
+
*
|
|
94
|
+
* ```js
|
|
95
|
+
* import { open } from 'fs/promises';
|
|
96
|
+
*
|
|
97
|
+
* const fd = await open('/dev/input/event0');
|
|
98
|
+
* // Create a stream from some character device.
|
|
99
|
+
* const stream = fd.createReadStream();
|
|
100
|
+
* setTimeout(() => {
|
|
101
|
+
* stream.close(); // This may not close the stream.
|
|
102
|
+
* // Artificially marking end-of-stream, as if the underlying resource had
|
|
103
|
+
* // indicated end-of-file by itself, allows the stream to close.
|
|
104
|
+
* // This does not cancel pending read operations, and if there is such an
|
|
105
|
+
* // operation, the process may still not be able to exit successfully
|
|
106
|
+
* // until it finishes.
|
|
107
|
+
* stream.push(null);
|
|
108
|
+
* stream.read(0);
|
|
109
|
+
* }, 100);
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* If `autoClose` is false, then the file descriptor won't be closed, even if
|
|
113
|
+
* there's an error. It is the application's responsibility to close it and make
|
|
114
|
+
* sure there's no file descriptor leak. If `autoClose` is set to true (default
|
|
115
|
+
* behavior), on `'error'` or `'end'` the file descriptor will be closed
|
|
116
|
+
* automatically.
|
|
117
|
+
*
|
|
118
|
+
* An example to read the last 10 bytes of a file which is 100 bytes long:
|
|
119
|
+
*
|
|
120
|
+
* ```js
|
|
121
|
+
* import { open } from 'fs/promises';
|
|
122
|
+
*
|
|
123
|
+
* const fd = await open('sample.txt');
|
|
124
|
+
* fd.createReadStream({ start: 90, end: 99 });
|
|
125
|
+
* ```
|
|
126
|
+
* @since v16.11.0
|
|
127
|
+
*/
|
|
128
|
+
createReadStream(options) {
|
|
129
|
+
return new ReadStream(this.options.path, options);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* `options` may also include a `start` option to allow writing data at some
|
|
133
|
+
* position past the beginning of the file, allowed values are in the
|
|
134
|
+
* \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than
|
|
135
|
+
* replacing it may require the `flags` `open` option to be set to `r+` rather than
|
|
136
|
+
* the default `r`. The `encoding` can be any one of those accepted by `Buffer`.
|
|
137
|
+
*
|
|
138
|
+
* If `autoClose` is set to true (default behavior) on `'error'` or `'finish'`the file descriptor will be closed automatically. If `autoClose` is false,
|
|
139
|
+
* then the file descriptor won't be closed, even if there's an error.
|
|
140
|
+
* It is the application's responsibility to close it and make sure there's no
|
|
141
|
+
* file descriptor leak.
|
|
142
|
+
*
|
|
143
|
+
* By default, the stream will emit a `'close'` event after it has been
|
|
144
|
+
* destroyed. Set the `emitClose` option to `false` to change this behavior.
|
|
145
|
+
* @since v16.11.0
|
|
146
|
+
*/
|
|
147
|
+
createWriteStream(options) {
|
|
148
|
+
return new WriteStream(this.options.path, options);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Forces all currently queued I/O operations associated with the file to the
|
|
152
|
+
* operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details.
|
|
153
|
+
*
|
|
154
|
+
* Unlike `filehandle.sync` this method does not flush modified metadata.
|
|
155
|
+
* @since v10.0.0
|
|
156
|
+
* @return Fulfills with `undefined` upon success.
|
|
157
|
+
*/
|
|
158
|
+
async datasync() {
|
|
159
|
+
warnNotImplemented("fs.FileHandle.datasync");
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Request that all data for the open file descriptor is flushed to the storage
|
|
163
|
+
* device. The specific implementation is operating system and device specific.
|
|
164
|
+
* Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail.
|
|
165
|
+
* @since v10.0.0
|
|
166
|
+
* @return Fufills with `undefined` upon success.
|
|
167
|
+
*/
|
|
168
|
+
async sync() {
|
|
169
|
+
warnNotImplemented("fs.FileHandle.sync");
|
|
170
|
+
}
|
|
171
|
+
async read(args) {
|
|
172
|
+
let buffer;
|
|
173
|
+
let offset;
|
|
174
|
+
let length;
|
|
175
|
+
let position;
|
|
176
|
+
if (typeof args[0] === "object") {
|
|
177
|
+
const options = args[0];
|
|
178
|
+
buffer = options.buffer;
|
|
179
|
+
offset = options.offset;
|
|
180
|
+
length = options.length;
|
|
181
|
+
position = options.position;
|
|
182
|
+
} else {
|
|
183
|
+
buffer = args[0];
|
|
184
|
+
offset = args[1];
|
|
185
|
+
length = args[2];
|
|
186
|
+
position = args[3];
|
|
187
|
+
}
|
|
188
|
+
if (offset) {
|
|
189
|
+
const status2 = this._file.seek_position(offset, GLib.SeekType.CUR);
|
|
190
|
+
if (status2 === GLib.IOStatus.ERROR) {
|
|
191
|
+
throw new Error("Error on set offset!");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (length)
|
|
195
|
+
this._file.set_buffer_size(length);
|
|
196
|
+
if (position) {
|
|
197
|
+
const status2 = this._file.seek_position(position, GLib.SeekType.SET);
|
|
198
|
+
if (status2 === GLib.IOStatus.ERROR) {
|
|
199
|
+
throw new Error("Error on set position!");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const [status, buf, bytesRead] = this._file.read_chars();
|
|
203
|
+
if (status === GLib.IOStatus.ERROR) {
|
|
204
|
+
throw new Error("Error on read!");
|
|
205
|
+
}
|
|
206
|
+
buffer = buf;
|
|
207
|
+
return {
|
|
208
|
+
bytesRead,
|
|
209
|
+
buffer
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Returns a `ReadableStream` that may be used to read the files data.
|
|
214
|
+
*
|
|
215
|
+
* An error will be thrown if this method is called more than once or is called after the `FileHandle` is closed
|
|
216
|
+
* or closing.
|
|
217
|
+
*
|
|
218
|
+
* ```js
|
|
219
|
+
* import { open } from 'fs/promises';
|
|
220
|
+
*
|
|
221
|
+
* const file = await open('./some/file/to/read');
|
|
222
|
+
*
|
|
223
|
+
* for await (const chunk of file.readableWebStream())
|
|
224
|
+
* console.log(chunk);
|
|
225
|
+
*
|
|
226
|
+
* await file.close();
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* While the `ReadableStream` will read the file to completion, it will not close the `FileHandle` automatically. User code must still call the `fileHandle.close()` method.
|
|
230
|
+
*
|
|
231
|
+
* @since v17.0.0
|
|
232
|
+
* @experimental
|
|
233
|
+
*/
|
|
234
|
+
readableWebStream() {
|
|
235
|
+
return new ReadableStream();
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Asynchronously reads the entire contents of a file. The underlying file will _not_ be closed automatically.
|
|
239
|
+
* The `FileHandle` must have been opened for reading.
|
|
240
|
+
* @param options An object that may contain an optional flag.
|
|
241
|
+
* If a flag is not provided, it defaults to `'r'`.
|
|
242
|
+
*/
|
|
243
|
+
async readFile(options) {
|
|
244
|
+
const encoding = getEncodingFromOptions(options, "buffer");
|
|
245
|
+
if (encoding)
|
|
246
|
+
this._file.set_encoding(encoding);
|
|
247
|
+
const [status, buf] = this._file.read_to_end();
|
|
248
|
+
if (status === GLib.IOStatus.ERROR) {
|
|
249
|
+
throw new Error("Error on read from file!");
|
|
250
|
+
}
|
|
251
|
+
const res = encodeUint8Array(encoding, buf);
|
|
252
|
+
return res;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Convenience method to create a `readline` interface and stream over the file. For example:
|
|
256
|
+
*
|
|
257
|
+
* ```js
|
|
258
|
+
* import { open } from 'node:fs/promises';
|
|
259
|
+
*
|
|
260
|
+
* const file = await open('./some/file/to/read');
|
|
261
|
+
*
|
|
262
|
+
* for await (const line of file.readLines()) {
|
|
263
|
+
* console.log(line);
|
|
264
|
+
* }
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* @since v18.11.0
|
|
268
|
+
* @param options See `filehandle.createReadStream()` for the options.
|
|
269
|
+
*/
|
|
270
|
+
readLines(options) {
|
|
271
|
+
notImplemented("fs.FileHandle.readLines");
|
|
272
|
+
}
|
|
273
|
+
async stat(opts) {
|
|
274
|
+
warnNotImplemented("fs.FileHandle.stat");
|
|
275
|
+
return new Stats(this.options.path.toString());
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Truncates the file.
|
|
279
|
+
*
|
|
280
|
+
* If the file was larger than `len` bytes, only the first `len` bytes will be
|
|
281
|
+
* retained in the file.
|
|
282
|
+
*
|
|
283
|
+
* The following example retains only the first four bytes of the file:
|
|
284
|
+
*
|
|
285
|
+
* ```js
|
|
286
|
+
* import { open } from 'fs/promises';
|
|
287
|
+
*
|
|
288
|
+
* let filehandle = null;
|
|
289
|
+
* try {
|
|
290
|
+
* filehandle = await open('temp.txt', 'r+');
|
|
291
|
+
* await filehandle.truncate(4);
|
|
292
|
+
* } finally {
|
|
293
|
+
* await filehandle?.close();
|
|
294
|
+
* }
|
|
295
|
+
* ```
|
|
296
|
+
*
|
|
297
|
+
* If the file previously was shorter than `len` bytes, it is extended, and the
|
|
298
|
+
* extended part is filled with null bytes (`'\0'`):
|
|
299
|
+
*
|
|
300
|
+
* If `len` is negative then `0` will be used.
|
|
301
|
+
* @since v10.0.0
|
|
302
|
+
* @param [len=0]
|
|
303
|
+
* @return Fulfills with `undefined` upon success.
|
|
304
|
+
*/
|
|
305
|
+
async truncate(len) {
|
|
306
|
+
warnNotImplemented("fs.FileHandle.truncate");
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Change the file system timestamps of the object referenced by the `FileHandle` then resolves the promise with no arguments upon success.
|
|
310
|
+
* @since v10.0.0
|
|
311
|
+
*/
|
|
312
|
+
async utimes(atime, mtime) {
|
|
313
|
+
warnNotImplemented("fs.FileHandle.utimes");
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Asynchronously writes data to a file, replacing the file if it already exists.`data` can be a string, a buffer, an
|
|
317
|
+
* [AsyncIterable](https://tc39.github.io/ecma262/#sec-asynciterable-interface) or
|
|
318
|
+
* [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol) object.
|
|
319
|
+
* The promise is resolved with no arguments upon success.
|
|
320
|
+
*
|
|
321
|
+
* If `options` is a string, then it specifies the `encoding`.
|
|
322
|
+
*
|
|
323
|
+
* The `FileHandle` has to support writing.
|
|
324
|
+
*
|
|
325
|
+
* It is unsafe to use `filehandle.writeFile()` multiple times on the same file
|
|
326
|
+
* without waiting for the promise to be resolved (or rejected).
|
|
327
|
+
*
|
|
328
|
+
* If one or more `filehandle.write()` calls are made on a file handle and then a`filehandle.writeFile()` call is made, the data will be written from the
|
|
329
|
+
* current position till the end of the file. It doesn't always write from the
|
|
330
|
+
* beginning of the file.
|
|
331
|
+
* @since v10.0.0
|
|
332
|
+
*/
|
|
333
|
+
async writeFile(data, options) {
|
|
334
|
+
warnNotImplemented("fs.FileHandle.writeFile");
|
|
335
|
+
}
|
|
336
|
+
async write(data, ...args) {
|
|
337
|
+
let position = null;
|
|
338
|
+
let encoding = null;
|
|
339
|
+
let offset = null;
|
|
340
|
+
let length = null;
|
|
341
|
+
if (typeof data === "string") {
|
|
342
|
+
position = args[0];
|
|
343
|
+
encoding = args[1];
|
|
344
|
+
} else {
|
|
345
|
+
offset = args[0];
|
|
346
|
+
length = args[1];
|
|
347
|
+
position = args[2];
|
|
348
|
+
}
|
|
349
|
+
encoding = getEncodingFromOptions(encoding, typeof data === "string" ? "utf8" : null);
|
|
350
|
+
if (encoding) {
|
|
351
|
+
console.log("set_encoding", encoding, this._file.get_encoding(), typeof data);
|
|
352
|
+
this._file.set_encoding(encoding === "buffer" ? null : encoding);
|
|
353
|
+
}
|
|
354
|
+
if (offset) {
|
|
355
|
+
const status2 = this._file.seek_position(offset, GLib.SeekType.CUR);
|
|
356
|
+
if (status2 === GLib.IOStatus.ERROR) {
|
|
357
|
+
throw new Error("Error on set offset!");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (length)
|
|
361
|
+
this._file.set_buffer_size(length);
|
|
362
|
+
if (position) {
|
|
363
|
+
const status2 = this._file.seek_position(position, GLib.SeekType.SET);
|
|
364
|
+
if (status2 === GLib.IOStatus.ERROR) {
|
|
365
|
+
throw new Error("Error on set position!");
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
let bytesWritten = 0;
|
|
369
|
+
let status;
|
|
370
|
+
if (typeof data === "string") {
|
|
371
|
+
status = this._file.write_unichar(data);
|
|
372
|
+
bytesWritten = data.length;
|
|
373
|
+
} else {
|
|
374
|
+
const [_status, _bytesWritten] = this._file.write_chars(data, length);
|
|
375
|
+
bytesWritten = _bytesWritten;
|
|
376
|
+
status = _status;
|
|
377
|
+
}
|
|
378
|
+
if (status === GLib.IOStatus.ERROR) {
|
|
379
|
+
throw new Error("Error on write to file!");
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
bytesWritten,
|
|
383
|
+
buffer: data
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Write an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s to the file.
|
|
388
|
+
*
|
|
389
|
+
* The promise is resolved with an object containing a two properties:
|
|
390
|
+
*
|
|
391
|
+
* It is unsafe to call `writev()` multiple times on the same file without waiting
|
|
392
|
+
* for the promise to be resolved (or rejected).
|
|
393
|
+
*
|
|
394
|
+
* On Linux, positional writes don't work when the file is opened in append mode.
|
|
395
|
+
* The kernel ignores the position argument and always appends the data to
|
|
396
|
+
* the end of the file.
|
|
397
|
+
* @since v12.9.0
|
|
398
|
+
* @param position The offset from the beginning of the file where the data from `buffers` should be written. If `position` is not a `number`, the data will be written at the current
|
|
399
|
+
* position.
|
|
400
|
+
*/
|
|
401
|
+
async writev(buffers, position) {
|
|
402
|
+
warnNotImplemented("fs.FileHandle.writev");
|
|
403
|
+
return {
|
|
404
|
+
bytesWritten: 0,
|
|
405
|
+
buffers: []
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Read from a file and write to an array of [ArrayBufferView](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) s
|
|
410
|
+
* @since v13.13.0, v12.17.0
|
|
411
|
+
* @param position The offset from the beginning of the file where the data should be read from. If `position` is not a `number`, the data will be read from the current position.
|
|
412
|
+
* @return Fulfills upon success an object containing two properties:
|
|
413
|
+
*/
|
|
414
|
+
async readv(buffers, position) {
|
|
415
|
+
warnNotImplemented("fs.FileHandle.readv");
|
|
416
|
+
return {
|
|
417
|
+
bytesRead: 0,
|
|
418
|
+
buffers: []
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Closes the file handle after waiting for any pending operation on the handle to
|
|
423
|
+
* complete.
|
|
424
|
+
*
|
|
425
|
+
* ```js
|
|
426
|
+
* import { open } from 'fs/promises';
|
|
427
|
+
*
|
|
428
|
+
* let filehandle;
|
|
429
|
+
* try {
|
|
430
|
+
* filehandle = await open('thefile.txt', 'r');
|
|
431
|
+
* } finally {
|
|
432
|
+
* await filehandle?.close();
|
|
433
|
+
* }
|
|
434
|
+
* ```
|
|
435
|
+
* @since v10.0.0
|
|
436
|
+
* @return Fulfills with `undefined` upon success.
|
|
437
|
+
*/
|
|
438
|
+
async close() {
|
|
439
|
+
this._file.close();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
export {
|
|
443
|
+
FileHandle
|
|
444
|
+
};
|