etherpad-lite/src/node/db/PadManager.ts

207 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-01-21 21:06:52 +00:00
'use strict';
2011-03-26 13:10:41 +00:00
/**
2011-05-30 15:53:11 +01:00
* The Pad Manager is a Factory for pad Objects
*/
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
2011-03-26 13:10:41 +00:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2024-02-22 11:36:43 +01:00
import {MapArrayType} from "../types/MapType";
2021-01-21 21:06:52 +00:00
const CustomError = require('../utils/customError');
const Pad = require('../db/Pad');
2020-11-23 13:24:19 -05:00
const db = require('./DB');
const settings = require('../utils/Settings');
2011-03-26 13:10:41 +00:00
/**
* A cache of all loaded Pads.
*
* Provides "get" and "set" functions,
* which should be used instead of indexing with brackets. These prepend a
* colon to the key, to avoid conflicting with built-in Object methods or with
* these functions themselves.
*
* If this is needed in other places, it would be wise to make this a prototype
* that's defined somewhere more sensible.
2011-03-26 13:10:41 +00:00
*/
2024-02-22 11:36:43 +01:00
const globalPads:MapArrayType<any> = {
get(name: string)
{
return this[`:${name}`];
},
set(name: string, value: any)
{
2020-11-23 13:24:19 -05:00
this[`:${name}`] = value;
},
2024-02-22 11:36:43 +01:00
remove(name: string) {
2020-11-23 13:24:19 -05:00
delete this[`:${name}`];
},
};
2011-03-26 13:10:41 +00:00
/**
* A cache of the list of all pads.
*
* Updated without db access as new pads are created/old ones removed.
*/
const padList = new class {
2024-02-22 11:36:43 +01:00
private _cachedList: string[] | null;
private _list: Set<string>;
private _loaded: Promise<void> | null;
constructor() {
this._cachedList = null;
this._list = new Set();
this._loaded = null;
}
/**
* Returns all pads in alphabetical order as array.
* @returns {Promise<string[]>} A promise that resolves to an array of pad IDs.
*/
2020-11-23 13:24:19 -05:00
async getPads() {
if (!this._loaded) {
this._loaded = (async () => {
const dbData = await db.findKeys('pad:*', '*:*:*');
if (dbData == null) return;
for (const val of dbData) this.addPad(val.replace(/^pad:/, ''));
})();
}
await this._loaded;
if (!this._cachedList) this._cachedList = [...this._list].sort();
return this._cachedList;
}
2024-02-22 11:36:43 +01:00
addPad(name: string) {
if (this._list.has(name)) return;
this._list.add(name);
this._cachedList = null;
}
2024-02-22 11:36:43 +01:00
removePad(name: string) {
if (!this._list.has(name)) return;
this._list.delete(name);
this._cachedList = null;
}
}();
// initialises the all-knowing data structure
2011-03-26 13:10:41 +00:00
/**
2011-05-30 15:53:11 +01:00
* Returns a Pad Object with the callback
2011-03-26 13:10:41 +00:00
* @param id A String with the id of the pad
* @param {string} [text] - Optional initial pad text if creating a new pad.
* @param {string} [authorId] - Optional author ID of the user that initiated the pad creation (if
* applicable).
2011-03-26 13:10:41 +00:00
*/
2024-02-22 11:36:43 +01:00
exports.getPad = async (id: string, text: string, authorId:string = '') => {
// check if this is a valid padId
if (!exports.isValidPadId(id)) {
2021-01-21 21:06:52 +00:00
throw new CustomError(`${id} is not a valid padId`, 'apierror');
2011-08-04 19:20:14 +01:00
}
// check if this is a valid text
if (text != null) {
// check if text is a string
2020-11-23 13:24:19 -05:00
if (typeof text !== 'string') {
2021-01-21 21:06:52 +00:00
throw new CustomError('text is not a string', 'apierror');
2011-08-08 17:35:40 +01:00
}
// check if text is less than 100k chars
if (text.length > 100000) {
2021-01-21 21:06:52 +00:00
throw new CustomError('text must be less than 100k chars', 'apierror');
2011-08-08 17:35:40 +01:00
}
}
let pad = globalPads.get(id);
// return pad if it's already loaded
if (pad != null) {
return pad;
}
// try to load pad
pad = new Pad.Pad(id);
2021-02-03 00:30:07 +01:00
// initialize the pad
await pad.init(text, authorId);
globalPads.set(id, pad);
padList.addPad(id);
return pad;
2020-11-23 13:24:19 -05:00
};
2021-01-21 21:06:52 +00:00
exports.listAllPads = async () => {
2020-11-23 13:24:19 -05:00
const padIDs = await padList.getPads();
2020-11-23 13:24:19 -05:00
return {padIDs};
};
// checks if a pad exists
2024-02-22 11:36:43 +01:00
exports.doesPadExist = async (padId: string) => {
2020-11-23 13:24:19 -05:00
const value = await db.get(`pad:${padId}`);
return (value != null && value.atext);
2020-11-23 13:24:19 -05:00
};
// alias for backwards compatibility
exports.doesPadExists = exports.doesPadExist;
/**
* An array of padId transformations. These represent changes in pad name policy over
* time, and allow us to "play back" these changes so legacy padIds can be found.
*/
const padIdTransforms = [
[/\s+/g, '_'],
2020-11-23 13:24:19 -05:00
[/:+/g, '_'],
];
// returns a sanitized padId, respecting legacy pad id formats
2024-02-22 11:36:43 +01:00
exports.sanitizePadId = async (padId: string) => {
for (let i = 0, n = padIdTransforms.length; i < n; ++i) {
2020-11-23 13:24:19 -05:00
const exists = await exports.doesPadExist(padId);
if (exists) {
return padId;
}
2020-11-23 13:24:19 -05:00
const [from, to] = padIdTransforms[i];
2024-02-22 11:36:43 +01:00
// @ts-ignore
padId = padId.replace(from, to);
}
if (settings.lowerCasePadIds) padId = padId.toLowerCase();
// we're out of possible transformations, so just return it
return padId;
2020-11-23 13:24:19 -05:00
};
2024-02-22 11:36:43 +01:00
exports.isValidPadId = (padId: string) => /^(g.[a-zA-Z0-9]{16}\$)?[^$]{1,50}$/.test(padId);
2013-01-08 20:19:10 +01:00
/**
* Removes the pad from database and unloads it.
*/
2024-02-22 11:36:43 +01:00
exports.removePad = async (padId: string) => {
2020-11-23 13:24:19 -05:00
const p = db.remove(`pad:${padId}`);
exports.unloadPad(padId);
padList.removePad(padId);
await p;
2020-11-23 13:24:19 -05:00
};
// removes a pad from the cache
2024-02-22 11:36:43 +01:00
exports.unloadPad = (padId: string) => {
globalPads.remove(padId);
2020-11-23 13:24:19 -05:00
};