Stream wiki generation

Avoids "string too long" errors when working with big tiddlers (>100MB)
This commit is contained in:
Jeremy Ruston 2024-01-19 10:52:12 +00:00
parent 4f37355a9f
commit 4133e7d6d6
1 changed files with 39 additions and 35 deletions

View File

@ -19,42 +19,46 @@ exports.path = /^\/wiki\/([^\/]+)$/;
exports.handler = function(request,response,state) { exports.handler = function(request,response,state) {
// Get the recipe name from the parameters // Get the recipe name from the parameters
var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]); var recipe_name = $tw.utils.decodeURIComponentSafe(state.params[0]);
// Get the tiddlers in the recipe // Check request is valid
var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name); if(recipe_name) {
// Render the template // Start the response
var template = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{ response.writeHead(200, "OK",{
variables: { "Content-Type": "text/html"
saveTiddlerFilter: ` });
$:/boot/boot.css // Get the tiddlers in the recipe
$:/boot/boot.js var titles = $tw.sqlTiddlerStore.getRecipeTiddlers(recipe_name);
$:/boot/bootprefix.js // Render the template
$:/core var template = $tw.wiki.renderTiddler("text/plain","$:/core/templates/tiddlywiki5.html",{
$:/library/sjcl.js variables: {
$:/plugins/tiddlywiki/tiddlyweb saveTiddlerFilter: `
$:/themes/tiddlywiki/snowwhite $:/boot/boot.css
$:/themes/tiddlywiki/vanilla $:/boot/boot.js
` $:/boot/bootprefix.js
$:/core
$:/library/sjcl.js
$:/plugins/tiddlywiki/tiddlyweb
$:/themes/tiddlywiki/snowwhite
$:/themes/tiddlywiki/vanilla
`
}
});
// Splice in our tiddlers
var marker = `<` + `script class="tiddlywiki-tiddler-store" type="application/json">[`,
markerPos = template.indexOf(marker);
if(markerPos === -1) {
throw new Error("Cannot find tiddler store in template");
} }
}); response.write(template.substring(0,markerPos + marker.length));
// Splice in our tiddlers $tw.utils.each(titles,function(title) {
var marker = `<` + `script class="tiddlywiki-tiddler-store" type="application/json">[`, var tiddler = $tw.sqlTiddlerStore.getTiddler(title,recipe_name);
markerPos = template.indexOf(marker); response.write(JSON.stringify(Object.assign({},tiddler,{revision: "0", bag: "bag-gamma"})));
if(markerPos === -1) { response.write(",")
throw new Error("Cannot find tiddler store in template"); });
} response.write(JSON.stringify({title: "$:/config/tiddlyweb/host",text: "$protocol$//$host$$pathname$/"}));
var htmlParts = []; response.write(",")
htmlParts.push(template.substring(0,markerPos + marker.length)); response.write(template.substring(markerPos + marker.length))
$tw.utils.each(titles,function(title) { // Finish response
var tiddler = $tw.sqlTiddlerStore.getTiddler(title,recipe_name); response.end();
htmlParts.push(JSON.stringify(Object.assign({},tiddler,{revision: "0", bag: "bag-gamma"})));
htmlParts.push(",")
});
htmlParts.push(JSON.stringify({title: "$:/config/tiddlyweb/host",text: "$protocol$//$host$$pathname$/"}));
htmlParts.push(",")
htmlParts.push(template.substring(markerPos + marker.length))
// Send response
if(htmlParts) {
state.sendResponse(200,{"Content-Type": "text/html"},htmlParts.join("\n"),"utf8");
} else { } else {
response.writeHead(404); response.writeHead(404);
response.end(); response.end();