swarmcloud-hls 2.0.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 ADDED
@@ -0,0 +1,43 @@
1
+ **English | [简体中文](Readme_zh.md)**
2
+
3
+ <h1 align="center"><a href="" target="_blank" rel="noopener noreferrer"><img width="250" src="https://swarmcloud.net/logo.png" alt="logo"></a></h1>
4
+ <h4 align="center">Let your viewers become your unlimitedly scalable CDN.</h4>
5
+ <p align="center">
6
+ <a href="https://www.npmjs.com/package/swarmcloud-hls"><img src="https://img.shields.io/npm/v/swarmcloud-hls.svg?style=flat" alt="npm"></a>
7
+ <a href="https://www.jsdelivr.com/package/npm/swarmcloud-hls"><img src="https://data.jsdelivr.com/v1/package/npm/swarmcloud-hls/badge" alt="jsdelivr"></a>
8
+ </p>
9
+
10
+ This SDK integrates 2 p2p streaming engines, one is [hlsjs based](https://swarmcloud.net/views/web/introduction.html), another is [ServiceWorker based](https://swarmcloud.net/views/hls-sw/introduction.html).
11
+ <br>
12
+ As you might expect, it supports all modern browsers (including iOS Safari) and all web players (including players without hls.js built-in)!
13
+
14
+ ## Browser Support
15
+ WebRTC has already been incorporated into the HTML5 standard and it is broadly deployed in modern browsers. The compatibility of SwarmCloud depends on the browser support of WebRTC, MediaSource API or Service Worker.
16
+
17
+ Compatibility|Chrome | Firefox | macOS Safari | Opera | Edge | iOS Safari | IE |
18
+ :-: | :-: | :-: | :-: | :-: | :-:| :-:| :-:
19
+ WebRTC Datachannel | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
20
+ MSE | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ | ❌ |
21
+ Service Worker | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
22
+ SwarmCloud | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ❌ |
23
+
24
+ ## Usage
25
+ See [documentation](https://swarmcloud.net/en/views/hls/usage.html)
26
+
27
+ ## Related Projects
28
+ - [android-p2p-engine](https://github.com/cdnbye/android-p2p-engine) - Live/VOD P2P Engine for Android and Android TV.
29
+ - [ios-p2p-engine](https://github.com/cdnbye/ios-p2p-engine) - iOS Video P2P Engine for Any Player.
30
+ - [flutter-p2p-engine](https://github.com/cdnbye/flutter-p2p-engine) - Live/VOD P2P Engine for Flutter, contributed by [mjl0602](https://github.com/mjl0602).
31
+ - [shaka-p2p-engine](https://github.com/cdnbye/shaka-p2p-engine) - P2P engine for Shaka Player.
32
+ - [dashjs-p2p-engine](https://github.com/cdnbye/dashjs-p2p-engine) - Web Video Delivery Technology with No Plugins for MPEG-dash.
33
+
34
+ ## FAQ
35
+ We have collected some [frequently asked questions](https://www.hdtvcloud.com/en/views/FAQ.html). Before reporting an issue, please search if the FAQ has the answer to your problem.
36
+
37
+ ## Contact Us
38
+ Email: service@cdnbye.com
39
+ <br>
40
+ Telegram: @cdnbye
41
+ <br>
42
+ Skype: live:86755838
43
+
@@ -0,0 +1 @@
1
+ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.SwarmCloudClapprPlugin=e():t.SwarmCloudClapprPlugin=e()}("undefined"!=typeof self?self:this,function(){return function(t){function e(n){if(o[n])return o[n].exports;var r=o[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var o={};return e.m=t,e.c=o,e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,o){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function r(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function i(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}var p=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),s=function t(e,o,n){null===e&&(e=Function.prototype);var r=Object.getOwnPropertyDescriptor(e,o);if(void 0===r){var i=Object.getPrototypeOf(e);return null===i?void 0:t(i,o,n)}if("value"in r)return r.value;var p=r.get;if(void 0!==p)return p.call(n)};window.P2pEngineHls||console.error("Please put p2p-engine script before this script"),window.Clappr||console.error("Please put clappr script before this script");var l=function(t){function e(t){n(this,e);var o=r(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return o._engine=null,o}return i(e,t),p(e,[{key:"_setup",value:function(){s(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"_setup",this).call(this),window.P2pEngineHls.isSupported()&&(this.options.playback.hlsjsConfig||(this.options.playback.hlsjsConfig={}),this.options.playback.hlsjsConfig.p2pConfig||(this.options.playback.hlsjsConfig.p2pConfig={}),this.options.playback.hlsjsConfig.p2pConfig.hlsjsInstance=this._hls,this._engine=new window.P2pEngineHls(this.options.playback.hlsjsConfig.p2pConfig))}},{key:"stop",value:function(){this._engine&&this._engine.destroy(),s(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"stop",this).call(this)}},{key:"destroy",value:function(){this._engine&&this._engine.destroy(),s(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"destroy",this).call(this)}},{key:"name",get:function(){return"cdnbye_playback"}}]),e}(window.Clappr.HLS);l.canPlay=window.Clappr.HLS.canPlay,t.exports=window.CDNByeClapprPlugin=l}])});
@@ -0,0 +1 @@
1
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.HlsProxy=t():e.HlsProxy=t()}(self,(()=>(()=>{var e={622:function(e){var t,r,n,o,a;t=/^(?=((?:[a-zA-Z0-9+\-.]+:)?))\1(?=((?:\/\/[^\/?#]*)?))\2(?=((?:(?:[^?#\/]*\/)*[^;?#\/]*)?))\3((?:;[^?#]*)?)(\?[^#]*)?(#[^]*)?$/,r=/^(?=([^\/?#]*))\1([^]*)$/,n=/(?:\/|^)\.(?=\/)/g,o=/(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g,a={buildAbsoluteURL:function(e,t,n){if(n=n||{},e=e.trim(),!(t=t.trim())){if(!n.alwaysNormalize)return e;var o=a.parseURL(e);if(!o)throw new Error("Error trying to parse base URL.");return o.path=a.normalizePath(o.path),a.buildURLFromParts(o)}var i=a.parseURL(t);if(!i)throw new Error("Error trying to parse relative URL.");if(i.scheme)return n.alwaysNormalize?(i.path=a.normalizePath(i.path),a.buildURLFromParts(i)):t;var s=a.parseURL(e);if(!s)throw new Error("Error trying to parse base URL.");if(!s.netLoc&&s.path&&"/"!==s.path[0]){var u=r.exec(s.path);s.netLoc=u[1],s.path=u[2]}s.netLoc&&!s.path&&(s.path="/");var f={scheme:s.scheme,netLoc:i.netLoc,path:null,params:i.params,query:i.query,fragment:i.fragment};if(!i.netLoc&&(f.netLoc=s.netLoc,"/"!==i.path[0]))if(i.path){var l=s.path,c=l.substring(0,l.lastIndexOf("/")+1)+i.path;f.path=a.normalizePath(c)}else f.path=s.path,i.params||(f.params=s.params,i.query||(f.query=s.query));return null===f.path&&(f.path=n.alwaysNormalize?a.normalizePath(i.path):i.path),a.buildURLFromParts(f)},parseURL:function(e){var r=t.exec(e);return r?{scheme:r[1]||"",netLoc:r[2]||"",path:r[3]||"",params:r[4]||"",query:r[5]||"",fragment:r[6]||""}:null},normalizePath:function(e){for(e=e.split("").reverse().join("").replace(n,"");e.length!==(e=e.replace(o,"")).length;);return e.split("").reverse().join("")},buildURLFromParts:function(e){return e.scheme+e.netLoc+e.path+e.params+e.query+e.fragment}},e.exports=a},47:(e,t)=>{"use strict";t.l=o;var r=2147483647;function n(e){if(e>r)throw new RangeError('The value "'+e+'" is invalid for option "size"');var t=new Uint8Array(e);return t.__proto__=o.prototype,t}function o(e,t,r){if("number"==typeof e){if("string"==typeof t)throw new TypeError('The "string" argument must be of type string. Received type number');return s(e)}return a(e,t,r)}function a(e,t,r){if("string"==typeof e)return function(e,t){"string"==typeof t&&""!==t||(t="utf8");if(!o.isEncoding(t))throw new TypeError("Unknown encoding: "+t);var r=0|l(e,t),a=n(r),i=a.write(e,t);i!==r&&(a=a.slice(0,i));return a}(e,t);if(ArrayBuffer.isView(e))return u(e);if(null==e)throw TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof e);if(h(e,ArrayBuffer)||e&&h(e.buffer,ArrayBuffer))return function(e,t,r){if(t<0||e.byteLength<t)throw new RangeError('"offset" is outside of buffer bounds');if(e.byteLength<t+(r||0))throw new RangeError('"length" is outside of buffer bounds');var n;n=void 0===t&&void 0===r?new Uint8Array(e):void 0===r?new Uint8Array(e,t):new Uint8Array(e,t,r);return n.__proto__=o.prototype,n}(e,t,r);if("number"==typeof e)throw new TypeError('The "value" argument must not be of type number. Received type number');var a=e.valueOf&&e.valueOf();if(null!=a&&a!==e)return o.from(a,t,r);var i=function(e){if(o.isBuffer(e)){var t=0|f(e.length),r=n(t);return 0===r.length||e.copy(r,0,0,t),r}if(void 0!==e.length)return"number"!=typeof e.length||function(e){return e!=e}(e.length)?n(0):u(e);if("Buffer"===e.type&&Array.isArray(e.data))return u(e.data)}(e);if(i)return i;if("undefined"!=typeof Symbol&&null!=Symbol.toPrimitive&&"function"==typeof e[Symbol.toPrimitive])return o.from(e[Symbol.toPrimitive]("string"),t,r);throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof e)}function i(e){if("number"!=typeof e)throw new TypeError('"size" argument must be of type number');if(e<0)throw new RangeError('The value "'+e+'" is invalid for option "size"')}function s(e){return i(e),n(e<0?0:0|f(e))}function u(e){for(var t=e.length<0?0:0|f(e.length),r=n(t),o=0;o<t;o+=1)r[o]=255&e[o];return r}function f(e){if(e>=r)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+r.toString(16)+" bytes");return 0|e}function l(e,t){if(o.isBuffer(e))return e.length;if(ArrayBuffer.isView(e)||h(e,ArrayBuffer))return e.byteLength;if("string"!=typeof e)throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type '+typeof e);var r=e.length,n=arguments.length>2&&!0===arguments[2];if(!n&&0===r)return 0;for(var a=!1;;)switch(t){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":return c(e).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;default:if(a)return n?-1:c(e).length;t=(""+t).toLowerCase(),a=!0}}function c(e,t){var r;t=t||1/0;for(var n=e.length,o=null,a=[],i=0;i<n;++i){if((r=e.charCodeAt(i))>55295&&r<57344){if(!o){if(r>56319){(t-=3)>-1&&a.push(239,191,189);continue}if(i+1===n){(t-=3)>-1&&a.push(239,191,189);continue}o=r;continue}if(r<56320){(t-=3)>-1&&a.push(239,191,189),o=r;continue}r=65536+(o-55296<<10|r-56320)}else o&&(t-=3)>-1&&a.push(239,191,189);if(o=null,r<128){if((t-=1)<0)break;a.push(r)}else if(r<2048){if((t-=2)<0)break;a.push(r>>6|192,63&r|128)}else if(r<65536){if((t-=3)<0)break;a.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((t-=4)<0)break;a.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return a}function h(e,t){return e instanceof t||null!=e&&null!=e.constructor&&null!=e.constructor.name&&e.constructor.name===t.name}"undefined"!=typeof Symbol&&null!=Symbol.species&&o[Symbol.species]===o&&Object.defineProperty(o,Symbol.species,{value:null,configurable:!0,enumerable:!1,writable:!1}),o.from=function(e,t,r){return a(e,t,r)},o.prototype.__proto__=Uint8Array.prototype,o.__proto__=Uint8Array,o.alloc=function(e,t,r){return function(e,t,r){return i(e),e<=0?n(e):void 0!==t?"string"==typeof r?n(e).fill(t,r):n(e).fill(t):n(e)}(e,t,r)},o.allocUnsafe=function(e){return s(e)},o.isBuffer=function(e){return null!=e&&!0===e._isBuffer&&e!==o.prototype},o.isEncoding=function(e){switch(String(e).toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"latin1":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return!0;default:return!1}},o.concat=function(e,t){if(!Array.isArray(e))throw new TypeError('"list" argument must be an Array of Buffers');if(0===e.length)return o.alloc(0);var r;if(void 0===t)for(t=0,r=0;r<e.length;++r)t+=e[r].length;var n=o.allocUnsafe(t),a=0;for(r=0;r<e.length;++r){var i=e[r];if(h(i,Uint8Array)&&(i=o.from(i)),!o.isBuffer(i))throw new TypeError('"list" argument must be an Array of Buffers');i.copy(n,a),a+=i.length}return n},o.byteLength=l,o.prototype._isBuffer=!0,o.prototype.copy=function(e,t,r,n){if(!o.isBuffer(e))throw new TypeError("argument should be a Buffer");if(r||(r=0),n||0===n||(n=this.length),t>=e.length&&(t=e.length),t||(t=0),n>0&&n<r&&(n=r),n===r)return 0;if(0===e.length||0===this.length)return 0;if(t<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),e.length-t<n-r&&(n=e.length-t+r);var a=n-r;if(this===e&&"function"==typeof Uint8Array.prototype.copyWithin)this.copyWithin(t,r,n);else if(this===e&&r<t&&t<n)for(var i=a-1;i>=0;--i)e[i+t]=this[i+r];else Uint8Array.prototype.set.call(e,this.subarray(r,n),t);return a}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var a=t[n]={exports:{}};return e[n].call(a.exports,a,a.exports,r),a.exports}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n={};return(()=>{"use strict";r.d(n,{default:()=>w});class e{static insertTimeOffsetTag(e,t){const r=e.split("\n");for(let e=0;e<r.length;e++)if(r[e].startsWith("#EXT-X-VERSION")){r[e]=`${r[e]}\n#EXT-X-START:TIME-OFFSET=${t}`;break}return r.join("\n")}}const t={DC_SIGNAL:"SIGNAL",DC_OPEN:"OPEN",DC_REQUEST:"REQUEST",DC_PIECE_NOT_FOUND:"PIECE_NOT_FOUND",DC_PIECE_ABORT:"PIECE_ABORT",DC_CLOSE:"CLOSE",DC_RESPONSE:"RESPONSE",DC_ERROR:"ERROR",DC_PIECE:"PIECE",DC_PIECE_DATA:"PIECE_DATA",DC_TIMEOUT:"TIMEOUT",DC_PIECE_ACK:"PIECE_ACK",DC_METADATA:"METADATA",DC_PLAT_ANDROID:"ANDROID",DC_PLAT_IOS:"IOS",DC_PLAT_WEB:"WEB",DC_CHOKE:"CHOKE",DC_UNCHOKE:"UNCHOKE",DC_HAVE:"HAVE",DC_LOST:"LOST",DC_GET_PEERS:"GET_PEERS",DC_PEERS:"PEERS",DC_STATS:"STATS",DC_SUBSCRIBE:"SUBSCRIBE",DC_UNSUBSCRIBE:"UNSUBSCRIBE",DC_SUBSCRIBE_ACCEPT:"SUBSCRIBE_ACCEPT",DC_SUBSCRIBE_REJECT:"SUBSCRIBE_REJECT",DC_SUBSCRIBE_LEVEL:"SUBSCRIBE_LEVEL",DC_PEER_SIGNAL:"PEER_SIGNAL",DC_PLAYLIST:"PLAYLIST",BM_LOST:"lost",BM_ADDED_SEG_:"BM_ADDED_SEG_",BM_ADDED_SN_:"BM_ADDED_SN_",BM_SEG_ADDED:"BM_SEG_ADDED",FRAG_CHANGED:"FRAG_CHANGED",FRAG_LOADED:"FRAG_LOADED",FRAG_LOADING:"FRAG_LOADING",RESTART_P2P:"RESTART_P2P",EXCEPTION:"exception",ERROR:"error",SW_PLAYLIST:"SW_PLAYLIST",SW_GET_PLAYLIST:"SW_GET_PLAYLIST",SW_MEDIA:"SW_MEDIA",SW_GET_MEDIA:"SW_GET_MEDIA",LEVEL_LOADED:"LEVEL_LOADED",MANIFEST_PARSED:"MANIFEST_PARSED",FRAG_LOADED:"FRAG_LOADED",SCH_DCHAVE:"SCH_DCHAVE"};var o=r(622);function a(e){const t=new Headers;return e&&t.set("range",e),t}const i=r(47).l,s=["m3u8"],u=1e3,f=["ts","mp4","m4s"],l="#EXT-X-ENDLIST\n",c=.01;let h=!1,p=!1;const E=new Map,d=new class{constructor(){this.sendMessageToClient=this.sendMessageToClient.bind(this)}async sendMessageToClient(e,t,r){return e?new Promise(((n,o)=>{const a=new MessageChannel;let i;const s=(e,t)=>setTimeout((()=>{a.port1.close(),a.port2.close(),o(`MessageChannel ${t} timed out after ${e} ms`)}),e);a.port1.onmessage=function(e){const a=e.data;a?a.pong?(clearTimeout(i),i=s(r,`${t.action}-data`)):n({data:a.data}):o("no data in event")},a.port1.onmessageerror=function(e){o(e)},e.postMessage(t,[a.port2]),i=s(50,`${t.action}-pong`)})):Promise.reject("client is null")}};let y=!1,g=null,_=null,m=!0;class w{constructor(e={}){g=e.httpHeadersForPlaylist?e.httpHeadersForPlaylist:null,_=e.httpHeadersForMediaFile?e.httpHeadersForMediaFile:null,!1===e.insertTimeOffsetTag&&(m=!1),e.allowedMediaFiles&&e.allowedMediaFiles.length>0&&f.push(...e.allowedMediaFiles),e.allowedPlaylistSuffix&&e.allowedPlaylistSuffix.length>0&&s.push(...e.allowedPlaylistSuffix)}}self.addEventListener("install",(()=>{self.skipWaiting()})),self.addEventListener("activate",(e=>e.waitUntil(self.clients.claim()))),self.addEventListener("fetch",(function(r){const{request:n,clientId:w}=r;if("GET"!==n.method)return;const{url:A}=n,T=function(e){const t=o.parseURL(e);return t.path.substring(t.path.lastIndexOf(".")+1)}(A);if(s.includes(T))return h&&console.info(`sw onFetch playlist ${A}`),r.respondWith(async function(r,n){E.size>50&&E.clear();const{url:o}=r,a=await clients.get(n);if(p&&a)try{const r=await d.sendMessageToClient(a,{action:t.SW_GET_PLAYLIST,data:{url:o}},200),{data:n}=r;if(n&&n.text){let{text:t}=n;return m&&!t.endsWith(l)&&(t=e.insertTimeOffsetTag(t,c)),new Response(t,{status:200,statusText:"OK",headers:{"Content-Type":"application/vnd.apple.mpegurl"}})}}catch(e){console.error(e)}let i;g&&(h&&console.info(`set additional header for ${o}`),i=new Headers,g(o,i,r.headers));return fetch(new Request(o,{mode:"cors",headers:i})).then((async r=>{if(r.ok&&a)try{const i=await r.clone().text(),s=await d.sendMessageToClient(a,{action:t.SW_PLAYLIST,data:{url:o,ver:"2.0.0",text:i}},200),{data:u}=s;if(h=u&&u.debug,!u.active)return h&&console.warn("window client is not active"),E.delete(n),r;if(E.set(n,a),p=!!u.sharePlaylist,m&&!i.endsWith(l)){const t=e.insertTimeOffsetTag(i,c);return new Response(t,{status:200,statusText:"OK",headers:r.headers})}}catch(e){h&&console.warn(e),E.delete(n)}return r}))}(n,w));if(f.includes(T)){if(h&&console.info(`sw onFetch media ${A}`),y&&h&&console.warn("sw is loading"),E.has(w)){const{headers:e,url:o}=n;let s,f,l=e.get("range")||void 0;try{const{start:e,end:t}=function(e){if(!e)return{};const t=e.trim().toLowerCase();if(!t.startsWith("bytes="))throw new Error("unit-must-be-bytes",{normalizedRangeHeader:t});if(t.includes(","))throw new Error("single-range-only",{normalizedRangeHeader:t});const r=/(\d*)-(\d*)/.exec(t);if(!r||!r[1]&&!r[2])throw new Error("invalid-range-values",{normalizedRangeHeader:t});return{start:""===r[1]?void 0:Number(r[1]),end:""===r[2]?void 0:Number(r[2])}}(l);s=e,f=t}catch(e){return void console.error(e)}if(0===s&&f===s)return;return r.respondWith(function(e,r,n,o,s,f){const l=(i=!1)=>{h&&console.info(`request ${e} from network`),y=!0;let s=a(o);return _&&(h&&console.info(`set additional header for ${e}`),_(e,s,r)),fetch(new Request(e,{headers:s,mode:"cors"})).then((async r=>(y=!1,!i&&r.ok&&n&&d.sendMessageToClient(n,{action:t.SW_MEDIA,data:{url:e,buffer:await r.clone().arrayBuffer(),range:o}},u).catch((e=>{h&&console.warn(e)})),r))).catch((e=>(y=!1,console.error(e),new Response(null,{status:502,statusText:"Hls Proxy Error"}))))};let c=!1;s||f?(s||f)&&(c=!0):(o=void 0,0===s&&void 0===f&&(h&&console.warn(`request ${e} with range 0-`),c=!0));return y=!0,d.sendMessageToClient(n,{action:t.SW_GET_MEDIA,data:{url:e,range:o}},7e3).then((async o=>{if(y=!1,o&&o.data&&o.data.buffer){const{data:p}=o,{buffer:E,incomplete:y}=p;let g;if(h&&console.info(`hit cache ${e}`),g=y?await function(e,r,n,o,s,f){let l="bytes=";s?(h&&console.info(`rangeStart ${s} rangeEnd ${f}`),l=`${l}${s+o.byteLength}-${f}`):l=`${l}${o.byteLength}-`;h&&console.warn(`continue download from ${r} range: ${l}`);let c=a(l);_&&(h&&console.info(`set additional header for ${r}`),_(r,c,n));return fetch(r,{headers:c,mode:"cors"}).then((e=>e.arrayBuffer())).then((n=>{let a;s&&(a=`bytes=${s}-${f}`);let l=i.from(n),c=i.concat([o,l]);const p=new Uint8Array(c).buffer;return d.sendMessageToClient(e,{action:t.SW_MEDIA,data:{url:r,buffer:p,range:a}},u).catch((e=>{h&&console.warn(e)})),p})).catch((e=>{console.error(`http partial download error ${e}`)}))}(n,e,r,E,s,f):p.buffer,c&&g.byteLength!==f-s+1)return console.error("buffer size is not equal to range length"),l();const m={status:c?206:200,statusText:c?"Partial Content":"OK",headers:{"Accept-Ranges":"bytes","Content-Length":g.byteLength}};return c&&(m.headers["Content-Range"]=`bytes ${s}-${f}/${g.byteLength}`),new Response(g,m)}return l()})).catch((t=>(y=!1,h&&(console.warn(t),console.warn(`while requesting ${e}`)),l())))}(o,e,E.get(w),l,s,f))}h&&console.warn("windowClient not exist when get media")}})),w.version="2.0.0"})(),n=n.default})()));