This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
/**
|
||||
* Encode a number as varint bytes (little-endian, continuation bit).
|
||||
* @param {number} value - Value to encode
|
||||
* @param {number[]} bytes - Accumulated bytes (default empty)
|
||||
* @returns {number[]} Varint bytes
|
||||
*/
|
||||
const encodeVarint = (value, bytes = []) => {
|
||||
const byte = value & 0x7F
|
||||
const remaining = value >>> 7
|
||||
if (remaining === 0) {
|
||||
return [...bytes, byte]
|
||||
}
|
||||
return encodeVarint(remaining, [...bytes, byte | 0x80])
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a TAK Protocol stream frame: 0xBF, varint payload length, payload.
|
||||
* @param {string|Buffer} payload - UTF-8 payload (e.g. CoT XML)
|
||||
@@ -5,17 +20,7 @@
|
||||
*/
|
||||
export function buildTakFrame(payload) {
|
||||
const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload, 'utf8')
|
||||
let n = buf.length
|
||||
const varint = []
|
||||
while (true) {
|
||||
const byte = n & 0x7F
|
||||
n >>>= 7
|
||||
if (n === 0) {
|
||||
varint.push(byte)
|
||||
break
|
||||
}
|
||||
varint.push(byte | 0x80)
|
||||
}
|
||||
const varint = encodeVarint(buf.length)
|
||||
return Buffer.concat([Buffer.from([0xBF]), Buffer.from(varint), buf])
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user