| 50 | * @returns {Promise<void>} promise |
| 51 | */ |
| 52 | const printData = async (data, indent) => { |
| 53 | if (!Array.isArray(data)) throw new Error("Not an array"); |
| 54 | if (Buffer.isBuffer(data[0])) { |
| 55 | for (const b of data) { |
| 56 | if (typeof b === "function") { |
| 57 | const innerData = await b(); |
| 58 | const info = /** @type {SizeInfo} */ (lazySizes.shift()); |
| 59 | const sizeInfo = `${(info.size / 1048576).toFixed(2)} MiB + ${( |
| 60 | info.lazySize / 1048576 |
| 61 | ).toFixed(2)} lazy MiB`; |
| 62 | console.log(`${indent}= lazy ${sizeInfo} {`); |
| 63 | await printData(innerData, `${indent} `); |
| 64 | console.log(`${indent}}`); |
| 65 | } else { |
| 66 | console.log(`${indent}= ${b.toString("hex")}`); |
| 67 | } |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | const referencedValues = new Map(); |
| 72 | const referencedValuesCounters = new Map(); |
| 73 | const referencedTypes = new Map(); |
| 74 | let currentReference = 0; |
| 75 | let currentTypeReference = 0; |
| 76 | let i = 0; |
| 77 | const read = () => data[i++]; |
| 78 | /** |
| 79 | * @param {string} content content |
| 80 | */ |
| 81 | const printLine = (content) => { |
| 82 | console.log(`${indent}${content}`); |
| 83 | }; |
| 84 | printLine(`Version: ${read()}`); |
| 85 | while (i < data.length) { |
| 86 | const item = read(); |
| 87 | if (item === ESCAPE) { |
| 88 | const nextItem = read(); |
| 89 | if (nextItem === ESCAPE_ESCAPE_VALUE) { |
| 90 | printLine("null"); |
| 91 | } else if (nextItem === ESCAPE_UNDEFINED) { |
| 92 | printLine("undefined"); |
| 93 | } else if (nextItem === ESCAPE_END_OBJECT) { |
| 94 | indent = indent.slice(0, -2); |
| 95 | printLine(`} = #${currentReference++}`); |
| 96 | } else if (typeof nextItem === "number" && nextItem < 0) { |
| 97 | const ref = currentReference + nextItem; |
| 98 | const value = referencedValues.get(ref); |
| 99 | referencedValuesCounters.set( |
| 100 | ref, |
| 101 | (referencedValuesCounters.get(ref) || 0) + 1 |
| 102 | ); |
| 103 | if (value) { |
| 104 | printLine( |
| 105 | `Reference ${nextItem} => ${JSON.stringify(value)} #${ref}` |
| 106 | ); |
| 107 | } else { |
| 108 | printLine(`Reference ${nextItem} => #${ref}`); |
| 109 | } |