@rebasepro/server 0.12.1-canary.gf5f1d39 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/dist/GCSStorageController-BEmDYFKc.js +216 -0
  2. package/dist/GCSStorageController-BEmDYFKc.js.map +1 -0
  3. package/dist/S3StorageController-B2EDXXMH.js +249 -0
  4. package/dist/S3StorageController-B2EDXXMH.js.map +1 -0
  5. package/dist/admin_block-CcSGdH7M.js +124 -0
  6. package/dist/admin_block-CcSGdH7M.js.map +1 -0
  7. package/dist/api/rest/write-validation.d.ts +22 -0
  8. package/dist/auth/bearer-token.d.ts +26 -0
  9. package/dist/auth/index.d.ts +1 -1
  10. package/dist/auth/middleware.d.ts +2 -0
  11. package/dist/auth/rate-limiter.d.ts +0 -11
  12. package/dist/auth/require-auth.d.ts +39 -0
  13. package/dist/auth-CuC9M2x6.js +8961 -0
  14. package/dist/auth-CuC9M2x6.js.map +1 -0
  15. package/dist/backend-CIxN4FVm.js +15 -0
  16. package/dist/backend-CIxN4FVm.js.map +1 -0
  17. package/dist/backup-CVggVhR2.js +163 -0
  18. package/dist/backup-CVggVhR2.js.map +1 -0
  19. package/dist/boot/boot.d.ts +53 -0
  20. package/dist/boot/driver.d.ts +35 -6
  21. package/dist/boot/fetch-bundle.d.ts +43 -0
  22. package/dist/boot/version-skew.d.ts +77 -0
  23. package/dist/contract-routes-Dj8i5AiM.js +264 -0
  24. package/dist/contract-routes-Dj8i5AiM.js.map +1 -0
  25. package/dist/cron/cron-scheduler.d.ts +34 -0
  26. package/dist/cron-loader-B1S2MCSl.js +63 -0
  27. package/dist/cron-loader-B1S2MCSl.js.map +1 -0
  28. package/dist/cron-routes-CrQ0tK-_.js +62 -0
  29. package/dist/cron-routes-CrQ0tK-_.js.map +1 -0
  30. package/dist/cron-scheduler-B3RFt0HS.js +647 -0
  31. package/dist/cron-scheduler-B3RFt0HS.js.map +1 -0
  32. package/dist/cron-store-BywZsyfZ.js +164 -0
  33. package/dist/cron-store-BywZsyfZ.js.map +1 -0
  34. package/dist/dynamic-import-Dvh-K5fl.js.map +1 -1
  35. package/dist/errors-CgkCzoj7.js +226 -0
  36. package/dist/errors-CgkCzoj7.js.map +1 -0
  37. package/dist/function-loader-B_1fYfUY.js +86 -0
  38. package/dist/function-loader-B_1fYfUY.js.map +1 -0
  39. package/dist/function-routes-C0cLIy3N.js +28 -0
  40. package/dist/function-routes-C0cLIy3N.js.map +1 -0
  41. package/dist/functions/define-function.d.ts +2 -2
  42. package/dist/index.d.ts +2 -1
  43. package/dist/index.es.js +8123 -17984
  44. package/dist/index.es.js.map +1 -1
  45. package/dist/init.d.ts +21 -5
  46. package/dist/{jwt-D-eI6TTu.js → jwt-DD6EtpGj.js} +32 -13
  47. package/dist/{jwt-D-eI6TTu.js.map → jwt-DD6EtpGj.js.map} +1 -1
  48. package/dist/logger-BYU66ENZ.js.map +1 -1
  49. package/dist/logs-routes-BYA72C_C.js +100 -0
  50. package/dist/logs-routes-BYA72C_C.js.map +1 -0
  51. package/dist/{openapi-generator-Bjzmb5cn.js → openapi-generator-BEwyiaAr.js} +9 -3
  52. package/dist/openapi-generator-BEwyiaAr.js.map +1 -0
  53. package/dist/{schema-editor-routes-DDxfOIid.js → schema-editor-routes-CbF20Mf1.js} +4 -3
  54. package/dist/{schema-editor-routes-DDxfOIid.js.map → schema-editor-routes-CbF20Mf1.js.map} +1 -1
  55. package/dist/singleton.d.ts +4 -2
  56. package/dist/{src-CoOAMnBh.js → src-Cum9kox5.js} +501 -86
  57. package/dist/src-Cum9kox5.js.map +1 -0
  58. package/dist/src-_qQ3RNCK.js +279 -0
  59. package/dist/src-_qQ3RNCK.js.map +1 -0
  60. package/dist/types-DSnOC4mF.js +38 -0
  61. package/dist/types-DSnOC4mF.js.map +1 -0
  62. package/package.json +10 -10
  63. package/dist/openapi-generator-Bjzmb5cn.js.map +0 -1
  64. package/dist/src-CoOAMnBh.js.map +0 -1
  65. package/dist/src-Ivjud8jD.js +0 -957
  66. package/dist/src-Ivjud8jD.js.map +0 -1
  67. /package/dist/{chunk-DSJWtz9O.js → rolldown-runtime-DSJWtz9O.js} +0 -0
@@ -0,0 +1,15 @@
1
+ import { createRequire as __createRequire } from "module";
2
+ import "process";
3
+ __createRequire(import.meta.url);
4
+ //#region ../types/src/types/backend.ts
5
+ /**
6
+ * Type guard: does this admin support SQL operations?
7
+ * @group Admin
8
+ */
9
+ function isSQLAdmin(admin) {
10
+ return !!admin && typeof admin.executeSql === "function";
11
+ }
12
+ //#endregion
13
+ export { isSQLAdmin as t };
14
+
15
+ //# sourceMappingURL=backend-CIxN4FVm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend-CIxN4FVm.js","names":[],"sources":["../../types/src/types/backend.ts"],"sourcesContent":["import type { CollectionConfig, FilterValues, WhereFilterOp } from \"./collections\";\nimport type { LogicalCondition } from \"../controllers/data\";\nimport type { AuthAdapter } from \"./auth_adapter\";\nimport type { HistoryConfig } from \"../controllers/client\";\nimport type { ChannelBusSetting } from \"./channel_bus\";\n\n// =============================================================================\n// DATABASE CONNECTION INTERFACES\n// =============================================================================\n\n/**\n * Abstract database connection interface.\n * Represents a connection to any database system.\n */\nexport interface DatabaseConnection {\n /**\n * Type identifier for this database (e.g., 'postgres', 'mongodb', 'mysql')\n */\n readonly type: string;\n\n /**\n * Whether the connection is currently active\n */\n readonly isConnected?: boolean;\n\n /**\n * Close the database connection and release resources.\n */\n close?(): Promise<void>;\n}\n\n// =============================================================================\n// QUERY BUILDING INTERFACES\n// =============================================================================\n\n/**\n * A single filter condition for database queries\n */\nexport interface QueryFilter {\n field: string;\n operator: WhereFilterOp;\n value: unknown;\n}\n\n/**\n * Options for fetching a collection of entities\n */\nexport interface FetchCollectionOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n offset?: number;\n startAfter?: unknown;\n searchString?: string;\n databaseId?: string;\n collection?: CollectionConfig;\n}\n\n/**\n * Options for searching entities\n */\nexport interface SearchOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n databaseId?: string;\n collection?: CollectionConfig;\n}\n\n/**\n * Options for counting entities\n */\nexport interface CountOptions<M extends Record<string, unknown> = Record<string, unknown>> {\n filter?: FilterValues<Extract<keyof M, string>>;\n /**\n * An `or(...)`/`and(...)` group, alongside `filter`.\n *\n * Counted as well as fetched, or `total` describes a different set of rows\n * from the one that was served — the same reason `filter` is here.\n */\n logical?: LogicalCondition;\n searchString?: string;\n databaseId?: string;\n}\n\n/**\n * Abstract condition builder interface.\n * Implementations translate Rebase filter conditions to database-specific queries.\n *\n * Note: This interface can be implemented as instance methods or as a class with static methods.\n * For static implementations (like DrizzleConditionBuilder), use the ConditionBuilderStatic type.\n *\n * @template T The type of condition returned by the builder (e.g., SQL for PostgreSQL, Filter<Document> for MongoDB)\n */\nexport interface ConditionBuilder<T = unknown> {\n /**\n * Build filter conditions from Rebase FilterValues\n */\n buildFilterConditions<M extends Record<string, unknown>>(\n filter: FilterValues<Extract<keyof M, string>>,\n collectionPath: string,\n ...args: unknown[]\n ): T[];\n\n /**\n * Build search conditions for text search\n */\n buildSearchConditions(\n searchString: string,\n properties: Record<string, unknown>,\n ...args: unknown[]\n ): T[];\n\n /**\n * Combine multiple conditions with AND operator\n */\n combineConditionsWithAnd(conditions: T[]): T | undefined;\n\n /**\n * Combine multiple conditions with OR operator\n */\n combineConditionsWithOr(conditions: T[]): T | undefined;\n}\n\n/**\n * Static condition builder type for implementations using static methods.\n * Use this type when the class provides static methods rather than instance methods.\n *\n * @example\n * // DrizzleConditionBuilder satisfies this type\n * const builder: ConditionBuilderStatic<SQL> = DrizzleConditionBuilder;\n */\nexport type ConditionBuilderStatic<T = unknown> = {\n buildFilterConditions<M extends Record<string, unknown>>(\n filter: FilterValues<Extract<keyof M, string>>,\n ...args: unknown[]\n ): T[];\n buildSearchConditions(\n searchString: string,\n properties: Record<string, unknown>,\n ...args: unknown[]\n ): T[];\n combineConditionsWithAnd(conditions: T[]): T | undefined;\n combineConditionsWithOr(conditions: T[]): T | undefined;\n};\n\n// =============================================================================\n// ENTITY REPOSITORY INTERFACES\n// =============================================================================\n\n/**\n * Abstract entity repository interface.\n * Handles all CRUD operations for entities in the database.\n *\n * Implementations should handle:\n * - Entity serialization/deserialization\n * - Relation resolution\n * - ID generation and conversion\n */\nexport interface DataRepository {\n /**\n * Fetch a single entity by ID\n */\n fetchOne<M extends Record<string, unknown>>(\n collectionPath: string,\n id: string | number,\n databaseId?: string\n ): Promise<Record<string, unknown> | undefined>;\n\n /**\n * Fetch a collection of entities with optional filtering, ordering, and pagination\n */\n fetchCollection<M extends Record<string, unknown>>(\n collectionPath: string,\n options?: FetchCollectionOptions<M>\n ): Promise<Record<string, unknown>[]>;\n\n /**\n * Search entities by text\n */\n searchRows<M extends Record<string, unknown>>(\n collectionPath: string,\n searchString: string,\n options?: SearchOptions<M>\n ): Promise<Record<string, unknown>[]>;\n\n /**\n * Count entities in a collection\n */\n count<M extends Record<string, unknown>>(\n collectionPath: string,\n options?: CountOptions<M>\n ): Promise<number>;\n\n /**\n * Save a entity (create or update)\n */\n save<M extends Record<string, unknown>>(\n collectionPath: string,\n values: Partial<M>,\n id?: string | number,\n databaseId?: string\n ): Promise<Record<string, unknown>>;\n\n /**\n * Delete a entity by ID\n */\n delete(\n collectionPath: string,\n id: string | number,\n databaseId?: string\n ): Promise<void>;\n\n /**\n * Check if a field value is unique in a collection\n */\n checkUniqueField(\n collectionPath: string,\n fieldName: string,\n value: unknown,\n excludeEntityId?: string,\n databaseId?: string\n ): Promise<boolean>;\n\n}\n\n// =============================================================================\n// REALTIME INTERFACES\n// =============================================================================\n\n/**\n * Configuration for subscribing to a collection\n */\nexport interface CollectionSubscriptionConfig {\n clientId: string;\n path: string;\n filter?: unknown;\n orderBy?: string;\n order?: \"desc\" | \"asc\";\n limit?: number;\n startAfter?: unknown;\n databaseId?: string;\n searchString?: string;\n}\n\n/**\n * Configuration for subscribing to a single entity\n */\nexport interface SingleSubscriptionConfig {\n clientId: string;\n path: string;\n id: string | number;\n}\n\n/**\n * Opt-in retention for one set of broadcast channels.\n *\n * Retention is configured on the server and nowhere else. A channel is created\n * by whoever names it, so letting a client ask for its own history depth would\n * let any visitor commit the backend to unbounded storage; and presence-only or\n * notification-only channels — the overwhelming majority — must not pay for a\n * feature they never use. With no rules configured nothing is written, no table\n * is created, and broadcast behaves exactly as it did before history existed.\n */\nexport interface ChannelRetentionRule {\n /**\n * Channel name to match. Either exact (`\"doc:42\"`) or a trailing-`*` prefix\n * (`\"doc:*\"`). Deliberately not a full glob or RegExp: this decides what\n * gets written to disk, and a rule whose blast radius is not obvious at a\n * glance is the wrong shape for that.\n */\n match: string;\n /** Keep at most this many of the most recent messages per channel. */\n limit?: number;\n /**\n * Keep messages for at most this long. Accepts a millisecond count or a\n * short duration string (`\"30s\"`, `\"15m\"`, `\"24h\"`, `\"7d\"`).\n */\n ttl?: number | string;\n}\n\n/**\n * Server-side realtime options.\n *\n * The channel bus contract and its config live in `./channel_bus` so that a\n * transport shipped as its own package depends on the contract alone.\n */\nexport interface RealtimeChannelsConfig {\n /**\n * Retention rules, most specific first — the first match wins. Omitted or\n * empty means no channel retains anything.\n */\n channels?: ChannelRetentionRule[];\n /**\n * How channel broadcast and presence reach other backend instances.\n * Defaults to `{ type: \"memory\" }` — i.e. they don't.\n */\n bus?: ChannelBusSetting;\n}\n\n/**\n * Abstract realtime provider interface.\n * Handles real-time subscriptions and notifications for entity changes.\n */\nexport interface RealtimeProvider {\n /**\n * Subscribe to collection changes\n */\n subscribeToCollection(\n subscriptionId: string,\n config: CollectionSubscriptionConfig,\n callback?: (rows: Record<string, unknown>[]) => void\n ): void;\n\n /**\n * Subscribe to single entity changes\n */\n subscribeToOne(\n subscriptionId: string,\n config: SingleSubscriptionConfig,\n callback?: (row: Record<string, unknown> | null) => void\n ): void;\n\n /**\n * Unsubscribe from a subscription\n */\n unsubscribe(subscriptionId: string): void;\n\n /**\n * Notify all relevant subscribers of a entity update\n */\n notifyUpdate(\n path: string,\n id: string,\n row: Record<string, unknown> | null,\n databaseId?: string\n ): Promise<void>;\n\n /**\n * Called when the HTTP server is ready and listening.\n * Useful for providers that need the server address for callbacks.\n */\n onServerReady?(serverInfo: { port: number; hostname?: string }): void;\n\n /**\n * Gracefully shut down the realtime provider.\n * Called during server shutdown to clean up resources.\n */\n destroy?(): Promise<void>;\n\n /**\n * Stop the internal LISTEN client (e.g., PostgreSQL LISTEN/NOTIFY).\n * Called during graceful shutdown before closing database connections.\n */\n stopListening?(): Promise<void>;\n}\n\n// =============================================================================\n// COLLECTION REGISTRY INTERFACES\n// =============================================================================\n\n/**\n * Abstract collection registry interface.\n * Manages registration and lookup of entity collections.\n */\nexport interface CollectionRegistryInterface {\n /**\n * Register a collection\n */\n register(collection: CollectionConfig): void;\n\n /**\n * Get a collection by its path\n */\n getCollectionByPath(path: string): CollectionConfig | undefined;\n\n /**\n * Get all registered collections\n */\n getCollections(): CollectionConfig[];\n\n /**\n * Get the currently registered global callbacks, if any.\n */\n getGlobalCallbacks(): any | undefined;\n}\n\n// =============================================================================\n// DATA TRANSFORMER INTERFACES\n// =============================================================================\n\n/**\n * Abstract data transformer interface.\n * Handles serialization/deserialization between frontend and database formats.\n */\nexport interface DataTransformer {\n /**\n * Transform entity data for storage in the database\n */\n serializeToDatabase<M extends Record<string, unknown>>(\n entity: M,\n collection: CollectionConfig\n ): Record<string, unknown>;\n\n /**\n * Transform database data back to entity format\n */\n deserializeFromDatabase<M extends Record<string, unknown>>(\n data: Record<string, unknown>,\n collection: CollectionConfig\n ): Promise<M>;\n}\n\n// =============================================================================\n// DATABASE ADMIN — CAPABILITY-SPECIFIC INTERFACES (1.3)\n// =============================================================================\n\n/**\n * Administrative operations for SQL-based databases (PostgreSQL, MySQL, etc.).\n * Used by the SQL Editor, RLS Editor, and schema browser.\n *\n * @group Admin\n */\nexport interface SQLAdmin {\n /**\n * Execute raw SQL against the database.\n */\n executeSql(sql: string, options?: { database?: string; role?: string; params?: unknown[] }): Promise<Record<string, unknown>[]>;\n\n /**\n * Fetch the available databases on the server.\n */\n fetchAvailableDatabases?(): Promise<string[]>;\n\n /**\n * Fetch the available *native PostgreSQL* database roles (from `pg_roles`).\n *\n * These are connection-level roles — what the SQL editor can `SET ROLE` to,\n * and what `SecurityRule.pgRoles` targets. They are NOT application roles;\n * for those use {@link fetchApplicationRoles}.\n */\n fetchAvailableRoles?(): Promise<string[]>;\n\n /**\n * Fetch the *application-level* roles in use in this project.\n *\n * These are the strings stored on the users table's `roles` column and\n * exposed to policies as `auth.roles()` — what `SecurityRule.roles`\n * matches against. Distinct from {@link fetchAvailableRoles}; the two are\n * not interchangeable.\n */\n fetchApplicationRoles?(): Promise<string[]>;\n\n /**\n * Fetch the current database name.\n */\n fetchCurrentDatabase?(): Promise<string | undefined>;\n}\n\n/**\n * Administrative operations for document-based databases (MongoDB, Firestore, etc.).\n * Used by future document administration tools.\n *\n * @group Admin\n */\nexport interface DocumentAdmin {\n /**\n * Execute an aggregation pipeline or equivalent query.\n */\n executeAggregate?(pipeline: Record<string, unknown>[]): Promise<Record<string, unknown>[]>;\n\n /**\n * Fetch statistics for a collection (document count, size, etc.).\n */\n fetchCollectionStats?(collectionName: string): Promise<{ count: number; sizeBytes?: number }>;\n}\n\n/**\n * Administrative operations for schema management.\n * Shared across SQL and document databases.\n *\n * @group Admin\n */\nexport interface SchemaAdmin {\n /**\n * Fetch database tables/collections not yet mapped to a Rebase collection.\n */\n fetchUnmappedTables?(mappedPaths?: string[]): Promise<string[]>;\n\n /**\n * Fetch column/field metadata for a single table/collection.\n * The return type is generic — SQL backends return TableMetadata,\n * document backends may return a different shape.\n */\n fetchTableMetadata?(tableName: string): Promise<unknown>;\n}\n\n/**\n * Metadata for a database branch.\n * @group Admin\n */\nexport interface BranchInfo {\n /** Branch name (without prefix). */\n name: string;\n /** The database this branch was created from. */\n parentDatabase: string;\n /** When the branch was created. */\n createdAt: Date;\n /** Size in bytes, if available from the server. */\n sizeBytes?: number;\n}\n\n/**\n * Administrative operations for database branching.\n * Allows creating isolated database copies for development/preview workflows.\n *\n * @group Admin\n */\nexport interface BranchAdmin {\n /** Create a new branch (database copy) from the current or specified source database. */\n createBranch(name: string, options?: { source?: string }): Promise<BranchInfo>;\n\n /** Delete a branch database. Cannot delete the main/default database. */\n deleteBranch(name: string): Promise<void>;\n\n /** List all branches (databases that were created via branching). */\n listBranches(): Promise<BranchInfo[]>;\n\n /** Get info about a specific branch. */\n getBranchInfo(name: string): Promise<BranchInfo | undefined>;\n}\n\n/**\n * Union type for all admin capabilities.\n * A backend may implement any combination of these interfaces.\n *\n * Use type guards (`isSQLAdmin`, `isDocumentAdmin`, `isSchemaAdmin`, `isBranchAdmin`)\n * to safely narrow the type before calling methods.\n *\n * @group Admin\n */\nexport type DatabaseAdmin = Partial<SQLAdmin> & Partial<DocumentAdmin> & Partial<SchemaAdmin> & Partial<BranchAdmin>;\n\n/**\n * Type guard: does this admin support SQL operations?\n * @group Admin\n */\nexport function isSQLAdmin(admin: DatabaseAdmin | undefined): admin is SQLAdmin {\n return !!admin && typeof (admin as SQLAdmin).executeSql === \"function\";\n}\n\n/**\n * Type guard: does this admin support document operations?\n * @group Admin\n */\nexport function isDocumentAdmin(admin: DatabaseAdmin | undefined): admin is DocumentAdmin {\n return !!admin && (\n typeof (admin as DocumentAdmin).executeAggregate === \"function\" ||\n typeof (admin as DocumentAdmin).fetchCollectionStats === \"function\"\n );\n}\n\n/**\n * Type guard: does this admin support schema management?\n * @group Admin\n */\nexport function isSchemaAdmin(admin: DatabaseAdmin | undefined): admin is SchemaAdmin {\n return !!admin && (\n typeof (admin as SchemaAdmin).fetchUnmappedTables === \"function\" ||\n typeof (admin as SchemaAdmin).fetchTableMetadata === \"function\"\n );\n}\n\n/**\n * Type guard: does this admin support database branching?\n * @group Admin\n */\nexport function isBranchAdmin(admin: DatabaseAdmin | undefined): admin is BranchAdmin {\n return !!admin && typeof (admin as BranchAdmin).createBranch === \"function\";\n}\n\n// =============================================================================\n// LIFECYCLE INTERFACES (1.4)\n// =============================================================================\n\n/**\n * Health check result returned by `healthCheck()`.\n * @group Lifecycle\n */\nexport interface HealthCheckResult {\n /** Whether the backend is healthy and able to serve requests. */\n healthy: boolean;\n /** Round-trip latency to the database in milliseconds. */\n latencyMs: number;\n /** Optional details (e.g., pool stats, replication lag). */\n details?: Record<string, unknown>;\n}\n\n/**\n * Lifecycle contract for backend components that hold resources\n * (database connections, WebSocket pools, timers, etc.).\n *\n * All methods are optional — simple backends (e.g., in-memory) can skip them.\n * @group Lifecycle\n */\nexport interface BackendLifecycle {\n /**\n * Initialize the backend: open connections, run migrations, seed data.\n * Called once during startup. Idempotent.\n */\n initialize?(): Promise<void>;\n\n /**\n * Check whether the backend is healthy and reachable.\n * Should be fast (< 1 s) and safe to call frequently.\n */\n healthCheck?(): Promise<HealthCheckResult>;\n\n /**\n * Gracefully shut down: close connections, flush buffers, cancel timers.\n * After calling `destroy()`, no other methods should be called.\n */\n destroy?(): Promise<void>;\n}\n\n// =============================================================================\n// BACKEND FACTORY INTERFACES\n// =============================================================================\n\n/**\n * Configuration for creating a database backend\n */\nexport interface BackendConfig {\n /**\n * Type of database backend\n */\n type: string;\n\n /**\n * Database connection (implementation-specific)\n */\n connection: unknown;\n\n /**\n * Schema definition (implementation-specific, e.g., Drizzle schema for PostgreSQL)\n */\n schema?: unknown;\n}\n\n/**\n * A complete backend instance with all required services.\n *\n * Now includes optional lifecycle management and admin capabilities.\n */\nexport interface BackendInstance extends BackendLifecycle {\n /**\n * Entity repository for CRUD operations\n */\n entityRepository: DataRepository;\n\n /**\n * Realtime provider for subscriptions\n */\n realtimeProvider: RealtimeProvider;\n\n /**\n * Collection registry\n */\n collectionRegistry: CollectionRegistryInterface;\n\n /**\n * The underlying database connection\n */\n connection: DatabaseConnection;\n\n /**\n * Administrative operations (SQL, schema, documents).\n * What's available depends on the backend type — use type guards\n * (`isSQLAdmin`, `isSchemaAdmin`, etc.) to narrow.\n */\n admin?: DatabaseAdmin;\n}\n\n/**\n * Factory function type for creating backend instances\n */\nexport type BackendFactory<TConfig extends BackendConfig = BackendConfig> =\n (config: TConfig) => BackendInstance;\n\n// =============================================================================\n// BACKEND BOOTSTRAPPER (1.2)\n// =============================================================================\n\n/**\n * A `BackendBootstrapper` encapsulates all driver-specific initialization logic.\n *\n * Instead of hard-coding Postgres setup into `initializeRebaseBackend()`,\n * each database backend provides its own bootstrapper that knows how to:\n * - Create the DataDriver from a config object\n * - Optionally initialize auth tables\n * - Optionally create a realtime service\n * - Mount driver-specific API routes\n *\n * The main `initializeRebaseBackend()` becomes a **coordinator** that iterates\n * registered bootstrappers, calls their hooks, and wires the results together.\n *\n * @group Backend\n *\n * @example\n * ```typescript\n * // Third-party MySQL bootstrapper\n * const mysqlBootstrapper: BackendBootstrapper = {\n * type: \"mysql\",\n * initializeDriver: async (config) => new MySQLDataDriver(config.connection),\n * initializeRealtime: async (config) => new MySQLChangeStreamRealtime(config.connection),\n * };\n *\n * initializeRebaseBackend({\n * ...config,\n * bootstrappers: [postgresBootstrapper, mysqlBootstrapper]\n * });\n * ```\n */\nexport interface BackendBootstrapper {\n /**\n * Which driver type this bootstrapper handles.\n * Must match the `type` field on the driver config object\n * (e.g., `\"postgres\"`, `\"mongodb\"`, `\"mysql\"`).\n */\n type: string;\n\n /**\n * Unique identifier for this bootstrapper instance.\n * Used to register the driver in the driver registry.\n * Defaults to `type` if not set.\n */\n id?: string;\n\n /**\n * Whether this bootstrapper provides the default driver.\n * When true, the coordinator uses this driver as the primary one.\n */\n isDefault?: boolean;\n\n /**\n * Run database migrations for this driver.\n * Called by the coordinator after all drivers are initialized.\n */\n runMigrations?(config: unknown, driverResult: InitializedDriver): Promise<void>;\n\n /**\n * Create a DataDriver from the given config.\n * This is the only **required** method.\n */\n initializeDriver(config: unknown): Promise<InitializedDriver>;\n\n /**\n * Initialize auth tables / services if this driver supports them.\n * Return undefined if auth is not supported by this backend.\n */\n initializeAuth?(config: unknown, driverResult: InitializedDriver): Promise<BootstrappedAuth | undefined>;\n\n /**\n * Initialize history tables / services if this driver supports them.\n * Return undefined if history is not supported by this backend.\n */\n initializeHistory?(config: HistoryConfig, driverResult: InitializedDriver): Promise<{ historyService: unknown } | undefined>;\n\n /**\n * Create a realtime provider for this driver.\n * Return undefined if the driver does not support realtime.\n */\n initializeRealtime?(config: unknown, driverResult: InitializedDriver): Promise<RealtimeProvider | undefined>;\n\n /**\n * Mount any driver-specific HTTP routes (e.g., custom admin endpoints).\n * Called after all drivers are initialized.\n */\n mountRoutes?(app: unknown, basePath: string, driverResult: InitializedDriver): void;\n\n /**\n * Return admin capabilities for this driver.\n */\n getAdmin?(driverResult: InitializedDriver): DatabaseAdmin | undefined;\n\n /**\n * Bring the database's collection tables up to date, additively.\n *\n * Optional because it is only meaningful for schema-ful drivers. A managed\n * runtime boots a compiled project against a database it has never seen; auth\n * tables are ensured on boot but collection tables were created by nothing,\n * so every data request answered 500 on a missing relation. The CLI's `db\n * push` cannot fill the gap — it needs Atlas, and the runtime image ships no\n * CLI.\n *\n * Implementations MUST be additive-only: create missing tables, columns and\n * enum types, and never drop, narrow or rewrite anything. This runs\n * unattended against live customer data with nobody reading a diff, so the\n * destructive half stays a deliberate migration.\n */\n ensureCollectionSchema?(\n collections: unknown[],\n driverResult: InitializedDriver,\n log?: (message: string) => void\n ): Promise<{ applied: number }>;\n\n /**\n * Apply the collections' row-level-security policies, additively and\n * idempotently — the companion to {@link ensureCollectionSchema}.\n *\n * That method creates the tables; a table with RLS disabled and no policies\n * is not servable, because authenticated requests run as a restricted role:\n * a read with no `SELECT` policy returns nothing (a public collection\n * answers 401) and a write with no `INSERT`/`UPDATE` policy is denied. The\n * `db push` CLI applies these from the same collections, but it cannot reach\n * a managed tenant's in-cluster database — the runtime, already connected,\n * is the only thing that can.\n *\n * MUST be idempotent (re-run on every boot) and MUST NOT be destructive.\n * Runs after auth initialization, because the generated policies call the\n * `auth.*` helper functions and `CREATE POLICY` validates they exist.\n */\n ensureCollectionPolicies?(\n collections: unknown[],\n driverResult: InitializedDriver,\n log?: (message: string) => void\n ): Promise<{ applied: number }>;\n\n /**\n * Initialize WebSocket server for realtime operations.\n */\n initializeWebsockets?(server: unknown, realtimeService: RealtimeProvider, driver: import(\"../controllers/data_driver\").DataDriver, config?: unknown, authAdapter?: AuthAdapter): Promise<void> | void;\n}\n\n/**\n * Result of `BackendBootstrapper.initializeDriver()`.\n * @group Backend\n */\nexport interface InitializedDriver {\n /** The DataDriver instance, ready for use. */\n driver: import(\"../controllers/data_driver\").DataDriver;\n\n /** The realtime service, if the driver created one during init. */\n realtimeProvider?: RealtimeProvider;\n\n /** A collection registry to register schema / tables into. */\n collectionRegistry?: CollectionRegistryInterface;\n\n /**\n * Collections the driver derived from the live database schema.\n *\n * Set by drivers that introspect in `baas` mode; the server serves these\n * instead of collections loaded from config files.\n */\n collections?: import(\"./collections\").CollectionConfig[];\n\n /** The underlying database connection (for lifecycle management). */\n connection?: DatabaseConnection;\n\n /**\n * Opaque handle that the bootstrapper can use in subsequent hooks\n * (e.g., `initializeAuth`, `mountRoutes`) to access driver internals.\n * Not used by the coordinator.\n */\n internals?: unknown;\n}\n\n/**\n * Result of `BackendBootstrapper.initializeAuth()`.\n * @group Backend\n */\nexport interface BootstrappedAuth {\n /** User management service. */\n userService: unknown;\n /** Role management service (optional, roles are now simple strings). */\n roleService?: unknown;\n /** Email service (optional). */\n emailService?: unknown;\n /** Combined Auth Repository for unified token and user management. */\n authRepository?: unknown;\n /**\n * Whether the auth schema in the database is one this runtime can serve.\n *\n * Folded into `healthCheck()` so a schema mismatch shows up as a degraded\n * health response. Without it, a server whose auth is entirely broken still\n * reports healthy — the database connection it probes is fine, and the\n * mismatch is only discovered one failed login at a time.\n */\n schemaHealthCheck?(): Promise<AuthSchemaHealth>;\n}\n\n/**\n * Result of {@link BootstrappedAuth.schemaHealthCheck}.\n * @group Lifecycle\n */\nexport interface AuthSchemaHealth {\n /** False when this runtime cannot be trusted to serve auth against this database. */\n healthy: boolean;\n /** Human-readable descriptions of each mismatch found. Empty when healthy. */\n problems: string[];\n /** Auth schema version recorded in the database, when it records one. */\n databaseVersion?: number | null;\n /** Auth schema version this runtime expects. */\n runtimeVersion?: number;\n}\n"],"mappings":";;;;;;;;AAqiBA,SAAgB,WAAW,OAAqD;CAC5E,OAAO,CAAC,CAAC,SAAS,OAAQ,MAAmB,eAAe;AAChE"}
@@ -0,0 +1,163 @@
1
+ import { createRequire as __createRequire } from "module";
2
+ import "process";
3
+ __createRequire(import.meta.url);
4
+ import { n as __exportAll } from "./rolldown-runtime-DSJWtz9O.js";
5
+ import { n as errorHandler, t as ApiError } from "./errors-CgkCzoj7.js";
6
+ import fs from "fs";
7
+ import path from "path";
8
+ import { Hono } from "hono";
9
+ //#region src/backup/backup-common.ts
10
+ /**
11
+ * Storage-generic backup helpers used by the admin backup routes.
12
+ *
13
+ * These live in server (which owns {@link StorageController}) so the
14
+ * admin API can list backups without depending on the Postgres driver —
15
+ * `server` must not import from `server-postgres`. The `pg_dump`-side
16
+ * copies of the tiny pure parsers live in `@rebasepro/server-postgres`;
17
+ * keep the two in sync if the naming scheme ever changes.
18
+ */
19
+ /** Parse a destination string (`s3://…`, `gs://…`, or a local path). */
20
+ function parseBackupDestination(out) {
21
+ const s3 = out.match(/^s3:\/\/([^/]+)\/?(.*)$/);
22
+ if (s3) return {
23
+ kind: "s3",
24
+ bucket: s3[1],
25
+ prefix: s3[2].replace(/\/+$/, "")
26
+ };
27
+ const gcs = out.match(/^gs:\/\/([^/]+)\/?(.*)$/);
28
+ if (gcs) return {
29
+ kind: "gcs",
30
+ bucket: gcs[1],
31
+ prefix: gcs[2].replace(/\/+$/, "")
32
+ };
33
+ return {
34
+ kind: "local",
35
+ path: out
36
+ };
37
+ }
38
+ /**
39
+ * Recover the UTC creation time encoded in a `rebase-<db>-<ts>.dump` name.
40
+ * Returns `null` for names that don't match, so foreign objects are ignored.
41
+ */
42
+ function parseBackupTimestamp(fileName) {
43
+ const match = (fileName.split("/").pop() ?? fileName).match(/-(\d{8})T(\d{6})Z\.dump$/);
44
+ if (!match) return null;
45
+ const [, ymd, hms] = match;
46
+ const iso = `${ymd.slice(0, 4)}-${ymd.slice(4, 6)}-${ymd.slice(6, 8)}T${hms.slice(0, 2)}:${hms.slice(2, 4)}:${hms.slice(4, 6)}Z`;
47
+ const date = new Date(iso);
48
+ return Number.isNaN(date.getTime()) ? null : date;
49
+ }
50
+ /**
51
+ * List the backups at a destination as {@link BackupInfo}, newest first.
52
+ * Only `.dump` files are considered.
53
+ */
54
+ async function listBackupObjects(dest, storage) {
55
+ if (dest.kind === "local") {
56
+ if (!fs.existsSync(dest.path)) return [];
57
+ const dir = fs.statSync(dest.path).isDirectory() ? dest.path : path.dirname(dest.path);
58
+ if (!fs.existsSync(dir)) return [];
59
+ return fs.readdirSync(dir).filter((f) => f.endsWith(".dump")).map((f) => {
60
+ const full = path.join(dir, f);
61
+ const st = fs.statSync(full);
62
+ const createdAt = parseBackupTimestamp(f) ?? st.mtime;
63
+ return {
64
+ key: full,
65
+ name: f,
66
+ sizeBytes: st.size,
67
+ createdAt: createdAt.toISOString(),
68
+ destinationKind: "local"
69
+ };
70
+ }).sort(byNewest);
71
+ }
72
+ if (!storage) return [];
73
+ return (await storage.listObjects(dest.prefix ? `${dest.prefix}/` : "", {
74
+ bucket: dest.bucket,
75
+ maxResults: 1e3
76
+ })).items.map((item) => item.fullPath).filter((key) => key.endsWith(".dump")).map((key) => {
77
+ const createdAt = parseBackupTimestamp(key);
78
+ return {
79
+ key,
80
+ name: key.split("/").pop() || key,
81
+ createdAt: createdAt ? createdAt.toISOString() : void 0,
82
+ destinationKind: dest.kind
83
+ };
84
+ }).sort(byNewest);
85
+ }
86
+ function byNewest(a, b) {
87
+ const ta = a.createdAt ? Date.parse(a.createdAt) : 0;
88
+ return (b.createdAt ? Date.parse(b.createdAt) : 0) - ta;
89
+ }
90
+ /** Read a single backup's bytes. Guards against path traversal for local. */
91
+ async function readBackupBytes(dest, key, storage) {
92
+ if (dest.kind === "local") {
93
+ const dir = (fs.existsSync(dest.path) ? fs.statSync(dest.path) : null)?.isDirectory() ? dest.path : path.dirname(dest.path);
94
+ const resolvedDir = path.resolve(dir);
95
+ const resolved = path.resolve(key);
96
+ if (!resolved.startsWith(resolvedDir + path.sep) || !resolved.endsWith(".dump")) return null;
97
+ if (!fs.existsSync(resolved)) return null;
98
+ return {
99
+ bytes: new Uint8Array(fs.readFileSync(resolved)),
100
+ name: path.basename(resolved)
101
+ };
102
+ }
103
+ if (!storage) return null;
104
+ if (!key.endsWith(".dump")) return null;
105
+ const file = await storage.getObject(key, dest.bucket);
106
+ if (!file) return null;
107
+ return {
108
+ bytes: new Uint8Array(await file.arrayBuffer()),
109
+ name: key.split("/").pop() || key
110
+ };
111
+ }
112
+ //#endregion
113
+ //#region src/backup/backup-routes.ts
114
+ /**
115
+ * Admin REST routes for the Backups panel.
116
+ *
117
+ * Routes (mounted under `/admin/backups`, admin-guarded by the caller):
118
+ * GET / → list available backups
119
+ * GET /download → download a backup's bytes (?key=…)
120
+ */
121
+ function createBackupRoutes(config) {
122
+ const router = new Hono();
123
+ router.onError(errorHandler);
124
+ router.get("/", async (c) => {
125
+ const dest = config.getDestination();
126
+ if (!dest) return c.json({
127
+ backups: [],
128
+ destinationKind: "local",
129
+ configured: false
130
+ });
131
+ const backups = await listBackupObjects(dest, config.storage);
132
+ return c.json({
133
+ backups,
134
+ destinationKind: dest.kind,
135
+ configured: true
136
+ });
137
+ });
138
+ router.get("/download", async (c) => {
139
+ const dest = config.getDestination();
140
+ if (!dest) throw ApiError.badRequest("Backups are not configured (set BACKUP_DESTINATION).");
141
+ const key = c.req.query("key");
142
+ if (!key) throw ApiError.badRequest("Missing 'key' query parameter.");
143
+ const result = await readBackupBytes(dest, key, config.storage);
144
+ if (!result) throw ApiError.notFound(`Backup not found: ${key}`);
145
+ c.header("Content-Type", "application/octet-stream");
146
+ c.header("Content-Disposition", `attachment; filename="${result.name}"`);
147
+ return c.body(result.bytes);
148
+ });
149
+ return router;
150
+ }
151
+ //#endregion
152
+ //#region src/backup/index.ts
153
+ var backup_exports = /* @__PURE__ */ __exportAll({
154
+ createBackupRoutes: () => createBackupRoutes,
155
+ listBackupObjects: () => listBackupObjects,
156
+ parseBackupDestination: () => parseBackupDestination,
157
+ parseBackupTimestamp: () => parseBackupTimestamp,
158
+ readBackupBytes: () => readBackupBytes
159
+ });
160
+ //#endregion
161
+ export { parseBackupTimestamp as a, parseBackupDestination as i, createBackupRoutes as n, readBackupBytes as o, listBackupObjects as r, backup_exports as t };
162
+
163
+ //# sourceMappingURL=backup-CVggVhR2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backup-CVggVhR2.js","names":[],"sources":["../src/backup/backup-common.ts","../src/backup/backup-routes.ts","../src/backup/index.ts"],"sourcesContent":["/**\n * Storage-generic backup helpers used by the admin backup routes.\n *\n * These live in server (which owns {@link StorageController}) so the\n * admin API can list backups without depending on the Postgres driver —\n * `server` must not import from `server-postgres`. The `pg_dump`-side\n * copies of the tiny pure parsers live in `@rebasepro/server-postgres`;\n * keep the two in sync if the naming scheme ever changes.\n */\nimport fs from \"fs\";\nimport path from \"path\";\nimport type { BackupInfo, BackupDestinationKind } from \"@rebasepro/types\";\nimport type { StorageController } from \"../storage\";\n\nexport type BackupDestination =\n | { kind: \"local\"; path: string }\n | { kind: \"s3\"; bucket: string; prefix: string }\n | { kind: \"gcs\"; bucket: string; prefix: string };\n\n/** Parse a destination string (`s3://…`, `gs://…`, or a local path). */\nexport function parseBackupDestination(out: string): BackupDestination {\n const s3 = out.match(/^s3:\\/\\/([^/]+)\\/?(.*)$/);\n if (s3) return { kind: \"s3\", bucket: s3[1], prefix: s3[2].replace(/\\/+$/, \"\") };\n const gcs = out.match(/^gs:\\/\\/([^/]+)\\/?(.*)$/);\n if (gcs) return { kind: \"gcs\", bucket: gcs[1], prefix: gcs[2].replace(/\\/+$/, \"\") };\n return { kind: \"local\", path: out };\n}\n\n/**\n * Recover the UTC creation time encoded in a `rebase-<db>-<ts>.dump` name.\n * Returns `null` for names that don't match, so foreign objects are ignored.\n */\nexport function parseBackupTimestamp(fileName: string): Date | null {\n const base = fileName.split(\"/\").pop() ?? fileName;\n const match = base.match(/-(\\d{8})T(\\d{6})Z\\.dump$/);\n if (!match) return null;\n const [, ymd, hms] = match;\n const iso =\n `${ymd.slice(0, 4)}-${ymd.slice(4, 6)}-${ymd.slice(6, 8)}` +\n `T${hms.slice(0, 2)}:${hms.slice(2, 4)}:${hms.slice(4, 6)}Z`;\n const date = new Date(iso);\n return Number.isNaN(date.getTime()) ? null : date;\n}\n\n/**\n * List the backups at a destination as {@link BackupInfo}, newest first.\n * Only `.dump` files are considered.\n */\nexport async function listBackupObjects(\n dest: BackupDestination,\n storage?: StorageController\n): Promise<BackupInfo[]> {\n if (dest.kind === \"local\") {\n if (!fs.existsSync(dest.path)) return [];\n const stat = fs.statSync(dest.path);\n const dir = stat.isDirectory() ? dest.path : path.dirname(dest.path);\n if (!fs.existsSync(dir)) return [];\n return fs\n .readdirSync(dir)\n .filter((f) => f.endsWith(\".dump\"))\n .map((f): BackupInfo => {\n const full = path.join(dir, f);\n const st = fs.statSync(full);\n const createdAt = parseBackupTimestamp(f) ?? st.mtime;\n return {\n key: full,\n name: f,\n sizeBytes: st.size,\n createdAt: createdAt.toISOString(),\n destinationKind: \"local\"\n };\n })\n .sort(byNewest);\n }\n\n if (!storage) return [];\n const result = await storage.listObjects(dest.prefix ? `${dest.prefix}/` : \"\", {\n bucket: dest.bucket,\n maxResults: 1000\n });\n return result.items\n .map((item) => item.fullPath)\n .filter((key) => key.endsWith(\".dump\"))\n .map((key): BackupInfo => {\n const createdAt = parseBackupTimestamp(key);\n return {\n key,\n name: key.split(\"/\").pop() || key,\n createdAt: createdAt ? createdAt.toISOString() : undefined,\n destinationKind: dest.kind as BackupDestinationKind\n };\n })\n .sort(byNewest);\n}\n\nfunction byNewest(a: BackupInfo, b: BackupInfo): number {\n const ta = a.createdAt ? Date.parse(a.createdAt) : 0;\n const tb = b.createdAt ? Date.parse(b.createdAt) : 0;\n return tb - ta;\n}\n\n/** Read a single backup's bytes. Guards against path traversal for local. */\nexport async function readBackupBytes(\n dest: BackupDestination,\n key: string,\n storage?: StorageController\n): Promise<{ bytes: Uint8Array<ArrayBuffer>; name: string } | null> {\n if (dest.kind === \"local\") {\n const stat = fs.existsSync(dest.path) ? fs.statSync(dest.path) : null;\n const dir = stat?.isDirectory() ? dest.path : path.dirname(dest.path);\n const resolvedDir = path.resolve(dir);\n const resolved = path.resolve(key);\n // Only allow reads inside the backup directory, and only .dump files.\n if (!resolved.startsWith(resolvedDir + path.sep) || !resolved.endsWith(\".dump\")) {\n return null;\n }\n if (!fs.existsSync(resolved)) return null;\n return { bytes: new Uint8Array(fs.readFileSync(resolved)), name: path.basename(resolved) };\n }\n\n if (!storage) return null;\n if (!key.endsWith(\".dump\")) return null;\n const file = await storage.getObject(key, dest.bucket);\n if (!file) return null;\n return { bytes: new Uint8Array(await file.arrayBuffer()), name: key.split(\"/\").pop() || key };\n}\n","import { Hono } from \"hono\";\nimport type { HonoEnv } from \"../api/types\";\nimport { ApiError, errorHandler } from \"../api/errors\";\nimport type { StorageController } from \"../storage\";\nimport { BackupDestination, listBackupObjects, readBackupBytes } from \"./backup-common\";\n\nexport interface BackupRoutesConfig {\n /**\n * Resolve the current backup destination, or `null` when backups are not\n * configured (`BACKUP_DESTINATION` unset). Read lazily so a restart isn't\n * required to pick up config.\n */\n getDestination: () => BackupDestination | null;\n /** Storage controller for object-storage destinations. */\n storage?: StorageController;\n}\n\n/**\n * Admin REST routes for the Backups panel.\n *\n * Routes (mounted under `/admin/backups`, admin-guarded by the caller):\n * GET / → list available backups\n * GET /download → download a backup's bytes (?key=…)\n */\nexport function createBackupRoutes(config: BackupRoutesConfig): Hono<HonoEnv> {\n const router = new Hono<HonoEnv>();\n router.onError(errorHandler);\n\n router.get(\"/\", async (c) => {\n const dest = config.getDestination();\n if (!dest) {\n return c.json({ backups: [], destinationKind: \"local\" as const, configured: false });\n }\n const backups = await listBackupObjects(dest, config.storage);\n return c.json({ backups, destinationKind: dest.kind, configured: true });\n });\n\n router.get(\"/download\", async (c) => {\n const dest = config.getDestination();\n if (!dest) {\n throw ApiError.badRequest(\"Backups are not configured (set BACKUP_DESTINATION).\");\n }\n const key = c.req.query(\"key\");\n if (!key) {\n throw ApiError.badRequest(\"Missing 'key' query parameter.\");\n }\n const result = await readBackupBytes(dest, key, config.storage);\n if (!result) {\n throw ApiError.notFound(`Backup not found: ${key}`);\n }\n c.header(\"Content-Type\", \"application/octet-stream\");\n c.header(\"Content-Disposition\", `attachment; filename=\"${result.name}\"`);\n return c.body(result.bytes);\n });\n\n return router;\n}\n","/**\n * Storage-generic backup helpers and admin routes for the Backups panel.\n * The `pg_dump`/`pg_restore` machinery lives in `@rebasepro/server-postgres`.\n */\nexport * from \"./backup-common\";\nexport { createBackupRoutes } from \"./backup-routes\";\nexport type { BackupRoutesConfig } from \"./backup-routes\";\n"],"mappings":"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"}
@@ -57,3 +57,56 @@ export declare function bootFromBundle(options?: BootOptions): Promise<BootedRun
57
57
  * bug and the trace is the point.
58
58
  */
59
59
  export declare function runFromBundle(options?: BootOptions): Promise<BootedRuntime>;
60
+ /**
61
+ * Say so, once per boot, when a data source's driver is older than this runtime.
62
+ *
63
+ * The check exists because of how a managed deployment is assembled: the image
64
+ * supplies `@rebasepro/server` (the entrypoint symlinks it over the bundle's
65
+ * copy) while every driver comes from the bundle's own `deps.declared`, pinned
66
+ * by the project's package.json. So the platform can ship a fix, roll every
67
+ * tenant onto the new image, and still have none of them running the fixed
68
+ * driver. Nothing detected that before this: the halves simply disagreed in
69
+ * silence until some capability turned out to be missing three layers down.
70
+ *
71
+ * A warning rather than a refusal. Old drivers are usually fine — the pairing is
72
+ * supported, and a boot that dies on version arithmetic would be a far worse
73
+ * failure than the drift it is guarding against. This only has to make the skew
74
+ * *visible*, so that the next person reading the log starts from the right
75
+ * question.
76
+ */
77
+ export declare function warnOnDriverSkew(dataSources: InitializedDataSource[], runtimeVersion: string | undefined): void;
78
+ /**
79
+ * Bring the database's collection tables up to date before serving.
80
+ *
81
+ * Delegates to whichever driver bootstrapped the default data source; a driver
82
+ * without `ensureCollectionSchema` (a schemaless one, or an older build) skips
83
+ * rather than failing, which is why this cannot break an existing deployment.
84
+ *
85
+ * Every path out of here says why, at info or louder. Guaranteeing the tables
86
+ * exist is this function's entire job, so "it declined, and said nothing" is the
87
+ * one outcome it must never produce: a deployment that skips comes up answering
88
+ * sign-in and 500ing every `/api/data/*` route, and the operator's only evidence
89
+ * is what these lines print. Silence here has already sent one investigation
90
+ * chasing a stale runtime image that was not stale.
91
+ *
92
+ * Failure is fatal on purpose. Booting anyway would produce exactly the state
93
+ * this exists to prevent — an app that answers sign-in and 500s every data
94
+ * request — and a crash-looping pod with the DDL error in its logs is a far
95
+ * better signal than a running one that silently cannot serve.
96
+ */
97
+ export declare function ensureCollectionSchema(bundle: LoadedBundle, dataSources: InitializedDataSource[], env: RebaseBootEnv): Promise<void>;
98
+ /**
99
+ * Apply the project's RLS policies before serving — the companion to
100
+ * {@link ensureCollectionSchema}, which creates the tables this makes servable.
101
+ *
102
+ * Runs after `initializeRebaseBackend`, not alongside table creation: the
103
+ * generated policies call the `auth.*` helper functions, and `CREATE POLICY`
104
+ * validates those exist, so this cannot run before auth is initialized. The
105
+ * gate conditions mirror `ensureCollectionSchema` (mode, bundle shape, driver
106
+ * support) — and because that function already ran and explained any skip on
107
+ * this same boot, the benign gates here return quietly rather than logging the
108
+ * same reason twice. The one thing it does say out loud is a driver that
109
+ * created tables but cannot apply policies: that is the difference between a
110
+ * served collection and a 401, and it must not pass in silence.
111
+ */
112
+ export declare function ensureCollectionPolicies(bundle: LoadedBundle, dataSources: InitializedDataSource[], env: RebaseBootEnv): Promise<void>;
@@ -1,4 +1,4 @@
1
- import type { BackendBootstrapper } from "@rebasepro/types";
1
+ import type { BackendBootstrapper, DatabaseAdapter } from "@rebasepro/types";
2
2
  import type { ResolvedDataSourceConfig } from "./sources";
3
3
  /**
4
4
  * The connection handle a driver hands back at boot: the client object, the
@@ -39,6 +39,16 @@ export interface InitializedDataSource {
39
39
  key: string;
40
40
  engine: string;
41
41
  driverPackage: string;
42
+ /**
43
+ * The driver's installed version, when it could be read.
44
+ *
45
+ * Carried so a boot that finds a capability missing can say whether the
46
+ * driver is *old* or merely *different* — see `version-skew.ts`. Optional
47
+ * because a driver resolved from outside a `node_modules` tree (a linked
48
+ * workspace, a bare specifier) has no manifest to read, and that must not
49
+ * be fatal.
50
+ */
51
+ driverVersion?: string;
42
52
  bootstrapper: BackendBootstrapper;
43
53
  connection: DriverConnection;
44
54
  }
@@ -47,6 +57,30 @@ export interface BundleSchema {
47
57
  enums?: Record<string, unknown>;
48
58
  relations?: Record<string, unknown>;
49
59
  }
60
+ /**
61
+ * Where to look for packages a bundle brought with it.
62
+ *
63
+ * A driver has to be resolved relative to the **bundle**, not to this package.
64
+ * A bare `import("@rebasepro/server-postgres")` resolves from wherever the
65
+ * runtime itself is installed, which on a real deployment is somewhere else
66
+ * entirely — the runtime image holds the server, the bundle holds the project's
67
+ * dependencies. Resolving from the bundle is also the honest semantics: the
68
+ * project declares which driver it uses, so the project's tree is where to look.
69
+ *
70
+ * Several roots, because a driver is installed wherever the project keeps its
71
+ * dependencies: beside a built bundle, but inside the backend package in a
72
+ * workspace running from source.
73
+ */
74
+ export declare function bundleResolutionRoots(bundleDir: string): string[];
75
+ /**
76
+ * Wrap a `DatabaseAdapter` as a `BackendBootstrapper` carrying a registry id.
77
+ *
78
+ * `initializeRebaseBackend` does this internally for its single-adapter
79
+ * convenience path, but that path hardcodes one anonymous driver. Multiple
80
+ * sources need an explicit `id` per adapter, because the id is exactly what
81
+ * `collection.dataSource` routes against.
82
+ */
83
+ export declare function adapterToBootstrapper(adapter: DatabaseAdapter, id: string, isDefault: boolean): BackendBootstrapper;
50
84
  /**
51
85
  * Build a driver for one data source.
52
86
  *
@@ -64,8 +98,3 @@ export declare function initializeDataSource(source: ResolvedDataSourceConfig, s
64
98
  * order a human would expect.
65
99
  */
66
100
  export declare function initializeDataSources(sources: ResolvedDataSourceConfig[], schema: BundleSchema | undefined, resolveFrom?: string[]): Promise<InitializedDataSource[]>;
67
- /**
68
- * @deprecated Use {@link DriverConnection}. This name collides with
69
- * `DatabaseConnection` from `@rebasepro/types`, which is a different shape.
70
- */
71
- export type DatabaseConnection = DriverConnection;
@@ -0,0 +1,43 @@
1
+ /** Where the runtime is told to fetch its bundle from. */
2
+ export declare const BUNDLE_URL_ENV = "REBASE_BUNDLE_URL";
3
+ /** Bearer token for that fetch. Does not expire — see the module note. */
4
+ export declare const BUNDLE_TOKEN_ENV = "REBASE_BUNDLE_TOKEN";
5
+ export interface FetchBundleOptions {
6
+ url: string;
7
+ token?: string;
8
+ /** Where to unpack. Defaults to a fresh directory under the OS temp dir. */
9
+ destination?: string;
10
+ /** Injected for tests. */
11
+ fetchImpl?: typeof fetch;
12
+ /** Injected for tests. */
13
+ extract?: (tarball: string, destination: string) => Promise<void>;
14
+ /** How long the download may take before it is abandoned. */
15
+ timeoutMs?: number;
16
+ }
17
+ /**
18
+ * Whether this process should fetch its bundle rather than read one from disk.
19
+ *
20
+ * An explicit `REBASE_BUNDLE` — a path — always wins. A platform that mounted a
21
+ * bundle AND set a URL means somebody is mid-migration between the two, and the
22
+ * local copy is the one that is definitely there.
23
+ */
24
+ export declare function shouldFetchBundle(env?: NodeJS.ProcessEnv): boolean;
25
+ /**
26
+ * Download and unpack a bundle, returning the directory it landed in.
27
+ *
28
+ * Downloads to a file rather than streaming into `tar`, deliberately. A stream
29
+ * that dies mid-transfer leaves `tar` having successfully extracted a prefix of
30
+ * the archive and exiting 0 — the half-unpacked bundle this module exists to
31
+ * refuse. Writing the whole tarball first means a truncated download is caught
32
+ * by `tar` as a corrupt archive, which is an error.
33
+ */
34
+ export declare function fetchBundle(options: FetchBundleOptions): Promise<string>;
35
+ /**
36
+ * Find the bundle root inside an unpacked directory.
37
+ *
38
+ * Tolerates one level of nesting, because whether a tarball has a top-level
39
+ * directory depends on how it was created — `tar czf x.tgz dist-bundle` and
40
+ * `tar czf x.tgz -C dist-bundle .` produce different shapes from the same
41
+ * files, and both are things a build script does.
42
+ */
43
+ export declare function bundleRootIn(directory: string): string | null;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Locate a package by walking `node_modules` up from a directory.
3
+ *
4
+ * Lives here rather than in `driver.ts` because both the driver loader and the
5
+ * version check need it, and `driver.ts` already imports this module.
6
+ */
7
+ export declare function findPackageDir(fromDir: string, packageName: string): string | undefined;
8
+ /**
9
+ * Compare two versions, or `undefined` when either cannot be parsed.
10
+ *
11
+ * Follows semver on the one rule that matters here: a prerelease sorts *below*
12
+ * the release it leads to, so `0.12.1-canary.g7f1150f` < `0.12.1`. Without that
13
+ * rule every canary would read as newer than the stable it precedes, and the
14
+ * canaries are precisely where fixes land first.
15
+ */
16
+ export declare function compareVersions(a: string, b: string): number | undefined;
17
+ export interface DriverSkew {
18
+ /** True only when both versions parsed AND the driver is genuinely older. */
19
+ stale: boolean;
20
+ /**
21
+ * A clause naming both versions, ready to append to a sentence. Present
22
+ * whenever both versions are known — including when they agree, because
23
+ * "the driver is current" is the fact that redirects an investigation away
24
+ * from staleness and toward the real cause.
25
+ */
26
+ detail?: string;
27
+ }
28
+ /**
29
+ * Describe how a driver's version relates to the runtime asking it for work.
30
+ *
31
+ * Returns `stale: false` whenever it cannot tell — an unknown version, an
32
+ * unparseable one, a driver that is newer. Silence beats a confident wrong
33
+ * diagnosis, and the caller still prints its own local fact either way.
34
+ */
35
+ export declare function describeDriverSkew(driverVersion: string | undefined, runtimeVersion: string | undefined): DriverSkew;
36
+ /**
37
+ * How to get a project's schema applied, written once and owned by the runtime.
38
+ *
39
+ * This text lives in `@rebasepro/server` on purpose. The equivalent guidance in
40
+ * the Postgres driver's schema-drift warning can only ever be as current as the
41
+ * driver a tenant pinned — so the tenants who most need to be told "your driver
42
+ * is too old" are exactly the ones whose driver still prints the old advice.
43
+ * The image's copy of this package is never stale, so guidance printed from
44
+ * here always reflects the platform as it is today.
45
+ *
46
+ * Both audiences get a line because the runtime cannot reliably tell which one
47
+ * it is serving, and naming only the pnpm scripts — as this once did everywhere
48
+ * — tells a managed operator to run a command that structurally cannot reach
49
+ * their in-cluster database.
50
+ */
51
+ export declare function schemaRecoveryGuidance(options?: {
52
+ staleDriver?: boolean;
53
+ }): string;
54
+ /**
55
+ * The installed version of a package, read from its `package.json`.
56
+ *
57
+ * Returns `undefined` rather than throwing for every failure — a missing or
58
+ * malformed manifest degrades the diagnosis, and must never take down a boot
59
+ * that would otherwise have served.
60
+ */
61
+ export declare function readPackageVersion(packageDir: string): string | undefined;
62
+ /**
63
+ * The version of `@rebasepro/server` this deployment is actually running.
64
+ *
65
+ * Resolved from the same roots the drivers are, and that is the point rather
66
+ * than a convenience. In a managed container `docker/entrypoint.mjs` replaces
67
+ * `/bundle/node_modules/@rebasepro/server` with a link to the image's copy, so
68
+ * reading the manifest *through the bundle* reports the version that will
69
+ * really execute — which is exactly the number the driver has to be compared
70
+ * against. Reading this package's own manifest by module path would report the
71
+ * same thing in the happy case and quietly lie in the case that matters: a
72
+ * bundle whose link did not happen.
73
+ *
74
+ * Returns `undefined` when the package cannot be found; every caller treats
75
+ * that as "cannot judge" and stays quiet.
76
+ */
77
+ export declare function readRuntimeVersion(roots: string[]): string | undefined;