-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
57 lines (50 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Load redis lua scripts.
* The name of the script must have the following format:
*
* cmdName-numKeys.lua
*
* cmdName must be in camel case format.
*
* For example:
* moveToFinish-3.lua
*
*/
'use strict';
const fsAsync = require('fs').promises;
const path = require('path');
const utils = require('../utils');
module.exports = (function() {
let scripts;
return async function(client) {
await utils.isRedisReady(client);
scripts = await (scripts || loadScripts());
return scripts.map(({ name, options }) =>
client.defineCommand(name, options)
);
};
})();
async function loadScripts() {
const scriptsDir = await fsAsync.readdir(__dirname);
const luaFiles = scriptsDir.filter(file => path.extname(file) === '.lua');
if (luaFiles.length === 0) {
/**
* To prevent unclarified runtime error "updateDelayset is not a function
* @see https://github.com/OptimalBits/bull/issues/920
*/
throw new Error('No .lua files found!');
}
return Promise.all(
luaFiles.map(async file => {
const lua = await fsAsync.readFile(path.join(__dirname, file));
const longName = path.basename(file, '.lua');
return {
name: longName.split('-')[0],
options: {
numberOfKeys: parseInt(longName.split('-')[1]),
lua: lua.toString()
}
};
})
);
}