Skip to content

Commit 5b5431b

Browse files
committed
feat: 🎸 export fsa() factory
1 parent 9d39b64 commit 5b5431b

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/fsa/__tests__/scenarios.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Superblock } from '../../core/Superblock';
2-
import { coreToFsa } from '../index';
2+
import { coreToFsa, fsa } from '../index';
33
import { CoreFileSystemDirectoryHandle } from '../CoreFileSystemDirectoryHandle';
44
import { onlyOnNode20 } from '../../__tests__/util';
5+
import { fileURLToPath } from 'url';
56

67
onlyOnNode20('coreToFsa scenarios', () => {
78
test('can create FSA from empty Superblock', () => {
@@ -21,6 +22,38 @@ onlyOnNode20('coreToFsa scenarios', () => {
2122
expect(fsa).toBeInstanceOf(CoreFileSystemDirectoryHandle);
2223
});
2324

25+
test('can create FSA using fsa() helper', async () => {
26+
const {dir, core} = fsa({ mode: 'readwrite' });
27+
core.fromJSON({
28+
'documents/readme.txt': 'Welcome!',
29+
'photos/vacation.jpg': Buffer.from('fake-jpg-data'),
30+
'empty-folder': null,
31+
}, '/');
32+
expect(dir).toBeInstanceOf(CoreFileSystemDirectoryHandle);
33+
expect(dir.name).toBe('');
34+
const dir2 = await dir.getDirectoryHandle('documents');
35+
const file = await dir2.getFileHandle('readme.txt');
36+
const fileContent = await file.getFile();
37+
expect(await fileContent.text()).toBe('Welcome!');
38+
});
39+
40+
test('can create a a file folder in empty filesystem', async () => {
41+
const {dir, core} = fsa({ mode: 'readwrite' });
42+
expect(core.toJSON()).toEqual({});
43+
const dir2 = await dir.getDirectoryHandle('new-folder', { create: true });
44+
expect(core.toJSON()).toEqual({
45+
'/new-folder': null,
46+
});
47+
const file = await dir2.getFileHandle('file.txt', { create: true });
48+
expect(core.toJSON()).toEqual({
49+
'/new-folder/file.txt': '',
50+
});
51+
await (await file.createWritable()).write('Hello, world!');
52+
expect(core.toJSON()).toEqual({
53+
'/new-folder/file.txt': 'Hello, world!',
54+
});
55+
});
56+
2457
test('can navigate filesystem structure', async () => {
2558
const core = Superblock.fromJSON(
2659
{

src/fsa/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ export const coreToFsa = (
2020
): CoreFileSystemDirectoryHandle => {
2121
return new CoreFileSystemDirectoryHandle(core, dirPath, ctx);
2222
};
23+
24+
/**
25+
* Create a new instance of an in-memory File System Access API
26+
* implementation rooted at the root directory of the filesystem.
27+
*
28+
* @param ctx Optional context for the File System Access API.
29+
* @returns A File System Access API implementation `dir` rooted at
30+
* the root directory of the filesystem, as well as the `core`
31+
* file system itself.
32+
*/
33+
export const fsa = (ctx?: Partial<CoreFsaContext>) => {
34+
const core = new Superblock();
35+
const dir = new CoreFileSystemDirectoryHandle(core, '/', ctx);
36+
return {dir, core};
37+
};

0 commit comments

Comments
 (0)