wingbot-mongodb 2.21.2 → 2.22.0-alpha.3

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 +1 @@
1
- {"processes":{"32821199-22af-42bd-8e0d-c255510f3efa":{"parent":null,"children":[]}},"files":{"/Users/david/Downloads/wingbot-mongodb/src/AttachmentCache.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/AuditLogStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/BaseStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/BotConfigStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/BotTokenStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/tokenFactory.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/ChatLogStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/NotificationsStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"],"/Users/david/Downloads/wingbot-mongodb/src/StateStorage.js":["32821199-22af-42bd-8e0d-c255510f3efa"]},"externalIds":{}}
1
+ {"processes":{"75b2d1cb-d4fe-472b-a674-acdcb631b536":{"parent":null,"children":[]}},"files":{"/Users/david/Development/wingbot-mongodb/src/AttachmentCache.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/AuditLogStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BaseStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/defaultLogger.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BotConfigStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/BotTokenStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/tokenFactory.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/ChatLogStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/NotificationsStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"],"/Users/david/Development/wingbot-mongodb/src/StateStorage.js":["75b2d1cb-d4fe-472b-a674-acdcb631b536"]},"externalIds":{}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot-mongodb",
3
- "version": "2.21.2",
3
+ "version": "2.22.0-alpha.3",
4
4
  "description": "MongoDB storage for wingbot.ai",
5
5
  "main": "src/main.js",
6
6
  "scripts": {
@@ -45,12 +45,12 @@
45
45
  "jsdoc-to-markdown": "^7.1.1",
46
46
  "jsdoc-tsimport-plugin": "^1.0.5",
47
47
  "mocha": "^9.2.2",
48
- "mongodb": "^3.7.3",
48
+ "mongodb": "^4.5.0",
49
49
  "nyc": "^15.1.0",
50
- "wingbot": "^3.30.0"
50
+ "wingbot": "^3.30.2"
51
51
  },
52
52
  "peerDependencies": {
53
- "mongodb": "^3.0.0"
53
+ "mongodb": "^4.0.0"
54
54
  },
55
55
  "optionalDependencies": {
56
56
  "wingbot": "^3.0.0"
@@ -3,7 +3,8 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
6
+ /** @typedef {import('mongodb').Db} Db */
7
+ /** @typedef {import('mongodb').Collection} Collection */
7
8
 
8
9
  /**
9
10
  * Cache storage for Facebook attachments
@@ -14,7 +15,7 @@ class AttachmentCache {
14
15
 
15
16
  /**
16
17
  *
17
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
18
+ * @param {Db|{():Promise<Db>}} mongoDb
18
19
  * @param {string} collectionName
19
20
  */
20
21
  constructor (mongoDb, collectionName = 'attachments') {
@@ -22,13 +23,13 @@ class AttachmentCache {
22
23
  this._collectionName = collectionName;
23
24
 
24
25
  /**
25
- * @type {mongodb.Collection}
26
+ * @type {Collection}
26
27
  */
27
28
  this._collection = null;
28
29
  }
29
30
 
30
31
  /**
31
- * @returns {Promise<mongodb.Collection>}
32
+ * @returns {Promise<Collection>}
32
33
  */
33
34
  async _getCollection () {
34
35
  if (this._collection === null) {
@@ -5,8 +5,7 @@
5
5
 
6
6
  const jsonwebtoken = require('jsonwebtoken');
7
7
  const BaseStorage = require('./BaseStorage');
8
-
9
- /** @typedef {import('mongodb/lib/db')} Db */
8
+ const defaultLogger = require('./defaultLogger');
10
9
 
11
10
  const LEVEL_CRITICAL = 'Critical';
12
11
  const LEVEL_IMPORTANT = 'Important';
@@ -91,6 +90,9 @@ const TYPE_INFO = 'Info';
91
90
  * @returns {Promise}
92
91
  */
93
92
 
93
+ /** @typedef {import('mongodb').Db} Db */
94
+ /** @typedef {import('mongodb').Collection} Collection */
95
+
94
96
  /**
95
97
  * Storage for audit logs with signatures chain
96
98
  */
@@ -105,7 +107,7 @@ class AuditLogStorage extends BaseStorage {
105
107
  * @param {string|Promise<string>} [secret]
106
108
  * @param {string|Promise<string>} [jwtVerifier]
107
109
  */
108
- constructor (mongoDb, collectionName = 'auditlog', log = console, isCosmo = false, secret = null, jwtVerifier = null) {
110
+ constructor (mongoDb, collectionName = 'auditlog', log = defaultLogger, isCosmo = false, secret = null, jwtVerifier = null) {
109
111
  super(mongoDb, collectionName, log, isCosmo);
110
112
 
111
113
  this.addIndex({
@@ -5,9 +5,11 @@
5
5
 
6
6
  const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
7
  const crypto = require('crypto');
8
+ const defaultLogger = require('./defaultLogger');
8
9
 
9
- /** @typedef {import('mongodb/lib/db')} Db */
10
- /** @typedef {import('mongodb/lib/collection')} Collection */
10
+ /** @typedef {import('mongodb').Db} Db */
11
+ /** @typedef {import('mongodb').Collection} Collection */
12
+ /** @typedef {import('mongodb').CreateIndexesOptions} CreateIndexesOptions */
11
13
 
12
14
  class BaseStorage {
13
15
 
@@ -42,7 +44,7 @@ class BaseStorage {
42
44
  *
43
45
  * }
44
46
  */
45
- constructor (mongoDb, collectionName, log = console, isCosmo = false) {
47
+ constructor (mongoDb, collectionName, log = defaultLogger, isCosmo = false) {
46
48
  this._mongoDb = mongoDb;
47
49
  this._collectionName = collectionName;
48
50
  this._isCosmo = isCosmo;
@@ -76,7 +78,7 @@ class BaseStorage {
76
78
  * Add custom indexing rule
77
79
  *
78
80
  * @param {object} index
79
- * @param {mongodb.IndexOptions} options
81
+ * @param {CreateIndexesOptions} options
80
82
  */
81
83
  addIndex (index, options) {
82
84
  if (!options.name) {
@@ -209,7 +211,7 @@ class BaseStorage {
209
211
 
210
212
  /**
211
213
  *
212
- * @private
214
+ * @protected
213
215
  * @template T
214
216
  * @param {T} object
215
217
  * @returns {T}
@@ -11,8 +11,8 @@ try {
11
11
  // noop
12
12
  }
13
13
 
14
- /** @typedef {import('mongodb/lib/db')} Db */
15
- /** @typedef {import('mongodb/lib/collection')} Collection */
14
+ /** @typedef {import('mongodb').Db} Db */
15
+ /** @typedef {import('mongodb').Collection} Collection */
16
16
 
17
17
  const CONFIG_ID = 'config';
18
18
 
@@ -3,7 +3,6 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
6
  const tokenFactory = require('./tokenFactory');
8
7
 
9
8
  const TOKEN_INDEX = 'token-index';
@@ -16,6 +15,9 @@ const USER_INDEX = 'user-page-index';
16
15
  * @prop {string} token
17
16
  */
18
17
 
18
+ /** @typedef {import('mongodb').Db} Db */
19
+ /** @typedef {import('mongodb').Collection} Collection */
20
+
19
21
  /**
20
22
  * Storage for webview tokens
21
23
  *
@@ -25,7 +27,7 @@ class BotTokenStorage {
25
27
 
26
28
  /**
27
29
  *
28
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
30
+ * @param {Db|{():Promise<Db>}} mongoDb
29
31
  * @param {string} collectionName
30
32
  */
31
33
  constructor (mongoDb, collectionName = 'tokens') {
@@ -33,13 +35,13 @@ class BotTokenStorage {
33
35
  this._collectionName = collectionName;
34
36
 
35
37
  /**
36
- * @type {mongodb.Collection}
38
+ * @type {Collection}
37
39
  */
38
40
  this._collection = null;
39
41
  }
40
42
 
41
43
  /**
42
- * @returns {Promise<mongodb.Collection>}
44
+ * @returns {Promise<Collection>}
43
45
  */
44
46
  async _getCollection () {
45
47
  if (this._collection === null) {
@@ -60,8 +62,7 @@ class BotTokenStorage {
60
62
  token: 1
61
63
  }, {
62
64
  unique: true,
63
- name: TOKEN_INDEX,
64
- dropDups: true
65
+ name: TOKEN_INDEX
65
66
  });
66
67
  }
67
68
  try {
@@ -75,8 +76,7 @@ class BotTokenStorage {
75
76
  pageId: 1
76
77
  }, {
77
78
  unique: true,
78
- name: USER_INDEX,
79
- dropDups: true
79
+ name: USER_INDEX
80
80
  });
81
81
  }
82
82
  }
@@ -4,11 +4,12 @@
4
4
  'use strict';
5
5
 
6
6
  const BaseStorage = require('./BaseStorage');
7
+ const defaultLogger = require('./defaultLogger');
7
8
 
8
9
  const PAGE_SENDER_TIMESTAMP = 'pageId_1_senderId_1_timestamp_-1';
9
10
  const TIMESTAMP = 'timestamp_1';
10
11
 
11
- /** @typedef {import('mongodb/lib/db')} Db */
12
+ /** @typedef {import('mongodb').Db} Db */
12
13
 
13
14
  /**
14
15
  * Storage for conversation logs
@@ -25,7 +26,7 @@ class ChatLogStorage extends BaseStorage {
25
26
  * @param {boolean} [isCosmo]
26
27
  * @param {string|Promise<string>} [secret]
27
28
  */
28
- constructor (mongoDb, collectionName = 'chatlogs', log = console, isCosmo = false, secret = null) {
29
+ constructor (mongoDb, collectionName = 'chatlogs', log = defaultLogger, isCosmo = false, secret = null) {
29
30
  super(mongoDb, collectionName, log, isCosmo);
30
31
 
31
32
  this.addIndex({
@@ -4,6 +4,7 @@
4
4
  'use strict';
5
5
 
6
6
  const mongodb = require('mongodb');
7
+ const defaultLogger = require('./defaultLogger');
7
8
 
8
9
  const { ObjectID } = mongodb;
9
10
 
@@ -84,7 +85,7 @@ class NotificationsStorage {
84
85
  * @param {{error:Function,log:Function}} [log] - console like logger
85
86
  * @param {boolean} isCosmo
86
87
  */
87
- constructor (mongoDb, collectionsPrefix = '', log = console, isCosmo = false) {
88
+ constructor (mongoDb, collectionsPrefix = '', log = defaultLogger, isCosmo = false) {
88
89
  this._mongoDb = mongoDb;
89
90
 
90
91
  this.taksCollection = `${collectionsPrefix}notification-tasks`;
@@ -3,8 +3,8 @@
3
3
  */
4
4
  'use strict';
5
5
 
6
- const mongodb = require('mongodb'); // eslint-disable-line no-unused-vars
7
6
  const BaseStorage = require('./BaseStorage');
7
+ const defaultLogger = require('./defaultLogger');
8
8
 
9
9
  const USER_INDEX = 'senderId_1_pageId_1';
10
10
  const LAST_INTERACTION_INDEX = 'lastInteraction_1';
@@ -22,6 +22,8 @@ const SEARCH = 'search-text';
22
22
  * @prop {string} [search]
23
23
  */
24
24
 
25
+ /** @typedef {import('mongodb').Db} Db */
26
+
25
27
  /**
26
28
  * Storage for chat states
27
29
  *
@@ -31,17 +33,17 @@ class StateStorage extends BaseStorage {
31
33
 
32
34
  /**
33
35
  *
34
- * @param {mongodb.Db|{():Promise<mongodb.Db>}} mongoDb
36
+ * @param {Db|{():Promise<Db>}} mongoDb
35
37
  * @param {string} collectionName
36
38
  * @param {{error:Function,log:Function}} [log] - console like logger
37
39
  * @param {boolean} isCosmo
38
40
  */
39
- constructor (mongoDb, collectionName = 'states', log = console, isCosmo = false) {
41
+ constructor (mongoDb, collectionName = 'states', log = defaultLogger, isCosmo = false) {
40
42
  super(mongoDb, collectionName, log, isCosmo);
41
43
 
42
44
  this.addIndex(
43
45
  { senderId: 1, pageId: 1 },
44
- { name: USER_INDEX, unique: true, dropDups: true }
46
+ { name: USER_INDEX, unique: true }
45
47
  );
46
48
  this.addIndex(
47
49
  { lastInteraction: isCosmo ? 1 : -1 },
@@ -77,7 +79,9 @@ class StateStorage extends BaseStorage {
77
79
  */
78
80
  async getState (senderId, pageId) {
79
81
  const c = await this._getCollection();
80
- return c.findOne({ senderId, pageId }, { projection: { _id: 0 } });
82
+ const doc = await c.findOne({ senderId, pageId }, { projection: { _id: 0 } });
83
+ // @ts-ignore
84
+ return doc;
81
85
  }
82
86
 
83
87
  /**
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @author {David Menger}
3
+ */
4
+ 'use strict';
5
+
6
+ function log (...args) {
7
+ const mapped = args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a, null, 2)));
8
+ process.stderr.write(` + ${mapped.join(' ')}\n`);
9
+ }
10
+
11
+ const defaultLogger = {
12
+ log,
13
+ error: (...args) => log('ERROR:', ...args),
14
+ info: log,
15
+ warn: (...args) => log('WARN:', ...args),
16
+ debug: log
17
+ };
18
+
19
+ module.exports = defaultLogger;