@@ -20,6 +20,18 @@ const getServePath = (dir, path) => {
2020 return { path : resolvedPath } ;
2121} ;
2222
23+ const maybeParseJSON = ( { value, name } ) => {
24+ if ( typeof value !== 'string' ) {
25+ return value ;
26+ }
27+
28+ try {
29+ return JSON . parse ( value ) ;
30+ } catch ( e ) {
31+ throw new Error ( `Invalid JSON for '${ name } ' input: ${ e . message } ` ) ;
32+ }
33+ } ;
34+
2335const getConfiguration = ( { constants, inputs } = { } ) => {
2436 const serveDir =
2537 ( constants && constants . PUBLISH_DIR ) || process . env . PUBLISH_DIR ;
@@ -36,34 +48,32 @@ const getConfiguration = ({ constants, inputs } = {}) => {
3648 ) ;
3749 }
3850
39- let thresholds =
40- ( inputs && inputs . thresholds ) || process . env . THRESHOLDS || { } ;
51+ const thresholds = maybeParseJSON ( {
52+ value : ( inputs && inputs . thresholds ) || process . env . THRESHOLDS || { } ,
53+ name : 'thresholds' ,
54+ } ) ;
4155
42- if ( typeof thresholds === 'string' ) {
43- try {
44- thresholds = JSON . parse ( thresholds ) ;
45- } catch ( e ) {
46- throw new Error ( `Invalid JSON for 'thresholds' input: ${ e . message } ` ) ;
47- }
48- }
56+ const extra_headers = maybeParseJSON ( {
57+ value : ( inputs && inputs . extra_headers ) || process . env . EXTRA_HEADERS ,
58+ name : 'extra_headers' ,
59+ } ) ;
4960
50- let audits = ( inputs && inputs . audits ) || process . env . AUDITS ;
51- if ( typeof audits === 'string' ) {
52- try {
53- audits = JSON . parse ( audits ) ;
54- } catch ( e ) {
55- throw new Error ( `Invalid JSON for 'audits' input: ${ e . message } ` ) ;
56- }
57- }
61+ let audits = maybeParseJSON ( {
62+ value : ( inputs && inputs . audits ) || process . env . AUDITS ,
63+ name : 'audits' ,
64+ } ) ;
5865
5966 if ( ! Array . isArray ( audits ) ) {
60- audits = [ { path : serveDir , url : auditUrl , thresholds, output_path } ] ;
67+ audits = [
68+ { path : serveDir , url : auditUrl , thresholds, output_path, extra_headers } ,
69+ ] ;
6170 } else {
6271 audits = audits . map ( ( a ) => {
6372 return {
6473 ...a ,
6574 thresholds : a . thresholds || thresholds ,
6675 output_path : a . output_path || output_path ,
76+ extra_headers : a . extra_headers || extra_headers ,
6777 ...getServePath ( serveDir , a . path ) ,
6878 } ;
6979 } ) ;
0 commit comments