Skip to content

Commit

Permalink
Create new static index route with ability to create/update bags and …
Browse files Browse the repository at this point in the history
…recipes

Also introduces a new /.system/filename route for stylesheets, scripts etc.
  • Loading branch information
Jermolene committed Mar 20, 2024
1 parent 1c64646 commit 6063256
Show file tree
Hide file tree
Showing 23 changed files with 304 additions and 318 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"scripts": {
"start": "node ./tiddlywiki.js ./editions/multiwikiserver --listen",
"test": "node ./tiddlywiki.js ./editions/test --verbose --version --build index && node ./tiddlywiki ./editions/multiwikiserver/ --listen debug-level=full --mws-test-server http://127.0.0.1:8080/ --quit",
"test": "node ./tiddlywiki.js ./editions/test --verbose --version --build index && node ./tiddlywiki ./editions/multiwikiserver/ --mws-listen debug-level=full --mws-test-server http://127.0.0.1:8080/ --quit",
"lint:fix": "eslint . --fix",
"lint": "eslint ."
},
Expand Down
19 changes: 0 additions & 19 deletions plugins/tiddlywiki/multiwikiserver/admin-ui/AdminLayout.tid

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions plugins/tiddlywiki/multiwikiserver/admin-ui/SideBarSegment.tid

This file was deleted.

2 changes: 0 additions & 2 deletions plugins/tiddlywiki/multiwikiserver/admin-ui/favicon.png.meta

This file was deleted.

2 changes: 0 additions & 2 deletions plugins/tiddlywiki/multiwikiserver/admin-ui/layout.tid

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Command.prototype.execute = function() {
}
// Set up server
this.server = $tw.mws.serverManager.createServer({
wiki: $tw.wiki
wiki: $tw.wiki,
variables: self.params
});
this.server.listen(null,null,null,{
callback: function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ TestRunner.prototype.runTests = function(callback) {
};

TestRunner.prototype.runTest = function(testSpec,callback) {
const self = this;
console.log(`Running Server Test: ${testSpec.description}`)
if(testSpec.method === "GET" || testSpec.method === "POST") {
const request = this.httpLibrary.request({
Expand All @@ -84,8 +85,8 @@ TestRunner.prototype.runTest = function(testSpec,callback) {
method: testSpec.method,
headers: testSpec.headers
}, function(response) {
if (response.statusCode < 200 || response.statusCode >= 300) {
return callback(`Request failed to ${response.url} with status code ${response.statusCode} and ${JSON.stringify(response.headers)}`);
if (response.statusCode < 200 || response.statusCode >= 400) {
return callback(`Request failed to ${self.urlServerParsed.toString()} with status code ${response.statusCode} and ${JSON.stringify(response.headers)}`);
}
response.setEncoding("utf8");
let buffer = "";
Expand All @@ -94,7 +95,7 @@ TestRunner.prototype.runTest = function(testSpec,callback) {
});
response.on("end", () => {
const jsonData = $tw.utils.parseJSONSafe(buffer,function() {return undefined;});
const testResult = testSpec.expectedResult(jsonData,buffer);
const testResult = testSpec.expectedResult(jsonData,buffer,response.headers);
callback(testResult ? null : "Test failed");
});
});
Expand Down Expand Up @@ -135,6 +136,20 @@ const testSpecs = [
expectedResult: (jsonData,data) => {
return jsonData["imported-tiddlers"] && $tw.utils.isArray(jsonData["imported-tiddlers"]) && jsonData["imported-tiddlers"][0] === "One White Pixel";
}
},
{
description: "Create a recipe",
method: "POST",
path: "/recipes",
headers: {
"Accept": '*/*',
"Content-Type": 'application/x-www-form-urlencoded',
"User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
},
data: "recipe_name=Elephants3214234&bag_names=one%20two%20three&description=A%20bag%20of%20elephants",
expectedResult: (jsonData,data,headers) => {
return headers.location === "/";
}
}
];

Expand Down
9 changes: 7 additions & 2 deletions plugins/tiddlywiki/multiwikiserver/modules/mws-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ Server.prototype.findMatchingRoute = function(request,state) {
} else {
match = potentialRoute.path.exec(pathname);
}
if(match && request.method === potentialRoute.method) {
// Allow POST as a synonym for PUT because HTML doesn't allow PUT forms
if(match && (request.method === potentialRoute.method || (request.method === "POST" && potentialRoute.method === "PUT"))) {
state.params = [];
for(var p=1; p<match.length; p++) {
state.params.push(match[p]);
Expand Down Expand Up @@ -346,6 +347,7 @@ Server.prototype.isAuthorized = function(authorizationType,username) {

Server.prototype.requestHandler = function(request,response,options) {
options = options || {};
const queryString = require("querystring");
// Compose the state object
var self = this;
var state = {};
Expand Down Expand Up @@ -399,14 +401,17 @@ Server.prototype.requestHandler = function(request,response,options) {
if(route.bodyFormat === "stream" || request.method === "GET" || request.method === "HEAD") {
// Let the route handle the request stream itself
route.handler(request,response,state);
} else if(route.bodyFormat === "string" || !route.bodyFormat) {
} else if(route.bodyFormat === "string" || route.bodyFormat === "www-form-urlencoded" || !route.bodyFormat) {
// Set the encoding for the incoming request
request.setEncoding("utf8");
var data = "";
request.on("data",function(chunk) {
data += chunk.toString();
});
request.on("end",function() {
if(route.bodyFormat === "www-form-urlencoded") {
data = queryString.parse(data);
}
state.data = data;
route.handler(request,response,state);
});
Expand Down
Loading

0 comments on commit 6063256

Please sign in to comment.