From 7d4a7aebf377bd0342b0a319a91abca52a769047 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 16:57:19 +0000 Subject: [PATCH 01/31] dofs: add local hardlink support Represent hardlinks as additional dirents pointing at the same file inode. Add a storage-level link helper and expose it through the SQLiteWorkspaceProvider sync and async APIs. Unlink now removes only the requested dirent and reaps file chunks and the inode only after the final link disappears. Rename displacement uses the same final-link rule, and renaming one hardlink onto another removes only the source name. Provider stats now report the actual link count for inode-backed entries. Tests cover shared inode identity, nlink reporting, writes through one name being visible through the other, unlink preservation, hardlink rename behavior, and common error paths. --- packages/dofs/src/fs/link.ts | 80 ++++++++++++++++++++++++++++++ packages/dofs/src/fs/rm.ts | 39 +++++++++++---- packages/dofs/src/index.ts | 1 + packages/dofs/src/provider.test.ts | 70 ++++++++++++++++++++++++++ packages/dofs/src/provider.ts | 73 ++++++++++++++++++++++----- 5 files changed, 241 insertions(+), 22 deletions(-) create mode 100644 packages/dofs/src/fs/link.ts diff --git a/packages/dofs/src/fs/link.ts b/packages/dofs/src/fs/link.ts new file mode 100644 index 00000000..bdeffa3c --- /dev/null +++ b/packages/dofs/src/fs/link.ts @@ -0,0 +1,80 @@ +import { createWorkspaceError } from "../errors.js"; +import { canonicalizePath } from "../path.js"; +import { incrementRev } from "../rev.js"; +import { ROOT_INODE } from "../schema/index.js"; +import type { Database } from "../storage.js"; +import { assertNotReadOnly } from "./mount-guard.js"; +import { resolveInode } from "./resolve.js"; + +function resolveParent(db: Database, parts: string[], canonical: string): number { + let parentInode = ROOT_INODE; + for (let i = 0; i < parts.length - 1; i++) { + const child = db.one<{ child_inode: number }>( + "SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + parentInode, + parts[i], + ); + if (child === undefined) { + throw createWorkspaceError("ENOENT", `parent directory missing: ${canonical}`, canonical); + } + const next = db.one<{ inode: number; type: "file" | "dir" | "symlink" }>( + "SELECT inode, type FROM vfs_nodes WHERE inode = ?", + child.child_inode, + ); + if (next === undefined) { + throw createWorkspaceError("ENOENT", `dangling dirent: ${canonical}`, canonical); + } + if (next.type !== "dir") { + throw createWorkspaceError( + "ENOTDIR", + `parent path segment is not a directory: ${canonical}`, + canonical, + ); + } + parentInode = next.inode; + } + return parentInode; +} + +export function link(db: Database, existingPath: string, newPath: string): void { + const { parts, path: canonicalNew } = canonicalizePath(newPath); + if (parts.length === 0) { + throw createWorkspaceError("EEXIST", "cannot link onto root", canonicalNew); + } + + assertNotReadOnly(db, canonicalNew); + + db.transactionSync(() => { + const source = resolveInode(db, existingPath); + if (source === null) { + throw createWorkspaceError("ENOENT", `no such file: ${existingPath}`, existingPath); + } + if (source.type !== "file") { + throw createWorkspaceError( + "EPERM", + `cannot hardlink non-file: ${existingPath}`, + existingPath, + ); + } + + const parentInode = resolveParent(db, parts, canonicalNew); + const leafName = parts[parts.length - 1]; + const existing = db.one<{ child_inode: number }>( + "SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + parentInode, + leafName, + ); + if (existing !== undefined) { + throw createWorkspaceError("EEXIST", `path exists: ${canonicalNew}`, canonicalNew); + } + + db.run( + "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", + parentInode, + leafName, + source.inode, + ); + const rev = incrementRev(db); + db.run("UPDATE vfs_nodes SET rev = ? WHERE inode = ?", rev, source.inode); + }); +} diff --git a/packages/dofs/src/fs/rm.ts b/packages/dofs/src/fs/rm.ts index 98050d45..a19f1aa2 100644 --- a/packages/dofs/src/fs/rm.ts +++ b/packages/dofs/src/fs/rm.ts @@ -95,26 +95,47 @@ export function rm(db: Database, path: string, options: RmOptions): void { const rev = incrementRev(db); if (node.type === "file" || !recursive) { - // Single inode removal — file, or empty directory. - removeInode(db, node.inode, node.type); + // Single entry removal — file, symlink, or empty directory. A + // file inode may have multiple dirents (hardlinks), so remove + // only the requested name and reap chunks/node after the final + // link disappears. + removeEntry(db, canonical, node.inode, node.type); recordDelete(db, rev, canonical); return; } // Recursive directory removal. Walk leaves first so each delete - // sees an empty parent by the time we get to it. + // sees an empty parent by the time we get to it. File entries may + // be hardlinked outside this subtree, so delete by path rather + // than by child inode. for (const entry of walkPostOrder(db, node.inode, canonical)) { - removeInode(db, entry.inode, entry.type); + removeEntry(db, entry.path, entry.inode, entry.type); recordDelete(db, rev, entry.path); } }); } -function removeInode(db: Database, inode: number, type: "file" | "dir" | "symlink"): void { - // Drop the dirent referencing this inode. There should be exactly one - // (no hardlinks yet); if zero, we're deleting the root which we've - // already refused. - db.run("DELETE FROM vfs_dirents WHERE child_inode = ?", inode); +function removeEntry( + db: Database, + path: string, + inode: number, + type: "file" | "dir" | "symlink", +): void { + const { parts, path: canonical } = canonicalizePath(path); + const name = parts[parts.length - 1]; + const parentPath = parts.length === 1 ? "/" : `/${parts.slice(0, -1).join("/")}`; + const parent = resolveInode(db, parentPath, { followSymlinks: false }); + if (parent === null || parent.type !== "dir") { + throw createWorkspaceError("ENOENT", `parent directory missing: ${canonical}`, canonical); + } + + db.run("DELETE FROM vfs_dirents WHERE parent_inode = ? AND name = ?", parent.inode, name); + const remaining = db.scalar( + "SELECT COUNT(*) FROM vfs_dirents WHERE child_inode = ?", + inode, + ); + if ((remaining ?? 0) > 0) return; + if (type === "file") { db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); } diff --git a/packages/dofs/src/index.ts b/packages/dofs/src/index.ts index 37f8803e..54c0b704 100644 --- a/packages/dofs/src/index.ts +++ b/packages/dofs/src/index.ts @@ -6,6 +6,7 @@ export { } from "./fs/filesystem.js"; export type { WorkspaceFoundEntry } from "./fs/find.js"; export type { GrepOptions, WorkspaceGrepMatch } from "./fs/grep.js"; +export { link } from "./fs/link.js"; export type { MkdirOptions } from "./fs/mkdir.js"; // Read-only mount enforcement. The workspace-side indexer writes // _vfs_mounts; the helpers here let it invalidate the in-Database diff --git a/packages/dofs/src/provider.test.ts b/packages/dofs/src/provider.test.ts index 67c4db51..858f877b 100644 --- a/packages/dofs/src/provider.test.ts +++ b/packages/dofs/src/provider.test.ts @@ -72,6 +72,76 @@ describe("SQLiteWorkspaceProvider — implemented methods", () => { }); }); + it("linkSync creates a second path to the same file inode", async () => { + await withProvider((p) => { + p.writeFileSync("/a.txt", "hi"); + p.linkSync("/a.txt", "/b.txt"); + + const a = p.statSync("/a.txt"); + const b = p.statSync("/b.txt"); + expect(a.ino).toBe(b.ino); + expect(a.nlink).toBe(2); + expect(b.nlink).toBe(2); + expect(p.readFileSync("/b.txt", "utf8")).toBe("hi"); + }); + }); + + it("writes through one hardlink are visible through the other", async () => { + await withProvider((p) => { + p.writeFileSync("/a.txt", "hi"); + p.linkSync("/a.txt", "/b.txt"); + p.writeFileSync("/b.txt", "bye"); + + expect(p.readFileSync("/a.txt", "utf8")).toBe("bye"); + expect(p.statSync("/a.txt").nlink).toBe(2); + expect(p.statSync("/b.txt").nlink).toBe(2); + }); + }); + + it("unlinkSync removes one hardlink without deleting the inode", async () => { + await withProvider((p) => { + p.writeFileSync("/a.txt", "hi"); + p.linkSync("/a.txt", "/b.txt"); + p.unlinkSync("/a.txt"); + + expect(p.existsSync("/a.txt")).toBe(false); + expect(p.readFileSync("/b.txt", "utf8")).toBe("hi"); + expect(p.statSync("/b.txt").nlink).toBe(1); + }); + }); + + it("renameSync from one hardlink onto another removes only the source name", async () => { + await withProvider((p) => { + p.writeFileSync("/a.txt", "hi"); + p.linkSync("/a.txt", "/b.txt"); + p.renameSync("/a.txt", "/b.txt"); + + expect(p.existsSync("/a.txt")).toBe(false); + expect(p.readFileSync("/b.txt", "utf8")).toBe("hi"); + expect(p.statSync("/b.txt").nlink).toBe(1); + }); + }); + + it("linkSync rejects invalid links", async () => { + await withProvider((p) => { + p.writeFileSync("/a.txt", "hi"); + p.mkdirSync("/dir", {}); + + expect(() => p.linkSync("/missing", "/missing-link")).toThrowError( + expect.objectContaining({ code: "ENOENT" }), + ); + expect(() => p.linkSync("/a.txt", "/a.txt")).toThrowError( + expect.objectContaining({ code: "EEXIST" }), + ); + expect(() => p.linkSync("/dir", "/dir-link")).toThrowError( + expect.objectContaining({ code: "EPERM" }), + ); + expect(() => p.linkSync("/a.txt", "/missing-parent/b.txt")).toThrowError( + expect.objectContaining({ code: "ENOENT" }), + ); + }); + }); + it("rmdirSync removes an empty directory", async () => { await withProvider((p) => { p.mkdirSync("/a", {}); diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 73005570..f84b2a54 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -8,6 +8,7 @@ // I/O, truncate, symlinks, watch). import { createWorkspaceError } from "./errors.js"; +import { link as linkImpl } from "./fs/link.js"; import type { MkdirOptions } from "./fs/mkdir.js"; import { mkdir as mkdirImpl } from "./fs/mkdir.js"; import { readdir as readdirImpl } from "./fs/readdir.js"; @@ -170,6 +171,7 @@ export class SQLiteWorkspaceProvider { isFile: s.isFile, isDirectory: s.isDirectory, isSymbolicLink: false, + nlink: linkCount(this.db, ino), }); } @@ -199,6 +201,7 @@ export class SQLiteWorkspaceProvider { isFile: node.type === "file", isDirectory: node.type === "dir", isSymbolicLink: isSymlink, + nlink: linkCount(this.db, node.inode), }); } @@ -244,6 +247,15 @@ export class SQLiteWorkspaceProvider { rmImpl(this.db, path, {}); } + link(existingPath: string, newPath: string): Promise { + this.linkSync(existingPath, newPath); + return Promise.resolve(); + } + + linkSync(existingPath: string, newPath: string): void { + linkImpl(this.db, existingPath, newPath); + } + rename(oldPath: string, newPath: string): Promise { this.renameSync(oldPath, newPath); return Promise.resolve(); @@ -258,7 +270,19 @@ export class SQLiteWorkspaceProvider { if (node === null) { throw createWorkspaceError("ENOENT", `no such path: ${oldPath}`, oldPath); } + const { parts: oldParts, path: oldCanonical } = canonicalizePath(oldPath); + const oldName = oldParts[oldParts.length - 1]; + const oldParentPath = oldParts.length === 1 ? "/" : `/${oldParts.slice(0, -1).join("/")}`; + const oldParent = resolveInode(this.db, oldParentPath, { followSymlinks: false }); + if (oldParent === null || oldParent.type !== "dir") { + throw createWorkspaceError( + "ENOENT", + `parent directory missing: ${oldCanonical}`, + oldCanonical, + ); + } const { parts, path: newCanonical } = canonicalizePath(newPath); + if (oldCanonical === newCanonical) return; if (parts.length === 0) { throw createWorkspaceError("EINVAL", "cannot rename onto root", newCanonical); } @@ -286,7 +310,8 @@ export class SQLiteWorkspaceProvider { newParent.inode, newName, ); - if (existing !== undefined && existing.child_inode !== node.inode) { + const destinationAlreadyNamesSource = existing?.child_inode === node.inode; + if (existing !== undefined && !destinationAlreadyNamesSource) { // Refuse to overwrite a non-empty directory or replace a // directory with a file (Linux rename semantics). if (existing.type === "dir") { @@ -298,20 +323,36 @@ export class SQLiteWorkspaceProvider { throw createWorkspaceError("ENOTEMPTY", `not empty: ${newCanonical}`, newCanonical); } } - // Unlink the displaced inode. vfs_chunks / vfs_blob_bytes - // referenced by file chunks become orphaned and gc() reaps - // them after the safety window. - this.db.run("DELETE FROM vfs_dirents WHERE child_inode = ?", existing.child_inode); - this.db.run("DELETE FROM vfs_chunks WHERE inode = ?", existing.child_inode); - this.db.run("DELETE FROM vfs_nodes WHERE inode = ?", existing.child_inode); + // Unlink only the displaced destination name. If other + // hardlinks still reference the displaced file inode, keep its + // chunks and node alive. + this.db.run( + "DELETE FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + newParent.inode, + newName, + ); + const remaining = this.db.scalar( + "SELECT COUNT(*) FROM vfs_dirents WHERE child_inode = ?", + existing.child_inode, + ); + if ((remaining ?? 0) === 0) { + this.db.run("DELETE FROM vfs_chunks WHERE inode = ?", existing.child_inode); + this.db.run("DELETE FROM vfs_nodes WHERE inode = ?", existing.child_inode); + } } - this.db.run("DELETE FROM vfs_dirents WHERE child_inode = ?", node.inode); this.db.run( - "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", - newParent.inode, - newName, - node.inode, + "DELETE FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + oldParent.inode, + oldName, ); + if (!destinationAlreadyNamesSource) { + this.db.run( + "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", + newParent.inode, + newName, + node.inode, + ); + } }); } @@ -641,6 +682,7 @@ interface StatsInputs { isFile: boolean; isDirectory: boolean; isSymbolicLink: boolean; + nlink: number; } // POSIX mode-bit constants. Linux FUSE rejects a stat whose mode @@ -657,12 +699,17 @@ function fileTypeBits(input: StatsInputs): number { return 0; } +function linkCount(db: Database, inode: number): number { + const count = db.scalar("SELECT COUNT(*) FROM vfs_dirents WHERE child_inode = ?", inode); + return Math.max(1, count ?? 0); +} + function wrapStats(input: StatsInputs): VirtualStatsLike { const mtime = new Date(input.mtimeMs); return { dev: 0, mode: (input.mode & 0o7777) | fileTypeBits(input), - nlink: 1, + nlink: input.nlink, uid: 0, gid: 0, rdev: 0, From 1728593f350a383fb769b6a0b6a9f5a276d2470f Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 16:57:28 +0000 Subject: [PATCH 02/31] wsd: wire FUSE hardlink operations Implement FUSE link(2) by forwarding to the DOFS provider hardlink primitive. Dirty or pending source buffers are flushed before linking so the provider has a real inode to reference, and the destination shares the same in-memory file entry when the source was already tracked by the driver. Expose provider nlink through getattr and stop treating link as an unimplemented FUSE operation. Tests cover same-inode linking, writes through a linked path, pending-create sources, and POSIX error translation for missing sources and existing destinations. --- packages/wsd/src/fuse/driver.test.ts | 59 ++++++++++++++++++++++++++- packages/wsd/src/fuse/driver.ts | 36 ++++++++++++++-- packages/wsd/src/fuse/options.test.ts | 9 ++-- packages/wsd/src/fuse/options.ts | 6 ++- packages/wsd/src/fuse/vfs.ts | 18 +++++++- 5 files changed, 117 insertions(+), 11 deletions(-) diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index e7165b22..e6b8743f 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -46,7 +46,7 @@ const fuseNativeOperationNames = [ "rmdir", ]; -const notImplementedOperationNames = ["error", "mknod", "link"]; +const notImplementedOperationNames = ["error", "mknod"]; test("FUSE ops expose the complete fuse-native operation surface", async () => { const ops = makeFUSEOps((await createNodeVirtualFileSystem()).vfs); @@ -621,6 +621,63 @@ test("FUSE create+chmod+flush persists the chmod'd mode in the VFS", async () => expect(vfs.statSync("/new.txt").mode & 0o7777).toBe(0o600); }); +test("FUSE link creates a second name for the same file inode", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + vfs.writeFileSync("/a.txt", Buffer.from("hi")); + const ops = makeFUSEOps(vfs); + + expect(await status((cb) => ops.link("/a.txt", "/b.txt", cb))).toBe(0); + + const a = await callback((cb) => ops.getattr("/a.txt", cb)); + const b = await callback((cb) => ops.getattr("/b.txt", cb)); + expect(a.errno).toBe(0); + expect(b.errno).toBe(0); + expect((a.result as { nlink: number; ino: number }).nlink).toBe(2); + expect((b.result as { nlink: number; ino: number }).nlink).toBe(2); + expect((a.result as { nlink: number; ino: number }).ino).toBe( + (b.result as { nlink: number; ino: number }).ino, + ); + + const open = await callback((cb) => ops.open("/b.txt", 0, cb)); + expect(open.errno).toBe(0); + const payload = Buffer.from("bye"); + expect( + await status((cb) => + ops.write("/b.txt", open.result as number, payload, payload.byteLength, 0, cb), + ), + ).toBe(payload.byteLength); + expect(await status((cb) => ops.flush("/b.txt", open.result as number, cb))).toBe(0); + + expect(Buffer.from(vfs.readFileSync("/a.txt")).toString("utf8")).toBe("bye"); +}); + +test("FUSE link flushes a pending-create source before linking", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + const ops = makeFUSEOps(vfs); + + const create = await callback((cb: (errno: number, result: unknown) => void) => + ops.create("/src.txt", 0o644, cb), + ); + expect(create.errno).toBe(0); + const fh = create.result as number; + const payload = Buffer.from("linked"); + await status((cb) => ops.write("/src.txt", fh, payload, payload.byteLength, 0, cb)); + + expect(await status((cb) => ops.link("/src.txt", "/dst.txt", cb))).toBe(0); + expect(Buffer.from(vfs.readFileSync("/dst.txt")).toString("utf8")).toBe("linked"); + expect(vfs.statSync("/src.txt").ino).toBe(vfs.statSync("/dst.txt").ino); + expect(vfs.statSync("/src.txt").nlink).toBe(2); +}); + +test("FUSE link surfaces POSIX errors", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + vfs.writeFileSync("/a.txt", Buffer.from("hi")); + const ops = makeFUSEOps(vfs); + + expect(await status((cb) => ops.link("/missing.txt", "/b.txt", cb))).toBe(-2); + expect(await status((cb) => ops.link("/a.txt", "/a.txt", cb))).toBe(-17); +}); + test("FUSE rename of a pending-create file overwrites an existing destination", async () => { // Regression: the pending-create branch in rename returned // EEXIST when the destination existed. POSIX rename(2) allows diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 2e141c78..0418ba54 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -12,6 +12,7 @@ const ERRNO = { ENOTDIR: -20, EISDIR: -21, EINVAL: -22, + EPERM: -1, EFBIG: -27, ENOTEMPTY: -39, ENODATA: -61, @@ -81,7 +82,7 @@ export interface FuseOps { getxattr(path: string, name: string, position: number, cb: StatusCallback): void; listxattr(path: string, cb: ResultCallback): void; removexattr(path: string, name: string, cb: StatusCallback): void; - link: NotImplementedOperation; + link(source: string, destination: string, cb: StatusCallback): void; symlink(target: string, path: string, cb: StatusCallback): void; } @@ -94,6 +95,7 @@ export interface FuseStat { uid: number; gid: number; nlink: number; + ino: number; } export interface FuseMount { @@ -196,6 +198,9 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO }; const files = new Map(); const rangedWriteVfs = vfs as NodeVirtualFileSystem & Partial; + const linkableVfs = vfs as NodeVirtualFileSystem & { + linkSync?: (existingPath: string, newPath: string) => void; + }; const markDirty = (entry: FileEntry, start: number, end: number): void => { entry.dirty = true; if (start < end) entry.dirtyRanges.push({ start, end }); @@ -224,6 +229,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO uid: typeof process.getuid === "function" ? process.getuid() : 0, gid: typeof process.getgid === "function" ? process.getgid() : 0, nlink: 1, + ino: 0, }; }; // Returns true on success, false if `needed` exceeds MAX_FILE_BYTES. @@ -693,7 +699,27 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb(vfs.existsSync(toVfs(path)) ? ERRNO.ENODATA : ERRNO.ENOENT); }, - link: notImplemented("link"), + link(source, destination, cb) { + try { + if (linkableVfs.linkSync === undefined) { + cb(ERRNO.ENOSYS); + return; + } + const flushErrno = flushEntry(source); + if (flushErrno !== 0) { + cb(flushErrno); + return; + } + linkableVfs.linkSync(toVfs(source), toVfs(destination)); + const entry = files.get(source); + if (entry !== undefined) files.set(destination, entry); + const override = meta.get(source); + if (override !== undefined) meta.set(destination, override); + cb(0); + } catch (error) { + cb(toErrno(error)); + } + }, symlink(target, path, cb) { try { // Symlink target text is stored verbatim — applications @@ -816,6 +842,8 @@ function statNode(stat: { ctime: Date; size: number; mode: number; + nlink?: number; + ino?: number; isDirectory(): boolean; }): FuseStat { return { @@ -826,7 +854,8 @@ function statNode(stat: { mode: stat.mode, uid: typeof process.getuid === "function" ? process.getuid() : 0, gid: typeof process.getgid === "function" ? process.getgid() : 0, - nlink: stat.isDirectory() ? 2 : 1, + nlink: stat.nlink ?? (stat.isDirectory() ? 2 : 1), + ino: stat.ino ?? 0, }; } @@ -839,5 +868,6 @@ function toErrno(error: unknown): number { if (code === "EISDIR") return ERRNO.EISDIR; if (code === "ENOTEMPTY") return ERRNO.ENOTEMPTY; if (code === "EINVAL") return ERRNO.EINVAL; + if (code === "EPERM") return ERRNO.EPERM; return ERRNO.EIO; } diff --git a/packages/wsd/src/fuse/options.test.ts b/packages/wsd/src/fuse/options.test.ts index 6ab97989..c148abf5 100644 --- a/packages/wsd/src/fuse/options.test.ts +++ b/packages/wsd/src/fuse/options.test.ts @@ -16,11 +16,12 @@ describe("buildFuseOptionString", () => { // tools that stat repeatedly (find, ls -l, git status) without // letting a stale view linger. negative_timeout at zero keeps // "file not found" answers fresh so a just-written file shows - // up immediately. big_writes plus 128 KiB max_read and - // max_write match the historical sizing that earlier - // experiments showed didn't move on bigger values. + // up immediately. use_ino lets hardlinks stat as the same inode. + // big_writes plus 128 KiB max_read and max_write match the + // historical sizing that earlier experiments showed didn't move + // on bigger values. expect(buildFuseOptionString(empty)).toBe( - "big_writes,max_write=131072,max_read=131072,auto_cache,attr_timeout=1,entry_timeout=1,negative_timeout=0,ac_attr_timeout=1", + "big_writes,use_ino,max_write=131072,max_read=131072,auto_cache,attr_timeout=1,entry_timeout=1,negative_timeout=0,ac_attr_timeout=1", ); }); diff --git a/packages/wsd/src/fuse/options.ts b/packages/wsd/src/fuse/options.ts index 37e3c395..46f3a9b3 100644 --- a/packages/wsd/src/fuse/options.ts +++ b/packages/wsd/src/fuse/options.ts @@ -15,7 +15,9 @@ // second so stat-heavy tools (find, ls -l, git status) skip repeated // FUSE round-trips. negative_timeout stays at zero so a just-written // file shows up immediately to a process that probed before it -// existed. big_writes plus 128 KiB max_read and max_write match the +// existed. use_ino tells the kernel to trust the inode numbers +// returned by getattr, which is required for hardlinks to stat as the +// same inode. big_writes plus 128 KiB max_read and max_write match the // historical sizing; experiments with larger values didn't move the // numbers. // @@ -57,7 +59,7 @@ export interface FuseOptionEnv { * object, so tests can drive it directly. */ export function buildFuseOptionString(env: FuseOptionEnv): string { - const opts: string[] = ["big_writes"]; + const opts: string[] = ["big_writes", "use_ino"]; const maxWrite = parsePositiveInt(env.WSD_FUSE_MAX_WRITE) ?? DEFAULT_MAX_WRITE; const maxRead = parsePositiveInt(env.WSD_FUSE_MAX_READ) ?? DEFAULT_MAX_READ; diff --git a/packages/wsd/src/fuse/vfs.ts b/packages/wsd/src/fuse/vfs.ts index b1da9291..3efed6b1 100644 --- a/packages/wsd/src/fuse/vfs.ts +++ b/packages/wsd/src/fuse/vfs.ts @@ -66,6 +66,8 @@ const FORWARDED_METHODS = [ "unlinkSync", "rename", "renameSync", + "link", + "linkSync", "readFile", "readFileSync", "writeFile", @@ -159,7 +161,21 @@ export async function createNodeVirtualFileSystem( stopSync = startSyncLoop(db, options.upstream); } - const vfs = create(new SQLiteVirtualProvider(db), { moduleHooks: false }); + const provider = new SQLiteVirtualProvider(db); + const vfs = create(provider, { moduleHooks: false }); + // @platformatic/vfs does not expose hardlink helpers on + // VirtualFileSystem, but FUSE needs link(2). Attach the provider + // primitive directly so the driver can call it while all ordinary + // VFS callers keep using the standard surface. + Object.defineProperty(vfs, "linkSync", { + value: (existingPath: string, newPath: string) => + (provider as unknown as { linkSync(existingPath: string, newPath: string): void }).linkSync( + existingPath, + newPath, + ), + writable: true, + configurable: true, + }); return { vfs, db, stopSync }; } From 9cde9274ee6a1da131c203c8f9b356857ac2b60d Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 16:58:12 +0000 Subject: [PATCH 03/31] script: cover hardlink writes in fs tests Add an integration-test case for writing through one hardlink and reading the bytes through the other name. This pins the behavior npm relies on when package-manager installs link cached files into a workspace and later consumers access the original path. --- script/fs-tests.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/script/fs-tests.sh b/script/fs-tests.sh index 83b55d42..c8bb98b9 100755 --- a/script/fs-tests.sh +++ b/script/fs-tests.sh @@ -156,6 +156,11 @@ run "unlink one keeps other" -- ' echo content > a && ln a b rm a && [ "$(cat b)" = "content" ] ' +run "write through hard link" -- ' + echo old > a && ln a b + echo new > b + [ "$(cat a)" = "new" ] +' section "extended attributes" if command -v setfattr >/dev/null && command -v getfattr >/dev/null; then From d7189fc6142fa4fb8dc4bfb865c39aeab80fc4ca Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 19:10:14 +0000 Subject: [PATCH 04/31] dofs: inline tiny sync writes Add inline_data to vfs_nodes and use it for synchronous writes up to 16 KiB. The synchronous provider path is the FUSE hot path, so tiny files no longer need chunk rows, blob rows, or manifest rows before they can be read back locally. Keep the async writeFile path chunk-backed for now so the existing sync wire format continues to carry small files without a protocol change. Read, stat, provider readFileSync, and fd splice helpers now understand both inline and chunk-backed files. The migration is idempotent for partially-staged old schemas that get the latest baseline table before migrations run. --- packages/dofs/src/fs/mount-guard.test.ts | 2 +- packages/dofs/src/fs/readFile.test.ts | 8 +++-- packages/dofs/src/fs/readFile.ts | 18 ++++++++++ packages/dofs/src/fs/stat.ts | 12 +++++-- packages/dofs/src/fs/writeFile.test.ts | 29 ++++++++++++++- packages/dofs/src/fs/writeFile.ts | 45 +++++++++++++++++++----- packages/dofs/src/provider.ts | 34 +++++++++++++++--- packages/dofs/src/schema/core.ts | 11 +++--- packages/dofs/src/schema/migrations.ts | 13 +++++++ 9 files changed, 147 insertions(+), 25 deletions(-) diff --git a/packages/dofs/src/fs/mount-guard.test.ts b/packages/dofs/src/fs/mount-guard.test.ts index 03bfa575..51c819be 100644 --- a/packages/dofs/src/fs/mount-guard.test.ts +++ b/packages/dofs/src/fs/mount-guard.test.ts @@ -146,7 +146,7 @@ describe("writeFile under a read-only mount", () => { // No throw; the bytes land in vfs_nodes. writeFileSync(db, "/workspace/rw/ok.txt", new TextEncoder().encode("hi"), {}, () => 0); const inode = db.scalar( - "SELECT inode FROM vfs_nodes WHERE manifest_hash IS NOT NULL", + "SELECT inode FROM vfs_nodes WHERE inline_data IS NOT NULL OR manifest_hash IS NOT NULL", ); expect(inode).toBeDefined(); }); diff --git a/packages/dofs/src/fs/readFile.test.ts b/packages/dofs/src/fs/readFile.test.ts index 04ecfcab..d4c05f0c 100644 --- a/packages/dofs/src/fs/readFile.test.ts +++ b/packages/dofs/src/fs/readFile.test.ts @@ -84,11 +84,13 @@ describe("readFile", () => { it("touches vfs_blobs.last_seen when chunks are read", async () => { await withDB(async (db) => { - await writeFile(db, "/x.txt", "content", {}, () => 100); - const before = db.scalar("SELECT last_seen FROM vfs_blobs"); + const bytes = new Uint8Array(CHUNK_SIZE + 1); + bytes.fill(0x61); + await writeFile(db, "/x.txt", bytes, {}, () => 100); + const before = db.scalar("SELECT MIN(last_seen) FROM vfs_blobs"); expect(before).toBe(100); await readFile(db, "/x.txt", "utf8", () => 200); - const after = db.scalar("SELECT last_seen FROM vfs_blobs"); + const after = db.scalar("SELECT MIN(last_seen) FROM vfs_blobs"); expect(after).toBe(200); }); }); diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index 6ff11ed3..85036508 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -11,6 +11,10 @@ interface ChunkRow { size: number; } +interface InlineRow { + inline_data: Uint8Array | null; +} + // Overloads match docs/04_filesystem_interface.md exactly. export function readFile(db: Database, path: string): Promise>; export function readFile( @@ -45,6 +49,20 @@ export async function readFile( throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } + const inline = db.one( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data; + if (inline !== undefined && inline !== null) { + if (wantString) return new TextDecoder().decode(inline); + return new ReadableStream({ + start(controller) { + controller.enqueue(inline); + controller.close(); + }, + }); + } + const chunks = db.all( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, diff --git a/packages/dofs/src/fs/stat.ts b/packages/dofs/src/fs/stat.ts index b228c9be..62748957 100644 --- a/packages/dofs/src/fs/stat.ts +++ b/packages/dofs/src/fs/stat.ts @@ -21,11 +21,19 @@ export function stat(db: Database, path: string): WorkspaceStatResult { const isDirectory = node.type === "dir"; const isFile = node.type === "file"; + const inlineSize = isFile + ? db.one<{ size: number | null }>( + "SELECT length(inline_data) AS size FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.size + : undefined; const size = isFile - ? (db.scalar( + ? (inlineSize ?? + db.scalar( "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", node.inode, - ) ?? 0) + ) ?? + 0) : 0; return { diff --git a/packages/dofs/src/fs/writeFile.test.ts b/packages/dofs/src/fs/writeFile.test.ts index 5b56dc60..2b6b0c08 100644 --- a/packages/dofs/src/fs/writeFile.test.ts +++ b/packages/dofs/src/fs/writeFile.test.ts @@ -5,7 +5,7 @@ import type { Database } from "../storage.js"; import { mkdir } from "./mkdir.js"; import { resolveInode } from "./resolve.js"; import { withDB } from "./with-db.js"; -import { CHUNK_SIZE, writeFile, writeFileRangesSync } from "./writeFile.js"; +import { CHUNK_SIZE, writeFile, writeFileRangesSync, writeFileSync } from "./writeFile.js"; // Reassemble a file's bytes by stitching its chunk rows together. // A deliberately minimal helper so writeFile tests can stand alone @@ -14,6 +14,11 @@ function readBack(db: Database, path: string): Uint8Array { const node = resolveInode(db, path); if (node === null) throw new Error(`no such path: ${path}`); if (node.type !== "file") throw new Error(`not a file: ${path}`); + const inline = db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data; + if (inline !== undefined && inline !== null) return inline; const chunks = db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -81,6 +86,28 @@ describe("writeFile", () => { }); }); + it("writeFileSync stores a small string inline without chunk rows", async () => { + await withDB(async (db) => { + writeFileSync(db, "/hello.txt", new TextEncoder().encode("hello fuse"), {}, () => 1234); + + const bytes = readBack(db, "/hello.txt"); + expect(new TextDecoder().decode(bytes)).toBe("hello fuse"); + + const row = db.one<{ inline_data: Uint8Array | null; chunk_count: number }>( + `SELECT n.inline_data AS inline_data, + (SELECT COUNT(*) FROM vfs_chunks WHERE inode = n.inode) AS chunk_count + FROM vfs_nodes n + JOIN vfs_dirents d ON d.child_inode = n.inode + WHERE d.parent_inode = ? AND d.name = ?`, + ROOT_INODE, + "hello.txt", + ); + expect(row?.inline_data).toBeInstanceOf(Uint8Array); + expect(new TextDecoder().decode(row?.inline_data ?? new Uint8Array())).toBe("hello fuse"); + expect(row?.chunk_count).toBe(0); + }); + }); + it("accepts a Uint8Array", async () => { await withDB(async (db) => { const data = new Uint8Array([1, 2, 3, 4, 5]); diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index d393852c..b8d51c0c 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -11,6 +11,7 @@ import { assertNotReadOnly } from "./mount-guard.js"; // Fixed chunk size. Exported so tests can size inputs precisely // without hard-coding the magic number twice. export const CHUNK_SIZE = 512 * 1024; +export const INLINE_FILE_MAX_BYTES = 16 * 1024; export type WriteFileContent = string | Uint8Array | ReadableStream; @@ -110,7 +111,7 @@ export async function writeFile( return; } const bytes = await materialize(content); - writeFileSync(db, path, bytes, options, now); + writeFileSync(db, path, bytes, options, now, false); } // Streaming write path. Reads the source one source-chunk at a time, @@ -325,6 +326,7 @@ export function writeFileSync( bytes: Uint8Array, options: WriteFileOptions, now: () => number, + inlineAllowed = true, ): void { const { parts, path: canonical } = canonicalizePath(path); if (parts.length === 0) { @@ -332,8 +334,8 @@ export function writeFileSync( } assertNotReadOnly(db, canonical); const mode = (options.mode ?? 0o644) & 0o7777; - const chunks = chunksOf(bytes); const mtime = now(); + const inline = inlineAllowed && bytes.byteLength <= INLINE_FILE_MAX_BYTES; db.transactionSync(() => { const parentInode = resolveParent(db, parts, canonical); @@ -354,8 +356,8 @@ export function writeFileSync( throw createWorkspaceError("EISDIR", `path is a directory: ${canonical}`, canonical); } inode = existing.child_inode; - // Replace the chunk list. Orphaned blobs (if any) are cleaned up - // by a later gc() pass. + // Replace the existing representation. Orphaned blobs (if any) + // are cleaned up by a later gc() pass. db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); } else { db.run( @@ -376,6 +378,20 @@ export function writeFileSync( ); } + const rev = incrementRev(db); + if (inline) { + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", + mode, + mtime, + rev, + bytes, + inode, + ); + return; + } + + const chunks = chunksOf(bytes); // Upsert blobs and write the new chunk list. for (let idx = 0; idx < chunks.length; idx++) { const chunk = chunks[idx]; @@ -390,9 +406,8 @@ export function writeFileSync( } const manifestHash = buildManifest(db, chunks, mtime); - const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", mode, mtime, rev, @@ -418,6 +433,7 @@ export function writeFileRangesSync( const mode = (options.mode ?? 0o644) & 0o7777; const ranges = normalizeRanges(dirtyRanges, bytes.byteLength); const mtime = now(); + const inline = bytes.byteLength <= INLINE_FILE_MAX_BYTES; db.transactionSync(() => { const parentInode = resolveParent(db, parts, canonical); @@ -459,6 +475,20 @@ export function writeFileRangesSync( ); } + const rev = incrementRev(db); + if (inline) { + db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", + mode, + mtime, + rev, + bytes, + inode, + ); + return; + } + const nextChunks: ChunkRef[] = []; const chunkCount = Math.ceil(bytes.byteLength / CHUNK_SIZE); for (let idx = 0; idx < chunkCount; idx++) { @@ -480,9 +510,8 @@ export function writeFileRangesSync( } const manifestHash = replaceChunkRows(db, inode, nextChunks, mtime); - const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", mode, mtime, rev, diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index f84b2a54..9de6b393 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -188,10 +188,7 @@ export class SQLiteWorkspaceProvider { const size = isSymlink ? (node.linkTarget ?? "").length : node.type === "file" - ? (this.db.scalar( - "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", - node.inode, - ) ?? 0) + ? fileSize(this.db, node.inode) : 0; return wrapStats({ mode: node.mode, @@ -376,6 +373,16 @@ export class SQLiteWorkspaceProvider { if (node.type !== "file") { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } + const inline = this.db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data; + const encoding = typeof options === "string" ? options : options?.encoding; + if (inline !== undefined && inline !== null) { + const out = Buffer.from(inline); + return encoding ? out.toString(encoding) : out; + } + const chunks = this.db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -395,7 +402,6 @@ export class SQLiteWorkspaceProvider { out.set(row.bytes, offset); offset += row.bytes.byteLength; } - const encoding = typeof options === "string" ? options : options?.encoding; return encoding ? out.toString(encoding) : out; } @@ -704,6 +710,18 @@ function linkCount(db: Database, inode: number): number { return Math.max(1, count ?? 0); } +function fileSize(db: Database, inode: number): number { + const inlineSize = db.one<{ size: number | null }>( + "SELECT length(inline_data) AS size FROM vfs_nodes WHERE inode = ?", + inode, + )?.size; + return ( + inlineSize ?? + db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? + 0 + ); +} + function wrapStats(input: StatsInputs): VirtualStatsLike { const mtime = new Date(input.mtimeMs); return { @@ -881,6 +899,12 @@ function readFileBytesSync(db: Database, path: string): Uint8Array { if (node.type !== "file") { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } + const inline = db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data; + if (inline !== undefined && inline !== null) return inline; + const chunks = db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, diff --git a/packages/dofs/src/schema/core.ts b/packages/dofs/src/schema/core.ts index 26ed6622..2dc2d6d6 100644 --- a/packages/dofs/src/schema/core.ts +++ b/packages/dofs/src/schema/core.ts @@ -1,10 +1,10 @@ // Filesystem-side tables. These hold the inode graph and the // content-addressed blob store. See docs/03_filesystem_schema.md. -// Bumped to 2 when `_vfs_mounts.mode` landed (read-only mount -// enforcement at the data layer). See `schema/migrations.ts` for the -// migration list; `sync.ts` carries the fresh-install DDL. -export const SCHEMA_VERSION = 2; +// Bumped to 3 when `vfs_nodes.inline_data` landed for tiny files. +// See `schema/migrations.ts` for the migration list; `sync.ts` +// carries the fresh-install DDL. +export const SCHEMA_VERSION = 3; export const ROOT_INODE = 1; export const CORE_STATEMENTS = [ @@ -21,7 +21,8 @@ export const CORE_STATEMENTS = [ mount_root TEXT, stub_size INTEGER, manifest_hash BLOB, - link_target TEXT + link_target TEXT, + inline_data BLOB )`, `CREATE TABLE IF NOT EXISTS vfs_dirents ( parent_inode INTEGER NOT NULL, diff --git a/packages/dofs/src/schema/migrations.ts b/packages/dofs/src/schema/migrations.ts index 10d1f3d4..f6676371 100644 --- a/packages/dofs/src/schema/migrations.ts +++ b/packages/dofs/src/schema/migrations.ts @@ -40,8 +40,21 @@ function v1_to_v2_add_mounts_mode(db: Database): void { ); } +// v2 → v3 — add inline_data for tiny regular-file payloads. Existing +// files keep their chunk rows; only subsequent small writes use the +// inline path. +function v2_to_v3_add_inline_data(db: Database): void { + const hasColumn = db + .all<{ name: string }>("PRAGMA table_info(vfs_nodes)") + .some((column) => column.name === "inline_data"); + if (!hasColumn) { + db.run("ALTER TABLE vfs_nodes ADD COLUMN inline_data BLOB"); + } +} + export const MIGRATIONS: readonly Migration[] = [ { from: 1, to: 2, migrator: v1_to_v2_add_mounts_mode }, + { from: 2, to: 3, migrator: v2_to_v3_add_inline_data }, ] as const; // Apply every migration whose `from` matches the current version, From e1b9e3f2251cd0c80d95474009fb217793d19f6f Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 19:13:10 +0000 Subject: [PATCH 05/31] wsd: evict clean FUSE buffers on release Track open regular-file handles by path and drop clean FileEntry buffers after the final release. Once flush has persisted data into the backing VFS, keeping the per-file Buffer around only duplicates memory already owned by the store and can mask later VFS-side writes. The eviction also handles hardlink aliases that share the same FileEntry object: clean aliases with no open handles are removed alongside the released path. A regression test writes a file through FUSE, flushes and releases it, mutates the backing VFS directly, and then verifies the next FUSE read hydrates the fresh VFS bytes instead of serving the stale clean buffer. --- packages/wsd/src/fuse/driver.test.ts | 26 ++++++++++++++++++++++ packages/wsd/src/fuse/driver.ts | 32 ++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index e6b8743f..1a30aa54 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -511,6 +511,32 @@ test("FUSE ops reject a relative mountPoint", async () => { expect(() => makeFUSEOps(vfs, "workspace")).toThrow(/absolute/); }); +test("FUSE release evicts clean file buffers after flush", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + const ops = makeFUSEOps(vfs); + + const create = await callback((cb: (errno: number, result: unknown) => void) => + ops.create("/evict.txt", 0o644, cb), + ); + expect(create.errno).toBe(0); + const fh = create.result as number; + const first = Buffer.from("first"); + expect(await status((cb) => ops.write("/evict.txt", fh, first, first.byteLength, 0, cb))).toBe( + first.byteLength, + ); + expect(await status((cb) => ops.flush("/evict.txt", fh, cb))).toBe(0); + expect(await status((cb) => ops.release("/evict.txt", fh, cb))).toBe(0); + + vfs.writeFileSync("/evict.txt", Buffer.from("second")); + const open = await callback((cb) => ops.open("/evict.txt", 0, cb)); + expect(open.errno).toBe(0); + const out = Buffer.alloc("second".length); + expect( + await status((cb) => ops.read("/evict.txt", open.result as number, out, out.byteLength, 0, cb)), + ).toBe(out.byteLength); + expect(out.toString()).toBe("second"); +}); + test("FUSE getattr reflects mtime and size after an external VFS write", async () => { // The auto_cache FUSE option asks the kernel to invalidate its // page cache for a file when the file's mtime or size changes diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 0418ba54..11e37e6d 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -124,6 +124,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO }; const handles = new Map(); + const fileOpenCounts = new Map(); let nextHandle = 1; const openHandle = (path: string): number => { @@ -132,6 +133,17 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO return handle; }; + const openFileHandle = (path: string): number => { + fileOpenCounts.set(path, (fileOpenCounts.get(path) ?? 0) + 1); + return openHandle(path); + }; + + const releaseFileHandle = (path: string): void => { + const next = (fileOpenCounts.get(path) ?? 1) - 1; + if (next <= 0) fileOpenCounts.delete(path); + else fileOpenCounts.set(path, next); + }; + // Sidecar metadata. platformatic VFS has no chmod/chown/utimes, so we store // overrides here and merge them into getattr. interface MetaOverride { @@ -350,7 +362,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO try { const entry = files.get(path); if (entry?.pendingCreate === true) { - cb(0, openHandle(path)); + cb(0, openFileHandle(path)); return; } const stat = vfs.statSync(toVfs(path)); @@ -359,7 +371,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO return; } - cb(0, openHandle(path)); + cb(0, openFileHandle(path)); } catch (error) { cb(toErrno(error), 0); } @@ -402,7 +414,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO mode, pendingMtime: new Date(), }); - cb(0, openHandle(path)); + cb(0, openFileHandle(path)); } catch (error) { cb(toErrno(error), 0); } @@ -477,12 +489,24 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO release(path, fh, cb) { handles.delete(fh); + releaseFileHandle(path); // Last chance to make the buffered writes durable in the // VFS — the kernel won't call write() again on this fh. // Multi-open is fine: the next release on a different fh // pointing at the same buffer re-spills the same bytes, // which writeFileSync handles idempotently. - cb(flushEntry(path)); + const errno = flushEntry(path); + if (errno === 0 && (fileOpenCounts.get(path) ?? 0) === 0) { + const entry = files.get(path); + if (entry !== undefined && !entry.dirty) { + for (const [candidatePath, candidateEntry] of files) { + if (candidateEntry === entry && (fileOpenCounts.get(candidatePath) ?? 0) === 0) { + files.delete(candidatePath); + } + } + } + } + cb(errno); }, releasedir(_path, fh, cb) { From a3a2471c14043a2f051ef16b1045057d20fbf31e Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 19:15:33 +0000 Subject: [PATCH 06/31] wsd: report FUSE buffer memory stats Expose a driver-level getBufferStats snapshot for the FUSE write buffer cache. The stats include resident entry count, dirty and pending-create counts, logical and capacity bytes, dirty logical bytes, and open handle/path counts. The snapshot is intentionally stripped before handing the operation object to fuse-native, so it is available to tests and future daemon diagnostics without registering a non-FUSE callback with libfuse. Tests cover stats rising while a write buffer is resident and dropping back to zero after the clean-release eviction path. --- packages/wsd/src/fuse/driver.test.ts | 41 +++++++++++++++++++++++ packages/wsd/src/fuse/driver.ts | 49 ++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index 1a30aa54..500bec94 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -511,6 +511,47 @@ test("FUSE ops reject a relative mountPoint", async () => { expect(() => makeFUSEOps(vfs, "workspace")).toThrow(/absolute/); }); +test("FUSE buffer stats report resident write buffers", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + const ops = makeFUSEOps(vfs); + + expect(ops.getBufferStats()).toMatchObject({ + entries: 0, + dirtyEntries: 0, + capacityBytes: 0, + logicalBytes: 0, + }); + + const create = await callback((cb: (errno: number, result: unknown) => void) => + ops.create("/stats.txt", 0o644, cb), + ); + expect(create.errno).toBe(0); + const fh = create.result as number; + const payload = Buffer.from("tracked"); + expect( + await status((cb) => ops.write("/stats.txt", fh, payload, payload.byteLength, 0, cb)), + ).toBe(payload.byteLength); + + expect(ops.getBufferStats()).toMatchObject({ + entries: 1, + dirtyEntries: 1, + pendingCreates: 1, + logicalBytes: payload.byteLength, + dirtyLogicalBytes: payload.byteLength, + openPaths: 1, + }); + expect(ops.getBufferStats().capacityBytes).toBeGreaterThanOrEqual(payload.byteLength); + + expect(await status((cb) => ops.release("/stats.txt", fh, cb))).toBe(0); + expect(ops.getBufferStats()).toMatchObject({ + entries: 0, + dirtyEntries: 0, + capacityBytes: 0, + logicalBytes: 0, + openPaths: 0, + }); +}); + test("FUSE release evicts clean file buffers after flush", async () => { const { vfs } = await createNodeVirtualFileSystem(); const ops = makeFUSEOps(vfs); diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 11e37e6d..6809eb99 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -84,6 +84,7 @@ export interface FuseOps { removexattr(path: string, name: string, cb: StatusCallback): void; link(source: string, destination: string, cb: StatusCallback): void; symlink(target: string, path: string, cb: StatusCallback): void; + getBufferStats(): FuseBufferStats; } export interface FuseStat { @@ -98,6 +99,17 @@ export interface FuseStat { ino: number; } +export interface FuseBufferStats { + entries: number; + dirtyEntries: number; + pendingCreates: number; + capacityBytes: number; + logicalBytes: number; + dirtyLogicalBytes: number; + openHandles: number; + openPaths: number; +} + export interface FuseMount { unmount(): Promise; } @@ -224,6 +236,31 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO const baseName = (path: string): string => posix.basename(path); const isPendingCreate = (path: string): boolean => files.get(path)?.pendingCreate === true; const exists = (path: string): boolean => isPendingCreate(path) || vfs.existsSync(toVfs(path)); + const getBufferStats = (): FuseBufferStats => { + let dirtyEntries = 0; + let pendingCreates = 0; + let capacityBytes = 0; + let logicalBytes = 0; + let dirtyLogicalBytes = 0; + for (const entry of files.values()) { + if (entry.dirty) dirtyEntries++; + if (entry.pendingCreate) pendingCreates++; + capacityBytes += entry.buf.byteLength; + logicalBytes += entry.size; + if (entry.dirty) dirtyLogicalBytes += entry.size; + } + return { + entries: files.size, + dirtyEntries, + pendingCreates, + capacityBytes, + logicalBytes, + dirtyLogicalBytes, + openHandles: handles.size, + openPaths: fileOpenCounts.size, + }; + }; + const pendingStat = (entry: FileEntry): FuseStat => { return { // Frozen from create() so consecutive stats of an unchanged @@ -316,6 +353,8 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb?.(0); }, + getBufferStats, + error: notImplemented("error"), readdir(path, cb) { @@ -783,13 +822,11 @@ export async function mountFuse(options: { const traceMode = process.env.WSD_FUSE_TRACE; const tracer: FuseTracer | undefined = traceMode === "summary" ? createFuseTracer() : undefined; const baseOps = makeFUSEOps(options.vfs, options.mountPoint); - const ops: FuseOps = + const { getBufferStats: _getBufferStats, ...fuseOps } = baseOps; + const ops = tracer === undefined - ? baseOps - : (wrapFuseOpsWithTracer( - baseOps as unknown as Record, - tracer, - ) as unknown as FuseOps); + ? fuseOps + : wrapFuseOpsWithTracer(fuseOps as unknown as Record, tracer); const fuse = new Fuse(options.mountPoint, ops, { autoUnmount: true, debug: false, From 3ad116e0d25f8a701db1b2c2f79547e5c8e0f3d5 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:18:38 +0000 Subject: [PATCH 07/31] dofs: add direct range write primitives Add local direct-write helpers for create, byte-range writes, and truncate. The helpers update inline data or affected chunk rows by inode, so hardlinks share the mutation and unchanged chunks can keep their existing hashes. DOFS can now update its source of truth incrementally without requiring a FUSE-owned whole-file buffer. Tests cover inline writes, sparse zero-fill, partial chunk updates, hardlink write-through, and chunk-backed truncate. --- packages/dofs/src/fs/writeFile.ts | 258 ++++++++++++++++++++++++ packages/dofs/src/fs/writeRange.test.ts | 156 ++++++++++++++ 2 files changed, 414 insertions(+) create mode 100644 packages/dofs/src/fs/writeRange.test.ts diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index b8d51c0c..6b13cb1a 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -317,6 +317,264 @@ function existingChunkRefs(db: Database, inode: number): ChunkRef[] { return db.all("SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", inode); } +function inlineDataForInode(db: Database, inode: number): Uint8Array | null { + return ( + db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + inode, + )?.inline_data ?? null + ); +} + +function fileSizeForInode(db: Database, inode: number): number { + const inline = inlineDataForInode(db, inode); + if (inline !== null) return inline.byteLength; + return ( + db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 + ); +} + +function readChunkBytes(db: Database, inode: number, idx: number): Uint8Array { + const inline = inlineDataForInode(db, inode); + if (inline !== null) { + const start = idx * CHUNK_SIZE; + return inline.subarray(start, Math.min(start + CHUNK_SIZE, inline.byteLength)); + } + const chunk = db.one<{ hash: Uint8Array }>( + "SELECT hash FROM vfs_chunks WHERE inode = ? AND idx = ?", + inode, + idx, + ); + if (chunk === undefined) return new Uint8Array(); + const row = db.one<{ bytes: Uint8Array }>( + "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", + chunk.hash, + ); + if (row === undefined) { + throw createWorkspaceError("EIO", "missing blob bytes"); + } + return row.bytes; +} + +function materializePrefix(db: Database, inode: number, size: number): Uint8Array { + const out = new Uint8Array(size); + let copied = 0; + for (let idx = 0; copied < size; idx++) { + const chunk = readChunkBytes(db, inode, idx); + if (chunk.byteLength > 0) { + out.set(chunk.subarray(0, Math.min(chunk.byteLength, size - copied)), copied); + } + copied += Math.min(CHUNK_SIZE, size - copied); + } + return out; +} + +function resolveFileInode(db: Database, path: string): { inode: number; mode: number } { + const { path: canonical } = canonicalizePath(path); + const node = db.one<{ inode: number; type: "file" | "dir"; mode: number }>( + `SELECT n.inode AS inode, n.type AS type, n.mode AS mode + FROM vfs_nodes n + WHERE n.inode = ( + SELECT child_inode + FROM vfs_dirents + WHERE parent_inode = ? AND name = ? + )`, + ...parentAndNameForResolvedPath(db, path), + ); + if (node === undefined) { + throw createWorkspaceError("ENOENT", `no such file: ${canonical}`, canonical); + } + if (node.type !== "file") { + throw createWorkspaceError("EISDIR", `path is a directory: ${canonical}`, canonical); + } + return { inode: node.inode, mode: node.mode }; +} + +function parentAndNameForResolvedPath(db: Database, path: string): [number, string] { + const { parts, path: canonical } = canonicalizePath(path); + if (parts.length === 0) { + throw createWorkspaceError("EISDIR", "cannot write to the root directory", canonical); + } + return [resolveParent(db, parts, canonical), parts[parts.length - 1]]; +} + +function writeInlineInode( + db: Database, + inode: number, + bytes: Uint8Array, + mode: number, + mtime: number, +): void { + db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); + const rev = incrementRev(db); + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", + mode, + mtime, + rev, + bytes, + inode, + ); +} + +function writeChunkedInode( + db: Database, + inode: number, + size: number, + mode: number, + mtime: number, + buildChunk: (idx: number, start: number, end: number, oldChunk?: ChunkRef) => ChunkRef, +): void { + const oldChunks = existingChunkRefs(db, inode); + const nextChunks: ChunkRef[] = []; + const chunkCount = Math.ceil(size / CHUNK_SIZE); + for (let idx = 0; idx < chunkCount; idx++) { + const start = idx * CHUNK_SIZE; + const end = Math.min(start + CHUNK_SIZE, size); + nextChunks.push(buildChunk(idx, start, end, oldChunks[idx])); + } + const manifestHash = replaceChunkRows(db, inode, nextChunks, mtime); + const rev = incrementRev(db); + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", + mode, + mtime, + rev, + manifestHash, + inode, + ); +} + +export function createFileSync( + db: Database, + path: string, + options: WriteFileOptions, + now: () => number, +): void { + const { path: canonical } = canonicalizePath(path); + assertNotReadOnly(db, canonical); + const [parentInode, leafName] = parentAndNameForResolvedPath(db, path); + const mode = (options.mode ?? 0o644) & 0o7777; + const mtime = now(); + + db.transactionSync(() => { + const existing = db.one<{ child_inode: number }>( + "SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + parentInode, + leafName, + ); + if (existing !== undefined) { + throw createWorkspaceError("EEXIST", `path exists: ${canonical}`, canonical); + } + db.run( + "INSERT INTO vfs_nodes (type, mode, mtime, rev, manifest_hash, inline_data) VALUES ('file', ?, ?, 0, NULL, ?)", + mode, + mtime, + new Uint8Array(), + ); + const inode = db.scalar("SELECT last_insert_rowid()"); + if (inode === undefined) throw createWorkspaceError("EIO", "failed to allocate inode"); + db.run( + "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", + parentInode, + leafName, + inode, + ); + const rev = incrementRev(db); + db.run("UPDATE vfs_nodes SET rev = ? WHERE inode = ?", rev, inode); + }); +} + +export function writeRangeSync( + db: Database, + path: string, + bytes: Uint8Array, + offset: number, + options: WriteFileOptions, + now: () => number, +): number { + const { path: canonical } = canonicalizePath(path); + assertNotReadOnly(db, canonical); + if (!Number.isInteger(offset) || offset < 0) { + throw createWorkspaceError("EINVAL", `invalid write offset: ${offset}`, canonical); + } + if (bytes.byteLength === 0) return 0; + const mtime = now(); + + db.transactionSync(() => { + const { inode, mode: existingMode } = resolveFileInode(db, path); + const mode = (options.mode ?? existingMode) & 0o7777; + const oldSize = fileSizeForInode(db, inode); + const writeEnd = offset + bytes.byteLength; + const nextSize = Math.max(oldSize, writeEnd); + + if (nextSize <= INLINE_FILE_MAX_BYTES) { + const next = materializePrefix(db, inode, nextSize); + next.set(bytes, offset); + writeInlineInode(db, inode, next, mode, mtime); + return; + } + + writeChunkedInode(db, inode, nextSize, mode, mtime, (idx, start, end, oldChunk) => { + const overlapsWrite = offset < end && start < writeEnd; + if (oldChunk !== undefined && oldChunk.size === end - start && !overlapsWrite) { + return oldChunk; + } + const chunkBytes = new Uint8Array(end - start); + const existing = readChunkBytes(db, inode, idx); + chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); + if (overlapsWrite) { + const copyStart = Math.max(start, offset); + const copyEnd = Math.min(end, writeEnd); + chunkBytes.set(bytes.subarray(copyStart - offset, copyEnd - offset), copyStart - start); + } + const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; + upsertChunkBlob(db, chunk, mtime); + return { hash: chunk.hash, size: chunk.size }; + }); + }); + + return bytes.byteLength; +} + +export function truncateFileSync( + db: Database, + path: string, + size: number, + now: () => number, +): void { + const { path: canonical } = canonicalizePath(path); + assertNotReadOnly(db, canonical); + if (!Number.isInteger(size) || size < 0) { + throw createWorkspaceError("EINVAL", `invalid truncate size: ${size}`, canonical); + } + const mtime = now(); + + db.transactionSync(() => { + const { inode, mode } = resolveFileInode(db, path); + const oldSize = fileSizeForInode(db, inode); + if (oldSize === size) return; + + if (size <= INLINE_FILE_MAX_BYTES) { + const next = materializePrefix(db, inode, size); + writeInlineInode(db, inode, next, mode, mtime); + return; + } + + writeChunkedInode(db, inode, size, mode, mtime, (idx, start, end, oldChunk) => { + if (oldChunk !== undefined && oldChunk.size === end - start) { + return oldChunk; + } + const chunkBytes = new Uint8Array(end - start); + const existing = readChunkBytes(db, inode, idx); + chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); + const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; + upsertChunkBlob(db, chunk, mtime); + return { hash: chunk.hash, size: chunk.size }; + }); + }); +} + // Synchronous entry point used by the VirtualProvider. Identical SQL // to the async path; differs only in that the bytes have already been // materialized. diff --git a/packages/dofs/src/fs/writeRange.test.ts b/packages/dofs/src/fs/writeRange.test.ts new file mode 100644 index 00000000..d79f2a87 --- /dev/null +++ b/packages/dofs/src/fs/writeRange.test.ts @@ -0,0 +1,156 @@ +import { describe, expect, it } from "vitest"; + +import type { Database } from "../storage.js"; +import { link } from "./link.js"; +import { readFile } from "./readFile.js"; +import { resolveInode } from "./resolve.js"; +import { withDB } from "./with-db.js"; +import { + CHUNK_SIZE, + createFileSync, + truncateFileSync, + writeFileSync, + writeRangeSync, +} from "./writeFile.js"; + +function bytesOf(text: string): Uint8Array { + return new TextEncoder().encode(text); +} + +async function readBytes(db: Database, path: string): Promise { + const stream = await readFile(db, path); + const reader = stream.getReader(); + const chunks: Uint8Array[] = []; + let total = 0; + while (true) { + const { value, done } = await reader.read(); + if (done) break; + if (value === undefined) continue; + chunks.push(value); + total += value.byteLength; + } + const out = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + out.set(chunk, offset); + offset += chunk.byteLength; + } + return out; +} + +function chunkRows( + db: Database, + path: string, +): Array<{ idx: number; hash: Uint8Array; size: number }> { + const node = resolveInode(db, path); + if (node === null) throw new Error(`missing node: ${path}`); + return db.all<{ idx: number; hash: Uint8Array; size: number }>( + "SELECT idx, hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", + node.inode, + ); +} + +function inlineData(db: Database, path: string): Uint8Array | null { + const node = resolveInode(db, path); + if (node === null) throw new Error(`missing node: ${path}`); + return ( + db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data ?? null + ); +} + +describe("direct range writes", () => { + it("creates an empty inline file", async () => { + await withDB(async (db) => { + createFileSync(db, "/empty.txt", { mode: 0o600 }, () => 1000); + + const node = resolveInode(db, "/empty.txt"); + expect(node?.type).toBe("file"); + expect(node?.mode).toBe(0o600); + expect(inlineData(db, "/empty.txt")).toEqual(new Uint8Array()); + expect(chunkRows(db, "/empty.txt")).toEqual([]); + }); + }); + + it("writes small ranges into inline_data", async () => { + await withDB(async (db) => { + createFileSync(db, "/small.txt", {}, () => 1000); + + expect(writeRangeSync(db, "/small.txt", bytesOf("hello"), 0, {}, () => 1001)).toBe(5); + expect(writeRangeSync(db, "/small.txt", bytesOf("y"), 4, {}, () => 1002)).toBe(1); + + expect(new TextDecoder().decode(await readBytes(db, "/small.txt"))).toBe("helly"); + expect(new TextDecoder().decode(inlineData(db, "/small.txt") ?? new Uint8Array())).toBe( + "helly", + ); + expect(chunkRows(db, "/small.txt")).toEqual([]); + }); + }); + + it("zero-fills sparse inline writes", async () => { + await withDB(async (db) => { + createFileSync(db, "/sparse.txt", {}, () => 1000); + + writeRangeSync(db, "/sparse.txt", bytesOf("x"), 3, {}, () => 1001); + + expect(Array.from(await readBytes(db, "/sparse.txt"))).toEqual([0, 0, 0, 120]); + }); + }); + + it("updates only affected chunk hashes for chunk-backed files", async () => { + await withDB(async (db) => { + const original = new Uint8Array(CHUNK_SIZE * 3); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + original.fill(3, CHUNK_SIZE * 2, CHUNK_SIZE * 3); + writeFileSync(db, "/large.bin", original, {}, () => 1000); + const before = chunkRows(db, "/large.bin"); + + writeRangeSync(db, "/large.bin", new Uint8Array([9, 9, 9]), CHUNK_SIZE + 10, {}, () => 1001); + const after = chunkRows(db, "/large.bin"); + + expect(after).toHaveLength(3); + expect(Buffer.from(after[0].hash).equals(Buffer.from(before[0].hash))).toBe(true); + expect(Buffer.from(after[1].hash).equals(Buffer.from(before[1].hash))).toBe(false); + expect(Buffer.from(after[2].hash).equals(Buffer.from(before[2].hash))).toBe(true); + const bytes = await readBytes(db, "/large.bin"); + expect(bytes[CHUNK_SIZE + 9]).toBe(2); + expect(Array.from(bytes.subarray(CHUNK_SIZE + 10, CHUNK_SIZE + 13))).toEqual([9, 9, 9]); + expect(bytes[CHUNK_SIZE + 13]).toBe(2); + }); + }); + + it("writes through hardlinks by shared inode", async () => { + await withDB(async (db) => { + createFileSync(db, "/a.txt", {}, () => 1000); + link(db, "/a.txt", "/b.txt"); + + writeRangeSync(db, "/b.txt", bytesOf("shared"), 0, {}, () => 1001); + + expect(new TextDecoder().decode(await readBytes(db, "/a.txt"))).toBe("shared"); + expect(resolveInode(db, "/a.txt")?.inode).toBe(resolveInode(db, "/b.txt")?.inode); + }); + }); + + it("truncates chunk-backed files without rewriting untouched chunks", async () => { + await withDB(async (db) => { + const original = new Uint8Array(CHUNK_SIZE * 2 + 100); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + original.fill(3, CHUNK_SIZE * 2); + writeFileSync(db, "/truncate.bin", original, {}, () => 1000); + const before = chunkRows(db, "/truncate.bin"); + + truncateFileSync(db, "/truncate.bin", CHUNK_SIZE + 50, () => 1001); + const after = chunkRows(db, "/truncate.bin"); + + expect(after).toHaveLength(2); + expect(after[1].size).toBe(50); + expect(Buffer.from(after[0].hash).equals(Buffer.from(before[0].hash))).toBe(true); + expect(Buffer.from(after[1].hash).equals(Buffer.from(before[1].hash))).toBe(false); + expect((await readBytes(db, "/truncate.bin")).byteLength).toBe(CHUNK_SIZE + 50); + }); + }); +}); From 55b64332f9071befa2792d71b7de3b157e805a85 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:20:10 +0000 Subject: [PATCH 08/31] dofs: use direct writes for fd mutations Route provider writeSync and truncateSync through the direct range and truncate primitives instead of materializing and rewriting the whole file. This keeps positional writes on the direct-write path for local provider callers and preserves untouched chunk hashes for large files. The provider fd tests now assert that a write into the middle chunk of a chunk-backed file reuses the surrounding chunk rows. --- packages/dofs/src/provider.fd.test.ts | 32 +++++++++++++++++++ packages/dofs/src/provider.ts | 44 ++++++--------------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/packages/dofs/src/provider.fd.test.ts b/packages/dofs/src/provider.fd.test.ts index 26763c75..7498bee9 100644 --- a/packages/dofs/src/provider.fd.test.ts +++ b/packages/dofs/src/provider.fd.test.ts @@ -4,6 +4,7 @@ import { describe, expect, it } from "vitest"; +import { resolveInode } from "./fs/resolve.js"; import { withDB } from "./fs/with-db.js"; import { SQLiteWorkspaceProvider } from "./provider.js"; @@ -15,6 +16,17 @@ async function withProvider(fn: (p: SQLiteWorkspaceProvider) => T | Promise( + "SELECT hash FROM vfs_chunks WHERE inode = ? ORDER BY idx", + node.inode, + ) + .map((row) => Buffer.from(row.hash)); +} + describe("SQLiteWorkspaceProvider — file descriptors", () => { it("openSync allocates a positive integer", async () => { await withProvider((p) => { @@ -238,6 +250,26 @@ describe("SQLiteWorkspaceProvider — writeSync", () => { }); }); + it("reuses untouched chunk rows for positional writes", async () => { + await withProvider((p) => { + const before = new Uint8Array(CHUNK_SIZE * 3); + before.fill(1, 0, CHUNK_SIZE); + before.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + before.fill(3, CHUNK_SIZE * 2); + p.writeFileSync("/big", Buffer.from(before)); + const oldHashes = chunkHashes(p, "/big"); + + const fd = p.openSync("/big", "r+") as number; + p.writeSync(fd, Buffer.from([9, 9, 9]), 0, 3, CHUNK_SIZE + 10); + p.closeSync(fd); + const newHashes = chunkHashes(p, "/big"); + + expect(newHashes[0].equals(oldHashes[0])).toBe(true); + expect(newHashes[1].equals(oldHashes[1])).toBe(false); + expect(newHashes[2].equals(oldHashes[2])).toBe(true); + }); + }); + it("openSync('a') starts the fd at EOF", async () => { await withProvider((p) => { p.writeFileSync("/a", "hello"); diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 9de6b393..db5023f7 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -25,9 +25,11 @@ import { type WatchOptions, } from "./fs/watch.js"; import { + truncateFileSync as truncateFileSyncImpl, type WriteFileRange, writeFileRangesSync as writeFileRangesSyncImpl, writeFileSync as writeFileSyncImpl, + writeRangeSync as writeRangeSyncImpl, } from "./fs/writeFile.js"; import { canonicalizePath } from "./path.js"; import type { Database } from "./storage.js"; @@ -555,10 +557,13 @@ export class SQLiteWorkspaceProvider { if (!state.writable) { throw createWorkspaceError("EBADF", `fd ${fd} is not writable`); } - const existing = readFileBytesSync(this.db, state.path); - const startAt = state.append ? existing.byteLength : (position ?? state.position); - const next = spliceBytes(existing, startAt, buffer, offset, length); - writeFileSyncImpl(this.db, state.path, next, {}, this.now); + const stat = this.statSync(state.path); + const startAt = state.append ? stat.size : (position ?? state.position); + const view = + buffer instanceof Buffer + ? new Uint8Array(buffer.buffer, buffer.byteOffset + offset, length) + : new Uint8Array(buffer.buffer, buffer.byteOffset + offset, length); + writeRangeSyncImpl(this.db, state.path, view, startAt, {}, this.now); if (position === null || position === undefined) { state.position = startAt + length; } @@ -578,18 +583,7 @@ export class SQLiteWorkspaceProvider { if (node.type !== "file") { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } - const existing = readFileBytesSync(this.db, path); - if (existing.byteLength === len) { - return; - } - let next: Uint8Array; - if (len < existing.byteLength) { - next = existing.subarray(0, len); - } else { - next = new Uint8Array(len); - next.set(existing, 0); - } - writeFileSyncImpl(this.db, path, next, {}, this.now); + truncateFileSyncImpl(this.db, path, len, this.now); } ftruncateSync(fd: number, len: number): void { @@ -926,21 +920,3 @@ function readFileBytesSync(db: Database, path: string): Uint8Array { } return out; } - -// Splice `length` bytes from `src[srcOffset..]` into a copy of `dst` -// at `at`. The result is at least as long as max(dst.length, at + length). -// Bytes in `[dst.length, at)` are zero-filled (writing past EOF). -function spliceBytes( - dst: Uint8Array, - at: number, - src: Uint8Array | Buffer, - srcOffset: number, - length: number, -): Uint8Array { - const newLength = Math.max(dst.byteLength, at + length); - const out = new Uint8Array(newLength); - out.set(dst, 0); - const srcView = src.subarray(srcOffset, srcOffset + length); - out.set(srcView, at); - return out; -} From ee45474395473a627461440c059c22395548c8da Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:21:05 +0000 Subject: [PATCH 09/31] dofs: expose direct write provider methods Expose createFileSync, writeRangeSync, and truncateFileSync on the SQLite workspace provider so local callers can use the direct incremental write path without going through file descriptors. The provider tests cover direct create, range overwrite, readback, and truncate through the new surface. --- packages/dofs/src/provider.fd.test.ts | 16 ++++++++++++++++ packages/dofs/src/provider.ts | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/packages/dofs/src/provider.fd.test.ts b/packages/dofs/src/provider.fd.test.ts index 7498bee9..e7b0f5b8 100644 --- a/packages/dofs/src/provider.fd.test.ts +++ b/packages/dofs/src/provider.fd.test.ts @@ -165,6 +165,22 @@ describe("SQLiteWorkspaceProvider — readSync", () => { }); }); +describe("SQLiteWorkspaceProvider — direct range methods", () => { + it("exposes direct create, write range, and truncate methods", async () => { + await withProvider((p) => { + p.createFileSync("/direct.txt", { mode: 0o600 }); + expect(p.statSync("/direct.txt").mode & 0o777).toBe(0o600); + + expect(p.writeRangeSync("/direct.txt", Buffer.from("abcdef"), 0)).toBe(6); + expect(p.writeRangeSync("/direct.txt", Buffer.from("Z"), 3)).toBe(1); + expect(p.readFileSync("/direct.txt", "utf8")).toBe("abcZef"); + + p.truncateFileSync("/direct.txt", 4); + expect(p.readFileSync("/direct.txt", "utf8")).toBe("abcZ"); + }); + }); +}); + describe("SQLiteWorkspaceProvider — writeSync", () => { it("writes at position 0 and updates content", async () => { await withProvider((p) => { diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index db5023f7..d67dfacb 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -25,6 +25,7 @@ import { type WatchOptions, } from "./fs/watch.js"; import { + createFileSync as createFileSyncImpl, truncateFileSync as truncateFileSyncImpl, type WriteFileRange, writeFileRangesSync as writeFileRangesSyncImpl, @@ -443,6 +444,28 @@ export class SQLiteWorkspaceProvider { writeFileRangesSyncImpl(this.db, path, bytes, ranges, { mode }, this.now); } + createFileSync(path: string, options?: { mode?: number }): void { + createFileSyncImpl(this.db, path, { mode: options?.mode }, this.now); + } + + writeRangeSync( + path: string, + data: string | Buffer | Uint8Array, + offset: number, + options?: { encoding?: BufferEncoding; mode?: number } | BufferEncoding, + ): number { + const mode = typeof options === "string" ? undefined : options?.mode; + const bytes = + typeof data === "string" + ? new TextEncoder().encode(data) + : new Uint8Array(data.buffer, data.byteOffset, data.byteLength); + return writeRangeSyncImpl(this.db, path, bytes, offset, { mode }, this.now); + } + + truncateFileSync(path: string, len: number): void { + truncateFileSyncImpl(this.db, path, len, this.now); + } + appendFile( _path: string, _data: string | Buffer, From 7cc81d15fcbff54eff93ad3f99f921aadef15b78 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:27:40 +0000 Subject: [PATCH 10/31] wsd: write FUSE data directly into DOFS Expose the DOFS direct-write methods through the wsd VFS wrapper and use them from the FUSE driver when available. Direct-mode create, write, truncate, and chmod update the backing provider immediately, so reads through the VFS see FUSE-written bytes before release and normal writes no longer allocate FileEntry buffers. Keep the previous staged-buffer path as a fallback for providers that do not expose the direct-write methods. Fallback-specific tests disable direct writes to keep covering flush, ranged spill, and buffer stats behavior. --- packages/dofs/src/provider.ts | 15 +++++++ packages/wsd/src/fuse/driver.test.ts | 50 ++++++++++++++++++----- packages/wsd/src/fuse/driver.ts | 59 ++++++++++++++++++++++++++++ packages/wsd/src/fuse/vfs.ts | 44 +++++++++++++++++++++ 4 files changed, 159 insertions(+), 9 deletions(-) diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index d67dfacb..5ec51c03 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -33,6 +33,7 @@ import { writeRangeSync as writeRangeSyncImpl, } from "./fs/writeFile.js"; import { canonicalizePath } from "./path.js"; +import { incrementRev } from "./rev.js"; import type { Database } from "./storage.js"; export interface SQLiteWorkspaceProviderOptions { @@ -466,6 +467,20 @@ export class SQLiteWorkspaceProvider { truncateFileSyncImpl(this.db, path, len, this.now); } + chmodSync(path: string, mode: number): void { + const node = resolveInode(this.db, path, { followSymlinks: false }); + if (node === null) { + throw createWorkspaceError("ENOENT", `no such path: ${path}`, path); + } + const rev = incrementRev(this.db); + this.db.run( + "UPDATE vfs_nodes SET mode = ?, rev = ? WHERE inode = ?", + mode & 0o7777, + rev, + node.inode, + ); + } + appendFile( _path: string, _data: string | Buffer, diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index 500bec94..a6a25980 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -48,6 +48,17 @@ const fuseNativeOperationNames = [ const notImplementedOperationNames = ["error", "mknod"]; +function disableDirectWrites(vfs: unknown): void { + const target = vfs as { + createFileSync?: unknown; + writeRangeSync?: unknown; + truncateFileSync?: unknown; + }; + delete target.createFileSync; + delete target.writeRangeSync; + delete target.truncateFileSync; +} + test("FUSE ops expose the complete fuse-native operation surface", async () => { const ops = makeFUSEOps((await createNodeVirtualFileSystem()).vfs); @@ -316,11 +327,9 @@ test("FUSE rename carries the buffered bytes to the new path", async () => { }); test("FUSE getattr size matches what readFileSync would return", async () => { - // getattr returns entry.size when the buffer is populated, so - // stat-after-write sees the new size even before flush. The VFS - // sees the pre-write inode size (0). After flush they have to - // agree, otherwise the buffer is silently masking a stale VFS - // state that an RPC reader would hit. + // Direct writes update the backing VFS immediately, so getattr and + // provider stat agree before fsync/release. The old buffered fallback + // used to expose a deferred-create window here. const { vfs } = await createNodeVirtualFileSystem(); const ops = makeFUSEOps(vfs); @@ -333,15 +342,15 @@ test("FUSE getattr size matches what readFileSync would return", async () => { await status((cb: (value: number) => void) => ops.write("/g.txt", fh, payload, payload.byteLength, 0, cb), ); - // Buffer-only state: FUSE getattr leads, VFS has no inode yet. Documents - // the intentional deferred-create window between create/write and a flushing op. + // Direct-write state: FUSE getattr and backing VFS stat agree before a + // flushing op. const beforeFlush = await callback((cb: (errno: number, result: unknown) => void) => ops.getattr("/g.txt", cb), ); expect((beforeFlush.result as { size: number }).size).toBe(12); - expect(() => vfs.statSync("/g.txt")).toThrow(); + expect(vfs.statSync("/g.txt").size).toBe(12); - // After flush both must agree — anything calling stat through + // After flush they still agree — anything calling stat through // the VFS (RPC, host-side platformatic/vfs) needs the truth. expect(await status((cb) => ops.fsync("/g.txt", fh, 0, cb))).toBe(0); const afterFlush = await callback((cb: (errno: number, result: unknown) => void) => @@ -391,6 +400,7 @@ test("FUSE ops translate kernel-relative paths onto the configured mount point", test("FUSE clean buffers are not spilled repeatedly", async () => { const { vfs } = await createNodeVirtualFileSystem(); + disableDirectWrites(vfs); const ops = makeFUSEOps(vfs); const create = await callback((cb: (errno: number, result: unknown) => void) => @@ -449,6 +459,7 @@ test("FUSE read-only hydrated buffers are not spilled on close", async () => { test("FUSE flush uses ranged writes when the backing VFS supports them", async () => { const { vfs } = await createNodeVirtualFileSystem(); + disableDirectWrites(vfs); const ops = makeFUSEOps(vfs); const create = await callback((cb: (errno: number, result: unknown) => void) => @@ -511,8 +522,29 @@ test("FUSE ops reject a relative mountPoint", async () => { expect(() => makeFUSEOps(vfs, "workspace")).toThrow(/absolute/); }); +test("FUSE direct writes are visible through the VFS before release", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + const ops = makeFUSEOps(vfs); + + const create = await callback((cb: (errno: number, result: unknown) => void) => + ops.create("/direct.txt", 0o644, cb), + ); + expect(create.errno).toBe(0); + const fh = create.result as number; + const payload = Buffer.from("direct"); + expect( + await status((cb) => ops.write("/direct.txt", fh, payload, payload.byteLength, 0, cb)), + ).toBe(payload.byteLength); + + expect(vfs.readFileSync("/direct.txt").toString()).toBe("direct"); + expect(ops.getBufferStats()).toMatchObject({ entries: 0, dirtyEntries: 0, capacityBytes: 0 }); + + expect(await status((cb) => ops.release("/direct.txt", fh, cb))).toBe(0); +}); + test("FUSE buffer stats report resident write buffers", async () => { const { vfs } = await createNodeVirtualFileSystem(); + disableDirectWrites(vfs); const ops = makeFUSEOps(vfs); expect(ops.getBufferStats()).toMatchObject({ diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 6809eb99..64d8660b 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -188,6 +188,17 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO ): void; } + interface DirectWriteVfs { + createFileSync(path: string, options?: { mode?: number }): void; + writeRangeSync( + path: string, + data: Buffer | Uint8Array, + offset: number, + options?: { mode?: number }, + ): number; + truncateFileSync(path: string, size: number): void; + } + interface FileEntry { buf: Buffer; // capacity buffer (may be larger than size) size: number; // logical end-of-file @@ -222,6 +233,12 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO }; const files = new Map(); const rangedWriteVfs = vfs as NodeVirtualFileSystem & Partial; + const directWriteVfs = vfs as NodeVirtualFileSystem & + Partial & { chmodSync?: (path: string, mode: number) => void }; + const hasDirectWrites = + directWriteVfs.createFileSync !== undefined && + directWriteVfs.writeRangeSync !== undefined && + directWriteVfs.truncateFileSync !== undefined; const linkableVfs = vfs as NodeVirtualFileSystem & { linkSync?: (existingPath: string, newPath: string) => void; }; @@ -436,6 +453,11 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb(ERRNO.EEXIST, 0); return; } + if (hasDirectWrites) { + directWriteVfs.createFileSync?.(toVfs(path), { mode }); + cb(0, openFileHandle(path)); + return; + } // Defer the VFS inode write until flush/release/fsync. Most create // workloads immediately write content, so persisting an empty file here // doubles provider work for tiny files. @@ -498,6 +520,22 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb(ERRNO.ENOENT); return; } + if (hasDirectWrites) { + if (position + length > MAX_FILE_BYTES) { + cb(ERRNO.EFBIG); + return; + } + try { + cb( + directWriteVfs.writeRangeSync?.(toVfs(path), buffer.subarray(0, length), position, { + mode: modeFromVfs(path), + }) ?? ERRNO.ENOSYS, + ); + } catch (error) { + cb(toErrno(error)); + } + return; + } try { const data = vfs.readFileSync(toVfs(path)); entry = { @@ -568,6 +606,19 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO } let entry = files.get(path); if (entry === undefined) { + if (hasDirectWrites) { + if (size > MAX_FILE_BYTES) { + cb(ERRNO.EFBIG); + return; + } + try { + directWriteVfs.truncateFileSync?.(toVfs(path), size); + cb(0); + } catch (error) { + cb(toErrno(error)); + } + return; + } try { const data = vfs.readFileSync(toVfs(path)); entry = { @@ -704,6 +755,14 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO const entry = files.get(path); if (entry !== undefined) entry.mode = mode; updateMeta(path, { mode }); + if (entry === undefined && directWriteVfs.chmodSync !== undefined) { + try { + directWriteVfs.chmodSync(toVfs(path), mode); + } catch (error) { + cb(toErrno(error)); + return; + } + } cb(0); }, diff --git a/packages/wsd/src/fuse/vfs.ts b/packages/wsd/src/fuse/vfs.ts index 3efed6b1..86a1c79e 100644 --- a/packages/wsd/src/fuse/vfs.ts +++ b/packages/wsd/src/fuse/vfs.ts @@ -73,6 +73,10 @@ const FORWARDED_METHODS = [ "writeFile", "writeFileSync", "writeFileRangesSync", + "createFileSync", + "writeRangeSync", + "truncateFileSync", + "chmodSync", "appendFile", "appendFileSync", "exists", @@ -176,6 +180,46 @@ export async function createNodeVirtualFileSystem( writable: true, configurable: true, }); + Object.defineProperty(vfs, "createFileSync", { + value: (path: string, options?: { mode?: number }) => + ( + provider as unknown as { createFileSync(path: string, options?: { mode?: number }): void } + ).createFileSync(path, options), + writable: true, + configurable: true, + }); + Object.defineProperty(vfs, "writeRangeSync", { + value: (path: string, data: Buffer | Uint8Array, offset: number, options?: { mode?: number }) => + ( + provider as unknown as { + writeRangeSync( + path: string, + data: Buffer | Uint8Array, + offset: number, + options?: { mode?: number }, + ): number; + } + ).writeRangeSync(path, data, offset, options), + writable: true, + configurable: true, + }); + Object.defineProperty(vfs, "truncateFileSync", { + value: (path: string, size: number) => + ( + provider as unknown as { truncateFileSync(path: string, size: number): void } + ).truncateFileSync(path, size), + writable: true, + configurable: true, + }); + Object.defineProperty(vfs, "chmodSync", { + value: (path: string, mode: number) => + (provider as unknown as { chmodSync(path: string, mode: number): void }).chmodSync( + path, + mode, + ), + writable: true, + configurable: true, + }); return { vfs, db, stopSync }; } From e64329473f14b6ea8fabc07d287cb5a339742861 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:48:14 +0000 Subject: [PATCH 11/31] wsd: keep direct FUSE reads off the buffer cache In direct-write mode, serve FUSE reads from the backing VFS without hydrating a FileEntry. This keeps a read-before-write sequence on the direct DOFS path instead of accidentally switching later writes to the buffered fallback. The regression test writes, reads, writes again, and asserts that the backing VFS sees the second write immediately while FUSE buffer stats remain empty. --- packages/wsd/src/fuse/driver.test.ts | 29 ++++++++++++++++++++++++++++ packages/wsd/src/fuse/driver.ts | 16 ++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index a6a25980..964043bf 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -522,6 +522,35 @@ test("FUSE ops reject a relative mountPoint", async () => { expect(() => makeFUSEOps(vfs, "workspace")).toThrow(/absolute/); }); +test("FUSE direct reads do not force later writes onto the buffered fallback", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + const ops = makeFUSEOps(vfs); + + const create = await callback((cb: (errno: number, result: unknown) => void) => + ops.create("/direct-read-write.txt", 0o644, cb), + ); + expect(create.errno).toBe(0); + const fh = create.result as number; + const first = Buffer.from("first"); + expect( + await status((cb) => ops.write("/direct-read-write.txt", fh, first, first.byteLength, 0, cb)), + ).toBe(first.byteLength); + + const out = Buffer.alloc(first.byteLength); + expect( + await status((cb) => ops.read("/direct-read-write.txt", fh, out, out.byteLength, 0, cb)), + ).toBe(first.byteLength); + expect(out.toString()).toBe("first"); + expect(ops.getBufferStats()).toMatchObject({ entries: 0, dirtyEntries: 0, capacityBytes: 0 }); + + const second = Buffer.from("second"); + expect( + await status((cb) => ops.write("/direct-read-write.txt", fh, second, second.byteLength, 0, cb)), + ).toBe(second.byteLength); + expect(vfs.readFileSync("/direct-read-write.txt").toString()).toBe("second"); + expect(ops.getBufferStats()).toMatchObject({ entries: 0, dirtyEntries: 0, capacityBytes: 0 }); +}); + test("FUSE direct writes are visible through the VFS before release", async () => { const { vfs } = await createNodeVirtualFileSystem(); const ops = makeFUSEOps(vfs); diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 64d8660b..d1fc94be 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -484,11 +484,21 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO read(path, _fh, buffer, length, position, cb) { let entry = files.get(path); if (entry === undefined) { - // File was created out-of-band (e.g. before this driver started - // tracking it). Lazy-hydrate from the VFS, carrying the - // persisted mode forward so a later flush doesn't downgrade it. try { const data = vfs.readFileSync(toVfs(path)); + if (hasDirectWrites) { + if (position >= data.length) { + cb(0); + return; + } + const end = Math.min(position + length, data.length); + data.copy(buffer, 0, position, end); + cb(end - position); + return; + } + // File was created out-of-band (e.g. before this driver started + // tracking it). Lazy-hydrate from the VFS, carrying the + // persisted mode forward so a later flush doesn't downgrade it. entry = { buf: data, size: data.length, From 4b806f7869cd69538b4765a66afc37ff5d34105c Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:51:25 +0000 Subject: [PATCH 12/31] dofs: sync inline direct-write bytes Teach change materialization to represent inline_data as a wire chunk and stage inline bytes in the blob store when direct writes land small files inline. Without this, direct-written inline files appeared on the sync wire as zero-byte files because they had no vfs_chunks rows. The fetch tests now cover a direct inline write through fetchChanges and fetchObjects so the change entry size, chunk hash, and object bytes stay consistent. --- packages/dofs/src/fs/writeFile.ts | 3 +++ packages/dofs/src/sync/changes.ts | 27 ++++++++++++++++++++++----- packages/dofs/src/sync/fetch.test.ts | 18 +++++++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 6b13cb1a..63b6b4f5 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -406,6 +406,9 @@ function writeInlineInode( mtime: number, ): void { db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); + if (bytes.byteLength > 0) { + stageBlob(db, sha256(bytes), bytes, mtime); + } const rev = incrementRev(db); db.run( "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", diff --git a/packages/dofs/src/sync/changes.ts b/packages/dofs/src/sync/changes.ts index 8604f73b..ced4be28 100644 --- a/packages/dofs/src/sync/changes.ts +++ b/packages/dofs/src/sync/changes.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { resolveInode } from "../fs/resolve.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; @@ -20,6 +21,12 @@ export function recordDelete(db: Database, rev: number, path: string): void { // tombstones. The puller uses it as a per-entry cursor so it can // advance fetchRev per committed batch instead of waiting for the // whole stream to drain. +function sha256(bytes: Uint8Array): Uint8Array { + const hash = createHash("sha256"); + hash.update(bytes); + return new Uint8Array(hash.digest()); +} + export type ChangeEntry = | { kind: "file"; @@ -73,12 +80,22 @@ export function materialiseChange(db: Database, path: string): ChangeEntry | nul } // file: collect chunk rows in index order. Each row carries hash // and size so the receiver can probe hasObjects without a - // separate manifest lookup. Total size is the sum of the chunks; - // an empty file produces zero rows and size 0. - const chunks = db.all<{ hash: Uint8Array; size: number }>( - "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", + // separate manifest lookup. Inline files do not have chunk rows; + // synthesize the single wire chunk from inline_data so sync still + // ships their bytes. Empty files produce zero rows and size 0. + const inline = db.one<{ inline_data: Uint8Array | null }>( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", live.inode, - ); + )?.inline_data; + const chunks = + inline !== undefined && inline !== null + ? inline.byteLength === 0 + ? [] + : [{ hash: sha256(inline), size: inline.byteLength }] + : db.all<{ hash: Uint8Array; size: number }>( + "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", + live.inode, + ); let size = 0; for (const c of chunks) size += c.size; return { diff --git a/packages/dofs/src/sync/fetch.test.ts b/packages/dofs/src/sync/fetch.test.ts index 49bc28ca..8270bce2 100644 --- a/packages/dofs/src/sync/fetch.test.ts +++ b/packages/dofs/src/sync/fetch.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from "vitest"; import { withDB } from "../fs/with-db.js"; -import { writeFile } from "../fs/writeFile.js"; +import { createFileSync, writeFile, writeRangeSync } from "../fs/writeFile.js"; import { coalesceChanges } from "./coalesce.js"; import { fetchChanges, fetchObjects, hasObjects } from "./fetch.js"; @@ -22,6 +22,22 @@ describe("fetch wire", () => { }); }); + it("fetchChanges and fetchObjects include inline direct writes", async () => { + await withDB(async (db) => { + createFileSync(db, "/inline.txt", {}, () => 1); + writeRangeSync(db, "/inline.txt", new TextEncoder().encode("inline direct"), 0, {}, () => 2); + + const entries = await drain(fetchChanges(db, 0)); + const file = entries.find((entry) => entry.kind === "file" && entry.path === "/inline.txt"); + expect(file).toMatchObject({ kind: "file", size: "inline direct".length }); + expect(file?.kind === "file" ? file.chunks : []).toHaveLength(1); + const hash = file?.kind === "file" ? file.chunks[0].hash : new Uint8Array(); + const objects = await drain(fetchObjects(db, [hash])); + expect(objects).toHaveLength(1); + expect(new TextDecoder().decode(objects[0].bytes)).toBe("inline direct"); + }); + }); + it("fetchObjects yields each hash exactly once", async () => { await withDB(async (db) => { await writeFile(db, "/a.txt", "shared", {}, () => 1); From 991ed30ce89713b19352a06a0a3c1dfd2cc6632b Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Mon, 8 Jun 2026 22:07:42 +0000 Subject: [PATCH 13/31] dofs: add positional readRangeSync primitive Add a positional read helper that slices vfs_nodes.inline_data for inline files and walks only the vfs_chunks rows overlapping the requested byte range for chunk-backed files. Direct-mode FUSE reads can now serve a kernel read without materializing the whole file on every syscall. Tests cover non-zero inline offsets, inline clamp past EOF, single chunk windows, reads crossing a chunk boundary, and past-EOF reads on chunk-backed files. --- packages/dofs/src/fs/readFile.ts | 71 ++++++++++++++++++++++++++ packages/dofs/src/fs/readRange.test.ts | 61 ++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 packages/dofs/src/fs/readRange.test.ts diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index 85036508..a937e0cc 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -1,6 +1,7 @@ import { createWorkspaceError } from "../errors.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; +import { CHUNK_SIZE } from "./writeFile.js"; export interface ReadFileOptions { encoding?: "utf8"; @@ -118,6 +119,76 @@ export async function readFile( }); } +// Positional read primitive. Slices `inline_data` for inline files and +// walks only the chunk rows that overlap [offset, offset+length) for +// chunk-backed files, so the FUSE driver can serve a kernel read +// without materializing the whole file. +export function readRangeSync( + db: Database, + path: string, + offset: number, + length: number, +): Uint8Array { + if (!Number.isInteger(offset) || offset < 0) { + throw createWorkspaceError("EINVAL", `invalid read offset: ${offset}`, path); + } + if (!Number.isInteger(length) || length < 0) { + throw createWorkspaceError("EINVAL", `invalid read length: ${length}`, path); + } + const node = resolveInode(db, path); + if (node === null) { + throw createWorkspaceError("ENOENT", `no such file: ${path}`, path); + } + if (node.type !== "file") { + throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); + } + if (length === 0) return new Uint8Array(); + + const inline = db.one( + "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.inline_data; + if (inline !== undefined && inline !== null) { + if (offset >= inline.byteLength) return new Uint8Array(); + const end = Math.min(offset + length, inline.byteLength); + return inline.subarray(offset, end); + } + + const totalSize = + db.scalar( + "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", + node.inode, + ) ?? 0; + if (offset >= totalSize) return new Uint8Array(); + const end = Math.min(offset + length, totalSize); + const firstIdx = Math.floor(offset / CHUNK_SIZE); + const lastIdx = Math.floor((end - 1) / CHUNK_SIZE); + const out = new Uint8Array(end - offset); + let written = 0; + for (let idx = firstIdx; idx <= lastIdx; idx++) { + const start = idx * CHUNK_SIZE; + const chunk = db.one<{ hash: Uint8Array }>( + "SELECT hash FROM vfs_chunks WHERE inode = ? AND idx = ?", + node.inode, + idx, + ); + if (chunk === undefined) continue; + const row = db.one<{ bytes: Uint8Array }>( + "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", + chunk.hash, + ); + if (row === undefined) { + throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path); + } + const srcStart = Math.max(0, offset - start); + const srcEnd = Math.min(row.bytes.byteLength, end - start); + if (srcEnd <= srcStart) continue; + out.set(row.bytes.subarray(srcStart, srcEnd), written); + written += srcEnd - srcStart; + } + return written === out.byteLength ? out : out.subarray(0, written); +} + function touchBlobs(db: Database, chunks: ChunkRow[], at: number): void { // Dedupe in case the same chunk hash appears multiple times in a // single file — keeps the UPDATE count low without changing semantics. diff --git a/packages/dofs/src/fs/readRange.test.ts b/packages/dofs/src/fs/readRange.test.ts new file mode 100644 index 00000000..15f6521a --- /dev/null +++ b/packages/dofs/src/fs/readRange.test.ts @@ -0,0 +1,61 @@ +import { describe, expect, it } from "vitest"; + +import { readRangeSync } from "./readFile.js"; +import { withDB } from "./with-db.js"; +import { CHUNK_SIZE, writeFileSync } from "./writeFile.js"; + +describe("readRangeSync", () => { + it("reads from inline files at non-zero offset", async () => { + await withDB((db) => { + writeFileSync(db, "/inline.txt", new TextEncoder().encode("hello world"), {}, () => 1); + + const slice = readRangeSync(db, "/inline.txt", 6, 5); + expect(new TextDecoder().decode(slice)).toBe("world"); + }); + }); + + it("clamps the inline read at end of file", async () => { + await withDB((db) => { + writeFileSync(db, "/inline.txt", new TextEncoder().encode("abc"), {}, () => 1); + + expect(readRangeSync(db, "/inline.txt", 0, 100).byteLength).toBe(3); + expect(readRangeSync(db, "/inline.txt", 2, 100).byteLength).toBe(1); + expect(readRangeSync(db, "/inline.txt", 3, 100).byteLength).toBe(0); + }); + }); + + it("reads a single chunk window without materializing other chunks", async () => { + await withDB((db) => { + const original = new Uint8Array(CHUNK_SIZE * 3); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + original.fill(3, CHUNK_SIZE * 2); + writeFileSync(db, "/large.bin", original, {}, () => 1); + + const slice = readRangeSync(db, "/large.bin", CHUNK_SIZE + 10, 5); + expect(Array.from(slice)).toEqual([2, 2, 2, 2, 2]); + }); + }); + + it("reads across a chunk boundary", async () => { + await withDB((db) => { + const original = new Uint8Array(CHUNK_SIZE + 100); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE); + writeFileSync(db, "/large.bin", original, {}, () => 1); + + const slice = readRangeSync(db, "/large.bin", CHUNK_SIZE - 2, 4); + expect(Array.from(slice)).toEqual([1, 1, 2, 2]); + }); + }); + + it("returns an empty view past the end of a chunk-backed file", async () => { + await withDB((db) => { + const original = new Uint8Array(CHUNK_SIZE + 1); + original.fill(7); + writeFileSync(db, "/large.bin", original, {}, () => 1); + + expect(readRangeSync(db, "/large.bin", CHUNK_SIZE + 1, 10).byteLength).toBe(0); + }); + }); +}); From 7bcf1c5874d689227f82e438fc76526579fec728 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:48:13 +0000 Subject: [PATCH 14/31] wsd: dispatch dofs methods through prototype chain Drop the SQLiteVirtualProvider wrapper class and its FORWARDED_METHODS dispatch table. Splice VirtualProvider onto SQLiteWorkspaceProvider's prototype chain at the wsd boundary instead, so @platformatic/vfs's instanceof guard accepts the dofs provider directly. The seam used to keep two parallel surfaces in sync by hand: a forwarded-method list on the wrapper class and a separate post-create Object.defineProperty block for methods @platformatic/vfs does not expose on VirtualFileSystem. Adding readRangeSync to one path but not the other caused FUSE reads to throw EIO at runtime. After the splice, @platformatic/vfs's create() returns a VirtualFileSystem whose VirtualProvider methods reach the dofs class directly. Only the dofs-specific extensions (linkSync, createFileSync, writeRangeSync, truncateFileSync, chmodSync, readRangeSync) need a post-create attachment, and they bind straight to the provider with no indirection. Wire readRangeSync through the provider's readSync and through the FUSE driver's direct-mode read so a kernel read no longer materializes the whole file on every syscall. The provider's old whole-file readFileBytesSync helper is gone with its last caller. --- packages/dofs/src/provider.ts | 60 ++------ packages/wsd/src/fuse/driver.test.ts | 36 +++++ packages/wsd/src/fuse/driver.ts | 19 ++- packages/wsd/src/fuse/vfs.ts | 202 +++++++-------------------- 4 files changed, 112 insertions(+), 205 deletions(-) diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 5ec51c03..757b306b 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -12,6 +12,7 @@ import { link as linkImpl } from "./fs/link.js"; import type { MkdirOptions } from "./fs/mkdir.js"; import { mkdir as mkdirImpl } from "./fs/mkdir.js"; import { readdir as readdirImpl } from "./fs/readdir.js"; +import { readRangeSync as readRangeSyncImpl } from "./fs/readFile.js"; import { readlink as readlinkImpl } from "./fs/readlink.js"; import { resolveInode } from "./fs/resolve.js"; import { rm as rmImpl } from "./fs/rm.js"; @@ -567,21 +568,21 @@ export class SQLiteWorkspaceProvider { throw createWorkspaceError("EBADF", `fd ${fd} is not readable`); } const startAt = position ?? state.position; - const bytes = readFileBytesSync(this.db, state.path); - if (startAt >= bytes.byteLength) { - return 0; - } - const end = Math.min(startAt + length, bytes.byteLength); - const n = end - startAt; + const slice = readRangeSyncImpl(this.db, state.path, startAt, length); const view = buffer instanceof Buffer ? buffer : Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength); - view.set(bytes.subarray(startAt, end), offset); + view.set(slice, offset); if (position === null || position === undefined) { - state.position += n; + state.position = startAt + slice.byteLength; } - return n; + return slice.byteLength; + } + + readRangeSync(path: string, offset: number, length: number): Buffer { + const slice = readRangeSyncImpl(this.db, path, offset, length); + return Buffer.from(slice.buffer, slice.byteOffset, slice.byteLength); } writeSync( @@ -917,44 +918,3 @@ function parseFlags(flags: string): ParsedFlags { throw createWorkspaceError("EINVAL", `unsupported fs flag: ${flags}`); } } - -// Pull a file's full content out of the chunk store into one buffer. -// Used by the fd-positional code paths because the simplest correct -// model for writeSync/truncate is "read whole file, splice, write -// whole file"; the content-addressed write path keeps untouched -// chunks deduped so this only costs the changed chunks on the wire. -function readFileBytesSync(db: Database, path: string): Uint8Array { - const node = resolveInode(db, path); - if (node === null) { - throw createWorkspaceError("ENOENT", `no such file: ${path}`, path); - } - if (node.type !== "file") { - throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); - } - const inline = db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data; - if (inline !== undefined && inline !== null) return inline; - - const chunks = db.all<{ hash: Uint8Array; size: number }>( - "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", - node.inode, - ); - let total = 0; - for (const c of chunks) total += c.size; - const out = new Uint8Array(total); - let pos = 0; - for (const chunk of chunks) { - const row = db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { - throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path); - } - out.set(row.bytes, pos); - pos += row.bytes.byteLength; - } - return out; -} diff --git a/packages/wsd/src/fuse/driver.test.ts b/packages/wsd/src/fuse/driver.test.ts index 964043bf..2c29e186 100644 --- a/packages/wsd/src/fuse/driver.test.ts +++ b/packages/wsd/src/fuse/driver.test.ts @@ -551,6 +551,42 @@ test("FUSE direct reads do not force later writes onto the buffered fallback", a expect(ops.getBufferStats()).toMatchObject({ entries: 0, dirtyEntries: 0, capacityBytes: 0 }); }); +test("FUSE direct reads use readRangeSync instead of materializing the whole file", async () => { + const { vfs } = await createNodeVirtualFileSystem(); + vfs.writeFileSync("/range.bin", Buffer.from("hello world")); + + let readFileCalls = 0; + const realReadFileSync = vfs.readFileSync.bind(vfs); + vfs.readFileSync = (...args: Parameters) => { + readFileCalls += 1; + return realReadFileSync(...args); + }; + let rangeCalls = 0; + const rangeAware = vfs as typeof vfs & { + readRangeSync: (path: string, offset: number, length: number) => Buffer; + }; + const realReadRangeSync = rangeAware.readRangeSync.bind(rangeAware); + rangeAware.readRangeSync = (path: string, offset: number, length: number) => { + rangeCalls += 1; + return realReadRangeSync(path, offset, length); + }; + + const ops = makeFUSEOps(vfs); + const open = await callback((cb: (errno: number, result: unknown) => void) => + ops.open("/range.bin", 0, cb), + ); + expect(open.errno).toBe(0); + const fh = open.result as number; + + const buf = Buffer.alloc(5); + expect(await status((cb) => ops.read("/range.bin", fh, buf, buf.byteLength, 6, cb))).toBe(5); + expect(buf.toString()).toBe("world"); + expect(rangeCalls).toBe(1); + expect(readFileCalls).toBe(0); + + expect(await status((cb) => ops.release("/range.bin", fh, cb))).toBe(0); +}); + test("FUSE direct writes are visible through the VFS before release", async () => { const { vfs } = await createNodeVirtualFileSystem(); const ops = makeFUSEOps(vfs); diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index d1fc94be..89c0c355 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -234,7 +234,10 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO const files = new Map(); const rangedWriteVfs = vfs as NodeVirtualFileSystem & Partial; const directWriteVfs = vfs as NodeVirtualFileSystem & - Partial & { chmodSync?: (path: string, mode: number) => void }; + Partial & { + chmodSync?: (path: string, mode: number) => void; + readRangeSync?: (path: string, offset: number, length: number) => Uint8Array; + }; const hasDirectWrites = directWriteVfs.createFileSync !== undefined && directWriteVfs.writeRangeSync !== undefined && @@ -484,6 +487,20 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO read(path, _fh, buffer, length, position, cb) { let entry = files.get(path); if (entry === undefined) { + if (hasDirectWrites && directWriteVfs.readRangeSync !== undefined) { + try { + const slice = directWriteVfs.readRangeSync(toVfs(path), position, length); + if (slice.byteLength === 0) { + cb(0); + return; + } + buffer.set(slice, 0); + cb(slice.byteLength); + } catch (error) { + cb(toErrno(error)); + } + return; + } try { const data = vfs.readFileSync(toVfs(path)); if (hasDirectWrites) { diff --git a/packages/wsd/src/fuse/vfs.ts b/packages/wsd/src/fuse/vfs.ts index 86a1c79e..1fa3fedf 100644 --- a/packages/wsd/src/fuse/vfs.ts +++ b/packages/wsd/src/fuse/vfs.ts @@ -8,116 +8,46 @@ export type NodeVirtualFileSystem = VirtualFileSystem; // @platformatic/vfs's create() guards on `provider instanceof // VirtualProvider` and silently falls back to MemoryProvider when -// the check fails. dofs's SQLiteWorkspaceProvider can't -// extend VirtualProvider directly without dragging the node-only -// @platformatic/vfs dependency into the workerd-targeted package, -// so we glue them together here. +// the check fails. dofs's SQLiteWorkspaceProvider can't import +// @platformatic/vfs (workerd target), so we splice VirtualProvider +// onto its prototype chain at the wsd boundary. The splice happens +// only here, never in dofs, so the workerd build stays clean. // -// The subclass forwards every method to the dofs provider -// instance held in its constructor. We can't use Object.assign or -// setPrototypeOf at the seam because @platformatic/vfs's -// VirtualFileSystem reads getters (readonly, supportsSymlinks, -// supportsWatch) off the provider that the dofs class -// declares as instance properties; the wrapping pattern lets us -// pass those through cleanly without re-implementing the data -// model. - -class SQLiteVirtualProvider extends VirtualProvider { - private readonly inner: SQLiteWorkspaceProvider; - - constructor(db: Database) { - super(); - this.inner = new SQLiteWorkspaceProvider(db); - } - - // VirtualProvider's static getters return false by default; the - // dofs provider declares the real values as instance - // properties. Re-expose them on this wrapper. - override get readonly(): boolean { - return this.inner.readonly; - } - override get supportsSymlinks(): boolean { - return this.inner.supportsSymlinks; - } - override get supportsWatch(): boolean { - return this.inner.supportsWatch; +// One-time splice: SQLiteWorkspaceProvider.prototype -> VirtualProvider.prototype. +// VirtualProvider's no-op default methods stay reachable for anything +// dofs doesn't override (most of them throw ENOSYS, which is fine). +let prototypePatched = false; +function ensureVirtualProviderPrototype(): void { + if (prototypePatched) return; + const proto = SQLiteWorkspaceProvider.prototype as object; + const parent = Object.getPrototypeOf(proto); + if (parent === VirtualProvider.prototype) { + prototypePatched = true; + return; } + // Walk to the top of the dofs chain and splice VirtualProvider in + // just above Object.prototype. Concretely the dofs class extends + // Object directly, so this is a single hop. + Object.setPrototypeOf(proto, VirtualProvider.prototype); + prototypePatched = true; } -// Wire forwarding methods on the prototype. Doing this in a loop -// outside the class body keeps the (large) method list out of the -// readable surface. Every method on the dofs provider that -// VirtualProvider declares is forwarded; the rest still throw the -// VirtualProvider default ENOSYS. -const FORWARDED_METHODS = [ - "open", - "openSync", - "stat", - "statSync", - "lstat", - "lstatSync", - "readdir", - "readdirSync", - "mkdir", - "mkdirSync", - "rmdir", - "rmdirSync", - "unlink", - "unlinkSync", - "rename", - "renameSync", - "link", +// Methods the dofs provider implements that @platformatic/vfs's +// VirtualFileSystem does not expose. We attach them to the vfs +// instance after create() so the FUSE driver and tests can call +// them through `vfs.x(...)` instead of reaching for the provider. +// +// Keep this list small: anything @platformatic/vfs already exposes +// (readFileSync, writeFileSync, statSync, ...) does not belong here. +const EXTRA_VFS_METHODS = [ "linkSync", - "readFile", - "readFileSync", - "writeFile", - "writeFileSync", - "writeFileRangesSync", "createFileSync", "writeRangeSync", "truncateFileSync", "chmodSync", - "appendFile", - "appendFileSync", - "exists", - "existsSync", - "copyFile", - "copyFileSync", - "internalModuleStat", - "realpath", - "realpathSync", - "access", - "accessSync", - "readlink", - "readlinkSync", - "symlink", - "symlinkSync", - "watch", - "watchAsync", - "watchFile", - "unwatchFile", - // Provider-specific fd extensions the @platformatic/vfs router - // sometimes pokes at. - "closeSync", - "readSync", - "writeSync", - "fstatSync", - "truncateSync", - "ftruncateSync", + "readRangeSync", ] as const; -for (const name of FORWARDED_METHODS) { - Object.defineProperty(SQLiteVirtualProvider.prototype, name, { - value: function (this: SQLiteVirtualProvider, ...args: unknown[]): unknown { - // biome-ignore lint/suspicious/noExplicitAny: dispatch table - const inner = (this as unknown as { inner: any }).inner; - return inner[name](...args); - }, - writable: true, - configurable: true, - }); -} - export interface CreateOptions { // Optional upstream sync surface. When set, the local store // performs an initial pull on construction. When unset, wsd runs @@ -152,6 +82,7 @@ const SYNC_TICK_MS = 250; export async function createNodeVirtualFileSystem( options: CreateOptions = {}, ): Promise { + ensureVirtualProviderPrototype(); const storage = new SQLiteTestStorage(); const db = new Database(storage); initializeSchema(db, () => Date.now()); @@ -165,61 +96,24 @@ export async function createNodeVirtualFileSystem( stopSync = startSyncLoop(db, options.upstream); } - const provider = new SQLiteVirtualProvider(db); - const vfs = create(provider, { moduleHooks: false }); - // @platformatic/vfs does not expose hardlink helpers on - // VirtualFileSystem, but FUSE needs link(2). Attach the provider - // primitive directly so the driver can call it while all ordinary - // VFS callers keep using the standard surface. - Object.defineProperty(vfs, "linkSync", { - value: (existingPath: string, newPath: string) => - (provider as unknown as { linkSync(existingPath: string, newPath: string): void }).linkSync( - existingPath, - newPath, - ), - writable: true, - configurable: true, - }); - Object.defineProperty(vfs, "createFileSync", { - value: (path: string, options?: { mode?: number }) => - ( - provider as unknown as { createFileSync(path: string, options?: { mode?: number }): void } - ).createFileSync(path, options), - writable: true, - configurable: true, - }); - Object.defineProperty(vfs, "writeRangeSync", { - value: (path: string, data: Buffer | Uint8Array, offset: number, options?: { mode?: number }) => - ( - provider as unknown as { - writeRangeSync( - path: string, - data: Buffer | Uint8Array, - offset: number, - options?: { mode?: number }, - ): number; - } - ).writeRangeSync(path, data, offset, options), - writable: true, - configurable: true, - }); - Object.defineProperty(vfs, "truncateFileSync", { - value: (path: string, size: number) => - ( - provider as unknown as { truncateFileSync(path: string, size: number): void } - ).truncateFileSync(path, size), - writable: true, - configurable: true, - }); - Object.defineProperty(vfs, "chmodSync", { - value: (path: string, mode: number) => - (provider as unknown as { chmodSync(path: string, mode: number): void }).chmodSync( - path, - mode, - ), - writable: true, - configurable: true, - }); + const provider = new SQLiteWorkspaceProvider(db); + const vfs = create(provider as unknown as VirtualProvider, { moduleHooks: false }); + // Forward the extra dofs methods that @platformatic/vfs's + // VirtualFileSystem doesn't expose. We bind directly to the + // provider — there is no `inner` indirection — so dispatch can't + // silently fall off if a method name only exists on one side. + // biome-ignore lint/suspicious/noExplicitAny: untyped extension surface + const providerAny = provider as any; + for (const name of EXTRA_VFS_METHODS) { + const fn = providerAny[name]; + if (typeof fn !== "function") continue; + Object.defineProperty(vfs, name, { + // biome-ignore lint/suspicious/noExplicitAny: untyped extension surface + value: (...args: any[]) => fn.apply(providerAny, args), + writable: true, + configurable: true, + }); + } return { vfs, db, stopSync }; } From 9851d518029f2ab3ef4d4e86442d4bea2fb0fe8b Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 09:52:54 +0000 Subject: [PATCH 15/31] dofs: keep untouched chunk rows on direct writes Replace the chunk-backed writeRangeSync and truncateFileSync update loop so it only touches the vfs_chunks rows whose contents or size actually changed. Untouched chunk rows keep their rowids and the manifest hash is invalidated rather than recomputed, so a tiny edit into the middle of a large file stops scaling with the total chunk count. Tests cover stable rowids across a small range write and manifest invalidation after a direct range write. --- packages/dofs/src/fs/writeFile.ts | 111 ++++++++++++++++-------- packages/dofs/src/fs/writeRange.test.ts | 51 +++++++++++ 2 files changed, 127 insertions(+), 35 deletions(-) diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 63b6b4f5..12d83eda 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -420,30 +420,68 @@ function writeInlineInode( ); } -function writeChunkedInode( +// Update an inode's chunk-backed representation in place. Iterates over +// the full chunk grid but only touches `vfs_chunks` rows whose contents +// or size actually changed, so untouched chunk rows keep their +// rowids and the surrounding rows do not churn. The manifest is +// invalidated rather than recomputed; sync rebuilds it lazily. +function applyChunkedInodeUpdate( db: Database, inode: number, size: number, mode: number, mtime: number, - buildChunk: (idx: number, start: number, end: number, oldChunk?: ChunkRef) => ChunkRef, + isTouched: (idx: number, start: number, end: number) => boolean, + buildChunkBytes: (idx: number, start: number, end: number, existing: Uint8Array) => Uint8Array, ): void { const oldChunks = existingChunkRefs(db, inode); - const nextChunks: ChunkRef[] = []; + const oldInline = inlineDataForInode(db, inode); const chunkCount = Math.ceil(size / CHUNK_SIZE); + const oldChunkCount = oldChunks.length; + for (let idx = 0; idx < chunkCount; idx++) { const start = idx * CHUNK_SIZE; const end = Math.min(start + CHUNK_SIZE, size); - nextChunks.push(buildChunk(idx, start, end, oldChunks[idx])); + const intendedSize = end - start; + const old = oldChunks[idx]; + const touched = isTouched(idx, start, end); + // Stable chunk: existed before with the same logical size and the + // caller did not flag it as touched. Skip without issuing SQL so + // its rowid stays put. + if (old !== undefined && old.size === intendedSize && !touched) continue; + + const existingBytes = + oldInline !== null + ? oldInline.subarray(start, Math.min(start + CHUNK_SIZE, oldInline.byteLength)) + : old !== undefined + ? readChunkBytes(db, inode, idx) + : new Uint8Array(); + const chunkBytes = buildChunkBytes(idx, start, end, existingBytes); + if (chunkBytes.byteLength !== intendedSize) { + throw createWorkspaceError("EIO", "chunk builder returned wrong size"); + } + const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; + upsertChunkBlob(db, chunk, mtime); + db.run( + "INSERT OR REPLACE INTO vfs_chunks (inode, idx, hash, size) VALUES (?, ?, ?, ?)", + inode, + idx, + chunk.hash, + chunk.size, + ); + } + + // Drop any old chunks past the new end of file (shrink case). + if (oldChunkCount > chunkCount) { + db.run("DELETE FROM vfs_chunks WHERE inode = ? AND idx >= ?", inode, chunkCount); } - const manifestHash = replaceChunkRows(db, inode, nextChunks, mtime); + const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = NULL WHERE inode = ?", mode, mtime, rev, - manifestHash, inode, ); } @@ -518,23 +556,24 @@ export function writeRangeSync( return; } - writeChunkedInode(db, inode, nextSize, mode, mtime, (idx, start, end, oldChunk) => { - const overlapsWrite = offset < end && start < writeEnd; - if (oldChunk !== undefined && oldChunk.size === end - start && !overlapsWrite) { - return oldChunk; - } - const chunkBytes = new Uint8Array(end - start); - const existing = readChunkBytes(db, inode, idx); - chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); - if (overlapsWrite) { - const copyStart = Math.max(start, offset); - const copyEnd = Math.min(end, writeEnd); - chunkBytes.set(bytes.subarray(copyStart - offset, copyEnd - offset), copyStart - start); - } - const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; - upsertChunkBlob(db, chunk, mtime); - return { hash: chunk.hash, size: chunk.size }; - }); + applyChunkedInodeUpdate( + db, + inode, + nextSize, + mode, + mtime, + (_idx, start, end) => offset < end && start < writeEnd, + (_idx, start, end, existing) => { + const chunkBytes = new Uint8Array(end - start); + chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); + if (offset < end && start < writeEnd) { + const copyStart = Math.max(start, offset); + const copyEnd = Math.min(end, writeEnd); + chunkBytes.set(bytes.subarray(copyStart - offset, copyEnd - offset), copyStart - start); + } + return chunkBytes; + }, + ); }); return bytes.byteLength; @@ -564,17 +603,19 @@ export function truncateFileSync( return; } - writeChunkedInode(db, inode, size, mode, mtime, (idx, start, end, oldChunk) => { - if (oldChunk !== undefined && oldChunk.size === end - start) { - return oldChunk; - } - const chunkBytes = new Uint8Array(end - start); - const existing = readChunkBytes(db, inode, idx); - chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); - const chunk = { hash: sha256(chunkBytes), bytes: chunkBytes, size: chunkBytes.byteLength }; - upsertChunkBlob(db, chunk, mtime); - return { hash: chunk.hash, size: chunk.size }; - }); + applyChunkedInodeUpdate( + db, + inode, + size, + mode, + mtime, + () => false, + (_idx, start, end, existing) => { + const chunkBytes = new Uint8Array(end - start); + chunkBytes.set(existing.subarray(0, Math.min(existing.byteLength, chunkBytes.byteLength))); + return chunkBytes; + }, + ); }); } diff --git a/packages/dofs/src/fs/writeRange.test.ts b/packages/dofs/src/fs/writeRange.test.ts index d79f2a87..82543f19 100644 --- a/packages/dofs/src/fs/writeRange.test.ts +++ b/packages/dofs/src/fs/writeRange.test.ts @@ -38,6 +38,26 @@ async function readBytes(db: Database, path: string): Promise { return out; } +function chunkRowIds(db: Database, path: string): Array<{ idx: number; rowid: number }> { + const node = resolveInode(db, path); + if (node === null) throw new Error(`missing node: ${path}`); + return db.all<{ idx: number; rowid: number }>( + "SELECT idx, rowid FROM vfs_chunks WHERE inode = ? ORDER BY idx", + node.inode, + ); +} + +function manifestHash(db: Database, path: string): Uint8Array | null { + const node = resolveInode(db, path); + if (node === null) throw new Error(`missing node: ${path}`); + return ( + db.one<{ manifest_hash: Uint8Array | null }>( + "SELECT manifest_hash FROM vfs_nodes WHERE inode = ?", + node.inode, + )?.manifest_hash ?? null + ); +} + function chunkRows( db: Database, path: string, @@ -134,6 +154,37 @@ describe("direct range writes", () => { }); }); + it("keeps untouched chunk rowids stable across a small range write", async () => { + await withDB(async (db) => { + const original = new Uint8Array(CHUNK_SIZE * 3); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + original.fill(3, CHUNK_SIZE * 2, CHUNK_SIZE * 3); + writeFileSync(db, "/large.bin", original, {}, () => 1000); + const beforeIds = chunkRowIds(db, "/large.bin"); + + writeRangeSync(db, "/large.bin", new Uint8Array([7]), CHUNK_SIZE + 10, {}, () => 1001); + const afterIds = chunkRowIds(db, "/large.bin"); + + expect(afterIds[0].rowid).toBe(beforeIds[0].rowid); + expect(afterIds[2].rowid).toBe(beforeIds[2].rowid); + expect(afterIds[1].rowid).not.toBe(beforeIds[1].rowid); + }); + }); + + it("invalidates the manifest hash after a direct range write", async () => { + await withDB(async (db) => { + const original = new Uint8Array(CHUNK_SIZE * 2); + original.fill(1, 0, CHUNK_SIZE); + original.fill(2, CHUNK_SIZE); + writeFileSync(db, "/large.bin", original, {}, () => 1000); + expect(manifestHash(db, "/large.bin")).not.toBe(null); + + writeRangeSync(db, "/large.bin", new Uint8Array([5]), 10, {}, () => 1001); + expect(manifestHash(db, "/large.bin")).toBe(null); + }); + }); + it("truncates chunk-backed files without rewriting untouched chunks", async () => { await withDB(async (db) => { const original = new Uint8Array(CHUNK_SIZE * 2 + 100); From a4cdf81d5cafcd1b67dfeaaa25c75c39e45aac6f Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 10:18:39 +0000 Subject: [PATCH 16/31] wsd: expose database table sizes on /__wsd/stats Add a JSON stats endpoint that reports DOFS table row counts, total inline and blob byte sizes, the orphan-blob subset, and process memory. Useful for watching how the store grows under load without attaching a debugger. Used to confirm that direct-mode FUSE writes accumulate per-write intermediate blobs that no chunk row references: an npm install of the sandbox-sdk repo reaches 4.7 GB of blob bytes of which 4.5 GB are orphaned, dominating the wsd resident set. --- packages/wsd/src/cli/wsd.ts | 48 ++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/wsd/src/cli/wsd.ts b/packages/wsd/src/cli/wsd.ts index c97b199d..6b577cdf 100644 --- a/packages/wsd/src/cli/wsd.ts +++ b/packages/wsd/src/cli/wsd.ts @@ -4,6 +4,7 @@ import { mkdir } from "node:fs/promises"; import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http"; import type { Socket } from "node:net"; import { isAbsolute } from "node:path"; +import type { Database } from "@cloudflare/dofs"; import { createWorkspaceClient, type WorkspaceClient } from "@cloudflare/workspace-rpc/client"; import { isStubTrackingEnabled, stubSnapshot } from "@cloudflare/workspace-rpc/debug"; import { @@ -104,6 +105,41 @@ interface WSDInfo { port: number; } +// Snapshot DOFS table sizes and process memory so an external caller +// can watch growth without attaching a debugger. Used by the +// /__wsd/stats endpoint while diagnosing the npm install OOM. +function collectDbStats(db: Database): Record { + // biome-ignore lint/suspicious/noExplicitAny: small ad-hoc shape + const out: Record = {}; + try { + out.vfs_nodes_count = db.scalar("SELECT COUNT(*) FROM vfs_nodes") ?? 0; + out.vfs_dirents_count = db.scalar("SELECT COUNT(*) FROM vfs_dirents") ?? 0; + out.vfs_chunks_count = db.scalar("SELECT COUNT(*) FROM vfs_chunks") ?? 0; + out.vfs_blobs_count = db.scalar("SELECT COUNT(*) FROM vfs_blobs") ?? 0; + out.vfs_blob_bytes_total = + db.scalar("SELECT COALESCE(SUM(LENGTH(bytes)), 0) FROM vfs_blob_bytes") ?? 0; + out.vfs_blobs_orphan = + db.scalar( + "SELECT COUNT(*) FROM vfs_blobs b WHERE NOT EXISTS (SELECT 1 FROM vfs_chunks c WHERE c.hash = b.hash)", + ) ?? 0; + out.vfs_blob_bytes_orphan = + db.scalar( + "SELECT COALESCE(SUM(LENGTH(bytes)), 0) FROM vfs_blob_bytes bb WHERE NOT EXISTS (SELECT 1 FROM vfs_chunks c WHERE c.hash = bb.hash)", + ) ?? 0; + out.vfs_inline_bytes_total = + db.scalar("SELECT COALESCE(SUM(LENGTH(inline_data)), 0) FROM vfs_nodes") ?? 0; + } catch (error) { + out.error = (error as Error).message; + } + const mem = process.memoryUsage(); + out.rss = mem.rss; + out.heap_used = mem.heapUsed; + out.heap_total = mem.heapTotal; + out.external = mem.external; + out.array_buffers = mem.arrayBuffers; + return out; +} + interface HTTPHandle { server: Server; // Tear down the WebSocketServer alongside the HTTP server. @@ -113,6 +149,7 @@ interface HTTPHandle { function createHTTPServer( info: WSDInfo, rpc: ReturnType, + getStats?: () => Record, ): HTTPHandle { // Holds the current outbound capnweb session opened via /connect. // Re-POSTing /connect (e.g. after a DO hibernate + new incarnation) @@ -192,6 +229,15 @@ function createHTTPServer( return; } + if (path === "/__wsd/stats") { + const stats = getStats?.() ?? {}; + const body = request.method === "HEAD" ? "" : JSON.stringify(stats); + send(response, 200, body, { + "content-type": "application/json; charset=utf-8", + }); + return; + } + if (path === "/__wsd/info") { const body = request.method === "HEAD" ? "" : JSON.stringify(info); send(response, 200, body, { @@ -481,7 +527,7 @@ async function main(): Promise { } : {}), }); - const http = createHTTPServer(info, rpc); + const http = createHTTPServer(info, rpc, () => collectDbStats(db)); let shuttingDown = false; const shutdown = async (signal: NodeJS.Signals): Promise => { From 17ee0c10bbf9155d33b8ed136123a2330343840e Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 10:34:15 +0000 Subject: [PATCH 17/31] dofs: buffer writes per open file and drop inline storage Add an in-memory write buffer keyed by inode that the FUSE driver opens on create/open and releases on the matching release. While a buffer is open, writeRangeSync, truncateFileSync, and readRangeSync operate on the buffer rather than committing to vfs_chunks per syscall. Release commits the buffered bytes once and drops the entry. Bytes have one owner (DOFS), the commit boundary is per file, and intermediate write states no longer accumulate orphan blob rows. Drop the vfs_nodes.inline_data column. The buffered model removes the per-write cost that motivated inline storage in the first place, and inline contributed two storage shapes that read, write, stat, and sync all had to special-case. Without it, the orphan-vs-live blob accounting and the sync wire stay one path. Schema version goes back to 2; the v2 -> v3 migration is gone. Nothing has shipped that depends on inline_data so no data migration is needed. Tests cover buffered multi-write convergence to a single blob, buffered reads before release, truncate through the buffer, and hardlink sharing by inode. --- packages/dofs/src/fs/mount-guard.test.ts | 2 +- packages/dofs/src/fs/readFile.ts | 42 ++-- packages/dofs/src/fs/stat.ts | 12 +- packages/dofs/src/fs/writeBuffer.test.ts | 146 +++++++++++++ packages/dofs/src/fs/writeBuffer.ts | 64 ++++++ packages/dofs/src/fs/writeFile.test.ts | 19 +- packages/dofs/src/fs/writeFile.ts | 256 +++++++++++++---------- packages/dofs/src/fs/writeRange.test.ts | 21 +- packages/dofs/src/provider.ts | 27 +-- packages/dofs/src/schema/core.ts | 6 +- packages/dofs/src/schema/migrations.ts | 13 -- packages/dofs/src/sync/changes.ts | 27 +-- packages/wsd/src/cli/wsd.ts | 2 - packages/wsd/src/fuse/vfs.ts | 2 + 14 files changed, 401 insertions(+), 238 deletions(-) create mode 100644 packages/dofs/src/fs/writeBuffer.test.ts create mode 100644 packages/dofs/src/fs/writeBuffer.ts diff --git a/packages/dofs/src/fs/mount-guard.test.ts b/packages/dofs/src/fs/mount-guard.test.ts index 51c819be..03bfa575 100644 --- a/packages/dofs/src/fs/mount-guard.test.ts +++ b/packages/dofs/src/fs/mount-guard.test.ts @@ -146,7 +146,7 @@ describe("writeFile under a read-only mount", () => { // No throw; the bytes land in vfs_nodes. writeFileSync(db, "/workspace/rw/ok.txt", new TextEncoder().encode("hi"), {}, () => 0); const inode = db.scalar( - "SELECT inode FROM vfs_nodes WHERE inline_data IS NOT NULL OR manifest_hash IS NOT NULL", + "SELECT inode FROM vfs_nodes WHERE manifest_hash IS NOT NULL", ); expect(inode).toBeDefined(); }); diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index a937e0cc..fee92b4b 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -1,6 +1,7 @@ import { createWorkspaceError } from "../errors.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; +import { getWriteBuffer } from "./writeBuffer.js"; import { CHUNK_SIZE } from "./writeFile.js"; export interface ReadFileOptions { @@ -12,10 +13,6 @@ interface ChunkRow { size: number; } -interface InlineRow { - inline_data: Uint8Array | null; -} - // Overloads match docs/04_filesystem_interface.md exactly. export function readFile(db: Database, path: string): Promise>; export function readFile( @@ -50,20 +47,6 @@ export async function readFile( throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } - const inline = db.one( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data; - if (inline !== undefined && inline !== null) { - if (wantString) return new TextDecoder().decode(inline); - return new ReadableStream({ - start(controller) { - controller.enqueue(inline); - controller.close(); - }, - }); - } - const chunks = db.all( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -119,10 +102,9 @@ export async function readFile( }); } -// Positional read primitive. Slices `inline_data` for inline files and -// walks only the chunk rows that overlap [offset, offset+length) for -// chunk-backed files, so the FUSE driver can serve a kernel read -// without materializing the whole file. +// Positional read primitive. Walks only the chunk rows that overlap +// [offset, offset+length), so the FUSE driver can serve a kernel +// read without materializing the whole file. export function readRangeSync( db: Database, path: string, @@ -144,14 +126,14 @@ export function readRangeSync( } if (length === 0) return new Uint8Array(); - const inline = db.one( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data; - if (inline !== undefined && inline !== null) { - if (offset >= inline.byteLength) return new Uint8Array(); - const end = Math.min(offset + length, inline.byteLength); - return inline.subarray(offset, end); + // If a write buffer is open for this inode, it is the source of + // truth: pending writes have not yet committed to vfs_chunks. + // Reading from SQLite here would return stale bytes. + const buffered = getWriteBuffer(db, node.inode); + if (buffered !== undefined && buffered.dirty) { + if (offset >= buffered.size) return new Uint8Array(); + const end = Math.min(offset + length, buffered.size); + return buffered.buf.subarray(offset, end); } const totalSize = diff --git a/packages/dofs/src/fs/stat.ts b/packages/dofs/src/fs/stat.ts index 62748957..b228c9be 100644 --- a/packages/dofs/src/fs/stat.ts +++ b/packages/dofs/src/fs/stat.ts @@ -21,19 +21,11 @@ export function stat(db: Database, path: string): WorkspaceStatResult { const isDirectory = node.type === "dir"; const isFile = node.type === "file"; - const inlineSize = isFile - ? db.one<{ size: number | null }>( - "SELECT length(inline_data) AS size FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.size - : undefined; const size = isFile - ? (inlineSize ?? - db.scalar( + ? (db.scalar( "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", node.inode, - ) ?? - 0) + ) ?? 0) : 0; return { diff --git a/packages/dofs/src/fs/writeBuffer.test.ts b/packages/dofs/src/fs/writeBuffer.test.ts new file mode 100644 index 00000000..99863096 --- /dev/null +++ b/packages/dofs/src/fs/writeBuffer.test.ts @@ -0,0 +1,146 @@ +import { describe, expect, it } from "vitest"; + +import type { Database } from "../storage.js"; +import { readRangeSync } from "./readFile.js"; +import { resolveInode } from "./resolve.js"; +import { withDB } from "./with-db.js"; +import { + CHUNK_SIZE, + createFileSync, + openWriteBufferSync, + releaseWriteBufferSync, + truncateFileSync, + writeRangeSync, +} from "./writeFile.js"; + +function bytesOf(text: string): Uint8Array { + return new TextEncoder().encode(text); +} + +function blobCount(db: Database): number { + return db.scalar("SELECT COUNT(*) FROM vfs_blobs") ?? 0; +} + +function orphanBlobCount(db: Database): number { + return ( + db.scalar( + "SELECT COUNT(*) FROM vfs_blobs b WHERE NOT EXISTS (SELECT 1 FROM vfs_chunks c WHERE c.hash = b.hash)", + ) ?? 0 + ); +} + +function chunkCount(db: Database, path: string): number { + const node = resolveInode(db, path); + if (node === null) throw new Error(`missing node: ${path}`); + return db.scalar("SELECT COUNT(*) FROM vfs_chunks WHERE inode = ?", node.inode) ?? 0; +} + +describe("buffered write lifecycle", () => { + it("buffers many small writes and stages blobs only on release", async () => { + await withDB(async (db) => { + createFileSync(db, "/buffered.bin", {}, () => 1000); + openWriteBufferSync(db, "/buffered.bin"); + + const big = new Uint8Array(CHUNK_SIZE); + big.fill(7); + // Write the same chunk-sized payload eight times at offset 0. + // Pre-buffer, each write would stage one orphan blob per + // intermediate state. + for (let i = 0; i < 8; i++) { + writeRangeSync(db, "/buffered.bin", big, 0, {}, () => 1001 + i); + } + // While the buffer is open we keep no chunk or blob rows yet. + expect(blobCount(db)).toBe(0); + + releaseWriteBufferSync(db, "/buffered.bin", () => 1100); + + // After release: exactly one blob, no orphans, content matches. + expect(blobCount(db)).toBe(1); + expect(orphanBlobCount(db)).toBe(0); + const final = readRangeSync(db, "/buffered.bin", 0, CHUNK_SIZE); + expect(final.byteLength).toBe(CHUNK_SIZE); + expect(final[0]).toBe(7); + }); + }); + + it("serves buffered reads before release", async () => { + await withDB(async (db) => { + createFileSync(db, "/buffered.txt", {}, () => 1000); + openWriteBufferSync(db, "/buffered.txt"); + + writeRangeSync(db, "/buffered.txt", bytesOf("hello"), 0, {}, () => 1001); + // Reading through the same db sees the buffered bytes even + // though no chunk row has been written. + expect(new TextDecoder().decode(readRangeSync(db, "/buffered.txt", 0, 5))).toBe("hello"); + expect(chunkCount(db, "/buffered.txt")).toBe(0); + + releaseWriteBufferSync(db, "/buffered.txt", () => 1100); + expect(new TextDecoder().decode(readRangeSync(db, "/buffered.txt", 0, 5))).toBe("hello"); + }); + }); + + it("truncate updates the buffer instead of rewriting chunks", async () => { + await withDB(async (db) => { + createFileSync(db, "/trunc.bin", {}, () => 1000); + openWriteBufferSync(db, "/trunc.bin"); + + const payload = new Uint8Array(CHUNK_SIZE * 2); + payload.fill(1); + writeRangeSync(db, "/trunc.bin", payload, 0, {}, () => 1001); + truncateFileSync(db, "/trunc.bin", CHUNK_SIZE - 100, () => 1002); + expect(chunkCount(db, "/trunc.bin")).toBe(0); + + releaseWriteBufferSync(db, "/trunc.bin", () => 1100); + expect(chunkCount(db, "/trunc.bin")).toBe(1); + const final = readRangeSync(db, "/trunc.bin", 0, CHUNK_SIZE); + expect(final.byteLength).toBe(CHUNK_SIZE - 100); + }); + }); + + it("hardlinks share the same buffered bytes by inode", async () => { + await withDB(async (db) => { + createFileSync(db, "/a.txt", {}, () => 1000); + // Both paths point at the same inode. Open under /a.txt, then + // write under /b.txt: the buffer is keyed by inode so the write + // lands in the same cache entry. + const { link } = await import("./link.js"); + link(db, "/a.txt", "/b.txt"); + openWriteBufferSync(db, "/a.txt"); + + // Multiple intermediate writes through both paths. Pre-buffer + // each one would have staged its own blob and orphaned the + // previous state; the buffer keeps them in memory until release. + writeRangeSync(db, "/b.txt", bytesOf("step-1"), 0, {}, () => 1001); + writeRangeSync(db, "/a.txt", bytesOf("step-2"), 0, {}, () => 1002); + writeRangeSync(db, "/b.txt", bytesOf("shared"), 0, {}, () => 1003); + expect(new TextDecoder().decode(readRangeSync(db, "/a.txt", 0, 6))).toBe("shared"); + expect(blobCount(db)).toBe(0); + + releaseWriteBufferSync(db, "/a.txt", () => 1100); + expect(new TextDecoder().decode(readRangeSync(db, "/b.txt", 0, 6))).toBe("shared"); + // Exactly one blob for the final state, regardless of how many + // intermediate writes the open window saw. + expect(blobCount(db)).toBe(1); + }); + }); + + it("commits a chunked file with one blob per chunk on release", async () => { + await withDB(async (db) => { + createFileSync(db, "/big.bin", {}, () => 1000); + openWriteBufferSync(db, "/big.bin"); + + // Three chunks of distinct content. Pre-buffer, each write + // would create the chunk row eagerly and a partial-tail write + // would create an orphan blob for the previous tail size. + const payload = new Uint8Array(CHUNK_SIZE * 3); + payload.fill(1, 0, CHUNK_SIZE); + payload.fill(2, CHUNK_SIZE, CHUNK_SIZE * 2); + payload.fill(3, CHUNK_SIZE * 2); + writeRangeSync(db, "/big.bin", payload, 0, {}, () => 1001); + + releaseWriteBufferSync(db, "/big.bin", () => 1100); + expect(chunkCount(db, "/big.bin")).toBe(3); + expect(orphanBlobCount(db)).toBe(0); + }); + }); +}); diff --git a/packages/dofs/src/fs/writeBuffer.ts b/packages/dofs/src/fs/writeBuffer.ts new file mode 100644 index 00000000..102c4036 --- /dev/null +++ b/packages/dofs/src/fs/writeBuffer.ts @@ -0,0 +1,64 @@ +// In-process write buffer cache. +// +// Holds per-inode mutable byte buffers between an explicit open and +// release. While a buffer is open, all reads and writes for that +// inode go through the buffer rather than the SQLite blob/chunk +// store. Release commits the bytes to chunks/inline once per file +// and evicts the entry, so per-syscall writes no longer accumulate +// orphan blob rows in the store. +// +// The cache is keyed by Database so a fresh database (a test, a +// rebooted DO incarnation) starts with an empty cache. + +import type { Database } from "../storage.js"; + +export interface WriteBufferEntry { + // Growable backing store. byteLength is capacity; logical length + // lives in `size`. + buf: Uint8Array; + // Logical end-of-file in `buf`. + size: number; + // True once writeRange/truncate mutates the buffer. A non-dirty + // buffer is one that the caller opened but never wrote to; release + // is a no-op in that case so we do not touch the existing chunks. + dirty: boolean; + // Open handle count. Each FUSE open/create increments this; each + // release decrements. The buffer commits and evicts when the count + // reaches zero. + openCount: number; + // Mode the caller wants persisted on release. Defaults to the + // inode's existing mode at open time when the caller has none. + mode: number; +} + +const caches = new WeakMap>(); + +function cacheFor(db: Database): Map { + let cache = caches.get(db); + if (cache === undefined) { + cache = new Map(); + caches.set(db, cache); + } + return cache; +} + +export function getWriteBuffer(db: Database, inode: number): WriteBufferEntry | undefined { + return caches.get(db)?.get(inode); +} + +export function setWriteBuffer(db: Database, inode: number, entry: WriteBufferEntry): void { + cacheFor(db).set(inode, entry); +} + +export function deleteWriteBuffer(db: Database, inode: number): void { + caches.get(db)?.delete(inode); +} + +export function ensureCapacity(entry: WriteBufferEntry, needed: number): void { + if (entry.buf.byteLength >= needed) return; + let cap = Math.max(entry.buf.byteLength * 2, 64 * 1024); + while (cap < needed) cap *= 2; + const next = new Uint8Array(cap); + next.set(entry.buf.subarray(0, entry.size), 0); + entry.buf = next; +} diff --git a/packages/dofs/src/fs/writeFile.test.ts b/packages/dofs/src/fs/writeFile.test.ts index 2b6b0c08..8bbd7062 100644 --- a/packages/dofs/src/fs/writeFile.test.ts +++ b/packages/dofs/src/fs/writeFile.test.ts @@ -14,11 +14,6 @@ function readBack(db: Database, path: string): Uint8Array { const node = resolveInode(db, path); if (node === null) throw new Error(`no such path: ${path}`); if (node.type !== "file") throw new Error(`not a file: ${path}`); - const inline = db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data; - if (inline !== undefined && inline !== null) return inline; const chunks = db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -86,25 +81,19 @@ describe("writeFile", () => { }); }); - it("writeFileSync stores a small string inline without chunk rows", async () => { + it("writeFileSync stores small strings as a single chunk row", async () => { await withDB(async (db) => { writeFileSync(db, "/hello.txt", new TextEncoder().encode("hello fuse"), {}, () => 1234); const bytes = readBack(db, "/hello.txt"); expect(new TextDecoder().decode(bytes)).toBe("hello fuse"); - const row = db.one<{ inline_data: Uint8Array | null; chunk_count: number }>( - `SELECT n.inline_data AS inline_data, - (SELECT COUNT(*) FROM vfs_chunks WHERE inode = n.inode) AS chunk_count - FROM vfs_nodes n - JOIN vfs_dirents d ON d.child_inode = n.inode - WHERE d.parent_inode = ? AND d.name = ?`, + const chunkCount = db.scalar( + "SELECT COUNT(*) FROM vfs_chunks WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?)", ROOT_INODE, "hello.txt", ); - expect(row?.inline_data).toBeInstanceOf(Uint8Array); - expect(new TextDecoder().decode(row?.inline_data ?? new Uint8Array())).toBe("hello fuse"); - expect(row?.chunk_count).toBe(0); + expect(chunkCount).toBe(1); }); }); diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 12d83eda..4b436ac3 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -7,11 +7,17 @@ import type { Database } from "../storage.js"; import { stageBlob } from "../sync/blobs.js"; import { buildManifest } from "../sync/manifests.js"; import { assertNotReadOnly } from "./mount-guard.js"; +import { + deleteWriteBuffer, + ensureCapacity as ensureBufferCapacity, + getWriteBuffer, + setWriteBuffer, + type WriteBufferEntry, +} from "./writeBuffer.js"; // Fixed chunk size. Exported so tests can size inputs precisely // without hard-coding the magic number twice. export const CHUNK_SIZE = 512 * 1024; -export const INLINE_FILE_MAX_BYTES = 16 * 1024; export type WriteFileContent = string | Uint8Array | ReadableStream; @@ -111,7 +117,7 @@ export async function writeFile( return; } const bytes = await materialize(content); - writeFileSync(db, path, bytes, options, now, false); + writeFileSync(db, path, bytes, options, now); } // Streaming write path. Reads the source one source-chunk at a time, @@ -317,29 +323,13 @@ function existingChunkRefs(db: Database, inode: number): ChunkRef[] { return db.all("SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", inode); } -function inlineDataForInode(db: Database, inode: number): Uint8Array | null { - return ( - db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - inode, - )?.inline_data ?? null - ); -} - function fileSizeForInode(db: Database, inode: number): number { - const inline = inlineDataForInode(db, inode); - if (inline !== null) return inline.byteLength; return ( db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 ); } function readChunkBytes(db: Database, inode: number, idx: number): Uint8Array { - const inline = inlineDataForInode(db, inode); - if (inline !== null) { - const start = idx * CHUNK_SIZE; - return inline.subarray(start, Math.min(start + CHUNK_SIZE, inline.byteLength)); - } const chunk = db.one<{ hash: Uint8Array }>( "SELECT hash FROM vfs_chunks WHERE inode = ? AND idx = ?", inode, @@ -356,19 +346,6 @@ function readChunkBytes(db: Database, inode: number, idx: number): Uint8Array { return row.bytes; } -function materializePrefix(db: Database, inode: number, size: number): Uint8Array { - const out = new Uint8Array(size); - let copied = 0; - for (let idx = 0; copied < size; idx++) { - const chunk = readChunkBytes(db, inode, idx); - if (chunk.byteLength > 0) { - out.set(chunk.subarray(0, Math.min(chunk.byteLength, size - copied)), copied); - } - copied += Math.min(CHUNK_SIZE, size - copied); - } - return out; -} - function resolveFileInode(db: Database, path: string): { inode: number; mode: number } { const { path: canonical } = canonicalizePath(path); const node = db.one<{ inode: number; type: "file" | "dir"; mode: number }>( @@ -398,28 +375,6 @@ function parentAndNameForResolvedPath(db: Database, path: string): [number, stri return [resolveParent(db, parts, canonical), parts[parts.length - 1]]; } -function writeInlineInode( - db: Database, - inode: number, - bytes: Uint8Array, - mode: number, - mtime: number, -): void { - db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); - if (bytes.byteLength > 0) { - stageBlob(db, sha256(bytes), bytes, mtime); - } - const rev = incrementRev(db); - db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", - mode, - mtime, - rev, - bytes, - inode, - ); -} - // Update an inode's chunk-backed representation in place. Iterates over // the full chunk grid but only touches `vfs_chunks` rows whose contents // or size actually changed, so untouched chunk rows keep their @@ -435,7 +390,6 @@ function applyChunkedInodeUpdate( buildChunkBytes: (idx: number, start: number, end: number, existing: Uint8Array) => Uint8Array, ): void { const oldChunks = existingChunkRefs(db, inode); - const oldInline = inlineDataForInode(db, inode); const chunkCount = Math.ceil(size / CHUNK_SIZE); const oldChunkCount = oldChunks.length; @@ -450,12 +404,7 @@ function applyChunkedInodeUpdate( // its rowid stays put. if (old !== undefined && old.size === intendedSize && !touched) continue; - const existingBytes = - oldInline !== null - ? oldInline.subarray(start, Math.min(start + CHUNK_SIZE, oldInline.byteLength)) - : old !== undefined - ? readChunkBytes(db, inode, idx) - : new Uint8Array(); + const existingBytes = old !== undefined ? readChunkBytes(db, inode, idx) : new Uint8Array(); const chunkBytes = buildChunkBytes(idx, start, end, existingBytes); if (chunkBytes.byteLength !== intendedSize) { throw createWorkspaceError("EIO", "chunk builder returned wrong size"); @@ -478,7 +427,7 @@ function applyChunkedInodeUpdate( const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", mode, mtime, rev, @@ -508,10 +457,9 @@ export function createFileSync( throw createWorkspaceError("EEXIST", `path exists: ${canonical}`, canonical); } db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev, manifest_hash, inline_data) VALUES ('file', ?, ?, 0, NULL, ?)", + "INSERT INTO vfs_nodes (type, mode, mtime, rev, manifest_hash) VALUES ('file', ?, ?, 0, NULL)", mode, mtime, - new Uint8Array(), ); const inode = db.scalar("SELECT last_insert_rowid()"); if (inode === undefined) throw createWorkspaceError("EIO", "failed to allocate inode"); @@ -526,6 +474,97 @@ export function createFileSync( }); } +// Open a write buffer for an existing file. Subsequent writes, +// truncates, and reads against the same Database operate on the +// buffer instead of the SQLite chunk/blob store. Release commits +// the bytes back to chunks/inline. +export function openWriteBufferSync(db: Database, path: string): void { + const { inode, mode } = resolveFileInode(db, path); + const existing = getWriteBuffer(db, inode); + if (existing !== undefined) { + existing.openCount += 1; + return; + } + setWriteBuffer(db, inode, { + buf: new Uint8Array(0), + size: 0, + dirty: false, + openCount: 1, + mode, + }); +} + +// Release one open of an inode's write buffer. When the open count +// reaches zero, commit the buffered bytes to chunk rows and drop +// the entry. The committed mode is the buffer's mode at release +// time so an intermediate chmod survives. +export function releaseWriteBufferSync(db: Database, path: string, now: () => number): void { + const node = resolveFileInode(db, path); + const entry = getWriteBuffer(db, node.inode); + if (entry === undefined) return; + entry.openCount -= 1; + if (entry.openCount > 0) return; + + if (!entry.dirty) { + deleteWriteBuffer(db, node.inode); + return; + } + + const mtime = now(); + const mode = entry.mode & 0o7777; + const buffered = entry.buf.subarray(0, entry.size); + + db.transactionSync(() => { + if (entry.size === 0) { + // An empty file owns no chunk rows; clear any old ones the + // buffer would otherwise have replaced and bump metadata. + db.run("DELETE FROM vfs_chunks WHERE inode = ?", node.inode); + const rev = incrementRev(db); + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", + mode, + mtime, + rev, + node.inode, + ); + return; + } + applyChunkedInodeUpdate( + db, + node.inode, + entry.size, + mode, + mtime, + (_idx, start, end) => start < entry.size && end > 0, + (_idx, start, end) => buffered.subarray(start, Math.min(end, entry.size)), + ); + }); + + deleteWriteBuffer(db, node.inode); +} + +// Hydrate a freshly-opened buffer with the inode's current bytes +// the first time we mutate it. Avoids paying the read cost when the +// caller opens a file just to truncate or overwrite it. +function hydrateBufferIfNeeded(db: Database, inode: number, entry: WriteBufferEntry): void { + if (entry.dirty) return; + const existingSize = fileSizeForInode(db, inode); + if (existingSize === 0) { + entry.dirty = true; + return; + } + ensureBufferCapacity(entry, existingSize); + let copied = 0; + for (let idx = 0; copied < existingSize; idx++) { + const chunk = readChunkBytes(db, inode, idx); + if (chunk.byteLength === 0) break; + entry.buf.set(chunk, copied); + copied += chunk.byteLength; + } + entry.size = existingSize; + entry.dirty = true; +} + export function writeRangeSync( db: Database, path: string, @@ -542,20 +581,32 @@ export function writeRangeSync( if (bytes.byteLength === 0) return 0; const mtime = now(); + const { inode, mode: existingMode } = resolveFileInode(db, path); + const mode = (options.mode ?? existingMode) & 0o7777; + const buffered = getWriteBuffer(db, inode); + + // Buffered path: mutate the in-memory bytes and defer storage + // writes until release. Reads through the same Database see the + // buffer's current bytes via readRangeSync's buffer check. + if (buffered !== undefined) { + hydrateBufferIfNeeded(db, inode, buffered); + const writeEnd = offset + bytes.byteLength; + ensureBufferCapacity(buffered, writeEnd); + if (offset > buffered.size) { + buffered.buf.fill(0, buffered.size, offset); + } + buffered.buf.set(bytes, offset); + if (writeEnd > buffered.size) buffered.size = writeEnd; + buffered.mode = mode; + buffered.dirty = true; + return bytes.byteLength; + } + db.transactionSync(() => { - const { inode, mode: existingMode } = resolveFileInode(db, path); - const mode = (options.mode ?? existingMode) & 0o7777; const oldSize = fileSizeForInode(db, inode); const writeEnd = offset + bytes.byteLength; const nextSize = Math.max(oldSize, writeEnd); - if (nextSize <= INLINE_FILE_MAX_BYTES) { - const next = materializePrefix(db, inode, nextSize); - next.set(bytes, offset); - writeInlineInode(db, inode, next, mode, mtime); - return; - } - applyChunkedInodeUpdate( db, inode, @@ -592,14 +643,34 @@ export function truncateFileSync( } const mtime = now(); + const { inode, mode } = resolveFileInode(db, path); + const buffered = getWriteBuffer(db, inode); + + if (buffered !== undefined) { + hydrateBufferIfNeeded(db, inode, buffered); + if (size > buffered.size) { + ensureBufferCapacity(buffered, size); + buffered.buf.fill(0, buffered.size, size); + } + buffered.size = size; + buffered.dirty = true; + return; + } + db.transactionSync(() => { - const { inode, mode } = resolveFileInode(db, path); const oldSize = fileSizeForInode(db, inode); if (oldSize === size) return; - if (size <= INLINE_FILE_MAX_BYTES) { - const next = materializePrefix(db, inode, size); - writeInlineInode(db, inode, next, mode, mtime); + if (size === 0) { + db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); + const rev = incrementRev(db); + db.run( + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", + mode, + mtime, + rev, + inode, + ); return; } @@ -628,7 +699,6 @@ export function writeFileSync( bytes: Uint8Array, options: WriteFileOptions, now: () => number, - inlineAllowed = true, ): void { const { parts, path: canonical } = canonicalizePath(path); if (parts.length === 0) { @@ -637,7 +707,6 @@ export function writeFileSync( assertNotReadOnly(db, canonical); const mode = (options.mode ?? 0o644) & 0o7777; const mtime = now(); - const inline = inlineAllowed && bytes.byteLength <= INLINE_FILE_MAX_BYTES; db.transactionSync(() => { const parentInode = resolveParent(db, parts, canonical); @@ -681,18 +750,6 @@ export function writeFileSync( } const rev = incrementRev(db); - if (inline) { - db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", - mode, - mtime, - rev, - bytes, - inode, - ); - return; - } - const chunks = chunksOf(bytes); // Upsert blobs and write the new chunk list. for (let idx = 0; idx < chunks.length; idx++) { @@ -709,7 +766,7 @@ export function writeFileSync( const manifestHash = buildManifest(db, chunks, mtime); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", mode, mtime, rev, @@ -735,8 +792,6 @@ export function writeFileRangesSync( const mode = (options.mode ?? 0o644) & 0o7777; const ranges = normalizeRanges(dirtyRanges, bytes.byteLength); const mtime = now(); - const inline = bytes.byteLength <= INLINE_FILE_MAX_BYTES; - db.transactionSync(() => { const parentInode = resolveParent(db, parts, canonical); const leafName = parts[parts.length - 1]; @@ -778,19 +833,6 @@ export function writeFileRangesSync( } const rev = incrementRev(db); - if (inline) { - db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); - db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL, inline_data = ? WHERE inode = ?", - mode, - mtime, - rev, - bytes, - inode, - ); - return; - } - const nextChunks: ChunkRef[] = []; const chunkCount = Math.ceil(bytes.byteLength / CHUNK_SIZE); for (let idx = 0; idx < chunkCount; idx++) { @@ -813,7 +855,7 @@ export function writeFileRangesSync( const manifestHash = replaceChunkRows(db, inode, nextChunks, mtime); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ?, inline_data = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", mode, mtime, rev, diff --git a/packages/dofs/src/fs/writeRange.test.ts b/packages/dofs/src/fs/writeRange.test.ts index 82543f19..bef6cd7e 100644 --- a/packages/dofs/src/fs/writeRange.test.ts +++ b/packages/dofs/src/fs/writeRange.test.ts @@ -70,31 +70,19 @@ function chunkRows( ); } -function inlineData(db: Database, path: string): Uint8Array | null { - const node = resolveInode(db, path); - if (node === null) throw new Error(`missing node: ${path}`); - return ( - db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data ?? null - ); -} - describe("direct range writes", () => { - it("creates an empty inline file", async () => { + it("creates an empty file with no chunk rows", async () => { await withDB(async (db) => { createFileSync(db, "/empty.txt", { mode: 0o600 }, () => 1000); const node = resolveInode(db, "/empty.txt"); expect(node?.type).toBe("file"); expect(node?.mode).toBe(0o600); - expect(inlineData(db, "/empty.txt")).toEqual(new Uint8Array()); expect(chunkRows(db, "/empty.txt")).toEqual([]); }); }); - it("writes small ranges into inline_data", async () => { + it("writes small ranges and stores them as a single chunk", async () => { await withDB(async (db) => { createFileSync(db, "/small.txt", {}, () => 1000); @@ -102,10 +90,7 @@ describe("direct range writes", () => { expect(writeRangeSync(db, "/small.txt", bytesOf("y"), 4, {}, () => 1002)).toBe(1); expect(new TextDecoder().decode(await readBytes(db, "/small.txt"))).toBe("helly"); - expect(new TextDecoder().decode(inlineData(db, "/small.txt") ?? new Uint8Array())).toBe( - "helly", - ); - expect(chunkRows(db, "/small.txt")).toEqual([]); + expect(chunkRows(db, "/small.txt")).toHaveLength(1); }); }); diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 757b306b..459d0167 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -27,6 +27,8 @@ import { } from "./fs/watch.js"; import { createFileSync as createFileSyncImpl, + openWriteBufferSync as openWriteBufferSyncImpl, + releaseWriteBufferSync as releaseWriteBufferSyncImpl, truncateFileSync as truncateFileSyncImpl, type WriteFileRange, writeFileRangesSync as writeFileRangesSyncImpl, @@ -378,16 +380,7 @@ export class SQLiteWorkspaceProvider { if (node.type !== "file") { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } - const inline = this.db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", - node.inode, - )?.inline_data; const encoding = typeof options === "string" ? options : options?.encoding; - if (inline !== undefined && inline !== null) { - const out = Buffer.from(inline); - return encoding ? out.toString(encoding) : out; - } - const chunks = this.db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -468,6 +461,14 @@ export class SQLiteWorkspaceProvider { truncateFileSyncImpl(this.db, path, len, this.now); } + openWriteBufferSync(path: string): void { + openWriteBufferSyncImpl(this.db, path); + } + + releaseWriteBufferSync(path: string): void { + releaseWriteBufferSyncImpl(this.db, path, this.now); + } + chmodSync(path: string, mode: number): void { const node = resolveInode(this.db, path, { followSymlinks: false }); if (node === null) { @@ -744,14 +745,8 @@ function linkCount(db: Database, inode: number): number { } function fileSize(db: Database, inode: number): number { - const inlineSize = db.one<{ size: number | null }>( - "SELECT length(inline_data) AS size FROM vfs_nodes WHERE inode = ?", - inode, - )?.size; return ( - inlineSize ?? - db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? - 0 + db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 ); } diff --git a/packages/dofs/src/schema/core.ts b/packages/dofs/src/schema/core.ts index 2dc2d6d6..fe36e1f7 100644 --- a/packages/dofs/src/schema/core.ts +++ b/packages/dofs/src/schema/core.ts @@ -1,10 +1,9 @@ // Filesystem-side tables. These hold the inode graph and the // content-addressed blob store. See docs/03_filesystem_schema.md. -// Bumped to 3 when `vfs_nodes.inline_data` landed for tiny files. // See `schema/migrations.ts` for the migration list; `sync.ts` // carries the fresh-install DDL. -export const SCHEMA_VERSION = 3; +export const SCHEMA_VERSION = 2; export const ROOT_INODE = 1; export const CORE_STATEMENTS = [ @@ -21,8 +20,7 @@ export const CORE_STATEMENTS = [ mount_root TEXT, stub_size INTEGER, manifest_hash BLOB, - link_target TEXT, - inline_data BLOB + link_target TEXT )`, `CREATE TABLE IF NOT EXISTS vfs_dirents ( parent_inode INTEGER NOT NULL, diff --git a/packages/dofs/src/schema/migrations.ts b/packages/dofs/src/schema/migrations.ts index f6676371..10d1f3d4 100644 --- a/packages/dofs/src/schema/migrations.ts +++ b/packages/dofs/src/schema/migrations.ts @@ -40,21 +40,8 @@ function v1_to_v2_add_mounts_mode(db: Database): void { ); } -// v2 → v3 — add inline_data for tiny regular-file payloads. Existing -// files keep their chunk rows; only subsequent small writes use the -// inline path. -function v2_to_v3_add_inline_data(db: Database): void { - const hasColumn = db - .all<{ name: string }>("PRAGMA table_info(vfs_nodes)") - .some((column) => column.name === "inline_data"); - if (!hasColumn) { - db.run("ALTER TABLE vfs_nodes ADD COLUMN inline_data BLOB"); - } -} - export const MIGRATIONS: readonly Migration[] = [ { from: 1, to: 2, migrator: v1_to_v2_add_mounts_mode }, - { from: 2, to: 3, migrator: v2_to_v3_add_inline_data }, ] as const; // Apply every migration whose `from` matches the current version, diff --git a/packages/dofs/src/sync/changes.ts b/packages/dofs/src/sync/changes.ts index ced4be28..2cef2326 100644 --- a/packages/dofs/src/sync/changes.ts +++ b/packages/dofs/src/sync/changes.ts @@ -1,4 +1,3 @@ -import { createHash } from "node:crypto"; import { resolveInode } from "../fs/resolve.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; @@ -21,12 +20,6 @@ export function recordDelete(db: Database, rev: number, path: string): void { // tombstones. The puller uses it as a per-entry cursor so it can // advance fetchRev per committed batch instead of waiting for the // whole stream to drain. -function sha256(bytes: Uint8Array): Uint8Array { - const hash = createHash("sha256"); - hash.update(bytes); - return new Uint8Array(hash.digest()); -} - export type ChangeEntry = | { kind: "file"; @@ -80,22 +73,12 @@ export function materialiseChange(db: Database, path: string): ChangeEntry | nul } // file: collect chunk rows in index order. Each row carries hash // and size so the receiver can probe hasObjects without a - // separate manifest lookup. Inline files do not have chunk rows; - // synthesize the single wire chunk from inline_data so sync still - // ships their bytes. Empty files produce zero rows and size 0. - const inline = db.one<{ inline_data: Uint8Array | null }>( - "SELECT inline_data FROM vfs_nodes WHERE inode = ?", + // separate manifest lookup. An empty file has zero chunk rows + // and reports size 0. + const chunks = db.all<{ hash: Uint8Array; size: number }>( + "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", live.inode, - )?.inline_data; - const chunks = - inline !== undefined && inline !== null - ? inline.byteLength === 0 - ? [] - : [{ hash: sha256(inline), size: inline.byteLength }] - : db.all<{ hash: Uint8Array; size: number }>( - "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", - live.inode, - ); + ); let size = 0; for (const c of chunks) size += c.size; return { diff --git a/packages/wsd/src/cli/wsd.ts b/packages/wsd/src/cli/wsd.ts index 6b577cdf..7ef1c7ad 100644 --- a/packages/wsd/src/cli/wsd.ts +++ b/packages/wsd/src/cli/wsd.ts @@ -126,8 +126,6 @@ function collectDbStats(db: Database): Record { db.scalar( "SELECT COALESCE(SUM(LENGTH(bytes)), 0) FROM vfs_blob_bytes bb WHERE NOT EXISTS (SELECT 1 FROM vfs_chunks c WHERE c.hash = bb.hash)", ) ?? 0; - out.vfs_inline_bytes_total = - db.scalar("SELECT COALESCE(SUM(LENGTH(inline_data)), 0) FROM vfs_nodes") ?? 0; } catch (error) { out.error = (error as Error).message; } diff --git a/packages/wsd/src/fuse/vfs.ts b/packages/wsd/src/fuse/vfs.ts index 1fa3fedf..4114cecf 100644 --- a/packages/wsd/src/fuse/vfs.ts +++ b/packages/wsd/src/fuse/vfs.ts @@ -46,6 +46,8 @@ const EXTRA_VFS_METHODS = [ "truncateFileSync", "chmodSync", "readRangeSync", + "openWriteBufferSync", + "releaseWriteBufferSync", ] as const; export interface CreateOptions { From 8745502313e60e4cf9c1617f514232761a554a0e Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 10:38:07 +0000 Subject: [PATCH 18/31] wsd: route FUSE writes through the DOFS write buffer Open a DOFS write buffer on FUSE create and open, release it on the matching FUSE release. While the buffer is open, FUSE writes, truncates, and reads operate on the buffer; release commits the final bytes to vfs_chunks in one shot. Also teach the synchronous read paths (provider.readFileSync, fs/readFile streaming, stat, provider.fileSize) to consult the buffer when one is open. Without that, an RPC or test reading through the VFS while a FUSE file is still being written would see stale chunk-store bytes. --- packages/dofs/src/fs/readFile.ts | 15 +++++++++++++++ packages/dofs/src/fs/stat.ts | 20 ++++++++++++++------ packages/dofs/src/provider.ts | 13 +++++++++++++ packages/wsd/src/fuse/driver.ts | 22 +++++++++++++++++++++- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index fee92b4b..9b393120 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -47,6 +47,21 @@ export async function readFile( throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } + // While a write buffer is open for this inode it is the source of + // truth. Skip the chunk store and serve the buffered bytes. + const buffered = getWriteBuffer(db, node.inode); + if (buffered !== undefined && buffered.dirty) { + const snapshot = new Uint8Array(buffered.size); + snapshot.set(buffered.buf.subarray(0, buffered.size)); + if (wantString) return new TextDecoder().decode(snapshot); + return new ReadableStream({ + start(controller) { + controller.enqueue(snapshot); + controller.close(); + }, + }); + } + const chunks = db.all( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, diff --git a/packages/dofs/src/fs/stat.ts b/packages/dofs/src/fs/stat.ts index b228c9be..1387ab66 100644 --- a/packages/dofs/src/fs/stat.ts +++ b/packages/dofs/src/fs/stat.ts @@ -2,6 +2,7 @@ import { createWorkspaceError } from "../errors.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; +import { getWriteBuffer } from "./writeBuffer.js"; export interface WorkspaceStatResult { name: string; @@ -21,12 +22,19 @@ export function stat(db: Database, path: string): WorkspaceStatResult { const isDirectory = node.type === "dir"; const isFile = node.type === "file"; - const size = isFile - ? (db.scalar( - "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", - node.inode, - ) ?? 0) - : 0; + let size = 0; + if (isFile) { + const buffered = getWriteBuffer(db, node.inode); + if (buffered !== undefined && buffered.dirty) { + size = buffered.size; + } else { + size = + db.scalar( + "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", + node.inode, + ) ?? 0; + } + } return { name, diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 459d0167..84fb4b51 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -25,6 +25,7 @@ import { type WatchHandle, type WatchOptions, } from "./fs/watch.js"; +import { getWriteBuffer } from "./fs/writeBuffer.js"; import { createFileSync as createFileSyncImpl, openWriteBufferSync as openWriteBufferSyncImpl, @@ -381,6 +382,14 @@ export class SQLiteWorkspaceProvider { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } const encoding = typeof options === "string" ? options : options?.encoding; + // While a buffer is open for this inode it owns the latest + // bytes; serve from it instead of the chunk store. + const buffered = getWriteBuffer(this.db, node.inode); + if (buffered !== undefined && buffered.dirty) { + const snapshot = Buffer.alloc(buffered.size); + snapshot.set(buffered.buf.subarray(0, buffered.size)); + return encoding ? snapshot.toString(encoding) : snapshot; + } const chunks = this.db.all<{ hash: Uint8Array; size: number }>( "SELECT hash, size FROM vfs_chunks WHERE inode = ? ORDER BY idx", node.inode, @@ -745,6 +754,10 @@ function linkCount(db: Database, inode: number): number { } function fileSize(db: Database, inode: number): number { + const buffered = getWriteBuffer(db, inode); + if (buffered !== undefined && buffered.dirty) { + return buffered.size; + } return ( db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 ); diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 89c0c355..194b961c 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -237,11 +237,17 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO Partial & { chmodSync?: (path: string, mode: number) => void; readRangeSync?: (path: string, offset: number, length: number) => Uint8Array; + openWriteBufferSync?: (path: string) => void; + releaseWriteBufferSync?: (path: string) => void; }; const hasDirectWrites = directWriteVfs.createFileSync !== undefined && directWriteVfs.writeRangeSync !== undefined && directWriteVfs.truncateFileSync !== undefined; + const hasBufferedWrites = + hasDirectWrites && + directWriteVfs.openWriteBufferSync !== undefined && + directWriteVfs.releaseWriteBufferSync !== undefined; const linkableVfs = vfs as NodeVirtualFileSystem & { linkSync?: (existingPath: string, newPath: string) => void; }; @@ -429,7 +435,9 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb(ERRNO.EISDIR, 0); return; } - + if (hasBufferedWrites) { + directWriteVfs.openWriteBufferSync?.(toVfs(path)); + } cb(0, openFileHandle(path)); } catch (error) { cb(toErrno(error), 0); @@ -458,6 +466,9 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO } if (hasDirectWrites) { directWriteVfs.createFileSync?.(toVfs(path), { mode }); + if (hasBufferedWrites) { + directWriteVfs.openWriteBufferSync?.(toVfs(path)); + } cb(0, openFileHandle(path)); return; } @@ -594,6 +605,15 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO release(path, fh, cb) { handles.delete(fh); releaseFileHandle(path); + if (hasBufferedWrites) { + try { + directWriteVfs.releaseWriteBufferSync?.(toVfs(path)); + cb(0); + } catch (error) { + cb(toErrno(error)); + } + return; + } // Last chance to make the buffered writes durable in the // VFS — the kernel won't call write() again on this fh. // Multi-open is fine: the next release on a different fh From ebdf15761f943164b0e0328fea89be71ee9ee611 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 11:32:03 +0000 Subject: [PATCH 19/31] dofs: fold rowid and rev reads into INSERT/UPDATE via RETURNING Every FS mutation pays the rev counter; tiny-file create also reads back last_insert_rowid. SQLite's RETURNING lets us fold both reads into the same statement, cutting one round-trip off each. incrementRev now does a single UPDATE ... RETURNING v instead of an UPDATE followed by a SELECT. The new file/dir/symlink inode inserts do INSERT ... RETURNING inode, so the bare last_insert_rowid lookup goes away. createFileSync also reorders so rev is computed up front and the node row lands with its final stamp in one INSERT, removing the post-insert UPDATE entirely. Cuts createFileSync from 7 SQL statements down to 5 per file (the existing-file overwrite branch already had the optimal shape). --- packages/dofs/src/fs/mkdir.ts | 9 +++-- packages/dofs/src/fs/symlink.ts | 9 +++-- packages/dofs/src/fs/writeFile.ts | 64 +++++++++++++------------------ packages/dofs/src/rev.ts | 11 ++++-- 4 files changed, 44 insertions(+), 49 deletions(-) diff --git a/packages/dofs/src/fs/mkdir.ts b/packages/dofs/src/fs/mkdir.ts index de2dde37..f092a18f 100644 --- a/packages/dofs/src/fs/mkdir.ts +++ b/packages/dofs/src/fs/mkdir.ts @@ -46,16 +46,17 @@ function createDir( mtime: number, rev: number, ): number { - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('dir', ?, ?, ?)", + // RETURNING folds the rowid read into the INSERT. + const row = db.one<{ inode: number }>( + "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('dir', ?, ?, ?) RETURNING inode", mode, mtime, rev, ); - const inode = db.scalar("SELECT last_insert_rowid()"); - if (inode === undefined) { + if (row === undefined) { throw createWorkspaceError("EIO", "failed to allocate inode"); } + const inode = row.inode; db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, diff --git a/packages/dofs/src/fs/symlink.ts b/packages/dofs/src/fs/symlink.ts index e34e6935..316c51d8 100644 --- a/packages/dofs/src/fs/symlink.ts +++ b/packages/dofs/src/fs/symlink.ts @@ -52,17 +52,18 @@ export function symlink(db: Database, target: string, path: string, now: () => n const rev = incrementRev(db); const mtime = now(); - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev, link_target) VALUES ('symlink', ?, ?, ?, ?)", + // RETURNING folds the rowid read into the INSERT. + const row = db.one<{ inode: number }>( + "INSERT INTO vfs_nodes (type, mode, mtime, rev, link_target) VALUES ('symlink', ?, ?, ?, ?) RETURNING inode", 0o777, mtime, rev, target, ); - const inode = db.scalar("SELECT last_insert_rowid()"); - if (inode === undefined) { + if (row === undefined) { throw createWorkspaceError("EIO", "failed to allocate inode"); } + const inode = row.inode; db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 4b436ac3..58b6163b 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -217,16 +217,7 @@ async function writeFileStreaming( inode = existing.child_inode; db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); } else { - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('file', ?, ?, 0)", - mode, - mtime, - ); - const allocated = db.scalar("SELECT last_insert_rowid()"); - if (allocated === undefined) { - throw createWorkspaceError("EIO", "failed to allocate inode"); - } - inode = allocated; + inode = insertFileNode(db, mode, mtime); db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, @@ -257,6 +248,21 @@ async function writeFileStreaming( }); } +// Allocate a fresh file inode row with the supplied mode and mtime, +// using SQLite's RETURNING so the new rowid comes back in the same +// statement instead of through a follow-up SELECT last_insert_rowid(). +function insertFileNode(db: Database, mode: number, mtime: number): number { + const row = db.one<{ inode: number }>( + "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('file', ?, ?, 0) RETURNING inode", + mode, + mtime, + ); + if (row === undefined) { + throw createWorkspaceError("EIO", "failed to allocate inode"); + } + return row.inode; +} + function upsertChunkBlob(db: Database, chunk: PreparedChunk, lastSeen: number): void { db.run( "INSERT INTO vfs_blobs (hash, size, last_seen) VALUES (?, ?, ?) ON CONFLICT(hash) DO UPDATE SET last_seen = excluded.last_seen", @@ -456,21 +462,23 @@ export function createFileSync( if (existing !== undefined) { throw createWorkspaceError("EEXIST", `path exists: ${canonical}`, canonical); } - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev, manifest_hash) VALUES ('file', ?, ?, 0, NULL)", + const rev = incrementRev(db); + // INSERT with RETURNING folds the last_insert_rowid lookup into + // the same statement, and computing rev up front lets us write + // the node row with its final stamp in one shot. + const row = db.one<{ inode: number }>( + "INSERT INTO vfs_nodes (type, mode, mtime, rev, manifest_hash) VALUES ('file', ?, ?, ?, NULL) RETURNING inode", mode, mtime, + rev, ); - const inode = db.scalar("SELECT last_insert_rowid()"); - if (inode === undefined) throw createWorkspaceError("EIO", "failed to allocate inode"); + if (row === undefined) throw createWorkspaceError("EIO", "failed to allocate inode"); db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, leafName, - inode, + row.inode, ); - const rev = incrementRev(db); - db.run("UPDATE vfs_nodes SET rev = ? WHERE inode = ?", rev, inode); }); } @@ -731,16 +739,7 @@ export function writeFileSync( // are cleaned up by a later gc() pass. db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); } else { - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('file', ?, ?, 0)", - mode, - mtime, - ); - const allocated = db.scalar("SELECT last_insert_rowid()"); - if (allocated === undefined) { - throw createWorkspaceError("EIO", "failed to allocate inode"); - } - inode = allocated; + inode = insertFileNode(db, mode, mtime); db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, @@ -814,16 +813,7 @@ export function writeFileRangesSync( inode = existing.child_inode; oldChunks = existingChunkRefs(db, inode); } else { - db.run( - "INSERT INTO vfs_nodes (type, mode, mtime, rev) VALUES ('file', ?, ?, 0)", - mode, - mtime, - ); - const allocated = db.scalar("SELECT last_insert_rowid()"); - if (allocated === undefined) { - throw createWorkspaceError("EIO", "failed to allocate inode"); - } - inode = allocated; + inode = insertFileNode(db, mode, mtime); db.run( "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", parentInode, diff --git a/packages/dofs/src/rev.ts b/packages/dofs/src/rev.ts index efdcddd9..df7f6d3c 100644 --- a/packages/dofs/src/rev.ts +++ b/packages/dofs/src/rev.ts @@ -9,10 +9,13 @@ import type { Database } from "./storage.js"; // otherwise race with concurrent mutations. The DO single-writer model // makes that unlikely in practice, but the contract is "wrap me". export function incrementRev(db: Database): number { - db.run("UPDATE vfs_meta SET v = v + 1 WHERE k = 'rev'"); - const next = db.scalar("SELECT v FROM vfs_meta WHERE k = ?", "rev"); - if (next === undefined) { + // RETURNING folds the read into the same statement so each mutation + // pays one round-trip instead of two. SQLite has supported it since + // 3.35; both node:sqlite and Cloudflare DO SqlStorage are on newer + // versions. + const row = db.one<{ v: number }>("UPDATE vfs_meta SET v = v + 1 WHERE k = 'rev' RETURNING v"); + if (row === undefined) { throw new Error("vfs_meta.rev row missing; was initializeSchema run?"); } - return next; + return row.v; } From 4c0c4ef0a8eb59da02f01a96e40981123f2e0334 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 11:38:28 +0000 Subject: [PATCH 20/31] dofs: cache file size on vfs_nodes so stat skips SUM Add vfs_nodes.size to denormalise the chunk-sum file size. stat and the provider's fileSize helper now read it directly from the node row that resolveInode already loaded, instead of running a separate COALESCE(SUM(size), 0) FROM vfs_chunks aggregate on every call. Every write path that lands chunks (writeFile streaming, writeFileSync, writeFileRangesSync, applyChunkedInodeUpdate, releaseWriteBufferSync, truncateFileSync) stamps the new size in the same UPDATE that bumps rev. resolveInode carries the cached value through to its callers so stat, lstat, and readRangeSync don't have to re-query. Schema bumps to v3 with a v2 -> v3 migration that backfills the column from the existing chunk rows. Open buffers still override the cached size for in-flight writes. --- packages/dofs/src/fs/readFile.ts | 7 ++----- packages/dofs/src/fs/resolve.test.ts | 3 +++ packages/dofs/src/fs/resolve.ts | 10 +++++++++- packages/dofs/src/fs/stat.ts | 13 ++++--------- packages/dofs/src/fs/writeFile.ts | 22 +++++++++++++--------- packages/dofs/src/provider.ts | 4 +--- packages/dofs/src/schema/core.ts | 5 +++-- packages/dofs/src/schema/migrations.ts | 21 +++++++++++++++++++++ 8 files changed, 56 insertions(+), 29 deletions(-) diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index 9b393120..93d5759a 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -151,11 +151,8 @@ export function readRangeSync( return buffered.buf.subarray(offset, end); } - const totalSize = - db.scalar( - "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", - node.inode, - ) ?? 0; + // node.size is the cached value resolveInode just loaded. + const totalSize = node.size; if (offset >= totalSize) return new Uint8Array(); const end = Math.min(offset + length, totalSize); const firstIdx = Math.floor(offset / CHUNK_SIZE); diff --git a/packages/dofs/src/fs/resolve.test.ts b/packages/dofs/src/fs/resolve.test.ts index e877dbbe..c8a939e0 100644 --- a/packages/dofs/src/fs/resolve.test.ts +++ b/packages/dofs/src/fs/resolve.test.ts @@ -40,6 +40,7 @@ describe("resolveInode", () => { type: "dir", mode: 0o755, mtime: 0, + size: 0, }); }, { now: () => 0 }, @@ -54,6 +55,7 @@ describe("resolveInode", () => { type: "file", mode: 0o644, mtime: 99, + size: 0, }); }); }); @@ -68,6 +70,7 @@ describe("resolveInode", () => { type: "file", mode: 0o644, mtime: 7, + size: 0, }); }); }); diff --git a/packages/dofs/src/fs/resolve.ts b/packages/dofs/src/fs/resolve.ts index c7e31761..dcf2d20e 100644 --- a/packages/dofs/src/fs/resolve.ts +++ b/packages/dofs/src/fs/resolve.ts @@ -8,6 +8,11 @@ export interface ResolvedInode { type: "file" | "dir" | "symlink"; mode: number; mtime: number; + // Cached file size from vfs_nodes.size. Always 0 for directories + // and symlinks; for files this matches SUM(vfs_chunks.size) for + // the inode. Stat callers consume it directly instead of doing a + // separate aggregate query. + size: number; // Populated only when type === "symlink". Higher layers (readlink, // lstat) consume this; resolveInode follows it transparently unless // the caller asks otherwise. @@ -26,6 +31,7 @@ interface NodeRow { type: "file" | "dir" | "symlink"; mode: number; mtime: number; + size: number; link_target: string | null; } @@ -104,6 +110,7 @@ function resolveParts( type: resolved.type, mode: resolved.mode, mtime: resolved.mtime, + size: resolved.size, link_target: resolved.linkTarget ?? null, }; continue; @@ -116,13 +123,14 @@ function resolveParts( type: current.type, mode: current.mode, mtime: current.mtime, + size: current.size, linkTarget: current.link_target ?? undefined, }; } function readNode(db: Database, inode: number): NodeRow | null { const row = db.one( - "SELECT inode, type, mode, mtime, link_target FROM vfs_nodes WHERE inode = ?", + "SELECT inode, type, mode, mtime, size, link_target FROM vfs_nodes WHERE inode = ?", inode, ); return row ?? null; diff --git a/packages/dofs/src/fs/stat.ts b/packages/dofs/src/fs/stat.ts index 1387ab66..8696bfa2 100644 --- a/packages/dofs/src/fs/stat.ts +++ b/packages/dofs/src/fs/stat.ts @@ -24,16 +24,11 @@ export function stat(db: Database, path: string): WorkspaceStatResult { const isFile = node.type === "file"; let size = 0; if (isFile) { + // Prefer the in-memory buffer when an open file has unflushed + // writes; otherwise read the cached size off vfs_nodes that + // resolveInode just loaded for us, no extra SQL. const buffered = getWriteBuffer(db, node.inode); - if (buffered !== undefined && buffered.dirty) { - size = buffered.size; - } else { - size = - db.scalar( - "SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", - node.inode, - ) ?? 0; - } + size = buffered !== undefined && buffered.dirty ? buffered.size : node.size; } return { diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 58b6163b..b0e943e5 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -237,11 +237,14 @@ async function writeFileStreaming( } const manifestHash = buildManifest(db, chunkRefs, mtime); const rev = incrementRev(db); + let totalSize = 0; + for (const ref of chunkRefs) totalSize += ref.size; db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = ?, manifest_hash = ? WHERE inode = ?", mode, mtime, rev, + totalSize, manifestHash, inode, ); @@ -330,9 +333,7 @@ function existingChunkRefs(db: Database, inode: number): ChunkRef[] { } function fileSizeForInode(db: Database, inode: number): number { - return ( - db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 - ); + return db.scalar("SELECT size FROM vfs_nodes WHERE inode = ?", inode) ?? 0; } function readChunkBytes(db: Database, inode: number, idx: number): Uint8Array { @@ -433,10 +434,11 @@ function applyChunkedInodeUpdate( const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = ?, manifest_hash = NULL WHERE inode = ?", mode, mtime, rev, + size, inode, ); } @@ -529,7 +531,7 @@ export function releaseWriteBufferSync(db: Database, path: string, now: () => nu db.run("DELETE FROM vfs_chunks WHERE inode = ?", node.inode); const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = 0, manifest_hash = NULL WHERE inode = ?", mode, mtime, rev, @@ -673,7 +675,7 @@ export function truncateFileSync( db.run("DELETE FROM vfs_chunks WHERE inode = ?", inode); const rev = incrementRev(db); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = NULL WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = 0, manifest_hash = NULL WHERE inode = ?", mode, mtime, rev, @@ -765,10 +767,11 @@ export function writeFileSync( const manifestHash = buildManifest(db, chunks, mtime); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = ?, manifest_hash = ? WHERE inode = ?", mode, mtime, rev, + bytes.byteLength, manifestHash, inode, ); @@ -845,10 +848,11 @@ export function writeFileRangesSync( const manifestHash = replaceChunkRows(db, inode, nextChunks, mtime); db.run( - "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, manifest_hash = ? WHERE inode = ?", + "UPDATE vfs_nodes SET mode = ?, mtime = ?, rev = ?, size = ?, manifest_hash = ? WHERE inode = ?", mode, mtime, rev, + bytes.byteLength, manifestHash, inode, ); diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 84fb4b51..2b36cbd5 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -758,9 +758,7 @@ function fileSize(db: Database, inode: number): number { if (buffered !== undefined && buffered.dirty) { return buffered.size; } - return ( - db.scalar("SELECT COALESCE(SUM(size), 0) FROM vfs_chunks WHERE inode = ?", inode) ?? 0 - ); + return db.scalar("SELECT size FROM vfs_nodes WHERE inode = ?", inode) ?? 0; } function wrapStats(input: StatsInputs): VirtualStatsLike { diff --git a/packages/dofs/src/schema/core.ts b/packages/dofs/src/schema/core.ts index fe36e1f7..f468c265 100644 --- a/packages/dofs/src/schema/core.ts +++ b/packages/dofs/src/schema/core.ts @@ -3,7 +3,7 @@ // See `schema/migrations.ts` for the migration list; `sync.ts` // carries the fresh-install DDL. -export const SCHEMA_VERSION = 2; +export const SCHEMA_VERSION = 3; export const ROOT_INODE = 1; export const CORE_STATEMENTS = [ @@ -20,7 +20,8 @@ export const CORE_STATEMENTS = [ mount_root TEXT, stub_size INTEGER, manifest_hash BLOB, - link_target TEXT + link_target TEXT, + size INTEGER NOT NULL DEFAULT 0 )`, `CREATE TABLE IF NOT EXISTS vfs_dirents ( parent_inode INTEGER NOT NULL, diff --git a/packages/dofs/src/schema/migrations.ts b/packages/dofs/src/schema/migrations.ts index 10d1f3d4..7ca834a4 100644 --- a/packages/dofs/src/schema/migrations.ts +++ b/packages/dofs/src/schema/migrations.ts @@ -40,8 +40,29 @@ function v1_to_v2_add_mounts_mode(db: Database): void { ); } +// v2 → v3 — denormalise file size onto vfs_nodes so stat doesn't +// have to SUM the chunk rows on every call. The column is +// backfilled from existing vfs_chunks; later writes maintain it. +function v2_to_v3_add_size_column(db: Database): void { + const hasColumn = db + .all<{ name: string }>("PRAGMA table_info(vfs_nodes)") + .some((column) => column.name === "size"); + if (!hasColumn) { + db.run("ALTER TABLE vfs_nodes ADD COLUMN size INTEGER NOT NULL DEFAULT 0"); + } + db.run( + `UPDATE vfs_nodes + SET size = COALESCE( + (SELECT SUM(size) FROM vfs_chunks WHERE vfs_chunks.inode = vfs_nodes.inode), + 0 + ) + WHERE type = 'file'`, + ); +} + export const MIGRATIONS: readonly Migration[] = [ { from: 1, to: 2, migrator: v1_to_v2_add_mounts_mode }, + { from: 2, to: 3, migrator: v2_to_v3_add_size_column }, ] as const; // Apply every migration whose `from` matches the current version, From b7bbde2d80ae24d8e209570cd9fecfd5ece2a09a Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 11:52:16 +0000 Subject: [PATCH 21/31] dofs: defer file creation to release time createFileSync used to commit an empty inode immediately, then\nopenWriteBufferSync attached a buffer that the eventual release\nturned into chunk rows. Two transactions per tiny file.\n\nAdd openWriteBufferForCreateSync that stashes a pending-create\nentry in the write-buffer cache without touching SQL. Release\ncommits the INSERT, dirent, and chunk rows in a single transaction.\nFor an open-write-close cycle on a fresh file this collapses two\ntransactions into one.\n\nThe pending entry is keyed by path (no inode exists yet) and\nbridged into the rest of the FS layer:\n\n- writeRangeSync, truncateFileSync, readRangeSync, readFile, and\n stat consult the path-keyed pending cache before falling back to\n resolveInode.\n- provider.existsSync, lstatSync, readFileSync, chmodSync see\n pending entries.\n- readdir merges pending leaves into the directory listing so an\n open-before-release readdir still surfaces the new file.\n- link, rename, and unlink call flushPendingByPath on each\n candidate path so the dirent operation always sees a real inode.\n The buffer's open handles continue addressing bytes through the\n inode-keyed cache after commit.\n\nThe FUSE driver routes FUSE create through the deferred path when\nthe provider advertises openWriteBufferForCreateSync. Existing\ntests for direct mode pass unchanged; the deferred path is the new\nfast lane. --- packages/dofs/src/fs/readFile.ts | 28 +++- packages/dofs/src/fs/readdir.ts | 26 +++- packages/dofs/src/fs/stat.ts | 17 ++- packages/dofs/src/fs/writeBuffer.ts | 86 ++++++++++++- packages/dofs/src/fs/writeFile.ts | 193 +++++++++++++++++++++++++++- packages/dofs/src/provider.ts | 53 +++++++- packages/wsd/src/fuse/driver.ts | 10 ++ packages/wsd/src/fuse/vfs.ts | 1 + 8 files changed, 401 insertions(+), 13 deletions(-) diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index 93d5759a..97535f9a 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -1,7 +1,8 @@ import { createWorkspaceError } from "../errors.js"; +import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; -import { getWriteBuffer } from "./writeBuffer.js"; +import { getPendingWriteBufferByPath, getWriteBuffer } from "./writeBuffer.js"; import { CHUNK_SIZE } from "./writeFile.js"; export interface ReadFileOptions { @@ -37,6 +38,21 @@ export async function readFile( optionsOrEncoding === "utf8" || (typeof optionsOrEncoding === "object" && optionsOrEncoding?.encoding === "utf8"); + // Pending-create files surface through the path-keyed buffer. + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + const snapshot = new Uint8Array(pending.size); + snapshot.set(pending.buf.subarray(0, pending.size)); + if (wantString) return new TextDecoder().decode(snapshot); + return new ReadableStream({ + start(controller) { + controller.enqueue(snapshot); + controller.close(); + }, + }); + } + // Resolve up front so we surface ENOENT/EISDIR before doing any // streaming work. const node = resolveInode(db, path); @@ -132,6 +148,16 @@ export function readRangeSync( if (!Number.isInteger(length) || length < 0) { throw createWorkspaceError("EINVAL", `invalid read length: ${length}`, path); } + // Pending-create files have no inode yet. Serve reads from the + // path-keyed buffer until release commits the row. + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + if (length === 0) return new Uint8Array(); + if (offset >= pending.size) return new Uint8Array(); + const end = Math.min(offset + length, pending.size); + return pending.buf.subarray(offset, end); + } const node = resolveInode(db, path); if (node === null) { throw createWorkspaceError("ENOENT", `no such file: ${path}`, path); diff --git a/packages/dofs/src/fs/readdir.ts b/packages/dofs/src/fs/readdir.ts index c0b4faa3..ded33350 100644 --- a/packages/dofs/src/fs/readdir.ts +++ b/packages/dofs/src/fs/readdir.ts @@ -2,6 +2,7 @@ import { createWorkspaceError } from "../errors.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; +import { listPendingByParent } from "./writeBuffer.js"; export interface WorkspaceDirentResult { name: string; @@ -34,10 +35,33 @@ export function readdir(db: Database, path: string): WorkspaceDirentResult[] { node.inode, ); - return rows.map((row) => ({ + const entries = rows.map((row) => ({ name: row.name, parentPath: canonical, isFile: row.type === "file", isDirectory: row.type === "dir", })); + + // Merge in pending-create buffers parented under this directory so + // a `readdir` between FUSE create and release still surfaces the + // file. Skip any whose name already appears in the SQL rows (in + // case a concurrent commit just landed it). + const pending = listPendingByParent(db, node.inode); + if (pending.length > 0) { + const seen = new Set(entries.map((e) => e.name)); + for (const entry of pending) { + if (entry.pending === undefined) continue; + const { leafName } = entry.pending; + if (seen.has(leafName)) continue; + entries.push({ + name: leafName, + parentPath: canonical, + isFile: true, + isDirectory: false, + }); + } + entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)); + } + + return entries; } diff --git a/packages/dofs/src/fs/stat.ts b/packages/dofs/src/fs/stat.ts index 8696bfa2..d5cde6c6 100644 --- a/packages/dofs/src/fs/stat.ts +++ b/packages/dofs/src/fs/stat.ts @@ -2,7 +2,7 @@ import { createWorkspaceError } from "../errors.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; import { resolveInode } from "./resolve.js"; -import { getWriteBuffer } from "./writeBuffer.js"; +import { getPendingWriteBufferByPath, getWriteBuffer } from "./writeBuffer.js"; export interface WorkspaceStatResult { name: string; @@ -14,7 +14,20 @@ export interface WorkspaceStatResult { } export function stat(db: Database, path: string): WorkspaceStatResult { - const { name } = canonicalizePath(path); + const { name, path: canonical } = canonicalizePath(path); + // Pending-create files have no inode yet; serve the buffer state + // so callers between create and release see the file as it stands. + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined && pending.pending !== undefined) { + return { + name, + mode: pending.mode & 0o7777, + mtime: pending.pending.mtime, + size: pending.size, + isFile: true, + isDirectory: false, + }; + } const node = resolveInode(db, path); if (node === null) { throw createWorkspaceError("ENOENT", `no such path: ${path}`, path); diff --git a/packages/dofs/src/fs/writeBuffer.ts b/packages/dofs/src/fs/writeBuffer.ts index 102c4036..c9edca12 100644 --- a/packages/dofs/src/fs/writeBuffer.ts +++ b/packages/dofs/src/fs/writeBuffer.ts @@ -29,29 +29,103 @@ export interface WriteBufferEntry { // Mode the caller wants persisted on release. Defaults to the // inode's existing mode at open time when the caller has none. mode: number; + // Pending-create state. When set, no inode row exists yet; release + // will INSERT the node + dirent + chunks in one transaction. The + // synthetic inode id used to key this entry in the cache is stored + // here so release can find and remove the entry without scanning + // the cache. + pending?: { + parentInode: number; + leafName: string; + canonicalPath: string; + pendingInode: number; + mtime: number; + }; } -const caches = new WeakMap>(); +interface DatabaseCache { + byInode: Map; + byPendingPath: Map; + nextPendingInode: number; +} + +const caches = new WeakMap(); -function cacheFor(db: Database): Map { +function cacheFor(db: Database): DatabaseCache { let cache = caches.get(db); if (cache === undefined) { - cache = new Map(); + cache = { byInode: new Map(), byPendingPath: new Map(), nextPendingInode: -1 }; caches.set(db, cache); } return cache; } export function getWriteBuffer(db: Database, inode: number): WriteBufferEntry | undefined { - return caches.get(db)?.get(inode); + return caches.get(db)?.byInode.get(inode); +} + +export function getPendingWriteBufferByPath( + db: Database, + canonicalPath: string, +): WriteBufferEntry | undefined { + return caches.get(db)?.byPendingPath.get(canonicalPath); +} + +// List pending-create buffers whose parent dirent matches `parentInode`. +// Used by readdir so freshly-created-but-not-yet-released files show +// up in directory listings between open and release. +export function listPendingByParent(db: Database, parentInode: number): WriteBufferEntry[] { + const cache = caches.get(db); + if (cache === undefined) return []; + const out: WriteBufferEntry[] = []; + for (const entry of cache.byPendingPath.values()) { + if (entry.pending?.parentInode === parentInode) out.push(entry); + } + return out; } export function setWriteBuffer(db: Database, inode: number, entry: WriteBufferEntry): void { - cacheFor(db).set(inode, entry); + const cache = cacheFor(db); + cache.byInode.set(inode, entry); + if (entry.pending !== undefined) { + cache.byPendingPath.set(entry.pending.canonicalPath, entry); + } } export function deleteWriteBuffer(db: Database, inode: number): void { - caches.get(db)?.delete(inode); + const cache = caches.get(db); + if (cache === undefined) return; + const entry = cache.byInode.get(inode); + if (entry?.pending !== undefined) { + cache.byPendingPath.delete(entry.pending.canonicalPath); + } + cache.byInode.delete(inode); +} + +// Allocate a synthetic negative inode id for a pending file. The +// real id is assigned by SQLite when release INSERTs the node row; +// the synthetic value just lets the buffer cache key entries +// before that point. +export function allocatePendingInode(db: Database): number { + const cache = cacheFor(db); + const next = cache.nextPendingInode; + cache.nextPendingInode -= 1; + return next; +} + +// Re-key a pending entry to the real inode assigned by SQLite at +// commit time, dropping the pending-path index. +export function promotePendingToInode(db: Database, pendingInode: number, realInode: number): void { + const cache = caches.get(db); + if (cache === undefined) return; + const entry = cache.byInode.get(pendingInode); + if (entry === undefined) return; + if (entry.pending !== undefined) { + cache.byPendingPath.delete(entry.pending.canonicalPath); + entry.pending = undefined; + } + cache.byInode.delete(pendingInode); + cache.byInode.set(realInode, entry); } export function ensureCapacity(entry: WriteBufferEntry, needed: number): void { diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index b0e943e5..723a14c8 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -8,9 +8,12 @@ import { stageBlob } from "../sync/blobs.js"; import { buildManifest } from "../sync/manifests.js"; import { assertNotReadOnly } from "./mount-guard.js"; import { + allocatePendingInode, deleteWriteBuffer, ensureCapacity as ensureBufferCapacity, + getPendingWriteBufferByPath, getWriteBuffer, + promotePendingToInode, setWriteBuffer, type WriteBufferEntry, } from "./writeBuffer.js"; @@ -489,6 +492,12 @@ export function createFileSync( // buffer instead of the SQLite chunk/blob store. Release commits // the bytes back to chunks/inline. export function openWriteBufferSync(db: Database, path: string): void { + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + pending.openCount += 1; + return; + } const { inode, mode } = resolveFileInode(db, path); const existing = getWriteBuffer(db, inode); if (existing !== undefined) { @@ -504,11 +513,60 @@ export function openWriteBufferSync(db: Database, path: string): void { }); } +// Create a new file lazily: stash a pending-create write buffer +// keyed by path, without touching SQL until release. createFileSync +// + openWriteBufferSync + writes + releaseWriteBufferSync would +// otherwise spend two transactions per file (one INSERT round and +// one chunk-commit round); this collapses them into a single +// INSERT-and-chunks transaction at release time. +// +// Throws EEXIST if a path already resolves to a live node or to +// another pending buffer. +export function openWriteBufferForCreateSync( + db: Database, + path: string, + options: WriteFileOptions, + now: () => number, +): void { + const { path: canonical } = canonicalizePath(path); + assertNotReadOnly(db, canonical); + if (getPendingWriteBufferByPath(db, canonical) !== undefined) { + throw createWorkspaceError("EEXIST", `path exists: ${canonical}`, canonical); + } + const [parentInode, leafName] = parentAndNameForResolvedPath(db, path); + const existing = db.one<{ child_inode: number }>( + "SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + parentInode, + leafName, + ); + if (existing !== undefined) { + throw createWorkspaceError("EEXIST", `path exists: ${canonical}`, canonical); + } + const mode = (options.mode ?? 0o644) & 0o7777; + const mtime = now(); + const pendingInode = allocatePendingInode(db); + setWriteBuffer(db, pendingInode, { + buf: new Uint8Array(0), + size: 0, + dirty: true, + openCount: 1, + mode, + pending: { parentInode, leafName, canonicalPath: canonical, pendingInode, mtime }, + }); +} + // Release one open of an inode's write buffer. When the open count // reaches zero, commit the buffered bytes to chunk rows and drop // the entry. The committed mode is the buffer's mode at release -// time so an intermediate chmod survives. +// time so an intermediate chmod survives. Pending-create entries +// emit their INSERT + dirent + chunks in the same transaction. export function releaseWriteBufferSync(db: Database, path: string, now: () => number): void { + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + releasePendingBuffer(db, pending, now); + return; + } const node = resolveFileInode(db, path); const entry = getWriteBuffer(db, node.inode); if (entry === undefined) return; @@ -553,6 +611,111 @@ export function releaseWriteBufferSync(db: Database, path: string, now: () => nu deleteWriteBuffer(db, node.inode); } +// Commit a pending-create buffer to SQLite. Returns the real inode +// allocated by the INSERT, or throws. Promotes the cache entry's key +// from the synthetic pending id to the real inode so subsequent +// reads/writes through the inode-keyed cache still see the same +// buffer. Caller owns the lifecycle of the now-promoted entry. +function commitPendingBuffer(db: Database, entry: WriteBufferEntry, now: () => number): number { + if (entry.pending === undefined) { + throw createWorkspaceError("EIO", "commitPendingBuffer called on non-pending entry"); + } + const { parentInode, leafName, canonicalPath, pendingInode } = entry.pending; + const mtime = now(); + const mode = entry.mode & 0o7777; + const buffered = entry.buf.subarray(0, entry.size); + + let realInode = 0; + try { + db.transactionSync(() => { + // Re-check at commit time: a non-buffered writeFile or another + // out-of-band path could have landed between open and release. + const collision = db.one<{ child_inode: number }>( + "SELECT child_inode FROM vfs_dirents WHERE parent_inode = ? AND name = ?", + parentInode, + leafName, + ); + if (collision !== undefined) { + throw createWorkspaceError( + "EEXIST", + `path exists at commit time: ${canonicalPath}`, + canonicalPath, + ); + } + const rev = incrementRev(db); + const row = db.one<{ inode: number }>( + "INSERT INTO vfs_nodes (type, mode, mtime, rev, size, manifest_hash) VALUES ('file', ?, ?, ?, ?, NULL) RETURNING inode", + mode, + mtime, + rev, + entry.size, + ); + if (row === undefined) { + throw createWorkspaceError("EIO", "failed to allocate inode"); + } + db.run( + "INSERT INTO vfs_dirents (parent_inode, name, child_inode) VALUES (?, ?, ?)", + parentInode, + leafName, + row.inode, + ); + if (entry.size > 0) { + const inode = row.inode; + const chunkCount = Math.ceil(entry.size / CHUNK_SIZE); + for (let idx = 0; idx < chunkCount; idx++) { + const start = idx * CHUNK_SIZE; + const end = Math.min(start + CHUNK_SIZE, entry.size); + const chunkBytes = buffered.subarray(start, end); + const chunk = { + hash: sha256(chunkBytes), + bytes: chunkBytes, + size: chunkBytes.byteLength, + }; + upsertChunkBlob(db, chunk, mtime); + db.run( + "INSERT INTO vfs_chunks (inode, idx, hash, size) VALUES (?, ?, ?, ?)", + inode, + idx, + chunk.hash, + chunk.size, + ); + } + } + realInode = row.inode; + }); + } catch (error) { + // Transaction rolled back; drop the buffer so the next caller + // starts clean. + deleteWriteBuffer(db, pendingInode); + throw error; + } + promotePendingToInode(db, pendingInode, realInode); + return realInode; +} + +// Commit a pending-create buffer identified by its canonical path, +// leaving the open count untouched. Used by link, rename, and unlink +// against a still-open file so the dirent operation sees a real +// inode. Returns true when a pending buffer was committed. +export function flushPendingByPath(db: Database, path: string, now: () => number): boolean { + const { path: canonical } = canonicalizePath(path); + const entry = getPendingWriteBufferByPath(db, canonical); + if (entry === undefined || entry.pending === undefined) return false; + commitPendingBuffer(db, entry, now); + return true; +} + +function releasePendingBuffer(db: Database, entry: WriteBufferEntry, now: () => number): void { + if (entry.pending === undefined) return; + entry.openCount -= 1; + if (entry.openCount > 0) return; + + const inode = commitPendingBuffer(db, entry, now); + // File is closed; drop the now-promoted entry. A subsequent open + // hits the SQL path and gets a fresh buffer if needed. + deleteWriteBuffer(db, inode); +} + // Hydrate a freshly-opened buffer with the inode's current bytes // the first time we mutate it. Avoids paying the read cost when the // caller opens a file just to truncate or overwrite it. @@ -591,6 +754,22 @@ export function writeRangeSync( if (bytes.byteLength === 0) return 0; const mtime = now(); + // Pending-create files don't have an inode yet; route the write + // straight into the path-keyed buffer. + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + const writeEnd = offset + bytes.byteLength; + ensureBufferCapacity(pending, writeEnd); + if (offset > pending.size) { + pending.buf.fill(0, pending.size, offset); + } + pending.buf.set(bytes, offset); + if (writeEnd > pending.size) pending.size = writeEnd; + pending.mode = (options.mode ?? pending.mode) & 0o7777; + pending.dirty = true; + return bytes.byteLength; + } + const { inode, mode: existingMode } = resolveFileInode(db, path); const mode = (options.mode ?? existingMode) & 0o7777; const buffered = getWriteBuffer(db, inode); @@ -653,6 +832,18 @@ export function truncateFileSync( } const mtime = now(); + // Pending-create files truncate in-place on the path-keyed buffer. + const pending = getPendingWriteBufferByPath(db, canonical); + if (pending !== undefined) { + if (size > pending.size) { + ensureBufferCapacity(pending, size); + pending.buf.fill(0, pending.size, size); + } + pending.size = size; + pending.dirty = true; + return; + } + const { inode, mode } = resolveFileInode(db, path); const buffered = getWriteBuffer(db, inode); diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 2b36cbd5..de7ff310 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -25,9 +25,11 @@ import { type WatchHandle, type WatchOptions, } from "./fs/watch.js"; -import { getWriteBuffer } from "./fs/writeBuffer.js"; +import { getPendingWriteBufferByPath, getWriteBuffer } from "./fs/writeBuffer.js"; import { createFileSync as createFileSyncImpl, + flushPendingByPath, + openWriteBufferForCreateSync as openWriteBufferForCreateSyncImpl, openWriteBufferSync as openWriteBufferSyncImpl, releaseWriteBufferSync as releaseWriteBufferSyncImpl, truncateFileSync as truncateFileSyncImpl, @@ -188,6 +190,20 @@ export class SQLiteWorkspaceProvider { } lstatSync(path: string, _options?: { bigint?: boolean }): VirtualStatsLike { + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(this.db, canonical); + if (pending !== undefined && pending.pending !== undefined) { + return wrapStats({ + mode: pending.mode & 0o7777, + size: pending.size, + mtimeMs: pending.pending.mtime, + ino: 0, + isFile: true, + isDirectory: false, + isSymbolicLink: false, + nlink: 1, + }); + } const node = resolveInode(this.db, path, { followSymlinks: false }); if (node === null) { throw createWorkspaceError("ENOENT", `no such path: ${path}`, path); @@ -249,6 +265,12 @@ export class SQLiteWorkspaceProvider { } unlinkSync(path: string): void { + // If a buffered create is still pending for this path, commit + // it first so rm sees a real inode to unlink (and so the + // resulting GC sees the orphaned blob, matching the non-buffered + // shape). The buffer's open handles continue to address bytes + // through the inode-keyed cache. + flushPendingByPath(this.db, path, this.now); rmImpl(this.db, path, {}); } @@ -258,6 +280,10 @@ export class SQLiteWorkspaceProvider { } linkSync(existingPath: string, newPath: string): void { + // Same shape as unlink: commit a still-pending source before + // adding the second dirent, otherwise link has nothing real to + // point at. + flushPendingByPath(this.db, existingPath, this.now); linkImpl(this.db, existingPath, newPath); } @@ -271,6 +297,8 @@ export class SQLiteWorkspaceProvider { // yet; we lean on the existing schema-level pieces here. When // rename grows up (cross-directory, overwriting an existing file, // ...) it should move into fs/rename.ts with its own tests. + flushPendingByPath(this.db, oldPath, this.now); + flushPendingByPath(this.db, newPath, this.now); const node = resolveInode(this.db, oldPath); if (node === null) { throw createWorkspaceError("ENOENT", `no such path: ${oldPath}`, oldPath); @@ -374,6 +402,14 @@ export class SQLiteWorkspaceProvider { path: string, options?: BufferEncoding | { encoding?: BufferEncoding | null } | null, ): Buffer | string { + const encoding = typeof options === "string" ? options : options?.encoding; + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(this.db, canonical); + if (pending !== undefined) { + const snapshot = Buffer.alloc(pending.size); + snapshot.set(pending.buf.subarray(0, pending.size)); + return encoding ? snapshot.toString(encoding) : snapshot; + } const node = resolveInode(this.db, path); if (node === null) { throw createWorkspaceError("ENOENT", `no such file: ${path}`, path); @@ -381,7 +417,6 @@ export class SQLiteWorkspaceProvider { if (node.type !== "file") { throw createWorkspaceError("EISDIR", `path is a directory: ${path}`, path); } - const encoding = typeof options === "string" ? options : options?.encoding; // While a buffer is open for this inode it owns the latest // bytes; serve from it instead of the chunk store. const buffered = getWriteBuffer(this.db, node.inode); @@ -474,11 +509,23 @@ export class SQLiteWorkspaceProvider { openWriteBufferSyncImpl(this.db, path); } + openWriteBufferForCreateSync(path: string, options?: { mode?: number }): void { + openWriteBufferForCreateSyncImpl(this.db, path, { mode: options?.mode }, this.now); + } + releaseWriteBufferSync(path: string): void { releaseWriteBufferSyncImpl(this.db, path, this.now); } chmodSync(path: string, mode: number): void { + const { path: canonical } = canonicalizePath(path); + const pending = getPendingWriteBufferByPath(this.db, canonical); + if (pending !== undefined) { + // Pending-create files don't have a row yet; stash the mode on + // the buffer so the eventual INSERT picks it up. + pending.mode = mode & 0o7777; + return; + } const node = resolveInode(this.db, path, { followSymlinks: false }); if (node === null) { throw createWorkspaceError("ENOENT", `no such path: ${path}`, path); @@ -514,6 +561,8 @@ export class SQLiteWorkspaceProvider { existsSync(path: string): boolean { try { + const { path: canonical } = canonicalizePath(path); + if (getPendingWriteBufferByPath(this.db, canonical) !== undefined) return true; return resolveInode(this.db, path) !== null; } catch { return false; diff --git a/packages/wsd/src/fuse/driver.ts b/packages/wsd/src/fuse/driver.ts index 194b961c..6bca34d1 100644 --- a/packages/wsd/src/fuse/driver.ts +++ b/packages/wsd/src/fuse/driver.ts @@ -238,6 +238,7 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO chmodSync?: (path: string, mode: number) => void; readRangeSync?: (path: string, offset: number, length: number) => Uint8Array; openWriteBufferSync?: (path: string) => void; + openWriteBufferForCreateSync?: (path: string, options?: { mode?: number }) => void; releaseWriteBufferSync?: (path: string) => void; }; const hasDirectWrites = @@ -248,6 +249,8 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO hasDirectWrites && directWriteVfs.openWriteBufferSync !== undefined && directWriteVfs.releaseWriteBufferSync !== undefined; + const hasDeferredCreate = + hasBufferedWrites && directWriteVfs.openWriteBufferForCreateSync !== undefined; const linkableVfs = vfs as NodeVirtualFileSystem & { linkSync?: (existingPath: string, newPath: string) => void; }; @@ -464,6 +467,13 @@ export function makeFUSEOps(vfs: NodeVirtualFileSystem, mountPoint = "/"): FuseO cb(ERRNO.EEXIST, 0); return; } + if (hasDeferredCreate) { + // Single transaction at release time: defer the inode INSERT + // and the chunk commit into one round trip. + directWriteVfs.openWriteBufferForCreateSync?.(toVfs(path), { mode }); + cb(0, openFileHandle(path)); + return; + } if (hasDirectWrites) { directWriteVfs.createFileSync?.(toVfs(path), { mode }); if (hasBufferedWrites) { diff --git a/packages/wsd/src/fuse/vfs.ts b/packages/wsd/src/fuse/vfs.ts index 4114cecf..ddd9ed52 100644 --- a/packages/wsd/src/fuse/vfs.ts +++ b/packages/wsd/src/fuse/vfs.ts @@ -47,6 +47,7 @@ const EXTRA_VFS_METHODS = [ "chmodSync", "readRangeSync", "openWriteBufferSync", + "openWriteBufferForCreateSync", "releaseWriteBufferSync", ] as const; From f4cad0a281f85e46e305093ce18f46294c6443d1 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:12:53 +0000 Subject: [PATCH 22/31] wsd: align FUSE max_read/max_write with the dofs chunk size Default max_read and max_write were 128 KiB. Sequential reads of a\nchunk-backed file used to issue four FUSE reads per 512 KiB chunk\nand four SQL fetches of the same blob bytes. Raising both to 524288\nmatches CHUNK_SIZE so a single FUSE read maps to a single chunk\nfetch.\n\nThe historical 128 KiB sizing predates readRangeSync, when reads\nstill materialised the whole file per syscall and the option didn't\nshow up in the numbers. With the per-range read path in place the\nFUSE syscall count for a sequential 64 MiB read drops 4x. --- packages/wsd/src/fuse/options.test.ts | 15 +++++++-------- packages/wsd/src/fuse/options.ts | 13 ++++++++----- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/wsd/src/fuse/options.test.ts b/packages/wsd/src/fuse/options.test.ts index c148abf5..7ab84f20 100644 --- a/packages/wsd/src/fuse/options.test.ts +++ b/packages/wsd/src/fuse/options.test.ts @@ -17,11 +17,10 @@ describe("buildFuseOptionString", () => { // letting a stale view linger. negative_timeout at zero keeps // "file not found" answers fresh so a just-written file shows // up immediately. use_ino lets hardlinks stat as the same inode. - // big_writes plus 128 KiB max_read and max_write match the - // historical sizing that earlier experiments showed didn't move - // on bigger values. + // big_writes plus 512 KiB max_read and max_write match the dofs + // CHUNK_SIZE so each FUSE read maps to one chunk fetch. expect(buildFuseOptionString(empty)).toBe( - "big_writes,use_ino,max_write=131072,max_read=131072,auto_cache,attr_timeout=1,entry_timeout=1,negative_timeout=0,ac_attr_timeout=1", + "big_writes,use_ino,max_write=524288,max_read=524288,auto_cache,attr_timeout=1,entry_timeout=1,negative_timeout=0,ac_attr_timeout=1", ); }); @@ -32,19 +31,19 @@ describe("buildFuseOptionString", () => { }); expect(out).toContain("max_write=1048576"); expect(out).toContain("max_read=1048576"); - expect(out).not.toContain("max_write=131072"); + expect(out).not.toContain("max_write=524288"); }); test("ignores non-numeric size overrides and falls back to the default", () => { const out = buildFuseOptionString({ WSD_FUSE_MAX_READ: "wat" }); - expect(out).toContain("max_read=131072"); + expect(out).toContain("max_read=524288"); }); test("rejects non-positive sizes", () => { const a = buildFuseOptionString({ WSD_FUSE_MAX_READ: "0" }); - expect(a).toContain("max_read=131072"); + expect(a).toContain("max_read=524288"); const b = buildFuseOptionString({ WSD_FUSE_MAX_WRITE: "-1" }); - expect(b).toContain("max_write=131072"); + expect(b).toContain("max_write=524288"); }); test("keeps auto_cache when WSD_FUSE_AUTO_CACHE is truthy or unset", () => { diff --git a/packages/wsd/src/fuse/options.ts b/packages/wsd/src/fuse/options.ts index 46f3a9b3..630f434a 100644 --- a/packages/wsd/src/fuse/options.ts +++ b/packages/wsd/src/fuse/options.ts @@ -17,9 +17,9 @@ // file shows up immediately to a process that probed before it // existed. use_ino tells the kernel to trust the inode numbers // returned by getattr, which is required for hardlinks to stat as the -// same inode. big_writes plus 128 KiB max_read and max_write match the -// historical sizing; experiments with larger values didn't move the -// numbers. +// same inode. big_writes plus 512 KiB max_read and max_write match +// the dofs CHUNK_SIZE so a single FUSE read maps to a single chunk +// fetch instead of four 128 KiB slices of the same blob. // // Every default is opt-out via the matching WSD_FUSE_* env var. // Setting an option to "0", "false", "no", "off", or "" turns it @@ -29,8 +29,11 @@ // libfuse 2.9 fails the whole mount with "unknown option" when it sees // them. A typo in WSD_FUSE_EXTRA_OPTS shouldn't take the daemon down. -const DEFAULT_MAX_READ = 131072; -const DEFAULT_MAX_WRITE = 131072; +// 512 KiB matches the dofs CHUNK_SIZE so a single FUSE read maps to a +// single chunk fetch. Earlier defaults at 128 KiB issued four reads +// per chunk and four SQL lookups for the same blob. +const DEFAULT_MAX_READ = 524288; +const DEFAULT_MAX_WRITE = 524288; const DEFAULT_AUTO_CACHE = true; const DEFAULT_ATTR_TIMEOUT = "1"; const DEFAULT_ENTRY_TIMEOUT = "1"; From 1db7fb81cfcc2c6b61712662def32912a56b2069 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:16:29 +0000 Subject: [PATCH 23/31] dofs: cache vfs_blob_bytes payloads by hash Reads against chunk-backed files used to fetch the same blob bytes\nrepeatedly: FUSE issues smaller reads than CHUNK_SIZE, and dedup'd\nblobs (e.g. a file of zeroes) collapse to a single row that every\nchunk's hash points at. The previous deployed bench measured pure\nread 64 MiB at 76x of tmpfs largely because of these repeats.\n\nAdd a small per-Database LRU cache keyed by hash. vfs_blob_bytes is\ncontent-addressed and immutable, so a cached payload stays valid\nfor the life of the database; new writes produce new hashes rather\nthan overwriting an existing entry.\n\nWire the cache into the hot read sites: readFile (streaming and\nstring), readRangeSync, provider.readFileSync, and the\nreadChunkBytes helper that powers write-path read-modify-write of\npartial chunks. Sync apply/push paths are left alone because they\nare not in the read hot loop. --- packages/dofs/src/fs/blobCache.test.ts | 56 +++++++++++++++++ packages/dofs/src/fs/blobCache.ts | 84 ++++++++++++++++++++++++++ packages/dofs/src/fs/readFile.ts | 32 ++++------ packages/dofs/src/fs/writeFile.ts | 10 ++- packages/dofs/src/provider.ts | 12 ++-- 5 files changed, 161 insertions(+), 33 deletions(-) create mode 100644 packages/dofs/src/fs/blobCache.test.ts create mode 100644 packages/dofs/src/fs/blobCache.ts diff --git a/packages/dofs/src/fs/blobCache.test.ts b/packages/dofs/src/fs/blobCache.test.ts new file mode 100644 index 00000000..a350ea55 --- /dev/null +++ b/packages/dofs/src/fs/blobCache.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it, vi } from "vitest"; + +import type { Database } from "../storage.js"; +import { clearBlobCache, getBlobBytes } from "./blobCache.js"; +import { readRangeSync } from "./readFile.js"; +import { withDB } from "./with-db.js"; +import { CHUNK_SIZE, writeFileSync } from "./writeFile.js"; + +describe("blobCache", () => { + it("reuses bytes for the same hash across calls", async () => { + await withDB(async (db) => { + writeFileSync(db, "/seed.bin", new Uint8Array(CHUNK_SIZE).fill(7), {}, () => 1); + // Pull the chunk hash out of vfs_chunks so we can hit the + // cache helper directly without going through readFile. + const row = db.one<{ hash: Uint8Array }>( + "SELECT hash FROM vfs_chunks WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", + "seed.bin", + ); + expect(row).toBeDefined(); + const hash = row?.hash as Uint8Array; + + // First call populates the cache; the second returns the + // exact same Uint8Array reference rather than re-querying. + const first = getBlobBytes(db, hash); + expect(first).toBeInstanceOf(Uint8Array); + const second = getBlobBytes(db, hash); + expect(second).toBe(first); + }); + }); + + it("readRangeSync avoids repeating vfs_blob_bytes lookups on sequential reads", async () => { + await withDB(async (db) => { + // 4 MiB of repeated content → one dedup'd blob in the store. + // A sequential read in 128 KiB windows used to issue one + // SELECT bytes per window, even though every window came out + // of the same blob. + const payload = new Uint8Array(CHUNK_SIZE * 8).fill(3); + writeFileSync(db, "/big.bin", payload, {}, () => 1); + clearBlobCache(db); + + const spy = vi.spyOn(db, "one"); + const window = 128 * 1024; + for (let offset = 0; offset < payload.byteLength; offset += window) { + readRangeSync(db, "/big.bin", offset, window); + } + const blobLookups = spy.mock.calls.filter( + ([query]) => typeof query === "string" && query.includes("vfs_blob_bytes"), + ).length; + spy.mockRestore(); + + // 8 distinct chunks were written and they all share one + // hash; we should fetch the blob bytes at most once. + expect(blobLookups).toBeLessThanOrEqual(1); + }); + }); +}); diff --git a/packages/dofs/src/fs/blobCache.ts b/packages/dofs/src/fs/blobCache.ts new file mode 100644 index 00000000..16451d3c --- /dev/null +++ b/packages/dofs/src/fs/blobCache.ts @@ -0,0 +1,84 @@ +// In-process LRU cache of vfs_blob_bytes payloads, keyed by hash. +// +// FUSE reads up to 128 KiB at a time (the kernel's default max_read); +// our chunk size is 512 KiB. A sequential read of a chunk-backed file +// re-fetches the same blob 4x by default. Worse, a 64 MiB file of +// repeated content (e.g. `dd if=/dev/zero`) deduplicates to a single +// blob in vfs_blobs, and we then re-fetch that one blob 512 times +// over the lifetime of one read pass. +// +// vfs_blob_bytes is content-addressed and immutable: a stored +// (hash, bytes) pair never changes for the life of the database. +// That makes the cache trivially correct — any write that mutates a +// file produces new chunk rows with new hashes, never overwriting +// the bytes the cache holds. +// +// The cache is bounded (CHUNK_CACHE_MAX_ENTRIES) and per-Database so +// independent test databases don't pollute each other. Eviction is +// LRU; access moves an entry to the most-recent position. + +import type { Database } from "../storage.js"; + +// Number of distinct blob payloads kept in memory per Database. +// At 512 KiB per blob this caps the cache at ~8 MiB, large enough +// to hold a handful of hot chunks for sequential reads of large +// files without dominating process memory. +const CHUNK_CACHE_MAX_ENTRIES = 16; + +const caches = new WeakMap>(); + +function cacheFor(db: Database): Map { + let cache = caches.get(db); + if (cache === undefined) { + cache = new Map(); + caches.set(db, cache); + } + return cache; +} + +// Stringify a 32-byte hash so it can key a JS Map. Latin-1 +// preserves every byte exactly and avoids the allocation cost of +// hex encoding for what is a very hot path. +function hashKey(hash: Uint8Array): string { + let out = ""; + for (let i = 0; i < hash.byteLength; i++) { + out += String.fromCharCode(hash[i]); + } + return out; +} + +// Look up blob bytes by hash. Cache hit returns the cached +// Uint8Array directly (callers must not mutate it). Cache miss +// queries vfs_blob_bytes and stores the result. Returns undefined +// if the blob isn't in the store. +export function getBlobBytes(db: Database, hash: Uint8Array): Uint8Array | undefined { + const cache = cacheFor(db); + const key = hashKey(hash); + const cached = cache.get(key); + if (cached !== undefined) { + // Reinsert to move to the most-recent position. Map iteration + // order is insertion order, so this gives us LRU eviction for + // free without a separate doubly-linked list. + cache.delete(key); + cache.set(key, cached); + return cached; + } + const row = db.one<{ bytes: Uint8Array }>( + "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", + hash, + ); + if (row === undefined) return undefined; + cache.set(key, row.bytes); + while (cache.size > CHUNK_CACHE_MAX_ENTRIES) { + const first = cache.keys().next(); + if (first.done === true) break; + cache.delete(first.value); + } + return row.bytes; +} + +// Reset the cache for `db`. Tests use this to keep cache state from +// leaking between cases that share a Database constructor pattern. +export function clearBlobCache(db: Database): void { + caches.delete(db); +} diff --git a/packages/dofs/src/fs/readFile.ts b/packages/dofs/src/fs/readFile.ts index 97535f9a..73043f50 100644 --- a/packages/dofs/src/fs/readFile.ts +++ b/packages/dofs/src/fs/readFile.ts @@ -1,6 +1,7 @@ import { createWorkspaceError } from "../errors.js"; import { canonicalizePath } from "../path.js"; import type { Database } from "../storage.js"; +import { getBlobBytes } from "./blobCache.js"; import { resolveInode } from "./resolve.js"; import { getPendingWriteBufferByPath, getWriteBuffer } from "./writeBuffer.js"; import { CHUNK_SIZE } from "./writeFile.js"; @@ -92,15 +93,12 @@ export async function readFile( let offset = 0; const touched = now(); for (const chunk of chunks) { - const row = db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { + const bytes = getBlobBytes(db, chunk.hash); + if (bytes === undefined) { throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path); } - out.set(row.bytes, offset); - offset += row.bytes.byteLength; + out.set(bytes, offset); + offset += bytes.byteLength; } if (chunks.length > 0) { touchBlobs(db, chunks, touched); @@ -119,16 +117,13 @@ export async function readFile( return; } const chunk = chunks[i++]; - const row = db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { + const bytes = getBlobBytes(db, chunk.hash); + if (bytes === undefined) { controller.error(createWorkspaceError("EIO", `missing blob bytes for ${path}`, path)); return; } db.run("UPDATE vfs_blobs SET last_seen = ? WHERE hash = ?", now(), chunk.hash); - controller.enqueue(row.bytes); + controller.enqueue(bytes); }, }); } @@ -193,17 +188,14 @@ export function readRangeSync( idx, ); if (chunk === undefined) continue; - const row = db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { + const bytes = getBlobBytes(db, chunk.hash); + if (bytes === undefined) { throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path); } const srcStart = Math.max(0, offset - start); - const srcEnd = Math.min(row.bytes.byteLength, end - start); + const srcEnd = Math.min(bytes.byteLength, end - start); if (srcEnd <= srcStart) continue; - out.set(row.bytes.subarray(srcStart, srcEnd), written); + out.set(bytes.subarray(srcStart, srcEnd), written); written += srcEnd - srcStart; } return written === out.byteLength ? out : out.subarray(0, written); diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 723a14c8..34452d4a 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -6,6 +6,7 @@ import { ROOT_INODE } from "../schema/index.js"; import type { Database } from "../storage.js"; import { stageBlob } from "../sync/blobs.js"; import { buildManifest } from "../sync/manifests.js"; +import { getBlobBytes } from "./blobCache.js"; import { assertNotReadOnly } from "./mount-guard.js"; import { allocatePendingInode, @@ -346,14 +347,11 @@ function readChunkBytes(db: Database, inode: number, idx: number): Uint8Array { idx, ); if (chunk === undefined) return new Uint8Array(); - const row = db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { + const bytes = getBlobBytes(db, chunk.hash); + if (bytes === undefined) { throw createWorkspaceError("EIO", "missing blob bytes"); } - return row.bytes; + return bytes; } function resolveFileInode(db: Database, path: string): { inode: number; mode: number } { diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index de7ff310..77268257 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -8,6 +8,7 @@ // I/O, truncate, symlinks, watch). import { createWorkspaceError } from "./errors.js"; +import { getBlobBytes } from "./fs/blobCache.js"; import { link as linkImpl } from "./fs/link.js"; import type { MkdirOptions } from "./fs/mkdir.js"; import { mkdir as mkdirImpl } from "./fs/mkdir.js"; @@ -434,15 +435,12 @@ export class SQLiteWorkspaceProvider { const out = Buffer.alloc(total); let offset = 0; for (const chunk of chunks) { - const row = this.db.one<{ bytes: Uint8Array }>( - "SELECT bytes FROM vfs_blob_bytes WHERE hash = ?", - chunk.hash, - ); - if (row === undefined) { + const bytes = getBlobBytes(this.db, chunk.hash); + if (bytes === undefined) { throw createWorkspaceError("EIO", `missing blob bytes for ${path}`, path); } - out.set(row.bytes, offset); - offset += row.bytes.byteLength; + out.set(bytes, offset); + offset += bytes.byteLength; } return encoding ? out.toString(encoding) : out; } From 417c07e00576d00fa2962e38e35042e7b9cd7835 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:38:02 +0000 Subject: [PATCH 24/31] docs: add a performance section to the README Capture the latest fs-bench and full sandbox-sdk npm install numbers\nfrom the wsd-container example on a standard-2 Cloudflare Container\n(1 vCPU, 6 GiB memory, 12 GB disk). Compare wsd against an in-memory\ntmpfs and against the container's ext4 disk so readers see the\nrealistic baseline for general usage, not just the tmpfs ratio. Call\nout which scenarios beat real disk (metadata-heavy work) and which\nstill lag (large sequential I/O, where chunk hashing dominates). --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index cfdfbb68..d2dea2ef 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,85 @@ package-specific status and usage notes. (`@cloudflare/workspace`) — the top-level Workspace package consumed by Durable Objects. Work in progress. +## Performance + +Numbers from `script/fs-bench.sh` and a full +`npm install` of [`cloudflare/sandbox-sdk`](https://github.com/cloudflare/sandbox-sdk) +(854 packages, 36,675 files), running +[`examples/wsd-container`](examples/wsd-container) on a Cloudflare +Containers **standard-2** instance (1 vCPU, 6 GiB memory, 12 GB disk). +The wsd FUSE mount lives at `/workspace`; the comparison columns are +an in-memory `tmpfs` at `/tmp` and the container's ext4 root disk at +`/var/tmp`. + +Ratios are `wsd / baseline` — lower is faster, values below 1.0 mean +wsd beats the baseline. + +### `fs-bench` (REPS=3, WARMUP=1, randomized targets) + +| Scenario | wsd | tmpfs | tmpfs ratio | ext4 disk | disk ratio | +|---|---:|---:|---:|---:|---:| +| **tiny-file churn** | | | | | | +| create 1000 files | 560.6 ms | 83.2 ms | 6.7x | 303.2 ms | 1.85x | +| stat 1000 files | 1971.9 ms | 1324.2 ms | 1.49x | 2659.3 ms | **0.91x** | +| rm 1000 files | 827.7 ms | 322.7 ms | 2.56x | 1281.8 ms | **0.66x** | +| **directory traversal** | | | | | | +| mkdir tree (10×10×10) | 1597.5 ms | 1585.7 ms | 1.01x | 3034.7 ms | **0.74x** | +| find tree | 1813.6 ms | 1819.9 ms | 1.00x | 4404.2 ms | **0.72x** | +| **large file I/O** | | | | | | +| write 64 MiB | 230.6 ms | 47.3 ms | 4.87x | 16.8 ms | 16.93x | +| copy 64 MiB | 1037.2 ms | 37.4 ms | 27.75x | 39.8 ms | 40.46x | +| read 64 MiB | 437.5 ms | 22.6 ms | 19.33x | 25.6 ms | 39.72x | +| pure read 64 MiB | 263.1 ms | 8.3 ms | 31.54x | 8.5 ms | 30.26x | +| pure copy 64 MiB | 852.9 ms | 21.7 ms | 39.27x | 22.0 ms | 41.47x | +| overwrite 64 MiB | 272.6 ms | 8.3 ms | 32.91x | 8.5 ms | 43.35x | +| **git** | | | | | | +| git init + commit 100 files | 459.2 ms | 40.3 ms | 9.56x | 635.4 ms | **0.72x** | +| git clone (shallow, ~1MB) | 549.1 ms | 421.0 ms | 1.30x | 576.2 ms | **0.84x** | +| **npm** | | | | | | +| npm init + tiny install | 598.5 ms | 630.7 ms | **0.95x** | 630.7 ms | **0.95x** | + +### Full `cloudflare/sandbox-sdk` `npm install` + +| Target | Duration | +|---|---:| +| tmpfs (`/tmp`) | 34.3 s | +| wsd FUSE (`/workspace`) | 124.7 s | +| ext4 disk (`/var/tmp`) | 63.9 s | + +wsd is ~2x slower than the container's ext4 disk for the full +`npm install`, and ~3.6x slower than tmpfs. The disk comparison is +the more realistic baseline for general usage. + +### Where wsd is faster than the disk baseline + +The in-memory inode store beats real disk on metadata-heavy work: +`stat`, `rm`, `mkdir tree`, `find tree`, `git init`, `git clone`, +`npm init`. Those eight scenarios cover most of the day-to-day cost +of tools like `git status`, module resolution, and incremental +builds. + +### Where wsd is slower + +Large sequential file I/O. The wsd write path hashes each +[`CHUNK_SIZE`](packages/dofs/src/fs/writeFile.ts) (512 KiB) chunk +into a content-addressed blob store on every release; that's how +the Durable Object can sync only the chunks that changed and +deduplicate identical content. The cost lands on raw +`dd`-style throughput numbers but rarely on real developer +workloads, which is why `npm init + tiny install` matches the disk +baseline despite `pure read 64 MiB` being 30x slower. + +### Reproducing + +```bash +bash script/run-fs-bench.sh +``` + +or against a deployed `wsd-container` instance, upload +[`script/fs-bench.sh`](script/fs-bench.sh) and run it with +`MOUNT=/workspace BASE=/tmp`. + ## Documentation - [`docs/`](docs/README.md) — design specification. Forward-looking; From 87f30d94f566ab832cfa940958e7cdfe830be5e8 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:52:00 +0000 Subject: [PATCH 25/31] docs: refresh README and schema docs for the buffered-write surface Drop the stale FUSE buffer flushing section from packages/wsd/README.md\nthat described the per-file FileEntry staging buffer with release/flush/\nfsync spills. Replace it with the current model: the FUSE driver opens\na DOFS write buffer on create/open, mutates it through writes and\ntruncates, and commits chunk rows in one transaction at release. Add\nthe /__wsd/stats endpoint to the endpoint list with a short note on\nwhen to reach for it.\n\nUpdate packages/dofs/README.md to enumerate the buffered-write\nsurface (openWriteBufferForCreateSync, openWriteBufferSync,\nreleaseWriteBufferSync, writeRangeSync, truncateFileSync,\ncreateFileSync, readRangeSync) and the content-addressed blob cache,\nand drop a stale pointer to a no-longer-relevant document.\n\nAdd the cached vfs_nodes.size column to docs/03_filesystem_schema.md\nso the DDL block matches the shipped schema, and explain why it is\ndenormalised onto the node row. --- docs/03_filesystem_schema.md | 9 +++-- packages/dofs/README.md | 14 +++++++- packages/wsd/README.md | 70 +++++++++++++++++++++--------------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/docs/03_filesystem_schema.md b/docs/03_filesystem_schema.md index b3e35771..e8c01432 100644 --- a/docs/03_filesystem_schema.md +++ b/docs/03_filesystem_schema.md @@ -52,7 +52,8 @@ CREATE TABLE vfs_nodes ( mount_root TEXT, -- nullable; tags mount provenance stub_size INTEGER, -- non-null while a lazy stub manifest_hash BLOB, -- references vfs_manifests.hash - link_target TEXT -- non-null when type = 'symlink' + link_target TEXT, -- non-null when type = 'symlink' + size INTEGER NOT NULL DEFAULT 0 -- cached file size, kept in sync on writes ); CREATE INDEX vfs_nodes_by_rev ON vfs_nodes(rev); ``` @@ -61,7 +62,11 @@ One row per live inode. `mount_root` records the mount this row originated from, used for write-rejection and writable-mount mirroring. `stub_size` is non-null while the file is a lazy-mount stub whose bytes haven't been fetched yet — `stat()` reports it as the file size -and the first read fetches the bytes. +and the first read fetches the bytes. `size` denormalises the +chunk-sum file size onto the node row so `stat`, `lstat`, and the +positional read primitive can read it directly instead of running +`SUM(size) FROM vfs_chunks` on every call. Every write path stamps +it alongside `mode`/`mtime`/`rev`. The `vfs_nodes_by_rev` index supports `coalesceChanges`'s `WHERE rev > sinceRev` scan over live inodes, which the sync protocol diff --git a/packages/dofs/README.md b/packages/dofs/README.md index b4c688fd..a1cfefe7 100644 --- a/packages/dofs/README.md +++ b/packages/dofs/README.md @@ -35,7 +35,7 @@ export class WorkspaceDO extends DurableObject { } ``` -> The `src/fs/*` primitives (`mkdir`, `writeFile`, `readFile`, `rm`, `readdir`, `stat`, `find`, `ls`, `grep`, `symlink`, `readlink`, `gc`, `watch`) are not re-exported from the package root yet — they are consumed in-tree by `SQLiteWorkspaceProvider` and by the sync `applyChanges` path. On the node side, instantiate `SQLiteWorkspaceProvider` (the `@platformatic/vfs` adapter) for a familiar node:fs-shaped surface; this is what `@cloudflare/workspace-wsd` mounts via FUSE. A higher-level DO-side `Workspace` class with the `fs`/`shell`/`push`/`pull` surface described in [`../../docs/README.md`](../../docs/README.md) is still future work — see [`../../PLAN.md`](../../PLAN.md). +> The `src/fs/*` primitives (`mkdir`, `writeFile`, `readFile`, `rm`, `readdir`, `stat`, `find`, `ls`, `grep`, `symlink`, `readlink`, `gc`, `watch`) are not re-exported from the package root yet — they are consumed in-tree by `SQLiteWorkspaceProvider` and by the sync `applyChanges` path. On the node side, instantiate `SQLiteWorkspaceProvider` (the `@platformatic/vfs` adapter) for a familiar node:fs-shaped surface; this is what `@cloudflare/workspace-wsd` mounts via FUSE. A higher-level DO-side `Workspace` class with the `fs`/`shell`/`push`/`pull` surface described in [`../../docs/README.md`](../../docs/README.md) is still future work. ## Implementation status @@ -46,4 +46,16 @@ export class WorkspaceDO extends DurableObject { - `SQLiteTestStorage` (backed by `node:sqlite`) available from `./testing` for unit tests against a real in-memory database; `RecordingStorage` available from the package root for workerd-safe schema assertions. - All filesystem primitives listed above are implemented and unit-tested. - `SQLiteWorkspaceProvider` (the `@platformatic/vfs` adapter) implemented and exported from the package entrypoint; consumed by `@cloudflare/workspace-wsd`. +- Buffered-write surface for the FUSE driver: `createFileSync`, + `writeRangeSync`, `truncateFileSync`, `readRangeSync`, + `openWriteBufferSync`, `openWriteBufferForCreateSync`, and + `releaseWriteBufferSync` on the provider. The driver opens a buffer + on FUSE create/open, mutates it through subsequent writes and + truncates, and commits chunks in one transaction at release time. + Reads against the same database see the buffered bytes immediately. +- Content-addressed blob cache: `readFile`, `readRangeSync`, + `provider.readFileSync`, and the partial-chunk read-modify-write + helper share a per-`Database` LRU keyed by `vfs_blob_bytes.hash`. + Repeated reads of dedup'd chunks (a file of zeroes, a re-used + package payload) skip SQLite after the first fetch. - Sync protocol building blocks implemented and exported; the typed RPC surface on top of them lives in `@cloudflare/workspace-rpc`. diff --git a/packages/wsd/README.md b/packages/wsd/README.md index 204c2e37..e4104c66 100644 --- a/packages/wsd/README.md +++ b/packages/wsd/README.md @@ -26,6 +26,7 @@ Current endpoints: - `GET /health` returns `200 OK` with `ok\n` once the HTTP server is up (it does not currently block on FUSE readiness). - `GET /__wsd/info` returns JSON with the selected FUSE backend, mount point, and bound port. +- `GET /__wsd/stats` returns JSON with DOFS table row counts, total inline and blob byte sizes, the orphan-blob subset, and process resident memory. Useful for watching how the store grows under load. - `GET /` returns `200 OK` with an empty JSON object: `{}`. - `POST /api` is a capnweb HTTP-batch RPC endpoint backed by `@cloudflare/workspace-rpc`. Non-POST methods return `405`. - `GET /ws` upgrades to a WebSocket carrying the same capnweb RPC surface. This is the container's primary sync carrier. @@ -41,33 +42,46 @@ Current filesystem support: - Optional host/DO synchronization: when `UPSTREAM_URL` is set, `wsd` opens a `SyncClient` from `@cloudflare/workspace-rpc/client` against that URL and runs the sync loop in the background. - No on-disk persistence yet — the in-memory VFS is rebuilt on each start, with sync pulling state back from the upstream when configured. -## FUSE buffer flushing - -The FUSE driver in `src/fuse/driver.ts` keeps a per-file in-memory -buffer (`files` Map) that `write` updates directly. The buffer is -the FUSE read path's source of truth, so reads stay fast even when -the backing `@platformatic/vfs` filesystem would otherwise need to -stream chunks from SQLite. - -Writes only become visible through the VFS surface (capnweb sync, -any host-side `@platformatic/vfs` consumer) once the driver spills -the buffer: - -- `release` — fires when the kernel drops the last reference to an - open file. The standard "close-and-forget" path. -- `flush` — fires on every `close(2)`, before `release`. Catches the - case where one process closes its handle while another keeps the - file open. -- `fsync(2)` — explicit user-driven sync. - -Plain `write` does not spill. A burst of small writes coalesces in -the buffer and pays the chunk/hash cost once on close. - -If you're tracking down "file looks empty over RPC" symptoms, -either the writer skipped `close(2)`/`fsync`, or one of the spill -ops is broken. The driver's `flushEntry` helper is the single place -VFS spills happen — a missing call site there is the most likely -cause. +## FUSE write model + +The FUSE driver in `src/fuse/driver.ts` is a thin adapter over the +DOFS provider. The byte owner is DOFS, not the FUSE driver: there +is no per-file staging buffer inside `wsd` for normal writes. + +When the backing provider advertises the buffered-write surface +(`openWriteBufferForCreateSync`, `openWriteBufferSync`, +`releaseWriteBufferSync`), the FUSE op map wires up to it directly: + +- `create` calls `openWriteBufferForCreateSync` on the provider. + No SQL runs yet — the new file is held in a path-keyed pending + buffer inside DOFS. +- `open` on an existing file calls `openWriteBufferSync` so subsequent + reads and writes route through the same inode-keyed cache. +- `write` and `truncate` mutate the DOFS write buffer directly. +- `read` serves from the buffer when one is open and dirty, otherwise + from the chunk store via `readRangeSync`. +- `release` commits the buffer to `vfs_chunks` in one transaction + per file and drops the entry. Pending-create entries do the INSERT, + dirent, and chunk rows together. + +Reads and stats during the open window see the buffered bytes. RPC +or sync callers reading through the VFS surface get the same view +as the in-flight FUSE writer. + +When the provider does not expose the buffered surface (legacy +in-process tests, alternate providers), the driver falls back to the +old staged path: per-file in-memory `FileEntry` buffer that spills on +`release` / `flush` / `fsync`. The fallback is exercised by tests +that explicitly disable the direct-write methods on the VFS. + +### `/__wsd/stats` for diagnosis + +When a workload is misbehaving — orphan blobs piling up, RSS growing +faster than expected, dirty buffers stuck — `GET /__wsd/stats` is the +first port of call. It returns table counts, total and orphan blob +byte sizes, inline byte totals, and the process's RSS/heap/external +figures. Poll it during a long-running install or test to watch +how the store grows. ## FUSE prerequisites @@ -137,4 +151,4 @@ Standalone binaries are release artifacts, not files published in the npm packag npm run build:bin --workspace=@cloudflare/workspace-wsd ``` -The binary is produced with Node's Single Executable Application (SEA) feature: `scripts/build-bin.mjs` bundles the CLI with `esbuild`, generates a SEA blob via `node --experimental-sea-config`, downloads the target's Node binary, and injects the blob with `postject`. macOS targets are stripped and re-signed ad-hoc. `fuse-native` prebuilds and `libfuse` are embedded as SEA assets per target. See `PLAN.md` Phase 3 for the full migration notes. +The binary is produced with Node's Single Executable Application (SEA) feature: `scripts/build-bin.mjs` bundles the CLI with `esbuild`, generates a SEA blob via `node --experimental-sea-config`, downloads the target's Node binary, and injects the blob with `postject`. macOS targets are stripped and re-signed ad-hoc. `fuse-native` prebuilds and `libfuse` are embedded as SEA assets per target. From b4d557db27f56499eeaf9f2bf3caa4c02d5e4323 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 12:52:00 +0000 Subject: [PATCH 26/31] dofs: add tests for cached size, pending creates, and flush-on-rename Cover the gaps that the recent storage and lifecycle changes left\nuncovered:\n\n- The v2 -> v3 schema migration backfills vfs_nodes.size from chunk\n row sums for each live file, leaves directories and empty files at\n zero, and reports the bumped schema_version.\n- The synchronous provider's stat reads the size column directly\n rather than re-summing vfs_chunks; the column is stamped on every\n writeFileSync.\n- openWriteBufferForCreateSync holds a new file in memory until\n release commits one transaction. The path-keyed pending cache is\n visible through stat, readRangeSync, and readdir but resolveInode\n returns null until release.\n- A second openWriteBufferForCreateSync against the same path throws\n EEXIST.\n- Provider linkSync, renameSync, and unlinkSync each commit a\n pending-create source first so the dirent operation always sees a\n real inode. --- packages/dofs/src/fs/writeBuffer.test.ts | 56 ++++++++++++++++++ packages/dofs/src/provider.test.ts | 53 +++++++++++++++++ packages/dofs/src/schema/index.test.ts | 74 ++++++++++++++++++++++++ 3 files changed, 183 insertions(+) diff --git a/packages/dofs/src/fs/writeBuffer.test.ts b/packages/dofs/src/fs/writeBuffer.test.ts index 99863096..74af8b1c 100644 --- a/packages/dofs/src/fs/writeBuffer.test.ts +++ b/packages/dofs/src/fs/writeBuffer.test.ts @@ -3,10 +3,12 @@ import { describe, expect, it } from "vitest"; import type { Database } from "../storage.js"; import { readRangeSync } from "./readFile.js"; import { resolveInode } from "./resolve.js"; +import { stat } from "./stat.js"; import { withDB } from "./with-db.js"; import { CHUNK_SIZE, createFileSync, + openWriteBufferForCreateSync, openWriteBufferSync, releaseWriteBufferSync, truncateFileSync, @@ -144,3 +146,57 @@ describe("buffered write lifecycle", () => { }); }); }); + +describe("deferred-create lifecycle", () => { + it("holds the file in memory until release commits one transaction", async () => { + await withDB(async (db) => { + openWriteBufferForCreateSync(db, "/pending.txt", { mode: 0o600 }, () => 1000); + + // No SQL row exists yet but the path is reachable via the + // path-keyed pending cache: stat, read, and write all see it. + expect(stat(db, "/pending.txt").size).toBe(0); + writeRangeSync(db, "/pending.txt", bytesOf("hello"), 0, {}, () => 1001); + expect(stat(db, "/pending.txt").size).toBe(5); + expect(new TextDecoder().decode(readRangeSync(db, "/pending.txt", 0, 5))).toBe("hello"); + expect(resolveInode(db, "/pending.txt")).toBeNull(); + expect(blobCount(db)).toBe(0); + + releaseWriteBufferSync(db, "/pending.txt", () => 1100); + + // Release committed the INSERT, dirent, and chunk rows in one + // transaction; the path now resolves to a real inode and the + // stat sees the persisted size. + const node = resolveInode(db, "/pending.txt"); + expect(node?.type).toBe("file"); + expect(node?.mode).toBe(0o600); + expect(stat(db, "/pending.txt").size).toBe(5); + expect(blobCount(db)).toBe(1); + expect(orphanBlobCount(db)).toBe(0); + }); + }); + + it("rejects a second openWriteBufferForCreateSync against the same path", async () => { + await withDB(async (db) => { + openWriteBufferForCreateSync(db, "/dupe.txt", {}, () => 1000); + expect(() => openWriteBufferForCreateSync(db, "/dupe.txt", {}, () => 1001)).toThrowError( + expect.objectContaining({ code: "EEXIST" }), + ); + }); + }); + + it("surfaces pending files in readdir before release", async () => { + await withDB(async (db) => { + const { readdir } = await import("./readdir.js"); + const { mkdir } = await import("./mkdir.js"); + mkdir(db, "/d", {}, () => 1000); + openWriteBufferForCreateSync(db, "/d/pending.txt", {}, () => 1001); + + const names = readdir(db, "/d").map((entry) => entry.name); + expect(names).toEqual(["pending.txt"]); + + releaseWriteBufferSync(db, "/d/pending.txt", () => 1100); + const after = readdir(db, "/d").map((entry) => entry.name); + expect(after).toEqual(["pending.txt"]); + }); + }); +}); diff --git a/packages/dofs/src/provider.test.ts b/packages/dofs/src/provider.test.ts index 858f877b..050e0dc1 100644 --- a/packages/dofs/src/provider.test.ts +++ b/packages/dofs/src/provider.test.ts @@ -283,3 +283,56 @@ describe("SQLiteWorkspaceProvider — unimplemented surface (stubs)", () => { }); }); }); + +describe("SQLiteWorkspaceProvider — pending-create flush on rename/link/unlink", () => { + it("linkSync commits a pending-create source before adding the second dirent", async () => { + await withProvider((p) => { + p.openWriteBufferForCreateSync("/src.txt", { mode: 0o644 }); + p.writeRangeSync("/src.txt", Buffer.from("linked"), 0); + p.linkSync("/src.txt", "/dst.txt"); + + expect((p.readFileSync("/dst.txt") as Buffer).toString()).toBe("linked"); + expect(p.statSync("/src.txt").ino).toBe(p.statSync("/dst.txt").ino); + expect(p.statSync("/src.txt").nlink).toBe(2); + + p.releaseWriteBufferSync("/src.txt"); + expect((p.readFileSync("/src.txt") as Buffer).toString()).toBe("linked"); + }); + }); + + it("renameSync commits a pending-create source before moving the dirent", async () => { + await withProvider((p) => { + p.openWriteBufferForCreateSync("/from.txt", { mode: 0o644 }); + p.writeRangeSync("/from.txt", Buffer.from("moved"), 0); + p.renameSync("/from.txt", "/to.txt"); + + expect((p.readFileSync("/to.txt") as Buffer).toString()).toBe("moved"); + expect(p.existsSync("/from.txt")).toBe(false); + }); + }); + + it("unlinkSync commits then removes a pending-create file", async () => { + await withProvider((p) => { + p.openWriteBufferForCreateSync("/gone.txt", { mode: 0o644 }); + p.writeRangeSync("/gone.txt", Buffer.from("bye"), 0); + p.unlinkSync("/gone.txt"); + expect(p.existsSync("/gone.txt")).toBe(false); + }); + }); +}); + +describe("SQLiteWorkspaceProvider — cached vfs_nodes.size", () => { + it("stat reads size from vfs_nodes without summing chunks", async () => { + await withProvider((p) => { + const payload = Buffer.alloc(123, 0x41); + p.writeFileSync("/sized.bin", payload); + expect(p.statSync("/sized.bin").size).toBe(123); + // Read the column directly to confirm the write path stamps it. + const row = p.db.one<{ size: number }>( + "SELECT size FROM vfs_nodes WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", + "sized.bin", + ); + expect(row?.size).toBe(123); + }); + }); +}); diff --git a/packages/dofs/src/schema/index.test.ts b/packages/dofs/src/schema/index.test.ts index 312bd612..9af00223 100644 --- a/packages/dofs/src/schema/index.test.ts +++ b/packages/dofs/src/schema/index.test.ts @@ -131,6 +131,80 @@ describe("initializeSchema", () => { ).toThrow(/CHECK constraint/); }); + it("backfills vfs_nodes.size from chunk sums on the v2 -> v3 upgrade", () => { + // Stage a database at the v2 shape: vfs_nodes without the + // `size` column, schema_version = 2. The migration adds the + // column with a default of 0 and then UPDATEs it from the + // SUM of vfs_chunks.size for each file inode. + const storage = new SQLiteTestStorage(); + const db = new Database(storage); + + db.transactionSync(() => { + db.run( + `CREATE TABLE vfs_meta ( + k TEXT PRIMARY KEY, + v INTEGER NOT NULL + )`, + ); + db.run( + `CREATE TABLE vfs_nodes ( + inode INTEGER PRIMARY KEY AUTOINCREMENT, + type TEXT NOT NULL CHECK(type IN ('file','dir','symlink')), + mode INTEGER NOT NULL DEFAULT 493, + mtime INTEGER NOT NULL, + rev INTEGER NOT NULL DEFAULT 0, + mount_root TEXT, + stub_size INTEGER, + manifest_hash BLOB, + link_target TEXT + )`, + ); + db.run( + `CREATE TABLE vfs_chunks ( + inode INTEGER NOT NULL, + idx INTEGER NOT NULL, + hash BLOB NOT NULL, + size INTEGER NOT NULL, + PRIMARY KEY (inode, idx) + )`, + ); + // A live file with two chunks summing to 7 bytes, a live dir, + // and a live file with no chunks (empty file). + db.run( + `INSERT INTO vfs_nodes (inode, type, mode, mtime, rev) VALUES + (1, 'dir', 493, 0, 0), + (2, 'file', 420, 0, 0), + (3, 'file', 420, 0, 0)`, + ); + db.run( + "INSERT INTO vfs_chunks (inode, idx, hash, size) VALUES (?, ?, ?, ?)", + 2, + 0, + new Uint8Array(32), + 3, + ); + db.run( + "INSERT INTO vfs_chunks (inode, idx, hash, size) VALUES (?, ?, ?, ?)", + 2, + 1, + new Uint8Array(32), + 4, + ); + db.run("INSERT INTO vfs_meta (k, v) VALUES (?, ?)", "schema_version", 2); + }); + + initializeSchema(db, () => 0); + + const sizes = db.all<{ inode: number; size: number }>( + "SELECT inode, size FROM vfs_nodes ORDER BY inode", + ); + expect(sizes).toEqual([ + { inode: 1, size: 0 }, + { inode: 2, size: 7 }, + { inode: 3, size: 0 }, + ]); + }); + it("is idempotent across repeat calls", () => { const storage = new SQLiteTestStorage(); const db = new Database(storage); From 152eef25188dae1f9cbe64573718569b50a92efa Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 13:41:11 +0000 Subject: [PATCH 27/31] dofs: bridge the write buffer cache through link/rename/unlink Three dirent-mutating provider paths only half-bridged the\nwrite-buffer cache, allowing real corruption modes.\n\n- linkSync flushed a pending-create source but ignored a\n pending-create destination. link's dirent check sees no row at\n newPath, the link succeeds, and the release on the displaced\n pending buffer re-checks the dirent in commitPendingBuffer,\n throws EEXIST, drops the entry, and silently loses the user's\n bytes. Flush newPath too so EEXIST surfaces immediately and the\n pending bytes land as a real file.\n- renameSync's overwrite branch deleted the displaced inode's\n chunks and node row but left any open write buffer pointing at\n the now-dead inode. The eventual release committed chunks against\n a missing row (0-row UPDATE) and the user lost the bytes\n silently. Drop the buffer for the displaced inode when its last\n link disappears.\n- unlinkSync flushed pending state then ran rm but left the\n inode-keyed buffer cache entry dangling on a freshly-deleted\n inode. Snapshot the target inode before rm, and if rm removed\n the last link drop the buffer too. Hardlinks that keep the inode\n alive keep the buffer alive.\n\nTests pin all three contracts: link into a pending destination,\nrename overwrite of an open destination, unlink of the last link,\nand unlink of a hardlinked file. --- packages/dofs/src/provider.test.ts | 84 ++++++++++++++++++++++++++++++ packages/dofs/src/provider.ts | 36 +++++++++++-- 2 files changed, 116 insertions(+), 4 deletions(-) diff --git a/packages/dofs/src/provider.test.ts b/packages/dofs/src/provider.test.ts index 050e0dc1..e2e3cb9a 100644 --- a/packages/dofs/src/provider.test.ts +++ b/packages/dofs/src/provider.test.ts @@ -319,6 +319,90 @@ describe("SQLiteWorkspaceProvider — pending-create flush on rename/link/unlink expect(p.existsSync("/gone.txt")).toBe(false); }); }); + + it("linkSync flushes a pending-create destination before colliding", async () => { + // Pending /dst is committed before link's existence check + // runs, so the user sees a normal EEXIST against a real inode + // rather than silently losing the pending bytes when the later + // release would have tripped its own EEXIST against the link's + // dirent. + await withProvider((p) => { + p.writeFileSync("/src.txt", "src bytes"); + p.openWriteBufferForCreateSync("/dst.txt", { mode: 0o644 }); + p.writeRangeSync("/dst.txt", Buffer.from("pending dst"), 0); + + expect(() => p.linkSync("/src.txt", "/dst.txt")).toThrowError( + expect.objectContaining({ code: "EEXIST" }), + ); + + // /dst.txt now exists with the previously-pending bytes; the + // release-after-collision finds the inode it expects and is a + // no-op rather than a data loss. + expect((p.readFileSync("/dst.txt") as Buffer).toString()).toBe("pending dst"); + expect(() => p.releaseWriteBufferSync("/dst.txt")).not.toThrow(); + expect((p.readFileSync("/dst.txt") as Buffer).toString()).toBe("pending dst"); + }); + }); + + it("renameSync overwrite evicts the displaced destination's buffer", async () => { + // Open a buffer over an existing /dst, mutate it, then overwrite + // /dst via rename. The buffer's inode is gone from SQL after + // rename; release must not commit chunks against the dead row + // and must not leave a dangling cache entry. + await withProvider((p) => { + p.writeFileSync("/src.txt", "src bytes"); + p.writeFileSync("/dst.txt", "dst bytes"); + const dstInodeBefore = p.statSync("/dst.txt").ino; + p.openWriteBufferSync("/dst.txt"); + p.writeRangeSync("/dst.txt", Buffer.from("dirty"), 0); + + p.renameSync("/src.txt", "/dst.txt"); + + // The path now resolves to the renamed source's inode, not + // the displaced one. Release is a no-op on the now-gone + // displaced inode; the renamed file's bytes are unchanged. + expect(p.statSync("/dst.txt").ino).not.toBe(dstInodeBefore); + expect((p.readFileSync("/dst.txt") as Buffer).toString()).toBe("src bytes"); + expect(() => p.releaseWriteBufferSync("/dst.txt")).not.toThrow(); + expect((p.readFileSync("/dst.txt") as Buffer).toString()).toBe("src bytes"); + }); + }); + + it("unlinkSync drops the inode-keyed buffer when the last link disappears", async () => { + const { getWriteBuffer } = await import("./fs/writeBuffer.js"); + await withProvider((p) => { + p.writeFileSync("/a.txt", "hello"); + const inode = p.statSync("/a.txt").ino; + p.openWriteBufferSync("/a.txt"); + p.writeRangeSync("/a.txt", Buffer.from("WORLD"), 0); + // Buffer is staged in the inode-keyed cache. + expect(getWriteBuffer(p.db, inode)).toBeDefined(); + p.unlinkSync("/a.txt"); + // unlink removed the last link, so the inode row is gone and + // the buffer must not be cached against the dead inode. + expect(p.existsSync("/a.txt")).toBe(false); + expect(getWriteBuffer(p.db, inode)).toBeUndefined(); + }); + }); + + it("unlinkSync keeps the buffer alive when a hardlink remains", async () => { + const { getWriteBuffer } = await import("./fs/writeBuffer.js"); + await withProvider((p) => { + p.writeFileSync("/a.txt", "shared"); + p.linkSync("/a.txt", "/b.txt"); + const inode = p.statSync("/a.txt").ino; + p.openWriteBufferSync("/a.txt"); + p.writeRangeSync("/a.txt", Buffer.from("UPDATED"), 0); + p.unlinkSync("/a.txt"); + // /b.txt still references the inode; the buffer survives in + // the inode-keyed cache and a release through the remaining + // name commits the staged bytes. + expect(p.existsSync("/b.txt")).toBe(true); + expect(getWriteBuffer(p.db, inode)).toBeDefined(); + p.releaseWriteBufferSync("/b.txt"); + expect((p.readFileSync("/b.txt") as Buffer).toString()).toBe("UPDATED"); + }); + }); }); describe("SQLiteWorkspaceProvider — cached vfs_nodes.size", () => { diff --git a/packages/dofs/src/provider.ts b/packages/dofs/src/provider.ts index 77268257..01764807 100644 --- a/packages/dofs/src/provider.ts +++ b/packages/dofs/src/provider.ts @@ -26,7 +26,11 @@ import { type WatchHandle, type WatchOptions, } from "./fs/watch.js"; -import { getPendingWriteBufferByPath, getWriteBuffer } from "./fs/writeBuffer.js"; +import { + deleteWriteBuffer, + getPendingWriteBufferByPath, + getWriteBuffer, +} from "./fs/writeBuffer.js"; import { createFileSync as createFileSyncImpl, flushPendingByPath, @@ -272,7 +276,21 @@ export class SQLiteWorkspaceProvider { // shape). The buffer's open handles continue to address bytes // through the inode-keyed cache. flushPendingByPath(this.db, path, this.now); + // Capture the target inode before rm runs so we can evict its + // write-buffer cache entry if rm removed the last link. Without + // this, a release-after-unlink leaves the buffer dangling on a + // dead inode and the eventual commit silently affects no rows. + const target = resolveInode(this.db, path, { followSymlinks: false }); rmImpl(this.db, path, {}); + if (target !== null) { + const stillAlive = this.db.scalar( + "SELECT inode FROM vfs_nodes WHERE inode = ?", + target.inode, + ); + if (stillAlive === undefined) { + deleteWriteBuffer(this.db, target.inode); + } + } } link(existingPath: string, newPath: string): Promise { @@ -281,10 +299,15 @@ export class SQLiteWorkspaceProvider { } linkSync(existingPath: string, newPath: string): void { - // Same shape as unlink: commit a still-pending source before - // adding the second dirent, otherwise link has nothing real to - // point at. + // Commit a still-pending source before adding the second dirent, + // otherwise link has nothing real to point at. Also commit a + // still-pending destination: link's existence check looks at + // dirents, so a pending buffer at newPath wouldn't trip it, and + // the eventual release on that pending buffer would re-check the + // dirent in commitPendingBuffer, throw EEXIST, drop the entry, + // and silently lose the user's bytes. flushPendingByPath(this.db, existingPath, this.now); + flushPendingByPath(this.db, newPath, this.now); linkImpl(this.db, existingPath, newPath); } @@ -372,6 +395,11 @@ export class SQLiteWorkspaceProvider { if ((remaining ?? 0) === 0) { this.db.run("DELETE FROM vfs_chunks WHERE inode = ?", existing.child_inode); this.db.run("DELETE FROM vfs_nodes WHERE inode = ?", existing.child_inode); + // The displaced inode is gone from SQL. Any open write + // buffer keyed by it would otherwise hold bytes pointed at + // a dead inode; release would then commit chunks against a + // missing row (0-row UPDATE, silent data loss). + deleteWriteBuffer(this.db, existing.child_inode); } } this.db.run( From b80cd161bdb51a5513b9d19da59e22cf688e0ffa Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 13:44:04 +0000 Subject: [PATCH 28/31] docs: refresh schema invariants and tidy stale naming The schema invariants block in docs/03_filesystem_schema.md still\ndescribed the pre-buffered-write contract: 'file row has either a\nlazy stub or manifest_hash NOT NULL plus chunks'. The buffered and\ndirect-write paths now commit chunks with manifest_hash = NULL and\nrely on sync walking vfs_chunks directly when no manifest is\npresent. Restate the invariant in two shapes (lazy stub vs\ncommitted file) and note that the manifest is opportunistic. Add a\nsize-column invariant covering the denormalisation that stat,\nlstat, and readRangeSync rely on.\n\nList chmodSync in the dofs README's enumeration of the\nbuffered-write provider surface so the public-surface bullet is\ncomplete.\n\nMark flushPendingByPath @internal: it exists only so provider\nlink/rename/unlink can bridge a pending-create buffer into the SQL\nworld ahead of a dirent operation, never as a consumer entry point.\n\nDrop 'inline' from a handful of test descriptions and source\ncomments that survived the inline_data column removal. --- docs/03_filesystem_schema.md | 21 +++++++++++++++++---- packages/dofs/README.md | 2 +- packages/dofs/src/fs/readRange.test.ts | 4 ++-- packages/dofs/src/fs/writeBuffer.ts | 2 +- packages/dofs/src/fs/writeFile.ts | 14 +++++++++----- packages/dofs/src/fs/writeRange.test.ts | 2 +- packages/dofs/src/sync/fetch.test.ts | 2 +- 7 files changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/03_filesystem_schema.md b/docs/03_filesystem_schema.md index e8c01432..d08d5791 100644 --- a/docs/03_filesystem_schema.md +++ b/docs/03_filesystem_schema.md @@ -238,10 +238,23 @@ DO reload doesn't re-list. - The root directory is always `inode = 1`, type `dir`, with no parent dirent. -- A `vfs_nodes` row with `type = 'file'` has either: - - `stub_size NOT NULL` and no `vfs_chunks` rows (lazy stub), **or** - - `manifest_hash NOT NULL`, a matching `vfs_manifests` row, and - one `vfs_chunks` row per chunk. +- A `vfs_nodes` row with `type = 'file'` is in one of two shapes: + - **lazy stub**: `stub_size NOT NULL`, no `vfs_chunks` rows. The + first read fetches the bytes and migrates the row to the + committed shape. + - **committed file**: zero or more `vfs_chunks` rows (one per + chunk; an empty file has zero). `manifest_hash` is optional. + When set, a matching `vfs_manifests` row lists the same chunk + hashes and lets sync skip the per-chunk fetch on receivers + that already have the manifest. When `NULL`, sync walks + `vfs_chunks` directly. The buffered-write path commits chunks + with `manifest_hash = NULL`; the legacy whole-file + `writeFileSync` path stamps a manifest. +- For every file row, `vfs_nodes.size = COALESCE(SUM(vfs_chunks.size), 0)` + over its `vfs_chunks` rows. Every write path stamps the column + in the same `UPDATE` that bumps `mode`/`mtime`/`rev`, so `stat`, + `lstat`, and `readRangeSync` can read it directly instead of + running the aggregate. - Every `vfs_chunks.hash` references an existing `vfs_blobs.hash`. - Every `vfs_blobs.hash` has a matching `vfs_blob_bytes` row. - Every `vfs_manifests.hash` referenced by diff --git a/packages/dofs/README.md b/packages/dofs/README.md index a1cfefe7..37965ea0 100644 --- a/packages/dofs/README.md +++ b/packages/dofs/README.md @@ -47,7 +47,7 @@ export class WorkspaceDO extends DurableObject { - All filesystem primitives listed above are implemented and unit-tested. - `SQLiteWorkspaceProvider` (the `@platformatic/vfs` adapter) implemented and exported from the package entrypoint; consumed by `@cloudflare/workspace-wsd`. - Buffered-write surface for the FUSE driver: `createFileSync`, - `writeRangeSync`, `truncateFileSync`, `readRangeSync`, + `writeRangeSync`, `truncateFileSync`, `readRangeSync`, `chmodSync`, `openWriteBufferSync`, `openWriteBufferForCreateSync`, and `releaseWriteBufferSync` on the provider. The driver opens a buffer on FUSE create/open, mutates it through subsequent writes and diff --git a/packages/dofs/src/fs/readRange.test.ts b/packages/dofs/src/fs/readRange.test.ts index 15f6521a..bad73801 100644 --- a/packages/dofs/src/fs/readRange.test.ts +++ b/packages/dofs/src/fs/readRange.test.ts @@ -5,7 +5,7 @@ import { withDB } from "./with-db.js"; import { CHUNK_SIZE, writeFileSync } from "./writeFile.js"; describe("readRangeSync", () => { - it("reads from inline files at non-zero offset", async () => { + it("reads small chunk-backed files at non-zero offset", async () => { await withDB((db) => { writeFileSync(db, "/inline.txt", new TextEncoder().encode("hello world"), {}, () => 1); @@ -14,7 +14,7 @@ describe("readRangeSync", () => { }); }); - it("clamps the inline read at end of file", async () => { + it("clamps the read at end of file", async () => { await withDB((db) => { writeFileSync(db, "/inline.txt", new TextEncoder().encode("abc"), {}, () => 1); diff --git a/packages/dofs/src/fs/writeBuffer.ts b/packages/dofs/src/fs/writeBuffer.ts index c9edca12..72ff047a 100644 --- a/packages/dofs/src/fs/writeBuffer.ts +++ b/packages/dofs/src/fs/writeBuffer.ts @@ -3,7 +3,7 @@ // Holds per-inode mutable byte buffers between an explicit open and // release. While a buffer is open, all reads and writes for that // inode go through the buffer rather than the SQLite blob/chunk -// store. Release commits the bytes to chunks/inline once per file +// store. Release commits the bytes to chunks once per file // and evicts the entry, so per-syscall writes no longer accumulate // orphan blob rows in the store. // diff --git a/packages/dofs/src/fs/writeFile.ts b/packages/dofs/src/fs/writeFile.ts index 34452d4a..eca85dbe 100644 --- a/packages/dofs/src/fs/writeFile.ts +++ b/packages/dofs/src/fs/writeFile.ts @@ -488,7 +488,7 @@ export function createFileSync( // Open a write buffer for an existing file. Subsequent writes, // truncates, and reads against the same Database operate on the // buffer instead of the SQLite chunk/blob store. Release commits -// the bytes back to chunks/inline. +// the bytes back to chunks. export function openWriteBufferSync(db: Database, path: string): void { const { path: canonical } = canonicalizePath(path); const pending = getPendingWriteBufferByPath(db, canonical); @@ -691,10 +691,14 @@ function commitPendingBuffer(db: Database, entry: WriteBufferEntry, now: () => n return realInode; } -// Commit a pending-create buffer identified by its canonical path, -// leaving the open count untouched. Used by link, rename, and unlink -// against a still-open file so the dirent operation sees a real -// inode. Returns true when a pending buffer was committed. +/** + * @internal + * Bridges a pending-create write buffer into the SQL world ahead of a + * dirent-mutating provider operation (link, rename, unlink). Leaves + * the open count untouched so a still-open handle keeps writing into + * the now-promoted buffer. Returns true when a pending buffer was + * committed. External callers should never invoke this directly. + */ export function flushPendingByPath(db: Database, path: string, now: () => number): boolean { const { path: canonical } = canonicalizePath(path); const entry = getPendingWriteBufferByPath(db, canonical); diff --git a/packages/dofs/src/fs/writeRange.test.ts b/packages/dofs/src/fs/writeRange.test.ts index bef6cd7e..1f952edf 100644 --- a/packages/dofs/src/fs/writeRange.test.ts +++ b/packages/dofs/src/fs/writeRange.test.ts @@ -94,7 +94,7 @@ describe("direct range writes", () => { }); }); - it("zero-fills sparse inline writes", async () => { + it("zero-fills sparse writes", async () => { await withDB(async (db) => { createFileSync(db, "/sparse.txt", {}, () => 1000); diff --git a/packages/dofs/src/sync/fetch.test.ts b/packages/dofs/src/sync/fetch.test.ts index 8270bce2..9ef3eaec 100644 --- a/packages/dofs/src/sync/fetch.test.ts +++ b/packages/dofs/src/sync/fetch.test.ts @@ -22,7 +22,7 @@ describe("fetch wire", () => { }); }); - it("fetchChanges and fetchObjects include inline direct writes", async () => { + it("fetchChanges and fetchObjects include small direct writes", async () => { await withDB(async (db) => { createFileSync(db, "/inline.txt", {}, () => 1); writeRangeSync(db, "/inline.txt", new TextEncoder().encode("inline direct"), 0, {}, () => 2); From 780699cf7dd5dedbefc84a0c4062bb7274952ce1 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 13:51:34 +0000 Subject: [PATCH 29/31] dofs,wsd: pin LRU eviction, multi-open buffers, cached size, and /__wsd/stats Round one of review surfaced a set of contracts that weren't yet\npinned by tests. None pointed at live bugs, but the surfaces are\nthe ones a future change is most likely to break silently.\n\nblobCache: two tests around the 16-entry LRU. The first inserts 17\ndistinct hashes and confirms the least-recently-used entry is\nevicted while the most recent stays cached. The second touches the\nLRU entry before adding the 17th hash, then asserts the touched\nentry survives and the new-LRU is evicted instead. Together they\npin both the bound and the recency update.\n\nwriteBuffer: a multi-open test that opens the same path twice,\nwrites dirty bytes, releases once (must not commit), and releases\nagain (must commit). It then re-opens with no writes and confirms\nthe content is unchanged. A second pair covers flushPendingByPath\ndirectly: promoting a still-open pending-create commits chunks but\nleaves the open count intact, so a later release commits the\nfinal bytes through the inode-keyed cache. A third returns false\nfor a path with no pending entry.\n\nprovider: the cached-size invariant gets five entry points instead\nof one - writeFileSync (first write and overwrite), writeRangeSync\nwith a sparse extend, truncateFileSync grow and shrink, buffered\nrelease, and async writeFile. Each one reads vfs_nodes.size\ndirectly to catch a path that updates content but forgets to\nstamp the column.\n\nwsd: a smoke test for the /__wsd/stats endpoint. It only asserts\nthe shape of the JSON body (table counts, orphan totals, process\nmemory) because the values themselves depend on the underlying\nstorage backend; the contract is that every field is present and\nnumeric. --- packages/dofs/src/fs/blobCache.test.ts | 69 +++++++++++++++++++++++ packages/dofs/src/fs/writeBuffer.test.ts | 71 ++++++++++++++++++++++++ packages/dofs/src/provider.test.ts | 69 ++++++++++++++++++++--- packages/wsd/src/cli/wsd.test.ts | 42 ++++++++++++++ 4 files changed, 242 insertions(+), 9 deletions(-) diff --git a/packages/dofs/src/fs/blobCache.test.ts b/packages/dofs/src/fs/blobCache.test.ts index a350ea55..37809f30 100644 --- a/packages/dofs/src/fs/blobCache.test.ts +++ b/packages/dofs/src/fs/blobCache.test.ts @@ -28,6 +28,75 @@ describe("blobCache", () => { }); }); + it("evicts the least-recently-used entry once 17 distinct hashes are cached", async () => { + await withDB(async (db) => { + // Stage 17 files with distinct content so each one gets a + // unique blob hash. The cache holds 16; the 17th fetch must + // evict the least-recently-used, which is the first hash. + const hashes: Uint8Array[] = []; + for (let i = 0; i < 17; i++) { + const path = `/distinct-${i}.bin`; + writeFileSync(db, path, new TextEncoder().encode(`payload-${i}`), {}, () => 1); + const row = db.one<{ hash: Uint8Array }>( + "SELECT hash FROM vfs_chunks WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", + `distinct-${i}.bin`, + ); + hashes.push(row?.hash as Uint8Array); + } + clearBlobCache(db); + + // First 16 fetches populate the cache. + for (let i = 0; i < 16; i++) getBlobBytes(db, hashes[i]); + // 17th fetch evicts the LRU (entry 0); spy to confirm the + // 18th fetch of hash 0 is a fresh SQL lookup, but hash 16 is + // cached and the 18th fetch of hash 1 (still the LRU) hits + // SQL too. + getBlobBytes(db, hashes[16]); + + const spy = vi.spyOn(db, "one"); + getBlobBytes(db, hashes[0]); // evicted, must hit SQL + getBlobBytes(db, hashes[16]); // hot, must NOT hit SQL + const lookups = spy.mock.calls.filter( + ([query]) => typeof query === "string" && query.includes("vfs_blob_bytes"), + ).length; + spy.mockRestore(); + + expect(lookups).toBe(1); + }); + }); + + it("moves a touched entry to most-recent so it survives eviction", async () => { + await withDB(async (db) => { + const hashes: Uint8Array[] = []; + for (let i = 0; i < 17; i++) { + const path = `/touched-${i}.bin`; + writeFileSync(db, path, new TextEncoder().encode(`touched-${i}`), {}, () => 1); + const row = db.one<{ hash: Uint8Array }>( + "SELECT hash FROM vfs_chunks WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", + `touched-${i}.bin`, + ); + hashes.push(row?.hash as Uint8Array); + } + clearBlobCache(db); + + for (let i = 0; i < 16; i++) getBlobBytes(db, hashes[i]); + // Touch hash 0 so it becomes most-recent. + getBlobBytes(db, hashes[0]); + // The 17th fetch should now evict hash 1, not hash 0. + getBlobBytes(db, hashes[16]); + + const spy = vi.spyOn(db, "one"); + getBlobBytes(db, hashes[0]); // touched, still cached + getBlobBytes(db, hashes[1]); // evicted, must hit SQL + const lookups = spy.mock.calls.filter( + ([query]) => typeof query === "string" && query.includes("vfs_blob_bytes"), + ).length; + spy.mockRestore(); + + expect(lookups).toBe(1); + }); + }); + it("readRangeSync avoids repeating vfs_blob_bytes lookups on sequential reads", async () => { await withDB(async (db) => { // 4 MiB of repeated content → one dedup'd blob in the store. diff --git a/packages/dofs/src/fs/writeBuffer.test.ts b/packages/dofs/src/fs/writeBuffer.test.ts index 74af8b1c..891f5e3e 100644 --- a/packages/dofs/src/fs/writeBuffer.test.ts +++ b/packages/dofs/src/fs/writeBuffer.test.ts @@ -199,4 +199,75 @@ describe("deferred-create lifecycle", () => { expect(after).toEqual(["pending.txt"]); }); }); + + it("defers commit until the matching release count is reached", async () => { + await withDB(async (db) => { + createFileSync(db, "/multi.txt", {}, () => 1000); + writeRangeSync(db, "/multi.txt", bytesOf("seed"), 0, {}, () => 1001); + // Establish the on-disk shape we'll observe against. + const seedBlobs = blobCount(db); + const seedChunks = chunkCount(db, "/multi.txt"); + + // Two opens of the same path share a single inode-keyed entry. + openWriteBufferSync(db, "/multi.txt"); + openWriteBufferSync(db, "/multi.txt"); + writeRangeSync(db, "/multi.txt", bytesOf("DIRTY"), 0, {}, () => 1002); + + // First release decrements but does not commit — chunk/blob + // shape unchanged. + releaseWriteBufferSync(db, "/multi.txt", () => 1003); + expect(blobCount(db)).toBe(seedBlobs); + expect(chunkCount(db, "/multi.txt")).toBe(seedChunks); + + // Second release commits. Reading back through the chunk + // store sees the dirty bytes. + releaseWriteBufferSync(db, "/multi.txt", () => 1004); + const final = readRangeSync(db, "/multi.txt", 0, 5); + expect(new TextDecoder().decode(final)).toBe("DIRTY"); + + // A fresh open after release starts a clean buffer; an + // immediate release without writes is a no-op. + openWriteBufferSync(db, "/multi.txt"); + releaseWriteBufferSync(db, "/multi.txt", () => 1005); + // No corruption: file content still matches. + const stable = readRangeSync(db, "/multi.txt", 0, 5); + expect(new TextDecoder().decode(stable)).toBe("DIRTY"); + }); + }); + + it("flushPendingByPath promotes the buffer without consuming the open count", async () => { + const { flushPendingByPath } = await import("./writeFile.js"); + await withDB(async (db) => { + openWriteBufferForCreateSync(db, "/promote.txt", {}, () => 1000); + writeRangeSync(db, "/promote.txt", bytesOf("before"), 0, {}, () => 1001); + expect(resolveInode(db, "/promote.txt")).toBeNull(); + + // Promote the pending entry without releasing it. + const committed = flushPendingByPath(db, "/promote.txt", () => 1002); + expect(committed).toBe(true); + + // The path now resolves; the open buffer survived the + // promotion under the real inode key. + const node = resolveInode(db, "/promote.txt"); + expect(node?.type).toBe("file"); + expect(chunkCount(db, "/promote.txt")).toBeGreaterThan(0); + + // Further writes route through the inode-keyed cache. The + // matching release commits the final bytes over the + // promoted state. + writeRangeSync(db, "/promote.txt", bytesOf("after-x"), 0, {}, () => 1003); + releaseWriteBufferSync(db, "/promote.txt", () => 1004); + const final = readRangeSync(db, "/promote.txt", 0, 7); + expect(new TextDecoder().decode(final)).toBe("after-x"); + }); + }); + + it("flushPendingByPath returns false when no pending buffer is open", async () => { + const { flushPendingByPath } = await import("./writeFile.js"); + await withDB(async (db) => { + createFileSync(db, "/already.txt", {}, () => 1000); + expect(flushPendingByPath(db, "/already.txt", () => 1001)).toBe(false); + expect(flushPendingByPath(db, "/missing.txt", () => 1002)).toBe(false); + }); + }); }); diff --git a/packages/dofs/src/provider.test.ts b/packages/dofs/src/provider.test.ts index e2e3cb9a..299f777e 100644 --- a/packages/dofs/src/provider.test.ts +++ b/packages/dofs/src/provider.test.ts @@ -406,17 +406,68 @@ describe("SQLiteWorkspaceProvider — pending-create flush on rename/link/unlink }); describe("SQLiteWorkspaceProvider — cached vfs_nodes.size", () => { - it("stat reads size from vfs_nodes without summing chunks", async () => { + function readSize(p: SQLiteWorkspaceProvider, name: string): number | undefined { + return p.db.one<{ size: number }>( + "SELECT size FROM vfs_nodes WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", + name, + )?.size; + } + + it("writeFileSync stamps size on first write and on overwrite", async () => { await withProvider((p) => { - const payload = Buffer.alloc(123, 0x41); - p.writeFileSync("/sized.bin", payload); + p.writeFileSync("/sized.bin", Buffer.alloc(123, 0x41)); expect(p.statSync("/sized.bin").size).toBe(123); - // Read the column directly to confirm the write path stamps it. - const row = p.db.one<{ size: number }>( - "SELECT size FROM vfs_nodes WHERE inode = (SELECT child_inode FROM vfs_dirents WHERE name = ?)", - "sized.bin", - ); - expect(row?.size).toBe(123); + expect(readSize(p, "sized.bin")).toBe(123); + + p.writeFileSync("/sized.bin", Buffer.alloc(7, 0x42)); + expect(p.statSync("/sized.bin").size).toBe(7); + expect(readSize(p, "sized.bin")).toBe(7); + }); + }); + + it("writeRangeSync extends the cached size on growth", async () => { + await withProvider((p) => { + p.createFileSync("/range.bin", { mode: 0o644 }); + p.writeRangeSync("/range.bin", Buffer.from("hello"), 0); + expect(readSize(p, "range.bin")).toBe(5); + p.writeRangeSync("/range.bin", Buffer.from("!!"), 10); + expect(p.statSync("/range.bin").size).toBe(12); + expect(readSize(p, "range.bin")).toBe(12); + }); + }); + + it("truncateFileSync updates the cached size on grow and shrink", async () => { + await withProvider((p) => { + p.writeFileSync("/trunc.bin", Buffer.alloc(100, 0x55)); + expect(readSize(p, "trunc.bin")).toBe(100); + + p.truncateFileSync("/trunc.bin", 250); + expect(p.statSync("/trunc.bin").size).toBe(250); + expect(readSize(p, "trunc.bin")).toBe(250); + + p.truncateFileSync("/trunc.bin", 0); + expect(p.statSync("/trunc.bin").size).toBe(0); + expect(readSize(p, "trunc.bin")).toBe(0); + }); + }); + + it("buffered release stamps the cached size of the committed bytes", async () => { + await withProvider((p) => { + p.openWriteBufferForCreateSync("/buffered.bin", { mode: 0o644 }); + p.writeRangeSync("/buffered.bin", Buffer.from("buffered-write"), 0); + expect(readSize(p, "buffered.bin")).toBeUndefined(); + p.releaseWriteBufferSync("/buffered.bin"); + expect(p.statSync("/buffered.bin").size).toBe(14); + expect(readSize(p, "buffered.bin")).toBe(14); + }); + }); + + it("async writeFile stamps size from the buffered bytes", async () => { + await withProvider(async (p) => { + const payload = Buffer.from("async payload"); + await p.writeFile("/streamed.bin", payload); + expect(p.statSync("/streamed.bin").size).toBe(payload.byteLength); + expect(readSize(p, "streamed.bin")).toBe(payload.byteLength); }); }); }); diff --git a/packages/wsd/src/cli/wsd.test.ts b/packages/wsd/src/cli/wsd.test.ts index 0c5cc601..64933b6d 100644 --- a/packages/wsd/src/cli/wsd.test.ts +++ b/packages/wsd/src/cli/wsd.test.ts @@ -141,6 +141,48 @@ test("/api serves a capnweb HTTP-batch WorkspaceRPC session", async (_ctx) => { expect(await stub.sync.hasObjects([])).toEqual([]); }); +test("/__wsd/stats returns DOFS table sizes and process memory", async (_ctx) => { + const port = await getAvailablePort(); + const mountPoint = await fs.mkdtemp(path.join(os.tmpdir(), "wsd-stats-")); + await startWsd({ port, mountPoint, env: { FUSE_MOUNT: "none" } }); + + const stats = await request(`http://127.0.0.1:${port}/__wsd/stats`); + expect(stats.statusCode).toBe(200); + expect(stats.headers["content-type"]).toMatch(/application\/json/); + + const body = JSON.parse(stats.body); + // DOFS table counts and blob byte totals. The root inode always + // exists, so vfs_nodes_count is at least 1; everything else is + // a non-negative count. Asserting Number.isFinite catches a + // handler that returned NaN, and the non-negative bound catches + // a future regression that returned -1 from a malformed read. + const counts = [ + "vfs_nodes_count", + "vfs_dirents_count", + "vfs_chunks_count", + "vfs_blobs_count", + "vfs_blob_bytes_total", + "vfs_blobs_orphan", + "vfs_blob_bytes_orphan", + ] as const; + for (const key of counts) { + expect(typeof body[key], key).toBe("number"); + expect(Number.isFinite(body[key]), key).toBe(true); + expect(body[key], key).toBeGreaterThanOrEqual(0); + } + expect(body.vfs_nodes_count).toBeGreaterThanOrEqual(1); + + // Process memory snapshot. RSS and heap_total are strictly + // positive in any live process; the rest are non-negative. + expect(body.rss).toBeGreaterThan(0); + expect(body.heap_total).toBeGreaterThan(0); + for (const key of ["heap_used", "external", "array_buffers"] as const) { + expect(typeof body[key], key).toBe("number"); + expect(Number.isFinite(body[key]), key).toBe(true); + expect(body[key], key).toBeGreaterThanOrEqual(0); + } +}); + test("wsd exposes file IO through the userspace shim when FUSE_MOUNT=shim", async (_ctx) => { // No FUSE backend required — the shim runs in user space and is // explicitly opt-in via FUSE_MOUNT=shim. Mirrors the real-FUSE From 4221f1821cb9a5bc7a59b49a48d1841c1fd12499 Mon Sep 17 00:00:00 2001 From: aron <263346377+aron-cf@users.noreply.github.com> Date: Tue, 9 Jun 2026 15:15:09 +0100 Subject: [PATCH 30/31] examples/think: commit worker-configuration.d.ts --- examples/think/worker-configuration.d.ts | 14426 +++++++++++++++++++++ 1 file changed, 14426 insertions(+) create mode 100644 examples/think/worker-configuration.d.ts diff --git a/examples/think/worker-configuration.d.ts b/examples/think/worker-configuration.d.ts new file mode 100644 index 00000000..d1946fc3 --- /dev/null +++ b/examples/think/worker-configuration.d.ts @@ -0,0 +1,14426 @@ +/* eslint-disable */ +// Generated by Wrangler by running `wrangler types` (hash: 81a5de230d8acbb2b145169dc1580780) +// Runtime types generated with workerd@1.20260529.1 2026-05-26 nodejs_compat +interface __BaseEnv_Env { + R2_SKILLS: R2Bucket; + AI: Ai; + TriageAgent: DurableObjectNamespace; + TRIAGE_WORKFLOW: Workflow[0]['payload']>; +} +declare namespace Cloudflare { + interface GlobalProps { + mainModule: typeof import("./src/index"); + durableNamespaces: "TriageAgent"; + } + interface Env extends __BaseEnv_Env {} +} +interface Env extends __BaseEnv_Env {} + +// Begin runtime types +/*! ***************************************************************************** +Copyright (c) Cloudflare. All rights reserved. +Copyright (c) Microsoft Corporation. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* eslint-disable */ +// noinspection JSUnusedGlobalSymbols +declare var onmessage: never; +/** + * The **`DOMException`** interface represents an abnormal event (called an **exception**) that occurs as a result of calling a method or accessing a property of a web API. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException) + */ +declare class DOMException extends Error { + constructor(message?: string, name?: string); + /** + * The **`message`** read-only property of the a message or description associated with the given error name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/message) + */ + readonly message: string; + /** + * The **`name`** read-only property of the one of the strings associated with an error name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/name) + */ + readonly name: string; + /** + * The **`code`** read-only property of the DOMException interface returns one of the legacy error code constants, or `0` if none match. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DOMException/code) + */ + readonly code: number; + static readonly INDEX_SIZE_ERR: number; + static readonly DOMSTRING_SIZE_ERR: number; + static readonly HIERARCHY_REQUEST_ERR: number; + static readonly WRONG_DOCUMENT_ERR: number; + static readonly INVALID_CHARACTER_ERR: number; + static readonly NO_DATA_ALLOWED_ERR: number; + static readonly NO_MODIFICATION_ALLOWED_ERR: number; + static readonly NOT_FOUND_ERR: number; + static readonly NOT_SUPPORTED_ERR: number; + static readonly INUSE_ATTRIBUTE_ERR: number; + static readonly INVALID_STATE_ERR: number; + static readonly SYNTAX_ERR: number; + static readonly INVALID_MODIFICATION_ERR: number; + static readonly NAMESPACE_ERR: number; + static readonly INVALID_ACCESS_ERR: number; + static readonly VALIDATION_ERR: number; + static readonly TYPE_MISMATCH_ERR: number; + static readonly SECURITY_ERR: number; + static readonly NETWORK_ERR: number; + static readonly ABORT_ERR: number; + static readonly URL_MISMATCH_ERR: number; + static readonly QUOTA_EXCEEDED_ERR: number; + static readonly TIMEOUT_ERR: number; + static readonly INVALID_NODE_TYPE_ERR: number; + static readonly DATA_CLONE_ERR: number; + get stack(): any; + set stack(value: any); +} +type WorkerGlobalScopeEventMap = { + fetch: FetchEvent; + scheduled: ScheduledEvent; + queue: QueueEvent; + unhandledrejection: PromiseRejectionEvent; + rejectionhandled: PromiseRejectionEvent; +}; +declare abstract class WorkerGlobalScope extends EventTarget { + EventTarget: typeof EventTarget; +} +/* The **`console`** object provides access to the debugging console (e.g., the Web console in Firefox). * + * The **`console`** object provides access to the debugging console (e.g., the Web console in Firefox). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console) + */ +interface Console { + "assert"(condition?: boolean, ...data: any[]): void; + /** + * The **`console.clear()`** static method clears the console if possible. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/clear_static) + */ + clear(): void; + /** + * The **`console.count()`** static method logs the number of times that this particular call to `count()` has been called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/count_static) + */ + count(label?: string): void; + /** + * The **`console.countReset()`** static method resets counter used with console/count_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/countReset_static) + */ + countReset(label?: string): void; + /** + * The **`console.debug()`** static method outputs a message to the console at the 'debug' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/debug_static) + */ + debug(...data: any[]): void; + /** + * The **`console.dir()`** static method displays a list of the properties of the specified JavaScript object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/dir_static) + */ + dir(item?: any, options?: any): void; + /** + * The **`console.dirxml()`** static method displays an interactive tree of the descendant elements of the specified XML/HTML element. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/dirxml_static) + */ + dirxml(...data: any[]): void; + /** + * The **`console.error()`** static method outputs a message to the console at the 'error' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static) + */ + error(...data: any[]): void; + /** + * The **`console.group()`** static method creates a new inline group in the Web console log, causing any subsequent console messages to be indented by an additional level, until console/groupEnd_static is called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/group_static) + */ + group(...data: any[]): void; + /** + * The **`console.groupCollapsed()`** static method creates a new inline group in the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/groupCollapsed_static) + */ + groupCollapsed(...data: any[]): void; + /** + * The **`console.groupEnd()`** static method exits the current inline group in the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/groupEnd_static) + */ + groupEnd(): void; + /** + * The **`console.info()`** static method outputs a message to the console at the 'info' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/info_static) + */ + info(...data: any[]): void; + /** + * The **`console.log()`** static method outputs a message to the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) + */ + log(...data: any[]): void; + /** + * The **`console.table()`** static method displays tabular data as a table. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/table_static) + */ + table(tabularData?: any, properties?: string[]): void; + /** + * The **`console.time()`** static method starts a timer you can use to track how long an operation takes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/time_static) + */ + time(label?: string): void; + /** + * The **`console.timeEnd()`** static method stops a timer that was previously started by calling console/time_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/timeEnd_static) + */ + timeEnd(label?: string): void; + /** + * The **`console.timeLog()`** static method logs the current value of a timer that was previously started by calling console/time_static. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/timeLog_static) + */ + timeLog(label?: string, ...data: any[]): void; + timeStamp(label?: string): void; + /** + * The **`console.trace()`** static method outputs a stack trace to the console. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/trace_static) + */ + trace(...data: any[]): void; + /** + * The **`console.warn()`** static method outputs a warning message to the console at the 'warning' log level. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/warn_static) + */ + warn(...data: any[]): void; +} +declare const console: Console; +type BufferSource = ArrayBufferView | ArrayBuffer; +type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array; +declare namespace WebAssembly { + class CompileError extends Error { + constructor(message?: string); + } + class RuntimeError extends Error { + constructor(message?: string); + } + type ValueType = "anyfunc" | "externref" | "f32" | "f64" | "i32" | "i64" | "v128"; + interface GlobalDescriptor { + value: ValueType; + mutable?: boolean; + } + class Global { + constructor(descriptor: GlobalDescriptor, value?: any); + value: any; + valueOf(): any; + } + type ImportValue = ExportValue | number; + type ModuleImports = Record; + type Imports = Record; + type ExportValue = Function | Global | Memory | Table; + type Exports = Record; + class Instance { + constructor(module: Module, imports?: Imports); + readonly exports: Exports; + } + interface MemoryDescriptor { + initial: number; + maximum?: number; + shared?: boolean; + } + class Memory { + constructor(descriptor: MemoryDescriptor); + readonly buffer: ArrayBuffer; + grow(delta: number): number; + } + type ImportExportKind = "function" | "global" | "memory" | "table"; + interface ModuleExportDescriptor { + kind: ImportExportKind; + name: string; + } + interface ModuleImportDescriptor { + kind: ImportExportKind; + module: string; + name: string; + } + abstract class Module { + static customSections(module: Module, sectionName: string): ArrayBuffer[]; + static exports(module: Module): ModuleExportDescriptor[]; + static imports(module: Module): ModuleImportDescriptor[]; + } + type TableKind = "anyfunc" | "externref"; + interface TableDescriptor { + element: TableKind; + initial: number; + maximum?: number; + } + class Table { + constructor(descriptor: TableDescriptor, value?: any); + readonly length: number; + get(index: number): any; + grow(delta: number, value?: any): number; + set(index: number, value?: any): void; + } + function instantiate(module: Module, imports?: Imports): Promise; + function validate(bytes: BufferSource): boolean; +} +/** + * The **`ServiceWorkerGlobalScope`** interface of the Service Worker API represents the global execution context of a service worker. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ServiceWorkerGlobalScope) + */ +interface ServiceWorkerGlobalScope extends WorkerGlobalScope { + DOMException: typeof DOMException; + WorkerGlobalScope: typeof WorkerGlobalScope; + btoa(data: string): string; + atob(data: string): string; + setTimeout(callback: (...args: any[]) => void, msDelay?: number): number; + setTimeout(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; + clearTimeout(timeoutId: number | null): void; + setInterval(callback: (...args: any[]) => void, msDelay?: number): number; + setInterval(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; + clearInterval(timeoutId: number | null): void; + queueMicrotask(task: Function): void; + structuredClone(value: T, options?: StructuredSerializeOptions): T; + reportError(error: any): void; + fetch(input: RequestInfo | URL, init?: RequestInit): Promise; + self: ServiceWorkerGlobalScope; + crypto: Crypto; + caches: CacheStorage; + scheduler: Scheduler; + performance: Performance; + Cloudflare: Cloudflare; + readonly origin: string; + Event: typeof Event; + ExtendableEvent: typeof ExtendableEvent; + CustomEvent: typeof CustomEvent; + PromiseRejectionEvent: typeof PromiseRejectionEvent; + FetchEvent: typeof FetchEvent; + TailEvent: typeof TailEvent; + TraceEvent: typeof TailEvent; + ScheduledEvent: typeof ScheduledEvent; + MessageEvent: typeof MessageEvent; + CloseEvent: typeof CloseEvent; + ReadableStreamDefaultReader: typeof ReadableStreamDefaultReader; + ReadableStreamBYOBReader: typeof ReadableStreamBYOBReader; + ReadableStream: typeof ReadableStream; + WritableStream: typeof WritableStream; + WritableStreamDefaultWriter: typeof WritableStreamDefaultWriter; + TransformStream: typeof TransformStream; + ByteLengthQueuingStrategy: typeof ByteLengthQueuingStrategy; + CountQueuingStrategy: typeof CountQueuingStrategy; + ErrorEvent: typeof ErrorEvent; + MessageChannel: typeof MessageChannel; + MessagePort: typeof MessagePort; + EventSource: typeof EventSource; + ReadableStreamBYOBRequest: typeof ReadableStreamBYOBRequest; + ReadableStreamDefaultController: typeof ReadableStreamDefaultController; + ReadableByteStreamController: typeof ReadableByteStreamController; + WritableStreamDefaultController: typeof WritableStreamDefaultController; + TransformStreamDefaultController: typeof TransformStreamDefaultController; + CompressionStream: typeof CompressionStream; + DecompressionStream: typeof DecompressionStream; + TextEncoderStream: typeof TextEncoderStream; + TextDecoderStream: typeof TextDecoderStream; + Headers: typeof Headers; + Body: typeof Body; + Request: typeof Request; + Response: typeof Response; + WebSocket: typeof WebSocket; + WebSocketPair: typeof WebSocketPair; + WebSocketRequestResponsePair: typeof WebSocketRequestResponsePair; + AbortController: typeof AbortController; + AbortSignal: typeof AbortSignal; + TextDecoder: typeof TextDecoder; + TextEncoder: typeof TextEncoder; + navigator: Navigator; + Navigator: typeof Navigator; + URL: typeof URL; + URLSearchParams: typeof URLSearchParams; + URLPattern: typeof URLPattern; + Blob: typeof Blob; + File: typeof File; + FormData: typeof FormData; + Crypto: typeof Crypto; + SubtleCrypto: typeof SubtleCrypto; + CryptoKey: typeof CryptoKey; + CacheStorage: typeof CacheStorage; + Cache: typeof Cache; + FixedLengthStream: typeof FixedLengthStream; + IdentityTransformStream: typeof IdentityTransformStream; + HTMLRewriter: typeof HTMLRewriter; +} +declare function addEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetAddEventListenerOptions | boolean): void; +declare function removeEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetEventListenerOptions | boolean): void; +/** + * The **`dispatchEvent()`** method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent) + */ +declare function dispatchEvent(event: WorkerGlobalScopeEventMap[keyof WorkerGlobalScopeEventMap]): boolean; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/btoa) */ +declare function btoa(data: string): string; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/atob) */ +declare function atob(data: string): string; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setTimeout) */ +declare function setTimeout(callback: (...args: any[]) => void, msDelay?: number): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setTimeout) */ +declare function setTimeout(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/clearTimeout) */ +declare function clearTimeout(timeoutId: number | null): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setInterval) */ +declare function setInterval(callback: (...args: any[]) => void, msDelay?: number): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/setInterval) */ +declare function setInterval(callback: (...args: Args) => void, msDelay?: number, ...args: Args): number; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/clearInterval) */ +declare function clearInterval(timeoutId: number | null): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/queueMicrotask) */ +declare function queueMicrotask(task: Function): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/structuredClone) */ +declare function structuredClone(value: T, options?: StructuredSerializeOptions): T; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/reportError) */ +declare function reportError(error: any): void; +/* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Window/fetch) */ +declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise; +declare const self: ServiceWorkerGlobalScope; +/** +* The Web Crypto API provides a set of low-level functions for common cryptographic tasks. +* The Workers runtime implements the full surface of this API, but with some differences in +* the [supported algorithms](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/#supported-algorithms) +* compared to those implemented in most browsers. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/) +*/ +declare const crypto: Crypto; +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare const caches: CacheStorage; +declare const scheduler: Scheduler; +/** +* The Workers runtime supports a subset of the Performance API, used to measure timing and performance, +* as well as timing of subrequests and other operations. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/) +*/ +declare const performance: Performance; +declare const Cloudflare: Cloudflare; +declare const origin: string; +declare const navigator: Navigator; +interface TestController { +} +interface ExecutionContext { + waitUntil(promise: Promise): void; + passThroughOnException(): void; + readonly exports: Cloudflare.Exports; + readonly props: Props; + cache?: CacheContext; + tracing?: Tracing; +} +type ExportedHandlerFetchHandler = (request: Request>, env: Env, ctx: ExecutionContext) => Response | Promise; +type ExportedHandlerConnectHandler = (socket: Socket, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTailHandler = (events: TraceItem[], env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTraceHandler = (traces: TraceItem[], env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTailStreamHandler = (event: TailStream.TailEvent, env: Env, ctx: ExecutionContext) => TailStream.TailEventHandlerType | Promise; +type ExportedHandlerScheduledHandler = (controller: ScheduledController, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerQueueHandler = (batch: MessageBatch, env: Env, ctx: ExecutionContext) => void | Promise; +type ExportedHandlerTestHandler = (controller: TestController, env: Env, ctx: ExecutionContext) => void | Promise; +interface ExportedHandler { + fetch?: ExportedHandlerFetchHandler; + connect?: ExportedHandlerConnectHandler; + tail?: ExportedHandlerTailHandler; + trace?: ExportedHandlerTraceHandler; + tailStream?: ExportedHandlerTailStreamHandler; + scheduled?: ExportedHandlerScheduledHandler; + test?: ExportedHandlerTestHandler; + email?: EmailExportedHandler; + queue?: ExportedHandlerQueueHandler; +} +interface StructuredSerializeOptions { + transfer?: any[]; +} +declare abstract class Navigator { + sendBeacon(url: string, body?: BodyInit): boolean; + readonly userAgent: string; + readonly hardwareConcurrency: number; + readonly platform: string; + readonly language: string; + readonly languages: string[]; +} +interface AlarmInvocationInfo { + readonly isRetry: boolean; + readonly retryCount: number; + readonly scheduledTime: number; +} +interface Cloudflare { + readonly compatibilityFlags: Record; +} +interface CachePurgeError { + code: number; + message: string; +} +interface CachePurgeResult { + success: boolean; + errors: CachePurgeError[]; +} +interface CachePurgeOptions { + tags?: string[]; + pathPrefixes?: string[]; + purgeEverything?: boolean; +} +interface CacheContext { + purge(options: CachePurgeOptions): Promise; +} +declare abstract class ColoLocalActorNamespace { + get(actorId: string): Fetcher; +} +interface DurableObject { + fetch(request: Request): Response | Promise; + connect?(socket: Socket): void | Promise; + alarm?(alarmInfo?: AlarmInvocationInfo): void | Promise; + webSocketMessage?(ws: WebSocket, message: string | ArrayBuffer): void | Promise; + webSocketClose?(ws: WebSocket, code: number, reason: string, wasClean: boolean): void | Promise; + webSocketError?(ws: WebSocket, error: unknown): void | Promise; +} +type DurableObjectStub = Fetcher & { + readonly id: DurableObjectId; + readonly name?: string; +}; +interface DurableObjectId { + toString(): string; + equals(other: DurableObjectId): boolean; + readonly name?: string; + readonly jurisdiction?: string; +} +declare abstract class DurableObjectNamespace { + newUniqueId(options?: DurableObjectNamespaceNewUniqueIdOptions): DurableObjectId; + idFromName(name: string): DurableObjectId; + idFromString(id: string): DurableObjectId; + get(id: DurableObjectId, options?: DurableObjectNamespaceGetDurableObjectOptions): DurableObjectStub; + getByName(name: string, options?: DurableObjectNamespaceGetDurableObjectOptions): DurableObjectStub; + jurisdiction(jurisdiction: DurableObjectJurisdiction): DurableObjectNamespace; +} +type DurableObjectJurisdiction = "eu" | "fedramp" | "fedramp-high"; +interface DurableObjectNamespaceNewUniqueIdOptions { + jurisdiction?: DurableObjectJurisdiction; +} +type DurableObjectLocationHint = "wnam" | "enam" | "sam" | "weur" | "eeur" | "apac" | "oc" | "afr" | "me"; +type DurableObjectRoutingMode = "primary-only"; +interface DurableObjectNamespaceGetDurableObjectOptions { + locationHint?: DurableObjectLocationHint; + routingMode?: DurableObjectRoutingMode; +} +interface DurableObjectClass<_T extends Rpc.DurableObjectBranded | undefined = undefined> { +} +interface DurableObjectState { + waitUntil(promise: Promise): void; + readonly exports: Cloudflare.Exports; + readonly props: Props; + readonly id: DurableObjectId; + readonly storage: DurableObjectStorage; + container?: Container; + facets: DurableObjectFacets; + blockConcurrencyWhile(callback: () => Promise): Promise; + acceptWebSocket(ws: WebSocket, tags?: string[]): void; + getWebSockets(tag?: string): WebSocket[]; + setWebSocketAutoResponse(maybeReqResp?: WebSocketRequestResponsePair): void; + getWebSocketAutoResponse(): WebSocketRequestResponsePair | null; + getWebSocketAutoResponseTimestamp(ws: WebSocket): Date | null; + setHibernatableWebSocketEventTimeout(timeoutMs?: number): void; + getHibernatableWebSocketEventTimeout(): number | null; + getTags(ws: WebSocket): string[]; + abort(reason?: string): void; +} +interface DurableObjectTransaction { + get(key: string, options?: DurableObjectGetOptions): Promise; + get(keys: string[], options?: DurableObjectGetOptions): Promise>; + list(options?: DurableObjectListOptions): Promise>; + put(key: string, value: T, options?: DurableObjectPutOptions): Promise; + put(entries: Record, options?: DurableObjectPutOptions): Promise; + delete(key: string, options?: DurableObjectPutOptions): Promise; + delete(keys: string[], options?: DurableObjectPutOptions): Promise; + rollback(): void; + getAlarm(options?: DurableObjectGetAlarmOptions): Promise; + setAlarm(scheduledTime: number | Date, options?: DurableObjectSetAlarmOptions): Promise; + deleteAlarm(options?: DurableObjectSetAlarmOptions): Promise; +} +interface DurableObjectStorage { + get(key: string, options?: DurableObjectGetOptions): Promise; + get(keys: string[], options?: DurableObjectGetOptions): Promise>; + list(options?: DurableObjectListOptions): Promise>; + put(key: string, value: T, options?: DurableObjectPutOptions): Promise; + put(entries: Record, options?: DurableObjectPutOptions): Promise; + delete(key: string, options?: DurableObjectPutOptions): Promise; + delete(keys: string[], options?: DurableObjectPutOptions): Promise; + deleteAll(options?: DurableObjectPutOptions): Promise; + transaction(closure: (txn: DurableObjectTransaction) => Promise): Promise; + getAlarm(options?: DurableObjectGetAlarmOptions): Promise; + setAlarm(scheduledTime: number | Date, options?: DurableObjectSetAlarmOptions): Promise; + deleteAlarm(options?: DurableObjectSetAlarmOptions): Promise; + sync(): Promise; + sql: SqlStorage; + kv: SyncKvStorage; + transactionSync(closure: () => T): T; + getCurrentBookmark(): Promise; + getBookmarkForTime(timestamp: number | Date): Promise; + onNextSessionRestoreBookmark(bookmark: string): Promise; +} +interface DurableObjectListOptions { + start?: string; + startAfter?: string; + end?: string; + prefix?: string; + reverse?: boolean; + limit?: number; + allowConcurrency?: boolean; + noCache?: boolean; +} +interface DurableObjectGetOptions { + allowConcurrency?: boolean; + noCache?: boolean; +} +interface DurableObjectGetAlarmOptions { + allowConcurrency?: boolean; +} +interface DurableObjectPutOptions { + allowConcurrency?: boolean; + allowUnconfirmed?: boolean; + noCache?: boolean; +} +interface DurableObjectSetAlarmOptions { + allowConcurrency?: boolean; + allowUnconfirmed?: boolean; +} +declare class WebSocketRequestResponsePair { + constructor(request: string, response: string); + get request(): string; + get response(): string; +} +interface DurableObjectFacets { + get(name: string, getStartupOptions: () => FacetStartupOptions | Promise>): Fetcher; + abort(name: string, reason: any): void; + delete(name: string): void; +} +interface FacetStartupOptions { + id?: DurableObjectId | string; + class: DurableObjectClass; +} +interface AnalyticsEngineDataset { + writeDataPoint(event?: AnalyticsEngineDataPoint): void; +} +interface AnalyticsEngineDataPoint { + indexes?: ((ArrayBuffer | string) | null)[]; + doubles?: number[]; + blobs?: ((ArrayBuffer | string) | null)[]; +} +/** + * The **`Event`** interface represents an event which takes place on an `EventTarget`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event) + */ +declare class Event { + constructor(type: string, init?: EventInit); + /** + * The **`type`** read-only property of the Event interface returns a string containing the event's type. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/type) + */ + get type(): string; + /** + * The **`eventPhase`** read-only property of the being evaluated. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/eventPhase) + */ + get eventPhase(): number; + /** + * The read-only **`composed`** property of the or not the event will propagate across the shadow DOM boundary into the standard DOM. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composed) + */ + get composed(): boolean; + /** + * The **`bubbles`** read-only property of the Event interface indicates whether the event bubbles up through the DOM tree or not. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/bubbles) + */ + get bubbles(): boolean; + /** + * The **`cancelable`** read-only property of the Event interface indicates whether the event can be canceled, and therefore prevented as if the event never happened. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelable) + */ + get cancelable(): boolean; + /** + * The **`defaultPrevented`** read-only property of the Event interface returns a boolean value indicating whether or not the call to Event.preventDefault() canceled the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/defaultPrevented) + */ + get defaultPrevented(): boolean; + /** + * The Event property **`returnValue`** indicates whether the default action for this event has been prevented or not. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/returnValue) + */ + get returnValue(): boolean; + /** + * The **`currentTarget`** read-only property of the Event interface identifies the element to which the event handler has been attached. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/currentTarget) + */ + get currentTarget(): EventTarget | undefined; + /** + * The read-only **`target`** property of the dispatched. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/target) + */ + get target(): EventTarget | undefined; + /** + * The deprecated **`Event.srcElement`** is an alias for the Event.target property. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/srcElement) + */ + get srcElement(): EventTarget | undefined; + /** + * The **`timeStamp`** read-only property of the Event interface returns the time (in milliseconds) at which the event was created. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/timeStamp) + */ + get timeStamp(): number; + /** + * The **`isTrusted`** read-only property of the when the event was generated by the user agent (including via user actions and programmatic methods such as HTMLElement.focus()), and `false` when the event was dispatched via The only exception is the `click` event, which initializes the `isTrusted` property to `false` in user agents. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/isTrusted) + */ + get isTrusted(): boolean; + /** + * The **`cancelBubble`** property of the Event interface is deprecated. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble) + */ + get cancelBubble(): boolean; + /** + * The **`cancelBubble`** property of the Event interface is deprecated. + * @deprecated + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/cancelBubble) + */ + set cancelBubble(value: boolean); + /** + * The **`stopImmediatePropagation()`** method of the If several listeners are attached to the same element for the same event type, they are called in the order in which they were added. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopImmediatePropagation) + */ + stopImmediatePropagation(): void; + /** + * The **`preventDefault()`** method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/preventDefault) + */ + preventDefault(): void; + /** + * The **`stopPropagation()`** method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/stopPropagation) + */ + stopPropagation(): void; + /** + * The **`composedPath()`** method of the Event interface returns the event's path which is an array of the objects on which listeners will be invoked. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Event/composedPath) + */ + composedPath(): EventTarget[]; + static readonly NONE: number; + static readonly CAPTURING_PHASE: number; + static readonly AT_TARGET: number; + static readonly BUBBLING_PHASE: number; +} +interface EventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; +} +type EventListener = (event: EventType) => void; +interface EventListenerObject { + handleEvent(event: EventType): void; +} +type EventListenerOrEventListenerObject = EventListener | EventListenerObject; +/** + * The **`EventTarget`** interface is implemented by objects that can receive events and may have listeners for them. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget) + */ +declare class EventTarget = Record> { + constructor(); + /** + * The **`addEventListener()`** method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener) + */ + addEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetAddEventListenerOptions | boolean): void; + /** + * The **`removeEventListener()`** method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener) + */ + removeEventListener(type: Type, handler: EventListenerOrEventListenerObject, options?: EventTargetEventListenerOptions | boolean): void; + /** + * The **`dispatchEvent()`** method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent) + */ + dispatchEvent(event: EventMap[keyof EventMap]): boolean; +} +interface EventTargetEventListenerOptions { + capture?: boolean; +} +interface EventTargetAddEventListenerOptions { + capture?: boolean; + passive?: boolean; + once?: boolean; + signal?: AbortSignal; +} +interface EventTargetHandlerObject { + handleEvent: (event: Event) => any | undefined; +} +/** + * The **`AbortController`** interface represents a controller object that allows you to abort one or more Web requests as and when desired. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController) + */ +declare class AbortController { + constructor(); + /** + * The **`signal`** read-only property of the AbortController interface returns an AbortSignal object instance, which can be used to communicate with/abort an asynchronous operation as desired. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/signal) + */ + get signal(): AbortSignal; + /** + * The **`abort()`** method of the AbortController interface aborts an asynchronous operation before it has completed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortController/abort) + */ + abort(reason?: any): void; +} +/** + * The **`AbortSignal`** interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal) + */ +declare abstract class AbortSignal extends EventTarget { + /** + * The **`AbortSignal.abort()`** static method returns an AbortSignal that is already set as aborted (and which does not trigger an AbortSignal/abort_event event). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_static) + */ + static abort(reason?: any): AbortSignal; + /** + * The **`AbortSignal.timeout()`** static method returns an AbortSignal that will automatically abort after a specified time. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/timeout_static) + */ + static timeout(delay: number): AbortSignal; + /** + * The **`AbortSignal.any()`** static method takes an iterable of abort signals and returns an AbortSignal. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/any_static) + */ + static any(signals: AbortSignal[]): AbortSignal; + /** + * The **`aborted`** read-only property returns a value that indicates whether the asynchronous operations the signal is communicating with are aborted (`true`) or not (`false`). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/aborted) + */ + get aborted(): boolean; + /** + * The **`reason`** read-only property returns a JavaScript value that indicates the abort reason. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/reason) + */ + get reason(): any; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_event) */ + get onabort(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/abort_event) */ + set onabort(value: any | null); + /** + * The **`throwIfAborted()`** method throws the signal's abort AbortSignal.reason if the signal has been aborted; otherwise it does nothing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/AbortSignal/throwIfAborted) + */ + throwIfAborted(): void; +} +interface Scheduler { + wait(delay: number, maybeOptions?: SchedulerWaitOptions): Promise; +} +interface SchedulerWaitOptions { + signal?: AbortSignal; +} +/** + * The **`ExtendableEvent`** interface extends the lifetime of the `install` and `activate` events dispatched on the global scope as part of the service worker lifecycle. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ExtendableEvent) + */ +declare abstract class ExtendableEvent extends Event { + /** + * The **`ExtendableEvent.waitUntil()`** method tells the event dispatcher that work is ongoing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ExtendableEvent/waitUntil) + */ + waitUntil(promise: Promise): void; +} +/** + * The **`CustomEvent`** interface represents events initialized by an application for any purpose. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent) + */ +declare class CustomEvent extends Event { + constructor(type: string, init?: CustomEventCustomEventInit); + /** + * The read-only **`detail`** property of the CustomEvent interface returns any data passed when initializing the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CustomEvent/detail) + */ + get detail(): T; +} +interface CustomEventCustomEventInit { + bubbles?: boolean; + cancelable?: boolean; + composed?: boolean; + detail?: any; +} +/** + * The **`Blob`** interface represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob) + */ +declare class Blob { + constructor(bits?: ((ArrayBuffer | ArrayBufferView) | string | Blob)[], options?: BlobOptions); + /** + * The **`size`** read-only property of the Blob interface returns the size of the Blob or File in bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/size) + */ + get size(): number; + /** + * The **`type`** read-only property of the Blob interface returns the MIME type of the file. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/type) + */ + get type(): string; + /** + * The **`slice()`** method of the Blob interface creates and returns a new `Blob` object which contains data from a subset of the blob on which it's called. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) + */ + slice(start?: number, end?: number, type?: string): Blob; + /** + * The **`arrayBuffer()`** method of the Blob interface returns a Promise that resolves with the contents of the blob as binary data contained in an ArrayBuffer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer) + */ + arrayBuffer(): Promise; + /** + * The **`bytes()`** method of the Blob interface returns a Promise that resolves with a Uint8Array containing the contents of the blob as an array of bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/bytes) + */ + bytes(): Promise; + /** + * The **`text()`** method of the string containing the contents of the blob, interpreted as UTF-8. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/text) + */ + text(): Promise; + /** + * The **`stream()`** method of the Blob interface returns a ReadableStream which upon reading returns the data contained within the `Blob`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream) + */ + stream(): ReadableStream; +} +interface BlobOptions { + type?: string; +} +/** + * The **`File`** interface provides information about files and allows JavaScript in a web page to access their content. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File) + */ +declare class File extends Blob { + constructor(bits: ((ArrayBuffer | ArrayBufferView) | string | Blob)[] | undefined, name: string, options?: FileOptions); + /** + * The **`name`** read-only property of the File interface returns the name of the file represented by a File object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/name) + */ + get name(): string; + /** + * The **`lastModified`** read-only property of the File interface provides the last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/File/lastModified) + */ + get lastModified(): number; +} +interface FileOptions { + type?: string; + lastModified?: number; +} +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare abstract class CacheStorage { + /** + * The **`open()`** method of the the Cache object matching the `cacheName`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CacheStorage/open) + */ + open(cacheName: string): Promise; + readonly default: Cache; +} +/** +* The Cache API allows fine grained control of reading and writing from the Cloudflare global network cache. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/) +*/ +declare abstract class Cache { + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#delete) */ + delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#match) */ + match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/cache/#put) */ + put(request: RequestInfo | URL, response: Response): Promise; +} +interface CacheQueryOptions { + ignoreMethod?: boolean; +} +/** +* The Web Crypto API provides a set of low-level functions for common cryptographic tasks. +* The Workers runtime implements the full surface of this API, but with some differences in +* the [supported algorithms](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/#supported-algorithms) +* compared to those implemented in most browsers. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/) +*/ +declare abstract class Crypto { + /** + * The **`Crypto.subtle`** read-only property returns a cryptographic operations. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/subtle) + */ + get subtle(): SubtleCrypto; + /** + * The **`Crypto.getRandomValues()`** method lets you get cryptographically strong random values. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) + */ + getRandomValues(buffer: T): T; + /** + * The **`randomUUID()`** method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/randomUUID) + */ + randomUUID(): string; + DigestStream: typeof DigestStream; +} +/** + * The **`SubtleCrypto`** interface of the Web Crypto API provides a number of low-level cryptographic functions. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto) + */ +declare abstract class SubtleCrypto { + /** + * The **`encrypt()`** method of the SubtleCrypto interface encrypts data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/encrypt) + */ + encrypt(algorithm: string | SubtleCryptoEncryptAlgorithm, key: CryptoKey, plainText: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`decrypt()`** method of the SubtleCrypto interface decrypts some encrypted data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/decrypt) + */ + decrypt(algorithm: string | SubtleCryptoEncryptAlgorithm, key: CryptoKey, cipherText: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`sign()`** method of the SubtleCrypto interface generates a digital signature. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/sign) + */ + sign(algorithm: string | SubtleCryptoSignAlgorithm, key: CryptoKey, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`verify()`** method of the SubtleCrypto interface verifies a digital signature. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/verify) + */ + verify(algorithm: string | SubtleCryptoSignAlgorithm, key: CryptoKey, signature: ArrayBuffer | ArrayBufferView, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`digest()`** method of the SubtleCrypto interface generates a _digest_ of the given data, using the specified hash function. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/digest) + */ + digest(algorithm: string | SubtleCryptoHashAlgorithm, data: ArrayBuffer | ArrayBufferView): Promise; + /** + * The **`generateKey()`** method of the SubtleCrypto interface is used to generate a new key (for symmetric algorithms) or key pair (for public-key algorithms). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/generateKey) + */ + generateKey(algorithm: string | SubtleCryptoGenerateKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`deriveKey()`** method of the SubtleCrypto interface can be used to derive a secret key from a master key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveKey) + */ + deriveKey(algorithm: string | SubtleCryptoDeriveKeyAlgorithm, baseKey: CryptoKey, derivedKeyAlgorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`deriveBits()`** method of the key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/deriveBits) + */ + deriveBits(algorithm: string | SubtleCryptoDeriveKeyAlgorithm, baseKey: CryptoKey, length?: number | null): Promise; + /** + * The **`importKey()`** method of the SubtleCrypto interface imports a key: that is, it takes as input a key in an external, portable format and gives you a CryptoKey object that you can use in the Web Crypto API. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/importKey) + */ + importKey(format: string, keyData: (ArrayBuffer | ArrayBufferView) | JsonWebKey, algorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + /** + * The **`exportKey()`** method of the SubtleCrypto interface exports a key: that is, it takes as input a CryptoKey object and gives you the key in an external, portable format. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/exportKey) + */ + exportKey(format: string, key: CryptoKey): Promise; + /** + * The **`wrapKey()`** method of the SubtleCrypto interface 'wraps' a key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/wrapKey) + */ + wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: string | SubtleCryptoEncryptAlgorithm): Promise; + /** + * The **`unwrapKey()`** method of the SubtleCrypto interface 'unwraps' a key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/SubtleCrypto/unwrapKey) + */ + unwrapKey(format: string, wrappedKey: ArrayBuffer | ArrayBufferView, unwrappingKey: CryptoKey, unwrapAlgorithm: string | SubtleCryptoEncryptAlgorithm, unwrappedKeyAlgorithm: string | SubtleCryptoImportKeyAlgorithm, extractable: boolean, keyUsages: string[]): Promise; + timingSafeEqual(a: ArrayBuffer | ArrayBufferView, b: ArrayBuffer | ArrayBufferView): boolean; +} +/** + * The **`CryptoKey`** interface of the Web Crypto API represents a cryptographic key obtained from one of the SubtleCrypto methods SubtleCrypto.generateKey, SubtleCrypto.deriveKey, SubtleCrypto.importKey, or SubtleCrypto.unwrapKey. + * Available only in secure contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey) + */ +declare abstract class CryptoKey { + /** + * The read-only **`type`** property of the CryptoKey interface indicates which kind of key is represented by the object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/type) + */ + readonly type: string; + /** + * The read-only **`extractable`** property of the CryptoKey interface indicates whether or not the key may be extracted using `SubtleCrypto.exportKey()` or `SubtleCrypto.wrapKey()`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/extractable) + */ + readonly extractable: boolean; + /** + * The read-only **`algorithm`** property of the CryptoKey interface returns an object describing the algorithm for which this key can be used, and any associated extra parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/algorithm) + */ + readonly algorithm: CryptoKeyKeyAlgorithm | CryptoKeyAesKeyAlgorithm | CryptoKeyHmacKeyAlgorithm | CryptoKeyRsaKeyAlgorithm | CryptoKeyEllipticKeyAlgorithm | CryptoKeyArbitraryKeyAlgorithm; + /** + * The read-only **`usages`** property of the CryptoKey interface indicates what can be done with the key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CryptoKey/usages) + */ + readonly usages: string[]; +} +interface CryptoKeyPair { + publicKey: CryptoKey; + privateKey: CryptoKey; +} +interface JsonWebKey { + kty: string; + use?: string; + key_ops?: string[]; + alg?: string; + ext?: boolean; + crv?: string; + x?: string; + y?: string; + d?: string; + n?: string; + e?: string; + p?: string; + q?: string; + dp?: string; + dq?: string; + qi?: string; + oth?: RsaOtherPrimesInfo[]; + k?: string; +} +interface RsaOtherPrimesInfo { + r?: string; + d?: string; + t?: string; +} +interface SubtleCryptoDeriveKeyAlgorithm { + name: string; + salt?: (ArrayBuffer | ArrayBufferView); + iterations?: number; + hash?: (string | SubtleCryptoHashAlgorithm); + $public?: CryptoKey; + info?: (ArrayBuffer | ArrayBufferView); +} +interface SubtleCryptoEncryptAlgorithm { + name: string; + iv?: (ArrayBuffer | ArrayBufferView); + additionalData?: (ArrayBuffer | ArrayBufferView); + tagLength?: number; + counter?: (ArrayBuffer | ArrayBufferView); + length?: number; + label?: (ArrayBuffer | ArrayBufferView); +} +interface SubtleCryptoGenerateKeyAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + modulusLength?: number; + publicExponent?: (ArrayBuffer | ArrayBufferView); + length?: number; + namedCurve?: string; +} +interface SubtleCryptoHashAlgorithm { + name: string; +} +interface SubtleCryptoImportKeyAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + length?: number; + namedCurve?: string; + compressed?: boolean; +} +interface SubtleCryptoSignAlgorithm { + name: string; + hash?: (string | SubtleCryptoHashAlgorithm); + dataLength?: number; + saltLength?: number; +} +interface CryptoKeyKeyAlgorithm { + name: string; +} +interface CryptoKeyAesKeyAlgorithm { + name: string; + length: number; +} +interface CryptoKeyHmacKeyAlgorithm { + name: string; + hash: CryptoKeyKeyAlgorithm; + length: number; +} +interface CryptoKeyRsaKeyAlgorithm { + name: string; + modulusLength: number; + publicExponent: ArrayBuffer | ArrayBufferView; + hash?: CryptoKeyKeyAlgorithm; +} +interface CryptoKeyEllipticKeyAlgorithm { + name: string; + namedCurve: string; +} +interface CryptoKeyArbitraryKeyAlgorithm { + name: string; + hash?: CryptoKeyKeyAlgorithm; + namedCurve?: string; + length?: number; +} +declare class DigestStream extends WritableStream { + constructor(algorithm: string | SubtleCryptoHashAlgorithm); + readonly digest: Promise; + get bytesWritten(): number | bigint; +} +/** + * The **`TextDecoder`** interface represents a decoder for a specific text encoding, such as `UTF-8`, `ISO-8859-2`, `KOI8-R`, `GBK`, etc. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder) + */ +declare class TextDecoder { + constructor(label?: string, options?: TextDecoderConstructorOptions); + /** + * The **`TextDecoder.decode()`** method returns a string containing text decoded from the buffer passed as a parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoder/decode) + */ + decode(input?: (ArrayBuffer | ArrayBufferView), options?: TextDecoderDecodeOptions): string; + get encoding(): string; + get fatal(): boolean; + get ignoreBOM(): boolean; +} +/** + * The **`TextEncoder`** interface takes a stream of code points as input and emits a stream of UTF-8 bytes. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder) + */ +declare class TextEncoder { + constructor(); + /** + * The **`TextEncoder.encode()`** method takes a string as input, and returns a Global_Objects/Uint8Array containing the text given in parameters encoded with the specific method for that TextEncoder object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encode) + */ + encode(input?: string): Uint8Array; + /** + * The **`TextEncoder.encodeInto()`** method takes a string to encode and a destination Uint8Array to put resulting UTF-8 encoded text into, and returns a dictionary object indicating the progress of the encoding. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoder/encodeInto) + */ + encodeInto(input: string, buffer: Uint8Array): TextEncoderEncodeIntoResult; + get encoding(): string; +} +interface TextDecoderConstructorOptions { + fatal: boolean; + ignoreBOM: boolean; +} +interface TextDecoderDecodeOptions { + stream: boolean; +} +interface TextEncoderEncodeIntoResult { + read: number; + written: number; +} +/** + * The **`ErrorEvent`** interface represents events providing information related to errors in scripts or in files. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent) + */ +declare class ErrorEvent extends Event { + constructor(type: string, init?: ErrorEventErrorEventInit); + /** + * The **`filename`** read-only property of the ErrorEvent interface returns a string containing the name of the script file in which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/filename) + */ + get filename(): string; + /** + * The **`message`** read-only property of the ErrorEvent interface returns a string containing a human-readable error message describing the problem. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/message) + */ + get message(): string; + /** + * The **`lineno`** read-only property of the ErrorEvent interface returns an integer containing the line number of the script file on which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/lineno) + */ + get lineno(): number; + /** + * The **`colno`** read-only property of the ErrorEvent interface returns an integer containing the column number of the script file on which the error occurred. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/colno) + */ + get colno(): number; + /** + * The **`error`** read-only property of the ErrorEvent interface returns a JavaScript value, such as an Error or DOMException, representing the error associated with this event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ErrorEvent/error) + */ + get error(): any; +} +interface ErrorEventErrorEventInit { + message?: string; + filename?: string; + lineno?: number; + colno?: number; + error?: any; +} +/** + * The **`MessageEvent`** interface represents a message received by a target object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent) + */ +declare class MessageEvent extends Event { + constructor(type: string, initializer: MessageEventInit); + /** + * The **`data`** read-only property of the The data sent by the message emitter; this can be any data type, depending on what originated this event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/data) + */ + readonly data: any; + /** + * The **`origin`** read-only property of the origin of the message emitter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/origin) + */ + readonly origin: string | null; + /** + * The **`lastEventId`** read-only property of the unique ID for the event. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/lastEventId) + */ + readonly lastEventId: string; + /** + * The **`source`** read-only property of the a WindowProxy, MessagePort, or a `MessageEventSource` (which can be a WindowProxy, message emitter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/source) + */ + readonly source: MessagePort | null; + /** + * The **`ports`** read-only property of the containing all MessagePort objects sent with the message, in order. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageEvent/ports) + */ + readonly ports: MessagePort[]; +} +interface MessageEventInit { + data: ArrayBuffer | string; +} +/** + * The **`PromiseRejectionEvent`** interface represents events which are sent to the global script context when JavaScript Promises are rejected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent) + */ +declare abstract class PromiseRejectionEvent extends Event { + /** + * The PromiseRejectionEvent interface's **`promise`** read-only property indicates the JavaScript rejected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent/promise) + */ + readonly promise: Promise; + /** + * The PromiseRejectionEvent **`reason`** read-only property is any JavaScript value or Object which provides the reason passed into Promise.reject(). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/PromiseRejectionEvent/reason) + */ + readonly reason: any; +} +/** + * The **`FormData`** interface provides a way to construct a set of key/value pairs representing form fields and their values, which can be sent using the Window/fetch, XMLHttpRequest.send() or navigator.sendBeacon() methods. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData) + */ +declare class FormData { + constructor(); + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: string | Blob): void; + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: string): void; + /** + * The **`append()`** method of the FormData interface appends a new value onto an existing key inside a `FormData` object, or adds the key if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/append) + */ + append(name: string, value: Blob, filename?: string): void; + /** + * The **`delete()`** method of the FormData interface deletes a key and its value(s) from a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/delete) + */ + delete(name: string): void; + /** + * The **`get()`** method of the FormData interface returns the first value associated with a given key from within a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/get) + */ + get(name: string): (File | string) | null; + /** + * The **`getAll()`** method of the FormData interface returns all the values associated with a given key from within a `FormData` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/getAll) + */ + getAll(name: string): (File | string)[]; + /** + * The **`has()`** method of the FormData interface returns whether a `FormData` object contains a certain key. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/has) + */ + has(name: string): boolean; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: string | Blob): void; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: string): void; + /** + * The **`set()`** method of the FormData interface sets a new value for an existing key inside a `FormData` object, or adds the key/value if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FormData/set) + */ + set(name: string, value: Blob, filename?: string): void; + /* Returns an array of key, value pairs for every entry in the list. */ + entries(): IterableIterator<[ + key: string, + value: File | string + ]>; + /* Returns a list of keys in the list. */ + keys(): IterableIterator; + /* Returns a list of values in the list. */ + values(): IterableIterator<(File | string)>; + forEach(callback: (this: This, value: File | string, key: string, parent: FormData) => void, thisArg?: This): void; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: File | string + ]>; +} +interface ContentOptions { + html?: boolean; +} +declare class HTMLRewriter { + constructor(); + on(selector: string, handlers: HTMLRewriterElementContentHandlers): HTMLRewriter; + onDocument(handlers: HTMLRewriterDocumentContentHandlers): HTMLRewriter; + transform(response: Response): Response; +} +interface HTMLRewriterElementContentHandlers { + element?(element: Element): void | Promise; + comments?(comment: Comment): void | Promise; + text?(element: Text): void | Promise; +} +interface HTMLRewriterDocumentContentHandlers { + doctype?(doctype: Doctype): void | Promise; + comments?(comment: Comment): void | Promise; + text?(text: Text): void | Promise; + end?(end: DocumentEnd): void | Promise; +} +interface Doctype { + readonly name: string | null; + readonly publicId: string | null; + readonly systemId: string | null; +} +interface Element { + tagName: string; + readonly attributes: IterableIterator; + readonly removed: boolean; + readonly namespaceURI: string; + getAttribute(name: string): string | null; + hasAttribute(name: string): boolean; + setAttribute(name: string, value: string): Element; + removeAttribute(name: string): Element; + before(content: string | ReadableStream | Response, options?: ContentOptions): Element; + after(content: string | ReadableStream | Response, options?: ContentOptions): Element; + prepend(content: string | ReadableStream | Response, options?: ContentOptions): Element; + append(content: string | ReadableStream | Response, options?: ContentOptions): Element; + replace(content: string | ReadableStream | Response, options?: ContentOptions): Element; + remove(): Element; + removeAndKeepContent(): Element; + setInnerContent(content: string | ReadableStream | Response, options?: ContentOptions): Element; + onEndTag(handler: (tag: EndTag) => void | Promise): void; +} +interface EndTag { + name: string; + before(content: string | ReadableStream | Response, options?: ContentOptions): EndTag; + after(content: string | ReadableStream | Response, options?: ContentOptions): EndTag; + remove(): EndTag; +} +interface Comment { + text: string; + readonly removed: boolean; + before(content: string, options?: ContentOptions): Comment; + after(content: string, options?: ContentOptions): Comment; + replace(content: string, options?: ContentOptions): Comment; + remove(): Comment; +} +interface Text { + readonly text: string; + readonly lastInTextNode: boolean; + readonly removed: boolean; + before(content: string | ReadableStream | Response, options?: ContentOptions): Text; + after(content: string | ReadableStream | Response, options?: ContentOptions): Text; + replace(content: string | ReadableStream | Response, options?: ContentOptions): Text; + remove(): Text; +} +interface DocumentEnd { + append(content: string, options?: ContentOptions): DocumentEnd; +} +/** + * This is the event type for `fetch` events dispatched on the ServiceWorkerGlobalScope. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent) + */ +declare abstract class FetchEvent extends ExtendableEvent { + /** + * The **`request`** read-only property of the the event handler. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent/request) + */ + readonly request: Request; + /** + * The **`respondWith()`** method of allows you to provide a promise for a Response yourself. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/FetchEvent/respondWith) + */ + respondWith(promise: Response | Promise): void; + passThroughOnException(): void; +} +type HeadersInit = Headers | Iterable> | Record; +/** + * The **`Headers`** interface of the Fetch API allows you to perform various actions on HTTP request and response headers. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers) + */ +declare class Headers { + constructor(init?: HeadersInit); + /** + * The **`get()`** method of the Headers interface returns a byte string of all the values of a header within a `Headers` object with a given name. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/get) + */ + get(name: string): string | null; + getAll(name: string): string[]; + /** + * The **`getSetCookie()`** method of the Headers interface returns an array containing the values of all Set-Cookie headers associated with a response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/getSetCookie) + */ + getSetCookie(): string[]; + /** + * The **`has()`** method of the Headers interface returns a boolean stating whether a `Headers` object contains a certain header. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/has) + */ + has(name: string): boolean; + /** + * The **`set()`** method of the Headers interface sets a new value for an existing header inside a `Headers` object, or adds the header if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set) + */ + set(name: string, value: string): void; + /** + * The **`append()`** method of the Headers interface appends a new value onto an existing header inside a `Headers` object, or adds the header if it does not already exist. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/append) + */ + append(name: string, value: string): void; + /** + * The **`delete()`** method of the Headers interface deletes a header from the current `Headers` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/delete) + */ + delete(name: string): void; + forEach(callback: (this: This, value: string, key: string, parent: Headers) => void, thisArg?: This): void; + /* Returns an iterator allowing to go through all key/value pairs contained in this object. */ + entries(): IterableIterator<[ + key: string, + value: string + ]>; + /* Returns an iterator allowing to go through all keys of the key/value pairs contained in this object. */ + keys(): IterableIterator; + /* Returns an iterator allowing to go through all values of the key/value pairs contained in this object. */ + values(): IterableIterator; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: string + ]>; +} +type BodyInit = ReadableStream | string | ArrayBuffer | ArrayBufferView | Blob | URLSearchParams | FormData | Iterable | AsyncIterable; +declare abstract class Body { + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/body) */ + get body(): ReadableStream | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bodyUsed) */ + get bodyUsed(): boolean; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/arrayBuffer) */ + arrayBuffer(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes) */ + bytes(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/text) */ + text(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */ + json(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/formData) */ + formData(): Promise; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */ + blob(): Promise; +} +/** + * The **`Response`** interface of the Fetch API represents the response to a request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response) + */ +declare var Response: { + prototype: Response; + new (body?: BodyInit | null, init?: ResponseInit): Response; + error(): Response; + redirect(url: string, status?: number): Response; + json(any: any, maybeInit?: (ResponseInit | Response)): Response; +}; +/** + * The **`Response`** interface of the Fetch API represents the response to a request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response) + */ +interface Response extends Body { + /** + * The **`clone()`** method of the Response interface creates a clone of a response object, identical in every way, but stored in a different variable. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/clone) + */ + clone(): Response; + /** + * The **`status`** read-only property of the Response interface contains the HTTP status codes of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/status) + */ + status: number; + /** + * The **`statusText`** read-only property of the Response interface contains the status message corresponding to the HTTP status code in Response.status. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/statusText) + */ + statusText: string; + /** + * The **`headers`** read-only property of the with the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/headers) + */ + headers: Headers; + /** + * The **`ok`** read-only property of the Response interface contains a Boolean stating whether the response was successful (status in the range 200-299) or not. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/ok) + */ + ok: boolean; + /** + * The **`redirected`** read-only property of the Response interface indicates whether or not the response is the result of a request you made which was redirected. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/redirected) + */ + redirected: boolean; + /** + * The **`url`** read-only property of the Response interface contains the URL of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/url) + */ + url: string; + webSocket: WebSocket | null; + cf: any | undefined; + /** + * The **`type`** read-only property of the Response interface contains the type of the response. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response/type) + */ + type: "default" | "error"; +} +interface ResponseInit { + status?: number; + statusText?: string; + headers?: HeadersInit; + cf?: any; + webSocket?: (WebSocket | null); + encodeBody?: "automatic" | "manual"; +} +type RequestInfo> = Request | string; +/** + * The **`Request`** interface of the Fetch API represents a resource request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request) + */ +declare var Request: { + prototype: Request; + new >(input: RequestInfo | URL, init?: RequestInit): Request; +}; +/** + * The **`Request`** interface of the Fetch API represents a resource request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request) + */ +interface Request> extends Body { + /** + * The **`clone()`** method of the Request interface creates a copy of the current `Request` object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/clone) + */ + clone(): Request; + /** + * The **`method`** read-only property of the `POST`, etc.) A String indicating the method of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/method) + */ + method: string; + /** + * The **`url`** read-only property of the Request interface contains the URL of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/url) + */ + url: string; + /** + * The **`headers`** read-only property of the with the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/headers) + */ + headers: Headers; + /** + * The **`redirect`** read-only property of the Request interface contains the mode for how redirects are handled. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/redirect) + */ + redirect: string; + fetcher: Fetcher | null; + /** + * The read-only **`signal`** property of the Request interface returns the AbortSignal associated with the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/signal) + */ + signal: AbortSignal; + cf?: Cf; + /** + * The **`integrity`** read-only property of the Request interface contains the subresource integrity value of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/integrity) + */ + integrity: string; + /** + * The **`keepalive`** read-only property of the Request interface contains the request's `keepalive` setting (`true` or `false`), which indicates whether the browser will keep the associated request alive if the page that initiated it is unloaded before the request is complete. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/keepalive) + */ + keepalive: boolean; + /** + * The **`cache`** read-only property of the Request interface contains the cache mode of the request. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/cache) + */ + cache?: "no-store" | "no-cache"; +} +interface RequestInit { + /* A string to set request's method. */ + method?: string; + /* A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ + headers?: HeadersInit; + /* A BodyInit object or null to set request's body. */ + body?: BodyInit | null; + /* A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ + redirect?: string; + fetcher?: (Fetcher | null); + cf?: Cf; + /* A string indicating how the request will interact with the browser's cache to set request's cache. */ + cache?: "no-store" | "no-cache"; + /* A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ + integrity?: string; + /* An AbortSignal to set request's signal. */ + signal?: (AbortSignal | null); + encodeResponseBody?: "automatic" | "manual"; +} +type Service Rpc.WorkerEntrypointBranded) | Rpc.WorkerEntrypointBranded | ExportedHandler | undefined = undefined> = T extends new (...args: any[]) => Rpc.WorkerEntrypointBranded ? Fetcher> : T extends Rpc.WorkerEntrypointBranded ? Fetcher : T extends Exclude ? never : Fetcher; +type Fetcher = (T extends Rpc.EntrypointBranded ? Rpc.Provider : unknown) & { + fetch(input: RequestInfo | URL, init?: RequestInit): Promise; + connect(address: SocketAddress | string, options?: SocketOptions): Socket; +}; +interface KVNamespaceListKey { + name: Key; + expiration?: number; + metadata?: Metadata; +} +type KVNamespaceListResult = { + list_complete: false; + keys: KVNamespaceListKey[]; + cursor: string; + cacheStatus: string | null; +} | { + list_complete: true; + keys: KVNamespaceListKey[]; + cacheStatus: string | null; +}; +interface KVNamespace { + get(key: Key, options?: Partial>): Promise; + get(key: Key, type: "text"): Promise; + get(key: Key, type: "json"): Promise; + get(key: Key, type: "arrayBuffer"): Promise; + get(key: Key, type: "stream"): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"text">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"json">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"arrayBuffer">): Promise; + get(key: Key, options?: KVNamespaceGetOptions<"stream">): Promise; + get(key: Array, type: "text"): Promise>; + get(key: Array, type: "json"): Promise>; + get(key: Array, options?: Partial>): Promise>; + get(key: Array, options?: KVNamespaceGetOptions<"text">): Promise>; + get(key: Array, options?: KVNamespaceGetOptions<"json">): Promise>; + list(options?: KVNamespaceListOptions): Promise>; + put(key: Key, value: string | ArrayBuffer | ArrayBufferView | ReadableStream, options?: KVNamespacePutOptions): Promise; + getWithMetadata(key: Key, options?: Partial>): Promise>; + getWithMetadata(key: Key, type: "text"): Promise>; + getWithMetadata(key: Key, type: "json"): Promise>; + getWithMetadata(key: Key, type: "arrayBuffer"): Promise>; + getWithMetadata(key: Key, type: "stream"): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"text">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"json">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"arrayBuffer">): Promise>; + getWithMetadata(key: Key, options: KVNamespaceGetOptions<"stream">): Promise>; + getWithMetadata(key: Array, type: "text"): Promise>>; + getWithMetadata(key: Array, type: "json"): Promise>>; + getWithMetadata(key: Array, options?: Partial>): Promise>>; + getWithMetadata(key: Array, options?: KVNamespaceGetOptions<"text">): Promise>>; + getWithMetadata(key: Array, options?: KVNamespaceGetOptions<"json">): Promise>>; + delete(key: Key): Promise; +} +interface KVNamespaceListOptions { + limit?: number; + prefix?: (string | null); + cursor?: (string | null); +} +interface KVNamespaceGetOptions { + type: Type; + cacheTtl?: number; +} +interface KVNamespacePutOptions { + expiration?: number; + expirationTtl?: number; + metadata?: (any | null); +} +interface KVNamespaceGetWithMetadataResult { + value: Value | null; + metadata: Metadata | null; + cacheStatus: string | null; +} +type QueueContentType = "text" | "bytes" | "json" | "v8"; +interface Queue { + metrics(): Promise; + send(message: Body, options?: QueueSendOptions): Promise; + sendBatch(messages: Iterable>, options?: QueueSendBatchOptions): Promise; +} +interface QueueSendMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface QueueSendMetadata { + metrics: QueueSendMetrics; +} +interface QueueSendResponse { + metadata: QueueSendMetadata; +} +interface QueueSendBatchMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface QueueSendBatchMetadata { + metrics: QueueSendBatchMetrics; +} +interface QueueSendBatchResponse { + metadata: QueueSendBatchMetadata; +} +interface QueueSendOptions { + contentType?: QueueContentType; + delaySeconds?: number; +} +interface QueueSendBatchOptions { + delaySeconds?: number; +} +interface MessageSendRequest { + body: Body; + contentType?: QueueContentType; + delaySeconds?: number; +} +interface QueueMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface MessageBatchMetrics { + backlogCount: number; + backlogBytes: number; + oldestMessageTimestamp?: Date; +} +interface MessageBatchMetadata { + metrics: MessageBatchMetrics; +} +interface QueueRetryOptions { + delaySeconds?: number; +} +interface Message { + readonly id: string; + readonly timestamp: Date; + readonly body: Body; + readonly attempts: number; + retry(options?: QueueRetryOptions): void; + ack(): void; +} +interface QueueEvent extends ExtendableEvent { + readonly messages: readonly Message[]; + readonly queue: string; + readonly metadata: MessageBatchMetadata; + retryAll(options?: QueueRetryOptions): void; + ackAll(): void; +} +interface MessageBatch { + readonly messages: readonly Message[]; + readonly queue: string; + readonly metadata: MessageBatchMetadata; + retryAll(options?: QueueRetryOptions): void; + ackAll(): void; +} +interface R2Error extends Error { + readonly name: string; + readonly code: number; + readonly message: string; + readonly action: string; + readonly stack: any; +} +interface R2ListOptions { + limit?: number; + prefix?: string; + cursor?: string; + delimiter?: string; + startAfter?: string; + include?: ("httpMetadata" | "customMetadata")[]; +} +interface R2Bucket { + head(key: string): Promise; + get(key: string, options: R2GetOptions & { + onlyIf: R2Conditional | Headers; + }): Promise; + get(key: string, options?: R2GetOptions): Promise; + put(key: string, value: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob, options?: R2PutOptions & { + onlyIf: R2Conditional | Headers; + }): Promise; + put(key: string, value: ReadableStream | ArrayBuffer | ArrayBufferView | string | null | Blob, options?: R2PutOptions): Promise; + createMultipartUpload(key: string, options?: R2MultipartOptions): Promise; + resumeMultipartUpload(key: string, uploadId: string): R2MultipartUpload; + delete(keys: string | string[]): Promise; + list(options?: R2ListOptions): Promise; +} +interface R2MultipartUpload { + readonly key: string; + readonly uploadId: string; + uploadPart(partNumber: number, value: ReadableStream | (ArrayBuffer | ArrayBufferView) | string | Blob, options?: R2UploadPartOptions): Promise; + abort(): Promise; + complete(uploadedParts: R2UploadedPart[]): Promise; +} +interface R2UploadedPart { + partNumber: number; + etag: string; +} +declare abstract class R2Object { + readonly key: string; + readonly version: string; + readonly size: number; + readonly etag: string; + readonly httpEtag: string; + readonly checksums: R2Checksums; + readonly uploaded: Date; + readonly httpMetadata?: R2HTTPMetadata; + readonly customMetadata?: Record; + readonly range?: R2Range; + readonly storageClass: string; + readonly ssecKeyMd5?: string; + writeHttpMetadata(headers: Headers): void; +} +interface R2ObjectBody extends R2Object { + get body(): ReadableStream; + get bodyUsed(): boolean; + arrayBuffer(): Promise; + bytes(): Promise; + text(): Promise; + json(): Promise; + blob(): Promise; +} +type R2Range = { + offset: number; + length?: number; +} | { + offset?: number; + length: number; +} | { + suffix: number; +}; +interface R2Conditional { + etagMatches?: string; + etagDoesNotMatch?: string; + uploadedBefore?: Date; + uploadedAfter?: Date; + secondsGranularity?: boolean; +} +interface R2GetOptions { + onlyIf?: (R2Conditional | Headers); + range?: (R2Range | Headers); + ssecKey?: (ArrayBuffer | string); +} +interface R2PutOptions { + onlyIf?: (R2Conditional | Headers); + httpMetadata?: (R2HTTPMetadata | Headers); + customMetadata?: Record; + md5?: ((ArrayBuffer | ArrayBufferView) | string); + sha1?: ((ArrayBuffer | ArrayBufferView) | string); + sha256?: ((ArrayBuffer | ArrayBufferView) | string); + sha384?: ((ArrayBuffer | ArrayBufferView) | string); + sha512?: ((ArrayBuffer | ArrayBufferView) | string); + storageClass?: string; + ssecKey?: (ArrayBuffer | string); +} +interface R2MultipartOptions { + httpMetadata?: (R2HTTPMetadata | Headers); + customMetadata?: Record; + storageClass?: string; + ssecKey?: (ArrayBuffer | string); +} +interface R2Checksums { + readonly md5?: ArrayBuffer; + readonly sha1?: ArrayBuffer; + readonly sha256?: ArrayBuffer; + readonly sha384?: ArrayBuffer; + readonly sha512?: ArrayBuffer; + toJSON(): R2StringChecksums; +} +interface R2StringChecksums { + md5?: string; + sha1?: string; + sha256?: string; + sha384?: string; + sha512?: string; +} +interface R2HTTPMetadata { + contentType?: string; + contentLanguage?: string; + contentDisposition?: string; + contentEncoding?: string; + cacheControl?: string; + cacheExpiry?: Date; +} +type R2Objects = { + objects: R2Object[]; + delimitedPrefixes: string[]; +} & ({ + truncated: true; + cursor: string; +} | { + truncated: false; +}); +interface R2UploadPartOptions { + ssecKey?: (ArrayBuffer | string); +} +declare abstract class ScheduledEvent extends ExtendableEvent { + readonly scheduledTime: number; + readonly cron: string; + noRetry(): void; +} +interface ScheduledController { + readonly scheduledTime: number; + readonly cron: string; + noRetry(): void; +} +interface QueuingStrategy { + highWaterMark?: (number | bigint); + size?: (chunk: T) => number | bigint; +} +interface UnderlyingSink { + type?: string; + start?: (controller: WritableStreamDefaultController) => void | Promise; + write?: (chunk: W, controller: WritableStreamDefaultController) => void | Promise; + abort?: (reason: any) => void | Promise; + close?: () => void | Promise; +} +interface UnderlyingByteSource { + type: "bytes"; + autoAllocateChunkSize?: number; + start?: (controller: ReadableByteStreamController) => void | Promise; + pull?: (controller: ReadableByteStreamController) => void | Promise; + cancel?: (reason: any) => void | Promise; +} +interface UnderlyingSource { + type?: "" | undefined; + start?: (controller: ReadableStreamDefaultController) => void | Promise; + pull?: (controller: ReadableStreamDefaultController) => void | Promise; + cancel?: (reason: any) => void | Promise; + expectedLength?: (number | bigint); +} +interface Transformer { + readableType?: string; + writableType?: string; + start?: (controller: TransformStreamDefaultController) => void | Promise; + transform?: (chunk: I, controller: TransformStreamDefaultController) => void | Promise; + flush?: (controller: TransformStreamDefaultController) => void | Promise; + cancel?: (reason: any) => void | Promise; + expectedLength?: number; +} +interface StreamPipeOptions { + preventAbort?: boolean; + preventCancel?: boolean; + /** + * Pipes this readable stream to a given writable stream destination. The way in which the piping process behaves under various error conditions can be customized with a number of passed options. It returns a promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. + * + * Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader. + * + * Errors and closures of the source and destination streams propagate as follows: + * + * An error in this source readable stream will abort destination, unless preventAbort is truthy. The returned promise will be rejected with the source's error, or with any error that occurs during aborting the destination. + * + * An error in destination will cancel this source readable stream, unless preventCancel is truthy. The returned promise will be rejected with the destination's error, or with any error that occurs during canceling the source. + * + * When this source readable stream closes, destination will be closed, unless preventClose is truthy. The returned promise will be fulfilled once this process completes, unless an error is encountered while closing the destination, in which case it will be rejected with that error. + * + * If destination starts out closed or closing, this source readable stream will be canceled, unless preventCancel is true. The returned promise will be rejected with an error indicating piping to a closed stream failed, or with any error that occurs during canceling the source. + * + * The signal option can be set to an AbortSignal to allow aborting an ongoing pipe operation via the corresponding AbortController. In this case, this source readable stream will be canceled, and destination aborted, unless the respective options preventCancel or preventAbort are set. + */ + preventClose?: boolean; + signal?: AbortSignal; +} +type ReadableStreamReadResult = { + done: false; + value: R; +} | { + done: true; + value?: undefined; +}; +/** + * The `ReadableStream` interface of the Streams API represents a readable stream of byte data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream) + */ +interface ReadableStream { + /** + * The **`locked`** read-only property of the ReadableStream interface returns whether or not the readable stream is locked to a reader. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/locked) + */ + get locked(): boolean; + /** + * The **`cancel()`** method of the ReadableStream interface returns a Promise that resolves when the stream is canceled. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/cancel) + */ + cancel(reason?: any): Promise; + /** + * The **`getReader()`** method of the ReadableStream interface creates a reader and locks the stream to it. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader) + */ + getReader(): ReadableStreamDefaultReader; + /** + * The **`getReader()`** method of the ReadableStream interface creates a reader and locks the stream to it. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/getReader) + */ + getReader(options: ReadableStreamGetReaderOptions): ReadableStreamBYOBReader; + /** + * The **`pipeThrough()`** method of the ReadableStream interface provides a chainable way of piping the current stream through a transform stream or any other writable/readable pair. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeThrough) + */ + pipeThrough(transform: ReadableWritablePair, options?: StreamPipeOptions): ReadableStream; + /** + * The **`pipeTo()`** method of the ReadableStream interface pipes the current `ReadableStream` to a given WritableStream and returns a Promise that fulfills when the piping process completes successfully, or rejects if any errors were encountered. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/pipeTo) + */ + pipeTo(destination: WritableStream, options?: StreamPipeOptions): Promise; + /** + * The **`tee()`** method of the two-element array containing the two resulting branches as new ReadableStream instances. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream/tee) + */ + tee(): [ + ReadableStream, + ReadableStream + ]; + values(options?: ReadableStreamValuesOptions): AsyncIterableIterator; + [Symbol.asyncIterator](options?: ReadableStreamValuesOptions): AsyncIterableIterator; +} +/** + * The `ReadableStream` interface of the Streams API represents a readable stream of byte data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStream) + */ +declare const ReadableStream: { + prototype: ReadableStream; + new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy): ReadableStream; + new (underlyingSource?: UnderlyingSource, strategy?: QueuingStrategy): ReadableStream; +}; +/** + * The **`ReadableStreamDefaultReader`** interface of the Streams API represents a default reader that can be used to read stream data supplied from a network (such as a fetch request). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader) + */ +declare class ReadableStreamDefaultReader { + constructor(stream: ReadableStream); + get closed(): Promise; + cancel(reason?: any): Promise; + /** + * The **`read()`** method of the ReadableStreamDefaultReader interface returns a Promise providing access to the next chunk in the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/read) + */ + read(): Promise>; + /** + * The **`releaseLock()`** method of the ReadableStreamDefaultReader interface releases the reader's lock on the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader/releaseLock) + */ + releaseLock(): void; +} +/** + * The `ReadableStreamBYOBReader` interface of the Streams API defines a reader for a ReadableStream that supports zero-copy reading from an underlying byte source. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader) + */ +declare class ReadableStreamBYOBReader { + constructor(stream: ReadableStream); + get closed(): Promise; + cancel(reason?: any): Promise; + /** + * The **`read()`** method of the ReadableStreamBYOBReader interface is used to read data into a view on a user-supplied buffer from an associated readable byte stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/read) + */ + read(view: T): Promise>; + /** + * The **`releaseLock()`** method of the ReadableStreamBYOBReader interface releases the reader's lock on the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader/releaseLock) + */ + releaseLock(): void; + readAtLeast(minElements: number, view: T): Promise>; +} +interface ReadableStreamBYOBReaderReadableStreamBYOBReaderReadOptions { + min?: number; +} +interface ReadableStreamGetReaderOptions { + /** + * Creates a ReadableStreamBYOBReader and locks the stream to the new reader. + * + * This call behaves the same way as the no-argument variant, except that it only works on readable byte streams, i.e. streams which were constructed specifically with the ability to handle "bring your own buffer" reading. The returned BYOB reader provides the ability to directly read individual chunks from the stream via its read() method, into developer-supplied buffers, allowing more precise control over allocation. + */ + mode: "byob"; +} +/** + * The **`ReadableStreamBYOBRequest`** interface of the Streams API represents a 'pull request' for data from an underlying source that will made as a zero-copy transfer to a consumer (bypassing the stream's internal queues). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest) + */ +declare abstract class ReadableStreamBYOBRequest { + /** + * The **`view`** getter property of the ReadableStreamBYOBRequest interface returns the current view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/view) + */ + get view(): Uint8Array | null; + /** + * The **`respond()`** method of the ReadableStreamBYOBRequest interface is used to signal to the associated readable byte stream that the specified number of bytes were written into the ReadableStreamBYOBRequest.view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/respond) + */ + respond(bytesWritten: number): void; + /** + * The **`respondWithNewView()`** method of the ReadableStreamBYOBRequest interface specifies a new view that the consumer of the associated readable byte stream should write to instead of ReadableStreamBYOBRequest.view. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBRequest/respondWithNewView) + */ + respondWithNewView(view: ArrayBuffer | ArrayBufferView): void; + get atLeast(): number | null; +} +/** + * The **`ReadableStreamDefaultController`** interface of the Streams API represents a controller allowing control of a ReadableStream's state and internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController) + */ +declare abstract class ReadableStreamDefaultController { + /** + * The **`desiredSize`** read-only property of the required to fill the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`close()`** method of the ReadableStreamDefaultController interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/close) + */ + close(): void; + /** + * The **`enqueue()`** method of the ```js-nolint enqueue(chunk) ``` - `chunk` - : The chunk to enqueue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/enqueue) + */ + enqueue(chunk?: R): void; + /** + * The **`error()`** method of the with the associated stream to error. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultController/error) + */ + error(reason: any): void; +} +/** + * The **`ReadableByteStreamController`** interface of the Streams API represents a controller for a readable byte stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController) + */ +declare abstract class ReadableByteStreamController { + /** + * The **`byobRequest`** read-only property of the ReadableByteStreamController interface returns the current BYOB request, or `null` if there are no pending requests. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/byobRequest) + */ + get byobRequest(): ReadableStreamBYOBRequest | null; + /** + * The **`desiredSize`** read-only property of the ReadableByteStreamController interface returns the number of bytes required to fill the stream's internal queue to its 'desired size'. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`close()`** method of the ReadableByteStreamController interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/close) + */ + close(): void; + /** + * The **`enqueue()`** method of the ReadableByteStreamController interface enqueues a given chunk on the associated readable byte stream (the chunk is copied into the stream's internal queues). + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/enqueue) + */ + enqueue(chunk: ArrayBuffer | ArrayBufferView): void; + /** + * The **`error()`** method of the ReadableByteStreamController interface causes any future interactions with the associated stream to error with the specified reason. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableByteStreamController/error) + */ + error(reason: any): void; +} +/** + * The **`WritableStreamDefaultController`** interface of the Streams API represents a controller allowing control of a WritableStream's state. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController) + */ +declare abstract class WritableStreamDefaultController { + /** + * The read-only **`signal`** property of the WritableStreamDefaultController interface returns the AbortSignal associated with the controller. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController/signal) + */ + get signal(): AbortSignal; + /** + * The **`error()`** method of the with the associated stream to error. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultController/error) + */ + error(reason?: any): void; +} +/** + * The **`TransformStreamDefaultController`** interface of the Streams API provides methods to manipulate the associated ReadableStream and WritableStream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController) + */ +declare abstract class TransformStreamDefaultController { + /** + * The **`desiredSize`** read-only property of the TransformStreamDefaultController interface returns the desired size to fill the queue of the associated ReadableStream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`enqueue()`** method of the TransformStreamDefaultController interface enqueues the given chunk in the readable side of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/enqueue) + */ + enqueue(chunk?: O): void; + /** + * The **`error()`** method of the TransformStreamDefaultController interface errors both sides of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/error) + */ + error(reason: any): void; + /** + * The **`terminate()`** method of the TransformStreamDefaultController interface closes the readable side and errors the writable side of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStreamDefaultController/terminate) + */ + terminate(): void; +} +interface ReadableWritablePair { + readable: ReadableStream; + /** + * Provides a convenient, chainable way of piping this readable stream through a transform stream (or any other { writable, readable } pair). It simply pipes the stream into the writable side of the supplied pair, and returns the readable side for further use. + * + * Piping a stream will lock it for the duration of the pipe, preventing any other consumer from acquiring a reader. + */ + writable: WritableStream; +} +/** + * The **`WritableStream`** interface of the Streams API provides a standard abstraction for writing streaming data to a destination, known as a sink. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream) + */ +declare class WritableStream { + constructor(underlyingSink?: UnderlyingSink, queuingStrategy?: QueuingStrategy); + /** + * The **`locked`** read-only property of the WritableStream interface returns a boolean indicating whether the `WritableStream` is locked to a writer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/locked) + */ + get locked(): boolean; + /** + * The **`abort()`** method of the WritableStream interface aborts the stream, signaling that the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/abort) + */ + abort(reason?: any): Promise; + /** + * The **`close()`** method of the WritableStream interface closes the associated stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/close) + */ + close(): Promise; + /** + * The **`getWriter()`** method of the WritableStream interface returns a new instance of WritableStreamDefaultWriter and locks the stream to that instance. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStream/getWriter) + */ + getWriter(): WritableStreamDefaultWriter; +} +/** + * The **`WritableStreamDefaultWriter`** interface of the Streams API is the object returned by WritableStream.getWriter() and once created locks the writer to the `WritableStream` ensuring that no other streams can write to the underlying sink. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter) + */ +declare class WritableStreamDefaultWriter { + constructor(stream: WritableStream); + /** + * The **`closed`** read-only property of the the stream errors or the writer's lock is released. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/closed) + */ + get closed(): Promise; + /** + * The **`ready`** read-only property of the that resolves when the desired size of the stream's internal queue transitions from non-positive to positive, signaling that it is no longer applying backpressure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/ready) + */ + get ready(): Promise; + /** + * The **`desiredSize`** read-only property of the to fill the stream's internal queue. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/desiredSize) + */ + get desiredSize(): number | null; + /** + * The **`abort()`** method of the the producer can no longer successfully write to the stream and it is to be immediately moved to an error state, with any queued writes discarded. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/abort) + */ + abort(reason?: any): Promise; + /** + * The **`close()`** method of the stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/close) + */ + close(): Promise; + /** + * The **`write()`** method of the operation. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/write) + */ + write(chunk?: W): Promise; + /** + * The **`releaseLock()`** method of the corresponding stream. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/releaseLock) + */ + releaseLock(): void; +} +/** + * The **`TransformStream`** interface of the Streams API represents a concrete implementation of the pipe chain _transform stream_ concept. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream) + */ +declare class TransformStream { + constructor(transformer?: Transformer, writableStrategy?: QueuingStrategy, readableStrategy?: QueuingStrategy); + /** + * The **`readable`** read-only property of the TransformStream interface returns the ReadableStream instance controlled by this `TransformStream`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream/readable) + */ + get readable(): ReadableStream; + /** + * The **`writable`** read-only property of the TransformStream interface returns the WritableStream instance controlled by this `TransformStream`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TransformStream/writable) + */ + get writable(): WritableStream; +} +declare class FixedLengthStream extends IdentityTransformStream { + constructor(expectedLength: number | bigint, queuingStrategy?: IdentityTransformStreamQueuingStrategy); +} +declare class IdentityTransformStream extends TransformStream { + constructor(queuingStrategy?: IdentityTransformStreamQueuingStrategy); +} +interface IdentityTransformStreamQueuingStrategy { + highWaterMark?: (number | bigint); +} +interface ReadableStreamValuesOptions { + preventCancel?: boolean; +} +/** + * The **`CompressionStream`** interface of the Compression Streams API is an API for compressing a stream of data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CompressionStream) + */ +declare class CompressionStream extends TransformStream { + constructor(format: "gzip" | "deflate" | "deflate-raw"); +} +/** + * The **`DecompressionStream`** interface of the Compression Streams API is an API for decompressing a stream of data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/DecompressionStream) + */ +declare class DecompressionStream extends TransformStream { + constructor(format: "gzip" | "deflate" | "deflate-raw"); +} +/** + * The **`TextEncoderStream`** interface of the Encoding API converts a stream of strings into bytes in the UTF-8 encoding. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextEncoderStream) + */ +declare class TextEncoderStream extends TransformStream { + constructor(); + get encoding(): string; +} +/** + * The **`TextDecoderStream`** interface of the Encoding API converts a stream of text in a binary encoding, such as UTF-8 etc., to a stream of strings. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/TextDecoderStream) + */ +declare class TextDecoderStream extends TransformStream { + constructor(label?: string, options?: TextDecoderStreamTextDecoderStreamInit); + get encoding(): string; + get fatal(): boolean; + get ignoreBOM(): boolean; +} +interface TextDecoderStreamTextDecoderStreamInit { + fatal?: boolean; + ignoreBOM?: boolean; +} +/** + * The **`ByteLengthQueuingStrategy`** interface of the Streams API provides a built-in byte length queuing strategy that can be used when constructing streams. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy) + */ +declare class ByteLengthQueuingStrategy implements QueuingStrategy { + constructor(init: QueuingStrategyInit); + /** + * The read-only **`ByteLengthQueuingStrategy.highWaterMark`** property returns the total number of bytes that can be contained in the internal queue before backpressure is applied. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy/highWaterMark) + */ + get highWaterMark(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/ByteLengthQueuingStrategy/size) */ + get size(): (chunk?: any) => number; +} +/** + * The **`CountQueuingStrategy`** interface of the Streams API provides a built-in chunk counting queuing strategy that can be used when constructing streams. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy) + */ +declare class CountQueuingStrategy implements QueuingStrategy { + constructor(init: QueuingStrategyInit); + /** + * The read-only **`CountQueuingStrategy.highWaterMark`** property returns the total number of chunks that can be contained in the internal queue before backpressure is applied. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy/highWaterMark) + */ + get highWaterMark(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/CountQueuingStrategy/size) */ + get size(): (chunk?: any) => number; +} +interface QueuingStrategyInit { + /** + * Creates a new ByteLengthQueuingStrategy with the provided high water mark. + * + * Note that the provided high water mark will not be validated ahead of time. Instead, if it is negative, NaN, or not a number, the resulting ByteLengthQueuingStrategy will cause the corresponding stream constructor to throw. + */ + highWaterMark: number; +} +interface TracePreviewInfo { + id: string; + slug: string; + name: string; +} +interface ScriptVersion { + id?: string; + tag?: string; + message?: string; +} +declare abstract class TailEvent extends ExtendableEvent { + readonly events: TraceItem[]; + readonly traces: TraceItem[]; +} +interface TraceItem { + readonly event: (TraceItemFetchEventInfo | TraceItemJsRpcEventInfo | TraceItemConnectEventInfo | TraceItemScheduledEventInfo | TraceItemAlarmEventInfo | TraceItemQueueEventInfo | TraceItemEmailEventInfo | TraceItemTailEventInfo | TraceItemCustomEventInfo | TraceItemHibernatableWebSocketEventInfo) | null; + readonly eventTimestamp: number | null; + readonly logs: TraceLog[]; + readonly exceptions: TraceException[]; + readonly diagnosticsChannelEvents: TraceDiagnosticChannelEvent[]; + readonly scriptName: string | null; + readonly entrypoint?: string; + readonly scriptVersion?: ScriptVersion; + readonly dispatchNamespace?: string; + readonly scriptTags?: string[]; + readonly tailAttributes?: Record; + readonly preview?: TracePreviewInfo; + readonly durableObjectId?: string; + readonly outcome: string; + readonly executionModel: string; + readonly truncated: boolean; + readonly cpuTime: number; + readonly wallTime: number; +} +interface TraceItemAlarmEventInfo { + readonly scheduledTime: Date; +} +interface TraceItemConnectEventInfo { +} +interface TraceItemCustomEventInfo { +} +interface TraceItemScheduledEventInfo { + readonly scheduledTime: number; + readonly cron: string; +} +interface TraceItemQueueEventInfo { + readonly queue: string; + readonly batchSize: number; +} +interface TraceItemEmailEventInfo { + readonly mailFrom: string; + readonly rcptTo: string; + readonly rawSize: number; +} +interface TraceItemTailEventInfo { + readonly consumedEvents: TraceItemTailEventInfoTailItem[]; +} +interface TraceItemTailEventInfoTailItem { + readonly scriptName: string | null; +} +interface TraceItemFetchEventInfo { + readonly response?: TraceItemFetchEventInfoResponse; + readonly request: TraceItemFetchEventInfoRequest; +} +interface TraceItemFetchEventInfoRequest { + readonly cf?: any; + readonly headers: Record; + readonly method: string; + readonly url: string; + getUnredacted(): TraceItemFetchEventInfoRequest; +} +interface TraceItemFetchEventInfoResponse { + readonly status: number; +} +interface TraceItemJsRpcEventInfo { + readonly rpcMethod: string; +} +interface TraceItemHibernatableWebSocketEventInfo { + readonly getWebSocketEvent: TraceItemHibernatableWebSocketEventInfoMessage | TraceItemHibernatableWebSocketEventInfoClose | TraceItemHibernatableWebSocketEventInfoError; +} +interface TraceItemHibernatableWebSocketEventInfoMessage { + readonly webSocketEventType: string; +} +interface TraceItemHibernatableWebSocketEventInfoClose { + readonly webSocketEventType: string; + readonly code: number; + readonly wasClean: boolean; +} +interface TraceItemHibernatableWebSocketEventInfoError { + readonly webSocketEventType: string; +} +interface TraceLog { + readonly timestamp: number; + readonly level: string; + readonly message: any; +} +interface TraceException { + readonly timestamp: number; + readonly message: string; + readonly name: string; + readonly stack?: string; +} +interface TraceDiagnosticChannelEvent { + readonly timestamp: number; + readonly channel: string; + readonly message: any; +} +interface TraceMetrics { + readonly cpuTime: number; + readonly wallTime: number; +} +interface UnsafeTraceMetrics { + fromTrace(item: TraceItem): TraceMetrics; +} +/** + * The **`URL`** interface is used to parse, construct, normalize, and encode URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL) + */ +declare class URL { + constructor(url: string | URL, base?: string | URL); + /** + * The **`origin`** read-only property of the URL interface returns a string containing the Unicode serialization of the origin of the represented URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/origin) + */ + get origin(): string; + /** + * The **`href`** property of the URL interface is a string containing the whole URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/href) + */ + get href(): string; + /** + * The **`href`** property of the URL interface is a string containing the whole URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/href) + */ + set href(value: string); + /** + * The **`protocol`** property of the URL interface is a string containing the protocol or scheme of the URL, including the final `':'`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/protocol) + */ + get protocol(): string; + /** + * The **`protocol`** property of the URL interface is a string containing the protocol or scheme of the URL, including the final `':'`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/protocol) + */ + set protocol(value: string); + /** + * The **`username`** property of the URL interface is a string containing the username component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/username) + */ + get username(): string; + /** + * The **`username`** property of the URL interface is a string containing the username component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/username) + */ + set username(value: string); + /** + * The **`password`** property of the URL interface is a string containing the password component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/password) + */ + get password(): string; + /** + * The **`password`** property of the URL interface is a string containing the password component of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/password) + */ + set password(value: string); + /** + * The **`host`** property of the URL interface is a string containing the host, which is the URL.hostname, and then, if the port of the URL is nonempty, a `':'`, followed by the URL.port of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/host) + */ + get host(): string; + /** + * The **`host`** property of the URL interface is a string containing the host, which is the URL.hostname, and then, if the port of the URL is nonempty, a `':'`, followed by the URL.port of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/host) + */ + set host(value: string); + /** + * The **`hostname`** property of the URL interface is a string containing either the domain name or IP address of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hostname) + */ + get hostname(): string; + /** + * The **`hostname`** property of the URL interface is a string containing either the domain name or IP address of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hostname) + */ + set hostname(value: string); + /** + * The **`port`** property of the URL interface is a string containing the port number of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/port) + */ + get port(): string; + /** + * The **`port`** property of the URL interface is a string containing the port number of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/port) + */ + set port(value: string); + /** + * The **`pathname`** property of the URL interface represents a location in a hierarchical structure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/pathname) + */ + get pathname(): string; + /** + * The **`pathname`** property of the URL interface represents a location in a hierarchical structure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/pathname) + */ + set pathname(value: string); + /** + * The **`search`** property of the URL interface is a search string, also called a _query string_, that is a string containing a `'?'` followed by the parameters of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/search) + */ + get search(): string; + /** + * The **`search`** property of the URL interface is a search string, also called a _query string_, that is a string containing a `'?'` followed by the parameters of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/search) + */ + set search(value: string); + /** + * The **`hash`** property of the URL interface is a string containing a `'#'` followed by the fragment identifier of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hash) + */ + get hash(): string; + /** + * The **`hash`** property of the URL interface is a string containing a `'#'` followed by the fragment identifier of the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/hash) + */ + set hash(value: string); + /** + * The **`searchParams`** read-only property of the access to the [MISSING: httpmethod('GET')] decoded query arguments contained in the URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/searchParams) + */ + get searchParams(): URLSearchParams; + /** + * The **`toJSON()`** method of the URL interface returns a string containing a serialized version of the URL, although in practice it seems to have the same effect as ```js-nolint toJSON() ``` None. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/toJSON) + */ + toJSON(): string; + /*function toString() { [native code] }*/ + toString(): string; + /** + * The **`URL.canParse()`** static method of the URL interface returns a boolean indicating whether or not an absolute URL, or a relative URL combined with a base URL, are parsable and valid. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/canParse_static) + */ + static canParse(url: string, base?: string): boolean; + /** + * The **`URL.parse()`** static method of the URL interface returns a newly created URL object representing the URL defined by the parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/parse_static) + */ + static parse(url: string, base?: string): URL | null; + /** + * The **`createObjectURL()`** static method of the URL interface creates a string containing a URL representing the object given in the parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/createObjectURL_static) + */ + static createObjectURL(object: File | Blob): string; + /** + * The **`revokeObjectURL()`** static method of the URL interface releases an existing object URL which was previously created by calling Call this method when you've finished using an object URL to let the browser know not to keep the reference to the file any longer. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URL/revokeObjectURL_static) + */ + static revokeObjectURL(object_url: string): void; +} +/** + * The **`URLSearchParams`** interface defines utility methods to work with the query string of a URL. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams) + */ +declare class URLSearchParams { + constructor(init?: (Iterable> | Record | string)); + /** + * The **`size`** read-only property of the URLSearchParams interface indicates the total number of search parameter entries. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/size) + */ + get size(): number; + /** + * The **`append()`** method of the URLSearchParams interface appends a specified key/value pair as a new search parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/append) + */ + append(name: string, value: string): void; + /** + * The **`delete()`** method of the URLSearchParams interface deletes specified parameters and their associated value(s) from the list of all search parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/delete) + */ + delete(name: string, value?: string): void; + /** + * The **`get()`** method of the URLSearchParams interface returns the first value associated to the given search parameter. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get) + */ + get(name: string): string | null; + /** + * The **`getAll()`** method of the URLSearchParams interface returns all the values associated with a given search parameter as an array. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll) + */ + getAll(name: string): string[]; + /** + * The **`has()`** method of the URLSearchParams interface returns a boolean value that indicates whether the specified parameter is in the search parameters. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/has) + */ + has(name: string, value?: string): boolean; + /** + * The **`set()`** method of the URLSearchParams interface sets the value associated with a given search parameter to the given value. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/set) + */ + set(name: string, value: string): void; + /** + * The **`URLSearchParams.sort()`** method sorts all key/value pairs contained in this object in place and returns `undefined`. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/URLSearchParams/sort) + */ + sort(): void; + /* Returns an array of key, value pairs for every entry in the search params. */ + entries(): IterableIterator<[ + key: string, + value: string + ]>; + /* Returns a list of keys in the search params. */ + keys(): IterableIterator; + /* Returns a list of values in the search params. */ + values(): IterableIterator; + forEach(callback: (this: This, value: string, key: string, parent: URLSearchParams) => void, thisArg?: This): void; + /*function toString() { [native code] }*/ + toString(): string; + [Symbol.iterator](): IterableIterator<[ + key: string, + value: string + ]>; +} +declare class URLPattern { + constructor(input?: (string | URLPatternInit), baseURL?: (string | URLPatternOptions), patternOptions?: URLPatternOptions); + get protocol(): string; + get username(): string; + get password(): string; + get hostname(): string; + get port(): string; + get pathname(): string; + get search(): string; + get hash(): string; + get hasRegExpGroups(): boolean; + test(input?: (string | URLPatternInit), baseURL?: string): boolean; + exec(input?: (string | URLPatternInit), baseURL?: string): URLPatternResult | null; +} +interface URLPatternInit { + protocol?: string; + username?: string; + password?: string; + hostname?: string; + port?: string; + pathname?: string; + search?: string; + hash?: string; + baseURL?: string; +} +interface URLPatternComponentResult { + input: string; + groups: Record; +} +interface URLPatternResult { + inputs: (string | URLPatternInit)[]; + protocol: URLPatternComponentResult; + username: URLPatternComponentResult; + password: URLPatternComponentResult; + hostname: URLPatternComponentResult; + port: URLPatternComponentResult; + pathname: URLPatternComponentResult; + search: URLPatternComponentResult; + hash: URLPatternComponentResult; +} +interface URLPatternOptions { + ignoreCase?: boolean; +} +/** + * A `CloseEvent` is sent to clients using WebSockets when the connection is closed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent) + */ +declare class CloseEvent extends Event { + constructor(type: string, initializer?: CloseEventInit); + /** + * The **`code`** read-only property of the CloseEvent interface returns a WebSocket connection close code indicating the reason the connection was closed. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/code) + */ + readonly code: number; + /** + * The **`reason`** read-only property of the CloseEvent interface returns the WebSocket connection close reason the server gave for closing the connection; that is, a concise human-readable prose explanation for the closure. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/reason) + */ + readonly reason: string; + /** + * The **`wasClean`** read-only property of the CloseEvent interface returns `true` if the connection closed cleanly. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CloseEvent/wasClean) + */ + readonly wasClean: boolean; +} +interface CloseEventInit { + code?: number; + reason?: string; + wasClean?: boolean; +} +type WebSocketEventMap = { + close: CloseEvent; + message: MessageEvent; + open: Event; + error: ErrorEvent; +}; +/** + * The `WebSocket` object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket) + */ +declare var WebSocket: { + prototype: WebSocket; + new (url: string, protocols?: (string[] | string)): WebSocket; + readonly READY_STATE_CONNECTING: number; + readonly CONNECTING: number; + readonly READY_STATE_OPEN: number; + readonly OPEN: number; + readonly READY_STATE_CLOSING: number; + readonly CLOSING: number; + readonly READY_STATE_CLOSED: number; + readonly CLOSED: number; +}; +/** + * The `WebSocket` object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket) + */ +interface WebSocket extends EventTarget { + accept(options?: WebSocketAcceptOptions): void; + /** + * The **`WebSocket.send()`** method enqueues the specified data to be transmitted to the server over the WebSocket connection, increasing the value of `bufferedAmount` by the number of bytes needed to contain the data. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/send) + */ + send(message: (ArrayBuffer | ArrayBufferView) | string): void; + /** + * The **`WebSocket.close()`** method closes the already `CLOSED`, this method does nothing. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/close) + */ + close(code?: number, reason?: string): void; + serializeAttachment(attachment: any): void; + deserializeAttachment(): any | null; + /** + * The **`WebSocket.readyState`** read-only property returns the current state of the WebSocket connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/readyState) + */ + readyState: number; + /** + * The **`WebSocket.url`** read-only property returns the absolute URL of the WebSocket as resolved by the constructor. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/url) + */ + url: string | null; + /** + * The **`WebSocket.protocol`** read-only property returns the name of the sub-protocol the server selected; this will be one of the strings specified in the `protocols` parameter when creating the WebSocket object, or the empty string if no connection is established. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/protocol) + */ + protocol: string | null; + /** + * The **`WebSocket.extensions`** read-only property returns the extensions selected by the server. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/extensions) + */ + extensions: string | null; + /** + * The **`WebSocket.binaryType`** property controls the type of binary data being received over the WebSocket connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/WebSocket/binaryType) + */ + binaryType: "blob" | "arraybuffer"; +} +interface WebSocketAcceptOptions { + /** + * When set to `true`, receiving a server-initiated WebSocket Close frame will not + * automatically send a reciprocal Close frame, leaving the connection in a half-open + * state. This is useful for proxying scenarios where you need to coordinate closing + * both sides independently. Defaults to `false` when the + * `no_web_socket_half_open_by_default` compatibility flag is enabled. + */ + allowHalfOpen?: boolean; +} +declare const WebSocketPair: { + new (): { + 0: WebSocket; + 1: WebSocket; + }; +}; +interface SqlStorage { + exec>(query: string, ...bindings: any[]): SqlStorageCursor; + get databaseSize(): number; + Cursor: typeof SqlStorageCursor; + Statement: typeof SqlStorageStatement; +} +declare abstract class SqlStorageStatement { +} +type SqlStorageValue = ArrayBuffer | string | number | null; +declare abstract class SqlStorageCursor> { + next(): { + done?: false; + value: T; + } | { + done: true; + value?: never; + }; + toArray(): T[]; + one(): T; + raw(): IterableIterator; + columnNames: string[]; + get rowsRead(): number; + get rowsWritten(): number; + [Symbol.iterator](): IterableIterator; +} +interface Socket { + get readable(): ReadableStream; + get writable(): WritableStream; + get closed(): Promise; + get opened(): Promise; + get upgraded(): boolean; + get secureTransport(): "on" | "off" | "starttls"; + close(): Promise; + startTls(options?: TlsOptions): Socket; +} +interface SocketOptions { + secureTransport?: string; + allowHalfOpen: boolean; + highWaterMark?: (number | bigint); +} +interface SocketAddress { + hostname: string; + port: number; +} +interface TlsOptions { + expectedServerHostname?: string; +} +interface SocketInfo { + remoteAddress?: string; + localAddress?: string; +} +/** + * The **`EventSource`** interface is web content's interface to server-sent events. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource) + */ +declare class EventSource extends EventTarget { + constructor(url: string, init?: EventSourceEventSourceInit); + /** + * The **`close()`** method of the EventSource interface closes the connection, if one is made, and sets the ```js-nolint close() ``` None. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close) + */ + close(): void; + /** + * The **`url`** read-only property of the URL of the source. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url) + */ + get url(): string; + /** + * The **`withCredentials`** read-only property of the the `EventSource` object was instantiated with CORS credentials set. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials) + */ + get withCredentials(): boolean; + /** + * The **`readyState`** read-only property of the connection. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState) + */ + get readyState(): number; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */ + get onopen(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */ + set onopen(value: any | null); + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */ + get onmessage(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */ + set onmessage(value: any | null); + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */ + get onerror(): any | null; + /* [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */ + set onerror(value: any | null); + static readonly CONNECTING: number; + static readonly OPEN: number; + static readonly CLOSED: number; + static from(stream: ReadableStream): EventSource; +} +interface EventSourceEventSourceInit { + withCredentials?: boolean; + fetcher?: Fetcher; +} +interface Container { + get running(): boolean; + start(options?: ContainerStartupOptions): void; + monitor(): Promise; + destroy(error?: any): Promise; + signal(signo: number): void; + getTcpPort(port: number): Fetcher; + setInactivityTimeout(durationMs: number | bigint): Promise; + interceptOutboundHttp(addr: string, binding: Fetcher): Promise; + interceptAllOutboundHttp(binding: Fetcher): Promise; + snapshotDirectory(options: ContainerDirectorySnapshotOptions): Promise; + snapshotContainer(options: ContainerSnapshotOptions): Promise; + interceptOutboundHttps(addr: string, binding: Fetcher): Promise; +} +interface ContainerDirectorySnapshot { + id: string; + size: number; + dir: string; + name?: string; +} +interface ContainerDirectorySnapshotOptions { + dir: string; + name?: string; +} +interface ContainerDirectorySnapshotRestoreParams { + snapshot: ContainerDirectorySnapshot; + mountPoint?: string; +} +interface ContainerSnapshot { + id: string; + size: number; + name?: string; +} +interface ContainerSnapshotOptions { + name?: string; +} +interface ContainerStartupOptions { + entrypoint?: string[]; + enableInternet: boolean; + env?: Record; + labels?: Record; + directorySnapshots?: ContainerDirectorySnapshotRestoreParams[]; + containerSnapshot?: ContainerSnapshot; +} +/** + * The **`MessagePort`** interface of the Channel Messaging API represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort) + */ +declare abstract class MessagePort extends EventTarget { + /** + * The **`postMessage()`** method of the transfers ownership of objects to other browsing contexts. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/postMessage) + */ + postMessage(data?: any, options?: (any[] | MessagePortPostMessageOptions)): void; + /** + * The **`close()`** method of the MessagePort interface disconnects the port, so it is no longer active. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/close) + */ + close(): void; + /** + * The **`start()`** method of the MessagePort interface starts the sending of messages queued on the port. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessagePort/start) + */ + start(): void; + get onmessage(): any | null; + set onmessage(value: any | null); +} +/** + * The **`MessageChannel`** interface of the Channel Messaging API allows us to create a new message channel and send data through it via its two MessagePort properties. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel) + */ +declare class MessageChannel { + constructor(); + /** + * The **`port1`** read-only property of the the port attached to the context that originated the channel. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port1) + */ + readonly port1: MessagePort; + /** + * The **`port2`** read-only property of the the port attached to the context at the other end of the channel, which the message is initially sent to. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/MessageChannel/port2) + */ + readonly port2: MessagePort; +} +interface MessagePortPostMessageOptions { + transfer?: any[]; +} +type LoopbackForExport Rpc.EntrypointBranded) | ExportedHandler | undefined = undefined> = T extends new (...args: any[]) => Rpc.WorkerEntrypointBranded ? LoopbackServiceStub> : T extends new (...args: any[]) => Rpc.DurableObjectBranded ? LoopbackDurableObjectClass> : T extends ExportedHandler ? LoopbackServiceStub : undefined; +type LoopbackServiceStub = Fetcher & (T extends CloudflareWorkersModule.WorkerEntrypoint ? (opts: { + props?: Props; +}) => Fetcher : (opts: { + props?: any; +}) => Fetcher); +type LoopbackDurableObjectClass = DurableObjectClass & (T extends CloudflareWorkersModule.DurableObject ? (opts: { + props?: Props; +}) => DurableObjectClass : (opts: { + props?: any; +}) => DurableObjectClass); +interface LoopbackDurableObjectNamespace extends DurableObjectNamespace { +} +interface LoopbackColoLocalActorNamespace extends ColoLocalActorNamespace { +} +interface SyncKvStorage { + get(key: string): T | undefined; + list(options?: SyncKvListOptions): Iterable<[ + string, + T + ]>; + put(key: string, value: T): void; + delete(key: string): boolean; +} +interface SyncKvListOptions { + start?: string; + startAfter?: string; + end?: string; + prefix?: string; + reverse?: boolean; + limit?: number; +} +interface WorkerStub { + getEntrypoint(name?: string, options?: WorkerStubEntrypointOptions): Fetcher; + getDurableObjectClass(name?: string, options?: WorkerStubEntrypointOptions): DurableObjectClass; +} +interface WorkerStubEntrypointOptions { + props?: any; + limits?: workerdResourceLimits; +} +interface WorkerLoader { + get(name: string | null, getCode: () => WorkerLoaderWorkerCode | Promise): WorkerStub; + load(code: WorkerLoaderWorkerCode): WorkerStub; +} +interface WorkerLoaderModule { + js?: string; + cjs?: string; + text?: string; + data?: ArrayBuffer; + json?: any; + py?: string; + wasm?: ArrayBuffer; +} +interface WorkerLoaderWorkerCode { + compatibilityDate: string; + compatibilityFlags?: string[]; + allowExperimental?: boolean; + limits?: workerdResourceLimits; + mainModule: string; + modules: Record; + env?: any; + globalOutbound?: (Fetcher | null); + tails?: Fetcher[]; + streamingTails?: Fetcher[]; +} +interface workerdResourceLimits { + cpuMs?: number; + subRequests?: number; +} +/** +* The Workers runtime supports a subset of the Performance API, used to measure timing and performance, +* as well as timing of subrequests and other operations. +* +* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/) +*/ +declare abstract class Performance { + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/#performancetimeorigin) */ + get timeOrigin(): number; + /* [Cloudflare Docs Reference](https://developers.cloudflare.com/workers/runtime-apis/performance/#performancenow) */ + now(): number; + /** + * The **`toJSON()`** method of the Performance interface is a Serialization; it returns a JSON representation of the Performance object. + * + * [MDN Reference](https://developer.mozilla.org/docs/Web/API/Performance/toJSON) + */ + toJSON(): object; +} +interface Tracing { + enterSpan(name: string, callback: (span: Span, ...args: A) => T, ...args: A): T; + Span: typeof Span; +} +declare abstract class Span { + get isTraced(): boolean; + setAttribute(key: string, value?: (boolean | number | string)): void; +} +// ============================================================================ +// Agent Memory +// +// Public type surface for user Workers binding to an Agent Memory namespace. +// ============================================================================ +/** Memory type — every memory is classified into exactly one. */ +type AgentMemoryMemoryType = "fact" | "event" | "instruction" | "task"; +/** Search intensity for recall. */ +type AgentMemoryThinkingLevel = "low" | "medium" | "high"; +/** Response verbosity for recall. */ +type AgentMemoryResponseLength = "short" | "medium" | "long"; +/** A conversation message passed to ingest(). */ +interface AgentMemoryMessage { + role: "system" | "user" | "assistant"; + content: string; + /** Optional message timestamp. */ + timestamp?: Date; +} +/** Raw memory content passed to remember(). */ +interface AgentMemoryIncomingMemory { + /** Raw memory content. The service classifies and summarizes automatically. */ + content: string; + /** Optional session identifier to associate with this memory. */ + sessionId?: string | null | undefined; +} +/** A stored memory returned from remember(), get(), and delete(). */ +interface AgentMemoryMemory { + /** Memory ID. */ + id: string; + /** Memory type. */ + type: AgentMemoryMemoryType; + /** Text summary. */ + summary: string; + /** Memory text. */ + content: string; + /** Session that created this memory. */ + sessionId: string | null; + /** Memory creation time. */ + createdAt: Date; + /** Memory last-update time. */ + updatedAt: Date; +} +/** Single entry in a list() response. Same shape as Memory minus full content. */ +type AgentMemoryMemoryListEntry = Omit; +/** A scored memory candidate in a recall result. */ +interface AgentMemoryScoredCandidate { + /** Candidate ID. */ + id: string; + /** Text summary. */ + summary: string; + /** Session that created this candidate, when known. */ + sessionId: string | null; + /** Relevance score (higher is better). Comparable only within a single query. */ + score: number; +} +/** Options for the ingest() method. */ +interface AgentMemoryIngestOptions { + /** Session identifier to associate with memories created during ingestion. */ + sessionId?: string | null | undefined; +} +/** Options for the getSummary() method. */ +interface AgentMemoryGetSummaryOptions { + /** Session identifier to retrieve session summary for. */ + sessionId?: string | null | undefined; +} +/** Response from the getSummary() method. */ +interface AgentMemoryGetSummaryResponse { + /** Markdown summary. */ + summary: string; +} +/** + * Options for the recall() method. + * + * `referenceDate` accepts a Date object, an ISO-8601 date string + * (YYYY-MM-DD), or a full ISO-8601 datetime string. When provided, this + * date is used as "today" for resolving relative time references + * ("how many days ago", "last week") instead of the server's wall-clock time. + */ +interface AgentMemoryRecallOptions { + /** Recall intensity: "low" (default), "medium", or "high". */ + thinkingLevel?: AgentMemoryThinkingLevel; + /** Response verbosity: "short", "medium" (default), or "long". */ + responseLength?: AgentMemoryResponseLength; + /** Temporal anchor for date arithmetic. */ + referenceDate?: Date | string; +} +/** Response from the recall() method. */ +interface AgentMemoryRecallResult { + /** Number of memories retrieved. */ + count: number; + /** LLM-generated answer synthesizing the matching memories. */ + answer: string; + /** Matching memories ranked by relevance. */ + candidates: AgentMemoryScoredCandidate[]; +} +/** + * Options for the list() method. + * + * `cursor` is the opaque continuation token returned by the previous page; + * pass it back unchanged to fetch the next page. `sessionId` and `type` + * are exact-match filters; combining them is allowed. + */ +interface AgentMemoryListMemoriesOptions { + /** Maximum number of memories to return. Default 20, max 500. */ + limit?: number; + /** Opaque cursor from a previous page. */ + cursor?: string; + /** Exact-match session filter. */ + sessionId?: string; + /** Exact-match memory-type filter. */ + type?: AgentMemoryMemoryType; +} +/** Response from the list() method. */ +interface AgentMemoryListMemoriesResult { + memories: AgentMemoryMemoryListEntry[]; + /** Continuation cursor; absent when this page exhausted the result set. */ + cursor?: string; +} +/** + * A single Agent Memory profile, scoped to a profile name. + * + * Returned by {@link AgentMemoryNamespace.getProfile}. + */ +declare abstract class AgentMemoryProfile { + /** + * Retrieve a memory by ID. + * + * @param memoryId - ULID of the memory to retrieve. + * @throws if the memory does not exist. + */ + get(memoryId: string): Promise; + /** + * Delete a memory by ID. + * + * Removes the memory and any source messages linked by the memory's + * source message IDs. + * + * @param memoryId - ULID of the memory to delete. + * @throws if the memory does not exist. + */ + delete(memoryId: string): Promise; + /** + * Store a memory in this profile. The content is automatically classified, + * summarized, and indexed. + * + * @param memory - Raw memory content to persist. + */ + remember(memory: AgentMemoryIncomingMemory): Promise; + /** + * Extract memories from a conversation. + * + * @param messages - Conversation messages to extract memories from. + * @param options - Optional ingest options. + */ + ingest(messages: Iterable, options?: AgentMemoryIngestOptions): Promise; + /** + * Get a profile summary. + * + * @param options - Optional getSummary options. + */ + getSummary(options?: AgentMemoryGetSummaryOptions): Promise; + /** + * Recall memories in this profile. + * + * @param query - Recall query matched against memory content and keywords. + * @param options - Optional recall parameters. + * @returns Matching memories with relevance scores and a synthesized answer. + */ + recall(query: string, options?: AgentMemoryRecallOptions): Promise; + /** + * List active memories in this profile. + * + * Returns a paginated, filterable view of stored memories. Superseded + * versions are excluded. Use the returned `cursor` (when present) to + * fetch the next page. + * + * @param options - Optional pagination and filter options. + */ + list(options?: AgentMemoryListMemoriesOptions): Promise; + /** + * Soft-delete every memory and message in this profile that is tagged + * with `sessionId`. + * + * Idempotent: deleting a sessionId that has no rows is a no-op. + * + * @param sessionId - Session to delete. + */ + deleteSession(sessionId: string): Promise; +} +/** + * Namespace-level Agent Memory binding. + * + * Used as the type of an `env.MEMORY`-style binding backed by the Agent + * Memory product. + * + * @example + * ```ts + * export default { + * async fetch(_request: Request, env: Env): Promise { + * const profile = await env.MEMORY.getProfile("wrangler-e2e"); + * const summary = await profile.getSummary(); + * return Response.json(summary); + * }, + * }; + * ``` + */ +declare abstract class AgentMemoryNamespace { + /** + * Get a memory profile by name. Profiles are isolated by namespace and + * addressed by a compound key (namespaceId:profileName). + * + * @param profileName - Profile name (validated against naming rules). + * @returns RPC target for interacting with the profile. + */ + getProfile(profileName: string): Promise; + /** + * Soft-delete a profile and schedule deferred purge. Marks all + * memories and messages as deleted. + * + * @param profileName - Name of the profile to delete. + */ + deleteProfile(profileName: string): Promise; +} +// ============ AI Search Error Interfaces ============ +interface AiSearchInternalError extends Error { +} +interface AiSearchNotFoundError extends Error { +} +// ============ AI Search Common Types ============ +/** A single message in a conversation-style search or chat request. */ +type AiSearchMessage = { + role: 'system' | 'developer' | 'user' | 'assistant' | 'tool'; + content: string | null; +}; +/** + * Common shape for `ai_search_options` used by both single-instance and multi-instance requests. + * Contains retrieval, query rewrite, reranking, and cache sub-options. + */ +type AiSearchOptions = { + retrieval?: { + /** Which retrieval backend to use. Defaults to the instance's configured index_method. */ + retrieval_type?: 'vector' | 'keyword' | 'hybrid'; + /** Fusion method for combining vector + keyword results. */ + fusion_method?: 'max' | 'rrf'; + /** How keyword terms are combined: "and" = all terms must match, "or" = any term matches. */ + keyword_match_mode?: 'and' | 'or'; + /** Minimum similarity score (0-1) for a result to be included. Default 0.4. */ + match_threshold?: number; + /** Maximum number of results to return (1-50). Default 10. */ + max_num_results?: number; + /** Vectorize metadata filters applied to the search. */ + filters?: VectorizeVectorMetadataFilter; + /** Number of surrounding chunks to include for context (0-3). Default 0. */ + context_expansion?: number; + /** If true, return only item metadata without chunk text. */ + metadata_only?: boolean; + /** If true (default), return empty results on retrieval failure instead of throwing. */ + return_on_failure?: boolean; + /** Boost results by metadata field values. Max 3 entries. */ + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + [key: string]: unknown; + }; + query_rewrite?: { + enabled?: boolean; + model?: string; + rewrite_prompt?: string; + [key: string]: unknown; + }; + reranking?: { + enabled?: boolean; + model?: string; + /** Match threshold (0-1, default 0.4) */ + match_threshold?: number; + [key: string]: unknown; + }; + cache?: { + enabled?: boolean; + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + }; + [key: string]: unknown; +}; +// ============ AI Search Request Types ============ +/** + * Request body for single-instance search. + * Exactly one of `query` or `messages` must be provided. + */ +type AiSearchSearchRequest = { + /** Simple query string. */ + query: string; + messages?: never; + ai_search_options?: AiSearchOptions; +} | { + query?: never; + /** Conversation-style input. At least one user message with non-empty content is required. */ + messages: AiSearchMessage[]; + ai_search_options?: AiSearchOptions; +}; +type AiSearchChatCompletionsRequest = { + messages: AiSearchMessage[]; + model?: string; + stream?: boolean; + ai_search_options?: AiSearchOptions; + [key: string]: unknown; +}; +// ============ AI Search Multi-Instance Types (Namespace-Scoped) ============ +/** `ai_search_options` shape for multi-instance requests — requires `instance_ids`. */ +type AiSearchMultiSearchOptions = AiSearchOptions & { + /** Instance IDs to search across (1-10). */ + instance_ids: string[]; +}; +/** + * Request for searching across multiple instances within a namespace. + * `ai_search_options` is required and must include `instance_ids`. + * Exactly one of `query` or `messages` must be provided. + */ +type AiSearchMultiSearchRequest = { + /** Simple query string. */ + query: string; + messages?: never; + ai_search_options: AiSearchMultiSearchOptions; +} | { + query?: never; + /** Conversation-style input. */ + messages: AiSearchMessage[]; + ai_search_options: AiSearchMultiSearchOptions; +}; +/** A search result chunk tagged with the instance it originated from. */ +type AiSearchMultiSearchChunk = AiSearchSearchResponse['chunks'][number] & { + instance_id: string; +}; +/** Describes a per-instance error during a multi-instance operation. */ +type AiSearchMultiSearchError = { + instance_id: string; + message: string; +}; +/** Response from a multi-instance search, with chunks tagged by instance and optional partial-failure errors. */ +type AiSearchMultiSearchResponse = { + search_query: string; + chunks: AiSearchMultiSearchChunk[]; + errors?: AiSearchMultiSearchError[]; +}; +/** Request for chat completions across multiple instances within a namespace. `ai_search_options` is required and must include `instance_ids`. */ +type AiSearchMultiChatCompletionsRequest = Omit & { + ai_search_options: AiSearchMultiSearchOptions; +}; +/** Response from multi-instance chat completions, with chunks tagged by instance and optional partial-failure errors. */ +type AiSearchMultiChatCompletionsResponse = Omit & { + chunks: AiSearchMultiSearchChunk[]; + errors?: AiSearchMultiSearchError[]; +}; +// ============ AI Search Response Types ============ +type AiSearchSearchResponse = { + search_query: string; + chunks: Array<{ + id: string; + type: string; + /** Match score (0-1) */ + score: number; + text: string; + item: { + timestamp?: number; + key: string; + metadata?: Record; + }; + scoring_details?: { + /** Keyword match score (0-1) */ + keyword_score?: number; + /** Vector similarity score (0-1) */ + vector_score?: number; + /** Keyword rank position */ + keyword_rank?: number; + /** Vector rank position */ + vector_rank?: number; + /** Reranking model score */ + reranking_score?: number; + /** Fusion method used to combine results */ + fusion_method?: 'rrf' | 'max'; + [key: string]: unknown; + }; + }>; +}; +type AiSearchChatCompletionsResponse = { + id?: string; + object?: string; + model?: string; + choices: Array<{ + index?: number; + message: { + role: 'system' | 'developer' | 'user' | 'assistant' | 'tool'; + content: string | null; + [key: string]: unknown; + }; + [key: string]: unknown; + }>; + chunks: AiSearchSearchResponse['chunks']; + [key: string]: unknown; +}; +type AiSearchStatsResponse = { + queued?: number; + running?: number; + completed?: number; + error?: number; + skipped?: number; + outdated?: number; + last_activity?: string; + /** Storage engine statistics. */ + engine?: { + vectorize?: { + vectorsCount: number; + dimensions: number; + }; + r2?: { + payloadSizeBytes: number; + metadataSizeBytes: number; + objectCount: number; + }; + }; +}; +// ============ AI Search Instance Info Types ============ +type AiSearchInstanceInfo = { + id: string; + type?: 'r2' | 'web-crawler' | string; + source?: string; + source_params?: unknown; + paused?: boolean; + status?: string; + namespace?: string; + created_at?: string; + modified_at?: string; + token_id?: string; + ai_gateway_id?: string; + rewrite_query?: boolean; + reranking?: boolean; + embedding_model?: string; + ai_search_model?: string; + rewrite_model?: string; + reranking_model?: string; + /** @deprecated Use index_method instead. */ + hybrid_search_enabled?: boolean; + /** Controls which storage backends are active. */ + index_method?: { + vector?: boolean; + keyword?: boolean; + }; + /** Fusion method for combining vector and keyword results. */ + fusion_method?: 'max' | 'rrf'; + indexing_options?: { + keyword_tokenizer?: 'porter' | 'trigram'; + } | null; + retrieval_options?: { + keyword_match_mode?: 'and' | 'or'; + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + } | null; + chunk?: boolean; + chunk_size?: number; + chunk_overlap?: number; + score_threshold?: number; + max_num_results?: number; + cache?: boolean; + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + custom_metadata?: Array<{ + field_name: string; + data_type: 'text' | 'number' | 'boolean' | 'datetime'; + }>; + /** Sync interval in seconds. */ + sync_interval?: 3600 | 7200 | 14400 | 21600 | 43200 | 86400; + metadata?: Record; + [key: string]: unknown; +}; +/** Pagination, search, and ordering parameters for listing instances within a namespace. */ +type AiSearchListInstancesParams = { + page?: number; + per_page?: number; + /** Search instances by ID. */ + search?: string; + /** Field to sort by. */ + order_by?: 'created_at'; + /** Sort direction. */ + order_by_direction?: 'asc' | 'desc'; +}; +type AiSearchListResponse = { + result: AiSearchInstanceInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Config Types ============ +type AiSearchConfig = { + /** Instance ID (1-32 chars, pattern: ^[a-z0-9_]+(?:-[a-z0-9_]+)*$) */ + id: string; + /** Instance type. Omit to create with built-in storage. */ + type?: 'r2' | 'web-crawler' | string; + /** Source URL (required for web-crawler type). */ + source?: string; + source_params?: unknown; + /** Token ID (UUID format) */ + token_id?: string; + ai_gateway_id?: string; + /** Enable query rewriting (default false) */ + rewrite_query?: boolean; + /** Enable reranking (default false) */ + reranking?: boolean; + embedding_model?: string; + ai_search_model?: string; + rewrite_model?: string; + reranking_model?: string; + /** @deprecated Use index_method instead. */ + hybrid_search_enabled?: boolean; + /** Controls which storage backends are used during indexing. Defaults to vector-only. */ + index_method?: { + vector?: boolean; + keyword?: boolean; + }; + /** Fusion method for combining vector and keyword results. "rrf" = reciprocal rank fusion (default), "max" = maximum score. */ + fusion_method?: 'max' | 'rrf'; + indexing_options?: { + keyword_tokenizer?: 'porter' | 'trigram'; + } | null; + retrieval_options?: { + keyword_match_mode?: 'and' | 'or'; + boost_by?: Array<{ + field: string; + direction?: 'asc' | 'desc' | 'exists' | 'not_exists'; + }>; + } | null; + chunk?: boolean; + chunk_size?: number; + chunk_overlap?: number; + /** Minimum similarity score (0-1) for a result to be included. */ + score_threshold?: number; + max_num_results?: number; + cache?: boolean; + /** Similarity threshold for cache hits. Stricter = fewer cache hits but higher relevance. */ + cache_threshold?: 'super_strict_match' | 'close_enough' | 'flexible_friend' | 'anything_goes'; + custom_metadata?: Array<{ + field_name: string; + data_type: 'text' | 'number' | 'boolean' | 'datetime'; + }>; + namespace?: string; + /** Sync interval in seconds. 3600=1h, 7200=2h, 14400=4h, 21600=6h, 43200=12h, 86400=24h. */ + sync_interval?: 3600 | 7200 | 14400 | 21600 | 43200 | 86400; + metadata?: Record; + [key: string]: unknown; +}; +// ============ AI Search Item Types ============ +type AiSearchItemInfo = { + id: string; + key: string; + status: 'completed' | 'error' | 'skipped' | 'queued' | 'running' | 'outdated'; + next_action?: 'INDEX' | 'DELETE' | null; + error?: string; + checksum?: string; + namespace?: string; + chunks_count?: number | null; + file_size?: number | null; + source_id?: string | null; + last_seen_at?: string; + created_at?: string; + metadata?: Record; + [key: string]: unknown; +}; +type AiSearchItemContentResult = { + body: ReadableStream; + contentType: string; + filename: string; + size: number; +}; +type AiSearchUploadItemOptions = { + metadata?: Record; +}; +type AiSearchListItemsParams = { + page?: number; + per_page?: number; + /** Search items by key name. */ + search?: string; + /** Sort order for results. */ + sort_by?: 'status' | 'modified_at'; + /** Filter items by processing status. */ + status?: 'queued' | 'running' | 'completed' | 'error' | 'skipped' | 'outdated'; + /** Filter items by source (e.g. "builtin" or "web-crawler:https://example.com"). */ + source?: string; + /** JSON-encoded Vectorize filter for metadata filtering. */ + metadata_filter?: string; +}; +type AiSearchListItemsResponse = { + result: AiSearchItemInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Item Logs Types ============ +type AiSearchItemLogsParams = { + /** Maximum number of log entries to return (1-100, default 50). */ + limit?: number; + /** Opaque cursor for pagination. Pass the `cursor` value from a previous response. */ + cursor?: string; +}; +type AiSearchItemLog = { + timestamp: string; + action: string; + message: string; + fileKey?: string; + chunkCount?: number; + processingTimeMs?: number; + errorType?: string; +}; +/** Paginated response for item processing logs (cursor-based). */ +type AiSearchItemLogsResponse = { + result: AiSearchItemLog[]; + result_info: { + count: number; + per_page: number; + cursor: string | null; + truncated: boolean; + }; +}; +// ============ AI Search Item Chunks Types ============ +type AiSearchItemChunksParams = { + /** Maximum number of chunks to return (1-100, default 20). */ + limit?: number; + /** Offset into the chunks list (default 0). */ + offset?: number; +}; +/** A single indexed chunk belonging to an item, including its text content and byte range. */ +type AiSearchItemChunk = { + id: string; + text: string; + start_byte: number; + end_byte: number; + item?: { + timestamp?: number; + key: string; + metadata?: Record; + }; +}; +/** Paginated response for item chunks (offset-based). */ +type AiSearchItemChunksResponse = { + result: AiSearchItemChunk[]; + result_info: { + count: number; + total: number; + limit: number; + offset: number; + }; +}; +// ============ AI Search Job Types ============ +type AiSearchJobInfo = { + id: string; + source: 'user' | 'schedule'; + description?: string; + last_seen_at?: string; + started_at?: string; + ended_at?: string; + end_reason?: string; +}; +type AiSearchJobLog = { + id: number; + message: string; + message_type: number; + created_at: number; +}; +type AiSearchCreateJobParams = { + description?: string; +}; +type AiSearchListJobsParams = { + page?: number; + per_page?: number; +}; +type AiSearchListJobsResponse = { + result: AiSearchJobInfo[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +type AiSearchJobLogsParams = { + page?: number; + per_page?: number; +}; +type AiSearchJobLogsResponse = { + result: AiSearchJobLog[]; + result_info?: { + count: number; + page: number; + per_page: number; + total_count: number; + }; +}; +// ============ AI Search Sub-Service Classes ============ +/** + * Single item service for an AI Search instance. + * Provides info, download, sync, logs, and chunks operations on a specific item. + */ +declare abstract class AiSearchItem { + /** Get metadata about this item. */ + info(): Promise; + /** + * Download the item's content. + * @returns Object with body stream, content type, filename, and size. + */ + download(): Promise; + /** + * Trigger re-indexing of this item. + * @returns The updated item info. + */ + sync(): Promise; + /** + * Retrieve processing logs for this item (cursor-based pagination). + * @param params Optional pagination parameters (limit, cursor). + * @returns Paginated log entries for this item. + */ + logs(params?: AiSearchItemLogsParams): Promise; + /** + * List indexed chunks for this item (offset-based pagination). + * @param params Optional pagination parameters (limit, offset). + * @returns Paginated chunk entries for this item. + */ + chunks(params?: AiSearchItemChunksParams): Promise; +} +/** + * Items collection service for an AI Search instance. + * Provides list, upload, and access to individual items. + */ +declare abstract class AiSearchItems { + /** List items in this instance. */ + list(params?: AiSearchListItemsParams): Promise; + /** + * Upload a file as an item. Behaves as an upsert: if an item with the same + * filename already exists, it is overwritten and re-indexed. + * @param name Filename for the uploaded item. + * @param content File content as a ReadableStream, Blob, or string. + * @param options Optional metadata to attach to the item. + * @returns The created item info. + */ + upload(name: string, content: ReadableStream | Blob | string, options?: AiSearchUploadItemOptions): Promise; + /** + * Upload a file and poll until processing completes. + * Behaves as an upsert: if an item with the same filename already exists, + * it is overwritten and re-indexed. + * @param name Filename for the uploaded item. + * @param content File content as a ReadableStream, Blob, or string. + * @param options Optional metadata and polling configuration. + * @returns The item info after processing completes (or timeout). + */ + uploadAndPoll(name: string, content: ReadableStream | Blob | string, options?: AiSearchUploadItemOptions & { + /** Polling interval in milliseconds (default 1000). */ + pollIntervalMs?: number; + /** Maximum time to wait in milliseconds (default 30000). */ + timeoutMs?: number; + }): Promise; + /** + * Get an item by ID. + * @param itemId The item identifier. + * @returns Item service for info, download, sync, logs, and chunks operations. + */ + get(itemId: string): AiSearchItem; + /** + * Delete an item from the instance. + * @param itemId The item identifier. + */ + delete(itemId: string): Promise; +} +/** + * Single job service for an AI Search instance. + * Provides info, logs, and cancel operations for a specific job. + */ +declare abstract class AiSearchJob { + /** Get metadata about this job. */ + info(): Promise; + /** Get logs for this job. */ + logs(params?: AiSearchJobLogsParams): Promise; + /** + * Cancel a running job. + * @returns The updated job info. + * @throws AiSearchNotFoundError if the job does not exist. + */ + cancel(): Promise; +} +/** + * Jobs collection service for an AI Search instance. + * Provides list, create, and access to individual jobs. + */ +declare abstract class AiSearchJobs { + /** List jobs for this instance. */ + list(params?: AiSearchListJobsParams): Promise; + /** + * Create a new indexing job. + * @param params Optional job parameters. + * @returns The created job info. + */ + create(params?: AiSearchCreateJobParams): Promise; + /** + * Get a job by ID. + * @param jobId The job identifier. + * @returns Job service for info, logs, and cancel operations. + */ + get(jobId: string): AiSearchJob; +} +// ============ AI Search Binding Classes ============ +/** + * Instance-level AI Search service. + * + * Used as: + * - The return type of `AiSearchNamespace.get(name)` (namespace binding) + * - The type of `env.BLOG_SEARCH` (single instance binding via `ai_search`) + * + * Provides search, chat, update, stats, items, and jobs operations. + * + * @example + * ```ts + * // Via namespace binding + * const instance = env.AI_SEARCH.get("blog"); + * const results = await instance.search({ + * query: "How does caching work?", + * }); + * + * // Via single instance binding + * const results = await env.BLOG_SEARCH.search({ + * messages: [{ role: "user", content: "How does caching work?" }], + * }); + * ``` + */ +declare abstract class AiSearchInstance { + /** + * Search the AI Search instance for relevant chunks. + * @param params Search request with query or messages and optional AI search options. + * @returns Search response with matching chunks and search query. + */ + search(params: AiSearchSearchRequest): Promise; + /** + * Generate chat completions with AI Search context (streaming). + * @param params Chat completions request with stream: true. + * @returns ReadableStream of server-sent events. + */ + chatCompletions(params: AiSearchChatCompletionsRequest & { + stream: true; + }): Promise; + /** + * Generate chat completions with AI Search context. + * @param params Chat completions request. + * @returns Chat completion response with choices and RAG chunks. + */ + chatCompletions(params: AiSearchChatCompletionsRequest): Promise; + /** + * Update the instance configuration. + * @param config Partial configuration to update. + * @returns Updated instance info. + */ + update(config: Partial): Promise; + /** Get metadata about this instance. */ + info(): Promise; + /** + * Get instance statistics (item count, indexing status, etc.). + * @returns Statistics with counts per status, last activity time, and engine details. + */ + stats(): Promise; + /** Items collection — list, upload, and manage items in this instance. */ + get items(): AiSearchItems; + /** Jobs collection — list, create, and inspect indexing jobs. */ + get jobs(): AiSearchJobs; +} +/** + * Namespace-level AI Search service. + * + * Used as the type of `env.AI_SEARCH` (namespace binding via `ai_search_namespaces`). + * Scoped to a single namespace. Provides dynamic instance access, creation, deletion, + * and multi-instance search/chat operations. + * + * @example + * ```ts + * // Access an instance within the namespace + * const blog = env.AI_SEARCH.get("blog"); + * const results = await blog.search({ query: "How does caching work?" }); + * + * // List all instances in the namespace + * const instances = await env.AI_SEARCH.list(); + * + * // Create a new instance with built-in storage + * const tenant = await env.AI_SEARCH.create({ id: "tenant-123" }); + * + * // Upload items into the instance + * await tenant.items.upload("doc.pdf", fileContent); + * + * // Search across multiple instances + * const multi = await env.AI_SEARCH.search({ + * query: "caching", + * ai_search_options: { instance_ids: ["blog", "docs"] }, + * }); + * + * // Delete an instance + * await env.AI_SEARCH.delete("tenant-123"); + * ``` + */ +declare abstract class AiSearchNamespace { + /** + * Get an instance by name within the bound namespace. + * @param name Instance name. + * @returns Instance service for search, chat, update, stats, items, and jobs. + */ + get(name: string): AiSearchInstance; + /** + * List instances in the bound namespace. + * @param params Optional pagination, search, and ordering parameters. + * @returns Array of instance metadata with pagination info. + */ + list(params?: AiSearchListInstancesParams): Promise; + /** + * Create a new instance within the bound namespace. + * @param config Instance configuration. Only `id` is required — omit `type` and `source` to create with built-in storage. + * @returns Instance service for the newly created instance. + * + * @example + * ```ts + * // Create with built-in storage (upload items manually) + * const instance = await env.AI_SEARCH.create({ id: "my-search" }); + * + * // Create with web crawler source + * const instance = await env.AI_SEARCH.create({ + * id: "docs-search", + * type: "web-crawler", + * source: "https://developers.cloudflare.com", + * }); + * ``` + */ + create(config: AiSearchConfig): Promise; + /** + * Delete an instance from the bound namespace. + * @param name Instance name to delete. + */ + delete(name: string): Promise; + /** + * Search across multiple instances within the bound namespace. + * Fans out to the specified instance_ids and merges results. + * @param params Search request with required `ai_search_options.instance_ids`. + * @returns Search response with chunks tagged by instance_id and optional partial-failure errors. + */ + search(params: AiSearchMultiSearchRequest): Promise; + /** + * Generate chat completions across multiple instances within the bound namespace (streaming). + * Fans out to the specified instance_ids, merges context, and generates a response. + * @param params Chat completions request with stream: true and required `ai_search_options.instance_ids`. + * @returns ReadableStream of server-sent events. + */ + chatCompletions(params: AiSearchMultiChatCompletionsRequest & { + stream: true; + }): Promise; + /** + * Generate chat completions across multiple instances within the bound namespace. + * Fans out to the specified instance_ids, merges context, and generates a response. + * @param params Chat completions request with required `ai_search_options.instance_ids`. + * @returns Chat completion response with choices, chunks tagged by instance_id, and optional partial-failure errors. + */ + chatCompletions(params: AiSearchMultiChatCompletionsRequest): Promise; +} +type AiImageClassificationInput = { + image: number[]; +}; +type AiImageClassificationOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiImageClassification { + inputs: AiImageClassificationInput; + postProcessedOutputs: AiImageClassificationOutput; +} +type AiImageToTextInput = { + image: number[]; + prompt?: string; + max_tokens?: number; + temperature?: number; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + raw?: boolean; + messages?: RoleScopedChatInput[]; +}; +type AiImageToTextOutput = { + description: string; +}; +declare abstract class BaseAiImageToText { + inputs: AiImageToTextInput; + postProcessedOutputs: AiImageToTextOutput; +} +type AiImageTextToTextInput = { + image: string; + prompt?: string; + max_tokens?: number; + temperature?: number; + ignore_eos?: boolean; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + raw?: boolean; + messages?: RoleScopedChatInput[]; +}; +type AiImageTextToTextOutput = { + description: string; +}; +declare abstract class BaseAiImageTextToText { + inputs: AiImageTextToTextInput; + postProcessedOutputs: AiImageTextToTextOutput; +} +type AiMultimodalEmbeddingsInput = { + image: string; + text: string[]; +}; +type AiIMultimodalEmbeddingsOutput = { + data: number[][]; + shape: number[]; +}; +declare abstract class BaseAiMultimodalEmbeddings { + inputs: AiImageTextToTextInput; + postProcessedOutputs: AiImageTextToTextOutput; +} +type AiObjectDetectionInput = { + image: number[]; +}; +type AiObjectDetectionOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiObjectDetection { + inputs: AiObjectDetectionInput; + postProcessedOutputs: AiObjectDetectionOutput; +} +type AiSentenceSimilarityInput = { + source: string; + sentences: string[]; +}; +type AiSentenceSimilarityOutput = number[]; +declare abstract class BaseAiSentenceSimilarity { + inputs: AiSentenceSimilarityInput; + postProcessedOutputs: AiSentenceSimilarityOutput; +} +type AiAutomaticSpeechRecognitionInput = { + audio: number[]; +}; +type AiAutomaticSpeechRecognitionOutput = { + text?: string; + words?: { + word: string; + start: number; + end: number; + }[]; + vtt?: string; +}; +declare abstract class BaseAiAutomaticSpeechRecognition { + inputs: AiAutomaticSpeechRecognitionInput; + postProcessedOutputs: AiAutomaticSpeechRecognitionOutput; +} +type AiSummarizationInput = { + input_text: string; + max_length?: number; +}; +type AiSummarizationOutput = { + summary: string; +}; +declare abstract class BaseAiSummarization { + inputs: AiSummarizationInput; + postProcessedOutputs: AiSummarizationOutput; +} +type AiTextClassificationInput = { + text: string; +}; +type AiTextClassificationOutput = { + score?: number; + label?: string; +}[]; +declare abstract class BaseAiTextClassification { + inputs: AiTextClassificationInput; + postProcessedOutputs: AiTextClassificationOutput; +} +type AiTextEmbeddingsInput = { + text: string | string[]; +}; +type AiTextEmbeddingsOutput = { + shape: number[]; + data: number[][]; +}; +declare abstract class BaseAiTextEmbeddings { + inputs: AiTextEmbeddingsInput; + postProcessedOutputs: AiTextEmbeddingsOutput; +} +type RoleScopedChatInput = { + role: "user" | "assistant" | "system" | "tool" | (string & NonNullable); + content: string; + name?: string; +}; +type AiTextGenerationToolLegacyInput = { + name: string; + description: string; + parameters?: { + type: "object" | (string & NonNullable); + properties: { + [key: string]: { + type: string; + description?: string; + }; + }; + required: string[]; + }; +}; +type AiTextGenerationToolInput = { + type: "function" | (string & NonNullable); + function: { + name: string; + description: string; + parameters?: { + type: "object" | (string & NonNullable); + properties: { + [key: string]: { + type: string; + description?: string; + }; + }; + required: string[]; + }; + }; +}; +type AiTextGenerationFunctionsInput = { + name: string; + code: string; +}; +type AiTextGenerationResponseFormat = { + type: string; + json_schema?: any; +}; +type AiTextGenerationInput = { + prompt?: string; + raw?: boolean; + stream?: boolean; + max_tokens?: number; + temperature?: number; + top_p?: number; + top_k?: number; + seed?: number; + repetition_penalty?: number; + frequency_penalty?: number; + presence_penalty?: number; + messages?: RoleScopedChatInput[]; + response_format?: AiTextGenerationResponseFormat; + tools?: AiTextGenerationToolInput[] | AiTextGenerationToolLegacyInput[] | (object & NonNullable); + functions?: AiTextGenerationFunctionsInput[]; +}; +type AiTextGenerationToolLegacyOutput = { + name: string; + arguments: unknown; +}; +type AiTextGenerationToolOutput = { + id: string; + type: "function"; + function: { + name: string; + arguments: string; + }; +}; +type UsageTags = { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; +}; +type AiTextGenerationOutput = { + response?: string; + tool_calls?: AiTextGenerationToolLegacyOutput[] & AiTextGenerationToolOutput[]; + usage?: UsageTags; +}; +declare abstract class BaseAiTextGeneration { + inputs: AiTextGenerationInput; + postProcessedOutputs: AiTextGenerationOutput; +} +type AiTextToSpeechInput = { + prompt: string; + lang?: string; +}; +type AiTextToSpeechOutput = Uint8Array | { + audio: string; +}; +declare abstract class BaseAiTextToSpeech { + inputs: AiTextToSpeechInput; + postProcessedOutputs: AiTextToSpeechOutput; +} +type AiTextToImageInput = { + prompt: string; + negative_prompt?: string; + height?: number; + width?: number; + image?: number[]; + image_b64?: string; + mask?: number[]; + num_steps?: number; + strength?: number; + guidance?: number; + seed?: number; +}; +type AiTextToImageOutput = ReadableStream; +declare abstract class BaseAiTextToImage { + inputs: AiTextToImageInput; + postProcessedOutputs: AiTextToImageOutput; +} +type AiTranslationInput = { + text: string; + target_lang: string; + source_lang?: string; +}; +type AiTranslationOutput = { + translated_text?: string; +}; +declare abstract class BaseAiTranslation { + inputs: AiTranslationInput; + postProcessedOutputs: AiTranslationOutput; +} +/** + * Workers AI support for OpenAI's Chat Completions API + */ +type ChatCompletionContentPartText = { + type: "text"; + text: string; +}; +type ChatCompletionContentPartImage = { + type: "image_url"; + image_url: { + url: string; + detail?: "auto" | "low" | "high"; + }; +}; +type ChatCompletionContentPartInputAudio = { + type: "input_audio"; + input_audio: { + /** Base64 encoded audio data. */ + data: string; + format: "wav" | "mp3"; + }; +}; +type ChatCompletionContentPartFile = { + type: "file"; + file: { + /** Base64 encoded file data. */ + file_data?: string; + /** The ID of an uploaded file. */ + file_id?: string; + filename?: string; + }; +}; +type ChatCompletionContentPartRefusal = { + type: "refusal"; + refusal: string; +}; +type ChatCompletionContentPart = ChatCompletionContentPartText | ChatCompletionContentPartImage | ChatCompletionContentPartInputAudio | ChatCompletionContentPartFile; +type FunctionDefinition = { + name: string; + description?: string; + parameters?: Record; + strict?: boolean | null; +}; +type ChatCompletionFunctionTool = { + type: "function"; + function: FunctionDefinition; +}; +type ChatCompletionCustomToolGrammarFormat = { + type: "grammar"; + grammar: { + definition: string; + syntax: "lark" | "regex"; + }; +}; +type ChatCompletionCustomToolTextFormat = { + type: "text"; +}; +type ChatCompletionCustomToolFormat = ChatCompletionCustomToolTextFormat | ChatCompletionCustomToolGrammarFormat; +type ChatCompletionCustomTool = { + type: "custom"; + custom: { + name: string; + description?: string; + format?: ChatCompletionCustomToolFormat; + }; +}; +type ChatCompletionTool = ChatCompletionFunctionTool | ChatCompletionCustomTool; +type ChatCompletionMessageFunctionToolCall = { + id: string; + type: "function"; + function: { + name: string; + /** JSON-encoded arguments string. */ + arguments: string; + }; +}; +type ChatCompletionMessageCustomToolCall = { + id: string; + type: "custom"; + custom: { + name: string; + input: string; + }; +}; +type ChatCompletionMessageToolCall = ChatCompletionMessageFunctionToolCall | ChatCompletionMessageCustomToolCall; +type ChatCompletionToolChoiceFunction = { + type: "function"; + function: { + name: string; + }; +}; +type ChatCompletionToolChoiceCustom = { + type: "custom"; + custom: { + name: string; + }; +}; +type ChatCompletionToolChoiceAllowedTools = { + type: "allowed_tools"; + allowed_tools: { + mode: "auto" | "required"; + tools: Array>; + }; +}; +type ChatCompletionToolChoiceOption = "none" | "auto" | "required" | ChatCompletionToolChoiceFunction | ChatCompletionToolChoiceCustom | ChatCompletionToolChoiceAllowedTools; +type DeveloperMessage = { + role: "developer"; + content: string | Array<{ + type: "text"; + text: string; + }>; + name?: string; +}; +type SystemMessage = { + role: "system"; + content: string | Array<{ + type: "text"; + text: string; + }>; + name?: string; +}; +/** + * Permissive merged content part used inside UserMessage arrays. + * + * Cabidela has a limitation where anyOf/oneOf with enum-based discrimination + * inside nested array items does not correctly match different branches for + * different array elements, so the schema uses a single merged object. + */ +type UserMessageContentPart = { + type: "text" | "image_url" | "input_audio" | "file"; + text?: string; + image_url?: { + url?: string; + detail?: "auto" | "low" | "high"; + }; + input_audio?: { + data?: string; + format?: "wav" | "mp3"; + }; + file?: { + file_data?: string; + file_id?: string; + filename?: string; + }; +}; +type UserMessage = { + role: "user"; + content: string | Array; + name?: string; +}; +type AssistantMessageContentPart = { + type: "text" | "refusal"; + text?: string; + refusal?: string; +}; +type AssistantMessage = { + role: "assistant"; + content?: string | null | Array; + refusal?: string | null; + name?: string; + audio?: { + id: string; + }; + tool_calls?: Array; + function_call?: { + name: string; + arguments: string; + }; +}; +type ToolMessage = { + role: "tool"; + content: string | Array<{ + type: "text"; + text: string; + }>; + tool_call_id: string; +}; +type FunctionMessage = { + role: "function"; + content: string; + name: string; +}; +type ChatCompletionMessageParam = DeveloperMessage | SystemMessage | UserMessage | AssistantMessage | ToolMessage | FunctionMessage; +type ChatCompletionsResponseFormatText = { + type: "text"; +}; +type ChatCompletionsResponseFormatJSONObject = { + type: "json_object"; +}; +type ResponseFormatJSONSchema = { + type: "json_schema"; + json_schema: { + name: string; + description?: string; + schema?: Record; + strict?: boolean | null; + }; +}; +type ResponseFormat = ChatCompletionsResponseFormatText | ChatCompletionsResponseFormatJSONObject | ResponseFormatJSONSchema; +type ChatCompletionsStreamOptions = { + include_usage?: boolean; + include_obfuscation?: boolean; +}; +type PredictionContent = { + type: "content"; + content: string | Array<{ + type: "text"; + text: string; + }>; +}; +type AudioParams = { + voice: string | { + id: string; + }; + format: "wav" | "aac" | "mp3" | "flac" | "opus" | "pcm16"; +}; +type WebSearchUserLocation = { + type: "approximate"; + approximate: { + city?: string; + country?: string; + region?: string; + timezone?: string; + }; +}; +type WebSearchOptions = { + search_context_size?: "low" | "medium" | "high"; + user_location?: WebSearchUserLocation; +}; +type ChatTemplateKwargs = { + /** Whether to enable reasoning, enabled by default. */ + enable_thinking?: boolean; + /** If false, preserves reasoning context between turns. */ + clear_thinking?: boolean; +}; +/** Shared optional properties used by both Prompt and Messages input branches. */ +type ChatCompletionsCommonOptions = { + model?: string; + audio?: AudioParams; + frequency_penalty?: number | null; + logit_bias?: Record | null; + logprobs?: boolean | null; + top_logprobs?: number | null; + max_tokens?: number | null; + max_completion_tokens?: number | null; + metadata?: Record | null; + modalities?: Array<"text" | "audio"> | null; + n?: number | null; + parallel_tool_calls?: boolean; + prediction?: PredictionContent; + presence_penalty?: number | null; + reasoning_effort?: "low" | "medium" | "high" | null; + chat_template_kwargs?: ChatTemplateKwargs; + response_format?: ResponseFormat; + seed?: number | null; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + stop?: string | Array | null; + store?: boolean | null; + stream?: boolean | null; + stream_options?: ChatCompletionsStreamOptions; + temperature?: number | null; + tool_choice?: ChatCompletionToolChoiceOption; + tools?: Array; + top_p?: number | null; + user?: string; + web_search_options?: WebSearchOptions; + function_call?: "none" | "auto" | { + name: string; + }; + functions?: Array; +}; +type PromptTokensDetails = { + cached_tokens?: number; + audio_tokens?: number; +}; +type CompletionTokensDetails = { + reasoning_tokens?: number; + audio_tokens?: number; + accepted_prediction_tokens?: number; + rejected_prediction_tokens?: number; +}; +type CompletionUsage = { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + prompt_tokens_details?: PromptTokensDetails; + completion_tokens_details?: CompletionTokensDetails; +}; +type ChatCompletionTopLogprob = { + token: string; + logprob: number; + bytes: Array | null; +}; +type ChatCompletionTokenLogprob = { + token: string; + logprob: number; + bytes: Array | null; + top_logprobs: Array; +}; +type ChatCompletionAudio = { + id: string; + /** Base64 encoded audio bytes. */ + data: string; + expires_at: number; + transcript: string; +}; +type ChatCompletionUrlCitation = { + type: "url_citation"; + url_citation: { + url: string; + title: string; + start_index: number; + end_index: number; + }; +}; +type ChatCompletionResponseMessage = { + role: "assistant"; + content: string | null; + refusal: string | null; + annotations?: Array; + audio?: ChatCompletionAudio; + tool_calls?: Array; + function_call?: { + name: string; + arguments: string; + } | null; +}; +type ChatCompletionLogprobs = { + content: Array | null; + refusal?: Array | null; +}; +type ChatCompletionChoice = { + index: number; + message: ChatCompletionResponseMessage; + finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | "function_call"; + logprobs: ChatCompletionLogprobs | null; +}; +type ChatCompletionsMessagesInput = { + messages: Array; +} & ChatCompletionsCommonOptions; +type ChatCompletionsOutput = { + id: string; + object: string; + created: number; + model: string; + choices: Array; + usage?: CompletionUsage; + system_fingerprint?: string | null; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; +}; +/** + * Workers AI support for OpenAI's Responses API + * Reference: https://github.com/openai/openai-node/blob/master/src/resources/responses/responses.ts + * + * It's a stripped down version from its source. + * It currently supports basic function calling, json mode and accepts images as input. + * + * It does not include types for WebSearch, CodeInterpreter, FileInputs, MCP, CustomTools. + * We plan to add those incrementally as model + platform capabilities evolve. + */ +type ResponsesInput = { + background?: boolean | null; + conversation?: string | ResponseConversationParam | null; + include?: Array | null; + input?: string | ResponseInput; + instructions?: string | null; + max_output_tokens?: number | null; + parallel_tool_calls?: boolean | null; + previous_response_id?: string | null; + prompt_cache_key?: string; + reasoning?: Reasoning | null; + safety_identifier?: string; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + stream?: boolean | null; + stream_options?: StreamOptions | null; + temperature?: number | null; + text?: ResponseTextConfig; + tool_choice?: ToolChoiceOptions | ToolChoiceFunction; + tools?: Array; + top_p?: number | null; + truncation?: "auto" | "disabled" | null; +}; +type ResponsesOutput = { + id?: string; + created_at?: number; + output_text?: string; + error?: ResponseError | null; + incomplete_details?: ResponseIncompleteDetails | null; + instructions?: string | Array | null; + object?: "response"; + output?: Array; + parallel_tool_calls?: boolean; + temperature?: number | null; + tool_choice?: ToolChoiceOptions | ToolChoiceFunction; + tools?: Array; + top_p?: number | null; + max_output_tokens?: number | null; + previous_response_id?: string | null; + prompt?: ResponsePrompt | null; + reasoning?: Reasoning | null; + safety_identifier?: string; + service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; + status?: ResponseStatus; + text?: ResponseTextConfig; + truncation?: "auto" | "disabled" | null; + usage?: ResponseUsage; +}; +type EasyInputMessage = { + content: string | ResponseInputMessageContentList; + role: "user" | "assistant" | "system" | "developer"; + type?: "message"; +}; +type ResponsesFunctionTool = { + name: string; + parameters: { + [key: string]: unknown; + } | null; + strict: boolean | null; + type: "function"; + description?: string | null; +}; +type ResponseIncompleteDetails = { + reason?: "max_output_tokens" | "content_filter"; +}; +type ResponsePrompt = { + id: string; + variables?: { + [key: string]: string | ResponseInputText | ResponseInputImage; + } | null; + version?: string | null; +}; +type Reasoning = { + effort?: ReasoningEffort | null; + generate_summary?: "auto" | "concise" | "detailed" | null; + summary?: "auto" | "concise" | "detailed" | null; +}; +type ResponseContent = ResponseInputText | ResponseInputImage | ResponseOutputText | ResponseOutputRefusal | ResponseContentReasoningText; +type ResponseContentReasoningText = { + text: string; + type: "reasoning_text"; +}; +type ResponseConversationParam = { + id: string; +}; +type ResponseCreatedEvent = { + response: Response; + sequence_number: number; + type: "response.created"; +}; +type ResponseCustomToolCallOutput = { + call_id: string; + output: string | Array; + type: "custom_tool_call_output"; + id?: string; +}; +type ResponseError = { + code: "server_error" | "rate_limit_exceeded" | "invalid_prompt" | "vector_store_timeout" | "invalid_image" | "invalid_image_format" | "invalid_base64_image" | "invalid_image_url" | "image_too_large" | "image_too_small" | "image_parse_error" | "image_content_policy_violation" | "invalid_image_mode" | "image_file_too_large" | "unsupported_image_media_type" | "empty_image_file" | "failed_to_download_image" | "image_file_not_found"; + message: string; +}; +type ResponseErrorEvent = { + code: string | null; + message: string; + param: string | null; + sequence_number: number; + type: "error"; +}; +type ResponseFailedEvent = { + response: Response; + sequence_number: number; + type: "response.failed"; +}; +type ResponseFormatText = { + type: "text"; +}; +type ResponseFormatJSONObject = { + type: "json_object"; +}; +type ResponseFormatTextConfig = ResponseFormatText | ResponseFormatTextJSONSchemaConfig | ResponseFormatJSONObject; +type ResponseFormatTextJSONSchemaConfig = { + name: string; + schema: { + [key: string]: unknown; + }; + type: "json_schema"; + description?: string; + strict?: boolean | null; +}; +type ResponseFunctionCallArgumentsDeltaEvent = { + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.function_call_arguments.delta"; +}; +type ResponseFunctionCallArgumentsDoneEvent = { + arguments: string; + item_id: string; + name: string; + output_index: number; + sequence_number: number; + type: "response.function_call_arguments.done"; +}; +type ResponseFunctionCallOutputItem = ResponseInputTextContent | ResponseInputImageContent; +type ResponseFunctionCallOutputItemList = Array; +type ResponseFunctionToolCall = { + arguments: string; + call_id: string; + name: string; + type: "function_call"; + id?: string; + status?: "in_progress" | "completed" | "incomplete"; +}; +interface ResponseFunctionToolCallItem extends ResponseFunctionToolCall { + id: string; +} +type ResponseFunctionToolCallOutputItem = { + id: string; + call_id: string; + output: string | Array; + type: "function_call_output"; + status?: "in_progress" | "completed" | "incomplete"; +}; +type ResponseIncludable = "message.input_image.image_url" | "message.output_text.logprobs"; +type ResponseIncompleteEvent = { + response: Response; + sequence_number: number; + type: "response.incomplete"; +}; +type ResponseInput = Array; +type ResponseInputContent = ResponseInputText | ResponseInputImage; +type ResponseInputImage = { + detail: "low" | "high" | "auto"; + type: "input_image"; + /** + * Base64 encoded image + */ + image_url?: string | null; +}; +type ResponseInputImageContent = { + type: "input_image"; + detail?: "low" | "high" | "auto" | null; + /** + * Base64 encoded image + */ + image_url?: string | null; +}; +type ResponseInputItem = EasyInputMessage | ResponseInputItemMessage | ResponseOutputMessage | ResponseFunctionToolCall | ResponseInputItemFunctionCallOutput | ResponseReasoningItem; +type ResponseInputItemFunctionCallOutput = { + call_id: string; + output: string | ResponseFunctionCallOutputItemList; + type: "function_call_output"; + id?: string | null; + status?: "in_progress" | "completed" | "incomplete" | null; +}; +type ResponseInputItemMessage = { + content: ResponseInputMessageContentList; + role: "user" | "system" | "developer"; + status?: "in_progress" | "completed" | "incomplete"; + type?: "message"; +}; +type ResponseInputMessageContentList = Array; +type ResponseInputMessageItem = { + id: string; + content: ResponseInputMessageContentList; + role: "user" | "system" | "developer"; + status?: "in_progress" | "completed" | "incomplete"; + type?: "message"; +}; +type ResponseInputText = { + text: string; + type: "input_text"; +}; +type ResponseInputTextContent = { + text: string; + type: "input_text"; +}; +type ResponseItem = ResponseInputMessageItem | ResponseOutputMessage | ResponseFunctionToolCallItem | ResponseFunctionToolCallOutputItem; +type ResponseOutputItem = ResponseOutputMessage | ResponseFunctionToolCall | ResponseReasoningItem; +type ResponseOutputItemAddedEvent = { + item: ResponseOutputItem; + output_index: number; + sequence_number: number; + type: "response.output_item.added"; +}; +type ResponseOutputItemDoneEvent = { + item: ResponseOutputItem; + output_index: number; + sequence_number: number; + type: "response.output_item.done"; +}; +type ResponseOutputMessage = { + id: string; + content: Array; + role: "assistant"; + status: "in_progress" | "completed" | "incomplete"; + type: "message"; +}; +type ResponseOutputRefusal = { + refusal: string; + type: "refusal"; +}; +type ResponseOutputText = { + text: string; + type: "output_text"; + logprobs?: Array; +}; +type ResponseReasoningItem = { + id: string; + summary: Array; + type: "reasoning"; + content?: Array; + encrypted_content?: string | null; + status?: "in_progress" | "completed" | "incomplete"; +}; +type ResponseReasoningSummaryItem = { + text: string; + type: "summary_text"; +}; +type ResponseReasoningContentItem = { + text: string; + type: "reasoning_text"; +}; +type ResponseReasoningTextDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.reasoning_text.delta"; +}; +type ResponseReasoningTextDoneEvent = { + content_index: number; + item_id: string; + output_index: number; + sequence_number: number; + text: string; + type: "response.reasoning_text.done"; +}; +type ResponseRefusalDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + output_index: number; + sequence_number: number; + type: "response.refusal.delta"; +}; +type ResponseRefusalDoneEvent = { + content_index: number; + item_id: string; + output_index: number; + refusal: string; + sequence_number: number; + type: "response.refusal.done"; +}; +type ResponseStatus = "completed" | "failed" | "in_progress" | "cancelled" | "queued" | "incomplete"; +type ResponseStreamEvent = ResponseCompletedEvent | ResponseCreatedEvent | ResponseErrorEvent | ResponseFunctionCallArgumentsDeltaEvent | ResponseFunctionCallArgumentsDoneEvent | ResponseFailedEvent | ResponseIncompleteEvent | ResponseOutputItemAddedEvent | ResponseOutputItemDoneEvent | ResponseReasoningTextDeltaEvent | ResponseReasoningTextDoneEvent | ResponseRefusalDeltaEvent | ResponseRefusalDoneEvent | ResponseTextDeltaEvent | ResponseTextDoneEvent; +type ResponseCompletedEvent = { + response: Response; + sequence_number: number; + type: "response.completed"; +}; +type ResponseTextConfig = { + format?: ResponseFormatTextConfig; + verbosity?: "low" | "medium" | "high" | null; +}; +type ResponseTextDeltaEvent = { + content_index: number; + delta: string; + item_id: string; + logprobs: Array; + output_index: number; + sequence_number: number; + type: "response.output_text.delta"; +}; +type ResponseTextDoneEvent = { + content_index: number; + item_id: string; + logprobs: Array; + output_index: number; + sequence_number: number; + text: string; + type: "response.output_text.done"; +}; +type Logprob = { + token: string; + logprob: number; + top_logprobs?: Array; +}; +type TopLogprob = { + token?: string; + logprob?: number; +}; +type ResponseUsage = { + input_tokens: number; + output_tokens: number; + total_tokens: number; +}; +type Tool = ResponsesFunctionTool; +type ToolChoiceFunction = { + name: string; + type: "function"; +}; +type ToolChoiceOptions = "none"; +type ReasoningEffort = "minimal" | "low" | "medium" | "high" | null; +type StreamOptions = { + include_obfuscation?: boolean; +}; +/** Marks keys from T that aren't in U as optional never */ +type Without = { + [P in Exclude]?: never; +}; +/** Either T or U, but not both (mutually exclusive) */ +type XOR = (T & Without) | (U & Without); +type Ai_Cf_Baai_Bge_Base_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Base_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Base_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Base_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Base_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Base_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Base_En_V1_5_Output; +} +type Ai_Cf_Openai_Whisper_Input = string | { + /** + * An array of integers that represent the audio data constrained to 8-bit unsigned integer values + */ + audio: number[]; +}; +interface Ai_Cf_Openai_Whisper_Output { + /** + * The transcription + */ + text: string; + word_count?: number; + words?: { + word?: string; + /** + * The second this word begins in the recording + */ + start?: number; + /** + * The ending second when the word completes + */ + end?: number; + }[]; + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper { + inputs: Ai_Cf_Openai_Whisper_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Output; +} +type Ai_Cf_Meta_M2M100_1_2B_Input = { + /** + * The text to be translated + */ + text: string; + /** + * The language code of the source text (e.g., 'en' for English). Defaults to 'en' if not specified + */ + source_lang?: string; + /** + * The language code to translate the text into (e.g., 'es' for Spanish) + */ + target_lang: string; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + /** + * The text to be translated + */ + text: string; + /** + * The language code of the source text (e.g., 'en' for English). Defaults to 'en' if not specified + */ + source_lang?: string; + /** + * The language code to translate the text into (e.g., 'es' for Spanish) + */ + target_lang: string; + }[]; +}; +type Ai_Cf_Meta_M2M100_1_2B_Output = { + /** + * The translated text in the target language + */ + translated_text?: string; +} | Ai_Cf_Meta_M2M100_1_2B_AsyncResponse; +interface Ai_Cf_Meta_M2M100_1_2B_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Meta_M2M100_1_2B { + inputs: Ai_Cf_Meta_M2M100_1_2B_Input; + postProcessedOutputs: Ai_Cf_Meta_M2M100_1_2B_Output; +} +type Ai_Cf_Baai_Bge_Small_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Small_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Small_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Small_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Small_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Small_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Small_En_V1_5_Output; +} +type Ai_Cf_Baai_Bge_Large_En_V1_5_Input = { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; +} | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: { + text: string | string[]; + /** + * The pooling method used in the embedding process. `cls` pooling will generate more accurate embeddings on larger inputs - however, embeddings created with cls pooling are not compatible with embeddings generated with mean pooling. The default pooling method is `mean` in order for this to not be a breaking change, but we highly suggest using the new `cls` pooling for better accuracy. + */ + pooling?: "mean" | "cls"; + }[]; +}; +type Ai_Cf_Baai_Bge_Large_En_V1_5_Output = { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} | Ai_Cf_Baai_Bge_Large_En_V1_5_AsyncResponse; +interface Ai_Cf_Baai_Bge_Large_En_V1_5_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Large_En_V1_5 { + inputs: Ai_Cf_Baai_Bge_Large_En_V1_5_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Large_En_V1_5_Output; +} +type Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Input = string | { + /** + * The input text prompt for the model to generate a response. + */ + prompt?: string; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * Controls the creativity of the AI's responses by adjusting how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + image: number[] | (string & NonNullable); + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; +}; +interface Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Output { + description?: string; +} +declare abstract class Base_Ai_Cf_Unum_Uform_Gen2_Qwen_500M { + inputs: Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Input; + postProcessedOutputs: Ai_Cf_Unum_Uform_Gen2_Qwen_500M_Output; +} +type Ai_Cf_Openai_Whisper_Tiny_En_Input = string | { + /** + * An array of integers that represent the audio data constrained to 8-bit unsigned integer values + */ + audio: number[]; +}; +interface Ai_Cf_Openai_Whisper_Tiny_En_Output { + /** + * The transcription + */ + text: string; + word_count?: number; + words?: { + word?: string; + /** + * The second this word begins in the recording + */ + start?: number; + /** + * The ending second when the word completes + */ + end?: number; + }[]; + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper_Tiny_En { + inputs: Ai_Cf_Openai_Whisper_Tiny_En_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Tiny_En_Output; +} +interface Ai_Cf_Openai_Whisper_Large_V3_Turbo_Input { + audio: string | { + body?: object; + contentType?: string; + }; + /** + * Supported tasks are 'translate' or 'transcribe'. + */ + task?: string; + /** + * The language of the audio being transcribed or translated. + */ + language?: string; + /** + * Preprocess the audio with a voice activity detection model. + */ + vad_filter?: boolean; + /** + * A text prompt to help provide context to the model on the contents of the audio. + */ + initial_prompt?: string; + /** + * The prefix appended to the beginning of the output of the transcription and can guide the transcription result. + */ + prefix?: string; + /** + * The number of beams to use in beam search decoding. Higher values may improve accuracy at the cost of speed. + */ + beam_size?: number; + /** + * Whether to condition on previous text during transcription. Setting to false may help prevent hallucination loops. + */ + condition_on_previous_text?: boolean; + /** + * Threshold for detecting no-speech segments. Segments with no-speech probability above this value are skipped. + */ + no_speech_threshold?: number; + /** + * Threshold for filtering out segments with high compression ratio, which often indicate repetitive or hallucinated text. + */ + compression_ratio_threshold?: number; + /** + * Threshold for filtering out segments with low average log probability, indicating low confidence. + */ + log_prob_threshold?: number; + /** + * Optional threshold (in seconds) to skip silent periods that may cause hallucinations. + */ + hallucination_silence_threshold?: number; +} +interface Ai_Cf_Openai_Whisper_Large_V3_Turbo_Output { + transcription_info?: { + /** + * The language of the audio being transcribed or translated. + */ + language?: string; + /** + * The confidence level or probability of the detected language being accurate, represented as a decimal between 0 and 1. + */ + language_probability?: number; + /** + * The total duration of the original audio file, in seconds. + */ + duration?: number; + /** + * The duration of the audio after applying Voice Activity Detection (VAD) to remove silent or irrelevant sections, in seconds. + */ + duration_after_vad?: number; + }; + /** + * The complete transcription of the audio. + */ + text: string; + /** + * The total number of words in the transcription. + */ + word_count?: number; + segments?: { + /** + * The starting time of the segment within the audio, in seconds. + */ + start?: number; + /** + * The ending time of the segment within the audio, in seconds. + */ + end?: number; + /** + * The transcription of the segment. + */ + text?: string; + /** + * The temperature used in the decoding process, controlling randomness in predictions. Lower values result in more deterministic outputs. + */ + temperature?: number; + /** + * The average log probability of the predictions for the words in this segment, indicating overall confidence. + */ + avg_logprob?: number; + /** + * The compression ratio of the input to the output, measuring how much the text was compressed during the transcription process. + */ + compression_ratio?: number; + /** + * The probability that the segment contains no speech, represented as a decimal between 0 and 1. + */ + no_speech_prob?: number; + words?: { + /** + * The individual word transcribed from the audio. + */ + word?: string; + /** + * The starting time of the word within the audio, in seconds. + */ + start?: number; + /** + * The ending time of the word within the audio, in seconds. + */ + end?: number; + }[]; + }[]; + /** + * The transcription in WebVTT format, which includes timing and text information for use in subtitles. + */ + vtt?: string; +} +declare abstract class Base_Ai_Cf_Openai_Whisper_Large_V3_Turbo { + inputs: Ai_Cf_Openai_Whisper_Large_V3_Turbo_Input; + postProcessedOutputs: Ai_Cf_Openai_Whisper_Large_V3_Turbo_Output; +} +type Ai_Cf_Baai_Bge_M3_Input = Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts | Ai_Cf_Baai_Bge_M3_Input_Embedding | { + /** + * Batch of the embeddings requests to run using async-queue + */ + requests: (Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts_1 | Ai_Cf_Baai_Bge_M3_Input_Embedding_1)[]; +}; +interface Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts { + /** + * A query you wish to perform against the provided contexts. If no query is provided the model with respond with embeddings for contexts + */ + query?: string; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_Embedding { + text: string | string[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_QueryAnd_Contexts_1 { + /** + * A query you wish to perform against the provided contexts. If no query is provided the model with respond with embeddings for contexts + */ + query?: string; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +interface Ai_Cf_Baai_Bge_M3_Input_Embedding_1 { + text: string | string[]; + /** + * When provided with too long context should the model error out or truncate the context to fit? + */ + truncate_inputs?: boolean; +} +type Ai_Cf_Baai_Bge_M3_Output = Ai_Cf_Baai_Bge_M3_Output_Query | Ai_Cf_Baai_Bge_M3_Output_EmbeddingFor_Contexts | Ai_Cf_Baai_Bge_M3_Output_Embedding | Ai_Cf_Baai_Bge_M3_AsyncResponse; +interface Ai_Cf_Baai_Bge_M3_Output_Query { + response?: { + /** + * Index of the context in the request + */ + id?: number; + /** + * Score of the context under the index. + */ + score?: number; + }[]; +} +interface Ai_Cf_Baai_Bge_M3_Output_EmbeddingFor_Contexts { + response?: number[][]; + shape?: number[]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} +interface Ai_Cf_Baai_Bge_M3_Output_Embedding { + shape?: number[]; + /** + * Embeddings of the requested text values + */ + data?: number[][]; + /** + * The pooling method used in the embedding process. + */ + pooling?: "mean" | "cls"; +} +interface Ai_Cf_Baai_Bge_M3_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Baai_Bge_M3 { + inputs: Ai_Cf_Baai_Bge_M3_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_M3_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * The number of diffusion steps; higher values can improve quality but take longer. + */ + steps?: number; +} +interface Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Output { + /** + * The generated image in Base64 format. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_1_Schnell { + inputs: Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_1_Schnell_Output; +} +type Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Input = Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Prompt | Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Messages; +interface Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + image?: number[] | (string & NonNullable); + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; +} +interface Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + image?: number[] | (string & NonNullable); + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * If true, the response will be streamed back incrementally. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Controls the creativity of the AI's responses by adjusting how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Output = { + /** + * The generated text response from the model + */ + response?: string; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct { + inputs: Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct_Output; +} +type Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Input = Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Prompt | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Messages | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Async_Batch; +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Async_Batch { + requests?: { + /** + * User-supplied reference. This field will be present in the response as well it can be used to reference the request and response. It's NOT validated to be unique. + */ + external_reference?: string; + /** + * Prompt for the text generation model + */ + prompt?: string; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; + response_format?: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_2; + }[]; +} +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +} | string | Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_AsyncResponse; +interface Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast { + inputs: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast_Output; +} +interface Ai_Cf_Meta_Llama_Guard_3_8B_Input { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender must alternate between 'user' and 'assistant'. + */ + role: "user" | "assistant"; + /** + * The content of the message as a string. + */ + content: string; + }[]; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Dictate the output format of the generated response. + */ + response_format?: { + /** + * Set to json_object to process and output generated text as JSON. + */ + type?: string; + }; +} +interface Ai_Cf_Meta_Llama_Guard_3_8B_Output { + response?: string | { + /** + * Whether the conversation is safe or not. + */ + safe?: boolean; + /** + * A list of what hazard categories predicted for the conversation, if the conversation is deemed unsafe. + */ + categories?: string[]; + }; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +declare abstract class Base_Ai_Cf_Meta_Llama_Guard_3_8B { + inputs: Ai_Cf_Meta_Llama_Guard_3_8B_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_Guard_3_8B_Output; +} +interface Ai_Cf_Baai_Bge_Reranker_Base_Input { + /** + * A query you wish to perform against the provided contexts. + */ + /** + * Number of returned results starting with the best score. + */ + top_k?: number; + /** + * List of provided contexts. Note that the index in this array is important, as the response will refer to it. + */ + contexts: { + /** + * One of the provided context content + */ + text?: string; + }[]; +} +interface Ai_Cf_Baai_Bge_Reranker_Base_Output { + response?: { + /** + * Index of the context in the request + */ + id?: number; + /** + * Score of the context under the index. + */ + score?: number; + }[]; +} +declare abstract class Base_Ai_Cf_Baai_Bge_Reranker_Base { + inputs: Ai_Cf_Baai_Bge_Reranker_Base_Input; + postProcessedOutputs: Ai_Cf_Baai_Bge_Reranker_Base_Output; +} +type Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Input = Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Prompt | Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Messages; +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + /** + * The content of the message as a string. + */ + content: string; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct { + inputs: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct_Output; +} +type Ai_Cf_Qwen_Qwq_32B_Input = Ai_Cf_Qwen_Qwq_32B_Prompt | Ai_Cf_Qwen_Qwq_32B_Messages; +interface Ai_Cf_Qwen_Qwq_32B_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwq_32B_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Qwen_Qwq_32B_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Qwen_Qwq_32B { + inputs: Ai_Cf_Qwen_Qwq_32B_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwq_32B_Output; +} +type Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Input = Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Prompt | Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Messages; +interface Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. Must be supplied for tool calls for Mistral-3. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct { + inputs: Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Input; + postProcessedOutputs: Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct_Output; +} +type Ai_Cf_Google_Gemma_3_12B_It_Input = Ai_Cf_Google_Gemma_3_12B_It_Prompt | Ai_Cf_Google_Gemma_3_12B_It_Messages; +interface Ai_Cf_Google_Gemma_3_12B_It_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Google_Gemma_3_12B_It_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Google_Gemma_3_12B_It_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + /** + * The name of the tool to be called + */ + name?: string; + }[]; +}; +declare abstract class Base_Ai_Cf_Google_Gemma_3_12B_It { + inputs: Ai_Cf_Google_Gemma_3_12B_It_Input; + postProcessedOutputs: Ai_Cf_Google_Gemma_3_12B_It_Output; +} +type Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Input = Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Async_Batch; +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Async_Batch { + requests: (Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt_Inner | Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages_Inner)[]; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Prompt_Inner { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * JSON schema that should be fulfilled for the response. + */ + guided_json?: object; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Messages_Inner { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role?: string; + /** + * The tool call id. If you don't know what to put here you can fall back to 000000001 + */ + tool_call_id?: string; + content?: string | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }[] | { + /** + * Type of the content provided + */ + type?: string; + text?: string; + image_url?: { + /** + * image uri with data (e.g. data:image/jpeg;base64,/9j/...). HTTP URL will not be accepted + */ + url?: string; + }; + }; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_JSON_Mode; + /** + * JSON schema that should be fufilled for the response. + */ + guided_json?: object; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +type Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Output = { + /** + * The generated text response from the model + */ + response: string; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * An array of tool calls requests made during the response generation + */ + tool_calls?: { + /** + * The tool call id. + */ + id?: string; + /** + * Specifies the type of tool (e.g., 'function'). + */ + type?: string; + /** + * Details of the function tool. + */ + function?: { + /** + * The name of the tool to be called + */ + name?: string; + /** + * The arguments passed to be passed to the tool call request + */ + arguments?: object; + }; + }[]; +}; +declare abstract class Base_Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct { + inputs: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Input; + postProcessedOutputs: Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct_Output; +} +type Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Input = Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Async_Batch; +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Async_Batch { + requests: (Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt_1 | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages_1)[]; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Prompt_1 { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_2; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Messages_1 { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_3; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_JSON_Mode_3 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Output = Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Chat_Completion_Response | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Text_Completion_Response | string | Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_AsyncResponse; +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Chat_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "chat.completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index?: number; + /** + * The message generated by the model + */ + message?: { + /** + * Role of the message author + */ + role: string; + /** + * The content of the message + */ + content: string; + /** + * Internal reasoning content (if available) + */ + reasoning_content?: string; + /** + * Tool calls made by the assistant + */ + tool_calls?: { + /** + * Unique identifier for the tool call + */ + id: string; + /** + * Type of tool call + */ + type: "function"; + function: { + /** + * Name of the function to call + */ + name: string; + /** + * JSON string of arguments for the function + */ + arguments: string; + }; + }[]; + }; + /** + * Reason why the model stopped generating + */ + finish_reason?: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Text_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "text_completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index: number; + /** + * The generated text completion + */ + text: string; + /** + * Reason why the model stopped generating + */ + finish_reason: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +interface Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8 { + inputs: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8_Output; +} +interface Ai_Cf_Deepgram_Nova_3_Input { + audio: { + body: object; + contentType: string; + }; + /** + * Sets how the model will interpret strings submitted to the custom_topic param. When strict, the model will only return topics submitted using the custom_topic param. When extended, the model will return its own detected topics in addition to those submitted using the custom_topic param. + */ + custom_topic_mode?: "extended" | "strict"; + /** + * Custom topics you want the model to detect within your input audio or text if present Submit up to 100 + */ + custom_topic?: string; + /** + * Sets how the model will interpret intents submitted to the custom_intent param. When strict, the model will only return intents submitted using the custom_intent param. When extended, the model will return its own detected intents in addition those submitted using the custom_intents param + */ + custom_intent_mode?: "extended" | "strict"; + /** + * Custom intents you want the model to detect within your input audio if present + */ + custom_intent?: string; + /** + * Identifies and extracts key entities from content in submitted audio + */ + detect_entities?: boolean; + /** + * Identifies the dominant language spoken in submitted audio + */ + detect_language?: boolean; + /** + * Recognize speaker changes. Each word in the transcript will be assigned a speaker number starting at 0 + */ + diarize?: boolean; + /** + * Identify and extract key entities from content in submitted audio + */ + dictation?: boolean; + /** + * Specify the expected encoding of your submitted audio + */ + encoding?: "linear16" | "flac" | "mulaw" | "amr-nb" | "amr-wb" | "opus" | "speex" | "g729"; + /** + * Arbitrary key-value pairs that are attached to the API response for usage in downstream processing + */ + extra?: string; + /** + * Filler Words can help transcribe interruptions in your audio, like 'uh' and 'um' + */ + filler_words?: boolean; + /** + * Key term prompting can boost or suppress specialized terminology and brands. + */ + keyterm?: string; + /** + * Keywords can boost or suppress specialized terminology and brands. + */ + keywords?: string; + /** + * The BCP-47 language tag that hints at the primary spoken language. Depending on the Model and API endpoint you choose only certain languages are available. + */ + language?: string; + /** + * Spoken measurements will be converted to their corresponding abbreviations. + */ + measurements?: boolean; + /** + * Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip. + */ + mip_opt_out?: boolean; + /** + * Mode of operation for the model representing broad area of topic that will be talked about in the supplied audio + */ + mode?: "general" | "medical" | "finance"; + /** + * Transcribe each audio channel independently. + */ + multichannel?: boolean; + /** + * Numerals converts numbers from written format to numerical format. + */ + numerals?: boolean; + /** + * Splits audio into paragraphs to improve transcript readability. + */ + paragraphs?: boolean; + /** + * Profanity Filter looks for recognized profanity and converts it to the nearest recognized non-profane word or removes it from the transcript completely. + */ + profanity_filter?: boolean; + /** + * Add punctuation and capitalization to the transcript. + */ + punctuate?: boolean; + /** + * Redaction removes sensitive information from your transcripts. + */ + redact?: string; + /** + * Search for terms or phrases in submitted audio and replaces them. + */ + replace?: string; + /** + * Search for terms or phrases in submitted audio. + */ + search?: string; + /** + * Recognizes the sentiment throughout a transcript or text. + */ + sentiment?: boolean; + /** + * Apply formatting to transcript output. When set to true, additional formatting will be applied to transcripts to improve readability. + */ + smart_format?: boolean; + /** + * Detect topics throughout a transcript or text. + */ + topics?: boolean; + /** + * Segments speech into meaningful semantic units. + */ + utterances?: boolean; + /** + * Seconds to wait before detecting a pause between words in submitted audio. + */ + utt_split?: number; + /** + * The number of channels in the submitted audio + */ + channels?: number; + /** + * Specifies whether the streaming endpoint should provide ongoing transcription updates as more audio is received. When set to true, the endpoint sends continuous updates, meaning transcription results may evolve over time. Note: Supported only for webosockets. + */ + interim_results?: boolean; + /** + * Indicates how long model will wait to detect whether a speaker has finished speaking or pauses for a significant period of time. When set to a value, the streaming endpoint immediately finalizes the transcription for the processed time range and returns the transcript with a speech_final parameter set to true. Can also be set to false to disable endpointing + */ + endpointing?: string; + /** + * Indicates that speech has started. You'll begin receiving Speech Started messages upon speech starting. Note: Supported only for webosockets. + */ + vad_events?: boolean; + /** + * Indicates how long model will wait to send an UtteranceEnd message after a word has been transcribed. Use with interim_results. Note: Supported only for webosockets. + */ + utterance_end_ms?: boolean; +} +interface Ai_Cf_Deepgram_Nova_3_Output { + results?: { + channels?: { + alternatives?: { + confidence?: number; + transcript?: string; + words?: { + confidence?: number; + end?: number; + start?: number; + word?: string; + }[]; + }[]; + }[]; + summary?: { + result?: string; + short?: string; + }; + sentiments?: { + segments?: { + text?: string; + start_word?: number; + end_word?: number; + sentiment?: string; + sentiment_score?: number; + }[]; + average?: { + sentiment?: string; + sentiment_score?: number; + }; + }; + }; +} +declare abstract class Base_Ai_Cf_Deepgram_Nova_3 { + inputs: Ai_Cf_Deepgram_Nova_3_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Nova_3_Output; +} +interface Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Input { + queries?: string | string[]; + /** + * Optional instruction for the task + */ + instruction?: string; + documents?: string | string[]; + text?: string | string[]; +} +interface Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Output { + data?: number[][]; + shape?: number[]; +} +declare abstract class Base_Ai_Cf_Qwen_Qwen3_Embedding_0_6B { + inputs: Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Input; + postProcessedOutputs: Ai_Cf_Qwen_Qwen3_Embedding_0_6B_Output; +} +type Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Input = { + /** + * readable stream with audio data and content-type specified for that data + */ + audio: { + body: object; + contentType: string; + }; + /** + * type of data PCM data that's sent to the inference server as raw array + */ + dtype?: "uint8" | "float32" | "float64"; +} | { + /** + * base64 encoded audio data + */ + audio: string; + /** + * type of data PCM data that's sent to the inference server as raw array + */ + dtype?: "uint8" | "float32" | "float64"; +}; +interface Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output { + /** + * if true, end-of-turn was detected + */ + is_complete?: boolean; + /** + * probability of the end-of-turn detection + */ + probability?: number; +} +declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { + inputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Input; + postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; +} +declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { + inputs: XOR; + postProcessedOutputs: XOR; +} +declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { + inputs: XOR; + postProcessedOutputs: XOR; +} +interface Ai_Cf_Leonardo_Phoenix_1_0_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * Controls how closely the generated image should adhere to the prompt; higher values make the image more aligned with the prompt + */ + guidance?: number; + /** + * Random seed for reproducibility of the image generation + */ + seed?: number; + /** + * The height of the generated image in pixels + */ + height?: number; + /** + * The width of the generated image in pixels + */ + width?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + num_steps?: number; + /** + * Specify what to exclude from the generated images + */ + negative_prompt?: string; +} +/** + * The generated image in JPEG format + */ +type Ai_Cf_Leonardo_Phoenix_1_0_Output = string; +declare abstract class Base_Ai_Cf_Leonardo_Phoenix_1_0 { + inputs: Ai_Cf_Leonardo_Phoenix_1_0_Input; + postProcessedOutputs: Ai_Cf_Leonardo_Phoenix_1_0_Output; +} +interface Ai_Cf_Leonardo_Lucid_Origin_Input { + /** + * A text description of the image you want to generate. + */ + prompt: string; + /** + * Controls how closely the generated image should adhere to the prompt; higher values make the image more aligned with the prompt + */ + guidance?: number; + /** + * Random seed for reproducibility of the image generation + */ + seed?: number; + /** + * The height of the generated image in pixels + */ + height?: number; + /** + * The width of the generated image in pixels + */ + width?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + num_steps?: number; + /** + * The number of diffusion steps; higher values can improve quality but take longer + */ + steps?: number; +} +interface Ai_Cf_Leonardo_Lucid_Origin_Output { + /** + * The generated image in Base64 format. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Leonardo_Lucid_Origin { + inputs: Ai_Cf_Leonardo_Lucid_Origin_Input; + postProcessedOutputs: Ai_Cf_Leonardo_Lucid_Origin_Output; +} +interface Ai_Cf_Deepgram_Aura_1_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "angus" | "asteria" | "arcas" | "orion" | "orpheus" | "athena" | "luna" | "zeus" | "perseus" | "helios" | "hera" | "stella"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_1_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_1 { + inputs: Ai_Cf_Deepgram_Aura_1_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_1_Output; +} +interface Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Input { + /** + * Input text to translate. Can be a single string or a list of strings. + */ + text: string | string[]; + /** + * Target langauge to translate to + */ + target_language: "asm_Beng" | "awa_Deva" | "ben_Beng" | "bho_Deva" | "brx_Deva" | "doi_Deva" | "eng_Latn" | "gom_Deva" | "gon_Deva" | "guj_Gujr" | "hin_Deva" | "hne_Deva" | "kan_Knda" | "kas_Arab" | "kas_Deva" | "kha_Latn" | "lus_Latn" | "mag_Deva" | "mai_Deva" | "mal_Mlym" | "mar_Deva" | "mni_Beng" | "mni_Mtei" | "npi_Deva" | "ory_Orya" | "pan_Guru" | "san_Deva" | "sat_Olck" | "snd_Arab" | "snd_Deva" | "tam_Taml" | "tel_Telu" | "urd_Arab" | "unr_Deva"; +} +interface Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Output { + /** + * Translated texts + */ + translations: string[]; +} +declare abstract class Base_Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B { + inputs: Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Input; + postProcessedOutputs: Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B_Output; +} +type Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Input = Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Async_Batch; +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_1; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_1 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Async_Batch { + requests: (Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt_1 | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages_1)[]; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Prompt_1 { + /** + * The input text prompt for the model to generate a response. + */ + prompt: string; + /** + * Name of the LoRA (Low-Rank Adaptation) model to fine-tune the base model. + */ + lora?: string; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_2; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_2 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Messages_1 { + /** + * An array of message objects representing the conversation history. + */ + messages: { + /** + * The role of the message sender (e.g., 'user', 'assistant', 'system', 'tool'). + */ + role: string; + content: string | { + /** + * Type of the content (text) + */ + type?: string; + /** + * Text content + */ + text?: string; + }[]; + }[]; + functions?: { + name: string; + code: string; + }[]; + /** + * A list of tools available for the assistant to use. + */ + tools?: ({ + /** + * The name of the tool. More descriptive the better. + */ + name: string; + /** + * A brief description of what the tool does. + */ + description: string; + /** + * Schema defining the parameters accepted by the tool. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + } | { + /** + * Specifies the type of tool (e.g., 'function'). + */ + type: string; + /** + * Details of the function tool. + */ + function: { + /** + * The name of the function. + */ + name: string; + /** + * A brief description of what the function does. + */ + description: string; + /** + * Schema defining the parameters accepted by the function. + */ + parameters: { + /** + * The type of the parameters object (usually 'object'). + */ + type: string; + /** + * List of required parameter names. + */ + required?: string[]; + /** + * Definitions of each parameter. + */ + properties: { + [k: string]: { + /** + * The data type of the parameter. + */ + type: string; + /** + * A description of the expected parameter. + */ + description: string; + }; + }; + }; + }; + })[]; + response_format?: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_3; + /** + * If true, a chat template is not applied and you must adhere to the specific model's expected formatting. + */ + raw?: boolean; + /** + * If true, the response will be streamed back incrementally using SSE, Server Sent Events. + */ + stream?: boolean; + /** + * The maximum number of tokens to generate in the response. + */ + max_tokens?: number; + /** + * Controls the randomness of the output; higher values produce more random results. + */ + temperature?: number; + /** + * Adjusts the creativity of the AI's responses by controlling how many possible words it considers. Lower values make outputs more predictable; higher values allow for more varied and creative responses. + */ + top_p?: number; + /** + * Limits the AI to choose from the top 'k' most probable words. Lower values make responses more focused; higher values introduce more variety and potential surprises. + */ + top_k?: number; + /** + * Random seed for reproducibility of the generation. + */ + seed?: number; + /** + * Penalty for repeated tokens; higher values discourage repetition. + */ + repetition_penalty?: number; + /** + * Decreases the likelihood of the model repeating the same lines verbatim. + */ + frequency_penalty?: number; + /** + * Increases the likelihood of the model introducing new topics. + */ + presence_penalty?: number; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_JSON_Mode_3 { + type?: "json_object" | "json_schema"; + json_schema?: unknown; +} +type Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Output = Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Chat_Completion_Response | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Text_Completion_Response | string | Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_AsyncResponse; +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Chat_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "chat.completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index?: number; + /** + * The message generated by the model + */ + message?: { + /** + * Role of the message author + */ + role: string; + /** + * The content of the message + */ + content: string; + /** + * Internal reasoning content (if available) + */ + reasoning_content?: string; + /** + * Tool calls made by the assistant + */ + tool_calls?: { + /** + * Unique identifier for the tool call + */ + id: string; + /** + * Type of tool call + */ + type: "function"; + function: { + /** + * Name of the function to call + */ + name: string; + /** + * JSON string of arguments for the function + */ + arguments: string; + }; + }[]; + }; + /** + * Reason why the model stopped generating + */ + finish_reason?: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Text_Completion_Response { + /** + * Unique identifier for the completion + */ + id?: string; + /** + * Object type identifier + */ + object?: "text_completion"; + /** + * Unix timestamp of when the completion was created + */ + created?: number; + /** + * Model used for the completion + */ + model?: string; + /** + * List of completion choices + */ + choices?: { + /** + * Index of the choice in the list + */ + index: number; + /** + * The generated text completion + */ + text: string; + /** + * Reason why the model stopped generating + */ + finish_reason: string; + /** + * Stop reason (may be null) + */ + stop_reason?: string | null; + /** + * Log probabilities (if requested) + */ + logprobs?: {} | null; + /** + * Log probabilities for the prompt (if requested) + */ + prompt_logprobs?: {} | null; + }[]; + /** + * Usage statistics for the inference request + */ + usage?: { + /** + * Total number of tokens in input + */ + prompt_tokens?: number; + /** + * Total number of tokens in output + */ + completion_tokens?: number; + /** + * Total number of input and output tokens + */ + total_tokens?: number; + }; +} +interface Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_AsyncResponse { + /** + * The async request id that can be used to obtain the results. + */ + request_id?: string; +} +declare abstract class Base_Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It { + inputs: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Input; + postProcessedOutputs: Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It_Output; +} +interface Ai_Cf_Pfnet_Plamo_Embedding_1B_Input { + /** + * Input text to embed. Can be a single string or a list of strings. + */ + text: string | string[]; +} +interface Ai_Cf_Pfnet_Plamo_Embedding_1B_Output { + /** + * Embedding vectors, where each vector is a list of floats. + */ + data: number[][]; + /** + * Shape of the embedding data as [number_of_embeddings, embedding_dimension]. + * + * @minItems 2 + * @maxItems 2 + */ + shape: [ + number, + number + ]; +} +declare abstract class Base_Ai_Cf_Pfnet_Plamo_Embedding_1B { + inputs: Ai_Cf_Pfnet_Plamo_Embedding_1B_Input; + postProcessedOutputs: Ai_Cf_Pfnet_Plamo_Embedding_1B_Output; +} +interface Ai_Cf_Deepgram_Flux_Input { + /** + * Encoding of the audio stream. Currently only supports raw signed little-endian 16-bit PCM. + */ + encoding: "linear16"; + /** + * Sample rate of the audio stream in Hz. + */ + sample_rate: string; + /** + * End-of-turn confidence required to fire an eager end-of-turn event. When set, enables EagerEndOfTurn and TurnResumed events. Valid Values 0.3 - 0.9. + */ + eager_eot_threshold?: string; + /** + * End-of-turn confidence required to finish a turn. Valid Values 0.5 - 0.9. + */ + eot_threshold?: string; + /** + * A turn will be finished when this much time has passed after speech, regardless of EOT confidence. + */ + eot_timeout_ms?: string; + /** + * Keyterm prompting can improve recognition of specialized terminology. Pass multiple keyterm query parameters to boost multiple keyterms. + */ + keyterm?: string; + /** + * Opts out requests from the Deepgram Model Improvement Program. Refer to Deepgram Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip + */ + mip_opt_out?: "true" | "false"; + /** + * Label your requests for the purpose of identification during usage reporting + */ + tag?: string; +} +/** + * Output will be returned as websocket messages. + */ +interface Ai_Cf_Deepgram_Flux_Output { + /** + * The unique identifier of the request (uuid) + */ + request_id?: string; + /** + * Starts at 0 and increments for each message the server sends to the client. + */ + sequence_id?: number; + /** + * The type of event being reported. + */ + event?: "Update" | "StartOfTurn" | "EagerEndOfTurn" | "TurnResumed" | "EndOfTurn"; + /** + * The index of the current turn + */ + turn_index?: number; + /** + * Start time in seconds of the audio range that was transcribed + */ + audio_window_start?: number; + /** + * End time in seconds of the audio range that was transcribed + */ + audio_window_end?: number; + /** + * Text that was said over the course of the current turn + */ + transcript?: string; + /** + * The words in the transcript + */ + words?: { + /** + * The individual punctuated, properly-cased word from the transcript + */ + word: string; + /** + * Confidence that this word was transcribed correctly + */ + confidence: number; + }[]; + /** + * Confidence that no more speech is coming in this turn + */ + end_of_turn_confidence?: number; +} +declare abstract class Base_Ai_Cf_Deepgram_Flux { + inputs: Ai_Cf_Deepgram_Flux_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Flux_Output; +} +interface Ai_Cf_Deepgram_Aura_2_En_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "amalthea" | "andromeda" | "apollo" | "arcas" | "aries" | "asteria" | "athena" | "atlas" | "aurora" | "callista" | "cora" | "cordelia" | "delia" | "draco" | "electra" | "harmonia" | "helena" | "hera" | "hermes" | "hyperion" | "iris" | "janus" | "juno" | "jupiter" | "luna" | "mars" | "minerva" | "neptune" | "odysseus" | "ophelia" | "orion" | "orpheus" | "pandora" | "phoebe" | "pluto" | "saturn" | "thalia" | "theia" | "vesta" | "zeus"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_2_En_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_2_En { + inputs: Ai_Cf_Deepgram_Aura_2_En_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_2_En_Output; +} +interface Ai_Cf_Deepgram_Aura_2_Es_Input { + /** + * Speaker used to produce the audio. + */ + speaker?: "sirio" | "nestor" | "carina" | "celeste" | "alvaro" | "diana" | "aquila" | "selena" | "estrella" | "javier"; + /** + * Encoding of the output audio. + */ + encoding?: "linear16" | "flac" | "mulaw" | "alaw" | "mp3" | "opus" | "aac"; + /** + * Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.. + */ + container?: "none" | "wav" | "ogg"; + /** + * The text content to be converted to speech + */ + text: string; + /** + * Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable + */ + sample_rate?: number; + /** + * The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type. + */ + bit_rate?: number; +} +/** + * The generated audio in MP3 format + */ +type Ai_Cf_Deepgram_Aura_2_Es_Output = string; +declare abstract class Base_Ai_Cf_Deepgram_Aura_2_Es { + inputs: Ai_Cf_Deepgram_Aura_2_Es_Input; + postProcessedOutputs: Ai_Cf_Deepgram_Aura_2_Es_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Dev { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Dev_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B_Output; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Input { + multipart: { + body?: object; + contentType?: string; + }; +} +interface Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Output { + /** + * Generated image as Base64 string. + */ + image?: string; +} +declare abstract class Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B { + inputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Input; + postProcessedOutputs: Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B_Output; +} +declare abstract class Base_Ai_Cf_Zai_Org_Glm_4_7_Flash { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { + inputs: ChatCompletionsInput; + postProcessedOutputs: ChatCompletionsOutput; +} +interface AiModels { + "@cf/huggingface/distilbert-sst-2-int8": BaseAiTextClassification; + "@cf/stabilityai/stable-diffusion-xl-base-1.0": BaseAiTextToImage; + "@cf/runwayml/stable-diffusion-v1-5-inpainting": BaseAiTextToImage; + "@cf/runwayml/stable-diffusion-v1-5-img2img": BaseAiTextToImage; + "@cf/lykon/dreamshaper-8-lcm": BaseAiTextToImage; + "@cf/bytedance/stable-diffusion-xl-lightning": BaseAiTextToImage; + "@cf/myshell-ai/melotts": BaseAiTextToSpeech; + "@cf/google/embeddinggemma-300m": BaseAiTextEmbeddings; + "@cf/microsoft/resnet-50": BaseAiImageClassification; + "@cf/meta/llama-2-7b-chat-int8": BaseAiTextGeneration; + "@cf/mistral/mistral-7b-instruct-v0.1": BaseAiTextGeneration; + "@cf/meta/llama-2-7b-chat-fp16": BaseAiTextGeneration; + "@hf/thebloke/llama-2-13b-chat-awq": BaseAiTextGeneration; + "@hf/thebloke/mistral-7b-instruct-v0.1-awq": BaseAiTextGeneration; + "@hf/thebloke/zephyr-7b-beta-awq": BaseAiTextGeneration; + "@hf/thebloke/openhermes-2.5-mistral-7b-awq": BaseAiTextGeneration; + "@hf/thebloke/neural-chat-7b-v3-1-awq": BaseAiTextGeneration; + "@hf/thebloke/deepseek-coder-6.7b-base-awq": BaseAiTextGeneration; + "@hf/thebloke/deepseek-coder-6.7b-instruct-awq": BaseAiTextGeneration; + "@cf/deepseek-ai/deepseek-math-7b-instruct": BaseAiTextGeneration; + "@cf/defog/sqlcoder-7b-2": BaseAiTextGeneration; + "@cf/openchat/openchat-3.5-0106": BaseAiTextGeneration; + "@cf/tiiuae/falcon-7b-instruct": BaseAiTextGeneration; + "@cf/thebloke/discolm-german-7b-v1-awq": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-0.5b-chat": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-7b-chat-awq": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-14b-chat-awq": BaseAiTextGeneration; + "@cf/tinyllama/tinyllama-1.1b-chat-v1.0": BaseAiTextGeneration; + "@cf/microsoft/phi-2": BaseAiTextGeneration; + "@cf/qwen/qwen1.5-1.8b-chat": BaseAiTextGeneration; + "@cf/mistral/mistral-7b-instruct-v0.2-lora": BaseAiTextGeneration; + "@hf/nousresearch/hermes-2-pro-mistral-7b": BaseAiTextGeneration; + "@hf/nexusflow/starling-lm-7b-beta": BaseAiTextGeneration; + "@hf/google/gemma-7b-it": BaseAiTextGeneration; + "@cf/meta-llama/llama-2-7b-chat-hf-lora": BaseAiTextGeneration; + "@cf/google/gemma-2b-it-lora": BaseAiTextGeneration; + "@cf/google/gemma-7b-it-lora": BaseAiTextGeneration; + "@hf/mistral/mistral-7b-instruct-v0.2": BaseAiTextGeneration; + "@cf/meta/llama-3-8b-instruct": BaseAiTextGeneration; + "@cf/fblgit/una-cybertron-7b-v2-bf16": BaseAiTextGeneration; + "@cf/meta/llama-3-8b-instruct-awq": BaseAiTextGeneration; + "@cf/meta/llama-3.1-8b-instruct-fp8": BaseAiTextGeneration; + "@cf/meta/llama-3.1-8b-instruct-awq": BaseAiTextGeneration; + "@cf/meta/llama-3.2-3b-instruct": BaseAiTextGeneration; + "@cf/meta/llama-3.2-1b-instruct": BaseAiTextGeneration; + "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b": BaseAiTextGeneration; + "@cf/ibm-granite/granite-4.0-h-micro": BaseAiTextGeneration; + "@cf/facebook/bart-large-cnn": BaseAiSummarization; + "@cf/llava-hf/llava-1.5-7b-hf": BaseAiImageToText; + "@cf/baai/bge-base-en-v1.5": Base_Ai_Cf_Baai_Bge_Base_En_V1_5; + "@cf/openai/whisper": Base_Ai_Cf_Openai_Whisper; + "@cf/meta/m2m100-1.2b": Base_Ai_Cf_Meta_M2M100_1_2B; + "@cf/baai/bge-small-en-v1.5": Base_Ai_Cf_Baai_Bge_Small_En_V1_5; + "@cf/baai/bge-large-en-v1.5": Base_Ai_Cf_Baai_Bge_Large_En_V1_5; + "@cf/unum/uform-gen2-qwen-500m": Base_Ai_Cf_Unum_Uform_Gen2_Qwen_500M; + "@cf/openai/whisper-tiny-en": Base_Ai_Cf_Openai_Whisper_Tiny_En; + "@cf/openai/whisper-large-v3-turbo": Base_Ai_Cf_Openai_Whisper_Large_V3_Turbo; + "@cf/baai/bge-m3": Base_Ai_Cf_Baai_Bge_M3; + "@cf/black-forest-labs/flux-1-schnell": Base_Ai_Cf_Black_Forest_Labs_Flux_1_Schnell; + "@cf/meta/llama-3.2-11b-vision-instruct": Base_Ai_Cf_Meta_Llama_3_2_11B_Vision_Instruct; + "@cf/meta/llama-3.3-70b-instruct-fp8-fast": Base_Ai_Cf_Meta_Llama_3_3_70B_Instruct_Fp8_Fast; + "@cf/meta/llama-guard-3-8b": Base_Ai_Cf_Meta_Llama_Guard_3_8B; + "@cf/baai/bge-reranker-base": Base_Ai_Cf_Baai_Bge_Reranker_Base; + "@cf/qwen/qwen2.5-coder-32b-instruct": Base_Ai_Cf_Qwen_Qwen2_5_Coder_32B_Instruct; + "@cf/qwen/qwq-32b": Base_Ai_Cf_Qwen_Qwq_32B; + "@cf/mistralai/mistral-small-3.1-24b-instruct": Base_Ai_Cf_Mistralai_Mistral_Small_3_1_24B_Instruct; + "@cf/google/gemma-3-12b-it": Base_Ai_Cf_Google_Gemma_3_12B_It; + "@cf/meta/llama-4-scout-17b-16e-instruct": Base_Ai_Cf_Meta_Llama_4_Scout_17B_16E_Instruct; + "@cf/qwen/qwen3-30b-a3b-fp8": Base_Ai_Cf_Qwen_Qwen3_30B_A3B_Fp8; + "@cf/deepgram/nova-3": Base_Ai_Cf_Deepgram_Nova_3; + "@cf/qwen/qwen3-embedding-0.6b": Base_Ai_Cf_Qwen_Qwen3_Embedding_0_6B; + "@cf/pipecat-ai/smart-turn-v2": Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2; + "@cf/openai/gpt-oss-120b": Base_Ai_Cf_Openai_Gpt_Oss_120B; + "@cf/openai/gpt-oss-20b": Base_Ai_Cf_Openai_Gpt_Oss_20B; + "@cf/leonardo/phoenix-1.0": Base_Ai_Cf_Leonardo_Phoenix_1_0; + "@cf/leonardo/lucid-origin": Base_Ai_Cf_Leonardo_Lucid_Origin; + "@cf/deepgram/aura-1": Base_Ai_Cf_Deepgram_Aura_1; + "@cf/ai4bharat/indictrans2-en-indic-1B": Base_Ai_Cf_Ai4Bharat_Indictrans2_En_Indic_1B; + "@cf/aisingapore/gemma-sea-lion-v4-27b-it": Base_Ai_Cf_Aisingapore_Gemma_Sea_Lion_V4_27B_It; + "@cf/pfnet/plamo-embedding-1b": Base_Ai_Cf_Pfnet_Plamo_Embedding_1B; + "@cf/deepgram/flux": Base_Ai_Cf_Deepgram_Flux; + "@cf/deepgram/aura-2-en": Base_Ai_Cf_Deepgram_Aura_2_En; + "@cf/deepgram/aura-2-es": Base_Ai_Cf_Deepgram_Aura_2_Es; + "@cf/black-forest-labs/flux-2-dev": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Dev; + "@cf/black-forest-labs/flux-2-klein-4b": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_4B; + "@cf/black-forest-labs/flux-2-klein-9b": Base_Ai_Cf_Black_Forest_Labs_Flux_2_Klein_9B; + "@cf/zai-org/glm-4.7-flash": Base_Ai_Cf_Zai_Org_Glm_4_7_Flash; + "@cf/moonshotai/kimi-k2.5": Base_Ai_Cf_Moonshotai_Kimi_K2_5; + "@cf/moonshotai/kimi-k2.6": Base_Ai_Cf_Moonshotai_Kimi_K2_6; + "@cf/nvidia/nemotron-3-120b-a12b": Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B; + "@cf/google/gemma-4-26b-a4b-it": Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT; +} +type AiOptions = { + /** + * Send requests as an asynchronous batch job, only works for supported models + * https://developers.cloudflare.com/workers-ai/features/batch-api + */ + queueRequest?: boolean; + /** + * Establish websocket connections, only works for supported models + */ + websocket?: boolean; + /** + * Tag your requests to group and view them in Cloudflare dashboard. + * + * Rules: + * Tags must only contain letters, numbers, and the symbols: : - . / @ + * Each tag can have maximum 50 characters. + * Maximum 5 tags are allowed each request. + * Duplicate tags will removed. + */ + tags?: string[]; + gateway?: GatewayOptions; + returnRawResponse?: boolean; + prefix?: string; + extraHeaders?: object; + signal?: AbortSignal; +}; +type AiModelsSearchParams = { + author?: string; + hide_experimental?: boolean; + page?: number; + per_page?: number; + search?: string; + source?: number; + task?: string; +}; +type AiModelsSearchObject = { + id: string; + source: number; + name: string; + description: string; + task: { + id: string; + name: string; + description: string; + }; + tags: string[]; + properties: { + property_id: string; + value: string; + }[]; +}; +type ChatCompletionsBase = ChatCompletionsMessagesInput; +type ChatCompletionsInput = ChatCompletionsMessagesInput; +interface InferenceUpstreamError extends Error { +} +interface AiInternalError extends Error { +} +type AiModelListType = Record; +type AiAsyncBatchResponse = { + request_id: string; +}; +declare abstract class Ai { + aiGatewayLogId: string | null; + gateway(gatewayId: string): AiGateway; + /** + * @deprecated Use the standalone `ai_search_namespaces` or `ai_search` Workers bindings instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(): AiSearchNamespace; + /** + * @deprecated AutoRAG has been replaced by AI Search. + * Use the standalone `ai_search_namespaces` or `ai_search` Workers bindings instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + * + * @param autoragId Instance ID + */ + autorag(autoragId: string): AutoRAG; + // Batch request + run(model: Name, inputs: { + requests: AiModelList[Name]['inputs'][]; + }, options: AiOptions & { + queueRequest: true; + }): Promise; + // Raw response + run(model: Name, inputs: AiModelList[Name]['inputs'], options: AiOptions & { + returnRawResponse: true; + }): Promise; + // WebSocket + run(model: Name, inputs: AiModelList[Name]['inputs'], options: AiOptions & { + websocket: true; + }): Promise; + // Streaming + run(model: Name, inputs: AiModelList[Name]['inputs'] & { + stream: true; + }, options?: AiOptions): Promise; + // Normal (default) - known model + run(model: Name, inputs: AiModelList[Name]['inputs'], options?: AiOptions): Promise; + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run(model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions): Promise>; + models(params?: AiModelsSearchParams): Promise; + toMarkdown(): ToMarkdownService; + toMarkdown(files: MarkdownDocument[], options?: ConversionRequestOptions): Promise; + toMarkdown(files: MarkdownDocument, options?: ConversionRequestOptions): Promise; +} +type GatewayRetries = { + maxAttempts?: 1 | 2 | 3 | 4 | 5; + retryDelayMs?: number; + backoff?: 'constant' | 'linear' | 'exponential'; +}; +type GatewayOptions = { + id: string; + cacheKey?: string; + cacheTtl?: number; + skipCache?: boolean; + metadata?: Record; + collectLog?: boolean; + eventId?: string; + requestTimeoutMs?: number; + retries?: GatewayRetries; +}; +type UniversalGatewayOptions = Exclude & { + /** + ** @deprecated + */ + id?: string; +}; +type AiGatewayPatchLog = { + score?: number | null; + feedback?: -1 | 1 | null; + metadata?: Record | null; +}; +type AiGatewayLog = { + id: string; + provider: string; + model: string; + model_type?: string; + path: string; + duration: number; + request_type?: string; + request_content_type?: string; + status_code: number; + response_content_type?: string; + success: boolean; + cached: boolean; + tokens_in?: number; + tokens_out?: number; + metadata?: Record; + step?: number; + cost?: number; + custom_cost?: boolean; + request_size: number; + request_head?: string; + request_head_complete: boolean; + response_size: number; + response_head?: string; + response_head_complete: boolean; + created_at: Date; +}; +type AIGatewayProviders = 'workers-ai' | 'anthropic' | 'aws-bedrock' | 'azure-openai' | 'google-vertex-ai' | 'huggingface' | 'openai' | 'perplexity-ai' | 'replicate' | 'groq' | 'cohere' | 'google-ai-studio' | 'mistral' | 'grok' | 'openrouter' | 'deepseek' | 'cerebras' | 'cartesia' | 'elevenlabs' | 'adobe-firefly'; +type AIGatewayHeaders = { + 'cf-aig-metadata': Record | string; + 'cf-aig-custom-cost': { + per_token_in?: number; + per_token_out?: number; + } | { + total_cost?: number; + } | string; + 'cf-aig-cache-ttl': number | string; + 'cf-aig-skip-cache': boolean | string; + 'cf-aig-cache-key': string; + 'cf-aig-event-id': string; + 'cf-aig-request-timeout': number | string; + 'cf-aig-max-attempts': number | string; + 'cf-aig-retry-delay': number | string; + 'cf-aig-backoff': string; + 'cf-aig-collect-log': boolean | string; + Authorization: string; + 'Content-Type': string; + [key: string]: string | number | boolean | object; +}; +type AIGatewayUniversalRequest = { + provider: AIGatewayProviders | string; // eslint-disable-line + endpoint: string; + headers: Partial; + query: unknown; +}; +interface AiGatewayInternalError extends Error { +} +interface AiGatewayLogNotFound extends Error { +} +declare abstract class AiGateway { + patchLog(logId: string, data: AiGatewayPatchLog): Promise; + getLog(logId: string): Promise; + run(data: AIGatewayUniversalRequest | AIGatewayUniversalRequest[], options?: { + gateway?: UniversalGatewayOptions; + extraHeaders?: object; + signal?: AbortSignal; + }): Promise; + getUrl(provider?: AIGatewayProviders | string): Promise; // eslint-disable-line +} +// Copyright (c) 2022-2025 Cloudflare, Inc. +// Licensed under the Apache 2.0 license found in the LICENSE file or at: +// https://opensource.org/licenses/Apache-2.0 +/** + * Artifacts — Git-compatible file storage on Cloudflare Workers. + * + * Provides programmatic access to create, manage, and fork repositories, + * and to issue and revoke scoped access tokens. + */ +/** Information about a repository. */ +interface ArtifactsRepoInfo { + /** Unique repository ID. */ + id: string; + /** Repository name. */ + name: string; + /** Repository description, or null if not set. */ + description: string | null; + /** Default branch name (e.g. "main"). */ + defaultBranch: string; + /** ISO 8601 creation timestamp. */ + createdAt: string; + /** ISO 8601 last-updated timestamp. */ + updatedAt: string; + /** ISO 8601 timestamp of the last push, or null if never pushed. */ + lastPushAt: string | null; + /** Fork source (e.g. "github:owner/repo", "artifacts:namespace/repo"), or null if not a fork. */ + source: string | null; + /** Whether the repository is read-only. */ + readOnly: boolean; + /** HTTPS git remote URL. */ + remote: string; +} +/** Result of creating a repository — includes the initial access token. */ +interface ArtifactsCreateRepoResult { + /** Unique repository ID. */ + id: string; + /** Repository name. */ + name: string; + /** Repository description, or null if not set. */ + description: string | null; + /** Default branch name. */ + defaultBranch: string; + /** HTTPS git remote URL. */ + remote: string; + /** Plaintext access token (only returned at creation time). */ + token: string; + /** ISO 8601 token expiry timestamp. */ + tokenExpiresAt: string; +} +/** Paginated list of repositories. */ +interface ArtifactsRepoListResult { + /** Repositories in this page (without the `remote` field). */ + repos: Omit[]; + /** Total number of repositories in the namespace. */ + total: number; + /** Cursor for the next page, if there are more results. */ + cursor?: string; +} +/** Result of creating an access token. */ +interface ArtifactsCreateTokenResult { + /** Unique token ID. */ + id: string; + /** Plaintext token (only returned at creation time). */ + plaintext: string; + /** Token scope: "read" or "write". */ + scope: 'read' | 'write'; + /** ISO 8601 token expiry timestamp. */ + expiresAt: string; +} +/** Token metadata (no plaintext). */ +interface ArtifactsTokenInfo { + /** Unique token ID. */ + id: string; + /** Token scope: "read" or "write". */ + scope: 'read' | 'write'; + /** Token state: "active", "expired", or "revoked". */ + state: 'active' | 'expired' | 'revoked'; + /** ISO 8601 creation timestamp. */ + createdAt: string; + /** ISO 8601 expiry timestamp. */ + expiresAt: string; +} +/** Paginated list of tokens for a repository. */ +interface ArtifactsTokenListResult { + /** Tokens in this page. */ + tokens: ArtifactsTokenInfo[]; + /** Total number of tokens for the repository. */ + total: number; +} +/** + * Handle for a single repository. Returned by Artifacts.get(). + * + * Methods may throw `ArtifactsError` with code `INTERNAL_ERROR` if an unexpected service error occurs. + */ +interface ArtifactsRepo extends ArtifactsRepoInfo { + /** + * Create an access token for this repo. + * @param scope Token scope: "write" (default) or "read". + * @param ttl Time-to-live in seconds (default 86400, min 60, max 31536000). + * @throws {ArtifactsError} with code `INVALID_TTL` if ttl is out of range. + */ + createToken(scope?: 'write' | 'read', ttl?: number): Promise; + /** List tokens for this repo (metadata only, no plaintext). */ + listTokens(): Promise; + /** + * Revoke a token by plaintext or ID. + * @param tokenOrId Plaintext token or token ID. + * @returns true if revoked, false if not found. + * @throws {ArtifactsError} with code `INVALID_INPUT` if tokenOrId is empty. + */ + revokeToken(tokenOrId: string): Promise; + // ── Fork ── + /** + * Fork this repo to a new repo. + * @param name Target repository name. + * @param opts Optional: description, readOnly flag, defaultBranchOnly (default true). + * @throws {ArtifactsError} with code `INVALID_REPO_NAME` if name is invalid. + * @throws {ArtifactsError} with code `ALREADY_EXISTS` if the target repo already exists. + * @throws {ArtifactsError} with code `FORK_IN_PROGRESS` if a fork is already running. + */ + fork(name: string, opts?: { + description?: string; + readOnly?: boolean; + defaultBranchOnly?: boolean; + }): Promise; +} +// ── Error types ────────────────────────────────────────────────────────────── +/** + * Error codes returned by Artifacts binding operations. + * + * Each code maps to a numeric code available on `ArtifactsError.numericCode`. + */ +type ArtifactsErrorCode = 'ALREADY_EXISTS' | 'NOT_FOUND' | 'IMPORT_IN_PROGRESS' | 'FORK_IN_PROGRESS' | 'INVALID_INPUT' | 'INVALID_REPO_NAME' | 'INVALID_TTL' | 'INVALID_URL' | 'REMOTE_AUTH_REQUIRED' | 'UPSTREAM_UNAVAILABLE' | 'MEMORY_LIMIT' | 'INTERNAL_ERROR'; +/** + * Error thrown by Artifacts binding operations. + * + * Uses a string `.code` discriminator following the Cloudflare platform + * convention (StreamError, ImagesError, etc.). The `.numericCode` matches + * the REST API `errors[].code` values. + */ +interface ArtifactsError extends Error { + readonly name: 'ArtifactsError'; + /** String error code for programmatic matching. */ + readonly code: ArtifactsErrorCode; + /** Numeric error code matching the REST API. */ + readonly numericCode: number; +} +// ── Binding ────────────────────────────────────────────────────────────────── +/** + * Artifacts binding — namespace-level operations. + * + * Methods may throw `ArtifactsError` with code `INTERNAL_ERROR` if an unexpected service error occurs. + */ +interface Artifacts { + /** + * Create a new repository with an initial access token. + * @param name Repository name (alphanumeric, dots, hyphens, underscores). + * @param opts Optional: readOnly flag, description, default branch name. + * @returns Repo metadata with initial token. + * @throws {ArtifactsError} with code `INVALID_REPO_NAME` if name is invalid. + * @throws {ArtifactsError} with code `ALREADY_EXISTS` if the repo already exists. + */ + create(name: string, opts?: { + readOnly?: boolean; + description?: string; + setDefaultBranch?: string; + }): Promise; + /** + * Get a handle to an existing repository. + * @param name Repository name. + * @returns Repo handle. + * @throws {ArtifactsError} with code `NOT_FOUND` if the repo does not exist. + * @throws {ArtifactsError} with code `IMPORT_IN_PROGRESS` if the repo is still importing. + * @throws {ArtifactsError} with code `FORK_IN_PROGRESS` if the repo is still forking. + */ + get(name: string): Promise; + /** + * Import a repository from an external git remote. + * @param params Source URL and optional branch/depth, plus target name and options. + * @returns Repo metadata with initial token. + * @throws {ArtifactsError} with code `INVALID_REPO_NAME` if the target name is invalid. + * @throws {ArtifactsError} with code `INVALID_INPUT` if the source URL is not valid HTTPS. + * @throws {ArtifactsError} with code `INVALID_URL` if the source URL does not point to a git repository. + * @throws {ArtifactsError} with code `REMOTE_AUTH_REQUIRED` if the remote requires authentication. + * @throws {ArtifactsError} with code `NOT_FOUND` if the remote repository does not exist. + * @throws {ArtifactsError} with code `UPSTREAM_UNAVAILABLE` if the remote cannot be reached. + * @throws {ArtifactsError} with code `MEMORY_LIMIT` if the import exceeds service memory limits. + * @throws {ArtifactsError} with code `ALREADY_EXISTS` if the target repo already exists. + */ + import(params: { + source: { + url: string; + branch?: string; + depth?: number; + }; + target: { + name: string; + opts?: { + description?: string; + readOnly?: boolean; + }; + }; + }): Promise; + /** + * List repositories with cursor-based pagination. + * @param opts Optional: limit (1–200, default 50), cursor for next page. + */ + list(opts?: { + limit?: number; + cursor?: string; + }): Promise; + /** + * Delete a repository and all associated tokens. + * @param name Repository name. + * @returns true if deleted, false if not found. + * @throws {ArtifactsError} with code `INVALID_REPO_NAME` if name is invalid. + */ + delete(name: string): Promise; +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGInternalError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGNotFoundError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGUnauthorizedError extends Error { +} +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +interface AutoRAGNameNotSetError extends Error { +} +type ComparisonFilter = { + key: string; + type: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte'; + value: string | number | boolean; +}; +type CompoundFilter = { + type: 'and' | 'or'; + filters: ComparisonFilter[]; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagSearchRequest = { + query: string; + filters?: CompoundFilter | ComparisonFilter; + max_num_results?: number; + ranking_options?: { + ranker?: string; + score_threshold?: number; + }; + reranking?: { + enabled?: boolean; + model?: string; + }; + rewrite_query?: boolean; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchRequest = AutoRagSearchRequest & { + stream?: boolean; + system_prompt?: string; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchRequestStreaming = Omit & { + stream: true; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagSearchResponse = { + object: 'vector_store.search_results.page'; + search_query: string; + data: { + file_id: string; + filename: string; + score: number; + attributes: Record; + content: { + type: 'text'; + text: string; + }[]; + }[]; + has_more: boolean; + next_page: string | null; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagListResponse = { + id: string; + enable: boolean; + type: string; + source: string; + vectorize_name: string; + paused: boolean; + status: string; +}[]; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +type AutoRagAiSearchResponse = AutoRagSearchResponse & { + response: string; +}; +/** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ +declare abstract class AutoRAG { + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + list(): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + search(params: AutoRagSearchRequest): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequestStreaming): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequest): Promise; + /** + * @deprecated Use the standalone AI Search Workers binding instead. + * See https://developers.cloudflare.com/ai-search/usage/workers-binding/ + */ + aiSearch(params: AutoRagAiSearchRequest): Promise; +} +type BrowserRunLifecycleEvent = 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2'; +type BrowserRunResourceType = 'document' | 'stylesheet' | 'image' | 'media' | 'font' | 'script' | 'texttrack' | 'xhr' | 'fetch' | 'prefetch' | 'eventsource' | 'websocket' | 'manifest' | 'signedexchange' | 'ping' | 'cspviolationreport' | 'preflight' | 'other'; +/** Options fields shared by all quick actions. */ +interface BrowserRunBaseOptions { + /** Adds `