Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kshook

Package Overview
Dependencies
Maintainers
0
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kshook

Ksike Hook library for easy implementation of Event Driven Design

  • 1.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

KsHook: Dynamic Event Handling Library

Ksike Hook library for easy implementation of Event Driven Design. The Hooks pattern is a design pattern that facilitates event-driven programming within a software architecture. It allows you to define events and configure them with subscribers and notifiers. Subscribers listen to specific events and define which notifier should handle them when the event is triggered. This pattern enhances modularity and extensibility in event-driven systems.

This library belong to the Ksike ecosystem:

  • KsMf - Microframework (WEB, REST API, CLI, Proxy, etc)
  • Ksdp - Design Patterns Library (GoF, GRASP, IoC, DI, etc)
  • KsCryp - Cryptographic Library (RSA, JWT, x509, HEX, Base64, Hash, etc)
  • KsHook - Event Driven Library
  • KsEval - Expression Evaluator Library
  • KsWC - Web API deployment Library

For further information, check the following topics:

Quick overview

The library comprises four key components: Hooks, Subscriber, Notifier, and Processor. These components work in tandem to provide a modular and customizable solution for managing events and executing actions based on event triggers.

Hooks

  • Represents the core interface responsible for dynamically loading instances of controller classes for Subscribers, Notifiers, and Processors.
  • Implements patterns such as service locator, inversion of control, strategy, dependency injection, and observers.

Subscriber

  • Manages all data associated with events, defining the type of database to use and the required fields for each subscriber record.
  • Interacts with different data storage sources (memory, databases, files, MySQL, MongoDB, Redis, etc.).
  • Defines the data type and handles interactions with various data storage solutions.

Notifier:

  • Implements actions based on parameters received, including the event name, subscriber content, and specified parameters at the time of event triggering.
  • Executes actions based on event data and subscriber information.

Processor:

  • Optional class defining how a logical expression is evaluated to determine whether the specified Notifier should be executed.
  • Evaluates logical expressions and determines whether to proceed with Notifier execution.
  • Provides flexibility to conditionally execute actions based on configurable logic.

Library Workflow:

  • Initialize an instance of the KsHook, configuring the supported Subscriber, Notifier, and Processor implementations.
  • Configure the list of observers for a specific event, managed by the Subscriber. Each observer record includes the names of the Notifier and Processor to use.
  • Trigger an event through the trigger function, specifying the event name, parameterized data, and the Subscriber or list of Subscribers from which the KsHook obtains the list of subscribers for the triggered event.

Usage Example:

Install the library:

npm install kshook

Load your custom notifier, subscriber, and processors:

const locator = require('./notifier.locator');
const { models, driver, manager } = require('./db');

Register subscribers for each event in the data model

------------------------------------------------------------------------------------------
| id | event  | notifier | value    |  param              | processor | expression       |
------------------------------------------------------------------------------------------
| 1  | logout | locator  | newrelic | null                | null      | null             |
| 2  | login  | locator  | alert    | { failure: dto.inf} | native    | failure EQUAL 11 |
------------------------------------------------------------------------------------------

Extended model useful for workflows:

----------------------------------------------------------------------------------------------------------------------------------------
| id | event  | notifier | value    |  param            | processor | expression       | notifier_alt | value_alt    |  param_alt      |
----------------------------------------------------------------------------------------------------------------------------------------
| 1  | logout | locator  | newrelic | null              | null      | null             | null         | null         | null            |
| 2  | login  | locator  | alert    | {failure:dto.inf} | native    | failure EQUAL 11 | email        | test@srv.com | {note:dto.note} |
----------------------------------------------------------------------------------------------------------------------------------------

For more details check the workflow section.

Configure and run

const KsHook = require('kshook');

// Initialize KsHook instance
const hook = KsHook.get();

// Configure supported components
hook.notifier.set({
    name: "locator",
    target: locator
});
hook.subscriber.get("Model").configure({
    models,
    driver,
    manager
});

// Trigger the 'login' event
const res = hook.trigger({
    subscriber: ["Model"],
    event: "login",
    data: { failure: 10, flow: "6868465468415", step: "STEP-1" }
});

Explore the potential of dynamic event handling with KsHook, a library designed to empower developers with flexibility, extensibility, and seamless event-driven architecture.

Keywords

FAQs

Package last updated on 21 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc