import { InvalidActionError } from '@sqltools/util/exception' ; import { IConnectionDriverConstructor , LSContextMap } from '@sqltools/types' ; import { createLogger } from '@sqltools/log/src' ; const log = createLogger ( 'ls-context' ) ; const Context = new Map ( ) ; class DriverMap < V = IConnectionDriverConstructor > extends Map < string , V > { set ( key , value ) { if ( typeof key !== 'string' ) throw 'invalid driver name!' ; key = key . toLowerCase ( ) ; log . info ( { place : 'driver-map' } , 'Driver %s registered!' , key ) ; return super . set ( key , value ) ; } get ( key ) { return super . get ( key . toLowerCase ( ) ) ; } has ( key ) { return super . has ( key . toLowerCase ( ) ) ; } delete ( key ) { return super . delete ( key . toLowerCase ( ) ) ; } } const DriversContext = new DriverMap ( ) ; const handler = { get ( _ , prop ) { if ( prop === 'clear' || prop === 'delete' ) { throw new InvalidActionError ( `Cannot ${ prop } on LSContext!` ) ; } if ( prop === 'drivers' ) return DriversContext ; return Context [ prop ] ; } , set ( ) { throw new InvalidActionError ( 'Cannot set values to extension context directly!' ) ; } , } ; const LSContext = new Proxy < LSContextMap > ( Context as any , handler ) ; export default LSContext ;