1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Allow setting boot, wiki, and pathPrefix for each request (#4649)

* Add pathPrefix to state, and options to request handler

* use ternary operator instead of default empty object

* Fix styling issues

* Update server.js

* Add boot to server and filesystem adapter
This commit is contained in:
Arlen22 2020-06-11 06:36:41 -04:00 committed by GitHub
parent 2c24f30cdd
commit d32fb6f900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 17 deletions

View File

@ -31,7 +31,7 @@ BasicAuthenticator.prototype.init = function() {
// Read the credentials data // Read the credentials data
this.credentialsFilepath = this.server.get("credentials"); this.credentialsFilepath = this.server.get("credentials");
if(this.credentialsFilepath) { if(this.credentialsFilepath) {
var resolveCredentialsFilepath = path.resolve($tw.boot.wikiPath,this.credentialsFilepath); var resolveCredentialsFilepath = path.resolve(this.server.boot.wikiPath,this.credentialsFilepath);
if(fs.existsSync(resolveCredentialsFilepath) && !fs.statSync(resolveCredentialsFilepath).isDirectory()) { if(fs.existsSync(resolveCredentialsFilepath) && !fs.statSync(resolveCredentialsFilepath).isDirectory()) {
var credentialsText = fs.readFileSync(resolveCredentialsFilepath,"utf8"), var credentialsText = fs.readFileSync(resolveCredentialsFilepath,"utf8"),
credentialsData = $tw.utils.parseCsvStringWithHeader(credentialsText); credentialsData = $tw.utils.parseCsvStringWithHeader(credentialsText);

View File

@ -21,7 +21,7 @@ exports.handler = function(request,response,state) {
fs = require("fs"), fs = require("fs"),
util = require("util"), util = require("util"),
suppliedFilename = decodeURIComponent(state.params[0]), suppliedFilename = decodeURIComponent(state.params[0]),
filename = path.resolve($tw.boot.wikiPath,"files",suppliedFilename), filename = path.resolve(state.boot.wikiPath,"files",suppliedFilename),
extension = path.extname(filename); extension = path.extname(filename);
fs.readFile(filename,function(err,content) { fs.readFile(filename,function(err,content) {
var status,content,type = "text/plain"; var status,content,type = "text/plain";

View File

@ -31,6 +31,7 @@ function Server(options) {
this.routes = options.routes || []; this.routes = options.routes || [];
this.authenticators = options.authenticators || []; this.authenticators = options.authenticators || [];
this.wiki = options.wiki; this.wiki = options.wiki;
this.boot = options.boot || $tw.boot;
this.servername = $tw.utils.transliterateToSafeASCII(this.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5"); this.servername = $tw.utils.transliterateToSafeASCII(this.wiki.getTiddlerText("$:/SiteTitle") || "TiddlyWiki5");
// Initialise the variables // Initialise the variables
this.variables = $tw.utils.extend({},this.defaultVariables); this.variables = $tw.utils.extend({},this.defaultVariables);
@ -69,8 +70,8 @@ function Server(options) {
tlsCertFilepath = this.get("tls-cert"); tlsCertFilepath = this.get("tls-cert");
if(tlsCertFilepath && tlsKeyFilepath) { if(tlsCertFilepath && tlsKeyFilepath) {
this.listenOptions = { this.listenOptions = {
key: fs.readFileSync(path.resolve($tw.boot.wikiPath,tlsKeyFilepath),"utf8"), key: fs.readFileSync(path.resolve(this.boot.wikiPath,tlsKeyFilepath),"utf8"),
cert: fs.readFileSync(path.resolve($tw.boot.wikiPath,tlsCertFilepath),"utf8") cert: fs.readFileSync(path.resolve(this.boot.wikiPath,tlsCertFilepath),"utf8")
}; };
this.protocol = "https"; this.protocol = "https";
} }
@ -112,15 +113,14 @@ Server.prototype.addAuthenticator = function(AuthenticatorClass) {
}; };
Server.prototype.findMatchingRoute = function(request,state) { Server.prototype.findMatchingRoute = function(request,state) {
var pathprefix = this.get("path-prefix") || "";
for(var t=0; t<this.routes.length; t++) { for(var t=0; t<this.routes.length; t++) {
var potentialRoute = this.routes[t], var potentialRoute = this.routes[t],
pathRegExp = potentialRoute.path, pathRegExp = potentialRoute.path,
pathname = state.urlInfo.pathname, pathname = state.urlInfo.pathname,
match; match;
if(pathprefix) { if(state.pathPrefix) {
if(pathname.substr(0,pathprefix.length) === pathprefix) { if(pathname.substr(0,state.pathPrefix.length) === state.pathPrefix) {
pathname = pathname.substr(pathprefix.length) || "/"; pathname = pathname.substr(state.pathPrefix.length) || "/";
match = potentialRoute.path.exec(pathname); match = potentialRoute.path.exec(pathname);
} else { } else {
match = false; match = false;
@ -156,14 +156,17 @@ Server.prototype.isAuthorized = function(authorizationType,username) {
return principals.indexOf("(anon)") !== -1 || (username && (principals.indexOf("(authenticated)") !== -1 || principals.indexOf(username) !== -1)); return principals.indexOf("(anon)") !== -1 || (username && (principals.indexOf("(authenticated)") !== -1 || principals.indexOf(username) !== -1));
} }
Server.prototype.requestHandler = function(request,response) { Server.prototype.requestHandler = function(request,response,options) {
options = options || {};
// Compose the state object // Compose the state object
var self = this; var self = this;
var state = {}; var state = {};
state.wiki = self.wiki; state.wiki = options.wiki || self.wiki;
state.boot = options.boot || self.boot;
state.server = self; state.server = self;
state.urlInfo = url.parse(request.url); state.urlInfo = url.parse(request.url);
state.queryParameters = querystring.parse(state.urlInfo.query); state.queryParameters = querystring.parse(state.urlInfo.query);
state.pathPrefix = options.pathPrefix || this.get("path-prefix") || "";
// Get the principals authorized to access this resource // Get the principals authorized to access this resource
var authorizationType = this.methodMappings[request.method] || "readers"; var authorizationType = this.methodMappings[request.method] || "readers";
// Check for the CSRF header if this is a write // Check for the CSRF header if this is a write

View File

@ -19,9 +19,10 @@ var fs = $tw.node ? require("fs") : null,
function FileSystemAdaptor(options) { function FileSystemAdaptor(options) {
var self = this; var self = this;
this.wiki = options.wiki; this.wiki = options.wiki;
this.boot = options.boot || $tw.boot;
this.logger = new $tw.utils.Logger("filesystem",{colour: "blue"}); this.logger = new $tw.utils.Logger("filesystem",{colour: "blue"});
// Create the <wiki>/tiddlers folder if it doesn't exist // Create the <wiki>/tiddlers folder if it doesn't exist
$tw.utils.createDirectory($tw.boot.wikiTiddlersPath); $tw.utils.createDirectory(this.boot.wikiTiddlersPath);
} }
FileSystemAdaptor.prototype.name = "filesystem"; FileSystemAdaptor.prototype.name = "filesystem";
@ -43,22 +44,22 @@ Return a fileInfo object for a tiddler, creating it if necessary:
type: the type of the tiddler file (NOT the type of the tiddler -- see below) type: the type of the tiddler file (NOT the type of the tiddler -- see below)
hasMetaFile: true if the file also has a companion .meta file hasMetaFile: true if the file also has a companion .meta file
The boot process populates $tw.boot.files for each of the tiddler files that it loads. The type is found by looking up the extension in $tw.config.fileExtensionInfo (eg "application/x-tiddler" for ".tid" files). The boot process populates this.boot.files for each of the tiddler files that it loads. The type is found by looking up the extension in $tw.config.fileExtensionInfo (eg "application/x-tiddler" for ".tid" files).
It is the responsibility of the filesystem adaptor to update $tw.boot.files for new files that are created. It is the responsibility of the filesystem adaptor to update this.boot.files for new files that are created.
*/ */
FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) { FileSystemAdaptor.prototype.getTiddlerFileInfo = function(tiddler,callback) {
// See if we've already got information about this file // See if we've already got information about this file
var title = tiddler.fields.title, var title = tiddler.fields.title,
fileInfo = $tw.boot.files[title]; fileInfo = this.boot.files[title];
if(!fileInfo) { if(!fileInfo) {
// Otherwise, we'll need to generate it // Otherwise, we'll need to generate it
fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{ fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{
directory: $tw.boot.wikiTiddlersPath, directory: this.boot.wikiTiddlersPath,
pathFilters: this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"), pathFilters: this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"),
wiki: this.wiki wiki: this.wiki
}); });
$tw.boot.files[title] = fileInfo; this.boot.files[title] = fileInfo;
} }
callback(null,fileInfo); callback(null,fileInfo);
}; };
@ -91,7 +92,7 @@ Delete a tiddler and invoke the callback with (err)
*/ */
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) { FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
var self = this, var self = this,
fileInfo = $tw.boot.files[title]; fileInfo = this.boot.files[title];
// Only delete the tiddler if we have writable information for the file // Only delete the tiddler if we have writable information for the file
if(fileInfo) { if(fileInfo) {
// Delete the file // Delete the file