Skip to content

Commit

Permalink
TLbV
Browse files Browse the repository at this point in the history
  • Loading branch information
boufni95 committed Jan 29, 2025
1 parent c0459de commit 9a82135
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/services/helpers/tlv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ export const integerToUint8Array = (number: number): Uint8Array => {
}

export type TLV = { [t: number]: Uint8Array[] }
export const parseTLV = (data: Uint8Array): TLV => {
export type TLbV = { [t: number]: Uint8Array[] }
export const parseTLV = (data: Uint8Array, log = false): TLV => {
const result: TLV = {}
let rest = data
while (rest.length > 0) {
const t = rest[0]
const l = rest[1]
if (log) console.log({ t, l })
const v = rest.slice(2, 2 + l)
rest = rest.slice(2 + l)
if (v.length < l) throw new Error(`not enough data to read on TLV ${t}`)
Expand All @@ -97,6 +99,7 @@ export const encodeTLV = (tlv: TLV): Uint8Array => {
.reverse()
.forEach(([t, vs]) => {
vs.forEach(v => {
if (v.length > 255) throw new Error(`value too long to encode in TLV ${t}`)
const entry = new Uint8Array(v.length + 2)
entry.set([parseInt(t)], 0)
entry.set([v.length], 1)
Expand All @@ -106,4 +109,35 @@ export const encodeTLV = (tlv: TLV): Uint8Array => {
})

return concatBytes(...entries)
}
export const parseTLbV = (data: Uint8Array, log = false): TLV => {
const result: TLV = {}
let rest = data
while (rest.length > 0) {
const t = rest[0]
const l = parseInt(bytesToHex(rest.slice(1, 5)), 16)
if (log) console.log({ t, l })
const v = rest.slice(5, 5 + l)
rest = rest.slice(5 + l)
if (v.length < l) throw new Error(`not enough data to read on TLV ${t}`)
result[t] = result[t] || []
result[t].push(v)
}
return result
}

export const encodeTLbV = (tlv: TLV): Uint8Array => {
const entries: Uint8Array[] = []
Object.entries(tlv)
.reverse()
.forEach(([t, vs]) => {
vs.forEach(v => {
const entry = new Uint8Array(v.length + 5)
entry.set([parseInt(t)], 0)
entry.set(integerToUint8Array(v.length), 1)
entry.set(v, 5)
entries.push(entry)
})
})
return concatBytes(...entries)
}
6 changes: 3 additions & 3 deletions src/services/webRTC/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Storage from '../storage/index.js'
import { ERROR, getLogger } from "../helpers/logger.js"
import * as Types from '../../../proto/autogenerated/ts/types.js'
import { NostrSend, SendData, SendInitiator } from "../nostr/handler.js"
import { encodeTLV, encodeTLVDataPacket } from '../helpers/tlv.js'
import { encodeTLbV, encodeTLV, encodeTLVDataPacket } from '../helpers/tlv.js'
type IceCandidate = { type: string, candidate?: string, sdpMid?: string, sdpMLineIndex?: number }
const configuration = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] }
type UserInfo = { userPub: string, appId: string }
Expand Down Expand Up @@ -101,9 +101,9 @@ export default class webRTC {
for (let i = 0; i < packets.length; i++) {
const packet = packets[i]
const tlv = encodeTLVDataPacket({ dataId: id, packetNum: i + 1, totalPackets: packets.length, data: packet })
const bytes = encodeTLV(tlv)
const bytes = encodeTLbV(tlv)
if (i === 0) {
console.log("sending first packet", bytes)
console.log("sending first packet", tlv)
}
channel.send(bytes)
}
Expand Down

0 comments on commit 9a82135

Please sign in to comment.