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

fix(cloudflare-module): use correct types for email and queue events/hooks #2710

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 10 additions & 15 deletions src/presets/cloudflare/runtime/cloudflare-module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import "#nitro-internal-pollyfills";
import { useNitroApp } from "nitro/runtime";
import { useRuntimeConfig } from "nitro/runtime";
import { runCronTasks } from "nitro/runtime/internal";
import { requestHasBody } from "nitro/runtime/internal";
import { getPublicAssetMeta } from "#nitro-internal-virtual/public-assets";

import {
getAssetFromKV,
mapRequestToAsset,
} from "@cloudflare/kv-asset-handler";
import type { ExecutionContext } from "@cloudflare/workers-types";
import type {
ExecutionContext,
ForwardableEmailMessage,
MessageBatch,
} from "@cloudflare/workers-types";
import wsAdapter from "crossws/adapters/cloudflare";
import { useNitroApp, useRuntimeConfig } from "nitro/runtime";
import { requestHasBody, runCronTasks } from "nitro/runtime/internal";
import { withoutBase } from "ufo";

import type { CloudflareEmailContext, CloudflareMessageBatch } from "../types";
import { getPublicAssetMeta } from "#nitro-internal-virtual/public-assets";

// @ts-ignore Bundled by Wrangler
// See https://github.com/cloudflare/kv-asset-handler#asset_manifest-required-for-es-modules
Expand Down Expand Up @@ -103,7 +102,7 @@ export default {
},

email(
event: CloudflareEmailContext,
event: ForwardableEmailMessage,
env: CFModuleEnv,
context: ExecutionContext
) {
Expand All @@ -113,11 +112,7 @@ export default {
);
},

queue(
event: CloudflareEmailContext,
env: CFModuleEnv,
context: ExecutionContext
) {
queue(event: MessageBatch, env: CFModuleEnv, context: ExecutionContext) {
(globalThis as any).__env__ = env;
context.waitUntil(
nitroApp.hooks.callHook("cloudflare:queue", { event, env, context })
Expand Down
42 changes: 7 additions & 35 deletions src/presets/cloudflare/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ExecutionContext } from "@cloudflare/workers-types";
import type {
ExecutionContext,
ForwardableEmailMessage,
MessageBatch,
} from "@cloudflare/workers-types";
import type { Config as WranglerConfig } from "./types.wrangler";

/**
Expand Down Expand Up @@ -52,47 +56,15 @@ export interface CloudflareOptions {
};
}

/** @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 "nitro/types" {
export interface NitroRuntimeHooks {
"cloudflare:email": (_: {
event: CloudflareEmailContext;
event: ForwardableEmailMessage;
env: any;
context: ExecutionContext;
}) => void;
"cloudflare:queue": (_: {
event: CloudflareEmailContext;
event: MessageBatch;
env: any;
context: ExecutionContext;
}) => void;
Expand Down