Skip to content

Commit

Permalink
feat: 🎸 create FSA types folder
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 15, 2023
1 parent ec2706f commit bb0c75a
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 69 deletions.
112 changes: 112 additions & 0 deletions src/fsa/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
export interface IPermissionStatus {
name: string;
state: 'granted' | 'denied' | 'prompt';
}

export interface IFileSystemHandle {
kind: 'file' | 'directory';
name: string;
isSameEntry(fileSystemHandle: IFileSystemHandle): boolean;
queryPermission(fileSystemHandlePermissionDescriptor: NodeFileSystemHandlePermissionDescriptor): IPermissionStatus;
remove(options?: { recursive?: boolean }): Promise<void>;
requestPermission( fileSystemHandlePermissionDescriptor: NodeFileSystemHandlePermissionDescriptor): IPermissionStatus;
}

export interface NodeFileSystemHandlePermissionDescriptor {
mode: 'read' | 'readwrite';
}

export interface IFileSystemDirectoryHandle extends IFileSystemHandle {
keys(): AsyncIterableIterator<string>;
entries(): AsyncIterableIterator<[string, IFileSystemHandle]>;
values(): AsyncIterableIterator<IFileSystemHandle>;
getDirectoryHandle(name: string, options?: GetDirectoryHandleOptions): Promise<IFileSystemDirectoryHandle>;
getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle>;
removeEntry(name: string, options?: RemoveEntryOptions): Promise<void>;
resolve(possibleDescendant: IFileSystemHandle): Promise<string[] | null>;
}

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;
}

export interface IFileSystemFileHandle extends IFileSystemHandle {
getFile(): Promise<File>;
createSyncAccessHandle: undefined | (() => Promise<IFileSystemSyncAccessHandle>);
createWritable(options?: CreateWritableOptions): Promise<IFileSystemWritableFileStream>;
}

export interface CreateWritableOptions {
keepExistingData?: boolean;
}

export interface IFileSystemSyncAccessHandle {
close(): Promise<void>;
flush(): Promise<void>;
getSize(): Promise<number>;
read(buffer: ArrayBuffer | ArrayBufferView, options?: FileSystemReadWriteOptions): Promise<number>;
truncate(newSize: number): Promise<void>;
write(
buffer: ArrayBuffer | ArrayBufferView | DataView,
options?: FileSystemReadWriteOptions
): Promise<number>;
}

export interface FileSystemReadWriteOptions {
/**
* A number representing the offset in bytes that the file should be read from.
*/
at?: number;
}

export interface IFileSystemWritableFileStream extends WritableStream {
seek(position: number): Promise<void>;
truncate(size: number): Promise<void>;
write(chunk: Data): Promise<void>;
write(params: FileSystemWritableFileStreamParams): Promise<void>;
}

export interface FileSystemWritableFileStreamParams {
type: 'write' | 'truncate' | 'seek';
data?: Data;
position?: number;
size?: number;
}

export type Data =
| ArrayBuffer
| ArrayBufferView
| Uint8Array
| Uint8ClampedArray
| Int8Array
| Uint16Array
| Int16Array
| Uint32Array
| Int32Array
| Float32Array
| Float64Array
| BigUint64Array
| BigInt64Array
| DataView
| Blob
| string;
31 changes: 4 additions & 27 deletions src/node-to-fsa/NodeFileSystemDirectoryHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
import { NodeFileSystemFileHandle } from './NodeFileSystemFileHandle';
import type { NodeFsaContext, NodeFsaFs } from './types';
import type Dirent from '../Dirent';
import type {GetDirectoryHandleOptions, GetFileHandleOptions, IFileSystemDirectoryHandle, IFileSystemFileHandle, RemoveEntryOptions} from '../fsa/types';

/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle
*/
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle implements IFileSystemDirectoryHandle {
protected readonly ctx: Partial<NodeFsaContext>;
public constructor(protected readonly fs: NodeFsaFs, public readonly __path: string, ctx: Partial<NodeFsaContext> = {}) {
super('directory', basename(__path, ctx.separator || '/'));
Expand Down Expand Up @@ -70,7 +71,7 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
public async getDirectoryHandle(
name: string,
options?: GetDirectoryHandleOptions,
): Promise<NodeFileSystemDirectoryHandle> {
): Promise<IFileSystemDirectoryHandle> {
assertName(name, 'getDirectoryHandle', 'FileSystemDirectoryHandle');
const filename = this.__path + this.ctx.separator! + name;
try {
Expand Down Expand Up @@ -106,7 +107,7 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
* the file you wish to retrieve.
* @param options An optional object containing options for the retrieved file.
*/
public async getFileHandle(name: string, options?: GetFileHandleOptions): Promise<NodeFileSystemFileHandle> {
public async getFileHandle(name: string, options?: GetFileHandleOptions): Promise<IFileSystemFileHandle> {
assertName(name, 'getFileHandle', 'FileSystemDirectoryHandle');
const filename = this.__path + this.ctx.separator! + name;
try {
Expand Down Expand Up @@ -197,27 +198,3 @@ export class NodeFileSystemDirectoryHandle extends NodeFileSystemHandle {
return null;
}
}

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;
}
5 changes: 3 additions & 2 deletions src/node-to-fsa/NodeFileSystemFileHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { NodeFileSystemSyncAccessHandle } from './NodeFileSystemSyncAccessHandle
import { basename, ctx as createCtx, newNotAllowedError } from './util';
import { NodeFileSystemWritableFileStream } from './NodeFileSystemWritableFileStream';
import type { NodeFsaContext, NodeFsaFs } from './types';
import type {IFileSystemFileHandle, IFileSystemSyncAccessHandle} from '../fsa/types';

export class NodeFileSystemFileHandle extends NodeFileSystemHandle {
export class NodeFileSystemFileHandle extends NodeFileSystemHandle implements IFileSystemFileHandle {
protected readonly ctx: NodeFsaContext;

constructor(protected readonly fs: NodeFsaFs, public readonly __path: string, ctx: Partial<NodeFsaContext> = {}) {
Expand Down Expand Up @@ -43,7 +44,7 @@ export class NodeFileSystemFileHandle extends NodeFileSystemHandle {
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle/createSyncAccessHandle
*/
public get createSyncAccessHandle(): undefined | (() => Promise<NodeFileSystemSyncAccessHandle>) {
public get createSyncAccessHandle(): undefined | (() => Promise<IFileSystemSyncAccessHandle>) {
if (!this.ctx.syncHandleAllowed) return undefined;
return async () => new NodeFileSystemSyncAccessHandle(this.fs, this.__path, this.ctx);
}
Expand Down
4 changes: 2 additions & 2 deletions src/node-to-fsa/NodeFileSystemHandle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { NodePermissionStatus } from './NodePermissionStatus';
import type { NodeFileSystemHandlePermissionDescriptor } from './types';
import type {IFileSystemHandle, NodeFileSystemHandlePermissionDescriptor} from '../fsa/types';

/**
* Represents a File System Access API file handle `FileSystemHandle` object,
* which was created from a Node.js `fs` module.
*
* @see [MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle)
*/
export abstract class NodeFileSystemHandle {
export abstract class NodeFileSystemHandle implements IFileSystemHandle {
constructor(public readonly kind: 'file' | 'directory', public readonly name: string) {}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/node-to-fsa/NodeFileSystemSyncAccessHandle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type {FileSystemReadWriteOptions, IFileSystemSyncAccessHandle} from '../fsa/types';
import type { NodeFsaContext, NodeFsaFs } from './types';

/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle
*/
export class NodeFileSystemSyncAccessHandle {
export class NodeFileSystemSyncAccessHandle implements IFileSystemSyncAccessHandle {
protected readonly fd: number;

constructor(
Expand Down Expand Up @@ -92,10 +93,3 @@ export class NodeFileSystemSyncAccessHandle {
}
}
}

export interface FileSystemReadWriteOptions {
/**
* A number representing the offset in bytes that the file should be read from.
*/
at?: number;
}
28 changes: 2 additions & 26 deletions src/node-to-fsa/NodeFileSystemWritableFileStream.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {Data, FileSystemWritableFileStreamParams, IFileSystemWritableFileStream} from '../fsa/types';
import type { IFileHandle } from '../promises';
import type { NodeFsaFs } from './types';

Expand Down Expand Up @@ -56,7 +57,7 @@ interface SwapFile {
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream
*/
export class NodeFileSystemWritableFileStream extends WritableStream {
export class NodeFileSystemWritableFileStream extends WritableStream implements IFileSystemWritableFileStream {
protected readonly swap: SwapFile;

constructor(protected readonly fs: NodeFsaFs, protected readonly path: string, keepExistingData: boolean) {
Expand Down Expand Up @@ -174,28 +175,3 @@ export class NodeFileSystemWritableFileStream extends WritableStream {
}
}
}

export interface FileSystemWritableFileStreamParams {
type: 'write' | 'truncate' | 'seek';
data?: Data;
position?: number;
size?: number;
}

export type Data =
| ArrayBuffer
| ArrayBufferView
| Uint8Array
| Uint8ClampedArray
| Int8Array
| Uint16Array
| Int16Array
| Uint32Array
| Int32Array
| Float32Array
| Float64Array
| BigUint64Array
| BigInt64Array
| DataView
| Blob
| string;
4 changes: 0 additions & 4 deletions src/node-to-fsa/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ export interface NodeFsaContext {
/** Whether synchronous file handles are allowed. */
syncHandleAllowed: boolean;
}

export interface NodeFileSystemHandlePermissionDescriptor {
mode: 'read' | 'readwrite';
}

0 comments on commit bb0c75a

Please sign in to comment.