You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.3 KiB
43 lines
1.3 KiB
import {logger} from "../../service/logging/global.ts";
|
|
|
|
export type SessionData = { [key: string]: any }
|
|
|
|
export default interface SessionInterface {
|
|
get_key(): string
|
|
set_key(key: string): void
|
|
persist(): Promise<void>
|
|
get_data(): SessionData
|
|
set_data(data: SessionData): void
|
|
get_attribute(key: string): any
|
|
set_attribute(key: string, value: any): void
|
|
init_session(): Promise<void>
|
|
}
|
|
|
|
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
|
|
}
|