woonplan-packages-redishelper 1.0.69 → 1.0.70

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/dist/setters.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { Redis } from "ioredis";
2
- import { RedisMessage } from "./types";
2
+ import { RedisConfig, RedisMessage, Struct } from "./types";
3
3
  declare const publishMessageResponse: (client: Redis, messageid: string, message: string) => Promise<number>;
4
4
  declare const sendMessageToStream: (client: Redis, channelname: string, message: RedisMessage) => Promise<string>;
5
- export { publishMessageResponse, sendMessageToStream };
5
+ export interface SocketToApiOptions {
6
+ endpoint: string;
7
+ data: Struct;
8
+ method: string;
9
+ }
10
+ declare const socketToApi: (config: RedisConfig, options?: SocketToApiOptions) => Promise<unknown>;
11
+ export { publishMessageResponse, sendMessageToStream, socketToApi };
package/dist/setters.js CHANGED
@@ -1,4 +1,15 @@
1
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
+ };
2
13
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
14
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
15
  if (ar || !(i in from)) {
@@ -9,8 +20,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
9
20
  return to.concat(ar || Array.prototype.slice.call(from));
10
21
  };
11
22
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.sendMessageToStream = exports.publishMessageResponse = void 0;
23
+ exports.socketToApi = exports.sendMessageToStream = exports.publishMessageResponse = void 0;
24
+ var memdb_1 = require("./memdb");
13
25
  var utils_1 = require("./utils");
26
+ var uuid_1 = require("uuid");
27
+ var data_1 = require("./data");
14
28
  var publishMessageResponse = function (client, messageid, message) {
15
29
  //console.log( `publishing to ${messageid} at ${new Date().toISOString()}` )
16
30
  return client.publish((0, utils_1.getMessageReponseKey)(messageid), message);
@@ -22,3 +36,29 @@ var sendMessageToStream = function (client, channelname, message) {
22
36
  return client.xadd.apply(client, __spreadArray([channelname, '*'], arrayOfKeysAndValues, false));
23
37
  };
24
38
  exports.sendMessageToStream = sendMessageToStream;
39
+ var timeout = 1500;
40
+ // send a message from socket to api, creating a message id to publish the response on
41
+ var socketToApi = function (config, options) {
42
+ if (options === void 0) { options = { endpoint: '', method: 'GET', data: {}, }; }
43
+ return new Promise(function (resolve, reject) {
44
+ var client = (0, memdb_1.getRedisClient)(config), callbackclient = (0, memdb_1.getRedisClient)(config), messageid = (0, uuid_1.v4)();
45
+ (0, data_1.subscribeToMessageResponse)(callbackclient, messageid)
46
+ .then(function () {
47
+ callbackclient.once("message", function (_channel, message) {
48
+ resolve(message.length ? message : null);
49
+ callbackclient.disconnect();
50
+ });
51
+ // setup timeout
52
+ setTimeout(function () {
53
+ if (callbackclient.status != "end") {
54
+ console.log("sub timedout");
55
+ reject('timeout');
56
+ callbackclient.disconnect();
57
+ }
58
+ }, timeout);
59
+ return sendMessageToStream(client, 'api', __assign(__assign({}, options), { messageid: messageid }));
60
+ })
61
+ .catch(reject);
62
+ });
63
+ };
64
+ exports.socketToApi = socketToApi;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "woonplan-packages-redishelper",
3
- "version": "1.0.69",
3
+ "version": "1.0.70",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/setters.ts CHANGED
@@ -1,7 +1,10 @@
1
1
 
2
2
  import { Redis } from "ioredis"
3
- import { RedisMessage } from "./types"
3
+ import { getRedisClient } from "./memdb"
4
+ import { RedisConfig, RedisMessage, Struct } from "./types"
4
5
  import { getMessageReponseKey, redisMessageToArrayOfKeysAndValues, reduceArraysToArray } from "./utils"
6
+ import { v4 as uuidv4 } from 'uuid';
7
+ import { subscribeToMessageResponse } from "./data";
5
8
 
6
9
  const publishMessageResponse = ( client:Redis, messageid:string, message:string ) => {
7
10
  //console.log( `publishing to ${messageid} at ${new Date().toISOString()}` )
@@ -15,6 +18,44 @@ const sendMessageToStream = ( client:Redis, channelname:string, message:RedisMes
15
18
  return client.xadd( channelname, '*', ...arrayOfKeysAndValues )
16
19
  }
17
20
 
21
+ export interface SocketToApiOptions{
22
+ endpoint:string,
23
+ data : Struct
24
+ method:string
25
+ }
26
+
27
+ const timeout = 1500
28
+
29
+ // send a message from socket to api, creating a message id to publish the response on
30
+ const socketToApi = ( config:RedisConfig, options:SocketToApiOptions = { endpoint : '', method: 'GET', data : {}, } ) => new Promise((resolve, reject) => {
31
+ const client = getRedisClient( config ),
32
+ callbackclient = getRedisClient( config ),
33
+ messageid = uuidv4()
34
+
35
+ subscribeToMessageResponse( callbackclient, messageid )
36
+ .then(() => {
37
+ callbackclient.once( "message", ( _channel, message ) => {
38
+ resolve( message.length ? message : null )
39
+ callbackclient.disconnect()
40
+ })
41
+
42
+ // setup timeout
43
+ setTimeout( () => {
44
+ if( callbackclient.status != "end" ){
45
+ console.log( `sub timedout`)
46
+ reject( 'timeout' )
47
+ callbackclient.disconnect()
48
+ }
49
+ }, timeout )
50
+
51
+ return sendMessageToStream( client, 'api', {
52
+ ...options,
53
+ messageid : messageid
54
+ })
55
+ })
56
+ .catch( reject )
57
+
58
+ })
18
59
 
19
60
 
20
- export { publishMessageResponse, sendMessageToStream }
61
+ export { publishMessageResponse, sendMessageToStream, socketToApi }