Skip to content

Commit

Permalink
Sent ArrayBuffer to peer instead of Buffer. Use custom function to ch…
Browse files Browse the repository at this point in the history
…ange string from utf-8 to hex.
  • Loading branch information
i-zolotarenko committed Oct 11, 2023
1 parent 9a14429 commit 548320d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/p2p-media-loader-core/src/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,8 +52,8 @@ declare module "bittorrent-tracker" {
event: E,
handler: PeerConnectionEventHandler<E>
): 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;
};
}
17 changes: 13 additions & 4 deletions packages/p2p-media-loader-core/src/p2p-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}; ${
Expand Down Expand Up @@ -202,8 +202,8 @@ function createTrackerClient({
streamHash,
peerHash,
}: {
streamHash: Buffer;
peerHash: Buffer;
streamHash: string;
peerHash: string;
}) {
return new TrackerClient({
infoHash: streamHash,
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions packages/p2p-media-loader-core/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,14 @@ export class Peer {
function* getBufferChunks(
data: ArrayBuffer,
maxChunkSize: number
): Generator<Buffer> {
): Generator<ArrayBuffer> {
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;
}
}

Expand Down

0 comments on commit 548320d

Please sign in to comment.