Added backend in typescript. (#6185)

This commit is contained in:
SamTV12345 2024-02-23 19:48:55 +01:00 committed by GitHub
parent c2b9df3b24
commit 295a2a758b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 211 additions and 118 deletions

View file

@ -1,5 +1,7 @@
'use strict';
import {APool} from "../types/PadType";
/**
* 2014 John McLear (Etherpad Foundation / McLear Ltd)
*
@ -22,17 +24,17 @@ const Stream = require('./Stream');
const authorManager = require('../db/AuthorManager');
const db = require('../db/DB');
const hooks = require('../../static/js/pluginfw/hooks');
const log4js = require('log4js');
import log4js from 'log4js';
const supportedElems = require('../../static/js/contentcollector').supportedElems;
const ueberdb = require('ueberdb2');
import ueberdb from 'ueberdb2';
const logger = log4js.getLogger('ImportEtherpad');
exports.setPadRaw = async (padId, r, authorId = '') => {
exports.setPadRaw = async (padId: string, r: string, authorId = '') => {
const records = JSON.parse(r);
// get supported block Elements from plugins, we will use this later.
hooks.callAll('ccRegisterBlockElements').forEach((element) => {
hooks.callAll('ccRegisterBlockElements').forEach((element:any) => {
supportedElems.add(element);
});
@ -43,8 +45,8 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
'pad',
];
let originalPadId = null;
const checkOriginalPadId = (padId) => {
let originalPadId:string|null = null;
const checkOriginalPadId = (padId: string) => {
if (originalPadId == null) originalPadId = padId;
if (originalPadId !== padId) throw new Error('unexpected pad ID in record');
};
@ -57,7 +59,10 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
const padDb = new ueberdb.Database('memory', {data});
await padDb.init();
try {
const processRecord = async (key, value) => {
const processRecord = async (key:string, value: null|{
padIDs: string|Record<string, unknown>,
pool: APool
}) => {
if (!value) return;
const keyParts = key.split(':');
const [prefix, id] = keyParts;
@ -79,7 +84,7 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
if (prefix === 'pad' && keyParts.length === 2) {
const pool = new AttributePool().fromJsonable(value.pool);
const unsupportedElements = new Set();
pool.eachAttrib((k, v) => {
pool.eachAttrib((k: string, v:any) => {
if (!supportedElems.has(k)) unsupportedElements.add(k);
});
if (unsupportedElements.size) {
@ -94,8 +99,10 @@ exports.setPadRaw = async (padId, r, authorId = '') => {
`importEtherpad hook function processes it: ${key}`);
return;
}
// @ts-ignore
await padDb.set(key, value);
};
// @ts-ignore
const readOps = new Stream(Object.entries(records)).map(([k, v]) => processRecord(k, v));
for (const op of readOps.batch(100).buffer(99)) await op;

View file

@ -15,15 +15,16 @@
* limitations under the License.
*/
const log4js = require('log4js');
import log4js from 'log4js';
const Changeset = require('../../static/js/Changeset');
const contentcollector = require('../../static/js/contentcollector');
const jsdom = require('jsdom');
import jsdom from 'jsdom';
import {PadType} from "../types/PadType";
const apiLogger = log4js.getLogger('ImportHtml');
let processor;
let processor:any;
exports.setPadHTML = async (pad, html, authorId = '') => {
exports.setPadHTML = async (pad: PadType, html:string, authorId = '') => {
if (processor == null) {
const [{rehype}, {default: minifyWhitespace}] =
await Promise.all([import('rehype'), import('rehype-minify-whitespace')]);
@ -46,7 +47,7 @@ exports.setPadHTML = async (pad, html, authorId = '') => {
try {
// we use a try here because if the HTML is bad it will blow up
cc.collectContent(document.body);
} catch (err) {
} catch (err: any) {
apiLogger.warn(`Error processing HTML: ${err.stack || err}`);
throw err;
}