├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | steps: 16 | - uses: actions/checkout@v3 17 | - uses: actions/setup-node@v3 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Create an AbortSignal that aborts after a delay. 3 | 4 | @param timeout - The milliseconds to wait. 5 | 6 | @example 7 | ``` 8 | import timeoutSignal from 'timeout-signal'; 9 | 10 | const signal = timeoutSignal(5000); 11 | 12 | try { 13 | const response = await fetch('https://www.google.com', {signal}); 14 | // Handle response 15 | } catch (error) { 16 | if (signal.aborted) { 17 | // Handle abortion 18 | } 19 | } 20 | ``` 21 | */ 22 | export default function timeoutSignal(timeout: number): AbortSignal; 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function timeoutSignal(timeout) { 2 | if (!Number.isInteger(timeout)) { 3 | throw new TypeError('Expected an integer'); 4 | } 5 | 6 | const controller = new AbortController(); 7 | 8 | const timeoutId = setTimeout(() => { 9 | controller.abort(); 10 | }, timeout); 11 | 12 | // Allow Node.js processes to exit early if only the timeout is running 13 | timeoutId?.unref?.(); 14 | 15 | return controller.signal; 16 | } 17 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import timeoutSignal from './index.js'; 3 | 4 | expectType(timeoutSignal(5000)); 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020, 2022 Richie Bendall 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "timeout-signal", 3 | "version": "2.0.0", 4 | "description": "Create an AbortSignal that aborts after a delay", 5 | "license": "MIT", 6 | "repository": "node-fetch/timeout-signal", 7 | "author": { 8 | "name": "Richie Bendall", 9 | "email": "richiebendall@gmail.com" 10 | }, 11 | "type": "module", 12 | "exports": "./index.js", 13 | "engines": { 14 | "node": ">=16" 15 | }, 16 | "scripts": { 17 | "test": "xo && ava && tsd" 18 | }, 19 | "files": [ 20 | "index.js", 21 | "index.d.ts" 22 | ], 23 | "keywords": [ 24 | "timeout", 25 | "signal", 26 | "abort" 27 | ], 28 | "devDependencies": { 29 | "ava": "^5.0.1", 30 | "p-event": "^5.0.1", 31 | "time-span": "^5.1.0", 32 | "tsd": "^0.24.1", 33 | "xo": "^0.52.4" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # timeout-signal 2 | 3 | > Create an AbortSignal that aborts after a delay 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install timeout-signal 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import timeoutSignal from 'timeout-signal'; 15 | 16 | const signal = timeoutSignal(5000); 17 | 18 | try { 19 | const response = await fetch('https://www.google.com', {signal}); 20 | // Handle response 21 | } catch (error) { 22 | if (signal.aborted) { 23 | // Handle abortion 24 | } 25 | } 26 | ``` 27 | 28 | ## API 29 | 30 | ### timeoutSignal(timeout) 31 | 32 | #### timeout 33 | 34 | Type: `integer` 35 | 36 | The milliseconds to wait. 37 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {pEvent} from 'p-event'; 3 | import timeSpan from 'time-span'; 4 | import timeoutSignal from './index.js'; 5 | 6 | test('main', async t => { 7 | t.timeout(500); 8 | 9 | const signal = timeoutSignal(250); 10 | const end = timeSpan(); 11 | 12 | t.false(signal.aborted); 13 | 14 | await pEvent(signal, 'abort'); 15 | 16 | t.true(signal.aborted); 17 | t.true(end() < 500); 18 | }); 19 | --------------------------------------------------------------------------------