@commercetools/sdk-middleware-auth
Advanced tools
Comparing version 6.1.4 to 6.2.0
@@ -149,2 +149,26 @@ 'use strict'; | ||
function defineError(statusCode, message) { | ||
var meta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
// eslint-disable-next-line no-multi-assign | ||
this.status = this.statusCode = this.code = statusCode; | ||
this.message = message; | ||
Object.assign(this, meta); | ||
this.name = this.constructor.name; // eslint-disable-next-line no-proto | ||
this.constructor.prototype.__proto__ = Error.prototype; | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor); | ||
} | ||
/* eslint-disable max-len, flowtype/require-parameter-type */ | ||
function NetworkError() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
defineError.call.apply(defineError, [this, 0 | ||
/* special code to indicate network errors */ | ||
].concat(args)); | ||
} | ||
function mergeAuthHeader(token, req) { | ||
@@ -163,2 +187,11 @@ return _objectSpread2(_objectSpread2({}, req), {}, { | ||
function calcDelayDuration(retryCount) { | ||
var retryDelay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 60000; | ||
var backoff = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; | ||
var maxDelay = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : Infinity; | ||
if (backoff) return retryCount !== 0 // do not increase if it's the first retry | ||
? Math.min(Math.round((Math.random() + 1) * retryDelay * Math.pow(2, retryCount)), maxDelay) : retryDelay; | ||
return retryDelay; | ||
} | ||
function executeRequest(_ref) { | ||
@@ -169,2 +202,3 @@ var fetcher = _ref.fetcher, | ||
body = _ref.body, | ||
request = _ref.request, | ||
tokenCache = _ref.tokenCache, | ||
@@ -174,65 +208,136 @@ requestState = _ref.requestState, | ||
response = _ref.response, | ||
tokenCacheKey = _ref.tokenCacheKey; | ||
fetcher(url, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: "Basic ".concat(basicAuth), | ||
'Content-Length': Buffer.byteLength(body).toString(), | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: body | ||
}).then(function (res) { | ||
if (res.ok) return res.json().then(function (_ref2) { | ||
var token = _ref2.access_token, | ||
expiresIn = _ref2.expires_in, | ||
refreshToken = _ref2.refresh_token; | ||
var expirationTime = calculateExpirationTime(expiresIn); // Cache new token | ||
tokenCacheKey = _ref.tokenCacheKey, | ||
timeout = _ref.timeout, | ||
getAbortController = _ref.getAbortController, | ||
_ref$retryConfig = _ref.retryConfig; | ||
_ref$retryConfig = _ref$retryConfig === void 0 ? {} : _ref$retryConfig; | ||
var _ref$retryConfig$retr = _ref$retryConfig.retryDelay, | ||
retryDelay = _ref$retryConfig$retr === void 0 ? 300 : _ref$retryConfig$retr, | ||
_ref$retryConfig$maxR = _ref$retryConfig.maxRetries, | ||
maxRetries = _ref$retryConfig$maxR === void 0 ? 10 : _ref$retryConfig$maxR, | ||
_ref$retryConfig$back = _ref$retryConfig.backoff, | ||
backoff = _ref$retryConfig$back === void 0 ? true : _ref$retryConfig$back, | ||
_ref$retryConfig$maxD = _ref$retryConfig.maxDelay, | ||
maxDelay = _ref$retryConfig$maxD === void 0 ? Infinity : _ref$retryConfig$maxD; | ||
tokenCache.set({ | ||
token: token, | ||
expirationTime: expirationTime, | ||
refreshToken: refreshToken | ||
}, tokenCacheKey); // Dispatch all pending requests | ||
// if timeout is configured and no instance of AbortController is passed then throw | ||
if (timeout && !getAbortController && typeof AbortController === 'undefined') { | ||
throw new Error('`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.'); | ||
} // ensure that the passed value of the timeout is of type number | ||
requestState.set(false); // Freeze and copy pending queue, reset original one for accepting | ||
// new pending tasks | ||
var executionQueue = pendingTasks.slice(); // eslint-disable-next-line no-param-reassign | ||
if (timeout && typeof timeout !== 'number') throw new Error('The passed value for timeout is not a number, please provide a timeout of type number.'); | ||
var retryCount = 0; | ||
pendingTasks = []; | ||
executionQueue.forEach(function (task) { | ||
// Assign the new token in the request header | ||
var requestWithAuth = mergeAuthHeader(token, task.request); // console.log('test', cache, pendingTasks) | ||
// Continue by calling the task's own next function | ||
function executeFetch() { | ||
var signal; | ||
var abortController; | ||
if (timeout || getAbortController) abortController = (getAbortController ? getAbortController() : null) || new AbortController(); | ||
task.next(requestWithAuth, task.response); | ||
}); | ||
}); // Handle error response | ||
if (abortController) { | ||
signal = abortController.signal; | ||
} | ||
return res.text().then(function (text) { | ||
var parsed; | ||
var timer; | ||
if (timeout) timer = setTimeout(function () { | ||
abortController.abort(); | ||
}, timeout); | ||
fetcher(url, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: "Basic ".concat(basicAuth), | ||
'Content-Length': Buffer.byteLength(body).toString(), | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: body, | ||
signal: signal | ||
}).then(function (res) { | ||
if (res.ok) return res.json().then(function (_ref2) { | ||
var token = _ref2.access_token, | ||
expiresIn = _ref2.expires_in, | ||
refreshToken = _ref2.refresh_token; | ||
var expirationTime = calculateExpirationTime(expiresIn); // Cache new token | ||
try { | ||
parsed = JSON.parse(text); | ||
} catch (error) { | ||
/* noop */ | ||
} | ||
tokenCache.set({ | ||
token: token, | ||
expirationTime: expirationTime, | ||
refreshToken: refreshToken | ||
}, tokenCacheKey); // Dispatch all pending requests | ||
var error = new Error(parsed ? parsed.message : text); | ||
if (parsed) error.body = parsed; // to notify that token is either fetched or failed | ||
requestState.set(false); // Freeze and copy pending queue, reset original one for accepting | ||
// new pending tasks | ||
var executionQueue = pendingTasks.slice(); // eslint-disable-next-line no-param-reassign | ||
pendingTasks = []; | ||
executionQueue.forEach(function (task) { | ||
// Assign the new token in the request header | ||
var requestWithAuth = mergeAuthHeader(token, task.request); // console.log('test', cache, pendingTasks) | ||
// Continue by calling the task's own next function | ||
task.next(requestWithAuth, task.response); | ||
}); | ||
}); // Handle error response | ||
return res.text().then(function (text) { | ||
var parsed; | ||
try { | ||
parsed = JSON.parse(text); | ||
} catch (error) { | ||
/* noop */ | ||
} | ||
var error = new Error(parsed ? parsed.message : text); | ||
if (parsed) error.body = parsed; // to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); // check that error message matches the pattern '...is suspended' | ||
if (error.message.includes('is suspended')) { | ||
// empty the tokenCache | ||
tokenCache.set(null); // retry | ||
if (retryCount < maxRetries) { | ||
setTimeout(executeFetch, calcDelayDuration(retryCount, retryDelay, maxRetries, backoff, maxDelay)); | ||
retryCount += 1; | ||
return; | ||
} // construct a suitable error message for the caller | ||
var errorResponse = { | ||
message: error.body.error, | ||
statusCode: error.body.statusCode, | ||
originalRequest: request, | ||
retryCount: retryCount | ||
}; | ||
response.reject(errorResponse); | ||
} | ||
response.reject(error); | ||
}); | ||
}).catch(function (error) { | ||
// to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); | ||
if (response && typeof response.reject === 'function') response.reject(error); | ||
requestState.set(false); | ||
response.reject(error); | ||
if (response && typeof response.reject === 'function' && (error === null || error === void 0 ? void 0 : error.type) === 'aborted') { | ||
var _error = new NetworkError(error.message, { | ||
type: error.type, | ||
request: request | ||
}); | ||
response.reject(_error); | ||
} | ||
}).finally(function () { | ||
clearTimeout(timer); | ||
}); | ||
}).catch(function (error) { | ||
// to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); | ||
if (response && typeof response.reject === 'function') response.reject(error); | ||
}); | ||
} | ||
executeFetch(); | ||
} | ||
@@ -250,3 +355,6 @@ | ||
tokenCacheKey = _ref3.tokenCacheKey, | ||
fetcher = _ref3.fetch; | ||
fetcher = _ref3.fetch, | ||
timeout = _ref3.timeout, | ||
getAbortController = _ref3.getAbortController, | ||
retryConfig = _ref3.retryConfig; | ||
if (!fetcher && typeof fetch === 'undefined') throw new Error('`fetch` is not available. Please pass in `fetch` as an option or have it globally available.'); | ||
@@ -296,6 +404,10 @@ if (!fetcher) // eslint-disable-next-line | ||
tokenCacheKey: tokenCacheKey, | ||
request: request, | ||
tokenCache: tokenCache, | ||
requestState: requestState, | ||
pendingTasks: pendingTasks, | ||
response: response | ||
response: response, | ||
timeout: timeout, | ||
getAbortController: getAbortController, | ||
retryConfig: retryConfig | ||
})); | ||
@@ -311,2 +423,3 @@ return; | ||
body: body, | ||
request: request, | ||
tokenCacheKey: tokenCacheKey, | ||
@@ -316,3 +429,6 @@ tokenCache: tokenCache, | ||
pendingTasks: pendingTasks, | ||
response: response | ||
response: response, | ||
timeout: timeout, | ||
getAbortController: getAbortController, | ||
retryConfig: retryConfig | ||
}); | ||
@@ -350,3 +466,3 @@ } | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -380,3 +496,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -409,3 +525,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -438,3 +554,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -441,0 +557,0 @@ response: response |
@@ -145,2 +145,26 @@ function _defineProperty(obj, key, value) { | ||
function defineError(statusCode, message) { | ||
var meta = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
// eslint-disable-next-line no-multi-assign | ||
this.status = this.statusCode = this.code = statusCode; | ||
this.message = message; | ||
Object.assign(this, meta); | ||
this.name = this.constructor.name; // eslint-disable-next-line no-proto | ||
this.constructor.prototype.__proto__ = Error.prototype; | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, this.constructor); | ||
} | ||
/* eslint-disable max-len, flowtype/require-parameter-type */ | ||
function NetworkError() { | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
defineError.call.apply(defineError, [this, 0 | ||
/* special code to indicate network errors */ | ||
].concat(args)); | ||
} | ||
function mergeAuthHeader(token, req) { | ||
@@ -159,2 +183,11 @@ return _objectSpread2(_objectSpread2({}, req), {}, { | ||
function calcDelayDuration(retryCount) { | ||
var retryDelay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 60000; | ||
var backoff = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; | ||
var maxDelay = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : Infinity; | ||
if (backoff) return retryCount !== 0 // do not increase if it's the first retry | ||
? Math.min(Math.round((Math.random() + 1) * retryDelay * Math.pow(2, retryCount)), maxDelay) : retryDelay; | ||
return retryDelay; | ||
} | ||
function executeRequest(_ref) { | ||
@@ -165,2 +198,3 @@ var fetcher = _ref.fetcher, | ||
body = _ref.body, | ||
request = _ref.request, | ||
tokenCache = _ref.tokenCache, | ||
@@ -170,65 +204,136 @@ requestState = _ref.requestState, | ||
response = _ref.response, | ||
tokenCacheKey = _ref.tokenCacheKey; | ||
fetcher(url, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: "Basic ".concat(basicAuth), | ||
'Content-Length': Buffer.byteLength(body).toString(), | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: body | ||
}).then(function (res) { | ||
if (res.ok) return res.json().then(function (_ref2) { | ||
var token = _ref2.access_token, | ||
expiresIn = _ref2.expires_in, | ||
refreshToken = _ref2.refresh_token; | ||
var expirationTime = calculateExpirationTime(expiresIn); // Cache new token | ||
tokenCacheKey = _ref.tokenCacheKey, | ||
timeout = _ref.timeout, | ||
getAbortController = _ref.getAbortController, | ||
_ref$retryConfig = _ref.retryConfig; | ||
_ref$retryConfig = _ref$retryConfig === void 0 ? {} : _ref$retryConfig; | ||
var _ref$retryConfig$retr = _ref$retryConfig.retryDelay, | ||
retryDelay = _ref$retryConfig$retr === void 0 ? 300 : _ref$retryConfig$retr, | ||
_ref$retryConfig$maxR = _ref$retryConfig.maxRetries, | ||
maxRetries = _ref$retryConfig$maxR === void 0 ? 10 : _ref$retryConfig$maxR, | ||
_ref$retryConfig$back = _ref$retryConfig.backoff, | ||
backoff = _ref$retryConfig$back === void 0 ? true : _ref$retryConfig$back, | ||
_ref$retryConfig$maxD = _ref$retryConfig.maxDelay, | ||
maxDelay = _ref$retryConfig$maxD === void 0 ? Infinity : _ref$retryConfig$maxD; | ||
tokenCache.set({ | ||
token: token, | ||
expirationTime: expirationTime, | ||
refreshToken: refreshToken | ||
}, tokenCacheKey); // Dispatch all pending requests | ||
// if timeout is configured and no instance of AbortController is passed then throw | ||
if (timeout && !getAbortController && typeof AbortController === 'undefined') { | ||
throw new Error('`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.'); | ||
} // ensure that the passed value of the timeout is of type number | ||
requestState.set(false); // Freeze and copy pending queue, reset original one for accepting | ||
// new pending tasks | ||
var executionQueue = pendingTasks.slice(); // eslint-disable-next-line no-param-reassign | ||
if (timeout && typeof timeout !== 'number') throw new Error('The passed value for timeout is not a number, please provide a timeout of type number.'); | ||
var retryCount = 0; | ||
pendingTasks = []; | ||
executionQueue.forEach(function (task) { | ||
// Assign the new token in the request header | ||
var requestWithAuth = mergeAuthHeader(token, task.request); // console.log('test', cache, pendingTasks) | ||
// Continue by calling the task's own next function | ||
function executeFetch() { | ||
var signal; | ||
var abortController; | ||
if (timeout || getAbortController) abortController = (getAbortController ? getAbortController() : null) || new AbortController(); | ||
task.next(requestWithAuth, task.response); | ||
}); | ||
}); // Handle error response | ||
if (abortController) { | ||
signal = abortController.signal; | ||
} | ||
return res.text().then(function (text) { | ||
var parsed; | ||
var timer; | ||
if (timeout) timer = setTimeout(function () { | ||
abortController.abort(); | ||
}, timeout); | ||
fetcher(url, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: "Basic ".concat(basicAuth), | ||
'Content-Length': Buffer.byteLength(body).toString(), | ||
'Content-Type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: body, | ||
signal: signal | ||
}).then(function (res) { | ||
if (res.ok) return res.json().then(function (_ref2) { | ||
var token = _ref2.access_token, | ||
expiresIn = _ref2.expires_in, | ||
refreshToken = _ref2.refresh_token; | ||
var expirationTime = calculateExpirationTime(expiresIn); // Cache new token | ||
try { | ||
parsed = JSON.parse(text); | ||
} catch (error) { | ||
/* noop */ | ||
} | ||
tokenCache.set({ | ||
token: token, | ||
expirationTime: expirationTime, | ||
refreshToken: refreshToken | ||
}, tokenCacheKey); // Dispatch all pending requests | ||
var error = new Error(parsed ? parsed.message : text); | ||
if (parsed) error.body = parsed; // to notify that token is either fetched or failed | ||
requestState.set(false); // Freeze and copy pending queue, reset original one for accepting | ||
// new pending tasks | ||
var executionQueue = pendingTasks.slice(); // eslint-disable-next-line no-param-reassign | ||
pendingTasks = []; | ||
executionQueue.forEach(function (task) { | ||
// Assign the new token in the request header | ||
var requestWithAuth = mergeAuthHeader(token, task.request); // console.log('test', cache, pendingTasks) | ||
// Continue by calling the task's own next function | ||
task.next(requestWithAuth, task.response); | ||
}); | ||
}); // Handle error response | ||
return res.text().then(function (text) { | ||
var parsed; | ||
try { | ||
parsed = JSON.parse(text); | ||
} catch (error) { | ||
/* noop */ | ||
} | ||
var error = new Error(parsed ? parsed.message : text); | ||
if (parsed) error.body = parsed; // to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); // check that error message matches the pattern '...is suspended' | ||
if (error.message.includes('is suspended')) { | ||
// empty the tokenCache | ||
tokenCache.set(null); // retry | ||
if (retryCount < maxRetries) { | ||
setTimeout(executeFetch, calcDelayDuration(retryCount, retryDelay, maxRetries, backoff, maxDelay)); | ||
retryCount += 1; | ||
return; | ||
} // construct a suitable error message for the caller | ||
var errorResponse = { | ||
message: error.body.error, | ||
statusCode: error.body.statusCode, | ||
originalRequest: request, | ||
retryCount: retryCount | ||
}; | ||
response.reject(errorResponse); | ||
} | ||
response.reject(error); | ||
}); | ||
}).catch(function (error) { | ||
// to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); | ||
if (response && typeof response.reject === 'function') response.reject(error); | ||
requestState.set(false); | ||
response.reject(error); | ||
if (response && typeof response.reject === 'function' && (error === null || error === void 0 ? void 0 : error.type) === 'aborted') { | ||
var _error = new NetworkError(error.message, { | ||
type: error.type, | ||
request: request | ||
}); | ||
response.reject(_error); | ||
} | ||
}).finally(function () { | ||
clearTimeout(timer); | ||
}); | ||
}).catch(function (error) { | ||
// to notify that token is either fetched or failed | ||
// in the below case token failed to be fetched | ||
// and reset requestState to false | ||
// so requestState could be shared between multi authMiddlewareBase functions | ||
requestState.set(false); | ||
if (response && typeof response.reject === 'function') response.reject(error); | ||
}); | ||
} | ||
executeFetch(); | ||
} | ||
@@ -246,3 +351,6 @@ | ||
tokenCacheKey = _ref3.tokenCacheKey, | ||
fetcher = _ref3.fetch; | ||
fetcher = _ref3.fetch, | ||
timeout = _ref3.timeout, | ||
getAbortController = _ref3.getAbortController, | ||
retryConfig = _ref3.retryConfig; | ||
if (!fetcher && typeof fetch === 'undefined') throw new Error('`fetch` is not available. Please pass in `fetch` as an option or have it globally available.'); | ||
@@ -292,6 +400,10 @@ if (!fetcher) // eslint-disable-next-line | ||
tokenCacheKey: tokenCacheKey, | ||
request: request, | ||
tokenCache: tokenCache, | ||
requestState: requestState, | ||
pendingTasks: pendingTasks, | ||
response: response | ||
response: response, | ||
timeout: timeout, | ||
getAbortController: getAbortController, | ||
retryConfig: retryConfig | ||
})); | ||
@@ -307,2 +419,3 @@ return; | ||
body: body, | ||
request: request, | ||
tokenCacheKey: tokenCacheKey, | ||
@@ -312,3 +425,6 @@ tokenCache: tokenCache, | ||
pendingTasks: pendingTasks, | ||
response: response | ||
response: response, | ||
timeout: timeout, | ||
getAbortController: getAbortController, | ||
retryConfig: retryConfig | ||
}); | ||
@@ -346,3 +462,3 @@ } | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -376,3 +492,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -405,3 +521,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -434,3 +550,3 @@ response: response | ||
var params = _objectSpread2(_objectSpread2({ | ||
var params = _objectSpread2(_objectSpread2(_objectSpread2({}, options), {}, { | ||
request: request, | ||
@@ -437,0 +553,0 @@ response: response |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).CommercetoolsSdkMiddlewareAuth={})}(this,function(t){"use strict";function e(e,t){var r,n=Object.keys(e);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(e),t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)),n}function g(o){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?e(Object(i),!0).forEach(function(t){var e,r,n;e=o,n=i[r=t],r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(o,Object.getOwnPropertyDescriptors(i)):e(Object(i)).forEach(function(t){Object.defineProperty(o,t,Object.getOwnPropertyDescriptor(i,t))})}return o}var r="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},u=[],h=[],f="undefined"!=typeof Uint8Array?Uint8Array:Array,c=!1;function p(){c=!0;for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e=0,r=t.length;e<r;++e)u[e]=t[e],h[t.charCodeAt(e)]=e;h["-".charCodeAt(0)]=62,h["_".charCodeAt(0)]=63}function l(t,e,r){for(var n,o,i=[],s=e;s<r;s+=3)n=(t[s]<<16)+(t[s+1]<<8)+t[s+2],i.push(u[(o=n)>>18&63]+u[o>>12&63]+u[o>>6&63]+u[63&o]);return i.join("")}function n(t){var e;c||p();for(var r=t.length,n=r%3,o="",i=[],s=0,a=r-n;s<a;s+=16383)i.push(l(t,s,a<s+16383?a:s+16383));return 1==n?(e=t[r-1],o+=u[e>>2],o+=u[e<<4&63],o+="=="):2==n&&(e=(t[r-2]<<8)+t[r-1],o+=u[e>>10],o+=u[e>>4&63],o+=u[e<<2&63],o+="="),i.push(o),i.join("")}function o(t,e,r,n,o){var i,s,a=8*o-n-1,u=(1<<a)-1,h=u>>1,f=-7,c=r?o-1:0,p=r?-1:1,l=t[e+c];for(c+=p,i=l&(1<<-f)-1,l>>=-f,f+=a;0<f;i=256*i+t[e+c],c+=p,f-=8);for(s=i&(1<<-f)-1,i>>=-f,f+=n;0<f;s=256*s+t[e+c],c+=p,f-=8);if(0===i)i=1-h;else{if(i===u)return s?NaN:1/0*(l?-1:1);s+=Math.pow(2,n),i-=h}return(l?-1:1)*s*Math.pow(2,i-n)}function i(t,e,r,n,o,i){var s,a,u,h=8*i-o-1,f=(1<<h)-1,c=f>>1,p=23===o?Math.pow(2,-24)-Math.pow(2,-77):0,l=n?0:i-1,g=n?1:-1,d=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(a=isNaN(e)?1:0,s=f):(s=Math.floor(Math.log(e)/Math.LN2),e*(u=Math.pow(2,-s))<1&&(s--,u*=2),2<=(e+=1<=s+c?p/u:p*Math.pow(2,1-c))*u&&(s++,u/=2),f<=s+c?(a=0,s=f):1<=s+c?(a=(e*u-1)*Math.pow(2,o),s+=c):(a=e*Math.pow(2,c-1)*Math.pow(2,o),s=0));8<=o;t[r+l]=255&a,l+=g,a/=256,o-=8);for(s=s<<o|a,h+=o;0<h;t[r+l]=255&s,l+=g,s/=256,h-=8);t[r+l-g]|=128*d}var s={}.toString,a=Array.isArray||function(t){return"[object Array]"==s.call(t)};function d(){return w.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function y(t,e){if(d()<e)throw new RangeError("Invalid typed array length");return w.TYPED_ARRAY_SUPPORT?(t=new Uint8Array(e)).__proto__=w.prototype:(null===t&&(t=new w(e)),t.length=e),t}function w(t,e,r){if(!(w.TYPED_ARRAY_SUPPORT||this instanceof w))return new w(t,e,r);if("number"!=typeof t)return v(this,t,e,r);if("string"==typeof e)throw new Error("If encoding is specified then the first argument must be a string");return A(this,t)}function v(t,e,r,n){if("number"==typeof e)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&e instanceof ArrayBuffer?function(t,e,r,n){if(e.byteLength,r<0||e.byteLength<r)throw new RangeError("'offset' is out of bounds");if(e.byteLength<r+(n||0))throw new RangeError("'length' is out of bounds");e=void 0===r&&void 0===n?new Uint8Array(e):void 0===n?new Uint8Array(e,r):new Uint8Array(e,r,n);w.TYPED_ARRAY_SUPPORT?(t=e).__proto__=w.prototype:t=b(t,e);return t}(t,e,r,n):"string"==typeof e?function(t,e,r){"string"==typeof r&&""!==r||(r="utf8");if(!w.isEncoding(r))throw new TypeError('"encoding" must be a valid string encoding');var n=0|R(e,r),o=(t=y(t,n)).write(e,r);o!==n&&(t=t.slice(0,o));return t}(t,e,r):function(t,e){if(m(e)){var r=0|_(e.length);return 0===(t=y(t,r)).length?t:(e.copy(t,0,0,r),t)}if(e){if("undefined"!=typeof ArrayBuffer&&e.buffer instanceof ArrayBuffer||"length"in e)return"number"!=typeof e.length||function(t){return t!=t}(e.length)?y(t,0):b(t,e);if("Buffer"===e.type&&a(e.data))return b(t,e.data)}throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}(t,e)}function E(t){if("number"!=typeof t)throw new TypeError('"size" argument must be a number');if(t<0)throw new RangeError('"size" argument must not be negative')}function A(t,e){if(E(e),t=y(t,e<0?0:0|_(e)),!w.TYPED_ARRAY_SUPPORT)for(var r=0;r<e;++r)t[r]=0;return t}function b(t,e){var r=e.length<0?0:0|_(e.length);t=y(t,r);for(var n=0;n<r;n+=1)t[n]=255&e[n];return t}function _(t){if(t>=d())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+d().toString(16)+" bytes");return 0|t}function m(t){return null!=t&&t._isBuffer}function R(t,e){if(m(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var r=t.length;if(0===r)return 0;for(var n=!1;;)switch(e){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return W(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return J(t).length;default:if(n)return W(t).length;e=(""+e).toLowerCase(),n=!0}}function T(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function P(t,e,r,n,o){if(0===t.length)return-1;if("string"==typeof r?(n=r,r=0):2147483647<r?r=2147483647:r<-2147483648&&(r=-2147483648),r=+r,isNaN(r)&&(r=o?0:t.length-1),r<0&&(r=t.length+r),r>=t.length){if(o)return-1;r=t.length-1}else if(r<0){if(!o)return-1;r=0}if("string"==typeof e&&(e=w.from(e,n)),m(e))return 0===e.length?-1:S(t,e,r,n,o);if("number"==typeof e)return e&=255,w.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):S(t,[e],r,n,o);throw new TypeError("val must be string, number or Buffer")}function S(t,e,r,n,o){var i=1,s=t.length,a=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return-1;s/=i=2,a/=2,r/=2}function u(t,e){return 1===i?t[e]:t.readUInt16BE(e*i)}if(o)for(var h=-1,f=r;f<s;f++)if(u(t,f)===u(e,-1===h?0:f-h)){if(-1===h&&(h=f),f-h+1===a)return h*i}else-1!==h&&(f-=f-h),h=-1;else for(s<r+a&&(r=s-a),f=r;0<=f;f--){for(var c=!0,p=0;p<a;p++)if(u(t,f+p)!==u(e,p)){c=!1;break}if(c)return f}return-1}function M(t,e,r,n){r=Number(r)||0;var o=t.length-r;(!n||o<(n=Number(n)))&&(n=o);var i=e.length;if(i%2!=0)throw new TypeError("Invalid hex string");i/2<n&&(n=i/2);for(var s=0;s<n;++s){var a=parseInt(e.substr(2*s,2),16);if(isNaN(a))return s;t[r+s]=a}return s}function U(t,e,r,n){return $(function(t){for(var e=[],r=0;r<t.length;++r)e.push(255&t.charCodeAt(r));return e}(e),t,r,n)}function O(t,e,r,n){return $(function(t,e){for(var r,n,o,i=[],s=0;s<t.length&&!((e-=2)<0);++s)r=t.charCodeAt(s),n=r>>8,o=r%256,i.push(o),i.push(n);return i}(e,t.length-r),t,r,n)}function k(t,e,r){return 0===e&&r===t.length?n(t):n(t.slice(e,r))}function I(t,e,r){r=Math.min(t.length,r);for(var n=[],o=e;o<r;){var i,s,a,u,h=t[o],f=null,c=239<h?4:223<h?3:191<h?2:1;if(o+c<=r)switch(c){case 1:h<128&&(f=h);break;case 2:128==(192&(i=t[o+1]))&&127<(u=(31&h)<<6|63&i)&&(f=u);break;case 3:i=t[o+1],s=t[o+2],128==(192&i)&&128==(192&s)&&2047<(u=(15&h)<<12|(63&i)<<6|63&s)&&(u<55296||57343<u)&&(f=u);break;case 4:i=t[o+1],s=t[o+2],a=t[o+3],128==(192&i)&&128==(192&s)&&128==(192&a)&&65535<(u=(15&h)<<18|(63&i)<<12|(63&s)<<6|63&a)&&u<1114112&&(f=u)}null===f?(f=65533,c=1):65535<f&&(f-=65536,n.push(f>>>10&1023|55296),f=56320|1023&f),n.push(f),o+=c}return function(t){var e=t.length;if(e<=C)return String.fromCharCode.apply(String,t);var r="",n=0;for(;n<e;)r+=String.fromCharCode.apply(String,t.slice(n,n+=C));return r}(n)}w.TYPED_ARRAY_SUPPORT=void 0===r.TYPED_ARRAY_SUPPORT||r.TYPED_ARRAY_SUPPORT,w.poolSize=8192,w._augment=function(t){return t.__proto__=w.prototype,t},w.from=function(t,e,r){return v(null,t,e,r)},w.TYPED_ARRAY_SUPPORT&&(w.prototype.__proto__=Uint8Array.prototype,w.__proto__=Uint8Array),w.alloc=function(t,e,r){return n=null,i=e,s=r,E(o=t),o<=0||void 0===i?y(n,o):"string"==typeof s?y(n,o).fill(i,s):y(n,o).fill(i);var n,o,i,s},w.allocUnsafe=function(t){return A(null,t)},w.allocUnsafeSlow=function(t){return A(null,t)},w.isBuffer=function(t){return null!=t&&(!!t._isBuffer||Z(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&Z(t.slice(0,0))}(t))},w.compare=function(t,e){if(!m(t)||!m(e))throw new TypeError("Arguments must be Buffers");if(t===e)return 0;for(var r=t.length,n=e.length,o=0,i=Math.min(r,n);o<i;++o)if(t[o]!==e[o]){r=t[o],n=e[o];break}return r<n?-1:n<r?1:0},w.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}},w.concat=function(t,e){if(!a(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return w.alloc(0);if(void 0===e)for(o=e=0;o<t.length;++o)e+=t[o].length;for(var r=w.allocUnsafe(e),n=0,o=0;o<t.length;++o){var i=t[o];if(!m(i))throw new TypeError('"list" argument must be an Array of Buffers');i.copy(r,n),n+=i.length}return r},w.byteLength=R,w.prototype._isBuffer=!0,w.prototype.swap16=function(){var t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(var e=0;e<t;e+=2)T(this,e,e+1);return this},w.prototype.swap32=function(){var t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(var e=0;e<t;e+=4)T(this,e,e+3),T(this,e+1,e+2);return this},w.prototype.swap64=function(){var t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(var e=0;e<t;e+=8)T(this,e,e+7),T(this,e+1,e+6),T(this,e+2,e+5),T(this,e+3,e+4);return this},w.prototype.toString=function(){var t=0|this.length;return 0==t?"":0===arguments.length?I(this,0,t):function(t,e,r){var n=!1;if((void 0===e||e<0)&&(e=0),e>this.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(e>>>=0))return"";for(t=t||"utf8";;)switch(t){case"hex":return j(this,e,r);case"utf8":case"utf-8":return I(this,e,r);case"ascii":return B(this,e,r);case"latin1":case"binary":return Y(this,e,r);case"base64":return k(this,e,r);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return D(this,e,r);default:if(n)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),n=!0}}.apply(this,arguments)},w.prototype.equals=function(t){if(!m(t))throw new TypeError("Argument must be a Buffer");return this===t||0===w.compare(this,t)},w.prototype.inspect=function(){var t="";return 0<this.length&&(t=this.toString("hex",0,50).match(/.{2}/g).join(" "),50<this.length&&(t+=" ... ")),"<Buffer "+t+">"},w.prototype.compare=function(t,e,r,n,o){if(!m(t))throw new TypeError("Argument must be a Buffer");if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===o&&(o=this.length),e<0||r>t.length||n<0||o>this.length)throw new RangeError("out of range index");if(o<=n&&r<=e)return 0;if(o<=n)return-1;if(r<=e)return 1;if(this===t)return 0;for(var i=(o>>>=0)-(n>>>=0),s=(r>>>=0)-(e>>>=0),a=Math.min(i,s),u=this.slice(n,o),h=t.slice(e,r),f=0;f<a;++f)if(u[f]!==h[f]){i=u[f],s=h[f];break}return i<s?-1:s<i?1:0},w.prototype.includes=function(t,e,r){return-1!==this.indexOf(t,e,r)},w.prototype.indexOf=function(t,e,r){return P(this,t,e,r,!0)},w.prototype.lastIndexOf=function(t,e,r){return P(this,t,e,r,!1)},w.prototype.write=function(t,e,r,n){if(void 0===e)n="utf8",r=this.length,e=0;else if(void 0===r&&"string"==typeof e)n=e,r=this.length,e=0;else{if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e|=0,isFinite(r)?(r|=0,void 0===n&&(n="utf8")):(n=r,r=void 0)}var o=this.length-e;if((void 0===r||o<r)&&(r=o),0<t.length&&(r<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");n=n||"utf8";for(var i,s,a,u,h,f,c=!1;;)switch(n){case"hex":return M(this,t,e,r);case"utf8":case"utf-8":return h=e,f=r,$(W(t,(u=this).length-h),u,h,f);case"ascii":return U(this,t,e,r);case"latin1":case"binary":return U(this,t,e,r);case"base64":return i=this,s=e,a=r,$(J(t),i,s,a);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return O(this,t,e,r);default:if(c)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),c=!0}},w.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var C=4096;function B(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(127&t[o]);return n}function Y(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(t[o]);return n}function j(t,e,r){var n=t.length;(!e||e<0)&&(e=0),(!r||r<0||n<r)&&(r=n);for(var o="",i=e;i<r;++i)o+=V(t[i]);return o}function D(t,e,r){for(var n=t.slice(e,r),o="",i=0;i<n.length;i+=2)o+=String.fromCharCode(n[i]+256*n[i+1]);return o}function q(t,e,r){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(r<t+e)throw new RangeError("Trying to access beyond buffer length")}function x(t,e,r,n,o,i){if(!m(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(o<e||e<i)throw new RangeError('"value" argument is out of bounds');if(r+n>t.length)throw new RangeError("Index out of range")}function L(t,e,r,n){e<0&&(e=65535+e+1);for(var o=0,i=Math.min(t.length-r,2);o<i;++o)t[r+o]=(e&255<<8*(n?o:1-o))>>>8*(n?o:1-o)}function N(t,e,r,n){e<0&&(e=4294967295+e+1);for(var o=0,i=Math.min(t.length-r,4);o<i;++o)t[r+o]=e>>>8*(n?o:3-o)&255}function z(t,e,r,n){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function K(t,e,r,n,o){return o||z(t,0,r,4),i(t,e,r,n,23,4),r+4}function F(t,e,r,n,o){return o||z(t,0,r,8),i(t,e,r,n,52,8),r+8}w.prototype.slice=function(t,e){var r=this.length;if((t=~~t)<0?(t+=r)<0&&(t=0):r<t&&(t=r),(e=void 0===e?r:~~e)<0?(e+=r)<0&&(e=0):r<e&&(e=r),e<t&&(e=t),w.TYPED_ARRAY_SUPPORT)(o=this.subarray(t,e)).__proto__=w.prototype;else for(var n=e-t,o=new w(n,void 0),i=0;i<n;++i)o[i]=this[i+t];return o},w.prototype.readUIntLE=function(t,e,r){t|=0,e|=0,r||q(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return n},w.prototype.readUIntBE=function(t,e,r){t|=0,e|=0,r||q(t,e,this.length);for(var n=this[t+--e],o=1;0<e&&(o*=256);)n+=this[t+--e]*o;return n},w.prototype.readUInt8=function(t,e){return e||q(t,1,this.length),this[t]},w.prototype.readUInt16LE=function(t,e){return e||q(t,2,this.length),this[t]|this[t+1]<<8},w.prototype.readUInt16BE=function(t,e){return e||q(t,2,this.length),this[t]<<8|this[t+1]},w.prototype.readUInt32LE=function(t,e){return e||q(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},w.prototype.readUInt32BE=function(t,e){return e||q(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},w.prototype.readIntLE=function(t,e,r){t|=0,e|=0,r||q(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return(o*=128)<=n&&(n-=Math.pow(2,8*e)),n},w.prototype.readIntBE=function(t,e,r){t|=0,e|=0,r||q(t,e,this.length);for(var n=e,o=1,i=this[t+--n];0<n&&(o*=256);)i+=this[t+--n]*o;return(o*=128)<=i&&(i-=Math.pow(2,8*e)),i},w.prototype.readInt8=function(t,e){return e||q(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},w.prototype.readInt16LE=function(t,e){e||q(t,2,this.length);var r=this[t]|this[t+1]<<8;return 32768&r?4294901760|r:r},w.prototype.readInt16BE=function(t,e){e||q(t,2,this.length);var r=this[t+1]|this[t]<<8;return 32768&r?4294901760|r:r},w.prototype.readInt32LE=function(t,e){return e||q(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},w.prototype.readInt32BE=function(t,e){return e||q(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},w.prototype.readFloatLE=function(t,e){return e||q(t,4,this.length),o(this,t,!0,23,4)},w.prototype.readFloatBE=function(t,e){return e||q(t,4,this.length),o(this,t,!1,23,4)},w.prototype.readDoubleLE=function(t,e){return e||q(t,8,this.length),o(this,t,!0,52,8)},w.prototype.readDoubleBE=function(t,e){return e||q(t,8,this.length),o(this,t,!1,52,8)},w.prototype.writeUIntLE=function(t,e,r,n){t=+t,e|=0,r|=0,n||x(this,t,e,r,Math.pow(2,8*r)-1,0);var o=1,i=0;for(this[e]=255&t;++i<r&&(o*=256);)this[e+i]=t/o&255;return e+r},w.prototype.writeUIntBE=function(t,e,r,n){t=+t,e|=0,r|=0,n||x(this,t,e,r,Math.pow(2,8*r)-1,0);var o=r-1,i=1;for(this[e+o]=255&t;0<=--o&&(i*=256);)this[e+o]=t/i&255;return e+r},w.prototype.writeUInt8=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,1,255,0),w.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),this[e]=255&t,e+1},w.prototype.writeUInt16LE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,2,65535,0),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):L(this,t,e,!0),e+2},w.prototype.writeUInt16BE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,2,65535,0),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):L(this,t,e,!1),e+2},w.prototype.writeUInt32LE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,4,4294967295,0),w.TYPED_ARRAY_SUPPORT?(this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t):N(this,t,e,!0),e+4},w.prototype.writeUInt32BE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,4,4294967295,0),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):N(this,t,e,!1),e+4},w.prototype.writeIntLE=function(t,e,r,n){var o;t=+t,e|=0,n||x(this,t,e,r,(o=Math.pow(2,8*r-1))-1,-o);var i=0,s=1,a=0;for(this[e]=255&t;++i<r&&(s*=256);)t<0&&0===a&&0!==this[e+i-1]&&(a=1),this[e+i]=(t/s>>0)-a&255;return e+r},w.prototype.writeIntBE=function(t,e,r,n){var o;t=+t,e|=0,n||x(this,t,e,r,(o=Math.pow(2,8*r-1))-1,-o);var i=r-1,s=1,a=0;for(this[e+i]=255&t;0<=--i&&(s*=256);)t<0&&0===a&&0!==this[e+i+1]&&(a=1),this[e+i]=(t/s>>0)-a&255;return e+r},w.prototype.writeInt8=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,1,127,-128),w.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),t<0&&(t=255+t+1),this[e]=255&t,e+1},w.prototype.writeInt16LE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,2,32767,-32768),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):L(this,t,e,!0),e+2},w.prototype.writeInt16BE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,2,32767,-32768),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):L(this,t,e,!1),e+2},w.prototype.writeInt32LE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,4,2147483647,-2147483648),w.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24):N(this,t,e,!0),e+4},w.prototype.writeInt32BE=function(t,e,r){return t=+t,e|=0,r||x(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),w.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):N(this,t,e,!1),e+4},w.prototype.writeFloatLE=function(t,e,r){return K(this,t,e,!0,r)},w.prototype.writeFloatBE=function(t,e,r){return K(this,t,e,!1,r)},w.prototype.writeDoubleLE=function(t,e,r){return F(this,t,e,!0,r)},w.prototype.writeDoubleBE=function(t,e,r){return F(this,t,e,!1,r)},w.prototype.copy=function(t,e,r,n){if(r=r||0,n||0===n||(n=this.length),e>=t.length&&(e=t.length),e=e||0,0<n&&n<r&&(n=r),n===r)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("sourceStart out of bounds");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e<n-r&&(n=t.length-e+r);var o,i=n-r;if(this===t&&r<e&&e<n)for(o=i-1;0<=o;--o)t[o+e]=this[o+r];else if(i<1e3||!w.TYPED_ARRAY_SUPPORT)for(o=0;o<i;++o)t[o+e]=this[o+r];else Uint8Array.prototype.set.call(t,this.subarray(r,r+i),e);return i},w.prototype.fill=function(t,e,r,n){if("string"==typeof t){var o;if("string"==typeof e?(n=e,e=0,r=this.length):"string"==typeof r&&(n=r,r=this.length),1!==t.length||(o=t.charCodeAt(0))<256&&(t=o),void 0!==n&&"string"!=typeof n)throw new TypeError("encoding must be a string");if("string"==typeof n&&!w.isEncoding(n))throw new TypeError("Unknown encoding: "+n)}else"number"==typeof t&&(t&=255);if(e<0||this.length<e||this.length<r)throw new RangeError("Out of range index");if(r<=e)return this;if(e>>>=0,r=void 0===r?this.length:r>>>0,"number"==typeof(t=t||0))for(a=e;a<r;++a)this[a]=t;else for(var i=m(t)?t:W(new w(t,n).toString()),s=i.length,a=0;a<r-e;++a)this[a+e]=i[a%s];return this};var G=/[^+\/0-9A-Za-z-_]/g;function V(t){return t<16?"0"+t.toString(16):t.toString(16)}function W(t,e){var r;e=e||1/0;for(var n=t.length,o=null,i=[],s=0;s<n;++s){if(55295<(r=t.charCodeAt(s))&&r<57344){if(!o){if(56319<r){-1<(e-=3)&&i.push(239,191,189);continue}if(s+1===n){-1<(e-=3)&&i.push(239,191,189);continue}o=r;continue}if(r<56320){-1<(e-=3)&&i.push(239,191,189),o=r;continue}r=65536+(o-55296<<10|r-56320)}else o&&-1<(e-=3)&&i.push(239,191,189);if(o=null,r<128){if(--e<0)break;i.push(r)}else if(r<2048){if((e-=2)<0)break;i.push(r>>6|192,63&r|128)}else if(r<65536){if((e-=3)<0)break;i.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function J(t){return function(t){var e,r,n,o;c||p();var i=t.length;if(0<i%4)throw new Error("Invalid string. Length must be a multiple of 4");n="="===t[i-2]?2:"="===t[i-1]?1:0,o=new f(3*i/4-n),e=0<n?i-4:i;for(var s=0,a=0;a<e;a+=4,0)r=h[t.charCodeAt(a)]<<18|h[t.charCodeAt(a+1)]<<12|h[t.charCodeAt(a+2)]<<6|h[t.charCodeAt(a+3)],o[s++]=r>>16&255,o[s++]=r>>8&255,o[s++]=255&r;return 2==n?(r=h[t.charCodeAt(a)]<<2|h[t.charCodeAt(a+1)]>>4,o[s++]=255&r):1==n&&(r=h[t.charCodeAt(a)]<<10|h[t.charCodeAt(a+1)]<<4|h[t.charCodeAt(a+2)]>>2,o[s++]=r>>8&255,o[s++]=255&r),o}(function(t){var e;if((t=((e=t).trim?e.trim():e.replace(/^\s+|\s+$/g,"")).replace(G,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function $(t,e,r,n){for(var o=0;o<n&&!(o+r>=e.length||o>=t.length);++o)e[o+r]=t[o];return o}function Z(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}function H(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");var e=t.credentials,r=e.clientId,n=e.clientSecret;if(!r||!n)throw new Error("Missing required credentials (clientId, clientSecret)");var o=t.scopes?t.scopes.join(" "):void 0,i=w.from("".concat(r,":").concat(n)).toString("base64"),s=t.oauthUri||"/oauth/token";return{basicAuth:i,url:t.host.replace(/\/$/,"")+s,body:"grant_type=client_credentials".concat(o?"&scope=".concat(o):"")}}function Q(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");if(!t.refreshToken)throw new Error("Missing required option (refreshToken)");var e=t.credentials,r=e.clientId,n=e.clientSecret;if(!r||!n)throw new Error("Missing required credentials (clientId, clientSecret)");var o=w.from("".concat(r,":").concat(n)).toString("base64"),i=t.oauthUri||"/oauth/token";return{basicAuth:o,url:t.host.replace(/\/$/,"")+i,body:"grant_type=refresh_token&refresh_token=".concat(encodeURIComponent(t.refreshToken))}}function X(t,e){return g(g({},e),{},{headers:g(g({},e.headers),{},{Authorization:"Bearer ".concat(t)})})}function tt(t){var e=t.fetcher,r=t.url,n=t.basicAuth,o=t.body,a=t.tokenCache,u=t.requestState,h=t.pendingTasks,i=t.response,f=t.tokenCacheKey;e(r,{method:"POST",headers:{Authorization:"Basic ".concat(n),"Content-Length":w.byteLength(o).toString(),"Content-Type":"application/x-www-form-urlencoded"},body:o}).then(function(t){return t.ok?t.json().then(function(t){var e,r=t.access_token,n=t.expires_in,o=t.refresh_token,i=(e=n,Date.now()+1e3*e-72e5);a.set({token:r,expirationTime:i,refreshToken:o},f),u.set(!1);var s=h.slice();h=[],s.forEach(function(t){var e=X(r,t.request);t.next(e,t.response)})}):t.text().then(function(t){var e;try{e=JSON.parse(t)}catch(r){}var r=new Error(e?e.message:t);e&&(r.body=e),u.set(!1),i.reject(r)})}).catch(function(t){u.set(!1),i&&"function"==typeof i.reject&&i.reject(t)})}function et(t,e,r){var n=t.request,o=t.response,i=t.url,s=t.basicAuth,a=t.body,u=t.pendingTasks,h=t.requestState,f=t.tokenCache,c=t.tokenCacheKey,p=t.fetch;if(!p&&"undefined"==typeof fetch)throw new Error("`fetch` is not available. Please pass in `fetch` as an option or have it globally available.");if(p=p||fetch,n.headers&&n.headers.authorization||n.headers&&n.headers.Authorization)e(n,o);else{var l=f.get(c);if(l&&l.token&&Date.now()<l.expirationTime)e(X(l.token,n),o);else if(u.push({request:n,response:o,next:e}),!h.get())if(h.set(!0),l&&l.refreshToken&&(!l.token||l.token&&Date.now()>l.expirationTime)){if(!r)throw new Error("Missing required options");tt(g(g({fetcher:p},Q(g(g({},r),{},{refreshToken:l.refreshToken}))),{},{tokenCacheKey:c,tokenCache:f,requestState:h,pendingTasks:u,response:o}))}else tt({fetcher:p,url:i,basicAuth:s,body:a,tokenCacheKey:c,tokenCache:f,requestState:h,pendingTasks:u,response:o})}}function rt(t){var e=t;return{get:function(){return e},set:function(t){return e=t}}}var nt=Object.freeze({__proto__:null,MANAGE_PROJECT:"manage_project",MANAGE_PRODUCTS:"manage_products",VIEW_PRODUCTS:"view_products",MANAGE_ORDERS:"manage_orders",VIEW_ORDERS:"view_orders",MANAGE_MY_ORDERS:"manage_my_orders",MANAGE_CUSTOMERS:"manage_customers",VIEW_CUSTOMERS:"view_customers",MANAGE_MY_PROFILE:"manage_my_profile",MANAGE_TYPES:"manage_types",VIEW_TYPES:"view_types",MANAGE_PAYMENTS:"manage_payments",VIEW_PAYMENTS:"view_payments",CREATE_ANONYMOUS_TOKEN:"create_anonymous_token",MANAGE_SUBSCRIPTIONS:"manage_subscriptions"});t.createAuthMiddlewareForAnonymousSessionFlow=function(n){var o=rt({}),i=[],s=rt(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):et(g(g({request:t,response:e},function(t){if(!t)throw new Error("Missing required options");if(!t.projectKey)throw new Error("Missing required option (projectKey)");var e=t.projectKey;t.oauthUri=t.oauthUri||"/oauth/".concat(e,"/anonymous/token");var r=H(t);return t.credentials.anonymousId&&(r.body+="&anonymous_id=".concat(t.credentials.anonymousId)),g({},r)}(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r,n)}}},t.createAuthMiddlewareForClientCredentialsFlow=function(o){var i=o.tokenCache||rt({token:"",expirationTime:-1}),s=rt(!1),a=[];return function(n){return function(t,e){var r;t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?n(t,e):et(g(g({request:t,response:e},H(o)),{},{pendingTasks:a,requestState:s,tokenCache:i,tokenCacheKey:{clientId:(r=o).credentials.clientId,host:r.host,projectKey:r.projectKey},fetch:o.fetch}),n)}}},t.createAuthMiddlewareForPasswordFlow=function(n){var o=rt({}),i=[],s=rt(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):et(g(g({request:t,response:e},function(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");var e=t.credentials,r=e.clientId,n=e.clientSecret,o=e.user,i=t.projectKey;if(!(r&&n&&o))throw new Error("Missing required credentials (clientId, clientSecret, user)");var s=o.username,a=o.password;if(!s||!a)throw new Error("Missing required user credentials (username, password)");var u=(t.scopes||[]).join(" "),h=u?"&scope=".concat(u):"",f=w.from("".concat(r,":").concat(n)).toString("base64"),c=t.oauthUri||"/oauth/".concat(i,"/customers/token");return{basicAuth:f,url:t.host.replace(/\/$/,"")+c,body:"grant_type=password&username=".concat(encodeURIComponent(s),"&password=").concat(encodeURIComponent(a)).concat(h)}}(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r,n)}}},t.createAuthMiddlewareForRefreshTokenFlow=function(n){var o=rt({}),i=[],s=rt(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):et(g(g({request:t,response:e},Q(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r)}}},t.createAuthMiddlewareWithExistingToken=function(){var i=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",s=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return function(o){return function(t,e){if("string"!=typeof i)throw new Error("authorization must be a string");var r=void 0===s.force||s.force;if(!i||(t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization)&&!1===r)return o(t,e);var n=g(g({},t),{},{headers:g(g({},t.headers),{},{Authorization:i})});return o(n,e)}}},t.scopes=nt,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).CommercetoolsSdkMiddlewareAuth={})}(this,function(t){"use strict";function e(e,t){var r,n=Object.keys(e);return Object.getOwnPropertySymbols&&(r=Object.getOwnPropertySymbols(e),t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)),n}function w(o){for(var t=1;t<arguments.length;t++){var i=null!=arguments[t]?arguments[t]:{};t%2?e(Object(i),!0).forEach(function(t){var e,r,n;e=o,n=i[r=t],r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(o,Object.getOwnPropertyDescriptors(i)):e(Object(i)).forEach(function(t){Object.defineProperty(o,t,Object.getOwnPropertyDescriptor(i,t))})}return o}var r="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{},u=[],h=[],f="undefined"!=typeof Uint8Array?Uint8Array:Array,c=!1;function l(){c=!0;for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",e=0,r=t.length;e<r;++e)u[e]=t[e],h[t.charCodeAt(e)]=e;h["-".charCodeAt(0)]=62,h["_".charCodeAt(0)]=63}function p(t,e,r){for(var n,o,i=[],s=e;s<r;s+=3)n=(t[s]<<16)+(t[s+1]<<8)+t[s+2],i.push(u[(o=n)>>18&63]+u[o>>12&63]+u[o>>6&63]+u[63&o]);return i.join("")}function a(t){var e;c||l();for(var r=t.length,n=r%3,o="",i=[],s=0,a=r-n;s<a;s+=16383)i.push(p(t,s,a<s+16383?a:s+16383));return 1==n?(e=t[r-1],o+=u[e>>2],o+=u[e<<4&63],o+="=="):2==n&&(e=(t[r-2]<<8)+t[r-1],o+=u[e>>10],o+=u[e>>4&63],o+=u[e<<2&63],o+="="),i.push(o),i.join("")}function n(t,e,r,n,o){var i,s,a=8*o-n-1,u=(1<<a)-1,h=u>>1,f=-7,c=r?o-1:0,l=r?-1:1,p=t[e+c];for(c+=l,i=p&(1<<-f)-1,p>>=-f,f+=a;0<f;i=256*i+t[e+c],c+=l,f-=8);for(s=i&(1<<-f)-1,i>>=-f,f+=n;0<f;s=256*s+t[e+c],c+=l,f-=8);if(0===i)i=1-h;else{if(i===u)return s?NaN:1/0*(p?-1:1);s+=Math.pow(2,n),i-=h}return(p?-1:1)*s*Math.pow(2,i-n)}function i(t,e,r,n,o,i){var s,a,u,h=8*i-o-1,f=(1<<h)-1,c=f>>1,l=23===o?Math.pow(2,-24)-Math.pow(2,-77):0,p=n?0:i-1,g=n?1:-1,d=e<0||0===e&&1/e<0?1:0;for(e=Math.abs(e),isNaN(e)||e===1/0?(a=isNaN(e)?1:0,s=f):(s=Math.floor(Math.log(e)/Math.LN2),e*(u=Math.pow(2,-s))<1&&(s--,u*=2),2<=(e+=1<=s+c?l/u:l*Math.pow(2,1-c))*u&&(s++,u/=2),f<=s+c?(a=0,s=f):1<=s+c?(a=(e*u-1)*Math.pow(2,o),s+=c):(a=e*Math.pow(2,c-1)*Math.pow(2,o),s=0));8<=o;t[r+p]=255&a,p+=g,a/=256,o-=8);for(s=s<<o|a,h+=o;0<h;t[r+p]=255&s,p+=g,s/=256,h-=8);t[r+p-g]|=128*d}var o={}.toString,s=Array.isArray||function(t){return"[object Array]"==o.call(t)};function g(){return _.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function d(t,e){if(g()<e)throw new RangeError("Invalid typed array length");return _.TYPED_ARRAY_SUPPORT?(t=new Uint8Array(e)).__proto__=_.prototype:(null===t&&(t=new _(e)),t.length=e),t}function _(t,e,r){if(!(_.TYPED_ARRAY_SUPPORT||this instanceof _))return new _(t,e,r);if("number"!=typeof t)return y(this,t,e,r);if("string"==typeof e)throw new Error("If encoding is specified then the first argument must be a string");return E(this,t)}function y(t,e,r,n){if("number"==typeof e)throw new TypeError('"value" argument must not be a number');return"undefined"!=typeof ArrayBuffer&&e instanceof ArrayBuffer?function(t,e,r,n){if(e.byteLength,r<0||e.byteLength<r)throw new RangeError("'offset' is out of bounds");if(e.byteLength<r+(n||0))throw new RangeError("'length' is out of bounds");e=void 0===r&&void 0===n?new Uint8Array(e):void 0===n?new Uint8Array(e,r):new Uint8Array(e,r,n);_.TYPED_ARRAY_SUPPORT?(t=e).__proto__=_.prototype:t=b(t,e);return t}(t,e,r,n):"string"==typeof e?function(t,e,r){"string"==typeof r&&""!==r||(r="utf8");if(!_.isEncoding(r))throw new TypeError('"encoding" must be a valid string encoding');var n=0|R(e,r),o=(t=d(t,n)).write(e,r);o!==n&&(t=t.slice(0,o));return t}(t,e,r):function(t,e){if(m(e)){var r=0|A(e.length);return 0===(t=d(t,r)).length?t:(e.copy(t,0,0,r),t)}if(e){if("undefined"!=typeof ArrayBuffer&&e.buffer instanceof ArrayBuffer||"length"in e)return"number"!=typeof e.length||function(t){return t!=t}(e.length)?d(t,0):b(t,e);if("Buffer"===e.type&&s(e.data))return b(t,e.data)}throw new TypeError("First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.")}(t,e)}function v(t){if("number"!=typeof t)throw new TypeError('"size" argument must be a number');if(t<0)throw new RangeError('"size" argument must not be negative')}function E(t,e){if(v(e),t=d(t,e<0?0:0|A(e)),!_.TYPED_ARRAY_SUPPORT)for(var r=0;r<e;++r)t[r]=0;return t}function b(t,e){var r=e.length<0?0:0|A(e.length);t=d(t,r);for(var n=0;n<r;n+=1)t[n]=255&e[n];return t}function A(t){if(t>=g())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+g().toString(16)+" bytes");return 0|t}function m(t){return null!=t&&t._isBuffer}function R(t,e){if(m(t))return t.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(t)||t instanceof ArrayBuffer))return t.byteLength;"string"!=typeof t&&(t=""+t);var r=t.length;if(0===r)return 0;for(var n=!1;;)switch(e){case"ascii":case"latin1":case"binary":return r;case"utf8":case"utf-8":case void 0:return N(t).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*r;case"hex":return r>>>1;case"base64":return z(t).length;default:if(n)return N(t).length;e=(""+e).toLowerCase(),n=!0}}function T(t,e,r){var n,o,i,s=!1;if((void 0===e||e<0)&&(e=0),e>this.length)return"";if((void 0===r||r>this.length)&&(r=this.length),r<=0)return"";if((r>>>=0)<=(e>>>=0))return"";for(t=t||"utf8";;)switch(t){case"hex":return function(t,e,r){var n=t.length;(!e||e<0)&&(e=0);(!r||r<0||n<r)&&(r=n);for(var o="",i=e;i<r;++i)o+=function(t){return t<16?"0"+t.toString(16):t.toString(16)}(t[i]);return o}(this,e,r);case"utf8":case"utf-8":return k(this,e,r);case"ascii":return function(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(127&t[o]);return n}(this,e,r);case"latin1":case"binary":return function(t,e,r){var n="";r=Math.min(t.length,r);for(var o=e;o<r;++o)n+=String.fromCharCode(t[o]);return n}(this,e,r);case"base64":return n=this,i=r,0===(o=e)&&i===n.length?a(n):a(n.slice(o,i));case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return function(t,e,r){for(var n=t.slice(e,r),o="",i=0;i<n.length;i+=2)o+=String.fromCharCode(n[i]+256*n[i+1]);return o}(this,e,r);default:if(s)throw new TypeError("Unknown encoding: "+t);t=(t+"").toLowerCase(),s=!0}}function P(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}function S(t,e,r,n,o){if(0===t.length)return-1;if("string"==typeof r?(n=r,r=0):2147483647<r?r=2147483647:r<-2147483648&&(r=-2147483648),r=+r,isNaN(r)&&(r=o?0:t.length-1),r<0&&(r=t.length+r),r>=t.length){if(o)return-1;r=t.length-1}else if(r<0){if(!o)return-1;r=0}if("string"==typeof e&&(e=_.from(e,n)),m(e))return 0===e.length?-1:C(t,e,r,n,o);if("number"==typeof e)return e&=255,_.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?o?Uint8Array.prototype.indexOf.call(t,e,r):Uint8Array.prototype.lastIndexOf.call(t,e,r):C(t,[e],r,n,o);throw new TypeError("val must be string, number or Buffer")}function C(t,e,r,n,o){var i=1,s=t.length,a=e.length;if(void 0!==n&&("ucs2"===(n=String(n).toLowerCase())||"ucs-2"===n||"utf16le"===n||"utf-16le"===n)){if(t.length<2||e.length<2)return-1;s/=i=2,a/=2,r/=2}function u(t,e){return 1===i?t[e]:t.readUInt16BE(e*i)}if(o)for(var h=-1,f=r;f<s;f++)if(u(t,f)===u(e,-1===h?0:f-h)){if(-1===h&&(h=f),f-h+1===a)return h*i}else-1!==h&&(f-=f-h),h=-1;else for(s<r+a&&(r=s-a),f=r;0<=f;f--){for(var c=!0,l=0;l<a;l++)if(u(t,f+l)!==u(e,l)){c=!1;break}if(c)return f}return-1}function M(t,e,r,n){return K(function(t){for(var e=[],r=0;r<t.length;++r)e.push(255&t.charCodeAt(r));return e}(e),t,r,n)}function U(t,e,r,n){return K(function(t,e){for(var r,n,o,i=[],s=0;s<t.length&&!((e-=2)<0);++s)r=t.charCodeAt(s),n=r>>8,o=r%256,i.push(o),i.push(n);return i}(e,t.length-r),t,r,n)}function k(t,e,r){r=Math.min(t.length,r);for(var n=[],o=e;o<r;){var i,s,a,u,h=t[o],f=null,c=239<h?4:223<h?3:191<h?2:1;if(o+c<=r)switch(c){case 1:h<128&&(f=h);break;case 2:128==(192&(i=t[o+1]))&&127<(u=(31&h)<<6|63&i)&&(f=u);break;case 3:i=t[o+1],s=t[o+2],128==(192&i)&&128==(192&s)&&2047<(u=(15&h)<<12|(63&i)<<6|63&s)&&(u<55296||57343<u)&&(f=u);break;case 4:i=t[o+1],s=t[o+2],a=t[o+3],128==(192&i)&&128==(192&s)&&128==(192&a)&&65535<(u=(15&h)<<18|(63&i)<<12|(63&s)<<6|63&a)&&u<1114112&&(f=u)}null===f?(f=65533,c=1):65535<f&&(f-=65536,n.push(f>>>10&1023|55296),f=56320|1023&f),n.push(f),o+=c}return function(t){var e=t.length;if(e<=O)return String.fromCharCode.apply(String,t);var r="",n=0;for(;n<e;)r+=String.fromCharCode.apply(String,t.slice(n,n+=O));return r}(n)}_.TYPED_ARRAY_SUPPORT=void 0===r.TYPED_ARRAY_SUPPORT||r.TYPED_ARRAY_SUPPORT,_.poolSize=8192,_._augment=function(t){return t.__proto__=_.prototype,t},_.from=function(t,e,r){return y(null,t,e,r)},_.TYPED_ARRAY_SUPPORT&&(_.prototype.__proto__=Uint8Array.prototype,_.__proto__=Uint8Array),_.alloc=function(t,e,r){return n=null,i=e,s=r,v(o=t),!(o<=0)&&void 0!==i?"string"==typeof s?d(n,o).fill(i,s):d(n,o).fill(i):d(n,o);var n,o,i,s},_.allocUnsafe=function(t){return E(null,t)},_.allocUnsafeSlow=function(t){return E(null,t)},_.isBuffer=function(t){return null!=t&&(!!t._isBuffer||F(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&F(t.slice(0,0))}(t))},_.compare=function(t,e){if(!m(t)||!m(e))throw new TypeError("Arguments must be Buffers");if(t===e)return 0;for(var r=t.length,n=e.length,o=0,i=Math.min(r,n);o<i;++o)if(t[o]!==e[o]){r=t[o],n=e[o];break}return r<n?-1:n<r?1:0},_.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}},_.concat=function(t,e){if(!s(t))throw new TypeError('"list" argument must be an Array of Buffers');if(0===t.length)return _.alloc(0);if(void 0===e)for(o=e=0;o<t.length;++o)e+=t[o].length;for(var r=_.allocUnsafe(e),n=0,o=0;o<t.length;++o){var i=t[o];if(!m(i))throw new TypeError('"list" argument must be an Array of Buffers');i.copy(r,n),n+=i.length}return r},_.byteLength=R,_.prototype._isBuffer=!0,_.prototype.swap16=function(){var t=this.length;if(t%2!=0)throw new RangeError("Buffer size must be a multiple of 16-bits");for(var e=0;e<t;e+=2)P(this,e,e+1);return this},_.prototype.swap32=function(){var t=this.length;if(t%4!=0)throw new RangeError("Buffer size must be a multiple of 32-bits");for(var e=0;e<t;e+=4)P(this,e,e+3),P(this,e+1,e+2);return this},_.prototype.swap64=function(){var t=this.length;if(t%8!=0)throw new RangeError("Buffer size must be a multiple of 64-bits");for(var e=0;e<t;e+=8)P(this,e,e+7),P(this,e+1,e+6),P(this,e+2,e+5),P(this,e+3,e+4);return this},_.prototype.toString=function(){var t=0|this.length;return 0==t?"":0===arguments.length?k(this,0,t):T.apply(this,arguments)},_.prototype.equals=function(t){if(!m(t))throw new TypeError("Argument must be a Buffer");return this===t||0===_.compare(this,t)},_.prototype.inspect=function(){var t="";return 0<this.length&&(t=this.toString("hex",0,50).match(/.{2}/g).join(" "),50<this.length&&(t+=" ... ")),"<Buffer "+t+">"},_.prototype.compare=function(t,e,r,n,o){if(!m(t))throw new TypeError("Argument must be a Buffer");if(void 0===e&&(e=0),void 0===r&&(r=t?t.length:0),void 0===n&&(n=0),void 0===o&&(o=this.length),e<0||r>t.length||n<0||o>this.length)throw new RangeError("out of range index");if(o<=n&&r<=e)return 0;if(o<=n)return-1;if(r<=e)return 1;if(this===t)return 0;for(var i=(o>>>=0)-(n>>>=0),s=(r>>>=0)-(e>>>=0),a=Math.min(i,s),u=this.slice(n,o),h=t.slice(e,r),f=0;f<a;++f)if(u[f]!==h[f]){i=u[f],s=h[f];break}return i<s?-1:s<i?1:0},_.prototype.includes=function(t,e,r){return-1!==this.indexOf(t,e,r)},_.prototype.indexOf=function(t,e,r){return S(this,t,e,r,!0)},_.prototype.lastIndexOf=function(t,e,r){return S(this,t,e,r,!1)},_.prototype.write=function(t,e,r,n){if(void 0===e)n="utf8",r=this.length,e=0;else if(void 0===r&&"string"==typeof e)n=e,r=this.length,e=0;else{if(!isFinite(e))throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");e|=0,isFinite(r)?(r|=0,void 0===n&&(n="utf8")):(n=r,r=void 0)}var o=this.length-e;if((void 0===r||o<r)&&(r=o),0<t.length&&(r<0||e<0)||e>this.length)throw new RangeError("Attempt to write outside buffer bounds");n=n||"utf8";for(var i,s,a,u,h,f,c=!1;;)switch(n){case"hex":return function(t,e,r,n){r=Number(r)||0;var o=t.length-r;(!n||o<(n=Number(n)))&&(n=o);var i=e.length;if(i%2!=0)throw new TypeError("Invalid hex string");i/2<n&&(n=i/2);for(var s=0;s<n;++s){var a=parseInt(e.substr(2*s,2),16);if(isNaN(a))return s;t[r+s]=a}return s}(this,t,e,r);case"utf8":case"utf-8":return h=e,f=r,K(N(t,(u=this).length-h),u,h,f);case"ascii":return M(this,t,e,r);case"latin1":case"binary":return M(this,t,e,r);case"base64":return i=this,s=e,a=r,K(z(t),i,s,a);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return U(this,t,e,r);default:if(c)throw new TypeError("Unknown encoding: "+n);n=(""+n).toLowerCase(),c=!0}},_.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var O=4096;function I(t,e,r){if(t%1!=0||t<0)throw new RangeError("offset is not uint");if(r<t+e)throw new RangeError("Trying to access beyond buffer length")}function B(t,e,r,n,o,i){if(!m(t))throw new TypeError('"buffer" argument must be a Buffer instance');if(o<e||e<i)throw new RangeError('"value" argument is out of bounds');if(r+n>t.length)throw new RangeError("Index out of range")}function Y(t,e,r,n){e<0&&(e=65535+e+1);for(var o=0,i=Math.min(t.length-r,2);o<i;++o)t[r+o]=(e&255<<8*(n?o:1-o))>>>8*(n?o:1-o)}function j(t,e,r,n){e<0&&(e=4294967295+e+1);for(var o=0,i=Math.min(t.length-r,4);o<i;++o)t[r+o]=e>>>8*(n?o:3-o)&255}function q(t,e,r,n){if(r+n>t.length)throw new RangeError("Index out of range");if(r<0)throw new RangeError("Index out of range")}function D(t,e,r,n,o){return o||q(t,0,r,4),i(t,e,r,n,23,4),r+4}function x(t,e,r,n,o){return o||q(t,0,r,8),i(t,e,r,n,52,8),r+8}_.prototype.slice=function(t,e){var r=this.length;if((t=~~t)<0?(t+=r)<0&&(t=0):r<t&&(t=r),(e=void 0===e?r:~~e)<0?(e+=r)<0&&(e=0):r<e&&(e=r),e<t&&(e=t),_.TYPED_ARRAY_SUPPORT)(o=this.subarray(t,e)).__proto__=_.prototype;else for(var n=e-t,o=new _(n,void 0),i=0;i<n;++i)o[i]=this[i+t];return o},_.prototype.readUIntLE=function(t,e,r){t|=0,e|=0,r||I(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return n},_.prototype.readUIntBE=function(t,e,r){t|=0,e|=0,r||I(t,e,this.length);for(var n=this[t+--e],o=1;0<e&&(o*=256);)n+=this[t+--e]*o;return n},_.prototype.readUInt8=function(t,e){return e||I(t,1,this.length),this[t]},_.prototype.readUInt16LE=function(t,e){return e||I(t,2,this.length),this[t]|this[t+1]<<8},_.prototype.readUInt16BE=function(t,e){return e||I(t,2,this.length),this[t]<<8|this[t+1]},_.prototype.readUInt32LE=function(t,e){return e||I(t,4,this.length),(this[t]|this[t+1]<<8|this[t+2]<<16)+16777216*this[t+3]},_.prototype.readUInt32BE=function(t,e){return e||I(t,4,this.length),16777216*this[t]+(this[t+1]<<16|this[t+2]<<8|this[t+3])},_.prototype.readIntLE=function(t,e,r){t|=0,e|=0,r||I(t,e,this.length);for(var n=this[t],o=1,i=0;++i<e&&(o*=256);)n+=this[t+i]*o;return(o*=128)<=n&&(n-=Math.pow(2,8*e)),n},_.prototype.readIntBE=function(t,e,r){t|=0,e|=0,r||I(t,e,this.length);for(var n=e,o=1,i=this[t+--n];0<n&&(o*=256);)i+=this[t+--n]*o;return(o*=128)<=i&&(i-=Math.pow(2,8*e)),i},_.prototype.readInt8=function(t,e){return e||I(t,1,this.length),128&this[t]?-1*(255-this[t]+1):this[t]},_.prototype.readInt16LE=function(t,e){e||I(t,2,this.length);var r=this[t]|this[t+1]<<8;return 32768&r?4294901760|r:r},_.prototype.readInt16BE=function(t,e){e||I(t,2,this.length);var r=this[t+1]|this[t]<<8;return 32768&r?4294901760|r:r},_.prototype.readInt32LE=function(t,e){return e||I(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24},_.prototype.readInt32BE=function(t,e){return e||I(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]},_.prototype.readFloatLE=function(t,e){return e||I(t,4,this.length),n(this,t,!0,23,4)},_.prototype.readFloatBE=function(t,e){return e||I(t,4,this.length),n(this,t,!1,23,4)},_.prototype.readDoubleLE=function(t,e){return e||I(t,8,this.length),n(this,t,!0,52,8)},_.prototype.readDoubleBE=function(t,e){return e||I(t,8,this.length),n(this,t,!1,52,8)},_.prototype.writeUIntLE=function(t,e,r,n){t=+t,e|=0,r|=0,n||B(this,t,e,r,Math.pow(2,8*r)-1,0);var o=1,i=0;for(this[e]=255&t;++i<r&&(o*=256);)this[e+i]=t/o&255;return e+r},_.prototype.writeUIntBE=function(t,e,r,n){t=+t,e|=0,r|=0,n||B(this,t,e,r,Math.pow(2,8*r)-1,0);var o=r-1,i=1;for(this[e+o]=255&t;0<=--o&&(i*=256);)this[e+o]=t/i&255;return e+r},_.prototype.writeUInt8=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,1,255,0),_.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),this[e]=255&t,e+1},_.prototype.writeUInt16LE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,2,65535,0),_.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):Y(this,t,e,!0),e+2},_.prototype.writeUInt16BE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,2,65535,0),_.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):Y(this,t,e,!1),e+2},_.prototype.writeUInt32LE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,4,4294967295,0),_.TYPED_ARRAY_SUPPORT?(this[e+3]=t>>>24,this[e+2]=t>>>16,this[e+1]=t>>>8,this[e]=255&t):j(this,t,e,!0),e+4},_.prototype.writeUInt32BE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,4,4294967295,0),_.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):j(this,t,e,!1),e+4},_.prototype.writeIntLE=function(t,e,r,n){var o;t=+t,e|=0,n||B(this,t,e,r,(o=Math.pow(2,8*r-1))-1,-o);var i=0,s=1,a=0;for(this[e]=255&t;++i<r&&(s*=256);)t<0&&0===a&&0!==this[e+i-1]&&(a=1),this[e+i]=(t/s>>0)-a&255;return e+r},_.prototype.writeIntBE=function(t,e,r,n){var o;t=+t,e|=0,n||B(this,t,e,r,(o=Math.pow(2,8*r-1))-1,-o);var i=r-1,s=1,a=0;for(this[e+i]=255&t;0<=--i&&(s*=256);)t<0&&0===a&&0!==this[e+i+1]&&(a=1),this[e+i]=(t/s>>0)-a&255;return e+r},_.prototype.writeInt8=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,1,127,-128),_.TYPED_ARRAY_SUPPORT||(t=Math.floor(t)),t<0&&(t=255+t+1),this[e]=255&t,e+1},_.prototype.writeInt16LE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,2,32767,-32768),_.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8):Y(this,t,e,!0),e+2},_.prototype.writeInt16BE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,2,32767,-32768),_.TYPED_ARRAY_SUPPORT?(this[e]=t>>>8,this[e+1]=255&t):Y(this,t,e,!1),e+2},_.prototype.writeInt32LE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,4,2147483647,-2147483648),_.TYPED_ARRAY_SUPPORT?(this[e]=255&t,this[e+1]=t>>>8,this[e+2]=t>>>16,this[e+3]=t>>>24):j(this,t,e,!0),e+4},_.prototype.writeInt32BE=function(t,e,r){return t=+t,e|=0,r||B(this,t,e,4,2147483647,-2147483648),t<0&&(t=4294967295+t+1),_.TYPED_ARRAY_SUPPORT?(this[e]=t>>>24,this[e+1]=t>>>16,this[e+2]=t>>>8,this[e+3]=255&t):j(this,t,e,!1),e+4},_.prototype.writeFloatLE=function(t,e,r){return D(this,t,e,!0,r)},_.prototype.writeFloatBE=function(t,e,r){return D(this,t,e,!1,r)},_.prototype.writeDoubleLE=function(t,e,r){return x(this,t,e,!0,r)},_.prototype.writeDoubleBE=function(t,e,r){return x(this,t,e,!1,r)},_.prototype.copy=function(t,e,r,n){if(r=r||0,n||0===n||(n=this.length),e>=t.length&&(e=t.length),e=e||0,0<n&&n<r&&(n=r),n===r)return 0;if(0===t.length||0===this.length)return 0;if(e<0)throw new RangeError("targetStart out of bounds");if(r<0||r>=this.length)throw new RangeError("sourceStart out of bounds");if(n<0)throw new RangeError("sourceEnd out of bounds");n>this.length&&(n=this.length),t.length-e<n-r&&(n=t.length-e+r);var o,i=n-r;if(this===t&&r<e&&e<n)for(o=i-1;0<=o;--o)t[o+e]=this[o+r];else if(i<1e3||!_.TYPED_ARRAY_SUPPORT)for(o=0;o<i;++o)t[o+e]=this[o+r];else Uint8Array.prototype.set.call(t,this.subarray(r,r+i),e);return i},_.prototype.fill=function(t,e,r,n){if("string"==typeof t){var o;if("string"==typeof e?(n=e,e=0,r=this.length):"string"==typeof r&&(n=r,r=this.length),1!==t.length||(o=t.charCodeAt(0))<256&&(t=o),void 0!==n&&"string"!=typeof n)throw new TypeError("encoding must be a string");if("string"==typeof n&&!_.isEncoding(n))throw new TypeError("Unknown encoding: "+n)}else"number"==typeof t&&(t&=255);if(e<0||this.length<e||this.length<r)throw new RangeError("Out of range index");if(r<=e)return this;if(e>>>=0,r=void 0===r?this.length:r>>>0,"number"==typeof(t=t||0))for(a=e;a<r;++a)this[a]=t;else for(var i=m(t)?t:N(new _(t,n).toString()),s=i.length,a=0;a<r-e;++a)this[a+e]=i[a%s];return this};var L=/[^+\/0-9A-Za-z-_]/g;function N(t,e){var r;e=e||1/0;for(var n=t.length,o=null,i=[],s=0;s<n;++s){if(55295<(r=t.charCodeAt(s))&&r<57344){if(!o){if(56319<r){-1<(e-=3)&&i.push(239,191,189);continue}if(s+1===n){-1<(e-=3)&&i.push(239,191,189);continue}o=r;continue}if(r<56320){-1<(e-=3)&&i.push(239,191,189),o=r;continue}r=65536+(o-55296<<10|r-56320)}else o&&-1<(e-=3)&&i.push(239,191,189);if(o=null,r<128){if(--e<0)break;i.push(r)}else if(r<2048){if((e-=2)<0)break;i.push(r>>6|192,63&r|128)}else if(r<65536){if((e-=3)<0)break;i.push(r>>12|224,r>>6&63|128,63&r|128)}else{if(!(r<1114112))throw new Error("Invalid code point");if((e-=4)<0)break;i.push(r>>18|240,r>>12&63|128,r>>6&63|128,63&r|128)}}return i}function z(t){return function(t){var e,r,n,o;c||l();var i=t.length;if(0<i%4)throw new Error("Invalid string. Length must be a multiple of 4");n="="===t[i-2]?2:"="===t[i-1]?1:0,o=new f(3*i/4-n),e=0<n?i-4:i;for(var s=0,a=0;a<e;a+=4,0)r=h[t.charCodeAt(a)]<<18|h[t.charCodeAt(a+1)]<<12|h[t.charCodeAt(a+2)]<<6|h[t.charCodeAt(a+3)],o[s++]=r>>16&255,o[s++]=r>>8&255,o[s++]=255&r;return 2==n?(r=h[t.charCodeAt(a)]<<2|h[t.charCodeAt(a+1)]>>4,o[s++]=255&r):1==n&&(r=h[t.charCodeAt(a)]<<10|h[t.charCodeAt(a+1)]<<4|h[t.charCodeAt(a+2)]>>2,o[s++]=r>>8&255,o[s++]=255&r),o}(function(t){var e;if((t=((e=t).trim?e.trim():e.replace(/^\s+|\s+$/g,"")).replace(L,"")).length<2)return"";for(;t.length%4!=0;)t+="=";return t}(t))}function K(t,e,r,n){for(var o=0;o<n&&!(o+r>=e.length||o>=t.length);++o)e[o+r]=t[o];return o}function F(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}function G(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");var e=t.credentials,r=e.clientId,n=e.clientSecret;if(!r||!n)throw new Error("Missing required credentials (clientId, clientSecret)");var o=t.scopes?t.scopes.join(" "):void 0,i=_.from("".concat(r,":").concat(n)).toString("base64"),s=t.oauthUri||"/oauth/token";return{basicAuth:i,url:t.host.replace(/\/$/,"")+s,body:"grant_type=client_credentials".concat(o?"&scope=".concat(o):"")}}function V(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");if(!t.refreshToken)throw new Error("Missing required option (refreshToken)");var e=t.credentials,r=e.clientId,n=e.clientSecret;if(!r||!n)throw new Error("Missing required credentials (clientId, clientSecret)");var o=_.from("".concat(r,":").concat(n)).toString("base64"),i=t.oauthUri||"/oauth/token";return{basicAuth:o,url:t.host.replace(/\/$/,"")+i,body:"grant_type=refresh_token&refresh_token=".concat(encodeURIComponent(t.refreshToken))}}function W(t,e){var r=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};this.status=this.statusCode=this.code=t,this.message=e,Object.assign(this,r),this.name=this.constructor.name,this.constructor.prototype.__proto__=Error.prototype,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}function J(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];W.call.apply(W,[this,0].concat(e))}function $(t,e){return w(w({},e),{},{headers:w(w({},e.headers),{},{Authorization:"Bearer ".concat(t)})})}function Z(t){var n=t.fetcher,i=t.url,s=t.basicAuth,a=t.body,u=t.request,h=t.tokenCache,f=t.requestState,c=t.pendingTasks,l=t.response,p=t.tokenCacheKey,g=t.timeout,d=t.getAbortController,e=t.retryConfig,r=(e=void 0===e?{}:e).retryDelay,y=void 0===r?300:r,o=e.maxRetries,w=void 0===o?10:o,v=e.backoff,E=void 0===v||v,b=e.maxDelay,A=void 0===b?1/0:b;if(g&&!d&&"undefined"==typeof AbortController)throw new Error("`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.");if(g&&"number"!=typeof g)throw new Error("The passed value for timeout is not a number, please provide a timeout of type number.");var m=0;!function o(){var t,e,r;(g||d)&&(e=(d?d():null)||new AbortController),e&&(t=e.signal),g&&(r=setTimeout(function(){e.abort()},g)),n(i,{method:"POST",headers:{Authorization:"Basic ".concat(s),"Content-Length":_.byteLength(a).toString(),"Content-Type":"application/x-www-form-urlencoded"},body:a,signal:t}).then(function(t){return t.ok?t.json().then(function(t){var e,r=t.access_token,n=t.expires_in,o=t.refresh_token,i=(e=n,Date.now()+1e3*e-72e5);h.set({token:r,expirationTime:i,refreshToken:o},p),f.set(!1);var s=c.slice();c=[],s.forEach(function(t){var e=$(r,t.request);t.next(e,t.response)})}):t.text().then(function(t){var e;try{e=JSON.parse(t)}catch(r){}var r=new Error(e?e.message:t);if(e&&(r.body=e),f.set(!1),r.message.includes("is suspended")){if(h.set(null),m<w)return setTimeout(o,function(t,e,r,n,o){var i=1<arguments.length&&void 0!==e?e:6e4,s=4<arguments.length&&void 0!==o?o:1/0;return 3<arguments.length&&void 0!==n&&!n||0===t?i:Math.min(Math.round((Math.random()+1)*i*Math.pow(2,t)),s)}(m,y,w,E,A)),void(m+=1);var n={message:r.body.error,statusCode:r.body.statusCode,originalRequest:u,retryCount:m};l.reject(n)}l.reject(r)})}).catch(function(t){var e;f.set(!1),l&&"function"==typeof l.reject&&l.reject(t),l&&"function"==typeof l.reject&&"aborted"===(null==t?void 0:t.type)&&(e=new J(t.message,{type:t.type,request:u}),l.reject(e))}).finally(function(){clearTimeout(r)})}()}function H(t,e,r){var n=t.request,o=t.response,i=t.url,s=t.basicAuth,a=t.body,u=t.pendingTasks,h=t.requestState,f=t.tokenCache,c=t.tokenCacheKey,l=t.fetch,p=t.timeout,g=t.getAbortController,d=t.retryConfig;if(!l&&"undefined"==typeof fetch)throw new Error("`fetch` is not available. Please pass in `fetch` as an option or have it globally available.");if(l=l||fetch,n.headers&&n.headers.authorization||n.headers&&n.headers.Authorization)e(n,o);else{var y=f.get(c);if(y&&y.token&&Date.now()<y.expirationTime)e($(y.token,n),o);else if(u.push({request:n,response:o,next:e}),!h.get())if(h.set(!0),y&&y.refreshToken&&(!y.token||y.token&&Date.now()>y.expirationTime)){if(!r)throw new Error("Missing required options");Z(w(w({fetcher:l},V(w(w({},r),{},{refreshToken:y.refreshToken}))),{},{tokenCacheKey:c,request:n,tokenCache:f,requestState:h,pendingTasks:u,response:o,timeout:p,getAbortController:g,retryConfig:d}))}else Z({fetcher:l,url:i,basicAuth:s,body:a,request:n,tokenCacheKey:c,tokenCache:f,requestState:h,pendingTasks:u,response:o,timeout:p,getAbortController:g,retryConfig:d})}}function Q(t){var e=t;return{get:function(){return e},set:function(t){return e=t}}}var X=Object.freeze({__proto__:null,MANAGE_PROJECT:"manage_project",MANAGE_PRODUCTS:"manage_products",VIEW_PRODUCTS:"view_products",MANAGE_ORDERS:"manage_orders",VIEW_ORDERS:"view_orders",MANAGE_MY_ORDERS:"manage_my_orders",MANAGE_CUSTOMERS:"manage_customers",VIEW_CUSTOMERS:"view_customers",MANAGE_MY_PROFILE:"manage_my_profile",MANAGE_TYPES:"manage_types",VIEW_TYPES:"view_types",MANAGE_PAYMENTS:"manage_payments",VIEW_PAYMENTS:"view_payments",CREATE_ANONYMOUS_TOKEN:"create_anonymous_token",MANAGE_SUBSCRIPTIONS:"manage_subscriptions"});t.createAuthMiddlewareForAnonymousSessionFlow=function(n){var o=Q({}),i=[],s=Q(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):H(w(w(w({},n),{},{request:t,response:e},function(t){if(!t)throw new Error("Missing required options");if(!t.projectKey)throw new Error("Missing required option (projectKey)");var e=t.projectKey;t.oauthUri=t.oauthUri||"/oauth/".concat(e,"/anonymous/token");var r=G(t);return t.credentials.anonymousId&&(r.body+="&anonymous_id=".concat(t.credentials.anonymousId)),w({},r)}(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r,n)}}},t.createAuthMiddlewareForClientCredentialsFlow=function(o){var i=o.tokenCache||Q({token:"",expirationTime:-1}),s=Q(!1),a=[];return function(n){return function(t,e){var r;t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?n(t,e):H(w(w(w({},o),{},{request:t,response:e},G(o)),{},{pendingTasks:a,requestState:s,tokenCache:i,tokenCacheKey:{clientId:(r=o).credentials.clientId,host:r.host,projectKey:r.projectKey},fetch:o.fetch}),n)}}},t.createAuthMiddlewareForPasswordFlow=function(n){var o=Q({}),i=[],s=Q(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):H(w(w(w({},n),{},{request:t,response:e},function(t){if(!t)throw new Error("Missing required options");if(!t.host)throw new Error("Missing required option (host)");if(!t.projectKey)throw new Error("Missing required option (projectKey)");if(!t.credentials)throw new Error("Missing required option (credentials)");var e=t.credentials,r=e.clientId,n=e.clientSecret,o=e.user,i=t.projectKey;if(!(r&&n&&o))throw new Error("Missing required credentials (clientId, clientSecret, user)");var s=o.username,a=o.password;if(!s||!a)throw new Error("Missing required user credentials (username, password)");var u=(t.scopes||[]).join(" "),h=u?"&scope=".concat(u):"",f=_.from("".concat(r,":").concat(n)).toString("base64"),c=t.oauthUri||"/oauth/".concat(i,"/customers/token");return{basicAuth:f,url:t.host.replace(/\/$/,"")+c,body:"grant_type=password&username=".concat(encodeURIComponent(s),"&password=").concat(encodeURIComponent(a)).concat(h)}}(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r,n)}}},t.createAuthMiddlewareForRefreshTokenFlow=function(n){var o=Q({}),i=[],s=Q(!1);return function(r){return function(t,e){t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization?r(t,e):H(w(w(w({},n),{},{request:t,response:e},V(n)),{},{pendingTasks:i,requestState:s,tokenCache:o,fetch:n.fetch}),r)}}},t.createAuthMiddlewareWithExistingToken=function(){var i=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",s=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};return function(o){return function(t,e){if("string"!=typeof i)throw new Error("authorization must be a string");var r=void 0===s.force||s.force;if(!i||(t.headers&&t.headers.authorization||t.headers&&t.headers.Authorization)&&!1===r)return o(t,e);var n=w(w({},t),{},{headers:w(w({},t.headers),{},{Authorization:i})});return o(n,e)}}},t.scopes=X,Object.defineProperty(t,"__esModule",{value:!0})}); |
@@ -7,3 +7,3 @@ { | ||
"name": "@commercetools/sdk-middleware-auth", | ||
"version": "6.1.4", | ||
"version": "6.2.0", | ||
"description": "Middleware for different authentication flows of commercetools platform API, to use with @commercetools/sdk-client", | ||
@@ -40,5 +40,5 @@ "keywords": [ | ||
"devDependencies": { | ||
"abort-controller": "^3.0.0", | ||
"nock": "12.0.3" | ||
}, | ||
"gitHead": "015a7a95bf67793d0cef452f592ff3ea6818aef3" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # commercetools-sdk-middlware-auth | ||
## Install | ||
### Install | ||
@@ -10,0 +10,0 @@ ```bash |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
161840
3337
2