@onereach/state-machine-driver-or-flow-mf 1.0.0-framework.2
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 +3 -0
- package/dist/index.d.mts +145 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +55 -0
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { DocumentDriver, DriverCapabilities, DriverDeleteOptions, DriverLoadOptions, DriverLoadResult, DriverSaveOptions, DriverSaveResult, StateMachineInstanceDriver, StateMachineInstanceReference, StateTemplateDriver, StateTemplateReference } from "@onereach/state-machine-store";
|
|
2
|
+
import { CONFIG, ICacheFacade, IMergeField, IMergeFieldType, IProcess, LockAcquireArgs } from "@onereach/flow-sdk/types/index.js";
|
|
3
|
+
//#region src/generic-driver/types.d.ts
|
|
4
|
+
interface FlowStorageDocumentDriverOptions<TReference, TConfig extends CONFIG> {
|
|
5
|
+
process: IProcess<TConfig>;
|
|
6
|
+
cache?: ICacheFacade;
|
|
7
|
+
mergeField(this: void, reference: TReference): IMergeField;
|
|
8
|
+
/**
|
|
9
|
+
Options for acquiring a lock on the document.
|
|
10
|
+
*/
|
|
11
|
+
lock?: LockAcquireOptions;
|
|
12
|
+
}
|
|
13
|
+
type LockAcquireOptions = {
|
|
14
|
+
/**
|
|
15
|
+
* How long acquired lock is valid for.
|
|
16
|
+
* @default 30_000 // milliseconds.
|
|
17
|
+
*/
|
|
18
|
+
lockTimeout?: LockAcquireArgs['lockTimeout'];
|
|
19
|
+
/**
|
|
20
|
+
* How long to wait for a lock if it is not available.
|
|
21
|
+
* @default 10_000 // milliseconds.
|
|
22
|
+
*/
|
|
23
|
+
waitTimeout?: LockAcquireArgs['waitTimeout'];
|
|
24
|
+
/**
|
|
25
|
+
* How long to delay between attempts to acquire a lock.
|
|
26
|
+
* @default 200 // milliseconds.
|
|
27
|
+
*/
|
|
28
|
+
delayTimeout?: LockAcquireArgs['delayTimeout'];
|
|
29
|
+
};
|
|
30
|
+
interface StoredDocument<TState = unknown> {
|
|
31
|
+
/**
|
|
32
|
+
Schema version of the document.
|
|
33
|
+
*/
|
|
34
|
+
schemaVersion: 1;
|
|
35
|
+
/**
|
|
36
|
+
Current revision of the document.
|
|
37
|
+
*/
|
|
38
|
+
revision: string;
|
|
39
|
+
/**
|
|
40
|
+
Previous revision of the document.
|
|
41
|
+
*/
|
|
42
|
+
parentRevision?: string;
|
|
43
|
+
/**
|
|
44
|
+
ISO 8601 timestamp of when the document was written.
|
|
45
|
+
*/
|
|
46
|
+
writtenAt: string;
|
|
47
|
+
/**
|
|
48
|
+
Current value of state machine instance's state.
|
|
49
|
+
*/
|
|
50
|
+
state: TState;
|
|
51
|
+
}
|
|
52
|
+
type DriverMetadata = {
|
|
53
|
+
driver: 'flow-mf';
|
|
54
|
+
mfType: IMergeFieldType;
|
|
55
|
+
mfPath: IMergeField['path'];
|
|
56
|
+
writtenAt: StoredDocument['writtenAt'];
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/generic-driver/generic-driver.d.ts
|
|
60
|
+
declare class OrFlowStorageDocumentDriver<TReference, TConfig extends CONFIG> implements DocumentDriver<TReference> {
|
|
61
|
+
static driverName: string;
|
|
62
|
+
private readonly options;
|
|
63
|
+
private readonly cache;
|
|
64
|
+
readonly capabilities: DriverCapabilities;
|
|
65
|
+
constructor(options: FlowStorageDocumentDriverOptions<TReference, TConfig>);
|
|
66
|
+
/**
|
|
67
|
+
* Acquires a distributed lock on the merge field and executes the function.
|
|
68
|
+
* @param mergeField - The merge field to lock.
|
|
69
|
+
* @param signal - The abort signal.
|
|
70
|
+
* @param function_ - The function to execute with the lock.
|
|
71
|
+
* @returns The result of the function.
|
|
72
|
+
*/
|
|
73
|
+
private withDistributedLock;
|
|
74
|
+
/**
|
|
75
|
+
* Reads the stored document from the thread merge field.
|
|
76
|
+
* @param mergeField - The merge field to read.
|
|
77
|
+
* @param signal - The abort signal.
|
|
78
|
+
* @returns The stored document.
|
|
79
|
+
*/
|
|
80
|
+
private readStoredDocument;
|
|
81
|
+
load<TState = unknown>(reference: TReference, options?: DriverLoadOptions): Promise<DriverLoadResult<TState> | undefined>;
|
|
82
|
+
/**
|
|
83
|
+
* Saves the state machine instance to the thread merge field.
|
|
84
|
+
* @param reference - The reference to the state machine instance.
|
|
85
|
+
* @param value - The state to save.
|
|
86
|
+
* @param options - The options for the save operation.
|
|
87
|
+
* @returns The result of the save operation.
|
|
88
|
+
*/
|
|
89
|
+
save<TState = unknown>(reference: TReference, value: TState, options?: DriverSaveOptions): Promise<DriverSaveResult>;
|
|
90
|
+
delete(reference: TReference, options?: DriverDeleteOptions): Promise<boolean>;
|
|
91
|
+
protected getMetadata(mergeField: IMergeField, writtenAt: string): DriverMetadata;
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/generic-driver/errors.d.ts
|
|
95
|
+
declare class InvalidStoredDocumentError extends Error {
|
|
96
|
+
readonly mergeField: IMergeField;
|
|
97
|
+
constructor(mergeField: IMergeField);
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/instance-driver/types.d.ts
|
|
101
|
+
interface OrStorageStateInstanceDriverOptions {
|
|
102
|
+
/**
|
|
103
|
+
* The cache to use for the driver to implement the distributed lock.
|
|
104
|
+
*/
|
|
105
|
+
cache?: ICacheFacade;
|
|
106
|
+
/**
|
|
107
|
+
* The TTL for the state machine instance object.
|
|
108
|
+
*
|
|
109
|
+
* @default 604_800_000 // (1 week in milliseconds)
|
|
110
|
+
*/
|
|
111
|
+
ttl?: number;
|
|
112
|
+
/**
|
|
113
|
+
Options for acquiring a lock on the document.
|
|
114
|
+
*/
|
|
115
|
+
lock?: LockAcquireOptions;
|
|
116
|
+
mergeField?(this: void, reference: StateTemplateReference): IMergeField;
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/instance-driver/instance-driver.d.ts
|
|
120
|
+
declare class OrFlowStorageStateInstanceDriver<TConfig extends CONFIG> extends OrFlowStorageDocumentDriver<StateMachineInstanceReference, TConfig> implements StateMachineInstanceDriver {
|
|
121
|
+
constructor(process: IProcess<TConfig>, options?: OrStorageStateInstanceDriverOptions);
|
|
122
|
+
}
|
|
123
|
+
declare function getInstanceKey(reference: StateMachineInstanceReference): string;
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/template-driver/types.d.ts
|
|
126
|
+
interface OrStorageStateTemplateDriverOptions {
|
|
127
|
+
/**
|
|
128
|
+
* The cache to use for the driver to implement the distributed lock.
|
|
129
|
+
*/
|
|
130
|
+
cache?: ICacheFacade;
|
|
131
|
+
/**
|
|
132
|
+
Options for acquiring a lock on the document.
|
|
133
|
+
*/
|
|
134
|
+
lock?: LockAcquireOptions;
|
|
135
|
+
mergeField?(this: void, reference: StateTemplateReference): IMergeField;
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/template-driver/template-driver.d.ts
|
|
139
|
+
declare class OrFlowStorageStateTemplateDriver<TConfig extends CONFIG> extends OrFlowStorageDocumentDriver<StateTemplateReference, TConfig> implements StateTemplateDriver {
|
|
140
|
+
constructor(process: IProcess<TConfig>, options?: OrStorageStateTemplateDriverOptions);
|
|
141
|
+
}
|
|
142
|
+
declare function getTemplateKey(reference: StateTemplateReference): string;
|
|
143
|
+
//#endregion
|
|
144
|
+
export { type DriverMetadata, type FlowStorageDocumentDriverOptions, InvalidStoredDocumentError, type LockAcquireOptions, OrFlowStorageDocumentDriver, OrFlowStorageStateInstanceDriver, OrFlowStorageStateTemplateDriver, type OrStorageStateInstanceDriverOptions, type OrStorageStateTemplateDriverOptions, type StoredDocument, getInstanceKey, getTemplateKey };
|
|
145
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/generic-driver/types.ts","../src/generic-driver/generic-driver.ts","../src/generic-driver/errors.ts","../src/instance-driver/types.ts","../src/instance-driver/instance-driver.ts","../src/template-driver/types.ts","../src/template-driver/template-driver.ts"],"mappings":";;;UASiB,iCAAiC,YAAY,gBAAgB;EAC5E,SAAS,SAAS;EAClB,QAAQ;EACR,WAAW,YAAY,WAAW,aAAa;;;;EAK/C,OAAO;;KAGG;;;;;EAKV,cAAc;;;;;EAMd,cAAc;;;;;EAMd,eAAe;;UAGA,eAAe;;;;EAI9B;;;;EAKA;;;;EAKA;;;;EAKA;;;;EAKA,OAAO;;KAGG;EACV;EACA,QAAQ;EACR,QAAQ;EACR,WAAW;;;;cCtDA,4BAA4B,YAAY,gBAAgB,mBAAmB,eAAe;SACvF;mBAEG;mBACA;WAED,cAAc;EASlB,YAAA,SAAS,iCAAiC,YAAY;;;;;;;;UAYpD;;;;;;;UA4BA;EAaD,KAAK,kBAChB,WAAW,YACX,UAAU,oBACT,QAAQ,iBAAiB;;;;;;;;EAuBf,KAAK,kBAChB,WAAW,YACX,OAAO,QACP,UAAU,oBACT,QAAQ;EA6BE,OAAO,WAAW,YAAY,UAAU,sBAAsB;YAuBjE,YAAY,YAAY,aAAa,oBAAoB;;;;cCrKxD,mCAAmC;WAC9B,YAAY;EAEhB,YAAA,YAAY;;;;UCAT;;;;EAIf,QAAQ;;;;;;EAOR;;;;EAKA,OAAO;EAEP,YAAY,YAAY,WAAW,yBAAyB;;;;cChBjD,iCAAiC,gBAAgB,gBACpD,4BAA4B,+BAA+B,oBACxD;EAEC,YAAA,SAAS,SAAS,UAAU,UAAU;;iBAwBpC,eAAe,WAAW;;;UC9BzB;;;;EAIf,QAAQ;;;;EAKR,OAAO;EAEP,YAAY,YAAY,WAAW,yBAAyB;;;;cCTjD,iCAAiC,gBAAgB,gBACpD,4BAA4B,wBAAwB,oBACjD;EAEC,YAAA,SAAS,SAAS,UAAU,UAAU;;iBAsBpC,eAAe,WAAW"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{Cache as e}from"@onereach/flow-sdk/services/cache.js";import{RevisionConflictError as t}from"@onereach/state-machine-store";import{randomUUID as n}from"node:crypto";var r=class extends Error{mergeField;constructor(e){super(`Invalid state-machine document: ${e.name} of type ${e.type}`),this.name=`InvalidStoredDocumentError`,this.mergeField=e}};function i(){return`or-mf:${n()}`}function a(e){if(e?.aborted)throw e.reason??Error(`Operation aborted`)}function o(e,n){if(n!==void 0){if(n===null){if(e!==void 0)throw new t(null,e);return}if(e!==n)throw new t(n,e)}}function s(e){if(typeof e!=`object`||!e)return!1;let t=e;return t.schemaVersion===1&&typeof t.revision==`string`&&typeof t.writtenAt==`string`&&typeof t.state==`object`&&t.state!==null}var c=class{static driverName=`flow-mf`;options;cache;capabilities={concurrency:`cooperative-lock`,watch:!1};constructor(t){this.options=t,this.cache=t.cache??e}async withDistributedLock(e,t,n){let r=await this.cache.lockAcquire(`${this.options.process.config.accountSlugId}:sm-mf:${e.name}`,{lockTimeout:3e4,waitTimeout:1e4,delayTimeout:200,...this.options.lock});try{return await n()}finally{await this.cache.lockRelease(r)}}async readStoredDocument(e,t){a(t);let n=await Promise.resolve(this.options.process.main.get(e));if(n){if(!s(n))throw new r(e);return n}}async load(e,t){a(t?.signal);let n=this.options.mergeField(e),r=await this.readStoredDocument(n,t?.signal);if(r)return{value:r.state,revision:r.revision,metadata:this.getMetadata(n,r.writtenAt)}}async save(e,t,n){a(n?.signal);let r=this.options.mergeField(e);return this.withDistributedLock(r,n?.signal,async()=>{let e=await this.readStoredDocument(r,n?.signal);o(e?.revision,n?.expectedRevision);let a=i(),s=new Date,c={schemaVersion:1,revision:a,parentRevision:e?.revision,writtenAt:s.toISOString(),state:t};return await Promise.resolve(this.options.process.main.set(r,c)),{revision:a,metadata:this.getMetadata(r,c.writtenAt)}})}async delete(e,n){a(n?.signal);let r=this.options.mergeField(e);return this.withDistributedLock(r,n?.signal,async()=>{let e=await this.readStoredDocument(r,n?.signal);if(!e){if(typeof n?.expectedRevision==`string`)throw new t(n.expectedRevision,void 0);return!1}return o(e.revision,n?.expectedRevision),await Promise.resolve(this.options.process.main.unset(r)),!0})}getMetadata(e,t){return{driver:`flow-mf`,mfType:e.type??`session`,mfPath:e.path,writtenAt:t}}},l=class extends c{constructor(e,t){super({process:e,cache:t?.cache,lock:t?.lock,mergeField:t?.mergeField??u(e,t?.ttl)})}};function u(e,t){return n=>e.main.getMergeField({name:d(n),botId:e.config.accountId,type:`global`,ttl:t??6048e5})}function d(e){return[`sm_mf_ins`,e.id].join(`_`)}var f=class extends c{constructor(e,t){super({process:e,cache:t?.cache,lock:t?.lock,mergeField:t?.mergeField??p(e)})}};function p(e){return t=>e.main.getMergeField({name:m(t),botId:e.config.accountId,type:`global`})}function m(e){return[`sm_mf_tpl`,e.id,e.version??`latest`].join(`_`)}export{r as InvalidStoredDocumentError,c as OrFlowStorageDocumentDriver,l as OrFlowStorageStateInstanceDriver,f as OrFlowStorageStateTemplateDriver,d as getInstanceKey,m as getTemplateKey};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/generic-driver/errors.ts","../src/generic-driver/helpers.ts","../src/generic-driver/generic-driver.ts","../src/instance-driver/instance-driver.ts","../src/template-driver/template-driver.ts"],"sourcesContent":["import type { IMergeField } from '@onereach/flow-sdk/types/index.js';\n\nexport class InvalidStoredDocumentError extends Error {\n public readonly mergeField: IMergeField;\n\n constructor(mergeField: IMergeField) {\n super(`Invalid state-machine document: ${mergeField.name} of type ${mergeField.type}`);\n\n this.name = 'InvalidStoredDocumentError';\n this.mergeField = mergeField;\n }\n}\n","import { RevisionConflictError } from '@onereach/state-machine-store';\nimport { randomUUID } from 'node:crypto';\n\nimport type { StoredDocument } from './types.ts';\n\nexport function createRevision(): string {\n return `or-mf:${randomUUID()}`;\n}\n\nexport function throwIfAborted(signal?: AbortSignal): void {\n if (!signal?.aborted) {\n return;\n }\n\n throw signal.reason ?? new Error('Operation aborted');\n}\n\n// TODO: move to @onereach/state-machine-store\nexport function assertExpectedRevision(actual: string | undefined, expected: string | null | undefined): void {\n /*\n * Unconditional operation.\n */\n if (expected === undefined) {\n return;\n }\n\n /*\n * Create-only.\n */\n if (expected === null) {\n if (actual !== undefined) {\n // eslint-disable-next-line unicorn/no-null\n throw new RevisionConflictError(null, actual);\n }\n\n return;\n }\n\n if (actual !== expected) {\n throw new RevisionConflictError(expected, actual);\n }\n}\n\n/*\n--------------------------------------------------------------------------\n*/\n/*\nStored document validation\n*/\n/*\n--------------------------------------------------------------------------\n*/\n\nexport function isStoredDocument(value: unknown): value is StoredDocument {\n if (typeof value !== 'object' || value === null) {\n return false;\n }\n\n const record = value as Record<string, unknown>;\n\n return (\n record.schemaVersion === 1 &&\n typeof record.revision === 'string' &&\n typeof record.writtenAt === 'string' &&\n typeof record.state === 'object' &&\n record.state !== null\n );\n}\n","import { Cache } from '@onereach/flow-sdk/services/cache.js';\nimport type { CONFIG, IMergeField, ICacheFacade } from '@onereach/flow-sdk/types/index.js';\nimport { RevisionConflictError } from '@onereach/state-machine-store';\nimport type {\n DocumentDriver,\n DriverCapabilities,\n DriverDeleteOptions,\n DriverLoadOptions,\n DriverLoadResult,\n DriverSaveOptions,\n DriverSaveResult,\n} from '@onereach/state-machine-store';\n\nimport { InvalidStoredDocumentError } from './errors.ts';\nimport { assertExpectedRevision, createRevision, isStoredDocument, throwIfAborted } from './helpers.ts';\nimport type { DriverMetadata, FlowStorageDocumentDriverOptions, StoredDocument } from './types.ts';\n\nexport class OrFlowStorageDocumentDriver<TReference, TConfig extends CONFIG> implements DocumentDriver<TReference> {\n public static driverName = 'flow-mf';\n\n private readonly options: FlowStorageDocumentDriverOptions<TReference, TConfig>;\n private readonly cache: ICacheFacade;\n\n public readonly capabilities: DriverCapabilities = {\n /**\n * Compare-and-swap is implemented cooperatively with\n * a distributed cache lock.\n */\n concurrency: 'cooperative-lock',\n watch: false,\n };\n\n constructor(options: FlowStorageDocumentDriverOptions<TReference, TConfig>) {\n this.options = options;\n this.cache = options.cache ?? Cache;\n }\n\n /**\n * Acquires a distributed lock on the merge field and executes the function.\n * @param mergeField - The merge field to lock.\n * @param signal - The abort signal.\n * @param function_ - The function to execute with the lock.\n * @returns The result of the function.\n */\n private async withDistributedLock<T>(\n mergeField: IMergeField,\n _signal: AbortSignal | undefined,\n function_: () => Promise<T>,\n ): Promise<T> {\n // TODO: implement on abort from signal\n\n // TODO: move to class options\n const lock = await this.cache.lockAcquire(`${this.options.process.config.accountSlugId}:sm-mf:${mergeField.name}`, {\n lockTimeout: 30_000, // how long the acquired lock remains valid, after which it will be released automatically\n waitTimeout: 10_000, // how long to wait for the lock to be released\n delayTimeout: 200, // how long to delay between attempts to acquire the lock\n ...this.options.lock,\n });\n\n try {\n return await function_();\n } finally {\n await this.cache.lockRelease(lock);\n }\n }\n\n /**\n * Reads the stored document from the thread merge field.\n * @param mergeField - The merge field to read.\n * @param signal - The abort signal.\n * @returns The stored document.\n */\n private async readStoredDocument<TState = unknown>(\n mergeField: IMergeField,\n signal?: AbortSignal,\n ): Promise<StoredDocument<TState> | undefined> {\n throwIfAborted(signal);\n\n const stored = await Promise.resolve(this.options.process.main.get<StoredDocument<TState> | undefined>(mergeField));\n if (!stored) return undefined;\n if (!isStoredDocument(stored)) throw new InvalidStoredDocumentError(mergeField);\n\n return stored;\n }\n\n public async load<TState = unknown>(\n reference: TReference,\n options?: DriverLoadOptions,\n ): Promise<DriverLoadResult<TState> | undefined> {\n throwIfAborted(options?.signal);\n\n const mergeField = this.options.mergeField(reference);\n const stored = await this.readStoredDocument<TState>(mergeField, options?.signal);\n\n if (!stored) return undefined;\n\n // TODO: extract metadata creation into a separate helper function\n return {\n value: stored.state,\n revision: stored.revision,\n metadata: this.getMetadata(mergeField, stored.writtenAt),\n };\n }\n\n /**\n * Saves the state machine instance to the thread merge field.\n * @param reference - The reference to the state machine instance.\n * @param value - The state to save.\n * @param options - The options for the save operation.\n * @returns The result of the save operation.\n */\n public async save<TState = unknown>(\n reference: TReference,\n value: TState,\n options?: DriverSaveOptions,\n ): Promise<DriverSaveResult> {\n throwIfAborted(options?.signal);\n\n const mergeField = this.options.mergeField(reference);\n\n return this.withDistributedLock(mergeField, options?.signal, async () => {\n const current = await this.readStoredDocument<TState>(mergeField, options?.signal);\n\n assertExpectedRevision(current?.revision, options?.expectedRevision);\n\n const revision = createRevision();\n const now = new Date();\n const document: StoredDocument = {\n schemaVersion: 1,\n revision,\n parentRevision: current?.revision,\n writtenAt: now.toISOString(),\n state: value,\n };\n\n await Promise.resolve(this.options.process.main.set(mergeField, document));\n\n return {\n revision,\n metadata: this.getMetadata(mergeField, document.writtenAt),\n };\n });\n }\n\n public async delete(reference: TReference, options?: DriverDeleteOptions): Promise<boolean> {\n throwIfAborted(options?.signal);\n\n const mergeField = this.options.mergeField(reference);\n\n return this.withDistributedLock(mergeField, options?.signal, async () => {\n const current = await this.readStoredDocument(mergeField, options?.signal);\n\n if (!current) {\n if (typeof options?.expectedRevision === 'string') {\n throw new RevisionConflictError(options.expectedRevision, undefined);\n }\n return false;\n }\n\n assertExpectedRevision(current.revision, options?.expectedRevision);\n\n await Promise.resolve(this.options.process.main.unset(mergeField));\n\n return true;\n });\n }\n\n protected getMetadata(mergeField: IMergeField, writtenAt: string): DriverMetadata {\n return {\n driver: 'flow-mf',\n mfType: mergeField.type ?? 'session',\n mfPath: mergeField.path,\n writtenAt,\n };\n }\n\n // public async publish(reference: TReference, options?: DriverPublishChangeOptions): Promise<void> {\n // await this.options.thread.emitAsync({\n // name: `sm-mf/${reference.id}`, // TODO: move to class options\n // params: {},\n // });\n // }\n}\n","import type { CONFIG, IMergeField, IProcess } from '@onereach/flow-sdk/types/index.js';\nimport type { StateMachineInstanceReference, StateMachineInstanceDriver } from '@onereach/state-machine-store';\n\nimport { OrFlowStorageDocumentDriver } from '../generic-driver/generic-driver.ts';\n\nimport { OrStorageStateInstanceDriverOptions } from './types.ts';\n\nexport class OrFlowStorageStateInstanceDriver<TConfig extends CONFIG>\n extends OrFlowStorageDocumentDriver<StateMachineInstanceReference, TConfig>\n implements StateMachineInstanceDriver\n{\n constructor(process: IProcess<TConfig>, options?: OrStorageStateInstanceDriverOptions) {\n super({\n process,\n cache: options?.cache,\n lock: options?.lock,\n mergeField: options?.mergeField ?? defaultInstanceKey(process, options?.ttl),\n });\n }\n}\n\nfunction defaultInstanceKey<TConfig extends CONFIG>(\n process: IProcess<TConfig>,\n ttl?: number,\n): (reference: StateMachineInstanceReference) => IMergeField {\n return (reference: StateMachineInstanceReference) => {\n return process.main.getMergeField({\n name: getInstanceKey(reference),\n botId: process.config.accountId, // make it global for account, not scoped by bot\n type: 'global',\n ttl: ttl ?? 604_800_000, // 1 week in milliseconds // TODO: how to respect merge-field TTL is it was already set?\n });\n };\n}\n\nexport function getInstanceKey(reference: StateMachineInstanceReference): string {\n return ['sm_mf_ins', reference.id].join('_');\n}\n","import type { CONFIG, IMergeField, IProcess } from '@onereach/flow-sdk/types/index.js';\nimport type { StateTemplateDriver, StateTemplateReference } from '@onereach/state-machine-store';\n\nimport { OrFlowStorageDocumentDriver } from '../generic-driver/generic-driver.ts';\n\nimport { OrStorageStateTemplateDriverOptions } from './types.ts';\n\nexport class OrFlowStorageStateTemplateDriver<TConfig extends CONFIG>\n extends OrFlowStorageDocumentDriver<StateTemplateReference, TConfig>\n implements StateTemplateDriver\n{\n constructor(process: IProcess<TConfig>, options?: OrStorageStateTemplateDriverOptions) {\n super({\n process,\n cache: options?.cache,\n lock: options?.lock,\n mergeField: options?.mergeField ?? defaultTemplateKey(process),\n });\n }\n}\n\nfunction defaultTemplateKey<TConfig extends CONFIG>(\n process: IProcess<TConfig>,\n): (reference: StateTemplateReference) => IMergeField {\n return (reference: StateTemplateReference) => {\n return process.main.getMergeField({\n name: getTemplateKey(reference),\n botId: process.config.accountId, // make it global for account, not scoped by bot\n type: 'global',\n });\n };\n}\n\nexport function getTemplateKey(reference: StateTemplateReference): string {\n return ['sm_mf_tpl', reference.id, reference.version ?? 'latest'].join('_');\n}\n"],"mappings":"4KAEA,IAAa,EAAb,cAAgD,KAAM,CACpD,WAEA,YAAY,EAAyB,CACnC,MAAM,mCAAmC,EAAW,KAAK,WAAW,EAAW,MAAM,EAErF,KAAK,KAAO,6BACZ,KAAK,WAAa,CACpB,CACF,ECNA,SAAgB,GAAyB,CACvC,MAAO,SAAS,EAAW,GAC7B,CAEA,SAAgB,EAAe,EAA4B,CACpD,MAAQ,QAIb,MAAM,EAAO,QAAc,MAAM,mBAAmB,CACtD,CAGA,SAAgB,EAAuB,EAA4B,EAA2C,CAIxG,OAAa,IAAA,GAOjB,IAAI,IAAa,KAAM,CACrB,GAAI,IAAW,IAAA,GAEb,MAAM,IAAI,EAAsB,KAAM,CAAM,EAG9C,MACF,CAEA,GAAI,IAAW,EACb,MAAM,IAAI,EAAsB,EAAU,CAAM,CAHlD,CAKF,CAYA,SAAgB,EAAiB,EAAyC,CACxE,GAAI,OAAO,GAAU,WAAY,EAC/B,MAAO,GAGT,IAAM,EAAS,EAEf,OACE,EAAO,gBAAkB,GACzB,OAAO,EAAO,UAAa,UAC3B,OAAO,EAAO,WAAc,UAC5B,OAAO,EAAO,OAAU,UACxB,EAAO,QAAU,IAErB,CClDA,IAAa,EAAb,KAAmH,CACjH,OAAc,WAAa,UAE3B,QACA,MAEA,aAAmD,CAKjD,YAAa,mBACb,MAAO,EACT,EAEA,YAAY,EAAgE,CAC1E,KAAK,QAAU,EACf,KAAK,MAAQ,EAAQ,OAAS,CAChC,CASA,MAAc,oBACZ,EACA,EACA,EACY,CAIZ,IAAM,EAAO,MAAM,KAAK,MAAM,YAAY,GAAG,KAAK,QAAQ,QAAQ,OAAO,cAAc,SAAS,EAAW,OAAQ,CACjH,YAAa,IACb,YAAa,IACb,aAAc,IACd,GAAG,KAAK,QAAQ,IAClB,CAAC,EAED,GAAI,CACF,OAAO,MAAM,EAAU,CACzB,QAAU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YAAa,EAAU,GAAI,EAAU,SAAW,QAAQ,CAAC,CAAC,KAAK,GAAG,CAC5E"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onereach/state-machine-driver-or-flow-mf",
|
|
3
|
+
"version": "1.0.0-framework.2",
|
|
4
|
+
"description": "State Machine driver for OneReach flows using merge fields",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"onereach",
|
|
7
|
+
"toolkit",
|
|
8
|
+
"state-machine"
|
|
9
|
+
],
|
|
10
|
+
"author": "OneReach Development Team <dev.team@onereach.ai>",
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"!dist/metafile*.json"
|
|
19
|
+
],
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@arethetypeswrong/core": "^0.18.5",
|
|
22
|
+
"@aws-sdk/client-kms": "^3.1068.0",
|
|
23
|
+
"@onereach/flow-sdk": "v9.0.0-monosplit.5",
|
|
24
|
+
"@onereach/flow-sdk-mocks": "v9.0.0-monosplit.5",
|
|
25
|
+
"@onereach/orb-em-core": "^8.0.27",
|
|
26
|
+
"@onereach/orb-em-plugin-emitter": "^8.0.27",
|
|
27
|
+
"@onereach/orb-test-helpers": "^8.0.27",
|
|
28
|
+
"@types/source-map-support": "^0.5.10",
|
|
29
|
+
"publint": "^0.3.24",
|
|
30
|
+
"rollup-plugin-visualizer": "^7.1.1",
|
|
31
|
+
"tsdown": "^0.22.14",
|
|
32
|
+
"typescript": "^6.0.3"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@onereach/state-machine-store": "1.0.0-framework.2"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@onereach/flow-sdk": "^8.0.0"
|
|
39
|
+
},
|
|
40
|
+
"exports": {
|
|
41
|
+
".": "./dist/index.mjs",
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsdown --config tsdown.config.mts",
|
|
46
|
+
"build:visualize": "VISUALIZE=1 pnpm build",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"preversion_": "pnpm build && pnpm test",
|
|
49
|
+
"publish:rc": "pnpm version prerelease --preid rc && pnpm publish --tag rc",
|
|
50
|
+
"test": "pnpm run unit",
|
|
51
|
+
"unit": "vitest run",
|
|
52
|
+
"preunit": "pnpm build && pnpm typecheck",
|
|
53
|
+
"preversion": "pnpm build"
|
|
54
|
+
}
|
|
55
|
+
}
|