Skip to content

Commit 33f9af0

Browse files
committed
feat: 🎸 implement keys() method
1 parent 8b27695 commit 33f9af0

6 files changed

Lines changed: 191 additions & 3 deletions

File tree

‎src/index.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,29 @@ export function createFsFromVolume(vol: _Volume): IFs {
4646
fs.promises = vol.promises;
4747

4848
fs._toUnixTimestamp = toUnixTimestamp;
49+
(fs as any).__vol = vol;
4950

5051
return fs;
5152
}
5253

5354
export const fs: IFs = createFsFromVolume(vol);
55+
56+
/**
57+
* Creates a new file system instance.
58+
*
59+
* @param json File system structure expressed as a JSON object.
60+
* Use `null` for empty directories and empty string for empty files.
61+
* @param cwd Current working directory. The JSON structure will be created
62+
* relative to this path.
63+
* @returns A `memfs` file system instance, which is a drop-in replacement for
64+
* the `fs` module.
65+
*/
66+
export const memfs = (json: DirectoryJSON = {}, cwd: string = '/') => {
67+
const volume = Volume.fromJSON(json, cwd);
68+
const fs = createFsFromVolume(volume);
69+
return fs as IFs & typeof import('fs');
70+
};
71+
5472
declare let module;
5573
module.exports = { ...module.exports, ...fs };
56-
5774
module.exports.semantic = true;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import {NodeFileSystemHandle} from "./NodeFileSystemHandle";
2+
import {basename} from "./util";
3+
import type {FsaNodeFs} from "./types";
4+
5+
/**
6+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
7+
*/
8+
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
9+
constructor (
10+
protected readonly fs: FsaNodeFs,
11+
protected readonly path: string,
12+
) {
13+
super('directory', basename(path));
14+
}
15+
16+
/**
17+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries
18+
*/
19+
public entries(): AsyncIterableIterator<[string, NodeFileSystemHandle]> {
20+
throw new Error('Not implemented');
21+
}
22+
23+
/**
24+
* Returns a new array iterator containing the keys for each item in
25+
* {@link NodeFileSystemDirectoryHandle} object.
26+
*
27+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys
28+
*/
29+
public keys(): AsyncIterableIterator<string> {
30+
const {path, fs} = this;
31+
return (async function*() {
32+
const list = await fs.promises.readdir(path);
33+
for (const name of list) yield name;
34+
})();
35+
}
36+
37+
/**
38+
* Returns a new array iterator containing the values for each index in the
39+
* {@link FileSystemDirectoryHandle} object.
40+
*
41+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values
42+
*/
43+
public values(): AsyncIterableIterator<NodeFileSystemHandle> {
44+
throw new Error('Not implemented');
45+
}
46+
47+
/**
48+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle
49+
* @param name A string representing the {@link NodeFileSystemHandle} name of
50+
* the subdirectory you wish to retrieve.
51+
* @param options An optional object containing options for the retrieved
52+
* subdirectory.
53+
*/
54+
public getDirectoryHandle(name: string, options?: GetDirectoryHandleOptions): Promise<NodeFileSystemDirectoryHandle> {
55+
throw new Error('Not implemented');
56+
}
57+
58+
/**
59+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle
60+
* @param name A string representing the {@link NodeFileSystemHandle} name of
61+
* the file you wish to retrieve.
62+
* @param options An optional object containing options for the retrieved file.
63+
*/
64+
public getFileHandle(name: string, options?: GetFileHandleOptions): Promise<NodeFileSystemDirectoryHandle> {
65+
throw new Error('Not implemented');
66+
}
67+
68+
/**
69+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry
70+
* @param name A string representing the {@link FileSystemHandle} name of the
71+
* entry you wish to remove.
72+
* @param options An optional object containing options.
73+
*/
74+
public removeEntry(name: string, options?: RemoveEntryOptions): Promise<void> {
75+
throw new Error('Not implemented');
76+
}
77+
78+
/**
79+
* The `resolve()` method of the {@link FileSystemDirectoryHandle} interface
80+
* returns an {@link Array} of directory names from the parent handle to the specified
81+
* child entry, with the name of the child entry as the last array item.
82+
*
83+
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve
84+
* @param possibleDescendant The {@link NodeFileSystemFileHandle} from which
85+
* to return the relative path.
86+
*/
87+
public resolve(possibleDescendant: NodeFileSystemHandle): Promise<string[] | null> {
88+
throw new Error('Not implemented');
89+
}
90+
}
91+
92+
export interface GetDirectoryHandleOptions {
93+
/**
94+
* A boolean value, which defaults to `false`. When set to `true` if the directory
95+
* is not found, one with the specified name will be created and returned.
96+
*/
97+
create?: boolean;
98+
}
99+
100+
export interface GetFileHandleOptions {
101+
/**
102+
* A Boolean. Default `false`. When set to `true` if the file is not found,
103+
* one with the specified name will be created and returned.
104+
*/
105+
create?: boolean;
106+
}
107+
108+
export interface RemoveEntryOptions {
109+
/**
110+
* A boolean value, which defaults to `false`. When set to true entries will
111+
* be removed recursively.
112+
*/
113+
recursive?: boolean;
114+
}

‎src/node-to-fsa/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
This adapter code converts an instance of [Node.js FS API](https://nodejs.org/api/fs.html) to a [File System Access API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API) (FSA) instance.
1+
This adapter code converts an instance of [Node.js FS API][node-fs] to a
2+
[File System Access API][fsa] (FSA) instance.
3+
4+
[node-fs]: https://nodejs.org/api/fs.html
5+
[fsa]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {DirectoryJSON, memfs} from '../..';
2+
import {NodeFileSystemDirectoryHandle} from '../NodeFileSystemDirectoryHandle';
3+
4+
const setup = (json: DirectoryJSON = {}) => {
5+
const fs = memfs(json, '/');
6+
const dir = new NodeFileSystemDirectoryHandle(fs, '/');
7+
return {dir, fs};
8+
};
9+
10+
test('can instantiate', () => {
11+
const {dir} = setup();
12+
expect(dir).toBeInstanceOf(NodeFileSystemDirectoryHandle);
13+
});
14+
15+
describe('.keys()', () => {
16+
test('returns an empty iterator for an empty directory', async () => {
17+
const {dir} = setup();
18+
const keys = dir.keys();
19+
expect(await keys.next()).toEqual({done: true, value: undefined});
20+
});
21+
22+
test('returns a folder', async () => {
23+
const {dir} = setup({folder: null});
24+
const list: string [] = [];
25+
for await (const key of dir.keys()) list.push(key);
26+
expect(list).toEqual(['folder']);
27+
});
28+
29+
test('returns two folders', async () => {
30+
const {dir} = setup({
31+
folder: null,
32+
'another/folder': null,
33+
});
34+
const list: string [] = [];
35+
for await (const key of dir.keys()) list.push(key);
36+
expect(list.length).toBe(2);
37+
});
38+
39+
test('returns a file', async () => {
40+
const {dir} = setup({
41+
'file.txt': 'Hello, world!',
42+
});
43+
const list: string [] = [];
44+
for await (const key of dir.keys()) list.push(key);
45+
expect(list).toEqual(['file.txt']);
46+
});
47+
});

‎src/node-to-fsa/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* Required Node.js `fs` module functions for File System Access API.
3+
*/
4+
export type FsaNodeFs = Pick<typeof import('fs'), 'promises'>;
5+
16
export interface NodeFileSystemHandlePermissionDescriptor {
27
mode: 'read' | 'readwrite';
38
}

‎src/volume.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as pathModule from 'path';
2-
import { PathLike, symlink } from 'fs';
32
import { Node, Link, File } from './node';
43
import Stats from './Stats';
54
import Dirent from './Dirent';
@@ -14,6 +13,7 @@ import { TEncodingExtended, TDataOut, assertEncoding, strToEncoding, ENCODING_UT
1413
import * as errors from './internal/errors';
1514
import util = require('util');
1615
import createPromisesApi from './promises';
16+
import type { PathLike, symlink } from 'fs';
1717

1818
const resolveCrossPlatform = pathModule.resolve;
1919
const {
@@ -930,6 +930,7 @@ export class Volume {
930930
return json;
931931
}
932932

933+
// TODO: `cwd` should probably not invoke `process.cwd()`.
933934
fromJSON(json: DirectoryJSON, cwd: string = process.cwd()) {
934935
for (let filename in json) {
935936
const data = json[filename];

0 commit comments

Comments
 (0)