@vbarbarosh/node-helpers 2.2.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/fs_mkdirp.test.js +1 -1
- package/src/redis_connect.js +21 -13
- package/src/redis_lshift.js +10 -1
- package/src/redis_lshift.test.js +57 -0
- package/src/redis_zshift.js +10 -2
- package/src/redis_zshift.test.js +58 -0
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"name": "@vbarbarosh/node-helpers",
|
|
5
|
-
"version": "2.
|
|
5
|
+
"version": "2.4.0",
|
|
6
6
|
"description": "A set of helpers for JavaScript/Node.js",
|
|
7
7
|
"files": [
|
|
8
8
|
"src"
|
|
@@ -31,6 +31,10 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"mocha": "^10.2.0",
|
|
34
|
-
"throttle": "^1.0.3"
|
|
34
|
+
"throttle": "^1.0.3",
|
|
35
|
+
"redis-v1": "npm:redis@1",
|
|
36
|
+
"redis-v2": "npm:redis@2",
|
|
37
|
+
"redis-v3": "npm:redis@3",
|
|
38
|
+
"redis-v4": "npm:redis@4"
|
|
35
39
|
}
|
|
36
40
|
}
|
package/src/fs_mkdirp.test.js
CHANGED
|
@@ -15,7 +15,7 @@ describe('fs_mkdirp', function () {
|
|
|
15
15
|
it('should create the same directory structure in parallel', async function () {
|
|
16
16
|
await fs_tempdir(async function (d) {
|
|
17
17
|
const tmp = fs_path_resolve(d, 'a/b/c/d/e/f/g/h');
|
|
18
|
-
await Promise.all(Array(10).fill(tmp).map(async function (
|
|
18
|
+
await Promise.all(Array(10).fill(tmp).map(async function () {
|
|
19
19
|
await fs_mkdirp(tmp);
|
|
20
20
|
assert(await fs_fi(tmp).then(v => v.isDirectory()));
|
|
21
21
|
}));
|
package/src/redis_connect.js
CHANGED
|
@@ -1,21 +1,29 @@
|
|
|
1
|
-
const
|
|
1
|
+
const redis = require('redis');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @returns {*}
|
|
8
|
-
*/
|
|
9
|
-
function redis_connect(url)
|
|
3
|
+
// tcp://127.0.0.1:6379
|
|
4
|
+
// tls://11.11.11.11:6378?password=xxx
|
|
5
|
+
// tls://11.11.11.11:6378?password=xx-xx-xx&ssl[verify_peer]=0&ssl[verify_peer_name]=0
|
|
6
|
+
async function redis_connect(url)
|
|
10
7
|
{
|
|
8
|
+
const tmp = new URL(url);
|
|
9
|
+
const password = tmp.password || tmp.searchParams.get('password');
|
|
10
|
+
|
|
11
11
|
if (url.match(/^tls:/)) {
|
|
12
|
-
|
|
13
|
-
tls:
|
|
14
|
-
|
|
15
|
-
},
|
|
12
|
+
const out = redis.createClient({
|
|
13
|
+
url: url.replace(/^tls:/, 'rediss:'),
|
|
14
|
+
password,
|
|
15
|
+
tls: {rejectUnauthorized: false},
|
|
16
|
+
socket: {tls: true, rejectUnauthorized: false},
|
|
16
17
|
});
|
|
18
|
+
await out.connect();
|
|
19
|
+
return out;
|
|
17
20
|
}
|
|
18
|
-
|
|
21
|
+
const out = await redis.createClient({
|
|
22
|
+
url: url.replace(/^tcp:/, 'redis:'),
|
|
23
|
+
password,
|
|
24
|
+
});
|
|
25
|
+
await out.connect();
|
|
26
|
+
return out;
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
module.exports = redis_connect;
|
package/src/redis_lshift.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
async function redis_lshift(redis, queue, limit = 1)
|
|
2
2
|
{
|
|
3
3
|
const last = limit - 1;
|
|
4
|
-
|
|
4
|
+
if (redis.lrange) {
|
|
5
|
+
const [items] = await new Promise(function (resolve, reject) {
|
|
6
|
+
redis.multi().lrange(queue, 0, last).ltrim(queue, limit, -1).exec(function (error, out) {
|
|
7
|
+
error ? reject(error) : resolve(out);
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
return items;
|
|
11
|
+
}
|
|
12
|
+
// redis@4
|
|
13
|
+
const [items] = await redis.multi().LRANGE(queue, 0, last).LTRIM(queue, limit, -1).exec();
|
|
5
14
|
return items;
|
|
6
15
|
}
|
|
7
16
|
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const redis_lshift = require('./redis_lshift');
|
|
3
|
+
|
|
4
|
+
const REDIS_HOST = '127.0.0.1';
|
|
5
|
+
const REDIS_PORT = 6379;
|
|
6
|
+
const REDIS_AUTH = null;
|
|
7
|
+
const REDIS_QUEUE = 'TESTING';
|
|
8
|
+
|
|
9
|
+
describe('redis_lshift', function () {
|
|
10
|
+
it('should work with redis@1', async function () {
|
|
11
|
+
await test_1_2_3(require('redis-v1'));
|
|
12
|
+
});
|
|
13
|
+
it('should work with redis@2', async function () {
|
|
14
|
+
await test_1_2_3(require('redis-v2'));
|
|
15
|
+
});
|
|
16
|
+
it('should work with redis@3', async function () {
|
|
17
|
+
await test_1_2_3(require('redis-v3'));
|
|
18
|
+
});
|
|
19
|
+
it('should work with redis@4', async function () {
|
|
20
|
+
await test_4(require('redis-v4'));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function test_1_2_3(redis)
|
|
25
|
+
{
|
|
26
|
+
const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
|
|
27
|
+
try {
|
|
28
|
+
const expected = 'aa,bb,cc,dd,ee'.split(',');
|
|
29
|
+
await new Promise(function (resolve, reject) {
|
|
30
|
+
r.del(REDIS_QUEUE, (e,v) => e ? reject(e) : resolve(v));
|
|
31
|
+
});
|
|
32
|
+
await new Promise(function (resolve, reject) {
|
|
33
|
+
r.rpush(REDIS_QUEUE, ...expected, (e,v) => e ? reject(e) : resolve(v));
|
|
34
|
+
});
|
|
35
|
+
const actual = await redis_lshift(r, REDIS_QUEUE, expected.length);
|
|
36
|
+
await assert.deepStrictEqual(actual, expected);
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
await r.quit();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function test_4(redis)
|
|
44
|
+
{
|
|
45
|
+
const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
|
|
46
|
+
await r.connect();
|
|
47
|
+
try {
|
|
48
|
+
const expected = 'aa,bb,cc,dd,ee'.split(',');
|
|
49
|
+
await r.DEL(REDIS_QUEUE);
|
|
50
|
+
await r.RPUSH(REDIS_QUEUE, expected);
|
|
51
|
+
const actual = await redis_lshift(r, REDIS_QUEUE, expected.length);
|
|
52
|
+
await assert.deepStrictEqual(actual, expected);
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
await r.quit();
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/redis_zshift.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
async function redis_zshift(redis, queue, limit = 1)
|
|
2
2
|
{
|
|
3
3
|
const last = limit - 1;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
if (redis.zrange) {
|
|
5
|
+
const [items] = await new Promise(function (resolve, reject) {
|
|
6
|
+
redis.multi().zrange(queue, 0, last).zremrangebyrank(queue, 0, last).exec(function (error, out) {
|
|
7
|
+
error ? reject(error) : resolve(out);
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
return items;
|
|
11
|
+
}
|
|
12
|
+
// redis@4
|
|
13
|
+
const [items] = await redis.multi().ZRANGE(queue, 0, last).ZREMRANGEBYRANK(queue, 0, last).exec();
|
|
6
14
|
return items;
|
|
7
15
|
}
|
|
8
16
|
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const redis_zshift = require('./redis_zshift');
|
|
3
|
+
|
|
4
|
+
const REDIS_HOST = '127.0.0.1';
|
|
5
|
+
const REDIS_PORT = 6379;
|
|
6
|
+
const REDIS_AUTH = null;
|
|
7
|
+
const REDIS_QUEUE = 'TESTING';
|
|
8
|
+
|
|
9
|
+
describe('redis_zshift', function () {
|
|
10
|
+
it('should work with redis@1', async function () {
|
|
11
|
+
await test_1_2_3(require('redis-v1'));
|
|
12
|
+
});
|
|
13
|
+
it('should work with redis@2', async function () {
|
|
14
|
+
await test_1_2_3(require('redis-v2'));
|
|
15
|
+
});
|
|
16
|
+
it('should work with redis@3', async function () {
|
|
17
|
+
await test_1_2_3(require('redis-v3'));
|
|
18
|
+
});
|
|
19
|
+
it('should work with redis@4', async function () {
|
|
20
|
+
await test_4(require('redis-v4'));
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function test_1_2_3(redis)
|
|
25
|
+
{
|
|
26
|
+
const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
|
|
27
|
+
try {
|
|
28
|
+
const expected = 'aa,bb,cc,dd,ee'.split(',');
|
|
29
|
+
await new Promise(function (resolve, reject) {
|
|
30
|
+
r.del(REDIS_QUEUE, (e,v) => e ? reject(e) : resolve(v));
|
|
31
|
+
});
|
|
32
|
+
await new Promise(function (resolve, reject) {
|
|
33
|
+
const args = [REDIS_QUEUE, ...expected.map((v,i) => [i,v]).flat()];
|
|
34
|
+
r.zadd(args, (e,v) => e ? reject(e) : resolve(v));
|
|
35
|
+
});
|
|
36
|
+
const actual = await redis_zshift(r, REDIS_QUEUE, expected.length);
|
|
37
|
+
await assert.deepStrictEqual(actual, expected);
|
|
38
|
+
}
|
|
39
|
+
finally {
|
|
40
|
+
await r.quit();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function test_4(redis)
|
|
45
|
+
{
|
|
46
|
+
const r = redis.createClient(REDIS_PORT, REDIS_HOST, {password: REDIS_AUTH || undefined});
|
|
47
|
+
await r.connect();
|
|
48
|
+
try {
|
|
49
|
+
const expected = 'aa,bb,cc,dd,ee'.split(',');
|
|
50
|
+
await r.DEL(REDIS_QUEUE);
|
|
51
|
+
await r.ZADD(REDIS_QUEUE, expected.map((value,i) => ({score: i, value})));
|
|
52
|
+
const actual = await redis_zshift(r, REDIS_QUEUE, 5);
|
|
53
|
+
await assert.deepStrictEqual(actual, expected);
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
await r.quit();
|
|
57
|
+
}
|
|
58
|
+
}
|