@rebasepro/server-core 0.7.0 → 0.8.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.
- package/build-errors.txt +52 -0
- package/dist/api/ast-schema-editor.d.ts +21 -0
- package/dist/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
- package/dist/api/errors.d.ts +53 -0
- package/dist/api/graphql/graphql-schema-generator.d.ts +44 -0
- package/dist/api/graphql/index.d.ts +1 -0
- package/dist/api/index.d.ts +9 -0
- package/dist/api/logs-routes.d.ts +37 -0
- package/dist/api/openapi-generator.d.ts +16 -0
- package/dist/api/rest/api-generator.d.ts +76 -0
- package/dist/api/rest/index.d.ts +1 -0
- package/dist/api/rest/query-parser.d.ts +8 -0
- package/dist/api/schema-editor-routes.d.ts +3 -0
- package/dist/api/server.d.ts +40 -0
- package/dist/api/types.d.ts +104 -0
- package/dist/auth/adapter-middleware.d.ts +43 -0
- package/dist/auth/admin-roles-route.d.ts +18 -0
- package/dist/auth/admin-user-ops.d.ts +79 -0
- package/dist/auth/admin-users-route.d.ts +28 -0
- package/dist/auth/api-keys/api-key-middleware.d.ts +39 -0
- package/dist/auth/api-keys/api-key-permission-guard.d.ts +32 -0
- package/dist/auth/api-keys/api-key-routes.d.ts +20 -0
- package/dist/auth/api-keys/api-key-store.d.ts +35 -0
- package/dist/auth/api-keys/api-key-types.d.ts +96 -0
- package/dist/auth/api-keys/index.d.ts +17 -0
- package/dist/auth/apple-oauth.d.ts +30 -0
- package/dist/auth/auth-hooks.d.ts +242 -0
- package/dist/auth/bitbucket-oauth.d.ts +11 -0
- package/dist/auth/builtin-auth-adapter.d.ts +51 -0
- package/dist/auth/crypto-utils.d.ts +16 -0
- package/dist/auth/custom-auth-adapter.d.ts +39 -0
- package/dist/auth/discord-oauth.d.ts +14 -0
- package/dist/auth/facebook-oauth.d.ts +14 -0
- package/dist/auth/github-oauth.d.ts +15 -0
- package/dist/auth/gitlab-oauth.d.ts +13 -0
- package/dist/auth/google-oauth.d.ts +47 -0
- package/dist/auth/index.d.ts +37 -0
- package/dist/auth/interfaces.d.ts +431 -0
- package/dist/auth/jwt.d.ts +55 -0
- package/dist/auth/linkedin-oauth.d.ts +18 -0
- package/dist/auth/magic-link-routes.d.ts +30 -0
- package/dist/auth/mfa-crypto.d.ts +23 -0
- package/dist/auth/mfa-routes.d.ts +7 -0
- package/dist/auth/mfa.d.ts +49 -0
- package/dist/auth/microsoft-oauth.d.ts +16 -0
- package/dist/auth/middleware.d.ts +113 -0
- package/dist/auth/password.d.ts +22 -0
- package/dist/auth/rate-limiter.d.ts +50 -0
- package/dist/auth/reset-password-admin.d.ts +29 -0
- package/dist/auth/rls-scope.d.ts +31 -0
- package/dist/auth/routes.d.ts +35 -0
- package/dist/auth/session-routes.d.ts +27 -0
- package/dist/auth/slack-oauth.d.ts +12 -0
- package/dist/auth/spotify-oauth.d.ts +12 -0
- package/dist/auth/twitter-oauth.d.ts +18 -0
- package/dist/backend-CIxN4FVm.js +15 -0
- package/dist/backend-CIxN4FVm.js.map +1 -0
- package/dist/base64-js-C_frYBkI.js +1687 -0
- package/dist/base64-js-C_frYBkI.js.map +1 -0
- package/dist/chunk-Dze3rakg.js +42 -0
- package/dist/collections/BackendCollectionRegistry.d.ts +13 -0
- package/dist/collections/loader.d.ts +5 -0
- package/dist/cron/cron-loader.d.ts +17 -0
- package/dist/cron/cron-routes.d.ts +14 -0
- package/dist/cron/cron-scheduler.d.ts +106 -0
- package/dist/cron/cron-store.d.ts +32 -0
- package/dist/cron/index.d.ts +6 -0
- package/dist/db/interfaces.d.ts +18 -0
- package/dist/dist-DMO-zF6D.js +832 -0
- package/dist/dist-DMO-zF6D.js.map +1 -0
- package/dist/email/index.d.ts +6 -0
- package/dist/email/smtp-email-service.d.ts +25 -0
- package/dist/email/templates.d.ts +50 -0
- package/dist/email/types.d.ts +126 -0
- package/dist/env.d.ts +102 -0
- package/dist/from-VbwD7xRf.js +3849 -0
- package/dist/from-VbwD7xRf.js.map +1 -0
- package/dist/functions/define-function.d.ts +50 -0
- package/dist/functions/function-loader.d.ts +17 -0
- package/dist/functions/function-routes.d.ts +10 -0
- package/dist/functions/index.d.ts +5 -0
- package/dist/history/history-routes.d.ts +23 -0
- package/dist/history/index.d.ts +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.es.js +61322 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +103633 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/init/docs.d.ts +4 -0
- package/dist/init/health.d.ts +2 -0
- package/dist/init/middlewares.d.ts +10 -0
- package/dist/init/shutdown.d.ts +11 -0
- package/dist/init/storage.d.ts +5 -0
- package/dist/init.d.ts +314 -0
- package/dist/jws-BqRRaK11.js +616 -0
- package/dist/jws-BqRRaK11.js.map +1 -0
- package/dist/jwt-BETC8a1J.js +3560 -0
- package/dist/jwt-BETC8a1J.js.map +1 -0
- package/dist/logger-BYU66ENZ.js +94 -0
- package/dist/logger-BYU66ENZ.js.map +1 -0
- package/dist/ms-BeBSuOXN.js +125 -0
- package/dist/ms-BeBSuOXN.js.map +1 -0
- package/dist/multipart-parser-CedBDOeC.js +299 -0
- package/dist/multipart-parser-CedBDOeC.js.map +1 -0
- package/dist/serve-spa.d.ts +30 -0
- package/dist/services/driver-registry.d.ts +78 -0
- package/dist/services/routed-realtime-service.d.ts +43 -0
- package/dist/services/webhook-service.d.ts +29 -0
- package/dist/singleton.d.ts +35 -0
- package/dist/src-CB2PIpBe.js +1182 -0
- package/dist/src-CB2PIpBe.js.map +1 -0
- package/dist/src-DjzOT1kG.js +29746 -0
- package/dist/src-DjzOT1kG.js.map +1 -0
- package/dist/storage/GCSStorageController.d.ts +43 -0
- package/dist/storage/LocalStorageController.d.ts +46 -0
- package/dist/storage/S3StorageController.d.ts +36 -0
- package/dist/storage/image-transform.d.ts +50 -0
- package/dist/storage/index.d.ts +31 -0
- package/dist/storage/routes.d.ts +70 -0
- package/dist/storage/storage-registry.d.ts +78 -0
- package/dist/storage/tus-handler.d.ts +53 -0
- package/dist/storage/types.d.ts +128 -0
- package/dist/types/index.d.ts +11 -0
- package/dist/utils/dev-port.d.ts +37 -0
- package/dist/utils/logger.d.ts +31 -0
- package/dist/utils/logging.d.ts +9 -0
- package/dist/utils/request-id.d.ts +4 -0
- package/dist/utils/request-logger.d.ts +19 -0
- package/dist/utils/sql.d.ts +27 -0
- package/package.json +22 -14
- package/src/api/graphql/graphql-schema-generator.ts +5 -5
- package/src/api/openapi-generator.ts +2 -2
- package/src/api/rest/api-generator.ts +44 -123
- package/src/api/rest/query-parser.ts +6 -23
- package/src/api/types.ts +2 -2
- package/src/auth/auth-hooks.ts +1 -1
- package/src/auth/builtin-auth-adapter.ts +1 -2
- package/src/auth/interfaces.ts +11 -2
- package/src/auth/middleware.ts +4 -4
- package/src/auth/routes.ts +1 -2
- package/src/functions/define-function.ts +59 -0
- package/src/functions/function-loader.ts +16 -0
- package/src/functions/index.ts +2 -0
- package/src/index.ts +70 -37
- package/src/init.ts +78 -21
- package/src/storage/GCSStorageController.ts +334 -0
- package/src/storage/index.ts +9 -3
- package/src/storage/routes.ts +191 -23
- package/src/storage/tus-handler.ts +16 -4
- package/src/storage/types.ts +25 -3
- package/test/api-generator.test.ts +1 -1
- package/test/auth-config-types.test.ts +40 -0
- package/test/define-function.test.ts +45 -0
- package/test/storage-routes.test.ts +160 -0
- package/test/backend-hooks-data.test.ts +0 -477
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google Cloud Storage controller (works with GCS and Firebase Storage)
|
|
3
|
+
*
|
|
4
|
+
* The `@google-cloud/storage` package is loaded lazily so it remains an
|
|
5
|
+
* optional peer dependency — users who don't need GCS never pay for the
|
|
6
|
+
* import cost or need it installed.
|
|
7
|
+
*/
|
|
8
|
+
import { StorageController, GCSStorageConfig } from "./types";
|
|
9
|
+
import { UploadFileProps, UploadFileResult, DownloadConfig, StorageListResult } from "@rebasepro/types";
|
|
10
|
+
/**
|
|
11
|
+
* Google Cloud Storage implementation of `StorageController`.
|
|
12
|
+
*
|
|
13
|
+
* Works with standard GCS buckets **and** Firebase Storage buckets
|
|
14
|
+
* (e.g. `"my-project.appspot.com"`).
|
|
15
|
+
*/
|
|
16
|
+
export declare class GCSStorageController implements StorageController {
|
|
17
|
+
private config;
|
|
18
|
+
private client;
|
|
19
|
+
constructor(config: GCSStorageConfig);
|
|
20
|
+
private getClient;
|
|
21
|
+
getType(): "gcs";
|
|
22
|
+
putObject({ file, key, metadata, bucket }: UploadFileProps): Promise<UploadFileResult>;
|
|
23
|
+
getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig>;
|
|
24
|
+
getObject(key: string, bucket?: string): Promise<File | null>;
|
|
25
|
+
deleteObject(key: string, bucket?: string): Promise<void>;
|
|
26
|
+
listObjects(prefix: string, options?: {
|
|
27
|
+
bucket?: string;
|
|
28
|
+
maxResults?: number;
|
|
29
|
+
pageToken?: string;
|
|
30
|
+
}): Promise<StorageListResult>;
|
|
31
|
+
/**
|
|
32
|
+
* Validate file before upload
|
|
33
|
+
*/
|
|
34
|
+
private validateFile;
|
|
35
|
+
/**
|
|
36
|
+
* Get the bucket name — either from parameter or config.
|
|
37
|
+
*/
|
|
38
|
+
private getBucket;
|
|
39
|
+
/**
|
|
40
|
+
* Flatten nested metadata to string values (GCS custom metadata requirement)
|
|
41
|
+
*/
|
|
42
|
+
private flattenMetadata;
|
|
43
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local filesystem storage controller
|
|
3
|
+
*/
|
|
4
|
+
import { StorageController, LocalStorageConfig } from "./types";
|
|
5
|
+
import { UploadFileProps, UploadFileResult, DownloadConfig, StorageListResult } from "@rebasepro/types";
|
|
6
|
+
/**
|
|
7
|
+
* Local filesystem storage implementation
|
|
8
|
+
* Stores files in a directory structure: {basePath}/{bucket}/{path}
|
|
9
|
+
*/
|
|
10
|
+
export declare class LocalStorageController implements StorageController {
|
|
11
|
+
private config;
|
|
12
|
+
private basePath;
|
|
13
|
+
constructor(config: LocalStorageConfig);
|
|
14
|
+
getType(): "local";
|
|
15
|
+
/**
|
|
16
|
+
* Ensure directory exists, creating it if necessary
|
|
17
|
+
*/
|
|
18
|
+
private ensureDir;
|
|
19
|
+
/**
|
|
20
|
+
* Get the full filesystem path for a storage path.
|
|
21
|
+
* Includes a path traversal guard to prevent escaping the base directory.
|
|
22
|
+
*/
|
|
23
|
+
private getFullPath;
|
|
24
|
+
/**
|
|
25
|
+
* Validate file before upload
|
|
26
|
+
*/
|
|
27
|
+
private validateFile;
|
|
28
|
+
putObject({ file, key, metadata, bucket }: UploadFileProps): Promise<UploadFileResult>;
|
|
29
|
+
getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig>;
|
|
30
|
+
getObject(key: string, bucket?: string): Promise<File | null>;
|
|
31
|
+
deleteObject(key: string, bucket?: string): Promise<void>;
|
|
32
|
+
listObjects(prefix: string, options?: {
|
|
33
|
+
bucket?: string;
|
|
34
|
+
maxResults?: number;
|
|
35
|
+
pageToken?: string;
|
|
36
|
+
}): Promise<StorageListResult>;
|
|
37
|
+
/**
|
|
38
|
+
* Get the absolute filesystem path for serving files
|
|
39
|
+
* Used by the storage routes to serve files directly
|
|
40
|
+
*/
|
|
41
|
+
getAbsolutePath(key: string, bucket?: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Get the base path for the storage
|
|
44
|
+
*/
|
|
45
|
+
getBasePath(): string;
|
|
46
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* S3-compatible storage controller (works with AWS S3 and MinIO)
|
|
3
|
+
*/
|
|
4
|
+
import { StorageController, S3StorageConfig } from "./types";
|
|
5
|
+
import { UploadFileProps, UploadFileResult, DownloadConfig, StorageListResult } from "@rebasepro/types";
|
|
6
|
+
/**
|
|
7
|
+
* S3-compatible storage implementation
|
|
8
|
+
* Works with AWS S3 and MinIO (with forcePathStyle option)
|
|
9
|
+
*/
|
|
10
|
+
export declare class S3StorageController implements StorageController {
|
|
11
|
+
private config;
|
|
12
|
+
private client;
|
|
13
|
+
constructor(config: S3StorageConfig);
|
|
14
|
+
getType(): "s3";
|
|
15
|
+
/**
|
|
16
|
+
* Validate file before upload
|
|
17
|
+
*/
|
|
18
|
+
private validateFile;
|
|
19
|
+
/**
|
|
20
|
+
* Get the bucket name - either from parameter or config
|
|
21
|
+
*/
|
|
22
|
+
private getBucket;
|
|
23
|
+
putObject({ file, key, metadata, bucket }: UploadFileProps): Promise<UploadFileResult>;
|
|
24
|
+
/**
|
|
25
|
+
* Flatten nested metadata to string values (S3 requirement)
|
|
26
|
+
*/
|
|
27
|
+
private flattenMetadata;
|
|
28
|
+
getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig>;
|
|
29
|
+
getObject(key: string, bucket?: string): Promise<File | null>;
|
|
30
|
+
deleteObject(key: string, bucket?: string): Promise<void>;
|
|
31
|
+
listObjects(prefix: string, options?: {
|
|
32
|
+
bucket?: string;
|
|
33
|
+
maxResults?: number;
|
|
34
|
+
pageToken?: string;
|
|
35
|
+
}): Promise<StorageListResult>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Image Transformation Service
|
|
3
|
+
*
|
|
4
|
+
* Provides on-the-fly image resize, crop, format conversion, and quality
|
|
5
|
+
* adjustment using the `sharp` library. Results are cached in an LRU
|
|
6
|
+
* in-memory cache to avoid redundant processing.
|
|
7
|
+
*/
|
|
8
|
+
/** Options that can be specified via query parameters. */
|
|
9
|
+
export interface ImageTransformOptions {
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
quality?: number;
|
|
13
|
+
format?: "webp" | "avif" | "jpeg" | "png";
|
|
14
|
+
fit?: "cover" | "contain" | "fill" | "inside" | "outside";
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Parse transform options from URL query parameters.
|
|
18
|
+
* Returns `null` when no transformation is requested.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseTransformOptions(query: Record<string, string>): ImageTransformOptions | null;
|
|
21
|
+
/** Check whether a content type is a transformable image. */
|
|
22
|
+
export declare function isTransformableImage(contentType: string): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Apply image transformations and return the result buffer + content type.
|
|
25
|
+
*/
|
|
26
|
+
export declare function transformImage(buffer: Buffer | Uint8Array, options: ImageTransformOptions): Promise<{
|
|
27
|
+
data: Buffer;
|
|
28
|
+
contentType: string;
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* Simple LRU cache for transformed images.
|
|
32
|
+
*
|
|
33
|
+
* Entries expire after `maxAgeMs` (default: 1 hour) and the cache
|
|
34
|
+
* evicts the oldest entry when `maxEntries` is exceeded.
|
|
35
|
+
*/
|
|
36
|
+
export declare class TransformCache {
|
|
37
|
+
private cache;
|
|
38
|
+
private readonly maxEntries;
|
|
39
|
+
private readonly maxAgeMs;
|
|
40
|
+
private readonly maxTotalBytes;
|
|
41
|
+
private totalBytes;
|
|
42
|
+
constructor(maxEntries?: number, maxAgeMs?: number, maxTotalBytes?: number);
|
|
43
|
+
/** Build a deterministic cache key from file key + transform options. */
|
|
44
|
+
buildKey(fileKey: string, options: ImageTransformOptions): string;
|
|
45
|
+
get(cacheKey: string): {
|
|
46
|
+
data: Buffer;
|
|
47
|
+
contentType: string;
|
|
48
|
+
} | null;
|
|
49
|
+
set(cacheKey: string, data: Buffer, contentType: string): void;
|
|
50
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage module for Rebase backend
|
|
3
|
+
*
|
|
4
|
+
* Provides pluggable file storage with three built-in providers:
|
|
5
|
+
* - **Local filesystem** — zero config, great for dev and single-server deployments.
|
|
6
|
+
* - **S3-compatible** — works with AWS S3, Cloudflare R2, MinIO, Hetzner Object Storage,
|
|
7
|
+
* Backblaze B2, DigitalOcean Spaces, and GCS (via S3 interop).
|
|
8
|
+
* - **Google Cloud Storage / Firebase Storage** — native GCS support via `@google-cloud/storage`
|
|
9
|
+
* (optional peer dependency, lazily loaded).
|
|
10
|
+
*
|
|
11
|
+
* For other providers (Azure Blob, etc.), implement the
|
|
12
|
+
* `StorageController` interface and pass the instance directly to the `storage` config.
|
|
13
|
+
*/
|
|
14
|
+
export * from "./types";
|
|
15
|
+
export { LocalStorageController } from "./LocalStorageController";
|
|
16
|
+
export { S3StorageController } from "./S3StorageController";
|
|
17
|
+
export { GCSStorageController } from "./GCSStorageController";
|
|
18
|
+
export { createStorageRoutes } from "./routes";
|
|
19
|
+
export type { StorageRoutesConfig } from "./routes";
|
|
20
|
+
export * from "./storage-registry";
|
|
21
|
+
export { parseTransformOptions, transformImage, isTransformableImage, TransformCache } from "./image-transform";
|
|
22
|
+
export type { ImageTransformOptions } from "./image-transform";
|
|
23
|
+
export { TusHandler } from "./tus-handler";
|
|
24
|
+
import { BackendStorageConfig, StorageController } from "./types";
|
|
25
|
+
/**
|
|
26
|
+
* Create a storage controller from a config object.
|
|
27
|
+
*
|
|
28
|
+
* For custom providers, implement `StorageController` directly instead
|
|
29
|
+
* of going through this factory.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createStorageController(config: BackendStorageConfig): StorageController;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage REST API routes using Hono
|
|
3
|
+
*
|
|
4
|
+
* Supports multi-backend routing via `StorageRegistry`. Each endpoint
|
|
5
|
+
* accepts an optional `storageId` parameter (query string or form field)
|
|
6
|
+
* to target a named storage backend. When omitted, the default backend
|
|
7
|
+
* is used.
|
|
8
|
+
*/
|
|
9
|
+
import { Hono } from "hono";
|
|
10
|
+
import { StorageController } from "./types";
|
|
11
|
+
import type { StorageRegistry } from "./storage-registry";
|
|
12
|
+
import { type StorageSourceDefinition, type AuthAdapter } from "@rebasepro/types";
|
|
13
|
+
import { HonoEnv } from "../api/types";
|
|
14
|
+
export interface StorageRoutesConfig {
|
|
15
|
+
/**
|
|
16
|
+
* Single storage controller (backward-compatible).
|
|
17
|
+
* Used as fallback when no `registry` is provided.
|
|
18
|
+
*/
|
|
19
|
+
controller?: StorageController;
|
|
20
|
+
/**
|
|
21
|
+
* Full storage registry for multi-backend routing.
|
|
22
|
+
* When provided, endpoints resolve the controller from `storageId`
|
|
23
|
+
* parameter. Takes precedence over `controller`.
|
|
24
|
+
*/
|
|
25
|
+
registry?: StorageRegistry;
|
|
26
|
+
/**
|
|
27
|
+
* Declared storage sources, surfaced by `GET /sources` so the client can
|
|
28
|
+
* bootstrap its registry. Carries the frontend `transport` (server vs
|
|
29
|
+
* direct) and human-readable labels. Server-transport sources are also
|
|
30
|
+
* derived from the registry; `direct` sources (e.g. Firebase Storage) only
|
|
31
|
+
* exist here since the backend does not proxy them.
|
|
32
|
+
*/
|
|
33
|
+
sources?: StorageSourceDefinition[];
|
|
34
|
+
/** Base path for storage routes (default: '/api/storage') */
|
|
35
|
+
basePath?: string;
|
|
36
|
+
/** Require authentication for write operations (default: true) */
|
|
37
|
+
requireAuth?: boolean;
|
|
38
|
+
/** Allow unauthenticated read access to stored files (default: false).
|
|
39
|
+
* When false and requireAuth is true, reads also require authentication. */
|
|
40
|
+
publicRead?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* When provided, storage routes delegate auth to this adapter instead
|
|
43
|
+
* of the built-in JWT module. This mirrors how data routes use
|
|
44
|
+
* `createAdapterAuthMiddleware()` and avoids the "JWT secret not
|
|
45
|
+
* configured" crash when `configureJwt()` was never called.
|
|
46
|
+
*/
|
|
47
|
+
authAdapter?: AuthAdapter;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Extract the wildcard portion of a route path from the full request path.
|
|
51
|
+
*
|
|
52
|
+
* Hono's `c.req.param('*')` does not work reliably in sub-routers mounted
|
|
53
|
+
* via `app.route(prefix, subRouter)`. Instead we derive the wildcard value
|
|
54
|
+
* from the fully-resolved `c.req.path` and `c.req.routePath`.
|
|
55
|
+
*
|
|
56
|
+
* For a route `/metadata/*` mounted at `/api/storage`, a request to
|
|
57
|
+
* `/api/storage/metadata/default/file.jpg` yields routePath
|
|
58
|
+
* `/api/storage/metadata/*`. We strip the prefix (everything before `/*`)
|
|
59
|
+
* plus one character for the trailing `/` to obtain `default/file.jpg`.
|
|
60
|
+
*/
|
|
61
|
+
export declare function extractWildcardPath(c: {
|
|
62
|
+
req: {
|
|
63
|
+
path: string;
|
|
64
|
+
routePath: string;
|
|
65
|
+
};
|
|
66
|
+
}): string;
|
|
67
|
+
/**
|
|
68
|
+
* Create storage REST API routes
|
|
69
|
+
*/
|
|
70
|
+
export declare function createStorageRoutes(config: StorageRoutesConfig): Hono<HonoEnv>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage Registry
|
|
3
|
+
*
|
|
4
|
+
* Manages multiple storage controllers for Rebase backend.
|
|
5
|
+
* Allows different storage backends for different use cases.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* - Single storage: Pass a single StorageController → maps to "(default)"
|
|
9
|
+
* - Multiple storages: Pass a map of { storageId: StorageController }
|
|
10
|
+
* - String properties use `storageId` in their config to specify which storage to use
|
|
11
|
+
* - Properties without `storageId` fallback to "(default)"
|
|
12
|
+
*/
|
|
13
|
+
import { StorageController } from "./types";
|
|
14
|
+
/**
|
|
15
|
+
* The default storage identifier used when:
|
|
16
|
+
* - A single storage controller is provided (not a map)
|
|
17
|
+
* - A property doesn't specify a storageId
|
|
18
|
+
*/
|
|
19
|
+
export declare const DEFAULT_STORAGE_ID = "(default)";
|
|
20
|
+
/**
|
|
21
|
+
* Registry for managing multiple storage controllers
|
|
22
|
+
*/
|
|
23
|
+
export interface StorageRegistry {
|
|
24
|
+
/**
|
|
25
|
+
* Register a storage controller with an ID
|
|
26
|
+
* @param id - Unique identifier for this storage (e.g., "media", "backups")
|
|
27
|
+
* @param controller - The StorageController instance
|
|
28
|
+
*/
|
|
29
|
+
register(id: string, controller: StorageController): void;
|
|
30
|
+
/**
|
|
31
|
+
* Get the default storage controller (id = "(default)")
|
|
32
|
+
* @throws Error if no default storage is registered
|
|
33
|
+
*/
|
|
34
|
+
getDefault(): StorageController;
|
|
35
|
+
/**
|
|
36
|
+
* Get a storage controller by ID
|
|
37
|
+
* @param id - Storage identifier, or undefined/null for default
|
|
38
|
+
* @returns The StorageController, or undefined if not found
|
|
39
|
+
*/
|
|
40
|
+
get(id: string | undefined | null): StorageController | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Get a storage controller by ID, with fallback to default
|
|
43
|
+
* @param id - Storage identifier, or undefined/null for default
|
|
44
|
+
* @returns The StorageController (falls back to default if id not found)
|
|
45
|
+
* @throws Error if neither the specified nor default storage exists
|
|
46
|
+
*/
|
|
47
|
+
getOrDefault(id: string | undefined | null): StorageController;
|
|
48
|
+
/**
|
|
49
|
+
* Check if a storage with the given ID exists
|
|
50
|
+
*/
|
|
51
|
+
has(id: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* List all registered storage IDs
|
|
54
|
+
*/
|
|
55
|
+
list(): string[];
|
|
56
|
+
/**
|
|
57
|
+
* Get the number of registered storage controllers
|
|
58
|
+
*/
|
|
59
|
+
size(): number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Default implementation of StorageRegistry
|
|
63
|
+
*/
|
|
64
|
+
export declare class DefaultStorageRegistry implements StorageRegistry {
|
|
65
|
+
private controllers;
|
|
66
|
+
/**
|
|
67
|
+
* Create a StorageRegistry from either a single controller or a map
|
|
68
|
+
* @param input - Single StorageController (maps to "(default)") or Record<string, StorageController>
|
|
69
|
+
*/
|
|
70
|
+
static create(input: StorageController | Record<string, StorageController>): DefaultStorageRegistry;
|
|
71
|
+
register(id: string, controller: StorageController): void;
|
|
72
|
+
getDefault(): StorageController;
|
|
73
|
+
get(id: string | undefined | null): StorageController | undefined;
|
|
74
|
+
getOrDefault(id: string | undefined | null): StorageController;
|
|
75
|
+
has(id: string): boolean;
|
|
76
|
+
list(): string[];
|
|
77
|
+
size(): number;
|
|
78
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TUS Protocol Handler
|
|
3
|
+
*
|
|
4
|
+
* Implements the TUS v1.0.0 resumable upload protocol with the
|
|
5
|
+
* Creation and Termination extensions. Uploads are stored in a
|
|
6
|
+
* temporary directory and moved to final storage on completion.
|
|
7
|
+
*
|
|
8
|
+
* @see https://tus.io/protocols/resumable-upload
|
|
9
|
+
*/
|
|
10
|
+
import type { Context } from "hono";
|
|
11
|
+
import type { StorageController } from "./types";
|
|
12
|
+
import type { StorageRegistry } from "./storage-registry";
|
|
13
|
+
/**
|
|
14
|
+
* TUS resumable upload handler.
|
|
15
|
+
*
|
|
16
|
+
* Each instance manages uploads for a single storage root. The
|
|
17
|
+
* `storageController` is used to finalize completed uploads by
|
|
18
|
+
* calling `putObject`.
|
|
19
|
+
*/
|
|
20
|
+
export declare class TusHandler {
|
|
21
|
+
private storageController?;
|
|
22
|
+
private storageRegistry?;
|
|
23
|
+
private uploads;
|
|
24
|
+
private tusDir;
|
|
25
|
+
private cleanupTimer?;
|
|
26
|
+
constructor(storageBaseDir: string, storageController?: StorageController | undefined, storageRegistry?: StorageRegistry | undefined);
|
|
27
|
+
/** Ensure the temp directory exists. */
|
|
28
|
+
private ensureDir;
|
|
29
|
+
/** Start periodic cleanup of stale uploads. */
|
|
30
|
+
startCleanup(): void;
|
|
31
|
+
/** Remove uploads that have been idle for longer than UPLOAD_EXPIRY_MS. */
|
|
32
|
+
private cleanupStale;
|
|
33
|
+
/**
|
|
34
|
+
* Parse the `Upload-Metadata` header.
|
|
35
|
+
*
|
|
36
|
+
* Format: `key base64value,key2 base64value2`
|
|
37
|
+
*/
|
|
38
|
+
private parseMetadata;
|
|
39
|
+
/** `OPTIONS /tus` — TUS capability discovery. */
|
|
40
|
+
options(): Response;
|
|
41
|
+
/** `POST /tus` — Create a new upload. */
|
|
42
|
+
create(c: Context): Promise<Response>;
|
|
43
|
+
/** `HEAD /tus/:id` — Query upload progress. */
|
|
44
|
+
head(c: Context, id: string): Response;
|
|
45
|
+
/** `PATCH /tus/:id` — Append data to an upload. */
|
|
46
|
+
patch(c: Context, id: string): Promise<Response>;
|
|
47
|
+
/** `DELETE /tus/:id` — Cancel and remove an upload. */
|
|
48
|
+
delete(c: Context, id: string): Promise<Response>;
|
|
49
|
+
/**
|
|
50
|
+
* Move a completed upload into the storage controller.
|
|
51
|
+
*/
|
|
52
|
+
private finalize;
|
|
53
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage configuration and types for Rebase backend
|
|
3
|
+
*/
|
|
4
|
+
import { UploadFileProps, UploadFileResult, DownloadConfig, StorageListResult } from "@rebasepro/types";
|
|
5
|
+
/**
|
|
6
|
+
* Local filesystem storage configuration
|
|
7
|
+
*/
|
|
8
|
+
export interface LocalStorageConfig {
|
|
9
|
+
type: "local";
|
|
10
|
+
/** Base directory for file storage (e.g., './uploads') */
|
|
11
|
+
basePath: string;
|
|
12
|
+
/** Maximum file size in bytes (default: 50MB) */
|
|
13
|
+
maxFileSize?: number;
|
|
14
|
+
/** Allowed MIME types (if not set, all types allowed) */
|
|
15
|
+
allowedMimeTypes?: string[];
|
|
16
|
+
/** Base URL for generating download URLs (default: auto-detected from request) */
|
|
17
|
+
baseUrl?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* S3-compatible storage configuration (works with AWS S3 and MinIO)
|
|
21
|
+
*/
|
|
22
|
+
export interface S3StorageConfig {
|
|
23
|
+
type: "s3";
|
|
24
|
+
/** S3 bucket name */
|
|
25
|
+
bucket: string;
|
|
26
|
+
/** AWS region (e.g., 'us-east-1') */
|
|
27
|
+
region?: string;
|
|
28
|
+
/** Custom endpoint URL (required for MinIO, Cloudflare R2, Hetzner Object Storage) */
|
|
29
|
+
endpoint?: string;
|
|
30
|
+
/** AWS access key ID */
|
|
31
|
+
accessKeyId: string;
|
|
32
|
+
/** AWS secret access key */
|
|
33
|
+
secretAccessKey: string;
|
|
34
|
+
/** Use path-style URLs (required for MinIO) */
|
|
35
|
+
forcePathStyle?: boolean;
|
|
36
|
+
/** Maximum file size in bytes (default: 50MB) */
|
|
37
|
+
maxFileSize?: number;
|
|
38
|
+
/** Allowed MIME types (if not set, all types allowed) */
|
|
39
|
+
allowedMimeTypes?: string[];
|
|
40
|
+
/** URL expiration time in seconds for signed URLs (default: 3600) */
|
|
41
|
+
signedUrlExpiration?: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Google Cloud Storage configuration (works with GCS and Firebase Storage)
|
|
45
|
+
*/
|
|
46
|
+
export interface GCSStorageConfig {
|
|
47
|
+
type: "gcs";
|
|
48
|
+
/** GCS bucket name (e.g. "my-project.appspot.com" for Firebase Storage) */
|
|
49
|
+
bucket: string;
|
|
50
|
+
/** GCP project ID (optional, auto-detected from credentials) */
|
|
51
|
+
projectId?: string;
|
|
52
|
+
/** Path to service account JSON key file */
|
|
53
|
+
keyFilename?: string;
|
|
54
|
+
/** Service account credentials object (alternative to keyFilename) */
|
|
55
|
+
credentials?: {
|
|
56
|
+
client_email: string;
|
|
57
|
+
private_key: string;
|
|
58
|
+
project_id?: string;
|
|
59
|
+
};
|
|
60
|
+
/** Maximum file size in bytes (default: 50MB) */
|
|
61
|
+
maxFileSize?: number;
|
|
62
|
+
/** Allowed MIME types */
|
|
63
|
+
allowedMimeTypes?: string[];
|
|
64
|
+
/** Signed URL expiration in seconds (default: 3600) */
|
|
65
|
+
signedUrlExpiration?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Storage configuration — local filesystem, S3-compatible, or Google Cloud Storage.
|
|
69
|
+
*
|
|
70
|
+
* **Built-in providers:**
|
|
71
|
+
* - `local` — Zero-config filesystem storage. Great for dev and single-server deployments (Hetzner, bare metal).
|
|
72
|
+
* - `s3` — Any S3-compatible provider. AWS S3, Cloudflare R2, MinIO, Hetzner Object Storage,
|
|
73
|
+
* Backblaze B2, DigitalOcean Spaces, and even GCS (via its S3-compatible interoperability API).
|
|
74
|
+
* - `gcs` — Native Google Cloud Storage / Firebase Storage via `@google-cloud/storage`.
|
|
75
|
+
*
|
|
76
|
+
* **Custom providers:**
|
|
77
|
+
* For other cloud storage (Azure Blob, etc.), implement the `StorageController`
|
|
78
|
+
* interface and pass the instance directly to the `storage` config.
|
|
79
|
+
*/
|
|
80
|
+
export type BackendStorageConfig = LocalStorageConfig | S3StorageConfig | GCSStorageConfig;
|
|
81
|
+
/**
|
|
82
|
+
* Storage controller interface for backend implementations
|
|
83
|
+
*/
|
|
84
|
+
export interface StorageController {
|
|
85
|
+
/**
|
|
86
|
+
* Upload an object
|
|
87
|
+
*/
|
|
88
|
+
putObject(props: UploadFileProps): Promise<UploadFileResult>;
|
|
89
|
+
/**
|
|
90
|
+
* Get a download URL (signed URL equivalent) for an object
|
|
91
|
+
*/
|
|
92
|
+
getSignedUrl(key: string, bucket?: string): Promise<DownloadConfig>;
|
|
93
|
+
/**
|
|
94
|
+
* Get object as a File
|
|
95
|
+
*/
|
|
96
|
+
getObject(key: string, bucket?: string): Promise<File | null>;
|
|
97
|
+
/**
|
|
98
|
+
* Delete an object
|
|
99
|
+
*/
|
|
100
|
+
deleteObject(key: string, bucket?: string): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* List objects in a prefix
|
|
103
|
+
*/
|
|
104
|
+
listObjects(prefix: string, options?: {
|
|
105
|
+
bucket?: string;
|
|
106
|
+
maxResults?: number;
|
|
107
|
+
pageToken?: string;
|
|
108
|
+
}): Promise<StorageListResult>;
|
|
109
|
+
/**
|
|
110
|
+
* Get the storage provider identifier.
|
|
111
|
+
*
|
|
112
|
+
* Built-in values are `'local'` and `'s3'`. Custom implementations
|
|
113
|
+
* should return their own identifier (e.g. `'gcs'`, `'azure'`).
|
|
114
|
+
*/
|
|
115
|
+
getType(): string;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Default maximum file size (50MB)
|
|
119
|
+
*/
|
|
120
|
+
export declare const DEFAULT_MAX_FILE_SIZE: number;
|
|
121
|
+
/**
|
|
122
|
+
* Common image MIME types
|
|
123
|
+
*/
|
|
124
|
+
export declare const IMAGE_MIME_TYPES: string[];
|
|
125
|
+
/**
|
|
126
|
+
* Common document MIME types
|
|
127
|
+
*/
|
|
128
|
+
export declare const DOCUMENT_MIME_TYPES: string[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Entity, FetchCollectionProps, FetchEntityProps } from "@rebasepro/types";
|
|
2
|
+
export interface ListenCollectionRequest<M extends Record<string, unknown> = Record<string, unknown>> extends FetchCollectionProps<M> {
|
|
3
|
+
subscriptionId: string;
|
|
4
|
+
onUpdate: (entities: Entity<M>[]) => void;
|
|
5
|
+
onError?: (error: Error) => void;
|
|
6
|
+
}
|
|
7
|
+
export interface ListenEntityRequest<M extends Record<string, unknown> = Record<string, unknown>> extends FetchEntityProps<M> {
|
|
8
|
+
subscriptionId: string;
|
|
9
|
+
onUpdate: (entity: Entity<M> | null) => void;
|
|
10
|
+
onError?: (error: Error) => void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev-mode port resolution utilities.
|
|
3
|
+
*
|
|
4
|
+
* Provides a `listen` wrapper that automatically retries the next port when
|
|
5
|
+
* the requested one is already in use, and writes the resolved port to a
|
|
6
|
+
* well-known temp file so the CLI / frontend can discover it.
|
|
7
|
+
*
|
|
8
|
+
* Port affinity: when a port file already exists (e.g. after a tsx watch
|
|
9
|
+
* restart), the saved port is tried FIRST so the backend stays on the same
|
|
10
|
+
* port the frontend was configured with.
|
|
11
|
+
*
|
|
12
|
+
* This module is dev-only and should never run in production.
|
|
13
|
+
*/
|
|
14
|
+
import type { Server } from "http";
|
|
15
|
+
/** Filename written next to the project `.env` so the CLI can read it. */
|
|
16
|
+
export declare const DEV_PORT_FILENAME = ".rebase-dev-port";
|
|
17
|
+
/**
|
|
18
|
+
* Try to `listen` on `startPort`. If the port is busy (`EADDRINUSE`), increment
|
|
19
|
+
* and retry up to `maxAttempts` times.
|
|
20
|
+
*
|
|
21
|
+
* When a port file already exists (written by a previous run), the saved port
|
|
22
|
+
* is tried first to maintain port affinity across tsx watch restarts.
|
|
23
|
+
*
|
|
24
|
+
* Resolves with the port that was actually bound.
|
|
25
|
+
*/
|
|
26
|
+
export declare function listenWithPortRetry(server: Server, startPort: number, options?: {
|
|
27
|
+
host?: string;
|
|
28
|
+
maxAttempts?: number;
|
|
29
|
+
/** Absolute path to write the resolved port file into. Defaults to `process.cwd()`. */
|
|
30
|
+
portFileDir?: string;
|
|
31
|
+
/** Service key to include in the state file for MCP server auto-discovery. */
|
|
32
|
+
serviceKey?: string;
|
|
33
|
+
}): Promise<number>;
|
|
34
|
+
/**
|
|
35
|
+
* Clean up the dev port file and state file (call on graceful shutdown).
|
|
36
|
+
*/
|
|
37
|
+
export declare function cleanupDevPortFile(dir: string): void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured Logger for Rebase Backend
|
|
3
|
+
*
|
|
4
|
+
* Outputs JSON lines when `NODE_ENV=production`, human-readable prefixed
|
|
5
|
+
* lines otherwise. Designed to work with Google Cloud Logging severity levels.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* import { logger } from "./utils/logger";
|
|
9
|
+
* logger.info("Server started", { port: 3001 });
|
|
10
|
+
* logger.error("Request failed", { path: "/api/test", error: err });
|
|
11
|
+
*/
|
|
12
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
13
|
+
export interface LogEntry {
|
|
14
|
+
severity: string;
|
|
15
|
+
message: string;
|
|
16
|
+
timestamp: string;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
export interface Logger {
|
|
20
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
21
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
22
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
23
|
+
error(message: string, data?: Record<string, unknown>): void;
|
|
24
|
+
child(defaultFields: Record<string, unknown>): Logger;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Singleton logger instance.
|
|
28
|
+
* In production: emits JSON lines with `severity`, `message`, `timestamp`.
|
|
29
|
+
* In development: emits human-readable prefixed lines.
|
|
30
|
+
*/
|
|
31
|
+
export declare const logger: Logger;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configure console log levels based on environment variable
|
|
3
|
+
* Call this early in your application to set up proper logging levels
|
|
4
|
+
*/
|
|
5
|
+
export declare function configureLogLevel(logLevel?: string): void;
|
|
6
|
+
/**
|
|
7
|
+
* Reset console methods to their original state
|
|
8
|
+
*/
|
|
9
|
+
export declare function resetConsole(): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured HTTP request logging middleware for Hono.
|
|
3
|
+
*
|
|
4
|
+
* Logs every request with method, path, status code, latency, and
|
|
5
|
+
* content-length. In production, outputs JSON for Cloud Logging; in
|
|
6
|
+
* development, emits a coloured one-liner.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { requestLogger } from "@rebasepro/server-core";
|
|
11
|
+
* app.use("/*", requestLogger());
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
import type { MiddlewareHandler } from "hono";
|
|
15
|
+
export interface RequestLoggerOptions {
|
|
16
|
+
/** Paths to skip logging (e.g. "/health"). Supports exact match. */
|
|
17
|
+
skip?: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare function requestLogger(options?: RequestLoggerOptions): MiddlewareHandler;
|