-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathindex.js
570 lines (517 loc) · 16.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
const Arborist = require('@npmcli/arborist')
const Nock = require('nock')
const npa = require('npm-package-arg')
const pacote = require('pacote')
const path = require('node:path')
const stringify = require('json-stringify-safe')
const { createReadStream } = require('node:fs')
const fs = require('node:fs/promises')
const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
const logReq = (req, ...keys) => {
const obj = JSON.parse(stringify(req))
const res = {}
for (const [k, v] of Object.entries(obj)) {
if (!keys.includes(k)) {
res[k] = v
}
}
return stringify(res, null, 2)
}
// helper to convert old audit results to new bulk results
// TODO eventually convert the fixture files themselves
const auditToBulk = audit => {
const bulk = {}
for (const advisory in audit.advisories) {
const {
id,
url,
title,
severity = 'high',
/* eslint-disable-next-line camelcase */
vulnerable_versions = '*',
module_name: name,
} = audit.advisories[advisory]
bulk[name] = bulk[name] || []
/* eslint-disable-next-line camelcase */
bulk[name].push({ id, url, title, severity, vulnerable_versions })
}
return bulk
}
class MockRegistry {
#tap
#nock
#registry
#authorization
#basic
#debug
#strict
constructor (opts) {
if (!opts.registry) {
throw new Error('mock registry requires a registry value')
}
this.#registry = new URL(opts.registry)
this.#authorization = opts.authorization
this.#basic = opts.basic
this.#debug = opts.debug
this.#strict = opts.strict
// Required for this.package
this.#tap = opts.tap
if (this.#tap) {
this.startNock()
}
}
static tnock (t, host, opts, { debug = false, strict = false } = {}) {
const noMatch = (req) => {
if (debug) {
/* eslint-disable-next-line no-console */
console.error('NO MATCH', t.name, req.options ? req.options : req.path)
}
if (strict) {
// There are network requests that get caught regardless of error code.
// Turning on strict mode requires that those requests get explicitly
// mocked with a 404, 500, etc.
// XXX: this is opt-in currently because it breaks some existing CLI
// tests. We should work towards making this the default for all tests.
t.comment(logReq(req, 'interceptors', 'socket', 'response', '_events'))
t.fail(`Unmatched request: ${req.method} ${req.path}`)
}
}
Nock.emitter.on('no match', noMatch)
Nock.disableNetConnect()
const server = Nock(host, opts)
if (strict) {
// this requires that mocks not be shared between sub tests but it helps
// find mistakes quicker instead of waiting for the entire test to end
t.afterEach((t) => {
t.strictSame(server.pendingMocks(), [], 'no pending mocks after each')
})
}
t.teardown(() => {
Nock.enableNetConnect()
server.done()
Nock.emitter.off('no match', noMatch)
Nock.cleanAll()
})
return server
}
get origin () {
return this.#registry.origin
}
get pathname () {
if (this.#registry.pathname.endsWith('/')) {
return this.#registry.pathname.slice(0, -1)
}
return this.#registry.pathname
}
get nock () {
return this.#nock
}
set nock (nock) {
this.#nock = nock
}
startNock () {
if (this.nock) {
return
}
if (!this.#tap) {
throw new Error('cannot mock packages without a tap fixture')
}
const reqheaders = {}
if (this.#authorization) {
reqheaders.authorization = `Bearer ${this.#authorization}`
}
if (this.#basic) {
reqheaders.authorization = `Basic ${this.#basic}`
}
this.nock = MockRegistry.tnock(
this.#tap,
this.origin,
{ reqheaders },
{ debug: this.#debug, strict: this.#strict }
)
}
fullPath (uri) {
return `${this.pathname}${uri}`
}
search ({ responseCode = 200, results = [], error }) {
// the flags, score, and searchScore parts of the response are never used
// by npm, only package is used
const response = results.map(p => ({ package: p }))
this.nock = this.nock.get(this.fullPath(`/-/v1/search`)).query(true)
if (error) {
this.nock = this.nock.replyWithError(error)
} else {
this.nock = this.nock.reply(responseCode, { objects: response })
}
return this.nock
}
whoami ({ username, body, responseCode = 200, times = 1 }) {
if (username) {
this.nock = this.nock.get(this.fullPath('/-/whoami')).times(times)
.reply(responseCode, { username })
} else {
this.nock = this.nock.get(this.fullPath('/-/whoami')).times(times)
.reply(responseCode, body)
}
}
setAccess ({ spec, body = {} }) {
this.nock = this.nock.post(
this.fullPath(`/-/package/${npa(spec).escapedName}/access`),
body
).reply(200)
}
getVisibility ({ spec, visibility, responseCode = 200 }) {
this.nock = this.nock.get(
this.fullPath(`/-/package/${npa(spec).escapedName}/visibility`))
.reply(responseCode, visibility)
}
setPermissions ({ spec, team, permissions }) {
if (team.startsWith('@')) {
team = team.slice(1)
}
const [scope, teamName] = team.split(':')
this.nock = this.nock.put(
this.fullPath(`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`),
{ package: spec, permissions }
).reply(200)
}
removePermissions ({ spec, team }) {
if (team.startsWith('@')) {
team = team.slice(1)
}
const [scope, teamName] = team.split(':')
this.nock = this.nock.delete(
this.fullPath(`/-/team/${encodeURIComponent(scope)}/${encodeURIComponent(teamName)}/package`),
{ package: spec }
).reply(200)
}
couchuser ({ username, body, responseCode = 200 }) {
if (body) {
this.nock = this.nock.get(
this.fullPath(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
).reply(responseCode, body)
} else {
this.nock = this.nock.get(
this.fullPath(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
).reply(responseCode, { _id: `org.couchdb.user:${username}`, email: '', name: username })
}
}
couchadduser ({ username, email, password, token = 'npm_default-test-token' }) {
this.nock = this.nock.put(this.fullPath(`/-/user/org.couchdb.user:${username}`), body => {
this.#tap.match(body, {
_id: `org.couchdb.user:${username}`,
name: username,
email, // Sole difference from couchlogin
password,
type: 'user',
roles: [],
})
if (!body.date) {
return false
}
return true
}).reply(201, {
id: 'org.couchdb.user:undefined',
rev: '_we_dont_use_revs_any_more',
token,
})
}
couchlogin ({ username, password, token = 'npm_default-test-token' }) {
this.nock = this.nock.put(this.fullPath(`/-/user/org.couchdb.user:${username}`), body => {
this.#tap.match(body, {
_id: `org.couchdb.user:${username}`,
name: username,
password,
type: 'user',
roles: [],
})
if (!body.date) {
return false
}
return true
}).reply(201, {
id: 'org.couchdb.user:undefined',
rev: '_we_dont_use_revs_any_more',
token,
})
}
webadduser ({ token = 'npm_default-test-token' }) {
const doneUrl = new URL('/npm-cli-test/done', this.origin).href
const loginUrl = new URL('/npm-cli-test/#', this.origin).href
this.nock = this.nock
.post(this.fullPath('/-/v1/#'), body => {
this.#tap.ok(body.create) // Sole difference from weblogin
return true
})
.reply(200, { doneUrl, loginUrl })
.get('/npm-cli-test/done')
.reply(200, { token })
}
weblogin ({ token = 'npm_default-test-token' }) {
const doneUrl = new URL('/npm-cli-test/done', this.origin).href
const loginUrl = new URL('/npm-cli-test/#', this.origin).href
this.nock = this.nock
.post(this.fullPath('/-/v1/#'), () => {
return true
})
.reply(200, { doneUrl, loginUrl })
.get('/npm-cli-test/done')
.reply(200, { token })
}
logout (token) {
this.nock = this.nock.delete(this.fullPath(`/-/user/token/${encodeURIComponent(token)}`))
.reply(200, { ok: true })
}
// team can be a team or a username
getPackages ({ user, team, packages = {}, times = 1, responseCode = 200 }) {
let uri
if (user) {
uri = `/-/user/${user}/package`
} else {
if (team.startsWith('@')) {
team = team.slice(1)
}
const [scope, teamName] = team.split(':').map(encodeURIComponent)
if (teamName) {
uri = `/-/team/${scope}/${teamName}/package`
} else {
uri = `/-/org/${scope}/package`
}
}
this.nock = this.nock.get(this.fullPath(uri)).times(times).reply(responseCode, packages)
}
getCollaborators ({ spec, collaborators = {} }) {
this.nock = this.nock.get(this.fullPath(`/-/package/${npa(spec).escapedName}/collaborators`)
).reply(200, collaborators)
}
advisory (advisory = {}) {
const id = advisory.id || parseInt(Math.random() * 1000000)
return {
id,
url: `https://github.com/advisories/GHSA-${id}`,
title: `Test advisory ${id}`,
severity: 'high',
vulnerable_versions: '*',
cwe: [
'cwe-0',
],
cvss: {
score: 0,
},
...advisory,
}
}
star (manifest, users) {
const spec = npa(manifest.name)
this.nock = this.nock.put(this.fullPath(`/${spec.escapedName}`), {
_id: manifest._id,
_rev: manifest._rev,
users,
}).reply(200, { ...manifest, users })
}
ping ({ body = {}, responseCode = 200 } = {}) {
this.nock = this.nock.get(this.fullPath('/-/ping')).reply(responseCode, body)
}
// full unpublish of an entire package
async unpublish ({ manifest }) {
let nock = this.nock
const spec = npa(manifest.name)
nock = nock.delete(this.fullPath(`/${spec.escapedName}/-rev/${manifest._rev}`)).reply(201)
return nock
}
getPackage (name, { times = 1, code = 200, query, resp = {} }) {
let nock = this.nock
nock = nock.get(`/${npa(name).escapedName}`).times(times)
if (query) {
nock = nock.query(query)
}
if (code === 404) {
nock = nock.reply(code, { error: 'Not found' })
} else {
nock = nock.reply(code, resp)
}
this.nock = nock
}
getTokens (tokens) {
return this.nock.get('/-/npm/v1/tokens')
.reply(200, {
objects: tokens,
urls: {},
total: tokens.length,
userHasOldFormatToken: false,
})
}
createToken ({ password, readonly = false, cidr = [] }) {
return this.nock.post('/-/npm/v1/tokens', {
password,
readonly,
cidr_whitelist: cidr,
}).reply(200, {
key: 'n3wk3y',
token: 'n3wt0k3n',
created: new Date(),
updated: new Date(),
readonly,
cidr_whitelist: cidr,
})
}
async package ({ manifest, times = 1, query, tarballs }) {
let nock = this.nock
const spec = npa(manifest.name)
nock = nock.get(this.fullPath(`/${spec.escapedName}`)).times(times)
if (query) {
nock = nock.query(query)
}
nock = nock.reply(200, manifest)
if (tarballs) {
for (const [version, tarball] of Object.entries(tarballs)) {
const m = manifest.versions[version]
nock = await this.tarball({ manifest: m, tarball })
}
}
this.nock = nock
}
async tarball ({ manifest, tarball }) {
const nock = this.nock
const dist = new URL(manifest.dist.tarball)
const tar = await pacote.tarball(tarball, { Arborist })
nock.get(this.fullPath(dist.pathname)).reply(200, tar)
return nock
}
// either pass in packuments if you need to set specific attributes besides version,
// or an array of versions
// the last packument in the packuments or versions array will be tagged latest
manifest ({ name = 'test-package', users, packuments, versions } = {}) {
packuments = this.packuments(versions || packuments, name)
const latest = packuments.slice(-1)[0]
const manifest = {
_id: `${name}@${latest.version}`,
_rev: '00-testdeadbeef',
name,
description: 'test package mock manifest',
dependencies: {},
versions: {},
time: {},
'dist-tags': { latest: latest.version },
...latest,
}
if (users) {
manifest.users = users
}
for (const packument of packuments) {
const unscoped = name.includes('/') ? name.split('/')[1] : name
manifest.versions[packument.version] = {
_id: `${name}@${packument.version}`,
name,
description: 'test package mock manifest',
dependencies: {},
dist: {
/* eslint-disable-next-line max-len */
tarball: `${this.origin}${this.fullPath(`/${name}/-/${unscoped}-${packument.version}.tgz`)}`,
},
maintainers: [],
...packument,
}
manifest.time[packument.version] = new Date()
}
return manifest
}
packuments (packuments = ['1.0.0'], name) {
return packuments.map(p => this.packument(p, name))
}
// Generate packument from shorthand
packument (packument, name = 'test-package') {
if (!packument.version) {
packument = { version: packument }
}
return {
name,
version: '1.0.0',
description: 'mocked test package',
dependencies: {},
...packument,
}
}
// bulk advisory audit endpoint
audit ({ responseCode = 200, results = {}, convert = false, times = 1 } = {}) {
this.nock = this.nock
.post(this.fullPath('/-/npm/v1/security/advisories/bulk'))
.times(times)
.reply(
responseCode,
convert ? auditToBulk(results) : results
)
}
// Used in Arborist to mock the registry from fixture data on disk
// Will eat up all GET requests to the entire registry, so it probably doesn't work with the other GET routes very well.
mocks ({ dir }) {
const exists = (p) => fs.stat(p).then((s) => s).catch(() => false)
this.nock = this.nock.get(/.*/).reply(async function () {
const { headers, path: url } = this.req
const isCorgi = headers.accept.includes('application/vnd.npm.install-v1+json')
const encodedUrl = url.replace(/@/g, '').replace(/%2f/gi, '/')
const f = path.join(dir, 'registry-mocks', 'content', encodedUrl)
let file = f
let contentType = 'application/octet-stream'
if (isCorgi && await exists(`${f}.min.json`)) {
file = `${f}.min.json`
contentType = corgiDoc
} else if (await exists(`${f}.json`)) {
file = `${f}.json`
contentType = 'application/json'
} else if (await exists(`${f}/index.json`)) {
file = `${f}index.json`
contentType = 'application/json'
}
const stats = await exists(file)
if (stats) {
const body = createReadStream(file)
body.pause()
return [200, body, { 'content-type': contentType, 'content-length': stats.size }]
}
return [404, { error: 'not found' }]
}).persist()
}
/**
* this is a simpler convience method for creating mockable registry with
* tarballs for specific versions
*/
async setup (packages) {
const format = Object.keys(packages).map(v => {
const [name, version] = v.split('@')
return { name, version }
}).reduce((acc, inc) => {
const exists = acc.find(pkg => pkg.name === inc.name)
if (exists) {
exists.tarballs = {
...exists.tarballs,
[inc.version]: packages[`${inc.name}@${inc.version}`],
}
} else {
acc.push({ name: inc.name,
tarballs: {
[inc.version]: packages[`${inc.name}@${inc.version}`],
},
})
}
return acc
}, [])
const registry = this
for (const pkg of format) {
const { name, tarballs } = pkg
const versions = Object.keys(tarballs)
const manifest = await registry.manifest({ name, versions })
for (const version of versions) {
const tarballPath = pkg.tarballs[version]
if (!tarballPath) {
throw new Error(`Tarball path not provided for version ${version}`)
}
await registry.tarball({
manifest: manifest.versions[version],
tarball: tarballPath,
})
}
}
}
}
module.exports = MockRegistry