Compare commits
129 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc077cbe29 | ||
|
|
9ecc9a68f9 | ||
|
|
2b7755a28b | ||
|
|
506626c751 | ||
|
|
2349675b98 | ||
|
|
c5a317fb45 | ||
|
|
9284264604 | ||
|
|
c2585e2c16 | ||
|
|
e7d4062773 | ||
|
|
3e32e4ac3d | ||
|
|
69137b760f | ||
|
|
a8205c834a | ||
|
|
d09bb5e774 | ||
|
|
6024e991ca | ||
|
|
f7e0bb551d | ||
|
|
7215f76cca | ||
|
|
c6b63e4666 | ||
|
|
ec31019c9e | ||
|
|
efe0c6fa7d | ||
|
|
d2bebdbd87 | ||
|
|
3048085dc4 | ||
|
|
1c8c8b9a59 | ||
|
|
2bbaaba5cc | ||
|
|
84285a0826 | ||
|
|
11c2b39640 | ||
|
|
98ecd80f57 | ||
|
|
53a7b06bc6 | ||
|
|
4ed20255be | ||
|
|
8201055cba | ||
|
|
af6bec9cc3 | ||
|
|
2932faa568 | ||
|
|
dc54507b68 | ||
|
|
72b08d6395 | ||
|
|
1284ad0a9f | ||
|
|
a275e385c4 | ||
|
|
357452911d | ||
|
|
fb1c509620 | ||
|
|
2e1836e614 | ||
|
|
ed181e36ed | ||
|
|
f99abfe86a | ||
|
|
e9ccd54a5d | ||
|
|
972e4d1ced | ||
|
|
28e28c8352 | ||
|
|
cd7ab38593 | ||
|
|
a8c6d17b08 | ||
|
|
6cb3ab01a0 | ||
|
|
11b27ed4d3 | ||
|
|
5afc181207 | ||
|
|
8195aa5d6a | ||
|
|
fc9db6b56a | ||
|
|
ac1148a50f | ||
|
|
edc865cb7d | ||
|
|
22e7f31395 | ||
|
|
dd48453baa | ||
|
|
26cc6d91c0 | ||
|
|
735de3e4c0 | ||
|
|
3ce952cfd5 | ||
|
|
7820063014 | ||
|
|
5101c6373c | ||
|
|
b326b6dfa4 | ||
|
|
38a1ab69fa | ||
|
|
191f4b10a3 | ||
|
|
a93f8002a9 | ||
|
|
d3025b9744 | ||
|
|
ac6ea7515a | ||
|
|
b95016be37 | ||
|
|
f39ad778ed | ||
|
|
73413c4caa | ||
|
|
6711bfa431 | ||
|
|
f17363a45d | ||
|
|
db2163892e | ||
|
|
5c74d1e44a | ||
|
|
bd6f5553a5 | ||
|
|
63b45e6208 | ||
|
|
d680e6302d | ||
|
|
420d159be7 | ||
|
|
6d3160a32c | ||
|
|
4056d3375c | ||
|
|
7566e487b0 | ||
|
|
3ae1ea56d3 | ||
|
|
d60c7a0055 | ||
|
|
6781d9536b | ||
|
|
ea919ec568 | ||
|
|
07611f42ad | ||
|
|
e4355f0e97 | ||
|
|
5275b835a7 | ||
|
|
636a8ae7bc | ||
|
|
320226f35a | ||
|
|
2003c3f743 | ||
|
|
e385089155 | ||
|
|
5462db3946 | ||
|
|
cb60444cb1 | ||
|
|
6691f1b75e | ||
|
|
5cb8bcc448 | ||
|
|
e11f253226 | ||
|
|
7381a1d145 | ||
|
|
6a4a87edcd | ||
|
|
99e8ec512a | ||
|
|
197d9f6a0f | ||
|
|
41fe20602a | ||
|
|
b5c886f36d | ||
|
|
6da896f6d8 | ||
|
|
5ec9698c08 | ||
|
|
4da905e4d3 | ||
|
|
62ac6f345b | ||
|
|
9afe1a7816 | ||
|
|
0368c20a58 | ||
|
|
66a13d0814 | ||
|
|
b8d8f4524e | ||
|
|
75efc2d68e | ||
|
|
0bdab07acf | ||
|
|
a6f02f060d | ||
|
|
90252c37b9 | ||
|
|
94fcb7092e | ||
|
|
803e4154b2 | ||
|
|
267913151b | ||
|
|
b1e36ccbf5 | ||
|
|
55e914660b | ||
|
|
0ff43c75a8 | ||
|
|
d15b5f1387 | ||
|
|
d5a4b93449 | ||
|
|
244f8a5541 | ||
|
|
ed11812d3f | ||
|
|
4881fb27da | ||
|
|
07e47b1e78 | ||
|
|
55a5939596 | ||
|
|
12d7cb4a7d | ||
|
|
c602a95d4c | ||
|
|
bf15ad0e12 |
32
.travis.yml
32
.travis.yml
@@ -2,10 +2,28 @@ language: node_js
|
||||
node_js:
|
||||
- "0.8"
|
||||
- "0.10"
|
||||
- "0.11"
|
||||
matrix:
|
||||
allow_failures:
|
||||
- node_js: "0.11"
|
||||
fast_finish: true
|
||||
script: "npm run-script test-travis"
|
||||
after_script: "npm install coveralls@2.10.0 && cat ./coverage/lcov.info | coveralls"
|
||||
- "0.12"
|
||||
- "1.8"
|
||||
- "2.5"
|
||||
- "3.3"
|
||||
- "4.8"
|
||||
- "5.12"
|
||||
- "6.10"
|
||||
- "7.10"
|
||||
sudo: false
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
||||
before_install:
|
||||
# Setup Node.js version-specific dependencies
|
||||
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
|
||||
|
||||
# Update Node.js modules
|
||||
- "test ! -d node_modules || npm prune"
|
||||
- "test ! -d node_modules || npm rebuild"
|
||||
script:
|
||||
# Run test script, depending on istanbul install
|
||||
- "test ! -z $(npm -ps ls istanbul) || npm test"
|
||||
- "test -z $(npm -ps ls istanbul) || npm run-script test-ci"
|
||||
after_script:
|
||||
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
|
||||
|
||||
170
HISTORY.md
170
HISTORY.md
@@ -1,3 +1,173 @@
|
||||
1.9.0 / 2017-05-25
|
||||
==================
|
||||
|
||||
* Set `X-Content-Type-Options: nosniff` header
|
||||
* deps: batch@0.6.1
|
||||
* deps: debug@2.6.8
|
||||
- Allow colors in workers
|
||||
- Deprecated `DEBUG_FD` environment variable set to `3` or higher
|
||||
- Fix `DEBUG_MAX_ARRAY_LENGTH`
|
||||
- Fix error when running under React Native
|
||||
- Use same color for same namespace
|
||||
- deps: ms@2.0.0
|
||||
* deps: http-errors@~1.6.1
|
||||
- Make `message` property enumerable for `HttpError`s
|
||||
- deps: inherits@2.0.3
|
||||
- deps: setprototypeof@1.0.3
|
||||
- deps: statuses@'>= 1.3.1 < 2'
|
||||
* deps: mime-types@~2.1.15
|
||||
- Add new mime types
|
||||
- Add `audio/mp3`
|
||||
|
||||
1.8.0 / 2016-06-17
|
||||
==================
|
||||
|
||||
* Make inline file search case-insensitive
|
||||
* deps: accepts@~1.3.3
|
||||
- deps: mime-types@~2.1.11
|
||||
- deps: negotiator@0.6.1
|
||||
- perf: improve header parsing speed
|
||||
* deps: http-errors@~1.5.0
|
||||
- Use `setprototypeof` module to replace `__proto__` setting
|
||||
- deps: inherits@2.0.1
|
||||
- deps: statuses@'>= 1.3.0 < 2'
|
||||
- perf: enable strict mode
|
||||
* deps: mime-types@~2.1.11
|
||||
- Add new mime types
|
||||
- Update primary extension for `audio/mp4`
|
||||
- deps: mime-db@~1.23.0
|
||||
|
||||
1.7.3 / 2016-01-24
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.13
|
||||
- deps: mime-types@~2.1.6
|
||||
* deps: batch@0.5.3
|
||||
- Fix invalid dependency for browserify
|
||||
* deps: escape-html@~1.0.3
|
||||
- perf: enable strict mode
|
||||
- perf: optimize string replacement
|
||||
- perf: use faster string coercion
|
||||
* deps: mime-types@~2.1.9
|
||||
- Add new mime types
|
||||
* deps: parseurl@~1.3.1
|
||||
- perf: enable strict mode
|
||||
|
||||
1.7.2 / 2015-07-30
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.12
|
||||
- deps: mime-types@~2.1.4
|
||||
* deps: mime-types@~2.1.4
|
||||
- Add new mime types
|
||||
|
||||
1.7.1 / 2015-07-05
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.10
|
||||
- deps: mime-types@~2.1.2
|
||||
* deps: mime-types@~2.1.2
|
||||
- Add new mime types
|
||||
|
||||
1.7.0 / 2015-06-15
|
||||
==================
|
||||
|
||||
* Accept `function` value for `template` option
|
||||
* Send non-chunked response for `OPTIONS`
|
||||
* Stat parent directory when necessary
|
||||
* Use `Date.prototype.toLocaleDateString` to format date
|
||||
* deps: accepts@~1.2.9
|
||||
- deps: mime-types@~2.1.1
|
||||
- deps: negotiator@0.5.3
|
||||
- perf: avoid argument reassignment & argument slice
|
||||
- perf: avoid negotiator recursive construction
|
||||
- perf: enable strict mode
|
||||
- perf: remove unnecessary bitwise operator
|
||||
* deps: escape-html@1.0.2
|
||||
* deps: mime-types@~2.1.1
|
||||
- Add new mime types
|
||||
* perf: enable strict mode
|
||||
* perf: remove argument reassignment
|
||||
|
||||
1.6.4 / 2015-05-12
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.7
|
||||
- deps: mime-types@~2.0.11
|
||||
- deps: negotiator@0.5.3
|
||||
* deps: debug@~2.2.0
|
||||
- deps: ms@0.7.1
|
||||
* deps: mime-types@~2.0.11
|
||||
- Add new mime types
|
||||
|
||||
1.6.3 / 2015-03-13
|
||||
==================
|
||||
|
||||
* Properly escape file names in HTML
|
||||
* deps: accepts@~1.2.5
|
||||
- deps: mime-types@~2.0.10
|
||||
* deps: debug@~2.1.3
|
||||
- Fix high intensity foreground color for bold
|
||||
- deps: ms@0.7.0
|
||||
* deps: escape-html@1.0.1
|
||||
* deps: mime-types@~2.0.10
|
||||
- Add new mime types
|
||||
|
||||
1.6.2 / 2015-02-16
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.4
|
||||
- deps: mime-types@~2.0.9
|
||||
- deps: negotiator@0.5.1
|
||||
* deps: http-errors@~1.3.1
|
||||
- Construct errors using defined constructors from `createError`
|
||||
- Fix error names that are not identifiers
|
||||
- Set a meaningful `name` property on constructed errors
|
||||
* deps: mime-types@~2.0.9
|
||||
- Add new mime types
|
||||
- deps: mime-db@~1.7.0
|
||||
|
||||
1.6.1 / 2015-01-31
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.2.3
|
||||
- deps: mime-types@~2.0.8
|
||||
* deps: mime-types@~2.0.8
|
||||
- Add new mime types
|
||||
- deps: mime-db@~1.6.0
|
||||
|
||||
1.6.0 / 2015-01-01
|
||||
==================
|
||||
|
||||
* Add link to root directory
|
||||
* deps: accepts@~1.2.2
|
||||
- deps: mime-types@~2.0.7
|
||||
- deps: negotiator@0.5.0
|
||||
* deps: batch@0.5.2
|
||||
* deps: debug@~2.1.1
|
||||
* deps: mime-types@~2.0.7
|
||||
- Add new mime types
|
||||
- Fix missing extensions
|
||||
- Fix various invalid MIME type entries
|
||||
- Remove example template MIME types
|
||||
- deps: mime-db@~1.5.0
|
||||
|
||||
1.5.3 / 2014-12-10
|
||||
==================
|
||||
|
||||
* deps: accepts@~1.1.4
|
||||
- deps: mime-types@~2.0.4
|
||||
* deps: http-errors@~1.2.8
|
||||
- Fix stack trace from exported function
|
||||
* deps: mime-types@~2.0.4
|
||||
- Add new mime types
|
||||
- deps: mime-db@~1.3.0
|
||||
|
||||
1.5.2 / 2014-12-03
|
||||
==================
|
||||
|
||||
* Fix icon name background alignment on mobile view
|
||||
|
||||
1.5.1 / 2014-11-22
|
||||
==================
|
||||
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -3,7 +3,7 @@
|
||||
Copyright (c) 2010 Sencha Inc.
|
||||
Copyright (c) 2011 LearnBoost
|
||||
Copyright (c) 2011 TJ Holowaychuk
|
||||
Copyright (c) 2014 Douglas Christopher Wilson
|
||||
Copyright (c) 2014-2015 Douglas Christopher Wilson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
||||
39
README.md
39
README.md
@@ -2,7 +2,8 @@
|
||||
|
||||
[![NPM Version][npm-image]][npm-url]
|
||||
[![NPM Downloads][downloads-image]][downloads-url]
|
||||
[![Build Status][travis-image]][travis-url]
|
||||
[![Linux Build][travis-image]][travis-url]
|
||||
[![Windows Build][appveyor-image]][appveyor-url]
|
||||
[![Test Coverage][coveralls-image]][coveralls-url]
|
||||
[![Gratipay][gratipay-image]][gratipay-url]
|
||||
|
||||
@@ -10,6 +11,10 @@
|
||||
|
||||
## Install
|
||||
|
||||
This is a [Node.js](https://nodejs.org/en/) module available through the
|
||||
[npm registry](https://www.npmjs.com/). Installation is done using the
|
||||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
||||
|
||||
```sh
|
||||
$ npm install serve-index
|
||||
```
|
||||
@@ -55,15 +60,31 @@ Optional path to a CSS stylesheet. Defaults to a built-in stylesheet.
|
||||
|
||||
##### template
|
||||
|
||||
Optional path to an HTML template. Defaults to a built-in template.
|
||||
Optional path to an HTML template or a function that will render a HTML
|
||||
string. Defaults to a built-in template.
|
||||
|
||||
The following tokens are replaced in templates:
|
||||
When given a string, the string is used as a file path to load and then the
|
||||
following tokens are replaced in templates:
|
||||
|
||||
* `{directory}` with the name of the directory.
|
||||
* `{files}` with the HTML of an unordered list of file links.
|
||||
* `{linked-path}` with the HTML of a link to the directory.
|
||||
* `{style}` with the specified stylesheet and embedded images.
|
||||
|
||||
When given as a function, the function is called as `template(locals, callback)`
|
||||
and it needs to invoke `callback(error, htmlString)`. The following are the
|
||||
provided locals:
|
||||
|
||||
* `directory` is the directory being displayed (where `/` is the root).
|
||||
* `displayIcons` is a Boolean for if icons should be rendered or not.
|
||||
* `fileList` is a sorted array of files in the directory. The array contains
|
||||
objects with the following properties:
|
||||
- `name` is the relative name for the file.
|
||||
- `stat` is a `fs.Stats` object for the file.
|
||||
* `path` is the full filesystem path to `directory`.
|
||||
* `style` is the default stylesheet or the contents of the `stylesheet` option.
|
||||
* `viewName` is the view name provided by the `view` option.
|
||||
|
||||
##### view
|
||||
|
||||
Display mode. `tiles` and `details` are available. Defaults to `tiles`.
|
||||
@@ -115,13 +136,15 @@ app.listen()
|
||||
[MIT](LICENSE). The [Silk](http://www.famfamfam.com/lab/icons/silk/) icons
|
||||
are created by/copyright of [FAMFAMFAM](http://www.famfamfam.com/).
|
||||
|
||||
[npm-image]: https://img.shields.io/npm/v/serve-index.svg?style=flat
|
||||
[npm-image]: https://img.shields.io/npm/v/serve-index.svg
|
||||
[npm-url]: https://npmjs.org/package/serve-index
|
||||
[travis-image]: https://img.shields.io/travis/expressjs/serve-index.svg?style=flat
|
||||
[travis-image]: https://img.shields.io/travis/expressjs/serve-index/master.svg?label=linux
|
||||
[travis-url]: https://travis-ci.org/expressjs/serve-index
|
||||
[coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-index.svg?style=flat
|
||||
[appveyor-image]: https://img.shields.io/appveyor/ci/dougwilson/serve-index/master.svg?label=windows
|
||||
[appveyor-url]: https://ci.appveyor.com/project/dougwilson/serve-index
|
||||
[coveralls-image]: https://img.shields.io/coveralls/expressjs/serve-index/master.svg
|
||||
[coveralls-url]: https://coveralls.io/r/expressjs/serve-index?branch=master
|
||||
[downloads-image]: https://img.shields.io/npm/dm/serve-index.svg?style=flat
|
||||
[downloads-image]: https://img.shields.io/npm/dm/serve-index.svg
|
||||
[downloads-url]: https://npmjs.org/package/serve-index
|
||||
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
|
||||
[gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg
|
||||
[gratipay-url]: https://www.gratipay.com/dougwilson/
|
||||
|
||||
28
appveyor.yml
Normal file
28
appveyor.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
environment:
|
||||
matrix:
|
||||
- nodejs_version: "0.8"
|
||||
- nodejs_version: "0.10"
|
||||
- nodejs_version: "0.12"
|
||||
- nodejs_version: "1.8"
|
||||
- nodejs_version: "2.5"
|
||||
- nodejs_version: "3.3"
|
||||
- nodejs_version: "4.8"
|
||||
- nodejs_version: "5.12"
|
||||
- nodejs_version: "6.10"
|
||||
- nodejs_version: "7.10"
|
||||
cache:
|
||||
- node_modules
|
||||
install:
|
||||
- ps: Install-Product node $env:nodejs_version
|
||||
- if "%nodejs_version%" equ "0.8" npm rm --save-dev istanbul
|
||||
- if exist node_modules npm prune
|
||||
- if exist node_modules npm rebuild
|
||||
- npm install
|
||||
build: off
|
||||
test_script:
|
||||
- node --version
|
||||
- npm --version
|
||||
- set npm_test_command=test
|
||||
- for /f %%l in ('npm -ps ls istanbul') do set npm_test_command=test-ci
|
||||
- npm run %npm_test_command%
|
||||
version: "{build}"
|
||||
319
index.js
319
index.js
@@ -2,20 +2,21 @@
|
||||
* serve-index
|
||||
* Copyright(c) 2011 Sencha Inc.
|
||||
* Copyright(c) 2011 TJ Holowaychuk
|
||||
* Copyright(c) 2014 Douglas Christopher Wilson
|
||||
* Copyright(c) 2014-2015 Douglas Christopher Wilson
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
// TODO: arrow key navigation
|
||||
// TODO: make icons extensible
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
* @private
|
||||
*/
|
||||
|
||||
var accepts = require('accepts');
|
||||
var createError = require('http-errors');
|
||||
var debug = require('debug')('serve-index');
|
||||
var escapeHtml = require('escape-html');
|
||||
var fs = require('fs')
|
||||
, path = require('path')
|
||||
, normalize = path.normalize
|
||||
@@ -27,6 +28,13 @@ var mime = require('mime-types');
|
||||
var parseUrl = require('parseurl');
|
||||
var resolve = require('path').resolve;
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
* @public
|
||||
*/
|
||||
|
||||
module.exports = serveIndex;
|
||||
|
||||
/*!
|
||||
* Icon cache.
|
||||
*/
|
||||
@@ -66,35 +74,35 @@ var mediaType = {
|
||||
*
|
||||
* See Readme.md for documentation of options.
|
||||
*
|
||||
* @param {String} path
|
||||
* @param {String} root
|
||||
* @param {Object} options
|
||||
* @return {Function} middleware
|
||||
* @api public
|
||||
* @public
|
||||
*/
|
||||
|
||||
exports = module.exports = function serveIndex(root, options){
|
||||
options = options || {};
|
||||
function serveIndex(root, options) {
|
||||
var opts = options || {};
|
||||
|
||||
// root required
|
||||
if (!root) throw new TypeError('serveIndex() root path required');
|
||||
if (!root) {
|
||||
throw new TypeError('serveIndex() root path required');
|
||||
}
|
||||
|
||||
// resolve root to absolute and normalize
|
||||
root = resolve(root);
|
||||
root = normalize(root + sep);
|
||||
var rootPath = normalize(resolve(root) + sep);
|
||||
|
||||
var hidden = options.hidden
|
||||
, icons = options.icons
|
||||
, view = options.view || 'tiles'
|
||||
, filter = options.filter
|
||||
, template = options.template || defaultTemplate
|
||||
, stylesheet = options.stylesheet || defaultStylesheet;
|
||||
var filter = opts.filter;
|
||||
var hidden = opts.hidden;
|
||||
var icons = opts.icons;
|
||||
var stylesheet = opts.stylesheet || defaultStylesheet;
|
||||
var template = opts.template || defaultTemplate;
|
||||
var view = opts.view || 'tiles';
|
||||
|
||||
return function serveIndex(req, res, next) {
|
||||
return function (req, res, next) {
|
||||
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
||||
res.statusCode = 'OPTIONS' === req.method
|
||||
? 200
|
||||
: 405;
|
||||
res.statusCode = 'OPTIONS' === req.method ? 200 : 405;
|
||||
res.setHeader('Allow', 'GET, HEAD, OPTIONS');
|
||||
res.setHeader('Content-Length', '0');
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
@@ -106,19 +114,19 @@ exports = module.exports = function serveIndex(root, options){
|
||||
var originalDir = decodeURIComponent(originalUrl.pathname);
|
||||
|
||||
// join / normalize from root dir
|
||||
var path = normalize(join(root, dir));
|
||||
var path = normalize(join(rootPath, dir));
|
||||
|
||||
// null byte(s), bad request
|
||||
if (~path.indexOf('\0')) return next(createError(400));
|
||||
|
||||
// malicious path
|
||||
if ((path + sep).substr(0, root.length) !== root) {
|
||||
if ((path + sep).substr(0, rootPath.length) !== rootPath) {
|
||||
debug('malicious path "%s"', path);
|
||||
return next(createError(403));
|
||||
}
|
||||
|
||||
// determine ".." display
|
||||
var showUp = normalize(resolve(path) + sep) !== root;
|
||||
var showUp = normalize(resolve(path) + sep) !== rootPath;
|
||||
|
||||
// check if we have a directory
|
||||
debug('stat "%s"', path);
|
||||
@@ -148,11 +156,11 @@ exports = module.exports = function serveIndex(root, options){
|
||||
|
||||
// content-negotiation
|
||||
var accept = accepts(req);
|
||||
var type = accept.types(mediaTypes);
|
||||
var type = accept.type(mediaTypes);
|
||||
|
||||
// not acceptable
|
||||
if (!type) return next(createError(406));
|
||||
exports[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet);
|
||||
serveIndex[mediaType[type]](req, res, files, next, originalDir, showUp, icons, path, view, template, stylesheet);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -162,26 +170,45 @@ exports = module.exports = function serveIndex(root, options){
|
||||
* Respond with text/html.
|
||||
*/
|
||||
|
||||
exports.html = function(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet){
|
||||
fs.readFile(template, 'utf8', function(err, str){
|
||||
if (err) return next(err);
|
||||
fs.readFile(stylesheet, 'utf8', function(err, style){
|
||||
if (err) return next(err);
|
||||
stat(path, files, function(err, stats){
|
||||
if (err) return next(err);
|
||||
files = files.map(function(file, i){ return { name: file, stat: stats[i] }; });
|
||||
files.sort(fileSort);
|
||||
if (showUp) files.unshift({ name: '..' });
|
||||
str = str
|
||||
.replace(/\{style\}/g, style.concat(iconStyle(files, icons)))
|
||||
.replace(/\{files\}/g, html(files, dir, icons, view))
|
||||
.replace(/\{directory\}/g, dir)
|
||||
.replace(/\{linked-path\}/g, htmlPath(dir));
|
||||
serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path, view, template, stylesheet) {
|
||||
var render = typeof template !== 'function'
|
||||
? createHtmlRender(template)
|
||||
: template
|
||||
|
||||
var buf = new Buffer(str, 'utf8');
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
res.setHeader('Content-Length', buf.length);
|
||||
res.end(buf);
|
||||
if (showUp) {
|
||||
files.unshift('..');
|
||||
}
|
||||
|
||||
// stat all files
|
||||
stat(path, files, function (err, stats) {
|
||||
if (err) return next(err);
|
||||
|
||||
// combine the stats into the file list
|
||||
var fileList = files.map(function (file, i) {
|
||||
return { name: file, stat: stats[i] };
|
||||
});
|
||||
|
||||
// sort file list
|
||||
fileList.sort(fileSort);
|
||||
|
||||
// read stylesheet
|
||||
fs.readFile(stylesheet, 'utf8', function (err, style) {
|
||||
if (err) return next(err);
|
||||
|
||||
// create locals for rendering
|
||||
var locals = {
|
||||
directory: dir,
|
||||
displayIcons: Boolean(icons),
|
||||
fileList: fileList,
|
||||
path: path,
|
||||
style: style,
|
||||
viewName: view
|
||||
};
|
||||
|
||||
// render html
|
||||
render(locals, function (err, body) {
|
||||
if (err) return next(err);
|
||||
send(res, 'text/html', body)
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -191,33 +218,111 @@ exports.html = function(req, res, files, next, dir, showUp, icons, path, view, t
|
||||
* Respond with application/json.
|
||||
*/
|
||||
|
||||
exports.json = function(req, res, files){
|
||||
var body = JSON.stringify(files);
|
||||
var buf = new Buffer(body, 'utf8');
|
||||
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.setHeader('Content-Length', buf.length);
|
||||
res.end(buf);
|
||||
serveIndex.json = function _json(req, res, files) {
|
||||
send(res, 'application/json', JSON.stringify(files))
|
||||
};
|
||||
|
||||
/**
|
||||
* Respond with text/plain.
|
||||
*/
|
||||
|
||||
exports.plain = function(req, res, files){
|
||||
var body = files.join('\n') + '\n';
|
||||
var buf = new Buffer(body, 'utf8');
|
||||
|
||||
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
|
||||
res.setHeader('Content-Length', buf.length);
|
||||
res.end(buf);
|
||||
serveIndex.plain = function _plain(req, res, files) {
|
||||
send(res, 'text/plain', (files.join('\n') + '\n'))
|
||||
};
|
||||
|
||||
/**
|
||||
* Map html `files`, returning an html unordered list.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function createHtmlFileList(files, dir, useIcons, view) {
|
||||
var html = '<ul id="files" class="view-' + escapeHtml(view) + '">'
|
||||
+ (view == 'details' ? (
|
||||
'<li class="header">'
|
||||
+ '<span class="name">Name</span>'
|
||||
+ '<span class="size">Size</span>'
|
||||
+ '<span class="date">Modified</span>'
|
||||
+ '</li>') : '');
|
||||
|
||||
html += files.map(function (file) {
|
||||
var classes = [];
|
||||
var isDir = file.stat && file.stat.isDirectory();
|
||||
var path = dir.split('/').map(function (c) { return encodeURIComponent(c); });
|
||||
|
||||
if (useIcons) {
|
||||
classes.push('icon');
|
||||
|
||||
if (isDir) {
|
||||
classes.push('icon-directory');
|
||||
} else {
|
||||
var ext = extname(file.name);
|
||||
var icon = iconLookup(file.name);
|
||||
|
||||
classes.push('icon');
|
||||
classes.push('icon-' + ext.substring(1));
|
||||
|
||||
if (classes.indexOf(icon.className) === -1) {
|
||||
classes.push(icon.className);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path.push(encodeURIComponent(file.name));
|
||||
|
||||
var date = file.stat && file.name !== '..'
|
||||
? file.stat.mtime.toLocaleDateString() + ' ' + file.stat.mtime.toLocaleTimeString()
|
||||
: '';
|
||||
var size = file.stat && !isDir
|
||||
? file.stat.size
|
||||
: '';
|
||||
|
||||
return '<li><a href="'
|
||||
+ escapeHtml(normalizeSlashes(normalize(path.join('/'))))
|
||||
+ '" class="' + escapeHtml(classes.join(' ')) + '"'
|
||||
+ ' title="' + escapeHtml(file.name) + '">'
|
||||
+ '<span class="name">' + escapeHtml(file.name) + '</span>'
|
||||
+ '<span class="size">' + escapeHtml(size) + '</span>'
|
||||
+ '<span class="date">' + escapeHtml(date) + '</span>'
|
||||
+ '</a></li>';
|
||||
}).join('\n');
|
||||
|
||||
html += '</ul>';
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create function to render html.
|
||||
*/
|
||||
|
||||
function createHtmlRender(template) {
|
||||
return function render(locals, callback) {
|
||||
// read template
|
||||
fs.readFile(template, 'utf8', function (err, str) {
|
||||
if (err) return callback(err);
|
||||
|
||||
var body = str
|
||||
.replace(/\{style\}/g, locals.style.concat(iconStyle(locals.fileList, locals.displayIcons)))
|
||||
.replace(/\{files\}/g, createHtmlFileList(locals.fileList, locals.directory, locals.displayIcons, locals.viewName))
|
||||
.replace(/\{directory\}/g, escapeHtml(locals.directory))
|
||||
.replace(/\{linked-path\}/g, htmlPath(locals.directory));
|
||||
|
||||
callback(null, body);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort function for with directories first.
|
||||
*/
|
||||
|
||||
function fileSort(a, b) {
|
||||
// sort ".." to the top
|
||||
if (a.name === '..' || b.name === '..') {
|
||||
return a.name === b.name ? 0
|
||||
: a.name === '..' ? -1 : 1;
|
||||
}
|
||||
|
||||
return Number(b.stat && b.stat.isDirectory()) - Number(a.stat && a.stat.isDirectory()) ||
|
||||
String(a.name).toLocaleLowerCase().localeCompare(String(b.name).toLocaleLowerCase());
|
||||
}
|
||||
@@ -227,11 +332,19 @@ function fileSort(a, b) {
|
||||
*/
|
||||
|
||||
function htmlPath(dir) {
|
||||
var curr = [];
|
||||
return dir.split('/').map(function(part){
|
||||
curr.push(encodeURIComponent(part));
|
||||
return part ? '<a href="' + curr.join('/') + '">' + part + '</a>' : '';
|
||||
}).join(' / ');
|
||||
var parts = dir.split('/');
|
||||
var crumb = new Array(parts.length);
|
||||
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var part = parts[i];
|
||||
|
||||
if (part) {
|
||||
parts[i] = encodeURIComponent(part);
|
||||
crumb[i] = '<a href="' + escapeHtml(parts.slice(0, i + 1).join('/')) + '">' + escapeHtml(part) + '</a>';
|
||||
}
|
||||
}
|
||||
|
||||
return crumb.join(' / ');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,11 +409,9 @@ function iconLookup(filename) {
|
||||
* Load icon images, return css string.
|
||||
*/
|
||||
|
||||
function iconStyle (files, useIcons) {
|
||||
function iconStyle(files, useIcons) {
|
||||
if (!useIcons) return '';
|
||||
var className;
|
||||
var i;
|
||||
var iconName;
|
||||
var list = [];
|
||||
var rules = {};
|
||||
var selector;
|
||||
@@ -310,7 +421,7 @@ function iconStyle (files, useIcons) {
|
||||
for (i = 0; i < files.length; i++) {
|
||||
var file = files[i];
|
||||
|
||||
var isDir = '..' == file.name || (file.stat && file.stat.isDirectory());
|
||||
var isDir = file.stat && file.stat.isDirectory();
|
||||
var icon = isDir
|
||||
? { className: 'icon-directory', fileName: icons.folder }
|
||||
: iconLookup(file.name);
|
||||
@@ -337,63 +448,6 @@ function iconStyle (files, useIcons) {
|
||||
return style;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map html `files`, returning an html unordered list.
|
||||
*/
|
||||
|
||||
function html(files, dir, useIcons, view) {
|
||||
return '<ul id="files" class="view-' + view + '">'
|
||||
+ (view == 'details' ? (
|
||||
'<li class="header">'
|
||||
+ '<span class="name">Name</span>'
|
||||
+ '<span class="size">Size</span>'
|
||||
+ '<span class="date">Modified</span>'
|
||||
+ '</li>') : '')
|
||||
+ files.map(function(file){
|
||||
var isDir = '..' == file.name || (file.stat && file.stat.isDirectory())
|
||||
, classes = []
|
||||
, path = dir.split('/').map(function (c) { return encodeURIComponent(c); });
|
||||
|
||||
if (useIcons) {
|
||||
classes.push('icon');
|
||||
|
||||
if (isDir) {
|
||||
classes.push('icon-directory');
|
||||
} else {
|
||||
var ext = extname(file.name);
|
||||
var icon = iconLookup(file.name);
|
||||
|
||||
classes.push('icon');
|
||||
classes.push('icon-' + ext.substring(1));
|
||||
|
||||
if (classes.indexOf(icon.className) === -1) {
|
||||
classes.push(icon.className);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path.push(encodeURIComponent(file.name));
|
||||
|
||||
var date = file.stat && file.name !== '..'
|
||||
? file.stat.mtime.toDateString() + ' ' + file.stat.mtime.toLocaleTimeString()
|
||||
: '';
|
||||
var size = file.stat && !isDir
|
||||
? file.stat.size
|
||||
: '';
|
||||
|
||||
return '<li><a href="'
|
||||
+ normalizeSlashes(normalize(path.join('/')))
|
||||
+ '" class="'
|
||||
+ classes.join(' ') + '"'
|
||||
+ ' title="' + file.name + '">'
|
||||
+ '<span class="name">'+file.name+'</span>'
|
||||
+ '<span class="size">'+size+'</span>'
|
||||
+ '<span class="date">'+date+'</span>'
|
||||
+ '</a></li>';
|
||||
|
||||
}).join('\n') + '</ul>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and cache the given `icon`.
|
||||
*
|
||||
@@ -435,6 +489,23 @@ function removeHidden(files) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a response.
|
||||
* @private
|
||||
*/
|
||||
|
||||
function send (res, type, body) {
|
||||
// security header for content sniffing
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff')
|
||||
|
||||
// standard headers
|
||||
res.setHeader('Content-Type', type + '; charset=utf-8')
|
||||
res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'))
|
||||
|
||||
// body
|
||||
res.end(body, 'utf8')
|
||||
}
|
||||
|
||||
/**
|
||||
* Stat all files and return array of stat
|
||||
* in same order.
|
||||
|
||||
27
package.json
27
package.json
@@ -1,23 +1,24 @@
|
||||
{
|
||||
"name": "serve-index",
|
||||
"description": "Serve directory listings",
|
||||
"version": "1.5.1",
|
||||
"version": "1.9.0",
|
||||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>",
|
||||
"license": "MIT",
|
||||
"repository": "expressjs/serve-index",
|
||||
"dependencies": {
|
||||
"accepts": "~1.1.3",
|
||||
"batch": "0.5.1",
|
||||
"debug": "~2.1.0",
|
||||
"http-errors": "~1.2.7",
|
||||
"mime-types": "~2.0.3",
|
||||
"parseurl": "~1.3.0"
|
||||
"accepts": "~1.3.3",
|
||||
"batch": "0.6.1",
|
||||
"debug": "2.6.8",
|
||||
"escape-html": "~1.0.3",
|
||||
"http-errors": "~1.6.1",
|
||||
"mime-types": "~2.1.15",
|
||||
"parseurl": "~1.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"after": "0.8.1",
|
||||
"istanbul": "0.3.2",
|
||||
"mocha": "~2.0.1",
|
||||
"supertest": "~0.15.0"
|
||||
"after": "0.8.2",
|
||||
"istanbul": "0.4.5",
|
||||
"mocha": "2.5.3",
|
||||
"supertest": "1.1.0"
|
||||
},
|
||||
"files": [
|
||||
"public/",
|
||||
@@ -30,7 +31,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --reporter spec --bail --check-leaks test/",
|
||||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
|
||||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
|
||||
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
|
||||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,11 +52,11 @@
|
||||
}
|
||||
|
||||
function search() {
|
||||
var str = $('search').value
|
||||
, links = $('files').all('a');
|
||||
var str = $('search').value.toLowerCase();
|
||||
var links = $('files').all('a');
|
||||
|
||||
links.each(function(link){
|
||||
var text = link.textContent;
|
||||
var text = link.textContent.toLowerCase();
|
||||
|
||||
if ('..' == text) return;
|
||||
if (str.length && ~text.indexOf(str)) {
|
||||
@@ -75,7 +75,7 @@
|
||||
<body class="directory">
|
||||
<input id="search" type="text" placeholder="Search" autocomplete="off" />
|
||||
<div id="wrapper">
|
||||
<h1>{linked-path}</h1>
|
||||
<h1><a href="/">~</a>{linked-path}</h1>
|
||||
{files}
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -249,7 +249,7 @@ ul#files.view-details li.header {
|
||||
display: inline-block;
|
||||
width: 100%;
|
||||
text-indent: 0;
|
||||
background-position: 0 0;
|
||||
background-position: 0 50%;
|
||||
}
|
||||
#files .icon .name {
|
||||
text-indent: 41px;
|
||||
|
||||
1
test/fixtures/users/#dir/tobi.txt
vendored
Normal file
1
test/fixtures/users/#dir/tobi.txt
vendored
Normal file
@@ -0,0 +1 @@
|
||||
ferret
|
||||
@@ -1,26 +0,0 @@
|
||||
|
||||
var bytes = require('bytes');
|
||||
|
||||
exports['default request body'] = function(app){
|
||||
it('should default to {}', function(done){
|
||||
app.request()
|
||||
.post('/')
|
||||
.end(function(res){
|
||||
res.body.should.equal('{}');
|
||||
done();
|
||||
})
|
||||
})
|
||||
};
|
||||
|
||||
exports['limit body to'] = function(size, type, app){
|
||||
it('should accept a limit option', function(done){
|
||||
app.request()
|
||||
.post('/')
|
||||
.set('Content-Length', bytes(size) + 1)
|
||||
.set('Content-Type', type)
|
||||
.end(function(res){
|
||||
res.should.have.status(413);
|
||||
done();
|
||||
})
|
||||
})
|
||||
}
|
||||
293
test/test.js
293
test/test.js
@@ -26,6 +26,15 @@ describe('serveIndex(root)', function () {
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should include security header', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.expect('X-Content-Type-Options', 'nosniff')
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should serve a directory index', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
@@ -109,7 +118,7 @@ describe('serveIndex(root)', function () {
|
||||
.get('/')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(/g# %3 o %2525 %37 dir/)
|
||||
.expect(/g# %3 o & %2525 %37 dir/)
|
||||
.expect(/users/)
|
||||
.expect(/file #1\.txt/)
|
||||
.expect(/nums/)
|
||||
@@ -117,6 +126,16 @@ describe('serveIndex(root)', function () {
|
||||
.expect(/さくら\.txt/)
|
||||
.expect(200, done)
|
||||
});
|
||||
|
||||
it('should include security header', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'application/json')
|
||||
.expect('X-Content-Type-Options', 'nosniff')
|
||||
.expect(200, done)
|
||||
})
|
||||
});
|
||||
|
||||
describe('when Accept: text/html is given', function () {
|
||||
@@ -128,7 +147,7 @@ describe('serveIndex(root)', function () {
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/html; charset=utf-8')
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/users"/)
|
||||
.expect(/<a href="\/file%20%231.txt"/)
|
||||
.expect(/<a href="\/todo.txt"/)
|
||||
@@ -136,6 +155,30 @@ describe('serveIndex(root)', function () {
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should include security header', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect('X-Content-Type-Options', 'nosniff')
|
||||
.expect(200, done)
|
||||
})
|
||||
|
||||
it('should property escape file names', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/html; charset=utf-8')
|
||||
.expect(/<a href="\/foo%20%26%20bar"/)
|
||||
.expect(/foo & bar/)
|
||||
.expect(bodyDoesNotContain('foo & bar'))
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should sort folders first', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
@@ -145,15 +188,16 @@ describe('serveIndex(root)', function () {
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/html; charset=utf-8')
|
||||
.end(function (err, res) {
|
||||
if (err) throw err;
|
||||
var urls = res.text.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
|
||||
if (err) done(err);
|
||||
var body = res.text.split('</h1>')[1];
|
||||
var urls = body.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
|
||||
assert.deepEqual(urls, [
|
||||
'/%23directory',
|
||||
'/collect',
|
||||
'/g%23%20%253%20o%20%252525%20%2537%20dir',
|
||||
'/g%23%20%253%20o%20%26%20%252525%20%2537%20dir',
|
||||
'/users',
|
||||
'/file%20%231.txt',
|
||||
'/foo%20bar',
|
||||
'/foo%20%26%20bar',
|
||||
'/nums',
|
||||
'/todo.txt',
|
||||
'/%E3%81%95%E3%81%8F%E3%82%89.txt'
|
||||
@@ -173,12 +217,22 @@ describe('serveIndex(root)', function () {
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/plain; charset=utf-8')
|
||||
.expect(/users/)
|
||||
.expect(/g# %3 o %2525 %37 dir/)
|
||||
.expect(/g# %3 o & %2525 %37 dir/)
|
||||
.expect(/file #1.txt/)
|
||||
.expect(/todo.txt/)
|
||||
.expect(/さくら\.txt/)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should include security header', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/plain')
|
||||
.expect('X-Content-Type-Options', 'nosniff')
|
||||
.expect(200, done)
|
||||
})
|
||||
});
|
||||
|
||||
describe('when Accept: application/x-bogus is given', function () {
|
||||
@@ -255,7 +309,7 @@ describe('serveIndex(root)', function () {
|
||||
});
|
||||
|
||||
it('should filter directory paths', function (done) {
|
||||
var cb = after(3, done)
|
||||
var cb = after(4, done)
|
||||
var server = createServer(fixtures, {'filter': filter})
|
||||
|
||||
function filter(name, index, list, dir) {
|
||||
@@ -289,6 +343,146 @@ describe('serveIndex(root)', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('with "template" option', function () {
|
||||
describe('when setting a custom template file', function () {
|
||||
var server;
|
||||
before(function () {
|
||||
server = createServer(fixtures, {'template': __dirname + '/shared/template.html'});
|
||||
});
|
||||
|
||||
it('should respond with file list', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/users"/)
|
||||
.expect(/<a href="\/file%20%231.txt"/)
|
||||
.expect(/<a href="\/todo.txt"/)
|
||||
.expect(200, done)
|
||||
});
|
||||
|
||||
it('should respond with testing template sentence', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, /This is the test template/, done)
|
||||
});
|
||||
|
||||
it('should have default styles', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, /ul#files/, done)
|
||||
});
|
||||
|
||||
it('should list directory twice', function (done) {
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(function (res) {
|
||||
var occurances = res.text.match(/directory \/users\//g)
|
||||
if (occurances && occurances.length === 2) return
|
||||
throw new Error('directory not listed twice')
|
||||
})
|
||||
.expect(200, done)
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setting a custom template function', function () {
|
||||
it('should invoke function to render', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, 'This is a template.');
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, 'This is a template.', done);
|
||||
});
|
||||
|
||||
it('should handle render errors', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(new Error('boom!'));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(500, 'boom!', done);
|
||||
});
|
||||
|
||||
it('should provide "directory" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.directory));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, '"/users/"', done);
|
||||
});
|
||||
|
||||
it('should provide "displayIcons" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.displayIcons));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, 'false', done);
|
||||
});
|
||||
|
||||
it('should provide "fileList" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.fileList.map(function (file) {
|
||||
file.stat = file.stat instanceof fs.Stats;
|
||||
return file;
|
||||
})));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect('[{"name":"..","stat":true},{"name":"#dir","stat":true},{"name":"index.html","stat":true},{"name":"tobi.txt","stat":true}]')
|
||||
.expect(200, done);
|
||||
});
|
||||
|
||||
it('should provide "path" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.path));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, JSON.stringify(path.join(fixtures, 'users/')), done);
|
||||
});
|
||||
|
||||
it('should provide "style" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.style));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, /#files \.icon \.name/, done);
|
||||
});
|
||||
|
||||
it('should provide "viewName" local', function (done) {
|
||||
var server = createServer(fixtures, {'template': function (locals, callback) {
|
||||
callback(null, JSON.stringify(locals.viewName));
|
||||
}});
|
||||
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, '"tiles"', done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('when using custom handler', function () {
|
||||
describe('exports.html', function () {
|
||||
alterProperty(serveIndex, 'html', serveIndex.html)
|
||||
@@ -436,6 +630,25 @@ describe('serveIndex(root)', function () {
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should include link to parent directory', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/users')
|
||||
.end(function (err, res) {
|
||||
if (err) return done(err);
|
||||
var body = res.text.split('</h1>')[1];
|
||||
var urls = body.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
|
||||
assert.deepEqual(urls, [
|
||||
'/',
|
||||
'/users/%23dir',
|
||||
'/users/index.html',
|
||||
'/users/tobi.txt'
|
||||
]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should work for directory with #', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
@@ -453,12 +666,26 @@ describe('serveIndex(root)', function () {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/g%23%20%253%20o%20%252525%20%2537%20dir/')
|
||||
.get('/g%23%20%253%20o%20%26%20%252525%20%2537%20dir/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/html; charset=utf-8')
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir\/empty.txt"/)
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir\/empty.txt"/)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('should property escape directory names', function (done) {
|
||||
var server = createServer()
|
||||
|
||||
request(server)
|
||||
.get('/g%23%20%253%20o%20%26%20%252525%20%2537%20dir/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect('Content-Type', 'text/html; charset=utf-8')
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%26%20%252525%20%2537%20dir"/)
|
||||
.expect(/g# %3 o & %2525 %37 dir/)
|
||||
.expect(bodyDoesNotContain('g# %3 o & %2525 %37 dir'))
|
||||
.end(done);
|
||||
});
|
||||
|
||||
@@ -472,50 +699,6 @@ describe('serveIndex(root)', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setting a custom template', function () {
|
||||
var server;
|
||||
before(function () {
|
||||
server = createServer(fixtures, {'template': __dirname + '/shared/template.html'});
|
||||
});
|
||||
|
||||
it('should respond with file list', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(/<a href="\/g%23%20%253%20o%20%252525%20%2537%20dir"/)
|
||||
.expect(/<a href="\/users"/)
|
||||
.expect(/<a href="\/file%20%231.txt"/)
|
||||
.expect(/<a href="\/todo.txt"/)
|
||||
.expect(200, done)
|
||||
});
|
||||
|
||||
it('should respond with testing template sentence', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, /This is the test template/, done)
|
||||
});
|
||||
|
||||
it('should have default styles', function (done) {
|
||||
request(server)
|
||||
.get('/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200, /ul#files/, done)
|
||||
});
|
||||
|
||||
it('should list directory twice', function (done) {
|
||||
request(server)
|
||||
.get('/users/')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(function (res) {
|
||||
var occurances = res.text.match(/directory \/users\//g)
|
||||
if (occurances && occurances.length === 2) return
|
||||
throw new Error('directory not listed twice')
|
||||
})
|
||||
.expect(200, done)
|
||||
});
|
||||
});
|
||||
|
||||
describe('when setting a custom stylesheet', function () {
|
||||
var server;
|
||||
before(function () {
|
||||
|
||||
Reference in New Issue
Block a user