@warren-bank/hls-proxy 0.20.4 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -154,7 +154,7 @@ options:
154
154
  * HTTP proxy binds to: `80`
155
155
  * HTTPS proxy binds to: `443`
156
156
  * _--req-headers_ is the filepath to a JSON data _Object_ containing key:value pairs
157
- * each _key_ is the name of an HTTP header to send in in every outbound request
157
+ * each _key_ is the name of an HTTP header to send in every outbound request
158
158
  * _--origin_ is the value of the corresponding HTTP request header
159
159
  * _--referer_ is the value of the corresponding HTTP request header
160
160
  * _--useragent_ is the value of the corresponding HTTP request header
@@ -201,6 +201,22 @@ options:
201
201
  * each _key_ is the name of a hook function
202
202
  * each _value_ is the implementation of the corresponding _Function_
203
203
  * hook function signatures:
204
+ * `"add_request_options": (url, is_m3u8) => request_options`
205
+ * conditionally add HTTP request options
206
+ * inputs:
207
+ * `url`
208
+ * string URL
209
+ * `is_m3u8`
210
+ * boolean that indicates whether `url` will request an HLS manifest
211
+ * return value:
212
+ * _Object_ having attributes that are combined with _--req-options_ and used to send the outbound request to `url`
213
+ * `"add_request_headers": (url, is_m3u8) => request_headers`
214
+ * conditionally add HTTP request headers
215
+ * return value:
216
+ * _Object_ containing key:value pairs that are combined with _--req-headers_
217
+ * each _key_ is the name of an HTTP header to send in the outbound request to `url`
218
+ * `"modify_m3u8_content": (m3u8_content, m3u8_url) => new_m3u8_content`
219
+ * conditionally modify the content of .m3u8 files __before__ they are parsed to extract URLs
204
220
  * `"redirect": (url) => new_url`
205
221
  * conditionally redirect the URLs encountered in .m3u8 files __before__ they are modified to pass through the proxy
206
222
  * `"prefetch": (url) => boolean`
@@ -49,21 +49,30 @@ const proxy = function({server, host, is_secure, req_headers, req_options, hooks
49
49
  res.setHeader('Access-Control-Max-Age', '86400')
50
50
  }
51
51
 
52
- const get_request_options = function(url, referer_url) {
53
- if (!req_options && !req_headers && !referer_url) return url
52
+ const get_request_options = function(url, is_m3u8, referer_url) {
53
+ const additional_req_options = (hooks && (hooks instanceof Object) && hooks.add_request_options && (typeof hooks.add_request_options === 'function'))
54
+ ? hooks.add_request_options(url, is_m3u8)
55
+ : null
56
+
57
+ const additional_req_headers = (hooks && (hooks instanceof Object) && hooks.add_request_headers && (typeof hooks.add_request_headers === 'function'))
58
+ ? hooks.add_request_headers(url, is_m3u8)
59
+ : null
60
+
61
+ if (!req_options && !additional_req_options && !req_headers && !additional_req_headers && !referer_url) return url
54
62
 
55
63
  const request_options = Object.assign(
56
64
  {},
57
65
  parse_url(url),
58
- (req_options || {})
66
+ (req_options || {}),
67
+ (additional_req_options || {})
59
68
  )
60
69
 
61
- if (!req_headers && !referer_url) return request_options
62
-
63
70
  request_options.headers = Object.assign(
64
71
  {},
65
- (request_options.headers || {}),
66
- (req_headers || {}),
72
+ (( req_options && req_options.headers) ? req_options.headers : {}),
73
+ ((additional_req_options && additional_req_options.headers) ? additional_req_options.headers : {}),
74
+ (req_headers || {}),
75
+ (additional_req_headers || {}),
67
76
  (referer_url ? {"referer": referer_url, "origin": referer_url.replace(regexs.origin, '$1')} : {})
68
77
  )
69
78
 
@@ -94,6 +103,10 @@ const proxy = function({server, host, is_secure, req_headers, req_options, hooks
94
103
  )}
95
104
 
96
105
  const modify_m3u8_content = function(m3u8_content, m3u8_url, referer_url) {
106
+ if (hooks && (hooks instanceof Object) && hooks.modify_m3u8_content && (typeof hooks.modify_m3u8_content === 'function')) {
107
+ m3u8_content = hooks.modify_m3u8_content(m3u8_content, m3u8_url) || m3u8_content
108
+ }
109
+
97
110
  const base_urls = {
98
111
  "relative": m3u8_url.replace(/[\?#].*$/, '').replace(/[^\/]+$/, ''),
99
112
  "absolute": m3u8_url.replace(/(:\/\/[^\/]+).*$/, '$1')
@@ -470,7 +483,7 @@ const proxy = function({server, host, is_secure, req_headers, req_options, hooks
470
483
  }
471
484
  }
472
485
 
473
- const options = get_request_options(url, referer_url)
486
+ const options = get_request_options(url, is_m3u8, referer_url)
474
487
  debug(1, 'proxying:', url)
475
488
  debug(3, 'm3u8:', (is_m3u8 ? 'true' : 'false'))
476
489
 
@@ -166,7 +166,7 @@ module.exports = function({should_prefetch_url, debug, debug_level, request, get
166
166
  index = ts.length
167
167
  ts[index] = {key: get_privatekey_from_url(url), databuffer: false}
168
168
 
169
- let options = get_request_options(url, referer_url)
169
+ let options = get_request_options(url, /* is_m3u8= */ false, referer_url)
170
170
  promise = request(options, '', {binary: true, stream: false})
171
171
  .then(({redirects, response}) => {
172
172
  debug(1, `prefetch (complete, ${response.length} bytes):`, debug_url)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@warren-bank/hls-proxy",
3
3
  "description": "Node.js server to proxy HLS video streams",
4
- "version": "0.20.4",
4
+ "version": "0.22.0",
5
5
  "scripts": {
6
6
  "start": "node hls-proxy/bin/hlsd.js",
7
7
  "sudo": "sudo node hls-proxy/bin/hlsd.js"