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
optimising-macrocalls
Arlen22 2020-06-11 06:36:41 -04:00 zatwierdzone przez GitHub
rodzic 2c24f30cdd
commit d32fb6f900
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
4 zmienionych plików z 21 dodań i 17 usunięć

Wyświetl plik

@ -31,7 +31,7 @@ BasicAuthenticator.prototype.init = function() {
// Read the credentials data
this.credentialsFilepath = this.server.get("credentials");
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()) {
var credentialsText = fs.readFileSync(resolveCredentialsFilepath,"utf8"),
credentialsData = $tw.utils.parseCsvStringWithHeader(credentialsText);

Wyświetl plik

@ -21,7 +21,7 @@ exports.handler = function(request,response,state) {
fs = require("fs"),
util = require("util"),
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);
fs.readFile(filename,function(err,content) {
var status,content,type = "text/plain";

Wyświetl plik

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

Wyświetl plik

@ -19,9 +19,10 @@ var fs = $tw.node ? require("fs") : null,
function FileSystemAdaptor(options) {
var self = this;
this.wiki = options.wiki;
this.boot = options.boot || $tw.boot;
this.logger = new $tw.utils.Logger("filesystem",{colour: "blue"});
// 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";
@ -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)
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) {
// See if we've already got information about this file
var title = tiddler.fields.title,
fileInfo = $tw.boot.files[title];
fileInfo = this.boot.files[title];
if(!fileInfo) {
// Otherwise, we'll need to generate it
fileInfo = $tw.utils.generateTiddlerFileInfo(tiddler,{
directory: $tw.boot.wikiTiddlersPath,
directory: this.boot.wikiTiddlersPath,
pathFilters: this.wiki.getTiddlerText("$:/config/FileSystemPaths","").split("\n"),
wiki: this.wiki
});
$tw.boot.files[title] = fileInfo;
this.boot.files[title] = fileInfo;
}
callback(null,fileInfo);
};
@ -91,7 +92,7 @@ Delete a tiddler and invoke the callback with (err)
*/
FileSystemAdaptor.prototype.deleteTiddler = function(title,callback,options) {
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
if(fileInfo) {
// Delete the file