@sanity/functions 1.7.3 → 1.8.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/dist/durables.d.ts +49 -0
- package/dist/durables.js +56 -2
- package/dist/durables.js.map +1 -1
- package/dist/invoke.js +4 -3
- package/dist/invoke.js.map +1 -1
- package/dist/types/durables.d.ts +5 -47
- package/package.json +1 -1
- package/src/durables.ts +93 -5
- package/src/invoke.ts +5 -3
- package/src/types/durables.ts +6 -50
package/dist/durables.d.ts
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
1
|
import type { DurableHandler } from './types/durables.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error type indicating an unrecoverable error in a durable function.
|
|
4
|
+
* Used to override the retry behavior in a durable step operation.
|
|
5
|
+
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
6
|
+
* @hidden
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const handler = durableEventHandler(async ({step}) => {
|
|
10
|
+
* async step.run(async ({attempt}) => {
|
|
11
|
+
* // step.run code
|
|
12
|
+
* }, retry: {maxAttempts: 3})
|
|
13
|
+
* }).catch((err) => {
|
|
14
|
+
* if (err instanceof UserNotFound) {
|
|
15
|
+
* throw new UnrecoverableError(err)
|
|
16
|
+
* }
|
|
17
|
+
* })
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class UnrecoverableError extends Error {
|
|
21
|
+
name: string;
|
|
22
|
+
}
|
|
2
23
|
/**
|
|
3
24
|
* Durables creation function that can be called with or without a config object.
|
|
4
25
|
* @alpha Durables are an experimental feature and may change in the future.
|
|
5
26
|
* @hidden
|
|
6
27
|
* @param handler
|
|
28
|
+
* @deprecated Use `durableEventHandler` instead
|
|
7
29
|
* @returns The handler function, unmodified.
|
|
8
30
|
*/
|
|
9
31
|
export declare function createDurable(handler: DurableHandler): DurableHandler & {
|
|
@@ -15,10 +37,36 @@ export declare function createDurable(handler: DurableHandler): DurableHandler &
|
|
|
15
37
|
* @hidden
|
|
16
38
|
* @param config
|
|
17
39
|
* @param handler
|
|
40
|
+
* @deprecated Use `durableEventHandler` instead
|
|
18
41
|
* @returns The handler function, unmodified and the provided config object.
|
|
19
42
|
*/
|
|
20
43
|
export declare function createDurable<TConfig extends {
|
|
21
44
|
name: string;
|
|
45
|
+
durableTimeout?: number;
|
|
46
|
+
}>(config: TConfig, handler: DurableHandler): DurableHandler & {
|
|
47
|
+
config: TConfig;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Durables creation function that can be called with or without a config object.
|
|
51
|
+
* @alpha Durables are an experimental feature and may change in the future.
|
|
52
|
+
* @hidden
|
|
53
|
+
* @param handler
|
|
54
|
+
* @returns The handler function, unmodified.
|
|
55
|
+
*/
|
|
56
|
+
export declare function durableEventHandler(handler: DurableHandler): DurableHandler & {
|
|
57
|
+
config?: undefined;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Durables creation function that can be called with or without a config object.
|
|
61
|
+
* @alpha Durables are an experimental feature and may change in the future.
|
|
62
|
+
* @hidden
|
|
63
|
+
* @param config
|
|
64
|
+
* @param handler
|
|
65
|
+
* @returns The handler function, unmodified and the provided config object.
|
|
66
|
+
*/
|
|
67
|
+
export declare function durableEventHandler<TConfig extends {
|
|
68
|
+
name: string;
|
|
69
|
+
durableTimeout?: number;
|
|
22
70
|
}>(config: TConfig, handler: DurableHandler): DurableHandler & {
|
|
23
71
|
config: TConfig;
|
|
24
72
|
};
|
|
@@ -28,5 +76,6 @@ export declare function createDurable<TConfig extends {
|
|
|
28
76
|
* @public
|
|
29
77
|
*/
|
|
30
78
|
export declare const durable: {
|
|
79
|
+
durableEventHandler: typeof durableEventHandler;
|
|
31
80
|
createDurable: typeof createDurable;
|
|
32
81
|
};
|
package/dist/durables.js
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Error type indicating an unrecoverable error in a durable function.
|
|
3
|
+
* Used to override the retry behavior in a durable step operation.
|
|
4
|
+
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
5
|
+
* @hidden
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const handler = durableEventHandler(async ({step}) => {
|
|
9
|
+
* async step.run(async ({attempt}) => {
|
|
10
|
+
* // step.run code
|
|
11
|
+
* }, retry: {maxAttempts: 3})
|
|
12
|
+
* }).catch((err) => {
|
|
13
|
+
* if (err instanceof UserNotFound) {
|
|
14
|
+
* throw new UnrecoverableError(err)
|
|
15
|
+
* }
|
|
16
|
+
* })
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export class UnrecoverableError extends Error {
|
|
20
|
+
name = 'UnrecoverableError';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Determine if `durableEventHandler({}, () => {})` or `durableEventHandler(() => {})`.
|
|
3
24
|
* @param configOrHandler
|
|
4
25
|
* @param maybeHandler
|
|
5
26
|
* @returns resolved arguments
|
|
@@ -29,6 +50,17 @@ const validateConfig = (config) => {
|
|
|
29
50
|
const event = 'event' in config ? config.event : undefined;
|
|
30
51
|
if (typeof name !== 'string')
|
|
31
52
|
errors.push('`config.name` must be a string');
|
|
53
|
+
if ('durableTimeout' in config && typeof config.durableTimeout !== 'number') {
|
|
54
|
+
errors.push('`config.durableTimeout` must be a number');
|
|
55
|
+
}
|
|
56
|
+
if ('durableTimeout' in config && typeof config.durableTimeout === 'number') {
|
|
57
|
+
if (config.durableTimeout < 60) {
|
|
58
|
+
errors.push('`config.durableTimeout` must be at least 60 seconds');
|
|
59
|
+
}
|
|
60
|
+
if (config.durableTimeout > 31_536_000) {
|
|
61
|
+
errors.push('`config.durableTimeout` must be at most 31_536_000 (1 year) seconds');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
32
64
|
if ('event' in config) {
|
|
33
65
|
if (typeof event !== 'object' || event === null) {
|
|
34
66
|
errors.push('`event` must be an object');
|
|
@@ -51,6 +83,7 @@ const validateConfig = (config) => {
|
|
|
51
83
|
* @public
|
|
52
84
|
* @param configOrHandler
|
|
53
85
|
* @param maybeHandler
|
|
86
|
+
* @deprecated Use `durableEventHandler` instead
|
|
54
87
|
* @returns The handler function, unmodified and the config object if provided
|
|
55
88
|
*/
|
|
56
89
|
export function createDurable(configOrHandler, maybeHandler) {
|
|
@@ -65,10 +98,31 @@ export function createDurable(configOrHandler, maybeHandler) {
|
|
|
65
98
|
// Separate config from the handler during build
|
|
66
99
|
return Object.assign(handler, { config });
|
|
67
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Durables creation function that can be called with or without a config object.
|
|
103
|
+
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
104
|
+
* @hidden
|
|
105
|
+
* @public
|
|
106
|
+
* @param configOrHandler
|
|
107
|
+
* @param maybeHandler
|
|
108
|
+
* @returns The handler function, unmodified and the config object if provided
|
|
109
|
+
*/
|
|
110
|
+
export function durableEventHandler(configOrHandler, maybeHandler) {
|
|
111
|
+
const { config, handler, hadConfigArg } = resolveFuncArgs(configOrHandler, maybeHandler);
|
|
112
|
+
const errors = [
|
|
113
|
+
...(hadConfigArg ? validateConfig(config) : []),
|
|
114
|
+
...(typeof handler !== 'function' ? ['`handler` must be a function'] : []),
|
|
115
|
+
];
|
|
116
|
+
if (errors.length > 0) {
|
|
117
|
+
throw new TypeError(errors.join(', '));
|
|
118
|
+
}
|
|
119
|
+
// Separate config from the handler during build
|
|
120
|
+
return Object.assign(handler, { config });
|
|
121
|
+
}
|
|
68
122
|
/**
|
|
69
123
|
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
70
124
|
* @hidden
|
|
71
125
|
* @public
|
|
72
126
|
*/
|
|
73
|
-
export const durable = { createDurable };
|
|
127
|
+
export const durable = { durableEventHandler, createDurable };
|
|
74
128
|
//# sourceMappingURL=durables.js.map
|
package/dist/durables.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durables.js","sourceRoot":"","sources":["../src/durables.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,eAAe,GAAG,CACtB,eAAyC,EACzC,YAA6B,EACwC,EAAE;IACvE,OAAO,OAAO,eAAe,KAAK,UAAU;QAC1C,CAAC,CAAC,EAAC,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,KAAK,EAAC;QACpE,CAAC,CAAC,EAAC,MAAM,EAAE,eAAe,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,IAAI,EAAC,CAAA;AAC1E,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,cAAc,GAAG,CAAC,MAAe,EAAE,EAAE;IACzC,MAAM,WAAW,GAAG,CAAC,UAAU,EAAE,eAAe,EAAE,MAAM,EAAE,qBAAqB,CAAC,CAAA;IAChF,MAAM,MAAM,GAAa,EAAE,CAAA;IAE3B,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAC5C,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAA;QACvC,OAAO,MAAM,CAAA;IACf,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAA;QACzC,OAAO,MAAM,CAAA;IACf,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;IACvD,MAAM,KAAK,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;IAC1D,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,MAAM,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"durables.js","sourceRoot":"","sources":["../src/durables.ts"],"names":[],"mappings":"AAEA;;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EAAE,aAAa,EAAC,CAAA"}
|
package/dist/invoke.js
CHANGED
|
@@ -5,6 +5,7 @@ let awsPromise;
|
|
|
5
5
|
const PARTITION_KEY = 'arc-app-res';
|
|
6
6
|
/** @internal */
|
|
7
7
|
export const MAX_RECURSION_COUNT = 16;
|
|
8
|
+
const INVOKE_DOCS_HINT = 'See docs for more information: https://www.sanity.io/docs/functions/function-to-function-invocation#ddd7727d1418';
|
|
8
9
|
/**
|
|
9
10
|
* @internal
|
|
10
11
|
* lazy load aws-lite
|
|
@@ -114,10 +115,10 @@ export async function invoke(name, payload, options) {
|
|
|
114
115
|
const functionType = contextResource?.type;
|
|
115
116
|
if (sync === true) {
|
|
116
117
|
if (functionType === undefined) {
|
|
117
|
-
throw new Error(`Function ${name} cannot be invoked synchronously
|
|
118
|
+
throw new Error(`Function ${name} cannot be invoked synchronously. See docs for more information: ${INVOKE_DOCS_HINT}`);
|
|
118
119
|
}
|
|
119
120
|
if (functionType !== 'sanity.function.pubsub') {
|
|
120
|
-
throw new Error(`Function ${name} of type ${functionType} cannot be invoked synchronously
|
|
121
|
+
throw new Error(`Function ${name} of type ${functionType} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`);
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
else if (functionType !== 'sanity.function.pubsub' && functionType !== 'sanity.function.queue') {
|
|
@@ -138,7 +139,7 @@ export async function invoke(name, payload, options) {
|
|
|
138
139
|
// Synchronous invocation
|
|
139
140
|
if (sync === true) {
|
|
140
141
|
if (!resource.function || !contextResource) {
|
|
141
|
-
throw new Error(`Function ${name} cannot be invoked synchronously
|
|
142
|
+
throw new Error(`Function ${name} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`);
|
|
142
143
|
}
|
|
143
144
|
const { Payload, FunctionError } = await aws.Lambda.Invoke({
|
|
144
145
|
FunctionName: resource.function.physicalResourceId,
|
package/dist/invoke.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"invo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a;YAC1B,QAAQ,EAAE,QAAQ;SACnB,CAAA;QACD,4DAA4D;QAC5D,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC;YAAE,OAAO,CAAC,cAAc,GAAG,IAAI,CAAA;QAC7D,MAAM,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;IACpC,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,wCAAwC,IAAI,EAAE,CAAC,CAAA;IACjE,CAAC;IAED,OAAM;AACR,CAAC;AAED,gBAAgB;AAChB,SAAS,gBAAgB,CAAC,OAAe,EAAE,IAAa;IACtD,IAAI,IAAI,EAAE,CAAC;QACT,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,CAAC;YACzD,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;QACxD,CAAC;IACH,CAAC;SAAM,CAAC;QACN,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC;YACpD,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAA;QAC1D,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/dist/types/durables.d.ts
CHANGED
|
@@ -57,56 +57,14 @@ export type DurableDuration = {
|
|
|
57
57
|
} | {
|
|
58
58
|
seconds: number;
|
|
59
59
|
};
|
|
60
|
-
/**
|
|
61
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
62
|
-
* @hidden
|
|
63
|
-
*/
|
|
64
|
-
export type DurableJitterStrategy = 'none' | 'full' | 'half';
|
|
65
60
|
/**
|
|
66
61
|
* @alpha: Using durables is considered experimental and may change in the future.
|
|
67
62
|
* @hidden
|
|
68
63
|
*/
|
|
69
64
|
export interface DurableRetryStrategy {
|
|
70
65
|
maxAttempts?: number;
|
|
71
|
-
|
|
72
|
-
maxDelay?: DurableDuration;
|
|
73
|
-
backoffRate?: number;
|
|
74
|
-
jitter?: DurableJitterStrategy;
|
|
75
|
-
retryableErrors?: (string | RegExp)[];
|
|
76
|
-
retryableErrorTypes?: (new () => Error)[];
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
80
|
-
* @hidden
|
|
81
|
-
*/
|
|
82
|
-
export type DurableStepSemantics = 'at-most-once-per-retry' | 'at-least-once-per-retry';
|
|
83
|
-
/**
|
|
84
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
85
|
-
* @hidden
|
|
86
|
-
*/
|
|
87
|
-
export interface DurableStepConfig {
|
|
88
|
-
strategy?: DurableRetryStrategy;
|
|
89
|
-
semantics?: DurableStepSemantics;
|
|
66
|
+
delay?: DurableDuration;
|
|
90
67
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Allowed operations in a retry code block
|
|
93
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
94
|
-
* @hidden
|
|
95
|
-
*/
|
|
96
|
-
export type DurableRetryOperations = Pick<DurableOperations, 'delegate' | 'waitForCallback' | 'waitForCondition'>;
|
|
97
|
-
/**
|
|
98
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
99
|
-
* @hidden
|
|
100
|
-
*/
|
|
101
|
-
export type DurableRetryBlock<T> = (envelope: {
|
|
102
|
-
step: DurableRetryOperations;
|
|
103
|
-
attempt: number;
|
|
104
|
-
}) => Promise<T>;
|
|
105
|
-
/**
|
|
106
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
107
|
-
* @hidden
|
|
108
|
-
*/
|
|
109
|
-
export type DurableRetry = <T>(name: string | undefined, config: DurableRetryStrategy, block: DurableRetryBlock<T>) => Promise<T>;
|
|
110
68
|
/**
|
|
111
69
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
112
70
|
* @hidden
|
|
@@ -172,7 +130,7 @@ export type DurableOperations = {
|
|
|
172
130
|
* step.run({
|
|
173
131
|
* name: 'run-with-retry'
|
|
174
132
|
* handler: () => { // handler code }
|
|
175
|
-
* retry: {
|
|
133
|
+
* retry: { maxAttempts: 2, delay: { seconds: 5 } }
|
|
176
134
|
* })
|
|
177
135
|
* ```
|
|
178
136
|
* @param name - Step name
|
|
@@ -183,7 +141,7 @@ export type DurableOperations = {
|
|
|
183
141
|
run<T>({ name, handler, retry }: {
|
|
184
142
|
name?: string;
|
|
185
143
|
handler: DurableStepRunHandler<T>;
|
|
186
|
-
retry?:
|
|
144
|
+
retry?: DurableRetryStrategy;
|
|
187
145
|
}): Promise<T>;
|
|
188
146
|
/**
|
|
189
147
|
* Calls another function and awaits its result.
|
|
@@ -202,7 +160,7 @@ export type DurableOperations = {
|
|
|
202
160
|
* }
|
|
203
161
|
*
|
|
204
162
|
* // my-durable-function/index.ts
|
|
205
|
-
*
|
|
163
|
+
* durableEventHandler(({step, context}) => {
|
|
206
164
|
* // With a function name
|
|
207
165
|
* step.delegate({name: 'my-function', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
|
|
208
166
|
* // With a resource object
|
|
@@ -218,6 +176,7 @@ export type DurableOperations = {
|
|
|
218
176
|
name?: string;
|
|
219
177
|
handler: string | BlueprintResource<`sanity.function.${string}`>;
|
|
220
178
|
event?: unknown;
|
|
179
|
+
retry?: DurableRetryStrategy;
|
|
221
180
|
}): Promise<T>;
|
|
222
181
|
/**
|
|
223
182
|
* Waits for an external callback
|
|
@@ -311,5 +270,4 @@ export type DurableHandler = (envelope: {
|
|
|
311
270
|
event?: GenericEvent;
|
|
312
271
|
step: DurableOperations;
|
|
313
272
|
logger: DurableLogger;
|
|
314
|
-
retry: DurableRetry;
|
|
315
273
|
}) => unknown | Promise<unknown>;
|
package/package.json
CHANGED
package/src/durables.ts
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
1
|
import type {DurableHandler} from './types/durables.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Error type indicating an unrecoverable error in a durable function.
|
|
5
|
+
* Used to override the retry behavior in a durable step operation.
|
|
6
|
+
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
7
|
+
* @hidden
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const handler = durableEventHandler(async ({step}) => {
|
|
11
|
+
* async step.run(async ({attempt}) => {
|
|
12
|
+
* // step.run code
|
|
13
|
+
* }, retry: {maxAttempts: 3})
|
|
14
|
+
* }).catch((err) => {
|
|
15
|
+
* if (err instanceof UserNotFound) {
|
|
16
|
+
* throw new UnrecoverableError(err)
|
|
17
|
+
* }
|
|
18
|
+
* })
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export class UnrecoverableError extends Error {
|
|
22
|
+
override name = 'UnrecoverableError'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Determine if `durableEventHandler({}, () => {})` or `durableEventHandler(() => {})`.
|
|
5
27
|
* @param configOrHandler
|
|
6
28
|
* @param maybeHandler
|
|
7
29
|
* @returns resolved arguments
|
|
8
30
|
*/
|
|
9
|
-
const resolveFuncArgs = <TConfig extends {name: string}>(
|
|
31
|
+
const resolveFuncArgs = <TConfig extends {name: string; durableTimeout?: number}>(
|
|
10
32
|
configOrHandler: TConfig | DurableHandler,
|
|
11
33
|
maybeHandler?: DurableHandler,
|
|
12
34
|
): {config?: TConfig; handler?: DurableHandler; hadConfigArg: boolean} => {
|
|
@@ -36,6 +58,18 @@ const validateConfig = (config: unknown) => {
|
|
|
36
58
|
const name = 'name' in config ? config.name : undefined
|
|
37
59
|
const event = 'event' in config ? config.event : undefined
|
|
38
60
|
if (typeof name !== 'string') errors.push('`config.name` must be a string')
|
|
61
|
+
if ('durableTimeout' in config && typeof config.durableTimeout !== 'number') {
|
|
62
|
+
errors.push('`config.durableTimeout` must be a number')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if ('durableTimeout' in config && typeof config.durableTimeout === 'number') {
|
|
66
|
+
if (config.durableTimeout < 60) {
|
|
67
|
+
errors.push('`config.durableTimeout` must be at least 60 seconds')
|
|
68
|
+
}
|
|
69
|
+
if (config.durableTimeout > 31_536_000) {
|
|
70
|
+
errors.push('`config.durableTimeout` must be at most 31_536_000 (1 year) seconds')
|
|
71
|
+
}
|
|
72
|
+
}
|
|
39
73
|
|
|
40
74
|
if ('event' in config) {
|
|
41
75
|
if (typeof event !== 'object' || event === null) {
|
|
@@ -56,10 +90,61 @@ const validateConfig = (config: unknown) => {
|
|
|
56
90
|
* @alpha Durables are an experimental feature and may change in the future.
|
|
57
91
|
* @hidden
|
|
58
92
|
* @param handler
|
|
93
|
+
* @deprecated Use `durableEventHandler` instead
|
|
59
94
|
* @returns The handler function, unmodified.
|
|
60
95
|
*/
|
|
61
96
|
export function createDurable(handler: DurableHandler): DurableHandler & {config?: undefined}
|
|
62
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Durables creation function that can be called with or without a config object.
|
|
100
|
+
* @alpha Durables are an experimental feature and may change in the future.
|
|
101
|
+
* @hidden
|
|
102
|
+
* @param config
|
|
103
|
+
* @param handler
|
|
104
|
+
* @deprecated Use `durableEventHandler` instead
|
|
105
|
+
* @returns The handler function, unmodified and the provided config object.
|
|
106
|
+
*/
|
|
107
|
+
export function createDurable<TConfig extends {name: string; durableTimeout?: number}>(
|
|
108
|
+
config: TConfig,
|
|
109
|
+
handler: DurableHandler,
|
|
110
|
+
): DurableHandler & {config: TConfig}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Durables creation function that can be called with or without a config object.
|
|
114
|
+
* @alpha Durable functions are an experimental feature and may change in the future.
|
|
115
|
+
* @hidden
|
|
116
|
+
* @public
|
|
117
|
+
* @param configOrHandler
|
|
118
|
+
* @param maybeHandler
|
|
119
|
+
* @deprecated Use `durableEventHandler` instead
|
|
120
|
+
* @returns The handler function, unmodified and the config object if provided
|
|
121
|
+
*/
|
|
122
|
+
export function createDurable<TConfig extends {name: string; durableTimeout?: number}>(
|
|
123
|
+
configOrHandler: TConfig | DurableHandler,
|
|
124
|
+
maybeHandler?: DurableHandler,
|
|
125
|
+
): DurableHandler {
|
|
126
|
+
const {config, handler, hadConfigArg} = resolveFuncArgs(configOrHandler, maybeHandler)
|
|
127
|
+
const errors = [
|
|
128
|
+
...(hadConfigArg ? validateConfig(config) : []),
|
|
129
|
+
...(typeof handler !== 'function' ? ['`handler` must be a function'] : []),
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
if (errors.length > 0) {
|
|
133
|
+
throw new TypeError(errors.join(', '))
|
|
134
|
+
}
|
|
135
|
+
// Separate config from the handler during build
|
|
136
|
+
return Object.assign(handler as DurableHandler, {config})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Durables creation function that can be called with or without a config object.
|
|
141
|
+
* @alpha Durables are an experimental feature and may change in the future.
|
|
142
|
+
* @hidden
|
|
143
|
+
* @param handler
|
|
144
|
+
* @returns The handler function, unmodified.
|
|
145
|
+
*/
|
|
146
|
+
export function durableEventHandler(handler: DurableHandler): DurableHandler & {config?: undefined}
|
|
147
|
+
|
|
63
148
|
/**
|
|
64
149
|
* Durables creation function that can be called with or without a config object.
|
|
65
150
|
* @alpha Durables are an experimental feature and may change in the future.
|
|
@@ -68,7 +153,10 @@ export function createDurable(handler: DurableHandler): DurableHandler & {config
|
|
|
68
153
|
* @param handler
|
|
69
154
|
* @returns The handler function, unmodified and the provided config object.
|
|
70
155
|
*/
|
|
71
|
-
export function
|
|
156
|
+
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: number}>(
|
|
157
|
+
config: TConfig,
|
|
158
|
+
handler: DurableHandler,
|
|
159
|
+
): DurableHandler & {config: TConfig}
|
|
72
160
|
|
|
73
161
|
/**
|
|
74
162
|
* Durables creation function that can be called with or without a config object.
|
|
@@ -79,7 +167,7 @@ export function createDurable<TConfig extends {name: string}>(config: TConfig, h
|
|
|
79
167
|
* @param maybeHandler
|
|
80
168
|
* @returns The handler function, unmodified and the config object if provided
|
|
81
169
|
*/
|
|
82
|
-
export function
|
|
170
|
+
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: number}>(
|
|
83
171
|
configOrHandler: TConfig | DurableHandler,
|
|
84
172
|
maybeHandler?: DurableHandler,
|
|
85
173
|
): DurableHandler {
|
|
@@ -101,4 +189,4 @@ export function createDurable<TConfig extends {name: string}>(
|
|
|
101
189
|
* @hidden
|
|
102
190
|
* @public
|
|
103
191
|
*/
|
|
104
|
-
export const durable = {createDurable}
|
|
192
|
+
export const durable = {durableEventHandler, createDurable}
|
package/src/invoke.ts
CHANGED
|
@@ -11,6 +11,8 @@ const PARTITION_KEY = 'arc-app-res'
|
|
|
11
11
|
/** @internal */
|
|
12
12
|
export const MAX_RECURSION_COUNT = 16
|
|
13
13
|
|
|
14
|
+
const INVOKE_DOCS_HINT = 'See docs for more information: https://www.sanity.io/docs/functions/function-to-function-invocation#ddd7727d1418'
|
|
15
|
+
|
|
14
16
|
/**
|
|
15
17
|
* @internal
|
|
16
18
|
* lazy load aws-lite
|
|
@@ -138,10 +140,10 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
138
140
|
const functionType = contextResource?.type
|
|
139
141
|
if (sync === true) {
|
|
140
142
|
if (functionType === undefined) {
|
|
141
|
-
throw new Error(`Function ${name} cannot be invoked synchronously
|
|
143
|
+
throw new Error(`Function ${name} cannot be invoked synchronously. See docs for more information: ${INVOKE_DOCS_HINT}`)
|
|
142
144
|
}
|
|
143
145
|
if (functionType !== 'sanity.function.pubsub') {
|
|
144
|
-
throw new Error(`Function ${name} of type ${functionType} cannot be invoked synchronously
|
|
146
|
+
throw new Error(`Function ${name} of type ${functionType} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`)
|
|
145
147
|
}
|
|
146
148
|
} else if (functionType !== 'sanity.function.pubsub' && functionType !== 'sanity.function.queue') {
|
|
147
149
|
throw new Error(`No invokeable resource for function: ${name}`)
|
|
@@ -164,7 +166,7 @@ export async function invoke<T = unknown>(name: string, payload: FunctionPayload
|
|
|
164
166
|
// Synchronous invocation
|
|
165
167
|
if (sync === true) {
|
|
166
168
|
if (!resource.function || !contextResource) {
|
|
167
|
-
throw new Error(`Function ${name} cannot be invoked synchronously
|
|
169
|
+
throw new Error(`Function ${name} cannot be invoked synchronously. ${INVOKE_DOCS_HINT}`)
|
|
168
170
|
}
|
|
169
171
|
const {Payload, FunctionError} = await aws.Lambda.Invoke({
|
|
170
172
|
FunctionName: resource.function.physicalResourceId,
|
package/src/types/durables.ts
CHANGED
|
@@ -53,60 +53,16 @@ export type DurableDuration =
|
|
|
53
53
|
| {minutes: number; seconds?: number}
|
|
54
54
|
| {seconds: number}
|
|
55
55
|
|
|
56
|
-
/**
|
|
57
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
58
|
-
* @hidden
|
|
59
|
-
*/
|
|
60
|
-
export type DurableJitterStrategy = 'none' | 'full' | 'half'
|
|
61
|
-
|
|
62
56
|
/**
|
|
63
57
|
* @alpha: Using durables is considered experimental and may change in the future.
|
|
64
58
|
* @hidden
|
|
65
59
|
*/
|
|
66
60
|
export interface DurableRetryStrategy {
|
|
67
61
|
maxAttempts?: number
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
backoffRate?: number
|
|
71
|
-
jitter?: DurableJitterStrategy
|
|
72
|
-
retryableErrors?: (string | RegExp)[]
|
|
73
|
-
retryableErrorTypes?: (new () => Error)[]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
78
|
-
* @hidden
|
|
79
|
-
*/
|
|
80
|
-
export type DurableStepSemantics = 'at-most-once-per-retry' | 'at-least-once-per-retry'
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
84
|
-
* @hidden
|
|
85
|
-
*/
|
|
86
|
-
export interface DurableStepConfig {
|
|
87
|
-
strategy?: DurableRetryStrategy
|
|
88
|
-
semantics?: DurableStepSemantics
|
|
62
|
+
// @todo: tie this to a string like "5s" or "1m" and normalize it to a duration object
|
|
63
|
+
delay?: DurableDuration
|
|
89
64
|
}
|
|
90
65
|
|
|
91
|
-
/**
|
|
92
|
-
* Allowed operations in a retry code block
|
|
93
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
94
|
-
* @hidden
|
|
95
|
-
*/
|
|
96
|
-
export type DurableRetryOperations = Pick<DurableOperations, 'delegate' | 'waitForCallback' | 'waitForCondition'>
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
100
|
-
* @hidden
|
|
101
|
-
*/
|
|
102
|
-
export type DurableRetryBlock<T> = (envelope: {step: DurableRetryOperations; attempt: number}) => Promise<T>
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* @alpha: Using durables is considered experimental and may change in the future.
|
|
106
|
-
* @hidden
|
|
107
|
-
*/
|
|
108
|
-
export type DurableRetry = <T>(name: string | undefined, config: DurableRetryStrategy, block: DurableRetryBlock<T>) => Promise<T>
|
|
109
|
-
|
|
110
66
|
/**
|
|
111
67
|
* @alpha Using durables is considered experimental and may change in the future.
|
|
112
68
|
* @hidden
|
|
@@ -173,7 +129,7 @@ export type DurableOperations = {
|
|
|
173
129
|
* step.run({
|
|
174
130
|
* name: 'run-with-retry'
|
|
175
131
|
* handler: () => { // handler code }
|
|
176
|
-
* retry: {
|
|
132
|
+
* retry: { maxAttempts: 2, delay: { seconds: 5 } }
|
|
177
133
|
* })
|
|
178
134
|
* ```
|
|
179
135
|
* @param name - Step name
|
|
@@ -181,7 +137,7 @@ export type DurableOperations = {
|
|
|
181
137
|
* @param retry - retry configuration
|
|
182
138
|
* @returns The value returned by the executed function
|
|
183
139
|
*/
|
|
184
|
-
run<T>({name, handler, retry}: {name?: string; handler: DurableStepRunHandler<T>; retry?:
|
|
140
|
+
run<T>({name, handler, retry}: {name?: string; handler: DurableStepRunHandler<T>; retry?: DurableRetryStrategy}): Promise<T>
|
|
185
141
|
/**
|
|
186
142
|
* Calls another function and awaits its result.
|
|
187
143
|
* similar to ctx.invoke()
|
|
@@ -199,7 +155,7 @@ export type DurableOperations = {
|
|
|
199
155
|
* }
|
|
200
156
|
*
|
|
201
157
|
* // my-durable-function/index.ts
|
|
202
|
-
*
|
|
158
|
+
* durableEventHandler(({step, context}) => {
|
|
203
159
|
* // With a function name
|
|
204
160
|
* step.delegate({name: 'my-function', handler: 'my-delegated-function', event: {data: {hello: 'world'}}})
|
|
205
161
|
* // With a resource object
|
|
@@ -219,6 +175,7 @@ export type DurableOperations = {
|
|
|
219
175
|
name?: string
|
|
220
176
|
handler: string | BlueprintResource<`sanity.function.${string}`>
|
|
221
177
|
event?: unknown
|
|
178
|
+
retry?: DurableRetryStrategy
|
|
222
179
|
}): Promise<T>
|
|
223
180
|
|
|
224
181
|
/**
|
|
@@ -315,5 +272,4 @@ export type DurableHandler = (envelope: {
|
|
|
315
272
|
event?: GenericEvent
|
|
316
273
|
step: DurableOperations
|
|
317
274
|
logger: DurableLogger
|
|
318
|
-
retry: DurableRetry
|
|
319
275
|
}) => unknown | Promise<unknown>
|