@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
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Abortable } from 'events';
|
|
2
|
+
import type { ObjectEncodingOptions, OpenMode } from 'fs'; // Types from @types/node
|
|
3
|
+
|
|
4
|
+
export type ReadOptions =
|
|
5
|
+
| (ObjectEncodingOptions & Abortable & {
|
|
6
|
+
flag?: OpenMode | undefined;
|
|
7
|
+
})
|
|
8
|
+
| BufferEncoding
|
|
9
|
+
| null
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { existsSync } from './sync.js';
|
|
2
|
+
|
|
3
|
+
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
4
|
+
|
|
5
|
+
export function randomName(): string {
|
|
6
|
+
return [...Array(6)].map(() =>
|
|
7
|
+
CHARS[Math.floor(Math.random() * CHARS.length)]
|
|
8
|
+
).join("");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// credits: https://github.com/denoland/deno_std/blob/63be40277264e71af60f9b118a2cb569e02beeab/node/_fs/_fs_mkdtemp.ts#L98
|
|
12
|
+
export function tempDirPath(prefix: string): string {
|
|
13
|
+
let path: string;
|
|
14
|
+
do {
|
|
15
|
+
path = prefix + randomName();
|
|
16
|
+
} while (existsSync(path));
|
|
17
|
+
|
|
18
|
+
return path;
|
|
19
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Writable } from "stream";
|
|
2
|
+
import { fileURLToPath, URL } from "url";
|
|
3
|
+
import { open, write, close } from "./callback.js";
|
|
4
|
+
|
|
5
|
+
import type { OpenFlags } from './types/index.js';
|
|
6
|
+
import type { PathLike, WriteStream as IWriteStream } from 'fs';
|
|
7
|
+
import type { CreateWriteStreamOptions } from 'fs/promises'; // Types from @types/node
|
|
8
|
+
|
|
9
|
+
// From Deno
|
|
10
|
+
const kIsPerformingIO = Symbol("kIsPerformingIO");
|
|
11
|
+
const kIoDone = Symbol("kIoDone");
|
|
12
|
+
|
|
13
|
+
export function toPathIfFileURL(
|
|
14
|
+
fileURLOrPath: string | Buffer | URL,
|
|
15
|
+
): string | Buffer {
|
|
16
|
+
if (!(fileURLOrPath instanceof URL)) {
|
|
17
|
+
return fileURLOrPath;
|
|
18
|
+
}
|
|
19
|
+
return fileURLToPath(fileURLOrPath);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Credits https://github.com/denoland/deno_std/blob/main/node/internal/fs/streams.ts
|
|
23
|
+
export class WriteStream extends Writable implements IWriteStream {
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Closes `writeStream`. Optionally accepts a
|
|
27
|
+
* callback that will be executed once the `writeStream`is closed.
|
|
28
|
+
* @since v0.9.4
|
|
29
|
+
*/
|
|
30
|
+
close(callback?: (err?: NodeJS.ErrnoException | null) => void, err: Error | null = null): void {
|
|
31
|
+
if (!this.fd) {
|
|
32
|
+
callback(err);
|
|
33
|
+
} else {
|
|
34
|
+
close(this.fd, (er?: Error | null) => {
|
|
35
|
+
callback(er || err);
|
|
36
|
+
});
|
|
37
|
+
this.fd = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The number of bytes written so far. Does not include data that is still queued
|
|
42
|
+
* for writing.
|
|
43
|
+
* @since v0.4.7
|
|
44
|
+
*/
|
|
45
|
+
bytesWritten = 0;
|
|
46
|
+
/**
|
|
47
|
+
* The path to the file the stream is writing to as specified in the first
|
|
48
|
+
* 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
|
|
49
|
+
* `Buffer`.
|
|
50
|
+
* @since v0.1.93
|
|
51
|
+
*/
|
|
52
|
+
path: string | Buffer;
|
|
53
|
+
/**
|
|
54
|
+
* This property is `true` if the underlying file has not been opened yet,
|
|
55
|
+
* i.e. before the `'ready'` event is emitted.
|
|
56
|
+
* @since v11.2.0
|
|
57
|
+
*/
|
|
58
|
+
pending: boolean;
|
|
59
|
+
|
|
60
|
+
fd: number | null = null;
|
|
61
|
+
flags: OpenFlags = "w";
|
|
62
|
+
mode = 0o666;
|
|
63
|
+
pos = 0;
|
|
64
|
+
[kIsPerformingIO] = false;
|
|
65
|
+
|
|
66
|
+
constructor(path: PathLike, opts: CreateWriteStreamOptions = {}) {
|
|
67
|
+
super(opts);
|
|
68
|
+
this.path = toPathIfFileURL(path);
|
|
69
|
+
|
|
70
|
+
if (opts.encoding) {
|
|
71
|
+
this.setDefaultEncoding(opts.encoding);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
override _construct(callback: (err?: Error) => void) {
|
|
76
|
+
open(
|
|
77
|
+
this.path.toString(),
|
|
78
|
+
this.flags!,
|
|
79
|
+
this.mode!,
|
|
80
|
+
(err: Error | null, fd: number) => {
|
|
81
|
+
if (err) {
|
|
82
|
+
callback(err);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.fd = fd;
|
|
87
|
+
callback();
|
|
88
|
+
this.emit("open", this.fd);
|
|
89
|
+
this.emit("ready");
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
override _write(
|
|
95
|
+
data: Buffer,
|
|
96
|
+
_encoding: string,
|
|
97
|
+
cb: (err?: Error | null) => void,
|
|
98
|
+
) {
|
|
99
|
+
this[kIsPerformingIO] = true;
|
|
100
|
+
write(
|
|
101
|
+
this.fd!,
|
|
102
|
+
data,
|
|
103
|
+
0,
|
|
104
|
+
data.length,
|
|
105
|
+
this.pos,
|
|
106
|
+
(er: Error | null, bytes: number) => {
|
|
107
|
+
this[kIsPerformingIO] = false;
|
|
108
|
+
if (this.destroyed) {
|
|
109
|
+
// Tell ._destroy() that it's safe to close the fd now.
|
|
110
|
+
cb(er);
|
|
111
|
+
return this.emit(kIoDone, er);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (er) {
|
|
115
|
+
return cb(er);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.bytesWritten += bytes;
|
|
119
|
+
cb();
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
if (this.pos !== undefined) {
|
|
124
|
+
this.pos += data.length;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
override _destroy(err: Error, cb: (err?: Error | null) => void) {
|
|
129
|
+
if (this[kIsPerformingIO]) {
|
|
130
|
+
this.once(kIoDone, (er) => this.close(cb, err || er));
|
|
131
|
+
} else {
|
|
132
|
+
this.close(cb, err);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
export function createWriteStream(
|
|
139
|
+
path: string | Buffer,
|
|
140
|
+
opts: CreateWriteStreamOptions,
|
|
141
|
+
): WriteStream {
|
|
142
|
+
return new WriteStream(path, opts);
|
|
143
|
+
}
|
package/test/file.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Hello World
|
package/test/watch.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// test
|