22
33var fs = require ( "fs" ) ;
44var path = require ( "path" ) ;
5- var filePath = require ( "path" ) ;
5+ var join = require ( "path" ) . join ;
66var connect = require ( "connect" ) ;
77var Immutable = require ( "immutable" ) ;
88var http = require ( "http" ) ;
@@ -18,29 +18,61 @@ var snippetUtils = require("../snippet").utils;
1818var lrSnippet = require ( "resp-modifier" ) ;
1919var utils = require ( "../utils" ) ;
2020
21+ function getCa ( options ) {
22+ var caOption = options . getIn ( [ "https" , "ca" ] ) ;
23+ // if not provided, use Browsersync self-signed
24+ if ( typeof caOption === "undefined" ) {
25+ return fs . readFileSync ( join ( __dirname , "certs" , "server.csr" ) ) ;
26+ }
27+ // if a string was given, read that file from disk
28+ if ( typeof caOption === "string" ) {
29+ return fs . readFileSync ( caOption ) ;
30+ }
31+ // if an array was given, read all
32+ if ( List . isList ( caOption ) ) {
33+ return caOption . toArray ( ) . map ( function ( x ) {
34+ return fs . readFileSync ( x ) ;
35+ } ) ;
36+ }
37+ }
38+
39+ function getKey ( options ) {
40+ return fs . readFileSync ( options . getIn ( [ "https" , "key" ] ) || join ( __dirname , "certs" , "server.key" ) ) ;
41+ }
42+
43+ function getCert ( options ) {
44+ return fs . readFileSync ( options . getIn ( [ "https" , "cert" ] ) || join ( __dirname , "certs" , "server.crt" ) ) ;
45+ }
46+
47+ function getHttpsServerDefaults ( options ) {
48+ return fromJS ( {
49+ key : getKey ( options ) ,
50+ cert : getCert ( options ) ,
51+ ca : getCa ( options ) ,
52+ passphrase : ""
53+ } ) ;
54+ }
55+
56+ function getPFXDefaults ( options ) {
57+ return fromJS ( {
58+ pfx : fs . readFileSync ( options . getIn ( [ "https" , "pfx" ] ) )
59+ } ) ;
60+ }
61+
2162var serverUtils = {
2263 /**
2364 * @param options
2465 * @returns {{key, cert} }
2566 */
26- getKeyAndCert : function ( options ) {
27- return {
28- key : fs . readFileSync ( options . getIn ( [ "https" , "key" ] ) || filePath . join ( __dirname , "certs/server.key" ) ) ,
29- cert : fs . readFileSync ( options . getIn ( [ "https" , "cert" ] ) || filePath . join ( __dirname , "certs/server.crt" ) ) ,
30- ca : fs . readFileSync ( options . getIn ( [ "https" , "ca" ] ) || filePath . join ( __dirname , "certs/server.csr" ) ) ,
31- passphrase : options . getIn ( [ "https" , "passphrase" ] ) || ""
32- } ;
33- } ,
34- /**
35- * @param filePath
36- * @param passphrase
37- * @returns {{pfx} }
38- */
39- getPFX : function ( filePath , passphrase ) {
40- return {
41- pfx : fs . readFileSync ( filePath ) ,
42- passphrase : passphrase ,
43- } ;
67+ getHttpsOptions : function ( options ) {
68+ var userOption = options . get ( "https" ) ;
69+ if ( Map . isMap ( userOption ) ) {
70+ if ( userOption . has ( "pfx" ) ) {
71+ return userOption . mergeDeep ( getPFXDefaults ( options ) ) ;
72+ }
73+ return userOption . mergeDeep ( getHttpsServerDefaults ( options ) ) ;
74+ }
75+ return getHttpsServerDefaults ( options ) ;
4476 } ,
4577 /**
4678 * Get either http or https server
@@ -53,11 +85,8 @@ var serverUtils = {
5385 var httpModule = serverUtils . getHttpModule ( options ) ;
5486
5587 if ( options . get ( "scheme" ) === "https" ) {
56- var pfxPath = options . getIn ( [ "https" , "pfx" ] ) ;
57- var pfxPassphrase = options . getIn ( [ "https" , "pfxPassphrase" ] ) ;
58- return pfxPath ?
59- httpModule . createServer ( serverUtils . getPFX ( pfxPath , pfxPassphrase ) , app ) :
60- httpModule . createServer ( serverUtils . getKeyAndCert ( options ) , app ) ;
88+ var opts = serverUtils . getHttpsOptions ( options ) ;
89+ return httpModule . createServer ( opts . toJS ( ) , app ) ;
6190 }
6291
6392 return httpModule . createServer ( app ) ;
0 commit comments