ueberdb2 4.0.11 → 4.0.17

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.
Files changed (81) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.cjs +44 -5
  3. package/.github/workflows/npmpublish.yml +3 -3
  4. package/databases/{cassandra_db.js → cassandra_db.ts} +45 -30
  5. package/databases/{couch_db.js → couch_db.ts} +78 -31
  6. package/databases/{dirty_db.js → dirty_db.ts} +19 -14
  7. package/databases/{dirty_git_db.js → dirty_git_db.ts} +19 -15
  8. package/databases/{elasticsearch_db.js → elasticsearch_db.ts} +30 -21
  9. package/databases/{memory_db.js → memory_db.ts} +8 -8
  10. package/databases/mock_db.ts +43 -0
  11. package/databases/{mongodb_db.js → mongodb_db.ts} +22 -16
  12. package/databases/{mssql_db.js → mssql_db.ts} +29 -21
  13. package/databases/{mysql_db.js → mysql_db.ts} +20 -15
  14. package/databases/{postgres_db.js → postgres_db.ts} +37 -22
  15. package/databases/{postgrespool_db.js → postgrespool_db.ts} +3 -3
  16. package/databases/redis_db.ts +129 -0
  17. package/databases/{rethink_db.js → rethink_db.ts} +35 -19
  18. package/databases/{sqlite_db.js → sqlite_db.ts} +37 -36
  19. package/dist/databases/cassandra_db.js +237 -0
  20. package/dist/databases/couch_db.js +181 -0
  21. package/dist/databases/dirty_db.js +78 -0
  22. package/dist/databases/dirty_git_db.js +77 -0
  23. package/dist/databases/elasticsearch_db.js +251 -0
  24. package/dist/databases/memory_db.js +39 -0
  25. package/dist/databases/mock_db.js +40 -0
  26. package/dist/databases/mongodb_db.js +127 -0
  27. package/dist/databases/mssql_db.js +187 -0
  28. package/dist/databases/mysql_db.js +170 -0
  29. package/dist/databases/postgres_db.js +192 -0
  30. package/dist/databases/postgrespool_db.js +12 -0
  31. package/dist/databases/redis_db.js +105 -0
  32. package/dist/databases/rethink_db.js +123 -0
  33. package/dist/databases/sqlite_db.js +140 -0
  34. package/dist/index.js +215 -0
  35. package/dist/lib/AbstractDatabase.js +38 -0
  36. package/dist/lib/CacheAndBufferLayer.js +657 -0
  37. package/dist/lib/logging.js +34 -0
  38. package/dist/test/lib/databases.js +72 -0
  39. package/dist/test/test.js +373 -0
  40. package/dist/test/test_bulk.js +74 -0
  41. package/dist/test/test_elasticsearch.js +157 -0
  42. package/dist/test/test_findKeys.js +69 -0
  43. package/dist/test/test_flush.js +83 -0
  44. package/dist/test/test_getSub.js +57 -0
  45. package/dist/test/test_lru.js +155 -0
  46. package/dist/test/test_memory.js +59 -0
  47. package/dist/test/test_metrics.js +772 -0
  48. package/dist/test/test_mysql.js +91 -0
  49. package/dist/test/test_postgres.js +40 -0
  50. package/dist/test/test_setSub.js +48 -0
  51. package/dist/test/test_tojson.js +62 -0
  52. package/docker-compose.yml +44 -0
  53. package/{index.js → index.ts} +76 -25
  54. package/lib/AbstractDatabase.ts +79 -0
  55. package/lib/{CacheAndBufferLayer.js → CacheAndBufferLayer.ts} +17 -16
  56. package/lib/{logging.js → logging.ts} +10 -6
  57. package/package.json +18 -3
  58. package/test/lib/{databases.js → databases.ts} +8 -5
  59. package/test/test.ts +328 -0
  60. package/test/test_bulk.ts +69 -0
  61. package/test/{test_elasticsearch.js → test_elasticsearch.ts} +48 -53
  62. package/test/{test_findKeys.js → test_findKeys.ts} +15 -17
  63. package/test/{test_flush.js → test_flush.ts} +16 -22
  64. package/test/test_getSub.ts +28 -0
  65. package/test/test_lru.ts +151 -0
  66. package/test/test_memory.ts +32 -0
  67. package/test/{test_metrics.js → test_metrics.ts} +73 -68
  68. package/test/{test_mysql.js → test_mysql.ts} +16 -22
  69. package/test/test_postgres.ts +16 -0
  70. package/test/{test_setSub.js → test_setSub.ts} +8 -12
  71. package/test/test_tojson.ts +34 -0
  72. package/databases/mock_db.js +0 -42
  73. package/databases/redis_db.js +0 -96
  74. package/lib/AbstractDatabase.js +0 -37
  75. package/test/test.js +0 -328
  76. package/test/test_bulk.js +0 -69
  77. package/test/test_getSub.js +0 -31
  78. package/test/test_lru.js +0 -145
  79. package/test/test_memory.js +0 -31
  80. package/test/test_postgres.js +0 -16
  81. package/test/test_tojson.js +0 -37
@@ -0,0 +1,140 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Database = void 0;
7
+ /**
8
+ * 2011 Peter 'Pita' Martischka
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS-IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ let SQDB;
23
+ try {
24
+ SQDB = require('sqlite3').Database;
25
+ }
26
+ catch (err) {
27
+ throw new Error('sqlite3 not found. It was removed from ueberdb\'s dependencies because it requires ' +
28
+ 'compilation which fails on several systems. If you still want to use sqlite, run ' +
29
+ '"npm install sqlite3" in your etherpad-lite ./src directory.');
30
+ }
31
+ const AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
32
+ const util_1 = __importDefault(require("util"));
33
+ const escape = (val) => `'${val.replace(/'/g, "''")}'`;
34
+ const Database = class SQLiteDB extends AbstractDatabase_1.default {
35
+ db;
36
+ constructor(settings) {
37
+ super();
38
+ this.db = null;
39
+ if (!settings || !settings.filename) {
40
+ settings = { filename: ':memory:' };
41
+ }
42
+ this.settings = settings;
43
+ // set settings for the dbWrapper
44
+ if (settings.filename === ':memory:') {
45
+ this.settings.cache = 0;
46
+ this.settings.writeInterval = 0;
47
+ this.settings.json = true;
48
+ }
49
+ else {
50
+ this.settings.cache = 1000;
51
+ this.settings.writeInterval = 100;
52
+ this.settings.json = true;
53
+ }
54
+ }
55
+ init(callback) {
56
+ util_1.default.callbackify(async () => {
57
+ this.db = new SQDB(this.settings.filename);
58
+ await this._query('CREATE TABLE IF NOT EXISTS store (key TEXT PRIMARY KEY, value TEXT)');
59
+ // @ts-ignore
60
+ })(callback);
61
+ }
62
+ async _query(sql, params = []) {
63
+ // It is unclear how util.promisify() deals with variadic functions, so it is not used here.
64
+ return await new Promise((resolve, reject) => {
65
+ // According to sqlite3's documentation, .run() method (and maybe .all() and .get(); the
66
+ // documentation is unclear) might call the callback multiple times. That's OK -- ECMAScript
67
+ // guarantees that it is safe to call a Promise executor's resolve and reject functions
68
+ // multiple times. The subsequent calls are ignored, except Node.js's 'process' object emits a
69
+ // 'multipleResolves' event to aid in debugging.
70
+ this.db && this.db.all(sql, params, (err, rows) => {
71
+ if (err != null)
72
+ return reject(err);
73
+ resolve(rows);
74
+ });
75
+ });
76
+ }
77
+ // Temporary callbackified version of _query. This will be removed once all database objects are
78
+ // asyncified.
79
+ _queryCb(sql, params, callback) {
80
+ // It is unclear how util.callbackify() handles optional parameters, so it is not used here.
81
+ const p = this._query(sql, params);
82
+ if (callback)
83
+ p.then((rows) => callback(null, rows), (err) => callback(err || new Error(err)));
84
+ }
85
+ get(key, callback) {
86
+ this._queryCb('SELECT value FROM store WHERE key = ?', [key], (err, rows) => callback(err, err == null && rows && rows.length ? rows[0].value : null));
87
+ }
88
+ findKeys(key, notKey, callback) {
89
+ let query = 'SELECT key FROM store WHERE key LIKE ?';
90
+ const params = [];
91
+ // desired keys are %key:%, e.g. pad:%
92
+ key = key.replace(/\*/g, '%');
93
+ params.push(key);
94
+ if (notKey != null) {
95
+ // not desired keys are notKey:%, e.g. %:%:%
96
+ notKey = notKey.replace(/\*/g, '%');
97
+ query += ' AND key NOT LIKE ?';
98
+ params.push(notKey);
99
+ }
100
+ this._queryCb(query, params, (err, results) => {
101
+ const value = [];
102
+ if (!err && Object.keys(results).length > 0) {
103
+ results.forEach((val) => {
104
+ value.push(val.key);
105
+ });
106
+ }
107
+ callback(err, value);
108
+ });
109
+ }
110
+ set(key, value, callback) {
111
+ this._queryCb('REPLACE INTO store VALUES (?,?)', [key, value], callback);
112
+ }
113
+ remove(key, callback) {
114
+ this._queryCb('DELETE FROM store WHERE key = ?', [key], callback);
115
+ }
116
+ doBulk(bulk, callback) {
117
+ let sql = 'BEGIN TRANSACTION;\n';
118
+ for (const i in bulk) {
119
+ if (bulk[i].type === 'set') {
120
+ sql += `REPLACE INTO store VALUES (${escape(bulk[i].key)}, ${escape(bulk[i].value)});\n`;
121
+ }
122
+ else if (bulk[i].type === 'remove') {
123
+ sql += `DELETE FROM store WHERE key = ${escape(bulk[i].key)};\n`;
124
+ }
125
+ }
126
+ sql += 'END TRANSACTION;';
127
+ this.db && this.db.exec(sql, (err) => {
128
+ if (err) {
129
+ console.error('ERROR WITH SQL: ');
130
+ console.error(sql);
131
+ }
132
+ callback(err);
133
+ });
134
+ }
135
+ close(callback) {
136
+ callback();
137
+ this.db && this.db.close((err) => callback(err));
138
+ }
139
+ };
140
+ exports.Database = Database;
package/dist/index.js ADDED
@@ -0,0 +1,215 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Database = void 0;
7
+ /**
8
+ * 2011 Peter 'Pita' Martischka
9
+ * 2020 John McLear
10
+ *
11
+ * Licensed under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License.
13
+ * You may obtain a copy of the License at
14
+ *
15
+ * http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS-IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ */
23
+ // @ts-ignore
24
+ const CacheAndBufferLayer_1 = __importDefault(require("./lib/CacheAndBufferLayer"));
25
+ const logging_1 = require("./lib/logging");
26
+ const util_1 = require("util");
27
+ const cbDb = {
28
+ init: () => { },
29
+ flush: () => { },
30
+ get: () => { },
31
+ remove: () => { },
32
+ findKeys: () => { },
33
+ close: () => { },
34
+ getSub: () => { },
35
+ setSub: () => { },
36
+ };
37
+ const fns = ['close', 'findKeys', 'flush', 'get', 'getSub', 'init', 'remove', 'set', 'setSub'];
38
+ for (const fn of fns) { // @ts-ignore
39
+ cbDb[fn] = (0, util_1.callbackify)(CacheAndBufferLayer_1.default.Database.prototype[fn]);
40
+ }
41
+ const makeDoneCallback = (callback, deprecated) => (err) => {
42
+ if (callback)
43
+ callback(err);
44
+ if (deprecated)
45
+ deprecated(err);
46
+ if (err != null && callback == null && deprecated == null)
47
+ throw err;
48
+ };
49
+ const Database = class {
50
+ type;
51
+ dbModule;
52
+ dbSettings;
53
+ wrapperSettings;
54
+ logger;
55
+ db;
56
+ metrics;
57
+ /**
58
+ * @param type The type of the database
59
+ * @param dbSettings The settings for that specific database type
60
+ * @param wrapperSettings
61
+ * @param logger Optional logger object. If no logger object is provided no logging will occur.
62
+ * The logger object is expected to be a log4js logger object or `console`. A logger object
63
+ * from another logging library should also work, but performance may be reduced if the logger
64
+ * object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
65
+ */
66
+ constructor(type, dbSettings, wrapperSettings, logger = null) {
67
+ if (!type) {
68
+ type = 'sqlite';
69
+ dbSettings = null;
70
+ wrapperSettings = null;
71
+ }
72
+ // saves all settings and require the db module
73
+ this.type = type;
74
+ this.dbModule = require(`./databases/${type}_db`);
75
+ this.dbSettings = dbSettings;
76
+ this.wrapperSettings = wrapperSettings;
77
+ this.logger = (0, logging_1.normalizeLogger)(logger);
78
+ const db = new this.dbModule.Database(this.dbSettings);
79
+ db.logger = this.logger;
80
+ this.db = new CacheAndBufferLayer_1.default.Database(db, this.wrapperSettings, this.logger);
81
+ // Expose the cache wrapper's metrics to the user. See lib/CacheAndBufferLayer.js for details.
82
+ //
83
+ // WARNING: This feature is EXPERIMENTAL -- do not assume it will continue to exist in its
84
+ // current form in a future version.
85
+ this.metrics = this.db.metrics;
86
+ }
87
+ /**
88
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
89
+ */
90
+ init(callback = null) {
91
+ if (callback != null) {
92
+ return cbDb.init.call(this.db);
93
+ }
94
+ return this.db.init();
95
+ }
96
+ /**
97
+ * Wrapper functions
98
+ */
99
+ /**
100
+ * Deprecated synonym of flush().
101
+ *
102
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
103
+ */
104
+ doShutdown(callback = null) {
105
+ return this.flush(callback);
106
+ }
107
+ /**
108
+ * Writes any unsaved changes to the underlying database.
109
+ *
110
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
111
+ */
112
+ flush(callback = null) {
113
+ if (!cbDb || !cbDb.flush === undefined)
114
+ return null;
115
+ if (callback != null) { // @ts-ignore
116
+ return cbDb.flush.call(this.db, callback);
117
+ }
118
+ return this.db.flush();
119
+ }
120
+ /**
121
+ * @param key
122
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
123
+ */
124
+ get(key, callback = null) {
125
+ if (callback != null) { // @ts-ignore
126
+ return cbDb.get.call(this.db, key, callback);
127
+ }
128
+ return this.db.get(key);
129
+ }
130
+ /**
131
+ * @param key
132
+ * @param notKey
133
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
134
+ */
135
+ findKeys(key, notKey, callback = null) {
136
+ if (callback != null) { // @ts-ignore
137
+ return cbDb.findKeys.call(this.db, key, notKey, callback);
138
+ }
139
+ return this.db.findKeys(key, notKey);
140
+ }
141
+ /**
142
+ * Removes an entry from the database if present.
143
+ *
144
+ * @param key
145
+ * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
146
+ * underlying database driver. If null, a Promise is returned.
147
+ * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
148
+ */
149
+ remove(key, cb = null, deprecated = null) {
150
+ if (cb != null) { // @ts-ignore
151
+ return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
152
+ }
153
+ return this.db.remove(key);
154
+ }
155
+ /**
156
+ * Adds or changes the value of an entry.
157
+ *
158
+ * @param key
159
+ * @param value
160
+ * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
161
+ * underlying database driver. If null, a Promise is returned.
162
+ * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
163
+ */
164
+ set(key, value, cb = null, deprecated = null) {
165
+ if (cb != null) { // @ts-ignore
166
+ return cbDb.get.call(this.db, key, value, makeDoneCallback(cb, deprecated));
167
+ }
168
+ return this.db.set(key, value);
169
+ }
170
+ /**
171
+ * @param key
172
+ * @param sub
173
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
174
+ */
175
+ getSub(key, sub, callback = null) {
176
+ if (callback != null) { // @ts-ignore
177
+ return cbDb.getSub.call(this.db, key, sub, callback);
178
+ }
179
+ return this.db.getSub(key, sub);
180
+ }
181
+ /**
182
+ * Adds or changes a subvalue of an entry.
183
+ *
184
+ * @param key
185
+ * @param sub
186
+ * @param value
187
+ * @param cb Deprecated. Node-style callback. Called when the write has been committed to the
188
+ * underlying database driver. If null, a Promise is returned.
189
+ * @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
190
+ */
191
+ setSub(key, sub, value, cb = null, deprecated = null) {
192
+ if (cb != null) {
193
+ // @ts-ignore
194
+ return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
195
+ }
196
+ return this.db.setSub(key, sub, value);
197
+ }
198
+ /**
199
+ * Flushes unwritten changes then closes the connection to the underlying database. After this
200
+ * returns, any future call to a method on this object may result in an error.
201
+ *
202
+ * @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
203
+ */
204
+ close(callback = null) {
205
+ if (callback != null) { // @ts-ignore
206
+ return cbDb.close.call(this.db, callback);
207
+ }
208
+ return this.db.close();
209
+ }
210
+ };
211
+ exports.Database = Database;
212
+ /**
213
+ * Deprecated synonym of Database.
214
+ */
215
+ exports.database = exports.Database;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const logging_1 = require("./logging");
4
+ const nullLogger = (0, logging_1.normalizeLogger)(null);
5
+ // Format: All characters match themselves except * matches any zero or more characters. No
6
+ // backslash escaping is supported, so it is impossible to create a pattern that matches only the
7
+ // '*' character.
8
+ const simpleGlobToRegExp = (s) => s.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
9
+ class AbstractDatabase {
10
+ logger;
11
+ // @ts-ignore
12
+ settings;
13
+ constructor() {
14
+ if (new.target === module.exports) {
15
+ throw new TypeError('cannot instantiate Abstract Database directly');
16
+ }
17
+ for (const fn of ['init', 'close', 'get', 'findKeys', 'remove', 'set']) {
18
+ // @ts-ignore
19
+ if (typeof this[fn] !== 'function')
20
+ throw new TypeError(`method ${fn} not defined`);
21
+ }
22
+ this.logger = nullLogger;
23
+ }
24
+ /**
25
+ * For findKey regex. Used by document dbs like mongodb or dirty.
26
+ */
27
+ createFindRegex(key, notKey) {
28
+ let regex = `^(?=${simpleGlobToRegExp(key)}$)`;
29
+ if (notKey != null)
30
+ regex += `(?!${simpleGlobToRegExp(notKey)}$)`;
31
+ return new RegExp(regex);
32
+ }
33
+ doBulk(operations, cb) {
34
+ throw new Error('the doBulk method must be implemented if write caching is enabled');
35
+ }
36
+ get isAsync() { return false; }
37
+ }
38
+ exports.default = AbstractDatabase;