hlx-url-rewriter
Advanced tools
Comparing version 0.0.8 to 0.0.9
122
default.js
const path = require('path'); | ||
const {URL} = require('url'); | ||
const debug = require('debug'); | ||
const {createUrl} = require('hlx-util'); | ||
const print = debug('hlx-url-rewriter'); | ||
function tryCatch(...params) { | ||
const func = params.shift(); | ||
try { | ||
return func(); | ||
} catch (err) { | ||
if (params.length > 0) { | ||
return tryCatch(...params); | ||
} | ||
throw err; | ||
} | ||
} | ||
function getUrlObj(url) { | ||
return tryCatch( | ||
() => { | ||
return new URL(url); | ||
}, | ||
() => { | ||
return null; | ||
} | ||
); | ||
} | ||
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 baseUrl = null; | ||
function defaultFunc(data) { | ||
if (data.type === 'playlist') { | ||
if (data.isMasterPlaylist) { | ||
rewriteUrl(data, true); | ||
rewriteUrl(data); | ||
const {variants, sessionDataList, sessionKeyList} = data; | ||
for (const variant of variants) { | ||
rewriteUrl(variant); | ||
rewriteUrl(variant, data); | ||
const {audio, video, subtitles, closedCaptions} = variant; | ||
[audio, video, subtitles, closedCaptions].forEach(rewriteUrls); | ||
[audio, video, subtitles, closedCaptions].forEach(list => { | ||
rewriteUrls(list, data); | ||
}); | ||
} | ||
[sessionDataList, sessionKeyList].forEach(rewriteUrls); | ||
[sessionDataList, sessionKeyList].forEach(list => { | ||
rewriteUrls(list, data); | ||
}); | ||
} else { | ||
rewriteUrl(data, true); | ||
rewriteUrls(data.segments); | ||
rewriteUrl(data); | ||
rewriteUrls(data.segments, data); | ||
} | ||
@@ -76,51 +31,34 @@ } else if (data.type === 'segment') { | ||
function rewriteUrls(list) { | ||
function rewriteUrls(list, base) { | ||
for (const item of list) { | ||
rewriteUrl(item); | ||
rewriteUrl(item, base); | ||
} | ||
} | ||
function rewriteUrl(data, saveAsBaseUrl) { | ||
rewrite(data, saveAsBaseUrl); | ||
if (data.type === 'segment') { | ||
rewrite(data.key); | ||
rewrite(data.map); | ||
} | ||
} | ||
function rewrite(data, saveAsBaseUrl) { | ||
function rewriteUrl(data, base = {}) { | ||
if (!data || data.__hlx_url_rewriter_visited__) { | ||
return; | ||
} | ||
let {uri} = data; | ||
print(`\t<<< "${uri}"`); | ||
if (saveAsBaseUrl) { | ||
baseUrl = null; | ||
const {uri, parentUri} = data; | ||
if (parentUri) { | ||
data.uri = rewrite(uri, parentUri); | ||
} else { | ||
data.uri = rewrite(uri, base.uri); | ||
} | ||
let type = getUrlType(uri); | ||
if (type === 'scheme-relative') { | ||
uri = `http:${uri}`; | ||
type = 'absolute'; | ||
if (data.type === 'segment') { | ||
rewriteUrl(data.key, data); | ||
rewriteUrl(data.map, data); | ||
} | ||
if (type === 'absolute') { | ||
const obj = getUrlObj(uri); | ||
if (saveAsBaseUrl) { | ||
baseUrl = obj; | ||
} | ||
data.uri = `${path.join(`/${obj.hostname}`, obj.pathname)}${obj.search}${obj.hash}`; | ||
} else if (type === 'path-absolute' && baseUrl) { | ||
data.uri = path.join(`/${baseUrl.hostname}`, uri); | ||
} else if (type === 'path-relative' && baseUrl) { | ||
data.uri = path.join(`/${baseUrl.hostname}`, path.dirname(baseUrl.pathname), uri); | ||
} | ||
print(`\t>>> "${data.uri}"`); | ||
data.__hlx_url_rewriter_visited__ = true; | ||
} | ||
function rewrite(uri, base) { | ||
print(`\t<<< "${uri}", "${base}"`); | ||
const obj = createUrl(uri, base); | ||
const result = `${path.join(`/${obj.hostname}`, obj.pathname)}${obj.search}${obj.hash}`; | ||
print(`\t>>> "${result}"`); | ||
return result; | ||
} | ||
module.exports = defaultFunc; |
{ | ||
"name": "hlx-url-rewriter", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A transform stream to modify URLs contained in HLS playlists", | ||
@@ -59,3 +59,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^4.1.1" | ||
"debug": "^4.1.1", | ||
"hlx-util": "0.0.2" | ||
}, | ||
@@ -62,0 +63,0 @@ "devDependencies": { |
@@ -5,2 +5,3 @@ [![Build Status](https://travis-ci.org/hlxjs/hlx-url-rewriter.svg?branch=master)](https://travis-ci.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) | ||
@@ -7,0 +8,0 @@ |
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
68
6477
2
76
+ Addedhlx-util@0.0.2
+ Addedhlx-util@0.0.2(transitive)