|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +/* eslint-disable @typescript-eslint/member-ordering */ |
| 3 | +/* eslint-disable jsdoc/require-jsdoc */ |
| 4 | +/* eslint-disable @typescript-eslint/explicit-member-accessibility */ |
| 5 | +// This file vendors in worker types in `cloudflare:workers` because they are not |
| 6 | +// exported from `@cloudflare/workers-types` yet. |
| 7 | + |
| 8 | +/* ! ***************************************************************************** |
| 9 | +Copyright (c) Cloudflare. All rights reserved. |
| 10 | +Copyright (c) Microsoft Corporation. All rights reserved. |
| 11 | +
|
| 12 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 13 | +this file except in compliance with the License. You may obtain a copy of the |
| 14 | +License at http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED |
| 17 | +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, |
| 18 | +MERCHANTABLITY OR NON-INFRINGEMENT. |
| 19 | +See the Apache Version 2.0 License for specific language governing permissions |
| 20 | +and limitations under the License. |
| 21 | +***************************************************************************** */ |
| 22 | + |
| 23 | +import type { ExecutionContext, Rpc } from '@cloudflare/workers-types'; |
| 24 | + |
| 25 | +const __WORKFLOW_ENTRYPOINT_BRAND = '__WORKFLOW_ENTRYPOINT_BRAND' as const; |
| 26 | + |
| 27 | +export type WorkflowEvent<T> = { |
| 28 | + payload: Readonly<T>; |
| 29 | + timestamp: Date; |
| 30 | + instanceId: string; |
| 31 | +}; |
| 32 | + |
| 33 | +export type WorkflowStepEvent<T> = { |
| 34 | + payload: Readonly<T>; |
| 35 | + timestamp: Date; |
| 36 | + type: string; |
| 37 | +}; |
| 38 | + |
| 39 | +export type WorkflowDurationLabel = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'; |
| 40 | +export type WorkflowSleepDuration = `${number} ${WorkflowDurationLabel}${'s' | ''}` | number; |
| 41 | +export type WorkflowDelayDuration = WorkflowSleepDuration; |
| 42 | +export type WorkflowTimeoutDuration = WorkflowSleepDuration; |
| 43 | +export type WorkflowRetentionDuration = WorkflowSleepDuration; |
| 44 | + |
| 45 | +export type WorkflowBackoff = 'constant' | 'linear' | 'exponential'; |
| 46 | + |
| 47 | +export type WorkflowStepConfig = { |
| 48 | + retries?: { |
| 49 | + limit: number; |
| 50 | + delay: WorkflowDelayDuration | number; |
| 51 | + backoff?: WorkflowBackoff; |
| 52 | + }; |
| 53 | + timeout?: WorkflowTimeoutDuration | number; |
| 54 | +}; |
| 55 | + |
| 56 | +export abstract class WorkflowStep { |
| 57 | + do<T extends Rpc.Serializable<T>>(name: string, callback: () => Promise<T>): Promise<T>; |
| 58 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the method. |
| 59 | + do<T extends Rpc.Serializable<T>>(name: string, config: WorkflowStepConfig, callback: () => Promise<T>): Promise<T>; |
| 60 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the method. |
| 61 | + sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>; |
| 62 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the method. |
| 63 | + sleepUntil: (name: string, timestamp: Date | number) => Promise<void>; |
| 64 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the method. |
| 65 | + waitForEvent<T extends Rpc.Serializable<T>>( |
| 66 | + name: string, |
| 67 | + options: { |
| 68 | + type: string; |
| 69 | + timeout?: WorkflowTimeoutDuration | number; |
| 70 | + }, |
| 71 | + ): Promise<WorkflowStepEvent<T>>; |
| 72 | +} |
| 73 | + |
| 74 | +export abstract class WorkflowEntrypoint<Env = unknown, T extends Rpc.Serializable<T> | unknown = unknown> |
| 75 | + implements Rpc.WorkflowEntrypointBranded |
| 76 | +{ |
| 77 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the property. |
| 78 | + [__WORKFLOW_ENTRYPOINT_BRAND]: never; |
| 79 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the property. |
| 80 | + protected ctx: ExecutionContext; |
| 81 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the property. |
| 82 | + protected env: Env; |
| 83 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the constructor. |
| 84 | + constructor(ctx: ExecutionContext, env: Env); |
| 85 | + // @ts-expect-error - This is a vendor file, so we don't need to implement the method. |
| 86 | + run(event: Readonly<WorkflowEvent<T>>, step: WorkflowStep): Promise<unknown>; |
| 87 | +} |
0 commit comments