-
Notifications
You must be signed in to change notification settings - Fork 46
Added conditional get support for client script middleware #19
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
"use strict"; | ||
|
||
var fs = require("fs"); | ||
var path = require("path"); | ||
var etag = require("etag") | ||
var fresh = require("fresh") | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
|
||
var script = path.resolve(__dirname + "/dist/index.min.js"); | ||
var minifiedScript = path.resolve(__dirname + "/dist/index.min.js"); | ||
var unminifiedScript = path.resolve(__dirname + "/dist/index.js"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For Windows support: var unminifiedScript = path.join(__dirname, "dist/index.js"); |
||
|
||
function init(options, connector, type) { | ||
|
||
var result; | ||
|
||
if (options && options.minify) { | ||
result = fs.readFileSync(script); | ||
} else { | ||
script = path.resolve(__dirname + "/dist/index.js"); | ||
result = fs.readFileSync(script); | ||
} | ||
function headers(res, body){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function headers (res, body) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
res.setHeader("Cache-Control", "public, max-age=0"); | ||
res.setHeader("Content-Type", "text/javascript"); | ||
res.setHeader("ETag", etag(body)); | ||
}; | ||
|
||
function body(options, connector) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function body (options, connector) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
var script = minifiedScript; | ||
if (options && !options.minify) { | ||
script = unminifiedScript; | ||
} | ||
|
||
return connector + fs.readFileSync(script); | ||
} | ||
|
||
function isConditionalGet(req) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function isConditionalGet (req) { |
||
return req.headers["if-none-match"] || req.headers["if-modified-since"]; | ||
} | ||
|
||
function notModified(res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. function notModified (res) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
res.removeHeader("Content-Type"); | ||
res.statusCode = 304; | ||
res.end(); | ||
} | ||
|
||
function init(options, connector, type) { | ||
var requestBody = body(options, connector); | ||
if (type && type === "file") { | ||
return connector + result; | ||
return requestBody; | ||
} | ||
|
||
return function (req, res) { | ||
res.setHeader("Content-Type", "text/javascript"); | ||
res.end(connector + result); | ||
headers(res, requestBody); | ||
|
||
if (isConditionalGet(req) && fresh(req.headers, res._headers)) { | ||
return notModified(res); | ||
} | ||
|
||
res.end(requestBody); | ||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Windows support: