(middleware, name, readFile)
| 254 | * @returns {Promise<BufferSerializableType[]>} deserialized data |
| 255 | */ |
| 256 | const deserialize = async (middleware, name, readFile) => { |
| 257 | const contents = await readFile(name); |
| 258 | if (contents.length === 0) throw new Error(`Empty file ${name}`); |
| 259 | let contentsIndex = 0; |
| 260 | let contentItem = contents[0]; |
| 261 | let contentItemLength = contentItem.length; |
| 262 | let contentPosition = 0; |
| 263 | if (contentItemLength === 0) throw new Error(`Empty file ${name}`); |
| 264 | const nextContent = () => { |
| 265 | contentsIndex++; |
| 266 | contentItem = contents[contentsIndex]; |
| 267 | contentItemLength = contentItem.length; |
| 268 | contentPosition = 0; |
| 269 | }; |
| 270 | /** |
| 271 | * Processes the provided n. |
| 272 | * @param {number} n number of bytes to ensure |
| 273 | */ |
| 274 | const ensureData = (n) => { |
| 275 | if (contentPosition === contentItemLength) { |
| 276 | nextContent(); |
| 277 | } |
| 278 | while (contentItemLength - contentPosition < n) { |
| 279 | const remaining = contentItem.subarray(contentPosition); |
| 280 | let lengthFromNext = n - remaining.length; |
| 281 | /** @type {Buffer[]} */ |
| 282 | const buffers = [remaining]; |
| 283 | for (let i = contentsIndex + 1; i < contents.length; i++) { |
| 284 | const l = contents[i].length; |
| 285 | if (l > lengthFromNext) { |
| 286 | buffers.push(contents[i].subarray(0, lengthFromNext)); |
| 287 | contents[i] = contents[i].subarray(lengthFromNext); |
| 288 | lengthFromNext = 0; |
| 289 | break; |
| 290 | } else { |
| 291 | buffers.push(contents[i]); |
| 292 | contentsIndex = i; |
| 293 | lengthFromNext -= l; |
| 294 | } |
| 295 | } |
| 296 | if (lengthFromNext > 0) throw new Error("Unexpected end of data"); |
| 297 | contentItem = Buffer.concat(buffers, n); |
| 298 | contentItemLength = n; |
| 299 | contentPosition = 0; |
| 300 | } |
| 301 | }; |
| 302 | /** |
| 303 | * Returns value value. |
| 304 | * @returns {number} value value |
| 305 | */ |
| 306 | const readUInt32LE = () => { |
| 307 | ensureData(4); |
| 308 | const value = contentItem.readUInt32LE(contentPosition); |
| 309 | contentPosition += 4; |
| 310 | return value; |
| 311 | }; |
| 312 | /** |
| 313 | * Returns value value. |
no test coverage detected