hlx-url-rewriter
Advanced tools
Comparing version 0.0.14 to 0.0.15
@@ -64,41 +64,35 @@ const path = require('path'); | ||
function createFullPath(obj) { | ||
if (obj.protocol === 'file:') { | ||
const {rootPath = '/'} = defaultFunc.options; | ||
if (obj.pathname.startsWith(rootPath)) { | ||
return path.join('/', path.relative(rootPath, obj.pathname)); | ||
} | ||
} | ||
return obj.pathname; | ||
function relativePath(from, to) { | ||
const fromDir = path.dirname(from); | ||
print(`\trelativePath: from="${fromDir}", to="${to}"`); | ||
return path.relative(fromDir, to); | ||
} | ||
function rewrite(uri, base) { | ||
print(`\t<<< "${uri}", "${base}"`); | ||
const obj = createUrl(uri, base); | ||
const baseObj = createUrl(base); | ||
if (!obj || !baseObj) { | ||
const {rootPath = '/'} = defaultFunc.options; | ||
const playlistUrl = createUrl(base); | ||
if (path.isAbsolute(uri) && playlistUrl.protocol === 'file:') { | ||
uri = `file://${path.join(rootPath, uri)}`; | ||
} | ||
print(`\t<<< "${uri}", "${base}", rootPath=${rootPath}`); | ||
const url = createUrl(uri, base); | ||
if (!url) { | ||
print(`\t>>> "${uri}"`); | ||
return uri; | ||
} | ||
if (obj.hostname && baseObj.hostname && obj.hostname !== baseObj.hostname) { | ||
print(`\t>>> "${obj.href}"`); | ||
return obj.href; | ||
let result; | ||
if (playlistUrl && url.protocol === playlistUrl.protocol && url.hostname === playlistUrl.hostname) { | ||
print('\tpattern-a'); | ||
result = relativePath(playlistUrl.pathname, url.pathname); | ||
} else if (playlistUrl && playlistUrl.protocol === 'file:') { | ||
print('\tpattern-b'); | ||
result = relativePath(playlistUrl.pathname, path.join(rootPath, url.hostname, url.pathname)); | ||
} else { | ||
print('\tpattern-c'); | ||
result = `/${url.hostname}${url.pathname}`; | ||
} | ||
if (obj.protocol !== baseObj.protocol) { | ||
print(`\t>>> "${obj.href}"`); | ||
return obj.href; | ||
} | ||
const pathname = createFullPath(obj); | ||
const basePathname = createFullPath(baseObj); | ||
print(`\tpathname=${pathname}, basePathname=${basePathname}`); | ||
const result = path.relative(path.dirname(basePathname), pathname); | ||
print(`\t>>> "${result}"`); | ||
return `${result}${obj.search}${obj.hash}`; | ||
return `${result}${url.search}${url.hash}`; | ||
} | ||
module.exports = defaultFunc; |
{ | ||
"name": "hlx-url-rewriter", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "A transform stream to modify URLs contained in HLS playlists", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,6 +9,8 @@ [![Build Status](https://travis-ci.org/hlxjs/hlx-url-rewriter.svg?branch=master)](https://travis-ci.org/hlxjs/hlx-url-rewriter) | ||
# hlx-url-rewriter | ||
A transform stream to modify URLs contained in HLS playlists | ||
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 based on the `rules`. | ||
* 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") | ||
@@ -50,16 +52,29 @@ ## Install | ||
| ------- | ------ | -------- | ------- | ------------- | | ||
| rules | function | No | internally defined default function (see below) | A function that takes an hls-parser object and modifies its url string. | | ||
| 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 | ||
The default behavior is something like this: | ||
```js | ||
// pseudo code | ||
function defaultFunc(data) { | ||
if (data.uri is relative) { | ||
// Do nothing | ||
} else { | ||
// Put in a root directory | ||
const url = new URL(data.uri); | ||
data.uri = `/${url.hostname}/${url.pathname}`; | ||
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) { | ||
return url | ||
} | ||
if (url is a file url || url and playlistUrl share the same 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 "/{url.hostname}/{url.pathname}" | ||
} | ||
@@ -72,5 +87,5 @@ } | ||
| ----------- | ------ | ------- | --------------------------------- | | ||
| 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. | | ||
| 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`. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9171
89
119