Converted imports to es

This commit is contained in:
SamTV12345 2024-03-12 18:52:44 +01:00
parent 078324c0d1
commit 4b8911dfee
33 changed files with 111 additions and 98 deletions

View file

@ -12,7 +12,7 @@ const Changeset = require('../../static/js/Changeset');
const ChatMessage = require('../../static/js/ChatMessage');
const AttributePool = require('../../static/js/AttributePool');
const Stream = require('../utils/Stream');
const assert = require('assert').strict;
import {strict as assert} from "assert";
const db = require('./DB');
const settings = require('../utils/Settings');
const authorManager = require('./AuthorManager');
@ -39,13 +39,13 @@ exports.cleanText = (txt:string): string => txt.replace(/\r\n/g, '\n')
class Pad {
private db: Database;
private atext: AText;
private readonly atext: AText;
private pool: APool;
private head: number;
private chatHead: number;
private publicStatus: boolean;
private id: string;
private savedRevisions: any[];
private readonly savedRevisions: any[];
/**
* @param id
* @param [database] - Database object to access this pad's records (and only this pad's records;

View file

@ -28,7 +28,7 @@ const readOnlyManager = require('./ReadOnlyManager');
const sessionManager = require('./SessionManager');
const settings = require('../utils/Settings');
const webaccess = require('../hooks/express/webaccess');
const log4js = require('log4js');
import log4js from 'log4js';
const authLogger = log4js.getLogger('auth');
const {padutils} = require('../../static/js/pad_utils');

View file

@ -2,8 +2,8 @@
const DB = require('./DB');
const Store = require('express-session').Store;
const log4js = require('log4js');
const util = require('util');
import log4js from 'log4js';
import util from 'util';
const logger = log4js.getLogger('SessionStore');

View file

@ -20,11 +20,11 @@
* require("./index").require("./path/to/template.ejs")
*/
const ejs = require('ejs');
const fs = require('fs');
import ejs from 'ejs';
import fs from 'fs';
const hooks = require('../../static/js/pluginfw/hooks.js');
const path = require('path');
const resolve = require('resolve');
import path from 'path';
import resolve from 'resolve';
const settings = require('../utils/Settings');
const templateCache = new Map();

View file

@ -38,7 +38,7 @@ const messageLogger = log4js.getLogger('message');
const accessLogger = log4js.getLogger('access');
const hooks = require('../../static/js/pluginfw/hooks.js');
const stats = require('../stats')
const assert = require('assert').strict;
import {strict as assert} from "assert";
import {RateLimiterMemory} from 'rate-limiter-flexible';
import {ChangesetRequest, PadUserInfo, SocketClientRequest} from "../types/SocketClientRequest";
import {APool, AText, PadAuthor, PadType} from "../types/PadType";

View file

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

View file

@ -119,11 +119,10 @@ exports.restartServer = async () => {
options.ca.push(fs.readFileSync(caFileName));
}
}
const https = require('https');
const https = await import('https')
exports.server = https.createServer(options, app);
} else {
const http = require('http');
const http = await import('http')
exports.server = http.createServer(app);
}

View file

@ -11,7 +11,7 @@ const settings = require('../../utils/Settings');
const installer = require('../../../static/js/pluginfw/installer');
const pluginDefs = require('../../../static/js/pluginfw/plugin_defs');
const plugins = require('../../../static/js/pluginfw/plugins');
const semver = require('semver');
import semver from 'semver';
exports.socketio = (hookName:string, args:ArgsExpressType, cb:Function) => {

View file

@ -5,7 +5,7 @@ import {PadQueryResult, PadSearchQuery} from "../../types/PadSearchQuery";
import {PadType} from "../../types/PadType";
const eejs = require('../../eejs');
const fsp = require('fs').promises;
import {promises as fsp} from 'fs'
const hooks = require('../../../static/js/pluginfw/hooks');
const plugins = require('../../../static/js/pluginfw/plugins');
const settings = require('../../utils/Settings');

View file

@ -1,10 +1,10 @@
'use strict';
const log4js = require('log4js');
import log4js from 'log4js';
const clientLogger = log4js.getLogger('client');
const {Formidable} = require('formidable');
import {Formidable} from 'formidable';
const apiHandler = require('../../handler/APIHandler');
const util = require('util');
import util from 'util';
exports.expressPreSession = async (hookName:string, {app}:any) => {
// The Etherpad client side sends information about how a disconnect happened
@ -14,17 +14,18 @@ exports.expressPreSession = async (hookName:string, {app}:any) => {
res.end('OK');
});
const parseJserrorForm = async (req:any) => {
const parseJserrorForm = async (req:any):Promise<string[]> => {
const form = new Formidable({
maxFileSize: 1, // Files are not expected. Not sure if 0 means unlimited, so 1 is used.
});
const [fields, files] = await form.parse(req);
return fields.errorInfo;
return fields.errorInfo!;
};
// The Etherpad client side sends information about client side javscript errors
app.post('/jserror', (req:any, res:any, next:Function) => {
(async () => {
// @ts-ignore
const data = JSON.parse(await parseJserrorForm(req));
clientLogger.warn(`${data.msg} --`, {
[util.inspect.custom]: (depth: number, options:any) => {

View file

@ -8,7 +8,7 @@ const exportHandler = require('../../handler/ExportHandler');
const importHandler = require('../../handler/ImportHandler');
const padManager = require('../../db/PadManager');
const readOnlyManager = require('../../db/ReadOnlyManager');
const rateLimit = require('express-rate-limit');
import rateLimit from 'express-rate-limit';
const securityManager = require('../../db/SecurityManager');
const webaccess = require('./webaccess');

View file

@ -19,14 +19,14 @@ import {ErrorCaused} from "../../types/ErrorCaused";
*/
const OpenAPIBackend = require('openapi-backend').default;
const IncomingForm = require('formidable').IncomingForm;
const cloneDeep = require('lodash.clonedeep');
const createHTTPError = require('http-errors');
import {IncomingForm} from 'formidable'
import cloneDeep from 'lodash.clonedeep';
import createHTTPError from 'http-errors';
const apiHandler = require('../../handler/APIHandler');
const settings = require('../../utils/Settings');
const log4js = require('log4js');
import log4js from 'log4js';
const logger = log4js.getLogger('API');
// https://github.com/OAI/OpenAPI-Specification/tree/master/schemas/v3.0
@ -401,6 +401,7 @@ for (const [resource, actions] of Object.entries(resources)) {
// add response objects
const responses:OpenAPISuccessResponse = {...defaultResponseRefs};
if (responseSchema) {
// @ts-ignore
responses[200] = cloneDeep(defaultResponses.Success);
responses[200].content!['application/json'].schema.properties.data = {
type: 'object',
@ -636,7 +637,7 @@ exports.expressPreSession = async (hookName:string, {app}:any) => {
// an unknown error happened
// log it and throw internal error
logger.error(errCaused.stack || errCaused.toString());
throw new createHTTPError.InternalError('internal error');
throw new createHTTPError.InternalServerError('internal error');
}
}
@ -657,7 +658,7 @@ exports.expressPreSession = async (hookName:string, {app}:any) => {
}
// start and bind to express
api.init();
await api.init();
app.use(apiRoot, async (req:any, res:any) => {
let response = null;
try {

View file

@ -5,7 +5,7 @@ import {ArgsExpressType} from "../../types/ArgsExpressType";
import events from 'events';
const express = require('../express');
import log4js from 'log4js';
const proxyaddr = require('proxy-addr');
import proxyaddr from 'proxy-addr';
const settings = require('../../utils/Settings');
import {Server, Socket} from 'socket.io'
const socketIORouter = require('../../handler/SocketIORouter');

View file

@ -1,16 +1,16 @@
'use strict';
const path = require('path');
import path from 'path';
const eejs = require('../../eejs');
const fs = require('fs');
const fsp = fs.promises;
import fs from 'fs';
import {promises as fsp} from 'fs';
const toolbar = require('../../utils/toolbar');
const hooks = require('../../../static/js/pluginfw/hooks');
const settings = require('../../utils/Settings');
const util = require('util');
import util from 'util';
const webaccess = require('./webaccess');
exports.expressPreSession = async (hookName:string, {app}:any) => {
exports.expressPreSession = async (hookName:string, {app}: { app: any }) => {
// This endpoint is intended to conform to:
// https://www.ietf.org/archive/id/draft-inadarei-api-health-check-06.html
app.get('/health', (req:any, res:any) => {

View file

@ -3,9 +3,9 @@
import {MapArrayType} from "../../types/MapType";
import {PartType} from "../../types/PartType";
const fs = require('fs').promises;
import {promises as fs} from 'fs'
const minify = require('../../utils/Minify');
const path = require('path');
import path from 'path';
const plugins = require('../../../static/js/pluginfw/plugin_defs');
const settings = require('../../utils/Settings');
const CachingMiddleware = require('../../utils/caching_middleware');

View file

@ -3,8 +3,8 @@
import {Dirent} from "node:fs";
import {PluginDef} from "../../types/PartType";
const path = require('path');
const fsp = require('fs').promises;
import path from 'path';
import {promises as fsp} from 'fs';
const plugins = require('../../../static/js/pluginfw/plugin_defs');
const sanitizePathname = require('../../utils/sanitizePathname');
const settings = require('../../utils/Settings');

View file

@ -4,11 +4,11 @@ import type {MapArrayType} from "../types/MapType";
import {I18nPluginDefs} from "../types/I18nPluginDefs";
const languages = require('languages4translatewiki');
const fs = require('fs');
const path = require('path');
const _ = require('underscore');
import fs from 'fs';
import path from 'path';
import _ from 'underscore';
const pluginDefs = require('../../static/js/pluginfw/plugin_defs.js');
const existsSync = require('../utils/path_exists');
import existsSync from '../utils/path_exists';
const settings = require('../utils/Settings');
// returns all existing messages merged together and grouped by langcode

View file

@ -3,14 +3,14 @@
import {DeriveModel} from "../types/DeriveModel";
import {LegacyParams} from "../types/LegacyParams";
const {Buffer} = require('buffer');
import {Buffer} from 'buffer';
const crypto = require('./crypto');
const db = require('../db/DB');
const log4js = require('log4js');
import log4js from 'log4js';
class Kdf {
async generateParams(): Promise<{ salt: string; digest: string; keyLen: number; secret: string }> { throw new Error('not implemented'); }
async derive(params: DeriveModel, info: any) { throw new Error('not implemented'); }
async derive(params: DeriveModel, info: any):Promise<string> { throw new Error('not implemented'); }
}
class LegacyStaticSecret extends Kdf {

View file

@ -1,7 +1,7 @@
'use strict';
const crypto = require('crypto');
const util = require('util');
import crypto from 'crypto';
import util from 'util';
/**

View file

@ -22,10 +22,10 @@
import {ChildProcess} from "node:child_process";
import {AsyncQueueTask} from "../types/AsyncQueueTask";
const spawn = require('child_process').spawn;
const async = require('async');
import {spawn} from 'child_process'
import async from 'async';
const settings = require('./Settings');
const os = require('os');
import os from 'os';
// on windows we have to spawn a process for each convertion,
// cause the plugin abicommand doesn't exist on this platform

View file

@ -18,9 +18,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const log4js = require('log4js');
const path = require('path');
const _ = require('underscore');
import log4js from 'log4js';
import path from 'path';
import _ from 'underscore';
const absPathLogger = log4js.getLogger('AbsolutePaths');

View file

@ -16,7 +16,8 @@
*/
const Stream = require('./Stream');
const assert = require('assert').strict;
import {strict as assert} from "assert";
const authorManager = require('../db/AuthorManager');
const hooks = require('../../static/js/pluginfw/hooks');
const padManager = require('../db/PadManager');

View file

@ -21,7 +21,7 @@ import {MapArrayType} from "../types/MapType";
const Changeset = require('../../static/js/Changeset');
const attributes = require('../../static/js/attributes');
const padManager = require('../db/PadManager');
const _ = require('underscore');
import _ from 'underscore';
const Security = require('../../static/js/security');
const hooks = require('../../static/js/pluginfw/hooks');
const eejs = require('../eejs');

View file

@ -17,11 +17,11 @@
* limitations under the License.
*/
const async = require('async');
const fs = require('fs').promises;
const log4js = require('log4js');
const os = require('os');
const path = require('path');
import async from 'async';
import {promises as fs} from 'fs';
import log4js from 'log4js';
import os from 'os';
import path from 'path';
const runCmd = require('./run_cmd');
const settings = require('./Settings');
@ -87,7 +87,6 @@ const queue = async.queue(doConvertTask, 1);
* @param {String} srcFile The path on disk to convert
* @param {String} destFile The path on disk where the converted file should be stored
* @param {String} type The type to convert into
* @param {Function} callback Standard callback function
*/
exports.convertFile = async (srcFile: string, destFile: string, type:string) => {
// Used for the moving of the file, not the conversion

View file

@ -19,7 +19,7 @@
* limitations under the License.
*/
const semver = require('semver');
import semver from 'semver';
/**
* Quits if Etherpad is not running on a given minimum Node version

View file

@ -28,17 +28,16 @@
*/
const absolutePaths = require('./AbsolutePaths');
const deepEqual = require('fast-deep-equal/es6');
const fs = require('fs');
const os = require('os');
const path = require('path');
import fs from 'fs';
import os from 'os';
import path from 'path';
const argv = require('./Cli').argv;
const jsonminify = require('jsonminify');
const log4js = require('log4js');
import jsonminify from 'jsonminify';
import log4js from 'log4js';
const randomString = require('./randomstring');
const suppressDisableMsg = ' -- To suppress these warning messages change ' +
'suppressErrorsInPadText to true in your settings.json\n';
const _ = require('underscore');
import _ from 'underscore';
const logger = log4js.getLogger('settings');

View file

@ -16,14 +16,13 @@
* limitations under the License.
*/
const Buffer = require('buffer').Buffer;
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
const zlib = require('zlib');
import {Buffer} from "buffer";
import fs, {promises as fsp} from 'fs';
import path from 'path';
import zlib from 'zlib';
const settings = require('./Settings');
const existsSync = require('./path_exists');
const util = require('util');
import existsSync from './path_exists';
import util from 'util';
/*
* The crypto module can be absent on reduced node installations.
@ -37,10 +36,11 @@ const util = require('util');
*/
const _crypto = require('crypto');
import _crypto from 'crypto';
import {createReadStream} from "node:fs";
let CACHE_DIR = path.join(settings.root, 'var/');
let CACHE_DIR:string|boolean|fs.Stats|undefined = path.join(settings.root, 'var/');
CACHE_DIR = existsSync(CACHE_DIR) ? CACHE_DIR : undefined;
type Headers = {
@ -141,7 +141,7 @@ module.exports = class CachingMiddleware {
res.writeHead(304, headers);
res.end();
} else if (req.method === 'GET') {
const readStream = fs.createReadStream(pathStr);
const readStream = createReadStream(pathStr);
res.writeHead(statusCode, headers);
readStream.pipe(res);
} else {
@ -188,6 +188,7 @@ module.exports = class CachingMiddleware {
await Promise.all([
fsp.writeFile(`${CACHE_DIR}minified_${cacheKey}`, buffer).catch(() => {}),
util.promisify(zlib.gzip)(buffer)
// @ts-ignore
.then((content: string) => fsp.writeFile(`${CACHE_DIR}minified_${cacheKey}.gz`, content))
.catch(() => {}),
]);

View file

@ -1,10 +1,10 @@
'use strict';
const fs = require('fs');
import fs from 'fs';
const check = (path:string) => {
const check = (path:string):false|fs.Stats => {
const existsSync = fs.statSync || fs.existsSync;
let result;
let result:false|fs.Stats;
try {
result = existsSync(path);
} catch (e) {
@ -13,4 +13,4 @@ const check = (path:string) => {
return result;
};
module.exports = check;
export default check;

View file

@ -3,7 +3,7 @@
* Generates a random String with the given length. Is needed to generate the
* Author, Group, readonly, session Ids
*/
const cryptoMod = require('crypto');
import cryptoMod from 'crypto';
const randomString = (len: number) => cryptoMod.randomBytes(len).toString('hex');

View file

@ -1,13 +1,13 @@
'use strict';
import {ErrorExtended, RunCMDOptions, RunCMDPromise} from "../types/RunCMDOptions";
import {ChildProcess} from "node:child_process";
import {ChildProcess, SpawnOptions} from "node:child_process";
import {PromiseWithStd} from "../types/PromiseWithStd";
import {Readable} from "node:stream";
const spawn = require('cross-spawn');
const log4js = require('log4js');
const path = require('path');
import spawn from 'cross-spawn';
import log4js from 'log4js';
import path from 'path';
const settings = require('./Settings');
const logger = log4js.getLogger('runCmd');
@ -32,6 +32,8 @@ const logLines = (readable: undefined | Readable | null, logLineFn: (arg0: (stri
});
};
/**
* Runs a command, logging its output to Etherpad's logs by default.
*
@ -74,7 +76,7 @@ const logLines = (readable: undefined | Readable | null, logLineFn: (arg0: (stri
* - `stderr`: Similar to `stdout` but for stderr.
* - `child`: The ChildProcess object.
*/
module.exports = exports = (args: string[], opts:RunCMDOptions = {}) => {
module.exports = exports = (args: string[], opts:SpawnOptions= {}) => {
logger.debug(`Executing command: ${args.join(' ')}`);
opts = {cwd: settings.root, ...opts};
@ -84,7 +86,8 @@ module.exports = exports = (args: string[], opts:RunCMDOptions = {}) => {
const stdio =
Array.isArray(opts.stdio) ? opts.stdio.slice() // Copy to avoid mutating the caller's array.
: typeof opts.stdio === 'function' ? [null, opts.stdio, opts.stdio]
: opts.stdio === 'string' ? [null, 'string', 'string']
// @ts-ignore
: opts.stdio === 'string' ? [null, 'string', 'string']
: Array(3).fill(opts.stdio);
const cmdLogger = log4js.getLogger(`runCmd|${args[0]}`);
if (stdio[1] == null) stdio[1] = (line: string) => cmdLogger.info(line);

View file

@ -1,6 +1,6 @@
'use strict';
const path = require('path');
import path from 'path';
// Normalizes p and ensures that it is a relative path that does not reach outside. See
// https://nvd.nist.gov/vuln/detail/CVE-2015-3297 for additional context.

View file

@ -30,6 +30,8 @@
}
],
"dependencies": {
"@manypkg/find-root": "^2.2.1",
"@types/proxy-addr": "^2.0.3",
"async": "^3.2.5",
"axios": "^1.6.7",
"clean-css": "^5.3.3",
@ -41,7 +43,6 @@
"express": "4.18.3",
"express-rate-limit": "^7.2.0",
"express-session": "npm:@etherpad/express-session@^1.18.2",
"fast-deep-equal": "^3.1.3",
"find-root": "1.1.0",
"formidable": "^3.5.1",
"http-errors": "^2.0.0",
@ -79,12 +80,21 @@
"etherpad-lite": "node/server.ts"
},
"devDependencies": {
"@playwright/test": "^1.42.1",
"@types/async": "^3.2.24",
"@types/cross-spawn": "^6.0.6",
"@types/ejs": "^3.1.5",
"@types/express": "^4.17.21",
"@types/formidable": "^3.4.5",
"@types/http-errors": "^2.0.4",
"@types/jsdom": "^21.1.6",
"@types/jsonminify": "^0.4.3",
"@types/lodash.clonedeep": "^4.5.9",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.26",
"@types/resolve": "^1.20.6",
"@types/semver": "^7.5.8",
"@types/set-cookie-parser": "^2.4.7",
"@types/sinon": "^17.0.3",
"@types/supertest": "^6.0.2",
"@types/underscore": "^1.11.15",
@ -95,7 +105,6 @@
"mocha-froth": "^0.2.10",
"nodeify": "^1.0.1",
"openapi-schema-validation": "^0.4.2",
"@playwright/test": "^1.42.1",
"set-cookie-parser": "^2.6.0",
"sinon": "^17.0.1",
"split-grid": "^1.0.11",

View file

@ -5,12 +5,12 @@ import {MapArrayType} from "../../node/types/MapType";
const AttributePool = require('../../static/js/AttributePool');
const apiHandler = require('../../node/handler/APIHandler');
const assert = require('assert').strict;
const io = require('socket.io-client');
const log4js = require('log4js');
import {io} from 'socket.io-client';
import log4js from 'log4js';
const {padutils} = require('../../static/js/pad_utils');
const process = require('process');
import process from 'process';
const server = require('../../node/server');
const setCookieParser = require('set-cookie-parser');
import setCookieParser from 'set-cookie-parser';
const settings = require('../../node/utils/Settings');
import supertest from 'supertest';
const webaccess = require('../../node/hooks/express/webaccess');