Comparing version 0.1.5 to 0.2.0
41
index.js
const fs = require('fs'); | ||
const { join } = require('path'); | ||
const { join, resolve } = require('path'); | ||
const tglob = require('tiny-glob/sync'); | ||
@@ -10,10 +10,8 @@ const parseurl = require('parseurl'); | ||
function find(uri, extns) { | ||
if (!!~uri.lastIndexOf('.')) return FILES[uri]; | ||
let i, j, x, arr=[], data, len=uri.length-1; | ||
function toAssume(uri, extns) { | ||
let i=0, x, len=uri.length - 1; | ||
if (uri.charCodeAt(len) === 47) uri=uri.substring(0, len); | ||
let tmp = `${uri}/index`; | ||
for (i=0; i < extns.length; i++) { | ||
let arr=[], tmp=`${uri}/index`; | ||
for (; i < extns.length; i++) { | ||
x = '.' + extns[i]; | ||
@@ -24,6 +22,11 @@ if (uri) arr.push(uri + x); | ||
for (i=0; i < arr.length; i++) { | ||
return arr; | ||
} | ||
function find(uri, extns) { | ||
if (!!~uri.lastIndexOf('.')) return FILES[uri]; | ||
let i=0, data, arr=toAssume(uri, extns); | ||
for (; i < arr.length; i++) { | ||
if (data=FILES[arr[i]]) break; | ||
} | ||
return data; | ||
@@ -41,2 +44,18 @@ } | ||
module.exports = function (dir, opts={}) { | ||
dir = resolve(dir || '.'); | ||
let notFound = opts.onNoMatch || is404; | ||
let setHeaders = opts.setHeaders || noop; | ||
let extensions = opts.extensions || ['html', 'htm']; | ||
if (opts.dev) { | ||
return function (req, res, next) { | ||
let uri = req.path || req.pathname || parseurl(req).pathname; | ||
let arr = uri.includes('.') ? [uri] : toAssume(uri, extensions); | ||
let file = arr.map(x => join(dir, x)).find(fs.existsSync); | ||
if (!file) return next ? next() : notFound(res); | ||
fs.createReadStream(file).pipe(res); | ||
} | ||
} | ||
let cc = opts.maxAge && `public,max-age=${opts.maxAge}`; | ||
@@ -61,6 +80,2 @@ cc && opts.immutable && (cc += ',immutable'); | ||
let notFound = opts.onNoMatch || is404; | ||
let setHeaders = opts.setHeaders || noop; | ||
let extensions = opts.extensions || ['html', 'htm']; | ||
return function (req, res, next) { | ||
@@ -67,0 +82,0 @@ let pathname = req.path || req.pathname || parseurl(req).pathname; |
{ | ||
"name": "sirv", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "The optimized & lightweight middleware for serving requests to static assets", | ||
@@ -5,0 +5,0 @@ "repository": "lukeed/sirv", |
@@ -59,2 +59,12 @@ # sirv [![Build Status](https://travis-ci.org/lukeed/sirv.svg?branch=master)](https://travis-ci.org/lukeed/sirv) | ||
#### opts.dev | ||
Type: `Boolean`<br> | ||
Default: `false` | ||
Enable "dev" mode, which disables/skips caching. Instead, `sirv` will traverse the file system ***on every request***. | ||
Additionally, `dev` mode will ignore `maxAge`, `immutable`, `etag`, and `setHeaders` as these options are geared towards production response headers. | ||
> **Important:** Do not use `dev` mode in production! | ||
#### opts.etag | ||
@@ -61,0 +71,0 @@ Type: `Boolean`<br> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7791
72
123