| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Serializer { | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 	constructor(middlewares, context) { | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 		this.serializeMiddlewares = middlewares.slice(); | 
					
						
							|  |  |  | 		this.deserializeMiddlewares = middlewares.slice().reverse(); | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 		this.context = context; | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 	serialize(obj, context) { | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 		const ctx = { ...context, ...this.context }; | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 		let current = obj; | 
					
						
							|  |  |  | 		for (const middleware of this.serializeMiddlewares) { | 
					
						
							| 
									
										
										
										
											2021-06-18 16:21:19 +08:00
										 |  |  | 			if (current && typeof current.then === "function") { | 
					
						
							| 
									
										
										
										
											2021-10-11 18:41:31 +08:00
										 |  |  | 				current = current.then(data => data && middleware.serialize(data, ctx)); | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 			} else if (current) { | 
					
						
							|  |  |  | 				try { | 
					
						
							|  |  |  | 					current = middleware.serialize(current, ctx); | 
					
						
							|  |  |  | 				} catch (err) { | 
					
						
							|  |  |  | 					current = Promise.reject(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return current; | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 	deserialize(value, context) { | 
					
						
							| 
									
										
										
										
											2019-06-19 19:16:05 +08:00
										 |  |  | 		const ctx = { ...context, ...this.context }; | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 		/** @type {any} */ | 
					
						
							|  |  |  | 		let current = value; | 
					
						
							|  |  |  | 		for (const middleware of this.deserializeMiddlewares) { | 
					
						
							| 
									
										
										
										
											2021-06-18 16:21:19 +08:00
										 |  |  | 			if (current && typeof current.then === "function") { | 
					
						
							| 
									
										
										
										
											2021-10-11 18:41:31 +08:00
										 |  |  | 				current = current.then(data => middleware.deserialize(data, ctx)); | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				current = middleware.deserialize(current, ctx); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return current; | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Serializer; |