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-lua2js

v4.0.1

Published

Convert a redis lua script into a simple node module

Downloads

143

Readme

redis-lua2js

Build Status npm package Coverage Status

convert redis lua scripts to a useful node module

Install

$ npm install --save-dev redis-lua2js

Usage

This module is not meant to be used on its own, but rather as part of another module, such as gulp-redis-lua2js or hook-redis-lua. In here, I will demonstrate the usage with the help of module-from-string:

index.js:

import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import Redis from 'ioredis';
import lua2js from 'redis-lua2js';
import { requireFromString } from 'module-from-string';

const ioredis = new Redis();
const lua = readFileSync(join(__dirname, 'pdel.lua'));
const js = lua2js(lua); // This is a node module string, the template of which you can see in src/lua.js
const pdel = requireFromString(js); // Parse the module as a string

ioredis.defineCommand(pdel.name, {
  lua: pdel.lua,
  numberOfKeys: pdel.numberOfKeys,
});
ioredis.pdel('*');

pdel.lua:

--!/usr/bin/env lua
-- name pdel
-- nkeys 1

local function deleteKeys (keys)
  for i, name in ipairs(keys) do
    redis.call("DEL", name)
  end
end

if type(redis.replicate_commands) == 'function' and redis.replicate_commands() then -- Redis 3.2+
  local count = 0
  local cursor = "0"
  local keys

  repeat
    cursor, keys = unpack(redis.call("SCAN", cursor, "MATCH", KEYS[1]))
    count = count + #keys
    deleteKeys(keys)
  until cursor == "0"

  return count
else
  local keys = redis.call("KEYS", KEYS[1])
  deleteKeys(keys)
  return #keys
end

API

lua2js(lua, { name, numberOfKeys, type = "commonjs" })

Takes the contents of a lua script and outputs a node module, as a string, which can be used to load the script into a redis client such as ioredis easily.

lua

Type: string, the contents of a lua script

name

Type: string?

By default it is parsed from the comments of the lua script, as demonstrated in the Usage section

The name of the redis command, will be used when installing to ioredis

numberOfKeys

Type: string?,

By default it is parsed from the comments of the lua script, as demonstrated in the Usage section

The number of keys that the redis command accepts

type

Type: "commonjs" | "module"

Whether the output module is commonjs or esm.

The parsed module will export the following:

name

Type: string | null, contains the name from the API above

numberOfKeys

Type: integer | null, contains the value of the number of keys argument from the API above

lua

Type: string, contains the contents of the lua script, useful for manually installing the script, for example, with ioredis.defineCommand

License

See the LICENSE file for license rights and limitations (MIT).