| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Defines the serializer middleware type used by this module.
|
|---|
| 9 | * @template T, K, C
|
|---|
| 10 | * @typedef {import("./SerializerMiddleware")<T, K, C>} SerializerMiddleware
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Represents Serializer.
|
|---|
| 15 | * @template DeserializedValue
|
|---|
| 16 | * @template SerializedValue
|
|---|
| 17 | * @template Context
|
|---|
| 18 | */
|
|---|
| 19 | class Serializer {
|
|---|
| 20 | /**
|
|---|
| 21 | * Creates an instance of Serializer.
|
|---|
| 22 | * @param {SerializerMiddleware<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>[]} middlewares serializer middlewares
|
|---|
| 23 | * @param {Context=} context context
|
|---|
| 24 | */
|
|---|
| 25 | constructor(middlewares, context) {
|
|---|
| 26 | this.serializeMiddlewares = [...middlewares];
|
|---|
| 27 | this.deserializeMiddlewares = [...middlewares].reverse();
|
|---|
| 28 | this.context = context;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Serializes this instance into the provided serializer context.
|
|---|
| 33 | * @template ExtendedContext
|
|---|
| 34 | * @param {DeserializedValue | Promise<DeserializedValue>} obj object
|
|---|
| 35 | * @param {Context & ExtendedContext} context context object
|
|---|
| 36 | * @returns {Promise<SerializedValue>} result
|
|---|
| 37 | */
|
|---|
| 38 | serialize(obj, context) {
|
|---|
| 39 | const ctx = { ...context, ...this.context };
|
|---|
| 40 | let current = obj;
|
|---|
| 41 | for (const middleware of this.serializeMiddlewares) {
|
|---|
| 42 | if (
|
|---|
| 43 | current &&
|
|---|
| 44 | typeof (/** @type {Promise<DeserializedValue>} */ (current).then) ===
|
|---|
| 45 | "function"
|
|---|
| 46 | ) {
|
|---|
| 47 | current =
|
|---|
| 48 | /** @type {Promise<DeserializedValue>} */
|
|---|
| 49 | (current).then((data) => data && middleware.serialize(data, ctx));
|
|---|
| 50 | } else if (current) {
|
|---|
| 51 | try {
|
|---|
| 52 | current = middleware.serialize(current, ctx);
|
|---|
| 53 | } catch (err) {
|
|---|
| 54 | current = Promise.reject(err);
|
|---|
| 55 | }
|
|---|
| 56 | } else {
|
|---|
| 57 | break;
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | return /** @type {Promise<SerializedValue>} */ (current);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Restores this instance from the provided deserializer context.
|
|---|
| 65 | * @template ExtendedContext
|
|---|
| 66 | * @param {SerializedValue | Promise<SerializedValue>} value value
|
|---|
| 67 | * @param {Context & ExtendedContext} context object
|
|---|
| 68 | * @returns {Promise<DeserializedValue>} result
|
|---|
| 69 | */
|
|---|
| 70 | deserialize(value, context) {
|
|---|
| 71 | const ctx = { ...context, ...this.context };
|
|---|
| 72 | let current = value;
|
|---|
| 73 | for (const middleware of this.deserializeMiddlewares) {
|
|---|
| 74 | current =
|
|---|
| 75 | current &&
|
|---|
| 76 | typeof (/** @type {Promise<SerializedValue>} */ (current).then) ===
|
|---|
| 77 | "function"
|
|---|
| 78 | ? /** @type {Promise<SerializedValue>} */ (current).then((data) =>
|
|---|
| 79 | middleware.deserialize(data, ctx)
|
|---|
| 80 | )
|
|---|
| 81 | : middleware.deserialize(current, ctx);
|
|---|
| 82 | }
|
|---|
| 83 | return /** @type {Promise<DeserializedValue>} */ (current);
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | module.exports = Serializer;
|
|---|