| 292 | } |
| 293 | |
| 294 | function packFrame(item) { |
| 295 | var length = item.body.length; |
| 296 | |
| 297 | // write TYPE_AND_BASE128_SIZE |
| 298 | var head = [(typeToNum[item.type] << 4) | (length & 0xf)]; |
| 299 | var i = 0; |
| 300 | length >>= 4; |
| 301 | while (length) { |
| 302 | head[i++] |= 0x80; |
| 303 | head[i] = length & 0x7f; |
| 304 | length >>= 7; |
| 305 | } |
| 306 | |
| 307 | if (typeof item.ref === "number") { |
| 308 | // write BIG_ENDIAN_MODIFIED_BASE_128_NUMBER |
| 309 | var offset = item.ref; |
| 310 | // Calculate how many digits we need in base 128 and move the pointer |
| 311 | i += Math.floor(Math.log(offset) / Math.log(0x80)) + 1; |
| 312 | // Write the last digit |
| 313 | head[i] = offset & 0x7f; |
| 314 | // Then write the rest |
| 315 | while (offset >>= 7) { |
| 316 | head[--i] = 0x80 | (--offset & 0x7f); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | var parts = [bodec.fromArray(head)]; |
| 321 | if (typeof item.ref === "string") { |
| 322 | parts.push(bodec.fromHex(item.ref)); |
| 323 | } |
| 324 | parts.push(deflate(item.body)); |
| 325 | return bodec.join(parts); |
| 326 | } |