@zenfs/devel 0.0.1 → 1.0.0

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 (105) hide show
  1. package/dist/bin/make-index.js +37 -74
  2. package/dist/bin/node-compat.d.ts +2 -0
  3. package/dist/bin/node-compat.js +337 -0
  4. package/dist/bin/zci.js +33 -44
  5. package/dist/bin/zenfs-test.js +48 -80
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +1 -0
  8. package/dist/make_index.d.ts +20 -0
  9. package/dist/make_index.js +49 -0
  10. package/dist/node_compat/analysis.d.ts +53 -0
  11. package/dist/node_compat/analysis.js +60 -0
  12. package/dist/node_compat/changelog.d.ts +25 -0
  13. package/dist/node_compat/changelog.js +60 -0
  14. package/dist/node_compat/commits.d.ts +26 -0
  15. package/dist/node_compat/commits.js +66 -0
  16. package/dist/node_compat/config.d.ts +6 -0
  17. package/dist/node_compat/config.js +18 -0
  18. package/dist/node_compat/docs.d.ts +58 -0
  19. package/dist/node_compat/docs.js +213 -0
  20. package/dist/node_compat/git.d.ts +22 -0
  21. package/dist/node_compat/git.js +74 -0
  22. package/dist/node_compat/index.d.ts +10 -0
  23. package/dist/node_compat/index.js +11 -0
  24. package/dist/node_compat/node.d.ts +42 -0
  25. package/dist/node_compat/node.js +84 -0
  26. package/dist/node_compat/output.d.ts +29 -0
  27. package/dist/node_compat/output.js +95 -0
  28. package/dist/node_compat/pr.d.ts +59 -0
  29. package/dist/node_compat/pr.js +125 -0
  30. package/dist/node_compat/state.d.ts +18 -0
  31. package/dist/node_compat/state.js +12 -0
  32. package/dist/node_compat/versions.d.ts +5 -0
  33. package/dist/node_compat/versions.js +13 -0
  34. package/dist/node_compat/zenfs.d.ts +3 -0
  35. package/dist/node_compat/zenfs.js +108 -0
  36. package/dist/tests/common.js +1 -1
  37. package/dist/tests/fs/append.test.d.ts +1 -0
  38. package/dist/tests/fs/append.test.js +27 -0
  39. package/dist/tests/fs/concurrency.test.d.ts +1 -0
  40. package/dist/tests/fs/concurrency.test.js +76 -0
  41. package/dist/tests/fs/dir.test.d.ts +1 -0
  42. package/dist/tests/fs/dir.test.js +83 -0
  43. package/dist/tests/fs/directory.test.d.ts +1 -0
  44. package/dist/tests/fs/directory.test.js +151 -0
  45. package/dist/tests/fs/errors.test.d.ts +1 -0
  46. package/dist/tests/fs/errors.test.js +141 -0
  47. package/dist/tests/fs/exists.test.d.ts +1 -0
  48. package/dist/tests/fs/exists.test.js +19 -0
  49. package/dist/tests/fs/links.test.d.ts +1 -0
  50. package/dist/tests/fs/links.test.js +248 -0
  51. package/dist/tests/fs/open.test.d.ts +1 -0
  52. package/dist/tests/fs/open.test.js +95 -0
  53. package/dist/tests/fs/permissions.test.d.ts +1 -0
  54. package/dist/tests/fs/permissions.test.js +346 -0
  55. package/dist/tests/fs/read.test.d.ts +1 -0
  56. package/dist/tests/fs/read.test.js +149 -0
  57. package/dist/tests/fs/readFile.test.d.ts +1 -0
  58. package/dist/tests/fs/readFile.test.js +110 -0
  59. package/dist/tests/fs/rename.test.d.ts +1 -0
  60. package/dist/tests/fs/rename.test.js +70 -0
  61. package/dist/tests/fs/scaling.test.d.ts +1 -0
  62. package/dist/tests/fs/scaling.test.js +27 -0
  63. package/dist/tests/fs/stat.test.d.ts +1 -0
  64. package/dist/tests/fs/stat.test.js +93 -0
  65. package/dist/tests/fs/streams.test.d.ts +1 -0
  66. package/dist/tests/fs/streams.test.js +161 -0
  67. package/dist/tests/fs/tempdir.test.d.ts +1 -0
  68. package/dist/tests/fs/tempdir.test.js +118 -0
  69. package/dist/tests/fs/times.test.d.ts +7 -0
  70. package/dist/tests/fs/times.test.js +134 -0
  71. package/dist/tests/fs/truncate.test.d.ts +1 -0
  72. package/dist/tests/fs/truncate.test.js +62 -0
  73. package/dist/tests/fs/watch.test.d.ts +1 -0
  74. package/dist/tests/fs/watch.test.js +320 -0
  75. package/dist/tests/fs/write.test.d.ts +1 -0
  76. package/dist/tests/fs/write.test.js +83 -0
  77. package/dist/tests/fs/xattr.test.d.ts +1 -0
  78. package/dist/tests/fs/xattr.test.js +137 -0
  79. package/dist/tests/setup/memory.d.ts +1 -0
  80. package/dist/tests/setup/memory.js +3 -0
  81. package/package.json +11 -6
  82. package/tests/readme.md +2 -2
  83. package/tests/fs/append.test.ts +0 -34
  84. package/tests/fs/concurrency.test.ts +0 -113
  85. package/tests/fs/dir.test.ts +0 -103
  86. package/tests/fs/directory.test.ts +0 -189
  87. package/tests/fs/errors.test.ts +0 -159
  88. package/tests/fs/exists.test.ts +0 -23
  89. package/tests/fs/links.test.ts +0 -308
  90. package/tests/fs/open.test.ts +0 -23
  91. package/tests/fs/permissions.test.ts +0 -344
  92. package/tests/fs/read.test.ts +0 -94
  93. package/tests/fs/readFile.test.ts +0 -58
  94. package/tests/fs/rename.test.ts +0 -93
  95. package/tests/fs/scaling.test.ts +0 -35
  96. package/tests/fs/stat.test.ts +0 -115
  97. package/tests/fs/streams.test.ts +0 -204
  98. package/tests/fs/tempdir.test.ts +0 -64
  99. package/tests/fs/times.test.ts +0 -85
  100. package/tests/fs/truncate.test.ts +0 -89
  101. package/tests/fs/watch.test.ts +0 -246
  102. package/tests/fs/write.test.ts +0 -118
  103. package/tests/fs/xattr.test.ts +0 -96
  104. package/tests/setup/memory.ts +0 -3
  105. package/tests/tsconfig.json +0 -8
@@ -0,0 +1,70 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { suite, test } from 'node:test';
4
+ import { config, fs } from '../common.js';
5
+ suite('Rename', config('rename'), () => {
6
+ /**
7
+ * Creates the following directory structure within `dir`:
8
+ * - _rename_me
9
+ * - lol.txt
10
+ * - file.dat
11
+ */
12
+ async function populate(dir) {
13
+ await fs.promises.mkdir(dir + '/_rename_me');
14
+ await fs.promises.writeFile(dir + '/file.dat', 'filedata');
15
+ await fs.promises.writeFile(dir + '/_rename_me/lol.txt', 'lololol');
16
+ }
17
+ /**
18
+ * Check that the directory structure created in populate_directory remains.
19
+ */
20
+ async function check_directory(dir) {
21
+ const contents = await fs.promises.readdir(dir);
22
+ assert.equal(contents.length, 2);
23
+ const subContents = await fs.promises.readdir(dir + '/_rename_me');
24
+ assert.equal(subContents.length, 1);
25
+ assert(await fs.promises.stat(dir + '/file.dat', { throwIfNoEntry: false }));
26
+ assert(await fs.promises.stat(dir + '/_rename_me/lol.txt', { throwIfNoEntry: false }));
27
+ }
28
+ test('rename directory', config('async'), async () => {
29
+ const oldDir = '/rename_test';
30
+ const newDir = '/rename_test2';
31
+ await fs.promises.mkdir(oldDir);
32
+ await populate(oldDir);
33
+ await fs.promises.rename(oldDir, oldDir);
34
+ await check_directory(oldDir);
35
+ await fs.promises.rename(oldDir, newDir);
36
+ await check_directory(newDir);
37
+ assert(!(await fs.promises.stat(oldDir, { throwIfNoEntry: false })));
38
+ await fs.promises.mkdir(oldDir);
39
+ await populate(oldDir);
40
+ await fs.promises.rename(oldDir, newDir + '/newDir');
41
+ });
42
+ test('rename file', config('async'), async () => {
43
+ const dir = '/rename_file_test';
44
+ const one = dir + '/fun.js';
45
+ const two = dir + '/fun2.js';
46
+ await fs.promises.mkdir(dir);
47
+ await fs.promises.writeFile(one, 'while(1) alert("Hey! Listen!");');
48
+ await fs.promises.rename(one, one);
49
+ await fs.promises.rename(one, two);
50
+ await fs.promises.writeFile(one, 'hey');
51
+ await fs.promises.rename(one, two);
52
+ assert.equal(await fs.promises.readFile(two, 'utf8'), 'hey');
53
+ assert(!(await fs.promises.stat(one, { throwIfNoEntry: false })));
54
+ });
55
+ test('File to Directory and Directory to File Rename', config('sync', 'async'), async () => {
56
+ const dir = '/rename_file_dir_test';
57
+ const file = '/rename_file_dir_test.txt';
58
+ await fs.promises.mkdir(dir);
59
+ await fs.promises.writeFile(file, 'file contents go here');
60
+ await assert.rejects(fs.promises.rename(file, dir), { code: 'EISDIR' });
61
+ assert.throws(() => fs.renameSync(file, dir), { code: 'EISDIR' });
62
+ });
63
+ test('rename directory inside itself', config('sync', 'async'), async () => {
64
+ const renDir1 = '/rename_dir_1';
65
+ const renDir2 = '/rename_dir_1/lol';
66
+ await fs.promises.mkdir(renDir1);
67
+ await assert.rejects(fs.promises.rename(renDir1, renDir2), { code: 'EINVAL' });
68
+ assert.throws(() => fs.renameSync(renDir1, renDir2), { code: 'EINVAL' });
69
+ });
70
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { sync } from '@zenfs/core';
3
+ import assert from 'node:assert/strict';
4
+ import { suite, test } from 'node:test';
5
+ import { config, fs } from '../common.js';
6
+ const n_files = 130;
7
+ const huge_size = 0x1000000;
8
+ // Tests for having a lot of various things (number of inodes/files, individual file size, etc.).
9
+ suite('Scaling', config('write'), () => {
10
+ test('Lots of inodes/files', config('sync', 'async'), async () => {
11
+ fs.mkdirSync('/n');
12
+ for (let i = 0; i < n_files; i++) {
13
+ fs.writeFileSync('/n/' + i, i.toString(16));
14
+ }
15
+ await sync();
16
+ assert.equal(fs.readdirSync('/n').length, n_files);
17
+ const results = [];
18
+ for (let i = 0; i < n_files; i++) {
19
+ results.push(fs.promises.readFile('/n/' + i, 'utf8').then(val => assert.equal(val, i.toString(16))));
20
+ }
21
+ await Promise.all(results);
22
+ });
23
+ test('Singular file size', config('sync'), () => {
24
+ fs.writeFileSync('/huge', new Uint8Array(huge_size));
25
+ assert.equal(fs.statSync('/huge').size, huge_size);
26
+ });
27
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,93 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import { defaultContext, hasAccess } from '@zenfs/core';
3
+ import assert from 'node:assert/strict';
4
+ import { suite, test } from 'node:test';
5
+ import { config, fs } from '../common.js';
6
+ suite('Stats', () => {
7
+ const existing_file = 'x.txt';
8
+ test('stat empty path', config('async'), async () => {
9
+ await assert.rejects(fs.promises.stat(''));
10
+ });
11
+ test('stat directory', config('async'), async () => {
12
+ const stats = await fs.promises.stat('/');
13
+ assert(stats instanceof fs.Stats);
14
+ });
15
+ test('lstat directory', config('async'), async () => {
16
+ const stats = await fs.promises.lstat('/');
17
+ assert(stats instanceof fs.Stats);
18
+ });
19
+ test('FileHandle.stat', config('async'), async () => {
20
+ const handle = await fs.promises.open(existing_file, 'r');
21
+ const stats = await handle.stat();
22
+ assert(stats instanceof fs.Stats);
23
+ await handle.close();
24
+ });
25
+ test('fstatSync file', config('sync'), () => {
26
+ const fd = fs.openSync(existing_file, 'r');
27
+ const stats = fs.fstatSync(fd);
28
+ assert(stats instanceof fs.Stats);
29
+ fs.close(fd);
30
+ });
31
+ test('hasAccess for non-root access', config('sync', 'permissions', 'root'), () => {
32
+ const newFile = 'new.txt';
33
+ fs.writeFileSync(newFile, 'hello', { mode: 0o640 });
34
+ const prevCredentials = { ...defaultContext.credentials };
35
+ const uid = 33;
36
+ const nonRootCredentials = {
37
+ uid,
38
+ gid: uid,
39
+ euid: uid,
40
+ egid: uid,
41
+ suid: uid,
42
+ sgid: uid,
43
+ };
44
+ fs.chownSync(newFile, 0, nonRootCredentials.gid); // creating with root-user so that non-root user can access
45
+ Object.assign(defaultContext.credentials, nonRootCredentials);
46
+ const stat = fs.statSync(newFile);
47
+ assert.equal(stat.gid, nonRootCredentials.gid);
48
+ assert.equal(stat.uid, 0);
49
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.R_OK), true);
50
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.W_OK), false);
51
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.X_OK), false);
52
+ // changing group
53
+ Object.assign(defaultContext.credentials, { ...nonRootCredentials, gid: 44, egid: 44 });
54
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.R_OK), false);
55
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.W_OK), false);
56
+ assert.equal(hasAccess(defaultContext, stat, fs.constants.X_OK), false);
57
+ Object.assign(defaultContext.credentials, prevCredentials);
58
+ });
59
+ const missing = '/does-not-exist';
60
+ test('statSync throws for a missing entry', config('sync'), () => {
61
+ assert.throws(() => fs.statSync(missing), { code: 'ENOENT' });
62
+ assert.throws(() => fs.statSync(missing, { throwIfNoEntry: true }), { code: 'ENOENT' });
63
+ });
64
+ test('statSync with throwIfNoEntry: false', config('sync'), () => {
65
+ assert.equal(fs.statSync(missing, { throwIfNoEntry: false }), undefined);
66
+ assert.equal(fs.statSync(existing_file + '/nope', { throwIfNoEntry: false }), undefined);
67
+ });
68
+ test('lstatSync with throwIfNoEntry: false', config('sync'), () => {
69
+ assert.throws(() => fs.lstatSync(missing), { code: 'ENOENT' });
70
+ assert.equal(fs.lstatSync(missing, { throwIfNoEntry: false }), undefined);
71
+ });
72
+ test('stat with throwIfNoEntry: false', config('async'), async () => {
73
+ await assert.rejects(fs.promises.stat(missing), { code: 'ENOENT' });
74
+ assert.equal(await fs.promises.stat(missing, { throwIfNoEntry: false }), undefined);
75
+ assert.equal(await fs.promises.stat(existing_file + '/nope', { throwIfNoEntry: false }), undefined);
76
+ });
77
+ test('stat callback with throwIfNoEntry: false', config('async'), async () => {
78
+ const { promise, resolve, reject } = Promise.withResolvers();
79
+ fs.stat(missing, { throwIfNoEntry: false }, (error, stats) => (error ? reject(error) : resolve(stats)));
80
+ assert.equal(await promise, undefined);
81
+ });
82
+ test('stat file', config('async'), async () => {
83
+ const stats = await fs.promises.stat(existing_file);
84
+ assert(!stats.isDirectory());
85
+ assert(stats.isFile());
86
+ assert(!stats.isSocket());
87
+ assert(!stats.isBlockDevice());
88
+ assert(!stats.isCharacterDevice());
89
+ assert(!stats.isFIFO());
90
+ assert(!stats.isSymbolicLink());
91
+ assert(stats instanceof fs.Stats);
92
+ });
93
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,161 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ import assert from 'node:assert/strict';
3
+ import { suite, test } from 'node:test';
4
+ import { config, fs } from '../common.js';
5
+ // Top-level initialization
6
+ const testFilePath = 'test-file.txt';
7
+ const testData = 'Hello, World!';
8
+ await fs.promises.writeFile(testFilePath, testData);
9
+ const testFilePathWrite = 'test-file-write.txt';
10
+ await fs.promises.writeFile(testFilePathWrite, ''); // Ensure the file exists
11
+ suite('Streams', config('streams'), () => {
12
+ test('ReadStream reads data correctly', (_, done) => {
13
+ const readStream = fs.createReadStream(testFilePath);
14
+ let data = '';
15
+ readStream.on('data', chunk => {
16
+ data += chunk;
17
+ });
18
+ readStream.on('end', () => {
19
+ assert.equal(data, testData);
20
+ done();
21
+ });
22
+ readStream.on('error', err => {
23
+ done(err);
24
+ });
25
+ });
26
+ test('ReadStream close method works', (_, done) => {
27
+ const readStream = fs.createReadStream(testFilePath);
28
+ let closed = false;
29
+ readStream.on('close', () => {
30
+ closed = true;
31
+ });
32
+ // Closing before the stream has finished reading is a premature close
33
+ readStream.close(err => {
34
+ assert.equal(err?.code, 'ERR_STREAM_PREMATURE_CLOSE');
35
+ assert(closed);
36
+ done();
37
+ });
38
+ });
39
+ test('WriteStream writes data correctly', async () => {
40
+ const writeStream = fs.createWriteStream(testFilePathWrite);
41
+ const { promise, resolve, reject } = Promise.withResolvers();
42
+ writeStream.on('finish', resolve);
43
+ writeStream.on('error', reject);
44
+ writeStream.end(testData, 'utf8');
45
+ await promise;
46
+ assert.equal(fs.readFileSync(testFilePathWrite, 'utf8'), testData);
47
+ });
48
+ test('WriteStream close method works', (_, done) => {
49
+ const writeStream = fs.createWriteStream(testFilePathWrite);
50
+ let closed = false;
51
+ writeStream.on('close', () => {
52
+ closed = true;
53
+ });
54
+ writeStream.close(err => {
55
+ assert.ifError(err);
56
+ assert(closed);
57
+ done();
58
+ });
59
+ });
60
+ test('createReadStream with start', async () => {
61
+ await fs.promises.writeFile('hello.txt', 'Hello world');
62
+ const stream = fs.createReadStream('hello.txt', { start: 6, encoding: 'utf-8' });
63
+ const data = (await stream.toArray()).join('');
64
+ assert.equal(data, 'world');
65
+ });
66
+ test('createReadStream with end', async () => {
67
+ await fs.promises.writeFile('hello.txt', 'Hello world');
68
+ const stream = fs.createReadStream('hello.txt', { end: 4, encoding: 'utf-8' });
69
+ const data = (await stream.toArray()).join('');
70
+ assert.equal(data, 'Hello');
71
+ });
72
+ test('FileHandle.createReadStream reads data correctly', async () => {
73
+ const fileHandle = await fs.promises.open(testFilePath, 'r');
74
+ const readStream = fileHandle.createReadStream({ encoding: 'utf-8' });
75
+ const [data] = await readStream.toArray();
76
+ assert.equal(data, testData);
77
+ await fileHandle.close();
78
+ });
79
+ test('FileHandle.createWriteStream writes data correctly', async () => {
80
+ const fileHandle = await fs.promises.open(testFilePathWrite, 'w');
81
+ const writeStream = fileHandle.createWriteStream();
82
+ const { promise, resolve, reject } = Promise.withResolvers();
83
+ writeStream.on('finish', resolve);
84
+ writeStream.on('error', reject);
85
+ writeStream.end(testData, 'utf8');
86
+ await promise;
87
+ const data = await fs.promises.readFile(testFilePathWrite, 'utf8');
88
+ assert.equal(data, testData);
89
+ await fileHandle.close();
90
+ });
91
+ test('readable web stream', async () => {
92
+ const fileHandle = await fs.promises.open(testFilePath, 'r');
93
+ const webStream = fileHandle.readableWebStream();
94
+ let data = '';
95
+ const decoder = new TextDecoder();
96
+ for await (const chunk of webStream) {
97
+ data += decoder.decode(chunk);
98
+ }
99
+ assert.equal(data, testData);
100
+ await fileHandle.close();
101
+ });
102
+ test('FileHandle.createReadStream after close should give an error', async () => {
103
+ const fileHandle = await fs.promises.open(testFilePath, 'r');
104
+ await fileHandle.close();
105
+ // Closing invalidates the descriptor, so it fails range validation
106
+ assert.throws(() => fileHandle.createReadStream(), { code: 'ERR_OUT_OF_RANGE' });
107
+ });
108
+ test('FileHandle.createWriteStream after close should give an error', async () => {
109
+ const fileHandle = await fs.promises.open(testFilePathWrite, 'w');
110
+ await fileHandle.close();
111
+ // Closing invalidates the descriptor, so it fails range validation
112
+ assert.throws(() => fileHandle.createWriteStream(), { code: 'ERR_OUT_OF_RANGE' });
113
+ });
114
+ // A file large enough to span several chunks, so the read path has to stitch
115
+ // them back together and the vnode cache has to grow to hold them.
116
+ const large = Buffer.alloc(200 * 1024);
117
+ for (let i = 0; i < large.length; i++)
118
+ large[i] = (i * 31 + 7) & 0xff;
119
+ test('createReadStream reads a multi-chunk file byte for byte', async () => {
120
+ await fs.promises.writeFile('large.bin', large);
121
+ const chunks = [];
122
+ for await (const chunk of fs.createReadStream('large.bin'))
123
+ chunks.push(chunk);
124
+ assert.deepEqual(Buffer.concat(chunks), large);
125
+ });
126
+ test('createReadStream honors highWaterMark as the chunk size', async () => {
127
+ await fs.promises.writeFile('large.bin', large);
128
+ const highWaterMark = 64 * 1024;
129
+ const sizes = [];
130
+ for await (const chunk of fs.createReadStream('large.bin', { highWaterMark }))
131
+ sizes.push(chunk.byteLength);
132
+ // Every chunk but the last is a full highWaterMark, and they sum to the file
133
+ assert.deepEqual(sizes.slice(0, -1), Array(sizes.length - 1).fill(highWaterMark));
134
+ assert.equal(sizes.reduce((a, b) => a + b, 0), large.length);
135
+ });
136
+ test('createReadStream with start and end spanning chunks', async () => {
137
+ await fs.promises.writeFile('large.bin', large);
138
+ // `end` is inclusive, and this range deliberately starts and stops mid-chunk
139
+ const start = 1000;
140
+ const end = 150_000;
141
+ const chunks = [];
142
+ for await (const chunk of fs.createReadStream('large.bin', { start, end, highWaterMark: 8192 }))
143
+ chunks.push(chunk);
144
+ assert.deepEqual(Buffer.concat(chunks), large.subarray(start, end + 1));
145
+ });
146
+ test('a read stream does not read past the end of the file', async () => {
147
+ await fs.promises.writeFile('small.bin', large.subarray(0, 10));
148
+ const chunks = [];
149
+ for await (const chunk of fs.createReadStream('small.bin', { highWaterMark: 64 * 1024 }))
150
+ chunks.push(chunk);
151
+ assert.equal(chunks.length, 1);
152
+ assert.deepEqual(chunks[0], large.subarray(0, 10));
153
+ });
154
+ test('a read stream over an empty file ends without a chunk', async () => {
155
+ await fs.promises.writeFile('empty.bin', '');
156
+ const chunks = [];
157
+ for await (const chunk of fs.createReadStream('empty.bin'))
158
+ chunks.push(chunk);
159
+ assert.deepEqual(chunks, []);
160
+ });
161
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,118 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ // SPDX-License-Identifier: LGPL-3.0-or-later
54
+ import { test, suite } from 'node:test';
55
+ import { config, fs } from '../common.js';
56
+ import assert from 'node:assert/strict';
57
+ import { basename } from 'node:path/posix';
58
+ await fs.promises.mkdir('/tmp');
59
+ suite('Temporary Directories', config('tempdir'), () => {
60
+ test('mkdtempSync', config('sync'), () => {
61
+ const path = fs.mkdtempSync('/tmp/test-', { encoding: 'utf8' });
62
+ assert.deepEqual(fs.readdirSync('/tmp'), [basename(path)]);
63
+ fs.rmdirSync(path);
64
+ });
65
+ test('mkdtemp', config('async'), async () => {
66
+ const path = await fs.promises.mkdtemp('/tmp/test-', { encoding: 'utf8' });
67
+ assert.deepEqual(await fs.promises.readdir('/tmp'), [basename(path)]);
68
+ await fs.promises.rmdir(path);
69
+ });
70
+ test('mkdtempDisposableSync', config('sync'), () => {
71
+ const env_1 = { stack: [], error: void 0, hasError: false };
72
+ try {
73
+ const result = __addDisposableResource(env_1, fs.mkdtempDisposableSync('/tmp/test-', { encoding: 'utf8' }), false);
74
+ assert.deepEqual(fs.readdirSync('/tmp'), [basename(result.path)]);
75
+ fs.rmdirSync(result.path);
76
+ }
77
+ catch (e_1) {
78
+ env_1.error = e_1;
79
+ env_1.hasError = true;
80
+ }
81
+ finally {
82
+ __disposeResources(env_1);
83
+ }
84
+ });
85
+ test('mkdtempDisposable', config('async'), async () => {
86
+ const env_2 = { stack: [], error: void 0, hasError: false };
87
+ try {
88
+ const result = __addDisposableResource(env_2, await fs.promises.mkdtempDisposable('/tmp/test-', { encoding: 'utf8' }), true);
89
+ assert.deepEqual(await fs.promises.readdir('/tmp'), [basename(result.path)]);
90
+ await fs.promises.rmdir(result.path);
91
+ }
92
+ catch (e_2) {
93
+ env_2.error = e_2;
94
+ env_2.hasError = true;
95
+ }
96
+ finally {
97
+ const result_1 = __disposeResources(env_2);
98
+ if (result_1)
99
+ await result_1;
100
+ }
101
+ });
102
+ /* A relative prefix is resolved against the working directory, rather than always being placed in `/tmp`.
103
+ Note the returned path keeps the prefix it was given, so it is only compared by basename here. */
104
+ test('mkdtempSync with a relative prefix', config('sync'), () => {
105
+ const path = fs.mkdtempSync('tmp/relative-', { encoding: 'utf8' });
106
+ assert(basename(path).startsWith('relative-'));
107
+ assert(fs.statSync(path).isDirectory());
108
+ assert(fs.readdirSync('/tmp').includes(basename(path)));
109
+ fs.rmdirSync(path);
110
+ });
111
+ test('mkdtemp with a relative prefix', config('async'), async () => {
112
+ const path = await fs.promises.mkdtemp('tmp/relative-', { encoding: 'utf8' });
113
+ assert(basename(path).startsWith('relative-'));
114
+ assert((await fs.promises.stat(path)).isDirectory());
115
+ assert((await fs.promises.readdir('/tmp')).includes(basename(path)));
116
+ await fs.promises.rmdir(path);
117
+ });
118
+ });
@@ -0,0 +1,7 @@
1
+ import type { StatsLike } from '@zenfs/core';
2
+ /**
3
+ * Gets unix timestamps from stats
4
+ *
5
+ * @internal
6
+ */
7
+ export declare function unixTimestamps(stats: StatsLike<number>): Record<'atime' | 'mtime', number>;
@@ -0,0 +1,134 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ import assert from 'node:assert/strict';
54
+ import { suite, test } from 'node:test';
55
+ import { wait } from 'utilium';
56
+ import { config, fs } from '../common.js';
57
+ const path = 'x.txt';
58
+ /**
59
+ * Gets unix timestamps from stats
60
+ *
61
+ * @internal
62
+ */
63
+ export function unixTimestamps(stats) {
64
+ return {
65
+ atime: Math.floor(stats.atimeMs),
66
+ mtime: Math.floor(stats.mtimeMs),
67
+ };
68
+ }
69
+ suite('Times', config('times'), () => {
70
+ async function runTest(atime, mtime) {
71
+ const env_1 = { stack: [], error: void 0, hasError: false };
72
+ try {
73
+ // Numbers are seconds since the epoch, while `Date`s and stats are in milliseconds
74
+ const times = {
75
+ atime: typeof atime == 'number' ? Math.floor(atime * 1000) : atime.getTime(),
76
+ mtime: typeof mtime == 'number' ? Math.floor(mtime * 1000) : mtime.getTime(),
77
+ };
78
+ await fs.promises.utimes(path, atime, mtime);
79
+ assert.deepEqual(unixTimestamps(await fs.promises.stat(path)), times);
80
+ await fs.promises.utimes('foobarbaz', atime, mtime).catch((error) => {
81
+ assert(Error.isError(error));
82
+ assert.equal(error.code, 'ENOENT');
83
+ });
84
+ const handle = __addDisposableResource(env_1, await fs.promises.open(path, 'r'), true);
85
+ await handle.utimes(atime, mtime);
86
+ assert.deepEqual(unixTimestamps(await handle.stat()), times);
87
+ fs.utimesSync(path, atime, mtime);
88
+ assert.deepEqual(unixTimestamps(fs.statSync(path)), times);
89
+ try {
90
+ fs.utimesSync('foobarbaz', atime, mtime);
91
+ }
92
+ catch (error) {
93
+ assert.equal(error.code, 'ENOENT');
94
+ }
95
+ try {
96
+ fs.futimesSync(-1, atime, mtime);
97
+ }
98
+ catch (error) {
99
+ // A negative descriptor fails range validation before it is looked up
100
+ assert.equal(error.code, 'ERR_OUT_OF_RANGE');
101
+ }
102
+ }
103
+ catch (e_1) {
104
+ env_1.error = e_1;
105
+ env_1.hasError = true;
106
+ }
107
+ finally {
108
+ const result_1 = __disposeResources(env_1);
109
+ if (result_1)
110
+ await result_1;
111
+ }
112
+ }
113
+ test('utimes works', async () => {
114
+ await test('new Date(...)', () => runTest(new Date('1982/09/10 13:37:00'), new Date('1982/09/10 13:37:00')));
115
+ await test('new Date()', () => runTest(new Date(), new Date()));
116
+ await test('number', () => runTest(123456.789, 123456.789));
117
+ const stats = fs.statSync(path);
118
+ await test('from stats', () => runTest(stats.atime, stats.mtime));
119
+ });
120
+ test('read changes atime', config('sync'), async () => {
121
+ const before = fs.statSync(path).atimeMs;
122
+ fs.readFileSync(path);
123
+ await wait(25);
124
+ const after = fs.statSync(path).atimeMs;
125
+ assert(before < after);
126
+ });
127
+ test('write changes mtime', config('sync', 'write'), async () => {
128
+ const before = fs.statSync(path).mtimeMs;
129
+ fs.writeFileSync(path, 'cool');
130
+ await wait(25);
131
+ const after = fs.statSync(path).mtimeMs;
132
+ assert(before < after);
133
+ });
134
+ });
@@ -0,0 +1 @@
1
+ export {};