woonplan-packages-redishelper 1.0.94 → 2.0.2
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/.gitattributes +2 -2
- package/jest.config.js +18 -0
- package/package.json +31 -17
- package/src/__tests__/Broker.test.ts +223 -0
- package/src/__tests__/Listener.test.ts +89 -0
- package/src/classes/Broker.ts +224 -0
- package/src/classes/BrokerClient.ts +63 -0
- package/src/classes/ListListener.ts +19 -0
- package/src/classes/ListRunner.ts +43 -0
- package/src/classes/Listener.ts +13 -0
- package/src/main.ts +3 -0
- package/src/services/utils.ts +51 -0
- package/src/types.d.ts +49 -0
- package/tsconfig.json +34 -5
- package/___index.js +0 -48
- package/data.ts +0 -8
- package/dist/data.d.ts +0 -3
- package/dist/data.js +0 -6
- package/dist/eventsource.d.ts +0 -3
- package/dist/eventsource.js +0 -95
- package/dist/getters.d.ts +0 -6
- package/dist/getters.js +0 -70
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -170
- package/dist/memdb.d.ts +0 -4
- package/dist/memdb.js +0 -9
- package/dist/setters.d.ts +0 -12
- package/dist/setters.js +0 -79
- package/dist/types.d.ts +0 -14
- package/dist/types.js +0 -2
- package/dist/utils.d.ts +0 -7
- package/dist/utils.js +0 -15
- package/eventsource.ts +0 -40
- package/getters.ts +0 -101
- package/index.ts +0 -211
- package/memdb.ts +0 -10
- package/setters.ts +0 -83
- package/types.ts +0 -16
- package/utils.ts +0 -10
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Redis } from "ioredis"
|
|
2
|
+
import Broker from "./Broker"
|
|
3
|
+
import BrokerClient from "./BrokerClient"
|
|
4
|
+
import ListRunner from "./ListRunner"
|
|
5
|
+
|
|
6
|
+
export default class Listener extends BrokerClient{
|
|
7
|
+
constructor( broker:Broker, client:Redis, list:string, callback:Function ){
|
|
8
|
+
super( broker, client, callback, list )
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async streamCallback( message:DecypheredMessage ){
|
|
12
|
+
const listrunnercallback = await this.callback( message.id, message.parameters )
|
|
13
|
+
|
|
14
|
+
if( message.parameters.listname )
|
|
15
|
+
return new ListRunner( this.client, message.parameters.listname, listrunnercallback)
|
|
16
|
+
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Redis } from "ioredis"
|
|
2
|
+
|
|
3
|
+
export default class ListRunner{
|
|
4
|
+
listname:string
|
|
5
|
+
itemsDone:number = 0
|
|
6
|
+
itemsPerCall:number
|
|
7
|
+
callback:Function
|
|
8
|
+
client : Redis
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
constructor( client:Redis, listname:string, callback:Function, itemsPerCall : number = 1 ){
|
|
12
|
+
this.client = client
|
|
13
|
+
this.callback = callback
|
|
14
|
+
this.listname = listname
|
|
15
|
+
this.itemsPerCall = itemsPerCall
|
|
16
|
+
|
|
17
|
+
this.run.call( this )
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async run():Promise<Function|void>{
|
|
22
|
+
const items = await this.nextitems.call( this, this.listname )
|
|
23
|
+
if( !items ){
|
|
24
|
+
console.log( `finished with ${this.itemsDone} jobs`)
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
try{
|
|
29
|
+
await this.callback( items )
|
|
30
|
+
}catch( e:any ){
|
|
31
|
+
console.log( e )
|
|
32
|
+
|
|
33
|
+
}finally{
|
|
34
|
+
this.itemsDone += this.itemsPerCall
|
|
35
|
+
return this.run.call( this )
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async nextitems( listname:string ):Promise<string[] | null>{
|
|
40
|
+
return this.client.lpop( listname, this.itemsPerCall )
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Redis } from "ioredis"
|
|
2
|
+
import Broker from "./Broker"
|
|
3
|
+
import BrokerClient from "./BrokerClient"
|
|
4
|
+
|
|
5
|
+
export default class Listener extends BrokerClient{
|
|
6
|
+
constructor( broker:Broker, client:Redis, stream:string, callback:Function, group ?: string ){
|
|
7
|
+
super( broker, client, callback, null, stream, group )
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
streamCallback(message: DecypheredMessage): Promise<any> {
|
|
11
|
+
return this.callback( message.id, message.parameters )
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
const isJSON = ( str:any ) => {
|
|
3
|
+
if( typeof str == 'number' ) return false
|
|
4
|
+
try{
|
|
5
|
+
JSON.parse( str )
|
|
6
|
+
}catch( error ){
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface ObjectToOmitFrom{
|
|
13
|
+
[index: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const omit = ( objectToOmitFrom:ObjectToOmitFrom, keys:(keyof ObjectToOmitFrom)[]) =>
|
|
18
|
+
Object.keys( objectToOmitFrom )
|
|
19
|
+
.filter( key => keys.indexOf( key ) == -1 )
|
|
20
|
+
.reduce( (struct:object,key) =>
|
|
21
|
+
Object.assign( struct, {[key]:objectToOmitFrom[key]} ) ,
|
|
22
|
+
{}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
const mapJsonReplacer = ( _key:any, value:Map<any,any> | any ) => {
|
|
28
|
+
if(value instanceof Map) {
|
|
29
|
+
return {
|
|
30
|
+
dataType: 'Map',
|
|
31
|
+
value: Array.from(value.entries()), // or with spread: value: [...value]
|
|
32
|
+
} as JsonStringifiedMap
|
|
33
|
+
} else {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const mapJsonReviver = ( _key:any, value:JsonStringifiedMap | any ) => {
|
|
39
|
+
if(typeof value === 'object' && value !== null) {
|
|
40
|
+
if (value.dataType === 'Map') {
|
|
41
|
+
return new Map(value.value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const stringifyMap = ( map:Map<any,any> ):string=> JSON.stringify( map, exports.mapJsonReplacer)
|
|
48
|
+
const parseMap = ( json:string ):Map<any,any> => JSON.parse( json, exports.mapJsonReviver)
|
|
49
|
+
const capitalizeFirstLetter = (string:string) => string.charAt(0).toUpperCase() + string.slice(1)
|
|
50
|
+
|
|
51
|
+
export { isJSON, omit, mapJsonReviver, stringifyMap, parseMap, mapJsonReplacer, capitalizeFirstLetter }
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
|
|
2
|
+
interface JsonStringifiedMap{
|
|
3
|
+
dataType : 'Map',
|
|
4
|
+
value : any[]
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
interface RedisMessage{
|
|
8
|
+
[index: string]: any;
|
|
9
|
+
}
|
|
10
|
+
interface Struct{
|
|
11
|
+
[index: string]: any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface RedisConfig{
|
|
15
|
+
REDISURL: string | number | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface RedisStreamEntry{
|
|
19
|
+
id : string
|
|
20
|
+
message:RedisMessage
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type StreamMessage = [
|
|
24
|
+
string, StreamMessageParameters
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
type StreamMessageParameters = string[]
|
|
28
|
+
type StreamResponse = [
|
|
29
|
+
string, StreamMessage[]
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
interface DecypheredParameters{
|
|
33
|
+
[index:string] : string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface DecypheredMessage{
|
|
37
|
+
id : string
|
|
38
|
+
parameters : DecypheredParameters
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface DecypheredResponse{
|
|
42
|
+
stream : string,
|
|
43
|
+
messages : DecypheredMessage[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface RollbarConfig{
|
|
47
|
+
accessToken : string
|
|
48
|
+
environment : string
|
|
49
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
3
|
+
"target": "es2021",
|
|
4
4
|
"module": "commonjs",
|
|
5
|
+
"moduleResolution": "node",
|
|
5
6
|
"declaration": true,
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
|
|
9
|
+
"strictNullChecks": true /* Enable strict null checks. */,
|
|
10
|
+
"strictFunctionTypes": true /* Enable strict checking of function types. */,
|
|
11
|
+
"noUnusedLocals": true /* Report errors on unused locals. */,
|
|
12
|
+
"noUnusedParameters": true /* Report errors on unused parameters. */,
|
|
13
|
+
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
|
|
14
|
+
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
|
|
15
|
+
"importHelpers": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"esModuleInterop": true,
|
|
18
|
+
"allowSyntheticDefaultImports": true,
|
|
19
|
+
"experimentalDecorators": true,
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"outDir": "./dist/tsc/",
|
|
22
|
+
"types": [
|
|
23
|
+
"node",
|
|
24
|
+
"jest"
|
|
25
|
+
],
|
|
26
|
+
"lib": [
|
|
27
|
+
"ES6",
|
|
28
|
+
"DOM"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"include": [
|
|
32
|
+
"src/**/*.ts"
|
|
33
|
+
],
|
|
34
|
+
"exclude": [
|
|
35
|
+
"node_modules",
|
|
36
|
+
"**/*.test.ts"
|
|
37
|
+
]
|
|
38
|
+
}
|
package/___index.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
exports.hookCallbackToRedisStream = async ( redis, groupname, channelname, consumername, callback ) => {
|
|
2
|
-
// try to create channel
|
|
3
|
-
group = redis.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM' );
|
|
4
|
-
group.catch((error)=>{
|
|
5
|
-
console.log( 'GROUP EXISTS' );
|
|
6
|
-
}).then(()=>{
|
|
7
|
-
console.log( 'listening to ' + channelname )
|
|
8
|
-
listenForMessage( redis, groupname, channelname, consumername, callback )
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const listenForMessage = ( redis, groupname, channelname, consumername, callback, lastId = "$" ) => {
|
|
16
|
-
redis.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>').then( (data) => {
|
|
17
|
-
const channelname = data[0][0];
|
|
18
|
-
const id = data[0][0];
|
|
19
|
-
|
|
20
|
-
const [key, messages] = data[0];
|
|
21
|
-
|
|
22
|
-
console.log( '----- ' + channelname + ' reached' );
|
|
23
|
-
|
|
24
|
-
for( let n in messages ){
|
|
25
|
-
const message = messages[n];
|
|
26
|
-
const id = message[0];
|
|
27
|
-
|
|
28
|
-
let messageData = {};
|
|
29
|
-
|
|
30
|
-
message[1].forEach( (key, n, messageArray ) => {
|
|
31
|
-
if( !( n == 0 || n % 2 == 0 ) ) return;
|
|
32
|
-
messageData[key] = messageArray[n+1];
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
callback( messageData ).then( result => {
|
|
36
|
-
redis.xack( channelname, groupname, id ).then( success => {
|
|
37
|
-
console.log( id + ' acknowledged')
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
listenForMessage( redis, groupname, channelname, consumername, callback );
|
|
42
|
-
}).catch(
|
|
43
|
-
error => {
|
|
44
|
-
console.log( error );
|
|
45
|
-
//listenForMessage( redis, groupname, channelname, consumername, callback );
|
|
46
|
-
}
|
|
47
|
-
);
|
|
48
|
-
}
|
package/data.ts
DELETED
package/dist/data.d.ts
DELETED
package/dist/data.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.subscribeToMessageResponse = void 0;
|
|
4
|
-
var utils_1 = require("./utils");
|
|
5
|
-
var subscribeToMessageResponse = function (client, messageid) { return client.subscribe((0, utils_1.getMessageReponseKey)(messageid)); };
|
|
6
|
-
exports.subscribeToMessageResponse = subscribeToMessageResponse;
|
package/dist/eventsource.d.ts
DELETED
package/dist/eventsource.js
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
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
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.filterStream = void 0;
|
|
51
|
-
var memdb_1 = require("./memdb");
|
|
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)
|
|
70
|
-
.then(function (result) {
|
|
71
|
-
var redisStreamResult = getObjectFromStreamResult(result);
|
|
72
|
-
if (!redisStreamResult.entries)
|
|
73
|
-
return [];
|
|
74
|
-
return redisStreamResult.entries.map(function (entry) { return ({
|
|
75
|
-
id: entry[0],
|
|
76
|
-
message: entry[1].reduce(function (prevValue, currentValue, n) {
|
|
77
|
-
var _a;
|
|
78
|
-
return (n % 2 ? __assign({}, prevValue) : __assign(__assign({}, prevValue), (_a = {}, _a[currentValue] = entry[1][n + 1], _a)));
|
|
79
|
-
}, {})
|
|
80
|
-
}); });
|
|
81
|
-
});
|
|
82
|
-
};
|
|
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)
|
|
91
|
-
.then(function (entries) { return entries.filter(function (entry) {
|
|
92
|
-
return Object.keys(entry.message).indexOf(key) > -1 && entry.message[key] == value;
|
|
93
|
-
}); });
|
|
94
|
-
};
|
|
95
|
-
exports.filterStream = filterStream;
|
package/dist/getters.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Redis } from "ioredis";
|
|
2
|
-
import { RedisConfig, Struct } from "./types";
|
|
3
|
-
declare const getOrRequestData: (config: RedisConfig, request: string, service: string, data?: Struct) => Promise<unknown>;
|
|
4
|
-
declare const subscribeToMessageResponse: (client: Redis, messageid: string) => Promise<number>;
|
|
5
|
-
declare const requestAndWaitForList: (client: Redis, service: string, listname: string, listsubject: string, props: {} | undefined, {}: {}) => Promise<unknown>;
|
|
6
|
-
export { getOrRequestData, requestAndWaitForList, subscribeToMessageResponse };
|
package/dist/getters.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.subscribeToMessageResponse = exports.requestAndWaitForList = exports.getOrRequestData = void 0;
|
|
4
|
-
var utils_1 = require("./utils");
|
|
5
|
-
var memdb_1 = require("./memdb");
|
|
6
|
-
var uuid_1 = require("uuid");
|
|
7
|
-
var timeout = 1500;
|
|
8
|
-
var getOrRequestData = function (config, request, service, data) {
|
|
9
|
-
if (data === void 0) { data = {}; }
|
|
10
|
-
return new Promise(function (resolve, reject) {
|
|
11
|
-
var getterclient = (0, memdb_1.getRedisClient)(config);
|
|
12
|
-
requestAndWaitForData(getterclient, config, request, service, data)
|
|
13
|
-
.then(function (result) { return resolve(result); })
|
|
14
|
-
.catch(function (error) {
|
|
15
|
-
console.log(error);
|
|
16
|
-
resolve(null);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
exports.getOrRequestData = getOrRequestData;
|
|
21
|
-
var requestAndWaitForData = function (getterclient, config, request, service, data) {
|
|
22
|
-
if (data === void 0) { data = {}; }
|
|
23
|
-
return new Promise(function (resolve) {
|
|
24
|
-
var messageid = (0, uuid_1.v4)();
|
|
25
|
-
// create a listener
|
|
26
|
-
var listenerclient = (0, memdb_1.getRedisClient)(config);
|
|
27
|
-
subscribeToMessageResponse(listenerclient, messageid).
|
|
28
|
-
then(function () {
|
|
29
|
-
//console.log( `subscribed to ${messageid} at ${new Date().toISOString()}` )
|
|
30
|
-
// we are subscribed
|
|
31
|
-
// send out the request
|
|
32
|
-
getterclient.xadd((0, utils_1.getKeyRequestedChannelName)(service), '*', 'request', request, 'messageid', messageid, 'data', JSON.stringify(data))
|
|
33
|
-
.catch(function (e) {
|
|
34
|
-
console.log(e);
|
|
35
|
-
resolve(null);
|
|
36
|
-
})
|
|
37
|
-
.finally(function () { return getterclient.disconnect(); });
|
|
38
|
-
// setting up a one time event listener
|
|
39
|
-
listenerclient.once("message", function (channel, message) {
|
|
40
|
-
if (message.length)
|
|
41
|
-
resolve(message);
|
|
42
|
-
else
|
|
43
|
-
resolve(null);
|
|
44
|
-
console.log("unsubscribing from ".concat(channel));
|
|
45
|
-
listenerclient.unsubscribe();
|
|
46
|
-
listenerclient.disconnect();
|
|
47
|
-
});
|
|
48
|
-
// setup timeout
|
|
49
|
-
setTimeout(function () {
|
|
50
|
-
if (listenerclient.status != "end") {
|
|
51
|
-
console.log("sub timedout: ".concat(request));
|
|
52
|
-
resolve(null);
|
|
53
|
-
listenerclient.unsubscribe();
|
|
54
|
-
listenerclient.disconnect();
|
|
55
|
-
}
|
|
56
|
-
}, timeout);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
var subscribeToMessageResponse = function (client, messageid) { return client.subscribe((0, utils_1.getMessageReponseKey)(messageid)); };
|
|
61
|
-
exports.subscribeToMessageResponse = subscribeToMessageResponse;
|
|
62
|
-
var requestAndWaitForList = function (client, service, listname, listsubject, props, _a) {
|
|
63
|
-
if (props === void 0) { props = {}; }
|
|
64
|
-
return new Promise(function (resolve) {
|
|
65
|
-
// send message for list to be created and then block untill we have a list
|
|
66
|
-
client.xadd((0, utils_1.getListRequestedChannelName)(service), '*', 'listname', listname, 'listsubject', listsubject, 'props', JSON.stringify(props))
|
|
67
|
-
.then(function () { return resolve(client.blmove(listname, listname, 'RIGHT', 'LEFT', 2)); });
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
exports.requestAndWaitForList = requestAndWaitForList;
|
package/dist/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { RedisConfig, RedisMessage } from "./types";
|
|
2
|
-
import { sendMessageToStream, publishMessageResponse, toApiRequest } from "./setters";
|
|
3
|
-
import { getOrRequestData, subscribeToMessageResponse, requestAndWaitForList } from "./getters";
|
|
4
|
-
import { filterStream } from "./eventsource";
|
|
5
|
-
declare const listenToStream: (config: RedisConfig, groupname: string, channelname: string, consumername: string, callback: Function) => Promise<boolean>;
|
|
6
|
-
declare const dataRequester: (config: RedisConfig, groupname: string, service: string, consumername: string, callback: Function) => void;
|
|
7
|
-
declare const toApiListener: (config: RedisConfig, groupname: string, channelname: string, consumername: string, callback: Function) => void;
|
|
8
|
-
export { listenToStream, RedisMessage, sendMessageToStream, toApiRequest, publishMessageResponse, filterStream, getOrRequestData, toApiListener, dataRequester, subscribeToMessageResponse, requestAndWaitForList };
|
package/dist/index.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.requestAndWaitForList = exports.subscribeToMessageResponse = exports.dataRequester = exports.toApiListener = exports.getOrRequestData = exports.filterStream = exports.publishMessageResponse = exports.toApiRequest = exports.sendMessageToStream = exports.listenToStream = void 0;
|
|
4
|
-
var setters_1 = require("./setters");
|
|
5
|
-
Object.defineProperty(exports, "sendMessageToStream", { enumerable: true, get: function () { return setters_1.sendMessageToStream; } });
|
|
6
|
-
Object.defineProperty(exports, "publishMessageResponse", { enumerable: true, get: function () { return setters_1.publishMessageResponse; } });
|
|
7
|
-
Object.defineProperty(exports, "toApiRequest", { enumerable: true, get: function () { return setters_1.toApiRequest; } });
|
|
8
|
-
var getters_1 = require("./getters");
|
|
9
|
-
Object.defineProperty(exports, "getOrRequestData", { enumerable: true, get: function () { return getters_1.getOrRequestData; } });
|
|
10
|
-
Object.defineProperty(exports, "subscribeToMessageResponse", { enumerable: true, get: function () { return getters_1.subscribeToMessageResponse; } });
|
|
11
|
-
Object.defineProperty(exports, "requestAndWaitForList", { enumerable: true, get: function () { return getters_1.requestAndWaitForList; } });
|
|
12
|
-
var memdb_1 = require("./memdb");
|
|
13
|
-
var utils_1 = require("./utils");
|
|
14
|
-
var eventsource_1 = require("./eventsource");
|
|
15
|
-
Object.defineProperty(exports, "filterStream", { enumerable: true, get: function () { return eventsource_1.filterStream; } });
|
|
16
|
-
var listenToStream = function (config, groupname, channelname, consumername, callback) { return new Promise(function (resolve, reject) {
|
|
17
|
-
var client = (0, memdb_1.getRedisClient)(config);
|
|
18
|
-
createGroup(client, groupname, channelname).then(function () {
|
|
19
|
-
listenForMessage(client, groupname, channelname, consumername, callback);
|
|
20
|
-
resolve(true);
|
|
21
|
-
}).catch(function (error) {
|
|
22
|
-
console.log(error);
|
|
23
|
-
reject(error);
|
|
24
|
-
});
|
|
25
|
-
}); };
|
|
26
|
-
exports.listenToStream = listenToStream;
|
|
27
|
-
var createGroup = function (client, groupname, channelname) { return new Promise(function (resolve, reject) {
|
|
28
|
-
client.xgroup('CREATE', channelname, groupname, '$', 'MKSTREAM').then(function () { return resolve(true); }).catch(function (error) {
|
|
29
|
-
// group exists
|
|
30
|
-
if (error.message.includes('BUSYGROUP'))
|
|
31
|
-
return resolve(true);
|
|
32
|
-
reject(error);
|
|
33
|
-
});
|
|
34
|
-
}); };
|
|
35
|
-
// this function is made as a responsding function to getters/requestAndWaitForKey
|
|
36
|
-
// dataRequester listens to a stream and then publishes a respond
|
|
37
|
-
var dataRequester = function (config, groupname, service, consumername, callback) {
|
|
38
|
-
var channelname = (0, utils_1.getKeyRequestedChannelName)(service);
|
|
39
|
-
var listenerclient = (0, memdb_1.getRedisClient)(config);
|
|
40
|
-
createGroup(listenerclient, groupname, channelname)
|
|
41
|
-
.then(function () { return listenForMessageForDataRequest(listenerclient, groupname, channelname, consumername, callback); });
|
|
42
|
-
};
|
|
43
|
-
exports.dataRequester = dataRequester;
|
|
44
|
-
var toApiListener = function (config, groupname, channelname, consumername, callback) {
|
|
45
|
-
var listenerclient = (0, memdb_1.getRedisClient)(config);
|
|
46
|
-
createGroup(listenerclient, groupname, channelname)
|
|
47
|
-
.then(function () { return listenForMessageForDataRequest(listenerclient, groupname, channelname, consumername, callback); });
|
|
48
|
-
};
|
|
49
|
-
exports.toApiListener = toApiListener;
|
|
50
|
-
var listenForMessageForApiRequest = function (listenerclient, groupname, channelname, consumername, callback) {
|
|
51
|
-
console.log("listening to ".concat(channelname, " as ").concat(consumername));
|
|
52
|
-
var streammsgid = '';
|
|
53
|
-
var messageid = '';
|
|
54
|
-
// block client while listening for a message
|
|
55
|
-
listenerclient.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>')
|
|
56
|
-
.then(function (results) {
|
|
57
|
-
return getMessagedataFromResult(results);
|
|
58
|
-
})
|
|
59
|
-
.then(function (_a) {
|
|
60
|
-
var messagedata = _a.messagedata, id = _a.id;
|
|
61
|
-
// messageid should have been set in getters/requestAndWaitForKey
|
|
62
|
-
if ('messageid' in messagedata)
|
|
63
|
-
messageid = messagedata.messageid;
|
|
64
|
-
//stream id from redis
|
|
65
|
-
streammsgid = id;
|
|
66
|
-
return messagedata;
|
|
67
|
-
})
|
|
68
|
-
.then(function (messagedata) {
|
|
69
|
-
return callback(messagedata, streammsgid);
|
|
70
|
-
})
|
|
71
|
-
.then(function (result) {
|
|
72
|
-
return (0, setters_1.publishMessageResponse)(listenerclient, messageid, result != null ? result : '');
|
|
73
|
-
})
|
|
74
|
-
.catch(function (e) {
|
|
75
|
-
console.log(e);
|
|
76
|
-
return (0, setters_1.publishMessageResponse)(listenerclient, messageid, '');
|
|
77
|
-
})
|
|
78
|
-
.finally(function () {
|
|
79
|
-
listenerclient.xack(channelname, groupname, streammsgid).then(function () {
|
|
80
|
-
console.log(streammsgid + ' acknowledged');
|
|
81
|
-
});
|
|
82
|
-
listenForMessageForApiRequest(listenerclient, groupname, channelname, consumername, callback);
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
var getMessagedataFromResult = function (results) {
|
|
86
|
-
if (!results.length)
|
|
87
|
-
throw ('no-messagedata');
|
|
88
|
-
var result = results[0], messages = result[1];
|
|
89
|
-
if (!messages.length)
|
|
90
|
-
throw ('no-messagedata');
|
|
91
|
-
var message = messages[0], id = message[0], keysandvalues = message[1];
|
|
92
|
-
var messagedata = {};
|
|
93
|
-
keysandvalues.forEach(function (key, n) {
|
|
94
|
-
if (!(n == 0 || n % 2 == 0))
|
|
95
|
-
return;
|
|
96
|
-
messagedata[key] = keysandvalues[n + 1];
|
|
97
|
-
});
|
|
98
|
-
return { messagedata: messagedata, id: id };
|
|
99
|
-
};
|
|
100
|
-
var listenForMessageForDataRequest = function (listenerclient, groupname, channelname, consumername, callback) {
|
|
101
|
-
console.log("listening to ".concat(channelname, " as ").concat(consumername));
|
|
102
|
-
var streammsgid = '';
|
|
103
|
-
var messageid = '';
|
|
104
|
-
// block client while listening for a message
|
|
105
|
-
listenerclient.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>')
|
|
106
|
-
.then(function (results) {
|
|
107
|
-
return getMessagedataFromResult(results);
|
|
108
|
-
})
|
|
109
|
-
.then(function (_a) {
|
|
110
|
-
var messagedata = _a.messagedata, id = _a.id;
|
|
111
|
-
// messageid should have been set in getters/requestAndWaitForKey
|
|
112
|
-
if ('messageid' in messagedata)
|
|
113
|
-
messageid = messagedata.messageid;
|
|
114
|
-
//stream id from redis
|
|
115
|
-
streammsgid = id;
|
|
116
|
-
return messagedata;
|
|
117
|
-
})
|
|
118
|
-
.then(function (messagedata) {
|
|
119
|
-
return callback(messagedata, streammsgid);
|
|
120
|
-
})
|
|
121
|
-
.then(function (result) {
|
|
122
|
-
return (0, setters_1.publishMessageResponse)(listenerclient, messageid, result != null ? result : '');
|
|
123
|
-
})
|
|
124
|
-
.catch(function (e) {
|
|
125
|
-
return (0, setters_1.publishMessageResponse)(listenerclient, messageid, JSON.stringify({
|
|
126
|
-
rejected: true,
|
|
127
|
-
error: e
|
|
128
|
-
}));
|
|
129
|
-
})
|
|
130
|
-
.finally(function () {
|
|
131
|
-
listenerclient.xack(channelname, groupname, streammsgid).then(function () {
|
|
132
|
-
console.log(streammsgid + ' acknowledged');
|
|
133
|
-
});
|
|
134
|
-
listenForMessageForDataRequest(listenerclient, groupname, channelname, consumername, callback);
|
|
135
|
-
});
|
|
136
|
-
};
|
|
137
|
-
var listenForMessage = function (client, groupname, channelname, consumername, callback, lastId) {
|
|
138
|
-
if (lastId === void 0) { lastId = "$"; }
|
|
139
|
-
console.log("listening to ".concat(channelname, " as ").concat(consumername));
|
|
140
|
-
client.xreadgroup('GROUP', groupname, consumername, 'COUNT', 1, 'BLOCK', 0, 'STREAMS', channelname, '>')
|
|
141
|
-
.then(function (results) {
|
|
142
|
-
results.forEach(function (result) {
|
|
143
|
-
var group = result[0];
|
|
144
|
-
var messages = result[1];
|
|
145
|
-
messages.forEach(function (message) {
|
|
146
|
-
var id = message[0];
|
|
147
|
-
var keysandvalues = message[1];
|
|
148
|
-
var messagedata = {};
|
|
149
|
-
keysandvalues.forEach(function (key, n) {
|
|
150
|
-
if (!(n == 0 || n % 2 == 0))
|
|
151
|
-
return;
|
|
152
|
-
messagedata[key] = keysandvalues[n + 1];
|
|
153
|
-
});
|
|
154
|
-
callback(messagedata, id, client).catch(function (error) {
|
|
155
|
-
console.log(error);
|
|
156
|
-
}).finally(function () {
|
|
157
|
-
// not sure yet? For now always acknowledge
|
|
158
|
-
client.xack(channelname, groupname, id).then(function () {
|
|
159
|
-
console.log(id + ' acknowledged');
|
|
160
|
-
});
|
|
161
|
-
// relisten
|
|
162
|
-
listenForMessage(client, groupname, channelname, consumername, callback);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
})
|
|
167
|
-
.catch(function (error) {
|
|
168
|
-
console.log(error);
|
|
169
|
-
});
|
|
170
|
-
};
|
package/dist/memdb.d.ts
DELETED