Selaa lähdekoodia

Improved transforms

Transforms are now only cached until the source file changes,
and the extension argument now expects a period (i.e ".html")
to be consistent with the path.extname function.
master
mortie 6 vuotta sitten
vanhempi
commit
827f3708eb
4 muutettua tiedostoa jossa 25 lisäystä ja 14 poistoa
  1. 3
    2
      README.md
  2. 2
    0
      example.js
  3. 19
    11
      js/static.js
  4. 1
    1
      package.json

+ 3
- 2
README.md Näytä tiedosto

@@ -140,7 +140,7 @@ Example (assumes you have the node-sass module installed):
```
var sass = require("node-sass");

app.transform("sass", "text/css", (path, writeStream) => {
app.transform(".sass", "text/css", (path, writeStream) => {
try {
var str = fs.readFileSync(path, "utf8");
} catch (err) {
@@ -160,4 +160,5 @@ app.transform("sass", "text/css", (path, writeStream) => {
Now, any .sass file served with `webframe.static` will be compiled into CSS,
and sent with the browser with the MIME type "text/css". The result is also
cached, so the expensive sass compilation will only happen the first time the
file is requested. The result will not be cached if you call writeStream.error.
file is requested, or when the file changes. The result will not be cached
if you call writeStream.error.

+ 2
- 0
example.js Näytä tiedosto

@@ -6,11 +6,13 @@ var app = new webframe.App();

// Endpoint which uses the "params" middleware to parse URL parameters
app.get("/params-example", [ mw.params ], (req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello, "+req.params.name);
});

// Endpoint which uses the "payload" middleware to get the payload as a string
app.post("/payload-example", [ mw.payload ], (req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end(req.payload);
});


+ 19
- 11
js/static.js Näytä tiedosto

@@ -63,8 +63,8 @@ function sendfile(path, app, pathname, req, res) {
}

var transformCache = {};
function handleTransform(path, req, res, app) {
var ext = pathlib.extname(req.url).substring(1);
function handleTransform(stat, path, req, res, app) {
var ext = pathlib.extname(path).substring(1);
if (ext === "")
return false;

@@ -74,9 +74,14 @@ function handleTransform(path, req, res, app) {

if (transformCache[path]) {
var c = transformCache[path];
res.writeHead(c.status, c.headers);
res.end(c.content);
return true;
if (stat.ctime === c.ctime) {
res.writeHead(c.data.status, c.data.headers);
res.end(c.data.content);
return true;
} else {
console.log("throwing out because ctime.");
delete transformCache[path];
}
}

var data = { content: "", headers: null, status: null };
@@ -107,7 +112,10 @@ function handleTransform(path, req, res, app) {
if (!nocache) {
data.status = writeStream.status;
data.headers = writeStream.headers;
transformCache[path] = data;
transformCache[path] = {
data: data,
ctime: stat.ctime
};
}
},

@@ -132,10 +140,6 @@ module.exports = function(root, before) {
// Join the web root with the request's path name
var path = pathlib.join(root, pn.replace(before, ""));

// If there's a transform, let that handle it
if (handleTransform(path, req, res, app))
return;

// Send a file
function send(path) {
sendfile(path, app, pn, req, res);
@@ -172,7 +176,11 @@ module.exports = function(root, before) {
path = pathlib.join(path, "index.html");
}

// Send the file
// If there's a transform, let that handle it
if (handleTransform(stat, path, req, res, app))
return;

// Otherwise, send the file
send(path);
});
}

+ 1
- 1
package.json Näytä tiedosto

@@ -1,6 +1,6 @@
{
"name": "webframe",
"version": "0.7.0",
"version": "0.8.0",
"description": "Web framework.",
"main": "index.js",
"scripts": {

Loading…
Peruuta
Tallenna