hud-sdk
Version:
Hud's Node SDK
2 lines • 9.6 kB
JavaScript
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r=t();for(var o in r)("object"==typeof exports?exports:e)[o]=r[o]}}(global,()=>(()=>{"use strict";var e={523:e=>{e.exports=require("./logger")},2030:e=>{e.exports=require("./instrument")},2240:e=>{e.exports=require("./core")},2546:e=>{e.exports=require("./lib")},4074:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.setHttpFlow=function(e,t){try{if(x())return;if(!m(e)||!m(t))return void S("setHttpFlow");if(void 0===(0,l.getFlowStateStorage)())return void h("setHttpFlow");const r=(0,l.getFlowState)();if(void 0===r?.httpMetric)return void E("setHttpFlow:no-http-context","setHttpFlow called outside an HTTP flow. Skipping.",s.UserErrorLogs.SetHttpFlowNoHttpContext());const o=t.toLowerCase(),a=t.toUpperCase(),u=(0,s.generateEndpointFlowId)(e,[o]),g=u;d.has(g)||(d.add(g),(0,i.storeEndpointDeclaration)(new n.EndpointDeclaration(u,[o],"manual","",e))),r.httpMetric.setFlowId(u),r.httpMetric.setRequestAttributes({method:a}),r.flowId=u}catch(e){(0,u.limitedLogger)("Failed to set http flow",{msg:e instanceof Error?e.message:void 0,stack:e instanceof Error?e.stack:void 0})}},t.startFlow=function(e,t){try{if(x())return;if(!m(e))return void S("startFlow");if(void 0===(0,l.getFlowStateStorage)())return void h("startFlow");if(b("startFlow",(0,l.getFlowState)()))return;const r=v(t,"startFlow"),o=q(r,e);if(void 0===o)return;const n=(0,a.openGenericFlowMetric)(o,r.type,r.queueAttributes);if(void 0===n)return;(0,a.runInFlow)(o,n,r.type,r.framework,void 0,e,!0)}catch(e){(0,u.limitedLogger)("Failed to start manual flow",{msg:e instanceof Error?e.message:void 0,stack:e instanceof Error?e.stack:void 0})}},t.endFlow=function(e){try{if(x())return;if(void 0===(0,l.getFlowStateStorage)())return void h("endFlow");const t=(0,l.getFlowState)();if(void 0===t||!0!==t.isCustomFlow||void 0===t.genericMetric||void 0===t.flowType||void 0===t.flowFramework)return void E("endFlow:no-start-flow","endFlow called without an active startFlow flow. Skipping.",s.UserErrorLogs.EndFlowNoStartedFlow());"string"==typeof e&&e.length>0&&(0,g.setUserDefinedError)(e),(0,a.finalizeFlow)()}catch(e){(0,u.limitedLogger)("Failed to end manual flow",{msg:e instanceof Error?e.message:void 0,stack:e instanceof Error?e.stack:void 0})}},t.wrapFlow=function(e,t,r){try{if(x())return t;if(!m(e))return S("wrapFlow"),t;if(void 0===(0,l.getFlowStateStorage)())return h("wrapFlow"),t;const o=v(r,"wrapFlow"),n=q(o,e);return void 0===n?t:(0,a.wrapScopedFlow)(n,t,o.type,o.framework,e,!0,()=>b("wrapFlow",(0,l.getFlowState)()),!0,void 0,()=>o.queueAttributes)}catch(e){return(0,u.limitedLogger)("Failed to wrap manual flow",{msg:e instanceof Error?e.message:void 0,stack:e instanceof Error?e.stack:void 0}),t}},t.resetManualFlowState=function(){f.clear(),c.clear(),d.clear()};const o=r(5809),n=r(8528),i=r(8269),a=r(2030),s=r(2546),u=r(523),l=r(7131),g=r(9633),d=new Set,c=new Set,f=new Set,p=new Set(Object.values(n.FlowType)),w=31536e6;function m(e){return"string"==typeof e&&e.length>0}function F(e,t){if(void 0===e)return;if(!(e instanceof Date)||Number.isNaN(e.getTime()))return void function(e){E(`${e}:invalid-enqueue-time`,`${e} called with an invalid enqueueTime (expected a Date). Ignoring it.`,s.UserErrorLogs.ManualFlowApiInvalidEnqueueTime(e))}(t);const r=e.getTime(),o=Date.now()-r;if(o>w)!function(e){E(`${e}:enqueue-time-too-old`,`${e} called with an enqueueTime more than a year in the past. Ignoring it.`,s.UserErrorLogs.ManualFlowApiEnqueueTimeTooOld(e))}(t);else{if(!(o<0))return r;!function(e){E(`${e}:enqueue-time-in-future`,`${e} called with an enqueueTime in the future. Ignoring it.`,s.UserErrorLogs.ManualFlowApiEnqueueTimeInFuture(e))}(t)}}function v(e,t){let r=n.FlowType.Custom,o=!1;void 0!==e?.type&&(p.has(e.type)?r=e.type:(o=!0,function(e){E(`${e}:invalid-flow-type`,`${e} called with an invalid flow type. Treating it as a custom flow.`,s.UserErrorLogs.ManualFlowApiInvalidType(e))}(t)));const i=e?.framework??"custom",a=r===n.FlowType.Queue?{messageCount:y(e?.messageCount,t),enqueueTime:F(e?.enqueueTime,t)}:void 0;return r===n.FlowType.Queue||o||void 0===e?.messageCount&&void 0===e?.enqueueTime||function(e){E(`${e}:queue-attributes-without-queue-type`,`${e} called with messageCount/enqueueTime but type is not "queue". Ignoring them.`,s.UserErrorLogs.ManualFlowApiQueueAttributesWithoutQueueType(e))}(t),{type:r,framework:i,queueAttributes:a}}function y(e,t){if(void 0!==e){if("number"==typeof e&&Number.isInteger(e)&&!(e<=0))return e;!function(e){E(`${e}:invalid-message-count`,`${e} called with an invalid messageCount (expected a positive integer). Ignoring it.`,s.UserErrorLogs.ManualFlowApiInvalidMessageCount(e))}(t)}}function E(e,t,r){c.has(e)||(u.app_logger.warning(t),u.customer_logger.logEntry(r),c.add(e))}function h(e){E(`${e}:before-register`,`${e} called before register. Skipping.`,s.UserErrorLogs.ManualFlowApiBeforeRegister(e))}function S(e){E(`${e}:invalid-flow-name`,`${e} called with an invalid flow name. Skipping.`,s.UserErrorLogs.ManualFlowApiInvalidFlowName(e))}function b(e,t){return void 0!==t?.httpMetric?(function(e){E(`${e}:inside-http-flow`,`${e} called inside an HTTP flow. Skipping.`,s.UserErrorLogs.CustomFlowApiInsideHttpFlow(e))}(e),!0):(void 0!==t?.genericMetric||void 0!==t?.flowId)&&(function(e){E(`${e}:inside-flow`,`${e} called inside another flow. Skipping.`,s.UserErrorLogs.CustomFlowApiInsideFlow(e))}(e),!0)}function x(){return void 0!==globalThis.hudRunningMode&&globalThis.hudRunningMode!==n.HudRunningMode.enabled}function q(e,t){const r=(0,s.generateGenericFlowId)(e.type,e.framework,t),n=o.hudConfig.dynamicConfig.maxCustomFlowIds;if(f.has(r)||!(f.size>=n))return f.add(r),(0,i.storeGenericDeclaration)({flow_id:r,type:e.type,framework:e.framework,name:t}),r;(0,u.limitedLogger)("Manual flow id limit reached",{name:t,maxFlowIds:n})}},5809:e=>{e.exports=require("./config")},7131:e=>{e.exports=require("./wrap")},8269:e=>{e.exports=require("./globalstate")},8528:e=>{e.exports=require("./dto")},9633:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.setUserDefinedError=function(e){const t=(0,u.getInvestigationStorage)();if(void 0===t)return d||(s.app_logger.warning("setFailure - No Storage"),s.customer_logger.logEntry(a.UserErrorLogs.SetFailureBeforeRegister()),d=!0),!1;if(!o.hudConfig.dynamicConfig.enableInvestigation)return!1;const r=t.getStore();if(void 0===r||r.status!==n.InvestigationStatus.Open)return c||(s.app_logger.warning("setFailure called not inside async context. Skipping."),s.customer_logger.logEntry(a.UserErrorLogs.SetFailureNoAsyncContext()),c=!0),!1;if("string"!=typeof e)return s.customer_logger.logEntry(a.UserErrorLogs.SetFailureErrorNotString()),!1;if(0===e.length)return s.customer_logger.logEntry(a.UserErrorLogs.SetFailureErrorEmpty()),!1;let p=e;if(e.length>l&&(s.customer_logger.logEntry(a.UserErrorLogs.SetFailureErrorTooLong()),p=e.substring(0,l)),!(0,i.getUniqueSetFailureErrors)().has(p)){const e=o.hudConfig.dynamicConfig.investigationRateLimits.maxUniqueSetFailureErrors;(0,i.getUniqueSetFailureErrors)().size>=e&&(f||(s.customer_logger.logEntry(a.UserErrorLogs.SetFailureUniqueErrorLimitReached(e)),(0,s.limitedLogger)("setFailure: max unique errors reached",{error:p}),f=!0),p=g),(0,i.getUniqueSetFailureErrors)().add(p)}return r.data.userDefinedError=p,!0};const o=r(5809),n=r(8528),i=r(8269),a=r(2546),s=r(523),u=r(7131),l=1024,g="HUD_MAX_ERRORS_REACHED";let d=!1,c=!1,f=!1}},t={};function r(o){var n=t[o];if(void 0!==n)return n.exports;var i=t[o]={exports:{}};return e[o](i,i.exports,r),i.exports}var o={};return(()=>{var e=o;Object.defineProperty(e,"__esModule",{value:!0}),e.wrapFlow=e.startFlow=e.setHttpFlow=e.endFlow=void 0,e.flush=async function(){try{if(p())return;await(0,n.flush)()}catch(e){(0,s.limitedLogger)("flush() failed. Skipping.",{error:e,stack:e instanceof Error?e.stack:void 0})}},e.setContext=w,e.setFailure=function(e,t){try{if(p())return;if(!(0,l.setUserDefinedError)(e))return;void 0!==t&&w(t)}catch(e){s.app_logger.warning("failed to set failure. Skipping.",{error:e,stack:e instanceof Error&&null!==e?e.stack:void 0})}};const t=r(5809),n=r(2240),i=r(8528),a=r(2546),s=r(523),u=r(7131),l=r(9633);var g=r(4074);Object.defineProperty(e,"endFlow",{enumerable:!0,get:function(){return g.endFlow}}),Object.defineProperty(e,"setHttpFlow",{enumerable:!0,get:function(){return g.setHttpFlow}}),Object.defineProperty(e,"startFlow",{enumerable:!0,get:function(){return g.startFlow}}),Object.defineProperty(e,"wrapFlow",{enumerable:!0,get:function(){return g.wrapFlow}});let d=!1,c=!1,f=!1;function p(){return void 0!==globalThis.hudRunningMode&&globalThis.hudRunningMode!==i.HudRunningMode.enabled}function w(e){try{if(p())return;const r=(0,u.getInvestigationStorage)();if(void 0===r)return void(f||(s.app_logger.warning("HudContext warning: No Storage"),s.customer_logger.logEntry(a.UserErrorLogs.SetContextBeforeRegister()),f=!0));if(!t.hudConfig.dynamicConfig.enableInvestigation)return;const o=r.getStore();if(void 0===o||o.status!==i.InvestigationStatus.Open)return void(d||(s.app_logger.warning("HudContext warning: setContext called not inside async context. Skipping."),s.customer_logger.logEntry(a.UserErrorLogs.SetContextNoAsyncContext()),d=!0));o.data.userContext={...o.data.userContext,...e}}catch(e){c||(s.app_logger.warning("HudContext warning: failed to set context. Skipping.",{error:e,stack:e instanceof Error&&null!==e?e.stack:void 0}),c=!0)}}})(),o})());
//# sourceMappingURL=api.js.map