@authhero/cloudflare-adapter 2.21.19 → 2.22.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/dist/cloudflare-adapter.cjs +124 -23
- package/dist/cloudflare-adapter.d.ts +138 -0
- package/dist/cloudflare-adapter.mjs +1187 -962
- package/package.json +3 -3
|
@@ -683,6 +683,43 @@ export interface StatsAdapter {
|
|
|
683
683
|
*/
|
|
684
684
|
getActiveUsers(tenantId: string): Promise<number>;
|
|
685
685
|
}
|
|
686
|
+
export interface CodeExecutionResult {
|
|
687
|
+
success: boolean;
|
|
688
|
+
error?: string;
|
|
689
|
+
durationMs: number;
|
|
690
|
+
apiCalls: Array<{
|
|
691
|
+
method: string;
|
|
692
|
+
args: unknown[];
|
|
693
|
+
}>;
|
|
694
|
+
}
|
|
695
|
+
export interface CodeExecutor {
|
|
696
|
+
execute(params: {
|
|
697
|
+
code: string;
|
|
698
|
+
hookCodeId?: string;
|
|
699
|
+
triggerId: string;
|
|
700
|
+
event: Record<string, unknown>;
|
|
701
|
+
/** Wall-clock timeout for the entire execution (ms). */
|
|
702
|
+
timeoutMs?: number;
|
|
703
|
+
/**
|
|
704
|
+
* CPU-time limit (ms) for runtimes that distinguish CPU time from
|
|
705
|
+
* wall-clock time (e.g. Cloudflare Workers for Platforms).
|
|
706
|
+
* Ignored by executors that do not support CPU-time limits.
|
|
707
|
+
*/
|
|
708
|
+
cpuLimitMs?: number;
|
|
709
|
+
}): Promise<CodeExecutionResult>;
|
|
710
|
+
/**
|
|
711
|
+
* Deploy user code to the execution environment.
|
|
712
|
+
* Called when hook code is created or updated.
|
|
713
|
+
* Optional — LocalCodeExecutor does not need this.
|
|
714
|
+
*/
|
|
715
|
+
deploy?(hookCodeId: string, code: string): Promise<void>;
|
|
716
|
+
/**
|
|
717
|
+
* Remove user code from the execution environment.
|
|
718
|
+
* Called when hook code is deleted.
|
|
719
|
+
* Optional — LocalCodeExecutor does not need this.
|
|
720
|
+
*/
|
|
721
|
+
remove?(hookCodeId: string): Promise<void>;
|
|
722
|
+
}
|
|
686
723
|
export interface R2SQLLogsAdapterConfig {
|
|
687
724
|
/**
|
|
688
725
|
* Cloudflare Pipeline HTTP endpoint URL for ingesting logs
|
|
@@ -910,6 +947,107 @@ export interface CloudflareConfig {
|
|
|
910
947
|
*/
|
|
911
948
|
analyticsEngineLogs?: AnalyticsEngineLogsAdapterConfig;
|
|
912
949
|
}
|
|
950
|
+
/**
|
|
951
|
+
* Cloudflare Workers for Platforms dispatch namespace binding type.
|
|
952
|
+
* This is the type of `env.DISPATCHER` when configured in wrangler.toml:
|
|
953
|
+
*
|
|
954
|
+
* ```toml
|
|
955
|
+
* [[dispatch_namespaces]]
|
|
956
|
+
* binding = "DISPATCHER"
|
|
957
|
+
* namespace = "authhero-hooks"
|
|
958
|
+
* ```
|
|
959
|
+
*/
|
|
960
|
+
export interface DispatchNamespace {
|
|
961
|
+
get(name: string, options?: Record<string, unknown>, init?: {
|
|
962
|
+
limits?: {
|
|
963
|
+
cpuMs?: number;
|
|
964
|
+
subrequests?: number;
|
|
965
|
+
};
|
|
966
|
+
}): {
|
|
967
|
+
fetch(request: Request | string, init?: RequestInit): Promise<Response>;
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
export interface CloudflareCodeExecutorConfig {
|
|
971
|
+
/** Cloudflare account ID */
|
|
972
|
+
accountId: string;
|
|
973
|
+
/** API token with Workers Scripts write permission */
|
|
974
|
+
apiToken: string;
|
|
975
|
+
/** Dispatch namespace name (e.g., "authhero-hooks") */
|
|
976
|
+
dispatchNamespace: string;
|
|
977
|
+
/**
|
|
978
|
+
* Dispatch namespace binding from the worker environment.
|
|
979
|
+
* When running inside a Cloudflare Worker, pass `env.DISPATCHER`.
|
|
980
|
+
* Enables low-latency same-origin invocation.
|
|
981
|
+
*/
|
|
982
|
+
dispatcher?: DispatchNamespace;
|
|
983
|
+
/**
|
|
984
|
+
* Fallback URL for invoking user workers when no dispatcher binding is available.
|
|
985
|
+
* The executor appends `/{scriptName}` to this URL.
|
|
986
|
+
* Only used when `dispatcher` is not provided.
|
|
987
|
+
*/
|
|
988
|
+
dispatchUrl?: string;
|
|
989
|
+
/**
|
|
990
|
+
* Cloudflare Workers compatibility date used when deploying scripts.
|
|
991
|
+
* Defaults to "2024-11-20" if not provided.
|
|
992
|
+
*/
|
|
993
|
+
compatibilityDate?: string;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Code executor that uses Cloudflare Workers for Platforms.
|
|
997
|
+
*
|
|
998
|
+
* User code is deployed as individual worker scripts in a dispatch namespace.
|
|
999
|
+
* At execution time, the pre-deployed worker is invoked via the dispatch
|
|
1000
|
+
* namespace binding (in-worker) or via HTTP (external fallback).
|
|
1001
|
+
*
|
|
1002
|
+
* Usage:
|
|
1003
|
+
* ```typescript
|
|
1004
|
+
* const codeExecutor = new CloudflareCodeExecutor({
|
|
1005
|
+
* accountId: env.CF_ACCOUNT_ID,
|
|
1006
|
+
* apiToken: env.CF_API_TOKEN,
|
|
1007
|
+
* dispatchNamespace: "authhero-hooks",
|
|
1008
|
+
* dispatcher: env.DISPATCHER,
|
|
1009
|
+
* });
|
|
1010
|
+
*
|
|
1011
|
+
* const { app } = init({ dataAdapter, codeExecutor });
|
|
1012
|
+
* ```
|
|
1013
|
+
*/
|
|
1014
|
+
export declare class CloudflareCodeExecutor implements CodeExecutor {
|
|
1015
|
+
private config;
|
|
1016
|
+
constructor(config: CloudflareCodeExecutorConfig);
|
|
1017
|
+
execute(params: {
|
|
1018
|
+
code: string;
|
|
1019
|
+
hookCodeId?: string;
|
|
1020
|
+
triggerId: string;
|
|
1021
|
+
event: Record<string, unknown>;
|
|
1022
|
+
/** Wall-clock timeout (ms). Used for HTTP-based fallback invocation. */
|
|
1023
|
+
timeoutMs?: number;
|
|
1024
|
+
/**
|
|
1025
|
+
* CPU-time limit (ms) passed to the Cloudflare dispatcher binding.
|
|
1026
|
+
* Unlike timeoutMs (wall-clock), this caps only actual CPU cycles;
|
|
1027
|
+
* I/O wait does not count against it. Defaults to 5 000 ms.
|
|
1028
|
+
*/
|
|
1029
|
+
cpuLimitMs?: number;
|
|
1030
|
+
}): Promise<CodeExecutionResult>;
|
|
1031
|
+
/**
|
|
1032
|
+
* Deploy user code as a worker to the dispatch namespace.
|
|
1033
|
+
* Wraps the code in a worker template and uploads via Cloudflare API.
|
|
1034
|
+
*/
|
|
1035
|
+
deploy(hookCodeId: string, code: string): Promise<void>;
|
|
1036
|
+
/**
|
|
1037
|
+
* Remove a user worker from the dispatch namespace.
|
|
1038
|
+
*/
|
|
1039
|
+
remove(hookCodeId: string): Promise<void>;
|
|
1040
|
+
}
|
|
1041
|
+
/**
|
|
1042
|
+
* Generates a Cloudflare Worker script that wraps user-authored code.
|
|
1043
|
+
*
|
|
1044
|
+
* The generated worker:
|
|
1045
|
+
* 1. Accepts POST requests with { triggerId, event }
|
|
1046
|
+
* 2. Creates a recording API proxy that captures method calls
|
|
1047
|
+
* 3. Runs the user's exported function (e.g., exports.onExecutePostLogin)
|
|
1048
|
+
* 4. Returns { success, apiCalls, error, durationMs } as JSON
|
|
1049
|
+
*/
|
|
1050
|
+
export declare function generateWorkerScript(userCode: string): string;
|
|
913
1051
|
export interface CloudflareAdapters {
|
|
914
1052
|
customDomains: CustomDomainsAdapter;
|
|
915
1053
|
cache: CacheAdapter;
|