@warren-bank/hls-proxy 3.4.5 → 3.4.7

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.
@@ -13,6 +13,7 @@ const url_location_landmarks = {
13
13
  same_line: [
14
14
  '#EXT-X-MEDIA:',
15
15
  '#EXT-X-I-FRAME-STREAM-INF:',
16
+ '#EXT-X-IMAGE-STREAM-INF:',
16
17
  '#EXT-X-RENDITION-REPORT:',
17
18
  '#EXT-X-DATERANGE:',
18
19
  '#EXT-X-CONTENT-STEERING:'
@@ -50,19 +50,22 @@ const get_middleware = function(params) {
50
50
 
51
51
  const is_m3u8 = (url_type === 'm3u8')
52
52
 
53
- const send_cache_segment = function(segment) {
54
- res.writeHead(200, { "Content-Type": utils.get_content_type(url_type) })
53
+ const send_cache_segment = function(segment, type) {
54
+ if (!type)
55
+ type = utils.get_content_type(url_type)
56
+
57
+ res.writeHead(200, {"Content-Type": type})
55
58
  res.end(segment)
56
59
  }
57
60
 
58
61
  if (cache_segments && !is_m3u8) {
59
- let segment = await get_segment(url, url_type) // Buffer (cached segment data), false (prefetch is pending: add callback), undefined (no prefetch is pending)
62
+ let {segment, type} = await get_segment(url, url_type) // possible datatypes of segment value: Buffer (cached segment data), false (prefetch is pending: add callback), null (no prefetch is pending)
60
63
 
61
- if (segment && segment.length) { // Buffer (cached segment data)
62
- send_cache_segment(segment)
64
+ if (segment && segment.length) { // Buffer (cached segment data)
65
+ send_cache_segment(segment, type)
63
66
  return
64
67
  }
65
- else if (segment === false) { // false (prefetch is pending: add callback)
68
+ else if (segment === false) { // false (prefetch is pending: add callback)
66
69
  add_listener(url, url_type, send_cache_segment)
67
70
  return
68
71
  }
@@ -89,8 +89,9 @@ module.exports = function(params) {
89
89
  if (segment.has)
90
90
  cache_storage_adapter.remove(segment.state)
91
91
 
92
- segment.has = false
93
- segment.cb = []
92
+ segment.has = false
93
+ segment.cb = []
94
+ segment.type = null
94
95
  }
95
96
 
96
97
  ts.splice(start, count)
@@ -140,7 +141,7 @@ module.exports = function(params) {
140
141
  let i, segment
141
142
 
142
143
  for (i=(ts.length - 1); i>=0; i--) {
143
- segment = ts[i] // {key, has, cb, state}
144
+ segment = ts[i] // {key, has, cb, type, state}
144
145
  if (segment && (segment.key === key)) {
145
146
  index = i
146
147
  break
@@ -181,7 +182,7 @@ module.exports = function(params) {
181
182
 
182
183
  // placeholder to prevent multiple download requests
183
184
  index = ts.length
184
- ts[index] = {key: get_privatekey_from_url(url), has: false, cb: [], state: {}}
185
+ ts[index] = {key: get_privatekey_from_url(url), has: false, cb: [], type: null, state: {}}
185
186
 
186
187
  let options = get_request_options(url, /* is_m3u8= */ false, referer_url)
187
188
  promise = request(options, '', {binary: true, stream: false, cookieJar: cookies.getCookieJar()})
@@ -197,11 +198,12 @@ module.exports = function(params) {
197
198
  touch_access(m3u8_url)
198
199
 
199
200
  const segment = ts[index]
200
- segment.has = true
201
+ segment.has = true
202
+ segment.type = utils.get_content_type(response.headers)
201
203
  cache_storage_adapter.set(segment.state, response)
202
204
  if (segment.cb.length) {
203
205
  segment.cb.forEach((cb) => {
204
- cb(response)
206
+ cb(response, segment.type)
205
207
 
206
208
  debug(1, 'cache (callback complete):', debug_url)
207
209
  })
@@ -228,10 +230,12 @@ module.exports = function(params) {
228
230
  }
229
231
 
230
232
  const get_segment = async function(url, url_type) {
231
- if (! should_prefetch_url(url, url_type)) return undefined
233
+ if (! should_prefetch_url(url, url_type))
234
+ return {segment: null, type: null}
232
235
 
233
236
  let debug_url = (debug_level >= 3) ? url : get_publickey_from_url(url)
234
237
  let segment = find_segment(url)
238
+ let type = null
235
239
 
236
240
  if (segment !== undefined) {
237
241
  const {m3u8_url, index} = segment
@@ -243,6 +247,7 @@ module.exports = function(params) {
243
247
  if (segment.has) {
244
248
  debug(1, 'cache (hit):', debug_url)
245
249
 
250
+ type = segment.type
246
251
  segment = await cache_storage_adapter.get(segment.state)
247
252
 
248
253
  // cleanup: remove all previous segments
@@ -262,11 +267,13 @@ module.exports = function(params) {
262
267
  }
263
268
  else {
264
269
  debug(1, 'cache (miss):', debug_url)
270
+
271
+ segment = null
265
272
  }
266
- return segment
273
+ return {segment, type}
267
274
  }
268
275
 
269
- const add_listener = function(url, url_type, cb) {
276
+ const add_listener = async function(url, url_type, cb) {
270
277
  if (! should_prefetch_url(url, url_type)) return false
271
278
 
272
279
  let debug_url = (debug_level >= 3) ? url : get_publickey_from_url(url)
@@ -280,7 +287,10 @@ module.exports = function(params) {
280
287
  segment = ts[index]
281
288
 
282
289
  if (segment.has) {
283
- cb(segment)
290
+ const type = segment.type
291
+ segment = await cache_storage_adapter.get(segment.state)
292
+
293
+ cb(segment, type)
284
294
 
285
295
  debug(1, 'cache (callback complete):', debug_url)
286
296
  }
@@ -61,8 +61,21 @@ const parse_req_url = function(params, req) {
61
61
  return result
62
62
  }
63
63
 
64
- const get_content_type = function(url_type) {
64
+ const get_content_type = function(data) {
65
65
  let content_type
66
+
67
+ if (!content_type && data && (typeof data === 'object') && data['content-type'])
68
+ content_type = data['content-type']
69
+
70
+ if (!content_type && data && (typeof data === 'string'))
71
+ content_type = get_content_type_from_url_type(data)
72
+
73
+ return content_type
74
+ }
75
+
76
+ const get_content_type_from_url_type = function(url_type) {
77
+ let content_type
78
+
66
79
  switch(url_type) {
67
80
  case 'm3u8':
68
81
  content_type = 'application/x-mpegurl'
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": "3.4.5",
4
+ "version": "3.4.7",
5
5
  "scripts": {
6
6
  "start": "node hls-proxy/bin/hlsd.js",
7
7
  "sudo": "sudo node hls-proxy/bin/hlsd.js"