Skip to content

Commit 9edc469

Browse files
authored
feat!: Protect against ms durations errors (#1136)
1 parent 78a6d9b commit 9edc469

21 files changed

+132
-79
lines changed

package-lock.json

Lines changed: 47 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/activity/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
import 'abort-controller/polyfill'; // eslint-disable-line import/no-unassigned-import
7373
import { AsyncLocalStorage } from 'node:async_hooks';
74-
import { msToNumber } from '@temporalio/common/lib/time';
74+
import { Duration, msToNumber } from '@temporalio/common/lib/time';
7575

7676
export {
7777
ActivityFunction,
@@ -279,7 +279,7 @@ export class Context {
279279
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
280280
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
281281
*/
282-
public sleep(ms: number | string): Promise<void> {
282+
public sleep(ms: Duration): Promise<void> {
283283
let handle: NodeJS.Timeout;
284284
const timer = new Promise<void>((resolve) => {
285285
handle = setTimeout(resolve, msToNumber(ms));

packages/client/src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AsyncLocalStorage } from 'node:async_hooks';
22
import * as grpc from '@grpc/grpc-js';
33
import type { RPCImpl } from 'protobufjs';
44
import { filterNullAndUndefined, normalizeTlsConfig, TLSConfig } from '@temporalio/common/lib/internal-non-workflow';
5-
import { msOptionalToNumber } from '@temporalio/common/lib/time';
5+
import { Duration, msOptionalToNumber } from '@temporalio/common/lib/time';
66
import { isServerErrorResponse, ServiceError } from './errors';
77
import { defaultGrpcRetryOptions, makeGrpcRetryInterceptor } from './grpc-retry';
88
import pkg from './pkg';
@@ -81,7 +81,7 @@ export interface ConnectionOptions {
8181
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
8282
* @default 10 seconds
8383
*/
84-
connectTimeout?: number | string;
84+
connectTimeout?: Duration;
8585
}
8686

8787
export type ConnectionOptionsWithDefaults = Required<Omit<ConnectionOptions, 'tls' | 'connectTimeout'>> & {

packages/client/src/schedule-types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { checkExtends, Replace } from '@temporalio/common/lib/type-helpers';
2-
import { SearchAttributes, Workflow } from '@temporalio/common';
2+
import { Duration, SearchAttributes, Workflow } from '@temporalio/common';
33
import type { temporal } from '@temporalio/proto';
44
import { WorkflowStartOptions } from './workflow-options';
55

@@ -46,7 +46,7 @@ export interface ScheduleOptions<A extends ScheduleOptionsAction = ScheduleOptio
4646
* @default 1 minute
4747
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
4848
*/
49-
catchupWindow?: number | string;
49+
catchupWindow?: Duration;
5050

5151
/**
5252
* When an Action times out or reaches the end of its Retry Policy, {@link pause}.
@@ -465,7 +465,7 @@ export interface ScheduleSpec {
465465
* @default 0
466466
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
467467
*/
468-
jitter?: number | string;
468+
jitter?: Duration;
469469

470470
/**
471471
* IANA timezone name, for example `US/Pacific`.
@@ -659,15 +659,15 @@ export interface IntervalSpec {
659659
*
660660
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
661661
*/
662-
every: number | string;
662+
every: Duration;
663663

664664
/**
665665
* Value is rounded to the nearest second.
666666
*
667667
* @default 0
668668
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
669669
*/
670-
offset?: number | string;
670+
offset?: Duration;
671671
}
672672

673673
/**

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"@opentelemetry/api": "^1.4.1",
1616
"@temporalio/proto": "file:../proto",
1717
"long": "^5.2.0",
18-
"ms": "^2.1.3",
18+
"ms": "^3.0.0-canary.1",
1919
"proto3-json-serializer": "^1.0.3",
2020
"protobufjs": "^7.0.0"
2121
},

packages/common/src/activity-options.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { coresdk } from '@temporalio/proto';
22
import { RetryPolicy } from './retry-policy';
33
import { checkExtends } from './type-helpers';
4+
import { Duration } from './time';
45

56
// Avoid importing the proto implementation to reduce workflow bundle size
67
// Copied from coresdk.workflow_commands.ActivityCancellationType
@@ -37,7 +38,7 @@ export interface ActivityOptions {
3738
* Heartbeat interval. Activity must heartbeat before this interval passes after a last heartbeat or activity start.
3839
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
3940
*/
40-
heartbeatTimeout?: string | number;
41+
heartbeatTimeout?: Duration;
4142

4243
/**
4344
* RetryPolicy that define how activity is retried in case of failure. If this is not set, then the server-defined default activity retry policy will be used. To ensure zero retries, set maximum attempts to 1.
@@ -56,7 +57,7 @@ export interface ActivityOptions {
5657
* @default `scheduleToCloseTimeout` or unlimited
5758
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
5859
*/
59-
startToCloseTimeout?: string | number;
60+
startToCloseTimeout?: Duration;
6061

6162
/**
6263
* Time that the Activity Task can stay in the Task Queue before it is picked up by a Worker. Do not specify this timeout unless using host specific Task Queues for Activity Tasks are being used for routing.
@@ -65,7 +66,7 @@ export interface ActivityOptions {
6566
* @default `scheduleToCloseTimeout` or unlimited
6667
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
6768
*/
68-
scheduleToStartTimeout?: string | number;
69+
scheduleToStartTimeout?: Duration;
6970

7071
/**
7172
* Total time that a workflow is willing to wait for Activity to complete.
@@ -76,7 +77,7 @@ export interface ActivityOptions {
7677
* @default unlimited
7778
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
7879
*/
79-
scheduleToCloseTimeout?: string | number;
80+
scheduleToCloseTimeout?: Duration;
8081

8182
/**
8283
* Determines what the SDK does when the Activity is cancelled.
@@ -120,7 +121,7 @@ export interface LocalActivityOptions {
120121
*
121122
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
122123
*/
123-
startToCloseTimeout?: string | number;
124+
startToCloseTimeout?: Duration;
124125

125126
/**
126127
* Limits time the local activity can idle internally before being executed. That can happen if
@@ -132,7 +133,7 @@ export interface LocalActivityOptions {
132133
* @default unlimited
133134
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
134135
*/
135-
scheduleToStartTimeout?: string | number;
136+
scheduleToStartTimeout?: Duration;
136137

137138
/**
138139
* Indicates how long the caller is willing to wait for local activity completion. Limits how
@@ -144,7 +145,7 @@ export interface LocalActivityOptions {
144145
* @default unlimited
145146
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
146147
*/
147-
scheduleToCloseTimeout?: string | number;
148+
scheduleToCloseTimeout?: Duration;
148149

149150
/**
150151
* If the activity is retrying and backoff would exceed this value, a server side timer will be scheduled for the next attempt.
@@ -153,7 +154,7 @@ export interface LocalActivityOptions {
153154
* @default 1 minute
154155
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
155156
**/
156-
localRetryThreshold?: string | number;
157+
localRetryThreshold?: Duration;
157158

158159
/**
159160
* Determines what the SDK does when the Activity is cancelled.

packages/common/src/deprecated-time.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as time from './time';
2-
import { Timestamp } from './time';
2+
import { type Timestamp, Duration } from './time';
33

44
/**
55
* Lossy conversion function from Timestamp to number due to possible overflow.
@@ -35,31 +35,31 @@ export function msNumberToTs(millis: number): Timestamp {
3535
* @hidden
3636
* @deprecated - meant for internal use only
3737
*/
38-
export function msToTs(str: string | number): Timestamp {
38+
export function msToTs(str: Duration): Timestamp {
3939
return time.msToTs(str);
4040
}
4141

4242
/**
4343
* @hidden
4444
* @deprecated - meant for internal use only
4545
*/
46-
export function msOptionalToTs(str: string | number | undefined): Timestamp | undefined {
46+
export function msOptionalToTs(str: Duration | undefined): Timestamp | undefined {
4747
return time.msOptionalToTs(str);
4848
}
4949

5050
/**
5151
* @hidden
5252
* @deprecated - meant for internal use only
5353
*/
54-
export function msOptionalToNumber(val: string | number | undefined): number | undefined {
54+
export function msOptionalToNumber(val: Duration | undefined): number | undefined {
5555
return time.msOptionalToNumber(val);
5656
}
5757

5858
/**
5959
* @hidden
6060
* @deprecated - meant for internal use only
6161
*/
62-
export function msToNumber(val: string | number): number {
62+
export function msToNumber(val: Duration): number {
6363
return time.msToNumber(val);
6464
}
6565

packages/common/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export * from './failure';
1919
export { Headers, Next } from './interceptors';
2020
export * from './interfaces';
2121
export * from './retry-policy';
22-
export { Timestamp } from './time';
22+
export { type Timestamp, Duration } from './time';
2323
export * from './workflow-handle';
2424
export * from './workflow-options';
2525

packages/common/src/retry-policy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { temporal } from '@temporalio/proto';
22
import { ValueError } from './errors';
3-
import { msOptionalToNumber, msOptionalToTs, msToNumber, msToTs, optionalTsToMs } from './time';
3+
import { Duration, msOptionalToNumber, msOptionalToTs, msToNumber, msToTs, optionalTsToMs } from './time';
44

55
/**
66
* Options for retrying Workflows and Activities
@@ -19,7 +19,7 @@ export interface RetryPolicy {
1919
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
2020
* @default 1 second
2121
*/
22-
initialInterval?: string | number;
22+
initialInterval?: Duration;
2323
/**
2424
* Maximum number of attempts. When exceeded, retries stop (even if {@link ActivityOptions.scheduleToCloseTimeout}
2525
* hasn't been reached).
@@ -35,7 +35,7 @@ export interface RetryPolicy {
3535
* @default 100x of {@link initialInterval}
3636
* @format number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
3737
*/
38-
maximumInterval?: string | number;
38+
maximumInterval?: Duration;
3939

4040
/**
4141
* List of application failures types to not retry.

0 commit comments

Comments
 (0)