@rebasepro/server 0.23.0 → 0.24.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 (181) hide show
  1. package/README.md +1 -1
  2. package/bin/rebase-server.js +4 -2
  3. package/dist/{GCSStorageController-CjrA4PMo.js → GCSStorageController-BSiP1c-f.js} +22 -8
  4. package/dist/GCSStorageController-BSiP1c-f.js.map +1 -0
  5. package/dist/{S3StorageController-B6pKDNVj.js → S3StorageController-CAwFRgjV.js} +19 -7
  6. package/dist/S3StorageController-CAwFRgjV.js.map +1 -0
  7. package/dist/api/ast-schema-editor.d.ts +92 -1
  8. package/dist/api/errors.d.ts +9 -0
  9. package/dist/api/live-schema-routes.d.ts +38 -8
  10. package/dist/api/logs-routes.d.ts +39 -1
  11. package/dist/api/openapi-generator.d.ts +17 -0
  12. package/dist/api/rest/api-generator.d.ts +44 -10
  13. package/dist/api/rest/write-validation.d.ts +2 -2
  14. package/dist/api/types.d.ts +17 -1
  15. package/dist/{ast-schema-editor-Mvr50v_S.js → ast-schema-editor-CWqS_sLJ.js} +309 -11
  16. package/dist/ast-schema-editor-CWqS_sLJ.js.map +1 -0
  17. package/dist/auth/access.d.ts +105 -0
  18. package/dist/auth/adapter-middleware.d.ts +2 -1
  19. package/dist/auth/address-ownership.d.ts +16 -1
  20. package/dist/auth/admin-roles-route.d.ts +4 -2
  21. package/dist/auth/admin-roles.d.ts +17 -20
  22. package/dist/auth/admin-users-route.d.ts +1 -0
  23. package/dist/auth/api-keys/api-key-middleware.d.ts +56 -55
  24. package/dist/auth/api-keys/api-key-routes.d.ts +41 -11
  25. package/dist/auth/api-keys/api-key-store.d.ts +31 -8
  26. package/dist/auth/api-keys/api-key-types.d.ts +14 -16
  27. package/dist/auth/api-keys/http-operation.d.ts +19 -0
  28. package/dist/auth/api-keys/index.d.ts +11 -11
  29. package/dist/auth/api-keys/key-grant.d.ts +41 -0
  30. package/dist/auth/api-keys/legacy-permissions.d.ts +33 -0
  31. package/dist/auth/auth-hooks.d.ts +46 -7
  32. package/dist/auth/builtin-auth-adapter.d.ts +8 -0
  33. package/dist/auth/cookie-utils.d.ts +7 -0
  34. package/dist/auth/deliverable-address.d.ts +6 -0
  35. package/dist/auth/email-change-routes.d.ts +41 -0
  36. package/dist/auth/expired-token-sweep.d.ts +67 -0
  37. package/dist/auth/impersonation.d.ts +110 -0
  38. package/dist/auth/index.d.ts +4 -2
  39. package/dist/auth/interfaces.d.ts +110 -59
  40. package/dist/auth/jwt.d.ts +49 -3
  41. package/dist/auth/magic-link-routes.d.ts +2 -6
  42. package/dist/auth/mfa-routes.d.ts +2 -9
  43. package/dist/auth/middleware.d.ts +17 -5
  44. package/dist/auth/otp-routes.d.ts +2 -6
  45. package/dist/auth/passwordless-signup.d.ts +27 -0
  46. package/dist/auth/platform-token.d.ts +122 -0
  47. package/dist/auth/rate-limiter.d.ts +41 -0
  48. package/dist/auth/routes.d.ts +45 -0
  49. package/dist/auth/scope-routes.d.ts +22 -0
  50. package/dist/auth/session-routes.d.ts +11 -6
  51. package/dist/auth/token-revocation.d.ts +50 -1
  52. package/dist/auth/verify-credential.d.ts +28 -0
  53. package/dist/{auth-B-GIMpDG.js → auth-DMLngxn_.js} +2159 -569
  54. package/dist/auth-DMLngxn_.js.map +1 -0
  55. package/dist/backend-DTAOsLQc.js.map +1 -1
  56. package/dist/backup/backup-common.d.ts +10 -0
  57. package/dist/backup/backup-routes.d.ts +24 -4
  58. package/dist/backup/backup-schedule.d.ts +33 -0
  59. package/dist/backup/backup-storage.d.ts +14 -0
  60. package/dist/backup/index.d.ts +2 -0
  61. package/dist/backup-CN0s50D2.js +444 -0
  62. package/dist/backup-CN0s50D2.js.map +1 -0
  63. package/dist/boot/bundle.d.ts +19 -0
  64. package/dist/boot/env.d.ts +49 -4
  65. package/dist/boot/security-headers.d.ts +26 -0
  66. package/dist/boot/static-routing.d.ts +56 -0
  67. package/dist/collection_patch-BRu-BvDv.js +472 -0
  68. package/dist/collection_patch-BRu-BvDv.js.map +1 -0
  69. package/dist/{contract-routes-CbFjuBwa.js → contract-routes-fz8i4pxs.js} +17 -4
  70. package/dist/contract-routes-fz8i4pxs.js.map +1 -0
  71. package/dist/cron/cron-scheduler.d.ts +25 -20
  72. package/dist/cron/cron-store.d.ts +6 -2
  73. package/dist/{cron-loader-DfTj2Hbi.js → cron-loader-CwaANlOG.js} +4 -4
  74. package/dist/cron-loader-CwaANlOG.js.map +1 -0
  75. package/dist/{cron-routes-eE8nif_b.js → cron-routes-Bc-SB0Se.js} +10 -7
  76. package/dist/cron-routes-Bc-SB0Se.js.map +1 -0
  77. package/dist/{cron-scheduler-B0pLfAix.js → cron-scheduler-CYQgco86.js} +52 -34
  78. package/dist/cron-scheduler-CYQgco86.js.map +1 -0
  79. package/dist/{cron-store-TcoGz-xS.js → cron-store-D2Q9-Aco.js} +10 -15
  80. package/dist/cron-store-D2Q9-Aco.js.map +1 -0
  81. package/dist/{ddl-bootstrap-C6mo0Kmz.js → ddl-bootstrap-BaqMSa4Y.js} +2 -2
  82. package/dist/{ddl-bootstrap-C6mo0Kmz.js.map → ddl-bootstrap-BaqMSa4Y.js.map} +1 -1
  83. package/dist/email/index.d.ts +2 -2
  84. package/dist/email/templates.d.ts +22 -0
  85. package/dist/email/types.d.ts +26 -0
  86. package/dist/env.d.ts +1 -2
  87. package/dist/{errors-DWsX4yTd.js → errors-D6_y86c5.js} +102 -8
  88. package/dist/errors-D6_y86c5.js.map +1 -0
  89. package/dist/{function-loader-xnbDAPfa.js → function-loader-D7o5Epjj.js} +2 -2
  90. package/dist/{function-loader-xnbDAPfa.js.map → function-loader-D7o5Epjj.js.map} +1 -1
  91. package/dist/{function-routes-Chet4-lB.js → function-routes-CaNG4waN.js} +24 -12
  92. package/dist/function-routes-CaNG4waN.js.map +1 -0
  93. package/dist/functions/context.d.ts +17 -6
  94. package/dist/functions/guards.d.ts +22 -5
  95. package/dist/functions/index.d.ts +2 -2
  96. package/dist/functions/index.js +90 -36
  97. package/dist/functions/index.js.map +1 -1
  98. package/dist/{history-recorder-B4MpJfJK.js → history-recorder-Nr8zLvoU.js} +4 -4
  99. package/dist/{history-recorder-B4MpJfJK.js.map → history-recorder-Nr8zLvoU.js.map} +1 -1
  100. package/dist/{history-store-BhxWOuz9.js → history-store-rcAm_xFR.js} +2 -2
  101. package/dist/{history-store-BhxWOuz9.js.map → history-store-rcAm_xFR.js.map} +1 -1
  102. package/dist/index.d.ts +8 -2
  103. package/dist/index.es.js +3084 -753
  104. package/dist/index.es.js.map +1 -1
  105. package/dist/init/health.d.ts +17 -2
  106. package/dist/init/shutdown.d.ts +10 -0
  107. package/dist/init.d.ts +54 -0
  108. package/dist/{jobs-CazMYhyy.js → jobs-DqYNfquG.js} +5 -5
  109. package/dist/{jobs-CazMYhyy.js.map → jobs-DqYNfquG.js.map} +1 -1
  110. package/dist/{jwt-DnQHNFCl.js → jwt-R6bSPMjk.js} +39 -15
  111. package/dist/{jwt-DnQHNFCl.js.map → jwt-R6bSPMjk.js.map} +1 -1
  112. package/dist/{keys-CogCQpxG.js → keys-GAVZqbqx.js} +3 -17
  113. package/dist/{keys-CogCQpxG.js.map → keys-GAVZqbqx.js.map} +1 -1
  114. package/dist/{logger-DO2PZc4i.js → logger-D-S-hO5e.js} +26 -3
  115. package/dist/logger-D-S-hO5e.js.map +1 -0
  116. package/dist/{logs-routes-Bj4TYYUl.js → logs-routes-DAdv37GI.js} +48 -8
  117. package/dist/logs-routes-DAdv37GI.js.map +1 -0
  118. package/dist/mcp/consent-page.d.ts +1 -1
  119. package/dist/mcp/mcp-routes.d.ts +7 -0
  120. package/dist/mcp/mcp-tools.d.ts +15 -9
  121. package/dist/mcp/oauth-metadata.d.ts +21 -16
  122. package/dist/mcp/oauth-routes.d.ts +7 -1
  123. package/dist/{openapi-generator-O_O24MAT.js → openapi-generator-DAq_XVDu.js} +104 -13
  124. package/dist/openapi-generator-DAq_XVDu.js.map +1 -0
  125. package/dist/{proxy-Czngl3p9.js → proxy-qRlqeUmO.js} +2 -2
  126. package/dist/{proxy-Czngl3p9.js.map → proxy-qRlqeUmO.js.map} +1 -1
  127. package/dist/{query-parser-DGRVFNM3.js → query-parser-BgiKJKvc.js} +6 -56
  128. package/dist/query-parser-BgiKJKvc.js.map +1 -0
  129. package/dist/{request-timeout-C_4C2BeR.js → request-timeout-DgH7j8qO.js} +3 -3
  130. package/dist/{request-timeout-C_4C2BeR.js.map → request-timeout-DgH7j8qO.js.map} +1 -1
  131. package/dist/schema-edit/apply-schema-change.d.ts +63 -3
  132. package/dist/schema-edit/project-root.d.ts +3 -2
  133. package/dist/schema-edit/remote-source.d.ts +9 -4
  134. package/dist/{schema-editor-routes-C5-lh_jO.js → schema-editor-routes-oIyuWl3L.js} +12 -7
  135. package/dist/schema-editor-routes-oIyuWl3L.js.map +1 -0
  136. package/dist/serve-spa.d.ts +58 -0
  137. package/dist/services/routed-realtime-service.d.ts +11 -0
  138. package/dist/soft-delete-params-BWPilMPF.js +59 -0
  139. package/dist/soft-delete-params-BWPilMPF.js.map +1 -0
  140. package/dist/{src-vkcwKXbT.js → src-CatHFUym.js} +439 -20
  141. package/dist/src-CatHFUym.js.map +1 -0
  142. package/dist/{src-pmvW7BFx.js → src-I3aG1PcY.js} +252 -70
  143. package/dist/src-I3aG1PcY.js.map +1 -0
  144. package/dist/storage/GCSStorageController.d.ts +2 -0
  145. package/dist/storage/LocalStorageController.d.ts +2 -0
  146. package/dist/storage/S3StorageController.d.ts +2 -0
  147. package/dist/storage/index.d.ts +2 -2
  148. package/dist/storage/property-limits.d.ts +41 -6
  149. package/dist/storage/request-keys.d.ts +15 -0
  150. package/dist/storage/requested-object.d.ts +74 -0
  151. package/dist/storage/routes.d.ts +36 -18
  152. package/dist/storage/tus-handler.d.ts +30 -5
  153. package/dist/storage/types.d.ts +19 -0
  154. package/dist/types-BfKcm9do.js.map +1 -1
  155. package/dist/utils/logger.d.ts +12 -0
  156. package/package.json +5 -5
  157. package/dist/GCSStorageController-CjrA4PMo.js.map +0 -1
  158. package/dist/S3StorageController-B6pKDNVj.js.map +0 -1
  159. package/dist/admin-roles-vYdp_Pil.js +0 -36
  160. package/dist/admin-roles-vYdp_Pil.js.map +0 -1
  161. package/dist/admin_block-DxKLmdiv.js +0 -206
  162. package/dist/admin_block-DxKLmdiv.js.map +0 -1
  163. package/dist/ast-schema-editor-Mvr50v_S.js.map +0 -1
  164. package/dist/auth/api-keys/api-key-permission-guard.d.ts +0 -65
  165. package/dist/auth-B-GIMpDG.js.map +0 -1
  166. package/dist/backup-D7YR94N3.js +0 -253
  167. package/dist/backup-D7YR94N3.js.map +0 -1
  168. package/dist/contract-routes-CbFjuBwa.js.map +0 -1
  169. package/dist/cron-loader-DfTj2Hbi.js.map +0 -1
  170. package/dist/cron-routes-eE8nif_b.js.map +0 -1
  171. package/dist/cron-scheduler-B0pLfAix.js.map +0 -1
  172. package/dist/cron-store-TcoGz-xS.js.map +0 -1
  173. package/dist/errors-DWsX4yTd.js.map +0 -1
  174. package/dist/function-routes-Chet4-lB.js.map +0 -1
  175. package/dist/logger-DO2PZc4i.js.map +0 -1
  176. package/dist/logs-routes-Bj4TYYUl.js.map +0 -1
  177. package/dist/openapi-generator-O_O24MAT.js.map +0 -1
  178. package/dist/query-parser-DGRVFNM3.js.map +0 -1
  179. package/dist/schema-editor-routes-C5-lh_jO.js.map +0 -1
  180. package/dist/src-pmvW7BFx.js.map +0 -1
  181. package/dist/src-vkcwKXbT.js.map +0 -1
package/README.md CHANGED
@@ -33,7 +33,7 @@ This is the central orchestrator for any Rebase backend. It provides the framewo
33
33
 
34
34
  Also exports `HonoEnv`, the auth, email, storage, history and cron modules, custom functions, logging utilities, and the driver registry. The abstract driver interfaces (`DatabaseAdapter`, `AuthAdapter`, `DataDriver`, …) are not re-exported: import them from `@rebasepro/types`.
35
35
 
36
- `@rebasepro/server/functions` is the portable surface a function file imports — `defineFunction`, the `requireAuth`/`requireAdmin` guards, `getUser`, `rebase` and the rest. The package also ships the `rebase-server` binary, which runs a built project bundle (`rebase-server ./dist-bundle`); it is what the `rebasepro/server` image executes.
36
+ `@rebasepro/server/functions` is the portable surface a function file imports — `defineFunction`, the `requireAuth`/`requireAdmin`/`requireScope` guards, `getUser`, `getScopes`, `hasScope`, `rebase` and the rest. The package also ships the `rebase-server` binary, which runs a built project bundle (`rebase-server ./dist-bundle`); it is what the `rebasepro/server` image executes.
37
37
 
38
38
  ## Quick Start
39
39
 
@@ -31,6 +31,9 @@ Key environment variables:
31
31
  window, requires CORS_ORIGINS, refuses local file
32
32
  storage and turns the OpenAPI docs off
33
33
  DATABASE_URL Connection string for the default database (required)
34
+ ALLOW_LOCALHOST_IN_PRODUCTION "true" when the database (or any other
35
+ service a variable names) runs on this same machine.
36
+ Production refuses localhost addresses without it
34
37
  JWT_SECRET Signing secret, >=32 chars (required in production)
35
38
  REBASE_SERVICE_KEY Server-to-server credential that bypasses row-level
36
39
  security. Treat it like a database superuser password
@@ -46,8 +49,7 @@ Key environment variables:
46
49
  tables and columns at boot, INCLUDING your collections',
47
50
  additively, and applies their row-level security. It
48
51
  never alters, drops or narrows: those go through
49
- 'rebase db push' from a checkout, along with
50
- junction-table RLS for many-to-many relations
52
+ 'rebase db push' from a checkout
51
53
 
52
54
  Additional databases and buckets are configured by suffixing the variable with
53
55
  the source key, e.g. DATABASE_URL__ANALYTICS or S3_BUCKET__MEDIA.
@@ -3,7 +3,8 @@ import __rebaseProcess from "process";
3
3
  globalThis.process ??= __rebaseProcess;
4
4
  __rebaseCreateRequire(import.meta.url);
5
5
  import { n as __exportAll } from "./rolldown-runtime-dW7B1o5h.js";
6
- import { c as listingPrefix, s as folderKey } from "./keys-CogCQpxG.js";
6
+ import { t as ApiError } from "./errors-D6_y86c5.js";
7
+ import { c as listingPrefix, s as folderKey } from "./keys-GAVZqbqx.js";
7
8
  import "./types-BfKcm9do.js";
8
9
  //#region src/storage/GCSStorageController.ts
9
10
  /**
@@ -146,7 +147,11 @@ var GCSStorageController = class {
146
147
  const contentType = fileMetadata.contentType || "application/octet-stream";
147
148
  const fileName = resolvedPath.split("/").pop() || resolvedPath;
148
149
  const blob = new Blob([new Uint8Array(contents)], { type: contentType });
149
- return new File([blob], fileName, { type: contentType });
150
+ const updated = typeof fileMetadata.updated === "string" ? Date.parse(fileMetadata.updated) : NaN;
151
+ return new File([blob], fileName, {
152
+ type: contentType,
153
+ lastModified: Number.isNaN(updated) ? void 0 : updated
154
+ });
150
155
  } catch (error) {
151
156
  if (isNotFoundError(error)) return null;
152
157
  throw error;
@@ -169,15 +174,17 @@ var GCSStorageController = class {
169
174
  }
170
175
  async listObjects(prefix, options) {
171
176
  const resolvedBucket = this.getBucket(options?.bucket);
172
- const [files, , filesApiResponse] = await (await this.getClient()).bucket(resolvedBucket).getFiles({
173
- prefix: listingPrefix(prefix),
177
+ const gcsBucket = (await this.getClient()).bucket(resolvedBucket);
178
+ const folderPrefix = listingPrefix(prefix);
179
+ const [listed, , filesApiResponse] = await gcsBucket.getFiles({
180
+ prefix: folderPrefix,
174
181
  delimiter: "/",
175
182
  maxResults: options?.maxResults ?? 1e3,
176
183
  pageToken: options?.pageToken,
177
184
  autoPaginate: false
178
185
  });
179
186
  return {
180
- items: files.map((file) => ({
187
+ items: listed.filter((file) => file.name !== folderPrefix).map((file) => ({
181
188
  bucket: resolvedBucket,
182
189
  fullPath: file.name,
183
190
  name: file.name.split("/").pop() || "",
@@ -199,12 +206,19 @@ var GCSStorageController = class {
199
206
  nextPageToken: filesApiResponse?.nextPageToken
200
207
  };
201
208
  }
209
+ /** See {@link StorageController.maxFileSize}. */
210
+ maxFileSize() {
211
+ return this.config.maxFileSize ?? 52428800;
212
+ }
202
213
  /**
203
214
  * Validate file before upload
204
215
  */
205
216
  validateFile(file) {
206
- const maxSize = this.config.maxFileSize ?? 52428800;
207
- if (file.size > maxSize) throw new Error(`File size ${file.size} exceeds maximum allowed size ${maxSize}`);
217
+ const maxSize = this.maxFileSize();
218
+ if (file.size > maxSize) throw new ApiError(413, "PAYLOAD_TOO_LARGE", `File size ${file.size} exceeds this storage source's maximum of ${maxSize} bytes.`, {
219
+ size: file.size,
220
+ maxFileSize: maxSize
221
+ });
208
222
  if (this.config.allowedMimeTypes && this.config.allowedMimeTypes.length > 0) {
209
223
  if (!this.config.allowedMimeTypes.includes(file.type)) throw new Error(`File type ${file.type} is not allowed. Allowed types: ${this.config.allowedMimeTypes.join(", ")}`);
210
224
  }
@@ -239,4 +253,4 @@ function isNotFoundError(error) {
239
253
  //#endregion
240
254
  export { GCSStorageController_exports as n, GCSStorageController as t };
241
255
 
242
- //# sourceMappingURL=GCSStorageController-CjrA4PMo.js.map
256
+ //# sourceMappingURL=GCSStorageController-BSiP1c-f.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GCSStorageController-BSiP1c-f.js","names":[],"sources":["../src/storage/GCSStorageController.ts"],"sourcesContent":["/**\n * Google Cloud Storage controller (works with GCS and Firebase Storage)\n *\n * The `@google-cloud/storage` package is loaded lazily so it remains an\n * optional peer dependency — users who don't need GCS never pay for the\n * import cost or need it installed.\n */\n\nimport {\n StorageController,\n GCSStorageConfig,\n DEFAULT_MAX_FILE_SIZE\n} from \"./types\";\nimport { folderKey, listingPrefix } from \"./keys\";\nimport { ApiError } from \"../api/errors\";\nimport {\n UploadFileProps,\n UploadFileResult,\n DownloadConfig,\n DownloadMetadata,\n StorageListResult,\n StorageReference\n} from \"@rebasepro/types\";\n\n// ---------------------------------------------------------------------------\n// Lazy import of @google-cloud/storage\n// ---------------------------------------------------------------------------\n\nlet gcsModule: typeof import(\"@google-cloud/storage\") | null = null;\n\nasync function loadGCS(): Promise<typeof import(\"@google-cloud/storage\")> {\n if (gcsModule) return gcsModule;\n try {\n gcsModule = await import(\"@google-cloud/storage\");\n return gcsModule;\n } catch {\n throw new Error(\n \"Google Cloud Storage is required for the GCS storage controller. \" +\n \"Install it with: pnpm add @google-cloud/storage\"\n );\n }\n}\n\n// ---------------------------------------------------------------------------\n// GCSStorageController\n// ---------------------------------------------------------------------------\n\n/**\n * Google Cloud Storage implementation of `StorageController`.\n *\n * Works with standard GCS buckets **and** Firebase Storage buckets\n * (e.g. `\"my-project.appspot.com\"`).\n */\nexport class GCSStorageController implements StorageController {\n private config: GCSStorageConfig;\n private client: InstanceType<typeof import(\"@google-cloud/storage\").Storage> | null = null;\n\n constructor(config: GCSStorageConfig) {\n this.config = config;\n }\n\n // ------------------------------------------------------------------\n // Lazy client initialisation\n // ------------------------------------------------------------------\n\n private async getClient(): Promise<InstanceType<typeof import(\"@google-cloud/storage\").Storage>> {\n if (this.client) return this.client;\n const { Storage } = await loadGCS();\n this.client = new Storage({\n projectId: this.config.projectId,\n keyFilename: this.config.keyFilename,\n credentials: this.config.credentials\n });\n return this.client;\n }\n\n // ------------------------------------------------------------------\n // StorageController interface\n // ------------------------------------------------------------------\n\n getType(): \"gcs\" {\n return \"gcs\";\n }\n\n /**\n * The configured bucket, and the logical `default` that maps to it.\n * @see S3StorageController.knownBuckets\n */\n knownBuckets(): string[] {\n return [\"default\", this.config.bucket];\n }\n\n async putObject({\n file,\n key,\n metadata,\n bucket\n }: UploadFileProps): Promise<UploadFileResult> {\n this.validateFile(file);\n\n const usedBucket = this.getBucket(bucket);\n const client = await this.getClient();\n const gcsFile = client.bucket(usedBucket).file(key);\n\n // Convert File to Buffer\n const arrayBuffer = await file.arrayBuffer();\n const buffer = Buffer.from(arrayBuffer);\n\n await gcsFile.save(buffer, {\n contentType: file.type,\n metadata: metadata ? { metadata: this.flattenMetadata(metadata) } : undefined\n });\n\n return {\n key,\n bucket: usedBucket,\n storageUrl: `gs://${usedBucket}/${key}`\n };\n }\n\n /**\n * Read the object's metadata and nothing else — no signing.\n *\n * Signing needs a private key. With `keyFilename` or `credentials` the\n * client has one; on Cloud Run, GKE or GCE it has only the metadata\n * server's token, and falls back to the IAM Credentials `signBlob` API,\n * which the runtime account may call on itself only when it holds\n * `iam.serviceAccounts.signBlob` there. That is not a default grant.\n */\n async getMetadata(key: string, bucket?: string): Promise<DownloadMetadata | null> {\n // Handle gs:// and s3:// URLs\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(gs|s3):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n try {\n const client = await this.getClient();\n const [fileMetadata] = await client.bucket(resolvedBucket).file(resolvedPath).getMetadata();\n\n return {\n bucket: resolvedBucket,\n fullPath: resolvedPath,\n name: resolvedPath.split(\"/\").pop() || resolvedPath,\n size: Number(fileMetadata.size) || 0,\n contentType: fileMetadata.contentType || \"application/octet-stream\",\n customMetadata: (fileMetadata.metadata as Record<string, string> | undefined) || {}\n };\n } catch (error: unknown) {\n if (isNotFoundError(error)) {\n return null;\n }\n throw error;\n }\n }\n\n async getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig> {\n // Metadata first, to check existence and populate the response.\n const metadata = await this.getMetadata(key, bucket);\n if (!metadata) {\n return {\n url: null,\n fileNotFound: true\n };\n }\n\n const client = await this.getClient();\n const expiresIn = this.config.signedUrlExpiration ?? 3600;\n const [url] = await client.bucket(metadata.bucket).file(metadata.fullPath).getSignedUrl({\n action: \"read\",\n expires: Date.now() + expiresIn * 1000\n });\n\n return {\n url,\n metadata\n };\n }\n\n async getObject(key: string, bucket?: string): Promise<File | null> {\n // Handle gs:// and s3:// URLs\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(gs|s3):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n try {\n const client = await this.getClient();\n const gcsFile = client.bucket(resolvedBucket).file(resolvedPath);\n\n const [contents] = await gcsFile.download();\n\n // Get content type from metadata\n const [fileMetadata] = await gcsFile.getMetadata();\n const contentType = fileMetadata.contentType || \"application/octet-stream\";\n const fileName = resolvedPath.split(\"/\").pop() || resolvedPath;\n\n const blob = new Blob([new Uint8Array(contents)], { type: contentType });\n // The object's own version — see `S3StorageController.getObject`.\n const updated = typeof fileMetadata.updated === \"string\" ? Date.parse(fileMetadata.updated) : NaN;\n return new File([blob], fileName, {\n type: contentType,\n lastModified: Number.isNaN(updated) ? undefined : updated\n });\n } catch (error: unknown) {\n if (isNotFoundError(error)) {\n return null;\n }\n throw error;\n }\n }\n\n async deleteObject(key: string, bucket?: string): Promise<void> {\n // Handle gs:// and s3:// URLs\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(gs|s3):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n const client = await this.getClient();\n const gcsFile = client.bucket(resolvedBucket).file(resolvedPath);\n\n await gcsFile.delete({ ignoreNotFound: true });\n }\n\n async listObjects(prefix: string, options?: {\n bucket?: string;\n maxResults?: number;\n pageToken?: string;\n }): Promise<StorageListResult> {\n const resolvedBucket = this.getBucket(options?.bucket);\n const client = await this.getClient();\n const gcsBucket = client.bucket(resolvedBucket);\n\n // Fetch files (objects) matching the prefix\n const folderPrefix = listingPrefix(prefix);\n const [listed, , filesApiResponse] = await gcsBucket.getFiles({\n prefix: folderPrefix,\n delimiter: \"/\",\n maxResults: options?.maxResults ?? 1000,\n pageToken: options?.pageToken,\n autoPaginate: false\n });\n\n // The folder's own marker object is not something in the folder —\n // see `S3StorageController.listObjects`.\n const files = listed.filter(file => file.name !== folderPrefix);\n const items: StorageReference[] = files.map(file => ({\n bucket: resolvedBucket,\n fullPath: file.name,\n name: file.name.split(\"/\").pop() || \"\",\n parent: null as never,\n root: null as never,\n toString: () => `gs://${resolvedBucket}/${file.name}`\n }));\n\n // Common prefixes (folder-like entries) come from the API response\n const apiPrefixes: string[] =\n (filesApiResponse as Record<string, unknown> | undefined)?.prefixes as string[] ?? [];\n\n const prefixes: StorageReference[] = apiPrefixes.map(p => {\n // Without the trailing slash GCS puts on a common prefix, so a\n // `fullPath` from a listing is a key any controller accepts.\n const folder = folderKey(p);\n return {\n bucket: resolvedBucket,\n fullPath: folder,\n name: folder.split(\"/\").pop() || \"\",\n parent: null as never,\n root: null as never,\n toString: () => `gs://${resolvedBucket}/${folder}`\n };\n });\n\n // The next page token is returned on the query object (second element)\n const nextPageToken =\n (filesApiResponse as Record<string, unknown> | undefined)?.nextPageToken as string | undefined;\n\n return {\n items,\n prefixes,\n nextPageToken\n };\n }\n\n // ------------------------------------------------------------------\n // Private helpers\n // ------------------------------------------------------------------\n\n /** See {@link StorageController.maxFileSize}. */\n maxFileSize(): number {\n return this.config.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;\n }\n\n /**\n * Validate file before upload\n */\n private validateFile(file: File): void {\n const maxSize = this.maxFileSize();\n if (file.size > maxSize) {\n // An ApiError, so the refusal reaches the caller as what it is. A\n // plain Error was a 500 on `/upload` and a 502 on TUS — after the\n // client had sent every byte.\n throw new ApiError(\n 413,\n \"PAYLOAD_TOO_LARGE\",\n `File size ${file.size} exceeds this storage source's maximum of ${maxSize} bytes.`,\n { size: file.size, maxFileSize: maxSize }\n );\n }\n\n if (this.config.allowedMimeTypes && this.config.allowedMimeTypes.length > 0) {\n if (!this.config.allowedMimeTypes.includes(file.type)) {\n throw new Error(`File type ${file.type} is not allowed. Allowed types: ${this.config.allowedMimeTypes.join(\", \")}`);\n }\n }\n }\n\n /**\n * Get the bucket name — either from parameter or config.\n */\n private getBucket(bucket?: string): string {\n // \"default\" is a logical bucket name used by local storage;\n // for GCS it should resolve to the configured bucket.\n if (!bucket || bucket === \"default\") return this.config.bucket;\n return bucket;\n }\n\n /**\n * Flatten nested metadata to string values (GCS custom metadata requirement)\n */\n private flattenMetadata(metadata: Record<string, unknown>): Record<string, string> {\n const flattened: Record<string, string> = {};\n for (const [key, value] of Object.entries(metadata)) {\n if (typeof value === \"string\") {\n flattened[key] = value;\n } else if (value !== undefined && value !== null) {\n flattened[key] = JSON.stringify(value);\n }\n }\n return flattened;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Error helpers\n// ---------------------------------------------------------------------------\n\n/**\n * Check whether an error represents a 404 / Not Found from the GCS client.\n */\nfunction isNotFoundError(error: unknown): boolean {\n if (typeof error !== \"object\" || error === null) return false;\n const err = error as Record<string, unknown>;\n if (err[\"code\"] === 404) return true;\n if (typeof err[\"message\"] === \"string\" && /not found/i.test(err[\"message\"])) return true;\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,IAAI,YAA2D;AAE/D,eAAe,UAA2D;CACtE,IAAI,WAAW,OAAO;CACtB,IAAI;EACA,YAAY,MAAM,OAAO;EACzB,OAAO;CACX,QAAQ;EACJ,MAAM,IAAI,MACN,kHAEJ;CACJ;AACJ;;;;;;;AAYA,IAAa,uBAAb,MAA+D;CAC3D;CACA,SAAsF;CAEtF,YAAY,QAA0B;EAClC,KAAK,SAAS;CAClB;CAMA,MAAc,YAAmF;EAC7F,IAAI,KAAK,QAAQ,OAAO,KAAK;EAC7B,MAAM,EAAE,YAAY,MAAM,QAAQ;EAClC,KAAK,SAAS,IAAI,QAAQ;GACtB,WAAW,KAAK,OAAO;GACvB,aAAa,KAAK,OAAO;GACzB,aAAa,KAAK,OAAO;EAC7B,CAAC;EACD,OAAO,KAAK;CAChB;CAMA,UAAiB;EACb,OAAO;CACX;;;;;CAMA,eAAyB;EACrB,OAAO,CAAC,WAAW,KAAK,OAAO,MAAM;CACzC;CAEA,MAAM,UAAU,EACZ,MACA,KACA,UACA,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AK,OAAO;CAChC,IAAI,OAAO,IAAI,eAAe,YAAY,aAAa,KAAK,IAAI,UAAU,GAAG,OAAO;CACpF,OAAO;AACX"}
@@ -3,7 +3,8 @@ import __rebaseProcess from "process";
3
3
  globalThis.process ??= __rebaseProcess;
4
4
  __rebaseCreateRequire(import.meta.url);
5
5
  import { n as __exportAll } from "./rolldown-runtime-dW7B1o5h.js";
6
- import { c as listingPrefix, s as folderKey } from "./keys-CogCQpxG.js";
6
+ import { t as ApiError } from "./errors-D6_y86c5.js";
7
+ import { c as listingPrefix, s as folderKey } from "./keys-GAVZqbqx.js";
7
8
  import "./types-BfKcm9do.js";
8
9
  //#region src/storage/S3StorageController.ts
9
10
  var S3StorageController_exports = /* @__PURE__ */ __exportAll({ S3StorageController: () => S3StorageController });
@@ -66,12 +67,19 @@ var S3StorageController = class {
66
67
  knownBuckets() {
67
68
  return ["default", this.config.bucket];
68
69
  }
70
+ /** See {@link StorageController.maxFileSize}. */
71
+ maxFileSize() {
72
+ return this.config.maxFileSize ?? 52428800;
73
+ }
69
74
  /**
70
75
  * Validate file before upload
71
76
  */
72
77
  validateFile(file) {
73
- const maxSize = this.config.maxFileSize ?? 52428800;
74
- if (file.size > maxSize) throw new Error(`File size ${file.size} exceeds maximum allowed size ${maxSize}`);
78
+ const maxSize = this.maxFileSize();
79
+ if (file.size > maxSize) throw new ApiError(413, "PAYLOAD_TOO_LARGE", `File size ${file.size} exceeds this storage source's maximum of ${maxSize} bytes.`, {
80
+ size: file.size,
81
+ maxFileSize: maxSize
82
+ });
75
83
  if (this.config.allowedMimeTypes && this.config.allowedMimeTypes.length > 0) {
76
84
  if (!this.config.allowedMimeTypes.includes(file.type)) throw new Error(`File type ${file.type} is not allowed. Allowed types: ${this.config.allowedMimeTypes.join(", ")}`);
77
85
  }
@@ -188,7 +196,10 @@ var S3StorageController = class {
188
196
  const contentType = response.ContentType || "application/octet-stream";
189
197
  const fileName = resolvedPath.split("/").pop() || resolvedPath;
190
198
  const blob = new Blob([buffer], { type: contentType });
191
- return new File([blob], fileName, { type: contentType });
199
+ return new File([blob], fileName, {
200
+ type: contentType,
201
+ lastModified: response.LastModified?.getTime()
202
+ });
192
203
  } catch (error) {
193
204
  const s3Error = error;
194
205
  if (s3Error.name === "NoSuchKey" || s3Error.$metadata?.httpStatusCode === 404) return null;
@@ -226,16 +237,17 @@ var S3StorageController = class {
226
237
  const resolvedBucket = this.getBucket(options?.bucket);
227
238
  const s3 = await loadS3();
228
239
  const client = await this.getClient();
240
+ const folderPrefix = listingPrefix(prefix);
229
241
  const command = new s3.ListObjectsV2Command({
230
242
  Bucket: resolvedBucket,
231
- Prefix: listingPrefix(prefix),
243
+ Prefix: folderPrefix,
232
244
  MaxKeys: options?.maxResults ?? 1e3,
233
245
  ContinuationToken: options?.pageToken,
234
246
  Delimiter: "/"
235
247
  });
236
248
  const response = await client.send(command);
237
249
  return {
238
- items: (response.Contents || []).map((obj) => ({
250
+ items: (response.Contents || []).filter((obj) => obj.Key !== folderPrefix).map((obj) => ({
239
251
  bucket: resolvedBucket,
240
252
  fullPath: obj.Key || "",
241
253
  name: (obj.Key || "").split("/").pop() || "",
@@ -261,4 +273,4 @@ var S3StorageController = class {
261
273
  //#endregion
262
274
  export { S3StorageController_exports as n, S3StorageController as t };
263
275
 
264
- //# sourceMappingURL=S3StorageController-B6pKDNVj.js.map
276
+ //# sourceMappingURL=S3StorageController-CAwFRgjV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"S3StorageController-CAwFRgjV.js","names":[],"sources":["../src/storage/S3StorageController.ts"],"sourcesContent":["/**\n * S3-compatible storage controller (works with AWS S3 and MinIO)\n */\n\nimport type { S3Client as S3ClientType, } from \"@aws-sdk/client-s3\";\nimport { DEFAULT_MAX_FILE_SIZE, S3StorageConfig, StorageController } from \"./types\";\nimport { folderKey, listingPrefix } from \"./keys\";\nimport { ApiError } from \"../api/errors\";\nimport {\n DownloadConfig,\n DownloadMetadata,\n StorageListResult,\n StorageReference,\n UploadFileProps,\n UploadFileResult\n} from \"@rebasepro/types\";\n\nlet _s3Module: typeof import(\"@aws-sdk/client-s3\") | undefined;\nlet _presignerModule: typeof import(\"@aws-sdk/s3-request-presigner\") | undefined;\n\nasync function loadS3() {\n if (!_s3Module) {\n try {\n _s3Module = await import(\"@aws-sdk/client-s3\");\n } catch {\n throw new Error(\n \"@aws-sdk/client-s3 is required for S3 storage. \" +\n \"Install it: pnpm add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner\"\n );\n }\n }\n return _s3Module;\n}\n\nasync function loadPresigner() {\n if (!_presignerModule) {\n try {\n _presignerModule = await import(\"@aws-sdk/s3-request-presigner\");\n } catch {\n throw new Error(\n \"@aws-sdk/s3-request-presigner is required for S3 storage. \" +\n \"Install it: pnpm add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner\"\n );\n }\n }\n return _presignerModule;\n}\n\n/**\n * S3-compatible storage implementation\n * Works with AWS S3 and MinIO (with forcePathStyle option)\n */\nexport class S3StorageController implements StorageController {\n private config: S3StorageConfig;\n private _client: S3ClientType | undefined;\n\n constructor(config: S3StorageConfig) {\n this.config = config;\n }\n\n /**\n * Lazily create and cache the S3 client on first use\n */\n private async getClient(): Promise<S3ClientType> {\n if (!this._client) {\n const s3 = await loadS3();\n this._client = new s3.S3Client({\n region: this.config.region || \"us-east-1\",\n endpoint: this.config.endpoint,\n forcePathStyle: this.config.forcePathStyle ?? !!this.config.endpoint, // Auto-enable for custom endpoints (MinIO)\n credentials: {\n accessKeyId: this.config.accessKeyId,\n secretAccessKey: this.config.secretAccessKey\n }\n });\n }\n return this._client;\n }\n\n getType(): \"s3\" {\n return \"s3\";\n }\n\n /**\n * The configured bucket, and the logical `default` that maps to it.\n *\n * Any other name went straight to S3 as a bucket name, so the request\n * parameter was a way to address *any* bucket the deployment's credentials\n * can reach — and a mistyped one came back as \"file not found\".\n */\n knownBuckets(): string[] {\n return [\"default\", this.config.bucket];\n }\n\n /** See {@link StorageController.maxFileSize}. */\n maxFileSize(): number {\n return this.config.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;\n }\n\n /**\n * Validate file before upload\n */\n private validateFile(file: File): void {\n const maxSize = this.maxFileSize();\n if (file.size > maxSize) {\n // An ApiError, so the refusal reaches the caller as what it is. A\n // plain Error was a 500 on `/upload` and a 502 on TUS — after the\n // client had sent every byte.\n throw new ApiError(\n 413,\n \"PAYLOAD_TOO_LARGE\",\n `File size ${file.size} exceeds this storage source's maximum of ${maxSize} bytes.`,\n { size: file.size, maxFileSize: maxSize }\n );\n }\n\n if (this.config.allowedMimeTypes && this.config.allowedMimeTypes.length > 0) {\n if (!this.config.allowedMimeTypes.includes(file.type)) {\n throw new Error(`File type ${file.type} is not allowed. Allowed types: ${this.config.allowedMimeTypes.join(\", \")}`);\n }\n }\n }\n\n /**\n * Get the bucket name - either from parameter or config\n */\n private getBucket(bucket?: string): string {\n // \"default\" is a logical bucket name used by local storage;\n // for S3 it should resolve to the configured bucket.\n if (!bucket || bucket === \"default\") return this.config.bucket;\n return bucket;\n }\n\n async putObject({\n file,\n key,\n metadata,\n bucket\n }: UploadFileProps): Promise<UploadFileResult> {\n this.validateFile(file);\n\n const usedBucket = this.getBucket(bucket);\n\n // Convert File to Buffer\n const arrayBuffer = await file.arrayBuffer();\n const buffer = Buffer.from(arrayBuffer);\n\n const s3 = await loadS3();\n const client = await this.getClient();\n\n const command = new s3.PutObjectCommand({\n Bucket: usedBucket,\n Key: key,\n Body: buffer,\n ContentType: file.type,\n Metadata: metadata ? this.flattenMetadata(metadata) : undefined\n });\n\n await client.send(command);\n\n return {\n key,\n bucket: usedBucket,\n storageUrl: `s3://${usedBucket}/${key}`\n };\n }\n\n /**\n * Flatten nested metadata to string values (S3 requirement)\n */\n private flattenMetadata(metadata: Record<string, unknown>): Record<string, string> {\n const flattened: Record<string, string> = {};\n for (const [key, value] of Object.entries(metadata)) {\n if (typeof value === \"string\") {\n flattened[key] = value;\n } else if (value !== undefined && value !== null) {\n flattened[key] = JSON.stringify(value);\n }\n }\n return flattened;\n }\n\n async getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig> {\n // Handle s3:// and gs:// URLs for backward compatibility\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(s3|gs):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n try {\n const s3 = await loadS3();\n const presigner = await loadPresigner();\n const client = await this.getClient();\n\n // First check if the object exists and get metadata\n const headCommand = new s3.HeadObjectCommand({\n Bucket: resolvedBucket,\n Key: resolvedPath\n });\n\n const headResult = await client.send(headCommand);\n\n // Generate a signed URL\n const getCommand = new s3.GetObjectCommand({\n Bucket: resolvedBucket,\n Key: resolvedPath\n });\n\n const expiresIn = this.config.signedUrlExpiration ?? 3600;\n const url = await presigner.getSignedUrl(client, getCommand, { expiresIn });\n\n const metadata: DownloadMetadata = {\n bucket: resolvedBucket,\n fullPath: resolvedPath,\n name: resolvedPath.split(\"/\").pop() || resolvedPath,\n size: headResult.ContentLength || 0,\n contentType: headResult.ContentType || \"application/octet-stream\",\n customMetadata: headResult.Metadata || {}\n };\n\n return {\n url,\n metadata\n };\n } catch (error: unknown) {\n const s3Error = error as { name?: string; $metadata?: { httpStatusCode?: number } };\n if (s3Error.name === \"NotFound\" || s3Error.$metadata?.httpStatusCode === 404) {\n return {\n url: null,\n fileNotFound: true\n };\n }\n throw error;\n }\n }\n\n async getObject(key: string, bucket?: string): Promise<File | null> {\n // Handle s3:// and gs:// URLs\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(s3|gs):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n try {\n const s3 = await loadS3();\n const client = await this.getClient();\n\n const command = new s3.GetObjectCommand({\n Bucket: resolvedBucket,\n Key: resolvedPath\n });\n\n const response = await client.send(command);\n\n if (!response.Body) {\n return null;\n }\n\n // Convert stream to buffer\n const chunks: Uint8Array[] = [];\n for await (const chunk of response.Body as AsyncIterable<Uint8Array>) {\n chunks.push(chunk);\n }\n const buffer = Buffer.concat(chunks);\n\n const contentType = response.ContentType || \"application/octet-stream\";\n const fileName = resolvedPath.split(\"/\").pop() || resolvedPath;\n\n const blob = new Blob([buffer], { type: contentType });\n // The object's own version. Left out, `File` stamps `Date.now()`,\n // and the routes build the `ETag`, `Last-Modified` and the\n // transform cache key from this — so every read was a new version:\n // no 304, no cache hit, and a fresh rendition written per request.\n return new File([blob], fileName, {\n type: contentType,\n lastModified: response.LastModified?.getTime()\n });\n } catch (error: unknown) {\n const s3Error = error as { name?: string; $metadata?: { httpStatusCode?: number } };\n if (s3Error.name === \"NoSuchKey\" || s3Error.$metadata?.httpStatusCode === 404) {\n return null;\n }\n throw error;\n }\n }\n\n async deleteObject(key: string, bucket?: string): Promise<void> {\n // Handle s3:// and gs:// URLs\n let resolvedPath = key;\n let resolvedBucket = this.getBucket(bucket);\n\n const match = key.match(/^(s3|gs):\\/\\//);\n if (match) {\n const protocolLength = match[0].length;\n const withoutProtocol = key.substring(protocolLength);\n const firstSlash = withoutProtocol.indexOf(\"/\");\n if (firstSlash > 0) {\n resolvedBucket = withoutProtocol.substring(0, firstSlash);\n resolvedPath = withoutProtocol.substring(firstSlash + 1);\n }\n }\n\n const s3 = await loadS3();\n const client = await this.getClient();\n\n const command = new s3.DeleteObjectCommand({\n Bucket: resolvedBucket,\n Key: resolvedPath\n });\n\n // Deleting something that is already gone is the outcome the caller\n // wanted, so it is not an error. AWS itself answers DeleteObject with\n // 204 for a key that never existed, but S3-compatible backends (MinIO,\n // Ceph, R2 in some configurations) do raise NoSuchKey — and the local\n // controller has always returned quietly in this case. Without this,\n // which of those a deployment happens to run decides whether\n // `DELETE /api/storage/:path` on a stale reference is a 200 or a 500.\n try {\n await client.send(command);\n } catch (error: unknown) {\n const s3Error = error as { name?: string; $metadata?: { httpStatusCode?: number } };\n if (s3Error.name === \"NoSuchKey\" || s3Error.$metadata?.httpStatusCode === 404) {\n return;\n }\n throw error;\n }\n }\n\n async listObjects(prefix: string, options?: {\n bucket?: string;\n maxResults?: number;\n pageToken?: string;\n }): Promise<StorageListResult> {\n const resolvedBucket = this.getBucket(options?.bucket);\n\n const s3 = await loadS3();\n const client = await this.getClient();\n\n const folderPrefix = listingPrefix(prefix);\n const command = new s3.ListObjectsV2Command({\n Bucket: resolvedBucket,\n Prefix: folderPrefix,\n MaxKeys: options?.maxResults ?? 1000,\n ContinuationToken: options?.pageToken,\n Delimiter: \"/\" // This gives us folder-like behavior\n });\n\n const response = await client.send(command);\n\n // The folder's own marker — the zero-byte `folder/` object that\n // `POST /folder` writes — is in `Contents` when the listed prefix is\n // that folder. It is how the folder exists, not something in it, and\n // listed it is an entry with an empty name. Local storage has no such\n // object to list.\n const objects = (response.Contents || []).filter(obj => obj.Key !== folderPrefix);\n const items: StorageReference[] = objects.map(obj => ({\n bucket: resolvedBucket,\n fullPath: obj.Key || \"\",\n name: (obj.Key || \"\").split(\"/\").pop() || \"\",\n parent: null as never,\n root: null as never,\n toString: () => `s3://${resolvedBucket}/${obj.Key}`\n }));\n\n const prefixes: StorageReference[] = (response.CommonPrefixes || []).map(common => {\n // Without the trailing slash S3 puts on a common prefix, so a\n // `fullPath` from a listing is a key any controller accepts.\n const folder = folderKey(common.Prefix || \"\");\n return {\n bucket: resolvedBucket,\n fullPath: folder,\n name: folder.split(\"/\").pop() || \"\",\n parent: null as never,\n root: null as never,\n toString: () => `s3://${resolvedBucket}/${folder}`\n };\n });\n\n return {\n items,\n prefixes,\n nextPageToken: response.NextContinu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 -59,6 +60,22 @@ export declare class AstSchemaEditor {
59
60
  * Resolve a file path and ensure it falls within the collectionsDir.
60
61
  */
61
62
  private safePath;
63
+ /**
64
+ * Bring the in-memory project back in line with the disk.
65
+ *
66
+ * The source-only editor builds ONE of these at boot and serves every
67
+ * request with it, and ts-morph reads a file once and then trusts its
68
+ * copy. `project.save()` writes that copy back whole, so anything that
69
+ * reached the disk in between — the live editor's committed change (it
70
+ * builds a fresh editor per request), or the developer's own edit in the
71
+ * IDE — was overwritten by the boot-time copy plus this edit. In `rebase
72
+ * dev` the tsx-watch restart usually closed the window; outside watch mode
73
+ * the loss was permanent.
74
+ *
75
+ * Called at the top of every public method, so each edit applies to what
76
+ * is on disk now.
77
+ */
78
+ private syncWithDisk;
62
79
  private getCollectionFile;
63
80
  /**
64
81
  * Find the object literal a collection is declared with, through whatever
@@ -85,6 +102,18 @@ export declare class AstSchemaEditor {
85
102
  private findProperty;
86
103
  private static quoteKey;
87
104
  private static isPlainObject;
105
+ /**
106
+ * The field that identifies each element of an array of objects, so an
107
+ * old element can be found again in the new array.
108
+ *
109
+ * Arrays travel whole (see `collection_patch.ts`), and an element's
110
+ * position is not its identity — adding an entity action at the front
111
+ * shifts every other one. `key`, `id`, `relationName`, `name`: the first
112
+ * one every new element carries, distinct, wins.
113
+ */
114
+ private static identityField;
115
+ /** The old array's object elements, keyed by `field`'s literal value. */
116
+ private oldElementsBy;
88
117
  private convertJsonToAstString;
89
118
  /**
90
119
  * Write only the keys the patch names, leaving every sibling alone.
@@ -122,6 +151,45 @@ export declare class AstSchemaEditor {
122
151
  saveCollection(collectionId: string, collectionData: Record<string, unknown>, options?: {
123
152
  partial?: boolean;
124
153
  }): Promise<void>;
154
+ /**
155
+ * Write what changed about a collection, and nothing else.
156
+ *
157
+ * The patch is the difference between the collection the panel loaded and
158
+ * the one it is saving (`diffCollections` in `@rebasepro/types`), so it
159
+ * names only keys the person changed. Each is written where it is; every
160
+ * other byte of the file — imports, comments, formatting, handlers,
161
+ * properties shared from another module — is left as it was.
162
+ *
163
+ * A key whose value in the file is code — `status: statusProperty`,
164
+ * `enum: LOCALE_ENUM`, a spread — is not overwritten with the JSON the
165
+ * panel saw of it, which is what the whole-collection save did. The edit
166
+ * is refused, naming the code, so it can be made where that code lives.
167
+ *
168
+ * All or nothing: a refused operation leaves the file as it was, including
169
+ * the operations before it.
170
+ */
171
+ applyPatch(collectionId: string, patch: CollectionPatch): Promise<void>;
172
+ private writePatch;
173
+ private applyPatchOp;
174
+ /**
175
+ * A patched value with every relation target it carries turned back into
176
+ * source — `() => xCollection` plus its import — according to where the
177
+ * path lands: a relation's `target`, a relation, a property, or a map of
178
+ * properties.
179
+ */
180
+ private patchValueAsSource;
181
+ /** A member of an object literal by name: an assignment, a shorthand, a method. */
182
+ private findMember;
183
+ private spreadIn;
184
+ /** `x as const`, `x satisfies T`, `(x)` — the value inside. */
185
+ private static unwrapExpression;
186
+ /**
187
+ * Whether a value in the file is data the panel's JSON fully describes —
188
+ * a literal — rather than code it saw only the result of.
189
+ */
190
+ private static isLiteralValue;
191
+ /** "`properties.status` is `statusProperty` in posts.ts…" */
192
+ private codeRefusal;
125
193
  private writeCollection;
126
194
  /**
127
195
  * Every property relation's `target`, as source.
@@ -168,5 +236,28 @@ export declare class AstSchemaEditor {
168
236
  private targetThunk;
169
237
  private hasDefaultImport;
170
238
  private getDefaultExportName;
171
- deleteCollection(collectionId: string): Promise<void>;
239
+ /**
240
+ * Delete a collection's file, and take it out of `index.ts`.
241
+ *
242
+ * This used to unlink `<slug>.ts` and stop. Every scaffold's `index.ts`
243
+ * imports every collection, so the next boot failed for the whole project
244
+ * ("Cannot find module …/tags"), and a collection another one linked to
245
+ * took that one down with it. Now:
246
+ *
247
+ * - a file any other module imports is refused, naming the importers — a
248
+ * relation to it has to go first, and that is a change the person makes;
249
+ * - `index.ts`'s import of it is removed, and so is each place it is listed
250
+ * in an array; any other use there is refused, naming the line;
251
+ * - the file is deleted.
252
+ *
253
+ * Only the source. The table and its rows are not touched.
254
+ *
255
+ * Returns what changed, as absolute paths, so the caller can commit it.
256
+ */
257
+ deleteCollection(collectionId: string): Promise<{
258
+ deleted: string;
259
+ changed: string[];
260
+ }>;
261
+ /** The import and export declarations in `file` whose relative specifier resolves to `targetPath`. */
262
+ private declarationsOf;
172
263
  }
@@ -23,6 +23,15 @@ export declare function schemaDriftRemedy(): {
23
23
  short: string;
24
24
  lines: string[];
25
25
  };
26
+ /**
27
+ * Did the database refuse a value as unreadable for its column's type —
28
+ * SQLSTATE class 22, anywhere down the cause chain?
29
+ *
30
+ * For a lookup by an id the caller supplied, that is an answer rather than a
31
+ * failure: `abc` against a UUID key names no row, exactly as a well-formed id
32
+ * nobody has does.
33
+ */
34
+ export declare function isDataException(error: unknown): boolean;
26
35
  /**
27
36
  * Standardized API error class.
28
37
  * Throw this from any route handler — the errorHandler middleware
@@ -29,7 +29,7 @@
29
29
  * — `applySchemaChange` re-checks rather than trusting the plan it is handed.
30
30
  */
31
31
  import { Hono } from "hono";
32
- import { type CollectionConfig, type DatabaseAdmin, type SchemaCommitPaths } from "@rebasepro/types";
32
+ import { type CollectionPatch, type CollectionConfig, type DatabaseAdmin, type SchemaCommitPaths } from "@rebasepro/types";
33
33
  import { HonoEnv } from "./types.js";
34
34
  import { type SchemaEditRepository } from "../schema-edit/apply-schema-change.js";
35
35
  import { type SchemaEditPolicy } from "../schema-edit/schema-edit-permissions.js";
@@ -87,8 +87,8 @@ export interface LiveSchemaRoutesConfig {
87
87
  *
88
88
  * Absent for a project that *is* the repository, which is what the defaults
89
89
  * describe. Present for one in a subdirectory, where the defaults would
90
- * write `backend/` and `drizzle/` beside `.git` and leave the project's real
91
- * generated files untouched — committing a source change alongside a stale
90
+ * write `backend/` beside `.git` and leave the project's real generated
91
+ * file untouched — committing a source change alongside a stale
92
92
  * schema, which is the failure committing the whole change exists to avoid.
93
93
  */
94
94
  commitPaths?: Partial<SchemaCommitPaths>;
@@ -97,8 +97,8 @@ export interface LiveSchemaRoutesConfig {
97
97
  * environment.
98
98
  *
99
99
  * If it does, applying here is not the whole job: the DDL runs against
100
- * *this* database and `drizzle/schema.sql` is committed, but no migration
101
- * is written — a server cannot mint one, since that needs Atlas and a
100
+ * *this* database and the collection is committed, but no migration is
101
+ * written — a server cannot mint one, since that needs Atlas and a
102
102
  * throwaway database. The next environment built by replaying migrations
103
103
  * would not have this change, and nothing would have said so.
104
104
  */
@@ -111,11 +111,31 @@ export interface LiveSchemaRoutesConfig {
111
111
  */
112
112
  policy?: SchemaEditPolicy;
113
113
  }
114
- /** What the panel posts: one collection, in the shape it should end up. */
114
+ /**
115
+ * What the panel posts about one collection.
116
+ *
117
+ * For a collection that exists, a `patch`: what the person changed, as the
118
+ * difference between the JSON the editor loaded and the JSON it is saving (see
119
+ * `collection_patch.ts` in `@rebasepro/types`). The whole collection travelled
120
+ * here once, and the write rewrote every key it carried and deleted whatever
121
+ * JSON had dropped — handlers, shared properties, imported enums, comments.
122
+ *
123
+ * For a collection that does not exist yet, the whole `collection`. A caller
124
+ * that sends a whole collection for one that exists has it turned into the
125
+ * patch of what differs (see {@link asPatch}).
126
+ */
115
127
  export interface ProposedChange {
116
128
  collectionId: string;
117
- /** The whole collection as it should be after the edit. */
118
- collection: Record<string, unknown>;
129
+ /** The whole collection — only for one that does not exist yet. */
130
+ collection?: Record<string, unknown>;
131
+ /** What changed about an existing collection. */
132
+ patch?: CollectionPatch;
133
+ /**
134
+ * Delete the collection's source: its file, and its entry in `index.ts`.
135
+ * Planned like any change — the table and its rows stay, and say so — and
136
+ * committed source-only.
137
+ */
138
+ remove?: true;
119
139
  }
120
140
  /**
121
141
  * The git identity for the caller.
@@ -128,6 +148,16 @@ export declare function commitAuthor(user: unknown): {
128
148
  name: string;
129
149
  email: string;
130
150
  } | undefined;
151
+ /**
152
+ * The change as a patch against the collection as it is.
153
+ *
154
+ * A whole collection posted for one that exists is the difference from the
155
+ * current one: a caller that sends JSON cannot then delete what JSON could not
156
+ * carry. A key it left out is removed only when its value is pure data — a
157
+ * key holding code (`callbacks`, a handler, a component) is not something a
158
+ * JSON caller could have meant to drop.
159
+ */
160
+ export declare function asPatch(existing: CollectionConfig, change: ProposedChange): CollectionPatch;
131
161
  /**
132
162
  * The proposed collection set: everything as it is, with one collection
133
163
  * replaced or added.
@@ -44,6 +44,15 @@ declare class LogRingBuffer {
44
44
  getLatest(count?: number): LogEntry[];
45
45
  }
46
46
  export declare const logBuffer: LogRingBuffer;
47
+ /**
48
+ * Which process this log belongs to.
49
+ *
50
+ * The ring is this process's memory, and nothing gathers the rings of a
51
+ * deployment's other replicas or of its split runtime roles. So a reader is
52
+ * told whose log it is: `HOSTNAME` is the pod name on Kubernetes and the
53
+ * container id under Docker; the pid tells two local processes apart.
54
+ */
55
+ export declare function logInstanceName(): string;
47
56
  /** Add a log entry */
48
57
  export declare function addLog(level: LogEntry["level"], source: LogEntry["source"], message: string, metadata?: Record<string, unknown>): void;
49
58
  export interface LogMiddlewareOptions {
@@ -55,7 +64,25 @@ export interface LogMiddlewareOptions {
55
64
  * server it is also the thing evicting real entries out of the ring.
56
65
  */
57
66
  ignorePaths?: string[];
67
+ /**
68
+ * Where the API is mounted (`/api`). The segment after it names the
69
+ * subsystem a request is filed under — see {@link sourceForRequestPath}.
70
+ * Unset, the first segment of the path is read.
71
+ */
72
+ basePath?: string;
58
73
  }
74
+ /**
75
+ * The `source` of a request entry: the subsystem its path addresses.
76
+ *
77
+ * `/api/auth/*` and the MCP authorization server under `/api/oauth/*` are
78
+ * `auth`, `/api/storage/*` is `storage`, and everything else is `api`. The
79
+ * Logs Explorer's Source filter matches this field, so a sign-in filed as
80
+ * `api` was invisible under Auth. The realtime socket is not an HTTP request
81
+ * and is not recorded here.
82
+ *
83
+ * Exported for its test.
84
+ */
85
+ export declare function sourceForRequestPath(path: string, basePath?: string): LogEntry["source"];
59
86
  /** Hono middleware to log API requests */
60
87
  export declare function logMiddleware(options?: LogMiddlewareOptions): MiddlewareHandler<HonoEnv>;
61
88
  /**
@@ -79,6 +106,17 @@ export interface LogStreamTiming {
79
106
  heartbeatMs?: number;
80
107
  maxPending?: number;
81
108
  }
82
- export declare function createLogsRoutes(timing?: LogStreamTiming): Hono<HonoEnv>;
109
+ /** What ends the stream from the server's side. */
110
+ export interface LogsRoutesOptions {
111
+ /**
112
+ * Aborted when the backend shuts down. Every open stream ends then, rather
113
+ * than when its client happens to leave: `server.close()` waits on every
114
+ * open connection, so one Studio → Logs tab held a SIGTERM for the whole
115
+ * force timeout — longer than Cloud Run's or `docker stop`'s grace period,
116
+ * which killed the process before the pool was closed.
117
+ */
118
+ closeSignal?: AbortSignal;
119
+ }
120
+ export declare function createLogsRoutes(timing?: LogStreamTiming, options?: LogsRoutesOptions): Hono<HonoEnv>;
83
121
  declare const _default: Hono<HonoEnv, import("hono/types").BlankSchema, "/">;
84
122
  export default _default;
@@ -28,3 +28,20 @@ export interface OpenApiGeneratorOptions {
28
28
  };
29
29
  }
30
30
  export declare function generateOpenApiSpec(collections: CollectionConfig[], options?: OpenApiGeneratorOptions): Record<string, unknown>;
31
+ /**
32
+ * Build the component schema for a collection (output / read shape).
33
+ *
34
+ * Every declared property except the ones {@link isDocumentedProperty} rules
35
+ * out, plus the foreign keys and relations {@link emitRelationProperties} adds.
36
+ */
37
+ export declare function buildCollectionSchema(collection: CollectionConfig, registeredSchemas: ReadonlySet<string>): Record<string, unknown>;
38
+ /**
39
+ * Build an input schema (for POST/PUT) — excludes auto-generated fields.
40
+ *
41
+ * `excludeFromApi` columns are left out too, and the server now agrees: a write
42
+ * naming one is refused (`write-validation.ts`), which is what the flag's name
43
+ * and the generated SDK's own documentation always said. This comment used to
44
+ * record that such a write was "still accepted" — the document was right and
45
+ * the server was the thing that had not caught up.
46
+ */
47
+ export declare function buildCollectionInputSchema(collection: CollectionConfig): Record<string, unknown>;