/
index.ts
33 lines (31 loc) · 1.14 KB
/
index.ts
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
import PnpmError from '@pnpm/error'
import fetch, { RetryTimeoutOptions } from '@pnpm/fetch'
import { Lockfile } from '@pnpm/lockfile-types'
import { DependenciesField } from '@pnpm/types'
import lockfileToAuditTree from './lockfileToAuditTree'
import { AuditReport } from './types'
export * from './types'
export default async function audit (
lockfile: Lockfile,
opts: {
include?: { [dependenciesField in DependenciesField]: boolean }
registry: string
retry?: RetryTimeoutOptions
timeout?: number
}
) {
const auditTree = lockfileToAuditTree(lockfile, { include: opts.include })
const registry = opts.registry.endsWith('/') ? opts.registry : `${opts.registry}/`
const auditUrl = `${registry}-/npm/v1/security/audits`
const res = await fetch(auditUrl, {
body: JSON.stringify(auditTree),
headers: { 'Content-Type': 'application/json' },
method: 'post',
retry: opts.retry,
timeout: opts.timeout,
})
if (res.status !== 200) {
throw new PnpmError('AUDIT_BAD_RESPONSE', `The audit endpoint (at ${auditUrl}) responded with ${res.status}: ${await res.text()}`)
}
return res.json() as Promise<AuditReport>
}