| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = class EventSource { | 
					
						
							|  |  |  | 	constructor(url) { | 
					
						
							|  |  |  | 		this.response = undefined; | 
					
						
							| 
									
										
										
										
											2021-10-25 18:12:52 +08:00
										 |  |  | 		const request = ( | 
					
						
							|  |  |  | 			url.startsWith("https:") ? require("https") : require("http") | 
					
						
							|  |  |  | 		).request( | 
					
						
							| 
									
										
										
										
											2021-01-22 04:05:23 +08:00
										 |  |  | 			url, | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				agent: false, | 
					
						
							|  |  |  | 				headers: { accept: "text/event-stream" } | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			res => { | 
					
						
							|  |  |  | 				this.response = res; | 
					
						
							|  |  |  | 				res.on("error", err => { | 
					
						
							|  |  |  | 					if (this.onerror) this.onerror(err); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		request.on("error", err => { | 
					
						
							|  |  |  | 			if (this.onerror) this.onerror({ message: err }); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		request.end(); | 
					
						
							| 
									
										
										
										
											2021-01-21 23:32:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	close() { | 
					
						
							|  |  |  | 		this.response.destroy(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set onopen(value) { | 
					
						
							|  |  |  | 		throw new Error("not implemented"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	set onmessage(value) { | 
					
						
							|  |  |  | 		throw new Error("not implemented"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; |