|
| 1 | +import { ValueError } from '../errors'; |
| 2 | +import type { Payload } from '../interfaces'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Reference returned from {@link StorageDriver.store}. `claimData` is an |
| 6 | + * opaque key/value map the driver uses to retrieve the payload later. |
| 7 | + * |
| 8 | + * @internal |
| 9 | + * @experimental |
| 10 | + */ |
| 11 | +export class StorageDriverClaim { |
| 12 | + constructor(readonly claimData: Record<string, string>) {} |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Workflow identity information passed to a storage driver. |
| 17 | + * |
| 18 | + * @internal |
| 19 | + * @experimental |
| 20 | + */ |
| 21 | +export interface StorageDriverWorkflowInfo { |
| 22 | + readonly kind: 'workflow'; |
| 23 | + /** The namespace of the workflow execution. */ |
| 24 | + readonly namespace: string; |
| 25 | + /** The workflow ID. */ |
| 26 | + readonly id?: string; |
| 27 | + /** The workflow run ID, if available. */ |
| 28 | + readonly runId?: string; |
| 29 | + /** The workflow type name, if available. */ |
| 30 | + readonly type?: string; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Activity identity information passed to a storage driver. |
| 35 | + * |
| 36 | + * @internal |
| 37 | + * @experimental |
| 38 | + */ |
| 39 | +export interface StorageDriverActivityInfo { |
| 40 | + readonly kind: 'activity'; |
| 41 | + /** The namespace of the activity execution. */ |
| 42 | + readonly namespace: string; |
| 43 | + /** The activity ID. */ |
| 44 | + readonly id?: string; |
| 45 | + /** The activity run ID (only for standalone activities). */ |
| 46 | + readonly runId?: string; |
| 47 | + /** The activity type name, if available. */ |
| 48 | + readonly type?: string; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Identity of the workflow or activity that produced the payloads being stored. |
| 53 | + * |
| 54 | + * @internal |
| 55 | + * @experimental |
| 56 | + */ |
| 57 | +export type StorageDriverTargetInfo = StorageDriverWorkflowInfo | StorageDriverActivityInfo; |
| 58 | + |
| 59 | +/** |
| 60 | + * Context handed to {@link StorageDriver.store} (and to the selector). |
| 61 | + * |
| 62 | + * @internal |
| 63 | + * @experimental |
| 64 | + */ |
| 65 | +export interface StorageDriverStoreContext { |
| 66 | + /** Aborts the in-flight operation; siblings are cancelled on first error. */ |
| 67 | + abortSignal?: AbortSignal; |
| 68 | + /** Identity of the workflow / activity that produced the payloads. */ |
| 69 | + target?: StorageDriverTargetInfo; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Context handed to {@link StorageDriver.retrieve}. |
| 74 | + * |
| 75 | + * @internal |
| 76 | + * @experimental |
| 77 | + */ |
| 78 | +export interface StorageDriverRetrieveContext { |
| 79 | + abortSignal?: AbortSignal; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * External storage backend driver. |
| 84 | + * |
| 85 | + * `name` is the per-instance routing key written into the wire format. |
| 86 | + * `type` is a stable cross-language driver-implementation identifier |
| 87 | + * reported via worker heartbeat (e.g. `"aws.s3driver"`). |
| 88 | + * |
| 89 | + * @internal |
| 90 | + * @experimental |
| 91 | + */ |
| 92 | +export interface StorageDriver { |
| 93 | + readonly name: string; |
| 94 | + readonly type: string; |
| 95 | + store(context: StorageDriverStoreContext, payloads: Payload[]): Promise<StorageDriverClaim[]>; |
| 96 | + retrieve(context: StorageDriverRetrieveContext, claims: StorageDriverClaim[]): Promise<Payload[]>; |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * User-supplied function that picks the destination driver for a given |
| 101 | + * payload, or returns `null` to keep the payload inline. |
| 102 | + * |
| 103 | + * @internal |
| 104 | + * @experimental |
| 105 | + */ |
| 106 | +export type StorageDriverSelector = (context: StorageDriverStoreContext, payload: Payload) => StorageDriver | null; |
| 107 | + |
| 108 | +// ============================================================================ |
| 109 | +// Configuration |
| 110 | +// ============================================================================ |
| 111 | + |
| 112 | +/** Default {@link ExternalStorage.payloadSizeThreshold}: 256 KiB. */ |
| 113 | +const DEFAULT_PAYLOAD_SIZE_THRESHOLD = 256 * 1024; |
| 114 | + |
| 115 | +/** |
| 116 | + * Configuration for external storage. Holds the registered drivers, an |
| 117 | + * optional selector, and the size threshold above which payloads are |
| 118 | + * eligible for offloading to external storage. A selector function is |
| 119 | + * required when more than one driver is registered. |
| 120 | + * |
| 121 | + * @internal |
| 122 | + * @experimental |
| 123 | + */ |
| 124 | +export class ExternalStorage { |
| 125 | + readonly drivers: StorageDriver[]; |
| 126 | + /** |
| 127 | + * Selects the destination driver for each payload, or returns `null` to keep |
| 128 | + * the payload inline. |
| 129 | + */ |
| 130 | + readonly driverSelector: StorageDriverSelector; |
| 131 | + readonly payloadSizeThreshold: number; |
| 132 | + private readonly driversByName: ReadonlyMap<string, StorageDriver>; |
| 133 | + |
| 134 | + constructor({ |
| 135 | + drivers, |
| 136 | + driverSelector, |
| 137 | + payloadSizeThreshold = DEFAULT_PAYLOAD_SIZE_THRESHOLD, |
| 138 | + }: { |
| 139 | + drivers: StorageDriver[]; |
| 140 | + driverSelector?: StorageDriverSelector; |
| 141 | + /** Omit for default (256 KiB). Set `0` to consider all payloads regardless of size. */ |
| 142 | + payloadSizeThreshold?: number; |
| 143 | + }) { |
| 144 | + if (!Array.isArray(drivers) || drivers.length === 0) { |
| 145 | + throw new ValueError('ExternalStorage requires at least one driver'); |
| 146 | + } |
| 147 | + if ( |
| 148 | + typeof payloadSizeThreshold !== 'number' || |
| 149 | + !Number.isFinite(payloadSizeThreshold) || |
| 150 | + payloadSizeThreshold < 0 |
| 151 | + ) { |
| 152 | + throw new ValueError( |
| 153 | + `ExternalStorage.payloadSizeThreshold must be a non-negative finite number, got ${String(payloadSizeThreshold)}` |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + const driversByName = new Map<string, StorageDriver>(); |
| 158 | + for (const driver of drivers) { |
| 159 | + if (typeof driver?.name !== 'string' || driver.name.length === 0) { |
| 160 | + throw new ValueError("Storage driver 'name' must be a non-empty string"); |
| 161 | + } |
| 162 | + if (driversByName.has(driver.name)) { |
| 163 | + throw new ValueError(`Duplicate storage driver name: '${driver.name}'`); |
| 164 | + } |
| 165 | + driversByName.set(driver.name, driver); |
| 166 | + } |
| 167 | + |
| 168 | + if (driverSelector === undefined && driversByName.size > 1) { |
| 169 | + throw new ValueError('ExternalStorage.driverSelector is required when more than one driver is registered'); |
| 170 | + } |
| 171 | + |
| 172 | + this.drivers = [...drivers]; |
| 173 | + this.driverSelector = driverSelector ?? (() => drivers[0] as StorageDriver); |
| 174 | + this.payloadSizeThreshold = payloadSizeThreshold; |
| 175 | + this.driversByName = driversByName; |
| 176 | + } |
| 177 | + |
| 178 | + /** Look up a registered driver by name. Returns `null` if no driver with that name is registered. */ |
| 179 | + getDriver(name: string): StorageDriver | null { |
| 180 | + return this.driversByName.get(name) ?? null; |
| 181 | + } |
| 182 | +} |
0 commit comments