Skip to content

Commit

Permalink
Add validateHTTPSegment function (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz authored Feb 8, 2025
1 parent e22ac76 commit a5f68ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Core<TStream extends Stream = Stream> {
],
},
validateP2PSegment: undefined,
validateHTTPSegment: undefined,
httpRequestSetup: undefined,
swarmId: undefined,
};
Expand Down
17 changes: 16 additions & 1 deletion packages/p2p-media-loader-core/src/http-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EventTarget } from "./utils/event-target.js";

type HttpConfig = Pick<
CoreConfig,
"httpNotReceivingBytesTimeoutMs" | "httpRequestSetup"
"httpNotReceivingBytesTimeoutMs" | "httpRequestSetup" | "validateHTTPSegment"
>;

export class HttpRequestExecutor {
Expand Down Expand Up @@ -101,6 +101,21 @@ export class HttpRequestExecutor {
this.requestControls.addLoadedChunk(chunk);
this.onChunkDownloaded(chunk.byteLength, "http");
}

const isValid =
(await this.httpConfig.validateHTTPSegment?.(
segment.url,
segment.byteRange,
this.request.data,
)) ?? true;

if (!isValid) {
this.request.clearLoadedBytes();
throw new RequestError<"http-segment-validation-failed">(
"http-segment-validation-failed"
);
}

requestControls.completeOnSuccess();
} catch (error) {
this.handleError(error);
Expand Down
21 changes: 20 additions & 1 deletion packages/p2p-media-loader-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,24 @@ export type StreamConfig = {
data: ArrayBuffer,
) => Promise<boolean>;

/**
* Optional function to validate a HTTP segment before fully integrating it into the playback buffer.
* @param url URL of the segment to validate.
* @param byteRange Optional byte range of the segment.
* @param data Downloaded segment data.
* @returns A promise that resolves with a boolean indicating if the segment is valid.
*
* @default
* ```typescript
* validateHTTPSegment: undefined
* ```
*/
validateHTTPSegment?: (
url: string,
byteRange: ByteRange | undefined,
data: ArrayBuffer,
) => Promise<boolean>;

/**
* Optional function to customize the setup of HTTP requests for segment downloads.
* @param segmentUrl URL of the segment.
Expand Down Expand Up @@ -631,7 +649,8 @@ export type RequestAbortErrorType = "abort" | "bytes-receiving-timeout";
export type HttpRequestErrorType =
| "http-error"
| "http-bytes-mismatch"
| "http-unexpected-status-code";
| "http-unexpected-status-code"
| "http-segment-validation-failed";

/** Defines the types of errors specific to peer-to-peer requests. */
export type PeerRequestErrorType =
Expand Down

0 comments on commit a5f68ba

Please sign in to comment.