| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | /* global __resourceQuery */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var urlBase = decodeURIComponent(__resourceQuery.slice(1)); | 
					
						
							| 
									
										
										
										
											2023-05-15 23:49:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options | 
					
						
							|  |  |  |  * @returns {() => void} function to destroy response | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | exports.keepAlive = function (options) { | 
					
						
							|  |  |  | 	var data = options.data; | 
					
						
							|  |  |  | 	var onError = options.onError; | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 	var active = options.active; | 
					
						
							|  |  |  | 	var module = options.module; | 
					
						
							| 
									
										
										
										
											2023-05-15 23:49:46 +08:00
										 |  |  | 	/** @type {import("http").IncomingMessage} */ | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	var response; | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | 	var request = ( | 
					
						
							|  |  |  | 		urlBase.startsWith("https") ? require("https") : require("http") | 
					
						
							|  |  |  | 	).request( | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 		urlBase + data, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			agent: false, | 
					
						
							|  |  |  | 			headers: { accept: "text/event-stream" } | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (res) { | 
					
						
							|  |  |  | 			response = res; | 
					
						
							|  |  |  | 			response.on("error", errorHandler); | 
					
						
							| 
									
										
										
										
											2021-01-22 08:01:17 +08:00
										 |  |  | 			if (!active && !module.hot) { | 
					
						
							|  |  |  | 				console.log( | 
					
						
							|  |  |  | 					"Hot Module Replacement is not enabled. Waiting for process restart..." | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2023-05-15 23:49:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Error} err error | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 	function errorHandler(err) { | 
					
						
							|  |  |  | 		err.message = | 
					
						
							|  |  |  | 			"Problem communicating active modules to the server: " + err.message; | 
					
						
							|  |  |  | 		onError(err); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	request.on("error", errorHandler); | 
					
						
							|  |  |  | 	request.end(); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	return function () { | 
					
						
							|  |  |  | 		response.destroy(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }; |