Moved path_exists and promises to es6

This commit is contained in:
SamTV12345 2024-08-18 19:52:21 +02:00
parent 6ed711a4d8
commit 4ff00e278a
8 changed files with 59 additions and 31 deletions

View file

@ -25,7 +25,8 @@ const randomString = require('../utils/randomstring');
const hooks = require('../../static/js/pluginfw/hooks');
import pad_utils from "../../static/js/pad_utils";
import {SmartOpAssembler} from "../../static/js/SmartOpAssembler";
const promises = require('../utils/promises');
import {} from '../utils/promises';
import {timesLimit} from "async";
/**
* Copied from the Etherpad source code. It converts Windows line breaks to Unix
@ -586,12 +587,14 @@ class Pad {
p.push(db.remove(`pad2readonly:${padID}`));
// delete all chat messages
p.push(promises.timesLimit(this.chatHead + 1, 500, async (i: string) => {
// @ts-ignore
p.push(timesLimit(this.chatHead + 1, 500, async (i: string) => {
await this.db.remove(`pad:${this.id}:chat:${i}`, null);
}));
// delete all revisions
p.push(promises.timesLimit(this.head + 1, 500, async (i: string) => {
// @ts-ignore
p.push(timesLimit(this.head + 1, 500, async (i: string) => {
await this.db.remove(`pad:${this.id}:revs:${i}`, null);
}));

View file

@ -21,7 +21,7 @@
*/
const CustomError = require('../utils/customError');
const promises = require('../utils/promises');
import {firstSatisfies} from '../utils/promises';
const randomString = require('../utils/randomstring');
const db = require('./DB');
const groupManager = require('./GroupManager');
@ -79,7 +79,7 @@ exports.findAuthorID = async (groupID:string, sessionCookie: string) => {
groupID: string;
validUntil: number;
}|null) => (si != null && si.groupID === groupID && now < si.validUntil);
const sessionInfo = await promises.firstSatisfies(sessionInfoPromises, isMatch);
const sessionInfo = await firstSatisfies(sessionInfoPromises, isMatch) as any;
if (sessionInfo == null) return undefined;
return sessionInfo.authorID;
};

View file

@ -8,7 +8,7 @@ const fs = require('fs');
const path = require('path');
const _ = require('underscore');
const pluginDefs = require('../../static/js/pluginfw/plugin_defs');
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

@ -74,7 +74,7 @@ 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');
import {Gate} from './utils/promises';
const stats = require('./stats')
const logger = log4js.getLogger('server');
@ -100,7 +100,7 @@ const removeSignalListener = (signal: NodeJS.Signals, listener: NodeJS.SignalsLi
};
let startDoneGate: { resolve: () => void; }
let startDoneGate: Gate<unknown>
exports.start = async () => {
switch (state) {
case State.INITIAL:
@ -181,12 +181,14 @@ exports.start = async () => {
} catch (err) {
logger.error('Error occurred while starting Etherpad');
state = State.STATE_TRANSITION_FAILED;
// @ts-ignore
startDoneGate.resolve();
return await exports.exit(err);
}
logger.info('Etherpad is running');
state = State.RUNNING;
// @ts-ignore
startDoneGate.resolve();
// Return the HTTP server to make it easier to write tests.
@ -228,11 +230,13 @@ exports.stop = async () => {
} catch (err) {
logger.error('Error occurred while stopping Etherpad');
state = State.STATE_TRANSITION_FAILED;
// @ts-ignore
stopDoneGate.resolve();
return await exports.exit(err);
}
logger.info('Etherpad stopped');
state = State.STOPPED;
// @ts-ignore
stopDoneGate.resolve();
};

View file

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

View file

@ -7,7 +7,7 @@
// `predicate`. Resolves to `undefined` if none of the Promises satisfy `predicate`, or if
// `promises` is empty. If `predicate` is nullish, the truthiness of the resolved value is used as
// the predicate.
exports.firstSatisfies = <T>(promises: Promise<T>[], predicate: null|Function) => {
export const firstSatisfies = <T>(promises: Promise<T>[], predicate: null|Function) => {
if (predicate == null) {
predicate = (x: any) => x;
}
@ -44,7 +44,7 @@ exports.firstSatisfies = <T>(promises: Promise<T>[], predicate: null|Function) =
// `total` is greater than `concurrency`, then `concurrency` Promises will be created right away,
// and each remaining Promise will be created once one of the earlier Promises resolves.) This async
// function resolves once all `total` Promises have resolved.
exports.timesLimit = async (total: number, concurrency: number, promiseCreator: Function) => {
export const timesLimit = async (total: number, concurrency: number, promiseCreator: Function) => {
if (total > 0 && concurrency <= 0) throw new RangeError('concurrency must be positive');
let next = 0;
const addAnother = () => promiseCreator(next++).finally(() => {
@ -61,7 +61,7 @@ exports.timesLimit = async (total: number, concurrency: number, promiseCreator:
* An ordinary Promise except the `resolve` and `reject` executor functions are exposed as
* properties.
*/
class Gate<T> extends Promise<T> {
export class Gate<T> extends Promise<T> {
// Coax `.then()` into returning an ordinary Promise, not a Gate. See
// https://stackoverflow.com/a/65669070 for the rationale.
static get [Symbol.species]() { return Promise; }
@ -75,4 +75,3 @@ class Gate<T> extends Promise<T> {
Object.assign(this, props);
}
}
exports.Gate = Gate;