소스 검색

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 년 전
부모
커밋
827f3708eb
4개의 변경된 파일25개의 추가작업 그리고 14개의 파일을 삭제
  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 파일 보기

@@ -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 파일 보기

@@ -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 파일 보기

@@ -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 파일 보기

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

Loading…
취소
저장