diff --git a/lib/index.js b/lib/index.js index 85dac79..6495c1c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,7 +2,7 @@ import bytes from 'bytes'; import chalk from 'chalk'; import getFiles from './get-files'; import hash from './hash'; -import retry from './retry'; +import retry from 'async-retry'; import Agent from './agent'; import EventEmitter from 'events'; import { basename, resolve } from 'path'; @@ -158,12 +158,11 @@ export default class Now extends EventEmitter { } const uploadChunk = () => { - Promise.all(parts.shift().map((sha) => retry(async (bail) => { + Promise.all(parts.shift().map((sha) => retry(async (bail, attempt) => { const file = this._files.get(sha); const { data, names } = file; - if (this._debug) console.time(`> [debug] /sync ${names.join(' ')}`); - + if (this._debug) console.time(`> [debug] /sync #${attempt} ${names.join(' ')}`); const stream = resumer().queue(data).end(); const res = await this._fetch('/now/sync', { method: 'POST', @@ -177,7 +176,7 @@ export default class Now extends EventEmitter { }, body: stream }); - if (this._debug) console.timeEnd(`> [debug] /sync ${names.join(' ')}`); + if (this._debug) console.timeEnd(`> [debug] /sync #${attempt} ${names.join(' ')}`); // no retry on 4xx if (200 !== res.status && (400 <= res.status || 500 > res.status)) { diff --git a/lib/retry.js b/lib/retry.js deleted file mode 100644 index 79e37df..0000000 --- a/lib/retry.js +++ /dev/null @@ -1,29 +0,0 @@ -import retrier from 'retry'; - -export default function retry (fn, opts) { - return new Promise((resolve, reject) => { - const op = retrier.operation(opts); - const { onRetry } = opts; - - // we allow the user to abort retrying - // this makes sense in the cases where - // knowledge is obtained that retrying - // would be futile (e.g.: auth errors) - const bail = (err) => reject(err); - - op.attempt((num) => { - if (num > 1 && onRetry) { - const errs = op.errors(); - onRetry(errs[errs.length - 1]); - } - - fn(bail) - .then((val) => resolve(val)) - .catch(err => { - if (!op.retry(err)) { - reject(op.mainError()); - } - }); - }); - }); -} diff --git a/package.json b/package.json index eca14c5..e9742b6 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "split-array": "1.0.1", "text-table": "0.2.0", "email-validator": "1.0.4", - "email-prompt": "0.1.4" + "email-prompt": "0.1.4", + "async-retry": "0.1.1" }, "devDependencies": { "alpha-sort": "1.0.2",