aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-13 10:06:28 +0200
committerChocobozzz <me@florianbigard.com>2023-07-17 11:31:46 +0200
commit9bc85bd2f8b5ae1c6675014e2eb0175d2a76ce1a (patch)
tree2ada1285414fd0da5b86e0e0baed7b1aaddafa87 /client/src/assets/player/shared
parent9a26d00e5ed22301733394b82abb1df68e6296e1 (diff)
downloadPeerTube-9bc85bd2f8b5ae1c6675014e2eb0175d2a76ce1a.tar.gz
PeerTube-9bc85bd2f8b5ae1c6675014e2eb0175d2a76ce1a.tar.zst
PeerTube-9bc85bd2f8b5ae1c6675014e2eb0175d2a76ce1a.zip
Prevent multiple sha requests
Diffstat (limited to 'client/src/assets/player/shared')
-rw-r--r--client/src/assets/player/shared/p2p-media-loader/segment-validator.ts19
-rw-r--r--client/src/assets/player/shared/player-options-builder/hls-options-builder.ts1
2 files changed, 17 insertions, 3 deletions
diff --git a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
index a2f7e676d..9cb6344a9 100644
--- a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
+++ b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts
@@ -15,6 +15,8 @@ export class SegmentValidator {
15 15
16 private destroyed = false 16 private destroyed = false
17 17
18 private segmentJSONPromise: Promise<SegmentsJSON>
19
18 constructor (private readonly options: { 20 constructor (private readonly options: {
19 serverUrl: string 21 serverUrl: string
20 segmentsSha256Url: string 22 segmentsSha256Url: string
@@ -23,15 +25,16 @@ export class SegmentValidator {
23 requiresPassword: boolean 25 requiresPassword: boolean
24 videoPassword: () => string 26 videoPassword: () => string
25 }) { 27 }) {
26
27 } 28 }
28 29
29 async validate (segment: Segment, _method: string, _peerId: string, retry = 1) { 30 async validate (segment: Segment, _method: string, _peerId: string, retry = 1) {
30 if (this.destroyed) return 31 if (this.destroyed) return
31 32
33 this.loadSha256SegmentsPromiseIfNeeded()
34
32 const filename = basename(removeQueryParams(segment.url)) 35 const filename = basename(removeQueryParams(segment.url))
33 36
34 const segmentValue = (await this.fetchSha256Segments())[filename] 37 const segmentValue = (await this.segmentJSONPromise)[filename]
35 38
36 if (!segmentValue && retry > maxRetries) { 39 if (!segmentValue && retry > maxRetries) {
37 throw new Error(`Unknown segment name ${filename} in segment validator`) 40 throw new Error(`Unknown segment name ${filename} in segment validator`)
@@ -42,6 +45,8 @@ export class SegmentValidator {
42 45
43 await wait(500) 46 await wait(500)
44 47
48 this.loadSha256SegmentsPromise()
49
45 await this.validate(segment, _method, _peerId, retry + 1) 50 await this.validate(segment, _method, _peerId, retry + 1)
46 51
47 return 52 return
@@ -76,6 +81,16 @@ export class SegmentValidator {
76 this.destroyed = true 81 this.destroyed = true
77 } 82 }
78 83
84 private loadSha256SegmentsPromiseIfNeeded () {
85 if (this.segmentJSONPromise) return
86
87 this.loadSha256SegmentsPromise()
88 }
89
90 private loadSha256SegmentsPromise () {
91 this.segmentJSONPromise = this.fetchSha256Segments()
92 }
93
79 private fetchSha256Segments (): Promise<SegmentsJSON> { 94 private fetchSha256Segments (): Promise<SegmentsJSON> {
80 let headers: { [ id: string ]: string } = {} 95 let headers: { [ id: string ]: string } = {}
81 96
diff --git a/client/src/assets/player/shared/player-options-builder/hls-options-builder.ts b/client/src/assets/player/shared/player-options-builder/hls-options-builder.ts
index 10df2db5d..1658f776c 100644
--- a/client/src/assets/player/shared/player-options-builder/hls-options-builder.ts
+++ b/client/src/assets/player/shared/player-options-builder/hls-options-builder.ts
@@ -108,7 +108,6 @@ export class HLSOptionsBuilder {
108 if (!isSameOrigin(this.options.serverUrl, url)) return 108 if (!isSameOrigin(this.options.serverUrl, url)) return
109 109
110 if (requiresPassword) xhr.setRequestHeader('x-peertube-video-password', this.options.videoPassword()) 110 if (requiresPassword) xhr.setRequestHeader('x-peertube-video-password', this.options.videoPassword())
111
112 else xhr.setRequestHeader('Authorization', this.options.authorizationHeader()) 111 else xhr.setRequestHeader('Authorization', this.options.authorizationHeader())
113 }, 112 },
114 113