@authhero/cloudflare-adapter 2.34.2 → 2.35.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.
@@ -1,4 +1,5 @@
1
1
  import { StatsAdapter, LogsDataAdapter, AnalyticsAdapter, ActionExecutionsAdapter, RateLimitScope, RateLimitAdapter, CustomDomainsAdapter, CodeExecutor, CodeExecutionResult, TenantsDataAdapter, CacheAdapter, GeoAdapter } from '@authhero/adapter-interfaces';
2
+ import { Context, MiddlewareHandler } from 'hono';
2
3
 
3
4
  interface R2SQLLogsAdapterConfig {
4
5
  /**
@@ -508,6 +509,102 @@ declare class WorkerLoaderCodeExecutor implements CodeExecutor {
508
509
  }): Promise<CodeExecutionResult>;
509
510
  }
510
511
 
512
+ /**
513
+ * Thin Cloudflare REST API client for the WFP+D1 provisioner.
514
+ *
515
+ * Each method maps 1:1 to a documented CF endpoint and returns the parsed
516
+ * response body. Errors surface as `CloudflareApiError` carrying the HTTP
517
+ * status, endpoint, and (when JSON) the CF error array — making them easy
518
+ * to log without re-fetching the response.
519
+ *
520
+ * Idempotency is the caller's responsibility — the provisioner sequences
521
+ * calls and tolerates "already exists" / "not found" depending on the
522
+ * operation (see `provisioner.ts`).
523
+ */
524
+ declare class CloudflareApiError extends Error {
525
+ readonly status: number;
526
+ readonly endpoint: string;
527
+ readonly errors: unknown[];
528
+ readonly body: string;
529
+ constructor(status: number, endpoint: string, body: string, errors?: unknown[]);
530
+ }
531
+ interface CfApiClientOptions {
532
+ accountId: string;
533
+ apiToken: string;
534
+ fetch?: typeof fetch;
535
+ timeoutMs?: number;
536
+ baseUrl?: string;
537
+ }
538
+ interface D1Database {
539
+ uuid: string;
540
+ name: string;
541
+ }
542
+ interface D1QueryResult {
543
+ success: boolean;
544
+ meta?: Record<string, unknown>;
545
+ results?: unknown[];
546
+ }
547
+ type ScriptBinding = {
548
+ type: "d1";
549
+ name: string;
550
+ id: string;
551
+ } | {
552
+ type: "plain_text";
553
+ name: string;
554
+ text: string;
555
+ } | {
556
+ type: "secret_text";
557
+ name: string;
558
+ text: string;
559
+ } | {
560
+ type: "service";
561
+ name: string;
562
+ service: string;
563
+ environment?: string;
564
+ };
565
+ interface ScriptUploadOptions {
566
+ /** Script source (JavaScript ES module). */
567
+ script: string;
568
+ /** Main module filename (must match part name in form data). */
569
+ mainModule: string;
570
+ /** Compatibility date, ISO yyyy-mm-dd. */
571
+ compatibilityDate: string;
572
+ /** Compatibility flags (e.g. `["nodejs_compat"]`). */
573
+ compatibilityFlags?: string[];
574
+ /** Bindings to attach (D1, plain_text, etc.). Secrets go via setSecret(). */
575
+ bindings?: ScriptBinding[];
576
+ /** Optional tags, stored on the script for operator-side lookup. */
577
+ tags?: string[];
578
+ }
579
+ declare class CloudflareApiClient {
580
+ private readonly accountId;
581
+ private readonly apiToken;
582
+ private readonly fetchImpl;
583
+ private readonly timeoutMs;
584
+ private readonly baseUrl;
585
+ constructor(options: CfApiClientOptions);
586
+ createD1Database(name: string): Promise<D1Database>;
587
+ listD1Databases(name?: string): Promise<D1Database[]>;
588
+ deleteD1Database(databaseId: string): Promise<void>;
589
+ /**
590
+ * Execute a single SQL statement (or batch of `;`-separated statements
591
+ * permitted by D1) against the given database. Use for applying
592
+ * migrations one file at a time — the per-call response size cap means
593
+ * very large single calls can fail; splitting per file keeps each call
594
+ * bounded by the migration author.
595
+ */
596
+ execD1(databaseId: string, sql: string): Promise<D1QueryResult[]>;
597
+ uploadNamespacedScript(namespace: string, scriptName: string, options: ScriptUploadOptions): Promise<void>;
598
+ deleteNamespacedScript(namespace: string, scriptName: string): Promise<void>;
599
+ /**
600
+ * Set a single secret on a namespaced script. The CF API replaces the
601
+ * value if a secret with that name already exists, so this is safely
602
+ * re-runnable.
603
+ */
604
+ setNamespacedScriptSecret(namespace: string, scriptName: string, secretName: string, secretValue: string): Promise<void>;
605
+ private request;
606
+ }
607
+
511
608
  /**
512
609
  * Public types for the Workers-for-Platforms + D1 tenant provisioner.
513
610
  *
@@ -600,6 +697,17 @@ interface CloudflareWfpD1ProvisionerOptions {
600
697
  * the per-script secrets API.
601
698
  */
602
699
  secrets: TenantSecretsResolver;
700
+ /**
701
+ * Extra bindings to attach to every provisioned tenant worker, appended
702
+ * after the always-present `AUTH_DB` (d1) and `CONTROL_PLANE_BASE_URL`
703
+ * (plain_text) bindings. Use this to wire e.g. a `service` binding to a
704
+ * shared upstream worker, or additional `plain_text` config the tenant
705
+ * bundle expects. Secrets still go through the `secrets` resolver, not here.
706
+ *
707
+ * `uploadNamespacedScript` forwards these verbatim into the CF script
708
+ * metadata, so any binding type the CF API accepts is valid.
709
+ */
710
+ extraBindings?: ScriptBinding[];
603
711
  /**
604
712
  * Naming convention for the namespaced script. Supports `{tenant_id}`
605
713
  * placeholder. Defaults to `"{tenant_id}"`.
@@ -763,89 +871,41 @@ interface WfpTenantProvisioningHook {
763
871
  }
764
872
  declare function createWfpTenantProvisioningHook(options: WfpTenantProvisioningHookOptions): WfpTenantProvisioningHook;
765
873
 
766
- /**
767
- * Thin Cloudflare REST API client for the WFP+D1 provisioner.
768
- *
769
- * Each method maps 1:1 to a documented CF endpoint and returns the parsed
770
- * response body. Errors surface as `CloudflareApiError` carrying the HTTP
771
- * status, endpoint, and (when JSON) the CF error array — making them easy
772
- * to log without re-fetching the response.
773
- *
774
- * Idempotency is the caller's responsibility — the provisioner sequences
775
- * calls and tolerates "already exists" / "not found" depending on the
776
- * operation (see `provisioner.ts`).
777
- */
778
- declare class CloudflareApiError extends Error {
779
- readonly status: number;
780
- readonly endpoint: string;
781
- readonly errors: unknown[];
782
- readonly body: string;
783
- constructor(status: number, endpoint: string, body: string, errors?: unknown[]);
784
- }
785
- interface CfApiClientOptions {
786
- accountId: string;
787
- apiToken: string;
788
- fetch?: typeof fetch;
789
- timeoutMs?: number;
790
- baseUrl?: string;
791
- }
792
- interface D1Database {
793
- uuid: string;
794
- name: string;
795
- }
796
- interface D1QueryResult {
797
- success: boolean;
798
- meta?: Record<string, unknown>;
799
- results?: unknown[];
800
- }
801
- interface ScriptBinding {
802
- type: "d1" | "plain_text" | "secret_text";
803
- name: string;
804
- id?: string;
805
- text?: string;
806
- }
807
- interface ScriptUploadOptions {
808
- /** Script source (JavaScript ES module). */
809
- script: string;
810
- /** Main module filename (must match part name in form data). */
811
- mainModule: string;
812
- /** Compatibility date, ISO yyyy-mm-dd. */
813
- compatibilityDate: string;
814
- /** Compatibility flags (e.g. `["nodejs_compat"]`). */
815
- compatibilityFlags?: string[];
816
- /** Bindings to attach (D1, plain_text, etc.). Secrets go via setSecret(). */
817
- bindings?: ScriptBinding[];
818
- /** Optional tags, stored on the script for operator-side lookup. */
819
- tags?: string[];
820
- }
821
- declare class CloudflareApiClient {
822
- private readonly accountId;
823
- private readonly apiToken;
824
- private readonly fetchImpl;
825
- private readonly timeoutMs;
826
- private readonly baseUrl;
827
- constructor(options: CfApiClientOptions);
828
- createD1Database(name: string): Promise<D1Database>;
829
- listD1Databases(name?: string): Promise<D1Database[]>;
830
- deleteD1Database(databaseId: string): Promise<void>;
874
+ interface WfpForwardOptions {
875
+ /** Tenants adapter, used to look up the resolved tenant's `deployment_type`. */
876
+ tenants: TenantsDataAdapter;
831
877
  /**
832
- * Execute a single SQL statement (or batch of `;`-separated statements
833
- * permitted by D1) against the given database. Use for applying
834
- * migrations one file at a time — the per-call response size cap means
835
- * very large single calls can fail; splitting per file keeps each call
836
- * bounded by the migration author.
878
+ * The control-plane tenant id. Requests for this tenant (and anything not a
879
+ * `wfp` tenant) fall through to the local app instead of being dispatched.
837
880
  */
838
- execD1(databaseId: string, sql: string): Promise<D1QueryResult[]>;
839
- uploadNamespacedScript(namespace: string, scriptName: string, options: ScriptUploadOptions): Promise<void>;
840
- deleteNamespacedScript(namespace: string, scriptName: string): Promise<void>;
881
+ controlPlaneTenantId: string;
882
+ /** Env binding name of the dispatch namespace. Defaults to `"DISPATCHER"`. */
883
+ dispatcherBinding?: string;
841
884
  /**
842
- * Set a single secret on a namespaced script. The CF API replaces the
843
- * value if a secret with that name already exists, so this is safely
844
- * re-runnable.
885
+ * Script-name convention for a tenant's worker. Supports `{tenant_id}`.
886
+ * Defaults to `"tenant-{tenant_id}-auth"` must match the provisioner's
887
+ * `scriptNameTemplate`.
845
888
  */
846
- setNamespacedScriptSecret(namespace: string, scriptName: string, secretName: string, secretValue: string): Promise<void>;
847
- private request;
889
+ scriptNameTemplate?: string;
890
+ /**
891
+ * Resolves the tenant id for the incoming request. Defaults to reading the
892
+ * `tenant-id` header (legacy compatibility). Replace it to resolve from a
893
+ * subdomain or custom domain. Return `undefined` to fall through locally.
894
+ */
895
+ resolveTenantId?: (c: Context) => string | undefined | Promise<string | undefined>;
848
896
  }
897
+ /**
898
+ * Hono middleware that forwards a request to its tenant's WFP worker over a
899
+ * dispatch namespace, instead of serving it from the current (control-plane)
900
+ * worker.
901
+ *
902
+ * For each request it resolves a tenant id, and **only** dispatches when that
903
+ * tenant exists and has `deployment_type === "wfp"`. The control-plane tenant,
904
+ * unknown tenants, and shared (colocated) tenants all fall through to the next
905
+ * handler so the local app serves them. The tenant worker receives the original
906
+ * request verbatim and owns the full response.
907
+ */
908
+ declare function createWfpForwardMiddleware(options: WfpForwardOptions): MiddlewareHandler;
849
909
 
850
910
  interface CloudflareAdapters {
851
911
  customDomains: CustomDomainsAdapter;
@@ -858,5 +918,5 @@ interface CloudflareAdapters {
858
918
  }
859
919
  declare function createAdapters(config: CloudflareConfig): CloudflareAdapters;
860
920
 
861
- export { CloudflareApiClient, CloudflareApiError, CloudflareCodeExecutor, DispatchNamespaceCodeExecutor, WorkerLoaderCodeExecutor, createAnalyticsEngineActionExecutionsAdapter, createAnalyticsEngineAnalyticsAdapter, createAnalyticsEngineLogsAdapter, createAnalyticsEngineStatsAdapter, createCloudflareRateLimitAdapter, createCloudflareWfpD1Provisioner, createR2SQLLogsAdapter, createR2SQLStatsAdapter, createWfpTenantProvisioningHook, createAdapters as default, generateWorkerScript };
862
- export type { AnalyticsEngineActionExecutionsAdapterConfig, AnalyticsEngineDataset, AnalyticsEngineLogsAdapterConfig, CfApiClientOptions, CloudflareAdapters, CloudflareCodeExecutorConfig, CloudflareConfig, CloudflareRateLimitBinding, CloudflareRateLimitBindings, CloudflareWfpD1Provisioner, CloudflareWfpD1ProvisionerOptions, D1Database, D1QueryResult, DispatchNamespace, DispatchNamespaceCodeExecutorConfig, ProvisionResult, ProvisionerMigration, R2SQLLogsAdapterConfig, ScriptBinding, ScriptUploadOptions, TenantSecretsResolver, WfpTenantProvisioningHook, WfpTenantProvisioningHookOptions, WorkerCode, WorkerLoader, WorkerLoaderCodeExecutorOptions, WorkerStub };
921
+ export { CloudflareApiClient, CloudflareApiError, CloudflareCodeExecutor, DispatchNamespaceCodeExecutor, WorkerLoaderCodeExecutor, createAnalyticsEngineActionExecutionsAdapter, createAnalyticsEngineAnalyticsAdapter, createAnalyticsEngineLogsAdapter, createAnalyticsEngineStatsAdapter, createCloudflareRateLimitAdapter, createCloudflareWfpD1Provisioner, createR2SQLLogsAdapter, createR2SQLStatsAdapter, createWfpForwardMiddleware, createWfpTenantProvisioningHook, createAdapters as default, generateWorkerScript };
922
+ export type { AnalyticsEngineActionExecutionsAdapterConfig, AnalyticsEngineDataset, AnalyticsEngineLogsAdapterConfig, CfApiClientOptions, CloudflareAdapters, CloudflareCodeExecutorConfig, CloudflareConfig, CloudflareRateLimitBinding, CloudflareRateLimitBindings, CloudflareWfpD1Provisioner, CloudflareWfpD1ProvisionerOptions, D1Database, D1QueryResult, DispatchNamespace, DispatchNamespaceCodeExecutorConfig, ProvisionResult, ProvisionerMigration, R2SQLLogsAdapterConfig, ScriptBinding, ScriptUploadOptions, TenantSecretsResolver, WfpForwardOptions, WfpTenantProvisioningHook, WfpTenantProvisioningHookOptions, WorkerCode, WorkerLoader, WorkerLoaderCodeExecutorOptions, WorkerStub };