@temporalio/activity 1.15.0 → 1.16.1
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/lib/index.d.ts +51 -24
- package/lib/index.js +51 -34
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +52 -25
package/lib/index.d.ts
CHANGED
|
@@ -55,16 +55,62 @@
|
|
|
55
55
|
*
|
|
56
56
|
* #### An Activity that sends progress heartbeats and can be Cancelled
|
|
57
57
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { activityInfo, log, sleep, CancelledFailure, heartbeat } from '@temporalio/activity';
|
|
60
|
+
*
|
|
61
|
+
* export async function fakeProgress(sleepIntervalMs = 1000): Promise<void> {
|
|
62
|
+
* try {
|
|
63
|
+
* // allow for resuming from heartbeat
|
|
64
|
+
* const startingPoint = activityInfo().heartbeatDetails || 1;
|
|
65
|
+
* log.info('Starting activity at progress', { startingPoint });
|
|
66
|
+
* for (let progress = startingPoint; progress <= 100; ++progress) {
|
|
67
|
+
* // simple utility to sleep in activity for given interval or throw if Activity is cancelled
|
|
68
|
+
* // don't confuse with Workflow.sleep which is only used in Workflow functions!
|
|
69
|
+
* log.info('Progress', { progress });
|
|
70
|
+
* await sleep(sleepIntervalMs);
|
|
71
|
+
* heartbeat(progress);
|
|
72
|
+
* }
|
|
73
|
+
* } catch (err) {
|
|
74
|
+
* if (err instanceof CancelledFailure) {
|
|
75
|
+
* log.warn('Fake progress activity cancelled', { message: err.message });
|
|
76
|
+
* // Cleanup
|
|
77
|
+
* }
|
|
78
|
+
* throw err;
|
|
79
|
+
* }
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
60
82
|
*
|
|
61
83
|
* #### An Activity that makes a cancellable HTTP request
|
|
62
84
|
*
|
|
63
85
|
* It passes the `AbortSignal` to {@link https://github.com/node-fetch/node-fetch#api | `fetch`}: `fetch(url, { signal:
|
|
64
86
|
* Context.current().cancellationSignal })`.
|
|
65
87
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* import fetch from 'node-fetch';
|
|
90
|
+
* import { cancellationSignal, heartbeat } from '@temporalio/activity';
|
|
91
|
+
* import type { AbortSignal as FetchAbortSignal } from 'node-fetch/externals';
|
|
92
|
+
*
|
|
93
|
+
* export async function cancellableFetch(url: string): Promise<Uint8Array> {
|
|
94
|
+
* const response = await fetch(url, { signal: cancellationSignal() as FetchAbortSignal });
|
|
95
|
+
* const contentLengthHeader = response.headers.get('Content-Length');
|
|
96
|
+
* if (contentLengthHeader === null) {
|
|
97
|
+
* throw new Error('expected Content-Length header to be set');
|
|
98
|
+
* }
|
|
99
|
+
* const contentLength = parseInt(contentLengthHeader);
|
|
100
|
+
* let bytesRead = 0;
|
|
101
|
+
* const chunks: Buffer[] = [];
|
|
102
|
+
*
|
|
103
|
+
* for await (const chunk of response.body) {
|
|
104
|
+
* if (!(chunk instanceof Buffer)) {
|
|
105
|
+
* throw new TypeError('Expected Buffer');
|
|
106
|
+
* }
|
|
107
|
+
* bytesRead += chunk.length;
|
|
108
|
+
* chunks.push(chunk);
|
|
109
|
+
* heartbeat(bytesRead / contentLength);
|
|
110
|
+
* }
|
|
111
|
+
* return Buffer.concat(chunks);
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
68
114
|
*
|
|
69
115
|
* @module
|
|
70
116
|
*/
|
|
@@ -73,26 +119,7 @@ import { Logger, Duration, MetricMeter, Priority, ActivityCancellationDetails, R
|
|
|
73
119
|
import { ActivityCancellationDetailsHolder } from '@temporalio/common/lib/activity-cancellation-details';
|
|
74
120
|
import { Client } from '@temporalio/client';
|
|
75
121
|
export { ActivityFunction, ActivityInterface, // eslint-disable-line @typescript-eslint/no-deprecated
|
|
76
|
-
ApplicationFailure, CancelledFailure, UntypedActivities, } from '@temporalio/common';
|
|
77
|
-
/**
|
|
78
|
-
* Throw this error from an Activity in order to make the Worker forget about this Activity.
|
|
79
|
-
*
|
|
80
|
-
* The Activity can then be completed asynchronously (from anywhere—usually outside the Worker) using
|
|
81
|
-
* {@link Client.activity}.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
*
|
|
85
|
-
* ```ts
|
|
86
|
-
*import { CompleteAsyncError } from '@temporalio/activity';
|
|
87
|
-
*
|
|
88
|
-
*export async function myActivity(): Promise<never> {
|
|
89
|
-
* // ...
|
|
90
|
-
* throw new CompleteAsyncError();
|
|
91
|
-
*}
|
|
92
|
-
* ```
|
|
93
|
-
*/
|
|
94
|
-
export declare class CompleteAsyncError extends Error {
|
|
95
|
-
}
|
|
122
|
+
ApplicationFailure, CancelledFailure, CompleteAsyncError, UntypedActivities, } from '@temporalio/common';
|
|
96
123
|
export declare const asyncLocalStorage: AsyncLocalStorage<Context>;
|
|
97
124
|
/**
|
|
98
125
|
* Holds information about the current Activity Execution. Retrieved inside an Activity with `Context.current().info`.
|
package/lib/index.js
CHANGED
|
@@ -56,25 +56,65 @@
|
|
|
56
56
|
*
|
|
57
57
|
* #### An Activity that sends progress heartbeats and can be Cancelled
|
|
58
58
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { activityInfo, log, sleep, CancelledFailure, heartbeat } from '@temporalio/activity';
|
|
61
|
+
*
|
|
62
|
+
* export async function fakeProgress(sleepIntervalMs = 1000): Promise<void> {
|
|
63
|
+
* try {
|
|
64
|
+
* // allow for resuming from heartbeat
|
|
65
|
+
* const startingPoint = activityInfo().heartbeatDetails || 1;
|
|
66
|
+
* log.info('Starting activity at progress', { startingPoint });
|
|
67
|
+
* for (let progress = startingPoint; progress <= 100; ++progress) {
|
|
68
|
+
* // simple utility to sleep in activity for given interval or throw if Activity is cancelled
|
|
69
|
+
* // don't confuse with Workflow.sleep which is only used in Workflow functions!
|
|
70
|
+
* log.info('Progress', { progress });
|
|
71
|
+
* await sleep(sleepIntervalMs);
|
|
72
|
+
* heartbeat(progress);
|
|
73
|
+
* }
|
|
74
|
+
* } catch (err) {
|
|
75
|
+
* if (err instanceof CancelledFailure) {
|
|
76
|
+
* log.warn('Fake progress activity cancelled', { message: err.message });
|
|
77
|
+
* // Cleanup
|
|
78
|
+
* }
|
|
79
|
+
* throw err;
|
|
80
|
+
* }
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
61
83
|
*
|
|
62
84
|
* #### An Activity that makes a cancellable HTTP request
|
|
63
85
|
*
|
|
64
86
|
* It passes the `AbortSignal` to {@link https://github.com/node-fetch/node-fetch#api | `fetch`}: `fetch(url, { signal:
|
|
65
87
|
* Context.current().cancellationSignal })`.
|
|
66
88
|
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
89
|
+
* ```ts
|
|
90
|
+
* import fetch from 'node-fetch';
|
|
91
|
+
* import { cancellationSignal, heartbeat } from '@temporalio/activity';
|
|
92
|
+
* import type { AbortSignal as FetchAbortSignal } from 'node-fetch/externals';
|
|
93
|
+
*
|
|
94
|
+
* export async function cancellableFetch(url: string): Promise<Uint8Array> {
|
|
95
|
+
* const response = await fetch(url, { signal: cancellationSignal() as FetchAbortSignal });
|
|
96
|
+
* const contentLengthHeader = response.headers.get('Content-Length');
|
|
97
|
+
* if (contentLengthHeader === null) {
|
|
98
|
+
* throw new Error('expected Content-Length header to be set');
|
|
99
|
+
* }
|
|
100
|
+
* const contentLength = parseInt(contentLengthHeader);
|
|
101
|
+
* let bytesRead = 0;
|
|
102
|
+
* const chunks: Buffer[] = [];
|
|
103
|
+
*
|
|
104
|
+
* for await (const chunk of response.body) {
|
|
105
|
+
* if (!(chunk instanceof Buffer)) {
|
|
106
|
+
* throw new TypeError('Expected Buffer');
|
|
107
|
+
* }
|
|
108
|
+
* bytesRead += chunk.length;
|
|
109
|
+
* chunks.push(chunk);
|
|
110
|
+
* heartbeat(bytesRead / contentLength);
|
|
111
|
+
* }
|
|
112
|
+
* return Buffer.concat(chunks);
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
69
115
|
*
|
|
70
116
|
* @module
|
|
71
117
|
*/
|
|
72
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
73
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
74
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
75
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
76
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
77
|
-
};
|
|
78
118
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
79
119
|
exports.metricMeter = exports.log = exports.Context = exports.asyncLocalStorage = exports.CompleteAsyncError = exports.CancelledFailure = exports.ApplicationFailure = void 0;
|
|
80
120
|
exports.activityInfo = activityInfo;
|
|
@@ -87,33 +127,10 @@ exports.getClient = getClient;
|
|
|
87
127
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
88
128
|
const common_1 = require("@temporalio/common");
|
|
89
129
|
const time_1 = require("@temporalio/common/lib/time");
|
|
90
|
-
const type_helpers_1 = require("@temporalio/common/lib/type-helpers");
|
|
91
130
|
var common_2 = require("@temporalio/common");
|
|
92
131
|
Object.defineProperty(exports, "ApplicationFailure", { enumerable: true, get: function () { return common_2.ApplicationFailure; } });
|
|
93
132
|
Object.defineProperty(exports, "CancelledFailure", { enumerable: true, get: function () { return common_2.CancelledFailure; } });
|
|
94
|
-
|
|
95
|
-
* Throw this error from an Activity in order to make the Worker forget about this Activity.
|
|
96
|
-
*
|
|
97
|
-
* The Activity can then be completed asynchronously (from anywhere—usually outside the Worker) using
|
|
98
|
-
* {@link Client.activity}.
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
*
|
|
102
|
-
* ```ts
|
|
103
|
-
*import { CompleteAsyncError } from '@temporalio/activity';
|
|
104
|
-
*
|
|
105
|
-
*export async function myActivity(): Promise<never> {
|
|
106
|
-
* // ...
|
|
107
|
-
* throw new CompleteAsyncError();
|
|
108
|
-
*}
|
|
109
|
-
* ```
|
|
110
|
-
*/
|
|
111
|
-
let CompleteAsyncError = class CompleteAsyncError extends Error {
|
|
112
|
-
};
|
|
113
|
-
exports.CompleteAsyncError = CompleteAsyncError;
|
|
114
|
-
exports.CompleteAsyncError = CompleteAsyncError = __decorate([
|
|
115
|
-
(0, type_helpers_1.SymbolBasedInstanceOfError)('CompleteAsyncError')
|
|
116
|
-
], CompleteAsyncError);
|
|
133
|
+
Object.defineProperty(exports, "CompleteAsyncError", { enumerable: true, get: function () { return common_2.CompleteAsyncError; } });
|
|
117
134
|
// Make it safe to use @temporalio/activity with multiple versions installed.
|
|
118
135
|
const asyncLocalStorageSymbol = Symbol.for('__temporal_activity_context_storage__');
|
|
119
136
|
if (!globalThis[asyncLocalStorageSymbol]) {
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmHG;;;AAuTH,oCAGC;AAsCD,sBAEC;AAeD,8BAEC;AAWD,8BAEC;AAQD,kDAEC;AAgBD,gDAEC;AAcD,8BAEC;AA1aD,uDAAqD;AACrD,+CAU4B;AAC5B,sDAAyD;AAKzD,6CAO4B;AAJ1B,4GAAA,kBAAkB,OAAA;AAClB,0GAAA,gBAAgB,OAAA;AAChB,4GAAA,kBAAkB,OAAA;AAIpB,6EAA6E;AAC7E,MAAM,uBAAuB,GAAG,MAAM,CAAC,GAAG,CA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;;;GAWG;AACH,SAAgB,SAAS;IACvB,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,MAAM,CAAC;AAClC,CAAC;AAED;;;;;;;GAOG;AACU,QAAA,WAAW,GAAgB;IACtC,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW;QACnC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,WAAW,CAAC,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;IAC9E,CAAC;IACD,eAAe,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,EAAE,IAAI,EAAE,WAAW;QACxD,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,WAAW,CAAC,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;IAC3F,CAAC;IACD,WAAW,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,EAAE,IAAI,EAAE,WAAW;QACpD,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;IACvF,CAAC;IACD,QAAQ,CAAC,IAAI;QACX,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACtD,CAAC;CACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporalio/activity",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.1",
|
|
4
4
|
"description": "Temporal.io SDK Activity sub-package",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"abort-controller": "^3.0.0",
|
|
17
|
-
"@temporalio/client": "1.
|
|
18
|
-
"@temporalio/common": "1.
|
|
17
|
+
"@temporalio/client": "1.16.1",
|
|
18
|
+
"@temporalio/common": "1.16.1"
|
|
19
19
|
},
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">= 20.0.0"
|
package/src/index.ts
CHANGED
|
@@ -55,16 +55,62 @@
|
|
|
55
55
|
*
|
|
56
56
|
* #### An Activity that sends progress heartbeats and can be Cancelled
|
|
57
57
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* import { activityInfo, log, sleep, CancelledFailure, heartbeat } from '@temporalio/activity';
|
|
60
|
+
*
|
|
61
|
+
* export async function fakeProgress(sleepIntervalMs = 1000): Promise<void> {
|
|
62
|
+
* try {
|
|
63
|
+
* // allow for resuming from heartbeat
|
|
64
|
+
* const startingPoint = activityInfo().heartbeatDetails || 1;
|
|
65
|
+
* log.info('Starting activity at progress', { startingPoint });
|
|
66
|
+
* for (let progress = startingPoint; progress <= 100; ++progress) {
|
|
67
|
+
* // simple utility to sleep in activity for given interval or throw if Activity is cancelled
|
|
68
|
+
* // don't confuse with Workflow.sleep which is only used in Workflow functions!
|
|
69
|
+
* log.info('Progress', { progress });
|
|
70
|
+
* await sleep(sleepIntervalMs);
|
|
71
|
+
* heartbeat(progress);
|
|
72
|
+
* }
|
|
73
|
+
* } catch (err) {
|
|
74
|
+
* if (err instanceof CancelledFailure) {
|
|
75
|
+
* log.warn('Fake progress activity cancelled', { message: err.message });
|
|
76
|
+
* // Cleanup
|
|
77
|
+
* }
|
|
78
|
+
* throw err;
|
|
79
|
+
* }
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
60
82
|
*
|
|
61
83
|
* #### An Activity that makes a cancellable HTTP request
|
|
62
84
|
*
|
|
63
85
|
* It passes the `AbortSignal` to {@link https://github.com/node-fetch/node-fetch#api | `fetch`}: `fetch(url, { signal:
|
|
64
86
|
* Context.current().cancellationSignal })`.
|
|
65
87
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* import fetch from 'node-fetch';
|
|
90
|
+
* import { cancellationSignal, heartbeat } from '@temporalio/activity';
|
|
91
|
+
* import type { AbortSignal as FetchAbortSignal } from 'node-fetch/externals';
|
|
92
|
+
*
|
|
93
|
+
* export async function cancellableFetch(url: string): Promise<Uint8Array> {
|
|
94
|
+
* const response = await fetch(url, { signal: cancellationSignal() as FetchAbortSignal });
|
|
95
|
+
* const contentLengthHeader = response.headers.get('Content-Length');
|
|
96
|
+
* if (contentLengthHeader === null) {
|
|
97
|
+
* throw new Error('expected Content-Length header to be set');
|
|
98
|
+
* }
|
|
99
|
+
* const contentLength = parseInt(contentLengthHeader);
|
|
100
|
+
* let bytesRead = 0;
|
|
101
|
+
* const chunks: Buffer[] = [];
|
|
102
|
+
*
|
|
103
|
+
* for await (const chunk of response.body) {
|
|
104
|
+
* if (!(chunk instanceof Buffer)) {
|
|
105
|
+
* throw new TypeError('Expected Buffer');
|
|
106
|
+
* }
|
|
107
|
+
* bytesRead += chunk.length;
|
|
108
|
+
* chunks.push(chunk);
|
|
109
|
+
* heartbeat(bytesRead / contentLength);
|
|
110
|
+
* }
|
|
111
|
+
* return Buffer.concat(chunks);
|
|
112
|
+
* }
|
|
113
|
+
* ```
|
|
68
114
|
*
|
|
69
115
|
* @module
|
|
70
116
|
*/
|
|
@@ -82,7 +128,7 @@ import {
|
|
|
82
128
|
RetryPolicy,
|
|
83
129
|
} from '@temporalio/common';
|
|
84
130
|
import { msToNumber } from '@temporalio/common/lib/time';
|
|
85
|
-
|
|
131
|
+
|
|
86
132
|
import { ActivityCancellationDetailsHolder } from '@temporalio/common/lib/activity-cancellation-details';
|
|
87
133
|
import { Client } from '@temporalio/client';
|
|
88
134
|
|
|
@@ -91,29 +137,10 @@ export {
|
|
|
91
137
|
ActivityInterface, // eslint-disable-line @typescript-eslint/no-deprecated
|
|
92
138
|
ApplicationFailure,
|
|
93
139
|
CancelledFailure,
|
|
140
|
+
CompleteAsyncError,
|
|
94
141
|
UntypedActivities,
|
|
95
142
|
} from '@temporalio/common';
|
|
96
143
|
|
|
97
|
-
/**
|
|
98
|
-
* Throw this error from an Activity in order to make the Worker forget about this Activity.
|
|
99
|
-
*
|
|
100
|
-
* The Activity can then be completed asynchronously (from anywhere—usually outside the Worker) using
|
|
101
|
-
* {@link Client.activity}.
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
*
|
|
105
|
-
* ```ts
|
|
106
|
-
*import { CompleteAsyncError } from '@temporalio/activity';
|
|
107
|
-
*
|
|
108
|
-
*export async function myActivity(): Promise<never> {
|
|
109
|
-
* // ...
|
|
110
|
-
* throw new CompleteAsyncError();
|
|
111
|
-
*}
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
@SymbolBasedInstanceOfError('CompleteAsyncError')
|
|
115
|
-
export class CompleteAsyncError extends Error {}
|
|
116
|
-
|
|
117
144
|
// Make it safe to use @temporalio/activity with multiple versions installed.
|
|
118
145
|
const asyncLocalStorageSymbol = Symbol.for('__temporal_activity_context_storage__');
|
|
119
146
|
if (!(globalThis as any)[asyncLocalStorageSymbol]) {
|