Skip to content

Commit 4d5f7d9

Browse files
committed
deps: @gar/promise-retry@1.0.3
1 parent 8dcfe69 commit 4d5f7d9

File tree

14 files changed

+158
-413
lines changed

14 files changed

+158
-413
lines changed

DEPENDENCIES.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ graph LR;
270270
debug-->ms;
271271
fdir-->picomatch;
272272
fs-minipass-->minipass;
273-
gar-promise-retry-->retry;
274273
glob-->minimatch;
275274
glob-->minipass;
276275
glob-->path-scurry;

node_modules/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
!/qrcode-terminal
124124
!/read-cmd-shim
125125
!/read
126-
!/retry
127126
!/safer-buffer
128127
!/semver
129128
!/signal-exit

node_modules/@gar/promise-retry/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Copyright (c) 2011 Tim Koschützki (tim@debuggable.com), Felix Geisendörfer (felix@debuggable.com)
12
Copyright (c) 2014 IndigoUnited
23

34
Permission is hereby granted, free of charge, to any person obtaining a copy

node_modules/@gar/promise-retry/lib/index.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1-
const retry = require('retry')
1+
const { RetryOperation } = require('./retry')
22

3-
const isRetryError = (err) => err?.code === 'EPROMISERETRY' && Object.hasOwn(err, 'retried')
3+
const createTimeout = (attempt, opts) => Math.min(Math.round((1 + (opts.randomize ? Math.random() : 0)) * Math.max(opts.minTimeout, 1) * Math.pow(opts.factor, attempt)), opts.maxTimeout)
4+
const isRetryError = err => err?.code === 'EPROMISERETRY' && Object.hasOwn(err, 'retried')
45

5-
async function promiseRetry (fn, options = {}) {
6-
const operation = retry.operation(options)
6+
const promiseRetry = async (fn, options = {}) => {
7+
let timeouts = []
8+
if (options instanceof Array) {
9+
timeouts = [...options]
10+
} else {
11+
if (options.retries === Infinity) {
12+
options.forever = true
13+
delete options.retries
14+
}
15+
const opts = {
16+
retries: 10,
17+
factor: 2,
18+
minTimeout: 1 * 1000,
19+
maxTimeout: Infinity,
20+
randomize: false,
21+
...options
22+
}
23+
if (opts.minTimeout > opts.maxTimeout) {
24+
throw new Error('minTimeout is greater than maxTimeout')
25+
}
26+
if (opts.retries) {
27+
for (let i = 0; i < opts.retries; i++) {
28+
timeouts.push(createTimeout(i, opts))
29+
}
30+
// sort the array numerically ascending (since the timeouts may be out of order at factor < 1)
31+
timeouts.sort((a, b) => a - b)
32+
} else if (options.forever) {
33+
timeouts.push(createTimeout(0, opts))
34+
}
35+
}
36+
37+
const operation = new RetryOperation(timeouts, {
38+
forever: options.forever,
39+
unref: options.unref,
40+
maxRetryTime: options.maxRetryTime
41+
})
742

843
return new Promise(function (resolve, reject) {
944
operation.attempt(async number => {
@@ -13,13 +48,12 @@ async function promiseRetry (fn, options = {}) {
1348
}, number, operation)
1449
return resolve(result)
1550
} catch (err) {
16-
if (isRetryError(err)) {
17-
if (operation.retry(err.retried || new Error())) {
18-
return
19-
}
51+
if (!isRetryError(err)) {
52+
return reject(err)
53+
}
54+
if (!operation.retry(err.retried || new Error())) {
2055
return reject(err.retried)
2156
}
22-
return reject(err)
2357
}
2458
})
2559
})
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
class RetryOperation {
2+
#attempts = 1
3+
#cachedTimeouts = null
4+
#errors = []
5+
#fn = null
6+
#maxRetryTime
7+
#operationStart = null
8+
#originalTimeouts
9+
#timeouts
10+
#timer = null
11+
#unref
12+
13+
constructor (timeouts, options = {}) {
14+
this.#originalTimeouts = [...timeouts]
15+
this.#timeouts = [...timeouts]
16+
this.#unref = options.unref
17+
this.#maxRetryTime = options.maxRetryTime || Infinity
18+
if (options.forever) {
19+
this.#cachedTimeouts = [...this.#timeouts]
20+
}
21+
}
22+
23+
get timeouts () {
24+
return [...this.#timeouts]
25+
}
26+
27+
get errors () {
28+
return [...this.#errors]
29+
}
30+
31+
get attempts () {
32+
return this.#attempts
33+
}
34+
35+
get mainError () {
36+
let mainError = null
37+
if (this.#errors.length) {
38+
let mainErrorCount = 0
39+
const counts = {}
40+
for (let i = 0; i < this.#errors.length; i++) {
41+
const error = this.#errors[i]
42+
const { message } = error
43+
if (!counts[message]) {
44+
counts[message] = 0
45+
}
46+
counts[message]++
47+
48+
if (counts[message] >= mainErrorCount) {
49+
mainError = error
50+
mainErrorCount = counts[message]
51+
}
52+
}
53+
}
54+
return mainError
55+
}
56+
57+
reset () {
58+
this.#attempts = 1
59+
this.#timeouts = [...this.#originalTimeouts]
60+
}
61+
62+
stop () {
63+
if (this.#timer) {
64+
clearTimeout(this.#timer)
65+
}
66+
67+
this.#timeouts = []
68+
this.#cachedTimeouts = null
69+
}
70+
71+
retry (err) {
72+
this.#errors.push(err)
73+
if (new Date().getTime() - this.#operationStart >= this.#maxRetryTime) {
74+
// XXX This puts the timeout error first, meaning it will never show as mainError, there may be no way to ever see this
75+
this.#errors.unshift(new Error('RetryOperation timeout occurred'))
76+
return false
77+
}
78+
79+
let timeout = this.#timeouts.shift()
80+
if (timeout === undefined) {
81+
// We're out of timeouts, clear the last error and repeat the final timeout
82+
if (this.#cachedTimeouts) {
83+
this.#errors.pop()
84+
timeout = this.#cachedTimeouts.at(-1)
85+
} else {
86+
return false
87+
}
88+
}
89+
90+
// TODO what if there already is a timer?
91+
this.#timer = setTimeout(() => {
92+
this.#attempts++
93+
this.#fn(this.#attempts)
94+
}, timeout)
95+
96+
if (this.#unref) {
97+
this.#timer.unref()
98+
}
99+
100+
return true
101+
}
102+
103+
attempt (fn) {
104+
this.#fn = fn
105+
this.#operationStart = new Date().getTime()
106+
this.#fn(this.#attempts)
107+
}
108+
}
109+
module.exports = { RetryOperation }

node_modules/@gar/promise-retry/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gar/promise-retry",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Retries a function that returns a promise, leveraging the power of the retry module.",
55
"main": "./lib/index.js",
66
"files": [
@@ -39,9 +39,6 @@
3939
"replay"
4040
],
4141
"license": "MIT",
42-
"dependencies": {
43-
"retry": "^0.13.1"
44-
},
4542
"engines": {
4643
"node": "^20.17.0 || >=22.9.0"
4744
}

node_modules/retry/License

Lines changed: 0 additions & 21 deletions
This file was deleted.

node_modules/retry/example/dns.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

node_modules/retry/example/stop.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

node_modules/retry/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)