@netacea/akamai 6.1.18 → 6.1.20

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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="akamai-edgeworkers" />
2
2
  import { RequestBody, HttpResponse } from 'http-request';
3
- import { NetaceaBaseArgs, FindBestMitigationResponse } from '@netacea/netaceaintegrationbase';
3
+ import { NetaceaBaseArgs, NetaceaCookieV3IssueReason, FindBestMitigationResponse } from '@netacea/netaceaintegrationbase';
4
4
  export { InjectResponse, NetaceaMitigationType } from '@netacea/netaceaintegrationbase';
5
5
 
6
6
  declare class CachedEncryption {
@@ -52,6 +52,7 @@ interface ProtoMitata {
52
52
  captcha: number;
53
53
  mitataMaxAge: number;
54
54
  netaceaSettings: NetaceaSettings;
55
+ issueReason: NetaceaCookieV3IssueReason;
55
56
  }
56
57
  interface AkamaiMitigateResponse {
57
58
  bestAction: BestActionResponse | undefined;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import{httpRequest as t}from"http-request";import{logger as e}from"log";import*as n from"crypto";import*as i from"encoding";var r,a,o,s;!function(t){t.ORIGIN="ORIGIN",t.HTTP="HTTP",t.KINESIS="KINESIS",t.NATIVE="NATIVE"}(r||(r={})),function(t){t.V1="V1",t.V2="V2"}(a||(a={})),function(t){t.MITIGATE="MITIGATE",t.INJECT="INJECT",t.INGEST="INGEST"}(o||(o={})),function(t){t.NO_SESSION="no_session",t.EXPIRED_SESSION="expired_session",t.INVALID_SESSION="invalid_session",t.IP_CHANGE="ip_change"}(s||(s={}));const c={none:"",block:"block",captcha:"captcha",allow:"allow",captchaPass:"captchapass"},u={match:"x-netacea-match",mitigate:"x-netacea-mitigate",captcha:"x-netacea-captcha",mitata:"x-netacea-mitata-value",mitataExpiry:"x-netacea-mitata-expiry",mitataCaptcha:"x-netacea-mitatacaptcha-value",mitataCaptchaExpiry:"x-netacea-mitatacaptcha-expiry",eventId:"x-netacea-event-id"},f={0:"",1:"ua_",2:"ip_",3:"visitor_",4:"datacenter_",5:"sev_",6:"organisation_",7:"asn_",8:"country_",9:"combination_"},h={0:"",1:"blocked",2:"allow",3:"hardblocked",4:"block"},p={0:"",1:"captcha_serve",2:"captcha_pass",3:"captcha_fail",4:"captcha_cookiepass",5:"captcha_cookiefail"},l={"":0,captchaServe:1,captchaPass:2,captchaFail:3,captchaCookiePass:4,captchaCookieFail:5},d={0:c.none,1:c.block,2:c.none,3:c.block,4:c.block},g={1:c.captcha,2:c.captchaPass,3:c.captcha,4:c.allow,5:c.captcha},y={clientIP:"cip",userId:"uid",gracePeriod:"grp",cookieId:"cid",match:"mat",mitigate:"mit",captcha:"cap",issueTimestamp:"ist",issueReason:"isr"},m={checkAllPostRequests:"fCAPR"},w={checkAllPostRequests:"checkAllPostRequests"};var E=Object.freeze({__proto__:null,COOKIEDELIMITER:"_/@#/",bestMitigationCaptchaMap:g,bestMitigationMap:d,captchaMap:p,captchaStatusCodes:l,matchMap:f,mitigateMap:h,mitigationTypes:c,netaceaCookieV3KeyMap:y,netaceaCookieV3OptionalKeyMap:m,netaceaHeaders:u,netaceaSettingsMap:w});const I="ignored",b=/^(.*)_\/@#\/(.*)_\/@#\/(.*)_\/@#\/(.*)_\/@#\/((\d)(\d)(\d))$/;function S(t){if(void 0===t)return;const e=t.match(b);if(null!=e){const[,t,n,i,r,a,o,s,c]=e;return{signature:t,expiry:n,userId:i,ipHash:r,mitigationType:a,match:parseInt(o),mitigate:parseInt(s),captcha:parseInt(c)}}}function v(t,e=0){return isNaN(t)?e:parseInt(t)}function C(t){return void 0!==t&&t.split("&").map((t=>t.split("=")[0])).filter((t=>!Object.values(m).includes(t))).every((t=>Object.values(y).includes(t)))}function A(t){if("object"!=typeof t||null===t)return!1;for(const e of Object.keys(y)){if(!(e in t))return!1;if(void 0===t[e])return!1}return!0}function N(t){if(void 0===t||""===t)return;const e=t.split("&"),n={clientIP:"",userId:"",cookieId:"",gracePeriod:0,match:0,mitigate:0,captcha:0,issueTimestamp:0,issueReason:"",checkAllPostRequests:void 0};for(const t of e){const[e,i]=t.split("="),r=decodeURIComponent(i);let a=Object.keys(y).find((t=>y[t]===e));void 0===a&&(a=Object.keys(m).find((t=>m[t]===e)));let o=""===r?void 0:Number(r);void 0!==o&&isNaN(o)&&(o=r),n[a]=o}return n}function k(t,e){const n=t.split(";").map((t=>t.trim())).filter((t=>t.toLowerCase().startsWith(e.toLowerCase())))[0];return void 0!==n&&n.length>0?n?.replace(`${e}=`,""):void 0}function T(t,e=!1){if(""===t)return"";return t.replace(/ /g,"").split(";").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).filter(((t,n,i)=>{const r=t=>t.split("=")[0],a=r(t),o=i.map(r);return e?n===o.lastIndexOf(a):n===o.indexOf(a)})).join("; ")}const B={cookieAttributes:Object.freeze({__proto__:null,configureCookiesDomain:function(t,e){let n=t=T(t??"",!0),i=e=T(e??"",!0);if(void 0!==t&&void 0!==e){const r=k(t,"Domain"),a=k(e,"Domain");void 0!==r&&void 0!==a?i=e.replace(a,r):void 0!==r&&void 0===a?i=e+(""!==e?`; Domain=${r}`:`Domain=${r}`):void 0===r&&void 0!==a&&(n=t+(""!==t?`; Domain=${a}`:`Domain=${a}`))}else if(void 0!==t&&void 0===e){const e=k(t,"Domain");void 0!==e&&(i=`Domain=${e}`)}else if(void 0===t&&void 0!==e){const t=k(e,"Domain");void 0!==t&&(n=`Domain=${t}`)}return{cookieAttributes:""!==n?n:void 0,captchaCookieAttributes:""!==i?i:void 0}},extractAndRemoveCookieAttr:function(t,e){const n=k(t,e);if(void 0!==n){return{extractedAttribute:n,cookieAttributes:t.replace(/ /g,"").replace(`${e}=${n}`,"").split(";").filter((t=>t.length>0)).join("; ")}}return{extractedAttribute:void 0,cookieAttributes:t}},extractCookieAttr:k,removeDuplicateAttrs:T})},{configureCookiesDomain:x}=B.cookieAttributes,R="PMUSER_NETACEA_";class _{constructor(t){this.getBoolean=(t,e)=>{const n=this.getString(t);return void 0!==n?"true"===(n?.toLowerCase()??"false"):e},this.request=t}getString(t,e){t.startsWith(R)&&(t=t.slice(15));const n=`${R}${t}`,i=this.request.getVariable(n)??e;return Boolean(i)?i:e}}class U{constructor(t){this.defaults=t}loadConfig(t){const e=new _(t),n=e.getString("MITIGATION_TYPE",this.defaults.mitigationType),i=e.getString("INGEST_TYPE",this.defaults.ingestType),r=e.getString("COOKIE_ATTR"),a=e.getString("CAP_COOKIE_ATTR"),{cookieAttributes:o,captchaCookieAttributes:s}=x(r,a);let c=e.getString("CAPT_PATH");void 0!==c&&c.length>0&&"/"!==c?.[0]&&(c=`/${c}`);const u=e.getString("SECRET_KEY",this.defaults.secretKey);if(void 0===u)throw new Error("A secret key is required to configure this integration.");const f={...this.defaults,apiKey:e.getString("API_KEY",this.defaults.apiKey),cookieEncryptionKey:e.getString("ENCRYPTION_KEY",this.defaults.cookieEncryptionKey),ingestType:i,mitigationType:n,netaceaCaptchaCookieAttributes:s??"",netaceaCaptchaCookieName:e.getString("CAPT_COOKIE_NAME",this.defaults.netaceaCaptchaCookieName),netaceaCookieAttributes:o??"",netaceaCookieExpirySeconds:P(this.defaults,n),netaceaCookieName:e.getString("COOKIE_NAME",this.defaults.netaceaCookieName),netaceaCaptchaPath:c,netaceaCaptchaHeaderKey:e.getString("CAPT_HEADER_KEY"),netaceaCaptchaHeaderValue:e.getString("CAPT_HEADER_VALUE"),netaceaCaptchaResponseStatus:e.getString("CAPT_STATUS"),netaceaBlockedResponseStatus:e.getString("BLOCK_STATUS"),captchaContentNegotiation:e.getBoolean("CAPT_CONTENT_NGT",!1),secretKey:u,useCaptchaRelAssets:e.getBoolean("CAPT_REL_ASSETS",this.defaults.useCaptchaRelAssets)??!1,protoMitataVariableName:"PMUSER_NETACEA_PROTO_MITATA",encryptedCookieNames:[]},h=e.getString("MITIGATION_URL");f.mitigationServiceUrl=void 0!==h?h:this.defaults.mitigationServiceUrl??"https://mitigations.netacea.net";const p=e.getString("INGEST_URL");return f.ingestServiceUrl=void 0!==p?p:this.defaults.ingestServiceUrl??"https://ingest.netacea.net",void 0===f.netaceaCookieName&&(f.netaceaCookieName="_mitata"),void 0===f.netaceaCaptchaCookieName&&(f.netaceaCaptchaCookieName="_mitatacaptcha"),f.encryptedCookieNames.push(f.netaceaCookieName),f.encryptedCookieNames.push(f.netaceaCaptchaCookieName),void 0!==f.netaceaCaptchaHeaderKey&&void 0===f.netaceaCaptchaHeaderValue&&(f.netaceaCaptchaHeaderValue="true"),f}}function P(t,e){const n=t.netaceaCookieExpirySeconds??t.mitataCookieExpirySeconds;return void 0===n?e===o.INGEST?3600:60:n}const O=/\.(js|css)$/i;function M(t,e,n){if(!n)return!1;const i=t.toLowerCase();return/\/?mitigations\//.test(i)&&$(t,e)}function $(t,e){const n=t.toLowerCase();return O.test(n)&&"get"===e.toLowerCase()}function L(t,e){return/\/?ataverifycaptcha$/.test(t.toLowerCase())&&"post"===e.toLowerCase()}var H=n.crypto??n;const K=i.TextEncoder??global.TextEncoder,j=i.TextDecoder??global.TextDecoder;function D(t){return"string"==typeof t?(new K).encode(t):t}async function V(t,e){const n=await q(D(e));return new Uint8Array(await H.subtle.sign("HMAC",n,t))}async function q(t){return await H.subtle.importKey("raw",t,{name:"HMAC",hash:"SHA-256"},!1,["sign","verify"])}for(var G={},F={byteLength:function(t){var e=Z(t),n=e[0],i=e[1];return 3*(n+i)/4-i},toByteArray:function(t){var e,n,i=Z(t),r=i[0],a=i[1],o=new J(function(t,e,n){return 3*(e+n)/4-n}(0,r,a)),s=0,c=a>0?r-4:r;for(n=0;n<c;n+=4)e=z[t.charCodeAt(n)]<<18|z[t.charCodeAt(n+1)]<<12|z[t.charCodeAt(n+2)]<<6|z[t.charCodeAt(n+3)],o[s++]=e>>16&255,o[s++]=e>>8&255,o[s++]=255&e;2===a&&(e=z[t.charCodeAt(n)]<<2|z[t.charCodeAt(n+1)]>>4,o[s++]=255&e);1===a&&(e=z[t.charCodeAt(n)]<<10|z[t.charCodeAt(n+1)]<<4|z[t.charCodeAt(n+2)]>>2,o[s++]=e>>8&255,o[s++]=255&e);return o},fromByteArray:function(t){for(var e,n=t.length,i=n%3,r=[],a=16383,o=0,s=n-i;o<s;o+=a)r.push(Q(t,o,o+a>s?s:o+a));1===i?(e=t[n-1],r.push(X[e>>2]+X[e<<4&63]+"==")):2===i&&(e=(t[n-2]<<8)+t[n-1],r.push(X[e>>10]+X[e>>4&63]+X[e<<2&63]+"="));return r.join("")}},X=[],z=[],J="undefined"!=typeof Uint8Array?Uint8Array:Array,W="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",Y=0;Y<64;++Y)X[Y]=W[Y],z[W.charCodeAt(Y)]=Y;function Z(t){var e=t.length;if(e%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var n=t.indexOf("=");return-1===n&&(n=e),[n,n===e?0:4-n%4]}function Q(t,e,n){for(var i,r,a=[],o=e;o<n;o+=3)i=(t[o]<<16&16711680)+(t[o+1]<<8&65280)+(255&t[o+2]),a.push(X[(r=i)>>18&63]+X[r>>12&63]+X[r>>6&63]+X[63&r]);return a.join("")}z["-".charCodeAt(0)]=62,z["_".charCodeAt(0)]=63;var tt={
1
+ import{httpRequest as t}from"http-request";import{logger as e}from"log";import*as n from"crypto";import*as i from"encoding";var r,a,o,s;function c(t,e=0){return isNaN(t)?e:parseInt(t)}!function(t){t.ORIGIN="ORIGIN",t.HTTP="HTTP",t.KINESIS="KINESIS",t.NATIVE="NATIVE"}(r||(r={})),function(t){t.V1="V1",t.V2="V2"}(a||(a={})),function(t){t.MITIGATE="MITIGATE",t.INJECT="INJECT",t.INGEST="INGEST"}(o||(o={})),function(t){t.CAPTCHA_GET="captcha_get",t.CAPTCHA_POST="captcha_post",t.EXPIRED_SESSION="expired_session",t.FORCED_REVALIDATION="forced_revalidation",t.INVALID_SESSION="invalid_session",t.IP_CHANGE="ip_change",t.NO_SESSION="no_session"}(s||(s={}));const u={none:"",block:"block",captcha:"captcha",allow:"allow",captchaPass:"captchapass"},f={match:"x-netacea-match",mitigate:"x-netacea-mitigate",captcha:"x-netacea-captcha",mitata:"x-netacea-mitata-value",mitataExpiry:"x-netacea-mitata-expiry",mitataCaptcha:"x-netacea-mitatacaptcha-value",mitataCaptchaExpiry:"x-netacea-mitatacaptcha-expiry",eventId:"x-netacea-event-id"},h={0:"",1:"ua_",2:"ip_",3:"visitor_",4:"datacenter_",5:"sev_",6:"organisation_",7:"asn_",8:"country_",9:"combination_"},p={0:"",1:"blocked",2:"allow",3:"hardblocked",4:"block"},l={0:"",1:"captcha_serve",2:"captcha_pass",3:"captcha_fail",4:"captcha_cookiepass",5:"captcha_cookiefail"},d={"":0,captchaServe:1,captchaPass:2,captchaFail:3,captchaCookiePass:4,captchaCookieFail:5},g={0:u.none,1:u.block,2:u.none,3:u.block,4:u.block},y={1:u.captcha,2:u.captchaPass,3:u.captcha,4:u.allow,5:u.captcha},m={clientIP:"cip",userId:"uid",gracePeriod:"grp",cookieId:"cid",match:"mat",mitigate:"mit",captcha:"cap",issueTimestamp:"ist",issueReason:"isr"},w={checkAllPostRequests:"fCAPR"},S={checkAllPostRequests:"checkAllPostRequests"};var E=Object.freeze({__proto__:null,COOKIEDELIMITER:"_/@#/",bestMitigationCaptchaMap:y,bestMitigationMap:g,captchaMap:l,captchaStatusCodes:d,matchMap:h,mitigateMap:p,mitigationTypes:u,netaceaCookieV3KeyMap:m,netaceaCookieV3OptionalKeyMap:w,netaceaHeaders:f,netaceaSettingsMap:S});const I="ignored",b=/^(.*)_\/@#\/(.*)_\/@#\/(.*)_\/@#\/(.*)_\/@#\/((\d)(\d)(\d))$/;function v(t){if(void 0===t)return;const e=t.match(b);if(null!=e){const[,t,n,i,r,a,o,s,c]=e;return{signature:t,expiry:n,userId:i,ipHash:r,mitigationType:a,match:parseInt(o),mitigate:parseInt(s),captcha:parseInt(c)}}}function C(t){return void 0!==t&&t.split("&").map((t=>t.split("=")[0])).filter((t=>!Object.values(w).includes(t))).every((t=>Object.values(m).includes(t)))}function A(t){if("object"!=typeof t||null===t)return!1;for(const e of Object.keys(m)){if(!(e in t))return!1;if(void 0===t[e])return!1}return!0}function N(t){if(void 0===t||""===t)return;const e=t.split("&"),n={clientIP:"",userId:"",cookieId:"",gracePeriod:0,match:0,mitigate:0,captcha:0,issueTimestamp:0,issueReason:"",checkAllPostRequests:void 0};for(const t of e){const[e,i]=t.split("="),r=decodeURIComponent(i);let a=Object.keys(m).find((t=>m[t]===e));void 0===a&&(a=Object.keys(w).find((t=>w[t]===e)));let o=""===r?void 0:Number(r);void 0!==o&&isNaN(o)&&(o=r),n[a]=o}return n}function k(){return{mitata:void 0,requiresReissue:!1,isExpired:!1,shouldExpire:!1,isSameIP:!1,isPrimaryHashValid:!1,captcha:0,match:0,mitigate:0,issueReason:s.NO_SESSION}}function T(t,e){const n=t.split(";").map((t=>t.trim())).filter((t=>t.toLowerCase().startsWith(e.toLowerCase())))[0];return void 0!==n&&n.length>0?n?.replace(`${e}=`,""):void 0}function R(t,e=!1){if(""===t)return"";return t.replace(/ /g,"").split(";").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).filter(((t,n,i)=>{const r=t=>t.split("=")[0],a=r(t),o=i.map(r);return e?n===o.lastIndexOf(a):n===o.indexOf(a)})).join("; ")}const _={cookieAttributes:Object.freeze({__proto__:null,configureCookiesDomain:function(t,e){let n=t=R(t??"",!0),i=e=R(e??"",!0);if(void 0!==t&&void 0!==e){const r=T(t,"Domain"),a=T(e,"Domain");void 0!==r&&void 0!==a?i=e.replace(a,r):void 0!==r&&void 0===a?i=e+(""!==e?`; Domain=${r}`:`Domain=${r}`):void 0===r&&void 0!==a&&(n=t+(""!==t?`; Domain=${a}`:`Domain=${a}`))}else if(void 0!==t&&void 0===e){const e=T(t,"Domain");void 0!==e&&(i=`Domain=${e}`)}else if(void 0===t&&void 0!==e){const t=T(e,"Domain");void 0!==t&&(n=`Domain=${t}`)}return{cookieAttributes:""!==n?n:void 0,captchaCookieAttributes:""!==i?i:void 0}},extractAndRemoveCookieAttr:function(t,e){const n=T(t,e);if(void 0!==n){return{extractedAttribute:n,cookieAttributes:t.replace(/ /g,"").replace(`${e}=${n}`,"").split(";").filter((t=>t.length>0)).join("; ")}}return{extractedAttribute:void 0,cookieAttributes:t}},extractCookieAttr:T,removeDuplicateAttrs:R})},{configureCookiesDomain:B}=_.cookieAttributes,x="PMUSER_NETACEA_";class O{constructor(t){this.getBoolean=(t,e)=>{const n=this.getString(t);return void 0!==n?"true"===(n?.toLowerCase()??"false"):e},this.request=t}getString(t,e){t.startsWith(x)&&(t=t.slice(15));const n=`${x}${t}`,i=this.request.getVariable(n)??e;return Boolean(i)?i:e}}class U{constructor(t){this.defaults=t}loadConfig(t){const e=new O(t),n=e.getString("MITIGATION_TYPE",this.defaults.mitigationType),i=e.getString("INGEST_TYPE",this.defaults.ingestType),r=e.getString("COOKIE_ATTR"),a=e.getString("CAP_COOKIE_ATTR"),{cookieAttributes:o,captchaCookieAttributes:s}=B(r,a);let c=e.getString("CAPT_PATH");void 0!==c&&c.length>0&&"/"!==c?.[0]&&(c=`/${c}`);const u=e.getString("SECRET_KEY",this.defaults.secretKey);if(void 0===u)throw new Error("A secret key is required to configure this integration.");const f={...this.defaults,apiKey:e.getString("API_KEY",this.defaults.apiKey),cookieEncryptionKey:e.getString("ENCRYPTION_KEY",this.defaults.cookieEncryptionKey),ingestType:i,mitigationType:n,netaceaCaptchaCookieAttributes:s??"",netaceaCaptchaCookieName:e.getString("CAPT_COOKIE_NAME",this.defaults.netaceaCaptchaCookieName),netaceaCookieAttributes:o??"",netaceaCookieExpirySeconds:P(this.defaults,n),netaceaCookieName:e.getString("COOKIE_NAME",this.defaults.netaceaCookieName),netaceaCaptchaPath:c,netaceaCaptchaHeaderKey:e.getString("CAPT_HEADER_KEY"),netaceaCaptchaHeaderValue:e.getString("CAPT_HEADER_VALUE"),netaceaCaptchaResponseStatus:e.getString("CAPT_STATUS"),netaceaBlockedResponseStatus:e.getString("BLOCK_STATUS"),captchaContentNegotiation:e.getBoolean("CAPT_CONTENT_NGT",!1),secretKey:u,useCaptchaRelAssets:e.getBoolean("CAPT_REL_ASSETS",this.defaults.useCaptchaRelAssets)??!1,protoMitataVariableName:"PMUSER_NETACEA_PROTO_MITATA",encryptedCookieNames:[]},h=e.getString("MITIGATION_URL");f.mitigationServiceUrl=void 0!==h?h:this.defaults.mitigationServiceUrl??"https://mitigations.netacea.net";const p=e.getString("INGEST_URL");return f.ingestServiceUrl=void 0!==p?p:this.defaults.ingestServiceUrl??"https://ingest.netacea.net",void 0===f.netaceaCookieName&&(f.netaceaCookieName="_mitata"),void 0===f.netaceaCaptchaCookieName&&(f.netaceaCaptchaCookieName="_mitatacaptcha"),f.encryptedCookieNames.push(f.netaceaCookieName),f.encryptedCookieNames.push(f.netaceaCaptchaCookieName),void 0!==f.netaceaCaptchaHeaderKey&&void 0===f.netaceaCaptchaHeaderValue&&(f.netaceaCaptchaHeaderValue="true"),f}}function P(t,e){const n=t.netaceaCookieExpirySeconds??t.mitataCookieExpirySeconds;return void 0===n?e===o.INGEST?3600:60:n}const M=/\.(js|css)$/i;function $(t,e,n){if(!n)return!1;const i=t.toLowerCase();return/\/?mitigations\//.test(i)&&L(t,e)}function L(t,e){const n=t.toLowerCase();return M.test(n)&&"get"===e.toLowerCase()}function H(t,e){return/\/?ataverifycaptcha$/.test(t.toLowerCase())&&"post"===e.toLowerCase()}var K=n.crypto??n;const D=i.TextEncoder??global.TextEncoder,j=i.TextDecoder??global.TextDecoder;function V(t){return"string"==typeof t?(new D).encode(t):t}async function q(t,e){const n=await G(V(e));return new Uint8Array(await K.subtle.sign("HMAC",n,t))}async function G(t){return await K.subtle.importKey("raw",t,{name:"HMAC",hash:"SHA-256"},!1,["sign","verify"])}for(var F={},X={byteLength:function(t){var e=Q(t),n=e[0],i=e[1];return 3*(n+i)/4-i},toByteArray:function(t){var e,n,i=Q(t),r=i[0],a=i[1],o=new W(function(t,e,n){return 3*(e+n)/4-n}(0,r,a)),s=0,c=a>0?r-4:r;for(n=0;n<c;n+=4)e=J[t.charCodeAt(n)]<<18|J[t.charCodeAt(n+1)]<<12|J[t.charCodeAt(n+2)]<<6|J[t.charCodeAt(n+3)],o[s++]=e>>16&255,o[s++]=e>>8&255,o[s++]=255&e;2===a&&(e=J[t.charCodeAt(n)]<<2|J[t.charCodeAt(n+1)]>>4,o[s++]=255&e);1===a&&(e=J[t.charCodeAt(n)]<<10|J[t.charCodeAt(n+1)]<<4|J[t.charCodeAt(n+2)]>>2,o[s++]=e>>8&255,o[s++]=255&e);return o},fromByteArray:function(t){for(var e,n=t.length,i=n%3,r=[],a=16383,o=0,s=n-i;o<s;o+=a)r.push(tt(t,o,o+a>s?s:o+a));1===i?(e=t[n-1],r.push(z[e>>2]+z[e<<4&63]+"==")):2===i&&(e=(t[n-2]<<8)+t[n-1],r.push(z[e>>10]+z[e>>4&63]+z[e<<2&63]+"="));return r.join("")}},z=[],J=[],W="undefined"!=typeof Uint8Array?Uint8Array:Array,Y="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",Z=0;Z<64;++Z)z[Z]=Y[Z],J[Y.charCodeAt(Z)]=Z;function Q(t){var e=t.length;if(e%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var n=t.indexOf("=");return-1===n&&(n=e),[n,n===e?0:4-n%4]}function tt(t,e,n){for(var i,r,a=[],o=e;o<n;o+=3)i=(t[o]<<16&16711680)+(t[o+1]<<8&65280)+(255&t[o+2]),a.push(z[(r=i)>>18&63]+z[r>>12&63]+z[r>>6&63]+z[63&r]);return a.join("")}J["-".charCodeAt(0)]=62,J["_".charCodeAt(0)]=63;var et={
2
2
  /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
3
3
  read:function(t,e,n,i,r){var a,o,s=8*r-i-1,c=(1<<s)-1,u=c>>1,f=-7,h=n?r-1:0,p=n?-1:1,l=t[e+h];for(h+=p,a=l&(1<<-f)-1,l>>=-f,f+=s;f>0;a=256*a+t[e+h],h+=p,f-=8);for(o=a&(1<<-f)-1,a>>=-f,f+=i;f>0;o=256*o+t[e+h],h+=p,f-=8);if(0===a)a=1-u;else{if(a===c)return o?NaN:1/0*(l?-1:1);o+=Math.pow(2,i),a-=u}return(l?-1:1)*o*Math.pow(2,a-i)},write:function(t,e,n,i,r,a){var o,s,c,u=8*a-r-1,f=(1<<u)-1,h=f>>1,p=23===r?Math.pow(2,-24)-Math.pow(2,-77):0,l=i?0:a-1,d=i?1:-1,g=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(s=isNaN(e)?1:0,o=f):(o=Math.floor(Math.log(e)/Math.LN2),e*(c=Math.pow(2,-o))<1&&(o--,c*=2),(e+=o+h>=1?p/c:p*Math.pow(2,1-h))*c>=2&&(o++,c/=2),o+h>=f?(s=0,o=f):o+h>=1?(s=(e*c-1)*Math.pow(2,r),o+=h):(s=e*Math.pow(2,h-1)*Math.pow(2,r),o=0));r>=8;t[n+l]=255&s,l+=d,s/=256,r-=8);for(o=o<<r|s,u+=r;u>0;t[n+l]=255&o,l+=d,o/=256,u-=8);t[n+l-d]|=128*g}};
4
4
  /*!
@@ -7,4 +7,4 @@ read:function(t,e,n,i,r){var a,o,s=8*r-i-1,c=(1<<s)-1,u=c>>1,f=-7,h=n?r-1:0,p=n?
7
7
  * @author Feross Aboukhadijeh <https://feross.org>
8
8
  * @license MIT
9
9
  */
10
- !function(t){const e=F,n=tt,i="function"==typeof Symbol&&"function"==typeof Symbol.for?Symbol.for("nodejs.util.inspect.custom"):null;t.Buffer=o,t.SlowBuffer=function(t){+t!=t&&(t=0);return o.alloc(+t)},t.INSPECT_MAX_BYTES=50;const r=2147483647;function a(t){if(t>r)throw new RangeError('The value "'+t+'" is invalid for option "size"');const e=new Uint8Array(t);return Object.setPrototypeOf(e,o.prototype),e}function o(t,e,n){if("number"==typeof t){if("string"==typeof e)throw new TypeError('The "string" argument must be of type string. Received type number');return u(t)}return s(t,e,n)}function s(t,e,n){if("string"==typeof t)return function(t,e){"string"==typeof e&&""!==e||(e="utf8");if(!o.isEncoding(e))throw new TypeError("Unknown encoding: "+e);const n=0|l(t,e);let i=a(n);const r=i.write(t,e);r!==n&&(i=i.slice(0,r));return i}(t,e);if(ArrayBuffer.isView(t))return function(t){if(z(t,Uint8Array)){const e=new Uint8Array(t);return h(e.buffer,e.byteOffset,e.byteLength)}return f(t)}(t);if(null==t)throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t);if(z(t,ArrayBuffer)||t&&z(t.buffer,ArrayBuffer))return h(t,e,n);if("undefined"!=typeof SharedArrayBuffer&&(z(t,SharedArrayBuffer)||t&&z(t.buffer,SharedArrayBuffer)))return h(t,e,n);if("number"==typeof t)throw new TypeError('The "value" argument must not be of type number. Received type number');const i=t.valueOf&&t.valueOf();if(null!=i&&i!==t)return o.from(i,e,n);const r=function(t){if(o.isBuffer(t)){const e=0|p(t.length),n=a(e);return 0===n.length||t.copy(n,0,0,e),n}if(void 0!==t.length)return"number"!=typeof t.length||J(t.length)?a(0):f(t);if("Buffer"===t.type&&Array.isArray(t.data))return f(t.data)}(t);if(r)return r;if("undefined"!=typeof Symbol&&null!=Symbol.toPrimitive&&"function"==typeof t[Symbol.toPrimitive])return o.from(t[Symbol.toPrimitive]("string"),e,n);throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t)}function c(t){if("number"!=typeof t)throw new TypeError('"size" argument must be of type number');if(t<0)throw new RangeError('The value "'+t+'" is invalid for option "size"')}function u(t){return c(t),a(t<0?0:0|p(t))}function f(t){const e=t.length<0?0:0|p(t.length),n=a(e);for(let i=0;i<e;i+=1)n[i]=255&t[i];return n}function h(t,e,n){if(e<0||t.byteLength<e)throw new RangeError('"offset" is outside of buffer bounds');if(t.byteLength<e+(n||0))throw new RangeError('"length" is outside of buffer bounds');let i;return i=void 0===e&&void 0===n?new Uint8Array(t):void 0===n?new Uint8Array(t,e):new Uint8Array(t,e,n),Object.setPrototypeOf(i,o.prototype),i}function p(t){if(t>=r)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+r.toString(16)+" bytes");return 0|t}function l(t,e){if(o.isBuffer(t))return t.length;if(ArrayBuffer.isView(t)||z(t,ArrayBuffer))return t.byteLength;if("string"!=typeof t)throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type '+typeof t);const n=t.length,i=arguments.length>2&&!0===arguments[2];if(!i&&0===n)return 0;let r=!1;for(;;)switch(e){case"ascii":case"latin1":case"binary":return n;case"utf8":case"utf-8":return q(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*n;case"hex":return n>>>1;case"base64":return G(t).length;default:if(r)return i?-1:q(t).length;e=(""+e).toLowerCase(),r=!0}}function d(t,e,n){let i=!1;if((void 0===e||e<0)&&(e=0),e>this.length)return"";if((void 0===n||n>this.length)&&(n=this.length),n<=0)return"";if((n>>>=0)<=(e>>>=0))return"";for(t||(t="utf8");;)switch(t){case"hex":return T(this,e,n);case"utf8":case"utf-8":return C(this,e,n);case"ascii":return N(this,e,n);case"latin1":case"binary":return k(this,e,n);case"base64":return v(this,e,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return B(this,e,n);default:if(i)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),i=!0}}function g(t,e,n){const i=t[e];t[e]=t[n],t[n]=i}function y(t,e,n,i,r){if(0===t.length)return-1;if("string"==typeof n?(i=n,n=0):n>2147483647?n=2147483647:n<-2147483648&&(n=-2147483648),J(n=+n)&&(n=r?0:t.length-1),n<0&&(n=t.length+n),n>=t.length){if(r)return-1;n=t.length-1}else if(n<0){if(!r)return-1;n=0}if("string"==typeof e&&(e=o.from(e,i)),o.isBuffer(e))return 0===e.length?-1:m(t,e,n,i,r);if("number"==typeof e)return e&=255,"function"==typeof Uint8Array.prototype.indexOf?r?Uint8Array.prototype.indexOf.call(t,e,n):Uint8Array.prototype.lastIndexOf.call(t,e,n):m(t,[e],n,i,r);throw new TypeError("val must be string, number or Buffer")}function m(t,e,n,i,r){let a,o=1,s=t.length,c=e.length;if(void 0!==i&&("ucs2"===(i=String(i).toLowerCase())||"ucs-2"===i||"utf16le"===i||"utf-16le"===i)){if(t.length<2||e.length<2)return-1;o=2,s/=2,c/=2,n/=2}function u(t,e){return 1===o?t[e]:t.readUInt16BE(e*o)}if(r){let i=-1;for(a=n;a<s;a++)if(u(t,a)===u(e,-1===i?0:a-i)){if(-1===i&&(i=a),a-i+1===c)return i*o}else-1!==i&&(a-=a-i),i=-1}else for(n+c>s&&(n=s-c),a=n;a>=0;a--){let n=!0;for(let i=0;i<c;i++)if(u(t,a+i)!==u(e,i)){n=!1;break}if(n)return a}return-1}function w(t,e,n,i){n=Number(n)||0;const r=t.length-n;i?(i=Number(i))>r&&(i=r):i=r;const a=e.length;let o;for(i>a/2&&(i=a/2),o=0;o<i;++o){const i=parseInt(e.substr(2*o,2),16);if(J(i))return o;t[n+o]=i}return o}function E(t,e,n,i){return X(q(e,t.length-n),t,n,i)}function I(t,e,n,i){return X(function(t){const e=[];for(let n=0;n<t.length;++n)e.push(255&t.charCodeAt(n));return e}(e),t,n,i)}function b(t,e,n,i){return X(G(e),t,n,i)}function S(t,e,n,i){return X(function(t,e){let n,i,r;const a=[];for(let o=0;o<t.length&&!((e-=2)<0);++o)n=t.charCodeAt(o),i=n>>8,r=n%256,a.push(r),a.push(i);return a}(e,t.length-n),t,n,i)}function v(t,n,i){return 0===n&&i===t.length?e.fromByteArray(t):e.fromByteArray(t.slice(n,i))}function C(t,e,n){n=Math.min(t.length,n);const i=[];let r=e;for(;r<n;){const e=t[r];let a=null,o=e>239?4:e>223?3:e>191?2:1;if(r+o<=n){let n,i,s,c;switch(o){case 1:e<128&&(a=e);break;case 2:n=t[r+1],128==(192&n)&&(c=(31&e)<<6|63&n,c>127&&(a=c));break;case 3:n=t[r+1],i=t[r+2],128==(192&n)&&128==(192&i)&&(c=(15&e)<<12|(63&n)<<6|63&i,c>2047&&(c<55296||c>57343)&&(a=c));break;case 4:n=t[r+1],i=t[r+2],s=t[r+3],128==(192&n)&&128==(192&i)&&128==(192&s)&&(c=(15&e)<<18|(63&n)<<12|(63&i)<<6|63&s,c>65535&&c<1114112&&(a=c))}}null===a?(a=65533,o=1):a>65535&&(a-=65536,i.push(a>>>10&1023|55296),a=56320|1023&a),i.push(a),r+=o}return function(t){const e=t.length;if(e<=A)return String.fromCharCode.apply(String,t);let n="",i=0;for(;i<e;)n+=String.fromCharCode.apply(String,t.slice(i,i+=A));return n}(i)}t.kMaxLength=r,o.TYPED_ARRAY_SUPPORT=function(){try{const t=new Uint8Array(1),e={foo:function(){return 42}};return Object.setPrototypeOf(e,Uint8Array.prototype),Object.setPrototypeOf(t,e),42===t.foo()}catch(t){return!1}}(),o.TYPED_ARRAY_SUPPORT||"undefined"==typeof console||"function"!=typeof console.error||console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."),Object.defineProperty(o.prototype,"parent",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.buffer}}),Object.defineProperty(o.prototype,"offset",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.byteOffset}}),o.poolSize=8192,o.from=function(t,e,n){return s(t,e,n)},Object.setPrototypeOf(o.prototype,Uint8Array.prototype),Object.setPrototypeOf(o,Uint8Array),o.alloc=function(t,e,n){return function(t,e,n){return c(t),t<=0?a(t):void 0!==e?"string"==typeof n?a(t).fill(e,n):a(t).fill(e):a(t)}(t,e,n)},o.allocUnsafe=function(t){return u(t)},o.allocUnsafeSlow=function(t){return u(t)},o.isBuffer=function(t){return null!=t&&!0===t._isBuffer&&t!==o.prototype},o.compare=function(t,e){if(z(t,Uint8Array)&&(t=o.from(t,t.offset,t.byteLength)),z(e,Uint8Array)&&(e=o.from(e,e.offset,e.byteLength)),!o.isBuffer(t)||!o.isBuffer(e))throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');if(t===e)return 0;let n=t.length,i=e.length;for(let r=0,a=Math.min(n,i);r<a;++r)if(t[r]!==e[r]){n=t[r],i=e[r];break}return n<i?-1:i<n?1:0},o.isEncoding=function(t){switch(String(t).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(t,e){if(!Array.isArray(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return o.alloc(0);let n;if(void 0===e)for(e=0,n=0;n<t.length;++n)e+=t[n].length;const i=o.allocUnsafe(e);let r=0;for(n=0;n<t.length;++n){let e=t[n];if(z(e,Uint8Array))r+e.length>i.length?(o.isBuffer(e)||(e=o.from(e)),e.copy(i,r)):Uint8Array.prototype.set.call(i,e,r);else{if(!o.isBuffer(e))throw new TypeError('"list" argument must be an Array of Buffers');e.copy(i,r)}r+=e.length}return i},o.byteLength=l,o.prototype._isBuffer=!0,o.prototype.swap16=function(){const t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(let e=0;e<t;e+=2)g(this,e,e+1);return this},o.prototype.swap32=function(){const t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(let e=0;e<t;e+=4)g(this,e,e+3),g(this,e+1,e+2);return this},o.prototype.swap64=function(){const t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(let e=0;e<t;e+=8)g(this,e,e+7),g(this,e+1,e+6),g(this,e+2,e+5),g(this,e+3,e+4);return this},o.prototype.toString=function(){const t=this.length;return 0===t?"":0===arguments.length?C(this,0,t):d.apply(this,arguments)},o.prototype.toLocaleString=o.prototype.toString,o.prototype.equals=function(t){if(!o.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===o.compare(this,t)},o.prototype.inspect=function(){let e="";const n=t.INSPECT_MAX_BYTES;return e=this.toString("hex",0,n).replace(/(.{2})/g,"$1 ").trim(),this.length>n&&(e+=" ... "),"<Buffer "+e+">"},i&&(o.prototype[i]=o.prototype.inspect),o.prototype.compare=function(t,e,n,i,r){if(z(t,Uint8Array)&&(t=o.from(t,t.offset,t.byteLength)),!o.isBuffer(t))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof t);if(void 0===e&&(e=0),void 0===n&&(n=t?t.length:0),void 0===i&&(i=0),void 0===r&&(r=this.length),e<0||n>t.length||i<0||r>this.length)throw new RangeError("out of range index");if(i>=r&&e>=n)return 0;if(i>=r)return-1;if(e>=n)return 1;if(this===t)return 0;let a=(r>>>=0)-(i>>>=0),s=(n>>>=0)-(e>>>=0);const c=Math.min(a,s),u=this.slice(i,r),f=t.slice(e,n);for(let t=0;t<c;++t)if(u[t]!==f[t]){a=u[t],s=f[t];break}return a<s?-1:s<a?1:0},o.prototype.includes=function(t,e,n){return-1!==this.indexOf(t,e,n)},o.prototype.indexOf=function(t,e,n){return y(this,t,e,n,!0)},o.prototype.lastIndexOf=function(t,e,n){return y(this,t,e,n,!1)},o.prototype.write=function(t,e,n,i){if(void 0===e)i="utf8",n=this.length,e=0;else if(void 0===n&&"string"==typeof e)i=e,n=this.length,e=0;else{if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e>>>=0,isFinite(n)?(n>>>=0,void 0===i&&(i="utf8")):(i=n,n=void 0)}const r=this.length-e;if((void 0===n||n>r)&&(n=r),t.length>0&&(n<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");i||(i="utf8");let a=!1;for(;;)switch(i){case"hex":return w(this,t,e,n);case"utf8":case"utf-8":return E(this,t,e,n);case"ascii":case"latin1":case"binary":return I(this,t,e,n);case"base64":return b(this,t,e,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return S(this,t,e,n);default:if(a)throw new TypeError("Unknown encoding: "+i);i=(""+i).toLowerCase(),a=!0}},o.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};const A=4096;function N(t,e,n){let i="";n=Math.min(t.length,n);for(let r=e;r<n;++r)i+=String.fromCharCode(127&t[r]);return i}function k(t,e,n){let i="";n=Math.min(t.length,n);for(let r=e;r<n;++r)i+=String.fromCharCode(t[r]);return i}function T(t,e,n){const i=t.length;(!e||e<0)&&(e=0),(!n||n<0||n>i)&&(n=i);let r="";for(let i=e;i<n;++i)r+=W[t[i]];return r}function B(t,e,n){const i=t.slice(e,n);let r="";for(let t=0;t<i.length-1;t+=2)r+=String.fromCharCode(i[t]+256*i[t+1]);return r}function x(t,e,n){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(t+e>n)throw new RangeError("Trying to access beyond buffer length")}function R(t,e,n,i,r,a){if(!o.isBuffer(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(e>r||e<a)throw new RangeError('"value" argument is out of bounds');if(n+i>t.length)throw new RangeError("Index out of range")}function _(t,e,n,i,r){K(e,i,r,t,n,7);let a=Number(e&BigInt(4294967295));t[n++]=a,a>>=8,t[n++]=a,a>>=8,t[n++]=a,a>>=8,t[n++]=a;let o=Number(e>>BigInt(32)&BigInt(4294967295));return t[n++]=o,o>>=8,t[n++]=o,o>>=8,t[n++]=o,o>>=8,t[n++]=o,n}function U(t,e,n,i,r){K(e,i,r,t,n,7);let a=Number(e&BigInt(4294967295));t[n+7]=a,a>>=8,t[n+6]=a,a>>=8,t[n+5]=a,a>>=8,t[n+4]=a;let o=Number(e>>BigInt(32)&BigInt(4294967295));return t[n+3]=o,o>>=8,t[n+2]=o,o>>=8,t[n+1]=o,o>>=8,t[n]=o,n+8}function P(t,e,n,i,r,a){if(n+i>t.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("Index out of range")}function O(t,e,i,r,a){return e=+e,i>>>=0,a||P(t,0,i,4),n.write(t,e,i,r,23,4),i+4}function M(t,e,i,r,a){return e=+e,i>>>=0,a||P(t,0,i,8),n.write(t,e,i,r,52,8),i+8}o.prototype.slice=function(t,e){const n=this.length;(t=~~t)<0?(t+=n)<0&&(t=0):t>n&&(t=n),(e=void 0===e?n:~~e)<0?(e+=n)<0&&(e=0):e>n&&(e=n),e<t&&(e=t);const i=this.subarray(t,e);return Object.setPrototypeOf(i,o.prototype),i},o.prototype.readUintLE=o.prototype.readUIntLE=function(t,e,n){t>>>=0,e>>>=0,n||x(t,e,this.length);let i=this[t],r=1,a=0;for(;++a<e&&(r*=256);)i+=this[t+a]*r;return i},o.prototype.readUintBE=o.prototype.readUIntBE=function(t,e,n){t>>>=0,e>>>=0,n||x(t,e,this.length);let i=this[t+--e],r=1;for(;e>0&&(r*=256);)i+=this[t+--e]*r;return i},o.prototype.readUint8=o.prototype.readUInt8=function(t,e){return t>>>=0,e||x(t,1,this.length),this[t]},o.prototype.readUint16LE=o.prototype.readUInt16LE=function(t,e){return t>>>=0,e||x(t,2,this.length),this[t]|this[t+1]<<8},o.prototype.readUint16BE=o.prototype.readUInt16BE=function(t,e){return t>>>=0,e||x(t,2,this.length),this[t]<<8|this[t+1]},o.prototype.readUint32LE=o.prototype.readUInt32LE=function(t,e){return t>>>=0,e||x(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},o.prototype.readUint32BE=o.prototype.readUInt32BE=function(t,e){return t>>>=0,e||x(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},o.prototype.readBigUInt64LE=Y((function(t){j(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||D(t,this.length-8);const i=e+256*this[++t]+65536*this[++t]+this[++t]*2**24,r=this[++t]+256*this[++t]+65536*this[++t]+n*2**24;return BigInt(i)+(BigInt(r)<<BigInt(32))})),o.prototype.readBigUInt64BE=Y((function(t){j(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||D(t,this.length-8);const i=e*2**24+65536*this[++t]+256*this[++t]+this[++t],r=this[++t]*2**24+65536*this[++t]+256*this[++t]+n;return(BigInt(i)<<BigInt(32))+BigInt(r)})),o.prototype.readIntLE=function(t,e,n){t>>>=0,e>>>=0,n||x(t,e,this.length);let i=this[t],r=1,a=0;for(;++a<e&&(r*=256);)i+=this[t+a]*r;return r*=128,i>=r&&(i-=Math.pow(2,8*e)),i},o.prototype.readIntBE=function(t,e,n){t>>>=0,e>>>=0,n||x(t,e,this.length);let i=e,r=1,a=this[t+--i];for(;i>0&&(r*=256);)a+=this[t+--i]*r;return r*=128,a>=r&&(a-=Math.pow(2,8*e)),a},o.prototype.readInt8=function(t,e){return t>>>=0,e||x(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},o.prototype.readInt16LE=function(t,e){t>>>=0,e||x(t,2,this.length);const n=this[t]|this[t+1]<<8;return 32768&n?4294901760|n:n},o.prototype.readInt16BE=function(t,e){t>>>=0,e||x(t,2,this.length);const n=this[t+1]|this[t]<<8;return 32768&n?4294901760|n:n},o.prototype.readInt32LE=function(t,e){return t>>>=0,e||x(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},o.prototype.readInt32BE=function(t,e){return t>>>=0,e||x(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},o.prototype.readBigInt64LE=Y((function(t){j(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||D(t,this.length-8);const i=this[t+4]+256*this[t+5]+65536*this[t+6]+(n<<24);return(BigInt(i)<<BigInt(32))+BigInt(e+256*this[++t]+65536*this[++t]+this[++t]*2**24)})),o.prototype.readBigInt64BE=Y((function(t){j(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||D(t,this.length-8);const i=(e<<24)+65536*this[++t]+256*this[++t]+this[++t];return(BigInt(i)<<BigInt(32))+BigInt(this[++t]*2**24+65536*this[++t]+256*this[++t]+n)})),o.prototype.readFloatLE=function(t,e){return t>>>=0,e||x(t,4,this.length),n.read(this,t,!0,23,4)},o.prototype.readFloatBE=function(t,e){return t>>>=0,e||x(t,4,this.length),n.read(this,t,!1,23,4)},o.prototype.readDoubleLE=function(t,e){return t>>>=0,e||x(t,8,this.length),n.read(this,t,!0,52,8)},o.prototype.readDoubleBE=function(t,e){return t>>>=0,e||x(t,8,this.length),n.read(this,t,!1,52,8)},o.prototype.writeUintLE=o.prototype.writeUIntLE=function(t,e,n,i){if(t=+t,e>>>=0,n>>>=0,!i){R(this,t,e,n,Math.pow(2,8*n)-1,0)}let r=1,a=0;for(this[e]=255&t;++a<n&&(r*=256);)this[e+a]=t/r&255;return e+n},o.prototype.writeUintBE=o.prototype.writeUIntBE=function(t,e,n,i){if(t=+t,e>>>=0,n>>>=0,!i){R(this,t,e,n,Math.pow(2,8*n)-1,0)}let r=n-1,a=1;for(this[e+r]=255&t;--r>=0&&(a*=256);)this[e+r]=t/a&255;return e+n},o.prototype.writeUint8=o.prototype.writeUInt8=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,1,255,0),this[e]=255&t,e+1},o.prototype.writeUint16LE=o.prototype.writeUInt16LE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,2,65535,0),this[e]=255&t,this[e+1]=t>>>8,e+2},o.prototype.writeUint16BE=o.prototype.writeUInt16BE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,2,65535,0),this[e]=t>>>8,this[e+1]=255&t,e+2},o.prototype.writeUint32LE=o.prototype.writeUInt32LE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,4,4294967295,0),this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t,e+4},o.prototype.writeUint32BE=o.prototype.writeUInt32BE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,4,4294967295,0),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},o.prototype.writeBigUInt64LE=Y((function(t,e=0){return _(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))})),o.prototype.writeBigUInt64BE=Y((function(t,e=0){return U(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))})),o.prototype.writeIntLE=function(t,e,n,i){if(t=+t,e>>>=0,!i){const i=Math.pow(2,8*n-1);R(this,t,e,n,i-1,-i)}let r=0,a=1,o=0;for(this[e]=255&t;++r<n&&(a*=256);)t<0&&0===o&&0!==this[e+r-1]&&(o=1),this[e+r]=(t/a>>0)-o&255;return e+n},o.prototype.writeIntBE=function(t,e,n,i){if(t=+t,e>>>=0,!i){const i=Math.pow(2,8*n-1);R(this,t,e,n,i-1,-i)}let r=n-1,a=1,o=0;for(this[e+r]=255&t;--r>=0&&(a*=256);)t<0&&0===o&&0!==this[e+r+1]&&(o=1),this[e+r]=(t/a>>0)-o&255;return e+n},o.prototype.writeInt8=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,1,127,-128),t<0&&(t=255+t+1),this[e]=255&t,e+1},o.prototype.writeInt16LE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,2,32767,-32768),this[e]=255&t,this[e+1]=t>>>8,e+2},o.prototype.writeInt16BE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,2,32767,-32768),this[e]=t>>>8,this[e+1]=255&t,e+2},o.prototype.writeInt32LE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,4,2147483647,-2147483648),this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24,e+4},o.prototype.writeInt32BE=function(t,e,n){return t=+t,e>>>=0,n||R(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},o.prototype.writeBigInt64LE=Y((function(t,e=0){return _(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))})),o.prototype.writeBigInt64BE=Y((function(t,e=0){return U(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))})),o.prototype.writeFloatLE=function(t,e,n){return O(this,t,e,!0,n)},o.prototype.writeFloatBE=function(t,e,n){return O(this,t,e,!1,n)},o.prototype.writeDoubleLE=function(t,e,n){return M(this,t,e,!0,n)},o.prototype.writeDoubleBE=function(t,e,n){return M(this,t,e,!1,n)},o.prototype.copy=function(t,e,n,i){if(!o.isBuffer(t))throw new TypeError("argument should be a Buffer");if(n||(n=0),i||0===i||(i=this.length),e>=t.length&&(e=t.length),e||(e=0),i>0&&i<n&&(i=n),i===n)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(n<0||n>=this.length)throw new RangeError("Index out of range");if(i<0)throw new RangeError("sourceEnd out of bounds");i>this.length&&(i=this.length),t.length-e<i-n&&(i=t.length-e+n);const r=i-n;return this===t&&"function"==typeof Uint8Array.prototype.copyWithin?this.copyWithin(e,n,i):Uint8Array.prototype.set.call(t,this.subarray(n,i),e),r},o.prototype.fill=function(t,e,n,i){if("string"==typeof t){if("string"==typeof e?(i=e,e=0,n=this.length):"string"==typeof n&&(i=n,n=this.length),void 0!==i&&"string"!=typeof i)throw new TypeError("encoding must be a string");if("string"==typeof i&&!o.isEncoding(i))throw new TypeError("Unknown encoding: "+i);if(1===t.length){const e=t.charCodeAt(0);("utf8"===i&&e<128||"latin1"===i)&&(t=e)}}else"number"==typeof t?t&=255:"boolean"==typeof t&&(t=Number(t));if(e<0||this.length<e||this.length<n)throw new RangeError("Out of range index");if(n<=e)return this;let r;if(e>>>=0,n=void 0===n?this.length:n>>>0,t||(t=0),"number"==typeof t)for(r=e;r<n;++r)this[r]=t;else{const a=o.isBuffer(t)?t:o.from(t,i),s=a.length;if(0===s)throw new TypeError('The value "'+t+'" is invalid for argument "value"');for(r=0;r<n-e;++r)this[r+e]=a[r%s]}return this};const $={};function L(t,e,n){$[t]=class extends n{constructor(){super(),Object.defineProperty(this,"message",{value:e.apply(this,arguments),writable:!0,configurable:!0}),this.name=`${this.name} [${t}]`,this.stack,delete this.name}get code(){return t}set code(t){Object.defineProperty(this,"code",{configurable:!0,enumerable:!0,value:t,writable:!0})}toString(){return`${this.name} [${t}]: ${this.message}`}}}function H(t){let e="",n=t.length;const i="-"===t[0]?1:0;for(;n>=i+4;n-=3)e=`_${t.slice(n-3,n)}${e}`;return`${t.slice(0,n)}${e}`}function K(t,e,n,i,r,a){if(t>n||t<e){const i="bigint"==typeof e?"n":"";let r;throw r=a>3?0===e||e===BigInt(0)?`>= 0${i} and < 2${i} ** ${8*(a+1)}${i}`:`>= -(2${i} ** ${8*(a+1)-1}${i}) and < 2 ** ${8*(a+1)-1}${i}`:`>= ${e}${i} and <= ${n}${i}`,new $.ERR_OUT_OF_RANGE("value",r,t)}!function(t,e,n){j(e,"offset"),void 0!==t[e]&&void 0!==t[e+n]||D(e,t.length-(n+1))}(i,r,a)}function j(t,e){if("number"!=typeof t)throw new $.ERR_INVALID_ARG_TYPE(e,"number",t)}function D(t,e,n){if(Math.floor(t)!==t)throw j(t,n),new $.ERR_OUT_OF_RANGE(n||"offset","an integer",t);if(e<0)throw new $.ERR_BUFFER_OUT_OF_BOUNDS;throw new $.ERR_OUT_OF_RANGE(n||"offset",`>= ${n?1:0} and <= ${e}`,t)}L("ERR_BUFFER_OUT_OF_BOUNDS",(function(t){return t?`${t} is outside of buffer bounds`:"Attempt to access memory outside buffer bounds"}),RangeError),L("ERR_INVALID_ARG_TYPE",(function(t,e){return`The "${t}" argument must be of type number. Received type ${typeof e}`}),TypeError),L("ERR_OUT_OF_RANGE",(function(t,e,n){let i=`The value of "${t}" is out of range.`,r=n;return Number.isInteger(n)&&Math.abs(n)>2**32?r=H(String(n)):"bigint"==typeof n&&(r=String(n),(n>BigInt(2)**BigInt(32)||n<-(BigInt(2)**BigInt(32)))&&(r=H(r)),r+="n"),i+=` It must be ${e}. Received ${r}`,i}),RangeError);const V=/[^+/0-9A-Za-z-_]/g;function q(t,e){let n;e=e||1/0;const i=t.length;let r=null;const a=[];for(let o=0;o<i;++o){if(n=t.charCodeAt(o),n>55295&&n<57344){if(!r){if(n>56319){(e-=3)>-1&&a.push(239,191,189);continue}if(o+1===i){(e-=3)>-1&&a.push(239,191,189);continue}r=n;continue}if(n<56320){(e-=3)>-1&&a.push(239,191,189),r=n;continue}n=65536+(r-55296<<10|n-56320)}else r&&(e-=3)>-1&&a.push(239,191,189);if(r=null,n<128){if((e-=1)<0)break;a.push(n)}else if(n<2048){if((e-=2)<0)break;a.push(n>>6|192,63&n|128)}else if(n<65536){if((e-=3)<0)break;a.push(n>>12|224,n>>6&63|128,63&n|128)}else{if(!(n<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;a.push(n>>18|240,n>>12&63|128,n>>6&63|128,63&n|128)}}return a}function G(t){return e.toByteArray(function(t){if((t=(t=t.split("=")[0]).trim().replace(V,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function X(t,e,n,i){let r;for(r=0;r<i&&!(r+n>=e.length||r>=t.length);++r)e[r+n]=t[r];return r}function z(t,e){return t instanceof e||null!=t&&null!=t.constructor&&null!=t.constructor.name&&t.constructor.name===e.name}function J(t){return t!=t}const W=function(){const t="0123456789abcdef",e=new Array(256);for(let n=0;n<16;++n){const i=16*n;for(let r=0;r<16;++r)e[i+r]=t[n]+t[r]}return e}();function Y(t){return"undefined"==typeof BigInt?Z:t}function Z(){throw new Error("BigInt not supported")}}(G);const et="_/@#/",nt="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");function it(t=16,e=nt){const n=new Uint16Array(t-1);H.getRandomValues(n);return`c${Array.from(n).map((t=>e[t%e.length])).join("")}`}async function rt(t,e){const n=await V(G.Buffer.from(t),e),i=G.Buffer.from(n).toString("hex");return G.Buffer.from(i).toString("base64")}async function at(t,e,n){const i=G.Buffer.from(t,"base64").toString("utf-8"),r=G.Buffer.from(i,"hex");return await async function(t,e,n){const i=await q(D(n));return await H.subtle.verify("HMAC",i,t,e)}(r,G.Buffer.from(e),n)}async function ot(t,e,n,i,r,a,o=86400){return{clientIP:t,userId:e??it(),match:n,mitigate:i,captcha:r,mitataMaxAge:o,netaceaSettings:a??{}}}async function st(t,e,n,i,r,a="/"){const o=[e.netaceaCookieName,e.netaceaCaptchaCookieName];if(void 0!==t.cachedEncryption&&o.includes(n)){return`${n}=${await t.cachedEncryption.encrypt(i)}; Max-Age=${r}; Path=${a}`}return`${n}=${i}; Max-Age=${r}; Path=${a}`}class ct extends Error{static fromResponse(t){let e="Unknown error";switch(t.status){case 403:e="Invalid credentials";break;case 500:e="Server error";break;case 502:e="Bad Gateway";break;case 503:e="Service Unavailable";break;case 400:e="Invalid request"}return new ct(e)}}async function ut(t,e,n,i,r,a,o){if(200!==i.status)throw ct.fromResponse(i);let s=parseInt(i.headers[u.match]),c=parseInt(i.headers[u.mitigate]),f=parseInt(i.headers[u.captcha]);isNaN(s)&&(s=n?.match??0),isNaN(c)&&(c=n?.mitigate??0),isNaN(f)&&(f=n?.captcha??0);let h=parseInt(i.headers[u.mitataExpiry]);isNaN(h)&&(h=86400);const p=await ot(a,r,s,c,f,o),l=await async function(t,e,n){if(Object.prototype.hasOwnProperty.call(n,u.mitataCaptcha)){const i=n[u.mitataCaptcha],r=parseInt(n[u.mitataCaptchaExpiry]),a=await st(t,e,e.netaceaCaptchaCookieName,i,r);if(void 0!==a)return a}}(t,e,i.headers),d=i.headers[u.eventId];return{status:i.status,match:s,mitigate:c,captcha:f,responseNetaceaCookie:p,responseNetaceaCaptchaCookie:l,body:i.body,eventId:d,mitataMaxAge:h,netaceaSettings:o??{}}}function ft(t){const e=Object.entries(t.getHeaders()),n={};for(const[t,i]of e)n[t]=i[0];return n}async function ht(t,e,n){const{method:i,path:r,headers:a,body:o,timeout:s}=n;let{host:c}=n;c.endsWith("/")&&(c=c.slice(0,-1));const u=c+r,{httpRequest:f}=t,h=await f(u,{body:o,headers:a,method:i,timeout:s??3e3});return{status:h.status,body:await h.text(),headers:ft(h)}}function pt(t){return Object.entries(t).filter((([,t])=>void 0!==t)).map((t=>t.join("="))).join("; ")}function lt(t){const e=t?.toLowerCase()??"text/html",n=e?.includes("text/html")||e?.includes("application/html"),i=e?.includes("application/json");return i&&!n?"application/json":"text/html"}function dt(t,e,n){let i;if(void 0===t||""===t)return"";if("string"==typeof t&&(i=JSON.parse(t)),!function(t){if(null==t)return!1;const e=t;return void 0!==e?.captchaSiteKey&&void 0!==e?.trackingId&&void 0!==e?.captchaURL}(i))throw new Error("Body is not a Mitigation Service JSON response!");const r=`${n}?trackingId=${i.trackingId}`,a=`https://${e}${r}`;return JSON.stringify({captchaRelativeURL:r,captchaAbsoluteURL:a})}async function gt(t,e,n,i){const r=void 0!==n.trackingId?`?trackingId=${n.trackingId}`:"";return await async function(t,e,n,i,r,a){const{userId:o,clientIP:s,host:c}=n,{mitigationServiceUrl:u,timeout:f}=e;if(void 0===u)throw new Error("Mitigation Service URL is not defined!");const h=function(t,e){const{apiKey:n,userId:i,userAgent:r,captchaCookie:a,clientIP:o,captchaSiteKey:s,captchaSecretKey:c,accept:u}=e,f={"X-Netacea-API-Key":n,"X-Netacea-Client-IP":o,"user-agent":r,cookie:pt({_mitatacaptcha:a})};void 0!==i&&(f["X-Netacea-UserId"]=i);void 0!==s&&void 0!==c&&(f["X-Netacea-Captcha-Site-Key"]=s,f["X-Netacea-Captcha-Secret-Key"]=c);t.captchaContentNegotiation&&void 0!==t.netaceaCaptchaPath&&(f["X-Netacea-Captcha-Content-Type"]=lt(u));return f}(e,n),p="application/json"===h["X-Netacea-Captcha-Content-Type"],l={host:u,path:i,headers:h,method:r,timeout:f},d=await ht(t,0,l);p&&void 0!==e.netaceaCaptchaPath&&void 0!==c&&(d.body=dt(d.body,c,e.netaceaCaptchaPath));return await ut(t,e,a,d,o,s)}(t,e,n,`/captcha${r}`,"GET",i)}async function yt(t,e,n,i){if(null==n)return;if("string"==typeof n)return await yt(t,e,n.split(";"),i);const r=`${i}=`;for(const o of n){const n=o.split(";")[0].trimStart();if(n.startsWith(r)){const o=n.slice(r.length);if(e.encryptedCookieNames.includes(i)&&(5===(a=o).split(".").length&&a.includes("..")))try{return await(t.cachedEncryption?.decrypt(o))}catch(t){return}return o}}var a}function mt(t,e,n=""){const i=t.getHeader(e)?.[0]?.trim();return i??n}function wt(t,e,n=""){return t.getVariable(e)??n}async function Et(t,e,n){return C(t)?function(t,e){if(void 0===t||""===t)return{mitata:void 0,requiresReissue:!1,isExpired:!1,shouldExpire:!1,isSameIP:!1,isPrimaryHashValid:!1,captcha:0,match:0,mitigate:0};const n=N(t);if(void 0!==n){const t=Math.floor(Date.now()/1e3),i=n.issueTimestamp+n.gracePeriod<t,r=e===n.clientIP;return{mitata:n,requiresReissue:i||!r,isExpired:i,shouldExpire:[1,3,5].includes(n.captcha),isSameIP:r,isPrimaryHashValid:!0,match:n.match,mitigate:n.mitigate,captcha:n.captcha}}return{mitata:void 0,requiresReissue:!1,isExpired:!1,shouldExpire:!1,isSameIP:!1,isPrimaryHashValid:!1,captcha:0,match:0,mitigate:0}}(t,e):await async function(t,e,n){if("string"!=typeof t||""===t)return{mitata:void 0,requiresReissue:!1,isExpired:!1,shouldExpire:!1,isSameIP:!1,isPrimaryHashValid:!1,captcha:0,match:0,mitigate:0};const i=S(t);if(void 0!==i){const t=Math.floor(Date.now()/1e3),r=parseInt(i.expiry)<t,a=[1,3,5].includes(i.captcha),o=e+"|"+i.expiry,s=await at(i.ipHash,o,n),c=[i.expiry,i.userId,i.ipHash,i.mitigationType].join(et);return{mitata:i,requiresReissue:r||!s,isExpired:r,shouldExpire:a,isSameIP:s,isPrimaryHashValid:await at(i.signature,c,n),match:i.match,mitigate:i.mitigate,captcha:i.captcha}}return{mitata:void 0,requiresReissue:!1,isExpired:!1,shouldExpire:!1,isSameIP:!1,isPrimaryHashValid:!1,captcha:0,match:0,mitigate:0}}(t,e,n??"")}function It(t,e){const n=t.split("&");for(const t of n){const[n,i]=t.split("=");if(n===e)return i}}function bt(t,e,n){const{netaceaBlockedResponseStatus:i,netaceaCaptchaResponseStatus:r}=t;let a;return a=Number("block"===e?i:r),(o=a)>=200&&o<600&&!Number.isNaN(o)&&3===o.toString().length?a:n;var o}async function St(t,e,{clientIP:n,userId:i,match:a,mitigate:o,captcha:c,issueReason:u,netaceaSettings:f,maxAge:h=86400,expiry:p}){const l=e.netaceaCookieExpirySeconds,d=[1,3,5].includes(c)?-l:l,g=p??Math.floor(Date.now()/1e3)+d,w=e.ingestType!==r.HTTP&&void 0!==t.cachedEncryption;if(!w&&void 0===e.secretKey)throw new Error("Cannot build cookie without secret key.");let E;if(w)I={clientIP:n,userId:i??"",match:a,mitigate:o,captcha:c,gracePeriod:d,cookieId:it(),issueTimestamp:Math.floor(Date.now()/1e3),issueReason:u??s.NO_SESSION,checkAllPostRequests:f.checkAllPostRequests},E=Object.entries(I).filter((([t,e])=>void 0!==e)).map((([t,e])=>t in m?`${m[t]}=${encodeURIComponent(e)}`:`${y[t]}=${encodeURIComponent(e)}`)).join("&");else{const t=[a,o,c].join("");E=await async function(t,e,n,i,r="000"){void 0===e&&(e=it());const a=[n,e,await rt(t+"|"+String(n),i),r].join(et);return`${await rt(a,i)}${et}${a}`}(n,i,g,e.secretKey,t)}var I;return await st(t,e,e.netaceaCookieName,E,h)}function vt(t,e){const n=e.getVariable(t.protoMitataVariableName);if(void 0!==n&&""!==n)try{return JSON.parse(n)}catch{return}}const{extractAndRemoveCookieAttr:Ct,removeDuplicateAttrs:At}=B.cookieAttributes;async function Nt(t,e,n,i){const r=L(n.path,n.method)?function(t,e){const n=(t,n)=>Number(e.getHeader(t)?.[0]??n),i=t.getHeader("x-netacea-client-ip")?.[0];if(null==i||""===i)throw new Error("Cannot build proto-mitata without Client IP");const r=t.getHeader("x-netacea-userid")?.[0]??"";let a={};const o=t.getHeader("x-netacea-settings")?.[0];return void 0!==o&&(a=JSON.parse(o)),{clientIP:i,userId:""!==r?r:it(),match:n("x-netacea-match",0),mitigate:n("x-netacea-mitigate",0),captcha:n("x-netacea-captcha",0),mitataMaxAge:n("x-netacea-mitata-expiry",86400),netaceaSettings:a}}(n,i):vt(e,n);if(void 0!==r){let n,{netaceaCookieAttributes:a}=e;if(""!==a){const{extractedAttribute:t,cookieAttributes:e}=Ct(a,"Max-Age");n=t,a=e}const o=await St(t,e,{clientIP:r.clientIP,userId:r.userId,match:r.match,mitigate:r.mitigate,captcha:r.captcha,maxAge:void 0!==n?parseInt(n,10):r.mitataMaxAge,netaceaSettings:r.netaceaSettings}),s=""!==a?At(`${o}; ${a}`,!0):o;i.addHeader("set-cookie",s)}}const{extractAndRemoveCookieAttr:kt,removeDuplicateAttrs:Tt}=B.cookieAttributes;async function Bt(t,e,n,i){const r=i.getHeader("set-cookie")??void 0;if(i.removeHeader("set-cookie"),200===i.status){await async function(t,e,n,i){const r=i?.find((t=>t.startsWith("_mitatacaptcha=")));if(void 0!==r){const i=r.slice(r.indexOf("=")+1);let[a,...o]=i.split(/; ?/);void 0!==t.cachedEncryption&&(a=await t.cachedEncryption.encrypt(a));let s=[`${e.netaceaCaptchaCookieName}=${a}`,...o].join("; ");if(""!==e.netaceaCaptchaCookieAttributes){const{extractedAttribute:t,cookieAttributes:n}=kt(Tt(e.netaceaCaptchaCookieAttributes,!0),"Max-Age"),{extractedAttribute:i}=kt(s,"Max-Age");void 0!==t&&void 0!==i?(s=s.replace(`Max-Age=${i}`,`Max-Age=${t}`),s=Tt(`${s}; ${n}`,!0)):s=Tt(`${s}; ${n}`,!0)}n.addHeader("set-cookie",s)}}(t,e,i,r),await Nt(t,e,n,i);const a=String(l.captchaPass),o=u.captcha,s=i.getHeader(o)?.[0];s!==a&&(i.status=403)}}function xt(t){return G.Buffer.from(t.replace(/-/g,"+").replace(/_/g,"/"),"base64")}function Rt(t,e){if("string"==typeof t&&void 0===e)throw new Error("Encoding should be provided to convert strings.");return function(t){return t.replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}(("string"==typeof t?G.Buffer.from(t,e):G.Buffer.from(t)).toString("base64"))}const _t=128,Ut=Rt(JSON.stringify({alg:"dir",enc:"A128CBC-HS256"}),"utf-8"),Pt=(new K).encode(Ut);async function Ot(t){return await H.subtle.importKey("raw",t,{name:"AES-CBC"},!1,["encrypt","decrypt"])}async function Mt({plaintext:t,secret:e,ivBase64Url:n}){const i=xt(e),r=i.subarray(_t>>3),a=i.subarray(0,_t>>3),o=await Ot(r),s=void 0!==n?xt(n):function(){const t=new Uint8Array(16);return H.getRandomValues(t),t}(),c=(new K).encode(t),u=await async function(t,e,n){const i=await H.subtle.encrypt({name:"AES-CBC",length:_t,iv:t},e,n);return new Uint8Array(i)}(s,o,c),f=new Uint8Array(s),h=Ht(Pt,f,u),p=await Lt(h,a);return[Ut,"",Rt(s),Rt(u),Rt(p)].join(".")}async function $t({jwe:t,secret:e}){const n=xt(e),i=n.subarray(_t>>3),r=n.subarray(0,_t>>3),{header:a,iv:o,payload:s,mac:c}=function(t){const e=t.split(".");if(5!==e.length)throw new Error(`JWE should have 5 parts, got ${e.length}`);const[n,i,r,a,o]=e;return{header:n,key:i,iv:r,payload:a,mac:o}}(t);if(a!==Ut)throw new Error("Incorrect JWE header");const u=xt(o),f=new Uint8Array(xt(s)),h=Ht((new K).encode(a),u,f);if(Rt(await Lt(h,r))!==c)throw new Error("MAC does not match JWE data");const p=await Ot(i),l=await async function(t,e,n){const i=await H.subtle.decrypt({name:"AES-CBC",length:_t,iv:t},e,n);return new Uint8Array(i)}(u,p,f);return(new j).decode(l)}async function Lt(t,e){return(await V(t,e)).slice(0,_t>>3)}function Ht(t,e,n){return function(t){const e=t.reduce(((t,{length:e})=>t+e),0),n=new Uint8Array(e);let i=0;return t.forEach((t=>{n.set(t,i),i+=t.length})),n}([t,e,n,Dt(t.length<<3)])}const Kt=2**32;function jt(t,e,n){if(e<0||e>=Kt)throw new RangeError(`value must be >= 0 and <= ${Kt-1}. Received ${e}`);t.set([e>>>24,e>>>16,e>>>8,255&e],n)}function Dt(t){const e=Math.floor(t/Kt),n=t%Kt,i=new Uint8Array(8);return jt(i,e,0),jt(i,n,4),i}class Vt{constructor(t){this.maxSize=t,this.cachedItems=[]}getCacheSize(){return this.cachedItems.length}delete(t){this.cachedItems=this.cachedItems.filter((e=>e.key!==t))}add(t,e){this.delete(t),this.cachedItems.push({key:t,value:e}),this.cachedItems.length>this.maxSize&&this.cachedItems.shift()}get(t){return this.cachedItems.find((e=>e.key===t))}}class qt{constructor(t){this.encryptionKey=t.encryptionKey,this.cache=new Vt(t.cacheMaxSize??5)}getCacheSize(){return this.cache.getCacheSize()}async memoizeFn(t,e){const n=this.cache.get(t);if(void 0!==n)return n.value;const i=await e();return this.cache.add(t,i),i}async encrypt(t){const e=`encrypt::${t}`;return await this.memoizeFn(e,(async()=>await Mt({plaintext:t,secret:this.encryptionKey})))}async decrypt(t){const e=`encrypt::${t}`;return await this.memoizeFn(e,(async()=>await $t({jwe:t,secret:this.encryptionKey})))}}function Gt(t){return C(t)?N(t):S(t)}function Ft(t,e){return e.bytesSent=""===e.bytesSent?"0":e.bytesSent,t.logVersion===a.V2?function({ip:t,userAgent:e,status:n,method:i,path:r,protocol:a,referer:o,bytesSent:s,requestTime:c,mitataCookie:u,sessionStatus:f,integrationType:h,integrationVersion:p}){const l=new Date;"/"!==r[0]&&(r=`/${r}`);let d;const g=r.split("?");g.length>1&&(d=`?${g[1]}`);const y=g[0],m=Gt(u)?.userId;return{status:n,method:i,bytes_sent:v(s),referrer:""===o?void 0:o,request:`${i} ${y}${d??""} ${a}`,request_time:v(c),integration_type:h,integration_version:p,client:t,user_agent:e,bc_type:""===f?void 0:f,hour:l.getUTCHours(),minute:l.getUTCMinutes(),"@timestamp":l.toISOString().replace("Z","+00:00"),path:y,protocol:a,query:d,user_id:m}}(e):function({ip:t,userAgent:e,status:n,method:i,path:r,protocol:a,referer:o,bytesSent:s,requestTime:c,mitataCookie:u,sessionStatus:f,integrationType:h,integrationVersion:p}){const l=(new Date).toUTCString();return{Request:`${i} ${r} ${a}`,TimeLocal:l,RealIp:t,UserAgent:e,Status:n,RequestTime:c?.toString(),BytesSent:s?.toString(),Referer:""===o?"-":o,NetaceaUserIdCookie:u??"",NetaceaMitigationApplied:f??"",IntegrationType:h??"",IntegrationVersion:p??""}}(e)}async function Xt(t,e,n){const i=Ft(e,n);if(e.ingestType!==r.KINESIS){const n={"X-Netacea-API-Key":e.apiKey??"","content-type":"application/json"},r=await async function(t,e,n,i){return await ht(t,0,{host:e.ingestServiceUrl,method:"POST",path:"/",headers:n,body:JSON.stringify(i),timeout:e.timeout})}(t,e,n,i);if(200!==r.status)throw ct.fromResponse(r)}}function zt(t,e,n,i,r){const a="unknown";r||(2===i?i=4:3===i&&(i=5));let s=f[e]??a+"_";s+=h[n]??a;let u=d[n];if(0!==i){s+=","+(p[i]??a);const t=g[i];void 0!==t&&(u=t)}return t.mitigationType===o.INJECT&&(u=c.none),{sessionStatus:s,mitigation:u,parts:{match:e,mitigate:n,captcha:i}}}var Jt="@netacea/akamai",Wt="6.1.18";async function Yt(t,e,n,i){e.ingestType===r.HTTP&&await async function(t,e,n,i){const r=i.getHeader("set-cookie")??[],a=0!==r.length?r?.join("; "):mt(n,"cookie");let o=await yt(t,e,a,e.netaceaCookieName)??"";if(void 0===o||""===o){const i=mt(n,"cookie");o=await yt(t,e,i,e.netaceaCookieName)??""}const{match:s,mitigate:c,captcha:u}=Gt(o)??{match:0,mitigate:0,captcha:0},{sessionStatus:f}=zt(e,s,c,u,L(n.path,n.method));try{Xt(t,e,{bytesSent:mt(i,"content-length","0"),ip:wt(n,"PMUSER_CLIENT_IP",""),method:n.method,path:n.url,protocol:mt(n,"protocol","HTTP/1.1"),referer:mt(n,"referer",""),requestTime:"0",sessionStatus:f,status:String(i.status),userAgent:mt(n,"user-agent","-"),mitataCookie:o,integrationType:Jt.replace("@netacea/",""),integrationVersion:Wt})}catch(e){const{logger:n}=t;n.log("Error in callIngest():",e)}}(t,e,n,i)}async function Zt(t){const e=function(t){return Object.keys(t).filter((t=>{const e=t.toLowerCase();return"cookie"!==e&&"referer"!==e&&!e.startsWith("x-netacea-")})).join(",")}(t);return await Qt(e)}async function Qt(t){let e="";try{e=await async function(t,e){const n=(new K).encode(e),i=await H.subtle.digest(t,n);return Array.from(new Uint8Array(i)).map((t=>t.toString(16).padStart(2,"0"))).join("")}("SHA-256",t)}catch(t){e=""}return e}async function te(t){const e=t.getHeaders(),n=await Zt(e),i=function(t,e){const n=t.getHeader(e);return null==n?[]:n}(t,"cookie").map((t=>t.split(/; ?/).map((t=>t.split("=")[0])))).flat(),r=await async function(t){const e=t.join(",");return await Qt(e)}(i);return[""===n?n:`h_${n.substring(1,15)}`,""===r?r:`c_${r.substring(1,15)}`]}const ee={mitigationStatusCode:void 0,sessionReason:void 0,sessionStartTime:void 0};async function ne(t,e,n,i){try{const t=vt(e,n),r=t?.userId??i?.userId??"",a=function(t,e,n,i){const r=i?.match??n?.match??0,a=i?.mitigate??n?.mitigate??0,o=i?.captcha??n?.captcha??0,{sessionStatus:s}=zt(t,r,a,o,L(e.path,e.method));return s}(e,n,i,t),[o,s]=await te(n),{mitigationType:c}=e,u=[r,a,Wt,o,s,ee.sessionReason??"",ee.sessionStartTime??"",c,ee.mitigationStatusCode??""].join(";");n.setVariable("PMUSER_NETACEA_DS2_CUSTOM_FIELD",u),function(){for(const t in ee)ee[t]=void 0}()}catch(e){const{logger:n}=t;n.log(`Error setting PMUSER_NETACEA_DS2_CUSTOM_FIELD ${e}`)}}function ie(t){for(const e in ee)void 0!==t[e]&&(ee[e]=t[e])}function re(t,e){ie({sessionReason:t,sessionStartTime:e??Math.floor(Date.now()/1e3)})}function ae(t){t.route({origin:"mitigations"})}var oe;async function se(t,e,n,i,r,a){const o={"X-Netacea-API-Key":e.apiKey,"X-Netacea-Client-IP":n.clientIp,"user-agent":n.userAgent,cookie:pt({_mitatacaptcha:n.netaceaSession.captchaToken})};void 0!==n.netaceaSession.userId&&(o["X-Netacea-UserId"]=n.netaceaSession.userId),void 0!==e.captchaSiteKey&&void 0!==e.captchaSecretKey&&(o["X-Netacea-Captcha-Site-Key"]=e.captchaSiteKey,o["X-Netacea-Captcha-Secret-Key"]=e.captchaSecretKey),e.captchaContentNegotiation&&void 0!==e.netaceaCaptchaPath&&(o["X-Netacea-Captcha-Content-Type"]=lt(i));const s=function(t){let e="";const n=[];for(const e in w)void 0===t[e]&&n.push(e);n.length>0&&(e=`?getSettings=${n.join(",")}`);return e}(a),c="application/json"===o["X-Netacea-Captcha-Content-Type"],u=await ht(t,0,{host:e.mitigationServiceUrl,path:"/"+s,headers:o,method:"GET",timeout:e.timeout});ie({mitigationStatusCode:u.status}),c&&void 0!==e.netaceaCaptchaPath&&(u.body=dt(u.body,r,e.netaceaCaptchaPath));let f={};return void 0!==u.headers["x-netacea-settings"]&&(f=function(t){const e={},{netaceaSettingsMap:n}=E;return decodeURIComponent(t).split("&").map((t=>t.split("="))).forEach((([t,i])=>{if(void 0!==i&&t in n){const r=parseInt(i,10);e[n[t]]=isNaN(r)?void 0:r}})),Object.entries(e).reduce(((t,[e,n])=>(void 0!==n&&(t[e]=n),t)),{})}(u.headers["x-netacea-settings"])),await ut(t,e,void 0,u,n.netaceaSession.userId,n.clientIp,{...a,...f})}!function(t){t[t.NO_SESSION=0]="NO_SESSION",t[t.NEW_SESSION=1]="NEW_SESSION",t[t.EXISTING_SESSION=2]="EXISTING_SESSION",t[t.RENEW_SESSION=3]="RENEW_SESSION"}(oe||(oe={}));const{mitigationTypes:ce}=E,ue=[ce.block,ce.captcha,ce.captchaPass];async function fe(t,e,n,i,r){const{netaceaCookieInfo:a,clientIp:o,netaceaSession:s}=n;let c={status:-1,match:(u=a).match,mitigate:u.mitigate,captcha:u.captcha,mitataMaxAge:86400,netaceaSettings:{},responseNetaceaCookie:void 0,responseNetaceaCaptchaCookie:void 0};var u;if(void 0===e.secretKey)throw new Error("Secret key is required to mitigate");const f=!a.isPrimaryHashValid||a.requiresReissue,h="post"===n.method.toLowerCase(),p=function(t){const e={};if(A(t)){const{netaceaSettingsMap:n}=E;for(const i in n){const r=t[n[i]];void 0!==r&&(e[i]=r)}}return e}(a.mitata),l=h&&1===p.checkAllPostRequests;if(f||l){c=await se(t,e,n,i,r,p),c.responseNetaceaCookie=await ot(o,s.userId,c.match,c.mitigate,c.captcha,c.netaceaSettings,c.mitataMaxAge);re(void 0===a.mitata?oe.NEW_SESSION:oe.RENEW_SESSION)}else{const t=a.mitata,e=A(d=t)?d.issueTimestamp:Number.parseInt(d.expiry,10)-60;re(oe.EXISTING_SESSION,e)}var d;const g=zt(e,c.match,c.mitigate,c.captcha,!1);return{body:c.body,apiCallStatus:c.status,responseNetaceaCookie:c?.responseNetaceaCookie,responseNetaceaCaptchaCookie:void 0,sessionStatus:g.sessionStatus,mitigation:g.mitigation,mitigated:ue.includes(g.mitigation),bestMitigation:g,eventId:c.eventId}}function he(t){return{apiCallStatus:0,responseNetaceaCookie:t,responseNetaceaCaptchaCookie:void 0,sessionStatus:"",mitigation:"000",mitigated:!1,bestMitigation:{sessionStatus:"",mitigation:"",parts:{match:0,mitigate:0,captcha:0}},eventId:void 0}}async function pe(t){return{sessionStatus:"",response:{headers:{},body:void 0,status:0,statusText:"",mitigation:"000"},bestAction:he(await ot(I,t,0,0,0,void 0,86400))}}async function le(t,e,n,i){try{switch(e.mitigationType){case o.MITIGATE:return await async function(t,e,n,i){const r=mt(n,"accept","text/html"),a=await fe(t,e,i,r,n.host),o={};let s,c;return a.mitigated&&(c=403,"captcha"===a.mitigation?(o["content-type"]="text/html; charset=UTF-8",s=a.body):s="Forbidden"),{response:{body:s,status:c,statusText:c?.toString(),headers:o,mitigation:a.mitigation},sessionStatus:a.sessionStatus,bestAction:a}}(t,e,n,i);case o.INJECT:return await async function(t,e,n,i){const r=mt(n,"accept","text/html"),a=await fe(t,e,i,r,n.host),o={"x-netacea-match":a.bestMitigation.parts.match.toString(),"x-netacea-mitigate":a.bestMitigation.parts.mitigate.toString(),"x-netacea-captcha":a.bestMitigation.parts.captcha.toString()};return void 0!==a.eventId&&(o["x-netacea-event-id"]=a.eventId),{injectHeaders:o,sessionStatus:a.sessionStatus,response:{headers:{}},bestAction:a}}(t,e,n,i);case o.INGEST:return await async function(t,e,n){if(void 0===e.secretKey)throw new Error("Secret key is required for ingest");const i=mt(n,"cookie"),r=await yt(t,e,i,e.netaceaCookieName),a=await Et(r,I,e.secretKey);return a.isPrimaryHashValid?a.requiresReissue?await pe(a.mitata?.userId):{sessionStatus:"",response:void 0,bestAction:he()}:await pe(void 0)}(t,e,n);default:throw new Error(`Netacea Error: Mitigation type ${e.mitigationType} not recognised`)}}catch(t){return console.error("Netacea FAILOPEN Error:",t),await Promise.resolve({injectHeaders:{"x-netacea-captcha":"0","x-netacea-match":"0","x-netacea-mitigate":"0"},sessionStatus:"",response:void 0,bestAction:void 0})}}async function de(t,e,n,i){let a={};if(void 0!==n?.bestAction?.responseNetaceaCookie){const t=n?.bestAction?.responseNetaceaCookie;void 0!==t&&i.setVariable(e.protoMitataVariableName,JSON.stringify(t))}if(e.mitigationType===o.INJECT){a=n.injectHeaders??{}}let s={};e.ingestType===r.ORIGIN&&(s=await async function(t,e,n){const i=mt(n,"cookie"),r=Gt(await yt(t,e,i,e.netaceaCookieName)??""),a=vt(e,n),{match:o,mitigate:s,captcha:c,userId:u}=a??r??{match:0,mitigate:0,captcha:0,userId:""},{sessionStatus:f}=zt(e,o,s,c,L(n.path,n.method));return{"X-Netacea-UserId":u,"X-Netacea-BC-Type":f,"X-Netacea-Integration-Type":Jt.replace("@netacea/",""),"X-Netacea-Integration-Version":Wt}}(t,e,i));for(const[t,e]of Object.entries({...a,...s}))i.addHeader(t,e)}class ge{constructor(n){this.configLoader=new U(n),this.defaultDeps={httpRequest:t,logger:e,cachedEncryption:void 0}}async requestHandler(t){const e=this.configLoader.loadConfig(t);void 0!==e.cookieEncryptionKey&&(this.defaultDeps.cachedEncryption=new qt({encryptionKey:e.cookieEncryptionKey}));const n=await async function(t,e,n){const i=wt(n,"PMUSER_CLIENT_IP"),r=mt(n,"user-agent"),a=mt(n,"cookie"),o=await yt(t,e,a,e.netaceaCookieName),s=await Et(o,i,e.secretKey),c=await yt(t,e,a,e.netaceaCaptchaCookieName);return{clientIp:i,userAgent:r,method:n.method,netaceaCookieInfo:s,netaceaSession:{userId:s.mitata?.userId,captchaToken:c,match:s.match,mitigate:s.mitigate,captcha:s.captcha}}}(this.defaultDeps,e,t);var i,r,a;i=t.path,r=t.method,void 0!==(a=e.netaceaCaptchaPath)&&a.length>0&&i===a&&"get"===r.toLowerCase()?await async function(t,e,n){const i=mt(n,"cookie"),r=await yt(t,e,i,e.netaceaCookieName),a=await yt(t,e,i,e.netaceaCaptchaCookieName),o=wt(n,"PMUSER_CLIENT_IP"),s=mt(n,"user-agent"),c=await Et(r,o,e.secretKey),u=bt(e,"captcha",403),f=await gt(t,e,{apiKey:e.apiKey??"",clientIP:o,userAgent:s,userId:c.mitata?.userId,captchaCookie:a,trackingId:It(n.query,"trackingId"),captchaSiteKey:e.captchaSiteKey,captchaSecretKey:e.captchaSecretKey,accept:mt(n,"accept","text/html"),host:n.host},{match:0,mitigate:0,captcha:1}),h=f.responseNetaceaCookie;void 0!==h&&n.setVariable(e.protoMitataVariableName,JSON.stringify(h));const p={};void 0!==e.netaceaCaptchaHeaderKey&&(p[e.netaceaCaptchaHeaderKey]=e.netaceaCaptchaHeaderValue),n.respondWith(u,p,f.body)}(this.defaultDeps,e,t):L(t.path,t.method)?await async function(t,e,n){const i=n.getVariable("PMUSER_CLIENT_IP")??"";n.setHeader("x-netacea-api-key",e.apiKey),n.setHeader("x-netacea-client-ip",i);const r=mt(n,"cookie"),a=await yt(t,e,r,e.netaceaCookieName)??"";let o="";const s=await Et(a,i,e.secretKey??"");if(A(s.mitata)){const t={checkAllPostRequests:s.mitata.checkAllPostRequests};n.setHeader("x-netacea-settings",JSON.stringify(t))}s.isPrimaryHashValid&&(o=s.mitata?.userId??""),n.setHeader("x-netacea-userid",o),ae(n)}(this.defaultDeps,e,t):M(t.path,t.method,e.useCaptchaRelAssets)?ae(t):await async function(t,e,n,i){const r=await le(t,e,n,i);if(void 0===r)throw new Error('Mitigation Service response "mitResult" was undefined.');const a=r.response?.mitigation;if("block"===a||"captcha"===a){const t=bt(e,a,r.response?.status??403),i={},o=r.bestAction?.responseNetaceaCookie;void 0!==o&&n.setVariable(e.protoMitataVariableName,JSON.stringify(o));const s=r.response?.body??"";(function(t){return t.includes("captchaRelativeURL")&&t.includes("captchaAbsoluteURL")})(s)&&(i["content-type"]="application/json"),"captcha"===a&&void 0!==e.netaceaCaptchaHeaderKey&&(i[e.netaceaCaptchaHeaderKey]=e.netaceaCaptchaHeaderValue),n.respondWith(t,i,s)}else await de(t,e,r,n)}(this.defaultDeps,e,t,n),await ne(this.defaultDeps,e,t,n.netaceaSession)}async responseHandler(t,e){const n=this.configLoader.loadConfig(t);if(void 0!==n.cookieEncryptionKey&&(this.defaultDeps.cachedEncryption=new qt({encryptionKey:n.cookieEncryptionKey})),L(t.path,t.method))await Bt(this.defaultDeps,n,t,e);else if($(t.path,t.method)&&!M(t.path,t.method,n.useCaptchaRelAssets)){mt(t,"cookie").includes(n.netaceaCookieName)&&await Nt(this.defaultDeps,n,t,e)}else M(t.path,t.method,n.useCaptchaRelAssets)||await Nt(this.defaultDeps,n,t,e);!function(t){t.removeHeader("x-netacea-match"),t.removeHeader("x-netacea-mitigate"),t.removeHeader("x-netacea-captcha"),t.removeHeader("x-netacea-mitata-expiry"),t.removeHeader("x-netacea-mitata-value"),t.removeHeader("x-netacea-mitatacaptcha-expiry"),t.removeHeader("x-netacea-mitatacaptcha-value"),t.removeHeader("x-netacea-event-id"),t.removeHeader("x-netacea-settings")}(e),await Yt(this.defaultDeps,n,t,e)}}export{o as NetaceaMitigationType,ge as default};
10
+ !function(t){const e=X,n=et,i="function"==typeof Symbol&&"function"==typeof Symbol.for?Symbol.for("nodejs.util.inspect.custom"):null;t.Buffer=o,t.SlowBuffer=function(t){+t!=t&&(t=0);return o.alloc(+t)},t.INSPECT_MAX_BYTES=50;const r=2147483647;function a(t){if(t>r)throw new RangeError('The value "'+t+'" is invalid for option "size"');const e=new Uint8Array(t);return Object.setPrototypeOf(e,o.prototype),e}function o(t,e,n){if("number"==typeof t){if("string"==typeof e)throw new TypeError('The "string" argument must be of type string. Received type number');return u(t)}return s(t,e,n)}function s(t,e,n){if("string"==typeof t)return function(t,e){"string"==typeof e&&""!==e||(e="utf8");if(!o.isEncoding(e))throw new TypeError("Unknown encoding: "+e);const n=0|l(t,e);let i=a(n);const r=i.write(t,e);r!==n&&(i=i.slice(0,r));return i}(t,e);if(ArrayBuffer.isView(t))return function(t){if(z(t,Uint8Array)){const e=new Uint8Array(t);return h(e.buffer,e.byteOffset,e.byteLength)}return f(t)}(t);if(null==t)throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t);if(z(t,ArrayBuffer)||t&&z(t.buffer,ArrayBuffer))return h(t,e,n);if("undefined"!=typeof SharedArrayBuffer&&(z(t,SharedArrayBuffer)||t&&z(t.buffer,SharedArrayBuffer)))return h(t,e,n);if("number"==typeof t)throw new TypeError('The "value" argument must not be of type number. Received type number');const i=t.valueOf&&t.valueOf();if(null!=i&&i!==t)return o.from(i,e,n);const r=function(t){if(o.isBuffer(t)){const e=0|p(t.length),n=a(e);return 0===n.length||t.copy(n,0,0,e),n}if(void 0!==t.length)return"number"!=typeof t.length||J(t.length)?a(0):f(t);if("Buffer"===t.type&&Array.isArray(t.data))return f(t.data)}(t);if(r)return r;if("undefined"!=typeof Symbol&&null!=Symbol.toPrimitive&&"function"==typeof t[Symbol.toPrimitive])return o.from(t[Symbol.toPrimitive]("string"),e,n);throw new TypeError("The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type "+typeof t)}function c(t){if("number"!=typeof t)throw new TypeError('"size" argument must be of type number');if(t<0)throw new RangeError('The value "'+t+'" is invalid for option "size"')}function u(t){return c(t),a(t<0?0:0|p(t))}function f(t){const e=t.length<0?0:0|p(t.length),n=a(e);for(let i=0;i<e;i+=1)n[i]=255&t[i];return n}function h(t,e,n){if(e<0||t.byteLength<e)throw new RangeError('"offset" is outside of buffer bounds');if(t.byteLength<e+(n||0))throw new RangeError('"length" is outside of buffer bounds');let i;return i=void 0===e&&void 0===n?new Uint8Array(t):void 0===n?new Uint8Array(t,e):new Uint8Array(t,e,n),Object.setPrototypeOf(i,o.prototype),i}function p(t){if(t>=r)throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+r.toString(16)+" bytes");return 0|t}function l(t,e){if(o.isBuffer(t))return t.length;if(ArrayBuffer.isView(t)||z(t,ArrayBuffer))return t.byteLength;if("string"!=typeof t)throw new TypeError('The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type '+typeof t);const n=t.length,i=arguments.length>2&&!0===arguments[2];if(!i&&0===n)return 0;let r=!1;for(;;)switch(e){case"ascii":case"latin1":case"binary":return n;case"utf8":case"utf-8":return q(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*n;case"hex":return n>>>1;case"base64":return G(t).length;default:if(r)return i?-1:q(t).length;e=(""+e).toLowerCase(),r=!0}}function d(t,e,n){let i=!1;if((void 0===e||e<0)&&(e=0),e>this.length)return"";if((void 0===n||n>this.length)&&(n=this.length),n<=0)return"";if((n>>>=0)<=(e>>>=0))return"";for(t||(t="utf8");;)switch(t){case"hex":return T(this,e,n);case"utf8":case"utf-8":return C(this,e,n);case"ascii":return N(this,e,n);case"latin1":case"binary":return k(this,e,n);case"base64":return v(this,e,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return R(this,e,n);default:if(i)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),i=!0}}function g(t,e,n){const i=t[e];t[e]=t[n],t[n]=i}function y(t,e,n,i,r){if(0===t.length)return-1;if("string"==typeof n?(i=n,n=0):n>2147483647?n=2147483647:n<-2147483648&&(n=-2147483648),J(n=+n)&&(n=r?0:t.length-1),n<0&&(n=t.length+n),n>=t.length){if(r)return-1;n=t.length-1}else if(n<0){if(!r)return-1;n=0}if("string"==typeof e&&(e=o.from(e,i)),o.isBuffer(e))return 0===e.length?-1:m(t,e,n,i,r);if("number"==typeof e)return e&=255,"function"==typeof Uint8Array.prototype.indexOf?r?Uint8Array.prototype.indexOf.call(t,e,n):Uint8Array.prototype.lastIndexOf.call(t,e,n):m(t,[e],n,i,r);throw new TypeError("val must be string, number or Buffer")}function m(t,e,n,i,r){let a,o=1,s=t.length,c=e.length;if(void 0!==i&&("ucs2"===(i=String(i).toLowerCase())||"ucs-2"===i||"utf16le"===i||"utf-16le"===i)){if(t.length<2||e.length<2)return-1;o=2,s/=2,c/=2,n/=2}function u(t,e){return 1===o?t[e]:t.readUInt16BE(e*o)}if(r){let i=-1;for(a=n;a<s;a++)if(u(t,a)===u(e,-1===i?0:a-i)){if(-1===i&&(i=a),a-i+1===c)return i*o}else-1!==i&&(a-=a-i),i=-1}else for(n+c>s&&(n=s-c),a=n;a>=0;a--){let n=!0;for(let i=0;i<c;i++)if(u(t,a+i)!==u(e,i)){n=!1;break}if(n)return a}return-1}function w(t,e,n,i){n=Number(n)||0;const r=t.length-n;i?(i=Number(i))>r&&(i=r):i=r;const a=e.length;let o;for(i>a/2&&(i=a/2),o=0;o<i;++o){const i=parseInt(e.substr(2*o,2),16);if(J(i))return o;t[n+o]=i}return o}function S(t,e,n,i){return F(q(e,t.length-n),t,n,i)}function E(t,e,n,i){return F(function(t){const e=[];for(let n=0;n<t.length;++n)e.push(255&t.charCodeAt(n));return e}(e),t,n,i)}function I(t,e,n,i){return F(G(e),t,n,i)}function b(t,e,n,i){return F(function(t,e){let n,i,r;const a=[];for(let o=0;o<t.length&&!((e-=2)<0);++o)n=t.charCodeAt(o),i=n>>8,r=n%256,a.push(r),a.push(i);return a}(e,t.length-n),t,n,i)}function v(t,n,i){return 0===n&&i===t.length?e.fromByteArray(t):e.fromByteArray(t.slice(n,i))}function C(t,e,n){n=Math.min(t.length,n);const i=[];let r=e;for(;r<n;){const e=t[r];let a=null,o=e>239?4:e>223?3:e>191?2:1;if(r+o<=n){let n,i,s,c;switch(o){case 1:e<128&&(a=e);break;case 2:n=t[r+1],128==(192&n)&&(c=(31&e)<<6|63&n,c>127&&(a=c));break;case 3:n=t[r+1],i=t[r+2],128==(192&n)&&128==(192&i)&&(c=(15&e)<<12|(63&n)<<6|63&i,c>2047&&(c<55296||c>57343)&&(a=c));break;case 4:n=t[r+1],i=t[r+2],s=t[r+3],128==(192&n)&&128==(192&i)&&128==(192&s)&&(c=(15&e)<<18|(63&n)<<12|(63&i)<<6|63&s,c>65535&&c<1114112&&(a=c))}}null===a?(a=65533,o=1):a>65535&&(a-=65536,i.push(a>>>10&1023|55296),a=56320|1023&a),i.push(a),r+=o}return function(t){const e=t.length;if(e<=A)return String.fromCharCode.apply(String,t);let n="",i=0;for(;i<e;)n+=String.fromCharCode.apply(String,t.slice(i,i+=A));return n}(i)}t.kMaxLength=r,o.TYPED_ARRAY_SUPPORT=function(){try{const t=new Uint8Array(1),e={foo:function(){return 42}};return Object.setPrototypeOf(e,Uint8Array.prototype),Object.setPrototypeOf(t,e),42===t.foo()}catch(t){return!1}}(),o.TYPED_ARRAY_SUPPORT||"undefined"==typeof console||"function"!=typeof console.error||console.error("This browser lacks typed array (Uint8Array) support which is required by `buffer` v5.x. Use `buffer` v4.x if you require old browser support."),Object.defineProperty(o.prototype,"parent",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.buffer}}),Object.defineProperty(o.prototype,"offset",{enumerable:!0,get:function(){if(o.isBuffer(this))return this.byteOffset}}),o.poolSize=8192,o.from=function(t,e,n){return s(t,e,n)},Object.setPrototypeOf(o.prototype,Uint8Array.prototype),Object.setPrototypeOf(o,Uint8Array),o.alloc=function(t,e,n){return function(t,e,n){return c(t),t<=0?a(t):void 0!==e?"string"==typeof n?a(t).fill(e,n):a(t).fill(e):a(t)}(t,e,n)},o.allocUnsafe=function(t){return u(t)},o.allocUnsafeSlow=function(t){return u(t)},o.isBuffer=function(t){return null!=t&&!0===t._isBuffer&&t!==o.prototype},o.compare=function(t,e){if(z(t,Uint8Array)&&(t=o.from(t,t.offset,t.byteLength)),z(e,Uint8Array)&&(e=o.from(e,e.offset,e.byteLength)),!o.isBuffer(t)||!o.isBuffer(e))throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');if(t===e)return 0;let n=t.length,i=e.length;for(let r=0,a=Math.min(n,i);r<a;++r)if(t[r]!==e[r]){n=t[r],i=e[r];break}return n<i?-1:i<n?1:0},o.isEncoding=function(t){switch(String(t).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(t,e){if(!Array.isArray(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return o.alloc(0);let n;if(void 0===e)for(e=0,n=0;n<t.length;++n)e+=t[n].length;const i=o.allocUnsafe(e);let r=0;for(n=0;n<t.length;++n){let e=t[n];if(z(e,Uint8Array))r+e.length>i.length?(o.isBuffer(e)||(e=o.from(e)),e.copy(i,r)):Uint8Array.prototype.set.call(i,e,r);else{if(!o.isBuffer(e))throw new TypeError('"list" argument must be an Array of Buffers');e.copy(i,r)}r+=e.length}return i},o.byteLength=l,o.prototype._isBuffer=!0,o.prototype.swap16=function(){const t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(let e=0;e<t;e+=2)g(this,e,e+1);return this},o.prototype.swap32=function(){const t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(let e=0;e<t;e+=4)g(this,e,e+3),g(this,e+1,e+2);return this},o.prototype.swap64=function(){const t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(let e=0;e<t;e+=8)g(this,e,e+7),g(this,e+1,e+6),g(this,e+2,e+5),g(this,e+3,e+4);return this},o.prototype.toString=function(){const t=this.length;return 0===t?"":0===arguments.length?C(this,0,t):d.apply(this,arguments)},o.prototype.toLocaleString=o.prototype.toString,o.prototype.equals=function(t){if(!o.isBuffer(t))throw new TypeError("Argument must be a Buffer");return this===t||0===o.compare(this,t)},o.prototype.inspect=function(){let e="";const n=t.INSPECT_MAX_BYTES;return e=this.toString("hex",0,n).replace(/(.{2})/g,"$1 ").trim(),this.length>n&&(e+=" ... "),"<Buffer "+e+">"},i&&(o.prototype[i]=o.prototype.inspect),o.prototype.compare=function(t,e,n,i,r){if(z(t,Uint8Array)&&(t=o.from(t,t.offset,t.byteLength)),!o.isBuffer(t))throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array. Received type '+typeof t);if(void 0===e&&(e=0),void 0===n&&(n=t?t.length:0),void 0===i&&(i=0),void 0===r&&(r=this.length),e<0||n>t.length||i<0||r>this.length)throw new RangeError("out of range index");if(i>=r&&e>=n)return 0;if(i>=r)return-1;if(e>=n)return 1;if(this===t)return 0;let a=(r>>>=0)-(i>>>=0),s=(n>>>=0)-(e>>>=0);const c=Math.min(a,s),u=this.slice(i,r),f=t.slice(e,n);for(let t=0;t<c;++t)if(u[t]!==f[t]){a=u[t],s=f[t];break}return a<s?-1:s<a?1:0},o.prototype.includes=function(t,e,n){return-1!==this.indexOf(t,e,n)},o.prototype.indexOf=function(t,e,n){return y(this,t,e,n,!0)},o.prototype.lastIndexOf=function(t,e,n){return y(this,t,e,n,!1)},o.prototype.write=function(t,e,n,i){if(void 0===e)i="utf8",n=this.length,e=0;else if(void 0===n&&"string"==typeof e)i=e,n=this.length,e=0;else{if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e>>>=0,isFinite(n)?(n>>>=0,void 0===i&&(i="utf8")):(i=n,n=void 0)}const r=this.length-e;if((void 0===n||n>r)&&(n=r),t.length>0&&(n<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");i||(i="utf8");let a=!1;for(;;)switch(i){case"hex":return w(this,t,e,n);case"utf8":case"utf-8":return S(this,t,e,n);case"ascii":case"latin1":case"binary":return E(this,t,e,n);case"base64":return I(this,t,e,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return b(this,t,e,n);default:if(a)throw new TypeError("Unknown encoding: "+i);i=(""+i).toLowerCase(),a=!0}},o.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};const A=4096;function N(t,e,n){let i="";n=Math.min(t.length,n);for(let r=e;r<n;++r)i+=String.fromCharCode(127&t[r]);return i}function k(t,e,n){let i="";n=Math.min(t.length,n);for(let r=e;r<n;++r)i+=String.fromCharCode(t[r]);return i}function T(t,e,n){const i=t.length;(!e||e<0)&&(e=0),(!n||n<0||n>i)&&(n=i);let r="";for(let i=e;i<n;++i)r+=W[t[i]];return r}function R(t,e,n){const i=t.slice(e,n);let r="";for(let t=0;t<i.length-1;t+=2)r+=String.fromCharCode(i[t]+256*i[t+1]);return r}function _(t,e,n){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(t+e>n)throw new RangeError("Trying to access beyond buffer length")}function B(t,e,n,i,r,a){if(!o.isBuffer(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(e>r||e<a)throw new RangeError('"value" argument is out of bounds');if(n+i>t.length)throw new RangeError("Index out of range")}function x(t,e,n,i,r){K(e,i,r,t,n,7);let a=Number(e&BigInt(4294967295));t[n++]=a,a>>=8,t[n++]=a,a>>=8,t[n++]=a,a>>=8,t[n++]=a;let o=Number(e>>BigInt(32)&BigInt(4294967295));return t[n++]=o,o>>=8,t[n++]=o,o>>=8,t[n++]=o,o>>=8,t[n++]=o,n}function O(t,e,n,i,r){K(e,i,r,t,n,7);let a=Number(e&BigInt(4294967295));t[n+7]=a,a>>=8,t[n+6]=a,a>>=8,t[n+5]=a,a>>=8,t[n+4]=a;let o=Number(e>>BigInt(32)&BigInt(4294967295));return t[n+3]=o,o>>=8,t[n+2]=o,o>>=8,t[n+1]=o,o>>=8,t[n]=o,n+8}function U(t,e,n,i,r,a){if(n+i>t.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("Index out of range")}function P(t,e,i,r,a){return e=+e,i>>>=0,a||U(t,0,i,4),n.write(t,e,i,r,23,4),i+4}function M(t,e,i,r,a){return e=+e,i>>>=0,a||U(t,0,i,8),n.write(t,e,i,r,52,8),i+8}o.prototype.slice=function(t,e){const n=this.length;(t=~~t)<0?(t+=n)<0&&(t=0):t>n&&(t=n),(e=void 0===e?n:~~e)<0?(e+=n)<0&&(e=0):e>n&&(e=n),e<t&&(e=t);const i=this.subarray(t,e);return Object.setPrototypeOf(i,o.prototype),i},o.prototype.readUintLE=o.prototype.readUIntLE=function(t,e,n){t>>>=0,e>>>=0,n||_(t,e,this.length);let i=this[t],r=1,a=0;for(;++a<e&&(r*=256);)i+=this[t+a]*r;return i},o.prototype.readUintBE=o.prototype.readUIntBE=function(t,e,n){t>>>=0,e>>>=0,n||_(t,e,this.length);let i=this[t+--e],r=1;for(;e>0&&(r*=256);)i+=this[t+--e]*r;return i},o.prototype.readUint8=o.prototype.readUInt8=function(t,e){return t>>>=0,e||_(t,1,this.length),this[t]},o.prototype.readUint16LE=o.prototype.readUInt16LE=function(t,e){return t>>>=0,e||_(t,2,this.length),this[t]|this[t+1]<<8},o.prototype.readUint16BE=o.prototype.readUInt16BE=function(t,e){return t>>>=0,e||_(t,2,this.length),this[t]<<8|this[t+1]},o.prototype.readUint32LE=o.prototype.readUInt32LE=function(t,e){return t>>>=0,e||_(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},o.prototype.readUint32BE=o.prototype.readUInt32BE=function(t,e){return t>>>=0,e||_(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},o.prototype.readBigUInt64LE=Y((function(t){D(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||j(t,this.length-8);const i=e+256*this[++t]+65536*this[++t]+this[++t]*2**24,r=this[++t]+256*this[++t]+65536*this[++t]+n*2**24;return BigInt(i)+(BigInt(r)<<BigInt(32))})),o.prototype.readBigUInt64BE=Y((function(t){D(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||j(t,this.length-8);const i=e*2**24+65536*this[++t]+256*this[++t]+this[++t],r=this[++t]*2**24+65536*this[++t]+256*this[++t]+n;return(BigInt(i)<<BigInt(32))+BigInt(r)})),o.prototype.readIntLE=function(t,e,n){t>>>=0,e>>>=0,n||_(t,e,this.length);let i=this[t],r=1,a=0;for(;++a<e&&(r*=256);)i+=this[t+a]*r;return r*=128,i>=r&&(i-=Math.pow(2,8*e)),i},o.prototype.readIntBE=function(t,e,n){t>>>=0,e>>>=0,n||_(t,e,this.length);let i=e,r=1,a=this[t+--i];for(;i>0&&(r*=256);)a+=this[t+--i]*r;return r*=128,a>=r&&(a-=Math.pow(2,8*e)),a},o.prototype.readInt8=function(t,e){return t>>>=0,e||_(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},o.prototype.readInt16LE=function(t,e){t>>>=0,e||_(t,2,this.length);const n=this[t]|this[t+1]<<8;return 32768&n?4294901760|n:n},o.prototype.readInt16BE=function(t,e){t>>>=0,e||_(t,2,this.length);const n=this[t+1]|this[t]<<8;return 32768&n?4294901760|n:n},o.prototype.readInt32LE=function(t,e){return t>>>=0,e||_(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},o.prototype.readInt32BE=function(t,e){return t>>>=0,e||_(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},o.prototype.readBigInt64LE=Y((function(t){D(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||j(t,this.length-8);const i=this[t+4]+256*this[t+5]+65536*this[t+6]+(n<<24);return(BigInt(i)<<BigInt(32))+BigInt(e+256*this[++t]+65536*this[++t]+this[++t]*2**24)})),o.prototype.readBigInt64BE=Y((function(t){D(t>>>=0,"offset");const e=this[t],n=this[t+7];void 0!==e&&void 0!==n||j(t,this.length-8);const i=(e<<24)+65536*this[++t]+256*this[++t]+this[++t];return(BigInt(i)<<BigInt(32))+BigInt(this[++t]*2**24+65536*this[++t]+256*this[++t]+n)})),o.prototype.readFloatLE=function(t,e){return t>>>=0,e||_(t,4,this.length),n.read(this,t,!0,23,4)},o.prototype.readFloatBE=function(t,e){return t>>>=0,e||_(t,4,this.length),n.read(this,t,!1,23,4)},o.prototype.readDoubleLE=function(t,e){return t>>>=0,e||_(t,8,this.length),n.read(this,t,!0,52,8)},o.prototype.readDoubleBE=function(t,e){return t>>>=0,e||_(t,8,this.length),n.read(this,t,!1,52,8)},o.prototype.writeUintLE=o.prototype.writeUIntLE=function(t,e,n,i){if(t=+t,e>>>=0,n>>>=0,!i){B(this,t,e,n,Math.pow(2,8*n)-1,0)}let r=1,a=0;for(this[e]=255&t;++a<n&&(r*=256);)this[e+a]=t/r&255;return e+n},o.prototype.writeUintBE=o.prototype.writeUIntBE=function(t,e,n,i){if(t=+t,e>>>=0,n>>>=0,!i){B(this,t,e,n,Math.pow(2,8*n)-1,0)}let r=n-1,a=1;for(this[e+r]=255&t;--r>=0&&(a*=256);)this[e+r]=t/a&255;return e+n},o.prototype.writeUint8=o.prototype.writeUInt8=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,1,255,0),this[e]=255&t,e+1},o.prototype.writeUint16LE=o.prototype.writeUInt16LE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,2,65535,0),this[e]=255&t,this[e+1]=t>>>8,e+2},o.prototype.writeUint16BE=o.prototype.writeUInt16BE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,2,65535,0),this[e]=t>>>8,this[e+1]=255&t,e+2},o.prototype.writeUint32LE=o.prototype.writeUInt32LE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,4,4294967295,0),this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t,e+4},o.prototype.writeUint32BE=o.prototype.writeUInt32BE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,4,4294967295,0),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},o.prototype.writeBigUInt64LE=Y((function(t,e=0){return x(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))})),o.prototype.writeBigUInt64BE=Y((function(t,e=0){return O(this,t,e,BigInt(0),BigInt("0xffffffffffffffff"))})),o.prototype.writeIntLE=function(t,e,n,i){if(t=+t,e>>>=0,!i){const i=Math.pow(2,8*n-1);B(this,t,e,n,i-1,-i)}let r=0,a=1,o=0;for(this[e]=255&t;++r<n&&(a*=256);)t<0&&0===o&&0!==this[e+r-1]&&(o=1),this[e+r]=(t/a>>0)-o&255;return e+n},o.prototype.writeIntBE=function(t,e,n,i){if(t=+t,e>>>=0,!i){const i=Math.pow(2,8*n-1);B(this,t,e,n,i-1,-i)}let r=n-1,a=1,o=0;for(this[e+r]=255&t;--r>=0&&(a*=256);)t<0&&0===o&&0!==this[e+r+1]&&(o=1),this[e+r]=(t/a>>0)-o&255;return e+n},o.prototype.writeInt8=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,1,127,-128),t<0&&(t=255+t+1),this[e]=255&t,e+1},o.prototype.writeInt16LE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,2,32767,-32768),this[e]=255&t,this[e+1]=t>>>8,e+2},o.prototype.writeInt16BE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,2,32767,-32768),this[e]=t>>>8,this[e+1]=255&t,e+2},o.prototype.writeInt32LE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,4,2147483647,-2147483648),this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24,e+4},o.prototype.writeInt32BE=function(t,e,n){return t=+t,e>>>=0,n||B(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t,e+4},o.prototype.writeBigInt64LE=Y((function(t,e=0){return x(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))})),o.prototype.writeBigInt64BE=Y((function(t,e=0){return O(this,t,e,-BigInt("0x8000000000000000"),BigInt("0x7fffffffffffffff"))})),o.prototype.writeFloatLE=function(t,e,n){return P(this,t,e,!0,n)},o.prototype.writeFloatBE=function(t,e,n){return P(this,t,e,!1,n)},o.prototype.writeDoubleLE=function(t,e,n){return M(this,t,e,!0,n)},o.prototype.writeDoubleBE=function(t,e,n){return M(this,t,e,!1,n)},o.prototype.copy=function(t,e,n,i){if(!o.isBuffer(t))throw new TypeError("argument should be a Buffer");if(n||(n=0),i||0===i||(i=this.length),e>=t.length&&(e=t.length),e||(e=0),i>0&&i<n&&(i=n),i===n)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(n<0||n>=this.length)throw new RangeError("Index out of range");if(i<0)throw new RangeError("sourceEnd out of bounds");i>this.length&&(i=this.length),t.length-e<i-n&&(i=t.length-e+n);const r=i-n;return this===t&&"function"==typeof Uint8Array.prototype.copyWithin?this.copyWithin(e,n,i):Uint8Array.prototype.set.call(t,this.subarray(n,i),e),r},o.prototype.fill=function(t,e,n,i){if("string"==typeof t){if("string"==typeof e?(i=e,e=0,n=this.length):"string"==typeof n&&(i=n,n=this.length),void 0!==i&&"string"!=typeof i)throw new TypeError("encoding must be a string");if("string"==typeof i&&!o.isEncoding(i))throw new TypeError("Unknown encoding: "+i);if(1===t.length){const e=t.charCodeAt(0);("utf8"===i&&e<128||"latin1"===i)&&(t=e)}}else"number"==typeof t?t&=255:"boolean"==typeof t&&(t=Number(t));if(e<0||this.length<e||this.length<n)throw new RangeError("Out of range index");if(n<=e)return this;let r;if(e>>>=0,n=void 0===n?this.length:n>>>0,t||(t=0),"number"==typeof t)for(r=e;r<n;++r)this[r]=t;else{const a=o.isBuffer(t)?t:o.from(t,i),s=a.length;if(0===s)throw new TypeError('The value "'+t+'" is invalid for argument "value"');for(r=0;r<n-e;++r)this[r+e]=a[r%s]}return this};const $={};function L(t,e,n){$[t]=class extends n{constructor(){super(),Object.defineProperty(this,"message",{value:e.apply(this,arguments),writable:!0,configurable:!0}),this.name=`${this.name} [${t}]`,this.stack,delete this.name}get code(){return t}set code(t){Object.defineProperty(this,"code",{configurable:!0,enumerable:!0,value:t,writable:!0})}toString(){return`${this.name} [${t}]: ${this.message}`}}}function H(t){let e="",n=t.length;const i="-"===t[0]?1:0;for(;n>=i+4;n-=3)e=`_${t.slice(n-3,n)}${e}`;return`${t.slice(0,n)}${e}`}function K(t,e,n,i,r,a){if(t>n||t<e){const i="bigint"==typeof e?"n":"";let r;throw r=a>3?0===e||e===BigInt(0)?`>= 0${i} and < 2${i} ** ${8*(a+1)}${i}`:`>= -(2${i} ** ${8*(a+1)-1}${i}) and < 2 ** ${8*(a+1)-1}${i}`:`>= ${e}${i} and <= ${n}${i}`,new $.ERR_OUT_OF_RANGE("value",r,t)}!function(t,e,n){D(e,"offset"),void 0!==t[e]&&void 0!==t[e+n]||j(e,t.length-(n+1))}(i,r,a)}function D(t,e){if("number"!=typeof t)throw new $.ERR_INVALID_ARG_TYPE(e,"number",t)}function j(t,e,n){if(Math.floor(t)!==t)throw D(t,n),new $.ERR_OUT_OF_RANGE(n||"offset","an integer",t);if(e<0)throw new $.ERR_BUFFER_OUT_OF_BOUNDS;throw new $.ERR_OUT_OF_RANGE(n||"offset",`>= ${n?1:0} and <= ${e}`,t)}L("ERR_BUFFER_OUT_OF_BOUNDS",(function(t){return t?`${t} is outside of buffer bounds`:"Attempt to access memory outside buffer bounds"}),RangeError),L("ERR_INVALID_ARG_TYPE",(function(t,e){return`The "${t}" argument must be of type number. Received type ${typeof e}`}),TypeError),L("ERR_OUT_OF_RANGE",(function(t,e,n){let i=`The value of "${t}" is out of range.`,r=n;return Number.isInteger(n)&&Math.abs(n)>2**32?r=H(String(n)):"bigint"==typeof n&&(r=String(n),(n>BigInt(2)**BigInt(32)||n<-(BigInt(2)**BigInt(32)))&&(r=H(r)),r+="n"),i+=` It must be ${e}. Received ${r}`,i}),RangeError);const V=/[^+/0-9A-Za-z-_]/g;function q(t,e){let n;e=e||1/0;const i=t.length;let r=null;const a=[];for(let o=0;o<i;++o){if(n=t.charCodeAt(o),n>55295&&n<57344){if(!r){if(n>56319){(e-=3)>-1&&a.push(239,191,189);continue}if(o+1===i){(e-=3)>-1&&a.push(239,191,189);continue}r=n;continue}if(n<56320){(e-=3)>-1&&a.push(239,191,189),r=n;continue}n=65536+(r-55296<<10|n-56320)}else r&&(e-=3)>-1&&a.push(239,191,189);if(r=null,n<128){if((e-=1)<0)break;a.push(n)}else if(n<2048){if((e-=2)<0)break;a.push(n>>6|192,63&n|128)}else if(n<65536){if((e-=3)<0)break;a.push(n>>12|224,n>>6&63|128,63&n|128)}else{if(!(n<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;a.push(n>>18|240,n>>12&63|128,n>>6&63|128,63&n|128)}}return a}function G(t){return e.toByteArray(function(t){if((t=(t=t.split("=")[0]).trim().replace(V,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function F(t,e,n,i){let r;for(r=0;r<i&&!(r+n>=e.length||r>=t.length);++r)e[r+n]=t[r];return r}function z(t,e){return t instanceof e||null!=t&&null!=t.constructor&&null!=t.constructor.name&&t.constructor.name===e.name}function J(t){return t!=t}const W=function(){const t="0123456789abcdef",e=new Array(256);for(let n=0;n<16;++n){const i=16*n;for(let r=0;r<16;++r)e[i+r]=t[n]+t[r]}return e}();function Y(t){return"undefined"==typeof BigInt?Z:t}function Z(){throw new Error("BigInt not supported")}}(F);const nt="_/@#/",it="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");function rt(t=16,e=it){const n=new Uint16Array(t-1);K.getRandomValues(n);return`c${Array.from(n).map((t=>e[t%e.length])).join("")}`}async function at(t,e){const n=await q(F.Buffer.from(t),e),i=F.Buffer.from(n).toString("hex");return F.Buffer.from(i).toString("base64")}async function ot(t,e,n){const i=F.Buffer.from(t,"base64").toString("utf-8"),r=F.Buffer.from(i,"hex");return await async function(t,e,n){const i=await G(V(n));return await K.subtle.verify("HMAC",i,t,e)}(r,F.Buffer.from(e),n)}async function st(t,e,n,i,r,a,o,s=86400){return{clientIP:t,userId:e??rt(),match:n,mitigate:i,captcha:r,mitataMaxAge:s,netaceaSettings:o??{},issueReason:a}}async function ct(t,e,n,i,r,a="/"){const o=[e.netaceaCookieName,e.netaceaCaptchaCookieName];if(void 0!==t.cachedEncryption&&o.includes(n)){return`${n}=${await t.cachedEncryption.encrypt(i)}; Max-Age=${r}; Path=${a}`}return`${n}=${i}; Max-Age=${r}; Path=${a}`}class ut extends Error{static fromResponse(t){let e="Unknown error";switch(t.status){case 403:e="Invalid credentials";break;case 500:e="Server error";break;case 502:e="Bad Gateway";break;case 503:e="Service Unavailable";break;case 400:e="Invalid request"}return new ut(e)}}async function ft(t,e,n,i,r,a,o=s.NO_SESSION,c){if(200!==i.status)throw ut.fromResponse(i);let u=parseInt(i.headers[f.match]),h=parseInt(i.headers[f.mitigate]),p=parseInt(i.headers[f.captcha]);isNaN(u)&&(u=n?.match??0),isNaN(h)&&(h=n?.mitigate??0),isNaN(p)&&(p=n?.captcha??0);let l=parseInt(i.headers[f.mitataExpiry]);isNaN(l)&&(l=86400);const d=await st(a,r,u,h,p,o,c),g=await async function(t,e,n){if(Object.prototype.hasOwnProperty.call(n,f.mitataCaptcha)){const i=n[f.mitataCaptcha],r=parseInt(n[f.mitataCaptchaExpiry]),a=await ct(t,e,e.netaceaCaptchaCookieName,i,r);if(void 0!==a)return a}}(t,e,i.headers),y=i.headers[f.eventId];return{status:i.status,match:u,mitigate:h,captcha:p,responseNetaceaCookie:d,responseNetaceaCaptchaCookie:g,body:i.body,eventId:y,mitataMaxAge:l,netaceaSettings:c??{}}}function ht(t){const e=Object.entries(t.getHeaders()),n={};for(const[t,i]of e)n[t]=i[0];return n}async function pt(t,e,n){const{method:i,path:r,headers:a,body:o,timeout:s}=n;let{host:c}=n;c.endsWith("/")&&(c=c.slice(0,-1));const u=c+r,{httpRequest:f}=t,h=await f(u,{body:o,headers:a,method:i,timeout:s??3e3});return{status:h.status,body:await h.text(),headers:ht(h)}}function lt(t){return Object.entries(t).filter((([,t])=>void 0!==t)).map((t=>t.join("="))).join("; ")}function dt(t){const e=t?.toLowerCase()??"text/html",n=e?.includes("text/html")||e?.includes("application/html"),i=e?.includes("application/json");return i&&!n?"application/json":"text/html"}function gt(t,e,n){let i;if(void 0===t||""===t)return"";if("string"==typeof t&&(i=JSON.parse(t)),!function(t){if(null==t)return!1;const e=t;return void 0!==e?.captchaSiteKey&&void 0!==e?.trackingId&&void 0!==e?.captchaURL}(i))throw new Error("Body is not a Mitigation Service JSON response!");const r=`${n}?trackingId=${i.trackingId}`,a=`https://${e}${r}`;return JSON.stringify({captchaRelativeURL:r,captchaAbsoluteURL:a})}async function yt(t,e,n,i){const r=void 0!==n.trackingId?`?trackingId=${n.trackingId}`:"";return await async function(t,e,n,i,r,a){const{userId:o,clientIP:s,host:c}=n,{mitigationServiceUrl:u,timeout:f}=e;if(void 0===u)throw new Error("Mitigation Service URL is not defined!");const h=function(t,e){const{apiKey:n,userId:i,userAgent:r,captchaCookie:a,clientIP:o,captchaSiteKey:s,captchaSecretKey:c,accept:u}=e,f={"X-Netacea-API-Key":n,"X-Netacea-Client-IP":o,"user-agent":r,cookie:lt({_mitatacaptcha:a})};void 0!==i&&(f["X-Netacea-UserId"]=i);void 0!==s&&void 0!==c&&(f["X-Netacea-Captcha-Site-Key"]=s,f["X-Netacea-Captcha-Secret-Key"]=c);t.captchaContentNegotiation&&void 0!==t.netaceaCaptchaPath&&(f["X-Netacea-Captcha-Content-Type"]=dt(u));return f}(e,n),p="application/json"===h["X-Netacea-Captcha-Content-Type"],l={host:u,path:i,headers:h,method:r,timeout:f},d=await pt(t,0,l);p&&void 0!==e.netaceaCaptchaPath&&void 0!==c&&(d.body=gt(d.body,c,e.netaceaCaptchaPath));return await ft(t,e,a,d,o,s)}(t,e,n,`/captcha${r}`,"GET",i)}async function mt(t,e,n,i){n=wt(n);const r=`${i}=`;for(const o of n){const n=o.split(";")[0].trimStart();if(n.startsWith(r)){const o=n.slice(r.length);if(e.encryptedCookieNames.includes(i)&&(5===(a=o).split(".").length&&a.includes("..")))try{return await(t.cachedEncryption?.decrypt(o))}catch(t){return}return o}}var a}function wt(t){return null==t?[]:"string"==typeof t?t.split(";"):t}function St(t,e,n=""){const i=t.getHeader(e)?.[0]?.trim();return i??n}function Et(t,e,n=""){return t.getVariable(e)??n}async function It(t,e,n){return C(t)?function(t,e){if(void 0===t||""===t)return k();const n=N(t);if(void 0!==n){const t=Math.floor(Date.now()/1e3),i=n.issueTimestamp+n.gracePeriod<t,r=e===n.clientIP;return{mitata:n,requiresReissue:i||!r,isExpired:i,shouldExpire:[1,3,5].includes(n.captcha),isSameIP:r,isPrimaryHashValid:!0,match:n.match,mitigate:n.mitigate,captcha:n.captcha,issueReason:n.issueReason}}return k()}(t,e):await async function(t,e,n){if("string"!=typeof t||""===t)return k();const i=v(t);if(void 0!==i){const t=Math.floor(Date.now()/1e3),r=parseInt(i.expiry)<t,a=[1,3,5].includes(i.captcha),o=e+"|"+i.expiry,s=await ot(i.ipHash,o,n),c=[i.expiry,i.userId,i.ipHash,i.mitigationType].join(nt);return{mitata:i,requiresReissue:r||!s,isExpired:r,shouldExpire:a,isSameIP:s,isPrimaryHashValid:await ot(i.signature,c,n),match:i.match,mitigate:i.mitigate,captcha:i.captcha}}return k()}(t,e,n??"")}function bt(t,e){const n=t.split("&");for(const t of n){const[n,i]=t.split("=");if(n===e)return i}}function vt(t,e,n){const{netaceaBlockedResponseStatus:i,netaceaCaptchaResponseStatus:r}=t;let a;return a=Number("block"===e?i:r),(o=a)>=200&&o<600&&!Number.isNaN(o)&&3===o.toString().length?a:n;var o}async function Ct(t,e,{clientIP:n,userId:i,match:a,mitigate:o,captcha:c,netaceaSettings:u,maxAge:f=86400,expiry:h,issueReason:p}){const l=e.netaceaCookieExpirySeconds,d=[1,3,5].includes(c)?-l:l,g=h??Math.floor(Date.now()/1e3)+d,y=e.ingestType!==r.HTTP&&void 0!==t.cachedEncryption;if(!y&&void 0===e.secretKey)throw new Error("Cannot build cookie without secret key.");let S;if(y)E={clientIP:n,userId:i??"",match:a,mitigate:o,captcha:c,gracePeriod:d,cookieId:rt(),issueTimestamp:Math.floor(Date.now()/1e3),issueReason:p??s.NO_SESSION,checkAllPostRequests:u.checkAllPostRequests},S=Object.entries(E).filter((([t,e])=>void 0!==e)).map((([t,e])=>t in w?`${w[t]}=${encodeURIComponent(e)}`:`${m[t]}=${encodeURIComponent(e)}`)).join("&");else{const t=[a,o,c].join("");S=await async function(t,e,n,i,r="000"){void 0===e&&(e=rt());const a=[n,e,await at(t+"|"+String(n),i),r].join(nt);return`${await at(a,i)}${nt}${a}`}(n,i,g,e.secretKey,t)}var E;return await ct(t,e,e.netaceaCookieName,S,f)}function At(t,e){const n=e.getVariable(t.protoMitataVariableName);if(void 0!==n&&""!==n)try{return JSON.parse(n)}catch{return}}const{extractAndRemoveCookieAttr:Nt,removeDuplicateAttrs:kt}=_.cookieAttributes;async function Tt(t,e,n,i,r){const a=H(n.path,n.method)?function(t,e,n){const i=(t,n)=>Number(e.getHeader(t)?.[0]??n),r=t.getHeader("x-netacea-client-ip")?.[0];if(null==r||""===r)throw new Error("Cannot build proto-mitata without Client IP");const a=t.getHeader("x-netacea-userid")?.[0]??"";let o={};const c=t.getHeader("x-netacea-settings")?.[0];void 0!==c&&(o=JSON.parse(c));let u=s.CAPTCHA_POST;return void 0!==n.netaceaCookieRequiredReissueReason&&(u=n.netaceaCookieRequiredReissueReason),{clientIP:r,userId:""!==a?a:rt(),match:i("x-netacea-match",0),mitigate:i("x-netacea-mitigate",0),captcha:i("x-netacea-captcha",0),mitataMaxAge:i("x-netacea-mitata-expiry",86400),netaceaSettings:o,issueReason:u}}(n,i,r):At(e,n);if(void 0!==a){let n,{netaceaCookieAttributes:r}=e;if(""!==r){const{extractedAttribute:t,cookieAttributes:e}=Nt(r,"Max-Age");n=t,r=e}const o=await Ct(t,e,{clientIP:a.clientIP,userId:a.userId,match:a.match,mitigate:a.mitigate,captcha:a.captcha,maxAge:void 0!==n?parseInt(n,10):a.mitataMaxAge,netaceaSettings:a.netaceaSettings,issueReason:a.issueReason??s.NO_SESSION}),c=""!==r?kt(`${o}; ${r}`,!0):o;i.addHeader("set-cookie",c)}}const{extractAndRemoveCookieAttr:Rt,removeDuplicateAttrs:_t}=_.cookieAttributes;async function Bt(t,e,n,i,r){const a=i.getHeader("set-cookie")??void 0;if(i.removeHeader("set-cookie"),200===i.status){await async function(t,e,n,i){const r=i?.find((t=>t.startsWith("_mitatacaptcha=")));if(void 0!==r){const i=r.slice(r.indexOf("=")+1);let[a,...o]=i.split(/; ?/);void 0!==t.cachedEncryption&&(a=await t.cachedEncryption.encrypt(a));let s=[`${e.netaceaCaptchaCookieName}=${a}`,...o].join("; ");if(""!==e.netaceaCaptchaCookieAttributes){const{extractedAttribute:t,cookieAttributes:n}=Rt(_t(e.netaceaCaptchaCookieAttributes,!0),"Max-Age"),{extractedAttribute:i}=Rt(s,"Max-Age");void 0!==t&&void 0!==i?(s=s.replace(`Max-Age=${i}`,`Max-Age=${t}`),s=_t(`${s}; ${n}`,!0)):s=_t(`${s}; ${n}`,!0)}n.addHeader("set-cookie",s)}}(t,e,i,a),await Tt(t,e,n,i,r);const o=String(d.captchaPass),s=f.captcha,c=i.getHeader(s)?.[0];c!==o&&(i.status=403)}}function xt(t){return F.Buffer.from(t.replace(/-/g,"+").replace(/_/g,"/"),"base64")}function Ot(t,e){if("string"==typeof t&&void 0===e)throw new Error("Encoding should be provided to convert strings.");return function(t){return t.replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}(("string"==typeof t?F.Buffer.from(t,e):F.Buffer.from(t)).toString("base64"))}const Ut=128,Pt=Ot(JSON.stringify({alg:"dir",enc:"A128CBC-HS256"}),"utf-8"),Mt=(new D).encode(Pt);async function $t(t){return await K.subtle.importKey("raw",t,{name:"AES-CBC"},!1,["encrypt","decrypt"])}async function Lt({plaintext:t,secret:e,ivBase64Url:n}){const i=xt(e),r=i.subarray(Ut>>3),a=i.subarray(0,Ut>>3),o=await $t(r),s=void 0!==n?xt(n):function(){const t=new Uint8Array(16);return K.getRandomValues(t),t}(),c=(new D).encode(t),u=await async function(t,e,n){const i=await K.subtle.encrypt({name:"AES-CBC",length:Ut,iv:t},e,n);return new Uint8Array(i)}(s,o,c),f=new Uint8Array(s),h=Dt(Mt,f,u),p=await Kt(h,a);return[Pt,"",Ot(s),Ot(u),Ot(p)].join(".")}async function Ht({jwe:t,secret:e}){const n=xt(e),i=n.subarray(Ut>>3),r=n.subarray(0,Ut>>3),{header:a,iv:o,payload:s,mac:c}=function(t){const e=t.split(".");if(5!==e.length)throw new Error(`JWE should have 5 parts, got ${e.length}`);const[n,i,r,a,o]=e;return{header:n,key:i,iv:r,payload:a,mac:o}}(t);if(a!==Pt)throw new Error("Incorrect JWE header");const u=xt(o),f=new Uint8Array(xt(s)),h=Dt((new D).encode(a),u,f);if(Ot(await Kt(h,r))!==c)throw new Error("MAC does not match JWE data");const p=await $t(i),l=await async function(t,e,n){const i=await K.subtle.decrypt({name:"AES-CBC",length:Ut,iv:t},e,n);return new Uint8Array(i)}(u,p,f);return(new j).decode(l)}async function Kt(t,e){return(await q(t,e)).slice(0,Ut>>3)}function Dt(t,e,n){return function(t){const e=t.reduce(((t,{length:e})=>t+e),0),n=new Uint8Array(e);let i=0;return t.forEach((t=>{n.set(t,i),i+=t.length})),n}([t,e,n,qt(t.length<<3)])}const jt=2**32;function Vt(t,e,n){if(e<0||e>=jt)throw new RangeError(`value must be >= 0 and <= ${jt-1}. Received ${e}`);t.set([e>>>24,e>>>16,e>>>8,255&e],n)}function qt(t){const e=Math.floor(t/jt),n=t%jt,i=new Uint8Array(8);return Vt(i,e,0),Vt(i,n,4),i}class Gt{constructor(t){this.maxSize=t,this.cachedItems=[]}getCacheSize(){return this.cachedItems.length}delete(t){this.cachedItems=this.cachedItems.filter((e=>e.key!==t))}add(t,e){this.delete(t),this.cachedItems.push({key:t,value:e}),this.cachedItems.length>this.maxSize&&this.cachedItems.shift()}get(t){return this.cachedItems.find((e=>e.key===t))}}class Ft{constructor(t){this.encryptionKey=t.encryptionKey,this.cache=new Gt(t.cacheMaxSize??5)}getCacheSize(){return this.cache.getCacheSize()}async memoizeFn(t,e){const n=this.cache.get(t);if(void 0!==n)return n.value;const i=await e();return this.cache.add(t,i),i}async encrypt(t){const e=`encrypt::${t}`;return await this.memoizeFn(e,(async()=>await Lt({plaintext:t,secret:this.encryptionKey})))}async decrypt(t){const e=`encrypt::${t}`;return await this.memoizeFn(e,(async()=>await Ht({jwe:t,secret:this.encryptionKey})))}}function Xt(t){return C(t)?N(t):v(t)}function zt(t,e){return e.bytesSent=""===e.bytesSent?"0":e.bytesSent,t.logVersion===a.V2?function({ip:t,userAgent:e,status:n,method:i,path:r,protocol:a,referer:o,bytesSent:s,requestTime:u,mitataCookie:f,sessionStatus:h,integrationType:p,integrationVersion:l}){const d=new Date;"/"!==r[0]&&(r=`/${r}`);let g;const y=r.split("?");y.length>1&&(g=`?${y[1]}`);const m=y[0],w=Xt(f)?.userId;return{status:n,method:i,bytes_sent:c(s),referrer:""===o?void 0:o,request:`${i} ${m}${g??""} ${a}`,request_time:c(u),integration_type:p,integration_version:l,client:t,user_agent:e,bc_type:""===h?void 0:h,hour:d.getUTCHours(),minute:d.getUTCMinutes(),"@timestamp":d.toISOString().replace("Z","+00:00"),path:m,protocol:a,query:g,user_id:w}}(e):function({ip:t,userAgent:e,status:n,method:i,path:r,protocol:a,referer:o,bytesSent:s,requestTime:c,mitataCookie:u,sessionStatus:f,integrationType:h,integrationVersion:p}){const l=(new Date).toUTCString();return{Request:`${i} ${r} ${a}`,TimeLocal:l,RealIp:t,UserAgent:e,Status:n,RequestTime:c?.toString(),BytesSent:s?.toString(),Referer:""===o?"-":o,NetaceaUserIdCookie:u??"",NetaceaMitigationApplied:f??"",IntegrationType:h??"",IntegrationVersion:p??""}}(e)}async function Jt(t,e,n){const i=zt(e,n);if(e.ingestType!==r.KINESIS){const n={"X-Netacea-API-Key":e.apiKey??"","content-type":"application/json"},r=await async function(t,e,n,i){return await pt(t,0,{host:e.ingestServiceUrl,method:"POST",path:"/",headers:n,body:JSON.stringify(i),timeout:e.timeout})}(t,e,n,i);if(200!==r.status)throw ut.fromResponse(r)}}function Wt(t,e,n,i,r){const a="unknown";r||(2===i?i=4:3===i&&(i=5));let s=h[e]??a+"_";s+=p[n]??a;let c=g[n];if(0!==i){s+=","+(l[i]??a);const t=y[i];void 0!==t&&(c=t)}return t.mitigationType===o.INJECT&&(c=u.none),{sessionStatus:s,mitigation:c,parts:{match:e,mitigate:n,captcha:i}}}var Yt="@netacea/akamai",Zt="6.1.20";async function Qt(t,e,n,i){e.ingestType===r.HTTP&&await async function(t,e,n,i){const r=i.getHeader("set-cookie")??[],a=0!==r.length?r?.join("; "):St(n,"cookie");let o=await mt(t,e,a,e.netaceaCookieName)??"";if(void 0===o||""===o){const i=St(n,"cookie");o=await mt(t,e,i,e.netaceaCookieName)??""}const{match:s,mitigate:c,captcha:u}=Xt(o)??{match:0,mitigate:0,captcha:0},{sessionStatus:f}=Wt(e,s,c,u,H(n.path,n.method));try{Jt(t,e,{bytesSent:St(i,"content-length","0"),ip:Et(n,"PMUSER_CLIENT_IP",""),method:n.method,path:n.url,protocol:St(n,"protocol","HTTP/1.1"),referer:St(n,"referer",""),requestTime:"0",sessionStatus:f,status:String(i.status),userAgent:St(n,"user-agent","-"),mitataCookie:o,integrationType:Yt.replace("@netacea/",""),integrationVersion:Zt})}catch(e){const{logger:n}=t;n.log("Error in callIngest():",e)}}(t,e,n,i)}async function te(t){const e=function(t){return Object.keys(t).filter((t=>{const e=t.toLowerCase();return"cookie"!==e&&"referer"!==e&&!e.startsWith("x-netacea-")})).join(",")}(t);return await ee(e)}async function ee(t){let e="";try{e=await async function(t,e){const n=(new D).encode(e),i=await K.subtle.digest(t,n);return Array.from(new Uint8Array(i)).map((t=>t.toString(16).padStart(2,"0"))).join("")}("SHA-256",t)}catch(t){e=""}return e}async function ne(t){const e=t.getHeaders(),n=await te(e),i=function(t,e){const n=t.getHeader(e);return null==n?[]:n}(t,"cookie").map((t=>t.split(/; ?/).map((t=>t.split("=")[0])))).flat(),r=await async function(t){const e=t.join(",");return await ee(e)}(i);return[""===n?n:`h_${n.substring(1,15)}`,""===r?r:`c_${r.substring(1,15)}`]}const ie={mitigationStatusCode:void 0,sessionReason:void 0,sessionStartTime:void 0};async function re(t,e,n,i){try{const t=At(e,n),r=t?.userId??i?.userId??"",a=function(t,e,n,i){const r=i?.match??n?.match??0,a=i?.mitigate??n?.mitigate??0,o=i?.captcha??n?.captcha??0,{sessionStatus:s}=Wt(t,r,a,o,H(e.path,e.method));return s}(e,n,i,t),[o,s]=await ne(n),{mitigationType:c}=e,u=[r,a,Zt,o,s,ie.sessionReason??"",ie.sessionStartTime??"",c,ie.mitigationStatusCode??""].join(";");n.setVariable("PMUSER_NETACEA_DS2_CUSTOM_FIELD",u),function(){for(const t in ie)ie[t]=void 0}()}catch(e){const{logger:n}=t;n.log(`Error setting PMUSER_NETACEA_DS2_CUSTOM_FIELD ${e}`)}}function ae(t){for(const e in ie)void 0!==t[e]&&(ie[e]=t[e])}function oe(t,e){ae({sessionReason:t,sessionStartTime:e??Math.floor(Date.now()/1e3)})}function se(t){t.route({origin:"mitigations"})}var ce;function ue(t){const e={};if(A(t)){const{netaceaSettingsMap:n}=E;for(const i in n){const r=t[n[i]];void 0!==r&&(e[i]=r)}}return e}async function fe(t,e,n,i,r,a){const o={"X-Netacea-API-Key":e.apiKey,"X-Netacea-Client-IP":n.clientIp,"user-agent":n.userAgent,cookie:lt({_mitatacaptcha:n.netaceaSession.captchaToken})};void 0!==n.netaceaSession.userId&&(o["X-Netacea-UserId"]=n.netaceaSession.userId),void 0!==e.captchaSiteKey&&void 0!==e.captchaSecretKey&&(o["X-Netacea-Captcha-Site-Key"]=e.captchaSiteKey,o["X-Netacea-Captcha-Secret-Key"]=e.captchaSecretKey),e.captchaContentNegotiation&&void 0!==e.netaceaCaptchaPath&&(o["X-Netacea-Captcha-Content-Type"]=dt(i));const s=function(t){let e="";const n=[];for(const e in S)void 0===t[e]&&n.push(e);n.length>0&&(e=`?getSettings=${n.join(",")}`);return e}(a),c="application/json"===o["X-Netacea-Captcha-Content-Type"],u=await pt(t,0,{host:e.mitigationServiceUrl,path:"/"+s,headers:o,method:"GET",timeout:e.timeout});ae({mitigationStatusCode:u.status}),c&&void 0!==e.netaceaCaptchaPath&&(u.body=gt(u.body,r,e.netaceaCaptchaPath));let f={};return void 0!==u.headers["x-netacea-settings"]&&(f=function(t){const e={},{netaceaSettingsMap:n}=E;return decodeURIComponent(t).split("&").map((t=>t.split("="))).forEach((([t,i])=>{if(void 0!==i&&t in n){const r=parseInt(i,10);e[n[t]]=isNaN(r)?void 0:r}})),Object.entries(e).reduce(((t,[e,n])=>(void 0!==n&&(t[e]=n),t)),{})}(u.headers["x-netacea-settings"])),await ft(t,e,void 0,u,n.netaceaSession.userId,n.clientIp,n.netaceaCookieRequiredReissueReason,{...a,...f})}!function(t){t[t.NO_SESSION=0]="NO_SESSION",t[t.NEW_SESSION=1]="NEW_SESSION",t[t.EXISTING_SESSION=2]="EXISTING_SESSION",t[t.RENEW_SESSION=3]="RENEW_SESSION"}(ce||(ce={}));const{mitigationTypes:he}=E,pe=[he.block,he.captcha,he.captchaPass];async function le(t,e,n,i,r){const{netaceaCookieInfo:a,clientIp:o,netaceaSession:s}=n;let c={status:-1,match:(u=a).match,mitigate:u.mitigate,captcha:u.captcha,mitataMaxAge:86400,netaceaSettings:{},responseNetaceaCookie:void 0,responseNetaceaCaptchaCookie:void 0};var u,f;if(void 0===e.secretKey)throw new Error("Secret key is required to mitigate");if(void 0!==n.netaceaCookieRequiredReissueReason){const u=ue(a.mitata);c=await fe(t,e,n,i,r,u),c.responseNetaceaCookie=await st(o,s.userId,c.match,c.mitigate,c.captcha,n.netaceaCookieRequiredReissueReason,c.netaceaSettings,c.mitataMaxAge);oe(void 0===a.mitata?ce.NEW_SESSION:ce.RENEW_SESSION)}else{const t=a.mitata,e=A(f=t)?f.issueTimestamp:Number.parseInt(f.expiry,10)-60;oe(ce.EXISTING_SESSION,e)}const h=Wt(e,c.match,c.mitigate,c.captcha,!1);return{body:c.body,apiCallStatus:c.status,responseNetaceaCookie:c?.responseNetaceaCookie,responseNetaceaCaptchaCookie:void 0,sessionStatus:h.sessionStatus,mitigation:h.mitigation,mitigated:pe.includes(h.mitigation),bestMitigation:h,eventId:c.eventId}}function de(t){return{apiCallStatus:0,responseNetaceaCookie:t,responseNetaceaCaptchaCookie:void 0,sessionStatus:"",mitigation:"000",mitigated:!1,bestMitigation:{sessionStatus:"",mitigation:"",parts:{match:0,mitigate:0,captcha:0}},eventId:void 0}}async function ge(t){return{sessionStatus:"",response:{headers:{},body:void 0,status:0,statusText:"",mitigation:"000"},bestAction:de(await st(I,t,0,0,0,s.NO_SESSION,void 0,86400))}}async function ye(t,e,n,i){try{switch(e.mitigationType){case o.MITIGATE:return await async function(t,e,n,i){const r=St(n,"accept","text/html"),a=await le(t,e,i,r,n.host),o={};let s,c;return a.mitigated&&(c=403,"captcha"===a.mitigation?(o["content-type"]="text/html; charset=UTF-8",s=a.body):s="Forbidden"),{response:{body:s,status:c,statusText:c?.toString(),headers:o,mitigation:a.mitigation},sessionStatus:a.sessionStatus,bestAction:a}}(t,e,n,i);case o.INJECT:return await async function(t,e,n,i){const r=St(n,"accept","text/html"),a=await le(t,e,i,r,n.host),o={"x-netacea-match":a.bestMitigation.parts.match.toString(),"x-netacea-mitigate":a.bestMitigation.parts.mitigate.toString(),"x-netacea-captcha":a.bestMitigation.parts.captcha.toString()};return void 0!==a.eventId&&(o["x-netacea-event-id"]=a.eventId),{injectHeaders:o,sessionStatus:a.sessionStatus,response:{headers:{}},bestAction:a}}(t,e,n,i);case o.INGEST:return await async function(t,e,n){if(void 0===e.secretKey)throw new Error("Secret key is required for ingest");const i=St(n,"cookie"),r=await mt(t,e,i,e.netaceaCookieName),a=await It(r,I,e.secretKey);return a.isPrimaryHashValid?a.requiresReissue?await ge(a.mitata?.userId):{sessionStatus:"",response:void 0,bestAction:de()}:await ge(void 0)}(t,e,n);default:throw new Error(`Netacea Error: Mitigation type ${e.mitigationType} not recognised`)}}catch(t){return console.error("Netacea FAILOPEN Error:",t),await Promise.resolve({injectHeaders:{"x-netacea-captcha":"0","x-netacea-match":"0","x-netacea-mitigate":"0"},sessionStatus:"",response:void 0,bestAction:void 0})}}async function me(t,e,n,i){let a={};if(void 0!==n?.bestAction?.responseNetaceaCookie){const t=n?.bestAction?.responseNetaceaCookie;void 0!==t&&i.setVariable(e.protoMitataVariableName,JSON.stringify(t))}if(e.mitigationType===o.INJECT){a=n.injectHeaders??{}}let s={};e.ingestType===r.ORIGIN&&(s=await async function(t,e,n){const i=St(n,"cookie"),r=Xt(await mt(t,e,i,e.netaceaCookieName)??""),a=At(e,n),{match:o,mitigate:s,captcha:c,userId:u}=a??r??{match:0,mitigate:0,captcha:0,userId:""},{sessionStatus:f}=Wt(e,o,s,c,H(n.path,n.method));return{"X-Netacea-UserId":u,"X-Netacea-BC-Type":f,"X-Netacea-Integration-Type":Yt.replace("@netacea/",""),"X-Netacea-Integration-Version":Zt}}(t,e,i));for(const[t,e]of Object.entries({...a,...s}))i.addHeader(t,e)}async function we(t,e,n){const i=Et(n,"PMUSER_CLIENT_IP"),r=St(n,"user-agent"),a=St(n,"cookie"),o=await mt(t,e,a,e.netaceaCookieName),c=await It(o,i,e.secretKey),u=function(t,e,n,i,r){if(void 0!==i&&!r.isPrimaryHashValid)return s.INVALID_SESSION;if(void 0===i)return function(t,e){for(const n of wt(t))if(n.startsWith(`${e}=`))return!0;return!1}(e,n)?s.INVALID_SESSION:s.NO_SESSION;if(r.isExpired)return s.EXPIRED_SESSION;if(!r.isSameIP)return s.IP_CHANGE;const a="post"===t.method.toLowerCase(),o=ue(r.mitata);if(a&&1===o.checkAllPostRequests)return s.FORCED_REVALIDATION}(n,a,e.netaceaCookieName,o,c),f=await mt(t,e,a,e.netaceaCaptchaCookieName);return{clientIp:i,userAgent:r,method:n.method,netaceaCookieInfo:c,netaceaCookieRequiredReissueReason:u,netaceaSession:{userId:c.mitata?.userId,captchaToken:f,match:c.match,mitigate:c.mitigate,captcha:c.captcha}}}class Se{constructor(n){this.configLoader=new U(n),this.defaultDeps={httpRequest:t,logger:e,cachedEncryption:void 0}}async requestHandler(t){const e=this.configLoader.loadConfig(t);void 0!==e.cookieEncryptionKey&&(this.defaultDeps.cachedEncryption=new Ft({encryptionKey:e.cookieEncryptionKey}));const n=await we(this.defaultDeps,e,t);var i,r,a;i=t.path,r=t.method,void 0!==(a=e.netaceaCaptchaPath)&&a.length>0&&i===a&&"get"===r.toLowerCase()?await async function(t,e,n,i){const r=St(n,"cookie"),a=await mt(t,e,r,e.netaceaCookieName),o=await mt(t,e,r,e.netaceaCaptchaCookieName),c=Et(n,"PMUSER_CLIENT_IP"),u=St(n,"user-agent"),f=await It(a,c,e.secretKey),h=vt(e,"captcha",403),p=await yt(t,e,{apiKey:e.apiKey??"",clientIP:c,userAgent:u,userId:f.mitata?.userId,captchaCookie:o,trackingId:bt(n.query,"trackingId"),captchaSiteKey:e.captchaSiteKey,captchaSecretKey:e.captchaSecretKey,accept:St(n,"accept","text/html"),host:n.host},{match:0,mitigate:0,captcha:1}),l=p.responseNetaceaCookie;void 0!==l&&(void 0===i.netaceaCookieRequiredReissueReason?l.issueReason=s.CAPTCHA_GET:l.issueReason=i.netaceaCookieRequiredReissueReason,n.setVariable(e.protoMitataVariableName,JSON.stringify(l)));const d={};void 0!==e.netaceaCaptchaHeaderKey&&(d[e.netaceaCaptchaHeaderKey]=e.netaceaCaptchaHeaderValue),n.respondWith(h,d,p.body)}(this.defaultDeps,e,t,n):H(t.path,t.method)?await async function(t,e,n){const i=n.getVariable("PMUSER_CLIENT_IP")??"";n.setHeader("x-netacea-api-key",e.apiKey),n.setHeader("x-netacea-client-ip",i);const r=St(n,"cookie"),a=await mt(t,e,r,e.netaceaCookieName)??"";let o="";const s=await It(a,i,e.secretKey??"");if(A(s.mitata)){const t={checkAllPostRequests:s.mitata.checkAllPostRequests};n.setHeader("x-netacea-settings",JSON.stringify(t))}s.isPrimaryHashValid&&(o=s.mitata?.userId??""),n.setHeader("x-netacea-userid",o),se(n)}(this.defaultDeps,e,t):$(t.path,t.method,e.useCaptchaRelAssets)?se(t):await async function(t,e,n,i){const r=await ye(t,e,n,i);if(void 0===r)throw new Error('Mitigation Service response "mitResult" was undefined.');const a=r.response?.mitigation;if("block"===a||"captcha"===a){const t=vt(e,a,r.response?.status??403),i={},o=r.bestAction?.responseNetaceaCookie;void 0!==o&&n.setVariable(e.protoMitataVariableName,JSON.stringify(o));const s=r.response?.body??"";(function(t){return t.includes("captchaRelativeURL")&&t.includes("captchaAbsoluteURL")})(s)&&(i["content-type"]="application/json"),"captcha"===a&&void 0!==e.netaceaCaptchaHeaderKey&&(i[e.netaceaCaptchaHeaderKey]=e.netaceaCaptchaHeaderValue),n.respondWith(t,i,s)}else await me(t,e,r,n)}(this.defaultDeps,e,t,n),await re(this.defaultDeps,e,t,n.netaceaSession)}async responseHandler(t,e){const n=this.configLoader.loadConfig(t);void 0!==n.cookieEncryptionKey&&(this.defaultDeps.cachedEncryption=new Ft({encryptionKey:n.cookieEncryptionKey}));const i=await we(this.defaultDeps,n,t);if(H(t.path,t.method))await Bt(this.defaultDeps,n,t,e,i);else if(L(t.path,t.method)&&!$(t.path,t.method,n.useCaptchaRelAssets)){i.netaceaCookieRequiredReissueReason!==s.NO_SESSION&&await Tt(this.defaultDeps,n,t,e,i)}else $(t.path,t.method,n.useCaptchaRelAssets)||await Tt(this.defaultDeps,n,t,e,i);!function(t){t.removeHeader("x-netacea-match"),t.removeHeader("x-netacea-mitigate"),t.removeHeader("x-netacea-captcha"),t.removeHeader("x-netacea-mitata-expiry"),t.removeHeader("x-netacea-mitata-value"),t.removeHeader("x-netacea-mitatacaptcha-expiry"),t.removeHeader("x-netacea-mitatacaptcha-value"),t.removeHeader("x-netacea-event-id"),t.removeHeader("x-netacea-settings")}(e),await Qt(this.defaultDeps,n,t,e)}}export{o as NetaceaMitigationType,Se as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netacea/akamai",
3
- "version": "6.1.18",
3
+ "version": "6.1.20",
4
4
  "description": "Netacea Akamai CDN Integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "license": "UNLICENSED",
22
22
  "dependencies": {
23
- "@netacea/netaceaintegrationbase": "^1.17.18",
23
+ "@netacea/netaceaintegrationbase": "^2.0.2",
24
24
  "buffer": "^6.0.3"
25
25
  },
26
26
  "files": [
@@ -73,5 +73,5 @@
73
73
  "tslib": "^2.0.3",
74
74
  "typescript": "^4.0.3"
75
75
  },
76
- "gitHead": "4ea0c67b78dd0b0e2d92358fe2be39ebfd7441e0"
76
+ "gitHead": "e143af04541aee913c1eb4fbc64f89b2154ca6be"
77
77
  }