Skip to content

Commit 67edb0e

Browse files
committed
chore: reduce dependencies
1 parent c5c6648 commit 67edb0e

4 files changed

Lines changed: 67 additions & 5 deletions

File tree

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
"chai-string": "^1.5.0",
107107
"concurrently": "^8.0.1",
108108
"cronometro": "^1.0.5",
109-
"delay": "^5.0.0",
110109
"dns-packet": "^5.4.0",
111110
"docsify-cli": "^4.4.3",
112111
"form-data": "^4.0.0",
@@ -119,8 +118,6 @@
119118
"jsfuzz": "^1.0.15",
120119
"mitata": "^0.1.6",
121120
"mocha": "^10.0.0",
122-
"mockttp": "^3.9.2",
123-
"p-timeout": "^3.2.0",
124121
"pre-commit": "^1.2.2",
125122
"proxy": "^1.0.2",
126123
"proxyquire": "^2.1.3",

test/node-fetch/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const crypto = require('crypto')
1010
const chaiPromised = require('chai-as-promised')
1111
const chaiIterator = require('chai-iterator')
1212
const chaiString = require('chai-string')
13-
const delay = require('delay')
13+
const { delay } = require('../utils/timeout')
1414
const { Blob } = require('buffer')
1515

1616
const {

test/node-fetch/utils/chai-timeout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pTimeout = require('p-timeout')
1+
const { pTimeout } = require('../../utils/timeout')
22

33
module.exports = ({ Assertion }, utils) => {
44
utils.addProperty(Assertion.prototype, 'timeout', async function () {

test/utils/timeout.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict'
2+
3+
/**
4+
* @type {<T = void, R = T>(time: number, options?: { promise?: Promise<T>; fallback?: () => R | Promise<R>; signal?: AbortSignal; rejection?: boolean; }) => Promise<T | Awaited<R>>}
5+
*/
6+
async function timeout (time, options = {}) {
7+
let timer
8+
/** @type {() => void} */
9+
let abort
10+
const { promise, fallback, signal, rejection } = options
11+
if (signal?.aborted) {
12+
throw new DOMException('The operation was aborted.', 'AbortError')
13+
}
14+
const delay = new Promise((resolve, reject) => {
15+
abort = () => {
16+
clearTimeout(timer)
17+
reject(
18+
signal?.reason ??
19+
new DOMException('The operation was aborted.', 'AbortError')
20+
)
21+
}
22+
signal?.addEventListener('abort', abort)
23+
timer = setTimeout(() => {
24+
if (typeof fallback === 'function') {
25+
try {
26+
resolve(fallback())
27+
} catch (err) {
28+
reject(err)
29+
}
30+
} else {
31+
if (rejection === false) {
32+
resolve(undefined)
33+
} else {
34+
reject(new DOMException('The operation was aborted.', 'AbortError'))
35+
}
36+
}
37+
}, time)
38+
})
39+
try {
40+
return promise ? await Promise.race([promise, delay]) : await delay
41+
} finally {
42+
clearTimeout(timer)
43+
signal?.removeEventListener('abort', abort)
44+
}
45+
}
46+
47+
/**
48+
* @type {<T, R = T>(promise: Promise<T>, time: number, fallback?: () => R | Promise<R>) => Promise<Awaited<R> | T>}
49+
*/
50+
function pTimeout (promise, time, fallback) {
51+
return timeout(time, { promise, fallback })
52+
}
53+
54+
/**
55+
* @param {number} time
56+
*/
57+
function delay (time) {
58+
return timeout(time, { rejection: false })
59+
}
60+
61+
module.exports = {
62+
timeout,
63+
delay,
64+
pTimeout
65+
}

0 commit comments

Comments
 (0)