Skip to content

Commit

Permalink
Annotations for the File System API (#9127)
Browse files Browse the repository at this point in the history
Summary:
This PR adds flow library definitions for the [File System API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)

I've only added methods and functions that are part of the [WHATWG Standard](https://fs.spec.whatwg.org/#introduction).

Furthermore, I had to add missing flow library definitions for `WritableStream` and `WritableStreamDefaultWriter`, which are a part of the [Streams API](https://streams.spec.whatwg.org/#default-writer-class), since these are a dependency of `FileSystemWritableFileStream`.

API References:
- `FileSystemHandle`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle), [WHATWG](https://fs.spec.whatwg.org/#api-filesystemhandle)
- `FileSystemFileHandle`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle), [WHATWG](https://fs.spec.whatwg.org/#api-filesystemfilehandle)
- `FileSystemDirectoryHandle`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryHandle), [WHATWG](https://fs.spec.whatwg.org/#api-filesystemdirectoryhandle)
- `FileSystemSyncAccessHandle`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemSyncAccessHandle), [WHATWG](https://fs.spec.whatwg.org/#api-filesystemsyncaccesshandle)
- `FileSystemWritableFileStream`: [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream), [WHATWG](https://fs.spec.whatwg.org/#api-filesystemwritablefilestream)

Pull Request resolved: #9127

Reviewed By: skyout

Differential Revision: D54821048

Pulled By: ZelJin

fbshipit-source-id: 7a61967e3ac13f0c566971b4f790ced076a3ce8a
  • Loading branch information
ZelJin authored and facebook-github-bot committed Mar 12, 2024
1 parent 64a8f3c commit 1a32689
Show file tree
Hide file tree
Showing 3 changed files with 433 additions and 308 deletions.
125 changes: 125 additions & 0 deletions lib/bom.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ type USBPermissionDescriptor = {|
exclusionFilters: Array<USBDeviceFilter>;
|}

type FileSystemHandlePermissionDescriptor = {|
mode: "read" | "readwrite";
|}

declare class PermissionStatus extends EventTarget {
onchange: ?((event: any) => mixed);
+state: PermissionState;
Expand Down Expand Up @@ -2441,3 +2445,124 @@ type SetUpOptions = {
index: number;
...
}

declare type FileSystemHandleKind = "file" | "directory";

// https://wicg.github.io/file-system-access/#api-filesystemhandle
declare class FileSystemHandle {
+kind: FileSystemHandleKind;
+name: string;

isSameEntry: (other: FileSystemHandle) => Promise<boolean>;
queryPermission?: (
descriptor: FileSystemHandlePermissionDescriptor
) => Promise<PermissionStatus>;
requestPermission?: (
descriptor: FileSystemHandlePermissionDescriptor
) => Promise<PermissionStatus>;
}

// https://fs.spec.whatwg.org/#api-filesystemfilehandle
declare class FileSystemFileHandle extends FileSystemHandle {
+kind: "file";

constructor(name: string): void;

getFile(): Promise<File>;
createSyncAccessHandle(): Promise<FileSystemSyncAccessHandle>;
createWritable(options?: {|
keepExistingData?: boolean,
|}): Promise<FileSystemWritableFileStream>;
}

// https://fs.spec.whatwg.org/#api-filesystemdirectoryhandle
declare class FileSystemDirectoryHandle extends FileSystemHandle {
+kind: "directory";

constructor(name: string): void;

getDirectoryHandle(
name: string,
options?: {| create?: boolean |}
): Promise<FileSystemDirectoryHandle>;
getFileHandle(
name: string,
options?: {| create?: boolean |}
): Promise<FileSystemFileHandle>;
removeEntry(name: string, options?: {| recursive?: boolean |}): Promise<void>;
resolve(possibleDescendant: FileSystemHandle): Promise<Array<string> | null>;

// Async iterator functions
@@asyncIterator(): AsyncIterator<[string, FileSystemHandle]>;
entries(): AsyncIterator<[string, FileSystemHandle]>;
keys(): AsyncIterator<string>;
values(): AsyncIterator<FileSystemHandle>;
}

// https://fs.spec.whatwg.org/#api-filesystemsyncaccesshandle
declare class FileSystemSyncAccessHandle {
close(): void;
flush(): void;
getSize(): number;
read(buffer: ArrayBuffer, options?: {| at: number |}): number;
truncate(newSize: number): void;
write(buffer: ArrayBuffer, options?: {| at: number |}): number;
}

// https://streams.spec.whatwg.org/#default-writer-class
declare class WritableStreamDefaultWriter {
+closed: Promise<void>;
+desiredSize: number;
+ready: Promise<void>;

constructor(): void;

abort(reason?: string): Promise<void>;
close(): Promise<void>;
releaseLock(): void;
write(chunk: any): Promise<void>;
}

// https://streams.spec.whatwg.org/#ws-class
declare class WriteableStream {
+locked: boolean;

constructor(): void;

abort(reason: string): Promise<string>;
close(): Promise<void>;
getWriter(): WritableStreamDefaultWriter;
}

// https://fs.spec.whatwg.org/#dictdef-writeparams
declare type FileSystemWriteableFileStreamDataTypes =
| ArrayBuffer
| $TypedArray
| DataView
| Blob
| string;

// https://fs.spec.whatwg.org/#dictdef-writeparams
declare type FileSystemWriteableFileStreamData =
| FileSystemWriteableFileStreamDataTypes
| {|
type: "write",
position?: number,
data: FileSystemWriteableFileStreamDataTypes,
|}
| {|
type: "seek",
position: number,
data: FileSystemWriteableFileStreamDataTypes,
|}
| {|
type: "size",
size: number,
|};

// https://fs.spec.whatwg.org/#api-filesystemwritablefilestream
declare class FileSystemWritableFileStream extends WriteableStream {
write(data: FileSystemWriteableFileStreamData): Promise<void>;
truncate(size: number): Promise<void>;
seek(position: number): Promise<void>;
}
Loading

0 comments on commit 1a32689

Please sign in to comment.