| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 		this.middlewares = middlewares; | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 		const ctx = Object.assign({}, context, this.context); | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 		return new Promise((resolve, reject) => | 
					
						
							|  |  |  | 			resolve( | 
					
						
							|  |  |  | 				this.middlewares.reduce((last, middleware) => { | 
					
						
							|  |  |  | 					if (last instanceof Promise) { | 
					
						
							| 
									
										
										
										
											2018-10-18 21:52:22 +08:00
										 |  |  | 						return last.then( | 
					
						
							|  |  |  | 							data => data && middleware.serialize(data, context) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} else if (last) { | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 						try { | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 							return middleware.serialize(last, ctx); | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 						} catch (err) { | 
					
						
							| 
									
										
										
										
											2018-11-02 18:57:42 +08:00
										 |  |  | 							return Promise.reject(err); | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 				}, obj) | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 			) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-24 23:54:56 +08:00
										 |  |  | 	deserialize(context) { | 
					
						
							|  |  |  | 		const ctx = Object.assign({}, context, this.context); | 
					
						
							|  |  |  | 		return Promise.resolve().then(() => | 
					
						
							|  |  |  | 			this.middlewares.reduceRight((last, middleware) => { | 
					
						
							|  |  |  | 				if (last instanceof Promise) | 
					
						
							|  |  |  | 					return last.then(data => middleware.deserialize(data, context)); | 
					
						
							|  |  |  | 				else return middleware.deserialize(last, ctx); | 
					
						
							|  |  |  | 			}, []) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Serializer; |