Prevent multiple sha requests

pull/5897/head
Chocobozzz 2023-07-13 10:06:28 +02:00
parent 9a26d00e5e
commit 9bc85bd2f8
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 17 additions and 3 deletions

View File

@ -15,6 +15,8 @@ export class SegmentValidator {
private destroyed = false
private segmentJSONPromise: Promise<SegmentsJSON>
constructor (private readonly options: {
serverUrl: string
segmentsSha256Url: string
@ -23,15 +25,16 @@ export class SegmentValidator {
requiresPassword: boolean
videoPassword: () => string
}) {
}
async validate (segment: Segment, _method: string, _peerId: string, retry = 1) {
if (this.destroyed) return
this.loadSha256SegmentsPromiseIfNeeded()
const filename = basename(removeQueryParams(segment.url))
const segmentValue = (await this.fetchSha256Segments())[filename]
const segmentValue = (await this.segmentJSONPromise)[filename]
if (!segmentValue && retry > maxRetries) {
throw new Error(`Unknown segment name ${filename} in segment validator`)
@ -42,6 +45,8 @@ export class SegmentValidator {
await wait(500)
this.loadSha256SegmentsPromise()
await this.validate(segment, _method, _peerId, retry + 1)
return
@ -76,6 +81,16 @@ export class SegmentValidator {
this.destroyed = true
}
private loadSha256SegmentsPromiseIfNeeded () {
if (this.segmentJSONPromise) return
this.loadSha256SegmentsPromise()
}
private loadSha256SegmentsPromise () {
this.segmentJSONPromise = this.fetchSha256Segments()
}
private fetchSha256Segments (): Promise<SegmentsJSON> {
let headers: { [ id: string ]: string } = {}

View File

@ -108,7 +108,6 @@ export class HLSOptionsBuilder {
if (!isSameOrigin(this.options.serverUrl, url)) return
if (requiresPassword) xhr.setRequestHeader('x-peertube-video-password', this.options.videoPassword())
else xhr.setRequestHeader('Authorization', this.options.authorizationHeader())
},