mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
db/PadManager.js: convert sanitizePadId() to Promises
The function is now iterative rather than recursive.
This commit is contained in:
parent
bf9e3f92b5
commit
eedae98e2f
1 changed files with 22 additions and 45 deletions
|
@ -112,15 +112,6 @@ var padList = {
|
||||||
|
|
||||||
// initialises the all-knowing data structure
|
// initialises the all-knowing data structure
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
var padIdTransforms = [
|
|
||||||
[/\s+/g, '_'],
|
|
||||||
[/:+/g, '_']
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a Pad Object with the callback
|
* Returns a Pad Object with the callback
|
||||||
* @param id A String with the id of the pad
|
* @param id A String with the id of the pad
|
||||||
|
@ -204,45 +195,31 @@ exports.doesPadExist = thenify(function(padId, callback)
|
||||||
// alias for backwards compatibility
|
// alias for backwards compatibility
|
||||||
exports.doesPadExists = exports.doesPadExist;
|
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, '_'],
|
||||||
|
[/:+/g, '_']
|
||||||
|
];
|
||||||
|
|
||||||
// returns a sanitized padId, respecting legacy pad id formats
|
// returns a sanitized padId, respecting legacy pad id formats
|
||||||
function sanitizePadId(padId, callback) {
|
exports.sanitizePadId = async function sanitizePadId(padId) {
|
||||||
var transform_index = arguments[2] || 0;
|
for (let i = 0, n = padIdTransforms.length; i < n; ++i) {
|
||||||
|
let exists = await exports.doesPadExist(padId);
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
return padId;
|
||||||
|
}
|
||||||
|
|
||||||
|
let [from, to] = padIdTransforms[i];
|
||||||
|
|
||||||
|
padId = padId.replace(from, to);
|
||||||
|
}
|
||||||
|
|
||||||
// we're out of possible transformations, so just return it
|
// we're out of possible transformations, so just return it
|
||||||
if (transform_index >= padIdTransforms.length) {
|
return padId;
|
||||||
callback(padId);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if padId exists
|
|
||||||
exports.doesPadExists(padId, function(junk, exists) {
|
|
||||||
if (exists) {
|
|
||||||
callback(padId);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the next transformation *that's different*
|
|
||||||
var transformedPadId = padId;
|
|
||||||
|
|
||||||
while(transformedPadId == padId && transform_index < padIdTransforms.length) {
|
|
||||||
transformedPadId = padId.replace(padIdTransforms[transform_index][0], padIdTransforms[transform_index][1]);
|
|
||||||
transform_index += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// check the next transform
|
|
||||||
sanitizePadId(transformedPadId, callback, transform_index);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// sanitizePadId can't use thenify: single arg callback
|
|
||||||
exports.sanitizePadId = function(padId, callback) {
|
|
||||||
if (callback) {
|
|
||||||
return sanitizePadId(padId, callback);
|
|
||||||
} else {
|
|
||||||
return new Promise(resolve => sanitizePadId(padId, resolve));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.isValidPadId = function(padId)
|
exports.isValidPadId = function(padId)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue