Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@supertenant/core / src / tracing / clsHooked / context.js
Size: Mime:
// (c) Copyright 2023 Supertenant Ltd. - all rights reserved.
// See LICENSE file in project root for license terms.
"use strict";const util=require("util"),assert=require("assert"),wrapEmitter=require("./emitter-listener"),async_hooks=require("async_hooks"),unset=require("./unset"),CONTEXTS_SYMBOL="instanaClsHooked@contexts";let currentUid=-1;function Namespace(name){this.name=name,this.active=null,this._set=[],this.id=null,this._contexts=new Map,this._indent=0}function getNamespace(name){return process.stNamespaces[name]}function createNamespace(name){assert.ok(name,"namespace must be given a name.");let namespace=new Namespace(name);return namespace.id=currentUid,async_hooks.createHook({init(asyncId,_type,_triggerId,_resource){var triggerId;currentUid=async_hooks.executionAsyncId(),namespace.active?namespace._contexts.set(asyncId,namespace.active):0===currentUid&&(triggerId=async_hooks.triggerAsyncId(),triggerId=namespace._contexts.get(triggerId))&&namespace._contexts.set(asyncId,triggerId)},before(asyncId){currentUid=async_hooks.executionAsyncId(),(asyncId=namespace._contexts.get(asyncId)||namespace._contexts.get(currentUid))&&namespace.enter(asyncId)},after(asyncId){currentUid=async_hooks.executionAsyncId(),(asyncId=namespace._contexts.get(asyncId)||namespace._contexts.get(currentUid))&&namespace.exit(asyncId)},destroy(asyncId){currentUid=async_hooks.executionAsyncId(),namespace._contexts.delete(asyncId)}}).enable(),process.stNamespaces[name]=namespace}function destroyNamespace(name){var namespace=getNamespace(name);assert.ok(namespace,`can't delete nonexistent namespace! "${name}"`),assert.ok(namespace.id,"don't assign to process.stNamespaces directly! "+util.inspect(namespace)),process.stNamespaces[name]=null}function reset(){process.stNamespaces&&Object.keys(process.stNamespaces).forEach(name=>{destroyNamespace(name)}),process.stNamespaces=Object.create(null)}module.exports={getNamespace:getNamespace,createNamespace:createNamespace,destroyNamespace:destroyNamespace,reset:reset},Namespace.prototype.set=function(key,value){var context;if(this.active)return(context=this.active)[key]=value,unset.bind(null,context,key,value);throw new Error("No context available. ns.run() or ns.bind() must be called first.")},Namespace.prototype.get=function(key){if(this.active)return this.active[key]},Namespace.prototype.createContext=function(){var context=Object.create(this.active||Object.prototype);return context._ns_name=this.name,context.id=currentUid,context},Namespace.prototype.createRootContext=function(){var context=Object.create(Object.prototype);return context._ns_name=this.name,context.id=currentUid,context},Namespace.prototype.run=function(fn,ctx){ctx=ctx||this.createContext();this.enter(ctx);try{return fn(ctx),ctx}finally{this.exit(ctx)}},Namespace.prototype.runAndReturn=function(fn,ctx){let value;return this.run(context=>{value=fn(context)},ctx),value},Namespace.prototype.runPromise=function(fn,ctx){let context=ctx||this.createContext();this.enter(context);ctx=fn(context);if(ctx&&ctx.then&&ctx.catch)return ctx.then(result=>(this.exit(context),result)).catch(err=>{throw this.exit(context),err});throw new Error("fn must return a promise.")},Namespace.prototype.runPromiseOrRunAndReturn=function(fn,ctx){let isPromise=!1,valueOrPromise;const context=ctx||this.createContext();this.enter(context);try{if(valueOrPromise=fn(context),isPromise=valueOrPromise&&valueOrPromise.then&&valueOrPromise.catch)return valueOrPromise.then(result=>(this.exit(context),result)).catch(err=>{throw this.exit(context),err})}finally{isPromise||this.exit(context)}return valueOrPromise},Namespace.prototype.bind=function(fn,context){context=context||this.active||this.createContext();let self=this;return function(){self.enter(context);try{return fn.apply(this,arguments)}finally{self.exit(context)}}},Namespace.prototype.bindEmitter=function(emitter){assert.ok(emitter.on&&emitter.addListener&&emitter.emit,"can only bind real EEs");let namespace=this,thisSymbol="context@"+this.name;wrapEmitter(emitter,function(listener){listener&&(listener[CONTEXTS_SYMBOL]||(listener[CONTEXTS_SYMBOL]=Object.create(null)),listener[CONTEXTS_SYMBOL][thisSymbol]={namespace:namespace,context:namespace.active})},function(unwrapped){if(!unwrapped||!unwrapped[CONTEXTS_SYMBOL])return unwrapped;let wrapped=unwrapped,unwrappedContexts=unwrapped[CONTEXTS_SYMBOL];return Object.keys(unwrappedContexts).forEach(name=>{name=unwrappedContexts[name];wrapped=name.namespace.bind(wrapped,name.context)}),wrapped})},Namespace.prototype.enter=function(context){assert.ok(context,"context must be provided for entering"),this._set.push(this.active),this.active=context},Namespace.prototype.exit=function(context){var index;assert.ok(context,"context must be provided for exiting"),this.active===context?(assert.ok(this._set.length,"can't remove top context"),this.active=this._set.pop()):(index=this._set.lastIndexOf(context))<0?assert.ok(0<=index,`context not currently entered; can't exit. 
${util.inspect(this)}
`+util.inspect(context)):(assert.ok(index,"can't remove top context"),this._set.splice(index,1))},process.stNamespaces=process.stNamespaces||{};