|
| 1 | +declare module "runtime:fs" { |
| 2 | + /** A path: a string, a `file:` URL, or a {@link FsFile} handle. */ |
| 3 | + export type PathLike = string | URL | FsFile; |
| 4 | + |
| 5 | + /** Anything `write` accepts as the body to write. */ |
| 6 | + export type WriteInput = |
| 7 | + | string |
| 8 | + | Blob |
| 9 | + | ArrayBuffer |
| 10 | + | ArrayBufferView |
| 11 | + | Response |
| 12 | + | ReadableStream<Uint8Array> |
| 13 | + | FsFile; |
| 14 | + |
| 15 | + /** File metadata, from {@link stat} / {@link FsFile.stat} (follows symlinks). */ |
| 16 | + export interface Stat { |
| 17 | + /** Size in bytes. */ |
| 18 | + size: number; |
| 19 | + isFile: boolean; |
| 20 | + isDir: boolean; |
| 21 | + isSymlink: boolean; |
| 22 | + /** Modification time in ms since the Unix epoch, or `null` if unavailable. */ |
| 23 | + mtimeMs: number | null; |
| 24 | + } |
| 25 | + |
| 26 | + /** One entry of a directory listing, from {@link readDir}. */ |
| 27 | + export interface DirEntry { |
| 28 | + name: string; |
| 29 | + isFile: boolean; |
| 30 | + isDir: boolean; |
| 31 | + isSymlink: boolean; |
| 32 | + } |
| 33 | + |
| 34 | + /** Options for {@link Glob.scan}. */ |
| 35 | + export interface GlobScanOptions { |
| 36 | + /** Directory to scan (default `"."`). */ |
| 37 | + cwd?: string; |
| 38 | + /** Match dotfiles / dot-directories (default `false`). */ |
| 39 | + dot?: boolean; |
| 40 | + /** Yield absolute paths instead of paths relative to `cwd` (default `false`). */ |
| 41 | + absolute?: boolean; |
| 42 | + /** Yield only files, skipping directories (default `true`). */ |
| 43 | + onlyFiles?: boolean; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A lazy, Blob-like file handle from {@link file}. Nothing is read until a |
| 48 | + * read method is called. **All methods are async.** |
| 49 | + */ |
| 50 | + export interface FsFile { |
| 51 | + /** The path this handle points at. */ |
| 52 | + readonly path: string; |
| 53 | + /** Read the whole file as text (UTF-8). */ |
| 54 | + text(): Promise<string>; |
| 55 | + /** Read and `JSON.parse` the file. */ |
| 56 | + json(): Promise<any>; |
| 57 | + /** Read the whole file as bytes. */ |
| 58 | + bytes(): Promise<Uint8Array>; |
| 59 | + /** Read the whole file as an `ArrayBuffer`. */ |
| 60 | + arrayBuffer(): Promise<ArrayBuffer>; |
| 61 | + /** A `ReadableStream` of the file's bytes. */ |
| 62 | + stream(): ReadableStream<Uint8Array>; |
| 63 | + /** Whether the file exists. */ |
| 64 | + exists(): Promise<boolean>; |
| 65 | + /** File metadata (follows symlinks). */ |
| 66 | + stat(): Promise<Stat>; |
| 67 | + /** Write `data` to this file; resolves to bytes written. */ |
| 68 | + write(data: WriteInput, options?: { append?: boolean }): Promise<number>; |
| 69 | + /** Delete this file. */ |
| 70 | + delete(): Promise<void>; |
| 71 | + /** |
| 72 | + * A `WritableStream` sink for incremental / piped writes: |
| 73 | + * `await readable.pipeTo(file("out").writable())`. The first chunk |
| 74 | + * truncates the file; later chunks append. |
| 75 | + */ |
| 76 | + writable(): WritableStream<Uint8Array>; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Glob matching and scanning. Patterns support `*`, `**`, `?`, `[classes]`, |
| 81 | + * and `{a,b}` alternation; `*` does not cross `/`, `**` does. |
| 82 | + */ |
| 83 | + export class Glob { |
| 84 | + constructor(pattern: string); |
| 85 | + /** Pure pattern match against a path (synchronous; no I/O). */ |
| 86 | + match(path: PathLike): boolean; |
| 87 | + /** Walk the (jailed) filesystem, yielding matching paths. Needs `FileRead`. */ |
| 88 | + scan(options?: string | GlobScanOptions): AsyncIterableIterator<string>; |
| 89 | + } |
| 90 | + |
| 91 | + /** A lazy, Blob-like handle for `path`. */ |
| 92 | + export function file(path: PathLike): FsFile; |
| 93 | + |
| 94 | + /** Write any web body to `dest`; resolves to bytes written. Needs `FileWrite`. */ |
| 95 | + export function write( |
| 96 | + dest: PathLike, |
| 97 | + input: WriteInput, |
| 98 | + options?: { append?: boolean }, |
| 99 | + ): Promise<number>; |
| 100 | + |
| 101 | + /** List the entries of a directory. Needs `FileRead`. */ |
| 102 | + export function readDir(path: PathLike): Promise<DirEntry[]>; |
| 103 | + |
| 104 | + /** Metadata for `path` (follows symlinks). Needs `FileRead`. */ |
| 105 | + export function stat(path: PathLike): Promise<Stat>; |
| 106 | + |
| 107 | + /** Whether `path` exists (missing → `false`). Needs `FileRead`. */ |
| 108 | + export function exists(path: PathLike): Promise<boolean>; |
| 109 | + |
| 110 | + /** Create a directory (`recursive` creates parents). Needs `FileWrite`. */ |
| 111 | + export function mkdir(path: PathLike, options?: { recursive?: boolean }): Promise<void>; |
| 112 | + |
| 113 | + /** Remove a file or (with `recursive`) a directory tree. Needs `FileWrite`. */ |
| 114 | + export function remove(path: PathLike, options?: { recursive?: boolean }): Promise<void>; |
| 115 | + |
| 116 | + /** Rename/move an entry (both jailed). Needs `FileWrite`. */ |
| 117 | + export function rename(from: PathLike, to: PathLike): Promise<void>; |
| 118 | + |
| 119 | + const fs: { |
| 120 | + file: typeof file; |
| 121 | + write: typeof write; |
| 122 | + readDir: typeof readDir; |
| 123 | + stat: typeof stat; |
| 124 | + exists: typeof exists; |
| 125 | + mkdir: typeof mkdir; |
| 126 | + remove: typeof remove; |
| 127 | + rename: typeof rename; |
| 128 | + Glob: typeof Glob; |
| 129 | + }; |
| 130 | + export default fs; |
| 131 | +} |
0 commit comments