import { z } from "zod"; /** * Canonical virtual path stored and exchanged by adapters. * * Public path-taking APIs also accept relative and non-canonical input because * `normalizePath()` resolves it first. `PathSchema` is for already-normalized * values at persistence and adapter seams. */ export const PathSchema: z.ZodType = z.string().refine( (value: string) => value === "/" || ( value.startsWith("/") && !value.endsWith("/") && !value.includes("//") && !value.includes("\\") && !value.includes("\0") && value.split("/").slice(1).every((part: string) => part.length > 0 && part !== "." && part !== "..") ), "Expected a canonical virtual filesystem path.", ); /** A validated canonical virtual filesystem path. */ export type PathType = import("./_schema_types.ts").PathType; /** Stable non-empty diagnostic name assigned to one adapter implementation. */ export const AdapterNameSchema: z.ZodType = z.string().min(1); /** A validated adapter diagnostic name. */ export type AdapterNameType = import("./_schema_types.ts").AdapterNameType; /** * Valid entry kinds exposed by the filesystem facade and every adapter. * * The package uses the same two kinds as the File System API. Adapters must not * invent a third kind for links, database rows, or provider-specific objects. */ export const EntryKindSchema: z.ZodType = z.enum(["file", "directory"]); /** A validated filesystem entry kind. */ export type EntryKindType = import("./_schema_types.ts").EntryKindType; /** * Execution contexts that can host browser storage access. * * `worker` is used only when the runtime exposes a generic worker shape but the * library cannot prove whether it is dedicated, shared, or service-worker * execution. `unknown` means that no supported browser execution context was * detected. */ export const OpfsContextSchema: z.ZodType = z.enum([ "window", "dedicated-worker", "shared-worker", "service-worker", "worker", "unknown", ]); /** A validated browser execution context classification. */ export type OpfsContextType = import("./_schema_types.ts").OpfsContextType; /** * Mutation coordination policies supported by {@link createFileSystem}. * * `auto` uses Web Locks when the current realm exposes them. Otherwise it uses * an in-realm FIFO lock. `none` disables library coordination and transfers all * concurrency responsibility to the caller or adapter. */ export const CoordinationModeSchema: z.ZodType = z.enum(["auto", "web-locks", "local", "none"]); /** A validated mutation coordination policy. */ export type CoordinationModeType = import("./_schema_types.ts").CoordinationModeType; /** * Write modes shared by the facade and adapters. * * `replace` starts from an empty file. `append` starts at the current end. * `update` preserves existing bytes and starts at the requested byte offset. */ export const WriteModeSchema: z.ZodType = z.enum(["replace", "append", "update"]); /** A validated file write mode. */ export type WriteModeType = import("./_schema_types.ts").WriteModeType; /** * How one operation is provided by the selected storage stack. * * `native` means the immediate backend performs the operation directly. * `emulated` means the facade composes weaker primitives. `partitioned` means * the logical operation is preserved by splitting one value into multiple * provider records or blocks. `unsupported` means no safe implementation is * available for the selected stack. */ export const SupportModeSchema: z.ZodType = z.enum(["native", "emulated", "partitioned", "unsupported"]); /** A validated storage support mode. */ export type SupportModeType = import("./_schema_types.ts").SupportModeType; /** * Metrics collection cost selected for one filesystem or protocol client. * * `basic` counts operations, bytes, failures, and chosen native/emulated paths. * `timing` also reads the monotonic clock around operations. `none` removes * metrics bookkeeping from hot paths when the caller is measuring raw overhead. */ export const MetricsModeSchema: z.ZodType = z.enum(["none", "basic", "timing"]); /** A validated metrics collection mode. */ export type MetricsModeType = import("./_schema_types.ts").MetricsModeType; /** * Physical partition policy for backends with a smaller value limit than the * logical file size the application wants to expose. */ export const PartitionModeSchema: z.ZodType = z.enum(["never", "auto", "always"]); /** A validated physical partition policy. */ export type PartitionModeType = import("./_schema_types.ts").PartitionModeType; /** * Inspectable physical layout used when one logical file spans provider values. * * `thresholdBytes` is the logical size where `auto` starts partitioning. When * omitted, callers can use `partBytes` as the conservative threshold. `stream` * means native stream writes use this layout so input size does not determine * facade memory growth. */ export const AdapterPartitionSchema: z.ZodType = z.object({ /** Partitioning policy selected for this adapter. */ mode: PartitionModeSchema, /** Physical part or block size used by the layout. */ partBytes: z.number().int().positive(), /** Logical size where `auto` starts partitioning when the adapter knows it. */ thresholdBytes: z.number().int().positive().optional(), /** Whether streamed writes already use the partitioned layout. */ stream: z.boolean().optional(), /** Maximum physical part or block count when the adapter knows it. */ maxParts: z.number().int().positive().optional(), /** Stable name of the physical layout strategy. */ layout: z.string().min(1), }).strict(); /** A validated physical partition layout. */ export type AdapterPartitionType = import("./_schema_types.ts").AdapterPartitionType; /** * Optional backend limits that can be inspected before work begins. * * Missing values mean the adapter cannot state a portable hard limit. They do * not mean unlimited. Provider-specific clients can expose additional limits * through their own public constants and request planners. */ export const AdapterLimitsSchema: z.ZodType = z.object({ /** Maximum logical file size accepted by this configured adapter. */ maxFileBytes: z.number().int().positive().optional(), /** Maximum materialized value accepted by one physical backend record. */ maxValueBytes: z.number().int().positive().optional(), /** Maximum serialized key size when the backend has one. */ maxKeyBytes: z.number().int().positive().optional(), /** Minimum legal provider part/block size when multipart work is used. */ minPartBytes: z.number().int().positive().optional(), /** Maximum legal provider part/block size. */ maxPartBytes: z.number().int().positive().optional(), /** Maximum provider part/block count for one logical object. */ maxParts: z.number().int().positive().optional(), /** Maximum useful provider concurrency known by this adapter. */ maxConcurrency: z.number().int().positive().optional(), /** Maximum bytes in one transactional/batched provider mutation. */ maxBatchBytes: z.number().int().positive().optional(), }).strict(); /** Portable hard limits known by one configured adapter. */ export type AdapterLimitsType = import("./_schema_types.ts").AdapterLimitsType; /** * Performance routes that the filesystem facade can deliberately bypass. * * Every field defaults to true. Disabling a route forces the semantically safe * fallback where one exists. This is useful for differential testing and for * applications that prefer a slower but more observable or more portable path. */ export const OptimizationSchema: z.ZodType = z.object({ /** Use adapter-native streaming reads instead of materialized `readFile()`. */ streamRead: z.boolean(), /** Use adapter-native streaming writes when the requested mode supports them. */ streamWrite: z.boolean(), /** Forward byte ranges directly instead of materializing and slicing locally. */ rangeRead: z.boolean(), /** Use adapter-native/server-side copy instead of read plus write. */ nativeCopy: z.boolean(), /** Use adapter-native move/rename instead of copy then remove. */ nativeMove: z.boolean(), }).strict(); /** Resolved performance-route policy for one filesystem facade. */ export type OptimizationType = import("./_schema_types.ts").OptimizationType; /** * Stable adapter capability description. * * These flags describe native adapter operations, not operations that the * facade can emulate. `streamWriteModes` is intentionally mode-specific: an * object store can stream a complete replacement while append/update still * require a read-modify-write cycle. `nativeCopy` identifies server-side or * host-native copy so the facade does not move bytes through JavaScript when * the backend can copy them directly. */ export const AdapterCapabilitiesSchema: z.ZodType = z.object({ /** Adapter can materialize file bytes through `readFile()`. */ read: z.boolean(), /** Adapter can commit materialized file bytes through `writeFile()`. */ write: z.boolean(), /** Adapter can open a native/bounded provider stream without facade materialization. */ streamRead: z.boolean(), /** Write modes that `writeStream()` can perform without facade materialization. */ streamWriteModes: z.array(WriteModeSchema).readonly(), /** Adapter can satisfy byte ranges without reading the complete file first. */ rangeRead: z.boolean(), /** Adapter can copy bytes without routing them through the filesystem facade. */ nativeCopy: z.boolean(), /** Adapter can move/rename through one backend-native operation. */ nativeMove: z.boolean(), /** Adapter exposes a long-lived asynchronous positional writer. */ positionalWrite: z.boolean(), /** Adapter exposes a synchronous random-access file resource. */ syncAccess: z.boolean(), }); /** Native operations implemented by one adapter. */ export type AdapterCapabilitiesType = import("./_schema_types.ts").AdapterCapabilitiesType; /** * Stable error categories exposed by the package. * * Adapters map runtime-specific failures into these categories so consumers do * not need separate branches for DOMException, Deno, Bun, Node, SQL, and * document-database error classes. */ export const ErrorCodeSchema: z.ZodType = z.enum([ "unavailable", "not-found", "already-exists", "type-mismatch", "invalid-path", "invalid-operation", "not-supported", "locked", "quota-exceeded", "permission-denied", "aborted", "too-large", "unknown", ]); /** A validated package error category. */ export type ErrorCodeType = import("./_schema_types.ts").ErrorCodeType; /** Version stored with record-backed filesystem entries. */ export const RecordVersionSchema: z.ZodType = z.literal(1); /** Persisted record format version. */ export type RecordVersionType = import("./_schema_types.ts").RecordVersionType; /** * Fields shared by every persisted record-store entry. * * The virtual path is the durable identity. `parent` is stored separately so * document and SQL backends can list one directory without scanning every * record or reconstructing parents from strings. */ const RecordBaseSchema = z.object({ /** Persistence format version used to reject incompatible record layouts. */ version: RecordVersionSchema, /** Canonical virtual path and durable logical record identity. */ path: PathSchema, /** Canonical direct-parent path indexed by listing-oriented backends. */ parent: PathSchema, /** Final path segment presented by directory iteration. */ name: z.string(), /** Last modification time represented as Unix epoch milliseconds. */ lastModified: z.number().int().nonnegative(), }); /** Persisted directory record used by record-store adapters. */ const DirectoryRecordSchemaDefinition = RecordBaseSchema.extend({ /** Discriminator that prevents a directory row from carrying file bytes. */ kind: z.literal("directory"), }); /** Persisted directory validator with an explicit public type boundary. */ export const DirectoryRecordSchema: z.ZodType = DirectoryRecordSchemaDefinition; /** A validated persisted directory record. */ export type DirectoryRecordType = import("./_schema_types.ts").DirectoryRecordType; /** Persisted file record used by record-store adapters. */ const FileRecordSchemaDefinition = RecordBaseSchema.extend({ /** Discriminator that selects the file-record branch. */ kind: z.literal("file"), /** Base64 file body used by JSON/document/SQL-compatible record stores. */ data: z.string(), /** Decoded byte length retained without re-decoding `data` during stat calls. */ size: z.number().int().nonnegative(), /** Media type retained by backends that can preserve file metadata. */ mediaType: z.string(), }); /** Persisted file validator with an explicit public type boundary. */ export const FileRecordSchema: z.ZodType = FileRecordSchemaDefinition; /** A validated persisted file record. */ export type FileRecordType = import("./_schema_types.ts").FileRecordType; /** * Persisted record format shared by RxDB, unstorage, db0, and Drizzle record drivers. * * File bytes use base64 text because every target ecosystem can preserve JSON * strings. This costs about one third more storage than raw bytes. Native file * adapters do not use this format. */ const RecordSchemaDefinition = z.discriminatedUnion("kind", [DirectoryRecordSchemaDefinition, FileRecordSchemaDefinition]); /** Persisted record validator with an explicit public type boundary. */ export const RecordSchema: z.ZodType = RecordSchemaDefinition; /** A validated record-store filesystem entry. */ export type RecordType = import("./_schema_types.ts").RecordType; /** SQL dialects currently exposed by db0's public Database contract. */ export const Db0DialectSchema: z.ZodType = z.enum(["mysql", "postgresql", "sqlite", "libsql"]); /** A validated db0 SQL dialect. */ export type Db0DialectType = import("./_schema_types.ts").Db0DialectType; /** Safe unqualified SQL identifier used for adapter-owned table names. */ export const SqlIdentifierSchema: z.ZodType = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/); /** A validated unqualified SQL identifier. */ export type SqlIdentifierType = import("./_schema_types.ts").SqlIdentifierType; /** Storage family owned by one backend driver. */ export const DriverKindSchema: z.ZodType = z.enum(["file", "record", "object"]); /** A validated backend driver family. */ export type DriverKindType = import("./_schema_types.ts").DriverKindType; /** Why one driver limit exists. */ export const LimitKindSchema: z.ZodType = z.enum(["hard", "policy", "dynamic"]); /** A validated limit kind. */ export type LimitKindType = import("./_schema_types.ts").LimitKindType; /** Layer that supplied one limit value. */ export const LimitSourceSchema: z.ZodType = z.enum(["provider", "implementation", "user", "probe"]); /** A validated limit source. */ export type LimitSourceType = import("./_schema_types.ts").LimitSourceType; /** Unit used by one numeric limit. */ export const LimitUnitSchema: z.ZodType = z.enum(["bytes", "count", "milliseconds", "operations"]); /** A validated limit unit. */ export type LimitUnitType = import("./_schema_types.ts").LimitUnitType; /** * One inspectable storage limit with explicit provenance. * * A hard provider limit is not interchangeable with a project safety policy or * a user-selected ceiling. `value` can be absent only for a dynamic limit whose * current value has not been probed. */ export const LimitSchema: z.ZodType = z.object({ /** Stable machine-readable limit code. */ code: z.string().min(1), /** Whether the limit is hard, policy-driven, or dynamic. */ kind: LimitKindSchema, /** Layer that supplied the limit value. */ source: LimitSourceSchema, /** Unit used by the numeric value. */ unit: LimitUnitSchema, /** Current numeric limit when known. */ value: z.number().nonnegative().optional(), /** Human-readable context for diagnostics. */ detail: z.string().min(1).optional(), }).strict().superRefine((value, ctx) => { if (value.value === undefined && value.kind !== "dynamic") { ctx.addIssue({ code: "custom", message: "Only dynamic limits can omit their current value." }); } }); /** A validated storage limit. */ export type LimitType = import("./_schema_types.ts").LimitType; /** Current state of one driver requirement. */ export const RequirementStateSchema: z.ZodType = z.enum(["available", "missing", "unknown"]); /** A validated requirement state. */ export type RequirementStateType = import("./_schema_types.ts").RequirementStateType; /** * One runtime, provider, permission, or configuration requirement. * * Definitions can report `unknown` before probing. Configured drivers should * prefer `available` or `missing` when the state is already known. */ export const RequirementSchema: z.ZodType = z.object({ /** Stable machine-readable requirement code. */ code: z.string().min(1), /** Current known availability state. */ state: RequirementStateSchema, /** Concrete reason when the requirement is missing. */ reason: z.string().min(1).optional(), }).strict().superRefine((value, ctx) => { if (value.state === "missing" && value.reason === undefined) { ctx.addIssue({ code: "custom", message: "Missing requirements need a concrete reason." }); } }); /** A validated driver requirement. */ export type RequirementType = import("./_schema_types.ts").RequirementType; /** Ownership state for one configured driver backend resource. */ export const DriverOwnershipSchema: z.ZodType = z.enum(["none", "borrowed", "owned"]); /** A validated configured-driver backend ownership state. */ export type DriverOwnershipType = import("./_schema_types.ts").DriverOwnershipType; /** * One independently controllable driver optimization. * * `changesBehavior` means request count, failure timing, storage layout, * consistency, atomicity, or another observable property can differ when the * optimization is enabled. Such optimizations must be disableable. */ export const DriverOptimizationSchema: z.ZodType = z.object({ /** Stable machine-readable optimization code. */ code: z.string().min(1), /** Current enabled state. */ enabled: z.boolean(), /** Whether this optimization changes observable behavior. */ changesBehavior: z.boolean(), /** Whether callers can turn this optimization off. */ disableable: z.boolean(), /** Human-readable detail for diagnostics or documentation. */ detail: z.string().min(1).optional(), }).strict().superRefine((value, ctx) => { if (value.changesBehavior && !value.disableable) { ctx.addIssue({ code: "custom", message: "Behavior-changing optimizations must be disableable." }); } }); /** A validated driver optimization declaration. */ export type DriverOptimizationType = import("./_schema_types.ts").DriverOptimizationType;