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

hook-redis-lua

v1.0.0

Published

Hook for requiring lua scripts with redis-lua2js

Downloads

2

Readme

node-hook-redis-lua

Travis npm package Coverage Status Dependency Status devDependency Status

import redis lua scripts as a normal node module

Install

$ npm install --save hook-redis-lua

Usage

If you have a redis lua script in your project, you can load it and install it into ioredis clients, or any other client, as follows:

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

index.js:

const Redis = require('ioredis');
const fs = require('fs');
const { hook, unhook } = require('hook-redis-lua');

hook(); // Uses default redis-lua2js behavior for parsing name and numberOfKeys out of lua comments

const pdel = require('./pdel.lua');
const ioredis = new Redis();
ioredis.defineCommand(pdel.name, {
  lua: pdel.lua,
  numberOfKeys: pdel.numberOfKeys,
});

ioredis.pdel('*');

console.log(pdel.name); // pdel
console.log(pdel.numberOfKeys); // 1
console.log(pdel.lua); // the content of pdel.lua

unhook(); // can't require lua scripts from here

API

hook({ name, numberOfKeys })

Hooks .lua files to be parsed when called by require with redis-lua2js

name

Type: any | (filename: string, source: string) => any, default: undefined

How to determine the name of the lua script. If undefined, it will use the default redis-lua2js behavior of parsing lua comments for the name. If a constant is passed, it will use this constant for the name of all scripts. If a function is passed, it will be used to determine the name of the script. The function must be synchronous.

Example: (filename) => path.basename(filename, path.extname(filename))

numberOfKeys

Type: any | (filename: string, source: string) => any, default: undefined

How to determine the number of keys of the lua script. If undefined, it will use the default redis-lua2js behavior of parsing lua comments for the number of keys. If a constant is passed, it will use this constant for the number of keys of all scripts. If a function is passed, it will be used to determine the number of keys of the script. The function must be synchronous.

Example: (filename, source) => getNumberOfKeysSomehow(source)

unhook()

Removes .lua hooks from require

For details on the properties exported by the hook, check redis-lua2js.

Tests

npm test

License

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