Skip to content

Commit 058ae46

Browse files
Refactor peer-connection (#370)
* Add main & secondary stream configs * Refactor dynamic configuration handling in Core.applyDynamicConfig * Refactored types * Fix jsdoc in types * Refactor mergeConfigs function to use Object.create(null) * Refactor mergeConfigs function to handle restricted properties * Fix types * Refactor core to use StreamConfig instead of CoreConfig * Refactor core configuration to use partial updates in HlsJsP2PEngine and ShakaP2PEngine * Refactor core configuration to use partial updates in HlsJsP2PEngine and ShakaP2PEngine * Refactor core configuration to use StreamConfig instead of CoreConfig * Add tests for core utils * Update eslint config to ignore test folder * Refactor core configuration to use DefinedCoreConfig * Refactor overrideConfig * Small improvements * Small improvements * Add isP2PDisabled in static stream configuration * Refactor applyDynamicConfig method to handle changes in isP2PDisabled and stream configurations * Refactor Peer class connection event handling * Refactor segment loading logic to improve P2P functionality * Refactor stream configuration handling in Core.applyDynamicConfig * Simplified dynamic configuration handling * Improved CoreConfig description in documentation * Remove redundant comments from codebase * Refactor segment loading logic to improve P2P functionality * Refactor naming for enhanced readability * Refactor segment loading logic to handle P2P functionality * Refactor error handling in RequestsContainer.destroy method * Improved naming * Improvements in Core and RequestsContainer * Refactor Peer class connection event listeners * Refactor error handling in catch block * Refactor Peer class connection event listeners and add missing event handlers * Refactor peer-connection * Resolved type error
1 parent 45e1b4b commit 058ae46

File tree

5 files changed

+59
-33
lines changed

5 files changed

+59
-33
lines changed

packages/p2p-media-loader-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"vitest": "^1.6.0"
5959
},
6060
"devDependencies": {
61+
"@types/streamx": "^2.9.5",
6162
"vite-plugin-node-polyfills": "^0.21.0"
6263
}
6364
}

packages/p2p-media-loader-core/src/declarations.d.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
declare module "bittorrent-tracker" {
2+
import type { Duplex, WritableEvents } from "streamx";
3+
24
export default class Client {
35
constructor(options: {
46
infoHash: Uint8Array;
@@ -31,22 +33,15 @@ declare module "bittorrent-tracker" {
3133

3234
export type PeerEvents = {
3335
connect: () => void;
34-
data: (data: Uint8Array) => void;
35-
close: () => void;
36-
finish: () => void;
37-
end: () => void;
38-
error: (error: { code: string }) => void;
39-
};
36+
} & WritableEvents<unknown>;
4037

41-
export type PeerConnection = {
38+
export type PeerConnection = Duplex & {
4239
id: string;
4340
idUtf8: string;
4441
initiator: boolean;
45-
_channel: RTCDataChannel;
4642
on<E extends keyof PeerEvents>(event: E, handler: PeerEvents[E]): void;
43+
off<E extends keyof PeerEvents>(event: E, handler: PeerEvents[E]): void;
4744
send(data: string | ArrayBuffer): void;
48-
write(data: string | ArrayBuffer): void;
49-
destroy(): void;
5045
};
5146
}
5247

packages/p2p-media-loader-core/src/p2p/peer-protocol.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ export class PeerProtocol {
6363
throw new Error(`Some segment data is already uploading.`);
6464
}
6565
const chunks = getBufferChunks(data, this.peerConfig.webRtcMaxMessageSize);
66-
const channel = this.connection._channel;
6766
const { promise, resolve, reject } = Utils.getControlledPromise<void>();
6867

6968
let isUploadingSegmentData = false;
@@ -82,26 +81,28 @@ export class PeerProtocol {
8281
return;
8382
}
8483

85-
while (channel.bufferedAmount <= channel.bufferedAmountLowThreshold) {
84+
while (true) {
8685
const chunk = chunks.next().value;
8786

8887
if (!chunk) {
8988
resolve();
9089
break;
9190
}
9291

93-
this.connection.send(chunk);
92+
const drained = this.connection.write(chunk);
9493
this.onChunkUploaded(chunk.byteLength, this.connection.idUtf8);
94+
if (!drained) break;
9595
}
9696
};
9797

9898
try {
99-
channel.addEventListener("bufferedamountlow", sendChunk);
99+
this.connection.on("drain", sendChunk);
100100
isUploadingSegmentData = true;
101101
sendChunk();
102102
await promise;
103103
} finally {
104-
channel.removeEventListener("bufferedamountlow", sendChunk);
104+
this.connection.off("drain", sendChunk);
105+
105106
if (this.uploadingContext === uploadingContext) {
106107
this.uploadingContext = undefined;
107108
}

packages/p2p-media-loader-core/src/p2p/peer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,16 @@ export class Peer {
304304
this.destroy();
305305
};
306306

307-
private onConnectionError = (error: { code: string }) => {
307+
private onConnectionError = (error: Error) => {
308308
this.logger(`peer connection error ${this.id} %O`, error);
309309

310-
if (error.code === "ERR_DATA_CHANNEL") {
310+
const code = (error as { code?: string }).code;
311+
312+
if (code === "ERR_DATA_CHANNEL") {
313+
this.destroy();
314+
} else if (code === "ERR_CONNECTION_FAILURE") {
311315
this.destroy();
312-
} else if (error.code === "ERR_CONNECTION_FAILURE") {
316+
} else if (code === "ERR_CONNECTION_FAILURE") {
313317
this.destroy();
314318
}
315319
};

pnpm-lock.yaml

Lines changed: 40 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)