hlx-url-rewriter
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -26,12 +26,56 @@ const {URL} = require('url'); | ||
function defaultFunc(url) { | ||
function getUrlType(url) { | ||
if (tryCatch( | ||
() => { | ||
url = new URL(url); | ||
return true; | ||
}, | ||
() => { | ||
return false; | ||
} | ||
)) { | ||
return 'absolute'; | ||
} | ||
if (url.startsWith('//')) { | ||
return 'scheme-relative'; | ||
} | ||
if (url.startsWith('/')) { | ||
return 'path-absolute'; | ||
} | ||
return 'path-relative'; | ||
} | ||
let baseHostName = ''; | ||
function defaultFunc(url, saveAsBaseUrl) { | ||
if (saveAsBaseUrl) { | ||
baseHostName = ''; | ||
} | ||
// Convert absolute urls into relative paths | ||
const obj = getUrlObj(url); | ||
if (!obj) { | ||
// url is already a relative path | ||
return url; | ||
let type = getUrlType(url); | ||
if (type === 'scheme-relative') { | ||
url = `http:${url}`; | ||
type = 'absolute'; | ||
} | ||
return `/${obj.hostname}${obj.pathname}${obj.search}${obj.hash}`; | ||
if (type === 'absolute') { | ||
const obj = getUrlObj(url); | ||
if (saveAsBaseUrl) { | ||
baseHostName = obj.hostname; | ||
} | ||
return `/${obj.hostname}${obj.pathname}${obj.search}${obj.hash}`; | ||
} | ||
if (type === 'path-absolute' && baseHostName) { | ||
return `/${baseHostName}${url}`; | ||
} | ||
return url; | ||
} | ||
module.exports = defaultFunc; |
{ | ||
"name": "hlx-url-rewriter", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A transform stream to modify URLs contained in HLS playlists", | ||
@@ -38,2 +38,3 @@ "main": "index.js", | ||
"capitalized-comments": 0, | ||
"camelcase": 0, | ||
"dot-notation": 0, | ||
@@ -40,0 +41,0 @@ "indent": 0, |
@@ -11,3 +11,3 @@ [![Build Status](https://travis-ci.org/hlxjs/hlx-url-rewriter.svg?branch=master)](https://travis-ci.org/hlxjs/hlx-url-rewriter) | ||
## Features | ||
* Being used with other [`hls-streams`](https://github.com/hls-streams) 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 based on the `rules`. | ||
@@ -14,0 +14,0 @@ ## Install |
@@ -20,6 +20,7 @@ const stream = require('stream'); | ||
function rewrite(playlist, rewriteFunc) { | ||
function rewriteUrl(item = {}) { | ||
const {uri} = item; | ||
if (uri) { | ||
item.uri = rewriteFunc(uri); | ||
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; | ||
} | ||
@@ -34,3 +35,3 @@ } | ||
} | ||
rewriteUrl(playlist); | ||
rewriteUrl(playlist, true); | ||
if (playlist.isMasterPlaylist) { | ||
@@ -37,0 +38,0 @@ const {variants, sessionDataList, sessionKeyList} = playlist; |
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
7006
117