connect-static-file
Advanced tools
Comparing version 1.2.1 to 2.0.0
'use strict'; | ||
const send = require('send'); | ||
const accepts = require('accepts'); | ||
const mime = require('mime'); | ||
var send = require('send'); | ||
var accepts = require('accepts'); | ||
var mime = require('mime'); | ||
const connectStaticFile = (path, options = {}) => { | ||
const sendOptions = { | ||
acceptRanges: options.acceptRanges, | ||
cacheControl: options.cacheControl, | ||
dotfiles: 'allow', | ||
etag: options.etag, | ||
extensions: options.extensions, | ||
immutable: options.immutable, | ||
index: false, | ||
lastModified: options.lastModified, | ||
maxAge: options.maxAge, | ||
root: path, | ||
}; | ||
module.exports = function connectStaticFile(path, options) { | ||
options = options || {}; | ||
const {encoded} = options; | ||
const headers = []; | ||
var sendOptions = { | ||
dotfiles : 'allow', | ||
etag : options.etag, | ||
extensions : options.extensions, | ||
index : false, | ||
lastModified: options.lastModified, | ||
maxAge : options.maxAge, | ||
root : path | ||
}; | ||
if (options.headers) { | ||
Object.keys(options.headers).forEach((name) => { | ||
headers.push({ | ||
name: name, | ||
value: options.headers[name], | ||
}); | ||
}); | ||
} | ||
var encoded = options.encoded; | ||
var headers = []; | ||
const connectStaticFileMiddleware = (request, response, next) => { | ||
const onError = (err) => { | ||
if (err.code === 'ENOENT') { | ||
// file not found, go to the next middleware without error | ||
next(); | ||
if (options.headers) { | ||
Object.keys(options.headers).forEach(function(name) { | ||
headers.push({ | ||
name : name, | ||
value: options.headers[name] | ||
}); | ||
}); | ||
} | ||
return; | ||
} | ||
function connectStaticFileMiddleware(request, response, next) { | ||
function onError(err) { | ||
if (err.code === 'ENOENT') { | ||
// file not found, go to the next middleware without error | ||
next(); | ||
next(err); | ||
}; | ||
return; | ||
} | ||
const onDirectory = () => { | ||
next(); | ||
}; | ||
next(err); | ||
} | ||
const onHeaders = (response, path, stat) => { | ||
for (let i = 0; i < headers.length; i++) { | ||
response.setHeader(headers[i].name, headers[i].value); | ||
} | ||
function onDirectory() { | ||
next(); | ||
} | ||
if (encoded) { | ||
response.setHeader('Content-Encoding', encoded); | ||
} | ||
function onHeaders(response, path, stat) { | ||
for (var i = 0; i < headers.length; i++) { | ||
response.setHeader(headers[i].name, headers[i].value); | ||
} | ||
if (!response.getHeader('Content-Type') && encoded) { | ||
// foo.css.gz -> foo.css | ||
let encodedPath = path; | ||
encodedPath = encodedPath.replace(/\.(?:gz|gzip|zlib|bz2|xz)$/i, ''); | ||
if (encoded) { | ||
response.setHeader('Content-Encoding', encoded); | ||
} | ||
const type = mime.lookup(encodedPath); | ||
const charset = mime.charsets.lookup(type); | ||
response.setHeader('Content-Type', type + (charset ? '; charset=' + charset : '')); | ||
} | ||
}; | ||
if (!response.getHeader('Content-Type') && encoded) { | ||
// foo.css.gz -> foo.css | ||
var encodedPath = path; | ||
encodedPath = encodedPath.replace(/\.(?:gz|gzip|zlib|bz2|xz)$/i, ''); | ||
if (encoded) { | ||
const accept = accepts(request); | ||
const method = accept.encodings([encoded]); | ||
var type = mime.lookup(encodedPath); | ||
var charset = mime.charsets.lookup(type); | ||
if (method !== encoded) { | ||
next(); | ||
return; | ||
} | ||
} | ||
response.setHeader('Content-Type', type + (charset ? '; charset=' + charset : '')); | ||
} | ||
} | ||
send(request, '', sendOptions) | ||
.on('error', onError) | ||
.on('directory', onDirectory) | ||
.on('headers', onHeaders) | ||
.pipe(response); | ||
}; | ||
if (encoded) { | ||
var accept = accepts(request); | ||
return connectStaticFileMiddleware; | ||
}; | ||
var method = accept.encodings([encoded]); | ||
if (method !== encoded) { | ||
next(); | ||
return; | ||
} | ||
} | ||
send(request, '', sendOptions) | ||
.on('error', onError) | ||
.on('directory', onDirectory) | ||
.on('headers', onHeaders) | ||
.pipe(response); | ||
} | ||
return connectStaticFileMiddleware; | ||
}; | ||
module.exports = connectStaticFile; |
{ | ||
"name": "connect-static-file", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "connect and express middleware to serve a single static file", | ||
"main": "lib/connect-static-file.js", | ||
"scripts": { | ||
"pretest": "jshint lib test && jscs lib test", | ||
"test": "istanbul cover node_modules/mocha/bin/_mocha test" | ||
"lint": "eslint lib test && echo Lint free!", | ||
"test": "npm run lint && istanbul cover node_modules/mocha/bin/_mocha test" | ||
}, | ||
@@ -39,88 +39,12 @@ "repository": { | ||
"devDependencies": { | ||
"babel-eslint": "^8.0.1", | ||
"chai": "^3.4.1", | ||
"chai-http": "^1.0.0", | ||
"eslint": "^4.8.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"express": "^4.15.3", | ||
"istanbul": "^0.4.0", | ||
"jscs": "^2.6.0", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.3.4", | ||
"mocha-sugar-free": ">= 1.1.0 < 2" | ||
}, | ||
"jshintConfig": { | ||
"node": true, | ||
"bitwise": true, | ||
"camelcase": true, | ||
"curly": true, | ||
"eqeqeq": true, | ||
"forin": true, | ||
"freeze": true, | ||
"immed": true, | ||
"indent": 8, | ||
"latedef": true, | ||
"newcap": true, | ||
"noarg": true, | ||
"nonbsp": true, | ||
"nonew": true, | ||
"quotmark": "single", | ||
"undef": true, | ||
"unused": "vars", | ||
"strict": true, | ||
"laxbreak": true | ||
}, | ||
"jscsConfig": { | ||
"esnext": true, | ||
"requireSemicolons": true, | ||
"validateLineBreaks": "LF", | ||
"validateIndentation": 8, | ||
"validateQuoteMarks": "'", | ||
"maximumLineLength": { | ||
"value": 120 | ||
}, | ||
"validateParameterSeparator": ", ", | ||
"disallowMultipleVarDecl": true, | ||
"disallowTrailingComma": true, | ||
"disallowTrailingWhitespace": true, | ||
"disallowYodaConditions": true, | ||
"disallowSpacesInFunctionDeclaration": { | ||
"beforeOpeningRoundBrace": true | ||
}, | ||
"disallowSpacesInFunctionExpression": { | ||
"beforeOpeningRoundBrace": true | ||
}, | ||
"disallowNewlineBeforeBlockStatements": true, | ||
"requireAlignedObjectValues": "all", | ||
"requireBlocksOnNewline": true, | ||
"requireCamelCaseOrUpperCaseIdentifiers": true, | ||
"requireCapitalizedConstructors": true, | ||
"requireCommaBeforeLineBreak": true, | ||
"requireCurlyBraces": [ | ||
"if", | ||
"else", | ||
"for", | ||
"while", | ||
"do", | ||
"try", | ||
"catch" | ||
], | ||
"requireDotNotation": "except_snake_case", | ||
"requireKeywordsOnNewLine": [ | ||
"else", | ||
"catch" | ||
], | ||
"requireLineBreakAfterVariableAssignment": true, | ||
"requireLineFeedAtFileEnd": true, | ||
"requirePaddingNewLinesAfterUseStrict": true, | ||
"requirePaddingNewLinesBeforeExport": true, | ||
"requirePaddingNewlinesBeforeKeywords": [ | ||
"do", | ||
"for", | ||
"if", | ||
"switch", | ||
"case", | ||
"try", | ||
"while", | ||
"return", | ||
"function" | ||
] | ||
} | ||
} |
72
7315
9