@sanity/functions 1.11.0 → 1.12.0
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 +1 -1
- package/dist/durables.js +12 -8
- package/dist/durables.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types/context.d.ts +5 -1
- package/dist/types/durables.d.ts +23 -9
- package/dist/types/invocation.d.ts +6 -0
- package/dist/utils.d.ts +17 -0
- package/dist/utils.js +73 -0
- package/dist/utils.js.map +1 -0
- package/package.json +4 -1
- package/src/durables.ts +17 -11
- package/src/index.ts +1 -0
- package/src/types/context.ts +5 -1
- package/src/types/durables.ts +29 -10
- package/src/types/invocation.ts +7 -0
- package/src/utils.ts +86 -0
package/dist/durables.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export declare function durableEventHandler(handler: DurableHandler): DurableHan
|
|
|
40
40
|
*/
|
|
41
41
|
export declare function durableEventHandler<TConfig extends {
|
|
42
42
|
name: string;
|
|
43
|
-
durableTimeout?: number;
|
|
43
|
+
durableTimeout?: string | number;
|
|
44
44
|
}>(config: TConfig, handler: DurableHandler): DurableHandler & {
|
|
45
45
|
config: TConfig;
|
|
46
46
|
};
|
package/dist/durables.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseDuration } from './utils.js';
|
|
1
2
|
/**
|
|
2
3
|
* Error type indicating an unrecoverable error in a durable function.
|
|
3
4
|
* Used to abort any remaining retry behavior in a durable step operation.
|
|
@@ -52,15 +53,18 @@ const validateConfig = (config) => {
|
|
|
52
53
|
const event = 'event' in config ? config.event : undefined;
|
|
53
54
|
if (typeof name !== 'string')
|
|
54
55
|
errors.push('`config.name` must be a string');
|
|
55
|
-
if ('durableTimeout' in config
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if ('durableTimeout' in config && typeof config.durableTimeout === 'number') {
|
|
59
|
-
if (config.durableTimeout < 60) {
|
|
60
|
-
errors.push('`config.durableTimeout` must be at least 60 seconds');
|
|
56
|
+
if ('durableTimeout' in config) {
|
|
57
|
+
if (typeof config.durableTimeout !== 'number' && typeof config.durableTimeout !== 'string') {
|
|
58
|
+
errors.push('`durableTimeout` is not a valid duration');
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const duration = typeof config.durableTimeout === 'string' ? parseDuration(config.durableTimeout, 's') : config.durableTimeout;
|
|
61
|
+
if (typeof duration === 'number') {
|
|
62
|
+
if (duration < 60) {
|
|
63
|
+
errors.push('`config.durableTimeout` must be at least 60 seconds');
|
|
64
|
+
}
|
|
65
|
+
if (duration > 31_536_000) {
|
|
66
|
+
errors.push('`config.durableTimeout` must be at most 1 year');
|
|
67
|
+
}
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
if ('event' in config) {
|
package/dist/durables.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"durables.js","sourceRoot":"","sources":["../src/durables.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"durables.js","sourceRoot":"","sources":["../src/durables.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,aAAa,EAAC,MAAM,YAAY,CAAA;AAExC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,IAAI,GAAG,oBAAoB,CAAA;CACrC;AAED;;;;;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;A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AAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACxC,CAAC;IACD,gDAAgD;IAChD,OAAO,MAAM,CAAC,MAAM,CAAC,OAAyB,EAAE,EAAC,MAAM,EAAC,CAAC,CAAA;AAC3D,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,EAAC,mBAAmB,EAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
package/dist/types/context.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { InvokeCallback } from './invocation.js';
|
|
1
|
+
import type { InvokeCallback, InvokedBy } from './invocation.js';
|
|
2
2
|
import type { ResourcesApi } from './resources.js';
|
|
3
3
|
/**
|
|
4
4
|
* The context object passed to the function handler.
|
|
@@ -12,6 +12,8 @@ export interface FunctionContext {
|
|
|
12
12
|
functionResourceType: string;
|
|
13
13
|
/** The resource ID of the function container; resource ID that houses the function. */
|
|
14
14
|
functionResourceId: string;
|
|
15
|
+
/** Optional info on the invoker of the function */
|
|
16
|
+
invokedBy?: InvokedBy;
|
|
15
17
|
/** Sanity lineage token */
|
|
16
18
|
lineage?: string | undefined;
|
|
17
19
|
/**
|
|
@@ -79,6 +81,8 @@ export interface ScheduledFunctionContext {
|
|
|
79
81
|
};
|
|
80
82
|
/** Resource interface that allows access to Blueprint Resources */
|
|
81
83
|
resources: ResourcesApi;
|
|
84
|
+
/** Invoker is never populated for scheduled functions */
|
|
85
|
+
invokedBy?: never;
|
|
82
86
|
}
|
|
83
87
|
/**
|
|
84
88
|
* The context object passed to the sync tag invalidate event handler.
|
package/dist/types/durables.d.ts
CHANGED
|
@@ -58,6 +58,11 @@ export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
|
58
58
|
/** Current attempt number, starting at 1. */
|
|
59
59
|
attempt: number;
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Using number assumes seconds, string would be an explicit value that could be parsed, the duration obj an explicit value that requires no normalizing
|
|
63
|
+
* @beta: Using durables is considered experimental and may change in the future.
|
|
64
|
+
*/
|
|
65
|
+
export type DurableDurationInput = number | string | DurableDuration;
|
|
61
66
|
/**
|
|
62
67
|
* The signature of the duration object passed to `wait`, `waitForCondition` and `waitForCallback` methods. It is also used when specifying the `retry.delay` of durable steps.
|
|
63
68
|
* It can be specified in days, hours, minutes, or seconds.
|
|
@@ -83,14 +88,24 @@ export type DurableDuration = {
|
|
|
83
88
|
*/
|
|
84
89
|
export interface DurableRetryStrategy {
|
|
85
90
|
maxAttempts?: number;
|
|
86
|
-
delay?:
|
|
91
|
+
delay?: DurableDurationInput;
|
|
87
92
|
}
|
|
88
93
|
/**
|
|
89
94
|
* @beta Using durables is considered experimental and may change in the future.
|
|
90
95
|
*/
|
|
91
96
|
export type DurableWaitForConditionDecision = {
|
|
92
97
|
shouldRetry: true;
|
|
93
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Either a number representing seconds
|
|
100
|
+
* a string of `<number> <unit>` pairs.
|
|
101
|
+
* or the DurableDuration object with seconds being required
|
|
102
|
+
* @example 10
|
|
103
|
+
* @example '10 seconds'
|
|
104
|
+
* @example '1 days 2 hours'
|
|
105
|
+
* @example '1 days 2 hours 30 minutes 15 seconds'
|
|
106
|
+
* @example `{ seconds: 15, days: 1 }`
|
|
107
|
+
*/
|
|
108
|
+
delay: DurableDurationInput;
|
|
94
109
|
} | {
|
|
95
110
|
shouldRetry: false;
|
|
96
111
|
};
|
|
@@ -368,7 +383,7 @@ export type DurableOperations = {
|
|
|
368
383
|
waitForCallback<T>({ name, submitter, timeout, retry, }: {
|
|
369
384
|
name?: string;
|
|
370
385
|
submitter: DurableStepWaitForCallbackSubmitter;
|
|
371
|
-
timeout?:
|
|
386
|
+
timeout?: DurableDurationInput;
|
|
372
387
|
retry?: DurableRetryStrategy;
|
|
373
388
|
}): Promise<T>;
|
|
374
389
|
/**
|
|
@@ -376,19 +391,20 @@ export type DurableOperations = {
|
|
|
376
391
|
* @example
|
|
377
392
|
* Wait for 30 seconds
|
|
378
393
|
* ```ts
|
|
379
|
-
* await step.wait({name: 'wait30Sec', duration:
|
|
394
|
+
* await step.wait({name: 'wait30Sec', duration: '30 seconds'});
|
|
380
395
|
* ```
|
|
381
396
|
* Wait for 2 days and 3 hours
|
|
382
397
|
* ```ts
|
|
383
|
-
* await step.wait({name: 'waitALongTime', duration:
|
|
398
|
+
* await step.wait({name: 'waitALongTime', duration: '2 days 3 hours'});
|
|
384
399
|
* ```
|
|
385
400
|
* @param name - Step name
|
|
386
401
|
* @param duration - Amount of time the function will wait before continuing.
|
|
402
|
+
* Duration is a string of `<number> <unit>` pairs. Must be at least '1 second'
|
|
387
403
|
* @returns Resolves after the duration
|
|
388
404
|
*/
|
|
389
405
|
wait({ name, duration }: {
|
|
390
406
|
name?: string;
|
|
391
|
-
duration:
|
|
407
|
+
duration: DurableDurationInput;
|
|
392
408
|
}): Promise<void>;
|
|
393
409
|
/**
|
|
394
410
|
* Waits for a specified condition to be met by executing a `poller` method to retrieve state and then inspecting the retrieved state in a `next` method to determine whether to retry and after what delay to do so.
|
|
@@ -413,9 +429,7 @@ export type DurableOperations = {
|
|
|
413
429
|
* if (state.article) return {shouldRetry: false}
|
|
414
430
|
* return {
|
|
415
431
|
* shouldRetry: true,
|
|
416
|
-
* delay:
|
|
417
|
-
* seconds: Math.min(attempt * 2, 60),
|
|
418
|
-
* },
|
|
432
|
+
* delay: '10 seconds',
|
|
419
433
|
* }
|
|
420
434
|
* },
|
|
421
435
|
* },
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { DurableDuration } from './types/durables.js';
|
|
2
|
+
type DurationUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'mo' | 'y';
|
|
3
|
+
/**
|
|
4
|
+
* Parses a duration string or number into a number in the specified unit.
|
|
5
|
+
* @param duration
|
|
6
|
+
* @param unit
|
|
7
|
+
* @return The parsed duration in the specified unit.
|
|
8
|
+
*/
|
|
9
|
+
export declare const parseDuration: (duration: string | number, unit?: DurationUnit) => number;
|
|
10
|
+
/**
|
|
11
|
+
* Converts a delay string into an aws like delay object with keys as DelayUnits and values as numbers.
|
|
12
|
+
* @param delay
|
|
13
|
+
* @return A normalized delay object with keys as DelayUnits and values as numbers.
|
|
14
|
+
* @throws Error if the delay string contains invalid units.
|
|
15
|
+
*/
|
|
16
|
+
export declare const normalizeDelay: (delay: string | Partial<DurableDuration> | number) => Partial<DurableDuration>;
|
|
17
|
+
export {};
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import parse from 'parse-duration';
|
|
2
|
+
const YEAR_IN_SEC = 31_536_000;
|
|
3
|
+
const YEAR_IN_MS = YEAR_IN_SEC * 1000;
|
|
4
|
+
const MAX_DURATION_SEC = 31_622_400;
|
|
5
|
+
// Extend the units object because of the calculations of parse-duration of year to secs - `31557600000`
|
|
6
|
+
const AWS_UNITS = Object.assign(Object.create(null), parse.unit, {
|
|
7
|
+
year: YEAR_IN_MS,
|
|
8
|
+
yr: YEAR_IN_MS,
|
|
9
|
+
y: YEAR_IN_MS,
|
|
10
|
+
month: YEAR_IN_MS / 12,
|
|
11
|
+
mth: YEAR_IN_MS / 12,
|
|
12
|
+
mo: YEAR_IN_MS / 12,
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Parses a duration string or number into a number in the specified unit.
|
|
16
|
+
* @param duration
|
|
17
|
+
* @param unit
|
|
18
|
+
* @return The parsed duration in the specified unit.
|
|
19
|
+
*/
|
|
20
|
+
export const parseDuration = (duration, unit = 's') => {
|
|
21
|
+
const resetParser = parse.unit;
|
|
22
|
+
parse.unit = AWS_UNITS;
|
|
23
|
+
try {
|
|
24
|
+
const parsed = typeof duration === 'number' ? duration : parse(duration, unit);
|
|
25
|
+
if (parsed === null || !Number.isFinite(parsed)) {
|
|
26
|
+
throw new TypeError(`Invalid duration: ${duration}`);
|
|
27
|
+
}
|
|
28
|
+
return parsed;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
parse.unit = resetParser;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Converts a delay string into an aws like delay object with keys as DelayUnits and values as numbers.
|
|
36
|
+
* @param delay
|
|
37
|
+
* @return A normalized delay object with keys as DelayUnits and values as numbers.
|
|
38
|
+
* @throws Error if the delay string contains invalid units.
|
|
39
|
+
*/
|
|
40
|
+
export const normalizeDelay = (delay) => {
|
|
41
|
+
// just return the normalized object
|
|
42
|
+
if (delay && typeof delay === 'object') {
|
|
43
|
+
return delay;
|
|
44
|
+
}
|
|
45
|
+
// If number, assume seconds.
|
|
46
|
+
// converts everything to 'ms'
|
|
47
|
+
let duration = typeof delay === 'number' ? delay * 1000 : parseDuration(delay, 'ms');
|
|
48
|
+
// Catch anything that isn't a whole number
|
|
49
|
+
if (!Number.isFinite(duration) || duration % 1000 !== 0) {
|
|
50
|
+
throw new Error('Duration must be a whole number greater then or equal to 1 second');
|
|
51
|
+
}
|
|
52
|
+
if (duration < 1000) {
|
|
53
|
+
throw new Error('Duration must be greater than or equal to 1 second');
|
|
54
|
+
}
|
|
55
|
+
if (duration > MAX_DURATION_SEC * 1000) {
|
|
56
|
+
throw new Error('Duration cannot be longer then 1 year (31_622_400 seconds)');
|
|
57
|
+
}
|
|
58
|
+
const result = {};
|
|
59
|
+
// convert final results to the proper duration units
|
|
60
|
+
for (const [key, unit] of [
|
|
61
|
+
['days', 'd'],
|
|
62
|
+
['hours', 'h'],
|
|
63
|
+
['minutes', 'm'],
|
|
64
|
+
['seconds', 's'],
|
|
65
|
+
]) {
|
|
66
|
+
const value = Math.floor(duration / AWS_UNITS[unit]);
|
|
67
|
+
if (value)
|
|
68
|
+
result[key] = value;
|
|
69
|
+
duration -= value * AWS_UNITS[unit];
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,gBAAgB,CAAA;AAKlC,MAAM,WAAW,GAAG,UAAU,CAAA;AAC9B,MAAM,UAAU,GAAG,WAAW,GAAG,IAAI,CAAA;AACrC,MAAM,gBAAgB,GAAG,UAAU,CAAA;AAEnC,wGAAwG;AACxG,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,EAAE;IAC/D,IAAI,EAAE,UAAU;IAChB,EAAE,EAAE,UAAU;IACd,CAAC,EAAE,UAAU;IAEb,KAAK,EAAE,UAAU,GAAG,EAAE;IACtB,GAAG,EAAE,UAAU,GAAG,EAAE;IACpB,EAAE,EAAE,UAAU,GAAG,EAAE;CACpB,CAAC,CAAA;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,QAAyB,EAAE,IAAI,GAAiB,GAAG,EAAU,EAAE;IAC3F,MAAM,WAAW,GAAG,KAAK,CAAC,IAAI,CAC7B;IAAC,KAAK,CAAC,IAA+B,GAAG,SAAS,CA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M,GAA2B,EAAE,CAAA;IAEzC,qDAAqD;IACrD,KAAK,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI;QACxB,CAAC,MAAM,EAAE,GAAG,CAAC;QACb,CAAC,OAAO,EAAE,GAAG,CAAC;QACd,CAAC,SAAS,EAAE,GAAG,CAAC;QAChB,CAAC,SAAS,EAAE,GAAG,CAAC;KACR,EAAE,CAAC;QACX,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAA;QACpD,IAAI,KAAK;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;QAC9B,QAAQ,IAAI,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;IACrC,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/functions",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Helper methods and type definitions for Sanity Functions",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -78,5 +78,8 @@
|
|
|
78
78
|
},
|
|
79
79
|
"allowScripts": {
|
|
80
80
|
"fsevents@2.3.3": true
|
|
81
|
+
},
|
|
82
|
+
"dependencies": {
|
|
83
|
+
"parse-duration": "^2.1.9"
|
|
81
84
|
}
|
|
82
85
|
}
|
package/src/durables.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {DurableHandler} from './types/durables.js'
|
|
2
|
+
import {parseDuration} from './utils.js'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Error type indicating an unrecoverable error in a durable function.
|
|
@@ -30,7 +31,7 @@ export class UnrecoverableError extends Error {
|
|
|
30
31
|
* @param maybeHandler
|
|
31
32
|
* @returns resolved arguments
|
|
32
33
|
*/
|
|
33
|
-
const resolveFuncArgs = <TConfig extends {name: string; durableTimeout?: number}>(
|
|
34
|
+
const resolveFuncArgs = <TConfig extends {name: string; durableTimeout?: string | number}>(
|
|
34
35
|
configOrHandler: TConfig | DurableHandler,
|
|
35
36
|
maybeHandler?: DurableHandler,
|
|
36
37
|
): {config?: TConfig; handler?: DurableHandler; hadConfigArg: boolean} => {
|
|
@@ -60,16 +61,21 @@ const validateConfig = (config: unknown) => {
|
|
|
60
61
|
const name = 'name' in config ? config.name : undefined
|
|
61
62
|
const event = 'event' in config ? config.event : undefined
|
|
62
63
|
if (typeof name !== 'string') errors.push('`config.name` must be a string')
|
|
63
|
-
if ('durableTimeout' in config && typeof config.durableTimeout !== 'number') {
|
|
64
|
-
errors.push('`config.durableTimeout` must be a number')
|
|
65
|
-
}
|
|
66
64
|
|
|
67
|
-
if ('durableTimeout' in config
|
|
68
|
-
if (config.durableTimeout
|
|
69
|
-
errors.push('`
|
|
65
|
+
if ('durableTimeout' in config) {
|
|
66
|
+
if (typeof config.durableTimeout !== 'number' && typeof config.durableTimeout !== 'string') {
|
|
67
|
+
errors.push('`durableTimeout` is not a valid duration')
|
|
70
68
|
}
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
|
|
70
|
+
const duration = typeof config.durableTimeout === 'string' ? parseDuration(config.durableTimeout, 's') : config.durableTimeout
|
|
71
|
+
if (typeof duration === 'number') {
|
|
72
|
+
if (duration < 60) {
|
|
73
|
+
errors.push('`config.durableTimeout` must be at least 60 seconds')
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (duration > 31_536_000) {
|
|
77
|
+
errors.push('`config.durableTimeout` must be at most 1 year')
|
|
78
|
+
}
|
|
73
79
|
}
|
|
74
80
|
}
|
|
75
81
|
|
|
@@ -102,7 +108,7 @@ export function durableEventHandler(handler: DurableHandler): DurableHandler & {
|
|
|
102
108
|
* @param handler
|
|
103
109
|
* @returns The handler function, unmodified and the provided config object.
|
|
104
110
|
*/
|
|
105
|
-
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: number}>(
|
|
111
|
+
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: string | number}>(
|
|
106
112
|
config: TConfig,
|
|
107
113
|
handler: DurableHandler,
|
|
108
114
|
): DurableHandler & {config: TConfig}
|
|
@@ -115,7 +121,7 @@ export function durableEventHandler<TConfig extends {name: string; durableTimeou
|
|
|
115
121
|
* @param maybeHandler
|
|
116
122
|
* @returns The handler function, unmodified and the config object if provided
|
|
117
123
|
*/
|
|
118
|
-
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: number}>(
|
|
124
|
+
export function durableEventHandler<TConfig extends {name: string; durableTimeout?: string | number}>(
|
|
119
125
|
configOrHandler: TConfig | DurableHandler,
|
|
120
126
|
maybeHandler?: DurableHandler,
|
|
121
127
|
): DurableHandler {
|
package/src/index.ts
CHANGED
package/src/types/context.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {InvokeCallback} from './invocation.js'
|
|
1
|
+
import type {InvokeCallback, InvokedBy} from './invocation.js'
|
|
2
2
|
import type {ResourcesApi} from './resources.js'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -13,6 +13,8 @@ export interface FunctionContext {
|
|
|
13
13
|
functionResourceType: string
|
|
14
14
|
/** The resource ID of the function container; resource ID that houses the function. */
|
|
15
15
|
functionResourceId: string
|
|
16
|
+
/** Optional info on the invoker of the function */
|
|
17
|
+
invokedBy?: InvokedBy
|
|
16
18
|
/** Sanity lineage token */
|
|
17
19
|
lineage?: string | undefined
|
|
18
20
|
/**
|
|
@@ -81,6 +83,8 @@ export interface ScheduledFunctionContext {
|
|
|
81
83
|
}
|
|
82
84
|
/** Resource interface that allows access to Blueprint Resources */
|
|
83
85
|
resources: ResourcesApi
|
|
86
|
+
/** Invoker is never populated for scheduled functions */
|
|
87
|
+
invokedBy?: never
|
|
84
88
|
}
|
|
85
89
|
|
|
86
90
|
/**
|
package/src/types/durables.ts
CHANGED
|
@@ -65,6 +65,12 @@ export interface DurableStepAttemptContext extends DurableStepCallbackContext {
|
|
|
65
65
|
attempt: number
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Using number assumes seconds, string would be an explicit value that could be parsed, the duration obj an explicit value that requires no normalizing
|
|
70
|
+
* @beta: Using durables is considered experimental and may change in the future.
|
|
71
|
+
*/
|
|
72
|
+
export type DurableDurationInput = number | string | DurableDuration
|
|
73
|
+
|
|
68
74
|
/**
|
|
69
75
|
* The signature of the duration object passed to `wait`, `waitForCondition` and `waitForCallback` methods. It is also used when specifying the `retry.delay` of durable steps.
|
|
70
76
|
* It can be specified in days, hours, minutes, or seconds.
|
|
@@ -81,14 +87,28 @@ export type DurableDuration =
|
|
|
81
87
|
*/
|
|
82
88
|
export interface DurableRetryStrategy {
|
|
83
89
|
maxAttempts?: number
|
|
84
|
-
|
|
85
|
-
delay?: DurableDuration
|
|
90
|
+
delay?: DurableDurationInput
|
|
86
91
|
}
|
|
87
92
|
|
|
88
93
|
/**
|
|
89
94
|
* @beta Using durables is considered experimental and may change in the future.
|
|
90
95
|
*/
|
|
91
|
-
export type DurableWaitForConditionDecision =
|
|
96
|
+
export type DurableWaitForConditionDecision =
|
|
97
|
+
| {
|
|
98
|
+
shouldRetry: true
|
|
99
|
+
/**
|
|
100
|
+
* Either a number representing seconds
|
|
101
|
+
* a string of `<number> <unit>` pairs.
|
|
102
|
+
* or the DurableDuration object with seconds being required
|
|
103
|
+
* @example 10
|
|
104
|
+
* @example '10 seconds'
|
|
105
|
+
* @example '1 days 2 hours'
|
|
106
|
+
* @example '1 days 2 hours 30 minutes 15 seconds'
|
|
107
|
+
* @example `{ seconds: 15, days: 1 }`
|
|
108
|
+
*/
|
|
109
|
+
delay: DurableDurationInput
|
|
110
|
+
}
|
|
111
|
+
| {shouldRetry: false}
|
|
92
112
|
|
|
93
113
|
/**
|
|
94
114
|
* @beta Using durables is considered experimental and may change in the future.
|
|
@@ -411,7 +431,7 @@ export type DurableOperations = {
|
|
|
411
431
|
}: {
|
|
412
432
|
name?: string
|
|
413
433
|
submitter: DurableStepWaitForCallbackSubmitter
|
|
414
|
-
timeout?:
|
|
434
|
+
timeout?: DurableDurationInput
|
|
415
435
|
retry?: DurableRetryStrategy
|
|
416
436
|
}): Promise<T>
|
|
417
437
|
|
|
@@ -420,17 +440,18 @@ export type DurableOperations = {
|
|
|
420
440
|
* @example
|
|
421
441
|
* Wait for 30 seconds
|
|
422
442
|
* ```ts
|
|
423
|
-
* await step.wait({name: 'wait30Sec', duration:
|
|
443
|
+
* await step.wait({name: 'wait30Sec', duration: '30 seconds'});
|
|
424
444
|
* ```
|
|
425
445
|
* Wait for 2 days and 3 hours
|
|
426
446
|
* ```ts
|
|
427
|
-
* await step.wait({name: 'waitALongTime', duration:
|
|
447
|
+
* await step.wait({name: 'waitALongTime', duration: '2 days 3 hours'});
|
|
428
448
|
* ```
|
|
429
449
|
* @param name - Step name
|
|
430
450
|
* @param duration - Amount of time the function will wait before continuing.
|
|
451
|
+
* Duration is a string of `<number> <unit>` pairs. Must be at least '1 second'
|
|
431
452
|
* @returns Resolves after the duration
|
|
432
453
|
*/
|
|
433
|
-
wait({name, duration}: {name?: string; duration:
|
|
454
|
+
wait({name, duration}: {name?: string; duration: DurableDurationInput}): Promise<void>
|
|
434
455
|
|
|
435
456
|
/**
|
|
436
457
|
* Waits for a specified condition to be met by executing a `poller` method to retrieve state and then inspecting the retrieved state in a `next` method to determine whether to retry and after what delay to do so.
|
|
@@ -455,9 +476,7 @@ export type DurableOperations = {
|
|
|
455
476
|
* if (state.article) return {shouldRetry: false}
|
|
456
477
|
* return {
|
|
457
478
|
* shouldRetry: true,
|
|
458
|
-
* delay:
|
|
459
|
-
* seconds: Math.min(attempt * 2, 60),
|
|
460
|
-
* },
|
|
479
|
+
* delay: '10 seconds',
|
|
461
480
|
* }
|
|
462
481
|
* },
|
|
463
482
|
* },
|
package/src/types/invocation.ts
CHANGED
package/src/utils.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import parse from 'parse-duration'
|
|
2
|
+
import type {DurableDuration} from './types/durables.js'
|
|
3
|
+
|
|
4
|
+
type DurationUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'mo' | 'y'
|
|
5
|
+
|
|
6
|
+
const YEAR_IN_SEC = 31_536_000
|
|
7
|
+
const YEAR_IN_MS = YEAR_IN_SEC * 1000
|
|
8
|
+
const MAX_DURATION_SEC = 31_622_400
|
|
9
|
+
|
|
10
|
+
// Extend the units object because of the calculations of parse-duration of year to secs - `31557600000`
|
|
11
|
+
const AWS_UNITS = Object.assign(Object.create(null), parse.unit, {
|
|
12
|
+
year: YEAR_IN_MS,
|
|
13
|
+
yr: YEAR_IN_MS,
|
|
14
|
+
y: YEAR_IN_MS,
|
|
15
|
+
|
|
16
|
+
month: YEAR_IN_MS / 12,
|
|
17
|
+
mth: YEAR_IN_MS / 12,
|
|
18
|
+
mo: YEAR_IN_MS / 12,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Parses a duration string or number into a number in the specified unit.
|
|
23
|
+
* @param duration
|
|
24
|
+
* @param unit
|
|
25
|
+
* @return The parsed duration in the specified unit.
|
|
26
|
+
*/
|
|
27
|
+
export const parseDuration = (duration: string | number, unit: DurationUnit = 's'): number => {
|
|
28
|
+
const resetParser = parse.unit
|
|
29
|
+
;(parse.unit as Record<string, number>) = AWS_UNITS
|
|
30
|
+
try {
|
|
31
|
+
const parsed = typeof duration === 'number' ? duration : parse(duration, unit)
|
|
32
|
+
|
|
33
|
+
if (parsed === null || !Number.isFinite(parsed)) {
|
|
34
|
+
throw new TypeError(`Invalid duration: ${duration}`)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return parsed
|
|
38
|
+
} finally {
|
|
39
|
+
parse.unit = resetParser
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Converts a delay string into an aws like delay object with keys as DelayUnits and values as numbers.
|
|
45
|
+
* @param delay
|
|
46
|
+
* @return A normalized delay object with keys as DelayUnits and values as numbers.
|
|
47
|
+
* @throws Error if the delay string contains invalid units.
|
|
48
|
+
*/
|
|
49
|
+
export const normalizeDelay = (delay: string | Partial<DurableDuration> | number): Partial<DurableDuration> => {
|
|
50
|
+
// just return the normalized object
|
|
51
|
+
if (delay && typeof delay === 'object') {
|
|
52
|
+
return delay
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// If number, assume seconds.
|
|
56
|
+
// converts everything to 'ms'
|
|
57
|
+
let duration = typeof delay === 'number' ? delay * 1000 : parseDuration(delay, 'ms')
|
|
58
|
+
// Catch anything that isn't a whole number
|
|
59
|
+
if (!Number.isFinite(duration) || duration % 1000 !== 0) {
|
|
60
|
+
throw new Error('Duration must be a whole number greater then or equal to 1 second')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (duration < 1000) {
|
|
64
|
+
throw new Error('Duration must be greater than or equal to 1 second')
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (duration > MAX_DURATION_SEC * 1000) {
|
|
68
|
+
throw new Error('Duration cannot be longer then 1 year (31_622_400 seconds)')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const result: Record<string, number> = {}
|
|
72
|
+
|
|
73
|
+
// convert final results to the proper duration units
|
|
74
|
+
for (const [key, unit] of [
|
|
75
|
+
['days', 'd'],
|
|
76
|
+
['hours', 'h'],
|
|
77
|
+
['minutes', 'm'],
|
|
78
|
+
['seconds', 's'],
|
|
79
|
+
] as const) {
|
|
80
|
+
const value = Math.floor(duration / AWS_UNITS[unit])
|
|
81
|
+
if (value) result[key] = value
|
|
82
|
+
duration -= value * AWS_UNITS[unit]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return result
|
|
86
|
+
}
|