Skip to content

Commit 5856a6f

Browse files
committed
fix(config): stop caring about opts.config
It was just way too confusing and caused things to break. BREAKING CHANGE: opts.config is no longer supported. Pass the options down in opts itself.
1 parent ddf911e commit 5856a6f

File tree

4 files changed

+137
-175
lines changed

4 files changed

+137
-175
lines changed

Diff for: config.js

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

33
const pkg = require('./package.json')
4-
const pudding = require('figgy-pudding')
4+
const figgyPudding = require('figgy-pudding')
55
const silentLog = require('./silentlog.js')
66

77
const AUTH_REGEX = /^(?:.*:)?(token|_authToken|username|_password|password|email|always-auth|_auth|otp)$/
88
const SCOPE_REGISTRY_REGEX = /@.*:registry$/gi
9-
const RegFetchConfig = pudding({
9+
module.exports = figgyPudding({
1010
'agent': {},
1111
'algorithms': {},
1212
'body': {},
@@ -86,9 +86,3 @@ const RegFetchConfig = pudding({
8686
return key.match(AUTH_REGEX) || key.match(SCOPE_REGISTRY_REGEX)
8787
}
8888
})
89-
90-
module.exports = config
91-
function config (opts) {
92-
opts = opts || {}
93-
return RegFetchConfig(opts, 'config' in opts && opts.config)
94-
}

Diff for: test/auth.js

+105-112
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ const tnock = require('./util/tnock.js')
99
const fetch = require('../index.js')
1010
const getAuth = require('../auth.js')
1111

12-
function confFromObj (obj) {
13-
const conf = new Map()
14-
Object.keys(obj || {}).forEach(k => { conf.set(k, obj[k]) })
15-
return conf
16-
}
17-
1812
npmlog.level = process.env.LOGLEVEL || 'silent'
1913
const OPTS = {
2014
log: npmlog,
@@ -25,31 +19,29 @@ const OPTS = {
2519
minTimeout: 1,
2620
maxTimeout: 10
2721
},
28-
config: confFromObj({
29-
registry: 'https://mock.reg/'
30-
})
22+
registry: 'https://mock.reg/'
3123
}
3224

3325
test('basic auth', t => {
34-
const config = new Map([
35-
['registry', 'https://my.custom.registry/here/'],
36-
['username', 'globaluser'],
37-
['password', Buffer.from('globalpass', 'utf8').toString('base64')],
38-
['email', '[email protected]'],
39-
['//my.custom.registry/here/:username', 'user'],
40-
['//my.custom.registry/here/:password', Buffer.from('pass', 'utf8').toString('base64')],
41-
['//my.custom.registry/here/:email', '[email protected]']
42-
])
43-
t.deepEqual(getAuth(config.get('registry'), config), {
26+
const config = {
27+
registry: 'https://my.custom.registry/here/',
28+
username: 'globaluser',
29+
password: Buffer.from('globalpass', 'utf8').toString('base64'),
30+
31+
'//my.custom.registry/here/:username': 'user',
32+
'//my.custom.registry/here/:password': Buffer.from('pass', 'utf8').toString('base64'),
33+
'//my.custom.registry/here/:email': '[email protected]'
34+
}
35+
t.deepEqual(getAuth(config.registry, config), {
4436
alwaysAuth: false,
4537
username: 'user',
4638
password: 'pass',
4739
4840
}, 'basic auth details generated')
4941

50-
const opts = Object.assign({}, OPTS, {config})
42+
const opts = Object.assign({}, OPTS, config)
5143
const encoded = Buffer.from(`user:pass`, 'utf8').toString('base64')
52-
tnock(t, opts.config.get('registry'))
44+
tnock(t, opts.registry)
5345
.matchHeader('authorization', auth => {
5446
t.equal(auth[0], `Basic ${encoded}`, 'got encoded basic auth')
5547
return auth[0] === `Basic ${encoded}`
@@ -61,19 +53,19 @@ test('basic auth', t => {
6153
})
6254

6355
test('token auth', t => {
64-
const config = new Map([
65-
['registry', 'https://my.custom.registry/here/'],
66-
['token', 'deadbeef'],
67-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
68-
['//my.custom.registry/here/:token', 'nope']
69-
])
70-
t.deepEqual(getAuth(config.get('registry'), config), {
56+
const config = {
57+
'registry': 'https://my.custom.registry/here/',
58+
'token': 'deadbeef',
59+
'//my.custom.registry/here/:_authToken': 'c0ffee',
60+
'//my.custom.registry/here/:token': 'nope'
61+
}
62+
t.deepEqual(getAuth(config.registry, config), {
7163
alwaysAuth: false,
7264
token: 'c0ffee'
7365
}, 'correct auth token picked out')
7466

75-
const opts = Object.assign({}, OPTS, {config})
76-
tnock(t, opts.config.get('registry'))
67+
const opts = Object.assign({}, OPTS, config)
68+
tnock(t, opts.registry)
7769
.matchHeader('authorization', auth => {
7870
t.equal(auth[0], 'Bearer c0ffee', 'got correct bearer token')
7971
return auth[0] === 'Bearer c0ffee'
@@ -85,18 +77,18 @@ test('token auth', t => {
8577
})
8678

8779
test('_auth auth', t => {
88-
const config = new Map([
89-
['registry', 'https://my.custom.registry/here/'],
90-
['_auth', 'deadbeef'],
91-
['//my.custom.registry/here/:_auth', 'c0ffee']
92-
])
93-
t.deepEqual(getAuth(config.get('registry'), config), {
80+
const config = {
81+
'registry': 'https://my.custom.registry/here/',
82+
'_auth': 'deadbeef',
83+
'//my.custom.registry/here/:_auth': 'c0ffee'
84+
}
85+
t.deepEqual(getAuth(config.registry, config), {
9486
alwaysAuth: false,
9587
_auth: 'c0ffee'
9688
}, 'correct _auth picked out')
9789

98-
const opts = Object.assign({}, OPTS, {config})
99-
tnock(t, opts.config.get('registry'))
90+
const opts = Object.assign({}, OPTS, config)
91+
tnock(t, opts.registry)
10092
.matchHeader('authorization', `Basic c0ffee`)
10193
.get('/hello')
10294
.reply(200, '"success"')
@@ -105,39 +97,39 @@ test('_auth auth', t => {
10597
})
10698

10799
test('globally-configured auth', t => {
108-
const basicConfig = new Map([
109-
['registry', 'https://different.registry/'],
110-
['username', 'globaluser'],
111-
['password', Buffer.from('globalpass', 'utf8').toString('base64')],
112-
['email', '[email protected]'],
113-
['//my.custom.registry/here/:username', 'user'],
114-
['//my.custom.registry/here/:password', Buffer.from('pass', 'utf8').toString('base64')],
115-
['//my.custom.registry/here/:email', '[email protected]']
116-
])
117-
t.deepEqual(getAuth(basicConfig.get('registry'), basicConfig), {
100+
const basicConfig = {
101+
'registry': 'https://different.registry/',
102+
'username': 'globaluser',
103+
'password': Buffer.from('globalpass', 'utf8').toString('base64'),
104+
'email': '[email protected]',
105+
'//my.custom.registry/here/:username': 'user',
106+
'//my.custom.registry/here/:password': Buffer.from('pass', 'utf8').toString('base64'),
107+
'//my.custom.registry/here/:email': '[email protected]'
108+
}
109+
t.deepEqual(getAuth(basicConfig.registry, basicConfig), {
118110
alwaysAuth: false,
119111
username: 'globaluser',
120112
password: 'globalpass',
121113
122114
}, 'basic auth details generated from global settings')
123115

124-
const tokenConfig = new Map([
125-
['registry', 'https://different.registry/'],
126-
['_authToken', 'deadbeef'],
127-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
128-
['//my.custom.registry/here/:token', 'nope']
129-
])
130-
t.deepEqual(getAuth(tokenConfig.get('registry'), tokenConfig), {
116+
const tokenConfig = {
117+
'registry': 'https://different.registry/',
118+
'_authToken': 'deadbeef',
119+
'//my.custom.registry/here/:_authToken': 'c0ffee',
120+
'//my.custom.registry/here/:token': 'nope'
121+
}
122+
t.deepEqual(getAuth(tokenConfig.registry, tokenConfig), {
131123
alwaysAuth: false,
132124
token: 'deadbeef'
133125
}, 'correct global auth token picked out')
134126

135-
const _authConfig = new Map([
136-
['registry', 'https://different.registry/'],
137-
['_auth', 'deadbeef'],
138-
['//my.custom.registry/here/:_auth', 'c0ffee']
139-
])
140-
t.deepEqual(getAuth(_authConfig.get('registry'), _authConfig), {
127+
const _authConfig = {
128+
'registry': 'https://different.registry/',
129+
'_auth': 'deadbeef',
130+
'//my.custom.registry/here/:_auth': 'c0ffee'
131+
}
132+
t.deepEqual(getAuth(_authConfig.registry, _authConfig), {
141133
alwaysAuth: false,
142134
_auth: 'deadbeef'
143135
}, 'correct global _auth picked out')
@@ -146,25 +138,25 @@ test('globally-configured auth', t => {
146138
})
147139

148140
test('otp token passed through', t => {
149-
const config = new Map([
150-
['registry', 'https://my.custom.registry/here/'],
151-
['token', 'deadbeef'],
152-
['otp', '694201'],
153-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
154-
['//my.custom.registry/here/:token', 'nope']
155-
])
156-
t.deepEqual(getAuth(config.get('registry'), config), {
141+
const config = {
142+
'registry': 'https://my.custom.registry/here/',
143+
'token': 'deadbeef',
144+
'otp': '694201',
145+
'//my.custom.registry/here/:_authToken': 'c0ffee',
146+
'//my.custom.registry/here/:token': 'nope'
147+
}
148+
t.deepEqual(getAuth(config.registry, config), {
157149
alwaysAuth: false,
158150
token: 'c0ffee',
159151
otp: '694201'
160152
}, 'correct auth token picked out')
161153

162-
const opts = Object.assign({}, OPTS, {config})
163-
tnock(t, opts.config.get('registry'))
154+
const opts = Object.assign({}, OPTS, config)
155+
tnock(t, opts.registry)
164156
.matchHeader('authorization', `Bearer c0ffee`)
165157
.matchHeader('npm-otp', otp => {
166-
t.equal(otp[0], config.get('otp'), 'got the right otp token')
167-
return otp[0] === config.get('otp')
158+
t.equal(otp[0], config.otp, 'got the right otp token')
159+
return otp[0] === config.otp
168160
})
169161
.get('/hello')
170162
.reply(200, '"success"')
@@ -173,15 +165,15 @@ test('otp token passed through', t => {
173165
})
174166

175167
test('different hosts for uri vs registry', t => {
176-
const config = new Map([
177-
['always-auth', false],
178-
['registry', 'https://my.custom.registry/here/'],
179-
['token', 'deadbeef'],
180-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
181-
['//my.custom.registry/here/:token', 'nope']
182-
])
168+
const config = {
169+
'always-auth': false,
170+
'registry': 'https://my.custom.registry/here/',
171+
'token': 'deadbeef',
172+
'//my.custom.registry/here/:_authToken': 'c0ffee',
173+
'//my.custom.registry/here/:token': 'nope'
174+
}
183175

184-
const opts = Object.assign({}, OPTS, {config})
176+
const opts = Object.assign({}, OPTS, config)
185177
tnock(t, 'https://some.other.host/')
186178
.matchHeader('authorization', auth => {
187179
t.notOk(auth, 'no authorization header was sent')
@@ -194,15 +186,15 @@ test('different hosts for uri vs registry', t => {
194186
})
195187

196188
test('http vs https auth sending', t => {
197-
const config = new Map([
198-
['always-auth', false],
199-
['registry', 'https://my.custom.registry/here/'],
200-
['token', 'deadbeef'],
201-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
202-
['//my.custom.registry/here/:token', 'nope']
203-
])
189+
const config = {
190+
'always-auth': false,
191+
'registry': 'https://my.custom.registry/here/',
192+
'token': 'deadbeef',
193+
'//my.custom.registry/here/:_authToken': 'c0ffee',
194+
'//my.custom.registry/here/:token': 'nope'
195+
}
204196

205-
const opts = Object.assign({}, OPTS, {config})
197+
const opts = Object.assign({}, OPTS, config)
206198
tnock(t, 'http://my.custom.registry/here/')
207199
.matchHeader('authorization', `Bearer c0ffee`)
208200
.get('/hello')
@@ -212,19 +204,19 @@ test('http vs https auth sending', t => {
212204
})
213205

214206
test('always-auth', t => {
215-
const config = new Map([
216-
['registry', 'https://my.custom.registry/here/'],
217-
['always-auth', 'true'],
218-
['token', 'deadbeef'],
219-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
220-
['//my.custom.registry/here/:token', 'nope']
221-
])
222-
t.deepEqual(getAuth(config.get('registry'), config), {
207+
const config = {
208+
'registry': 'https://my.custom.registry/here/',
209+
'always-auth': 'true',
210+
'token': 'deadbeef',
211+
'//my.custom.registry/here/:_authToken': 'c0ffee',
212+
'//my.custom.registry/here/:token': 'nope'
213+
}
214+
t.deepEqual(getAuth(config.registry, config), {
223215
alwaysAuth: true,
224216
token: 'c0ffee'
225217
}, 'correct auth token picked out')
226218

227-
const opts = Object.assign({}, OPTS, {config})
219+
const opts = Object.assign({}, OPTS, config)
228220
tnock(t, 'https://some.other.host/')
229221
.matchHeader('authorization', `Bearer c0ffee`)
230222
.get('/hello')
@@ -234,25 +226,25 @@ test('always-auth', t => {
234226
})
235227

236228
test('scope-based auth', t => {
237-
const config = new Map([
238-
['registry', 'https://my.custom.registry/here/'],
239-
['scope', '@myscope'],
240-
['@myscope:registry', 'https://my.custom.registry/here/'],
241-
['token', 'deadbeef'],
242-
['//my.custom.registry/here/:_authToken', 'c0ffee'],
243-
['//my.custom.registry/here/:token', 'nope']
244-
])
245-
t.deepEqual(getAuth(config.get('@myscope:registry'), config), {
229+
const config = {
230+
'registry': 'https://my.custom.registry/here/',
231+
'scope': '@myscope',
232+
'@myscope:registry': 'https://my.custom.registry/here/',
233+
'token': 'deadbeef',
234+
'//my.custom.registry/here/:_authToken': 'c0ffee',
235+
'//my.custom.registry/here/:token': 'nope'
236+
}
237+
t.deepEqual(getAuth(config['@myscope:registry'], config), {
246238
alwaysAuth: false,
247239
token: 'c0ffee'
248240
}, 'correct auth token picked out')
249-
t.deepEqual(getAuth(config.get('@myscope:registry'), config), {
241+
t.deepEqual(getAuth(config['@myscope:registry'], config), {
250242
alwaysAuth: false,
251243
token: 'c0ffee'
252244
}, 'correct auth token picked out without scope config having an @')
253245

254-
const opts = Object.assign({}, OPTS, {config})
255-
tnock(t, opts.config.get('@myscope:registry'))
246+
const opts = Object.assign({}, OPTS, config)
247+
tnock(t, opts['@myscope:registry'])
256248
.matchHeader('authorization', auth => {
257249
t.equal(auth[0], 'Bearer c0ffee', 'got correct bearer token for scope')
258250
return auth[0] === 'Bearer c0ffee'
@@ -262,7 +254,8 @@ test('scope-based auth', t => {
262254
.reply(200, '"success"')
263255
return fetch.json('/hello', opts)
264256
.then(res => t.equal(res, 'success', 'token auth succeeded'))
265-
.then(() => config.set('scope', 'myscope'))
266-
.then(() => fetch.json('/hello', opts))
257+
.then(() => fetch.json('/hello', Object.assign({}, opts, {
258+
scope: 'myscope'
259+
})))
267260
.then(res => t.equal(res, 'success', 'token auth succeeded without @ in scope'))
268261
})

0 commit comments

Comments
 (0)