diff --git a/package.json b/package.json index abba1a4..79a4ba4 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "ISC", "dependencies": { "better-sqlite3": "^11.0.0", - "ms": "^2.1.3", + "ms": "3.0.0-canary.1", "serialize-error": "^11.0.3", "zod": "3.23.8" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9f5961e..c6056dc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: ^11.0.0 version: 11.0.0 ms: - specifier: ^2.1.3 - version: 2.1.3 + specifier: 3.0.0-canary.1 + version: 3.0.0-canary.1 node-cron: specifier: '>=3.0.0' version: 3.0.3 @@ -267,8 +267,9 @@ packages: mkdirp-classic@0.5.3: resolution: {integrity: sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==} - ms@2.1.3: - resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + ms@3.0.0-canary.1: + resolution: {integrity: sha512-kh8ARjh8rMN7Du2igDRO9QJnqCb2xYTJxyQYK7vJJS4TvLLmsbyhiKpSW+t+y26gyOyMd0riphX0GeWKU3ky5g==} + engines: {node: '>=12.13'} napi-build-utils@1.0.2: resolution: {integrity: sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg==} @@ -538,7 +539,7 @@ snapshots: mkdirp-classic@0.5.3: {} - ms@2.1.3: {} + ms@3.0.0-canary.1: {} napi-build-utils@1.0.2: {} diff --git a/src/v3/lib/job.ts b/src/v3/lib/job.ts index 54d7953..7f91012 100644 --- a/src/v3/lib/job.ts +++ b/src/v3/lib/job.ts @@ -4,6 +4,7 @@ import { Pipe, type PipeInto } from "./pipe" import { execution, registration, type ExecutionContext, type RegistrationContext } from "./context" import type { Step, Task } from "./storage" import { hydrateError, interrupt, isInterrupt, isPromise, NonRecoverableError, serialize } from "./utils" +import parseMs, { type StringValue as DurationString } from 'ms' type CancelReason = | { type: 'timeout', ms: number } @@ -182,8 +183,9 @@ export class Job< } /** @public */ - static sleep(ms: number): Promise { + static sleep(ms: number | DurationString): Promise { const e = getExecutionContext() + if (typeof ms === 'string') ms = parseMs(ms) return e.sleep(ms) } diff --git a/src/v3/lib/ms.d.ts b/src/v3/lib/ms.d.ts new file mode 100644 index 0000000..2cc6e8f --- /dev/null +++ b/src/v3/lib/ms.d.ts @@ -0,0 +1,21 @@ +declare module 'ms' { + declare type Unit = 'Years' | 'Year' | 'Yrs' | 'Yr' | 'Y' | 'Weeks' | 'Week' | 'W' | 'Days' | 'Day' | 'D' | 'Hours' | 'Hour' | 'Hrs' | 'Hr' | 'H' | 'Minutes' | 'Minute' | 'Mins' | 'Min' | 'M' | 'Seconds' | 'Second' | 'Secs' | 'Sec' | 's' | 'Milliseconds' | 'Millisecond' | 'Msecs' | 'Msec' | 'Ms' + declare type UnitAnyCase = Unit | Uppercase | Lowercase + export declare type StringValue = `${number}` | `${number}${UnitAnyCase}` | `${number} ${UnitAnyCase}` + interface Options { + /** + * Set to `true` to use verbose formatting. Defaults to `false`. + */ + long?: boolean + } + /** + * Parse or format the given `val`. + * + * @param value - The string or number to convert + * @param options - Options for the conversion + * @throws Error if `value` is not a non-empty string or a number + */ + declare function ms(value: StringValue, options?: Options): number + declare function ms(value: number, options?: Options): string + export default ms +} \ No newline at end of file diff --git a/src/v3/tests/step.test.ts b/src/v3/tests/step.test.ts index f53704b..219398f 100644 --- a/src/v3/tests/step.test.ts +++ b/src/v3/tests/step.test.ts @@ -11,7 +11,7 @@ test('sleep', async (t) => { const aaa = new Job({ id: 'aaa', }, async () => { - await Job.sleep(100) + await Job.sleep("100 ms") }) const db = new Database() diff --git a/src/v3/userland/bar.ts b/src/v3/userland/bar.ts index 673c3e0..01381cd 100644 --- a/src/v3/userland/bar.ts +++ b/src/v3/userland/bar.ts @@ -6,7 +6,7 @@ export const bar = new Job({ id: 'bar', }, async (input: { name: string }) => { - await Job.sleep(10) + await Job.sleep("10 ms") fooBarPipe.dispatch({ id: 1 }) diff --git a/src/v3/userland/foo.ts b/src/v3/userland/foo.ts index e6b4119..5a13148 100644 --- a/src/v3/userland/foo.ts +++ b/src/v3/userland/foo.ts @@ -22,7 +22,7 @@ export const foo = new Job({ const data = await Job.waitFor(fooBarPipe) - await Job.sleep(10) + await Job.sleep("1s") return a }) \ No newline at end of file