Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated settings to es6 #6592

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
50 changes: 34 additions & 16 deletions src/node/db/DB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,57 @@
*/

import {Database} from 'ueberdb2';
const settings = require('../utils/Settings');
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
7 changes: 5 additions & 2 deletions src/node/db/Pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ 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');
const settings = require('../utils/Settings');
import db from './DB';
import settings from '../utils/Settings';
const authorManager = require('./AuthorManager');
const padManager = require('./PadManager');
const padMessageHandler = require('../handler/PadMessageHandler');
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
5 changes: 3 additions & 2 deletions src/node/db/PadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {PadType} from "../types/PadType";

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

/**
* A cache of all loaded Pads.
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
2 changes: 1 addition & 1 deletion src/node/db/SecurityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const hooks = require('../../static/js/pluginfw/hooks');
const padManager = require('./PadManager');
const readOnlyManager = require('./ReadOnlyManager');
const sessionManager = require('./SessionManager');
const settings = require('../utils/Settings');
import settings from '../utils/Settings';
const webaccess = require('../hooks/express/webaccess');
const log4js = require('log4js');
const authLogger = log4js.getLogger('auth');
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
2 changes: 1 addition & 1 deletion src/node/eejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const fs = require('fs');
const hooks = require('../../static/js/pluginfw/hooks');
const path = require('path');
const resolve = require('resolve');
const settings = require('../utils/Settings');
import settings from '../utils/Settings';
import {pluginInstallPath} from '../../static/js/pluginfw/installer'

const templateCache = new Map();
Expand Down
8 changes: 4 additions & 4 deletions src/node/handler/APIKeyHandler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const absolutePaths = require('../utils/AbsolutePaths');
import fs from 'fs';
import log4js from 'log4js';
const randomString = require('../utils/randomstring');
const argv = require('../utils/Cli').argv;
const settings = require('../utils/Settings');
import randomString from '../utils/randomstring';
import {argvP} from '../utils/Cli'
import settings from '../utils/Settings';

const apiHandlerLogger = log4js.getLogger('APIHandler');

// ensure we have an apikey
export let apikey:string|null = null;
const apikeyFilename = absolutePaths.makeAbsolute(argv.apikey || './APIKEY.txt');
const apikeyFilename = absolutePaths.makeAbsolute(argvP.apikey || './APIKEY.txt');


if(settings.authenticationMethod === 'apikey') {
Expand Down
2 changes: 1 addition & 1 deletion src/node/handler/ImportHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const padManager = require('../db/PadManager');
const padMessageHandler = require('./PadMessageHandler');
import {promises as fs} from 'fs';
import path from 'path';
const settings = require('../utils/Settings');
import settings from '../utils/Settings';
const {Formidable} = require('formidable');
import os from 'os';
const importHtml = require('../utils/ImportHtml');
Expand Down
6 changes: 3 additions & 3 deletions src/node/handler/PadMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ const AttributeManager = require('../../static/js/AttributeManager');
const authorManager = require('../db/AuthorManager');
import padutils from '../../static/js/pad_utils';
const readOnlyManager = require('../db/ReadOnlyManager');
const settings = require('../utils/Settings');
import settings from '../utils/Settings';
const securityManager = require('../db/SecurityManager');
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
6 changes: 3 additions & 3 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');
const settings = require('../utils/Settings');
const stats = require('../../node/stats')
import log4js from 'log4js';
import settings from '../utils/Settings';
import stats from '../../node/stats';

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

Expand Down
Loading
Loading