aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/p2p-media-loader
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/p2p-media-loader')
-rw-r--r--client/src/assets/player/p2p-media-loader/segment-validator.ts37
1 files changed, 31 insertions, 6 deletions
diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts
index 72c32f9e0..0614f73d2 100644
--- a/client/src/assets/player/p2p-media-loader/segment-validator.ts
+++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts
@@ -1,17 +1,42 @@
1import { Segment } from 'p2p-media-loader-core' 1import { Segment } from 'p2p-media-loader-core'
2import { basename } from 'path' 2import { basename } from 'path'
3 3
4type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }
5
4function segmentValidatorFactory (segmentsSha256Url: string) { 6function segmentValidatorFactory (segmentsSha256Url: string) {
5 const segmentsJSON = fetchSha256Segments(segmentsSha256Url) 7 let segmentsJSON = fetchSha256Segments(segmentsSha256Url)
6 const regex = /bytes=(\d+)-(\d+)/ 8 const regex = /bytes=(\d+)-(\d+)/
7 9
8 return async function segmentValidator (segment: Segment) { 10 return async function segmentValidator (segment: Segment, canRefetchSegmentHashes = true) {
9 const filename = basename(segment.url) 11 const filename = basename(segment.url)
10 const captured = regex.exec(segment.range)
11 12
12 const range = captured[1] + '-' + captured[2] 13 const segmentValue = (await segmentsJSON)[filename]
14
15 if (!segmentValue && !canRefetchSegmentHashes) {
16 throw new Error(`Unknown segment name ${filename} in segment validator`)
17 }
18
19 if (!segmentValue) {
20 console.log('Refetching sha segments.')
21
22 // Refetch
23 segmentsJSON = fetchSha256Segments(segmentsSha256Url)
24 segmentValidator(segment, false)
25 return
26 }
27
28 let hashShouldBe: string
29 let range = ''
30
31 if (typeof segmentValue === 'string') {
32 hashShouldBe = segmentValue
33 } else {
34 const captured = regex.exec(segment.range)
35 range = captured[1] + '-' + captured[2]
36
37 hashShouldBe = segmentValue[range]
38 }
13 39
14 const hashShouldBe = (await segmentsJSON)[filename][range]
15 if (hashShouldBe === undefined) { 40 if (hashShouldBe === undefined) {
16 throw new Error(`Unknown segment name ${filename}/${range} in segment validator`) 41 throw new Error(`Unknown segment name ${filename}/${range} in segment validator`)
17 } 42 }
@@ -36,7 +61,7 @@ export {
36 61
37function fetchSha256Segments (url: string) { 62function fetchSha256Segments (url: string) {
38 return fetch(url) 63 return fetch(url)
39 .then(res => res.json()) 64 .then(res => res.json() as Promise<SegmentsJSON>)
40 .catch(err => { 65 .catch(err => {
41 console.error('Cannot get sha256 segments', err) 66 console.error('Cannot get sha256 segments', err)
42 return {} 67 return {}