blob: 4a0caec5e621cbb469978665513e7f513c2d5057 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import { wait } from '@root-helpers/utils'
import { Segment } from 'p2p-media-loader-core'
import { basename } from 'path'
type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }
const maxRetries = 3
function segmentValidatorFactory (segmentsSha256Url: string, isLive: boolean) {
let segmentsJSON = fetchSha256Segments(segmentsSha256Url)
const regex = /bytes=(\d+)-(\d+)/
return async function segmentValidator (segment: Segment, _method: string, _peerId: string, retry = 1) {
// Wait for hash generation from the server
if (isLive) await wait(1000)
const filename = basename(segment.url)
const segmentValue = (await segmentsJSON)[filename]
if (!segmentValue && retry > maxRetries) {
throw new Error(`Unknown segment name ${filename} in segment validator`)
}
if (!segmentValue) {
console.log('Refetching sha segments for %s.', filename)
await wait(1000)
segmentsJSON = fetchSha256Segments(segmentsSha256Url)
await segmentValidator(segment, _method, _peerId, retry + 1)
return
}
let hashShouldBe: string
let range = ''
if (typeof segmentValue === 'string') {
hashShouldBe = segmentValue
} else {
const captured = regex.exec(segment.range)
range = captured[1] + '-' + captured[2]
hashShouldBe = segmentValue[range]
}
if (hashShouldBe === undefined) {
throw new Error(`Unknown segment name ${filename}/${range} in segment validator`)
}
const calculatedSha = await sha256Hex(segment.data)
if (calculatedSha !== hashShouldBe) {
throw new Error(
`Hashes does not correspond for segment ${filename}/${range}` +
`(expected: ${hashShouldBe} instead of ${calculatedSha})`
)
}
}
}
// ---------------------------------------------------------------------------
export {
segmentValidatorFactory
}
// ---------------------------------------------------------------------------
function fetchSha256Segments (url: string) {
return fetch(url)
.then(res => res.json() as Promise<SegmentsJSON>)
.catch(err => {
console.error('Cannot get sha256 segments', err)
return {}
})
}
async function sha256Hex (data?: ArrayBuffer) {
if (!data) return undefined
if (window.crypto.subtle) {
return window.crypto.subtle.digest('SHA-256', data)
.then(data => bufferToHex(data))
}
// Fallback for non HTTPS context
const shaModule = await import('sha.js')
return new shaModule.sha256().update(Buffer.from(data)).digest('hex')
}
// Thanks: https://stackoverflow.com/a/53307879
function bufferToHex (buffer?: ArrayBuffer) {
if (!buffer) return ''
let s = ''
const h = '0123456789abcdef'
const o = new Uint8Array(buffer)
o.forEach((v: any) => s += h[ v >> 4 ] + h[ v & 15 ])
return s
}
|