mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +00:00
Added support for retrieving ingredients over HTTP
This makes it possible to directly reference recipes and tiddlers stored on GitHub, for example.
This commit is contained in:
parent
dc3546380a
commit
6bbc6c0dac
@ -5,6 +5,10 @@ throttling so that we don't get error EMFILE "Too many open files".
|
|||||||
|
|
||||||
var fs = require("fs"),
|
var fs = require("fs"),
|
||||||
path = require("path"),
|
path = require("path"),
|
||||||
|
url = require("url"),
|
||||||
|
util = require("util"),
|
||||||
|
http = require("http"),
|
||||||
|
https = require("https"),
|
||||||
utils = require("./Utils.js");
|
utils = require("./Utils.js");
|
||||||
|
|
||||||
var FileRetriever = exports;
|
var FileRetriever = exports;
|
||||||
@ -12,15 +16,53 @@ var FileRetriever = exports;
|
|||||||
var fileRequestQueue = utils.queue(function(task,callback) {
|
var fileRequestQueue = utils.queue(function(task,callback) {
|
||||||
fs.readFile(task.filepath,"utf8", function(err,data) {
|
fs.readFile(task.filepath,"utf8", function(err,data) {
|
||||||
callback(err,data);
|
callback(err,data);
|
||||||
|
console.error("Retrieved " + task.filepath);
|
||||||
});
|
});
|
||||||
},10);
|
},10);
|
||||||
|
|
||||||
|
var httpRequestQueue = utils.queue(function(task,callback) {
|
||||||
|
var opts = url.parse(task.url);
|
||||||
|
var httpLib = opts.protocol === "http:" ? http : https;
|
||||||
|
var request = httpLib.get(opts,function(res) {
|
||||||
|
if(res.statusCode != 200) {
|
||||||
|
var err = new Error("HTTP error");
|
||||||
|
err.code = res.statusCode.toString();
|
||||||
|
callback(err);
|
||||||
|
} else {
|
||||||
|
var data = [];
|
||||||
|
res.on("data", function(chunk) {
|
||||||
|
data.push(chunk)
|
||||||
|
});
|
||||||
|
res.on("end", function() {
|
||||||
|
callback(null,data.join(""));
|
||||||
|
console.error("Retrieved " + task.url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
request.addListener("error", function(err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
request.end();
|
||||||
|
},4);
|
||||||
|
|
||||||
// Retrieve a file given a filepath specifier and a context path. If the filepath isn't an absolute
|
// Retrieve a file given a filepath specifier and a context path. If the filepath isn't an absolute
|
||||||
// filepath or an absolute URL, then it is interpreted relative to the context path, which can also be
|
// filepath or an absolute URL, then it is interpreted relative to the context path, which can also be
|
||||||
// a filepath or a URL. It returns the final path used to reach the file. On completion, the callback
|
// a filepath or a URL. It returns the final path used to reach the file. On completion, the callback
|
||||||
// function is called as callback(err,data)
|
// function is called as callback(err,data)
|
||||||
FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
|
FileRetriever.retrieveFile = function(filepath,contextPath,callback) {
|
||||||
var newpath = path.resolve(path.dirname(contextPath),filepath);
|
var httpRegExp = /^(https?:\/\/)/gi,
|
||||||
fileRequestQueue.push({filepath: newpath},callback);
|
newpath,
|
||||||
return newpath;
|
filepathIsHttp = httpRegExp.test(filepath),
|
||||||
|
contextPathIsHttp = httpRegExp.test(contextPath);
|
||||||
|
if(contextPathIsHttp || filepathIsHttp) {
|
||||||
|
// If we've got a full HTTP URI then we're good to go
|
||||||
|
newpath = url.resolve(contextPath,filepath);
|
||||||
|
httpRequestQueue.push({url: newpath},callback);
|
||||||
|
return newpath;
|
||||||
|
} else {
|
||||||
|
// It's a file requested in a file context
|
||||||
|
newpath = path.resolve(path.dirname(contextPath),filepath);
|
||||||
|
fileRequestQueue.push({filepath: newpath},callback);
|
||||||
|
return newpath;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ Recipe.prototype.readIngredient = function(filepath,contextPath,callback) {
|
|||||||
var metafile = filepath + ".meta";
|
var metafile = filepath + ".meta";
|
||||||
me.incFetchCount();
|
me.incFetchCount();
|
||||||
retrieveFile(metafile,contextPath,function(err,data) {
|
retrieveFile(metafile,contextPath,function(err,data) {
|
||||||
if(err && err.code !== 'ENOENT') {
|
if(err && err.code !== "ENOENT" && err.code !== "404") {
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
if(!err) {
|
if(!err) {
|
||||||
|
2
test/data/recipes/newtiddlerswithgithub.recipe
Normal file
2
test/data/recipes/newtiddlerswithgithub.recipe
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
recipe: https://raw.github.com/TiddlyWiki/tiddlywiki/master/tiddlywikinonoscript.html.recipe
|
||||||
|
recipe: ../tiddlywiki.com/index.html.recipe
|
Loading…
Reference in New Issue
Block a user