@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
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { fileURLToPath, URL } from "url";
|
|
3
|
+
import { open, write, close } from "./callback.js";
|
|
4
|
+
const kIsPerformingIO = Symbol("kIsPerformingIO");
|
|
5
|
+
const kIoDone = Symbol("kIoDone");
|
|
6
|
+
function toPathIfFileURL(fileURLOrPath) {
|
|
7
|
+
if (!(fileURLOrPath instanceof URL)) {
|
|
8
|
+
return fileURLOrPath;
|
|
9
|
+
}
|
|
10
|
+
return fileURLToPath(fileURLOrPath);
|
|
11
|
+
}
|
|
12
|
+
class WriteStream extends Writable {
|
|
13
|
+
/**
|
|
14
|
+
* Closes `writeStream`. Optionally accepts a
|
|
15
|
+
* callback that will be executed once the `writeStream`is closed.
|
|
16
|
+
* @since v0.9.4
|
|
17
|
+
*/
|
|
18
|
+
close(callback, err = null) {
|
|
19
|
+
if (!this.fd) {
|
|
20
|
+
callback(err);
|
|
21
|
+
} else {
|
|
22
|
+
close(this.fd, (er) => {
|
|
23
|
+
callback(er || err);
|
|
24
|
+
});
|
|
25
|
+
this.fd = null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The number of bytes written so far. Does not include data that is still queued
|
|
30
|
+
* for writing.
|
|
31
|
+
* @since v0.4.7
|
|
32
|
+
*/
|
|
33
|
+
bytesWritten = 0;
|
|
34
|
+
/**
|
|
35
|
+
* The path to the file the stream is writing to as specified in the first
|
|
36
|
+
* argument to {@link createWriteStream}. If `path` is passed as a string, then`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then`writeStream.path` will be a
|
|
37
|
+
* `Buffer`.
|
|
38
|
+
* @since v0.1.93
|
|
39
|
+
*/
|
|
40
|
+
path;
|
|
41
|
+
/**
|
|
42
|
+
* This property is `true` if the underlying file has not been opened yet,
|
|
43
|
+
* i.e. before the `'ready'` event is emitted.
|
|
44
|
+
* @since v11.2.0
|
|
45
|
+
*/
|
|
46
|
+
pending;
|
|
47
|
+
fd = null;
|
|
48
|
+
flags = "w";
|
|
49
|
+
mode = 438;
|
|
50
|
+
pos = 0;
|
|
51
|
+
[kIsPerformingIO] = false;
|
|
52
|
+
constructor(path, opts = {}) {
|
|
53
|
+
super(opts);
|
|
54
|
+
this.path = toPathIfFileURL(path);
|
|
55
|
+
if (opts.encoding) {
|
|
56
|
+
this.setDefaultEncoding(opts.encoding);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
_construct(callback) {
|
|
60
|
+
open(
|
|
61
|
+
this.path.toString(),
|
|
62
|
+
this.flags,
|
|
63
|
+
this.mode,
|
|
64
|
+
(err, fd) => {
|
|
65
|
+
if (err) {
|
|
66
|
+
callback(err);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.fd = fd;
|
|
70
|
+
callback();
|
|
71
|
+
this.emit("open", this.fd);
|
|
72
|
+
this.emit("ready");
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
_write(data, _encoding, cb) {
|
|
77
|
+
this[kIsPerformingIO] = true;
|
|
78
|
+
write(
|
|
79
|
+
this.fd,
|
|
80
|
+
data,
|
|
81
|
+
0,
|
|
82
|
+
data.length,
|
|
83
|
+
this.pos,
|
|
84
|
+
(er, bytes) => {
|
|
85
|
+
this[kIsPerformingIO] = false;
|
|
86
|
+
if (this.destroyed) {
|
|
87
|
+
cb(er);
|
|
88
|
+
return this.emit(kIoDone, er);
|
|
89
|
+
}
|
|
90
|
+
if (er) {
|
|
91
|
+
return cb(er);
|
|
92
|
+
}
|
|
93
|
+
this.bytesWritten += bytes;
|
|
94
|
+
cb();
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
if (this.pos !== void 0) {
|
|
98
|
+
this.pos += data.length;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
_destroy(err, cb) {
|
|
102
|
+
if (this[kIsPerformingIO]) {
|
|
103
|
+
this.once(kIoDone, (er) => this.close(cb, err || er));
|
|
104
|
+
} else {
|
|
105
|
+
this.close(cb, err);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function createWriteStream(path, opts) {
|
|
110
|
+
return new WriteStream(path, opts);
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
WriteStream,
|
|
114
|
+
createWriteStream,
|
|
115
|
+
toPathIfFileURL
|
|
116
|
+
};
|
|
@@ -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
|
+
};
|