0.0.1 • Published 5 years ago

hls-streams-url-rewriter v0.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Build Status Coverage Status Dependency Status Development Dependency Status XO code style

url-rewriter

A transform stream to modify URLs contained in HLS playlists

Features

  • Being used with other hls-streams objects, it provides a functionality to filter every playlists and rewrite the urls in them based on the rules.

Install

NPM

Usage

const {createReadStream} = require('hls-streams-file-reader');
const {createUrlRewriter} = require('hls-streams-url-rewriter'); // url-rewriter
const {createTerminator} = require('hls-streams-terminator')

const src = createReadStream('https://foo.bar/sample.m3u8');
const rewrite = createUrlRewriter(urlStr => {
  // Convert an absolute url into a relative one
  const url = new URL(urlStr);
  return url.pathname;
});
const dest = createTerminator();

// Rewrite all urls
src.pipe(rewrite).pipe(dest)
.on('data', data => {
  console.log(data.uri); // should be a relative url
});

API

The features are built on top of the Node's transform streams.

createUrlRewriter(rules)

Creates a new TransformStream object.

params

NameTypeRequiredDefaultDescription
rulesfunctionNointernally defined default function (see below)A function that takes an original url string and returns a modified url string. The function is called asynchronously each time the stream encountered a url line or url attribute in playlists.

default function

Default behavior is something like below (pseudo code):

function defaultFunc(url) {
  // Convert absolute urls into relative paths
  if (url is relative) {
    return url;
  } else {
    // Make a root directory with the hostname
    return `/${url.hostname}/${url.pathname}`;
  }
}

return value

An instance of TransformStream.