A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

Overview

RESPRESS

enter image description here A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast. Using this library you can spin off a RESP-TCP server very fast, the server can be connected to using any redis compatible library or tool like redis-cli or ioredis . You can find a server example in the example folder.

Installation

Simply install the library using

npm install respress --save

to use it just require the library

Getting Started

let  RESPRESS = require("respress");
let  app = new  RESPRESS();

Now you are ready to spin off your server, it is very similar to express, the idea was to keep it simple for developers to implement RESP in their code.

let  RESPRESS = require("respress");
let  app = new  RESPRESS();

//Register a command PING which accepts multiple arguments and is returned 
//in req.params.message
app.cmd("PING [message...]", (req, res) => {
    if (req.params.message) {
        res.send(req.params.message);
    }
    else {
        res.send("PONG");
    }
})

//Listen to client connections through the events provided 
app.on("clientConnect", (client) => { console.log("NEW CLIENT YAY!", client.id) });

//Start server and listen to port 9001
app.listen({port:9001} , (server) => {});

Now you can use the server, lets use redis-cli to connect

$ redis-cli -p 9001 -a letMeIn!
127.0.0.1:9001> ping
"PONG"
127.0.0.1:9001> 

Yes, it is that simple. Now lets get into details.

Take a look at the example server, will make your life easier. Link to example

Registering Commands

As seen before to register commands its simple. There are many things you can use to register commands and will be described below.

A simple command with no arguments

The command "COMMAND" is registered, once it is called by the client the handler function is called returning req and res. Req can be used to capture parsed commands and arguments where as res can be used to respond to the client.

app.cmd("COMMAND", (req, res) => {
    res.send(app.cmds.commandList)
})

A Command with arguments

Positional arguments can be placed when registering a command, examples below;

//Register a required argument called subcommand, use <> to register required
// req.params.subcommand returns a single string 
app.cmd("CLIENT <subcommand>", (req, res) => {
    if (req.params.subcommand.toLowerCase() == "getname") {
        res.send(req.client);
    }
    else {
        res.send(new Error("Missing correct subcommand"));
    }
})

//Register multiple series of messages as optional, use [] to define its optional and
// use .... after the argument name to indicate that it can be multiple messages, this 
// is returned to req.params.message as an array
app.cmd("PING [message...]", (req, res) => {
    if (req.params.message) {
        res.send(req.params.message);
    }
    else {
        res.send("PONG")
    }

})

//Using the * will be the default handler of commands that are not defined. req.params._ 
//will return an array of all the params tokenized in an array for your usage.
app.cmd("*",(req,res)=>{
    res.send("A Default execution for the command")
})

Authentication

To register an authentication handler all you need to do is use the following code below. the usage of app.auth method to register an authentication handler. req.params.username and req.params.password will be returned for authentication. res.auth() needs to be called with true or false to indicate if authentication was successful.

app.auth((req, res) => {
    if (req.params.password == "letMeIn!") {
        res.auth(true);
    }
    else {
        //You shall not pass
        res.auth(false);
    }
})

Sending Responses

To send responses all command handlers have a res.send() argument that can be used to respond to the clients command request. You can send different response types as below;

res.send("You can send a simple string");
res.send(["Or","send","an", "array"]);
res.send({obj:"An object can be sent and will be stringified"})
res.send(new Error("Error: can be sent and managed correctly by client");
res.sent(()=>{console.log("Sending a function, it will be sent as a string definition."})

Command Argument Definitions

You can use multiple methods to register your positional arguments, below are some examples;

//<argument> is required and [argument] is optional 
get <source> [proxy]'

//Use an alias where <username|email> will populate 
//req.params with both .username and .email with the same value
get <username|email> [password]

//Variadic arguments all to have multiple messages associated to a name. 
// Use ... to indicated variadic arguments. 
// req.params.files will be an array with all messages assigned.
download <url> [files..]

API

The apis can be found in the DOCs folder. Links as follows;

Contribution

Contributions are welcome, if you are interested please let me know and would be happy to get you on-board. Needs help with

  • Unit Tests
  • Code Refactor / Cleanup
  • Add middleware for pre-request and pre-response
You might also like...

Define tool for JS/TS

JSDef A Define tool for js/ts Install npm i jsdefn HOW TO Create Defs import { JSDef } from 'jsdefn' const $ = JSDef({ "A": "apple" }) Get define

Apr 18, 2022

This is a place to define better practices in code.

Better Practices Categories Angular General Programming GitHub Pages JavaScript Naming Conventions React Influence This guide is heavily influenced by

Sep 3, 2022

Pfapi plugin uses local and redis caches to achieve single digit milliseconds on average api response time.

Pfapi plugin uses local and redis caches to achieve single digit milliseconds on average api response time.

Strapi plugin pfapi Pfapi plugin provides configurable, secure and fast API services. APIs are configurable through the admin panel with components an

Sep 17, 2022

Binary-encoded serialization of JavaScript objects with generator-based parser and serializer

YaBSON Schemaless binary-encoded serialization of JavaScript objects with generator-based parser and serializer This library is designed to transfer l

Aug 9, 2022

Glorious Binary Serialization and Deserialization for TypeScript.

Glorious Binary Serialization and Deserialization for TypeScript.

Glorious SerDes for TypeScript The library you can rely on, For binary serialization and deserialization, In Node, Deno, and the Web environment, Whic

Dec 11, 2022

Serialize arbitrary NodeJS closures and customize serialization behavior.

Closure Serializer This is a fork of the Pulumi Closure Serializer. @pulumi/pulumi. Motivation Functionless allows developers to write cloud applicati

Jul 19, 2022

⛑️ JSON serialization should never fail

⛑️ JSON serialization should never fail. Features Prevent JSON.serialize() from: Throwing Changing types Filtering or transforming values unexpectedly

Dec 15, 2022

FIXYL is a tool for testing and verifying software that uses the FIX protocol.

FIXYL is a tool for testing and verifying software that uses the FIX protocol.

FIXYL FIXYL is a tool for testing and verifying software that uses the FIX protocol. It allows establishing FIX sessions and exchanging (and manipulat

Dec 29, 2022
Owner
Yousef Wadi
Yousef Wadi
Javascript-testing-practical-approach-2021-course-v3 - Javascript Testing, a Practical Approach (v3)

Javascript Testing, a Practical Approach Description This is the reference repository with all the contents and the examples of the "Javascript Testin

Stefano Magni 2 Nov 14, 2022
Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime.

Bun Bakery Bun-Bakery is a web framework for Bun. It uses a file based router in style like svelte-kit. No need to define routes during runtime. Quick

Dennis Dudek 44 Dec 6, 2022
A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol and uWebSockets.js

speedymppserver A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol

Lapis 4 Oct 14, 2022
RedisInsight - Developer GUI for Redis, by Redis.

RedisInsight - Developer GUI for Redis, by Redis. RedisInsight is a visual tool that provides capabilities to design, develop and optimize your Redis

null 2.3k Dec 31, 2022
Making webshell and terminal supports trzsz ( trz / tsz ), which similar to rz / sz, and compatible with tmux.

trzsz.js Making webshell and terminal supports trzsz ( trz / tsz ), which similar to ( rz / sz ), and compatible with tmux. Why? Considering laptop ->

null 95 Jan 3, 2023
An adapter where you can define which function to run

Switch Functions An adapter where you can define which function to run Installation This is a Node.js module available through the npm registry. Befor

Badass Team 2 Jun 17, 2022
This package enables you to define your routes using the flat-routes convention.

Remix Flat Routes This package enables you to define your routes using the flat-routes convention. This is based on the gist by Ryan Florence ?? Insta

Kiliman 180 Jan 3, 2023
A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

remote-map-cache A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fa

Tomato6966 8 Oct 31, 2022
superserial provides serialization in any way you can imagine.

superserial After data transfer, when the object needs to be restored, JSON has many limitations. It does not support values such as Infinity and NaN,

DenoStack 24 Dec 23, 2022
Serialization library for data-oriented design structures in JavaScript

Data-oriented Serialization for SoA/AoA A zero-dependency serialization library for data-oriented design structures like SoA (Structure of Arrays) and

null 11 Sep 27, 2022