| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | const memoize = require("../util/memoize");
|
|---|
| 8 |
|
|---|
| 9 | const LAZY_TARGET = Symbol("lazy serialization target");
|
|---|
| 10 | const LAZY_SERIALIZED_VALUE = Symbol("lazy serialization data");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {SerializerMiddleware<EXPECTED_ANY, EXPECTED_ANY, Record<string, EXPECTED_ANY>>} LazyTarget */
|
|---|
| 13 | /** @typedef {Record<string, EXPECTED_ANY>} LazyOptions */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * Defines the lazy function type used by this module.
|
|---|
| 17 | * @template InputValue
|
|---|
| 18 | * @template OutputValue
|
|---|
| 19 | * @template {LazyTarget} InternalLazyTarget
|
|---|
| 20 | * @template {LazyOptions | undefined} InternalLazyOptions
|
|---|
| 21 | * @typedef {(() => InputValue | Promise<InputValue>) & Partial<{ [LAZY_TARGET]: InternalLazyTarget, options: InternalLazyOptions, [LAZY_SERIALIZED_VALUE]?: OutputValue | LazyFunction<OutputValue, InputValue, InternalLazyTarget, InternalLazyOptions> | undefined }>} LazyFunction
|
|---|
| 22 | */
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Represents SerializerMiddleware.
|
|---|
| 26 | * @template DeserializedType
|
|---|
| 27 | * @template SerializedType
|
|---|
| 28 | * @template Context
|
|---|
| 29 | */
|
|---|
| 30 | class SerializerMiddleware {
|
|---|
| 31 | /* istanbul ignore next */
|
|---|
| 32 | /**
|
|---|
| 33 | * Serializes this instance into the provided serializer context.
|
|---|
| 34 | * @abstract
|
|---|
| 35 | * @param {DeserializedType} data data
|
|---|
| 36 | * @param {Context} context context object
|
|---|
| 37 | * @returns {SerializedType | Promise<SerializedType> | null} serialized data
|
|---|
| 38 | */
|
|---|
| 39 | serialize(data, context) {
|
|---|
| 40 | const AbstractMethodError = require("../errors/AbstractMethodError");
|
|---|
| 41 |
|
|---|
| 42 | throw new AbstractMethodError();
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | /* istanbul ignore next */
|
|---|
| 46 | /**
|
|---|
| 47 | * Restores this instance from the provided deserializer context.
|
|---|
| 48 | * @abstract
|
|---|
| 49 | * @param {SerializedType} data data
|
|---|
| 50 | * @param {Context} context context object
|
|---|
| 51 | * @returns {DeserializedType | Promise<DeserializedType>} deserialized data
|
|---|
| 52 | */
|
|---|
| 53 | deserialize(data, context) {
|
|---|
| 54 | const AbstractMethodError = require("../errors/AbstractMethodError");
|
|---|
| 55 |
|
|---|
| 56 | throw new AbstractMethodError();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * Creates a lazy from the provided value.
|
|---|
| 61 | * @template TLazyInputValue
|
|---|
| 62 | * @template TLazyOutputValue
|
|---|
| 63 | * @template {LazyTarget} TLazyTarget
|
|---|
| 64 | * @template {LazyOptions | undefined} TLazyOptions
|
|---|
| 65 | * @param {TLazyInputValue | (() => TLazyInputValue)} value contained value or function to value
|
|---|
| 66 | * @param {TLazyTarget} target target middleware
|
|---|
| 67 | * @param {TLazyOptions=} options lazy options
|
|---|
| 68 | * @param {TLazyOutputValue=} serializedValue serialized value
|
|---|
| 69 | * @returns {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy function
|
|---|
| 70 | */
|
|---|
| 71 | static createLazy(
|
|---|
| 72 | value,
|
|---|
| 73 | target,
|
|---|
| 74 | options = /** @type {TLazyOptions} */ ({}),
|
|---|
| 75 | serializedValue = undefined
|
|---|
| 76 | ) {
|
|---|
| 77 | if (SerializerMiddleware.isLazy(value, target)) return value;
|
|---|
| 78 | const fn =
|
|---|
| 79 | /** @type {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
|---|
| 80 | (typeof value === "function" ? value : () => value);
|
|---|
| 81 | fn[LAZY_TARGET] = target;
|
|---|
| 82 | fn.options = options;
|
|---|
| 83 | fn[LAZY_SERIALIZED_VALUE] = serializedValue;
|
|---|
| 84 | return fn;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Checks whether this serializer middleware is lazy.
|
|---|
| 89 | * @template {LazyTarget} TLazyTarget
|
|---|
| 90 | * @param {EXPECTED_ANY} fn lazy function
|
|---|
| 91 | * @param {TLazyTarget=} target target middleware
|
|---|
| 92 | * @returns {fn is LazyFunction<EXPECTED_ANY, EXPECTED_ANY, TLazyTarget, EXPECTED_ANY>} true, when fn is a lazy function (optionally of that target)
|
|---|
| 93 | */
|
|---|
| 94 | static isLazy(fn, target) {
|
|---|
| 95 | if (typeof fn !== "function") return false;
|
|---|
| 96 | const t = fn[LAZY_TARGET];
|
|---|
| 97 | return target ? t === target : Boolean(t);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Returns options.
|
|---|
| 102 | * @template TLazyInputValue
|
|---|
| 103 | * @template TLazyOutputValue
|
|---|
| 104 | * @template {LazyTarget} TLazyTarget
|
|---|
| 105 | * @template {Record<string, EXPECTED_ANY>} TLazyOptions
|
|---|
| 106 | * @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} fn lazy function
|
|---|
| 107 | * @returns {LazyOptions | undefined} options
|
|---|
| 108 | */
|
|---|
| 109 | static getLazyOptions(fn) {
|
|---|
| 110 | if (typeof fn !== "function") return;
|
|---|
| 111 | return fn.options;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Gets lazy serialized value.
|
|---|
| 116 | * @template TLazyInputValue
|
|---|
| 117 | * @template TLazyOutputValue
|
|---|
| 118 | * @template {LazyTarget} TLazyTarget
|
|---|
| 119 | * @template {LazyOptions} TLazyOptions
|
|---|
| 120 | * @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions> | EXPECTED_ANY} fn lazy function
|
|---|
| 121 | * @returns {TLazyOutputValue | undefined} serialized value
|
|---|
| 122 | */
|
|---|
| 123 | static getLazySerializedValue(fn) {
|
|---|
| 124 | if (typeof fn !== "function") return;
|
|---|
| 125 | return fn[LAZY_SERIALIZED_VALUE];
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Sets lazy serialized value.
|
|---|
| 130 | * @template TLazyInputValue
|
|---|
| 131 | * @template TLazyOutputValue
|
|---|
| 132 | * @template {LazyTarget} TLazyTarget
|
|---|
| 133 | * @template {LazyOptions} TLazyOptions
|
|---|
| 134 | * @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} fn lazy function
|
|---|
| 135 | * @param {TLazyOutputValue} value serialized value
|
|---|
| 136 | * @returns {void}
|
|---|
| 137 | */
|
|---|
| 138 | static setLazySerializedValue(fn, value) {
|
|---|
| 139 | fn[LAZY_SERIALIZED_VALUE] = value;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Returns new lazy.
|
|---|
| 144 | * @template TLazyInputValue DeserializedValue
|
|---|
| 145 | * @template TLazyOutputValue SerializedValue
|
|---|
| 146 | * @template {LazyTarget} TLazyTarget
|
|---|
| 147 | * @template {LazyOptions | undefined} TLazyOptions
|
|---|
| 148 | * @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy lazy function
|
|---|
| 149 | * @param {(value: TLazyInputValue) => TLazyOutputValue} serialize serialize function
|
|---|
| 150 | * @returns {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} new lazy
|
|---|
| 151 | */
|
|---|
| 152 | static serializeLazy(lazy, serialize) {
|
|---|
| 153 | const fn =
|
|---|
| 154 | /** @type {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} */
|
|---|
| 155 | (
|
|---|
| 156 | memoize(() => {
|
|---|
| 157 | const r = lazy();
|
|---|
| 158 | if (
|
|---|
| 159 | r &&
|
|---|
| 160 | typeof (/** @type {Promise<TLazyInputValue>} */ (r).then) ===
|
|---|
| 161 | "function"
|
|---|
| 162 | ) {
|
|---|
| 163 | return (
|
|---|
| 164 | /** @type {Promise<TLazyInputValue>} */
|
|---|
| 165 | (r).then((data) => data && serialize(data))
|
|---|
| 166 | );
|
|---|
| 167 | }
|
|---|
| 168 | return serialize(/** @type {TLazyInputValue} */ (r));
|
|---|
| 169 | })
|
|---|
| 170 | );
|
|---|
| 171 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
|---|
| 172 | fn.options = lazy.options;
|
|---|
| 173 | lazy[LAZY_SERIALIZED_VALUE] = fn;
|
|---|
| 174 | return fn;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /**
|
|---|
| 178 | * Returns new lazy.
|
|---|
| 179 | * @template TLazyInputValue SerializedValue
|
|---|
| 180 | * @template TLazyOutputValue DeserializedValue
|
|---|
| 181 | * @template {LazyTarget} TLazyTarget
|
|---|
| 182 | * @template {LazyOptions | undefined} TLazyOptions
|
|---|
| 183 | * @param {LazyFunction<TLazyInputValue, TLazyOutputValue, TLazyTarget, TLazyOptions>} lazy lazy function
|
|---|
| 184 | * @param {(data: TLazyInputValue) => TLazyOutputValue} deserialize deserialize function
|
|---|
| 185 | * @returns {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} new lazy
|
|---|
| 186 | */
|
|---|
| 187 | static deserializeLazy(lazy, deserialize) {
|
|---|
| 188 | const fn =
|
|---|
| 189 | /** @type {LazyFunction<TLazyOutputValue, TLazyInputValue, TLazyTarget, TLazyOptions>} */ (
|
|---|
| 190 | memoize(() => {
|
|---|
| 191 | const r = lazy();
|
|---|
| 192 | if (
|
|---|
| 193 | r &&
|
|---|
| 194 | typeof (/** @type {Promise<TLazyInputValue>} */ (r).then) ===
|
|---|
| 195 | "function"
|
|---|
| 196 | ) {
|
|---|
| 197 | return (
|
|---|
| 198 | /** @type {Promise<TLazyInputValue>} */
|
|---|
| 199 | (r).then((data) => deserialize(data))
|
|---|
| 200 | );
|
|---|
| 201 | }
|
|---|
| 202 | return deserialize(/** @type {TLazyInputValue} */ (r));
|
|---|
| 203 | })
|
|---|
| 204 | );
|
|---|
| 205 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
|---|
| 206 | fn.options = lazy.options;
|
|---|
| 207 | fn[LAZY_SERIALIZED_VALUE] = lazy;
|
|---|
| 208 | return fn;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Returns new lazy.
|
|---|
| 213 | * @template TLazyInputValue
|
|---|
| 214 | * @template TLazyOutputValue
|
|---|
| 215 | * @template {LazyTarget} TLazyTarget
|
|---|
| 216 | * @template {LazyOptions} TLazyOptions
|
|---|
| 217 | * @param {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions> | undefined} lazy lazy function
|
|---|
| 218 | * @returns {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions> | undefined} new lazy
|
|---|
| 219 | */
|
|---|
| 220 | static unMemoizeLazy(lazy) {
|
|---|
| 221 | if (!SerializerMiddleware.isLazy(lazy)) return lazy;
|
|---|
| 222 | /** @type {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
|---|
| 223 | const fn = () => {
|
|---|
| 224 | throw new Error(
|
|---|
| 225 | "A lazy value that has been unmemorized can't be called again"
|
|---|
| 226 | );
|
|---|
| 227 | };
|
|---|
| 228 | fn[LAZY_SERIALIZED_VALUE] = SerializerMiddleware.unMemoizeLazy(
|
|---|
| 229 | /** @type {LazyFunction<TLazyInputValue | TLazyOutputValue, TLazyInputValue | TLazyOutputValue, TLazyTarget, TLazyOptions>} */
|
|---|
| 230 | (lazy[LAZY_SERIALIZED_VALUE])
|
|---|
| 231 | );
|
|---|
| 232 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
|---|
| 233 | fn.options = lazy.options;
|
|---|
| 234 | return fn;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | module.exports = SerializerMiddleware;
|
|---|