Skip to content

Commit

Permalink
Setting headers instead of passing them
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 26, 2019
1 parent 25ea1a2 commit 5682a39
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
34 changes: 17 additions & 17 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,26 @@ function rate (req, res, next) {
}

function stream (req, res) {
const headers = {
"content-length": req.file.stats.size,
"content-type": mime.lookup(req.file.path),
"last-modified": req.file.stats.mtime.toUTCString()
};
res.header("content-length", req.file.stats.size);
res.header("content-type", mime.lookup(req.file.path));
res.header("last-modified", req.file.stats.mtime.toUTCString());

let status = 200,
letag, options;

if (req.server.canETag(req.parsed.pathname, req.method)) {
letag = headers.etag = req.server.etag(req.parsed.pathname, req.file.stats.size, req.file.stats.mtime);
letag = req.server.etag(req.parsed.pathname, req.file.stats.size, req.file.stats.mtime);

if (letag !== void 0) {
res.header("etag", letag);
}
}

if (req.method === "GET") {
if (letag !== void 0 && req.headers["if-none-match"] === letag) {
delete headers["content-length"];
res.send("", 304, headers);
} else if (req.headers["if-none-match"] === void 0 && Date.parse(req.headers["if-modified-since"]) >= req.file.stats.mtime) {
delete headers["content-length"];
res.send("", 304, headers);
if ((letag !== void 0 && req.headers["if-none-match"] === letag) || (req.headers["if-none-match"] === void 0 && Date.parse(req.headers["if-modified-since"]) >= req.file.stats.mtime)) { // eslint-disable-line no-extra-parens
res.removeHeader("content-type");
res.removeHeader("content-length");
res.send("", 304);
} else if (req.server.router.http2 === false) {
options = {};

Expand All @@ -205,18 +205,18 @@ function stream (req, res) {
}

status = 206;
headers["content-range"] = `bytes ${options.start}-${options.end}/${req.file.stats.size}`;
headers["content-length"] = options.end - options.start + 1;
res.header("content-range", `bytes ${options.start}-${options.end}/${req.file.stats.size}`);
res.header("content-length", options.end - options.start + 1);
}

res.send(fs.createReadStream(req.file.path, options), status, headers);
res.send(fs.createReadStream(req.file.path, options), status);
} else if (req.headers.range !== void 0) {
res.error(416); // Partial responses are not supported at this time for HTTP2
} else {
res.send(fs.createReadStream(req.file.path), status, headers);
res.send(fs.createReadStream(req.file.path), status);
}
} else {
res.send("", 200, headers);
res.send("");
}

return void 0;
Expand Down
10 changes: 7 additions & 3 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Tenso extends Base {
mimetype: "application/json"
},
headers: {
"content-type": "text/plain",
"content-type": "application/json",
"vary": "accept, accept-encoding, accept-language, access-control-allow-origin"
},
host: "0.0.0.0",
Expand Down Expand Up @@ -249,9 +249,13 @@ class Tenso extends Base {
res.send = (body = "", status = 200, headers = {}) => {
let lbody = body;

for (const [key, value] of Object.entries(headers)) {
res.header(key, value);
}

if (lbody === null || typeof lbody.on !== "function") {
lbody = serialize(req, lbody, status, headers);
lbody = hypermedia(req.server, req, lbody, headers);
lbody = serialize(req, res, lbody, status);
lbody = hypermedia(req, res, lbody);
lbody = this.final(req, res, lbody);
lbody = this.render(req, res, lbody, headers);
}
Expand Down
10 changes: 6 additions & 4 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,10 @@ function scheme (arg = "") {
return arg.includes("://") || arg[0] === "/";
}

function hypermedia (server, req, rep, headers) {
const collection = req.parsed.pathname,
function hypermedia (req, res, rep) {
const server = req.server,
headers = res.getHeaders(),
collection = req.parsed.pathname,
links = [],
seen = new Set(),
exists = rep !== null;
Expand Down Expand Up @@ -659,9 +661,9 @@ function sort (arg, req) {
return output;
}

function serialize (req, arg, status = 200, headers = {}) {
function serialize (req, res, arg, status = 200) {
let format = req.server.config.mimeType,
accepts = explode(headers["content-type"] || req.parsed.searchParams.get("format") || req.headers.accept || format, ","),
accepts = explode(req.parsed.searchParams.get("format") || req.headers.accept || res.getHeader("content-type") || format, ","),
errz = arg instanceof Error,
result, serializer;

Expand Down

0 comments on commit 5682a39

Please sign in to comment.