@microsoft/agents-hosting 1.9.0-beta.18.g6b8111bba5 → 1.9.0-beta.20.g0c48a31a21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -10
- package/dist/package.json +3 -3
- package/dist/src/app/agentApplication.d.ts +1 -0
- package/dist/src/app/agentApplication.js +18 -5
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +9 -2
- package/dist/src/app/agentApplicationBuilder.js +9 -0
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +10 -4
- package/dist/src/app/auth/handlerStorage.d.ts +5 -3
- package/dist/src/app/auth/handlerStorage.js +36 -10
- package/dist/src/app/auth/handlerStorage.js.map +1 -1
- package/dist/src/app/auth/types.d.ts +2 -2
- package/dist/src/app/index.d.ts +1 -0
- package/dist/src/app/proactive/proactive.js +10 -6
- package/dist/src/app/proactive/proactive.js.map +1 -1
- package/dist/src/app/proactive/proactiveOptions.d.ts +2 -2
- package/dist/src/app/rateLimit.d.ts +315 -0
- package/dist/src/app/rateLimit.js +333 -0
- package/dist/src/app/rateLimit.js.map +1 -0
- package/dist/src/app/turnState.d.ts +6 -3
- package/dist/src/app/turnState.js +43 -4
- package/dist/src/app/turnState.js.map +1 -1
- package/dist/src/errorHelper.js +40 -9
- package/dist/src/errorHelper.js.map +1 -1
- package/dist/src/observability/traces.d.ts +6 -4
- package/dist/src/observability/traces.js +10 -9
- package/dist/src/observability/traces.js.map +1 -1
- package/dist/src/state/agentState.d.ts +8 -3
- package/dist/src/state/agentState.js +26 -8
- package/dist/src/state/agentState.js.map +1 -1
- package/dist/src/state/conversationState.d.ts +2 -2
- package/dist/src/state/conversationState.js.map +1 -1
- package/dist/src/state/userState.d.ts +2 -2
- package/dist/src/state/userState.js.map +1 -1
- package/dist/src/statusCodes.d.ts +5 -0
- package/dist/src/statusCodes.js +5 -0
- package/dist/src/statusCodes.js.map +1 -1
- package/dist/src/storage/fileStorage.d.ts +83 -71
- package/dist/src/storage/fileStorage.js +272 -136
- package/dist/src/storage/fileStorage.js.map +1 -1
- package/dist/src/storage/memoryStorage.d.ts +85 -62
- package/dist/src/storage/memoryStorage.js +240 -109
- package/dist/src/storage/memoryStorage.js.map +1 -1
- package/dist/src/storage/storage.d.ts +101 -16
- package/dist/src/storage/storage.js +25 -0
- package/dist/src/storage/storage.js.map +1 -1
- package/dist/src/storage/storageCompatibility.d.ts +17 -0
- package/dist/src/storage/storageCompatibility.js +157 -0
- package/dist/src/storage/storageCompatibility.js.map +1 -0
- package/package.json +3 -3
- package/src/app/agentApplication.ts +16 -0
- package/src/app/agentApplicationBuilder.ts +13 -2
- package/src/app/agentApplicationOptions.ts +11 -4
- package/src/app/auth/handlerStorage.ts +51 -7
- package/src/app/auth/types.ts +2 -2
- package/src/app/index.ts +7 -0
- package/src/app/proactive/proactive.ts +19 -9
- package/src/app/proactive/proactiveOptions.ts +2 -2
- package/src/app/rateLimit.ts +659 -0
- package/src/app/turnState.ts +50 -7
- package/src/errorHelper.ts +46 -8
- package/src/observability/traces.ts +8 -7
- package/src/state/agentState.ts +39 -13
- package/src/state/conversationState.ts +2 -2
- package/src/state/userState.ts +2 -2
- package/src/statusCodes.ts +6 -0
- package/src/storage/fileStorage.ts +289 -134
- package/src/storage/memoryStorage.ts +266 -109
- package/src/storage/storage.ts +115 -17
- package/src/storage/storageCompatibility.ts +185 -0
|
@@ -3,208 +3,363 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import fs from 'node:fs'
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { randomUUID } from 'node:crypto'
|
|
9
|
+
import { ExceptionHelper } from '@microsoft/agents-activity'
|
|
10
10
|
import { debug, redactString, trace } from '@microsoft/agents-telemetry'
|
|
11
|
+
import { Errors } from '../errorHelper'
|
|
11
12
|
import { StorageTraceDefinitions } from '../observability'
|
|
13
|
+
import { getStorageWriteExpiry } from './storageExpiry'
|
|
14
|
+
import {
|
|
15
|
+
StorageDeleteOptions,
|
|
16
|
+
StorageDeleteResults,
|
|
17
|
+
StorageOperationStatus,
|
|
18
|
+
StorageReadResults,
|
|
19
|
+
StorageWriteMode,
|
|
20
|
+
StorageWriteOptions,
|
|
21
|
+
StorageWriteResults,
|
|
22
|
+
Storage,
|
|
23
|
+
StorageV2,
|
|
24
|
+
StoreItem,
|
|
25
|
+
} from './storage'
|
|
12
26
|
|
|
13
27
|
const logger = debug('agents:file-storage')
|
|
14
28
|
|
|
29
|
+
class FileStorageInternals {
|
|
30
|
+
private readonly statePath: string
|
|
31
|
+
private readonly versionsPath: string
|
|
32
|
+
private readonly expirationsPath: string
|
|
33
|
+
readonly state: Record<string, unknown>
|
|
34
|
+
readonly versions: Record<string, string>
|
|
35
|
+
readonly expirations: Record<string, number>
|
|
36
|
+
|
|
37
|
+
constructor (folder: string) {
|
|
38
|
+
fs.mkdirSync(folder, { recursive: true })
|
|
39
|
+
this.statePath = path.join(folder, 'state.json')
|
|
40
|
+
this.versionsPath = path.join(folder, 'state.versions.json')
|
|
41
|
+
this.expirationsPath = path.join(folder, 'state.expirations.json')
|
|
42
|
+
if (!fs.existsSync(this.statePath)) fs.writeFileSync(this.statePath, '{}')
|
|
43
|
+
this.state = JSON.parse(fs.readFileSync(this.statePath, 'utf8')) as Record<string, unknown>
|
|
44
|
+
this.versions = fs.existsSync(this.versionsPath)
|
|
45
|
+
? JSON.parse(fs.readFileSync(this.versionsPath, 'utf8')) as Record<string, string>
|
|
46
|
+
: {}
|
|
47
|
+
this.expirations = fs.existsSync(this.expirationsPath)
|
|
48
|
+
? JSON.parse(fs.readFileSync(this.expirationsPath, 'utf8')) as Record<string, number>
|
|
49
|
+
: {}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
flush (): void {
|
|
53
|
+
fs.writeFileSync(this.statePath, JSON.stringify(this.state, null, 2))
|
|
54
|
+
if (fs.existsSync(this.versionsPath) || Object.keys(this.versions).length > 0) {
|
|
55
|
+
fs.writeFileSync(this.versionsPath, JSON.stringify(this.versions, null, 2))
|
|
56
|
+
}
|
|
57
|
+
if (fs.existsSync(this.expirationsPath) || Object.keys(this.expirations).length > 0) {
|
|
58
|
+
fs.writeFileSync(this.expirationsPath, JSON.stringify(this.expirations, null, 2))
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getOrCreateVersion (key: string): string {
|
|
63
|
+
const version = this.versions[key] ?? randomUUID()
|
|
64
|
+
this.versions[key] = version
|
|
65
|
+
return version
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
isExpired (key: string): boolean {
|
|
69
|
+
const expiresAt = this.expirations[key]
|
|
70
|
+
return expiresAt !== undefined && expiresAt <= Date.now()
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
remove (key: string): void {
|
|
74
|
+
delete this.state[key]
|
|
75
|
+
delete this.versions[key]
|
|
76
|
+
delete this.expirations[key]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
15
80
|
/**
|
|
16
81
|
* A file-based storage implementation that persists data to the local filesystem.
|
|
17
82
|
*
|
|
18
83
|
* @remarks
|
|
19
|
-
* FileStorage stores all data in a single JSON file named
|
|
84
|
+
* FileStorage stores all data in a single JSON file named `state.json` within a specified folder.
|
|
20
85
|
* This implementation is suitable for development scenarios, local testing, and single-instance
|
|
21
86
|
* deployments where shared state across multiple instances is not required.
|
|
22
87
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* wrapped in Promise interfaces
|
|
88
|
+
* Values remain a key-value JSON object in `state.json`. V2 keeps generated storage versions in
|
|
89
|
+
* `state.versions.json` so a value's own `eTag` property is not changed. All operations use
|
|
90
|
+
* synchronous file I/O wrapped in Promise interfaces. Use {@link FileStorageV2} for the
|
|
91
|
+
* structured StorageV2 contract.
|
|
92
|
+
* V2 supports create-only, replace, and expected-version conditions.
|
|
93
|
+
*
|
|
94
|
+
* The inherited constructor creates the folder and `state.json` when needed, then loads values
|
|
95
|
+
* and V2 version metadata into memory. It creates `state.versions.json` on the first V2 write.
|
|
26
96
|
*
|
|
27
97
|
* ### Warning
|
|
28
98
|
* This implementation does not provide:
|
|
29
99
|
* - Thread safety for concurrent access
|
|
30
|
-
* - Optimistic concurrency control (eTag support)
|
|
31
100
|
* - Atomic operations across multiple keys
|
|
32
101
|
* - Scale for large datasets
|
|
33
102
|
*
|
|
34
|
-
* For production scenarios requiring these features,
|
|
35
|
-
* database-backed storage implementations instead.
|
|
103
|
+
* For production scenarios requiring these features, use a database-backed storage implementation.
|
|
36
104
|
*
|
|
37
105
|
* @example
|
|
38
106
|
* ```typescript
|
|
39
|
-
* const
|
|
107
|
+
* const legacyStorage = new FileStorage('./data')
|
|
108
|
+
* const storageV2 = new FileStorageV2('./data-v2')
|
|
40
109
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* 'conversation456': { turn: 5, context: 'discussing weather' }
|
|
45
|
-
* });
|
|
110
|
+
* await storageV2.write({
|
|
111
|
+
* user123: { name: 'John', lastSeen: new Date().toISOString() }
|
|
112
|
+
* })
|
|
46
113
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* console.log(data.user123); // { name: 'John', lastSeen: '...' }
|
|
114
|
+
* const result = await storageV2.read(['user123'])
|
|
115
|
+
* console.log(result.user123.value)
|
|
50
116
|
*
|
|
51
|
-
*
|
|
52
|
-
* await storage.delete(['conversation456']);
|
|
117
|
+
* await storageV2.delete(['user123'])
|
|
53
118
|
* ```
|
|
54
|
-
*
|
|
55
119
|
*/
|
|
56
|
-
export class FileStorage implements Storage {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
120
|
+
export class FileStorage extends FileStorageInternals implements Storage {
|
|
121
|
+
/**
|
|
122
|
+
* Reads legacy items from `state.json`.
|
|
123
|
+
*
|
|
124
|
+
* @param keys The keys to read; must not be empty.
|
|
125
|
+
* @returns Existing items keyed by storage key. Missing keys are omitted.
|
|
126
|
+
*/
|
|
127
|
+
async read (keys: string[]): Promise<StoreItem> {
|
|
128
|
+
return trace(StorageTraceDefinitions.read, async ({ record }) => {
|
|
129
|
+
record({ keyCount: keys?.length })
|
|
130
|
+
if (!keys || keys.length === 0) {
|
|
131
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageReadKeysRequired)
|
|
132
|
+
}
|
|
133
|
+
const items = Object.fromEntries(keys
|
|
134
|
+
.filter(key => Boolean(this.state[key]))
|
|
135
|
+
.map(key => [key, this.state[key]])) as StoreItem
|
|
136
|
+
return items
|
|
137
|
+
})
|
|
138
|
+
}
|
|
60
139
|
|
|
61
140
|
/**
|
|
62
|
-
*
|
|
141
|
+
* Writes legacy items to `state.json`.
|
|
63
142
|
*
|
|
64
|
-
* @param
|
|
65
|
-
* @throws
|
|
143
|
+
* @param changes The items to write, keyed by storage key.
|
|
144
|
+
* @throws If `changes` is invalid or the file cannot be written.
|
|
145
|
+
*/
|
|
146
|
+
async write (changes: StoreItem): Promise<void> {
|
|
147
|
+
return trace(StorageTraceDefinitions.write, async ({ record }) => {
|
|
148
|
+
record({ keyCount: changes ? Object.keys(changes).length : undefined })
|
|
149
|
+
if (!changes || typeof changes !== 'object' || Array.isArray(changes)) {
|
|
150
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageWriteChangesRequired)
|
|
151
|
+
}
|
|
152
|
+
Object.assign(this.state, changes)
|
|
153
|
+
for (const key of Object.keys(changes)) {
|
|
154
|
+
delete this.versions[key]
|
|
155
|
+
delete this.expirations[key]
|
|
156
|
+
}
|
|
157
|
+
this.flush()
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Deletes legacy items from `state.json`.
|
|
66
163
|
*
|
|
67
|
-
* @
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
164
|
+
* @param keys The keys to delete; must not be empty. Missing keys are ignored.
|
|
165
|
+
*/
|
|
166
|
+
async delete (keys: string[]): Promise<void> {
|
|
167
|
+
return trace(StorageTraceDefinitions.delete, async ({ record }) => {
|
|
168
|
+
record({ keyCount: keys?.length })
|
|
169
|
+
if (!keys || keys.length === 0) {
|
|
170
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageDeleteKeysRequired)
|
|
171
|
+
}
|
|
172
|
+
for (const key of keys) {
|
|
173
|
+
this.remove(key)
|
|
174
|
+
}
|
|
175
|
+
this.flush()
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A file-backed provider for the structured {@link StorageV2} contract.
|
|
182
|
+
*
|
|
183
|
+
* This is the V2 counterpart to {@link FileStorage}. Values are stored in `state.json`, while
|
|
184
|
+
* generated storage versions are stored separately in `state.versions.json`; an `eTag` property
|
|
185
|
+
* in an application value is therefore preserved. Reads and mutations return a result for every
|
|
186
|
+
* requested key and support create-only, replace, and expected-version conditions.
|
|
187
|
+
*
|
|
188
|
+
* Like {@link FileStorage}, this provider is for development, local testing, and single-instance
|
|
189
|
+
* deployments. It does not provide cross-process concurrency or multi-key atomicity.
|
|
190
|
+
*/
|
|
191
|
+
export class FileStorageV2 extends StorageV2 {
|
|
192
|
+
private readonly internals: FileStorageInternals
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Creates a V2 file storage provider.
|
|
72
196
|
*
|
|
197
|
+
* @param folder The absolute or relative folder where `state.json` is stored.
|
|
198
|
+
* @throws May throw filesystem errors if the folder or state file cannot be created or read.
|
|
73
199
|
*/
|
|
74
200
|
constructor (folder: string) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
fs.mkdirSync(folder, { recursive: true })
|
|
78
|
-
}
|
|
79
|
-
if (!fs.existsSync(path.join(folder, 'state.json'))) {
|
|
80
|
-
fs.writeFileSync(path.join(folder, 'state.json'), '{}')
|
|
81
|
-
}
|
|
82
|
-
if (!fs.existsSync(path.join(folder, 'state.metadata.json'))) {
|
|
83
|
-
fs.writeFileSync(path.join(folder, 'state.metadata.json'), '{}')
|
|
84
|
-
}
|
|
85
|
-
const data = fs.readFileSync(path.join(folder, 'state.json'), 'utf8')
|
|
86
|
-
this._stateFile = JSON.parse(data)
|
|
87
|
-
const metadata = fs.readFileSync(path.join(folder, 'state.metadata.json'), 'utf8')
|
|
88
|
-
this._metadataFile = JSON.parse(metadata)
|
|
89
|
-
this._metadataFile.expirations ??= {}
|
|
201
|
+
super()
|
|
202
|
+
this.internals = new FileStorageInternals(folder)
|
|
90
203
|
}
|
|
91
204
|
|
|
92
205
|
/**
|
|
93
|
-
* Reads
|
|
94
|
-
*
|
|
95
|
-
* @param keys Array of keys to read from storage
|
|
96
|
-
* @returns Promise resolving to an object containing the requested items (keys that don't exist are omitted)
|
|
97
|
-
* @throws ReferenceError if keys array is empty or undefined
|
|
98
|
-
*
|
|
99
|
-
* @remarks
|
|
100
|
-
* This method reads from the in-memory cache that was loaded during construction,
|
|
101
|
-
* making it very fast but potentially returning stale data if the file was
|
|
102
|
-
* modified by external processes.
|
|
206
|
+
* Reads items and their stored versions from the local state files.
|
|
103
207
|
*
|
|
208
|
+
* @param keys The keys to read. Empty batches are valid.
|
|
209
|
+
* @returns A result for every key, with `notFound` for missing items. Returned values are cloned.
|
|
104
210
|
*/
|
|
105
|
-
read (keys: string[])
|
|
211
|
+
async read<T extends object = Record<string, unknown>> (keys: string[]): Promise<StorageReadResults<T>> {
|
|
106
212
|
return trace(StorageTraceDefinitions.read, async ({ record }) => {
|
|
107
213
|
record({ keyCount: keys?.length })
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
logger.info('Item expired, deleting from storage', { key: redactString(key, true) })
|
|
117
|
-
delete this._stateFile[key]
|
|
118
|
-
delete this._metadataFile.expirations![key]
|
|
119
|
-
storageChanged = true
|
|
120
|
-
continue
|
|
121
|
-
}
|
|
122
|
-
const item = this._stateFile[key]
|
|
123
|
-
if (item) {
|
|
124
|
-
data[key] = item
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (storageChanged) {
|
|
128
|
-
this.writeFiles()
|
|
214
|
+
validateV2Keys(keys)
|
|
215
|
+
let createdVersion = false
|
|
216
|
+
const results = Object.fromEntries(keys.map(key => {
|
|
217
|
+
if (!Object.prototype.hasOwnProperty.call(this.internals.state, key) || this.internals.isExpired(key)) {
|
|
218
|
+
if (this.internals.isExpired(key)) {
|
|
219
|
+
logger.info('Item expired, deleting from storage', { key: redactString(key, true) })
|
|
220
|
+
this.internals.remove(key)
|
|
221
|
+
createdVersion = true
|
|
129
222
|
}
|
|
130
|
-
|
|
223
|
+
return [key, { key, status: StorageOperationStatus.NotFound }]
|
|
131
224
|
}
|
|
132
|
-
|
|
225
|
+
const value = structuredClone(this.internals.state[key]) as T
|
|
226
|
+
const version = this.internals.versions[key]
|
|
227
|
+
createdVersion ||= version === undefined
|
|
228
|
+
return [key, {
|
|
229
|
+
key,
|
|
230
|
+
status: StorageOperationStatus.Succeeded,
|
|
231
|
+
value,
|
|
232
|
+
version: version ?? this.internals.getOrCreateVersion(key),
|
|
233
|
+
}]
|
|
234
|
+
}))
|
|
235
|
+
if (createdVersion) this.internals.flush()
|
|
236
|
+
return results
|
|
133
237
|
})
|
|
134
238
|
}
|
|
135
239
|
|
|
136
240
|
/**
|
|
137
|
-
* Writes
|
|
138
|
-
*
|
|
139
|
-
* @param changes Object containing key-value pairs to write to storage
|
|
140
|
-
* @returns Promise that resolves when the write operation completes
|
|
141
|
-
*
|
|
142
|
-
* @remarks
|
|
143
|
-
* This method updates both the in-memory cache and writes the entire state
|
|
144
|
-
* to the state.json file. The file is written with pretty-printing (2-space indentation)
|
|
145
|
-
* for better readability during development and debugging.
|
|
146
|
-
*
|
|
147
|
-
* > [!NOTE]
|
|
148
|
-
* > This implementation does not support eTag-based optimistic concurrency control.
|
|
149
|
-
* > Any eTag values in the changes object are ignored.
|
|
241
|
+
* Writes cloned items and generated versions to the local state files.
|
|
150
242
|
*
|
|
243
|
+
* @param changes The values to write, keyed by storage key.
|
|
244
|
+
* @param options Create-only, replace, or expected-version conditions.
|
|
245
|
+
* @returns A result for every supplied key, including concurrency-condition outcomes.
|
|
151
246
|
*/
|
|
152
|
-
write (changes:
|
|
247
|
+
async write<T extends object = Record<string, unknown>> (changes: Record<string, T>, options?: StorageWriteOptions): Promise<StorageWriteResults> {
|
|
153
248
|
return trace(StorageTraceDefinitions.write, async ({ record }) => {
|
|
249
|
+
record({ keyCount: changes ? Object.keys(changes).length : undefined })
|
|
250
|
+
validateExpectedVersion(options?.expectedVersion)
|
|
251
|
+
validateV2Changes(changes)
|
|
252
|
+
|
|
253
|
+
const results: StorageWriteResults = {}
|
|
154
254
|
const expiresAt = getStorageWriteExpiry(options)
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
255
|
+
const mode = options?.mode ?? StorageWriteMode.Upsert
|
|
256
|
+
validateWriteMode(mode)
|
|
257
|
+
let changed = false
|
|
258
|
+
let createdVersion = false
|
|
259
|
+
for (const [key, value] of Object.entries(changes)) {
|
|
260
|
+
if (this.internals.isExpired(key)) {
|
|
261
|
+
logger.info('Item expired, deleting from storage', { key: redactString(key, true) })
|
|
262
|
+
this.internals.remove(key)
|
|
263
|
+
changed = true
|
|
264
|
+
}
|
|
265
|
+
const current = this.internals.state[key]
|
|
266
|
+
createdVersion ||= current !== undefined && this.internals.versions[key] === undefined
|
|
267
|
+
const currentVersion = current === undefined
|
|
268
|
+
? undefined
|
|
269
|
+
: this.internals.getOrCreateVersion(key)
|
|
270
|
+
if (mode === StorageWriteMode.CreateOnly && current !== undefined) {
|
|
271
|
+
results[key] = { key, status: StorageOperationStatus.Conflict, version: currentVersion }
|
|
272
|
+
} else if (mode === StorageWriteMode.Replace && current === undefined) {
|
|
273
|
+
results[key] = { key, status: StorageOperationStatus.NotFound }
|
|
274
|
+
} else if (options?.expectedVersion !== undefined && options.expectedVersion !== currentVersion) {
|
|
275
|
+
results[key] = { key, status: StorageOperationStatus.ConditionNotMet, version: currentVersion }
|
|
161
276
|
} else {
|
|
162
|
-
|
|
277
|
+
const version = randomUUID()
|
|
278
|
+
this.internals.state[key] = structuredClone(value)
|
|
279
|
+
this.internals.versions[key] = version
|
|
280
|
+
if (expiresAt === undefined) delete this.internals.expirations[key]
|
|
281
|
+
else this.internals.expirations[key] = expiresAt
|
|
282
|
+
results[key] = { key, status: StorageOperationStatus.Succeeded, version }
|
|
283
|
+
changed = true
|
|
163
284
|
}
|
|
164
285
|
}
|
|
165
|
-
this.
|
|
286
|
+
if (changed || createdVersion) this.internals.flush()
|
|
287
|
+
return results
|
|
166
288
|
})
|
|
167
289
|
}
|
|
168
290
|
|
|
169
291
|
/**
|
|
170
|
-
* Deletes
|
|
171
|
-
*
|
|
172
|
-
* @param keys Array of keys to delete from storage
|
|
173
|
-
* @returns Promise that resolves when the delete operation completes
|
|
174
|
-
*
|
|
175
|
-
* @throws ReferenceError if keys array is empty or undefined
|
|
176
|
-
*
|
|
177
|
-
* @remarks
|
|
178
|
-
* This method removes the specified keys from both the in-memory cache
|
|
179
|
-
* and writes the updated state to the state.json file. Keys that don't
|
|
180
|
-
* exist in storage are silently ignored.
|
|
292
|
+
* Deletes items and their versions from the local state files.
|
|
181
293
|
*
|
|
294
|
+
* @param keys The keys to delete. Empty batches are valid.
|
|
295
|
+
* @param options An optional expected storage version.
|
|
296
|
+
* @returns A result for every supplied key.
|
|
182
297
|
*/
|
|
183
|
-
delete (keys: string[])
|
|
298
|
+
async delete (keys: string[], options?: StorageDeleteOptions): Promise<StorageDeleteResults> {
|
|
184
299
|
return trace(StorageTraceDefinitions.delete, async ({ record }) => {
|
|
185
300
|
record({ keyCount: keys?.length })
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
301
|
+
validateExpectedVersion(options?.expectedVersion)
|
|
302
|
+
validateV2Keys(keys)
|
|
303
|
+
|
|
304
|
+
const results: StorageDeleteResults = {}
|
|
305
|
+
let changed = false
|
|
306
|
+
let createdVersion = false
|
|
307
|
+
for (const key of keys) {
|
|
308
|
+
if (this.internals.isExpired(key)) {
|
|
309
|
+
logger.info('Item expired, deleting from storage', { key: redactString(key, true) })
|
|
310
|
+
this.internals.remove(key)
|
|
311
|
+
changed = true
|
|
312
|
+
}
|
|
313
|
+
const current = this.internals.state[key]
|
|
314
|
+
createdVersion ||= current !== undefined && this.internals.versions[key] === undefined
|
|
315
|
+
const currentVersion = current === undefined
|
|
316
|
+
? undefined
|
|
317
|
+
: this.internals.getOrCreateVersion(key)
|
|
318
|
+
if (current === undefined) {
|
|
319
|
+
results[key] = { key, status: StorageOperationStatus.NotFound }
|
|
320
|
+
} else if (options?.expectedVersion !== undefined && options.expectedVersion !== currentVersion) {
|
|
321
|
+
results[key] = { key, status: StorageOperationStatus.ConditionNotMet, version: currentVersion }
|
|
189
322
|
} else {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
}
|
|
194
|
-
this.writeFiles()
|
|
195
|
-
resolve(undefined)
|
|
323
|
+
this.internals.remove(key)
|
|
324
|
+
results[key] = { key, status: StorageOperationStatus.Succeeded, version: currentVersion }
|
|
325
|
+
changed = true
|
|
196
326
|
}
|
|
197
|
-
}
|
|
327
|
+
}
|
|
328
|
+
if (changed || createdVersion) this.internals.flush()
|
|
329
|
+
return results
|
|
198
330
|
})
|
|
199
331
|
}
|
|
332
|
+
}
|
|
200
333
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
334
|
+
function validateV2Keys (keys: string[]): void {
|
|
335
|
+
if (!Array.isArray(keys)) {
|
|
336
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageReadKeysRequired)
|
|
204
337
|
}
|
|
338
|
+
if (keys.some(key => typeof key !== 'string' || key.trim() === '')) {
|
|
339
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageV2KeyRequired)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function validateV2Changes (changes: Record<string, unknown>): void {
|
|
344
|
+
if (changes === null || typeof changes !== 'object' || Array.isArray(changes)) {
|
|
345
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageWriteChangesRequired)
|
|
346
|
+
}
|
|
347
|
+
if (Object.keys(changes).some(key => key.trim() === '')) {
|
|
348
|
+
throw ExceptionHelper.generateException(ReferenceError, Errors.StorageV2KeyRequired)
|
|
349
|
+
}
|
|
350
|
+
if (Object.values(changes).some(value => value === null || typeof value !== 'object' || Array.isArray(value))) {
|
|
351
|
+
throw ExceptionHelper.generateException(TypeError, Errors.StorageV2ValueRequired)
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function validateExpectedVersion (expectedVersion: string | undefined): void {
|
|
356
|
+
if (expectedVersion === '') {
|
|
357
|
+
throw ExceptionHelper.generateException(RangeError, Errors.StorageV2ExpectedVersionEmpty)
|
|
358
|
+
}
|
|
359
|
+
}
|
|
205
360
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
361
|
+
function validateWriteMode (mode: StorageWriteMode): void {
|
|
362
|
+
if (!Object.values(StorageWriteMode).includes(mode)) {
|
|
363
|
+
throw ExceptionHelper.generateException(RangeError, Errors.StorageV2WriteModeUnsupported, undefined, { mode: String(mode) })
|
|
209
364
|
}
|
|
210
365
|
}
|