1 import { Segment } from 'p2p-media-loader-core'
2 import { basename } from 'path'
4 function segmentValidatorFactory (segmentsSha256Url: string) {
5 const segmentsJSON = fetchSha256Segments(segmentsSha256Url)
6 const regex = /bytes=(\d+)-(\d+)/
8 return async function segmentValidator (segment: Segment) {
9 const filename = basename(segment.url)
10 const captured = regex.exec(segment.range)
12 const range = captured[1] + '-' + captured[2]
14 const hashShouldBe = (await segmentsJSON)[filename][range]
15 if (hashShouldBe === undefined) {
16 throw new Error(`Unknown segment name ${filename}/${range} in segment validator`)
19 const calculatedSha = bufferToEx(await sha256(segment.data))
20 if (calculatedSha !== hashShouldBe) {
22 `Hashes does not correspond for segment ${filename}/${range}` +
23 `(expected: ${hashShouldBe} instead of ${calculatedSha})`
29 // ---------------------------------------------------------------------------
32 segmentValidatorFactory
35 // ---------------------------------------------------------------------------
37 function fetchSha256Segments (url: string) {
39 .then(res => res.json())
41 console.error('Cannot get sha256 segments', err)
46 function sha256 (data?: ArrayBuffer) {
47 if (!data) return undefined
49 return window.crypto.subtle.digest('SHA-256', data)
52 // Thanks: https://stackoverflow.com/a/53307879
53 function bufferToEx (buffer?: ArrayBuffer) {
54 if (!buffer) return ''
57 const h = '0123456789abcdef'
58 const o = new Uint8Array(buffer)
60 o.forEach((v: any) => s += h[ v >> 4 ] + h[ v & 15 ])