@warren-bank/hls-proxy 0.19.0 → 0.20.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.
@@ -191,10 +191,29 @@ const proxy = function({server, host, is_secure, req_headers, req_options, hooks
191
191
  // only used with prefetch
192
192
  const perform_prefetch = (cache_segments)
193
193
  ? (urls, dont_touch_access) => {
194
- urls.forEach((matching_url, index) => {
195
- prefetch_segment(m3u8_url, matching_url, referer_url, dont_touch_access)
194
+ if (!urls || !Array.isArray(urls) || !urls.length)
195
+ return
196
196
 
197
- urls[index] = undefined
197
+ let promise
198
+
199
+ if (is_vod || has_cache(m3u8_url)) {
200
+ promise = Promise.resolve()
201
+ }
202
+ else {
203
+ const matching_url = urls[0]
204
+ urls[0] = undefined
205
+
206
+ promise = prefetch_segment(m3u8_url, matching_url, referer_url, dont_touch_access)
207
+ }
208
+
209
+ promise.then(() => {
210
+ urls.forEach((matching_url, index) => {
211
+ if (matching_url) {
212
+ prefetch_segment(m3u8_url, matching_url, referer_url, dont_touch_access)
213
+
214
+ urls[index] = undefined
215
+ }
216
+ })
198
217
  })
199
218
  }
200
219
  : null
@@ -142,7 +142,9 @@ module.exports = function({should_prefetch_url, debug, debug_level, request, get
142
142
  }
143
143
 
144
144
  const prefetch_segment = function(m3u8_url, url, referer_url, dont_touch_access) {
145
- if (! should_prefetch_url(url)) return
145
+ let promise = Promise.resolve()
146
+
147
+ if (! should_prefetch_url(url)) return promise
146
148
 
147
149
  if (cache[m3u8_url] === undefined) {
148
150
  // initialize a new data structure
@@ -165,7 +167,7 @@ module.exports = function({should_prefetch_url, debug, debug_level, request, get
165
167
  ts[index] = {key: get_privatekey_from_url(url), databuffer: false}
166
168
 
167
169
  let options = get_request_options(url, referer_url)
168
- request(options, '', {binary: true, stream: false})
170
+ promise = request(options, '', {binary: true, stream: false})
169
171
  .then(({response}) => {
170
172
  debug(1, `prefetch (complete, ${response.length} bytes):`, debug_url)
171
173
 
@@ -201,6 +203,8 @@ module.exports = function({should_prefetch_url, debug, debug_level, request, get
201
203
  if (index !== undefined) ts_garbage_collect(m3u8_url, index, 1)
202
204
  })
203
205
  }
206
+
207
+ return promise
204
208
  }
205
209
 
206
210
  const get_segment = function(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.19.0",
4
+ "version": "0.20.0",
5
5
  "scripts": {
6
6
  "start": "node hls-proxy/bin/hlsd.js",
7
7
  "sudo": "sudo node hls-proxy/bin/hlsd.js"