hlx-url-rewriter
A transform stream to modify URLs contained in HLS playlists
Features
- Being used with other
hlx
objects, it provides a functionality to filter every playlists and rewrite the urls in them based on the rules
.
Install
Usage
const {createReadStream} = require('hlx-file-reader');
const {createUrlRewriter} = require('hlx-url-rewriter');
const {createTerminator} = require('hlx-terminator')
const src = createReadStream('https://foo.bar/sample.m3u8');
const rewrite = createUrlRewriter(data => {
if (data.type === 'playlist') {
const url = new URL(data.uri);
data.uri = url.pathname;
}
});
const dest = createTerminator();
src.pipe(rewrite).pipe(dest)
.on('data', data => {
console.log(data.uri);
});
API
The features are built on top of the Node's transform streams.
createUrlRewriter([rules, options])
Creates a new TransformStream
object.
params
Name | Type | Required | Default | Description |
---|
rules | function | No | internally defined default function (see below) | A function that takes an hls-parser object and modifies its url string. |
options | function | No | see below | An object preserving options |
default function
The default behavior is something like this:
function defaultFunc(data) {
if (data.uri is relative) {
} else {
const url = new URL(data.uri);
data.uri = `/${url.hostname}/${url.pathname}`;
}
}
options
Name | Type | Default | Description |
---|
rootPath | string | "/" | Required if file urls are included in playlists. A file url will be converted to an absolute path assuming the rootPath is the root. |
return value
An instance of TransformStream
.