From 17fe32ec0c1078983cb4a35686ea1d5be3b9a8e8 Mon Sep 17 00:00:00 2001 From: Ray Bellis Date: Mon, 21 Jan 2019 16:28:05 +0000 Subject: [PATCH] start using "thenify" to support callback and promises PadManager.sanitizePadId() can't use thenify: single arg callback --- src/node/db/API.js | 121 +++++++++++++++++---------------- src/node/db/AuthorManager.js | 41 +++++------ src/node/db/DB.js | 21 +----- src/node/db/GroupManager.js | 29 ++++---- src/node/db/Pad.js | 49 ++++++------- src/node/db/PadManager.js | 38 +++++++---- src/node/db/ReadOnlyManager.js | 13 ++-- src/node/db/SessionManager.js | 25 +++---- src/package.json | 1 + 9 files changed, 170 insertions(+), 168 deletions(-) diff --git a/src/node/db/API.js b/src/node/db/API.js index b6acefcd4..df2e25f10 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -33,6 +33,7 @@ var exportTxt = require("../utils/ExportTxt"); var importHtml = require("../utils/ImportHtml"); var cleanText = require("./Pad").cleanText; var PadDiff = require("../utils/padDiff"); +const thenify = require("thenify").withCallback; /**********************/ /**GROUP FUNCTIONS*****/ @@ -103,14 +104,14 @@ Example returns: } */ -exports.getAttributePool = function(padID, callback) +exports.getAttributePool = thenify(function(padID, callback) { getPadSafe(padID, true, function(err, pad) { if (ERR(err, callback)) return; callback(null, {pool: pad.pool}); }); -} +}); /** getRevisionChangeset (padID, [rev]) @@ -125,7 +126,7 @@ Example returns: } */ -exports.getRevisionChangeset = function(padID, rev, callback) +exports.getRevisionChangeset = thenify(function(padID, rev, callback) { // check if rev is a number if (rev !== undefined && typeof rev !== "number") { @@ -179,7 +180,7 @@ exports.getRevisionChangeset = function(padID, rev, callback) callback(null, changeset); }) }); -} +}); /** getText(padID, [rev]) returns the text of a pad @@ -189,7 +190,7 @@ Example returns: {code: 0, message:"ok", data: {text:"Welcome Text"}} {code: 1, message:"padID does not exist", data: null} */ -exports.getText = function(padID, rev, callback) +exports.getText = thenify(function(padID, rev, callback) { // check if rev is a number if (rev !== undefined && typeof rev !== "number") { @@ -242,7 +243,7 @@ exports.getText = function(padID, rev, callback) var padText = exportTxt.getTXTFromAtext(pad, pad.atext); callback(null, {"text": padText}); }); -} +}); /** setText(padID, text) sets the text of a pad @@ -253,7 +254,7 @@ Example returns: {code: 1, message:"padID does not exist", data: null} {code: 1, message:"text too long", data: null} */ -exports.setText = function(padID, text, callback) +exports.setText = thenify(function(padID, text, callback) { // text is required if (typeof text !== "string") { @@ -271,7 +272,7 @@ exports.setText = function(padID, text, callback) // update the clients on the pad padMessageHandler.updatePadClients(pad, callback); }); -} +}); /** appendText(padID, text) appends text to a pad @@ -282,7 +283,7 @@ Example returns: {code: 1, message:"padID does not exist", data: null} {code: 1, message:"text too long", data: null} */ -exports.appendText = function(padID, text, callback) +exports.appendText = thenify(function(padID, text, callback) { // text is required if (typeof text !== "string") { @@ -299,7 +300,7 @@ exports.appendText = function(padID, text, callback) // update the clients on the pad padMessageHandler.updatePadClients(pad, callback); }); -}; +}); /** getHTML(padID, [rev]) returns the html of a pad @@ -309,7 +310,7 @@ Example returns: {code: 0, message:"ok", data: {text:"Welcome Text"}} {code: 1, message:"padID does not exist", data: null} */ -exports.getHTML = function(padID, rev, callback) +exports.getHTML = thenify(function(padID, rev, callback) { if (rev !== undefined && typeof rev !== "number") { if (isNaN(parseInt(rev))) { @@ -362,7 +363,7 @@ exports.getHTML = function(padID, rev, callback) callback(null, data); }); }); -} +}); /** setHTML(padID, html) sets the text of a pad based on HTML @@ -372,7 +373,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.setHTML = function(padID, html, callback) +exports.setHTML = thenify(function(padID, html, callback) { // html is required if (typeof html !== "string") { @@ -395,7 +396,7 @@ exports.setHTML = function(padID, html, callback) padMessageHandler.updatePadClients(pad, callback); }); }); -} +}); /******************/ /**CHAT FUNCTIONS */ @@ -413,7 +414,7 @@ Example returns: {code: 1, message:"padID does not exist", data: null} */ -exports.getChatHistory = function(padID, start, end, callback) +exports.getChatHistory = thenify(function(padID, start, end, callback) { if (start && end) { if (start < 0) { @@ -458,7 +459,7 @@ exports.getChatHistory = function(padID, start, end, callback) callback(null, {messages: msgs}); }); }); -} +}); /** appendChatMessage(padID, text, authorID, time), creates a chat message for the pad id, time is a timestamp @@ -468,7 +469,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.appendChatMessage = function(padID, text, authorID, time, callback) +exports.appendChatMessage = thenify(function(padID, text, authorID, time, callback) { // text is required if (typeof text !== "string") { @@ -486,7 +487,7 @@ exports.appendChatMessage = function(padID, text, authorID, time, callback) padMessageHandler.sendChatMessageToPadClients(time, authorID, text, padID); callback(); -} +}); /*****************/ /**PAD FUNCTIONS */ @@ -500,7 +501,7 @@ Example returns: {code: 0, message:"ok", data: {revisions: 56}} {code: 1, message:"padID does not exist", data: null} */ -exports.getRevisionsCount = function(padID, callback) +exports.getRevisionsCount = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -508,7 +509,7 @@ exports.getRevisionsCount = function(padID, callback) callback(null, {revisions: pad.getHeadRevisionNumber()}); }); -} +}); /** getSavedRevisionsCount(padID) returns the number of saved revisions of this pad @@ -518,7 +519,7 @@ Example returns: {code: 0, message:"ok", data: {savedRevisions: 42}} {code: 1, message:"padID does not exist", data: null} */ -exports.getSavedRevisionsCount = function(padID, callback) +exports.getSavedRevisionsCount = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -526,7 +527,7 @@ exports.getSavedRevisionsCount = function(padID, callback) callback(null, {savedRevisions: pad.getSavedRevisionsNumber()}); }); -} +}); /** listSavedRevisions(padID) returns the list of saved revisions of this pad @@ -536,7 +537,7 @@ Example returns: {code: 0, message:"ok", data: {savedRevisions: [2, 42, 1337]}} {code: 1, message:"padID does not exist", data: null} */ -exports.listSavedRevisions = function(padID, callback) +exports.listSavedRevisions = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -544,7 +545,7 @@ exports.listSavedRevisions = function(padID, callback) callback(null, {savedRevisions: pad.getSavedRevisionsList()}); }); -} +}); /** saveRevision(padID) returns the list of saved revisions of this pad @@ -554,7 +555,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.saveRevision = function(padID, rev, callback) +exports.saveRevision = thenify(function(padID, rev, callback) { // check if rev is a number if (rev !== undefined && typeof rev !== "number") { @@ -601,7 +602,7 @@ exports.saveRevision = function(padID, rev, callback) callback(); }); }); -} +}); /** getLastEdited(padID) returns the timestamp of the last revision of the pad @@ -611,7 +612,7 @@ Example returns: {code: 0, message:"ok", data: {lastEdited: 1340815946602}} {code: 1, message:"padID does not exist", data: null} */ -exports.getLastEdited = function(padID, callback) +exports.getLastEdited = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -622,7 +623,7 @@ exports.getLastEdited = function(padID, callback) callback(null, {lastEdited: value}); }); }); -} +}); /** createPad(padName [, text]) creates a new pad in this group @@ -632,7 +633,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"pad does already exist", data: null} */ -exports.createPad = function(padID, text, callback) +exports.createPad = thenify(function(padID, text, callback) { if (padID) { // ensure there is no $ in the padID @@ -653,7 +654,7 @@ exports.createPad = function(padID, text, callback) if (ERR(err, callback)) return; callback(); }); -} +}); /** deletePad(padID) deletes a pad @@ -663,14 +664,14 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.deletePad = function(padID, callback) +exports.deletePad = thenify(function(padID, callback) { getPadSafe(padID, true, function(err, pad) { if (ERR(err, callback)) return; pad.remove(callback); }); -} +}); /** restoreRevision(padID, [rev]) Restores revision from past as new changeset @@ -679,7 +680,7 @@ exports.deletePad = function(padID, callback) {code:0, message:"ok", data:null} {code: 1, message:"padID does not exist", data: null} */ -exports.restoreRevision = function(padID, rev, callback) +exports.restoreRevision = thenify(function(padID, rev, callback) { // check if rev is a number if (rev !== undefined && typeof rev !== "number") { @@ -762,7 +763,7 @@ exports.restoreRevision = function(padID, rev, callback) }); }); -}; +}); /** copyPad(sourceID, destinationID[, force=false]) copies a pad. If force is true, @@ -773,14 +774,14 @@ Example returns: {code: 0, message:"ok", data: {padID: destinationID}} {code: 1, message:"padID does not exist", data: null} */ -exports.copyPad = function(sourceID, destinationID, force, callback) +exports.copyPad = thenify(function(sourceID, destinationID, force, callback) { getPadSafe(sourceID, true, function(err, pad) { if (ERR(err, callback)) return; pad.copy(destinationID, force, callback); }); -} +}); /** movePad(sourceID, destinationID[, force=false]) moves a pad. If force is true, @@ -791,7 +792,7 @@ Example returns: {code: 0, message:"ok", data: {padID: destinationID}} {code: 1, message:"padID does not exist", data: null} */ -exports.movePad = function(sourceID, destinationID, force, callback) +exports.movePad = thenify(function(sourceID, destinationID, force, callback) { getPadSafe(sourceID, true, function(err, pad) { if (ERR(err, callback)) return; @@ -801,7 +802,7 @@ exports.movePad = function(sourceID, destinationID, force, callback) pad.remove(callback); }); }); -} +}); /** getReadOnlyLink(padID) returns the read only link of a pad @@ -810,7 +811,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.getReadOnlyID = function(padID, callback) +exports.getReadOnlyID = thenify(function(padID, callback) { // we don't need the pad object, but this function does all the security stuff for us getPadSafe(padID, true, function(err) { @@ -822,7 +823,7 @@ exports.getReadOnlyID = function(padID, callback) callback(null, {readOnlyID: readOnlyId}); }); }); -} +}); /** getPadID(roID) returns the padID of a pad based on the readonlyID(roID) @@ -832,7 +833,7 @@ Example returns: {code: 0, message:"ok", data: {padID: padID}} {code: 1, message:"padID does not exist", data: null} */ -exports.getPadID = function(roID, callback) +exports.getPadID = thenify(function(roID, callback) { // get the PadId readOnlyManager.getPadId(roID, function(err, retrievedPadID) { @@ -845,7 +846,7 @@ exports.getPadID = function(roID, callback) callback(null, {padID: retrievedPadID}); }); -} +}); /** setPublicStatus(padID, publicStatus) sets a boolean for the public status of a pad @@ -855,7 +856,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.setPublicStatus = function(padID, publicStatus, callback) +exports.setPublicStatus = thenify(function(padID, publicStatus, callback) { // ensure this is a group pad if (padID && padID.indexOf("$") === -1) { @@ -876,7 +877,7 @@ exports.setPublicStatus = function(padID, publicStatus, callback) callback(); }); -} +}); /** getPublicStatus(padID) return true of false @@ -886,7 +887,7 @@ Example returns: {code: 0, message:"ok", data: {publicStatus: true}} {code: 1, message:"padID does not exist", data: null} */ -exports.getPublicStatus = function(padID, callback) +exports.getPublicStatus = thenify(function(padID, callback) { // ensure this is a group pad if (padID && padID.indexOf("$") == -1) { @@ -900,7 +901,7 @@ exports.getPublicStatus = function(padID, callback) callback(null, {publicStatus: pad.getPublicStatus()}); }); -} +}); /** setPassword(padID, password) returns ok or a error message @@ -910,7 +911,7 @@ Example returns: {code: 0, message:"ok", data: null} {code: 1, message:"padID does not exist", data: null} */ -exports.setPassword = function(padID, password, callback) +exports.setPassword = thenify(function(padID, password, callback) { // ensure this is a group pad if (padID && padID.indexOf("$") == -1) { @@ -927,7 +928,7 @@ exports.setPassword = function(padID, password, callback) callback(); }); -} +}); /** isPasswordProtected(padID) returns true or false @@ -937,7 +938,7 @@ Example returns: {code: 0, message:"ok", data: {passwordProtection: true}} {code: 1, message:"padID does not exist", data: null} */ -exports.isPasswordProtected = function(padID, callback) +exports.isPasswordProtected = thenify(function(padID, callback) { // ensure this is a group pad if (padID && padID.indexOf("$") == -1) { @@ -951,7 +952,7 @@ exports.isPasswordProtected = function(padID, callback) callback(null, {isPasswordProtected: pad.isPasswordProtected()}); }); -} +}); /** listAuthorsOfPad(padID) returns an array of authors who contributed to this pad @@ -961,7 +962,7 @@ Example returns: {code: 0, message:"ok", data: {authorIDs : ["a.s8oes9dhwrvt0zif", "a.akf8finncvomlqva"]} {code: 1, message:"padID does not exist", data: null} */ -exports.listAuthorsOfPad = function(padID, callback) +exports.listAuthorsOfPad = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -969,7 +970,7 @@ exports.listAuthorsOfPad = function(padID, callback) callback(null, {authorIDs: pad.getAllAuthors()}); }); -} +}); /** sendClientsMessage(padID, msg) sends a message to all clients connected to the @@ -994,7 +995,7 @@ Example returns: {code: 1, message:"padID does not exist"} */ -exports.sendClientsMessage = function(padID, msg, callback) { +exports.sendClientsMessage = thenify(function(padID, msg, callback) { getPadSafe(padID, true, function(err, pad) { if (ERR(err, callback)) { return; @@ -1002,7 +1003,7 @@ exports.sendClientsMessage = function(padID, msg, callback) { padMessageHandler.handleCustomMessage(padID, msg, callback); } ); -} +}); /** checkToken() returns ok when the current api token is valid @@ -1012,10 +1013,10 @@ Example returns: {"code":0,"message":"ok","data":null} {"code":4,"message":"no or wrong API Key","data":null} */ -exports.checkToken = function(callback) +exports.checkToken = thenify(function(callback) { callback(); -} +}); /** getChatHead(padID) returns the chatHead (last number of the last chat-message) of the pad @@ -1025,7 +1026,7 @@ Example returns: {code: 0, message:"ok", data: {chatHead: 42}} {code: 1, message:"padID does not exist", data: null} */ -exports.getChatHead = function(padID, callback) +exports.getChatHead = thenify(function(padID, callback) { // get the pad getPadSafe(padID, true, function(err, pad) { @@ -1033,7 +1034,7 @@ exports.getChatHead = function(padID, callback) callback(null, {chatHead: pad.chatHead}); }); -} +}); /** createDiffHTML(padID, startRev, endRev) returns an object of diffs from 2 points in a pad @@ -1043,7 +1044,7 @@ Example returns: {"code":0,"message":"ok","data":{"html":"Welcome to Etherpad!

This pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!

Get involved with Etherpad at http://etherpad.org
aw

","authors":["a.HKIv23mEbachFYfH",""]}} {"code":4,"message":"no or wrong API Key","data":null} */ -exports.createDiffHTML = function(padID, startRev, endRev, callback) { +exports.createDiffHTML = thenify(function(padID, startRev, endRev, callback) { // check if startRev is a number if (startRev !== undefined && typeof startRev !== "number") { // try to parse the number @@ -1104,7 +1105,7 @@ exports.createDiffHTML = function(padID, startRev, endRev, callback) { callback(err, {html: html, authors: authors}) }); }); -} +}); /******************************/ /** INTERNAL HELPER FUNCTIONS */ diff --git a/src/node/db/AuthorManager.js b/src/node/db/AuthorManager.js index d04c4ca32..0db187706 100644 --- a/src/node/db/AuthorManager.js +++ b/src/node/db/AuthorManager.js @@ -22,6 +22,7 @@ var ERR = require("async-stacktrace"); var db = require("./DB").db; var customError = require("../utils/customError"); var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString; +const thenify = require("thenify").withCallback; exports.getColorPalette = function() { return [ @@ -39,7 +40,7 @@ exports.getColorPalette = function() { /** * Checks if the author exists */ -exports.doesAuthorExists = function(authorID, callback) +exports.doesAuthorExists = thenify(function(authorID, callback) { // check if the database entry of this author exists db.get("globalAuthor:" + authorID, function(err, author) { @@ -47,14 +48,14 @@ exports.doesAuthorExists = function(authorID, callback) callback(null, author !== null); }); -} +}); /** * Returns the AuthorID for a token. * @param {String} token The token * @param {Function} callback callback (err, author) */ -exports.getAuthor4Token = function(token, callback) +exports.getAuthor4Token = thenify(function(token, callback) { mapAuthorWithDBKey("token2author", token, function(err, author) { if (ERR(err, callback)) return; @@ -62,7 +63,7 @@ exports.getAuthor4Token = function(token, callback) // return only the sub value authorID callback(null, author ? author.authorID : author); }); -} +}); /** * Returns the AuthorID for a mapper. @@ -70,7 +71,7 @@ exports.getAuthor4Token = function(token, callback) * @param {String} name The name of the author (optional) * @param {Function} callback callback (err, author) */ -exports.createAuthorIfNotExistsFor = function(authorMapper, name, callback) +exports.createAuthorIfNotExistsFor = thenify(function(authorMapper, name, callback) { mapAuthorWithDBKey("mapper2author", authorMapper, function(err, author) { if (ERR(err, callback)) return; @@ -83,7 +84,7 @@ exports.createAuthorIfNotExistsFor = function(authorMapper, name, callback) // return the authorID callback(null, author); }); -} +}); /** * Returns the AuthorID for a mapper. We can map using a mapperkey, @@ -126,7 +127,7 @@ function mapAuthorWithDBKey (mapperkey, mapper, callback) * Internal function that creates the database entry for an author * @param {String} name The name of the author */ -exports.createAuthor = function(name, callback) +exports.createAuthor = thenify(function(name, callback) { // create the new author name var author = "a." + randomString(16); @@ -142,27 +143,27 @@ exports.createAuthor = function(name, callback) db.set("globalAuthor:" + author, authorObj); callback(null, {authorID: author}); -} +}); /** * Returns the Author Obj of the author * @param {String} author The id of the author * @param {Function} callback callback(err, authorObj) */ -exports.getAuthor = function(author, callback) +exports.getAuthor = thenify(function(author, callback) { db.get("globalAuthor:" + author, callback); -} +}); /** * Returns the color Id of the author * @param {String} author The id of the author * @param {Function} callback callback(err, colorId) */ -exports.getAuthorColorId = function(author, callback) +exports.getAuthorColorId = thenify(function(author, callback) { db.getSub("globalAuthor:" + author, ["colorId"], callback); -} +}); /** * Sets the color Id of the author @@ -170,20 +171,20 @@ exports.getAuthorColorId = function(author, callback) * @param {String} colorId The color id of the author * @param {Function} callback (optional) */ -exports.setAuthorColorId = function(author, colorId, callback) +exports.setAuthorColorId = thenify(function(author, colorId, callback) { db.setSub("globalAuthor:" + author, ["colorId"], colorId, callback); -} +}); /** * Returns the name of the author * @param {String} author The id of the author * @param {Function} callback callback(err, name) */ -exports.getAuthorName = function(author, callback) +exports.getAuthorName = thenify(function(author, callback) { db.getSub("globalAuthor:" + author, ["name"], callback); -} +}); /** * Sets the name of the author @@ -191,17 +192,17 @@ exports.getAuthorName = function(author, callback) * @param {String} name The name of the author * @param {Function} callback (optional) */ -exports.setAuthorName = function(author, name, callback) +exports.setAuthorName = thenify(function(author, name, callback) { db.setSub("globalAuthor:" + author, ["name"], name, callback); -} +}); /** * Returns an array of all pads this author contributed to * @param {String} author The id of the author * @param {Function} callback (optional) */ -exports.listPadsOfAuthor = function(authorID, callback) +exports.listPadsOfAuthor = thenify(function(authorID, callback) { /* There are two other places where this array is manipulated: * (1) When the author is added to a pad, the author object is also updated @@ -230,7 +231,7 @@ exports.listPadsOfAuthor = function(authorID, callback) callback(null, {padIDs: pads}); }); -} +}); /** * Adds a new pad to the list of contributions diff --git a/src/node/db/DB.js b/src/node/db/DB.js index fa94259b5..e6ddd5fb3 100644 --- a/src/node/db/DB.js +++ b/src/node/db/DB.js @@ -22,6 +22,7 @@ var ueberDB = require("ueberdb2"); var settings = require("../utils/Settings"); var log4js = require('log4js'); +const thenify = require("thenify").withCallback; // set database settings var db = new ueberDB.database(settings.dbType, settings.dbSettings, null, log4js.getLogger("ueberDB")); @@ -35,7 +36,7 @@ exports.db = null; * Initalizes the database with the settings provided by the settings module * @param {Function} callback */ -function init(callback) { +exports.init = thenify(function (callback) { // initalize the database async db.init(function(err) { if (err) { @@ -49,20 +50,4 @@ function init(callback) { callback(null); } }); -} - -/** - * Initalizes the database with the settings provided by the settings module - * If the callback is not supplied a Promise is returned instead. - * @param {Function} callback - */ -exports.init = function(callback) -{ - if (callback === undefined) { - return new Promise(resolve => init(resolve)); - } else if (typeof callback === "function") { - init(callback); - } else { - throw new TypeError("DB.init callback parameter"); - } -} +}); diff --git a/src/node/db/GroupManager.js b/src/node/db/GroupManager.js index 88aa8aa04..d72efdff1 100644 --- a/src/node/db/GroupManager.js +++ b/src/node/db/GroupManager.js @@ -25,8 +25,9 @@ var db = require("./DB").db; var async = require("async"); var padManager = require("./PadManager"); var sessionManager = require("./SessionManager"); +const thenify = require("thenify").withCallback; -exports.listAllGroups = function(callback) { +exports.listAllGroups = thenify(function(callback) { db.get("groups", function (err, groups) { if (ERR(err, callback)) return; @@ -43,9 +44,9 @@ exports.listAllGroups = function(callback) { } callback(null, {groupIDs: groupIDs}); }); -} +}); -exports.deleteGroup = function(groupID, callback) +exports.deleteGroup = thenify(function(groupID, callback) { var group; @@ -158,18 +159,18 @@ exports.deleteGroup = function(groupID, callback) if (ERR(err, callback)) return; callback(); }); -} +}); -exports.doesGroupExist = function(groupID, callback) +exports.doesGroupExist = thenify(function(groupID, callback) { // try to get the group entry db.get("group:" + groupID, function (err, group) { if (ERR(err, callback)) return; callback(null, group != null); }); -} +}); -exports.createGroup = function(callback) +exports.createGroup = thenify(function(callback) { // search for non existing groupID var groupID = "g." + randomString(16); @@ -195,9 +196,9 @@ exports.createGroup = function(callback) callback(null, {groupID: groupID}); }); }); -} +}); -exports.createGroupIfNotExistsFor = function(groupMapper, callback) +exports.createGroupIfNotExistsFor = thenify(function(groupMapper, callback) { // ensure mapper is optional if (typeof groupMapper !== "string") { @@ -237,9 +238,9 @@ exports.createGroupIfNotExistsFor = function(groupMapper, callback) // there is no group for this mapper, let's create a group createGroupForMapper(callback) }); -} +}); -exports.createGroupPad = function(groupID, padName, text, callback) +exports.createGroupPad = thenify(function(groupID, padName, text, callback) { // create the padID var padID = groupID + "$" + padName; @@ -297,9 +298,9 @@ exports.createGroupPad = function(groupID, padName, text, callback) callback(null, {padID: padID}); }); -} +}); -exports.listPads = function(groupID, callback) +exports.listPads = thenify(function(groupID, callback) { exports.doesGroupExist(groupID, function(err, exists) { if (ERR(err, callback)) return; @@ -321,4 +322,4 @@ exports.listPads = function(groupID, callback) callback(null, {padIDs: pads}); }); }); -} +}); diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index c5a75a89f..34d000631 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -18,6 +18,7 @@ var readOnlyManager = require("./ReadOnlyManager"); var crypto = require("crypto"); var randomString = require("../utils/randomstring"); var hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks'); +const thenify = require("thenify").withCallback; // serialization/deserialization attributes var attributeBlackList = ["id"]; @@ -131,22 +132,22 @@ Pad.prototype.saveToDatabase = function saveToDatabase() { } // get time of last edit (changeset application) -Pad.prototype.getLastEdit = function getLastEdit(callback) { +Pad.prototype.getLastEdit = thenify(function getLastEdit(callback) { var revNum = this.getHeadRevisionNumber(); db.getSub("pad:" + this.id + ":revs:" + revNum, ["meta", "timestamp"], callback); -} +}); -Pad.prototype.getRevisionChangeset = function getRevisionChangeset(revNum, callback) { +Pad.prototype.getRevisionChangeset = thenify(function getRevisionChangeset(revNum, callback) { db.getSub("pad:" + this.id + ":revs:" + revNum, ["changeset"], callback); -}; +}); -Pad.prototype.getRevisionAuthor = function getRevisionAuthor(revNum, callback) { +Pad.prototype.getRevisionAuthor = thenify(function getRevisionAuthor(revNum, callback) { db.getSub("pad:" + this.id + ":revs:" + revNum, ["meta", "author"], callback); -}; +}); -Pad.prototype.getRevisionDate = function getRevisionDate(revNum, callback) { +Pad.prototype.getRevisionDate = thenify(function getRevisionDate(revNum, callback) { db.getSub("pad:" + this.id + ":revs:" + revNum, ["meta", "timestamp"], callback); -}; +}); Pad.prototype.getAllAuthors = function getAllAuthors() { var authors = []; @@ -160,7 +161,7 @@ Pad.prototype.getAllAuthors = function getAllAuthors() { return authors; }; -Pad.prototype.getInternalRevisionAText = function getInternalRevisionAText(targetRev, callback) { +Pad.prototype.getInternalRevisionAText = thenify(function getInternalRevisionAText(targetRev, callback) { var _this = this; var keyRev = this.getKeyRevisionNumber(targetRev); @@ -228,13 +229,13 @@ Pad.prototype.getInternalRevisionAText = function getInternalRevisionAText(targe if (ERR(err, callback)) return; callback(null, atext); }); -}; +}); -Pad.prototype.getRevision = function getRevisionChangeset(revNum, callback) { +Pad.prototype.getRevision = thenify(function getRevisionChangeset(revNum, callback) { db.get("pad:" + this.id + ":revs:" + revNum, callback); -}; +}); -Pad.prototype.getAllAuthorColors = function getAllAuthorColors(callback) { +Pad.prototype.getAllAuthorColors = thenify(function getAllAuthorColors(callback) { var authors = this.getAllAuthors(); var returnTable = {}; var colorPalette = authorManager.getColorPalette(); @@ -254,7 +255,7 @@ Pad.prototype.getAllAuthorColors = function getAllAuthorColors(callback) { function(err) { callback(err, returnTable); }); -}; +}); Pad.prototype.getValidRevisionRange = function getValidRevisionRange(startRev, endRev) { startRev = parseInt(startRev, 10); @@ -325,7 +326,7 @@ Pad.prototype.appendChatMessage = function appendChatMessage(text, userId, time) this.saveToDatabase(); }; -Pad.prototype.getChatMessage = function getChatMessage(entryNum, callback) { +Pad.prototype.getChatMessage = thenify(function getChatMessage(entryNum, callback) { var _this = this; var entry; @@ -359,9 +360,9 @@ Pad.prototype.getChatMessage = function getChatMessage(entryNum, callback) { if (ERR(err, callback)) return; callback(null, entry); }); -}; +}); -Pad.prototype.getChatMessages = function getChatMessages(start, end, callback) { +Pad.prototype.getChatMessages = thenify(function getChatMessages(start, end, callback) { // collect the numbers of chat entries and in which order we need them var neededEntries = []; var order = 0; @@ -398,9 +399,9 @@ Pad.prototype.getChatMessages = function getChatMessages(start, end, callback) { callback(null, cleanedEntries); }); -}; +}); -Pad.prototype.init = function init(text, callback) { +Pad.prototype.init = thenify(function init(text, callback) { var _this = this; // replace text with default text if text isn't set @@ -432,9 +433,9 @@ Pad.prototype.init = function init(text, callback) { hooks.callAll("padLoad", { 'pad': _this }); callback(null); }); -}; +}); -Pad.prototype.copy = function copy(destinationID, force, callback) { +Pad.prototype.copy = thenify(function copy(destinationID, force, callback) { var sourceID = this.id; var _this = this; var destGroupID; @@ -581,9 +582,9 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { if (ERR(err, callback)) return; callback(null, { padID: destinationID }); }); -}; +}); -Pad.prototype.remove = function remove(callback) { +Pad.prototype.remove = thenify(function remove(callback) { var padID = this.id; var _this = this; @@ -676,7 +677,7 @@ Pad.prototype.remove = function remove(callback) { if (ERR(err, callback)) return; callback(); }); -}; +}); // set in db Pad.prototype.setPublicStatus = function setPublicStatus(publicStatus) { diff --git a/src/node/db/PadManager.js b/src/node/db/PadManager.js index b198de81c..469a7f496 100644 --- a/src/node/db/PadManager.js +++ b/src/node/db/PadManager.js @@ -22,6 +22,7 @@ var ERR = require("async-stacktrace"); var customError = require("../utils/customError"); var Pad = require("../db/Pad").Pad; var db = require("./DB").db; +const thenify = require("thenify").withCallback; /** * A cache of all loaded Pads. @@ -53,7 +54,7 @@ var padList = { list: [], sorted : false, initiated: false, - init: function(cb) { + init: thenify(function(cb) { db.findKeys("pad:*", "*:*:*", function(err, dbData) { if (ERR(err, cb)) return; @@ -68,18 +69,18 @@ var padList = { }); return this; - }, - load: function(cb) { + }), + load: thenify(function(cb) { if (this.initiated) { cb && cb(); } else { this.init(cb); } - }, + }), /** * Returns all pads in alphabetical order as array. */ - getPads: function(cb) { + getPads: thenify(function(cb) { this.load(function() { if (!padList.sorted) { padList.list = padList.list.sort(); @@ -88,7 +89,7 @@ var padList = { cb && cb(padList.list); }) - }, + }), addPad: function(name) { if (!this.initiated) return; @@ -125,7 +126,7 @@ var padIdTransforms = [ * @param id A String with the id of the pad * @param {Function} callback */ -exports.getPad = function(id, text, callback) +exports.getPad = thenify(function(id, text, callback) { // check if this is a valid padId if (!exports.isValidPadId(id)) { @@ -177,17 +178,17 @@ exports.getPad = function(id, text, callback) padList.addPad(id); callback(null, pad); }); -} +}); -exports.listAllPads = function(cb) +exports.listAllPads = thenify(function(cb) { padList.getPads(function(list) { cb && cb(null, {padIDs: list}); }); -} +}); // checks if a pad exists -exports.doesPadExists = function(padId, callback) +exports.doesPadExists = thenify(function(padId, callback) { db.get("pad:" + padId, function(err, value) { if (ERR(err, callback)) return; @@ -198,10 +199,10 @@ exports.doesPadExists = function(padId, callback) callback(null, false); } }); -} +}); // returns a sanitized padId, respecting legacy pad id formats -exports.sanitizePadId = function(padId, callback) { +function sanitizePadId(padId, callback) { var transform_index = arguments[2] || 0; // we're out of possible transformations, so just return it @@ -228,10 +229,19 @@ exports.sanitizePadId = function(padId, callback) { } // check the next transform - exports.sanitizePadId(transformedPadId, callback, transform_index); + 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) { return /^(g.[a-zA-Z0-9]{16}\$)?[^$]{1,50}$/.test(padId); diff --git a/src/node/db/ReadOnlyManager.js b/src/node/db/ReadOnlyManager.js index ac7bee045..14c6f75c7 100644 --- a/src/node/db/ReadOnlyManager.js +++ b/src/node/db/ReadOnlyManager.js @@ -23,12 +23,13 @@ var ERR = require("async-stacktrace"); var db = require("./DB").db; var async = require("async"); var randomString = require("../utils/randomstring"); +const thenify = require("thenify").withCallback; /** * returns a read only id for a pad * @param {String} padId the id of the pad */ -exports.getReadOnlyId = function (padId, callback) +exports.getReadOnlyId = thenify(function (padId, callback) { var readOnlyId; @@ -59,22 +60,22 @@ exports.getReadOnlyId = function (padId, callback) // return the results callback(null, readOnlyId); }) -} +}); /** * returns the padId for a read only id * @param {String} readOnlyId read only id */ -exports.getPadId = function(readOnlyId, callback) +exports.getPadId = thenify(function(readOnlyId, callback) { db.get("readonly2pad:" + readOnlyId, callback); -} +}); /** * returns the padId and readonlyPadId in an object for any id * @param {String} padIdOrReadonlyPadId read only id or real pad id */ -exports.getIds = function(id, callback) { +exports.getIds = thenify(function(id, callback) { if (id.indexOf("r.") == 0) { exports.getPadId(id, function (err, value) { if (ERR(err, callback)) return; @@ -94,4 +95,4 @@ exports.getIds = function(id, callback) { }); }); } -} +}); diff --git a/src/node/db/SessionManager.js b/src/node/db/SessionManager.js index 28ad4e036..f1f759da6 100644 --- a/src/node/db/SessionManager.js +++ b/src/node/db/SessionManager.js @@ -25,8 +25,9 @@ var db = require("./DB").db; var async = require("async"); var groupManager = require("./GroupManager"); var authorManager = require("./AuthorManager"); +const thenify = require("thenify").withCallback; -exports.doesSessionExist = function(sessionID, callback) +exports.doesSessionExist = thenify(function(sessionID, callback) { //check if the database entry of this session exists db.get("session:" + sessionID, function (err, session) @@ -34,12 +35,12 @@ exports.doesSessionExist = function(sessionID, callback) if(ERR(err, callback)) return; callback(null, session != null); }); -} +}); /** * Creates a new session between an author and a group */ -exports.createSession = function(groupID, authorID, validUntil, callback) +exports.createSession = thenify(function(groupID, authorID, validUntil, callback) { var sessionID; @@ -172,9 +173,9 @@ exports.createSession = function(groupID, authorID, validUntil, callback) // return error and sessionID callback(null, {sessionID: sessionID}); }) -} +}); -exports.getSessionInfo = function(sessionID, callback) +exports.getSessionInfo = thenify(function(sessionID, callback) { // check if the database entry of this session exists db.get("session:" + sessionID, function (err, session) @@ -189,12 +190,12 @@ exports.getSessionInfo = function(sessionID, callback) callback(null, session); } }); -} +}); /** * Deletes a session */ -exports.deleteSession = function(sessionID, callback) +exports.deleteSession = thenify(function(sessionID, callback) { var authorID, groupID; var group2sessions, author2sessions; @@ -267,9 +268,9 @@ exports.deleteSession = function(sessionID, callback) if(ERR(err, callback)) return; callback(); }) -} +}); -exports.listSessionsOfGroup = function(groupID, callback) +exports.listSessionsOfGroup = thenify(function(groupID, callback) { groupManager.doesGroupExist(groupID, function(err, exists) { @@ -283,9 +284,9 @@ exports.listSessionsOfGroup = function(groupID, callback) listSessionsWithDBKey("group2sessions:" + groupID, callback); } }); -} +}); -exports.listSessionsOfAuthor = function(authorID, callback) +exports.listSessionsOfAuthor = thenify(function(authorID, callback) { authorManager.doesAuthorExists(authorID, function(err, exists) { @@ -299,7 +300,7 @@ exports.listSessionsOfAuthor = function(authorID, callback) listSessionsWithDBKey("author2sessions:" + authorID, callback); } }); -} +}); // this function is basically the code listSessionsOfAuthor and listSessionsOfGroup has in common function listSessionsWithDBKey (dbkey, callback) diff --git a/src/package.json b/src/package.json index e3cb756a6..fa63b0710 100644 --- a/src/package.json +++ b/src/package.json @@ -57,6 +57,7 @@ "slide": "1.1.6", "socket.io": "2.1.1", "swagger-node-express": "2.1.3", + "thenify": "^3.3.0", "tinycon": "0.0.1", "ueberdb2": "0.4.0", "uglify-js": "2.6.2",