import {logger} from '../../service/logging/global.ts' /** * Base type for session data. */ export type SessionData = { [key: string]: any } /** * Base type for the abstract session interface. */ export default interface SessionInterface { get_key(): string set_key(key: string): void persist(): Promise get_data(): SessionData set_data(data: SessionData): void get_attribute(key: string): any set_attribute(key: string, value: any): void init_session(): Promise } /** * Returns true if the given object is a valid session. * @param what * @return boolean */ export function isSessionInterface(what: any): what is SessionInterface { const name_length_checks = [ { name: 'get_key', length: 0 }, { name: 'set_key', length: 1 }, { name: 'persist', length: 0 }, { name: 'get_data', length: 0 }, { name: 'set_data', length: 1 }, { name: 'get_attribute', length: 1 }, { name: 'set_attribute', length: 2 }, { name: 'init_session', length: 0 }, ] for ( const check of name_length_checks ) { const { name, length } = check if ( !(typeof what[name] === 'function') ) { logger.debug(`Invalid session interface: typeof ${name} is not a function.`) return false } if ( what[name].length !== length ) { logger.debug(`Invalid session interface: method ${name} should expect ${length} arguments, ${what[name].length} actual.`) return false } } return true }