Skip to content

Instantly share code, notes, and snippets.

@Kage0x3B
Created August 19, 2023 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kage0x3B/5c6b52cd28b34dfbb8299efa8e926332 to your computer and use it in GitHub Desktop.
Save Kage0x3B/5c6b52cd28b34dfbb8299efa8e926332 to your computer and use it in GitHub Desktop.
Handwritten typescript type definitions for @forlagshuset/simple-fs
declare module '@forlagshuset/simple-fs' {
type DataType = Blob;
type FileMode = 'DIR' | 'FILE' | 'SYMBOLIC_LINK';
type FileFlags = 'READ' | 'WRITE';
type FileFlagsObject = Record<string, unknown>;
class Path {
public path: string;
constructor(path: Path | string);
normalize(): this;
get basename(): string;
get extension(): string;
get parent(): Path;
get isRoot(): boolean;
}
class Node {
public path: string;
public mode: FileMode;
public size: number;
public flags: FileFlagsObject;
public atime: number;
public ctime: number;
public mtime: number;
public blksize?: number;
public nblocks: number;
public data: DataType;
constructor(path: string, mode?: FileMode, size?: number, flags?: FileFlagsObject, data?: DataType, atime?: number, ctime?: number, mtime?: number);
}
class Stats {
public node: Node;
public dev: string;
public size: number;
public atime: number;
public ctime: number;
public mtime: number;
public type: FileMode;
constructor(node: Node, devName: string);
isFile(): this is File;
isDirectory(): boolean;
isSymbolicLink(): boolean;
isSocket(): false;
isFIFO(): false;
isCharacterDevice(): false
isBlockDevice(): false
}
class FileInfo extends Stats {
path: string;
}
class FileSystem {
constructor(opts: { storage?: BaseStorage, name?: string } = {});
mkdir(path: Path | string): Promise<string>;
/**
* Recursively creates directory, eq. to mkdir -p
* @param path
* @param root
*/
mkdirParents(path: Path | string, root = ''): Promise<string>;
rmdir(path: Path | string): Promise<void>;
readFile(path: Path | string): Promise<DataType>;
writeFile(path: Path | string, data: DataType, options?: Partial<FileFlagsObject>): Promise<string>;
/**
* Same as writeFile but it recursively creates directory if not exists
*
* @param path
* @param options
*/
outputFile(path: Path | string, data: DataType, options?: Partial<FileFlagsObject>): Promise<string>;
/**
* Dexie specific to insert multiple files in one go Chrome is quite slow with lots of insertion,
* hence this makes chrome happy
*
* @see https://dev.to/skhmt/why-are-indexeddb-operations-significantly-slower-in-chrome-vs-firefox-1bnd
* @param objs
*/
bulkOutputFiles(objs: { path: string | Path; blob: Blob, options?: Partial<FileFlagsObject> }[]): Promise<void>;
/**
* @deprecated Not implemented yet
* @param oldPath
* @param newPath
*/
rename(oldPath: string | Path, newPath: string | Path): never;
async unlink(path: string | Path): Promise<void>;
async rmdirRecursive(path: string | Path): Promise<void>;
async exists(path: string | Path): Promise<boolean>;
async stats(path: string | Path): Promise<Stats>;
async ls(path: string | Path, filters?: Partial<InternalFileData>): Promise<boolean>;
}
interface InternalFileData {
path: string;
node: Node;
parentId: string;
}
abstract class BaseStorage {
abstract create(path: string, node: Node, parentId: string): Promise<string>;
abstract remove(path: string): Promise<void>;
abstract put(path: string, node: Node, parentId: string): Promise<string>;
abstract get(path: string): Promise<InternalFileData>
abstract where(params: Partial<InternalFileData>): Promise<InternalFileData[]>
abstract isEmpty(parentId: string): Promise<boolean>;
}
class IndexedDbStorage extends BaseStorage {
public name: 'indexeddb';
private storage: unknown;
constructor(storageName = 'default');
public transaction(mode, cb): any;
getBy(key: string, value: string): Promise<InternalFileData[]>;
}
class MemoryStorage extends BaseStorage {
public name: 'memory';
private data: Record<string, unknown>;
constructor(storageName = 'default');
public transaction(mode, cb): any;
getBy(key: string, value: string): Promise<InternalFileData[]>;
}
interface ICustomStorage {
create(path: string, node: Node, parentId: string): Promise<string>;
remove(path: string): Promise<void>;
put(path: string, node: Node, parentId: string): Promise<string>;
get(path: string): Promise<InternalFileData>;
where(params: Partial<InternalFileData>): Promise<InternalFileData[]>;
isEmpty(parentId: string): Promise<boolean>;
}
export type {
ICustomStorage
};
export {
FileSystem,
IndexedDbStorage,
MemoryStorage
};
export default {
FileSystem,
IndexedDbStorage,
MemoryStorage
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment