47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { rewritePlaylist, signUrl, verifyToken } = require('../src/hls');
|
|
|
|
test('rewrites playlist URIs to signed proxy URLs', () => {
|
|
const base = 'http://upstream.example/live/user/pass/index.m3u8';
|
|
const body = [
|
|
'#EXTM3U',
|
|
'#EXT-X-KEY:METHOD=AES-128,URI="keys/key.bin"',
|
|
'#EXTINF:6,',
|
|
'segment1.ts',
|
|
'#EXTINF:6,',
|
|
'http://cdn.example/segment2.ts',
|
|
'#EXT-X-ENDLIST',
|
|
].join('\n');
|
|
|
|
const rewritten = rewritePlaylist(body, base, 2, 'http://proxy.example');
|
|
const lines = rewritten.split('\n');
|
|
|
|
assert.equal(lines[0], '#EXTM3U');
|
|
assert.match(lines[1], /^#EXT-X-KEY:METHOD=AES-128,URI="http:\/\/proxy\.example\/hls\/2\/.+"/);
|
|
assert.match(lines[3], /^http:\/\/proxy\.example\/hls\/2\/.+/);
|
|
assert.match(lines[5], /^http:\/\/proxy\.example\/hls\/2\/.+/);
|
|
|
|
const token = lines[3].split('/hls/2/')[1];
|
|
assert.equal(
|
|
verifyToken(2, token),
|
|
'http://upstream.example/live/user/pass/segment1.ts'
|
|
);
|
|
|
|
const keyToken = lines[1].match(/URI="http:\/\/proxy\.example\/hls\/2\/([^"]+)"/)[1];
|
|
assert.equal(
|
|
verifyToken(2, keyToken),
|
|
'http://upstream.example/live/user/pass/keys/key.bin'
|
|
);
|
|
});
|
|
|
|
test('rejects tampered tokens', () => {
|
|
const token = signUrl('http://upstream.example/segment.ts', 1);
|
|
assert.equal(verifyToken(1, token), 'http://upstream.example/segment.ts');
|
|
assert.equal(verifyToken(2, token), null);
|
|
const tampered = token.slice(0, -1) + (token.endsWith('a') ? 'b' : 'a');
|
|
assert.equal(verifyToken(1, tampered), null);
|
|
});
|