Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve containsDotFile and only run if necessary #41

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions benchmarks/containsDotFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const benchmark = require('benchmark')
const { containsDotFile } = require('../lib/containsDotFile')

const hasDotFileSimple = '.github'.split('/')
const hasDotFile = './.github'.split('/')
const noDotFile = './index.html'.split('/')

new benchmark.Suite()
.add(hasDotFileSimple.join('/'), function () { containsDotFile(hasDotFileSimple) }, { minSamples: 100 })
.add(noDotFile.join('/'), function () { containsDotFile(noDotFile) }, { minSamples: 100 })
.add(hasDotFile.join('/'), function () { containsDotFile(hasDotFile) }, { minSamples: 100 })
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
.run({ async: false })
31 changes: 23 additions & 8 deletions lib/SendStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ const ERROR_RESPONSES = {
500: createHtmlDocument('Error', 'Internal Server Error')
}

const validDotFilesOptions = [
'allow',
'ignore',
'deny'
]

/**
* Initialize a `SendStream` with the given `path`.
*
Expand Down Expand Up @@ -105,10 +111,10 @@ function SendStream (req, path, options) {
: true

this._dotfiles = opts.dotfiles !== undefined
? opts.dotfiles
: 'ignore'
? validDotFilesOptions.indexOf(opts.dotfiles)
: 1 // 'ignore'

if (this._dotfiles !== 'ignore' && this._dotfiles !== 'allow' && this._dotfiles !== 'deny') {
if (this._dotfiles === -1) {
throw new TypeError('dotfiles option must be "allow", "deny", or "ignore"')
}

Expand Down Expand Up @@ -548,16 +554,25 @@ SendStream.prototype.pipe = function pipe (res) {
}

// dotfile handling
if (containsDotFile(parts)) {
debug('%s dotfile "%s"', this._dotfiles, path)
if (
(
debug.enabled || // if debugging is enabled, then check for all cases to log allow case
this._dotfiles !== 0 // if debugging is not enabled, then only check if 'deny' or 'ignore' is set
) &&
containsDotFile(parts)
) {
switch (this._dotfiles) {
case 'allow':
/* istanbul ignore next: unreachable, because NODE_DEBUG can not be set after process is running */
case 0: // 'allow'
debug('allow dotfile "%s"', path)
break
case 'deny':
case 2: // 'deny'
debug('deny dotfile "%s"', path)
this.error(403)
return res
case 'ignore':
case 1: // 'ignore'
default:
debug('ignore dotfile "%s"', path)
this.error(404)
return res
}
Expand Down
8 changes: 4 additions & 4 deletions lib/containsDotFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* @api private
*/
function containsDotFile (parts) {
for (let i = 0; i < parts.length; i++) {
const part = parts[i]
if (part.length > 1 && part[0] === '.') {
for (let i = 0, il = parts.length; i < il; ++i) {
if (parts[i].length !== 1 && parts[i][0] === '.') {
return true
}
}

return false
}
exports.containsDotFile = containsDotFile

module.exports.containsDotFile = containsDotFile
18 changes: 18 additions & 0 deletions test/containsDotFile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const { test } = require('tap')
const { containsDotFile } = require('../lib/containsDotFile')

test('containsDotFile', function (t) {
const testCases = [
['/.github', true],
['.github', true],
['index.html', false],
['./index.html', false]
]
t.plan(testCases.length)

for (let i = 0; i < testCases.length; ++i) {
t.strictSame(containsDotFile(testCases[i][0].split('/')), testCases[i][1], testCases[i][0])
}
})