hlx-url-rewriter
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -52,3 +52,37 @@ const {URL} = require('url'); | ||
function defaultFunc(url, saveAsBaseUrl) { | ||
function defaultFunc(data) { | ||
if (data.type === 'playlist') { | ||
if (data.isMasterPlaylist) { | ||
rewrite(data, true); | ||
const {variants, sessionDataList, sessionKeyList} = data; | ||
for (const variant of variants) { | ||
rewrite(variant); | ||
const {audio, video, subtitles, closedCaptions} = variant; | ||
[audio, video, subtitles, closedCaptions].forEach(rewriteUrls); | ||
} | ||
[sessionDataList, sessionKeyList].forEach(rewriteUrls); | ||
} else { | ||
rewrite(data, true); | ||
rewriteUrls(data.segments); | ||
} | ||
} | ||
} | ||
function rewriteUrls(list) { | ||
for (const item of list) { | ||
rewrite(item); | ||
if (item.type === 'segment') { | ||
rewrite(item.key); | ||
rewrite(item.map); | ||
} | ||
} | ||
} | ||
function rewrite(data, saveAsBaseUrl) { | ||
if (!data || data.__hlx_url_rewriter_visited__) { | ||
return; | ||
} | ||
let {uri} = data; | ||
if (saveAsBaseUrl) { | ||
@@ -58,7 +92,6 @@ baseHostName = ''; | ||
// Convert absolute urls into relative paths | ||
let type = getUrlType(url); | ||
let type = getUrlType(uri); | ||
if (type === 'scheme-relative') { | ||
url = `http:${url}`; | ||
uri = `http:${uri}`; | ||
type = 'absolute'; | ||
@@ -68,16 +101,13 @@ } | ||
if (type === 'absolute') { | ||
const obj = getUrlObj(url); | ||
const obj = getUrlObj(uri); | ||
if (saveAsBaseUrl) { | ||
baseHostName = obj.hostname; | ||
} | ||
return `/${obj.hostname}${obj.pathname}${obj.search}${obj.hash}`; | ||
data.uri = `/${obj.hostname}${obj.pathname}${obj.search}${obj.hash}`; | ||
} else if (type === 'path-absolute' && baseHostName) { | ||
data.uri = `/${baseHostName}${uri}`; | ||
} | ||
if (type === 'path-absolute' && baseHostName) { | ||
return `/${baseHostName}${url}`; | ||
} | ||
return url; | ||
data.__hlx_url_rewriter_visited__ = true; | ||
} | ||
module.exports = defaultFunc; |
{ | ||
"name": "hlx-url-rewriter", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A transform stream to modify URLs contained in HLS playlists", | ||
@@ -12,3 +12,4 @@ "main": "index.js", | ||
"coverage": "nyc npm test && nyc report --reporter=text-lcov | coveralls", | ||
"test": "npm run lint && npm audit --audit-level high && ava --verbose" | ||
"audit": "npm audit --audit-level high", | ||
"test": "npm run lint && npm run audit && ava --verbose" | ||
}, | ||
@@ -15,0 +16,0 @@ "repository": { |
@@ -24,6 +24,8 @@ [![Build Status](https://travis-ci.org/hlxjs/hlx-url-rewriter.svg?branch=master)](https://travis-ci.org/hlxjs/hlx-url-rewriter) | ||
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 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; | ||
} | ||
}); | ||
@@ -47,14 +49,15 @@ const dest = createTerminator(); | ||
| ------- | ------ | -------- | ------- | ------------- | | ||
| rules | function | No | internally 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. | | ||
| rules | function | No | internally defined default function (see below) | A function that takes an hls-parser object and modifies its url string. | | ||
#### default function | ||
Default behavior is something like below (pseudo code): | ||
The default behavior is something like this: | ||
```js | ||
function defaultFunc(url) { | ||
// Convert absolute urls into relative paths | ||
if (url is relative) { | ||
return url; | ||
// pseudo code | ||
function defaultFunc(data) { | ||
if (data.uri is relative) { | ||
// Do nothing | ||
} else { | ||
// Make a root directory with the hostname | ||
return `/${url.hostname}/${url.pathname}`; | ||
// Put in a root directory | ||
const url = new URL(data.uri); | ||
data.uri = `/${url.hostname}/${url.pathname}`; | ||
} | ||
@@ -61,0 +64,0 @@ } |
@@ -11,39 +11,7 @@ const stream = require('stream'); | ||
_transform(data, _, cb) { | ||
if (data.type === 'playlist') { | ||
rewrite(data, this.rules); | ||
} | ||
this.push(data); | ||
cb(); | ||
this.rules(data); | ||
cb(null, data); | ||
} | ||
} | ||
function rewrite(playlist, rewriteFunc) { | ||
function rewriteUrl(item = {}, isBase) { | ||
const {uri, __hlx_url_rewriter_visited__: visited} = item; | ||
if (uri && !visited) { | ||
item.uri = rewriteFunc(uri, isBase); | ||
item.__hlx_url_rewriter_visited__ = true; | ||
} | ||
} | ||
function rewriteUrls(list) { | ||
for (const item of list) { | ||
rewriteUrl(item); | ||
rewriteUrl(item.key); | ||
rewriteUrl(item.map); | ||
} | ||
} | ||
rewriteUrl(playlist, true); | ||
if (playlist.isMasterPlaylist) { | ||
const {variants, sessionDataList, sessionKeyList} = playlist; | ||
[variants, sessionDataList, sessionKeyList].forEach(rewriteUrls); | ||
for (const variant of variants) { | ||
const {audio, video, subtitles, closedCaptions} = variant; | ||
[audio, video, subtitles, closedCaptions].forEach(rewriteUrls); | ||
} | ||
} else { | ||
const {segments} = playlist; | ||
rewriteUrls(segments); | ||
} | ||
} | ||
module.exports = 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
67
6808
114