Skip to content

Commit

Permalink
chore: reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx committed Dec 21, 2023
1 parent c5c6648 commit c34bdd9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
"chai-string": "^1.5.0",
"concurrently": "^8.0.1",
"cronometro": "^1.0.5",
"delay": "^5.0.0",
"dns-packet": "^5.4.0",
"docsify-cli": "^4.4.3",
"form-data": "^4.0.0",
Expand All @@ -119,8 +118,6 @@
"jsfuzz": "^1.0.15",
"mitata": "^0.1.6",
"mocha": "^10.0.0",
"mockttp": "^3.9.2",
"p-timeout": "^3.2.0",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"proxyquire": "^2.1.3",
Expand Down
2 changes: 1 addition & 1 deletion test/node-fetch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const crypto = require('crypto')
const chaiPromised = require('chai-as-promised')
const chaiIterator = require('chai-iterator')
const chaiString = require('chai-string')
const delay = require('delay')
const { delay } = require('../utils/timeout')
const { Blob } = require('buffer')

const {
Expand Down
2 changes: 1 addition & 1 deletion test/node-fetch/utils/chai-timeout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pTimeout = require('p-timeout')
const { pTimeout } = require('../../utils/timeout')

module.exports = ({ Assertion }, utils) => {
utils.addProperty(Assertion.prototype, 'timeout', async function () {
Expand Down
65 changes: 65 additions & 0 deletions test/utils/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

/**
* @type {<T = void, R = T>(time: number, options?: { promise?: Promise<T>; fallback?: () => R | Promise<R>; signal?: AbortSignal; rejection?: boolean; }) => Promise<T | Awaited<R>>}
*/
async function timeout (time, options = {}) {
let timer
/** @type {() => void} */
let abort
const { promise, fallback, signal, rejection } = options
if (signal?.aborted) {
throw new DOMException('The operation was aborted.', 'AbortError')
}
const delay = new Promise((resolve, reject) => {
abort = () => {
clearTimeout(timer)
reject(
signal?.reason ??
new DOMException('The operation was aborted.', 'AbortError')
)
}
signal?.addEventListener('abort', abort)
timer = setTimeout(() => {
if (typeof fallback === 'function') {
try {
resolve(fallback())
} catch (err) {
reject(err)
}
} else {
if (rejection === false) {
resolve(undefined)
} else {
reject(new DOMException('The operation was aborted.', 'AbortError'))
}
}
}, time)
})
try {
return promise ? await Promise.race([promise, delay]) : await delay
} finally {
clearTimeout(timer)
signal?.removeEventListener('abort', abort)
}
}

/**
* @type {<T, R = T>(promise: Promise<T>, time: number, fallback?: () => R | Promise<R>) => Promise<Awaited<R> | T>}
*/
function pTimeout (promise, time, fallback) {
return timeout(time, { promise, fallback })
}

/**
* @param {number} time
*/
function delay (time) {
return timeout(time, { rejection: false })
}

module.exports = {
timeout,
delay,
pTimeout
}

0 comments on commit c34bdd9

Please sign in to comment.