|
| 1 | +import { InvokeStore } from '@aws/lambda-invoke-store'; |
| 2 | +import type { |
| 3 | + BaseRecord, |
| 4 | + BatchProcessingOptions, |
| 5 | + EventSourceDataClassTypes, |
| 6 | + PartialItemFailureResponse, |
| 7 | +} from './types.js'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Manages storage of batch processing state with automatic context detection. |
| 11 | + * |
| 12 | + * This class abstracts the storage mechanism for batch processing state, |
| 13 | + * automatically choosing between InvokeStore (when in Lambda context) and |
| 14 | + * fallback instance variables (when outside Lambda context). The decision is |
| 15 | + * made at runtime on every method call to support Lambda's concurrent execution |
| 16 | + * isolation. |
| 17 | + */ |
| 18 | +class BatchProcessingStore { |
| 19 | + readonly #recordsKey = Symbol('powertools.batch.records'); |
| 20 | + readonly #handlerKey = Symbol('powertools.batch.handler'); |
| 21 | + readonly #optionsKey = Symbol('powertools.batch.options'); |
| 22 | + readonly #failureMessagesKey = Symbol('powertools.batch.failureMessages'); |
| 23 | + readonly #successMessagesKey = Symbol('powertools.batch.successMessages'); |
| 24 | + readonly #batchResponseKey = Symbol('powertools.batch.batchResponse'); |
| 25 | + readonly #errorsKey = Symbol('powertools.batch.errors'); |
| 26 | + |
| 27 | + #fallbackRecords: BaseRecord[] = []; |
| 28 | + #fallbackHandler: CallableFunction = () => {}; |
| 29 | + #fallbackOptions?: BatchProcessingOptions; |
| 30 | + #fallbackFailureMessages: EventSourceDataClassTypes[] = []; |
| 31 | + #fallbackSuccessMessages: EventSourceDataClassTypes[] = []; |
| 32 | + #fallbackBatchResponse: PartialItemFailureResponse = { |
| 33 | + batchItemFailures: [], |
| 34 | + }; |
| 35 | + #fallbackErrors: Error[] = []; |
| 36 | + |
| 37 | + public getRecords(): BaseRecord[] { |
| 38 | + if (InvokeStore.getContext() === undefined) { |
| 39 | + return this.#fallbackRecords; |
| 40 | + } |
| 41 | + return (InvokeStore.get(this.#recordsKey) as BaseRecord[]) ?? []; |
| 42 | + } |
| 43 | + |
| 44 | + public setRecords(records: BaseRecord[]): void { |
| 45 | + if (InvokeStore.getContext() === undefined) { |
| 46 | + this.#fallbackRecords = records; |
| 47 | + return; |
| 48 | + } |
| 49 | + InvokeStore.set(this.#recordsKey, records); |
| 50 | + } |
| 51 | + |
| 52 | + public getHandler(): CallableFunction { |
| 53 | + if (InvokeStore.getContext() === undefined) { |
| 54 | + return this.#fallbackHandler; |
| 55 | + } |
| 56 | + return ( |
| 57 | + (InvokeStore.get(this.#handlerKey) as CallableFunction) ?? (() => {}) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public setHandler(handler: CallableFunction): void { |
| 62 | + if (InvokeStore.getContext() === undefined) { |
| 63 | + this.#fallbackHandler = handler; |
| 64 | + return; |
| 65 | + } |
| 66 | + InvokeStore.set(this.#handlerKey, handler); |
| 67 | + } |
| 68 | + |
| 69 | + public getOptions(): BatchProcessingOptions | undefined { |
| 70 | + if (InvokeStore.getContext() === undefined) { |
| 71 | + return this.#fallbackOptions; |
| 72 | + } |
| 73 | + return InvokeStore.get(this.#optionsKey) as |
| 74 | + | BatchProcessingOptions |
| 75 | + | undefined; |
| 76 | + } |
| 77 | + |
| 78 | + public setOptions(options: BatchProcessingOptions | undefined): void { |
| 79 | + if (InvokeStore.getContext() === undefined) { |
| 80 | + this.#fallbackOptions = options; |
| 81 | + return; |
| 82 | + } |
| 83 | + InvokeStore.set(this.#optionsKey, options); |
| 84 | + } |
| 85 | + |
| 86 | + public getFailureMessages(): EventSourceDataClassTypes[] { |
| 87 | + if (InvokeStore.getContext() === undefined) { |
| 88 | + return this.#fallbackFailureMessages; |
| 89 | + } |
| 90 | + return ( |
| 91 | + (InvokeStore.get( |
| 92 | + this.#failureMessagesKey |
| 93 | + ) as EventSourceDataClassTypes[]) ?? [] |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public setFailureMessages(messages: EventSourceDataClassTypes[]): void { |
| 98 | + if (InvokeStore.getContext() === undefined) { |
| 99 | + this.#fallbackFailureMessages = messages; |
| 100 | + return; |
| 101 | + } |
| 102 | + InvokeStore.set(this.#failureMessagesKey, messages); |
| 103 | + } |
| 104 | + |
| 105 | + public getSuccessMessages(): EventSourceDataClassTypes[] { |
| 106 | + if (InvokeStore.getContext() === undefined) { |
| 107 | + return this.#fallbackSuccessMessages; |
| 108 | + } |
| 109 | + return ( |
| 110 | + (InvokeStore.get( |
| 111 | + this.#successMessagesKey |
| 112 | + ) as EventSourceDataClassTypes[]) ?? [] |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public setSuccessMessages(messages: EventSourceDataClassTypes[]): void { |
| 117 | + if (InvokeStore.getContext() === undefined) { |
| 118 | + this.#fallbackSuccessMessages = messages; |
| 119 | + return; |
| 120 | + } |
| 121 | + InvokeStore.set(this.#successMessagesKey, messages); |
| 122 | + } |
| 123 | + |
| 124 | + public getBatchResponse(): PartialItemFailureResponse { |
| 125 | + if (InvokeStore.getContext() === undefined) { |
| 126 | + return this.#fallbackBatchResponse; |
| 127 | + } |
| 128 | + return ( |
| 129 | + (InvokeStore.get( |
| 130 | + this.#batchResponseKey |
| 131 | + ) as PartialItemFailureResponse) ?? { batchItemFailures: [] } |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + public setBatchResponse(response: PartialItemFailureResponse): void { |
| 136 | + if (InvokeStore.getContext() === undefined) { |
| 137 | + this.#fallbackBatchResponse = response; |
| 138 | + return; |
| 139 | + } |
| 140 | + InvokeStore.set(this.#batchResponseKey, response); |
| 141 | + } |
| 142 | + |
| 143 | + public getErrors(): Error[] { |
| 144 | + if (InvokeStore.getContext() === undefined) { |
| 145 | + return this.#fallbackErrors; |
| 146 | + } |
| 147 | + return (InvokeStore.get(this.#errorsKey) as Error[]) ?? []; |
| 148 | + } |
| 149 | + |
| 150 | + public setErrors(errors: Error[]): void { |
| 151 | + if (InvokeStore.getContext() === undefined) { |
| 152 | + this.#fallbackErrors = errors; |
| 153 | + return; |
| 154 | + } |
| 155 | + InvokeStore.set(this.#errorsKey, errors); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export { BatchProcessingStore }; |
0 commit comments