woonplan-packages-redishelper 1.0.89 → 1.0.92

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.
@@ -1,3 +1,3 @@
1
1
  import { RedisConfig, RedisStreamEntry } from "./types";
2
- declare const filterStream: (config: RedisConfig, streamname: string, key: string, value: string) => Promise<RedisStreamEntry[]>;
2
+ declare const filterStream: (config: RedisConfig, streamname: string, key: string, value: string, count?: number) => Promise<RedisStreamEntry[]>;
3
3
  export { filterStream };
@@ -10,14 +10,68 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
13
49
  Object.defineProperty(exports, "__esModule", { value: true });
14
50
  exports.filterStream = void 0;
15
51
  var memdb_1 = require("./memdb");
16
- var getStreamInfo = function (config, streamname, count) { return (0, memdb_1.getRedisClient)(config).xinfo('stream', streamname, 'FULL', 'COUNT', count); };
17
- var getEntriesFromStream = function (config, streamname) {
18
- return getStreamInfo(config, streamname, 0)
52
+ var getStreamInfo = function (config, streamname, count) { return __awaiter(void 0, void 0, void 0, function () {
53
+ var _a;
54
+ return __generator(this, function (_b) {
55
+ switch (_b.label) {
56
+ case 0:
57
+ _b.trys.push([0, 2, , 3]);
58
+ return [4 /*yield*/, (0, memdb_1.getRedisClient)(config).xinfo('stream', streamname, 'FULL', 'COUNT', count)];
59
+ case 1: return [2 /*return*/, _b.sent()];
60
+ case 2:
61
+ _a = _b.sent();
62
+ return [2 /*return*/, []];
63
+ case 3: return [2 /*return*/];
64
+ }
65
+ });
66
+ }); };
67
+ var getEntriesFromStream = function (config, streamname, count) {
68
+ if (count === void 0) { count = 0; }
69
+ return getStreamInfo(config, streamname, count)
19
70
  .then(function (result) {
20
- return result[9].map(function (entry) { return ({
71
+ var redisStreamResult = getObjectFromStreamResult(result);
72
+ if (!redisStreamResult.entries)
73
+ return [];
74
+ return redisStreamResult.entries.map(function (entry) { return ({
21
75
  id: entry[0],
22
76
  message: entry[1].reduce(function (prevValue, currentValue, n) {
23
77
  var _a;
@@ -26,8 +80,14 @@ var getEntriesFromStream = function (config, streamname) {
26
80
  }); });
27
81
  });
28
82
  };
29
- var filterStream = function (config, streamname, key, value) {
30
- return getEntriesFromStream(config, streamname)
83
+ var getObjectFromStreamResult = function (arr) { return arr.reduce(function (obj, current, n) {
84
+ if (!(n % 2) && arr.length + 1 >= n)
85
+ obj[current] = arr[n + 1];
86
+ return obj;
87
+ }, {}); };
88
+ var filterStream = function (config, streamname, key, value, count) {
89
+ if (count === void 0) { count = 0; }
90
+ return getEntriesFromStream(config, streamname, count)
31
91
  .then(function (entries) { return entries.filter(function (entry) {
32
92
  return Object.keys(entry.message).indexOf(key) > -1 && entry.message[key] == value;
33
93
  }); });
package/eventsource.ts CHANGED
@@ -1,22 +1,37 @@
1
1
  import { getRedisClient } from "./memdb"
2
2
  import { RedisConfig, RedisStreamEntry, RedisMessage } from "./types"
3
3
 
4
- const getStreamInfo = ( config:RedisConfig, streamname:string, count:number ) => getRedisClient( config ).xinfo('stream', streamname,'FULL', 'COUNT', count )
4
+ const getStreamInfo = async ( config:RedisConfig, streamname:string, count:number ) => {
5
+ try{
6
+ return await getRedisClient( config ).xinfo('stream', streamname,'FULL', 'COUNT', count )
7
+ }catch{
8
+ return []
9
+ }
10
+ }
5
11
 
6
- const getEntriesFromStream = ( config:RedisConfig, streamname:string ) =>
7
- getStreamInfo( config, streamname, 0 )
8
- .then(( result:any ) =>
9
- result[9].map( (entry:[string,string[]]) => ({
12
+ const getEntriesFromStream = ( config:RedisConfig, streamname:string, count:number = 0 ) =>
13
+ getStreamInfo( config, streamname, count )
14
+ .then(( result:any[] ) => {
15
+ const redisStreamResult = getObjectFromStreamResult( result )
16
+ if( !redisStreamResult.entries ) return []
17
+
18
+ return redisStreamResult.entries.map( (entry:[string,string[]]) => ({
10
19
  id : entry[0],
11
20
  message : entry[1].reduce( (prevValue:RedisMessage, currentValue:string, n:number) => (n%2 ? {...prevValue} : { ...prevValue, [currentValue] : entry[1][n+1] }) as RedisMessage, {} as RedisMessage )
12
21
  }
13
22
  )
14
23
  ) as RedisStreamEntry[]
15
- )
24
+ })
25
+
26
+
27
+ const getObjectFromStreamResult = ( arr:any[] ) => arr.reduce( (obj,current,n:number) => {
28
+ if( !(n % 2) && arr.length+1 >= n ) obj[current] = arr[n+1]
29
+ return obj
30
+ }, {} )
16
31
 
17
32
 
18
- const filterStream = ( config:RedisConfig, streamname:string, key:string, value:string ) =>
19
- getEntriesFromStream( config, streamname )
33
+ const filterStream = ( config:RedisConfig, streamname:string, key:string, value:string, count:number = 0 ) =>
34
+ getEntriesFromStream( config, streamname, count )
20
35
  .then((entries:RedisStreamEntry[]) => entries.filter( entry => {
21
36
  return Object.keys( entry.message ).indexOf( key ) > -1 && entry.message[key] == value
22
37
  }))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "woonplan-packages-redishelper",
3
- "version": "1.0.89",
3
+ "version": "1.0.92",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",