-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
191 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import {NodeFileSystemHandle} from "./NodeFileSystemHandle"; | ||
import {basename} from "./util"; | ||
import type {FsaNodeFs} from "./types"; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle | ||
*/ | ||
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle { | ||
constructor ( | ||
protected readonly fs: FsaNodeFs, | ||
protected readonly path: string, | ||
) { | ||
super('directory', basename(path)); | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/entries | ||
*/ | ||
public entries(): AsyncIterableIterator<[string, NodeFileSystemHandle]> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* Returns a new array iterator containing the keys for each item in | ||
* {@link NodeFileSystemDirectoryHandle} object. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/keys | ||
*/ | ||
public keys(): AsyncIterableIterator<string> { | ||
const {path, fs} = this; | ||
return (async function*() { | ||
const list = await fs.promises.readdir(path); | ||
for (const name of list) yield name; | ||
})(); | ||
} | ||
|
||
/** | ||
* Returns a new array iterator containing the values for each index in the | ||
* {@link FileSystemDirectoryHandle} object. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/values | ||
*/ | ||
public values(): AsyncIterableIterator<NodeFileSystemHandle> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getDirectoryHandle | ||
* @param name A string representing the {@link NodeFileSystemHandle} name of | ||
* the subdirectory you wish to retrieve. | ||
* @param options An optional object containing options for the retrieved | ||
* subdirectory. | ||
*/ | ||
public getDirectoryHandle(name: string, options?: GetDirectoryHandleOptions): Promise<NodeFileSystemDirectoryHandle> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/getFileHandle | ||
* @param name A string representing the {@link NodeFileSystemHandle} name of | ||
* the file you wish to retrieve. | ||
* @param options An optional object containing options for the retrieved file. | ||
*/ | ||
public getFileHandle(name: string, options?: GetFileHandleOptions): Promise<NodeFileSystemDirectoryHandle> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/removeEntry | ||
* @param name A string representing the {@link FileSystemHandle} name of the | ||
* entry you wish to remove. | ||
* @param options An optional object containing options. | ||
*/ | ||
public removeEntry(name: string, options?: RemoveEntryOptions): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* The `resolve()` method of the {@link FileSystemDirectoryHandle} interface | ||
* returns an {@link Array} of directory names from the parent handle to the specified | ||
* child entry, with the name of the child entry as the last array item. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle/resolve | ||
* @param possibleDescendant The {@link NodeFileSystemFileHandle} from which | ||
* to return the relative path. | ||
*/ | ||
public resolve(possibleDescendant: NodeFileSystemHandle): Promise<string[] | null> { | ||
throw new Error('Not implemented'); | ||
} | ||
} | ||
|
||
export interface GetDirectoryHandleOptions { | ||
/** | ||
* A boolean value, which defaults to `false`. When set to `true` if the directory | ||
* is not found, one with the specified name will be created and returned. | ||
*/ | ||
create?: boolean; | ||
} | ||
|
||
export interface GetFileHandleOptions { | ||
/** | ||
* A Boolean. Default `false`. When set to `true` if the file is not found, | ||
* one with the specified name will be created and returned. | ||
*/ | ||
create?: boolean; | ||
} | ||
|
||
export interface RemoveEntryOptions { | ||
/** | ||
* A boolean value, which defaults to `false`. When set to true entries will | ||
* be removed recursively. | ||
*/ | ||
recursive?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
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. | ||
This adapter code converts an instance of [Node.js FS API][node-fs] to a | ||
[File System Access API][fsa] (FSA) instance. | ||
|
||
[node-fs]: https://nodejs.org/api/fs.html | ||
[fsa]: https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API |
47 changes: 47 additions & 0 deletions
47
src/node-to-fsa/__tests__/NodeFileSystemDirectoryHandle.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {DirectoryJSON, memfs} from '../..'; | ||
import {NodeFileSystemDirectoryHandle} from '../NodeFileSystemDirectoryHandle'; | ||
|
||
const setup = (json: DirectoryJSON = {}) => { | ||
const fs = memfs(json, '/'); | ||
const dir = new NodeFileSystemDirectoryHandle(fs, '/'); | ||
return {dir, fs}; | ||
}; | ||
|
||
test('can instantiate', () => { | ||
const {dir} = setup(); | ||
expect(dir).toBeInstanceOf(NodeFileSystemDirectoryHandle); | ||
}); | ||
|
||
describe('.keys()', () => { | ||
test('returns an empty iterator for an empty directory', async () => { | ||
const {dir} = setup(); | ||
const keys = dir.keys(); | ||
expect(await keys.next()).toEqual({done: true, value: undefined}); | ||
}); | ||
|
||
test('returns a folder', async () => { | ||
const {dir} = setup({folder: null}); | ||
const list: string [] = []; | ||
for await (const key of dir.keys()) list.push(key); | ||
expect(list).toEqual(['folder']); | ||
}); | ||
|
||
test('returns two folders', async () => { | ||
const {dir} = setup({ | ||
folder: null, | ||
'another/folder': null, | ||
}); | ||
const list: string [] = []; | ||
for await (const key of dir.keys()) list.push(key); | ||
expect(list.length).toBe(2); | ||
}); | ||
|
||
test('returns a file', async () => { | ||
const {dir} = setup({ | ||
'file.txt': 'Hello, world!', | ||
}); | ||
const list: string [] = []; | ||
for await (const key of dir.keys()) list.push(key); | ||
expect(list).toEqual(['file.txt']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
/** | ||
* Required Node.js `fs` module functions for File System Access API. | ||
*/ | ||
export type FsaNodeFs = Pick<typeof import('fs'), 'promises'>; | ||
|
||
export interface NodeFileSystemHandlePermissionDescriptor { | ||
mode: 'read' | 'readwrite'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters