From c9a8727f026367a86c50d103035f1c02d89431fd Mon Sep 17 00:00:00 2001 From: Gar Date: Mon, 15 Aug 2022 13:57:25 -0700 Subject: [PATCH] fix: linting --- lib/check-response.js | 4 ++- lib/clean-url.js | 4 ++- test/check-response.js | 12 +++---- test/errors.js | 26 ++++++--------- test/index.js | 76 ++++++++++++++++++------------------------ 5 files changed, 55 insertions(+), 67 deletions(-) diff --git a/lib/check-response.js b/lib/check-response.js index 71451390..066ac3c3 100644 --- a/lib/check-response.js +++ b/lib/check-response.js @@ -61,7 +61,9 @@ function checkErrors (method, res, startTime, opts) { let parsed = body try { parsed = JSON.parse(body.toString('utf8')) - } catch (e) {} + } catch { + // ignore errors + } if (res.status === 401 && res.headers.get('www-authenticate')) { const auth = res.headers.get('www-authenticate') .split(/,\s*/) diff --git a/lib/clean-url.js b/lib/clean-url.js index ba31dc46..a419b47d 100644 --- a/lib/clean-url.js +++ b/lib/clean-url.js @@ -14,7 +14,9 @@ const cleanUrl = (str) => { if (url.password) { str = str.replace(url.password, replace) } - } catch {} + } catch { + // ignore errors + } return str .replace(tokenRegex, `npm_${replace}`) diff --git a/test/check-response.js b/test/check-response.js index 93e69182..e9e1e34a 100644 --- a/test/check-response.js +++ b/test/check-response.js @@ -66,7 +66,7 @@ t.test('log x-fetch-attempts header value', async t => { let msg process.on('log', (level, ...args) => { if (level === 'http') { - ;[, msg] = args + [, msg] = args } }) await t.rejects(checkResponse({ @@ -92,7 +92,7 @@ t.test('log the url fetched', t => { let header, msg process.on('log', (level, ...args) => { if (level === 'http') { - ;[header, msg] = args + [header, msg] = args } }) checkResponse({ @@ -121,7 +121,7 @@ t.test('redact password from log', t => { let header, msg process.on('log', (level, ...args) => { if (level === 'http') { - ;[header, msg] = args + [header, msg] = args } }) checkResponse({ @@ -149,7 +149,7 @@ t.test('redact well known token from log', t => { let header, msg process.on('log', (level, ...args) => { if (level === 'http') { - ;[header, msg] = args + [header, msg] = args } }) checkResponse({ @@ -176,7 +176,7 @@ t.test('report auth for registry, but not for this request', async t => { let header, msg process.on('log', (level, ...args) => { if (level === 'warn') { - ;[header, msg] = args + [header, msg] = args } }) await t.rejects(checkResponse({ @@ -214,7 +214,7 @@ t.test('logs the value of x-local-cache-status when set', t => { let header, msg process.on('log', (level, ...args) => { if (level === 'http') { - ;[header, msg] = args + [header, msg] = args } }) checkResponse({ diff --git a/test/errors.js b/test/errors.js index 5c644197..fb71d783 100644 --- a/test/errors.js +++ b/test/errors.js @@ -130,7 +130,7 @@ t.test('OTP error', t => { ) }) -t.test('OTP error with prompt', t => { +t.test('OTP error with prompt', async t => { let OTP = null tnock(t, OPTS.registry) .get('/otplease').times(2) @@ -150,16 +150,13 @@ t.test('OTP error with prompt', t => { }) const otpPrompt = async () => '12345' - return fetch('/otplease', { ...OPTS, otpPrompt }) - .then(res => { - t.strictSame(res.status, 200, 'got 200 response') - return res.json() - }).then(body => { - t.strictSame(body, { ok: 'this is fine' }, 'got expected body') - }) + const res = await fetch('/otplease', { ...OPTS, otpPrompt }) + t.strictSame(res.status, 200, 'got 200 response') + const body = await res.json() + t.strictSame(body, { ok: 'this is fine' }, 'got expected body') }) -t.test('OTP error with prompt, expired OTP in settings', t => { +t.test('OTP error with prompt, expired OTP in settings', async t => { let OTP = null tnock(t, OPTS.registry) .get('/otplease').times(2) @@ -183,13 +180,10 @@ t.test('OTP error with prompt, expired OTP in settings', t => { }) const otpPrompt = async () => '12345' - return fetch('/otplease', { ...OPTS, otpPrompt, otp: '98765' }) - .then(res => { - t.strictSame(res.status, 200, 'got 200 response') - return res.json() - }).then(body => { - t.strictSame(body, { ok: 'this is fine' }, 'got expected body') - }) + const res = await fetch('/otplease', { ...OPTS, otpPrompt, otp: '98765' }) + t.strictSame(res.status, 200, 'got 200 response') + const body = await res.json() + t.strictSame(body, { ok: 'this is fine' }, 'got expected body') }) t.test('OTP error with prompt that fails', t => { diff --git a/test/index.js b/test/index.js index f020706b..3806a507 100644 --- a/test/index.js +++ b/test/index.js @@ -124,7 +124,7 @@ t.test('stream body param', t => { .then(json => t.same(json, { hello: 'world' })) }) -t.test('JSON body param', t => { +t.test('JSON body param', async t => { tnock(t, defaultOpts.registry) .matchHeader('content-type', ctype => { t.equal(ctype[0], 'application/json', 'content-type automatically set') @@ -143,10 +143,8 @@ t.test('JSON body param', t => { body: { hello: 'world' }, gzip: true, } - return fetch('/hello', opts) - .then(res => { - t.equal(res.status, 200, 'request succeeded') - }) + const res = await fetch('/hello', opts) + t.equal(res.status, 200, 'request succeeded') }) t.test('gzip + buffer body param', t => { @@ -275,43 +273,41 @@ t.test('query string with ?write=true', t => { .then(res => t.strictSame(res, { write: 'go for it' })) }) -t.test('fetch.json.stream()', t => { +t.test('fetch.json.stream()', async t => { tnock(t, defaultOpts.registry).get('/hello').reply(200, { a: 1, b: 2, c: 3, }) - return fetch.json.stream('/hello', '$*', OPTS).collect().then(data => { - t.same(data, [ - { key: 'a', value: 1 }, - { key: 'b', value: 2 }, - { key: 'c', value: 3 }, - ], 'got a streamed JSON body') - }) + const data = await fetch.json.stream('/hello', '$*', OPTS).collect() + t.same(data, [ + { key: 'a', value: 1 }, + { key: 'b', value: 2 }, + { key: 'c', value: 3 }, + ], 'got a streamed JSON body') }) -t.test('fetch.json.stream opts.mapJSON', t => { +t.test('fetch.json.stream opts.mapJSON', async t => { tnock(t, defaultOpts.registry).get('/hello').reply(200, { a: 1, b: 2, c: 3, }) - return fetch.json.stream('/hello', '*', { + const data = await fetch.json.stream('/hello', '*', { ...OPTS, mapJSON (value, [key]) { return [key, value] }, - }).collect().then(data => { - t.same(data, [ - ['a', 1], - ['b', 2], - ['c', 3], - ], 'data mapped') - }) + }).collect() + t.same(data, [ + ['a', 1], + ['b', 2], + ['c', 3], + ], 'data mapped') }) -t.test('fetch.json.stream gets fetch error on stream', t => { - return t.rejects(fetch.json.stream('/hello', '*', { +t.test('fetch.json.stream gets fetch error on stream', async t => { + await t.rejects(fetch.json.stream('/hello', '*', { ...OPTS, body: Promise.reject(new Error('no body for you')), method: 'POST', @@ -321,17 +317,15 @@ t.test('fetch.json.stream gets fetch error on stream', t => { }) }) -t.test('opts.ignoreBody', t => { +t.test('opts.ignoreBody', async t => { tnock(t, defaultOpts.registry) .get('/hello') .reply(200, { hello: 'world' }) - return fetch('/hello', { ...OPTS, ignoreBody: true }) - .then(res => { - t.equal(res.body, null, 'body omitted') - }) + const res = await fetch('/hello', { ...OPTS, ignoreBody: true }) + t.equal(res.body, null, 'body omitted') }) -t.test('method configurable', t => { +t.test('method configurable', async t => { tnock(t, defaultOpts.registry) .delete('/hello') .reply(200) @@ -339,10 +333,8 @@ t.test('method configurable', t => { ...OPTS, method: 'DELETE', } - return fetch('/hello', opts) - .then(res => { - t.equal(res.status, 200, 'successfully used DELETE method') - }) + const res = await fetch('/hello', opts) + t.equal(res.status, 200, 'successfully used DELETE method') }) t.test('npm-notice header logging', async t => { @@ -355,7 +347,7 @@ t.test('npm-notice header logging', async t => { let header, msg process.on('log', (level, ...args) => { if (level === 'notice') { - ;[header, msg] = args + [header, msg] = args } }) @@ -463,7 +455,7 @@ t.test('pickRegistry through opts.spec', t => { )) }) -t.test('miscellaneous headers', t => { +t.test('miscellaneous headers', async t => { tnock(t, defaultOpts.registry) .matchHeader('npm-session', session => t.strictSame(session, ['foobarbaz'], 'session set from options')) @@ -478,7 +470,7 @@ t.test('miscellaneous headers', t => { .get('/hello') .reply(200, { hello: 'world' }) - return fetch('/hello', { + const res = await fetch('/hello', { ...OPTS, registry: null, // always falls back on falsey registry value npmSession: 'foobarbaz', @@ -486,22 +478,20 @@ t.test('miscellaneous headers', t => { userAgent: 'agent of use', npmCommand: 'hello-world', authType: 'auth', - }).then(res => { - t.equal(res.status, 200, 'got successful response') }) + t.equal(res.status, 200, 'got successful response') }) -t.test('miscellaneous headers not being set if not present in options', t => { +t.test('miscellaneous headers not being set if not present in options', async t => { tnock(t, defaultOpts.registry) .matchHeader('npm-auth-type', authType => t.strictSame(authType, undefined, 'auth-type not set from options')) .get('/hello') .reply(200, { hello: 'world' }) - return fetch('/hello', { + const res = await fetch('/hello', { ...OPTS, authType: undefined, - }).then(res => { - t.equal(res.status, 200, 'got successful response') }) + t.equal(res.status, 200, 'got successful response') })