Commit a5f68ba 1 parent e22ac76 commit a5f68ba Copy full SHA for a5f68ba
File tree 3 files changed +37
-2
lines changed
packages/p2p-media-loader-core/src
3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ export class Core<TStream extends Stream = Stream> {
65
65
] ,
66
66
} ,
67
67
validateP2PSegment : undefined ,
68
+ validateHTTPSegment : undefined ,
68
69
httpRequestSetup : undefined ,
69
70
swarmId : undefined ,
70
71
} ;
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import { EventTarget } from "./utils/event-target.js";
12
12
13
13
type HttpConfig = Pick <
14
14
CoreConfig ,
15
- "httpNotReceivingBytesTimeoutMs" | "httpRequestSetup"
15
+ "httpNotReceivingBytesTimeoutMs" | "httpRequestSetup" | "validateHTTPSegment"
16
16
> ;
17
17
18
18
export class HttpRequestExecutor {
@@ -101,6 +101,21 @@ export class HttpRequestExecutor {
101
101
this . requestControls . addLoadedChunk ( chunk ) ;
102
102
this . onChunkDownloaded ( chunk . byteLength , "http" ) ;
103
103
}
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
+
104
119
requestControls . completeOnSuccess ( ) ;
105
120
} catch ( error ) {
106
121
this . handleError ( error ) ;
Original file line number Diff line number Diff line change @@ -408,6 +408,24 @@ export type StreamConfig = {
408
408
data : ArrayBuffer ,
409
409
) => Promise < boolean > ;
410
410
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
+
411
429
/**
412
430
* Optional function to customize the setup of HTTP requests for segment downloads.
413
431
* @param segmentUrl URL of the segment.
@@ -631,7 +649,8 @@ export type RequestAbortErrorType = "abort" | "bytes-receiving-timeout";
631
649
export type HttpRequestErrorType =
632
650
| "http-error"
633
651
| "http-bytes-mismatch"
634
- | "http-unexpected-status-code" ;
652
+ | "http-unexpected-status-code"
653
+ | "http-segment-validation-failed" ;
635
654
636
655
/** Defines the types of errors specific to peer-to-peer requests. */
637
656
export type PeerRequestErrorType =
You can’t perform that action at this time.
0 commit comments