mirror of
https://github.com/Rolands-Laucis/Socio.git
synced 2026-05-21 06:46:19 -06:00
rename session hooks to subs
This commit is contained in:
parent
a70cc7daac
commit
383cf09ac1
2 changed files with 15 additions and 15 deletions
|
|
@ -9,7 +9,7 @@ import type { id, ClientMessageKind, Bit } from './types.js';
|
|||
import type { RateLimit } from './ratelimit.js'
|
||||
import { MapReplacer } from './utils.js';
|
||||
|
||||
type HookObj = {
|
||||
type SubObj = {
|
||||
tables: string[],
|
||||
sql: string,
|
||||
params: object | null,
|
||||
|
|
@ -20,7 +20,7 @@ export type SocioSessionOptions = { logging?: LogHandlerOptions, default_perms?:
|
|||
export class SocioSession extends LogHandler {
|
||||
//private:
|
||||
#ws: WebSocket;
|
||||
#hooks: Map<id, HookObj> = new Map();//msg_id:HookObj
|
||||
#subs: Map<id, SubObj> = new Map();
|
||||
#authenticated = false //usually boolean, but can be any truthy or falsy value to show the state of the session. Can be a token or smth for your own use, bcs the client will only receive a boolean
|
||||
#perms: Map<string, string[]> = new Map(); //verb:[tables strings] keeps a dict of access permissions of verb type and to which tables this session has been granted
|
||||
#destroyed:number = 0;
|
||||
|
|
@ -61,16 +61,16 @@ export class SocioSession extends LogHandler {
|
|||
}
|
||||
|
||||
//TODO this used to be well optimized datastructures back in 0.2.1, but had to simplify down, bcs it gets complicated
|
||||
RegisterHook(tables: string[], id: id, sql:string, params: object | null, rate_limit:RateLimit | null) {
|
||||
if (!this.#hooks.has(id))
|
||||
this.#hooks.set(id, { tables, sql, params, rate_limiter: rate_limit ? new RateLimiter(rate_limit) : null });
|
||||
else throw new E('MSG ID already registered as hook!', tables, id, sql, params);
|
||||
RegisterSub(tables: string[], id: id, sql:string, params: object | null, rate_limit:RateLimit | null) {
|
||||
if (!this.#subs.has(id))
|
||||
this.#subs.set(id, { tables, sql, params, rate_limiter: rate_limit ? new RateLimiter(rate_limit) : null });
|
||||
else throw new E('MSG ID already registered as Sub!', tables, id, sql, params);
|
||||
}
|
||||
UnRegisterHook(id: id): Bit {
|
||||
return this.#hooks.delete(id) ? 1 : 0;
|
||||
UnRegisterSub(id: id): Bit {
|
||||
return this.#subs.delete(id) ? 1 : 0;
|
||||
}
|
||||
GetHooksForTables(tables: string[]=[]){
|
||||
return [...this.#hooks.entries()]
|
||||
GetSubsForTables(tables: string[]=[]){
|
||||
return [...this.#subs.entries()]
|
||||
.filter(([key, h]) => h.tables.some(t => tables.includes(t)))
|
||||
.map(([key, h]) => { return {...h, id:key}})
|
||||
}
|
||||
|
|
@ -113,7 +113,7 @@ export class SocioSession extends LogHandler {
|
|||
this.#destroyed = 0;
|
||||
}
|
||||
|
||||
ClearHooks(){this.#hooks.clear();}
|
||||
ClearSubs(){this.#subs.clear();}
|
||||
CopySessionFrom(old_client:SocioSession){
|
||||
this.#authenticated = old_client.#authenticated;
|
||||
this.#perms = old_client.#perms;
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ export class SocioServer extends LogHandler {
|
|||
//set up hook
|
||||
const tables = ParseQueryTables(data.sql || '');
|
||||
if (tables)
|
||||
client.RegisterHook(tables, data.id as id, data.sql as string, data.params || null, data?.rate_limit || null);
|
||||
client.RegisterSub(tables, data.id as id, data.sql as string, data.params || null, data?.rate_limit || null);
|
||||
|
||||
//send response
|
||||
client.Send('UPD', {
|
||||
|
|
@ -252,7 +252,7 @@ export class SocioServer extends LogHandler {
|
|||
if (await this.#lifecycle_hooks.unsub(client, kind, data))
|
||||
return;
|
||||
|
||||
client.Send('RES', { id: data.id, result: client.UnRegisterHook(data?.unreg_id || '') });
|
||||
client.Send('RES', { id: data.id, result: client.UnRegisterSub(data?.unreg_id || '') });
|
||||
break;
|
||||
case 'SQL':
|
||||
//if the client happens to want to use an endpoint keyname instead of SQL, retrieve the SQL string from a hook call and procede with that.
|
||||
|
|
@ -482,7 +482,7 @@ export class SocioServer extends LogHandler {
|
|||
//or go through each session's every hook and query the DB for its result, then send it to the client
|
||||
try{
|
||||
for (const client of this.#sessions.values()){
|
||||
client.GetHooksForTables(tables).forEach(hook => { //for each hook. GetHooksForTables always returns array. If empty, then the foreach wont run, so each sql guaranteed to have hooks array
|
||||
client.GetSubsForTables(tables).forEach(hook => { //for each hook. GetSubsForTables always returns array. If empty, then the foreach wont run, so each sql guaranteed to have hooks array
|
||||
//rate limit check
|
||||
if (hook?.rate_limiter && hook.rate_limiter.CheckLimit()) return;
|
||||
|
||||
|
|
@ -637,7 +637,7 @@ export class SocioServer extends LogHandler {
|
|||
get methods() { return GetAllMethodNamesOf(this) }
|
||||
|
||||
#ClearClientSessionSubs(client_id:string){
|
||||
this.#sessions.get(client_id)?.ClearHooks(); //clear query subs
|
||||
this.#sessions.get(client_id)?.ClearSubs(); //clear query subs
|
||||
for (const prop of this.#props.values()) { prop.updates.delete(client_id); }; //clear prop subs
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue