Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudflare-module): support cloudflare:email and cloudflare:queue hooks #2487

Merged
merged 7 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";

import type { CloudflareEmailContext, CloudflareMessageBatch } from "../types";

// @ts-ignore Bundled by Wrangler
// See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules
import manifest from "__STATIC_CONTENT_MANIFEST";
Expand Down Expand Up @@ -99,6 +101,28 @@ export default {
);
}
},

email(
event: CloudflareEmailContext,
env: CFModuleEnv,
context: ExecutionContext
) {
(globalThis as any).__env__ = env;
context.waitUntil(
nitroApp.hooks.callHook("cloudflare:email", { event, env, context })
);
},

queue(
event: CloudflareEmailContext,
env: CFModuleEnv,
context: ExecutionContext
) {
(globalThis as any).__env__ = env;
context.waitUntil(
nitroApp.hooks.callHook("cloudflare:queue", { event, env, context })
);
},
};

function assetsCacheControl(_request: Request) {
Expand Down
48 changes: 48 additions & 0 deletions src/presets/cloudflare/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Config as WranglerConfig } from "./types.wrangler";
import type { ExecutionContext } from "@cloudflare/workers-types";

/**
* https://developers.cloudflare.com/pages/platform/functions/routing/#functions-invocation-routes
Expand Down Expand Up @@ -50,3 +51,50 @@ export interface CloudflareOptions {
defaultRoutes?: boolean;
};
}

/** @experimental */
export interface CloudflareEmailContext {
readonly from?: string;
readonly to?: string;
readonly headers?: Headers;
readonly raw?: ReadableStream;
readonly rawSize?: number;

setReject?(reason: string): void;
forward?(rcptTo: string, headers?: Headers): Promise<void>;
reply?(message: CloudflareEmailContext): Promise<void>;
}

export interface CloudflareQueueRetryOptions {
delaySeconds?: number;
}

export interface CloudflareMessageBody<Body = unknown> {
readonly id: string;
readonly timestamp: Date;
readonly body: Body;
adk(): void;
retry(options?: CloudflareQueueRetryOptions): void;
}

export interface CloudflareMessageBatch<Body = unknown> {
readonly queue: string;
readonly messages: CloudflareMessageBody<Body>[];
ackAll(): void;
retryAll(options?: CloudflareQueueRetryOptions): void;
}

declare module "nitropack/types" {
export interface NitroRuntimeHooks {
"cloudflare:email": (_: {
event: CloudflareEmailContext;
env: any;
context: ExecutionContext;
}) => void;
"cloudflare:queue": (_: {
event: CloudflareEmailContext;
env: any;
context: ExecutionContext;
}) => void;
}
}