@@ -7,118 +7,115 @@ exports.printInstallReport = printInstallReport
77exports . printParseableReport = printParseableReport
88exports . printFullReport = printFullReport
99
10- const Bluebird = require ( 'bluebird' )
1110const auditReport = require ( 'npm-audit-report' )
11+ const npmConfig = require ( '../config/figgy-config.js' )
12+ const figgyPudding = require ( 'figgy-pudding' )
1213const treeToShrinkwrap = require ( '../shrinkwrap.js' ) . treeToShrinkwrap
1314const packageId = require ( '../utils/package-id.js' )
1415const output = require ( '../utils/output.js' )
1516const npm = require ( '../npm.js' )
1617const qw = require ( 'qw' )
17- const registryFetch = require ( 'npm-registry-fetch' )
18- const zlib = require ( 'zlib' )
19- const gzip = Bluebird . promisify ( zlib . gzip )
20- const log = require ( 'npmlog' )
18+ const regFetch = require ( 'npm-registry-fetch' )
2119const perf = require ( '../utils/perf.js' )
22- const url = require ( 'url' )
2320const npa = require ( 'npm-package-arg' )
2421const uuid = require ( 'uuid' )
2522const ssri = require ( 'ssri' )
2623const cloneDeep = require ( 'lodash.clonedeep' )
27- const pacoteOpts = require ( '../config/pacote.js' )
2824
2925// used when scrubbing module names/specifiers
3026const runId = uuid . v4 ( )
3127
28+ const InstallAuditConfig = figgyPudding ( {
29+ color : { } ,
30+ json : { } ,
31+ unicode : { }
32+ } , {
33+ other ( key ) {
34+ return / : r e g i s t r y $ / . test ( key )
35+ }
36+ } )
37+
3238function submitForInstallReport ( auditData ) {
33- const cfg = npm . config // avoid the no-dynamic-lookups test
34- const scopedRegistries = cfg . keys . filter ( _ => / : r e g i s t r y $ / . test ( _ ) ) . map ( _ => cfg . get ( _ ) )
35- perf . emit ( 'time' , 'audit compress' )
36- // TODO: registryFetch will be adding native support for `Content-Encoding: gzip` at which point
37- // we'll pass in something like `gzip: true` and not need to JSON stringify, gzip or headers.
38- return gzip ( JSON . stringify ( auditData ) ) . then ( body => {
39- perf . emit ( 'timeEnd' , 'audit compress' )
40- log . info ( 'audit' , 'Submitting payload of ' + body . length + 'bytes' )
41- scopedRegistries . forEach ( reg => {
42- // we don't care about the response so destroy the stream if we can, or leave it flowing
43- // so it can eventually finish and clean up after itself
44- fetchAudit ( url . resolve ( reg , '/-/npm/v1/security/audits/quick' ) )
45- . then ( _ => {
46- _ . body . on ( 'error' , ( ) => { } )
47- if ( _ . body . destroy ) {
48- _ . body . destroy ( )
49- } else {
50- _ . body . resume ( )
51- }
52- } , _ => { } )
53- } )
54- perf . emit ( 'time' , 'audit submit' )
55- return fetchAudit ( '/-/npm/v1/security/audits/quick' , body ) . then ( response => {
56- perf . emit ( 'timeEnd' , 'audit submit' )
57- perf . emit ( 'time' , 'audit body' )
58- return response . json ( )
59- } ) . then ( result => {
60- perf . emit ( 'timeEnd' , 'audit body' )
61- return result
62- } )
39+ const opts = InstallAuditConfig ( npmConfig ( ) )
40+ const scopedRegistries = [ ...opts . keys ( ) ] . filter (
41+ k => / : r e g i s t r y $ / . test ( k )
42+ ) . map ( k => opts [ k ] )
43+ scopedRegistries . forEach ( registry => {
44+ // we don't care about the response so destroy the stream if we can, or leave it flowing
45+ // so it can eventually finish and clean up after itself
46+ regFetch ( '/-/npm/v1/security/audits/quick' , opts . concat ( {
47+ method : 'POST' ,
48+ registry,
49+ gzip : true ,
50+ body : auditData
51+ } ) ) . then ( _ => {
52+ _ . body . on ( 'error' , ( ) => { } )
53+ if ( _ . body . destroy ) {
54+ _ . body . destroy ( )
55+ } else {
56+ _ . body . resume ( )
57+ }
58+ } , _ => { } )
6359 } )
64- }
65-
66- function submitForFullReport ( auditData ) {
67- perf . emit ( 'time' , 'audit compress' )
68- // TODO: registryFetch will be adding native support for `Content-Encoding: gzip` at which point
69- // we'll pass in something like `gzip: true` and not need to JSON stringify, gzip or headers.
70- return gzip ( JSON . stringify ( auditData ) ) . then ( body => {
71- perf . emit ( 'timeEnd' , 'audit compress' )
72- log . info ( 'audit' , 'Submitting payload of ' + body . length + ' bytes' )
73- perf . emit ( 'time' , 'audit submit' )
74- return fetchAudit ( '/-/npm/v1/security/audits' , body ) . then ( response => {
75- perf . emit ( 'timeEnd' , 'audit submit' )
76- perf . emit ( 'time' , 'audit body' )
77- return response . json ( )
78- } ) . then ( result => {
79- perf . emit ( 'timeEnd' , 'audit body' )
80- result . runId = runId
81- return result
82- } )
60+ perf . emit ( 'time' , 'audit submit' )
61+ return regFetch ( '/-/npm/v1/security/audits/quick' , opts . concat ( {
62+ method : 'POST' ,
63+ gzip : true ,
64+ body : auditData
65+ } ) ) . then ( response => {
66+ perf . emit ( 'timeEnd' , 'audit submit' )
67+ perf . emit ( 'time' , 'audit body' )
68+ return response . json ( )
69+ } ) . then ( result => {
70+ perf . emit ( 'timeEnd' , 'audit body' )
71+ return result
8372 } )
8473}
8574
86- function fetchAudit ( href , body ) {
87- const opts = pacoteOpts ( )
88- return registryFetch ( href , {
75+ function submitForFullReport ( auditData ) {
76+ perf . emit ( 'time' , 'audit submit' )
77+ const opts = InstallAuditConfig ( npmConfig ( ) )
78+ return regFetch ( '/-/npm/v1/security/audits' , opts . concat ( {
8979 method : 'POST' ,
90- headers : { 'Content-Encoding' : 'gzip' , 'Content-Type' : 'application/json' } ,
91- config : npm . config ,
92- npmSession : opts . npmSession ,
93- projectScope : npm . projectScope ,
94- log : log ,
95- body : body
80+ gzip : true ,
81+ body : auditData
82+ } ) ) . then ( response => {
83+ perf . emit ( 'timeEnd' , 'audit submit' )
84+ perf . emit ( 'time' , 'audit body' )
85+ return response . json ( )
86+ } ) . then ( result => {
87+ perf . emit ( 'timeEnd' , 'audit body' )
88+ result . runId = runId
89+ return result
9690 } )
9791}
9892
9993function printInstallReport ( auditResult ) {
94+ const opts = InstallAuditConfig ( npmConfig ( ) )
10095 return auditReport ( auditResult , {
10196 reporter : 'install' ,
102- withColor : npm . color ,
103- withUnicode : npm . config . get ( ' unicode' )
97+ withColor : opts . color ,
98+ withUnicode : opts . unicode
10499 } ) . then ( result => output ( result . report ) )
105100}
106101
107102function printFullReport ( auditResult ) {
103+ const opts = InstallAuditConfig ( npmConfig ( ) )
108104 return auditReport ( auditResult , {
109105 log : output ,
110- reporter : npm . config . get ( ' json' ) ? 'json' : 'detail' ,
111- withColor : npm . color ,
112- withUnicode : npm . config . get ( ' unicode' )
106+ reporter : opts . json ? 'json' : 'detail' ,
107+ withColor : opts . color ,
108+ withUnicode : opts . unicode
113109 } ) . then ( result => output ( result . report ) )
114110}
115111
116112function printParseableReport ( auditResult ) {
113+ const opts = InstallAuditConfig ( npmConfig ( ) )
117114 return auditReport ( auditResult , {
118115 log : output ,
119116 reporter : 'parseable' ,
120- withColor : npm . color ,
121- withUnicode : npm . config . get ( ' unicode' )
117+ withColor : opts . color ,
118+ withUnicode : opts . unicode
122119 } ) . then ( result => output ( result . report ) )
123120}
124121
0 commit comments