@gjsify/fs 0.3.13 → 0.3.14

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/lib/esm/dir.js CHANGED
@@ -1,160 +1,152 @@
1
- import Gio from "@girs/gio-2.0";
2
- import GLib from "@girs/glib-2.0";
3
1
  import { normalizePath } from "./utils.js";
4
2
  import { Dirent } from "./dirent.js";
5
3
  import { createNodeError } from "./errors.js";
4
+ import GLib from "@girs/glib-2.0";
5
+ import Gio from "@girs/gio-2.0";
6
+
7
+ //#region src/dir.ts
6
8
  const DIR_ATTRS = "standard::name,standard::type,standard::is-symlink,standard::size,standard::symlink-target,unix::uid,unix::gid,unix::mode,time::modified,time::access,time::created";
7
- class Dir {
8
- path;
9
- _enumerator;
10
- _closed = false;
11
- constructor(path, enumerator) {
12
- this.path = path;
13
- this._enumerator = enumerator;
14
- }
15
- _assertOpen() {
16
- if (this._closed) {
17
- const err = new Error("Directory handle was closed");
18
- err.code = "ERR_DIR_CLOSED";
19
- throw err;
20
- }
21
- }
22
- readSync() {
23
- this._assertOpen();
24
- try {
25
- const info = this._enumerator.next_file(null);
26
- if (info === null) return null;
27
- const name = info.get_name();
28
- const fileType = info.get_file_type();
29
- const childPath = this.path.endsWith("/") ? this.path + name : this.path + "/" + name;
30
- return new Dirent(childPath, name, fileType);
31
- } catch (err) {
32
- throw createNodeError(err, "readdir", this.path);
33
- }
34
- }
35
- read(callback) {
36
- if (callback !== void 0) {
37
- if (typeof callback !== "function") {
38
- throw new TypeError('The "callback" argument must be of type function');
39
- }
40
- try {
41
- this._assertOpen();
42
- const dirent = this.readSync();
43
- Promise.resolve().then(() => callback(null, dirent));
44
- } catch (err) {
45
- Promise.resolve().then(() => callback(err, null));
46
- }
47
- return;
48
- }
49
- return new Promise((resolve, reject) => {
50
- try {
51
- this._assertOpen();
52
- resolve(this.readSync());
53
- } catch (err) {
54
- reject(err);
55
- }
56
- });
57
- }
58
- closeSync() {
59
- this._assertOpen();
60
- this._closed = true;
61
- try {
62
- this._enumerator.close(null);
63
- } catch {
64
- }
65
- this._enumerator = null;
66
- }
67
- close(callback) {
68
- if (callback !== void 0) {
69
- if (typeof callback !== "function") {
70
- throw new TypeError('The "callback" argument must be of type function');
71
- }
72
- try {
73
- this.closeSync();
74
- Promise.resolve().then(() => callback(null));
75
- } catch (err) {
76
- Promise.resolve().then(() => callback(err));
77
- }
78
- return;
79
- }
80
- return new Promise((resolve, reject) => {
81
- try {
82
- this.closeSync();
83
- resolve();
84
- } catch (err) {
85
- reject(err);
86
- }
87
- });
88
- }
89
- async *[Symbol.asyncIterator]() {
90
- try {
91
- while (true) {
92
- const dirent = await this.read();
93
- if (dirent === null) break;
94
- yield dirent;
95
- }
96
- } finally {
97
- if (!this._closed) {
98
- await this.close();
99
- }
100
- }
101
- }
102
- }
9
+ var Dir = class {
10
+ path;
11
+ _enumerator;
12
+ _closed = false;
13
+ constructor(path, enumerator) {
14
+ this.path = path;
15
+ this._enumerator = enumerator;
16
+ }
17
+ _assertOpen() {
18
+ if (this._closed) {
19
+ const err = new Error("Directory handle was closed");
20
+ err.code = "ERR_DIR_CLOSED";
21
+ throw err;
22
+ }
23
+ }
24
+ readSync() {
25
+ this._assertOpen();
26
+ try {
27
+ const info = this._enumerator.next_file(null);
28
+ if (info === null) return null;
29
+ const name = info.get_name();
30
+ const fileType = info.get_file_type();
31
+ const childPath = this.path.endsWith("/") ? this.path + name : this.path + "/" + name;
32
+ return new Dirent(childPath, name, fileType);
33
+ } catch (err) {
34
+ throw createNodeError(err, "readdir", this.path);
35
+ }
36
+ }
37
+ read(callback) {
38
+ if (callback !== undefined) {
39
+ if (typeof callback !== "function") {
40
+ throw new TypeError("The \"callback\" argument must be of type function");
41
+ }
42
+ try {
43
+ this._assertOpen();
44
+ const dirent = this.readSync();
45
+ Promise.resolve().then(() => callback(null, dirent));
46
+ } catch (err) {
47
+ Promise.resolve().then(() => callback(err, null));
48
+ }
49
+ return;
50
+ }
51
+ return new Promise((resolve, reject) => {
52
+ try {
53
+ this._assertOpen();
54
+ resolve(this.readSync());
55
+ } catch (err) {
56
+ reject(err);
57
+ }
58
+ });
59
+ }
60
+ closeSync() {
61
+ this._assertOpen();
62
+ this._closed = true;
63
+ try {
64
+ this._enumerator.close(null);
65
+ } catch {}
66
+ this._enumerator = null;
67
+ }
68
+ close(callback) {
69
+ if (callback !== undefined) {
70
+ if (typeof callback !== "function") {
71
+ throw new TypeError("The \"callback\" argument must be of type function");
72
+ }
73
+ try {
74
+ this.closeSync();
75
+ Promise.resolve().then(() => callback(null));
76
+ } catch (err) {
77
+ Promise.resolve().then(() => callback(err));
78
+ }
79
+ return;
80
+ }
81
+ return new Promise((resolve, reject) => {
82
+ try {
83
+ this.closeSync();
84
+ resolve();
85
+ } catch (err) {
86
+ reject(err);
87
+ }
88
+ });
89
+ }
90
+ async *[Symbol.asyncIterator]() {
91
+ try {
92
+ while (true) {
93
+ const dirent = await this.read();
94
+ if (dirent === null) break;
95
+ yield dirent;
96
+ }
97
+ } finally {
98
+ if (!this._closed) {
99
+ await this.close();
100
+ }
101
+ }
102
+ }
103
+ };
103
104
  function _openDir(pathStr) {
104
- const file = Gio.File.new_for_path(pathStr);
105
- let enumerator;
106
- try {
107
- enumerator = file.enumerate_children(DIR_ATTRS, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
108
- } catch (err) {
109
- throw createNodeError(err, "opendir", pathStr);
110
- }
111
- return new Dir(pathStr, enumerator);
105
+ const file = Gio.File.new_for_path(pathStr);
106
+ let enumerator;
107
+ try {
108
+ enumerator = file.enumerate_children(DIR_ATTRS, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
109
+ } catch (err) {
110
+ throw createNodeError(err, "opendir", pathStr);
111
+ }
112
+ return new Dir(pathStr, enumerator);
112
113
  }
113
114
  function opendirSync(path) {
114
- return _openDir(normalizePath(path));
115
+ return _openDir(normalizePath(path));
115
116
  }
116
117
  function opendir(path, optionsOrCallback, callback) {
117
- let cb;
118
- if (typeof optionsOrCallback === "function") {
119
- cb = optionsOrCallback;
120
- } else {
121
- cb = callback;
122
- }
123
- if (typeof cb !== "function") {
124
- throw new TypeError('The "callback" argument must be of type function');
125
- }
126
- Promise.resolve().then(() => {
127
- try {
128
- const dir = opendirSync(path);
129
- cb(null, dir);
130
- } catch (err) {
131
- cb(err, null);
132
- }
133
- });
118
+ let cb;
119
+ if (typeof optionsOrCallback === "function") {
120
+ cb = optionsOrCallback;
121
+ } else {
122
+ cb = callback;
123
+ }
124
+ if (typeof cb !== "function") {
125
+ throw new TypeError("The \"callback\" argument must be of type function");
126
+ }
127
+ Promise.resolve().then(() => {
128
+ try {
129
+ const dir = opendirSync(path);
130
+ cb(null, dir);
131
+ } catch (err) {
132
+ cb(err, null);
133
+ }
134
+ });
134
135
  }
135
136
  async function opendirAsync(path, _options) {
136
- return new Promise((resolve, reject) => {
137
- const pathStr = normalizePath(path);
138
- const file = Gio.File.new_for_path(pathStr);
139
- file.enumerate_children_async(
140
- DIR_ATTRS,
141
- Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
142
- GLib.PRIORITY_DEFAULT,
143
- null,
144
- (_source, result) => {
145
- try {
146
- const enumerator = file.enumerate_children_finish(result);
147
- resolve(new Dir(pathStr, enumerator));
148
- } catch (err) {
149
- reject(createNodeError(err, "opendir", pathStr));
150
- }
151
- }
152
- );
153
- });
137
+ return new Promise((resolve, reject) => {
138
+ const pathStr = normalizePath(path);
139
+ const file = Gio.File.new_for_path(pathStr);
140
+ file.enumerate_children_async(DIR_ATTRS, Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, GLib.PRIORITY_DEFAULT, null, (_source, result) => {
141
+ try {
142
+ const enumerator = file.enumerate_children_finish(result);
143
+ resolve(new Dir(pathStr, enumerator));
144
+ } catch (err) {
145
+ reject(createNodeError(err, "opendir", pathStr));
146
+ }
147
+ });
148
+ });
154
149
  }
155
- export {
156
- Dir,
157
- opendir,
158
- opendirAsync,
159
- opendirSync
160
- };
150
+
151
+ //#endregion
152
+ export { Dir, opendir, opendirAsync, opendirSync };
package/lib/esm/dirent.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import Gio from "@girs/gio-2.0";
2
2
  import { basename, dirname } from "node:path";
3
+
4
+ //#region src/dirent.ts
3
5
  const S_IFMT = 61440;
4
6
  const S_IFSOCK = 49152;
5
7
  const S_IFLNK = 40960;
@@ -8,132 +10,139 @@ const S_IFBLK = 24576;
8
10
  const S_IFDIR = 16384;
9
11
  const S_IFCHR = 8192;
10
12
  const S_IFIFO = 4096;
11
- class Dirent {
12
- /**
13
- * The file name that this `fs.Dirent` object refers to. The type of this
14
- * value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
15
- * @since v10.10.0
16
- */
17
- name;
18
- /**
19
- * The path to the parent directory of the file this `fs.Dirent` object refers to.
20
- * @since v20.12.0, v18.20.0
21
- */
22
- parentPath;
23
- _isFile = false;
24
- _isDirectory = false;
25
- _isBlockDevice = false;
26
- _isCharacterDevice = false;
27
- _isSymbolicLink = false;
28
- _isFIFO = false;
29
- _isSocket = false;
30
- /** This is not part of node.fs and is used internal by gjsify */
31
- _file;
32
- /** This is not part of node.fs and is used internal by gjsify */
33
- constructor(path, filename, fileType) {
34
- if (!filename) filename = basename(path);
35
- this.name = filename;
36
- this.parentPath = dirname(path);
37
- this._file = Gio.File.new_for_path(path);
38
- const type = fileType ?? this._file.query_file_type(Gio.FileQueryInfoFlags.NONE, null);
39
- switch (type) {
40
- case Gio.FileType.DIRECTORY:
41
- this._isDirectory = true;
42
- break;
43
- case Gio.FileType.MOUNTABLE:
44
- break;
45
- case Gio.FileType.REGULAR:
46
- this._isFile = true;
47
- break;
48
- case Gio.FileType.SYMBOLIC_LINK:
49
- case Gio.FileType.SHORTCUT:
50
- this._isSymbolicLink = true;
51
- break;
52
- case Gio.FileType.SPECIAL:
53
- this._classifySpecialFile(path);
54
- break;
55
- }
56
- }
57
- /**
58
- * Classify a SPECIAL file type using the unix::mode attribute from Gio.FileInfo.
59
- * Falls back to marking nothing if the mode attribute is unavailable.
60
- */
61
- _classifySpecialFile(path) {
62
- try {
63
- const file = Gio.File.new_for_path(path);
64
- const info = file.query_info("unix::mode", Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
65
- const mode = info.get_attribute_uint32("unix::mode");
66
- if (mode === 0) return;
67
- const fmt = mode & S_IFMT;
68
- switch (fmt) {
69
- case S_IFBLK:
70
- this._isBlockDevice = true;
71
- break;
72
- case S_IFCHR:
73
- this._isCharacterDevice = true;
74
- break;
75
- case S_IFSOCK:
76
- this._isSocket = true;
77
- break;
78
- case S_IFIFO:
79
- this._isFIFO = true;
80
- break;
81
- }
82
- } catch {
83
- }
84
- }
85
- /**
86
- * Returns `true` if the `fs.Dirent` object describes a regular file.
87
- * @since v10.10.0
88
- */
89
- isFile() {
90
- return this._isFile;
91
- }
92
- /**
93
- * Returns `true` if the `fs.Dirent` object describes a file system
94
- * directory.
95
- * @since v10.10.0
96
- */
97
- isDirectory() {
98
- return this._isDirectory;
99
- }
100
- /**
101
- * Returns `true` if the `fs.Dirent` object describes a block device.
102
- * @since v10.10.0
103
- */
104
- isBlockDevice() {
105
- return this._isBlockDevice;
106
- }
107
- /**
108
- * Returns `true` if the `fs.Dirent` object describes a character device.
109
- * @since v10.10.0
110
- */
111
- isCharacterDevice() {
112
- return this._isCharacterDevice;
113
- }
114
- /**
115
- * Returns `true` if the `fs.Dirent` object describes a symbolic link.
116
- * @since v10.10.0
117
- */
118
- isSymbolicLink() {
119
- return this._isSymbolicLink;
120
- }
121
- /**
122
- * Returns `true` if the `fs.Dirent` object describes a first-in-first-out
123
- * (FIFO) pipe.
124
- * @since v10.10.0
125
- */
126
- isFIFO() {
127
- return this._isFIFO;
128
- }
129
- /**
130
- * Returns `true` if the `fs.Dirent` object describes a socket.
131
- * @since v10.10.0
132
- */
133
- isSocket() {
134
- return this._isSocket;
135
- }
136
- }
137
- export {
138
- Dirent
13
+ /**
14
+ * A representation of a directory entry, which can be a file or a subdirectory
15
+ * within the directory, as returned by reading from an `fs.Dir`. The
16
+ * directory entry is a combination of the file name and file type pairs.
17
+ *
18
+ * Additionally, when {@link readdir} or {@link readdirSync} is called with
19
+ * the `withFileTypes` option set to `true`, the resulting array is filled with `fs.Dirent` objects, rather than strings or `Buffer` s.
20
+ * @since v10.10.0
21
+ */
22
+ var Dirent = class {
23
+ /**
24
+ * The file name that this `fs.Dirent` object refers to. The type of this
25
+ * value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
26
+ * @since v10.10.0
27
+ */
28
+ name;
29
+ /**
30
+ * The path to the parent directory of the file this `fs.Dirent` object refers to.
31
+ * @since v20.12.0, v18.20.0
32
+ */
33
+ parentPath;
34
+ _isFile = false;
35
+ _isDirectory = false;
36
+ _isBlockDevice = false;
37
+ _isCharacterDevice = false;
38
+ _isSymbolicLink = false;
39
+ _isFIFO = false;
40
+ _isSocket = false;
41
+ /** This is not part of node.fs and is used internal by gjsify */
42
+ _file;
43
+ /** This is not part of node.fs and is used internal by gjsify */
44
+ constructor(path, filename, fileType) {
45
+ if (!filename) filename = basename(path);
46
+ this.name = filename;
47
+ this.parentPath = dirname(path);
48
+ this._file = Gio.File.new_for_path(path);
49
+ const type = fileType ?? this._file.query_file_type(Gio.FileQueryInfoFlags.NONE, null);
50
+ switch (type) {
51
+ case Gio.FileType.DIRECTORY:
52
+ this._isDirectory = true;
53
+ break;
54
+ case Gio.FileType.MOUNTABLE: break;
55
+ case Gio.FileType.REGULAR:
56
+ this._isFile = true;
57
+ break;
58
+ case Gio.FileType.SYMBOLIC_LINK:
59
+ case Gio.FileType.SHORTCUT:
60
+ this._isSymbolicLink = true;
61
+ break;
62
+ case Gio.FileType.SPECIAL:
63
+ this._classifySpecialFile(path);
64
+ break;
65
+ }
66
+ }
67
+ /**
68
+ * Classify a SPECIAL file type using the unix::mode attribute from Gio.FileInfo.
69
+ * Falls back to marking nothing if the mode attribute is unavailable.
70
+ */
71
+ _classifySpecialFile(path) {
72
+ try {
73
+ const file = Gio.File.new_for_path(path);
74
+ const info = file.query_info("unix::mode", Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
75
+ const mode = info.get_attribute_uint32("unix::mode");
76
+ if (mode === 0) return;
77
+ const fmt = mode & S_IFMT;
78
+ switch (fmt) {
79
+ case S_IFBLK:
80
+ this._isBlockDevice = true;
81
+ break;
82
+ case S_IFCHR:
83
+ this._isCharacterDevice = true;
84
+ break;
85
+ case S_IFSOCK:
86
+ this._isSocket = true;
87
+ break;
88
+ case S_IFIFO:
89
+ this._isFIFO = true;
90
+ break;
91
+ }
92
+ } catch {}
93
+ }
94
+ /**
95
+ * Returns `true` if the `fs.Dirent` object describes a regular file.
96
+ * @since v10.10.0
97
+ */
98
+ isFile() {
99
+ return this._isFile;
100
+ }
101
+ /**
102
+ * Returns `true` if the `fs.Dirent` object describes a file system
103
+ * directory.
104
+ * @since v10.10.0
105
+ */
106
+ isDirectory() {
107
+ return this._isDirectory;
108
+ }
109
+ /**
110
+ * Returns `true` if the `fs.Dirent` object describes a block device.
111
+ * @since v10.10.0
112
+ */
113
+ isBlockDevice() {
114
+ return this._isBlockDevice;
115
+ }
116
+ /**
117
+ * Returns `true` if the `fs.Dirent` object describes a character device.
118
+ * @since v10.10.0
119
+ */
120
+ isCharacterDevice() {
121
+ return this._isCharacterDevice;
122
+ }
123
+ /**
124
+ * Returns `true` if the `fs.Dirent` object describes a symbolic link.
125
+ * @since v10.10.0
126
+ */
127
+ isSymbolicLink() {
128
+ return this._isSymbolicLink;
129
+ }
130
+ /**
131
+ * Returns `true` if the `fs.Dirent` object describes a first-in-first-out
132
+ * (FIFO) pipe.
133
+ * @since v10.10.0
134
+ */
135
+ isFIFO() {
136
+ return this._isFIFO;
137
+ }
138
+ /**
139
+ * Returns `true` if the `fs.Dirent` object describes a socket.
140
+ * @since v10.10.0
141
+ */
142
+ isSocket() {
143
+ return this._isSocket;
144
+ }
139
145
  };
146
+
147
+ //#endregion
148
+ export { Dirent };
@@ -1,33 +1,36 @@
1
1
  import { Buffer } from "node: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;
2
+
3
+ //#region src/encoding.ts
4
+ function getEncodingFromOptions(options = {
5
+ encoding: null,
6
+ flag: "r"
7
+ }, defaultEncoding = "utf8") {
8
+ if (options === null) {
9
+ return defaultEncoding;
10
+ }
11
+ if (typeof options === "string") {
12
+ return options;
13
+ }
14
+ if (typeof options === "object" && typeof options.encoding === "string") {
15
+ return options.encoding;
16
+ }
17
+ return defaultEncoding;
13
18
  }
14
19
  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
+ if (encoding === "buffer") {
21
+ return Buffer.from(data);
22
+ }
23
+ const decoder = new TextDecoder(encoding);
24
+ return decoder.decode(data);
20
25
  }
21
26
  function decode(str, encoding) {
22
- if (!encoding) return str;
23
- else {
24
- const decoder = new TextDecoder(encoding);
25
- const encoder = new TextEncoder();
26
- return decoder.decode(encoder.encode(str));
27
- }
27
+ if (!encoding) return str;
28
+ else {
29
+ const decoder = new TextDecoder(encoding);
30
+ const encoder = new TextEncoder();
31
+ return decoder.decode(encoder.encode(str));
32
+ }
28
33
  }
29
- export {
30
- decode,
31
- encodeUint8Array,
32
- getEncodingFromOptions
33
- };
34
+
35
+ //#endregion
36
+ export { decode, encodeUint8Array, getEncodingFromOptions };