| 
									
										
										
										
											2019-10-27 03:59:49 -06:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.create = function(opts) { | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     var Greenlock = require("@root/greenlock"); | 
					
						
							| 
									
										
										
										
											2019-11-05 03:09:42 -07:00
										 |  |  |     var greenlock = opts.greenlock; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!greenlock) { | 
					
						
							|  |  |  |         opts = parsePackage(opts); | 
					
						
							|  |  |  |         opts.packageAgent = addGreenlockAgent(opts); | 
					
						
							|  |  |  |         greenlock = Greenlock.create(opts); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             if (opts.notify) { | 
					
						
							|  |  |  |                 greenlock._defaults.notify = opts.notify; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } catch (e) { | 
					
						
							|  |  |  |             console.error("Developer Error: notify not attached correctly"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     // re-export as top-level function to simplify rpc with workers
 | 
					
						
							|  |  |  |     greenlock.getAcmeHttp01ChallengeResponse = function(opts) { | 
					
						
							|  |  |  |         return greenlock.challenges.get(opts); | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-10-27 03:59:49 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     return greenlock; | 
					
						
							| 
									
										
										
										
											2019-10-27 03:59:49 -06:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | function addGreenlockAgent(opts) { | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     // Add greenlock as part of Agent, unless this is greenlock
 | 
					
						
							|  |  |  |     var packageAgent = opts.packageAgent || ""; | 
					
						
							|  |  |  |     if (!/greenlock(-express|-pro)?/i.test(packageAgent)) { | 
					
						
							|  |  |  |         var pkg = require("./package.json"); | 
					
						
							|  |  |  |         packageAgent += " Greenlock_Express/" + pkg.version; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     return packageAgent.trim(); | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 05:52:30 -06:00
										 |  |  | // ex: "John Doe <john@example.com> (https://john.doe)"
 | 
					
						
							|  |  |  | // ex: "John Doe <john@example.com>"
 | 
					
						
							|  |  |  | // ex: "<john@example.com>"
 | 
					
						
							|  |  |  | // ex: "john@example.com"
 | 
					
						
							|  |  |  | var looseEmailRe = /(^|[\s<])([^'" <>:;`]+@[^'" <>:;`]+\.[^'" <>:;`]+)/; | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | function parsePackage(opts) { | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     // 'package' is sometimes a reserved word
 | 
					
						
							|  |  |  |     var pkg = opts.package || opts.pkg; | 
					
						
							|  |  |  |     if (!pkg) { | 
					
						
							|  |  |  |         opts.maintainerEmail = parseMaintainer(opts.maintainerEmail); | 
					
						
							|  |  |  |         return opts; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     if (!opts.packageAgent) { | 
					
						
							|  |  |  |         var err = "missing `package.THING`, which is used for the ACME client user agent string"; | 
					
						
							|  |  |  |         if (!pkg.name) { | 
					
						
							|  |  |  |             throw new Error(err.replace("THING", "name")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!pkg.version) { | 
					
						
							|  |  |  |             throw new Error(err.replace("THING", "version")); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         opts.packageAgent = pkg.name + "/" + pkg.version; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     if (!opts.maintainerEmail) { | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             opts.maintainerEmail = pkg.author.email || pkg.author.match(looseEmailRe)[2]; | 
					
						
							|  |  |  |         } catch (e) {} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!opts.maintainerEmail) { | 
					
						
							|  |  |  |         throw new Error("missing or malformed `package.author`, which is used as the contact for support notices"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     opts.package = undefined; | 
					
						
							|  |  |  |     opts.maintainerEmail = parseMaintainer(opts.maintainerEmail); | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     return opts; | 
					
						
							| 
									
										
										
										
											2019-10-28 01:06:43 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-31 05:52:30 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | function parseMaintainer(maintainerEmail) { | 
					
						
							| 
									
										
										
										
											2019-11-01 15:14:07 -06:00
										 |  |  |     try { | 
					
						
							|  |  |  |         maintainerEmail = maintainerEmail.match(looseEmailRe)[2]; | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |         maintainerEmail = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!maintainerEmail) { | 
					
						
							|  |  |  |         throw new Error("missing or malformed `maintainerEmail`, which is used as the contact for support notices"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return maintainerEmail; | 
					
						
							| 
									
										
										
										
											2019-10-31 05:52:30 -06:00
										 |  |  | } |