Skip to content

Commit f637f8a

Browse files
committed
Converted more tests.
1 parent 8459a52 commit f637f8a

26 files changed

+180
-90
lines changed

src/node/db/AuthorManager.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* limitations under the License.
2020
*/
2121

22-
const db = require('./DB');
22+
import db from './DB';
2323
const CustomError = require('../utils/customError');
2424
const hooks = require('../../static/js/pluginfw/hooks.js');
2525
const {randomString, padutils: {warnDeprecated}} = require('../../static/js/pad_utils');
@@ -131,6 +131,7 @@ const mapAuthorWithDBKey = async (mapperkey: string, mapper:string) => {
131131

132132
// there is an author with this mapper
133133
// update the timestamp of this author
134+
// @ts-ignore
134135
await db.setSub(`globalAuthor:${author}`, ['timestamp'], Date.now());
135136

136137
// return the author
@@ -222,6 +223,7 @@ exports.getAuthor = async (author: string) => await db.get(`globalAuthor:${autho
222223
* Returns the color Id of the author
223224
* @param {String} author The id of the author
224225
*/
226+
// @ts-ignore
225227
exports.getAuthorColorId = async (author: string) => await db.getSub(`globalAuthor:${author}`, ['colorId']);
226228

227229
/**
@@ -230,12 +232,14 @@ exports.getAuthorColorId = async (author: string) => await db.getSub(`globalAuth
230232
* @param {String} colorId The color id of the author
231233
*/
232234
exports.setAuthorColorId = async (author: string, colorId: string) => await db.setSub(
233-
`globalAuthor:${author}`, ['colorId'], colorId);
235+
// @ts-ignore
236+
`globalAuthor:${author}`, ['colorId'], colorId);
234237

235238
/**
236239
* Returns the name of the author
237240
* @param {String} author The id of the author
238241
*/
242+
// @ts-ignore
239243
exports.getAuthorName = async (author: string) => await db.getSub(`globalAuthor:${author}`, ['name']);
240244

241245
/**
@@ -244,7 +248,8 @@ exports.getAuthorName = async (author: string) => await db.getSub(`globalAuthor:
244248
* @param {String} name The name of the author
245249
*/
246250
exports.setAuthorName = async (author: string, name: string) => await db.setSub(
247-
`globalAuthor:${author}`, ['name'], name);
251+
// @ts-ignore
252+
`globalAuthor:${author}`, ['name'], name);
248253

249254
/**
250255
* Returns an array of all pads this author contributed to

src/node/db/DB.ts

+33-15
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,55 @@
2424
import {Database} from 'ueberdb2';
2525
import settings from '../utils/Settings';
2626
import log4js from 'log4js';
27-
const stats = require('../stats')
27+
import stats from '../stats';
2828

2929
const logger = log4js.getLogger('ueberDB');
3030

3131
/**
3232
* The UeberDB Object that provides the database functions
3333
*/
34-
exports.db = null;
34+
export let db:Database|null = null;
3535

3636
/**
3737
* Initializes the database with the settings provided by the settings module
3838
*/
39-
exports.init = async () => {
40-
exports.db = new Database(settings.dbType, settings.dbSettings, null, logger);
41-
await exports.db.init();
42-
if (exports.db.metrics != null) {
43-
for (const [metric, value] of Object.entries(exports.db.metrics)) {
39+
export const init = async () => {
40+
db = new Database(settings.dbType, settings.dbSettings, null, logger);
41+
await db.init();
42+
if (db.metrics != null) {
43+
for (const [metric, value] of Object.entries(db.metrics)) {
4444
if (typeof value !== 'number') continue;
45-
stats.gauge(`ueberdb_${metric}`, () => exports.db.metrics[metric]);
45+
stats.gauge(`ueberdb_${metric}`, () => db!.metrics[metric]);
4646
}
4747
}
4848
for (const fn of ['get', 'set', 'findKeys', 'getSub', 'setSub', 'remove']) {
49-
const f = exports.db[fn];
50-
exports[fn] = async (...args:string[]) => await f.call(exports.db, ...args);
51-
Object.setPrototypeOf(exports[fn], Object.getPrototypeOf(f));
52-
Object.defineProperties(exports[fn], Object.getOwnPropertyDescriptors(f));
49+
// @ts-ignore
50+
const f = db[fn];
51+
// @ts-ignore
52+
dbInstance[fn] = async (...args:string[]) => await f.call(db, ...args);
53+
// @ts-ignore
54+
Object.setPrototypeOf(dbInstance[fn], Object.getPrototypeOf(f));
55+
// @ts-ignore
56+
Object.defineProperties(dbInstance[fn], Object.getOwnPropertyDescriptors(f));
5357
}
5458
};
5559

56-
exports.shutdown = async (hookName: string, context:any) => {
57-
if (exports.db != null) await exports.db.close();
58-
exports.db = null;
60+
export const shutdown = async (hookName: string, context:any) => {
61+
if (db != null) await db.close();
62+
db = null;
5963
logger.log('Database closed');
6064
};
65+
66+
let dbInstance = {} as {
67+
get: (key:string) => any;
68+
set: (key:string, value:any) => void;
69+
findKeys: (key:string) => string[];
70+
getSub: (key:string, subkey:string) => any;
71+
setSub: (key:string, subkey:string, value:any) => void;
72+
remove: (key:string) => void;
73+
init: () => Promise<void>;
74+
}
75+
76+
dbInstance.init = init
77+
78+
export default dbInstance

src/node/db/GroupManager.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
const CustomError = require('../utils/customError');
2323
const randomString = require('../../static/js/pad_utils').randomString;
24-
const db = require('./DB');
24+
import db from './DB';
2525
const padManager = require('./PadManager');
2626
const sessionManager = require('./SessionManager');
2727

@@ -69,6 +69,7 @@ exports.deleteGroup = async (groupID: string): Promise<void> => {
6969
// UeberDB's setSub() method atomically reads the record, updates the appropriate property, and
7070
// writes the result. Setting a property to `undefined` deletes that property (JSON.stringify()
7171
// ignores such properties).
72+
// @ts-ignore
7273
db.setSub('groups', [groupID], undefined),
7374
...Object.keys(group.mappings || {}).map(async (m) => await db.remove(`mapper2group:${m}`)),
7475
]);
@@ -99,6 +100,7 @@ exports.createGroup = async () => {
99100
// Add the group to the `groups` record after the group's individual record is created so that
100101
// the state is consistent. Note: UeberDB's setSub() method atomically reads the record, updates
101102
// the appropriate property, and writes the result.
103+
// @ts-ignore
102104
await db.setSub('groups', [groupID], 1);
103105
return {groupID};
104106
};
@@ -121,6 +123,7 @@ exports.createGroupIfNotExistsFor = async (groupMapper: string|object) => {
121123
// deleted. Although the core Etherpad API does not support multiple mappings for the same
122124
// group, the database record does support multiple mappings in case a plugin decides to extend
123125
// the core Etherpad functionality. (It's also easy to implement it this way.)
126+
// @ts-ignore
124127
db.setSub(`group:${result.groupID}`, ['mappings', groupMapper], 1),
125128
]);
126129
return result;
@@ -157,6 +160,7 @@ exports.createGroupPad = async (groupID: string, padName: string, text: string,
157160
await padManager.getPad(padID, text, authorId);
158161

159162
// create an entry in the group for this pad
163+
// @ts-ignore
160164
await db.setSub(`group:${groupID}`, ['pads', padID], 1);
161165

162166
return {padID};
@@ -176,6 +180,7 @@ exports.listPads = async (groupID: string): Promise<{ padIDs: string[]; }> => {
176180
}
177181

178182
// group exists, let's get the pads
183+
// @ts-ignore
179184
const result = await db.getSub(`group:${groupID}`, ['pads']);
180185
const padIDs = Object.keys(result);
181186

src/node/db/Pad.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ChatMessage = require('../../static/js/ChatMessage');
1313
const AttributePool = require('../../static/js/AttributePool');
1414
const Stream = require('../utils/Stream');
1515
const assert = require('assert').strict;
16-
const db = require('./DB');
16+
import db from './DB';
1717
import settings from '../utils/Settings';
1818
const authorManager = require('./AuthorManager');
1919
const padManager = require('./PadManager');
@@ -55,6 +55,7 @@ class Pad {
5555
* own database table, or to validate imported pad data before it is written to the database.
5656
*/
5757
constructor(id:string, database = db) {
58+
// @ts-ignore
5859
this.db = database;
5960
this.atext = Changeset.makeAText('\n');
6061
this.pool = new AttributePool();
@@ -427,6 +428,7 @@ class Pad {
427428
yield* Stream.range(0, this.chatHead + 1).map((i) => copyRecord(`:chat:${i}`));
428429
// @ts-ignore
429430
yield this.copyAuthorInfoToDestinationPad(destinationID);
431+
// @ts-ignore
430432
if (destGroupID) yield db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
431433
}).call(this);
432434
for (const p of new Stream(promises).batch(100).buffer(99)) await p;
@@ -510,6 +512,7 @@ class Pad {
510512

511513
// Group pad? Add it to the group's list
512514
if (destGroupID) {
515+
// @ts-ignore
513516
await db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
514517
}
515518

src/node/db/PadManager.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {PadType} from "../types/PadType";
2424

2525
const CustomError = require('../utils/customError');
2626
const Pad = require('../db/Pad');
27-
const db = require('./DB');
27+
import db from './DB';
2828
import settings from '../utils/Settings';
2929

3030
/**
@@ -74,6 +74,7 @@ const padList = new class {
7474
async getPads() {
7575
if (!this._loaded) {
7676
this._loaded = (async () => {
77+
// @ts-ignore
7778
const dbData = await db.findKeys('pad:*', '*:*:*');
7879
if (dbData == null) return;
7980
for (const val of dbData) this.addPad(val.replace(/^pad:/, ''));

src/node/db/ReadOnlyManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121

2222

23-
const db = require('./DB');
24-
const randomString = require('../utils/randomstring');
23+
import db from './DB';
24+
import randomString from '../utils/randomstring';
2525

2626

2727
/**

src/node/db/SessionManager.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
const CustomError = require('../utils/customError');
2424
const promises = require('../utils/promises');
2525
const randomString = require('../utils/randomstring');
26-
const db = require('./DB');
26+
import db from './DB';
2727
const groupManager = require('./GroupManager');
2828
const authorManager = require('./AuthorManager');
2929

@@ -151,7 +151,9 @@ exports.createSession = async (groupID: string, authorID: string, validUntil: nu
151151
await Promise.all([
152152
// UeberDB's setSub() method atomically reads the record, updates the appropriate (sub)object
153153
// property, and writes the result.
154+
// @ts-ignore
154155
db.setSub(`group2sessions:${groupID}`, ['sessionIDs', sessionID], 1),
156+
// @ts-ignore
155157
db.setSub(`author2sessions:${authorID}`, ['sessionIDs', sessionID], 1),
156158
]);
157159

@@ -196,7 +198,9 @@ exports.deleteSession = async (sessionID:string) => {
196198
// UeberDB's setSub() method atomically reads the record, updates the appropriate (sub)object
197199
// property, and writes the result. Setting a property to `undefined` deletes that property
198200
// (JSON.stringify() ignores such properties).
201+
// @ts-ignore
199202
db.setSub(`group2sessions:${groupID}`, ['sessionIDs', sessionID], undefined),
203+
// @ts-ignore
200204
db.setSub(`author2sessions:${authorID}`, ['sessionIDs', sessionID], undefined),
201205
]);
202206

src/node/db/SessionStore.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const DB = require('./DB');
3+
import DB from './DB';
44
const Store = require('@etherpad/express-session').Store;
55
const log4js = require('log4js');
66
const util = require('util');
@@ -19,7 +19,7 @@ class SessionStore extends Store {
1919
* Etherpad is restarted. Use `null` to prevent `touch()` from ever updating the record.
2020
* Ignored if the cookie does not expire.
2121
*/
22-
constructor(refresh = null) {
22+
constructor(refresh:number|null = null) {
2323
super();
2424
this._refresh = refresh;
2525
// Maps session ID to an object with the following properties:
@@ -111,4 +111,4 @@ for (const m of ['get', 'set', 'destroy', 'touch']) {
111111
SessionStore.prototype[m] = util.callbackify(SessionStore.prototype[`_${m}`]);
112112
}
113113

114-
module.exports = SessionStore;
114+
export default SessionStore

src/node/handler/PadMessageHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import log4js from 'log4js';
3737
const messageLogger = log4js.getLogger('message');
3838
const accessLogger = log4js.getLogger('access');
3939
const hooks = require('../../static/js/pluginfw/hooks.js');
40-
const stats = require('../stats')
40+
import stats from '../stats';
4141
const assert = require('assert').strict;
4242
import {RateLimiterMemory} from 'rate-limiter-flexible';
4343
import {ChangesetRequest, PadUserInfo, SocketClientRequest} from "../types/SocketClientRequest";

src/node/handler/SocketIORouter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import {MapArrayType} from "../types/MapType";
2424
import {SocketModule} from "../types/SocketModule";
25-
const log4js = require('log4js');
25+
import log4js from 'log4js';
2626
import settings from '../utils/Settings';
27-
const stats = require('../../node/stats')
27+
import stats from '../../node/stats';
2828

2929
const logger = log4js.getLogger('socket.io');
3030

src/node/hooks/express.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import expressSession from '@etherpad/express-session';
1313
import fs from 'fs';
1414
const hooks = require('../../static/js/pluginfw/hooks');
1515
import log4js from 'log4js';
16-
const SessionStore = require('../db/SessionStore');
16+
import SessionStore from '../db/SessionStore';
1717
import settings from '../utils/Settings';
18-
const stats = require('../stats')
18+
import stats from '../stats';
1919
import util from 'util';
2020
const webaccess = require('./express/webaccess');
2121

src/node/hooks/express/errorhandling.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import {ArgsExpressType} from "../../types/ArgsExpressType";
44
import {ErrorCaused} from "../../types/ErrorCaused";
55

6-
const stats = require('../../stats')
6+
import stats from '../../stats';
77

88
exports.expressCreateServer = (hook_name:string, args: ArgsExpressType, cb:Function) => {
99
exports.app = args.app;

src/node/security/SecretRotator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {LegacyParams} from "../types/LegacyParams";
55

66
const {Buffer} = require('buffer');
77
const crypto = require('./crypto');
8-
const db = require('../db/DB');
8+
import db from '../db/DB';
99
const log4js = require('log4js');
1010

1111
class Kdf {
@@ -173,6 +173,7 @@ export class SecretRotator {
173173
// TODO: This is racy. If two instances start up at the same time and there are no existing
174174
// matching publications, each will generate and publish their own paramters. In practice this
175175
// is unlikely to happen, and if it does it can be fixed by restarting both Etherpad instances.
176+
// @ts-ignore
176177
const dbKeys:string[] = await db.findKeys(`${this._dbPrefix}:*`, null) || [];
177178
let currentParams:any = null;
178179
let currentId = null;

src/node/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ NodeVersion.enforceMinNodeVersion(pkg.engines.node.replace(">=", ""));
6969
NodeVersion.checkDeprecationStatus(pkg.engines.node.replace(">=", ""), '2.1.0');
7070

7171
const UpdateCheck = require('./utils/UpdateCheck');
72-
const db = require('./db/DB');
72+
import db from './db/DB';
7373
const express = require('./hooks/express');
7474
const hooks = require('../static/js/pluginfw/hooks');
7575
const pluginDefs = require('../static/js/pluginfw/plugin_defs');
7676
const plugins = require('../static/js/pluginfw/plugins');
7777
const {Gate} = require('./utils/promises');
78-
const stats = require('./stats')
78+
import stats from './stats';
7979

8080
const logger = log4js.getLogger('server');
8181

src/node/stats.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
22

3-
const measured = require('measured-core');
3+
// @ts-ignore
4+
import measured from 'measured-core';
5+
6+
const coll = measured.createCollection()
47

5-
module.exports = measured.createCollection();
8+
export default coll;
69

710
// @ts-ignore
8-
module.exports.shutdown = async (hookName, context) => {
9-
module.exports.end();
10-
};
11+
export const shutdown = async (hookName, context) => {
12+
coll.end();
13+
};

src/node/utils/ImportEtherpad.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const AttributePool = require('../../static/js/AttributePool');
2222
const {Pad} = require('../db/Pad');
2323
const Stream = require('./Stream');
2424
const authorManager = require('../db/AuthorManager');
25-
const db = require('../db/DB');
25+
import db from '../db/DB';
2626
const hooks = require('../../static/js/pluginfw/hooks');
2727
import log4js from 'log4js';
2828
const supportedElems = require('../../static/js/contentcollector').supportedElems;

0 commit comments

Comments
 (0)