Socket
Socket
Sign inDemoInstall

connect-history-api-fallback

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

4

CHANGELOG.md
# Changelog
## Unreleased
- Rewrite rules are now applied before the request URL is checked for dots.
- Rewrite rules can be defined as functions to have greater control over the `dot rule`.
## v1.0.0

@@ -4,0 +8,0 @@ This version introduces a fair amount of breaking changes. Specifically, instances of the historyApiFallback need to be created via the exported function. Previously, this was not necessary.

37

lib/index.js

@@ -46,2 +46,15 @@ 'use strict';

var parsedUrl = url.parse(req.url);
var rewriteTarget;
options.rewrites = options.rewrites || [];
for (var i = 0; i < options.rewrites.length; i++) {
var rewrite = options.rewrites[i];
var match = parsedUrl.pathname.match(rewrite.from);
if (match !== null) {
rewriteTarget = evaluateRewriteRule(parsedUrl, match, rewrite.to);
logger('Rewriting', req.method, req.url, 'to', rewriteTarget);
req.url = rewriteTarget;
return next();
}
}
if (parsedUrl.pathname.indexOf('.') !== -1) {

@@ -57,12 +70,3 @@ logger(

var rewriteTarget = options.index || '/index.html';
options.rewrites = options.rewrites || [];
for (var i = 0; i < options.rewrites.length; i++) {
var rewrite = options.rewrites[i];
if (rewrite.from.test(parsedUrl.pathname)) {
rewriteTarget = rewrite.to;
}
}
rewriteTarget = options.index || '/index.html';
logger('Rewriting', req.method, req.url, 'to', rewriteTarget);

@@ -74,2 +78,15 @@ req.url = rewriteTarget;

function evaluateRewriteRule(parsedUrl, match, rule) {
if (typeof rule === 'string') {
return rule;
} else if (typeof rule !== 'function') {
throw new Error('Rewrite rule can only be of type string of function.');
}
return rule({
parsedUrl: parsedUrl,
match: match
});
}
function acceptsHtml(header) {

@@ -76,0 +93,0 @@ return header.indexOf('text/html') !== -1 || header.indexOf('*/*') !== -1;

{
"name": "connect-history-api-fallback",
"version": "1.0.0",
"version": "1.1.0",
"description": "Provides a fallback for non-existing directories so that the HTML 5 history API can be used.",

@@ -5,0 +5,0 @@ "keyswords": [

<h1 align="center">connect-history-api-fallback</h1>
<p align="center">Middleware to proxy requests through a specified index page, useful for Single Page Applications that utilise the HTML5 History API.</p>
[![Build Status](https://travis-ci.org/bripkens/connect-history-api-fallback.svg?branch=master)](https://travis-ci.org/bripkens/connect-history-api-fallback)
[![Build Status](https://travis-ci.org/bripkens/connect-history-api-fallback.svg?branch=master)](https://travis-ci.org/bripkens/connect-history-api-fallback)[![Dependency Status](https://img.shields.io/gemnasium/bripkens/connect-history-api-fallback.svg)](https://gemnasium.com/npms/connect-history-api-fallback)

@@ -80,9 +80,9 @@ [![NPM](https://nodei.co/npm/connect-history-api-fallback.png?downloads=true&downloadRank=true)](https://nodei.co/npm/connect-history-api-fallback/)

### rewrites
Override the index when the request url matches a regex pattern
Override the index when the request url matches a regex pattern. You can either rewrite to a static string or use a function to transform the incoming request.
The following will rewrite a request that matches the `/\/soccer/` pattern to `/soccer.html`.
```javascript
history({
rewrites: [
{ from: /\/soccer/, to: '/soccer.html'},
{ from: /\/tennis/, to: '/tennis.html'}
{ from: /\/soccer/, to: '/soccer.html'}
]

@@ -92,2 +92,22 @@ });

Alternatively functions can be used to have more control over the rewrite process. For instance, the following listing shows how requests to `/libs/jquery/jquery.1.12.0.min.js` and the like can be routed to `./bower_components/libs/jquery/jquery.1.12.0.min.js`. You can also make use of this if you have an API version in the URL path.
```javascript
history({
rewrites: [
{
from: /^\/libs\/.*$/,
to: function(context) {
return '/bower_components' + context.parsedUrl.pathname;
}
}
]
});
```
The function will always be called with a context object that has the following properties:
- **parsedUrl**: Information about the URL as provided by the [URL module's](https://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost) `url.parse`.
- **match**: An Array of matched results as provided by `String.match(...)`.
### verbose

@@ -94,0 +114,0 @@ This middleware does not log any information by default. If you wish to activate logging, then you can do so via the `verbose` option or by specifying a logger function.

@@ -146,2 +146,28 @@ 'use strict';

tests['should support functions as rewrite rule'] = function(test) {
middleware = historyApiFallback({
rewrites: [
{
from: /^\/libs\/(.*)$/,
to: function(context) {
return './bower_components' + context.parsedUrl.pathname;
}
}
]
});
req.url = '/libs/jquery/jquery.1.12.0.min.js';
middleware(req, null, next);
test.equal(req.url, './bower_components/libs/jquery/jquery.1.12.0.min.js');
test.ok(next.called);
next = sinon.stub();
var expected = req.url = '/js/main.js';
middleware(req, null, next);
test.equal(req.url, expected);
test.ok(next.called);
test.done();
};
tests['should test rewrite rules'] = function(test) {

@@ -148,0 +174,0 @@ req.url = '/socer';

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc