@gjsify/fs 0.0.1 → 0.0.2
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 +131 -0
- package/lib/cjs/dirent.js +127 -0
- package/lib/cjs/encoding.js +53 -0
- package/lib/cjs/file-handle.js +473 -0
- package/lib/cjs/fs-watcher.js +79 -0
- package/lib/cjs/index.js +95 -0
- package/lib/cjs/promises.js +189 -0
- package/lib/cjs/read-stream.js +107 -0
- package/lib/cjs/stats.js +74 -0
- package/lib/cjs/sync.js +155 -0
- package/lib/cjs/types/encoding-option.js +15 -0
- package/lib/cjs/types/file-read-options.js +15 -0
- package/lib/cjs/types/file-read-result.js +15 -0
- package/lib/cjs/types/flag-and-open-mode.js +15 -0
- package/lib/cjs/types/index.js +22 -0
- package/lib/cjs/types/open-flags.js +15 -0
- package/lib/cjs/types/read-options.js +15 -0
- package/lib/cjs/utils.js +37 -0
- package/lib/cjs/write-stream.js +135 -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/src/callback.ts
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { open as openP, rm as rmP } from './promises.js'
|
|
2
|
+
import { warnNotImplemented } from '@gjsify/utils';
|
|
3
|
+
import { PathLike, OpenMode, Mode, ReadPosition, ReadAsyncOptions, NoParamCallback, RmOptions } from 'fs';
|
|
4
|
+
import { FileHandle } from './file-handle.js';
|
|
5
|
+
import { Buffer } from 'buffer';
|
|
6
|
+
|
|
7
|
+
export { realpath } from '@gjsify/deno_std/node/_fs/_fs_realpath';
|
|
8
|
+
export { readdir } from '@gjsify/deno_std/node/_fs/_fs_readdir';
|
|
9
|
+
export { symlink } from '@gjsify/deno_std/node/_fs/_fs_symlink';
|
|
10
|
+
export { lstat } from '@gjsify/deno_std/node/_fs/_fs_lstat';
|
|
11
|
+
export { stat } from '@gjsify/deno_std/node/_fs/_fs_stat';
|
|
12
|
+
|
|
13
|
+
type OpenCallback = (err: NodeJS.ErrnoException | null, fd: number) => void;
|
|
14
|
+
|
|
15
|
+
type WriteStrCallback = (err: NodeJS.ErrnoException | null, written: number, str: string) => void;
|
|
16
|
+
type WriteBufCallback = <TBuffer extends NodeJS.ArrayBufferView> (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void;
|
|
17
|
+
|
|
18
|
+
type ReadCallback = (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: NodeJS.ArrayBufferView) => void;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details.
|
|
22
|
+
*
|
|
23
|
+
* `mode` sets the file mode (permission and sticky bits), but only if the file was
|
|
24
|
+
* created. On Windows, only the write permission can be manipulated; see {@link chmod}.
|
|
25
|
+
*
|
|
26
|
+
* The callback gets two arguments `(err, fd)`.
|
|
27
|
+
*
|
|
28
|
+
* Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
|
|
29
|
+
* by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains
|
|
30
|
+
* a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).
|
|
31
|
+
*
|
|
32
|
+
* Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc.
|
|
33
|
+
* @since v0.0.2
|
|
34
|
+
* @param [flags='r'] See `support of file system `flags``.
|
|
35
|
+
* @param [mode=0o666]
|
|
36
|
+
*/
|
|
37
|
+
export function open(path: PathLike, flags: OpenMode | undefined, mode: Mode | undefined | null, callback: OpenCallback): void;
|
|
38
|
+
/**
|
|
39
|
+
* Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
|
|
40
|
+
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
|
41
|
+
* @param [flags='r'] See `support of file system `flags``.
|
|
42
|
+
*/
|
|
43
|
+
export function open(path: PathLike, flags: OpenMode | undefined, callback: OpenCallback): void;
|
|
44
|
+
/**
|
|
45
|
+
* Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
|
|
46
|
+
* @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
|
|
47
|
+
*/
|
|
48
|
+
export function open(path: PathLike, callback: OpenCallback): void;
|
|
49
|
+
|
|
50
|
+
export function open(path: PathLike, ...args: any[]): void {
|
|
51
|
+
let flags: number | OpenMode | undefined;
|
|
52
|
+
let mode: Mode | undefined | null;
|
|
53
|
+
let callback: OpenCallback
|
|
54
|
+
|
|
55
|
+
switch (args.length) {
|
|
56
|
+
case 1:
|
|
57
|
+
callback = args[0]
|
|
58
|
+
break;
|
|
59
|
+
case 2:
|
|
60
|
+
flags = args[0]
|
|
61
|
+
callback = args[1]
|
|
62
|
+
break;
|
|
63
|
+
case 3:
|
|
64
|
+
flags = args[0]
|
|
65
|
+
mode = args[1]
|
|
66
|
+
callback = args[2]
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
openP(path, flags as any, mode)
|
|
73
|
+
.then((fileHandle) => {
|
|
74
|
+
callback(null, fileHandle.fd);
|
|
75
|
+
})
|
|
76
|
+
.catch((err) => {
|
|
77
|
+
callback(err, -1);
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Write `buffer` to the file specified by `fd`.
|
|
83
|
+
*
|
|
84
|
+
* `offset` determines the part of the buffer to be written, and `length` is
|
|
85
|
+
* an integer specifying the number of bytes to write.
|
|
86
|
+
*
|
|
87
|
+
* `position` refers to the offset from the beginning of the file where this data
|
|
88
|
+
* should be written. If `typeof position !== 'number'`, the data will be written
|
|
89
|
+
* at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html).
|
|
90
|
+
*
|
|
91
|
+
* The callback will be given three arguments `(err, bytesWritten, buffer)` where`bytesWritten` specifies how many _bytes_ were written from `buffer`.
|
|
92
|
+
*
|
|
93
|
+
* If this method is invoked as its `util.promisify()` ed version, it returns
|
|
94
|
+
* a promise for an `Object` with `bytesWritten` and `buffer` properties.
|
|
95
|
+
*
|
|
96
|
+
* It is unsafe to use `fs.write()` multiple times on the same file without waiting
|
|
97
|
+
* for the callback. For this scenario, {@link createWriteStream} is
|
|
98
|
+
* recommended.
|
|
99
|
+
*
|
|
100
|
+
* On Linux, positional writes don't work when the file is opened in append mode.
|
|
101
|
+
* The kernel ignores the position argument and always appends the data to
|
|
102
|
+
* the end of the file.
|
|
103
|
+
* @since v0.0.2
|
|
104
|
+
*/
|
|
105
|
+
export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
|
106
|
+
fd: number,
|
|
107
|
+
buffer: TBuffer,
|
|
108
|
+
offset: number | undefined | null,
|
|
109
|
+
length: number | undefined | null,
|
|
110
|
+
position: number | undefined | null,
|
|
111
|
+
callback: WriteBufCallback
|
|
112
|
+
): void;
|
|
113
|
+
/**
|
|
114
|
+
* Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
|
115
|
+
* @param fd A file descriptor.
|
|
116
|
+
* @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
|
|
117
|
+
* @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
|
|
118
|
+
*/
|
|
119
|
+
export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
|
120
|
+
fd: number,
|
|
121
|
+
buffer: TBuffer,
|
|
122
|
+
offset: number | undefined | null,
|
|
123
|
+
length: number | undefined | null,
|
|
124
|
+
callback: WriteBufCallback
|
|
125
|
+
): void;
|
|
126
|
+
/**
|
|
127
|
+
* Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
|
128
|
+
* @param fd A file descriptor.
|
|
129
|
+
* @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
|
|
130
|
+
*/
|
|
131
|
+
export function write<TBuffer extends NodeJS.ArrayBufferView>(
|
|
132
|
+
fd: number,
|
|
133
|
+
buffer: TBuffer,
|
|
134
|
+
offset: number | undefined | null,
|
|
135
|
+
callback: WriteBufCallback
|
|
136
|
+
): void;
|
|
137
|
+
/**
|
|
138
|
+
* Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
|
|
139
|
+
* @param fd A file descriptor.
|
|
140
|
+
*/
|
|
141
|
+
export function write<TBuffer extends NodeJS.ArrayBufferView>(fd: number, buffer: TBuffer, callback: WriteBufCallback): void;
|
|
142
|
+
/**
|
|
143
|
+
* Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
|
144
|
+
* @param fd A file descriptor.
|
|
145
|
+
* @param string A string to write.
|
|
146
|
+
* @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
|
147
|
+
* @param encoding The expected string encoding.
|
|
148
|
+
*/
|
|
149
|
+
export function write(
|
|
150
|
+
fd: number,
|
|
151
|
+
string: string,
|
|
152
|
+
position: number | undefined | null,
|
|
153
|
+
encoding: BufferEncoding | undefined | null,
|
|
154
|
+
callback: WriteStrCallback
|
|
155
|
+
): void;
|
|
156
|
+
/**
|
|
157
|
+
* Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
|
158
|
+
* @param fd A file descriptor.
|
|
159
|
+
* @param string A string to write.
|
|
160
|
+
* @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
|
|
161
|
+
*/
|
|
162
|
+
export function write(fd: number, string: string, position: number | undefined | null, callback: WriteStrCallback): void;
|
|
163
|
+
/**
|
|
164
|
+
* Asynchronously writes `string` to the file referenced by the supplied file descriptor.
|
|
165
|
+
* @param fd A file descriptor.
|
|
166
|
+
* @param string A string to write.
|
|
167
|
+
*/
|
|
168
|
+
export function write(fd: number, string: string, callback: WriteStrCallback): void;
|
|
169
|
+
|
|
170
|
+
export function write<TBuffer extends NodeJS.ArrayBufferView>(fd: number, data: string | TBuffer, ...args: any[]): void {
|
|
171
|
+
|
|
172
|
+
const fileHandle = FileHandle.getInstance(fd);
|
|
173
|
+
|
|
174
|
+
if (typeof data === 'string') {
|
|
175
|
+
const callback: WriteStrCallback = args[args.length -1];
|
|
176
|
+
|
|
177
|
+
fileHandle.write(data, ...args.pop())
|
|
178
|
+
.then((res) => {
|
|
179
|
+
callback(null, res.bytesWritten, res.buffer);
|
|
180
|
+
})
|
|
181
|
+
.catch((err) => {
|
|
182
|
+
callback(err, 0, '');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const callback: WriteBufCallback = args[args.length -1];
|
|
189
|
+
const offset: number | undefined = args[0];
|
|
190
|
+
const length: number | undefined = args[1];
|
|
191
|
+
const position: number | undefined = args[2];
|
|
192
|
+
|
|
193
|
+
fileHandle.write(data, offset, length, position)
|
|
194
|
+
.then((res) => {
|
|
195
|
+
callback(null, res.bytesWritten, res.buffer);
|
|
196
|
+
})
|
|
197
|
+
.catch((err) => {
|
|
198
|
+
callback(err, 0, Buffer.from([]));
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Read data from the file specified by `fd`.
|
|
204
|
+
*
|
|
205
|
+
* The callback is given the three arguments, `(err, bytesRead, buffer)`.
|
|
206
|
+
*
|
|
207
|
+
* If the file is not modified concurrently, the end-of-file is reached when the
|
|
208
|
+
* number of bytes read is zero.
|
|
209
|
+
*
|
|
210
|
+
* If this method is invoked as its `util.promisify()` ed version, it returns
|
|
211
|
+
* a promise for an `Object` with `bytesRead` and `buffer` properties.
|
|
212
|
+
* @since v0.0.2
|
|
213
|
+
* @param buffer The buffer that the data will be written to.
|
|
214
|
+
* @param offset The position in `buffer` to write the data to.
|
|
215
|
+
* @param length The number of bytes to read.
|
|
216
|
+
* @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
|
|
217
|
+
* `position` is an integer, the file position will be unchanged.
|
|
218
|
+
*/
|
|
219
|
+
export function read<TBuffer extends NodeJS.ArrayBufferView>(
|
|
220
|
+
fd: number,
|
|
221
|
+
buffer: TBuffer,
|
|
222
|
+
offset: number,
|
|
223
|
+
length: number,
|
|
224
|
+
position: ReadPosition | null,
|
|
225
|
+
callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
|
|
226
|
+
): void;
|
|
227
|
+
/**
|
|
228
|
+
* Similar to the above `fs.read` function, this version takes an optional `options` object.
|
|
229
|
+
* If not otherwise specified in an `options` object,
|
|
230
|
+
* `buffer` defaults to `Buffer.alloc(16384)`,
|
|
231
|
+
* `offset` defaults to `0`,
|
|
232
|
+
* `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0
|
|
233
|
+
* `position` defaults to `null`
|
|
234
|
+
* @since v12.17.0, 13.11.0
|
|
235
|
+
*/
|
|
236
|
+
export function read<TBuffer extends NodeJS.ArrayBufferView>(
|
|
237
|
+
fd: number,
|
|
238
|
+
options: ReadAsyncOptions<TBuffer>,
|
|
239
|
+
callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
|
|
240
|
+
): void;
|
|
241
|
+
export function read(fd: number, callback: ReadCallback): void;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Read data from the file specified by `fd`.
|
|
245
|
+
*
|
|
246
|
+
* The callback is given the three arguments, `(err, bytesRead, buffer)`.
|
|
247
|
+
*
|
|
248
|
+
* If the file is not modified concurrently, the end-of-file is reached when the
|
|
249
|
+
* number of bytes read is zero.
|
|
250
|
+
*
|
|
251
|
+
* If this method is invoked as its `util.promisify()` ed version, it returns
|
|
252
|
+
* a promise for an `Object` with `bytesRead` and `buffer` properties.
|
|
253
|
+
* @since v0.0.2
|
|
254
|
+
* @param buffer The buffer that the data will be written to.
|
|
255
|
+
* @param offset The position in `buffer` to write the data to.
|
|
256
|
+
* @param length The number of bytes to read.
|
|
257
|
+
* @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
|
|
258
|
+
* `position` is an integer, the file position will be unchanged.
|
|
259
|
+
*/
|
|
260
|
+
export function read<TBuffer extends NodeJS.ArrayBufferView>(
|
|
261
|
+
fd: number,
|
|
262
|
+
buffer: TBuffer,
|
|
263
|
+
offset: number,
|
|
264
|
+
length: number,
|
|
265
|
+
position: ReadPosition | null,
|
|
266
|
+
callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
|
|
267
|
+
): void;
|
|
268
|
+
/**
|
|
269
|
+
* Similar to the above `fs.read` function, this version takes an optional `options` object.
|
|
270
|
+
* If not otherwise specified in an `options` object,
|
|
271
|
+
* `buffer` defaults to `Buffer.alloc(16384)`,
|
|
272
|
+
* `offset` defaults to `0`,
|
|
273
|
+
* `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0
|
|
274
|
+
* `position` defaults to `null`
|
|
275
|
+
* @since v12.17.0, 13.11.0
|
|
276
|
+
*/
|
|
277
|
+
export function read<TBuffer extends NodeJS.ArrayBufferView>(
|
|
278
|
+
fd: number,
|
|
279
|
+
options: ReadAsyncOptions<TBuffer>,
|
|
280
|
+
callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void
|
|
281
|
+
): void;
|
|
282
|
+
export function read(fd: number, callback: ReadCallback): void;
|
|
283
|
+
|
|
284
|
+
export function read(fd: number, ...args: any[]): void {
|
|
285
|
+
|
|
286
|
+
const fileHandle = FileHandle.getInstance(fd);
|
|
287
|
+
|
|
288
|
+
const callback: ReadCallback = args[args.length -1];
|
|
289
|
+
const err = new Error(warnNotImplemented('fs.read'));
|
|
290
|
+
callback(err, 0, Buffer.from(''));
|
|
291
|
+
|
|
292
|
+
let buffer: NodeJS.ArrayBufferView | undefined;
|
|
293
|
+
let offset: number | null | undefined;
|
|
294
|
+
let length: number | null | undefined;
|
|
295
|
+
let position: ReadPosition | null | undefined;
|
|
296
|
+
|
|
297
|
+
if (typeof args[0] === 'object') {
|
|
298
|
+
const options: ReadAsyncOptions<any> = args[0];
|
|
299
|
+
buffer = options.buffer;
|
|
300
|
+
offset = options.offset;
|
|
301
|
+
length = options.length;
|
|
302
|
+
position = options.position;
|
|
303
|
+
} else {
|
|
304
|
+
buffer = args[0];
|
|
305
|
+
offset = args[1];
|
|
306
|
+
length = args[2];
|
|
307
|
+
position = args[3];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
fileHandle.read(buffer, offset, length, position)
|
|
312
|
+
.then((res) => {
|
|
313
|
+
callback(null, res.bytesRead, res.buffer);
|
|
314
|
+
})
|
|
315
|
+
.catch((err) => {
|
|
316
|
+
callback(err, 0, Buffer.from([]));
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Closes the file descriptor. No arguments other than a possible exception are
|
|
322
|
+
* given to the completion callback.
|
|
323
|
+
*
|
|
324
|
+
* Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
|
|
325
|
+
* through any other `fs` operation may lead to undefined behavior.
|
|
326
|
+
*
|
|
327
|
+
* See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
|
|
328
|
+
* @since v0.0.2
|
|
329
|
+
*/
|
|
330
|
+
export function close(fd: number, callback?: NoParamCallback): void {
|
|
331
|
+
FileHandle.getInstance(fd).close()
|
|
332
|
+
.then(() => {
|
|
333
|
+
callback(null);
|
|
334
|
+
})
|
|
335
|
+
.catch((err) => callback(err))
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Asynchronously removes files and directories (modeled on the standard POSIX `rm`utility). No arguments other than a possible exception are given to the
|
|
340
|
+
* completion callback.
|
|
341
|
+
* @since v14.14.0
|
|
342
|
+
*/
|
|
343
|
+
export function rm(path: PathLike, callback: NoParamCallback): void;
|
|
344
|
+
export function rm(path: PathLike, options: RmOptions, callback: NoParamCallback): void;
|
|
345
|
+
|
|
346
|
+
export function rm(path: PathLike, ...args: any[]): void {
|
|
347
|
+
|
|
348
|
+
let options: RmOptions = {};
|
|
349
|
+
let callback: NoParamCallback = args[args.length -1];
|
|
350
|
+
|
|
351
|
+
if (args.length >= 2) {
|
|
352
|
+
options = args[0];
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
rmP(path, options)
|
|
356
|
+
.then(() => {
|
|
357
|
+
callback(null);
|
|
358
|
+
})
|
|
359
|
+
.catch((err) => {
|
|
360
|
+
callback(err);
|
|
361
|
+
});
|
|
362
|
+
}
|
package/src/dirent.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import Gio from '@girs/gio-2.0';
|
|
2
|
+
import { basename } from 'path';
|
|
3
|
+
|
|
4
|
+
import type { Dirent as OriginalDirent } from 'fs'; // Types from @types/node
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A representation of a directory entry, which can be a file or a subdirectory
|
|
8
|
+
* within the directory, as returned by reading from an `fs.Dir`. The
|
|
9
|
+
* directory entry is a combination of the file name and file type pairs.
|
|
10
|
+
*
|
|
11
|
+
* Additionally, when {@link readdir} or {@link readdirSync} is called with
|
|
12
|
+
* the `withFileTypes` option set to `true`, the resulting array is filled with `fs.Dirent` objects, rather than strings or `Buffer` s.
|
|
13
|
+
* @since v10.10.0
|
|
14
|
+
*/
|
|
15
|
+
export class Dirent implements OriginalDirent {
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The file name that this `fs.Dirent` object refers to. The type of this
|
|
19
|
+
* value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
|
|
20
|
+
* @since v10.10.0
|
|
21
|
+
*/
|
|
22
|
+
name: string;
|
|
23
|
+
|
|
24
|
+
private _isFile = false;
|
|
25
|
+
private _isDirectory = false;
|
|
26
|
+
private _isBlockDevice = false;
|
|
27
|
+
private _isCharacterDevice = false;
|
|
28
|
+
private _isSymbolicLink = false;
|
|
29
|
+
private _isFIFO = false;
|
|
30
|
+
private _isSocket = false;
|
|
31
|
+
|
|
32
|
+
/** This is not part of node.fs and is used internal by gjsify */
|
|
33
|
+
protected _file: Gio.File;
|
|
34
|
+
|
|
35
|
+
/** This is not part of node.fs and is used internal by gjsify */
|
|
36
|
+
constructor(path: string, filename?: string) {
|
|
37
|
+
if (!filename) filename = basename(path);
|
|
38
|
+
this.name = filename;
|
|
39
|
+
this._file = Gio.File.new_for_path(path);
|
|
40
|
+
const type = this._file.query_file_type(Gio.FileQueryInfoFlags.NONE, null);
|
|
41
|
+
|
|
42
|
+
switch (type) {
|
|
43
|
+
case Gio.FileType.DIRECTORY:
|
|
44
|
+
this._isDirectory = true;
|
|
45
|
+
break;
|
|
46
|
+
case Gio.FileType.MOUNTABLE:
|
|
47
|
+
break;
|
|
48
|
+
case Gio.FileType.REGULAR:
|
|
49
|
+
this._isFile = true;
|
|
50
|
+
break;
|
|
51
|
+
case Gio.FileType.SYMBOLIC_LINK:
|
|
52
|
+
case Gio.FileType.SHORTCUT:
|
|
53
|
+
this._isSymbolicLink = true; // ?
|
|
54
|
+
break;
|
|
55
|
+
case Gio.FileType.SPECIAL:
|
|
56
|
+
// File is a "special" file, such as a socket, fifo, block device, or character device.
|
|
57
|
+
this._isBlockDevice = Gio.unix_is_system_device_path(path);
|
|
58
|
+
// TODO: this._isCharacterDevice =
|
|
59
|
+
// TODO: this._isSocket =
|
|
60
|
+
// TODO: this._isFifo =
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns `true` if the `fs.Dirent` object describes a regular file.
|
|
68
|
+
* @since v10.10.0
|
|
69
|
+
*/
|
|
70
|
+
isFile(): boolean {
|
|
71
|
+
return this._isFile;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns `true` if the `fs.Dirent` object describes a file system
|
|
75
|
+
* directory.
|
|
76
|
+
* @since v10.10.0
|
|
77
|
+
*/
|
|
78
|
+
isDirectory(): boolean {
|
|
79
|
+
return this._isDirectory;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Returns `true` if the `fs.Dirent` object describes a block device.
|
|
83
|
+
* @since v10.10.0
|
|
84
|
+
*/
|
|
85
|
+
isBlockDevice(): boolean {
|
|
86
|
+
return this._isBlockDevice;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns `true` if the `fs.Dirent` object describes a character device.
|
|
90
|
+
* @since v10.10.0
|
|
91
|
+
*/
|
|
92
|
+
isCharacterDevice(): boolean {
|
|
93
|
+
return this._isCharacterDevice;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns `true` if the `fs.Dirent` object describes a symbolic link.
|
|
97
|
+
* @since v10.10.0
|
|
98
|
+
*/
|
|
99
|
+
isSymbolicLink(): boolean {
|
|
100
|
+
return this._isSymbolicLink;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns `true` if the `fs.Dirent` object describes a first-in-first-out
|
|
104
|
+
* (FIFO) pipe.
|
|
105
|
+
* @since v10.10.0
|
|
106
|
+
*/
|
|
107
|
+
isFIFO(): boolean {
|
|
108
|
+
return this._isFIFO;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Returns `true` if the `fs.Dirent` object describes a socket.
|
|
112
|
+
* @since v10.10.0
|
|
113
|
+
*/
|
|
114
|
+
isSocket(): boolean {
|
|
115
|
+
return this._isSocket;
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/encoding.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
|
|
3
|
+
import type { ReadOptions } from './types/index.js';
|
|
4
|
+
import type { ObjectEncodingOptions, BufferEncodingOption } from 'fs'; // Types from @types/node
|
|
5
|
+
|
|
6
|
+
export function getEncodingFromOptions(options: ReadOptions | ObjectEncodingOptions | BufferEncodingOption= { encoding: null, flag: 'r' }, defaultEncoding: null | BufferEncoding | "buffer" = 'utf8'): BufferEncoding | 'buffer' {
|
|
7
|
+
if (options === null) {
|
|
8
|
+
return defaultEncoding;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (typeof options === 'string') {
|
|
12
|
+
return options as BufferEncoding | 'buffer';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (typeof options === 'object' && typeof options.encoding === 'string') {
|
|
16
|
+
return options.encoding;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return defaultEncoding;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function encodeUint8Array(encoding: BufferEncoding | 'buffer', data: Uint8Array) {
|
|
23
|
+
if (encoding === 'buffer') {
|
|
24
|
+
return Buffer.from(data);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const decoder = new TextDecoder(encoding);
|
|
28
|
+
return decoder.decode(data);
|
|
29
|
+
// return byteArray.toString(data);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Credits https://github.com/denoland/deno_std/blob/63be40277264e71af60f9b118a2cb569e02beeab/node/_fs/_fs_mkdtemp.ts#L82
|
|
33
|
+
export function decode(str: string, encoding?: string): string {
|
|
34
|
+
if (!encoding) return str;
|
|
35
|
+
else {
|
|
36
|
+
const decoder = new TextDecoder(encoding);
|
|
37
|
+
const encoder = new TextEncoder();
|
|
38
|
+
return decoder.decode(encoder.encode(str));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from '@gjsify/unit';
|
|
2
|
+
import { promises } from 'fs';
|
|
3
|
+
import { Buffer } from 'buffer';
|
|
4
|
+
|
|
5
|
+
export default async () => {
|
|
6
|
+
await describe('FileHandle', async () => {
|
|
7
|
+
await it(`should open a file for writing`, async () => {
|
|
8
|
+
const path = './test/openP.txt';
|
|
9
|
+
const fileHandle = await promises.open(path, 'w+', 0o666);
|
|
10
|
+
|
|
11
|
+
console.log('FileHandle: file open');
|
|
12
|
+
|
|
13
|
+
let buffWrite = Buffer.from('Hello World', 'utf8'),
|
|
14
|
+
buffStart = 0,
|
|
15
|
+
buffLength = buffWrite.length,
|
|
16
|
+
filePos = 0;
|
|
17
|
+
const res = await fileHandle.write(buffWrite, buffStart, buffLength, filePos);
|
|
18
|
+
console.log('FileHandle: file written');
|
|
19
|
+
|
|
20
|
+
// console.log('written', res.bytesWritten);
|
|
21
|
+
|
|
22
|
+
expect(res.bytesWritten).toBe(buffWrite.length);
|
|
23
|
+
|
|
24
|
+
expect(res.buffer).toBe(buffWrite);
|
|
25
|
+
|
|
26
|
+
await fileHandle.close();
|
|
27
|
+
console.log('FileHandle: file closed');
|
|
28
|
+
|
|
29
|
+
await promises.rm(path);
|
|
30
|
+
console.log('FileHandle: file removed');
|
|
31
|
+
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|