This commit is contained in:
boufni95 2025-01-29 17:31:25 +00:00
parent c0459de3ac
commit 9a8213591a
2 changed files with 38 additions and 4 deletions

View file

@ -76,12 +76,14 @@ export const integerToUint8Array = (number: number): Uint8Array => {
} }
export type TLV = { [t: 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 = {} const result: TLV = {}
let rest = data let rest = data
while (rest.length > 0) { while (rest.length > 0) {
const t = rest[0] const t = rest[0]
const l = rest[1] const l = rest[1]
if (log) console.log({ t, l })
const v = rest.slice(2, 2 + l) const v = rest.slice(2, 2 + l)
rest = rest.slice(2 + l) rest = rest.slice(2 + l)
if (v.length < l) throw new Error(`not enough data to read on TLV ${t}`) if (v.length < l) throw new Error(`not enough data to read on TLV ${t}`)
@ -97,6 +99,7 @@ export const encodeTLV = (tlv: TLV): Uint8Array => {
.reverse() .reverse()
.forEach(([t, vs]) => { .forEach(([t, vs]) => {
vs.forEach(v => { 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) const entry = new Uint8Array(v.length + 2)
entry.set([parseInt(t)], 0) entry.set([parseInt(t)], 0)
entry.set([v.length], 1) entry.set([v.length], 1)
@ -106,4 +109,35 @@ export const encodeTLV = (tlv: TLV): Uint8Array => {
}) })
return concatBytes(...entries) 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)
} }

View file

@ -4,7 +4,7 @@ import Storage from '../storage/index.js'
import { ERROR, getLogger } from "../helpers/logger.js" import { ERROR, getLogger } from "../helpers/logger.js"
import * as Types from '../../../proto/autogenerated/ts/types.js' import * as Types from '../../../proto/autogenerated/ts/types.js'
import { NostrSend, SendData, SendInitiator } from "../nostr/handler.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 } type IceCandidate = { type: string, candidate?: string, sdpMid?: string, sdpMLineIndex?: number }
const configuration = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] } const configuration = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] }
type UserInfo = { userPub: string, appId: string } type UserInfo = { userPub: string, appId: string }
@ -101,9 +101,9 @@ export default class webRTC {
for (let i = 0; i < packets.length; i++) { for (let i = 0; i < packets.length; i++) {
const packet = packets[i] const packet = packets[i]
const tlv = encodeTLVDataPacket({ dataId: id, packetNum: i + 1, totalPackets: packets.length, data: packet }) const tlv = encodeTLVDataPacket({ dataId: id, packetNum: i + 1, totalPackets: packets.length, data: packet })
const bytes = encodeTLV(tlv) const bytes = encodeTLbV(tlv)
if (i === 0) { if (i === 0) {
console.log("sending first packet", bytes) console.log("sending first packet", tlv)
} }
channel.send(bytes) channel.send(bytes)
} }