* 从 Hermes -meta 参数注入的数据读取(备用方案)
(buffer)
| 124 | * 从 Hermes -meta 参数注入的数据读取(备用方案) |
| 125 | */ |
| 126 | function readMetadataFromHermesMeta(buffer) { |
| 127 | const metadata = {}; |
| 128 | const searchPattern = Buffer.from('contentHash=', 'utf8'); |
| 129 | |
| 130 | // 在整个文件中查找模式 |
| 131 | let index = buffer.indexOf(searchPattern); |
| 132 | |
| 133 | if (index !== -1) { |
| 134 | // 找到了 "contentHash=",读取后面的 hash 值 |
| 135 | const hashStart = index + searchPattern.length; |
| 136 | |
| 137 | // Hash 应该是 64 个十六进制字符 (SHA256) |
| 138 | let hashEnd = hashStart; |
| 139 | while (hashEnd < buffer.length && hashEnd < hashStart + 64) { |
| 140 | const byte = buffer[hashEnd]; |
| 141 | // 检查是否是有效的十六进制字符 (0-9, a-f, A-F) |
| 142 | if ((byte >= 48 && byte <= 57) || // 0-9 |
| 143 | (byte >= 97 && byte <= 102) || // a-f |
| 144 | (byte >= 65 && byte <= 70)) { // A-F |
| 145 | hashEnd++; |
| 146 | } else { |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (hashEnd > hashStart) { |
| 152 | metadata.contentHash = buffer.toString('utf8', hashStart, hashEnd); |
| 153 | console.log(`✅ Found contentHash in Hermes -meta: ${metadata.contentHash.slice(0, 16)}...`); |
| 154 | } |
| 155 | } else { |
| 156 | console.warn('⚠️ contentHash not found in Hermes -meta parameter'); |
| 157 | } |
| 158 | |
| 159 | return metadata; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * 从普通 JS bundle 的注释中读取 metadata |
no test coverage detected