2
2
ActivityFunction ,
3
3
ActivityOptions ,
4
4
compileRetryPolicy ,
5
+ extractWorkflowType ,
5
6
IllegalStateError ,
6
7
LocalActivityOptions ,
7
8
mapToPayloads ,
@@ -37,7 +38,7 @@ import {
37
38
Handler ,
38
39
WorkflowInfo ,
39
40
} from './interfaces' ;
40
- import { LocalActivityDoBackoff , getActivator , maybeGetActivator } from './internals' ;
41
+ import { Activator , LocalActivityDoBackoff , getActivator , maybeGetActivator } from './internals' ;
41
42
import { LoggerSinks , Sinks } from './sinks' ;
42
43
import { untrackPromise } from './stack-helpers' ;
43
44
import { ChildWorkflowHandle , ExternalWorkflowHandle } from './workflow-handle' ;
@@ -108,7 +109,7 @@ function timerNextHandler(input: TimerInput) {
108
109
* If given a negative number or 0, value will be set to 1.
109
110
*/
110
111
export function sleep ( ms : number | string ) : Promise < void > {
111
- const activator = getActivator ( ) ;
112
+ const activator = assertInWorkflowContext ( 'Workflow.sleep(...) may only be used from a Workflow Execution' ) ;
112
113
const seq = activator . nextSeqs . timer ++ ;
113
114
114
115
const durationMs = Math . max ( 1 , msToNumber ( ms ) ) ;
@@ -250,7 +251,9 @@ async function scheduleLocalActivityNextHandler({
250
251
* @hidden
251
252
*/
252
253
export function scheduleActivity < R > ( activityType : string , args : any [ ] , options : ActivityOptions ) : Promise < R > {
253
- const activator = getActivator ( ) ;
254
+ const activator = assertInWorkflowContext (
255
+ 'Workflow.scheduleActivity(...) may only be used from a Workflow Execution'
256
+ ) ;
254
257
if ( options === undefined ) {
255
258
throw new TypeError ( 'Got empty activity options' ) ;
256
259
}
@@ -275,7 +278,9 @@ export async function scheduleLocalActivity<R>(
275
278
args : any [ ] ,
276
279
options : LocalActivityOptions
277
280
) : Promise < R > {
278
- const activator = getActivator ( ) ;
281
+ const activator = assertInWorkflowContext (
282
+ 'Workflow.scheduleLocalActivity(...) may only be used from a Workflow Execution'
283
+ ) ;
279
284
if ( options === undefined ) {
280
285
throw new TypeError ( 'Got empty activity options' ) ;
281
286
}
@@ -574,7 +579,9 @@ const CONDITION_0_PATCH = '__sdk_internal_patch_number:1';
574
579
* It takes a Workflow ID and optional run ID.
575
580
*/
576
581
export function getExternalWorkflowHandle ( workflowId : string , runId ?: string ) : ExternalWorkflowHandle {
577
- const activator = getActivator ( ) ;
582
+ const activator = assertInWorkflowContext (
583
+ 'Workflow.getExternalWorkflowHandle(...) may only be used from a Workflow Execution. Consider using Client.workflow.getHandle(...) instead.)'
584
+ ) ;
578
585
return {
579
586
workflowId,
580
587
runId,
@@ -696,9 +703,11 @@ export async function startChild<T extends Workflow>(
696
703
workflowTypeOrFunc : string | T ,
697
704
options ?: WithWorkflowArgs < T , ChildWorkflowOptions >
698
705
) : Promise < ChildWorkflowHandle < T > > {
699
- const activator = getActivator ( ) ;
706
+ const activator = assertInWorkflowContext (
707
+ 'Workflow.startChild(...) may only be used from a Workflow Execution. Consider using Client.workflow.start(...) instead.)'
708
+ ) ;
700
709
const optionsWithDefaults = addDefaultWorkflowOptions ( options ?? ( { } as any ) ) ;
701
- const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc . name ;
710
+ const workflowType = extractWorkflowType ( workflowTypeOrFunc ) ;
702
711
const execute = composeInterceptors (
703
712
activator . interceptors . outbound ,
704
713
'startChildWorkflowExecution' ,
@@ -795,9 +804,11 @@ export async function executeChild<T extends Workflow>(
795
804
workflowTypeOrFunc : string | T ,
796
805
options ?: WithWorkflowArgs < T , ChildWorkflowOptions >
797
806
) : Promise < WorkflowResultType < T > > {
798
- const activator = getActivator ( ) ;
807
+ const activator = assertInWorkflowContext (
808
+ 'Workflow.executeChild(...) may only be used from a Workflow Execution. Consider using Client.workflow.execute(...) instead.'
809
+ ) ;
799
810
const optionsWithDefaults = addDefaultWorkflowOptions ( options ?? ( { } as any ) ) ;
800
- const workflowType = typeof workflowTypeOrFunc === 'string' ? workflowTypeOrFunc : workflowTypeOrFunc . name ;
811
+ const workflowType = extractWorkflowType ( workflowTypeOrFunc ) ;
801
812
const execute = composeInterceptors (
802
813
activator . interceptors . outbound ,
803
814
'startChildWorkflowExecution' ,
@@ -841,7 +852,8 @@ export async function executeChild<T extends Workflow>(
841
852
* }
842
853
*/
843
854
export function workflowInfo ( ) : WorkflowInfo {
844
- return getActivator ( ) . info ;
855
+ const activator = assertInWorkflowContext ( 'Workflow.workflowInfo(...) may only be used from a Workflow Execution.' ) ;
856
+ return activator . info ;
845
857
}
846
858
847
859
/**
@@ -891,7 +903,9 @@ export function proxySinks<T extends Sinks>(): T {
891
903
{
892
904
get ( _ , fnName ) {
893
905
return ( ...args : any [ ] ) => {
894
- const activator = getActivator ( ) ;
906
+ const activator = assertInWorkflowContext (
907
+ 'Proxied sinks functions may only be used from a Workflow Execution.'
908
+ ) ;
895
909
activator . sinkCalls . push ( {
896
910
ifaceName : ifaceName as string ,
897
911
fnName : fnName as string ,
@@ -917,8 +931,10 @@ export function proxySinks<T extends Sinks>(): T {
917
931
export function makeContinueAsNewFunc < F extends Workflow > (
918
932
options ?: ContinueAsNewOptions
919
933
) : ( ...args : Parameters < F > ) => Promise < never > {
920
- const activator = getActivator ( ) ;
921
- const info = workflowInfo ( ) ;
934
+ const activator = assertInWorkflowContext (
935
+ 'Workflow.continueAsNew(...) and Workflow.makeContinueAsNewFunc(...) may only be used from a Workflow Execution.'
936
+ ) ;
937
+ const info = activator . info ;
922
938
const { workflowType, taskQueue, ...rest } = options ?? { } ;
923
939
const requiredOptions = {
924
940
workflowType : workflowType ?? info . workflowType ,
@@ -1041,7 +1057,9 @@ export function deprecatePatch(patchId: string): void {
1041
1057
}
1042
1058
1043
1059
function patchInternal ( patchId : string , deprecated : boolean ) : boolean {
1044
- const activator = getActivator ( ) ;
1060
+ const activator = assertInWorkflowContext (
1061
+ 'Workflow.patch(...) and Workflow.deprecatePatch may only be used from a Workflow Execution.'
1062
+ ) ;
1045
1063
// Patch operation does not support interception at the moment, if it did,
1046
1064
// this would be the place to start the interception chain
1047
1065
@@ -1075,6 +1093,7 @@ export function condition(fn: () => boolean, timeout: number | string): Promise<
1075
1093
export function condition ( fn : ( ) => boolean ) : Promise < void > ;
1076
1094
1077
1095
export async function condition ( fn : ( ) => boolean , timeout ?: number | string ) : Promise < void | boolean > {
1096
+ assertInWorkflowContext ( 'Workflow.condition(...) may only be used from a Workflow Execution.' ) ;
1078
1097
// Prior to 1.5.0, `condition(fn, 0)` was treated as equivalent to `condition(fn, undefined)`
1079
1098
if ( timeout === 0 && ! patched ( CONDITION_0_PATCH ) ) {
1080
1099
return conditionInner ( fn ) ;
@@ -1162,7 +1181,7 @@ export function setHandler<Ret, Args extends any[], T extends SignalDefinition<A
1162
1181
def : T ,
1163
1182
handler : Handler < Ret , Args , T > | undefined
1164
1183
) : void {
1165
- const activator = getActivator ( ) ;
1184
+ const activator = assertInWorkflowContext ( 'Workflow.setHandler(...) may only be used from a Workflow Execution.' ) ;
1166
1185
if ( def . type === 'signal' ) {
1167
1186
if ( typeof handler === 'function' ) {
1168
1187
activator . signalHandlers . set ( def . name , handler as any ) ;
@@ -1195,7 +1214,9 @@ export function setHandler<Ret, Args extends any[], T extends SignalDefinition<A
1195
1214
* @param handler a function that will handle signals for non-registered signal names, or `undefined` to unset the handler.
1196
1215
*/
1197
1216
export function setDefaultSignalHandler ( handler : DefaultSignalHandler | undefined ) : void {
1198
- const activator = getActivator ( ) ;
1217
+ const activator = assertInWorkflowContext (
1218
+ 'Workflow.setDefaultSignalHandler(...) may only be used from a Workflow Execution.'
1219
+ ) ;
1199
1220
if ( typeof handler === 'function' ) {
1200
1221
activator . defaultSignalHandler = handler ;
1201
1222
activator . dispatchBufferedSignals ( ) ;
@@ -1236,7 +1257,9 @@ export function setDefaultSignalHandler(handler: DefaultSignalHandler | undefine
1236
1257
* @param searchAttributes The Record to merge. Use a value of `[]` to clear a Search Attribute.
1237
1258
*/
1238
1259
export function upsertSearchAttributes ( searchAttributes : SearchAttributes ) : void {
1239
- const activator = getActivator ( ) ;
1260
+ const activator = assertInWorkflowContext (
1261
+ 'Workflow.upsertSearchAttributes(...) may only be used from a Workflow Execution.'
1262
+ ) ;
1240
1263
1241
1264
const mergedSearchAttributes = { ...activator . info . searchAttributes , ...searchAttributes } ;
1242
1265
if ( ! mergedSearchAttributes ) {
@@ -1281,6 +1304,7 @@ export const log: LoggerSinks['defaultWorkerLogger'] = Object.fromEntries(
1281
1304
return [
1282
1305
level ,
1283
1306
( message : string , attrs : Record < string , unknown > ) => {
1307
+ assertInWorkflowContext ( 'Workflow.log(...) may only be used from a Workflow Execution.)' ) ;
1284
1308
return loggerSinks . defaultWorkerLogger [ level ] ( message , {
1285
1309
// Inject the call time in nanosecond resolution as expected by the worker logger.
1286
1310
[ LogTimestamp ] : getActivator ( ) . getTimeOfDay ( ) ,
@@ -1290,3 +1314,9 @@ export const log: LoggerSinks['defaultWorkerLogger'] = Object.fromEntries(
1290
1314
] ;
1291
1315
} )
1292
1316
) as any ;
1317
+
1318
+ function assertInWorkflowContext ( message : string ) : Activator {
1319
+ const activator = maybeGetActivator ( ) ;
1320
+ if ( activator == null ) throw new IllegalStateError ( message ) ;
1321
+ return activator ;
1322
+ }
0 commit comments