wingbot 3.25.1 → 3.26.0-alpha.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.25.1",
3
+ "version": "3.26.0-alpha.1",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/BotApp.js CHANGED
@@ -22,6 +22,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
22
22
  /** @typedef {import('./Processor').Plugin} Plugin */
23
23
 
24
24
  /** @typedef {import('./CallbackAuditLog')} AuditLog */
25
+ /** @typedef {import('./BotAppSender').TlsOptions} TlsOptions */
25
26
 
26
27
  /**
27
28
  * @typedef {object} BotAppOptions
@@ -32,6 +33,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
32
33
  * @prop {ChatLogStorage} [chatLogStorage]
33
34
  * @prop {boolean} [preferSynchronousResponse]
34
35
  * @prop {AuditLog} [auditLog]
36
+ * @prop {TlsOptions} [tls]
35
37
  *
36
38
  * @typedef {ProcessorOptions & BotAppOptions} Options
37
39
  */
@@ -63,6 +65,7 @@ class BotApp {
63
65
  appId = null,
64
66
  preferSynchronousResponse = false,
65
67
  auditLog = null,
68
+ tls = null,
66
69
  ...processorOptions
67
70
  } = options;
68
71
 
@@ -70,6 +73,7 @@ class BotApp {
70
73
  this._fetch = fetch; // mock
71
74
  this._appId = appId;
72
75
  this._auditLog = auditLog;
76
+ this._tls = tls;
73
77
 
74
78
  let { apiUrl } = options;
75
79
 
@@ -211,7 +215,8 @@ class BotApp {
211
215
  appId,
212
216
  secret,
213
217
  mid,
214
- fetch: this._fetch
218
+ fetch: this._fetch,
219
+ tls: this._tls
215
220
  };
216
221
 
217
222
  const sender = new BotAppSender(options, senderId, message, this._senderLogger);
@@ -6,17 +6,25 @@
6
6
  const jwt = require('jsonwebtoken');
7
7
  const { default: fetch, Headers } = require('node-fetch');
8
8
  const crypto = require('crypto');
9
+ const { Agent } = require('https');
9
10
  const { promisify } = require('util');
10
11
  const ReturnSender = require('./ReturnSender');
11
12
 
12
13
  /** @typedef {import('./ReturnSender').ReturnSenderOptions} ReturnSenderOptions */
13
14
  /** @typedef {import('./ReturnSender').ChatLogStorage} ChatLogStorage */
14
15
 
16
+ /**
17
+ * @typedef {object} TlsOptions
18
+ * @prop {string|Promise<string>} key
19
+ * @prop {string|Promise<string>} cert
20
+ */
21
+
15
22
  /**
16
23
  * @typedef {object} BotAppSenderOptions
17
24
  * @prop {string} apiUrl
18
25
  * @prop {string} pageId
19
26
  * @prop {string} appId
27
+ * @prop {TlsOptions} [tls]
20
28
  * @prop {string} [mid]
21
29
  * @prop {Function} [fetch]
22
30
  * @prop {string|Promise<string>} secret
@@ -47,6 +55,9 @@ class BotAppSender extends ReturnSender {
47
55
 
48
56
  this._secret = Promise.resolve(options.secret);
49
57
 
58
+ this._tls = options.tls;
59
+ this._agent = null;
60
+
50
61
  this._fetch = options.fetch || fetch;
51
62
  }
52
63
 
@@ -65,6 +76,21 @@ class BotAppSender extends ReturnSender {
65
76
  }, goodSecret);
66
77
  }
67
78
 
79
+ async _getAgent () {
80
+ if (!this._tls) {
81
+ return null;
82
+ }
83
+ if (!this._agent) {
84
+ const [key, cert] = await Promise.all([
85
+ Promise.resolve(this._tls.key), Promise.resolve(this._tls.cert)
86
+ ]);
87
+
88
+ this._agent = new Agent({ cert, key });
89
+ }
90
+
91
+ return this._agent;
92
+ }
93
+
68
94
  async _send (payload) {
69
95
  // attach sender
70
96
  if (typeof payload.sender === 'undefined') {
@@ -78,14 +104,19 @@ class BotAppSender extends ReturnSender {
78
104
 
79
105
  const body = JSON.stringify(payload);
80
106
 
81
- const token = await BotAppSender.signBody(body, this._secret, this._appId);
107
+ const [token, agent] = await Promise.all([
108
+ BotAppSender.signBody(body, this._secret, this._appId),
109
+ this._getAgent()
110
+ ]);
82
111
 
83
112
  const headers = new Headers();
84
113
 
85
114
  headers.set('Authorization', token);
86
115
  headers.set('Content-Type', 'application/json');
87
116
 
88
- const response = await this._fetch(this._apiUrl, { headers, body, method: 'POST' })
117
+ const response = await this._fetch(this._apiUrl, {
118
+ headers, body, agent, method: 'POST'
119
+ })
89
120
  .then((r) => r.json());
90
121
 
91
122
  const { request, errors = null } = response;