@temporal-contract/worker 0.0.4 → 0.0.6
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 +72 -0
- package/dist/activity.cjs +108 -32
- package/dist/activity.d.cts +98 -3
- package/dist/activity.d.mts +98 -3
- package/dist/activity.mjs +104 -3
- package/dist/errors-BqVTpfcf.mjs +155 -0
- package/dist/errors-BqYWpdvd.d.cts +137 -0
- package/dist/errors-C1RFkCuD.d.mts +137 -0
- package/dist/errors-DjSZg-93.cjs +227 -0
- package/dist/worker.cjs +65 -0
- package/dist/worker.d.cts +68 -0
- package/dist/worker.d.mts +68 -0
- package/dist/worker.mjs +64 -0
- package/dist/workflow.cjs +255 -10
- package/dist/workflow.d.cts +299 -2
- package/dist/workflow.d.mts +299 -2
- package/dist/workflow.mjs +244 -2
- package/package.json +39 -23
- package/dist/handler-B7B5QHez.mjs +0 -398
- package/dist/handler-Czi-kgwZ.d.cts +0 -316
- package/dist/handler-D9BllGor.cjs +0 -481
- package/dist/handler-DThqdaaS.d.mts +0 -316
package/dist/worker.cjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
let _temporalio_worker = require("@temporalio/worker");
|
|
2
|
+
let node_url = require("node:url");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
|
|
5
|
+
//#region src/worker.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a typed Temporal worker with contract-based configuration
|
|
8
|
+
*
|
|
9
|
+
* This helper simplifies worker creation by:
|
|
10
|
+
* - Using the contract's task queue automatically
|
|
11
|
+
* - Providing type-safe configuration
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { NativeConnection } from '@temporalio/worker';
|
|
16
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
17
|
+
* import { activities } from './activities';
|
|
18
|
+
* import myContract from './contract';
|
|
19
|
+
*
|
|
20
|
+
* const connection = await NativeConnection.connect({
|
|
21
|
+
* address: 'localhost:7233',
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const worker = await createWorker({
|
|
25
|
+
* contract: myContract,
|
|
26
|
+
* connection,
|
|
27
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
28
|
+
* activities,
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* await worker.run();
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
async function createWorker(options) {
|
|
35
|
+
const { contract, activities, ...workerOptions } = options;
|
|
36
|
+
return await _temporalio_worker.Worker.create({
|
|
37
|
+
...workerOptions,
|
|
38
|
+
activities,
|
|
39
|
+
taskQueue: contract.taskQueue
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Helper to create a workflowsPath from a file URL
|
|
44
|
+
*
|
|
45
|
+
* Useful for creating the workflowsPath option when using ES modules
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { workflowsPathFromURL } from '@temporal-contract/worker/worker';
|
|
50
|
+
*
|
|
51
|
+
* const worker = await createWorker({
|
|
52
|
+
* contract: myContract,
|
|
53
|
+
* connection,
|
|
54
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows'),
|
|
55
|
+
* activities,
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function workflowsPathFromURL(baseURL, relativePath) {
|
|
60
|
+
return (0, node_url.fileURLToPath)(new URL(`${relativePath}${(0, node_path.extname)(baseURL)}`, baseURL));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
exports.createWorker = createWorker;
|
|
65
|
+
exports.workflowsPathFromURL = workflowsPathFromURL;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ActivitiesHandler } from "./activity.cjs";
|
|
2
|
+
import { ContractDefinition } from "@temporal-contract/contract";
|
|
3
|
+
import { Worker, WorkerOptions } from "@temporalio/worker";
|
|
4
|
+
|
|
5
|
+
//#region src/worker.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a Temporal worker
|
|
9
|
+
*/
|
|
10
|
+
interface CreateWorkerOptions<TContract extends ContractDefinition> extends Omit<WorkerOptions, "activities" | "taskQueue"> {
|
|
11
|
+
/**
|
|
12
|
+
* The contract definition for this worker
|
|
13
|
+
*/
|
|
14
|
+
contract: TContract;
|
|
15
|
+
/**
|
|
16
|
+
* Activities handler for this worker
|
|
17
|
+
*/
|
|
18
|
+
activities: ActivitiesHandler<TContract>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a typed Temporal worker with contract-based configuration
|
|
22
|
+
*
|
|
23
|
+
* This helper simplifies worker creation by:
|
|
24
|
+
* - Using the contract's task queue automatically
|
|
25
|
+
* - Providing type-safe configuration
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { NativeConnection } from '@temporalio/worker';
|
|
30
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
31
|
+
* import { activities } from './activities';
|
|
32
|
+
* import myContract from './contract';
|
|
33
|
+
*
|
|
34
|
+
* const connection = await NativeConnection.connect({
|
|
35
|
+
* address: 'localhost:7233',
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* const worker = await createWorker({
|
|
39
|
+
* contract: myContract,
|
|
40
|
+
* connection,
|
|
41
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
42
|
+
* activities,
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* await worker.run();
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function createWorker<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<Worker>;
|
|
49
|
+
/**
|
|
50
|
+
* Helper to create a workflowsPath from a file URL
|
|
51
|
+
*
|
|
52
|
+
* Useful for creating the workflowsPath option when using ES modules
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { workflowsPathFromURL } from '@temporal-contract/worker/worker';
|
|
57
|
+
*
|
|
58
|
+
* const worker = await createWorker({
|
|
59
|
+
* contract: myContract,
|
|
60
|
+
* connection,
|
|
61
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows'),
|
|
62
|
+
* activities,
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare function workflowsPathFromURL(baseURL: string, relativePath: string): string;
|
|
67
|
+
//#endregion
|
|
68
|
+
export { CreateWorkerOptions, createWorker, workflowsPathFromURL };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ActivitiesHandler } from "./activity.mjs";
|
|
2
|
+
import { Worker, WorkerOptions } from "@temporalio/worker";
|
|
3
|
+
import { ContractDefinition } from "@temporal-contract/contract";
|
|
4
|
+
|
|
5
|
+
//#region src/worker.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Options for creating a Temporal worker
|
|
9
|
+
*/
|
|
10
|
+
interface CreateWorkerOptions<TContract extends ContractDefinition> extends Omit<WorkerOptions, "activities" | "taskQueue"> {
|
|
11
|
+
/**
|
|
12
|
+
* The contract definition for this worker
|
|
13
|
+
*/
|
|
14
|
+
contract: TContract;
|
|
15
|
+
/**
|
|
16
|
+
* Activities handler for this worker
|
|
17
|
+
*/
|
|
18
|
+
activities: ActivitiesHandler<TContract>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create a typed Temporal worker with contract-based configuration
|
|
22
|
+
*
|
|
23
|
+
* This helper simplifies worker creation by:
|
|
24
|
+
* - Using the contract's task queue automatically
|
|
25
|
+
* - Providing type-safe configuration
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { NativeConnection } from '@temporalio/worker';
|
|
30
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
31
|
+
* import { activities } from './activities';
|
|
32
|
+
* import myContract from './contract';
|
|
33
|
+
*
|
|
34
|
+
* const connection = await NativeConnection.connect({
|
|
35
|
+
* address: 'localhost:7233',
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* const worker = await createWorker({
|
|
39
|
+
* contract: myContract,
|
|
40
|
+
* connection,
|
|
41
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
42
|
+
* activities,
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* await worker.run();
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
declare function createWorker<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<Worker>;
|
|
49
|
+
/**
|
|
50
|
+
* Helper to create a workflowsPath from a file URL
|
|
51
|
+
*
|
|
52
|
+
* Useful for creating the workflowsPath option when using ES modules
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { workflowsPathFromURL } from '@temporal-contract/worker/worker';
|
|
57
|
+
*
|
|
58
|
+
* const worker = await createWorker({
|
|
59
|
+
* contract: myContract,
|
|
60
|
+
* connection,
|
|
61
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows'),
|
|
62
|
+
* activities,
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare function workflowsPathFromURL(baseURL: string, relativePath: string): string;
|
|
67
|
+
//#endregion
|
|
68
|
+
export { CreateWorkerOptions, createWorker, workflowsPathFromURL };
|
package/dist/worker.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Worker } from "@temporalio/worker";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { extname } from "node:path";
|
|
4
|
+
|
|
5
|
+
//#region src/worker.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a typed Temporal worker with contract-based configuration
|
|
8
|
+
*
|
|
9
|
+
* This helper simplifies worker creation by:
|
|
10
|
+
* - Using the contract's task queue automatically
|
|
11
|
+
* - Providing type-safe configuration
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { NativeConnection } from '@temporalio/worker';
|
|
16
|
+
* import { createWorker } from '@temporal-contract/worker/worker';
|
|
17
|
+
* import { activities } from './activities';
|
|
18
|
+
* import myContract from './contract';
|
|
19
|
+
*
|
|
20
|
+
* const connection = await NativeConnection.connect({
|
|
21
|
+
* address: 'localhost:7233',
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const worker = await createWorker({
|
|
25
|
+
* contract: myContract,
|
|
26
|
+
* connection,
|
|
27
|
+
* workflowsPath: require.resolve('./workflows'),
|
|
28
|
+
* activities,
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* await worker.run();
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
async function createWorker(options) {
|
|
35
|
+
const { contract, activities, ...workerOptions } = options;
|
|
36
|
+
return await Worker.create({
|
|
37
|
+
...workerOptions,
|
|
38
|
+
activities,
|
|
39
|
+
taskQueue: contract.taskQueue
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Helper to create a workflowsPath from a file URL
|
|
44
|
+
*
|
|
45
|
+
* Useful for creating the workflowsPath option when using ES modules
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { workflowsPathFromURL } from '@temporal-contract/worker/worker';
|
|
50
|
+
*
|
|
51
|
+
* const worker = await createWorker({
|
|
52
|
+
* contract: myContract,
|
|
53
|
+
* connection,
|
|
54
|
+
* workflowsPath: workflowsPathFromURL(import.meta.url, './workflows'),
|
|
55
|
+
* activities,
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
function workflowsPathFromURL(baseURL, relativePath) {
|
|
60
|
+
return fileURLToPath(new URL(`${relativePath}${extname(baseURL)}`, baseURL));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createWorker, workflowsPathFromURL };
|
package/dist/workflow.cjs
CHANGED
|
@@ -1,11 +1,256 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_errors = require('./errors-DjSZg-93.cjs');
|
|
2
|
+
let _temporal_contract_boxed = require("@temporal-contract/boxed");
|
|
3
|
+
let _temporalio_workflow = require("@temporalio/workflow");
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
//#region src/workflow.ts
|
|
6
|
+
/**
|
|
7
|
+
* Create a typed workflow implementation with automatic validation
|
|
8
|
+
*
|
|
9
|
+
* This wraps a workflow implementation with:
|
|
10
|
+
* - Input/output validation
|
|
11
|
+
* - Typed workflow context with activities
|
|
12
|
+
* - Workflow info access
|
|
13
|
+
*
|
|
14
|
+
* Workflows must be defined in separate files and imported by the Temporal Worker
|
|
15
|
+
* via workflowsPath.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // workflows/processOrder.ts
|
|
20
|
+
* import { declareWorkflow } from '@temporal-contract/worker';
|
|
21
|
+
* import myContract from '../contract';
|
|
22
|
+
*
|
|
23
|
+
* export const processOrder = declareWorkflow({
|
|
24
|
+
* workflowName: 'processOrder',
|
|
25
|
+
* contract: myContract,
|
|
26
|
+
* implementation: async (context, orderId, customerId) => {
|
|
27
|
+
* // context.activities: typed activities (workflow + global)
|
|
28
|
+
* // context.info: WorkflowInfo
|
|
29
|
+
*
|
|
30
|
+
* const inventory = await context.activities.validateInventory(orderId);
|
|
31
|
+
*
|
|
32
|
+
* if (!inventory.available) {
|
|
33
|
+
* throw new Error('Out of stock');
|
|
34
|
+
* }
|
|
35
|
+
*
|
|
36
|
+
* const payment = await context.activities.chargePayment(customerId, 100);
|
|
37
|
+
*
|
|
38
|
+
* // Global activity
|
|
39
|
+
* await context.activities.sendEmail(
|
|
40
|
+
* customerId,
|
|
41
|
+
* 'Order processed',
|
|
42
|
+
* 'Your order has been processed'
|
|
43
|
+
* );
|
|
44
|
+
*
|
|
45
|
+
* return {
|
|
46
|
+
* orderId,
|
|
47
|
+
* status: payment.success ? 'success' : 'failed',
|
|
48
|
+
* transactionId: payment.transactionId,
|
|
49
|
+
* };
|
|
50
|
+
* },
|
|
51
|
+
* activityOptions: {
|
|
52
|
+
* startToCloseTimeout: '1 minute',
|
|
53
|
+
* },
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* Then in your worker setup:
|
|
58
|
+
* ```ts
|
|
59
|
+
* // worker.ts
|
|
60
|
+
* import { Worker } from '@temporalio/worker';
|
|
61
|
+
* import { activitiesHandler } from './activities';
|
|
62
|
+
*
|
|
63
|
+
* const worker = await Worker.create({
|
|
64
|
+
* workflowsPath: require.resolve('./workflows'), // Imports processOrder
|
|
65
|
+
* activities: activitiesHandler.activities,
|
|
66
|
+
* taskQueue: activitiesHandler.contract.taskQueue,
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function declareWorkflow({ workflowName, contract, implementation, activityOptions }) {
|
|
71
|
+
const definition = contract.workflows[workflowName];
|
|
72
|
+
return async (...args) => {
|
|
73
|
+
const input = args.length === 1 ? args[0] : args;
|
|
74
|
+
const inputResult = await definition.input["~standard"].validate(input);
|
|
75
|
+
if (inputResult.issues) throw new require_errors.WorkflowInputValidationError(String(workflowName), inputResult.issues);
|
|
76
|
+
const validatedInput = inputResult.value;
|
|
77
|
+
let contextActivities = {};
|
|
78
|
+
if (definition.activities || contract.activities) contextActivities = createValidatedActivities((0, _temporalio_workflow.proxyActivities)(activityOptions), definition.activities, contract.activities);
|
|
79
|
+
async function validateChildWorkflowOutput(childDefinition, result$1, childWorkflowName) {
|
|
80
|
+
const outputResult$1 = await childDefinition.output["~standard"].validate(result$1);
|
|
81
|
+
if (outputResult$1.issues) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow "${childWorkflowName}" output validation failed: ${outputResult$1.issues.map((i) => i.message).join("; ")}`));
|
|
82
|
+
return _temporal_contract_boxed.Result.Ok(outputResult$1.value);
|
|
83
|
+
}
|
|
84
|
+
async function getAndValidateChildWorkflow(childContract, childWorkflowName, args$1) {
|
|
85
|
+
const childDefinition = childContract.workflows[childWorkflowName];
|
|
86
|
+
if (!childDefinition) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowNotFoundError(String(childWorkflowName), Object.keys(childContract.workflows)));
|
|
87
|
+
const inputResult$1 = await childDefinition.input["~standard"].validate(args$1);
|
|
88
|
+
if (inputResult$1.issues) return _temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow "${String(childWorkflowName)}" input validation failed: ${inputResult$1.issues.map((i) => i.message).join("; ")}`));
|
|
89
|
+
const validatedInput$1 = inputResult$1.value;
|
|
90
|
+
return _temporal_contract_boxed.Result.Ok({
|
|
91
|
+
definition: childDefinition,
|
|
92
|
+
validatedInput: validatedInput$1,
|
|
93
|
+
taskQueue: childContract.taskQueue
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function createTypedChildHandle(handle, childDefinition, childWorkflowName) {
|
|
97
|
+
return {
|
|
98
|
+
workflowId: handle.workflowId,
|
|
99
|
+
result: () => {
|
|
100
|
+
return _temporal_contract_boxed.Future.make((resolve) => {
|
|
101
|
+
(async () => {
|
|
102
|
+
try {
|
|
103
|
+
resolve(await validateChildWorkflowOutput(childDefinition, await handle.result(), childWorkflowName));
|
|
104
|
+
} catch (error) {
|
|
105
|
+
resolve(_temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Child workflow execution failed: ${error instanceof Error ? error.message : String(error)}`, error)));
|
|
106
|
+
}
|
|
107
|
+
})();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function createStartChildWorkflow(childContract, childWorkflowName, options) {
|
|
113
|
+
return _temporal_contract_boxed.Future.make((resolve) => {
|
|
114
|
+
(async () => {
|
|
115
|
+
const validationResult = await getAndValidateChildWorkflow(childContract, childWorkflowName, options.args);
|
|
116
|
+
if (validationResult.isError()) {
|
|
117
|
+
resolve(_temporal_contract_boxed.Result.Error(validationResult.error));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const { definition: childDefinition, validatedInput: validatedInput$1, taskQueue } = validationResult.value;
|
|
121
|
+
try {
|
|
122
|
+
const { args: _args, ...temporalOptions } = options;
|
|
123
|
+
const typedHandle = createTypedChildHandle(await (0, _temporalio_workflow.startChild)(childWorkflowName, {
|
|
124
|
+
...temporalOptions,
|
|
125
|
+
taskQueue,
|
|
126
|
+
args: [validatedInput$1]
|
|
127
|
+
}), childDefinition, String(childWorkflowName));
|
|
128
|
+
resolve(_temporal_contract_boxed.Result.Ok(typedHandle));
|
|
129
|
+
} catch (error) {
|
|
130
|
+
resolve(_temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to start child workflow: ${error instanceof Error ? error.message : String(error)}`, error)));
|
|
131
|
+
}
|
|
132
|
+
})();
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function createExecuteChildWorkflow(childContract, childWorkflowName, options) {
|
|
136
|
+
return _temporal_contract_boxed.Future.make((resolve) => {
|
|
137
|
+
(async () => {
|
|
138
|
+
const validationResult = await getAndValidateChildWorkflow(childContract, childWorkflowName, options.args);
|
|
139
|
+
if (validationResult.isError()) {
|
|
140
|
+
resolve(_temporal_contract_boxed.Result.Error(validationResult.error));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const { definition: childDefinition, validatedInput: validatedInput$1, taskQueue } = validationResult.value;
|
|
144
|
+
try {
|
|
145
|
+
const { args: _args, ...temporalOptions } = options;
|
|
146
|
+
const outputValidationResult = await validateChildWorkflowOutput(childDefinition, await (0, _temporalio_workflow.executeChild)(childWorkflowName, {
|
|
147
|
+
...temporalOptions,
|
|
148
|
+
taskQueue,
|
|
149
|
+
args: [validatedInput$1]
|
|
150
|
+
}), String(childWorkflowName));
|
|
151
|
+
if (outputValidationResult.isError()) {
|
|
152
|
+
resolve(_temporal_contract_boxed.Result.Error(outputValidationResult.error));
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
resolve(_temporal_contract_boxed.Result.Ok(outputValidationResult.value));
|
|
156
|
+
} catch (error) {
|
|
157
|
+
resolve(_temporal_contract_boxed.Result.Error(new require_errors.ChildWorkflowError(`Failed to execute child workflow: ${error instanceof Error ? error.message : String(error)}`, error)));
|
|
158
|
+
}
|
|
159
|
+
})();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function createDefineSignal(signalName, handler) {
|
|
163
|
+
if (!definition.signals) throw new Error(`Signal "${String(signalName)}" cannot be defined: workflow "${String(workflowName)}" has no signals in its contract`);
|
|
164
|
+
const signalDef = definition.signals[signalName];
|
|
165
|
+
if (!signalDef) throw new Error(`Signal "${String(signalName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
166
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineSignal)(signalName), async (...args$1) => {
|
|
167
|
+
const input$1 = args$1.length === 1 ? args$1[0] : args$1;
|
|
168
|
+
const inputResult$1 = await signalDef.input["~standard"].validate(input$1);
|
|
169
|
+
if (inputResult$1.issues) throw new require_errors.SignalInputValidationError(signalName, inputResult$1.issues);
|
|
170
|
+
await handler(inputResult$1.value);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function createDefineQuery(queryName, handler) {
|
|
174
|
+
if (!definition.queries) throw new Error(`Query "${String(queryName)}" cannot be defined: workflow "${String(workflowName)}" has no queries in its contract`);
|
|
175
|
+
const queryDef = definition.queries[queryName];
|
|
176
|
+
if (!queryDef) throw new Error(`Query "${String(queryName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
177
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineQuery)(queryName), (...args$1) => {
|
|
178
|
+
const input$1 = args$1.length === 1 ? args$1[0] : args$1;
|
|
179
|
+
const inputResult$1 = queryDef.input["~standard"].validate(input$1);
|
|
180
|
+
if (inputResult$1 instanceof Promise) throw new Error(`Query "${String(queryName)}" validation must be synchronous. Use a schema library that supports synchronous validation for queries.`);
|
|
181
|
+
if (inputResult$1.issues) throw new require_errors.QueryInputValidationError(queryName, inputResult$1.issues);
|
|
182
|
+
const result$1 = handler(inputResult$1.value);
|
|
183
|
+
const outputResult$1 = queryDef.output["~standard"].validate(result$1);
|
|
184
|
+
if (outputResult$1 instanceof Promise) throw new Error(`Query "${String(queryName)}" output validation must be synchronous. Use a schema library that supports synchronous validation for queries.`);
|
|
185
|
+
if (outputResult$1.issues) throw new require_errors.QueryOutputValidationError(queryName, outputResult$1.issues);
|
|
186
|
+
return outputResult$1.value;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
function createDefineUpdate(updateName, handler) {
|
|
190
|
+
if (!definition.updates) throw new Error(`Update "${String(updateName)}" cannot be defined: workflow "${String(workflowName)}" has no updates in its contract`);
|
|
191
|
+
const updateDef = definition.updates[updateName];
|
|
192
|
+
if (!updateDef) throw new Error(`Update "${String(updateName)}" not found in workflow "${String(workflowName)}" contract`);
|
|
193
|
+
(0, _temporalio_workflow.setHandler)((0, _temporalio_workflow.defineUpdate)(updateName), async (...args$1) => {
|
|
194
|
+
const input$1 = args$1.length === 1 ? args$1[0] : args$1;
|
|
195
|
+
const inputResult$1 = await updateDef.input["~standard"].validate(input$1);
|
|
196
|
+
if (inputResult$1.issues) throw new require_errors.UpdateInputValidationError(updateName, inputResult$1.issues);
|
|
197
|
+
const result$1 = await handler(inputResult$1.value);
|
|
198
|
+
const outputResult$1 = await updateDef.output["~standard"].validate(result$1);
|
|
199
|
+
if (outputResult$1.issues) throw new require_errors.UpdateOutputValidationError(updateName, outputResult$1.issues);
|
|
200
|
+
return outputResult$1.value;
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
const result = await implementation({
|
|
204
|
+
activities: contextActivities,
|
|
205
|
+
info: (0, _temporalio_workflow.workflowInfo)(),
|
|
206
|
+
startChildWorkflow: createStartChildWorkflow,
|
|
207
|
+
executeChildWorkflow: createExecuteChildWorkflow,
|
|
208
|
+
defineSignal: createDefineSignal,
|
|
209
|
+
defineQuery: createDefineQuery,
|
|
210
|
+
defineUpdate: createDefineUpdate
|
|
211
|
+
}, validatedInput);
|
|
212
|
+
const outputResult = await definition.output["~standard"].validate(result);
|
|
213
|
+
if (outputResult.issues) throw new require_errors.WorkflowOutputValidationError(String(workflowName), outputResult.issues);
|
|
214
|
+
return outputResult.value;
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Create a validated activities proxy that parses inputs and outputs
|
|
219
|
+
*
|
|
220
|
+
* This wrapper ensures data integrity across the network boundary between
|
|
221
|
+
* workflow and activity execution.
|
|
222
|
+
*/
|
|
223
|
+
function createValidatedActivities(rawActivities, workflowActivitiesDefinition, contractActivitiesDefinition) {
|
|
224
|
+
const validatedActivities = {};
|
|
225
|
+
const allActivitiesDefinition = {
|
|
226
|
+
...contractActivitiesDefinition,
|
|
227
|
+
...workflowActivitiesDefinition
|
|
228
|
+
};
|
|
229
|
+
for (const [activityName, activityDef] of Object.entries(allActivitiesDefinition)) {
|
|
230
|
+
const rawActivity = rawActivities[activityName];
|
|
231
|
+
if (!rawActivity) throw new Error(`Activity implementation not found for: "${activityName}". Available activities: ${Object.keys(rawActivities).length > 0 ? Object.keys(rawActivities).join(", ") : "none"}`);
|
|
232
|
+
validatedActivities[activityName] = async (input) => {
|
|
233
|
+
const inputResult = await activityDef.input["~standard"].validate(input);
|
|
234
|
+
if (inputResult.issues) throw new require_errors.ActivityInputValidationError(activityName, inputResult.issues);
|
|
235
|
+
const result = await rawActivity(inputResult.value);
|
|
236
|
+
const outputResult = await activityDef.output["~standard"].validate(result);
|
|
237
|
+
if (outputResult.issues) throw new require_errors.ActivityOutputValidationError(activityName, outputResult.issues);
|
|
238
|
+
return outputResult.value;
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
return validatedActivities;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
//#endregion
|
|
245
|
+
exports.ActivityInputValidationError = require_errors.ActivityInputValidationError;
|
|
246
|
+
exports.ActivityOutputValidationError = require_errors.ActivityOutputValidationError;
|
|
247
|
+
exports.ChildWorkflowError = require_errors.ChildWorkflowError;
|
|
248
|
+
exports.ChildWorkflowNotFoundError = require_errors.ChildWorkflowNotFoundError;
|
|
249
|
+
exports.QueryInputValidationError = require_errors.QueryInputValidationError;
|
|
250
|
+
exports.QueryOutputValidationError = require_errors.QueryOutputValidationError;
|
|
251
|
+
exports.SignalInputValidationError = require_errors.SignalInputValidationError;
|
|
252
|
+
exports.UpdateInputValidationError = require_errors.UpdateInputValidationError;
|
|
253
|
+
exports.UpdateOutputValidationError = require_errors.UpdateOutputValidationError;
|
|
254
|
+
exports.WorkflowInputValidationError = require_errors.WorkflowInputValidationError;
|
|
255
|
+
exports.WorkflowOutputValidationError = require_errors.WorkflowOutputValidationError;
|
|
256
|
+
exports.declareWorkflow = declareWorkflow;
|