• Stars
    star
    189
  • Rank 197,248 (Top 5 %)
  • Language
    C
  • License
    GNU Lesser Genera...
  • Created almost 4 years ago
  • Updated about 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++
10,454
star
2

dagger-browser

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

djinni

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

creative-kit-sample

Sample Apps For Creative Kit
Swift
129
star
5

recycling-center

Bringing reactive dataflow to RecyclerViews
Java
117
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
85
star
7

NextMind

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

login-kit-sample

Sample App For Login Kit
Java
65
star
9

snapml-templates

Jupyter Notebook
55
star
10

storykit

This repo contains the tutorial and sample code of using story kit.
JavaScript
54
star
11

lens-studio-templates

Lens Studio Templates
JavaScript
46
star
12

aws-support-tickets-aggregator

AWS support tickets aggregation service
Python
42
star
13

passport-snapchat

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

express-4.x-passport-snapchat-example

Express 4.x app using Passport for authorization with Snapchat.
JavaScript
34
star
15

bitmoji-kit-sample

Sample Apps for Bitmoji Kit
Swift
24
star
16

camera-kit-unity-sample

Sample app for Camera Kit in Unity
JavaScript
24
star
17

stuffing

Stuff multiple Applications into a single APK
Kotlin
22
star
18

KeyDB-docs

KeyDB's official documentation repo
JavaScript
14
star
19

snapchat-google-tag-manager

Snap Pixel Google Tag Manager Community Template
Smarty
14
star
20

snap-kit-carthage

Mirror for Carthage artifacts in GCS
Shell
11
star
21

ad-kit-ios

Ad Kit SDK for iOS
Objective-C
11
star
22

snap-kit-spm

Objective-C
10
star
23

snap-for-developers-sample

8
star
24

minis-samples

Sample Snap Minis
TypeScript
7
star
25

launchpad

Java
7
star
26

snap-kit-firebase-extensions

TypeScript
7
star
27

business-sdk-python

Python
4
star
28

pixel-server-gateway

JavaScript
4
star
29

business-sdk-go

Go
4
star
30

app-ads-kit-ios

Snap App Ads Kit SDK for iOS
Objective-C
4
star
31

business-sdk-php

PHP
4
star
32

business-sdk-java

Java
3
star
33

business-sdk-ruby

Ruby
3
star
34

capi-google-tag-manager-serverside-tag

Google Tag Manager Community Template For Snap Conversion API
Smarty
3
star
35

camera-kit-flutter-sample

Camera Kit sample app using Flutter wrapper
Swift
2
star