Skip to content

Commit a5f68ba

Browse files
authored
Add validateHTTPSegment function (#467)
1 parent e22ac76 commit a5f68ba

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export class Core<TStream extends Stream = Stream> {
6565
],
6666
},
6767
validateP2PSegment: undefined,
68+
validateHTTPSegment: undefined,
6869
httpRequestSetup: undefined,
6970
swarmId: undefined,
7071
};

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { EventTarget } from "./utils/event-target.js";
1212

1313
type HttpConfig = Pick<
1414
CoreConfig,
15-
"httpNotReceivingBytesTimeoutMs" | "httpRequestSetup"
15+
"httpNotReceivingBytesTimeoutMs" | "httpRequestSetup" | "validateHTTPSegment"
1616
>;
1717

1818
export class HttpRequestExecutor {
@@ -101,6 +101,21 @@ export class HttpRequestExecutor {
101101
this.requestControls.addLoadedChunk(chunk);
102102
this.onChunkDownloaded(chunk.byteLength, "http");
103103
}
104+
105+
const isValid =
106+
(await this.httpConfig.validateHTTPSegment?.(
107+
segment.url,
108+
segment.byteRange,
109+
this.request.data,
110+
)) ?? true;
111+
112+
if (!isValid) {
113+
this.request.clearLoadedBytes();
114+
throw new RequestError<"http-segment-validation-failed">(
115+
"http-segment-validation-failed"
116+
);
117+
}
118+
104119
requestControls.completeOnSuccess();
105120
} catch (error) {
106121
this.handleError(error);

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,24 @@ export type StreamConfig = {
408408
data: ArrayBuffer,
409409
) => Promise<boolean>;
410410

411+
/**
412+
* Optional function to validate a HTTP segment before fully integrating it into the playback buffer.
413+
* @param url URL of the segment to validate.
414+
* @param byteRange Optional byte range of the segment.
415+
* @param data Downloaded segment data.
416+
* @returns A promise that resolves with a boolean indicating if the segment is valid.
417+
*
418+
* @default
419+
* ```typescript
420+
* validateHTTPSegment: undefined
421+
* ```
422+
*/
423+
validateHTTPSegment?: (
424+
url: string,
425+
byteRange: ByteRange | undefined,
426+
data: ArrayBuffer,
427+
) => Promise<boolean>;
428+
411429
/**
412430
* Optional function to customize the setup of HTTP requests for segment downloads.
413431
* @param segmentUrl URL of the segment.
@@ -631,7 +649,8 @@ export type RequestAbortErrorType = "abort" | "bytes-receiving-timeout";
631649
export type HttpRequestErrorType =
632650
| "http-error"
633651
| "http-bytes-mismatch"
634-
| "http-unexpected-status-code";
652+
| "http-unexpected-status-code"
653+
| "http-segment-validation-failed";
635654

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

0 commit comments

Comments
 (0)