Socket
Socket
Sign inDemoInstall

koa-add-trailing-slashes

Package Overview
Dependencies
Maintainers
2
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-add-trailing-slashes - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

60

index.js

@@ -8,2 +8,6 @@ 'use strict';

if (opts.defer !== false) {
opts.defer = opts.defer || true;
}
if (opts.index !== false) {

@@ -13,19 +17,30 @@ opts.index = opts.index || 'index.html';

if (opts.chained !== false) {
opts.chained = opts.chained || true;
}
return function* (next) {
yield next;
var url = getBaseUrl(this.originalUrl, this.url);
if (opts.defer) {
yield next;
}
if (noBodyOrIndex(this.body, this.path, opts.index) && missingSlash(url, this.path)) {
var query = this.url.slice(this.path.length);
var path = this.path.substring(1);
var path;
if (!path.length) {
path = '/';
} else {
path += '/';
}
// We have already done a redirect and we will continue if we are in chained mode
if (opts.chained && this.status === 301) {
path = getPath(this.response.get('Location'), this.querystring);
} else if (this.status !== 301) {
path = getPath(this.originalUrl, this.querystring);
}
if (path && noBodyOrIndex(this.status, this.body, path, opts.index) && missingSlash(path)) {
var query = this.querystring.length ? '?' + this.querystring : '';
this.status = 301;
this.redirect(url + path + query);
this.redirect(path + '/' + query);
}
if (!opts.defer) {
yield next;
}
};

@@ -38,18 +53,17 @@ }

function getBaseUrl(original, url) {
var noInitalSlash = url.substring(1).length;
if (noInitalSlash !== 0) {
return original.slice(0, -noInitalSlash);
}
return original;
function noBodyOrIndex(status, body, path, index) {
return !body || status !== 200 ||
(index && body.path && getFilename(body.path) === index && getFilename(body.path) !== getFilename(path));
}
function noBodyOrIndex(body, path, index) {
return !body ||
(index && body.path && getFilename(body.path) === index && getFilename(body.path) !== getFilename(path));
function missingSlash(path) {
return path.slice(-1) !== '/';
}
function missingSlash(url, path) {
return path.slice(-1) !== '/' || url !== '/' && url.slice(-1) !== '/';
function getPath(original, querystring) {
if (querystring.length) {
return original.slice(0, -querystring.length - 1);
}
return original;
}
{
"name": "koa-add-trailing-slashes",
"version": "1.0.0",
"description": "Koa middleware that makes sure all requests have trailing slashes",
"version": "1.1.0",
"description": "Koa middleware that makes sure all requests have a trailing slashes",
"main": "index.js",

@@ -21,3 +21,4 @@ "scripts": {

"trailing",
"redirect"
"redirect",
"add"
],

@@ -24,0 +25,0 @@ "author": "VG",

# koa-add-trailing-slashes
Koa middleware that adds trailing slashes to a URL if it not already has it.
Koa middleware that adds trailing slashes on an URL.

@@ -23,4 +23,7 @@ [![Build Status](https://img.shields.io/travis/vgno/koa-add-trailing-slashes/master.svg?style=flat-square)](http://travis-ci.org/vgno/koa-add-trailing-slashes) [![Coverage Status](https://img.shields.io/coveralls/vgno/koa-add-trailing-slashes/master.svg?style=flat-square)](https://coveralls.io/r/vgno/koa-add-trailing-slashes) [![npm](https://img.shields.io/npm/v/koa-add-trailing-slashes.svg?style=flat-square)](https://www.npmjs.com/package/koa-add-trailing-slashes)

- `index` - Default file name, defaults to 'index.html'. Will automatically add slashes to folders that contain this index file, expected to be used with `koa-static`. Set to false to disable this.
- `index` - Default file name, defaults to 'index.html'. Will automatically add slashes to folders that contain this index file, expected to be used with `koa-static`. Defaults to `index.html`.
- `defer` - If true, serves after yield next, allowing any downstream middleware to respond first. Defaults to `true`.
- `chained` - If the middleware should continue modifying the url if it detects that a redirect already have been performed. Defaults to `true`.
## Example

@@ -43,9 +46,10 @@ ```js

## Important
Make sure this us added before an eventual [koa-static](https://github.com/koajs/static) to make sure requests to files are not changed and managed correctly.
Make sure this is added before an eventual [koa-static](https://github.com/koajs/static) middleware to make sure requests to files are not changed and managed correctly. This because it will not rewrite the URL if a `body` has been set along with status `200`. Once exception to this is if the `body` is the index file described above, to make sure a trailing slash is added to the end of a folder that serves the index file.
Will not rewrite the URL if a `body` has been set in general. The special case being if the `body` is the index file described above.
If all paths always should be rewritten one can set `defer` to `false`.
For example if the path in the browser is `/foo` and `koa-static` resolves that to `foo/index.html` internally and `opts.index` is not disabled the path will end up as `/foo/`.
__Example__
If the url in the browser is `/foo` and `koa-static` resolves that to `foo/index.html` internally along with `opts.index` matching the filename, in this case `index.html`, the path will end up as `/foo/`.
## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc