From 92d67676ff25843cc8adc741ec498683097f860f Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sat, 9 Jun 2018 19:26:38 -0400 Subject: [PATCH 01/21] build: support Node.js 10.x --- .travis.yml | 1 + appveyor.yml | 5 ++++- test/test.js | 6 +++--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 96db0529..08f340b6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ node_js: - "7.10" - "8.11" - "9.11" + - "10.4" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index b7e9ff35..653418b1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,10 +12,13 @@ environment: - nodejs_version: "7.10" - nodejs_version: "8.11" - nodejs_version: "9.11" + - nodejs_version: "10.4" cache: - node_modules install: - - ps: Install-Product node $env:nodejs_version + - ps: >- + try { Install-Product node $env:nodejs_version -ErrorAction Stop } + catch { Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version) } - npm config set shrinkwrap false - if "%nodejs_version%" equ "0.8" npm config set strict-ssl false - if "%nodejs_version%" equ "0.8" npm rm --save-dev istanbul diff --git a/test/test.js b/test/test.js index b0c9531d..283aad51 100644 --- a/test/test.js +++ b/test/test.js @@ -93,11 +93,11 @@ describe('serveIndex(root)', function () { }) it('should treat an ENAMETOOLONG as a 414', function (done) { - var path = Array(11000).join('foobar') - var server = createServer() + var dir = path.join(fixtures, Array(10000).join('/foobar')) + var server = createServer(dir) request(server) - .get('/' + path) + .get('/') .expect(414, done) }) From abedc7fcfeb8071e236e13f32056de0f3534dbe2 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Tue, 25 Dec 2018 23:50:40 -0500 Subject: [PATCH 02/21] deps: mime-types@~2.1.21 --- HISTORY.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c163beb1..e1b38a52 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -8,9 +8,9 @@ unreleased - deps: depd@~1.1.2 - deps: setprototypeof@1.1.0 - deps: statuses@'>= 1.4.0 < 2' - * deps: mime-types@~2.1.18 + * deps: mime-types@~2.1.21 - Add new mime types - - deps: mime-db@~1.33.0 + - deps: mime-db@~1.37.0 1.9.1 / 2017-09-28 ================== diff --git a/package.json b/package.json index a28de91c..5e227570 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "debug": "2.6.9", "escape-html": "~1.0.3", "http-errors": "~1.6.3", - "mime-types": "~2.1.18", + "mime-types": "~2.1.21", "parseurl": "~1.3.2" }, "devDependencies": { From fadb1567655f09d09595575840cc391b64350c7d Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 30 Dec 2018 00:02:39 -0500 Subject: [PATCH 03/21] build: Node.js@6.16 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 08f340b6..08c0490c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ node_js: - "3.3" - "4.9" - "5.12" - - "6.14" + - "6.16" - "7.10" - "8.11" - "9.11" diff --git a/appveyor.yml b/appveyor.yml index 653418b1..5142d045 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -8,7 +8,7 @@ environment: - nodejs_version: "3.3" - nodejs_version: "4.9" - nodejs_version: "5.12" - - nodejs_version: "6.14" + - nodejs_version: "6.16" - nodejs_version: "7.10" - nodejs_version: "8.11" - nodejs_version: "9.11" From 05b0e2184820599dc9f29b8ccd3900049f6b98b8 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 30 Dec 2018 00:09:51 -0500 Subject: [PATCH 04/21] build: Node.js@8.15 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 08c0490c..63742f1d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ node_js: - "5.12" - "6.16" - "7.10" - - "8.11" + - "8.15" - "9.11" - "10.4" sudo: false diff --git a/appveyor.yml b/appveyor.yml index 5142d045..4f4ee126 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,7 +10,7 @@ environment: - nodejs_version: "5.12" - nodejs_version: "6.16" - nodejs_version: "7.10" - - nodejs_version: "8.11" + - nodejs_version: "8.15" - nodejs_version: "9.11" - nodejs_version: "10.4" cache: From 935ee09c822886d108fe8284b44d0af40ce2462f Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 30 Dec 2018 00:14:12 -0500 Subject: [PATCH 05/21] build: Node.js@10.15 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 63742f1d..8684f988 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ node_js: - "7.10" - "8.15" - "9.11" - - "10.4" + - "10.15" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index 4f4ee126..daa32220 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,7 +12,7 @@ environment: - nodejs_version: "7.10" - nodejs_version: "8.15" - nodejs_version: "9.11" - - nodejs_version: "10.4" + - nodejs_version: "10.15" cache: - node_modules install: From 9c56901718dd438776d03b1ea07510809adf5fd2 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 3 Jan 2019 17:02:48 -0500 Subject: [PATCH 06/21] build: support Node.js 11.x --- .travis.yml | 1 + appveyor.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8684f988..bb29852d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ node_js: - "8.15" - "9.11" - "10.15" + - "11.6" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index daa32220..0ae3e983 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,6 +13,7 @@ environment: - nodejs_version: "8.15" - nodejs_version: "9.11" - nodejs_version: "10.15" + - nodejs_version: "11.6" cache: - node_modules install: From 156303efe069ebdd9bd648f4db043e6cf6f35fb5 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Fri, 4 Jan 2019 21:40:16 -0500 Subject: [PATCH 07/21] deps: http-errors@~1.7.1 --- HISTORY.md | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index e1b38a52..92727cb4 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,10 +4,11 @@ unreleased * Show font icon for more font types * deps: accepts@~1.3.5 - deps: mime-types@~2.1.18 - * deps: http-errors@~1.6.3 + * deps: http-errors@~1.7.1 + - Set constructor name when possible - deps: depd@~1.1.2 - deps: setprototypeof@1.1.0 - - deps: statuses@'>= 1.4.0 < 2' + - deps: statuses@'>= 1.5.0 < 2' * deps: mime-types@~2.1.21 - Add new mime types - deps: mime-db@~1.37.0 diff --git a/package.json b/package.json index 5e227570..d9ec75ed 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "batch": "0.6.1", "debug": "2.6.9", "escape-html": "~1.0.3", - "http-errors": "~1.6.3", + "http-errors": "~1.7.1", "mime-types": "~2.1.21", "parseurl": "~1.3.2" }, From 6636afe7fdfd1ace6b5dee7c95690dd13e4f3a09 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 31 Mar 2019 22:54:48 -0400 Subject: [PATCH 08/21] build: Node.js@6.17 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index bb29852d..8f302a1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ node_js: - "3.3" - "4.9" - "5.12" - - "6.16" + - "6.17" - "7.10" - "8.15" - "9.11" diff --git a/appveyor.yml b/appveyor.yml index 0ae3e983..7a73e445 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -8,7 +8,7 @@ environment: - nodejs_version: "3.3" - nodejs_version: "4.9" - nodejs_version: "5.12" - - nodejs_version: "6.16" + - nodejs_version: "6.17" - nodejs_version: "7.10" - nodejs_version: "8.15" - nodejs_version: "9.11" From 4382b8f6254010658c6ba2363da8f0710174780a Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 31 Mar 2019 23:02:00 -0400 Subject: [PATCH 09/21] build: Node.js@11.13 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8f302a1f..e0627b97 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ node_js: - "8.15" - "9.11" - "10.15" - - "11.6" + - "11.13" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index 7a73e445..2b29b5c7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,7 +13,7 @@ environment: - nodejs_version: "8.15" - nodejs_version: "9.11" - nodejs_version: "10.15" - - nodejs_version: "11.6" + - nodejs_version: "11.13" cache: - node_modules install: From 173f71a6a17afa8fd3bf1d28633d381b1dd71aac Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 1 Apr 2019 00:38:48 -0400 Subject: [PATCH 10/21] deps: mime-types@~2.1.22 --- HISTORY.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 92727cb4..64630626 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -9,9 +9,9 @@ unreleased - deps: depd@~1.1.2 - deps: setprototypeof@1.1.0 - deps: statuses@'>= 1.5.0 < 2' - * deps: mime-types@~2.1.21 + * deps: mime-types@~2.1.22 - Add new mime types - - deps: mime-db@~1.37.0 + - deps: mime-db@~1.38.0 1.9.1 / 2017-09-28 ================== diff --git a/package.json b/package.json index d9ec75ed..29ba0236 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "debug": "2.6.9", "escape-html": "~1.0.3", "http-errors": "~1.7.1", - "mime-types": "~2.1.21", + "mime-types": "~2.1.22", "parseurl": "~1.3.2" }, "devDependencies": { From 32b7a1adec77ec4cd2fad1b84e40eb96d9fece0b Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 1 Apr 2019 00:45:20 -0400 Subject: [PATCH 11/21] deps: http-errors@~1.7.2 --- HISTORY.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 64630626..a3a66f69 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,10 +4,10 @@ unreleased * Show font icon for more font types * deps: accepts@~1.3.5 - deps: mime-types@~2.1.18 - * deps: http-errors@~1.7.1 + * deps: http-errors@~1.7.2 - Set constructor name when possible - deps: depd@~1.1.2 - - deps: setprototypeof@1.1.0 + - deps: setprototypeof@1.1.1 - deps: statuses@'>= 1.5.0 < 2' * deps: mime-types@~2.1.22 - Add new mime types diff --git a/package.json b/package.json index 29ba0236..1fba8dc0 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "batch": "0.6.1", "debug": "2.6.9", "escape-html": "~1.0.3", - "http-errors": "~1.7.1", + "http-errors": "~1.7.2", "mime-types": "~2.1.22", "parseurl": "~1.3.2" }, From d3ee7d8f5548a12d13a06984b4e396df90e1135a Mon Sep 17 00:00:00 2001 From: Joonas Rouhiainen Date: Wed, 22 Aug 2018 23:13:14 +0300 Subject: [PATCH 12/21] Use 400 error on URI decode failure instead of 500 closes #85 closes #88 --- HISTORY.md | 1 + index.js | 24 ++++++++++++++++++++++-- test/test.js | 8 ++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index a3a66f69..eaca0ead 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ unreleased ========== * Show font icon for more font types + * Use 400 error on URI decode failure instead of 500 * deps: accepts@~1.3.5 - deps: mime-types@~2.1.18 * deps: http-errors@~1.7.2 diff --git a/index.js b/index.js index e707bbc5..0ee0e1a0 100644 --- a/index.js +++ b/index.js @@ -107,10 +107,14 @@ function serveIndex(root, options) { return; } + // get dir + var dir = getRequestedDir(req) + + // bad request + if (dir === null) return next(createError(400)) + // parse URLs - var url = parseUrl(req); var originalUrl = parseUrl.original(req); - var dir = decodeURIComponent(url.pathname); var originalDir = decodeURIComponent(originalUrl.pathname); // join / normalize from root dir @@ -327,6 +331,22 @@ function fileSort(a, b) { String(a.name).toLocaleLowerCase().localeCompare(String(b.name).toLocaleLowerCase()); } +/** + * Get the requested directory from request. + * + * @param req + * @return {string} + * @api private + */ + +function getRequestedDir (req) { + try { + return decodeURIComponent(parseUrl(req).pathname) + } catch (e) { + return null + } +} + /** * Map html `dir`, returning a linked path. */ diff --git a/test/test.js b/test/test.js index 283aad51..d16f834e 100644 --- a/test/test.js +++ b/test/test.js @@ -76,6 +76,14 @@ describe('serveIndex(root)', function () { .expect(400, done) }) + it('should deny path that does not decode', function (done) { + var server = createServer() + + request(server) + .head('/%FF') + .expect(400, done) + }) + it('should deny path outside root', function (done) { var server = createServer() From d8ff9b9e41d772b5a03341dcf624a9009b9c9ab9 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 28 Apr 2019 00:55:52 -0400 Subject: [PATCH 13/21] deps: mime-types@~2.1.24 --- HISTORY.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index eaca0ead..7a465cc2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -10,9 +10,9 @@ unreleased - deps: depd@~1.1.2 - deps: setprototypeof@1.1.1 - deps: statuses@'>= 1.5.0 < 2' - * deps: mime-types@~2.1.22 + * deps: mime-types@~2.1.24 - Add new mime types - - deps: mime-db@~1.38.0 + - deps: mime-db@~1.40.0 1.9.1 / 2017-09-28 ================== diff --git a/package.json b/package.json index 1fba8dc0..b905a455 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "debug": "2.6.9", "escape-html": "~1.0.3", "http-errors": "~1.7.2", - "mime-types": "~2.1.22", + "mime-types": "~2.1.24", "parseurl": "~1.3.2" }, "devDependencies": { From a30f269f9981c38aadbc6a5a06c3f888c8b2c656 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Sun, 28 Apr 2019 00:56:30 -0400 Subject: [PATCH 14/21] deps: parseurl@~1.3.3 --- HISTORY.md | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 7a465cc2..5eef8b55 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -13,6 +13,7 @@ unreleased * deps: mime-types@~2.1.24 - Add new mime types - deps: mime-db@~1.40.0 + * deps: parseurl@~1.3.3 1.9.1 / 2017-09-28 ================== diff --git a/package.json b/package.json index b905a455..860b6d66 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "escape-html": "~1.0.3", "http-errors": "~1.7.2", "mime-types": "~2.1.24", - "parseurl": "~1.3.2" + "parseurl": "~1.3.3" }, "devDependencies": { "after": "0.8.2", From fa3fe02a2feea0493a5f47322f844acc3898d04a Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 29 Apr 2019 23:52:19 -0400 Subject: [PATCH 15/21] deps: accepts@~1.3.7 --- HISTORY.md | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 5eef8b55..5d9f2650 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -3,8 +3,9 @@ unreleased * Show font icon for more font types * Use 400 error on URI decode failure instead of 500 - * deps: accepts@~1.3.5 - - deps: mime-types@~2.1.18 + * deps: accepts@~1.3.7 + - deps: mime-types@~2.1.24 + - deps: negotiator@0.6.2 * deps: http-errors@~1.7.2 - Set constructor name when possible - deps: depd@~1.1.2 diff --git a/package.json b/package.json index 860b6d66..85b0da25 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "license": "MIT", "repository": "expressjs/serve-index", "dependencies": { - "accepts": "~1.3.5", + "accepts": "~1.3.7", "batch": "0.6.1", "debug": "2.6.9", "escape-html": "~1.0.3", From e7ae81345957db0979492ffc20efcb0ef17ad5d7 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 29 Apr 2019 23:54:04 -0400 Subject: [PATCH 16/21] build: eslint-plugin-markdown@1.0.0-beta.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 85b0da25..8c5535ae 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "devDependencies": { "after": "0.8.2", "eslint": "3.19.0", - "eslint-plugin-markdown": "1.0.0-beta.6", + "eslint-plugin-markdown": "1.0.0-beta.8", "istanbul": "0.4.5", "mocha": "2.5.3", "supertest": "1.1.0" From 4482ec96394e7a8461e855e8a869c61f48a190b0 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 29 Apr 2019 23:54:49 -0400 Subject: [PATCH 17/21] build: Node.js@8.16 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0627b97..a9ebefa6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ node_js: - "5.12" - "6.17" - "7.10" - - "8.15" + - "8.16" - "9.11" - "10.15" - "11.13" diff --git a/appveyor.yml b/appveyor.yml index 2b29b5c7..4d19f380 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,7 +10,7 @@ environment: - nodejs_version: "5.12" - nodejs_version: "6.17" - nodejs_version: "7.10" - - nodejs_version: "8.15" + - nodejs_version: "8.16" - nodejs_version: "9.11" - nodejs_version: "10.15" - nodejs_version: "11.13" From 5583f9ec06680715c04c116f604fded006241123 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 29 Apr 2019 23:55:03 -0400 Subject: [PATCH 18/21] build: Node.js@11.14 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a9ebefa6..4501017d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ node_js: - "8.16" - "9.11" - "10.15" - - "11.13" + - "11.14" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index 4d19f380..14220345 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,7 +13,7 @@ environment: - nodejs_version: "8.16" - nodejs_version: "9.11" - nodejs_version: "10.15" - - nodejs_version: "11.13" + - nodejs_version: "11.14" cache: - node_modules install: From 002a25112259f32d5d38f0b82857ab81260d8126 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 2 May 2019 23:35:13 -0400 Subject: [PATCH 19/21] Fix text and json responses to match html sorting --- HISTORY.md | 1 + index.js | 54 +++++++++++++++++++++++++++++++++++++++------------- test/test.js | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 13 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 5d9f2650..0e375afb 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,7 @@ unreleased ========== + * Fix text and json responses to match html sorting * Show font icon for more font types * Use 400 error on URI decode failure instead of 500 * deps: accepts@~1.3.7 diff --git a/index.js b/index.js index 0ee0e1a0..09086acc 100644 --- a/index.js +++ b/index.js @@ -184,14 +184,9 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path } // stat all files - stat(path, files, function (err, stats) { + stat(path, files, function (err, fileList) { 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); @@ -222,16 +217,42 @@ serveIndex.html = function _html(req, res, files, next, dir, showUp, icons, path * Respond with application/json. */ -serveIndex.json = function _json(req, res, files) { - send(res, 'application/json', JSON.stringify(files)) +serveIndex.json = function _json (req, res, files, next, dir, showUp, icons, path) { + // stat all files + stat(path, files, function (err, fileList) { + if (err) return next(err) + + // sort file list + fileList.sort(fileSort) + + // serialize + var body = JSON.stringify(fileList.map(function (file) { + return file.name + })) + + send(res, 'application/json', body) + }) }; /** * Respond with text/plain. */ -serveIndex.plain = function _plain(req, res, files) { - send(res, 'text/plain', (files.join('\n') + '\n')) +serveIndex.plain = function _plain (req, res, files, next, dir, showUp, icons, path) { + // stat all files + stat(path, files, function (err, fileList) { + if (err) return next(err) + + // sort file list + fileList.sort(fileSort) + + // serialize + var body = fileList.map(function (file) { + return file.name + }).join('\n') + '\n' + + send(res, 'text/plain', body) + }) }; /** @@ -527,8 +548,12 @@ function send (res, type, body) { } /** - * Stat all files and return array of stat - * in same order. + * Stat all files and return array of objects in the form + * `{ name, stat }`. + * + * @param {Array} files + * @return {Array} + * @api private */ function stat(dir, files, cb) { @@ -542,7 +567,10 @@ function stat(dir, files, cb) { if (err && err.code !== 'ENOENT') return done(err); // pass ENOENT as null stat, not error - done(null, stat || null); + done(null, { + name: file, + stat: stat || null + }) }); }); }); diff --git a/test/test.js b/test/test.js index d16f834e..c9213c2e 100644 --- a/test/test.js +++ b/test/test.js @@ -144,6 +144,26 @@ describe('serveIndex(root)', function () { .expect('X-Content-Type-Options', 'nosniff') .expect(200, done) }) + + it('should sort folders first', function (done) { + request(createServer()) + .get('/') + .set('Accept', 'application/json') + .expect(200) + .expect('Content-Type', 'application/json; charset=utf-8') + .expect([ + '#directory', + 'collect', + 'g# %3 o & %2525 %37 dir', + 'users', + 'file #1.txt', + 'foo & bar', + 'nums', + 'todo.txt', + 'さくら.txt' + ]) + .end(done) + }) }); describe('when Accept: text/html is given', function () { @@ -241,6 +261,27 @@ describe('serveIndex(root)', function () { .expect('X-Content-Type-Options', 'nosniff') .expect(200, done) }) + + it('should sort folders first', function (done) { + request(createServer()) + .get('/') + .set('Accept', 'text/plain') + .expect(200) + .expect('Content-Type', 'text/plain; charset=utf-8') + .expect([ + '#directory', + 'collect', + 'g# %3 o & %2525 %37 dir', + 'users', + 'file #1.txt', + 'foo & bar', + 'nums', + 'todo.txt', + 'さくら.txt', + '' + ].join('\n')) + .end(done) + }) }); describe('when Accept: application/x-bogus is given', function () { From 4c8c716dede06f0aa49b2ad0b247c42ff433b430 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 2 May 2019 23:37:20 -0400 Subject: [PATCH 20/21] build: Node.js@11.15 --- .travis.yml | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4501017d..33891274 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ node_js: - "8.16" - "9.11" - "10.15" - - "11.14" + - "11.15" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index 14220345..ee68b1e4 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -13,7 +13,7 @@ environment: - nodejs_version: "8.16" - nodejs_version: "9.11" - nodejs_version: "10.15" - - nodejs_version: "11.14" + - nodejs_version: "11.15" cache: - node_modules install: From 20e83c893b701c3a117a0f6836be0f5a818bb925 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Thu, 2 May 2019 23:37:45 -0400 Subject: [PATCH 21/21] build: support Node.js 12.x --- .travis.yml | 1 + appveyor.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 33891274..f99fd84c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ node_js: - "9.11" - "10.15" - "11.15" + - "12.1" sudo: false cache: directories: diff --git a/appveyor.yml b/appveyor.yml index ee68b1e4..a05daedb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -14,6 +14,7 @@ environment: - nodejs_version: "9.11" - nodejs_version: "10.15" - nodejs_version: "11.15" + - nodejs_version: "12.1" cache: - node_modules install: