!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()}(this,(()=>(()=>{var e={558:function(e){!function(t){var r=/^(?=((?:[a-zA-Z0-9+\-.]+:)?))\1(?=((?:\/\/[^\/?#]*)?))\2(?=((?:(?:[^?#\/]*\/)*[^;?#\/]*)?))\3((?:;[^?#]*)?)(\?[^#]*)?(#[^]*)?$/,n=/^(?=([^\/?#]*))\1([^]*)$/,o=/(?:\/|^)\.(?=\/)/g,i=/(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g,s={buildAbsoluteURL:function(e,t,r){if(r=r||{},e=e.trim(),!(t=t.trim())){if(!r.alwaysNormalize)return e;var o=s.parseURL(e);if(!o)throw new Error("Error trying to parse base URL.");return o.path=s.normalizePath(o.path),s.buildURLFromParts(o)}var i=s.parseURL(t);if(!i)throw new Error("Error trying to parse relative URL.");if(i.scheme)return r.alwaysNormalize?(i.path=s.normalizePath(i.path),s.buildURLFromParts(i)):t;var a=s.parseURL(e);if(!a)throw new Error("Error trying to parse base URL.");if(!a.netLoc&&a.path&&"/"!==a.path[0]){var u=n.exec(a.path);a.netLoc=u[1],a.path=u[2]}a.netLoc&&!a.path&&(a.path="/");var f={scheme:a.scheme,netLoc:i.netLoc,path:null,params:i.params,query:i.query,fragment:i.fragment};if(!i.netLoc&&(f.netLoc=a.netLoc,"/"!==i.path[0]))if(i.path){var l=a.path,c=l.substring(0,l.lastIndexOf("/")+1)+i.path;f.path=s.normalizePath(c)}else f.path=a.path,i.params||(f.params=a.params,i.query||(f.query=a.query));return null===f.path&&(f.path=r.alwaysNormalize?s.normalizePath(i.path):i.path),s.buildURLFromParts(f)},parseURL:function(e){var t=r.exec(e);return t?{scheme:t[1]||"",netLoc:t[2]||"",path:t[3]||"",params:t[4]||"",query:t[5]||"",fragment:t[6]||""}:null},normalizePath:function(e){for(e=e.split("").reverse().join("").replace(o,"");e.length!==(e=e.replace(i,"")).length;);return e.split("").reverse().join("")},buildURLFromParts:function(e){return e.scheme+e.netLoc+e.path+e.params+e.query+e.fragment}};e.exports=s}()},424:(e,t)=>{"use strict";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 a(e)}return i(e,t,r)}function i(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),i=n(r),s=i.write(e,t);s!==r&&(i=i.slice(0,s));return i}(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(y(e,ArrayBuffer)||e&&y(e.buffer,ArrayBuffer))return function(e,t,r){if(t<0||e.byteLength=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)||y(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 i=!1;;)switch(t){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":return g(e).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;default:if(i)return n?-1:g(e).length;t=(""+t).toLowerCase(),i=!0}}function c(e,t,r,n){r=Number(r)||0;const o=e.length-r;n?(n=Number(n))>o&&(n=o):n=o;const i=t.length;let s;for(n>i/2&&(n=i/2),s=0;s>8,o=r%256,i.push(o),i.push(n);return i}(t,e.length-r),e,r,n)}function E(e,t,r,n){let o;for(o=0;o=t.length||o>=e.length);++o)t[o+r]=e[o];return o}function g(e,t){var r;t=t||1/0;for(var n=e.length,o=null,i=[],s=0;s55295&&r<57344){if(!o){if(r>56319){(t-=3)>-1&&i.push(239,191,189);continue}if(s+1===n){(t-=3)>-1&&i.push(239,191,189);continue}o=r;continue}if(r<56320){(t-=3)>-1&&i.push(239,191,189),o=r;continue}r=65536+(o-55296<<10|r-56320)}else o&&(t-=3)>-1&&i.push(239,191,189);if(o=null,r<128){if((t-=1)<0)break;i.push(r)}else if(r<2048){if((t-=2)<0)break;i.push(r>>6|192,63&r|128)}else if(r<65536){if((t-=3)<0)break;i.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;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function y(e,t){return e instanceof t||null!=e&&null!=e.constructor&&null!=e.constructor.name&&e.constructor.name===t.name}function _(e){return e!=e}"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 i(e,t,r)},o.prototype.__proto__=Uint8Array.prototype,o.__proto__=Uint8Array,o.alloc=function(e,t,r){return function(e,t,r){return s(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 a(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&&(t=e.length),t||(t=0),n>0&&n=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=0;--s)e[s+t]=this[s+r];else Uint8Array.prototype.set.call(e,this.subarray(r,n),t);return i},o.prototype.write=function(e,t,r,n){if(void 0===t)n="utf8",r=this.length,t=0;else if(void 0===r&&"string"==typeof t)n=t,r=this.length,t=0;else{if(!isFinite(t))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");t>>>=0,isFinite(r)?(r>>>=0,void 0===n&&(n="utf8")):(n=r,r=void 0)}const o=this.length-t;if((void 0===r||r>o)&&(r=o),e.length>0&&(r<0||t<0)||t>this.length)throw new RangeError("Attempt to write outside buffer bounds");n||(n="utf8");let i=!1;for(;;)switch(n){case"hex":return c(this,e,t,r);case"utf8":case"utf-8":return h(this,e,t,r);case"ascii":case"latin1":case"binary":return p(this,e,t,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return d(this,e,t,r);default:if(i)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),i=!0}}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.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:()=>v});class e{static insertTimeOffsetTag(e,t){let r=e.split("\n");r=r.filter((e=>!e.startsWith("#EXT-X-START")));for(let e=0;e{const s=new MessageChannel;let a;const u=(e,t)=>setTimeout((()=>{s.port1.close(),s.port2.close(),i(`MessageChannel ${t} timed out after ${e} ms`)}),e);s.port1.onmessage=function(e){const n=e.data;n?n.pong?(clearTimeout(a),a=u(r,`${t.action}-data`)):o({data:n.data}):i("no data in event")},s.port1.onmessageerror=function(e){i(e)},e.postMessage(t,[s.port2].concat(n)),a=u(50,`${t.action}-pong`)})):Promise.reject("client is null")}};let A,T=!1,R=null,S=null,b=!1;class v{constructor(e={}){R=e.httpHeadersForPlaylist||null,S=e.httpHeadersForMediaFile||null,!0===e.insertTimeOffsetTag?(b=!0,e.playlistTimeOffset&&(d=e.playlistTimeOffset)):b=!1,e.allowedMediaFiles&&e.allowedMediaFiles.length>0&&(h=[...e.allowedMediaFiles]),e.allowedPlaylistSuffix&&e.allowedPlaylistSuffix.length>0&&(f=[...e.allowedPlaylistSuffix]),e.mediaFileSeparator&&(E=e.mediaFileSeparator)}}function C(r){const{request:n,clientId:o}=r,{headers:a,url:v}=n;if("GET"!==n.method)return;if(v.includes(self.registration.scope+`${i}/keepalive/`))return new Response;let C=s(v);if(f.includes(C)||v===A)return y&&console.info(`sw onFetch playlist ${v}`),async function(r,n){m.size>50&&m.clear();const{url:o,headers:i}=r,s=await clients.get(n);if(_&&s)try{const r=await w.sendMessageToClient(s,{action:t.SW_GET_PLAYLIST,data:{url:o}},c),{data:n}=r;if(n&&n.text){let{text:t}=n;return b&&!t.endsWith(p)&&(t=e.insertTimeOffsetTag(t,d)),new Response(t,{status:200,statusText:"OK",headers:{"Content-Type":"application/vnd.apple.mpegurl"}})}}catch(e){console.error(e)}let a;R&&(a=new Headers,y&&console.info(`set additional header for ${o}`),R(o,a,i));return fetch(new Request(o,{mode:"cors",headers:a})).then((async r=>{if(r.ok&&s)try{const i=await r.clone().text();if(!function(e){return 0===e.indexOf("#EXTM3U")}(i))return r;r.redirected&&(y&&console.info(`resp redirected to url ${r.url} `),A=r.url);const a=await w.sendMessageToClient(s,{action:t.SW_PLAYLIST,data:{url:o,redirectedUrl:r.redirected?A:void 0,ver:"2.12.10",text:i}},l),{data:u}=a;if(!u)return r;if(y||(y=u.debug),!u.active)return y&&console.warn("window client is not active"),m.delete(n),r;if(m.set(n,s),_=!!u.sharePlaylist,b&&!i.endsWith(p)){const t=e.insertTimeOffsetTag(i,d),n=new Headers(r.headers);return r.redirected&&n.set("Location",A),new Response(t,{status:r.redirected?302:200,statusText:"OK",headers:n})}}catch(e){y&&console.warn(e),m.delete(n)}return r}))}(n,o);if("."!==E&&(C=s(v,E)),h.includes(C)||"*"===h[0]){let e;S&&(e=new Headers,y&&console.info(`set additional header for ${v}`),S(v,e,a));let r=a.get("Range")||void 0;if(r&&(e||(e=new Headers),e.set("Range",r)),m.has(o)){let n,s;y&&console.info(`sw onFetch media ${v} range ${r}`);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]?0:Number(r[1]),end:""===r[2]?void 0:Number(r[2])}}(r);n=e,s=t}catch(e){return void console.error(e)}if(s-n<=1)return;const a=function(e,t){const r=new URL(e);return r.searchParams.get(t)?(r.searchParams.delete(t),r.href):""}(v,i);return a?fetch(new Request(a,{mode:"cors",headers:e})):function(e,r,n,o,i,s,a){y&&T&&console.warn("hls proxy is loading");let f=!1;i||s?(i||s)&&(f=!0):(o=void 0,0===i&&void 0===s&&(y&&console.warn(`request ${e} with range 0-`),f=!0));return T=!0,w.sendMessageToClient(n,{action:t.SW_GET_MEDIA,data:{url:e,range:o}},g).then((async t=>{if(T=!1,t&&t.data&&t.data.buffer){const{data:e}=t,{buffer:r}=e;if(f&&r.byteLength!==s-i+1)return void console.error("buffer size is not equal to range length "+(s-i+1));const n={"Accept-Ranges":"bytes","Content-Length":r.byteLength,"Access-Control-Allow-Origin":"*"},o=function(e){const t=u[e];return t||""}(a);o&&(n["Content-Type"]=o);const l={status:f?206:200,statusText:f?"Partial Content":"OK",headers:n};return f&&(l.headers["Content-Range"]=`bytes ${i}-${s}/${r.byteLength}`),new Response(r,l)}return fetch(new Request(e,{mode:"cors",headers:r}))})).catch((t=>(console.error(t),T=!1,y&&(console.warn(t),console.warn(`while requesting ${e}`)),fetch(new Request(e,{mode:"cors",headers:r})))))}(v,e,m.get(o),r,n,s,C)}return y&&console.warn("windowClient not exist when get media"),fetch(new Request(v,{mode:"cors",headers:e}))}}self.addEventListener("install",(()=>{self.skipWaiting()})),self.addEventListener("activate",(e=>e.waitUntil(self.clients.claim()))),self.addEventListener("fetch",(e=>{const t=C(e);t&&e.respondWith(t)})),self.addEventListener("message",(async function({data:e}){})),v.fileResponse=C,v.version="2.12.10"})(),n=n.default})()));