From 548320dcefd820f300b54e0965a112e2610c871e Mon Sep 17 00:00:00 2001 From: Igor Zolotarenko Date: Wed, 11 Oct 2023 15:13:54 +0300 Subject: [PATCH] Sent ArrayBuffer to peer instead of Buffer. Use custom function to change string from utf-8 to hex. --- .../p2p-media-loader-core/src/declarations.d.ts | 8 ++++---- .../p2p-media-loader-core/src/p2p-loader.ts | 17 +++++++++++++---- packages/p2p-media-loader-core/src/peer.ts | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/p2p-media-loader-core/src/declarations.d.ts b/packages/p2p-media-loader-core/src/declarations.d.ts index e78e3c8b..aa9c8e88 100644 --- a/packages/p2p-media-loader-core/src/declarations.d.ts +++ b/packages/p2p-media-loader-core/src/declarations.d.ts @@ -1,8 +1,8 @@ declare module "bittorrent-tracker" { export default class Client { constructor(options: { - infoHash: string | Buffer; - peerId: string | Buffer; + infoHash: string; + peerId: string; announce: string[]; port: number; rtcConfig?: RTCConfiguration; @@ -52,8 +52,8 @@ declare module "bittorrent-tracker" { event: E, handler: PeerConnectionEventHandler ): void; - send(data: string | Blob | Buffer): void; - write(data: string | Blob | Buffer): void; + send(data: string | ArrayBuffer): void; + write(data: string | ArrayBuffer): void; destroy(): void; }; } diff --git a/packages/p2p-media-loader-core/src/p2p-loader.ts b/packages/p2p-media-loader-core/src/p2p-loader.ts index 248aa52d..34f41e87 100644 --- a/packages/p2p-media-loader-core/src/p2p-loader.ts +++ b/packages/p2p-media-loader-core/src/p2p-loader.ts @@ -32,8 +32,8 @@ export class P2PLoader { ); this.trackerClient = createTrackerClient({ - streamHash: Buffer.from(this.streamExternalId, "utf-8"), - peerHash: Buffer.from(this.peerId, "utf-8"), + streamHash: utf8ToHex(this.streamExternalId), + peerHash: utf8ToHex(this.peerId), }); this.logger( `create tracker client: ${LoggerUtils.getStreamString(stream)}; ${ @@ -202,8 +202,8 @@ function createTrackerClient({ streamHash, peerHash, }: { - streamHash: Buffer; - peerHash: Buffer; + streamHash: string; + peerHash: string; }) { return new TrackerClient({ infoHash: streamHash, @@ -225,3 +225,12 @@ function createTrackerClient({ }, }); } + +function utf8ToHex(utf8String: string) { + let result = ""; + for (let i = 0; i < utf8String.length; i++) { + result += utf8String.charCodeAt(i).toString(16); + } + + return result; +} diff --git a/packages/p2p-media-loader-core/src/peer.ts b/packages/p2p-media-loader-core/src/peer.ts index e4d6238f..5eca7939 100644 --- a/packages/p2p-media-loader-core/src/peer.ts +++ b/packages/p2p-media-loader-core/src/peer.ts @@ -265,14 +265,14 @@ export class Peer { function* getBufferChunks( data: ArrayBuffer, maxChunkSize: number -): Generator { +): Generator { let bytesLeft = data.byteLength; while (bytesLeft > 0) { const bytesToSend = bytesLeft >= maxChunkSize ? maxChunkSize : bytesLeft; const from = data.byteLength - bytesLeft; const buffer = data.slice(from, from + bytesToSend); bytesLeft -= bytesToSend; - yield Buffer.from(buffer); + yield buffer; } }