npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-lua-helper

v0.2.0

Published

A helper package to load lua scripts for redis featuring a #include macro

Downloads

8

Readme

Purpose

This small package is intended to help out with managing Lua scripts for redis as those scripts need to be sent via the client and can profit from a wrapper. More importantly, this wrapper allows for scripts to include code that is in other script files via #include 'file.lua' which promotes code reusability.

Note that this package is client agnostic and still requires you to create a wrapper for your client. You may also want to write the created code to new files for debugging purposes.

Usage

Install this module via npm..

npm install redis-lua-helper

.. and include is as such:

var RedisLuaHelper = require('redis-lua-helper');
var rlh = RedisLuaHelper(options);

#include macro

In your lua script files, you may use a #include 'filename' macro. This macro will include a different script at the place where this macro was placed. File paths are relative to the current file. Included files may contain macros as well, but circular dependencies will raise errors.

Example of one Lua script including the other via #include:

File foo.lua:

print('This is foo!')
#include 'bar'

File bar.lua:

print('This is bar!')

Processed contents when loading foo script:

print('This is foo!')

-- #include bar:
print('This is bar!')
-- End of bar

API

RLH ( options | scriptPath )

Create a new instance of the RedisLuaHelper class, expects either the path of your script folder or a full configuration object. Below are all possible options and there default values:

var RedisLuaHelper = require('redis-lua-helper');

// using an options object
var rlh = RedisLuaHelper({
	'root': 		__dirname + '/scripts',
	'macro':		'#include',
	'extension':	'lua',
	'encoding':		'utf8'
});

// just provide the script path
var rlh2 = RedisLuaHelper(__dirname + '/scripts');

The root field defines the path relative from which scripts will be loaded. The extension forces that script files may only be loaded if they share that extension. File encoding defaults to utf8 but can be overridden. The macro option allows you to override the default #include 'file' to something else.

RLH#load ( fileName1, fileName2, .. callback )

Load one or more Lua script files into the instance cache. The callback function should expect an error argument and an array of loaded script files. Examples:

// load a single script
rlh.load('myscript', function (err, scripts) {} );

// you can load any amount of scripts in this fashion
rlh.load('myscript1', 'myscript2', function (err, scripts) {} );

// filenames can be provided as an array as well
rlh.load(['myscript3', 'myscript4'], function (err, scripts) {} );

Notice: Right now, checking for circular dependencies may cause problems if you try to call load multiple times in a row before awaiting the first call to finish. To avoid this, you should place all filenames that you wish to load in an array and call load once.

RLH#loadDir ( dirpath, callback )

Loads all script files in a given directory, relative to the root directory. Does not include files in subdirectories. If the dirpath argument is omitted, the root directory will be used instead.

// load all files in the root directory (not including subdirectories)
rlh.loadDir( function (err, scripts) {} );

// load all files in /root/subdir
rlh.loadDir( 'subdir', function (err, scripts) {} );

RLH#code ( scriptName )

Returns the code of a previously loaded script. You must load a script first before you can access its code.

var code = rlh.code('myscript');

RLH#shasum ( scriptName )

Returns the shasum of a previously loaded script. You must load a script first before you can access its shasum.

// returns the scripts shasum
// i.e. 6b1bf486c81ceb7edf3c093f4c48582e38c0e791
var shasum = rlh.shasum('myscript');

RLH#clearCache()

Clears the entire cache, is the same as creating a fresh instance.

// clear the script cache
rlh.clearCache();

TODO

  • Create a loading queue so you can issue multiple load commands in parallel.
  • Scan code for KEYS[*] and store information to be accessed via helper.keys('script').
  • Allow to create compiled files and store them in filesystem for debugging and caching.
  • Add bindings for popular redis clients, .sync() and .eval(), with config options to provide a 'client' and 'adapter' (=client type).
  • Reformat README.md for linebreaks -.-

Changelog

0.1.0

Initial release.

License

MIT License, see LICENSE file.