# hlx-url-rewriter

> A transform stream to modify URLs contained in HLS playlists

Latest version **0.0.25** (published 2019-09-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install hlx-url-rewriter
pnpm add hlx-url-rewriter
yarn add hlx-url-rewriter
bun add hlx-url-rewriter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.25 |
| Published | 2019-09-29 |
| First published | 2019-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 2 |
| Unpacked size | 9.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Kuu Miyazaki |
| Maintainers | kuu |
| Keywords | video, HLS, stream, media |

## Links

- npm: https://www.npmjs.com/package/hlx-url-rewriter
- Repository: https://github.com/hlxjs/hlx-url-rewriter
- Homepage: https://github.com/hlxjs/hlx-url-rewriter#readme
- Issues: https://github.com/hlxjs/hlx-url-rewriter/issues
- npm.io page: https://npm.io/package/hlx-url-rewriter

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.1.1
- [hlx-util](https://npm.io/package/hlx-util.md) 0.0.10

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.0.25 (latest) — 2019-09-29
- 0.0.24 — 2019-09-28
- 0.0.23 — 2019-09-28
- 0.0.22 — 2019-09-28
- 0.0.21 — 2019-09-22
- 0.0.20 — 2019-09-11
- 0.0.19 — 2019-09-10
- 0.0.18 — 2019-09-09
- 0.0.17 — 2019-09-09
- 0.0.16 — 2019-09-07
- 0.0.15 — 2019-09-07
- 0.0.14 — 2019-08-11
- 0.0.13 — 2019-08-04
- 0.0.12 — 2019-07-28
- 0.0.11 — 2019-07-28
- … 10 more at https://npm.io/package/hlx-url-rewriter/versions

## README

[![Build Status](https://travis-ci.org/hlxjs/hlx-url-rewriter.svg?branch=master)](https://travis-ci.org/hlxjs/hlx-url-rewriter)
[![Coverage Status](https://coveralls.io/repos/github/hlxjs/hlx-url-rewriter/badge.svg?branch=master)](https://coveralls.io/github/hlxjs/hlx-url-rewriter?branch=master)
[![Dependency Status](https://david-dm.org/hlxjs/hlx-url-rewriter.svg)](https://david-dm.org/hlxjs/hlx-url-rewriter)
[![Development Dependency Status](https://david-dm.org/hlxjs/hlx-url-rewriter/dev-status.svg)](https://david-dm.org/hlxjs/hlx-url-rewriter#info=devDependencies)
[![Known Vulnerabilities](https://snyk.io/test/github/hlxjs/hlx-url-rewriter/badge.svg)](https://snyk.io/test/github/hlxjs/hlx-url-rewriter)
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)

# hlx-url-rewriter
A transform stream to rewrite URLs in HLS playlists

## Features
* Being used with other [`hlx`](https://github.com/hlxjs) objects, it provides a functionality to filter every playlists and rewrite the urls in them.
* You can specify a custom function to parse/modify playlists or the default function will be used.
* The default function rewrites urls in accordance with the rules described below (See "default function")

## Install
[![NPM](https://nodei.co/npm/hlx-url-rewriter.png?mini=true)](https://nodei.co/npm/hlx-url-rewriter/)

## Usage

```js
const {createReadStream} = require('hlx-file-reader');
const {createUrlRewriter} = require('hlx-url-rewriter'); // url-rewriter
const {createTerminator} = require('hlx-terminator')

const src = createReadStream('https://foo.bar/sample.m3u8');
const rewrite = createUrlRewriter(data => {
  // Convert playlist's urls from absolute to relative
  if (data.type === 'playlist') {
    const url = new URL(data.uri);
    data.uri = 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](https://nodejs.org/api/stream.html#stream_class_stream_transform).

### `createUrlRewriter([rules, options])`
Creates a new `TransformStream` object.

#### params
| Name    | Type   | Required | Default | Description   |
| ------- | ------ | -------- | ------- | ------------- |
| rewriteFunc | function | No       | An internally defined default function | A function that takes an hls-parser object and modifies url strings included in the object. |
| options | function | No       | see below | An object preserving options |

#### default function
Pseudo code:
```
function defaultFunc(playlist, playlistUrl) {
  for each url contained in the playlist {
    if (url is not an absolute url) {
      resolve url with playlistUrl to make it absolute
        ex1: "http://example.com/path/to/file" + "../../path2/to/file" = "http://example.com/path2/to/file"
        ex2: "file:///path/to/file" + "../../path2/to/file" = "file:///path2/to/file"
        ex3: "http://example.com/path/to/file" + "/path2/to/file" = "http://example.com/path2/to/file"
        ex4: "file:///path/to/file" + "/path2/to/file" = "file://{options.rootPath}/path/to/file"
      }
    }

    if (url is not an absolute url || playlistUrl == '') {
      return url
    }
    if (url and playlistUrl share the same protocol AND hostname) {
      return a relative path from playlistUrl.pathname to url.pathname
    }
    if (playlistUrl is a file url) {
      return a relative path from playlistUrl.pathname to "{options.rootPath}/{url.hostname}/{url.pathname}"
    }
    return a relative path from playlistUrl.pathname to "/{url.hostname}/{url.pathname}"
  }
}
```

#### options
| Name        | Type   | Default | Description                       |
| ----------- | ------ | ------- | --------------------------------- |
| rootPath | string | "/" | This value will be used by the default function to convert a file url into a relative path. |

#### return value
An instance of `TransformStream`.

---
_Source: https://npm.io/package/hlx-url-rewriter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
