UNPKG

dd-trace

Version:

Datadog APM tracing client for JavaScript

1 lines 16.8 kB
(()=>{"use strict";var t={};t.d=(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},t.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),t.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var e={};t.r(e),t.d(e,{LRUCache:()=>u});let i="object"==typeof performance&&performance&&"function"==typeof performance.now?performance:Date,s=new Set,h="object"==typeof process&&process?process:{},r=(t,e,i,s)=>{"function"==typeof h.emitWarning?h.emitWarning(t,e,i,s):console.error(`[${i}] ${e}: ${t}`)},o=globalThis.AbortController,a=globalThis.AbortSignal;if(void 0===o){a=class{onabort;_onabort=[];reason;aborted=!1;addEventListener(t,e){this._onabort.push(e)}},o=class{constructor(){e()}signal=new a;abort(t){if(!this.signal.aborted){for(let e of(this.signal.reason=t,this.signal.aborted=!0,this.signal._onabort))e(t);this.signal.onabort?.(t)}}};let t=h.env?.LRU_CACHE_IGNORE_AC_WARNING!=="1",e=()=>{t&&(t=!1,r("AbortController is not defined. If using lru-cache in node 14, load an AbortController polyfill from the `node-abort-controller` package. A minimal polyfill is provided for use by LRUCache.fetch(), but it should not be relied upon in other contexts (eg, passing it to other APIs that use AbortController/AbortSignal might have undesirable effects). You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.","NO_ABORT_CONTROLLER","ENOTSUP",e))}}Symbol("type");let l=t=>t&&t===Math.floor(t)&&t>0&&isFinite(t),n=t=>l(t)?t<=256?Uint8Array:t<=65536?Uint16Array:t<=0x100000000?Uint32Array:t<=Number.MAX_SAFE_INTEGER?d:null:null;class d extends Array{constructor(t){super(t),this.fill(0)}}class c{heap;length;static #t=!1;static create(t){let e=n(t);if(!e)return[];c.#t=!0;let i=new c(t,e);return c.#t=!1,i}constructor(t,e){if(!c.#t)throw TypeError("instantiate Stack using Stack.create(n)");this.heap=new e(t),this.length=0}push(t){this.heap[this.length++]=t}pop(){return this.heap[--this.length]}}class u{#e;#i;#s;#h;#r;#o;ttl;ttlResolution;ttlAutopurge;updateAgeOnGet;updateAgeOnHas;allowStale;noDisposeOnSet;noUpdateTTL;maxEntrySize;sizeCalculation;noDeleteOnFetchRejection;noDeleteOnStaleGet;allowStaleOnFetchAbort;allowStaleOnFetchRejection;ignoreFetchAbort;#a;#l;#n;#d;#c;#u;#f;#p;#g;#v;#m;#y;#S;#z;#L;#_;#x;static unsafeExposeInternals(t){return{starts:t.#S,ttls:t.#z,sizes:t.#y,keyMap:t.#n,keyList:t.#d,valList:t.#c,next:t.#u,prev:t.#f,get head(){return t.#p},get tail(){return t.#g},free:t.#v,isBackgroundFetch:e=>t.#k(e),backgroundFetch:(e,i,s,h)=>t.#F(e,i,s,h),moveToTail:e=>t.#T(e),indexes:e=>t.#b(e),rindexes:e=>t.#A(e),isStale:e=>t.#w(e)}}get max(){return this.#e}get maxSize(){return this.#i}get calculatedSize(){return this.#l}get size(){return this.#a}get fetchMethod(){return this.#r}get memoMethod(){return this.#o}get dispose(){return this.#s}get disposeAfter(){return this.#h}constructor(t){const{max:e=0,ttl:i,ttlResolution:h=1,ttlAutopurge:o,updateAgeOnGet:a,updateAgeOnHas:d,allowStale:f,dispose:p,disposeAfter:g,noDisposeOnSet:v,noUpdateTTL:m,maxSize:y=0,maxEntrySize:S=0,sizeCalculation:z,fetchMethod:L,memoMethod:_,noDeleteOnFetchRejection:x,noDeleteOnStaleGet:k,allowStaleOnFetchRejection:F,allowStaleOnFetchAbort:T,ignoreFetchAbort:b}=t;if(0!==e&&!l(e))throw TypeError("max option must be a nonnegative integer");const A=e?n(e):Array;if(!A)throw Error("invalid max value: "+e);if(this.#e=e,this.#i=y,this.maxEntrySize=S||this.#i,this.sizeCalculation=z,this.sizeCalculation){if(!this.#i&&!this.maxEntrySize)throw TypeError("cannot set sizeCalculation without setting maxSize or maxEntrySize");if("function"!=typeof this.sizeCalculation)throw TypeError("sizeCalculation set to non-function")}if(void 0!==_&&"function"!=typeof _)throw TypeError("memoMethod must be a function if defined");if(this.#o=_,void 0!==L&&"function"!=typeof L)throw TypeError("fetchMethod must be a function if specified");if(this.#r=L,this.#_=!!L,this.#n=new Map,this.#d=Array(e).fill(void 0),this.#c=Array(e).fill(void 0),this.#u=new A(e),this.#f=new A(e),this.#p=0,this.#g=0,this.#v=c.create(e),this.#a=0,this.#l=0,"function"==typeof p&&(this.#s=p),"function"==typeof g?(this.#h=g,this.#m=[]):(this.#h=void 0,this.#m=void 0),this.#L=!!this.#s,this.#x=!!this.#h,this.noDisposeOnSet=!!v,this.noUpdateTTL=!!m,this.noDeleteOnFetchRejection=!!x,this.allowStaleOnFetchRejection=!!F,this.allowStaleOnFetchAbort=!!T,this.ignoreFetchAbort=!!b,0!==this.maxEntrySize){if(0!==this.#i&&!l(this.#i))throw TypeError("maxSize must be a positive integer if specified");if(!l(this.maxEntrySize))throw TypeError("maxEntrySize must be a positive integer if specified");this.#E()}if(this.allowStale=!!f,this.noDeleteOnStaleGet=!!k,this.updateAgeOnGet=!!a,this.updateAgeOnHas=!!d,this.ttlResolution=l(h)||0===h?h:1,this.ttlAutopurge=!!o,this.ttl=i||0,this.ttl){if(!l(this.ttl))throw TypeError("ttl must be a positive integer if specified");this.#M()}if(0===this.#e&&0===this.ttl&&0===this.#i)throw TypeError("At least one of max, maxSize, or ttl is required");if(!this.ttlAutopurge&&!this.#e&&!this.#i){const t="LRU_CACHE_UNBOUNDED";s.has(t)||(s.add(t),r("TTL caching without ttlAutopurge, max, or maxSize can result in unbounded memory consumption.","UnboundedCacheWarning",t,u))}}getRemainingTTL(t){return this.#n.has(t)?1/0:0}#M(){let t=new d(this.#e),e=new d(this.#e);this.#z=t,this.#S=e,this.#O=(s,h,r=i.now())=>{if(e[s]=0!==h?r:0,t[s]=h,0!==h&&this.ttlAutopurge){let t=setTimeout(()=>{this.#w(s)&&this.#D(this.#d[s],"expire")},h+1);t.unref&&t.unref()}},this.#C=s=>{e[s]=0!==t[s]?i.now():0},this.#W=(i,r)=>{if(t[r]){let o=t[r],a=e[r];if(!o||!a)return;i.ttl=o,i.start=a,i.now=s||h();let l=i.now-a;i.remainingTTL=o-l}};let s=0,h=()=>{let t=i.now();if(this.ttlResolution>0){s=t;let e=setTimeout(()=>s=0,this.ttlResolution);e.unref&&e.unref()}return t};this.getRemainingTTL=i=>{let r=this.#n.get(i);if(void 0===r)return 0;let o=t[r],a=e[r];return o&&a?o-((s||h())-a):1/0},this.#w=i=>{let r=e[i],o=t[i];return!!o&&!!r&&(s||h())-r>o}}#C=()=>{};#W=()=>{};#O=()=>{};#w=()=>!1;#E(){let t=new d(this.#e);this.#l=0,this.#y=t,this.#R=e=>{this.#l-=t[e],t[e]=0},this.#I=(t,e,i,s)=>{if(this.#k(e))return 0;if(!l(i))if(s){if("function"!=typeof s)throw TypeError("sizeCalculation must be a function");if(!l(i=s(e,t)))throw TypeError("sizeCalculation return invalid (expect positive integer)")}else throw TypeError("invalid size value (must be positive integer). When maxSize or maxEntrySize is used, sizeCalculation or size must be set.");return i},this.#B=(e,i,s)=>{if(t[e]=i,this.#i){let i=this.#i-t[e];for(;this.#l>i;)this.#U(!0)}this.#l+=t[e],s&&(s.entrySize=i,s.totalCalculatedSize=this.#l)}}#R=t=>{};#B=(t,e,i)=>{};#I=(t,e,i,s)=>{if(i||s)throw TypeError("cannot set size without setting maxSize or maxEntrySize on cache");return 0};*#b({allowStale:t=this.allowStale}={}){if(this.#a)for(let e=this.#g;this.#j(e)&&((t||!this.#w(e))&&(yield e),e!==this.#p);)e=this.#f[e]}*#A({allowStale:t=this.allowStale}={}){if(this.#a)for(let e=this.#p;this.#j(e)&&((t||!this.#w(e))&&(yield e),e!==this.#g);)e=this.#u[e]}#j(t){return void 0!==t&&this.#n.get(this.#d[t])===t}*entries(){for(let t of this.#b())void 0===this.#c[t]||void 0===this.#d[t]||this.#k(this.#c[t])||(yield[this.#d[t],this.#c[t]])}*rentries(){for(let t of this.#A())void 0===this.#c[t]||void 0===this.#d[t]||this.#k(this.#c[t])||(yield[this.#d[t],this.#c[t]])}*keys(){for(let t of this.#b()){let e=this.#d[t];void 0===e||this.#k(this.#c[t])||(yield e)}}*rkeys(){for(let t of this.#A()){let e=this.#d[t];void 0===e||this.#k(this.#c[t])||(yield e)}}*values(){for(let t of this.#b())void 0===this.#c[t]||this.#k(this.#c[t])||(yield this.#c[t])}*rvalues(){for(let t of this.#A())void 0===this.#c[t]||this.#k(this.#c[t])||(yield this.#c[t])}[Symbol.iterator](){return this.entries()}[Symbol.toStringTag]="LRUCache";find(t,e={}){for(let i of this.#b()){let s=this.#c[i],h=this.#k(s)?s.__staleWhileFetching:s;if(void 0!==h&&t(h,this.#d[i],this))return this.get(this.#d[i],e)}}forEach(t,e=this){for(let i of this.#b()){let s=this.#c[i],h=this.#k(s)?s.__staleWhileFetching:s;void 0!==h&&t.call(e,h,this.#d[i],this)}}rforEach(t,e=this){for(let i of this.#A()){let s=this.#c[i],h=this.#k(s)?s.__staleWhileFetching:s;void 0!==h&&t.call(e,h,this.#d[i],this)}}purgeStale(){let t=!1;for(let e of this.#A({allowStale:!0}))this.#w(e)&&(this.#D(this.#d[e],"expire"),t=!0);return t}info(t){let e=this.#n.get(t);if(void 0===e)return;let s=this.#c[e],h=this.#k(s)?s.__staleWhileFetching:s;if(void 0===h)return;let r={value:h};if(this.#z&&this.#S){let t=this.#z[e],s=this.#S[e];t&&s&&(r.ttl=t-(i.now()-s),r.start=Date.now())}return this.#y&&(r.size=this.#y[e]),r}dump(){let t=[];for(let e of this.#b({allowStale:!0})){let s=this.#d[e],h=this.#c[e],r=this.#k(h)?h.__staleWhileFetching:h;if(void 0===r||void 0===s)continue;let o={value:r};if(this.#z&&this.#S){o.ttl=this.#z[e];let t=i.now()-this.#S[e];o.start=Math.floor(Date.now()-t)}this.#y&&(o.size=this.#y[e]),t.unshift([s,o])}return t}load(t){for(let[e,s]of(this.clear(),t)){if(s.start){let t=Date.now()-s.start;s.start=i.now()-t}this.set(e,s.value,s)}}set(t,e,i={}){if(void 0===e)return this.delete(t),this;let{ttl:s=this.ttl,start:h,noDisposeOnSet:r=this.noDisposeOnSet,sizeCalculation:o=this.sizeCalculation,status:a}=i,{noUpdateTTL:l=this.noUpdateTTL}=i,n=this.#I(t,e,i.size||0,o);if(this.maxEntrySize&&n>this.maxEntrySize)return a&&(a.set="miss",a.maxEntrySizeExceeded=!0),this.#D(t,"set"),this;let d=0===this.#a?void 0:this.#n.get(t);if(void 0===d)d=0===this.#a?this.#g:0!==this.#v.length?this.#v.pop():this.#a===this.#e?this.#U(!1):this.#a,this.#d[d]=t,this.#c[d]=e,this.#n.set(t,d),this.#u[this.#g]=d,this.#f[d]=this.#g,this.#g=d,this.#a++,this.#B(d,n,a),a&&(a.set="add"),l=!1;else{this.#T(d);let i=this.#c[d];if(e!==i){if(this.#_&&this.#k(i)){i.__abortController.abort(Error("replaced"));let{__staleWhileFetching:e}=i;void 0!==e&&!r&&(this.#L&&this.#s?.(e,t,"set"),this.#x&&this.#m?.push([e,t,"set"]))}else!r&&(this.#L&&this.#s?.(i,t,"set"),this.#x&&this.#m?.push([i,t,"set"]));if(this.#R(d),this.#B(d,n,a),this.#c[d]=e,a){a.set="replace";let t=i&&this.#k(i)?i.__staleWhileFetching:i;void 0!==t&&(a.oldValue=t)}}else a&&(a.set="update")}if(0===s||this.#z||this.#M(),this.#z&&(l||this.#O(d,s,h),a&&this.#W(a,d)),!r&&this.#x&&this.#m){let t,e=this.#m;for(;t=e?.shift();)this.#h?.(...t)}return this}pop(){try{for(;this.#a;){let t=this.#c[this.#p];if(this.#U(!0),this.#k(t)){if(t.__staleWhileFetching)return t.__staleWhileFetching}else if(void 0!==t)return t}}finally{if(this.#x&&this.#m){let t,e=this.#m;for(;t=e?.shift();)this.#h?.(...t)}}}#U(t){let e=this.#p,i=this.#d[e],s=this.#c[e];return this.#_&&this.#k(s)?s.__abortController.abort(Error("evicted")):(this.#L||this.#x)&&(this.#L&&this.#s?.(s,i,"evict"),this.#x&&this.#m?.push([s,i,"evict"])),this.#R(e),t&&(this.#d[e]=void 0,this.#c[e]=void 0,this.#v.push(e)),1===this.#a?(this.#p=this.#g=0,this.#v.length=0):this.#p=this.#u[e],this.#n.delete(i),this.#a--,e}has(t,e={}){let{updateAgeOnHas:i=this.updateAgeOnHas,status:s}=e,h=this.#n.get(t);if(void 0!==h){let t=this.#c[h];if(this.#k(t)&&void 0===t.__staleWhileFetching)return!1;if(!this.#w(h))return i&&this.#C(h),s&&(s.has="hit",this.#W(s,h)),!0;s&&(s.has="stale",this.#W(s,h))}else s&&(s.has="miss");return!1}peek(t,e={}){let{allowStale:i=this.allowStale}=e,s=this.#n.get(t);if(void 0===s||!i&&this.#w(s))return;let h=this.#c[s];return this.#k(h)?h.__staleWhileFetching:h}#F(t,e,i,s){let h=void 0===e?void 0:this.#c[e];if(this.#k(h))return h;let r=new o,{signal:a}=i;a?.addEventListener("abort",()=>r.abort(a.reason),{signal:r.signal});let l={signal:r.signal,options:i,context:s},n=(s,h=!1)=>{let{aborted:o}=r.signal,a=i.ignoreFetchAbort&&void 0!==s;return(i.status&&(o&&!h?(i.status.fetchAborted=!0,i.status.fetchError=r.signal.reason,a&&(i.status.fetchAbortIgnored=!0)):i.status.fetchResolved=!0),!o||a||h)?(this.#c[e]===u&&(void 0===s?u.__staleWhileFetching?this.#c[e]=u.__staleWhileFetching:this.#D(t,"fetch"):(i.status&&(i.status.fetchUpdated=!0),this.set(t,s,l.options))),s):d(r.signal.reason)},d=s=>{let{aborted:h}=r.signal,o=h&&i.allowStaleOnFetchAbort,a=o||i.allowStaleOnFetchRejection,l=a||i.noDeleteOnFetchRejection;if(this.#c[e]===u&&(l&&void 0!==u.__staleWhileFetching?o||(this.#c[e]=u.__staleWhileFetching):this.#D(t,"fetch")),a)return i.status&&void 0!==u.__staleWhileFetching&&(i.status.returnedStale=!0),u.__staleWhileFetching;if(u.__returned===u)throw s},c=(e,s)=>{let o=this.#r?.(t,h,l);o&&o instanceof Promise&&o.then(t=>e(void 0===t?void 0:t),s),r.signal.addEventListener("abort",()=>{(!i.ignoreFetchAbort||i.allowStaleOnFetchAbort)&&(e(void 0),i.allowStaleOnFetchAbort&&(e=t=>n(t,!0)))})};i.status&&(i.status.fetchDispatched=!0);let u=new Promise(c).then(n,t=>(i.status&&(i.status.fetchRejected=!0,i.status.fetchError=t),d(t))),f=Object.assign(u,{__abortController:r,__staleWhileFetching:h,__returned:void 0});return void 0===e?(this.set(t,f,{...l.options,status:void 0}),e=this.#n.get(t)):this.#c[e]=f,f}#k(t){return!!this.#_&&!!t&&t instanceof Promise&&t.hasOwnProperty("__staleWhileFetching")&&t.__abortController instanceof o}async fetch(t,e={}){let{allowStale:i=this.allowStale,updateAgeOnGet:s=this.updateAgeOnGet,noDeleteOnStaleGet:h=this.noDeleteOnStaleGet,ttl:r=this.ttl,noDisposeOnSet:o=this.noDisposeOnSet,size:a=0,sizeCalculation:l=this.sizeCalculation,noUpdateTTL:n=this.noUpdateTTL,noDeleteOnFetchRejection:d=this.noDeleteOnFetchRejection,allowStaleOnFetchRejection:c=this.allowStaleOnFetchRejection,ignoreFetchAbort:u=this.ignoreFetchAbort,allowStaleOnFetchAbort:f=this.allowStaleOnFetchAbort,context:p,forceRefresh:g=!1,status:v,signal:m}=e;if(!this.#_)return v&&(v.fetch="get"),this.get(t,{allowStale:i,updateAgeOnGet:s,noDeleteOnStaleGet:h,status:v});let y={allowStale:i,updateAgeOnGet:s,noDeleteOnStaleGet:h,ttl:r,noDisposeOnSet:o,size:a,sizeCalculation:l,noUpdateTTL:n,noDeleteOnFetchRejection:d,allowStaleOnFetchRejection:c,allowStaleOnFetchAbort:f,ignoreFetchAbort:u,status:v,signal:m},S=this.#n.get(t);if(void 0===S){v&&(v.fetch="miss");let e=this.#F(t,S,y,p);return e.__returned=e}{let e=this.#c[S];if(this.#k(e)){let t=i&&void 0!==e.__staleWhileFetching;return v&&(v.fetch="inflight",t&&(v.returnedStale=!0)),t?e.__staleWhileFetching:e.__returned=e}let h=this.#w(S);if(!g&&!h)return v&&(v.fetch="hit"),this.#T(S),s&&this.#C(S),v&&this.#W(v,S),e;let r=this.#F(t,S,y,p),o=void 0!==r.__staleWhileFetching&&i;return v&&(v.fetch=h?"stale":"refresh",o&&h&&(v.returnedStale=!0)),o?r.__staleWhileFetching:r.__returned=r}}async forceFetch(t,e={}){let i=await this.fetch(t,e);if(void 0===i)throw Error("fetch() returned undefined");return i}memo(t,e={}){let i=this.#o;if(!i)throw Error("no memoMethod provided to constructor");let{context:s,forceRefresh:h,...r}=e,o=this.get(t,r);if(!h&&void 0!==o)return o;let a=i(t,o,{options:r,context:s});return this.set(t,a,r),a}get(t,e={}){let{allowStale:i=this.allowStale,updateAgeOnGet:s=this.updateAgeOnGet,noDeleteOnStaleGet:h=this.noDeleteOnStaleGet,status:r}=e,o=this.#n.get(t);if(void 0!==o){let e=this.#c[o],a=this.#k(e);return(r&&this.#W(r,o),this.#w(o))?(r&&(r.get="stale"),a)?(r&&i&&void 0!==e.__staleWhileFetching&&(r.returnedStale=!0),i?e.__staleWhileFetching:void 0):(h||this.#D(t,"expire"),r&&i&&(r.returnedStale=!0),i?e:void 0):(r&&(r.get="hit"),a)?e.__staleWhileFetching:(this.#T(o),s&&this.#C(o),e)}r&&(r.get="miss")}#G(t,e){this.#f[e]=t,this.#u[t]=e}#T(t){t!==this.#g&&(t===this.#p?this.#p=this.#u[t]:this.#G(this.#f[t],this.#u[t]),this.#G(this.#g,t),this.#g=t)}delete(t){return this.#D(t,"delete")}#D(t,e){let i=!1;if(0!==this.#a){let s=this.#n.get(t);if(void 0!==s)if(i=!0,1===this.#a)this.#N(e);else{this.#R(s);let i=this.#c[s];if(this.#k(i)?i.__abortController.abort(Error("deleted")):(this.#L||this.#x)&&(this.#L&&this.#s?.(i,t,e),this.#x&&this.#m?.push([i,t,e])),this.#n.delete(t),this.#d[s]=void 0,this.#c[s]=void 0,s===this.#g)this.#g=this.#f[s];else if(s===this.#p)this.#p=this.#u[s];else{let t=this.#f[s];this.#u[t]=this.#u[s];let e=this.#u[s];this.#f[e]=this.#f[s]}this.#a--,this.#v.push(s)}}if(this.#x&&this.#m?.length){let t,e=this.#m;for(;t=e?.shift();)this.#h?.(...t)}return i}clear(){return this.#N("delete")}#N(t){for(let e of this.#A({allowStale:!0})){let i=this.#c[e];if(this.#k(i))i.__abortController.abort(Error("deleted"));else{let s=this.#d[e];this.#L&&this.#s?.(i,s,t),this.#x&&this.#m?.push([i,s,t])}}if(this.#n.clear(),this.#c.fill(void 0),this.#d.fill(void 0),this.#z&&this.#S&&(this.#z.fill(0),this.#S.fill(0)),this.#y&&this.#y.fill(0),this.#p=0,this.#g=0,this.#v.length=0,this.#l=0,this.#a=0,this.#x&&this.#m){let t,e=this.#m;for(;t=e?.shift();)this.#h?.(...t)}}}module.exports=e})();