Skip to content

Commit

Permalink
extract hasTrailingSlash
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Jan 13, 2023
1 parent 383bfd7 commit 7746d06
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
16 changes: 3 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const normalizeList = require('./lib/normalizeList')
const parseHttpDate = require('./lib/parseHttpDate')
const parseTokenList = require('./lib/parseTokenList')
const setHeaders = require('./lib/setHeaders')
const hasTrailingSlash = require('./lib/hasTrailingSlash')

/**
* Path function references.
Expand Down Expand Up @@ -222,17 +223,6 @@ SendStream.prototype.error = function error (status, err) {
res.end(doc[0])
}

/**
* Check if the pathname ends with "/".
*
* @return {boolean}
* @private
*/

SendStream.prototype.hasTrailingSlash = function hasTrailingSlash () {
return this.path[this.path.length - 1] === '/'
}

/**
* Check if this is a conditional GET request.
*
Expand Down Expand Up @@ -409,7 +399,7 @@ SendStream.prototype.redirect = function redirect (path) {
return
}

if (this.hasTrailingSlash()) {
if (hasTrailingSlash(this.path)) {
this.error(403)
return
}
Expand Down Expand Up @@ -515,7 +505,7 @@ SendStream.prototype.pipe = function pipe (res) {
}

// index file support
if (this._index.length && this.hasTrailingSlash()) {
if (this._index.length && hasTrailingSlash(this.path)) {
this.sendIndex(path)
return res
}
Expand Down
11 changes: 11 additions & 0 deletions lib/hasTrailingSlash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

/**
* Check if the pathname ends with "/".
*
* @return {boolean}
* @private
*/
module.exports = function hasTrailingSlash (path) {
return path[path.length - 1] === '/'
}
5 changes: 0 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ declare namespace send {
*/
error(status: number, error?: Error): void;

/**
* Check if the pathname ends with "/".
*/
hasTrailingSlash(): boolean;

/**
* Check if this is a conditional GET request.
*/
Expand Down

0 comments on commit 7746d06

Please sign in to comment.