Skip to content

Commit

Permalink
Converted more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Aug 18, 2024
1 parent 154e673 commit fa5e235
Show file tree
Hide file tree
Showing 26 changed files with 172 additions and 78 deletions.
11 changes: 8 additions & 3 deletions src/node/db/AuthorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* limitations under the License.
*/

const db = require('./DB');
import db from './DB';
const CustomError = require('../utils/customError');
const hooks = require('../../static/js/pluginfw/hooks');
import padutils, {randomString} from "../../static/js/pad_utils";
Expand Down Expand Up @@ -131,6 +131,7 @@ const mapAuthorWithDBKey = async (mapperkey: string, mapper:string) => {

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

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

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

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

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

/**
* Returns an array of all pads this author contributed to
Expand Down
48 changes: 33 additions & 15 deletions src/node/db/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,55 @@
import {Database} from 'ueberdb2';
import settings from '../utils/Settings';
import log4js from 'log4js';
const stats = require('../stats')
import stats from '../stats';

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

/**
* The UeberDB Object that provides the database functions
*/
exports.db = null;
export let db:Database|null = null;

/**
* Initializes the database with the settings provided by the settings module
*/
exports.init = async () => {
exports.db = new Database(settings.dbType, settings.dbSettings, null, logger);
await exports.db.init();
if (exports.db.metrics != null) {
for (const [metric, value] of Object.entries(exports.db.metrics)) {
export const init = async () => {
db = new Database(settings.dbType, settings.dbSettings, null, logger);
await db.init();
if (db.metrics != null) {
for (const [metric, value] of Object.entries(db.metrics)) {
if (typeof value !== 'number') continue;
stats.gauge(`ueberdb_${metric}`, () => exports.db.metrics[metric]);
stats.gauge(`ueberdb_${metric}`, () => db!.metrics[metric]);
}
}
for (const fn of ['get', 'set', 'findKeys', 'getSub', 'setSub', 'remove']) {
const f = exports.db[fn];
exports[fn] = async (...args:string[]) => await f.call(exports.db, ...args);
Object.setPrototypeOf(exports[fn], Object.getPrototypeOf(f));
Object.defineProperties(exports[fn], Object.getOwnPropertyDescriptors(f));
// @ts-ignore
const f = db[fn];
// @ts-ignore
dbInstance[fn] = async (...args:string[]) => await f.call(db, ...args);
// @ts-ignore
Object.setPrototypeOf(dbInstance[fn], Object.getPrototypeOf(f));
// @ts-ignore
Object.defineProperties(dbInstance[fn], Object.getOwnPropertyDescriptors(f));
}
};

exports.shutdown = async (hookName: string, context:any) => {
if (exports.db != null) await exports.db.close();
exports.db = null;
export const shutdown = async (hookName: string, context:any) => {
if (db != null) await db.close();
db = null;
logger.log('Database closed');
};

let dbInstance = {} as {
get: (key:string) => any;
set: (key:string, value:any) => void;
findKeys: (key:string) => string[];
getSub: (key:string, subkey:string) => any;
setSub: (key:string, subkey:string, value:any) => void;
remove: (key:string) => void;
init: () => Promise<void>;
}

dbInstance.init = init

export default dbInstance
7 changes: 6 additions & 1 deletion src/node/db/GroupManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

const CustomError = require('../utils/customError');
import {randomString} from "../../static/js/pad_utils";
const db = require('./DB');
import db from './DB';
const padManager = require('./PadManager');
const sessionManager = require('./SessionManager');

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

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

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

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

Expand Down
5 changes: 4 additions & 1 deletion src/node/db/Pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ChatMessage from '../../static/js/ChatMessage';
import AttributePool from '../../static/js/AttributePool';
const Stream = require('../utils/Stream');
const assert = require('assert').strict;
const db = require('./DB');
import db from './DB';
import settings from '../utils/Settings';
const authorManager = require('./AuthorManager');
const padManager = require('./PadManager');
Expand Down Expand Up @@ -56,6 +56,7 @@ class Pad {
* own database table, or to validate imported pad data before it is written to the database.
*/
constructor(id:string, database = db) {
// @ts-ignore
this.db = database;
this.atext = makeAText('\n');
this.pool = new AttributePool();
Expand Down Expand Up @@ -428,6 +429,7 @@ class Pad {
yield* Stream.range(0, this.chatHead + 1).map((i) => copyRecord(`:chat:${i}`));
// @ts-ignore
yield this.copyAuthorInfoToDestinationPad(destinationID);
// @ts-ignore
if (destGroupID) yield db.setSub(`group:${destGroupID}`, ['pads', destinationID], 1);
}).call(this);
for (const p of new Stream(promises).batch(100).buffer(99)) await p;
Expand Down Expand Up @@ -511,6 +513,7 @@ class Pad {

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

Expand Down
3 changes: 2 additions & 1 deletion src/node/db/PadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {PadType} from "../types/PadType";

const CustomError = require('../utils/customError');
const Pad = require('../db/Pad');
const db = require('./DB');
import db from './DB';
import settings from '../utils/Settings';

/**
Expand Down Expand Up @@ -74,6 +74,7 @@ const padList = new class {
async getPads() {
if (!this._loaded) {
this._loaded = (async () => {
// @ts-ignore
const dbData = await db.findKeys('pad:*', '*:*:*');
if (dbData == null) return;
for (const val of dbData) this.addPad(val.replace(/^pad:/, ''));
Expand Down
4 changes: 2 additions & 2 deletions src/node/db/ReadOnlyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/


const db = require('./DB');
const randomString = require('../utils/randomstring');
import db from './DB';
import randomString from '../utils/randomstring';


/**
Expand Down
6 changes: 5 additions & 1 deletion src/node/db/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
const CustomError = require('../utils/customError');
const promises = require('../utils/promises');
const randomString = require('../utils/randomstring');
const db = require('./DB');
import db from './DB';
const groupManager = require('./GroupManager');
const authorManager = require('./AuthorManager');

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/node/db/SessionStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

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

module.exports = SessionStore;
export default SessionStore
4 changes: 2 additions & 2 deletions src/node/handler/PadMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const plugins = require('../../static/js/pluginfw/plugin_defs');
import log4js from 'log4js';
const messageLogger = log4js.getLogger('message');
const accessLogger = log4js.getLogger('access');
const hooks = require('../../static/js/pluginfw/hooks');
const stats = require('../stats')
const hooks = require('../../static/js/pluginfw/hooks.js');
import stats from '../stats';
const assert = require('assert').strict;
import {RateLimiterMemory} from 'rate-limiter-flexible';
import {ChangesetRequest, PadUserInfo, SocketClientRequest} from "../types/SocketClientRequest";
Expand Down
4 changes: 2 additions & 2 deletions src/node/handler/SocketIORouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import {MapArrayType} from "../types/MapType";
import {SocketModule} from "../types/SocketModule";
const log4js = require('log4js');
import log4js from 'log4js';
import settings from '../utils/Settings';
const stats = require('../../node/stats')
import stats from '../../node/stats';

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

Expand Down
4 changes: 2 additions & 2 deletions src/node/hooks/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import expressSession from '@etherpad/express-session';
import fs from 'fs';
const hooks = require('../../static/js/pluginfw/hooks');
import log4js from 'log4js';
const SessionStore = require('../db/SessionStore');
import SessionStore from '../db/SessionStore';
import settings from '../utils/Settings';
const stats = require('../stats')
import stats from '../stats';
import util from 'util';
const webaccess = require('./express/webaccess');

Expand Down
2 changes: 1 addition & 1 deletion src/node/hooks/express/errorhandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import {ArgsExpressType} from "../../types/ArgsExpressType";
import {ErrorCaused} from "../../types/ErrorCaused";

const stats = require('../../stats')
import stats from '../../stats';

exports.expressCreateServer = (hook_name:string, args: ArgsExpressType, cb:Function) => {
exports.app = args.app;
Expand Down
3 changes: 2 additions & 1 deletion src/node/security/SecretRotator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {LegacyParams} from "../types/LegacyParams";

const {Buffer} = require('buffer');
const crypto = require('./crypto');
const db = require('../db/DB');
import db from '../db/DB';
const log4js = require('log4js');

class Kdf {
Expand Down Expand Up @@ -173,6 +173,7 @@ export class SecretRotator {
// TODO: This is racy. If two instances start up at the same time and there are no existing
// matching publications, each will generate and publish their own paramters. In practice this
// is unlikely to happen, and if it does it can be fixed by restarting both Etherpad instances.
// @ts-ignore
const dbKeys:string[] = await db.findKeys(`${this._dbPrefix}:*`, null) || [];
let currentParams:any = null;
let currentId = null;
Expand Down
4 changes: 2 additions & 2 deletions src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ NodeVersion.enforceMinNodeVersion(pkg.engines.node.replace(">=", ""));
NodeVersion.checkDeprecationStatus(pkg.engines.node.replace(">=", ""), '2.1.0');

const UpdateCheck = require('./utils/UpdateCheck');
const db = require('./db/DB');
import db from './db/DB';
const express = require('./hooks/express');
const hooks = require('../static/js/pluginfw/hooks');
const pluginDefs = require('../static/js/pluginfw/plugin_defs');
const plugins = require('../static/js/pluginfw/plugins');
const {Gate} = require('./utils/promises');
const stats = require('./stats')
import stats from './stats';

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

Expand Down
13 changes: 8 additions & 5 deletions src/node/stats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

const measured = require('measured-core');
// @ts-ignore
import measured from 'measured-core';

const coll = measured.createCollection()

module.exports = measured.createCollection();
export default coll;

// @ts-ignore
module.exports.shutdown = async (hookName, context) => {
module.exports.end();
};
export const shutdown = async (hookName, context) => {
coll.end();
};
2 changes: 1 addition & 1 deletion src/node/utils/ImportEtherpad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AttributePool from '../../static/js/AttributePool';
const {Pad} = require('../db/Pad');
const Stream = require('./Stream');
const authorManager = require('../db/AuthorManager');
const db = require('../db/DB');
import db from '../db/DB';
const hooks = require('../../static/js/pluginfw/hooks');
import log4js from 'log4js';
const supportedElems = require('../../static/js/contentcollector').supportedElems;
Expand Down
Loading

0 comments on commit fa5e235

Please sign in to comment.