( middleware, data, name, writeFile, hashFunction = DEFAULTS.HASH_FUNCTION )
| 109 | * @returns {Promise<SerializeResult>} resulting file pointer and promise |
| 110 | */ |
| 111 | const serialize = async ( |
| 112 | middleware, |
| 113 | data, |
| 114 | name, |
| 115 | writeFile, |
| 116 | hashFunction = DEFAULTS.HASH_FUNCTION |
| 117 | ) => { |
| 118 | /** @type {(Buffer[] | Buffer | Promise<SerializeResult>)[]} */ |
| 119 | const processedData = []; |
| 120 | /** @type {WeakMap<SerializeResult, LazyFunction>} */ |
| 121 | const resultToLazy = new WeakMap(); |
| 122 | /** @type {Buffer[] | undefined} */ |
| 123 | let lastBuffers; |
| 124 | for (const item of await data) { |
| 125 | if (typeof item === "function") { |
| 126 | if (!SerializerMiddleware.isLazy(item)) { |
| 127 | throw new Error("Unexpected function"); |
| 128 | } |
| 129 | if (!SerializerMiddleware.isLazy(item, middleware)) { |
| 130 | throw new Error( |
| 131 | "Unexpected lazy value with non-this target (can't pass through lazy values)" |
| 132 | ); |
| 133 | } |
| 134 | lastBuffers = undefined; |
| 135 | const serializedInfo = SerializerMiddleware.getLazySerializedValue(item); |
| 136 | if (serializedInfo) { |
| 137 | if (typeof serializedInfo === "function") { |
| 138 | throw new Error( |
| 139 | "Unexpected lazy value with non-this target (can't pass through lazy values)" |
| 140 | ); |
| 141 | } else { |
| 142 | processedData.push(serializedInfo); |
| 143 | } |
| 144 | } else { |
| 145 | const content = item(); |
| 146 | if (content) { |
| 147 | const options = SerializerMiddleware.getLazyOptions(item); |
| 148 | processedData.push( |
| 149 | serialize( |
| 150 | middleware, |
| 151 | /** @type {BufferSerializableType[]} */ |
| 152 | (content), |
| 153 | (options && options.name) || true, |
| 154 | writeFile, |
| 155 | hashFunction |
| 156 | ).then((result) => { |
| 157 | /** @type {LazyOptions} */ |
| 158 | (item.options).size = result.size; |
| 159 | resultToLazy.set(result, item); |
| 160 | return result; |
| 161 | }) |
| 162 | ); |
| 163 | } else { |
| 164 | throw new Error( |
| 165 | "Unexpected falsy value returned by lazy value function" |
| 166 | ); |
| 167 | } |
| 168 | } |
no test coverage detected