| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T, K | 
					
						
							|  |  |  |  * @typedef {import("./SerializerMiddleware")<T, K>} SerializerMiddleware | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | class Serializer { | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {SerializerMiddleware<any, any>[]} middlewares serializer middlewares | 
					
						
							|  |  |  | 	 * @param {TODO=} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {any} obj object | 
					
						
							|  |  |  | 	 * @param {TODO} context content | 
					
						
							|  |  |  | 	 * @returns {Promise<any>} result | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 01:15:44 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {any} value value | 
					
						
							|  |  |  | 	 * @param {TODO} context context | 
					
						
							|  |  |  | 	 * @returns {Promise<any>} result | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 			current = | 
					
						
							|  |  |  | 				current && typeof current.then === "function" | 
					
						
							|  |  |  | 					? current.then(data => middleware.deserialize(data, ctx)) | 
					
						
							|  |  |  | 					: middleware.deserialize(current, ctx); | 
					
						
							| 
									
										
										
										
											2020-01-24 06:51:49 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return current; | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Serializer; |