1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-30 14:46:19 +00:00
TiddlyWiki5/core/modules/server/server-sent-events.js

71 lines
1.9 KiB
JavaScript
Raw Normal View History

Add server sent events (#5279) * Create server-sent-events.js * Create sse-change-listener.js * Implement server sent events * Convert to ES5 and wrap in function * Use the host string from tiddlyweb * Improve comments in sse-server.js * Can't use object reference as key * Add retry timeout * Fix a bug * bug fix * Fix formatting * Fix ES5 compat * capitalize comments * more fixes * Refactor tiddlywek/sse-server.js * Extract helper functions for handling wikis and connections. * Replace JSDoc comments. * Fix formatting according to TW core. * Simplify the logic for adding and removing connections. * Fix formatting of tiddlyweb/sse-client.js Fix formatting according to TW core. * Fix formatting of server-sent-events.js Fix formatting and comments following TW core guidelines. * Extract a debounce function in sse-client.js * Avoid using startsWith in server-sent-events.js startsWith is part of ES2015, while TiddlyWiki uses the 5.1 dialect. * New sse-enabled WebServer parameter * If not set to "yes", disabled SSE request handling. * Add documentation for the parameter in core/language/en-GB/Help/listen.tid * Add new tiddler editions/tw5.com/tiddlers/webserver/WebServer Parameter_ sse-enabled.tid * Disable polling for changes if SSE is enabled * Add sse_enabled to /status JSON response * Store syncer polling status in $:/config/SyncDisablePolling * Handled disabling polling in core/modules/syncer.js * Simply boolean logic in syncer.js * Delete trailing whitespaces in syncer.js Co-authored-by: Arlen22 <arlenbee@gmail.com>
2021-01-15 10:37:55 +00:00
/*\
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;
})();