From be026aa308e366ed3e1e67d14e1de8f6ef3e0000 Mon Sep 17 00:00:00 2001 From: Arlen22 Date: Wed, 14 Jul 2021 12:16:57 -0400 Subject: [PATCH] Revert "Add server sent events (#5279)" (#5880) This reverts commit 17b4f53ba27fbf09b531f8fcb2e8dd2bfbf507c4 according to Github Desktop. git checkout that commit revert commit in GitHub Desktop git switch -c revert-sse uncommit in Github Desktop switch to master, bringing changes resolve deletions with command line --- core/language/en-GB/Help/listen.tid | 1 - core/modules/server/routes/get-status.js | 1 - core/modules/server/server-sent-events.js | 70 -------------- core/modules/syncer.js | 39 ++++---- .../WebServer Parameter_ sse-enabled.tid | 11 --- plugins/tiddlywiki/tiddlyweb/sse-client.js | 56 ----------- plugins/tiddlywiki/tiddlyweb/sse-server.js | 94 ------------------- .../tiddlywiki/tiddlyweb/tiddlywebadaptor.js | 4 +- 8 files changed, 17 insertions(+), 259 deletions(-) delete mode 100644 core/modules/server/server-sent-events.js delete mode 100644 editions/tw5.com/tiddlers/webserver/WebServer Parameter_ sse-enabled.tid delete mode 100644 plugins/tiddlywiki/tiddlyweb/sse-client.js delete mode 100644 plugins/tiddlywiki/tiddlyweb/sse-server.js diff --git a/core/language/en-GB/Help/listen.tid b/core/language/en-GB/Help/listen.tid index 41425637f..45df72381 100644 --- a/core/language/en-GB/Help/listen.tid +++ b/core/language/en-GB/Help/listen.tid @@ -22,7 +22,6 @@ All parameters are optional with safe defaults, and can be specified in any orde * ''readers'' - comma-separated list of principals allowed to read from this wiki * ''writers'' - comma-separated list of principals allowed to write to this wiki * ''csrf-disable'' - set to "yes" to disable CSRF checks (defaults to "no") -* ''sse-enabled'' - set to "yes" to enable Server-sent events (defaults to "no") * ''root-tiddler'' - the tiddler to serve at the root (defaults to "$:/core/save/all") * ''root-render-type'' - the content type to which the root tiddler should be rendered (defaults to "text/plain") * ''root-serve-type'' - the content type with which the root tiddler should be served (defaults to "text/html") diff --git a/core/modules/server/routes/get-status.js b/core/modules/server/routes/get-status.js index 1d7979336..dd4c1909a 100644 --- a/core/modules/server/routes/get-status.js +++ b/core/modules/server/routes/get-status.js @@ -21,7 +21,6 @@ exports.handler = function(request,response,state) { username: state.authenticatedUsername || state.server.get("anon-username") || "", anonymous: !state.authenticatedUsername, read_only: !state.server.isAuthorized("writers",state.authenticatedUsername), - sse_enabled: state.server.get("sse-enabled") === "yes", space: { recipe: "default" }, diff --git a/core/modules/server/server-sent-events.js b/core/modules/server/server-sent-events.js deleted file mode 100644 index 377d64c7f..000000000 --- a/core/modules/server/server-sent-events.js +++ /dev/null @@ -1,70 +0,0 @@ -/*\ -title: $:/core/modules/server/server-sent-events.js -type: application/javascript -module-type: library -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -/* -parameters: - prefix - usually the plugin path, such as `plugins/tiddlywiki/tiddlyweb`. The - route will match `/events/${prefix}` exactly. - - handler - a function that will be called each time a request comes in with the - request and state from the route and an emit function to call. -*/ - -var ServerSentEvents = function ServerSentEvents(prefix, handler) { - this.handler = handler; - this.prefix = prefix; -}; - -ServerSentEvents.prototype.getExports = function() { - return { - bodyFormat: "stream", - method: "GET", - path: new RegExp("^/events/" + this.prefix + "$"), - handler: this.handleEventRequest.bind(this) - }; -}; - -ServerSentEvents.prototype.handleEventRequest = function(request,response,state) { - if(ServerSentEvents.prototype.isEventStreamRequest(request)) { - response.writeHead(200, { - "Content-Type": "text/event-stream", - "Cache-Control": "no-cache", - "Connection": "keep-alive" - }); - this.handler(request,state,this.emit.bind(this,response),this.end.bind(this,response)); - } else { - response.writeHead(406,"Not Acceptable",{}); - response.end(); - } -}; - -ServerSentEvents.prototype.isEventStreamRequest = function(request) { - return request.headers.accept && - request.headers.accept.match(/^text\/event-stream/); -}; - -ServerSentEvents.prototype.emit = function(response,event,data) { - if(typeof event !== "string" || event.indexOf("\n") !== -1) { - throw new Error("Type must be a single-line string"); - } - if(typeof data !== "string" || data.indexOf("\n") !== -1) { - throw new Error("Data must be a single-line string"); - } - response.write("event: " + event + "\ndata: " + data + "\n\n", "utf8"); -}; - -ServerSentEvents.prototype.end = function(response) { - response.end(); -}; - -exports.ServerSentEvents = ServerSentEvents; - -})(); diff --git a/core/modules/syncer.js b/core/modules/syncer.js index 23d21097f..d5eae1f48 100644 --- a/core/modules/syncer.js +++ b/core/modules/syncer.js @@ -20,7 +20,6 @@ Syncer.prototype.titleIsAnonymous = "$:/status/IsAnonymous"; Syncer.prototype.titleIsReadOnly = "$:/status/IsReadOnly"; Syncer.prototype.titleUserName = "$:/status/UserName"; Syncer.prototype.titleSyncFilter = "$:/config/SyncFilter"; -Syncer.prototype.titleSyncDisablePolling = "$:/config/SyncDisablePolling"; Syncer.prototype.titleSyncPollingInterval = "$:/config/SyncPollingInterval"; Syncer.prototype.titleSyncDisableLazyLoading = "$:/config/SyncDisableLazyLoading"; Syncer.prototype.titleSavedNotification = "$:/language/Notifications/Save/Done"; @@ -90,7 +89,7 @@ function Syncer(options) { if(filteredChanges.length > 0) { self.processTaskQueue(); } else { - // Look for deletions of tiddlers we're already syncing + // Look for deletions of tiddlers we're already syncing var outstandingDeletion = false $tw.utils.each(changes,function(change,title,object) { if(change.deleted && $tw.utils.hop(self.tiddlerInfo,title)) { @@ -122,7 +121,7 @@ function Syncer(options) { self.login(username,password,function() {}); } else { // No username and password, so we display a prompt - self.handleLoginEvent(); + self.handleLoginEvent(); } }); $tw.rootWidget.addEventListener("tm-logout",function() { @@ -139,7 +138,7 @@ function Syncer(options) { if(!this.disableUI && this.wiki.getTiddlerText(this.titleSyncDisableLazyLoading) !== "yes") { this.wiki.addEventListener("lazyLoad",function(title) { self.handleLazyLoadEvent(title); - }); + }); } // Get the login status this.getStatus(function(err,isLoggedIn) { @@ -174,8 +173,8 @@ Syncer.prototype.getTiddlerRevision = function(title) { if(this.syncadaptor && this.syncadaptor.getTiddlerRevision) { return this.syncadaptor.getTiddlerRevision(title); } else { - return this.wiki.getTiddler(title).fields.revision; - } + return this.wiki.getTiddler(title).fields.revision; + } }; /* @@ -268,7 +267,7 @@ Syncer.prototype.getStatus = function(callback) { // Mark us as not logged in this.wiki.addTiddler({title: this.titleIsLoggedIn,text: "no"}); // Get login status - this.syncadaptor.getStatus(function(err,isLoggedIn,username,isReadOnly,isAnonymous,isPollingDisabled) { + this.syncadaptor.getStatus(function(err,isLoggedIn,username,isReadOnly,isAnonymous) { if(err) { self.logger.alert(err); } else { @@ -279,9 +278,6 @@ Syncer.prototype.getStatus = function(callback) { if(isLoggedIn) { self.wiki.addTiddler({title: self.titleUserName,text: username || ""}); } - if(isPollingDisabled) { - self.wiki.addTiddler({title: self.titleSyncDisablePolling, text: "yes"}); - } } // Invoke the callback if(callback) { @@ -305,15 +301,12 @@ Syncer.prototype.syncFromServer = function() { } }, triggerNextSync = function() { - if(pollingEnabled) { - self.pollTimerId = setTimeout(function() { - self.pollTimerId = null; - self.syncFromServer.call(self); - },self.pollTimerInterval); - } + self.pollTimerId = setTimeout(function() { + self.pollTimerId = null; + self.syncFromServer.call(self); + },self.pollTimerInterval); }, - syncSystemFromServer = (self.wiki.getTiddlerText("$:/config/SyncSystemTiddlersFromServer") === "yes"), - pollingEnabled = (self.wiki.getTiddlerText(self.titleSyncDisablePolling) !== "yes"); + syncSystemFromServer = (self.wiki.getTiddlerText("$:/config/SyncSystemTiddlersFromServer") === "yes" ? true : false); if(this.syncadaptor && this.syncadaptor.getUpdatedTiddlers) { this.logger.log("Retrieving updated tiddler list"); cancelNextSync(); @@ -336,7 +329,7 @@ Syncer.prototype.syncFromServer = function() { }); if(updates.modifications.length > 0 || updates.deletions.length > 0) { self.processTaskQueue(); - } + } } }); } else if(this.syncadaptor && this.syncadaptor.getSkinnyTiddlers) { @@ -516,7 +509,7 @@ Syncer.prototype.processTaskQueue = function() { } else { self.updateDirtyStatus(); // Process the next task - self.processTaskQueue.call(self); + self.processTaskQueue.call(self); } }); } else { @@ -524,11 +517,11 @@ Syncer.prototype.processTaskQueue = function() { this.updateDirtyStatus(); // And trigger a timeout if there is a pending task if(task === true) { - this.triggerTimeout(); + this.triggerTimeout(); } } } else { - this.updateDirtyStatus(); + this.updateDirtyStatus(); } }; @@ -562,7 +555,7 @@ Syncer.prototype.chooseNextTask = function() { isReadyToSave = !tiddlerInfo || !tiddlerInfo.timestampLastSaved || tiddlerInfo.timestampLastSaved < thresholdLastSaved; if(hasChanged) { if(isReadyToSave) { - return new SaveTiddlerTask(this,title); + return new SaveTiddlerTask(this,title); } else { havePending = true; } diff --git a/editions/tw5.com/tiddlers/webserver/WebServer Parameter_ sse-enabled.tid b/editions/tw5.com/tiddlers/webserver/WebServer Parameter_ sse-enabled.tid deleted file mode 100644 index cdcdee008..000000000 --- a/editions/tw5.com/tiddlers/webserver/WebServer Parameter_ sse-enabled.tid +++ /dev/null @@ -1,11 +0,0 @@ -caption: sse-enabled -created: 20210113204602693 -modified: 20210115120126953 -tags: [[WebServer Parameters]] -title: WebServer Parameter: sse-enabled -type: text/vnd.tiddlywiki - -The [[web server configuration parameter|WebServer Parameters]] ''sse-enabled'' enables [[Server sent events|https://en.wikipedia.org/wiki/Server-sent_events]], allowing changes to be propagated in almost real time to all browser windows or tabs. - -Setting ''sse-enabled'' to `yes` enables Server-sent events; `no`, or any other value, disables them. - diff --git a/plugins/tiddlywiki/tiddlyweb/sse-client.js b/plugins/tiddlywiki/tiddlyweb/sse-client.js deleted file mode 100644 index e06abe381..000000000 --- a/plugins/tiddlywiki/tiddlyweb/sse-client.js +++ /dev/null @@ -1,56 +0,0 @@ -/*\ -title: $:/plugins/tiddlywiki/tiddlyweb/sse-client.js -type: application/javascript -module-type: startup - -GET /recipes/default/tiddlers/:title - -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -exports.name = "/events/plugins/tiddlywiki/tiddlyweb"; -exports.after = ["startup"]; -exports.synchronous = true; -exports.platforms = ["browser"]; -exports.startup = function() { - // Make sure we're actually being used - if($tw.syncadaptor.name !== "tiddlyweb") { - return; - } - // Get the mount point in case a path prefix is used - var host = $tw.syncadaptor.getHost(); - // Make sure it ends with a slash (it usually does) - if(host[host.length - 1] !== "/") { - host += "/"; - } - // Setup the event listener - setupEvents(host); -}; - -function debounce(callback) { - var timeout = null; - return function() { - clearTimeout(timeout); - timeout = setTimeout(callback,$tw.syncer.throttleInterval); - }; -} - -function setupEvents(host) { - if(window.EventSource) { - var events = new EventSource(host + "events/plugins/tiddlywiki/tiddlyweb"); - var debouncedSync = debounce($tw.syncer.syncFromServer.bind($tw.syncer)); - events.addEventListener("change",debouncedSync); - events.onerror = function() { - events.close(); - setTimeout(function() { - setupEvents(host); - },$tw.syncer.errorRetryInterval); - }; - } -} - -})(); diff --git a/plugins/tiddlywiki/tiddlyweb/sse-server.js b/plugins/tiddlywiki/tiddlyweb/sse-server.js deleted file mode 100644 index a31b8064b..000000000 --- a/plugins/tiddlywiki/tiddlyweb/sse-server.js +++ /dev/null @@ -1,94 +0,0 @@ -/*\ -title: $:/plugins/tiddlywiki/tiddlyweb/sse-server.js -type: application/javascript -module-type: route - -GET /events/plugins/tiddlywiki/tiddlyweb - -\*/ -(function(){ - -/*jslint node: true, browser: true */ -/*global $tw: false */ -"use strict"; - -var wikis = []; -var connections = []; - -/* -Setup up the array for this wiki and add the change listener -*/ -function setupWiki(wiki) { - var index = wikis.length; - // Add a new array for this wiki (object references work as keys) - wikis.push(wiki); - connections.push([]); - // Listen to change events for this wiki - wiki.addEventListener("change",function(changes) { - var jsonChanges = JSON.stringify(changes); - getWikiConnections(wiki).forEach(function(item) { - item.emit("change",jsonChanges); - }); - }); - return index; -} - -/* -Setup this particular wiki if we haven't seen it before -*/ -function ensureWikiSetup(wiki) { - if(wikis.indexOf(wiki) === -1) { - setupWiki(wiki); - } -} - -/* -Return the array of connections for a particular wiki -*/ -function getWikiConnections(wiki) { - return connections[wikis.indexOf(wiki)]; -} - -function addWikiConnection(wiki,connection) { - getWikiConnections(wiki).push(connection); -} - -function removeWikiConnection(wiki,connection) { - var wikiConnections = getWikiConnections(wiki); - var index = wikiConnections.indexOf(connection); - if(index !== -1) { - wikiConnections.splice(index,1); - } -} - -function handleConnection(request,state,emit,end) { - if(isDisabled(state)) { - return; - } - - ensureWikiSetup(state.wiki); - // Add the connection to the list of connections for this wiki - var connection = { - request: request, - state: state, - emit: emit, - end: end - }; - addWikiConnection(state.wiki,connection); - request.on("close",function() { - removeWikiConnection(state.wiki,connection); - }); -} - -function isDisabled(state) { - return state.server.get("sse-enabled") !== "yes"; -} - -// Import the ServerSentEvents class -var ServerSentEvents = require("$:/core/modules/server/server-sent-events.js").ServerSentEvents; -// Instantiate the class -var events = new ServerSentEvents("plugins/tiddlywiki/tiddlyweb", handleConnection); -// Export the route definition for this server sent events instance -module.exports = events.getExports(); - -})(); diff --git a/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js b/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js index ff4db2185..683c305c8 100644 --- a/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js +++ b/plugins/tiddlywiki/tiddlyweb/tiddlywebadaptor.js @@ -91,12 +91,10 @@ TiddlyWebAdaptor.prototype.getStatus = function(callback) { self.isLoggedIn = json.username !== "GUEST"; self.isReadOnly = !!json["read_only"]; self.isAnonymous = !!json.anonymous; - - var isSseEnabled = !!json.sse_enabled; } // Invoke the callback if present if(callback) { - callback(null,self.isLoggedIn,json.username,self.isReadOnly,self.isAnonymous,isSseEnabled); + callback(null,self.isLoggedIn,json.username,self.isReadOnly,self.isAnonymous); } } });