@@ -7,118 +7,115 @@ exports.printInstallReport = printInstallReport
7
7
exports . printParseableReport = printParseableReport
8
8
exports . printFullReport = printFullReport
9
9
10
- const Bluebird = require ( 'bluebird' )
11
10
const auditReport = require ( 'npm-audit-report' )
11
+ const npmConfig = require ( '../config/figgy-config.js' )
12
+ const figgyPudding = require ( 'figgy-pudding' )
12
13
const treeToShrinkwrap = require ( '../shrinkwrap.js' ) . treeToShrinkwrap
13
14
const packageId = require ( '../utils/package-id.js' )
14
15
const output = require ( '../utils/output.js' )
15
16
const npm = require ( '../npm.js' )
16
17
const 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' )
21
19
const perf = require ( '../utils/perf.js' )
22
- const url = require ( 'url' )
23
20
const npa = require ( 'npm-package-arg' )
24
21
const uuid = require ( 'uuid' )
25
22
const ssri = require ( 'ssri' )
26
23
const cloneDeep = require ( 'lodash.clonedeep' )
27
- const pacoteOpts = require ( '../config/pacote.js' )
28
24
29
25
// used when scrubbing module names/specifiers
30
26
const runId = uuid . v4 ( )
31
27
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
+
32
38
function 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
+ } , _ => { } )
63
59
} )
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
83
72
} )
84
73
}
85
74
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 ( {
89
79
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
96
90
} )
97
91
}
98
92
99
93
function printInstallReport ( auditResult ) {
94
+ const opts = InstallAuditConfig ( npmConfig ( ) )
100
95
return auditReport ( auditResult , {
101
96
reporter : 'install' ,
102
- withColor : npm . color ,
103
- withUnicode : npm . config . get ( ' unicode' )
97
+ withColor : opts . color ,
98
+ withUnicode : opts . unicode
104
99
} ) . then ( result => output ( result . report ) )
105
100
}
106
101
107
102
function printFullReport ( auditResult ) {
103
+ const opts = InstallAuditConfig ( npmConfig ( ) )
108
104
return auditReport ( auditResult , {
109
105
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
113
109
} ) . then ( result => output ( result . report ) )
114
110
}
115
111
116
112
function printParseableReport ( auditResult ) {
113
+ const opts = InstallAuditConfig ( npmConfig ( ) )
117
114
return auditReport ( auditResult , {
118
115
log : output ,
119
116
reporter : 'parseable' ,
120
- withColor : npm . color ,
121
- withUnicode : npm . config . get ( ' unicode' )
117
+ withColor : opts . color ,
118
+ withUnicode : opts . unicode
122
119
} ) . then ( result => output ( result . report ) )
123
120
}
124
121
0 commit comments