Skip to content

Commit

Permalink
Removing 304 response logic from 'stream()'
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Aug 5, 2024
1 parent 4d9430e commit 0de7ba8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 57 deletions.
29 changes: 11 additions & 18 deletions dist/woodland.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ const FUNCTION = "function";
const GET = "GET";
const HEAD = "HEAD";
const HYPHEN = "-";
const IF_NONE_MATCH = "if-none-match";
const IF_MODIFIED_SINCE = "if-modified-since";
const INDEX_HTM = "index.htm";
const INDEX_HTML = "index.html";
const INFO = "info";
Expand Down Expand Up @@ -316,26 +314,21 @@ function stream (req, res, file = {
}

if (req.method === GET) {
if (file.etag.length > INT_0 && req.headers[IF_NONE_MATCH] === file.etag || req.headers[IF_NONE_MATCH] === void 0 && Date.parse(req.headers[IF_MODIFIED_SINCE]) >= file.stats.mtime) {
res.removeHeader(CONTENT_LENGTH);
res.send(EMPTY, INT_304);
} else {
let status = INT_200;
let options, headers;
let status = INT_200;
let options, headers;

if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset
if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset

if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}
if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}

res.send(node_fs.createReadStream(file.path, options), status);
}

res.send(node_fs.createReadStream(file.path, options), status);
} else if (req.method === HEAD) {
res.send(EMPTY);
} else if (req.method === OPTIONS) {
Expand Down
29 changes: 11 additions & 18 deletions dist/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ const FUNCTION = "function";
const GET = "GET";
const HEAD = "HEAD";
const HYPHEN = "-";
const IF_NONE_MATCH = "if-none-match";
const IF_MODIFIED_SINCE = "if-modified-since";
const INDEX_HTM = "index.htm";
const INDEX_HTML = "index.html";
const INFO = "info";
Expand Down Expand Up @@ -298,26 +296,21 @@ function stream (req, res, file = {
}

if (req.method === GET) {
if (file.etag.length > INT_0 && req.headers[IF_NONE_MATCH] === file.etag || req.headers[IF_NONE_MATCH] === void 0 && Date.parse(req.headers[IF_MODIFIED_SINCE]) >= file.stats.mtime) {
res.removeHeader(CONTENT_LENGTH);
res.send(EMPTY, INT_304);
} else {
let status = INT_200;
let options, headers;
let status = INT_200;
let options, headers;

if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset
if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset

if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}
if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}

res.send(createReadStream(file.path, options), status);
}

res.send(createReadStream(file.path, options), status);
} else if (req.method === HEAD) {
res.send(EMPTY);
} else if (req.method === OPTIONS) {
Expand Down
2 changes: 0 additions & 2 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export const FUNCTION = "function";
export const GET = "GET";
export const HEAD = "HEAD";
export const HYPHEN = "-";
export const IF_NONE_MATCH = "if-none-match";
export const IF_MODIFIED_SINCE = "if-modified-since";
export const INDEX_HTM = "index.htm";
export const INDEX_HTML = "index.html";
export const INFO = "info";
Expand Down
30 changes: 11 additions & 19 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ import {
GET,
HEAD,
HYPHEN,
IF_MODIFIED_SINCE,
IF_NONE_MATCH,
INT_0,
INT_10,
INT_1e6,
INT_2,
INT_200,
INT_206,
INT_3,
INT_304,
INT_404,
INT_405,
INT_500,
Expand Down Expand Up @@ -203,26 +200,21 @@ export function stream (req, res, file = {
}

if (req.method === GET) {
if (file.etag.length > INT_0 && req.headers[IF_NONE_MATCH] === file.etag || req.headers[IF_NONE_MATCH] === void 0 && Date.parse(req.headers[IF_MODIFIED_SINCE]) >= file.stats.mtime) {
res.removeHeader(CONTENT_LENGTH);
res.send(EMPTY, INT_304);
} else {
let status = INT_200;
let options, headers;
let status = INT_200;
let options, headers;

if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size, status);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset
if (RANGE in req.headers) {
[headers, options] = partialHeaders(req, res, file.stats.size, status);
res.removeHeader(CONTENT_LENGTH);
res.header(CONTENT_RANGE, headers[CONTENT_RANGE]);
options.end--; // last byte offset

if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}
if (CONTENT_LENGTH in headers) {
res.header(CONTENT_LENGTH, headers[CONTENT_LENGTH]);
}

res.send(createReadStream(file.path, options), status);
}

res.send(createReadStream(file.path, options), status);
} else if (req.method === HEAD) {
res.send(EMPTY);
} else if (req.method === OPTIONS) {
Expand Down

0 comments on commit 0de7ba8

Please sign in to comment.