Skip to content

Commit db90766

Browse files
authored
feat: use proc-log and drop npmlog support (#85)
BREAKING CHANGE: this drops support for passing in a `log` property. All logs are now emitted on the process object via `proc-log`
1 parent 6644733 commit db90766

9 files changed

+70
-90
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,6 @@ to the registry.
337337

338338
See also [`opts.proxy`](#opts-proxy)
339339

340-
##### <a name="opts-log"></a> `opts.log`
341-
342-
* Type: [`npmlog`](https://npm.im/npmlog)-like
343-
* Default: null
344-
345-
Logger object to use for logging operation details. Must have the same methods
346-
as `npmlog`.
347-
348340
##### <a name="opts-mapJSON"></a> `opts.mapJSON`
349341

350342
* Type: Function

lib/check-response.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
const errors = require('./errors.js')
44
const { Response } = require('minipass-fetch')
55
const defaultOpts = require('./default-opts.js')
6+
const log = require('proc-log')
67

78
/* eslint-disable-next-line max-len */
89
const moreInfoUrl = 'https://github.com/npm/cli/wiki/No-auth-for-URI,-but-auth-present-for-scoped-registry'
910
const checkResponse =
10-
async ({ method, uri, res, registry, startTime, auth, opts }) => {
11+
async ({ method, uri, res, startTime, auth, opts }) => {
1112
opts = { ...defaultOpts, ...opts }
1213
if (res.headers.has('npm-notice') && !res.headers.has('x-local-cache')) {
13-
opts.log.notice('', res.headers.get('npm-notice'))
14+
log.notice('', res.headers.get('npm-notice'))
1415
}
1516

1617
if (res.status >= 400) {
17-
logRequest(method, res, startTime, opts)
18+
logRequest(method, res, startTime)
1819
if (auth && auth.scopeAuthKey && !auth.token && !auth.auth) {
1920
// we didn't have auth for THIS request, but we do have auth for
2021
// requests to the registry indicated by the spec's scope value.
2122
// Warn the user.
22-
opts.log.warn('registry', `No auth for URI, but auth present for scoped registry.
23+
log.warn('registry', `No auth for URI, but auth present for scoped registry.
2324
2425
URI: ${uri}
2526
Scoped Registry Key: ${auth.scopeAuthKey}
@@ -38,7 +39,7 @@ More info here: ${moreInfoUrl}`)
3839
}
3940
module.exports = checkResponse
4041

41-
function logRequest (method, res, startTime, opts) {
42+
function logRequest (method, res, startTime) {
4243
const elapsedTime = Date.now() - startTime
4344
const attempt = res.headers.get('x-fetch-attempts')
4445
const attemptStr = attempt && attempt > 1 ? ` attempt #${attempt}` : ''
@@ -58,7 +59,7 @@ function logRequest (method, res, startTime, opts) {
5859
urlStr = res.url
5960
}
6061

61-
opts.log.http(
62+
log.http(
6263
'fetch',
6364
`${method.toUpperCase()} ${res.status} ${urlStr} ${elapsedTime}ms${attemptStr}${cacheStr}`
6465
)

lib/default-opts.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const pkg = require('../package.json')
22
module.exports = {
3-
log: require('./silentlog.js'),
43
maxSockets: 12,
54
method: 'GET',
65
registry: 'https://registry.npmjs.org/',

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
"minipass-fetch": "^1.4.1",
3737
"minipass-json-stream": "^1.0.1",
3838
"minizlib": "^2.1.2",
39-
"npm-package-arg": "^9.0.0"
39+
"npm-package-arg": "^9.0.0",
40+
"proc-log": "^2.0.0"
4041
},
4142
"devDependencies": {
4243
"@npmcli/template-oss": "^2.7.1",
4344
"cacache": "^15.3.0",
4445
"nock": "^13.2.4",
45-
"npmlog": "^6.0.1",
4646
"require-inject": "^1.4.4",
4747
"ssri": "^8.0.1",
4848
"tap": "^15.1.6"

test/auth.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
'use strict'
22

3-
const npmlog = require('npmlog')
43
const t = require('tap')
54
const tnock = require('./util/tnock.js')
65

76
const fetch = require('..')
87
const getAuth = require('../lib/auth.js')
98

10-
npmlog.level = process.env.LOGLEVEL || 'silent'
119
const OPTS = {
12-
log: npmlog,
1310
timeout: 0,
1411
retry: {
1512
retries: 1,

test/cache.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const { promisify } = require('util')
44
const statAsync = promisify(require('fs').stat)
5-
const npmlog = require('npmlog')
65
const path = require('path')
76
const t = require('tap')
87
const tnock = require('./util/tnock.js')
@@ -11,10 +10,8 @@ const fetch = require('..')
1110

1211
const testDir = t.testdir({})
1312

14-
npmlog.level = process.env.LOGLEVEL || 'silent'
1513
const REGISTRY = 'https://mock.reg'
1614
const OPTS = {
17-
log: npmlog,
1815
memoize: false,
1916
timeout: 0,
2017
retry: {

test/check-response.js

+48-48
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const t = require('tap')
33

44
const checkResponse = require('../lib/check-response.js')
55
const errors = require('../lib/errors.js')
6-
const silentLog = require('../lib/silentlog.js')
76
const registry = 'registry'
87
const startTime = Date.now()
98

@@ -56,27 +55,27 @@ t.test('all checks are ok, nothing to report', t => {
5655
t.end()
5756
})
5857

59-
t.test('log x-fetch-attempts header value', t => {
58+
t.test('log x-fetch-attempts header value', async t => {
6059
const headers = new Headers()
6160
headers.get = header => header === 'x-fetch-attempts' ? 3 : undefined
6261
const res = Object.assign({}, mockFetchRes, {
6362
headers,
6463
status: 400,
6564
})
6665
t.plan(2)
67-
t.rejects(checkResponse({
66+
let msg
67+
process.on('log', (level, ...args) => {
68+
if (level === 'http') {
69+
;[, msg] = args
70+
}
71+
})
72+
await t.rejects(checkResponse({
6873
method: 'get',
6974
res,
7075
registry,
7176
startTime,
72-
opts: {
73-
log: Object.assign({}, silentLog, {
74-
http (header, msg) {
75-
t.ok(msg.endsWith('attempt #3'), 'should log correct number of attempts')
76-
},
77-
}),
78-
},
7977
}))
78+
t.ok(msg.endsWith('attempt #3'), 'should log correct number of attempts')
8079
})
8180

8281
t.test('log the url fetched', t => {
@@ -90,21 +89,22 @@ t.test('log the url fetched', t => {
9089
body: new EE(),
9190
})
9291
t.plan(2)
92+
let header, msg
93+
process.on('log', (level, ...args) => {
94+
if (level === 'http') {
95+
;[header, msg] = args
96+
}
97+
})
9398
checkResponse({
9499
method: 'get',
95100
res,
96101
registry,
97102
startTime,
98-
opts: {
99-
log: Object.assign({}, silentLog, {
100-
http (header, msg) {
101-
t.equal(header, 'fetch')
102-
t.match(msg, /^GET 200 http:\/\/example.com\/foo\/bar\/baz [0-9]+m?s/)
103-
},
104-
}),
105-
},
103+
106104
})
107105
res.body.emit('end')
106+
t.equal(header, 'fetch')
107+
t.match(msg, /^GET 200 http:\/\/example.com\/foo\/bar\/baz [0-9]+m?s/)
108108
})
109109

110110
t.test('redact password from log', t => {
@@ -118,34 +118,40 @@ t.test('redact password from log', t => {
118118
body: new EE(),
119119
})
120120
t.plan(2)
121+
let header, msg
122+
process.on('log', (level, ...args) => {
123+
if (level === 'http') {
124+
;[header, msg] = args
125+
}
126+
})
121127
checkResponse({
122128
method: 'get',
123129
res,
124130
registry,
125131
startTime,
126-
opts: {
127-
log: Object.assign({}, silentLog, {
128-
http (header, msg) {
129-
t.equal(header, 'fetch')
130-
t.match(msg, /^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s/)
131-
},
132-
}),
133-
},
134132
})
135133
res.body.emit('end')
134+
t.equal(header, 'fetch')
135+
t.match(msg, /^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s/)
136136
})
137137

138138
/* eslint-disable-next-line max-len */
139139
const moreInfoUrl = 'https://github.com/npm/cli/wiki/No-auth-for-URI,-but-auth-present-for-scoped-registry'
140140

141-
t.test('report auth for registry, but not for this request', t => {
141+
t.test('report auth for registry, but not for this request', async t => {
142142
const res = Object.assign({}, mockFetchRes, {
143143
buffer: () => Promise.resolve(Buffer.from('ok')),
144144
status: 400,
145145
url: 'https://example.com/',
146146
})
147147
t.plan(3)
148-
t.rejects(checkResponse({
148+
let header, msg
149+
process.on('log', (level, ...args) => {
150+
if (level === 'warn') {
151+
;[header, msg] = args
152+
}
153+
})
154+
await t.rejects(checkResponse({
149155
method: 'get',
150156
res,
151157
uri: 'https://example.com/',
@@ -156,20 +162,14 @@ t.test('report auth for registry, but not for this request', t => {
156162
auth: null,
157163
token: null,
158164
},
159-
opts: {
160-
log: Object.assign({}, silentLog, {
161-
warn (header, msg) {
162-
t.equal(header, 'registry')
163-
t.equal(msg, `No auth for URI, but auth present for scoped registry.
165+
}), errors.HttpErrorGeneral)
166+
t.equal(header, 'registry')
167+
t.equal(msg, `No auth for URI, but auth present for scoped registry.
164168
165169
URI: https://example.com/
166170
Scoped Registry Key: //some-scope-registry.com/
167171
168172
More info here: ${moreInfoUrl}`)
169-
},
170-
}),
171-
},
172-
}), errors.HttpErrorGeneral)
173173
})
174174

175175
t.test('logs the value of x-local-cache-status when set', t => {
@@ -183,22 +183,22 @@ t.test('logs the value of x-local-cache-status when set', t => {
183183
body: new EE(),
184184
})
185185
t.plan(2)
186+
let header, msg
187+
process.on('log', (level, ...args) => {
188+
if (level === 'http') {
189+
;[header, msg] = args
190+
}
191+
})
186192
checkResponse({
187193
method: 'get',
188194
res,
189195
registry,
190196
startTime,
191-
opts: {
192-
log: Object.assign({}, silentLog, {
193-
http (header, msg) {
194-
t.equal(header, 'fetch')
195-
t.match(
196-
msg,
197-
/^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s \(cache hit\)$/
198-
)
199-
},
200-
}),
201-
},
202197
})
203198
res.body.emit('end')
199+
t.equal(header, 'fetch')
200+
t.match(
201+
msg,
202+
/^GET 200 http:\/\/username:\*\*\*@example.com\/foo\/bar\/baz [0-9]+m?s \(cache hit\)$/
203+
)
204204
})

test/errors.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
'use strict'
22

33
const npa = require('npm-package-arg')
4-
const npmlog = require('npmlog')
54
const t = require('tap')
65
const tnock = require('./util/tnock.js')
76
const errors = require('../lib/errors.js')
87

98
const fetch = require('..')
109

11-
npmlog.level = process.env.LOGLEVEL || 'silent'
1210
const OPTS = {
13-
log: npmlog,
1411
timeout: 0,
1512
retry: {
1613
retries: 1,

test/index.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict'
22

33
const Minipass = require('minipass')
4-
const npmlog = require('npmlog')
54
const ssri = require('ssri')
65
const t = require('tap')
76
const zlib = require('zlib')
8-
const silentLog = require('../lib/silentlog.js')
97
const defaultOpts = require('../lib/default-opts.js')
108
const tnock = require('./util/tnock.js')
119

@@ -17,9 +15,7 @@ defaultOpts.registry = 'https://mock.reg/'
1715

1816
const fetch = require('..')
1917

20-
npmlog.level = process.env.LOGLEVEL || 'silent'
2118
const OPTS = {
22-
log: npmlog,
2319
timeout: 0,
2420
retry: {
2521
retries: 1,
@@ -349,24 +345,25 @@ t.test('method configurable', t => {
349345
})
350346
})
351347

352-
t.test('npm-notice header logging', t => {
348+
t.test('npm-notice header logging', async t => {
353349
tnock(t, defaultOpts.registry)
354350
.get('/hello')
355351
.reply(200, { hello: 'world' }, {
356352
'npm-notice': 'npm <3 u',
357353
})
358-
const opts = {
359-
...OPTS,
360-
log: Object.assign({}, silentLog, {
361-
notice (header, msg) {
362-
t.equal(header, '', 'empty log header thing')
363-
t.equal(msg, 'npm <3 u', 'logged out npm-notice at NOTICE level')
364-
},
365-
}),
366-
}
354+
355+
let header, msg
356+
process.on('log', (level, ...args) => {
357+
if (level === 'notice') {
358+
;[header, msg] = args
359+
}
360+
})
361+
367362
t.plan(3)
368-
return fetch('/hello', opts)
369-
.then(res => t.equal(res.status, 200, 'got successful response'))
363+
const res = await fetch('/hello', { ...OPTS })
364+
t.equal(res.status, 200, 'got successful response')
365+
t.equal(header, '', 'empty log header thing')
366+
t.equal(msg, 'npm <3 u', 'logged out npm-notice at NOTICE level')
370367
})
371368

372369
t.test('optionally verifies request body integrity', t => {

0 commit comments

Comments
 (0)