• Stars
    star
    202
  • Rank 193,691 (Top 4 %)
  • Language
    C
  • License
    GNU Lesser Genera...
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Javascript Module for KeyDB and Redis

ModJS

A Javascript Module for KeyDB and Redis.

ModJS allows you to extend Redis and KeyDB with new functionality implemented in JavaScript (ES6). ModJS uses the V8 JIT engine so complex scripts can execute significantly faster than their Lua equivalents. In addition ModJS supports many node.js modules offering extensive library support for common tasks.

Quick Start Guide

The fastest way to install ModJS is with docker.

Once installed there are two ways to use ModJS, the first is similar to Lua with the EVALJS Command:

> EVALJS "redis.call('get', 'testkey')"

While EVALJS is quick and easy, a much more powerful method exists in the form of startup scripts. In a startup script you can define your own custom commands and call them from any client as though they were built-in. In addition, these commands can skip the parsing step of EVALJS and so will execute much faster.

Adding a Command

All commands must be defined at the time the module is loaded in a startup script. A startup script is simply a javascript file who's path is passed to ModJS at load time.

Below we create an example script named startup.js which defines a new command named "concat". This command fetches two keys and returns the string concatenation of the two values. We then register this function with the server so that it will be directly callable from Redis or KeyDB clients.

function concat(key1, key2) {
    var str1 = redis.call('get', key1);
    var str2 = redis.call('get', key2);
    return str1 + str2;
}

keydb.register(concat);

Note: The redis and keydb objects may be used interchangebly.

To run this script on startup simply add the path as a module parameter, e.g. loadmodule /path/to/modjs.so /path/to/startup.js

We may now use this command from any client. E.g.:

127.0.0.1:6379> set keyA foo
OK
127.0.0.1:6379> set keyB bar
OK
127.0.0.1:6379> concat keyA keyB
"foobar"

Importing scripts from npm

The above examples were simple enough not to require external libraries, however for more complex tasks it may be desireable to import modules fetched via npm. ModJS implements the require() api with similar semantics to node.js.

In this example we will use the popular lodash library, installed with: npm install loadash. Below we've updated our example script to use the camelCase() function in lodash:

var _ = require("lodash")

function concat(key1, key2) {
    var str1 = redis.call('get', key1);
    var str2 = redis.call('get', key2);
    return _.camelCase(str1 + " " + str2)
}

keydb.register(concat);

The lodash module is imported with require() as it would be in a node.js script. Note that require() will search for modules starting from the working directory of Redis or KeyDB. Once loaded this new script will concatenate the two strings using camel case.

A quick note on compatibility: ModJS does not implement most I/O functionality available in Node. As a result libraries that open files, sockets, etc may not run in ModJS. This limitation is to ensure correct replication behavior of scripts. In the future we may enable an unsafe mode that provides more of this functionality.

Consistency Gurantees and Programming Model

ModJS offers the same consistency gurantees as provided with Lua scripts. Each JS command is executed atomically regardless of whether EVALJS or registered commands are used.

Global variables and functions created in startup sripts are available for subsequent use in registered commands and EVALJS functions. Modules imported via the require() method exist in their own javascript context and may only export via the exports object.

Compiling ModJS

ModJS requires you to first build V8, as a result we recommend using a pre-compiled docker image. However if you wish to compile ModJS first follow the instructions to download and build V8 here: https://v8.dev/docs/build

Once you have built the core V8 library we must then build the monolith with the following command:

/path/to/v8/$ ninja -C out.gn/x64.release.sample v8_monolith 

If V8 compiled successfully you are now ready to build ModJS. ModJS can be built with one line:

make V8_PATH=/path/to/v8

Docker with ModJS

  • Visit the official Docker Repository here: eqalpha/modjs
  • Dockerfiles can be found here

Launch a container with ModJS

KeyDB:

$ sudo docker run eqalpha/modjs

Redis

$ sudo docker run eqalpha/modjs:redis-latest

Launch container bound to host:

$ sudo docker run -p 6379:6379 eqalpha/modjs

Launch with Startup Script

When launching with the docker container you will need to share your script with the docker container by mounting it as a volume to the "scripts" volume in the container:

With Redis:

$ sudo  docker  run  -p  6379:6379  -v  /path/to/startupjs/vol:/scripts  eqalpha/modjs:redis-latest  redis-server  --loadmodule  /usr/lib/keydb/modules/modjs.so  /scripts/startup.js

With KeyDB:

$ sudo  docker  run  -p  6379:6379  -v  /path/to/startupjs/vol:/scripts  eqalpha/modjs  keydb-server  /etc/keydb/keydb.conf  --loadmodule  /usr/lib/keydb/modules/modjs.so  /scripts/startup.js

More Repositories

1

KeyDB

A Multithreaded Fork of Redis
C++
11,327
star
2

dagger-browser

Simple tool for browsing Dagger graphs generated via an SPI plugin
TypeScript
315
star
3

djinni

A tool for generating cross-language type declarations and interface bindings. Djinni's new home is in the Snapchat org.
C++
170
star
4

creative-kit-sample

Sample Apps For Creative Kit
Swift
139
star
5

recycling-center

Bringing reactive dataflow to RecyclerViews
Java
119
star
6

camera-kit-reference

Documentation and samples of Snap's Camera Kit SDK. For the high-level documentation of Camera Kit, please visit: https://docs.snap.com/snap-kit/camera-kit/home
101
star
7

NextMind

Documentation (incl. tutorials, Unity assets and API reference) for the NextMind SDK
87
star
8

snapml-templates

Jupyter Notebook
73
star
9

login-kit-sample

Sample App For Login Kit
Java
72
star
10

camera-kit-react-native

Camera Kit wrapper for React Native
TypeScript
69
star
11

storykit

This repo contains the tutorial and sample code of using story kit.
JavaScript
55
star
12

lens-studio-templates

Lens Studio Templates
JavaScript
49
star
13

passport-snapchat

Snapchat (OAuth 2.0) authorization strategy for PassportJS
TypeScript
43
star
14

aws-support-tickets-aggregator

AWS support tickets aggregation service
Python
42
star
15

express-4.x-passport-snapchat-example

Express 4.x app using Passport for authorization with Snapchat.
JavaScript
41
star
16

camera-kit-unity-sample

Sample app for Camera Kit in Unity
JavaScript
29
star
17

bitmoji-kit-sample

Sample Apps for Bitmoji Kit
Swift
25
star
18

stuffing

Stuff multiple Applications into a single APK
Kotlin
23
star
19

KeyDB-docs

KeyDB's official documentation repo
JavaScript
17
star
20

snapchat-google-tag-manager

Snap Pixel Google Tag Manager Community Template
Smarty
15
star
21

snap-kit-carthage

Mirror for Carthage artifacts in GCS
Shell
11
star
22

ad-kit-ios

Ad Kit SDK for iOS
Objective-C
11
star
23

snap-kit-spm

Objective-C
9
star
24

snap-for-developers-sample

8
star
25

minis-samples

Sample Snap Minis
TypeScript
8
star
26

launchpad

Java
7
star
27

snap-kit-firebase-extensions

TypeScript
7
star
28

app-ads-kit-ios

Snap App Ads Kit SDK for iOS
Objective-C
5
star
29

business-sdk-python

Python
4
star
30

pixel-server-gateway

JavaScript
4
star
31

business-sdk-go

Go
4
star
32

capi-google-tag-manager-serverside-tag

Google Tag Manager Community Template For Snap Conversion API
Smarty
4
star
33

business-sdk-php

PHP
4
star
34

Lens-Studio-Plugins

Open-Sourced Plugins for Lens Studio 5
4
star
35

business-sdk-java

Java
3
star
36

business-sdk-ruby

Ruby
3
star
37

camera-kit-flutter-sample

Camera Kit sample app using Flutter wrapper
Swift
3
star
38

business-sdk-v3-java

Java
2
star
39

ts-inject

Typesafe dependency injection framework for TypeScript projects, providing easy-to-use, maintainable, and scalable code with strong type safety.
TypeScript
1
star