UNPKG

captcha-igid

Version:
209 lines (174 loc) 7.45 kB
const { DBClass, CustomPersistenceDB, CustomStaticDB } = require('copious-transitions') // global_persistence and global_session -- both purport to have general P2P interfacing capabilities // PersistenceManager address a range of data types. const {MessageRelayer} = require('message-relay-services') // const apiKeys = require(process.cwd() + '/local/api_keys') // // // CONFIGURE THE RELAY message_fowarding IN ORDER TO DETERMINE THE OWNERSHIP OF DATA PATHWAYS // // We may allow the persitence manager to choose the message relay, // or override with the one chosen by the application... // // these are memory based regions accessible by other procs on the same machine. // persistence for these keys are keys lasting between sesssions. // originally persistence had to do with forwarding messages that the system cares about. // contacts will be hanlded differently (using interplanetary contact) // these now have to do with a larger map for a mesh of session awareness const WRITE_OBJECT_MAP_EVERY_INTERVAL = 1000*60*15 // 15 minutes const WRITE_UNUSED_LARGE_ENTRIES_EVERY_INTERVAL = 1000*60*60 // ones an hour // let g_persistence = false let g_ephemeral = false let g_keyValueDB = false let g_keyValueSessions = false async function run_persistence() { // describe the entry point to super storage if ( g_persistence ) { } } async function dropConnections() { if ( g_persistence ) { await g_persistence.client_going_down("captcha") } } // ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- // // store_cache -- in DBClass --> set_key_value --> (M)g_keyValueDB. ..._LRUManager class CaptchaDBClass extends DBClass { // constructor() { // // SessionCacheManager addresses just a few data types, enough to express going sessions let SessionCacheManagerClass = false if ( typeof apiKeys.session_module === "string" ) { // this module may be deprecated... if used, install globally if ( apiKeys.session_module === "global_session" ) { SessionCacheManagerClass = require('global_session') } else { const {SessionCacheManager} = require(apiKeys.session_module) SessionCacheManagerClass = SessionCacheManager } } g_persistence = new SessionCacheManagerClass(apiKeys.persistence,apiKeys.message_relays) // these keys live as long as a session and no longer.. g_ephemeral = new SessionCacheManagerClass(apiKeys.session,apiKeys.session_relay) g_keyValueDB = g_persistence; // leave it to the module to figure out how to connect g_keyValueSessions = g_ephemeral // .get_LRUManager(); // // // pass app messages to the backend // from the default copious-transitions == CustomPersistenceDB, CustomStaticDB let stash_interval = WRITE_OBJECT_MAP_EVERY_INTERVAL // initialize the persistence interface with the same communication as the CategoricalUserManager let message_fowarding = new MessageRelayer(apiKeys.users) let persistenceDB = new CustomPersistenceDB(message_fowarding,stash_interval,'user') // static will provide basic components if not obtained from nxinx stash_interval = WRITE_UNUSED_LARGE_ENTRIES_EVERY_INTERVAL let static_messaging = new MessageRelayer(apiKeys.users) let staticDB = new CustomStaticDB(static_messaging,stash_interval,'user','igid') // // The key value DB is an LRU DB (e.g. such as Redis) that takes on large values of less predictable size. // super(g_keyValueDB,g_keyValueSessions,persistenceDB,staticDB) } // // // initialize(conf) { super.initialize(conf) } // // FileAndRelays will use the MessageRelayer from global_persistence to send the user data to the backend... // // apiKeys = require(process.cwd() + '/local/api_keys') configures it... // // The relayer may talk to a relay service or an endpoint.... (for captcha, it will be user endpoint...) // // the end points are categorical handlers that are tied to message pathways... in this case a 'user' pathway.. // // (see path_handlers.js) // // // async store_user(fdata,all) { if ( G_users_trns.tagged('user') ) { let [udata,axioms] = G_users_trns.update(fdata) // custom user storage (seconday service) clean up fields // let key_key = G_users_trns.kv_store_key() // application key for key-value store from data object let key = udata[key_key] if ( all ) { let ucwid = fdata.ucwid this.derivation_keys[ucwid] = axioms } // store the user in just the LRU await this.store_cache(key,udata,G_users_trns.back_ref()); // this app will use cache to echo persitent storage //return relationship_id } } // // // async fetch_user(fdata,all) { if ( G_users_trns.from_cache() ) { let udata = await this.fetch_user_from_key_value_store(fdata[G_users_trns.kv_store_key()]) if ( udata ) { if ( all ) { udata.public_derivation = this.derivation_keys[udata.ucwid] } return(udata) } } return(false) // never saw this user } // exists // // a bit more severe than fetch... will fail by default when going to persistent storage async exists(collection,post_body) { let query = post_body if ( G_users_trns.tagged(collection) ) { if ( G_users_trns.from_cache() ) { // try to find the user in value storage (local LRU or in-house horizontally scaled LRU) let udata = await this.fetch_user_from_key_value_store(post_body[G_users_trns.kv_store_key()]) if ( udata ) { return(true) } } query = G_users_trns.existence_query(post_body) } // failing a local lookup go to the persistant big database in the sky or nothing return(super.exists(collection,query)) } // ---- ---- ---- ---- ---- ---- ---- ---- update_user(udata) { // let key_key = G_users_trns.kv_store_key() let key = udata[key_key] // this.store_cache(key,udata,G_users_trns.back_ref()); // this app will use cache to echo persitent storage super.update_user(udata,key_key) // use persitent storage (change the remotely stored disk record + local cache (static)) } // // // last_step_initalization() { run_persistence() } store(transition,data) { if ( G_contact_trns.tagged(transition) ) { data.conversation = this.domain // persistence subsystem adds _tracking this.contact_relay.publish('publish-contacts','admin-contacts',data) } } remove(transition,data) { if ( G_contact_trns.tagged(transition) ) { data.conversation = this.domain // the object tracking should be known at this point this.contact_relay.publish('delete-contacts','admin-contacts',data) } } // // // drop() { dropConnections() } // disconnect() { return new Promise((resolve,reject) => { g_persistence.client_going_down() if ( g_keyValueDB.disconnect(true) ) { resolve(true) } else { reject(false) } }) } } // // module.exports = new CaptchaDBClass()