-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
197 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Publish Any Commit | ||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- run: corepack enable | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 20 | ||
|
||
- name: Install dependencies | ||
run: npm install | ||
|
||
- name: Build | ||
run: npm run prepublishOnly --if-present | ||
|
||
- run: npx pkg-pr-new publish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export class TimeoutError extends Error { | ||
timeout: number; | ||
|
||
constructor(timeout: number) { | ||
super(`Timed out after ${timeout}ms`); | ||
this.name = this.constructor.name; | ||
this.timeout = timeout; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
// https://betterstack.com/community/guides/scaling-nodejs/nodejs-timeouts/ | ||
export async function promiseTimeout<T>( | ||
promiseArg: Promise<T>, | ||
timeout: number, | ||
): Promise<T> { | ||
let timer: NodeJS.Timeout; | ||
const timeoutPromise = new Promise<never>((_, reject) => { | ||
timer = setTimeout(() => { | ||
reject(new TimeoutError(timeout)); | ||
}, timeout); | ||
}); | ||
|
||
try { | ||
return await Promise.race([ promiseArg, timeoutPromise ]); | ||
} finally { | ||
clearTimeout(timer!); | ||
} | ||
} | ||
|
||
export async function runWithTimeout<T>( | ||
scope: () => Promise<T>, | ||
timeout: number, | ||
): Promise<T> { | ||
return await promiseTimeout(scope(), timeout); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import * as utility from '../src/index.js'; | ||
import { runWithTimeout, TimeoutError, promiseTimeout } from '../src/index.js'; | ||
|
||
function sleep(ms: number) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
describe('test/timeout.test.ts', () => { | ||
describe('runWithTimeout()', () => { | ||
it('should timeout', async () => { | ||
await assert.rejects(async () => { | ||
await runWithTimeout(async () => { | ||
await sleep(20); | ||
}, 10); | ||
}, (err: unknown) => { | ||
assert(err instanceof TimeoutError); | ||
assert.equal(err.timeout, 10); | ||
assert.equal(err.message, 'Timed out after 10ms'); | ||
// console.error(err); | ||
return true; | ||
}); | ||
|
||
await assert.rejects(async () => { | ||
await utility.runWithTimeout(async () => { | ||
await sleep(1000); | ||
}, 15); | ||
}, (err: unknown) => { | ||
assert(err instanceof TimeoutError); | ||
assert.equal(err.timeout, 15); | ||
assert.equal(err.message, 'Timed out after 15ms'); | ||
// console.error(err); | ||
return true; | ||
}); | ||
}); | ||
|
||
it('should timeout', async () => { | ||
const result = await runWithTimeout(async () => { | ||
await sleep(20); | ||
return 100000; | ||
}, 100); | ||
assert.equal(result, 100000); | ||
}); | ||
}); | ||
|
||
describe('promiseTimeout()', () => { | ||
it('should timeout', async () => { | ||
await assert.rejects(async () => { | ||
await promiseTimeout(sleep(20), 10); | ||
}, (err: unknown) => { | ||
assert(err instanceof TimeoutError); | ||
assert.equal(err.timeout, 10); | ||
assert.equal(err.message, 'Timed out after 10ms'); | ||
// console.error(err); | ||
return true; | ||
}); | ||
}); | ||
}); | ||
}); |