Skip to content

Commit

Permalink
Merge pull request #436 from Novage/fix/data-handling
Browse files Browse the repository at this point in the history
Fix request.data handling
  • Loading branch information
DimaDemchenko authored Nov 18, 2024
2 parents 2ebcb33 + 5c9c892 commit 7f2f1fa
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/p2p-media-loader-core/src/hybrid-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class HybridLoader {
break;

case "succeed":
if (!request.data || !type) break;
if (!type) break;
if (type === "http") {
this.p2pLoaders.currentLoader.broadcastAnnouncement();
}
Expand Down
3 changes: 1 addition & 2 deletions packages/p2p-media-loader-core/src/p2p/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export class Peer {
const { request, controls } = downloadingContext;

const isWrongSegment =
!request.data ||
downloadingContext.request.segment.externalId !== command.i ||
downloadingContext.requestId !== command.r;

Expand All @@ -165,7 +164,7 @@ export class Peer {
(await this.peerConfig.validateP2PSegment?.(
request.segment.url,
request.segment.byteRange,
request.data
request.data,
)) ?? true;

if (this.downloadingContext !== downloadingContext) return;
Expand Down
7 changes: 3 additions & 4 deletions packages/p2p-media-loader-core/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class Request {
this._loadedBytes = 0;
this.bytes = [];
this._totalBytes = undefined;
this.finalData = undefined;
}

get status() {
Expand All @@ -123,8 +124,7 @@ export class Request {
return this._totalBytes;
}

get data(): ArrayBuffer | undefined {
if (this.status !== "succeed") return;
get data(): ArrayBuffer {
if (!this.finalData) this.finalData = Utils.joinChunks(this.bytes);
return this.finalData;
}
Expand Down Expand Up @@ -284,12 +284,11 @@ export class Request {

this.manageBandwidthCalculatorsState("stop");
this.notReceivingBytesTimeout.clear();
this.finalData = Utils.joinChunks(this.bytes);
this.setStatus("succeed");
this._totalBytes = this._loadedBytes;
this.onSegmentLoaded({
segmentUrl: this.segment.url,
bytesLength: this.finalData.byteLength,
bytesLength: this.data.byteLength,
downloadSource: this.currentAttempt.downloadSource,
peerId:
this.currentAttempt.downloadSource === "p2p"
Expand Down

0 comments on commit 7f2f1fa

Please sign in to comment.