mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-14 13:54:50 +00:00
c05c0d3df6
* Module-ize server routes and add static file support (#2510)
* Refactor server routes to modules
New module type: serverroute
Caveats: Loading order is not deterministic but this would only matter
if two route modules attempted to use the same path regexp (that would
be silly).
* Add static assets plugin
This plugin allows the node server to fetch static assets in the /assets
directory. I felt that this was a feature that goes above the core
functionality. That is why I added it as a plugin. with the modular
route extensions this was a breeze.
* Add serverroute description to ModuleTypes
* Coding standards tweaks
* Fix filename typo
* Move support for attachments from a plugin into the core
* Missing "else"
* Refactor server handling
* Introduce a new named parameter scheme for commands
* Move the SimpleServer class into it's own module
* Deprecate the --server command because of the unwieldy syntax
* Add a new --listen command using the new syntax
For example:
tiddlywiki mywiki --listen host:0.0.0.0 port:8090
* Add check for unknown parameters
* Add support for multiple basic authentication credentials in a CSV file
Beware: Passwords are stored in plain text. If that's a problem, use an authenticating proxy and the trusted header authentication approach.
* Refactor module locations
* Rename "serverroute" module type to "route"
* Remove support for verifying optional named command parameters
The idea was to be able to flag unknown parameter names, but requiring a command to pre-specify all the parameter names makes it harder for (say) the listen command to be extensible so that plugins can add new optional parameters that they handle. (This is particularly in the context of work in progress to encapsulate authenticators into their own modules).
* Refactor the two authenticators into separate modules and add support for authorization
* Correct mistaken path.join vs. path.resolve
See https://stackoverflow.com/a/39836259
* Docs for the named command parameters
I'd be grateful if anyone with sufficient Windows experience could confirm that the note about double quotes in "NamedCommandParameters" is correct.
* Be consistent about lower case parameter names
* Do the right thing when we have a username but no password
With a username parameter but no password parameter we'll attribute edits to that username, but not require authentication.
* Remove obsolete code
* Add support for requiring authentication without restricting the username
* Refactor authorization checks
* Return read_only status in /status response
* Fix two code typos
* Add basic support for detecting readonly status and avoiding write errors
We now have syncadaptors returning readonly status and avoid attempting to write to the server if it's going to fail
* Add readonly-styles
We hide editing-related buttons in read only mode
I've made this part of the tiddlyweb plugin but I think a case could be made for putting it into the core.
* Add custom request header as CSRF mitigation
By default we require the header X-Requested-With to be set to TiddlyWiki. Can be overriden by setting csrfdisable to "yes"
See https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Protecting_REST_Services:_Use_of_Custom_Request_Headers
* Add support for HTTPS
* First pass at a route for serving rendered tiddlers
cc @Drakor
* Tweaks to the single tiddler static view
Adding a simple sidebar
* Switch to "dash" separated parameter names
* Typo
* Docs: Update ServerCommand and ListenCommand
* First pass at docs for the new web server stuff
Writing the docs is turning out to be quite an undertaking, much harder than writing the code!
* Get rid of extraneous paragraphs in static renderings
* Rejig anonymous user handling
Now we can support wikis that are read-only for anonymous access, but allow a user to login for read/write access.
* More docs
Slowly getting there...
* Static tiddler rendering: Fix HTML content in page title
* Docs updates
* Fix server command parameter names
Missed off 30ce7ea
* Docs: Missing quotes
* Avoid inadvertent dependency on Node.js > v9.6.0
The listenOptions parameter of the plain HTTP version of CreateServer was only introduced in v9.6.0
cc @Drakor @pmario
* Typo
178 lines
4.7 KiB
JavaScript
178 lines
4.7 KiB
JavaScript
/*\
|
|
title: $:/core/modules/commander.js
|
|
type: application/javascript
|
|
module-type: global
|
|
|
|
The $tw.Commander class is a command interpreter
|
|
|
|
\*/
|
|
(function(){
|
|
|
|
/*jslint node: true, browser: true */
|
|
/*global $tw: false */
|
|
"use strict";
|
|
|
|
/*
|
|
Parse a sequence of commands
|
|
commandTokens: an array of command string tokens
|
|
wiki: reference to the wiki store object
|
|
streams: {output:, error:}, each of which has a write(string) method
|
|
callback: a callback invoked as callback(err) where err is null if there was no error
|
|
*/
|
|
var Commander = function(commandTokens,callback,wiki,streams) {
|
|
var path = require("path");
|
|
this.commandTokens = commandTokens;
|
|
this.nextToken = 0;
|
|
this.callback = callback;
|
|
this.wiki = wiki;
|
|
this.streams = streams;
|
|
this.outputPath = path.resolve($tw.boot.wikiPath,$tw.config.wikiOutputSubDir);
|
|
};
|
|
|
|
/*
|
|
Log a string if verbose flag is set
|
|
*/
|
|
Commander.prototype.log = function(str) {
|
|
if(this.verbose) {
|
|
this.streams.output.write(str + "\n");
|
|
}
|
|
};
|
|
|
|
/*
|
|
Write a string if verbose flag is set
|
|
*/
|
|
Commander.prototype.write = function(str) {
|
|
if(this.verbose) {
|
|
this.streams.output.write(str);
|
|
}
|
|
};
|
|
|
|
/*
|
|
Add a string of tokens to the command queue
|
|
*/
|
|
Commander.prototype.addCommandTokens = function(commandTokens) {
|
|
var params = commandTokens.slice(0);
|
|
params.unshift(0);
|
|
params.unshift(this.nextToken);
|
|
Array.prototype.splice.apply(this.commandTokens,params);
|
|
};
|
|
|
|
/*
|
|
Execute the sequence of commands and invoke a callback on completion
|
|
*/
|
|
Commander.prototype.execute = function() {
|
|
this.executeNextCommand();
|
|
};
|
|
|
|
/*
|
|
Execute the next command in the sequence
|
|
*/
|
|
Commander.prototype.executeNextCommand = function() {
|
|
var self = this;
|
|
// Invoke the callback if there are no more commands
|
|
if(this.nextToken >= this.commandTokens.length) {
|
|
this.callback(null);
|
|
} else {
|
|
// Get and check the command token
|
|
var commandName = this.commandTokens[this.nextToken++];
|
|
if(commandName.substr(0,2) !== "--") {
|
|
this.callback("Missing command: " + commandName);
|
|
} else {
|
|
commandName = commandName.substr(2); // Trim off the --
|
|
// Accumulate the parameters to the command
|
|
var params = [];
|
|
while(this.nextToken < this.commandTokens.length &&
|
|
this.commandTokens[this.nextToken].substr(0,2) !== "--") {
|
|
params.push(this.commandTokens[this.nextToken++]);
|
|
}
|
|
// Get the command info
|
|
var command = $tw.commands[commandName],
|
|
c,err;
|
|
if(!command) {
|
|
this.callback("Unknown command: " + commandName);
|
|
} else {
|
|
if(this.verbose) {
|
|
this.streams.output.write("Executing command: " + commandName + " " + params.join(" ") + "\n");
|
|
}
|
|
// Parse named parameters if required
|
|
if(command.info.namedParameterMode) {
|
|
params = this.extractNamedParameters(params,command.info.mandatoryParameters);
|
|
if(typeof params === "string") {
|
|
return this.callback(params);
|
|
}
|
|
}
|
|
if(command.info.synchronous) {
|
|
// Synchronous command
|
|
c = new command.Command(params,this);
|
|
err = c.execute();
|
|
if(err) {
|
|
this.callback(err);
|
|
} else {
|
|
this.executeNextCommand();
|
|
}
|
|
} else {
|
|
// Asynchronous command
|
|
c = new command.Command(params,this,function(err) {
|
|
if(err) {
|
|
self.callback(err);
|
|
} else {
|
|
self.executeNextCommand();
|
|
}
|
|
});
|
|
err = c.execute();
|
|
if(err) {
|
|
this.callback(err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
/*
|
|
Given an array of parameter strings `params` in name:value format, and an array of mandatory parameter names in `mandatoryParameters`, returns a hashmap of values or a string if error
|
|
*/
|
|
Commander.prototype.extractNamedParameters = function(params,mandatoryParameters) {
|
|
mandatoryParameters = mandatoryParameters || [];
|
|
var errors = [],
|
|
paramsByName = Object.create(null);
|
|
// Extract the parameters
|
|
$tw.utils.each(params,function(param) {
|
|
var index = param.indexOf("=");
|
|
if(index < 1) {
|
|
errors.push("malformed named parameter: '" + param + "'");
|
|
}
|
|
paramsByName[param.slice(0,index)] = $tw.utils.trim(param.slice(index+1));
|
|
});
|
|
// Check the mandatory parameters are present
|
|
$tw.utils.each(mandatoryParameters,function(mandatoryParameter) {
|
|
if(!$tw.utils.hop(paramsByName,mandatoryParameter)) {
|
|
errors.push("missing mandatory parameter: '" + mandatoryParameter + "'");
|
|
}
|
|
});
|
|
// Return any errors
|
|
if(errors.length > 0) {
|
|
return errors.join(" and\n");
|
|
} else {
|
|
return paramsByName;
|
|
}
|
|
};
|
|
|
|
Commander.initCommands = function(moduleType) {
|
|
moduleType = moduleType || "command";
|
|
$tw.commands = {};
|
|
$tw.modules.forEachModuleOfType(moduleType,function(title,module) {
|
|
var c = $tw.commands[module.info.name] = {};
|
|
// Add the methods defined by the module
|
|
for(var f in module) {
|
|
if($tw.utils.hop(module,f)) {
|
|
c[f] = module[f];
|
|
}
|
|
}
|
|
});
|
|
};
|
|
|
|
exports.Commander = Commander;
|
|
|
|
})();
|