@gjsify/fs 0.3.21 → 0.4.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.
Files changed (69) hide show
  1. package/lib/esm/_virtual/_rolldown/runtime.js +1 -1
  2. package/lib/esm/callback.js +1 -1
  3. package/lib/esm/cp.js +1 -1
  4. package/lib/esm/dir.js +1 -1
  5. package/lib/esm/dirent.js +1 -1
  6. package/lib/esm/encoding.js +1 -1
  7. package/lib/esm/errors.js +1 -1
  8. package/lib/esm/fd-ops.js +1 -1
  9. package/lib/esm/file-handle.js +1 -1
  10. package/lib/esm/fs-watcher.js +1 -1
  11. package/lib/esm/glob.js +1 -1
  12. package/lib/esm/read-stream.js +1 -1
  13. package/lib/esm/stat-watcher.js +1 -1
  14. package/lib/esm/statfs.js +1 -1
  15. package/lib/esm/stats.js +1 -1
  16. package/lib/esm/sync.js +1 -1
  17. package/lib/esm/utils.js +1 -1
  18. package/lib/esm/utimes.js +1 -1
  19. package/lib/esm/write-stream.js +1 -1
  20. package/package.json +51 -48
  21. package/src/callback.spec.ts +0 -296
  22. package/src/callback.ts +0 -684
  23. package/src/cp.spec.ts +0 -181
  24. package/src/cp.ts +0 -328
  25. package/src/dir.spec.ts +0 -204
  26. package/src/dir.ts +0 -199
  27. package/src/dirent.ts +0 -165
  28. package/src/encoding.ts +0 -45
  29. package/src/errors.spec.ts +0 -389
  30. package/src/errors.ts +0 -19
  31. package/src/extended.spec.ts +0 -706
  32. package/src/fd-ops.spec.ts +0 -234
  33. package/src/fd-ops.ts +0 -251
  34. package/src/file-handle.spec.ts +0 -115
  35. package/src/file-handle.ts +0 -856
  36. package/src/fs-watcher.ts +0 -198
  37. package/src/glob.spec.ts +0 -201
  38. package/src/glob.ts +0 -205
  39. package/src/index.ts +0 -313
  40. package/src/new-apis.spec.ts +0 -505
  41. package/src/promises.spec.ts +0 -812
  42. package/src/promises.ts +0 -686
  43. package/src/read-stream.ts +0 -128
  44. package/src/stat-watcher.ts +0 -116
  45. package/src/stat.spec.ts +0 -87
  46. package/src/statfs.spec.ts +0 -67
  47. package/src/statfs.ts +0 -92
  48. package/src/stats.ts +0 -207
  49. package/src/streams.spec.ts +0 -513
  50. package/src/symlink.spec.ts +0 -188
  51. package/src/sync.spec.ts +0 -377
  52. package/src/sync.ts +0 -562
  53. package/src/test.mts +0 -27
  54. package/src/types/encoding-option.ts +0 -3
  55. package/src/types/file-read-options.ts +0 -15
  56. package/src/types/file-read-result.ts +0 -4
  57. package/src/types/flag-and-open-mode.ts +0 -6
  58. package/src/types/index.ts +0 -6
  59. package/src/types/open-flags.ts +0 -14
  60. package/src/types/read-options.ts +0 -9
  61. package/src/utils.ts +0 -31
  62. package/src/utimes.spec.ts +0 -113
  63. package/src/utimes.ts +0 -97
  64. package/src/watch.spec.ts +0 -171
  65. package/src/watchfile.spec.ts +0 -185
  66. package/src/write-stream.ts +0 -142
  67. package/test/file.txt +0 -1
  68. package/tsconfig.json +0 -29
  69. package/tsconfig.tsbuildinfo +0 -1
package/src/sync.spec.ts DELETED
@@ -1,377 +0,0 @@
1
- import { describe, it, expect } from '@gjsify/unit';
2
- import { join, dirname } from 'node:path';
3
- import { fileURLToPath } from "node:url";
4
-
5
- const __filename = fileURLToPath(import.meta.url)
6
- const __dirname = dirname(__filename)
7
-
8
- import { existsSync, readdirSync, readFileSync, mkdirSync, rmdirSync, writeFileSync, unlinkSync, watch, mkdtempSync, rmSync, realpathSync, symlinkSync, statSync } from 'node:fs';
9
- import { Buffer } from 'node:buffer';
10
- import { tmpdir } from 'node:os';
11
-
12
- export default async () => {
13
- await describe('fs.existsSync', async () => {
14
-
15
- const existingFiles = ['/tmp', '/etc/hosts'];
16
- const nonExistingFiles = ['asdasd', '/asdasd', ''];
17
-
18
- await it('should return true for existing files', () => {
19
- for (const file of existingFiles) {
20
- const result = existsSync(file);
21
- expect(result).toBe(true)
22
- }
23
- });
24
-
25
- await it('should return false for non existing files', () => {
26
- for (const file of nonExistingFiles) {
27
- const result = existsSync(file);
28
- expect(result).toBe(false)
29
- }
30
- });
31
- });
32
-
33
- await describe('fs.readdirSync', async () => {
34
- await it('should return no files for an empty directory', () => {
35
- const dir = mkdtempSync('fs-test-');
36
- const files = readdirSync(dir);
37
- expect(files.length).toBe(0);
38
-
39
- // Clear
40
- rmdirSync(dir);
41
- });
42
-
43
- await it('should return the files for non-empty directory', () => {
44
- const dir = mkdtempSync('fs-test-');
45
- const txt1 = join(dir, 'test1.txt');
46
- const txt2 = join(dir, 'test2.txt');
47
- const dir1 = join(dir, 'empty-dir');
48
- writeFileSync(txt1, '');
49
- writeFileSync(txt2, '');
50
- mkdirSync(dir1);
51
- const files = readdirSync(dir);
52
- expect(files.length).toEqual(3);
53
-
54
- // Clear
55
- rmSync(txt1);
56
- rmSync(txt2);
57
- rmdirSync(dir1);
58
- rmdirSync(dir);
59
- });
60
-
61
- await it('should return the file with the name "file.txt"', () => {
62
- const dir = mkdtempSync('fs-test-');
63
- const expectedFileName = 'file.txt';
64
- const file = join(dir, expectedFileName);
65
-
66
- writeFileSync(file, '');
67
-
68
- const files = readdirSync(dir);
69
- expect(files[0]).toEqual(expectedFileName);
70
-
71
- // Clear
72
- rmSync(file);
73
- rmdirSync(dir);
74
- });
75
-
76
- await it('should return with file types if option "withFileTypes" is `true`', () => {
77
- const dir = mkdtempSync('fs-test-');
78
- const expectedFile = 'file.txt';
79
- const expectedDir = 'subdir';
80
- const file = join(dir, expectedFile);
81
- const subdir = join(dir, expectedDir);
82
-
83
- writeFileSync(file, '');
84
- mkdirSync(subdir);
85
-
86
- const files = readdirSync(dir, { withFileTypes: true });
87
-
88
- expect(files.length).toBe(2);
89
-
90
- const fileWithTypes = files.find((f) => f.name === expectedFile);
91
- const dirWithTypes = files.find((f) => f.name === expectedDir);
92
-
93
- expect(fileWithTypes!.isFile()).toBeTruthy();
94
- expect(fileWithTypes!.isDirectory()).toBeFalsy();
95
-
96
- expect(dirWithTypes!.isFile()).toBeFalsy();
97
- expect(dirWithTypes!.isDirectory()).toBeTruthy();
98
-
99
- // Clear
100
- rmSync(file);
101
- rmdirSync(subdir);
102
- rmdirSync(dir);
103
- });
104
- });
105
-
106
- await describe('fs.readFileSync', async () => {
107
- await it('should return a Buffer if no encoding was specified', () => {
108
- const bufferData = readFileSync('/etc/hosts');
109
- expect(bufferData instanceof Buffer).toBeTruthy();
110
- });
111
-
112
- await it('should return a string when encoding is utf-8', () => {
113
- const dir = mkdtempSync('fs-rfs-');
114
- const filePath = join(dir, 'test.txt');
115
- writeFileSync(filePath, 'Hello World');
116
- const utf8Data = readFileSync(filePath, 'utf-8');
117
- expect(typeof utf8Data === 'string').toBeTruthy();
118
- rmSync(filePath);
119
- rmdirSync(dir);
120
- });
121
-
122
- await it('should return the correct file content', () => {
123
- const dir = mkdtempSync('fs-rfs-content-');
124
- const filePath = join(dir, 'test.txt');
125
- writeFileSync(filePath, 'Hello World');
126
- const utf8Data = readFileSync(filePath, 'utf-8');
127
- expect(utf8Data).toBe('Hello World');
128
- rmSync(filePath);
129
- rmdirSync(dir);
130
- });
131
- });
132
-
133
- await describe('fs.mkdirSync', async () => {
134
- const dir = './foobar';
135
-
136
- await it(`should create the directory "${dir}" without error`, () => {
137
- mkdirSync(dir);
138
- });
139
-
140
- await it(`${dir} should exists`, () => {
141
- expect(existsSync(dir)).toBeTruthy();
142
- });
143
- });
144
-
145
- await describe('fs.rmdirSync', async () => {
146
- const dir = './foobar';
147
-
148
- await it(`should be remove the directory "${dir}" without error`, () => {
149
- rmdirSync(dir);
150
- });
151
-
152
- await it(`"${dir}" should not exists (anymore)`, () => {
153
- expect(existsSync(dir)).toBeFalsy();
154
- });
155
- });
156
-
157
- await describe('fs.writeFileSync', async () => {
158
- const watchMe = join(__dirname, 'test/watch.js');
159
-
160
- await it(`should be executed without error`, () => {
161
- writeFileSync(watchMe, '// test');
162
- });
163
-
164
- await it(`fs.watch should watch ${watchMe} for changes`, async () => {
165
- await new Promise<void>((resolve) => {
166
- let watcher: ReturnType<typeof watch>;
167
- try {
168
- watcher = watch(watchMe, {persistent: true}, console.log);
169
- } catch (err: any) {
170
- // EMFILE (too many open files) is a system-level issue, not a code bug
171
- if (err?.code === 'EMFILE') { resolve(); return; }
172
- throw err;
173
- }
174
- // FSWatcher inherits from EventEmitter at runtime
175
- const w = watcher as unknown as import('node:events').EventEmitter;
176
- w.on('change', console.log).on('rename', console.log);
177
-
178
- setTimeout(() => {
179
- writeFileSync(watchMe, '// test');
180
- setTimeout(() => {
181
- try { unlinkSync(watchMe); } catch {}
182
- watcher.close();
183
- resolve();
184
- }, 100);
185
- }, 100);
186
-
187
- setTimeout(() => { watcher.close(); resolve(); }, 2000);
188
- });
189
- });
190
- });
191
-
192
- await describe('fs.mkdtempSync', async () => {
193
-
194
- await it('should be a function', () => {
195
- expect(typeof mkdtempSync).toBe("function");
196
- });
197
-
198
- await it('should create a new directory', () => {
199
- const directory = mkdtempSync('fs-test-');
200
- expect(existsSync(directory)).toBeTruthy();
201
- rmdirSync(directory);
202
- });
203
- });
204
-
205
- await describe('fs.realpathSync', async () => {
206
-
207
- await it('should be a function', () => {
208
- expect(typeof realpathSync).toBe("function");
209
- });
210
-
211
- await it('should return the real and absolute path', () => {
212
- const dir = mkdtempSync(join(tmpdir(), 'fs-rp-'));
213
- const target = join(dir, 'target.txt');
214
- const link = join(dir, 'link.txt');
215
- writeFileSync(target, 'data');
216
- symlinkSync(target, link);
217
-
218
- const realPath = realpathSync(target);
219
- const realSymLinkPath = realpathSync(link);
220
-
221
- // Should point to the real file, not the symlink
222
- expect(realSymLinkPath).toBe(realPath);
223
-
224
- unlinkSync(link);
225
- rmSync(target);
226
- rmdirSync(dir);
227
- });
228
- });
229
-
230
- await describe('fs.mkdirSync recursive', async () => {
231
- await it('should return the first directory created when recursive is true', () => {
232
- const dir = mkdtempSync(join(tmpdir(), 'fs-mkdir-rec-'));
233
- const nested = join(dir, 'a', 'b', 'c');
234
- const result = mkdirSync(nested, { recursive: true });
235
- // The first created directory should be 'a' (the top-level new dir)
236
- expect(typeof result).toBe('string');
237
- expect(result).toBe(join(dir, 'a'));
238
- expect(existsSync(nested)).toBe(true);
239
- rmSync(dir, { recursive: true });
240
- });
241
-
242
- await it('should return undefined when all directories already exist', () => {
243
- const dir = mkdtempSync(join(tmpdir(), 'fs-mkdir-rec-exist-'));
244
- const result = mkdirSync(dir, { recursive: true });
245
- expect(result).toBeUndefined();
246
- rmdirSync(dir);
247
- });
248
-
249
- await it('should throw EEXIST when non-recursive and dir exists', () => {
250
- const dir = mkdtempSync(join(tmpdir(), 'fs-mkdir-exist-'));
251
- let threw = false;
252
- try {
253
- mkdirSync(dir);
254
- } catch (e: unknown) {
255
- threw = true;
256
- expect((e as NodeJS.ErrnoException).code).toBe('EEXIST');
257
- }
258
- expect(threw).toBe(true);
259
- rmdirSync(dir);
260
- });
261
-
262
- await it('should throw ENOENT when non-recursive and parent missing', () => {
263
- const dir = join(tmpdir(), 'fs-mkdir-noparent-' + Date.now(), 'child');
264
- let threw = false;
265
- try {
266
- mkdirSync(dir);
267
- } catch (e: unknown) {
268
- threw = true;
269
- expect((e as NodeJS.ErrnoException).code).toBe('ENOENT');
270
- }
271
- expect(threw).toBe(true);
272
- });
273
- });
274
-
275
- await describe('fs.rmSync error handling', async () => {
276
- await it('should throw when removing non-empty dir without recursive', () => {
277
- const dir = mkdtempSync(join(tmpdir(), 'fs-rmsync-notempty-'));
278
- writeFileSync(join(dir, 'file.txt'), 'data');
279
- let threw = false;
280
- try {
281
- rmSync(dir);
282
- } catch (e: unknown) {
283
- threw = true;
284
- // Node.js throws ERR_FS_EISDIR, GJS throws ENOTEMPTY — both are correct
285
- const code = (e as NodeJS.ErrnoException).code;
286
- expect(code === 'ENOTEMPTY' || code === 'ERR_FS_EISDIR').toBe(true);
287
- }
288
- expect(threw).toBe(true);
289
- rmSync(dir, { recursive: true });
290
- });
291
-
292
- await it('should not throw when force is true and path does not exist', () => {
293
- const path = join(tmpdir(), 'fs-rmsync-force-nonexistent-' + Date.now());
294
- let threw = false;
295
- try {
296
- rmSync(path, { force: true });
297
- } catch {
298
- threw = true;
299
- }
300
- expect(threw).toBe(false);
301
- });
302
-
303
- await it('should remove non-empty directory with recursive: true', () => {
304
- const dir = mkdtempSync(join(tmpdir(), 'fs-rmsync-rec-'));
305
- mkdirSync(join(dir, 'sub'));
306
- writeFileSync(join(dir, 'sub', 'file.txt'), 'data');
307
- writeFileSync(join(dir, 'root.txt'), 'data');
308
- rmSync(dir, { recursive: true });
309
- expect(existsSync(dir)).toBe(false);
310
- });
311
- });
312
-
313
- await describe('fs.Dirent type methods', async () => {
314
- await it('should return false for isCharacterDevice, isSocket, isFIFO on regular file', () => {
315
- const dir = mkdtempSync(join(tmpdir(), 'fs-dirent-'));
316
- const filePath = join(dir, 'test.txt');
317
- writeFileSync(filePath, 'data');
318
- const entries = readdirSync(dir, { withFileTypes: true });
319
- const entry = entries[0];
320
- expect(entry.isCharacterDevice()).toBe(false);
321
- expect(entry.isSocket()).toBe(false);
322
- expect(entry.isFIFO()).toBe(false);
323
- expect(entry.isBlockDevice()).toBe(false);
324
- expect(entry.isFile()).toBe(true);
325
- rmSync(filePath);
326
- rmdirSync(dir);
327
- });
328
-
329
- await it('should return false for isCharacterDevice, isSocket, isFIFO on directory', () => {
330
- const dir = mkdtempSync(join(tmpdir(), 'fs-dirent-dir-'));
331
- const subdir = join(dir, 'sub');
332
- mkdirSync(subdir);
333
- const entries = readdirSync(dir, { withFileTypes: true });
334
- const entry = entries[0];
335
- expect(entry.isCharacterDevice()).toBe(false);
336
- expect(entry.isSocket()).toBe(false);
337
- expect(entry.isFIFO()).toBe(false);
338
- expect(entry.isBlockDevice()).toBe(false);
339
- expect(entry.isDirectory()).toBe(true);
340
- rmdirSync(subdir);
341
- rmdirSync(dir);
342
- });
343
-
344
- await it('statSync should detect isCharacterDevice for /dev/null', () => {
345
- const s = statSync('/dev/null');
346
- expect(s.isCharacterDevice()).toBe(true);
347
- expect(s.isFile()).toBe(false);
348
- expect(s.isDirectory()).toBe(false);
349
- expect(s.isSocket()).toBe(false);
350
- expect(s.isFIFO()).toBe(false);
351
- expect(s.isBlockDevice()).toBe(false);
352
- });
353
- });
354
-
355
- await describe('fs.FSWatcher ref/unref', async () => {
356
- await it('ref() and unref() should return the watcher itself', () => {
357
- const dir = mkdtempSync(join(tmpdir(), 'fs-watcher-'));
358
- const filePath = join(dir, 'watch.txt');
359
- writeFileSync(filePath, 'data');
360
- let watcher: ReturnType<typeof watch> | null = null;
361
- try {
362
- watcher = watch(filePath);
363
- const refResult = watcher.ref();
364
- expect(refResult).toBe(watcher);
365
- const unrefResult = watcher.unref();
366
- expect(unrefResult).toBe(watcher);
367
- } catch (err: any) {
368
- // EMFILE is a system-level issue, not a code bug
369
- if (err?.code !== 'EMFILE') throw err;
370
- } finally {
371
- if (watcher) watcher.close();
372
- rmSync(filePath);
373
- rmdirSync(dir);
374
- }
375
- });
376
- });
377
- }