Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

combo-url-parser

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

combo-url-parser - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

89

lib/middleware.js
var parser = require('./parser.js');
var request = require('request');
var CombinedStream = require('combined-stream');
var mime = require('mime');
var async = require('async');
/**

@@ -11,24 +11,73 @@ * Middleware for connect

module.exports = function () {
return function (req, res, next) {
var urls = parser(req.url);
if (!urls) {
next();
return;
}
var headers = ['content-type', 'vary'].map(function (key) {
return [key, key.split('-').map(function (word) {
return word[0].toUpperCase() + word.substr(1)
}).join('-')]
});
// http://stackoverflow.com/questions/7109732/express-setting-content-type-based-on-path-file
var type = mime.lookup(urls[0]);
return function (req, res, next) {
var urls = parser(req.url);
if (!res.getHeader('content-type')) {
res.setHeader('Content-Type', type + '; charset=UTF-8');
if (!urls) {
next();
return;
}
async.mapLimit(urls, 8, function (url, cb) {
var fullURL = 'http://' + req.headers.host + url;
request({
url: fullURL,
encoding: null
}, function (err, resp, body) {
if (err) {
cb(err);
return;
}
if (resp.statusCode === 404) {
var error = new Error('404');
error.url = fullURL;
cb(error);
}
cb(null, {response: resp, body: body});
});
}, function (err, results) {
if (err) {
if (err.message === '404') {
err.statsCde = 404;
res.end('Sorry, Can not found '+ err.url);
} else {
next(err);
}
return;
}
var response = results[0].response;
headers.forEach(function (key) {
var v = response.headers[key[0]];
if (v) {
res.setHeader(key[1], v);
}
});
res.setHeader('Access-Control-Allow-Origin', '*');
var newline = new Buffer('\n');
res.end(Buffer.concat(results.reduce(function(all, result){
if (all.length) {
all.push(newline);
}
all.push(result.body);
return all;
}, [])));
});
}
var combinedStream = CombinedStream.create();
urls.forEach(function (path) {
combinedStream.append(request('http://' + req.headers.host + path));
combinedStream.append('\n');
});
combinedStream.pipe(res);
}
};

@@ -5,3 +5,3 @@ {

"description": "A util to parser comboed url",
"version": "0.1.2",
"version": "0.1.3",
"repository": {

@@ -18,5 +18,4 @@ "type": "git",

"dependencies": {
"combined-stream": "0.0.4",
"request": "~2.34.0",
"mime": "~1.2.11"
"async": "~0.7.0"
},

@@ -23,0 +22,0 @@ "devDependencies": {

@@ -27,3 +27,9 @@ [![build status](https://secure.travis-ci.org/maxbbn/combo-url-parser.png)](http://travis-ci.org/maxbbn/combo-url-parser)

### 0.1.3
- 中间件,去掉配置,现在不需要配置 port
- 优化逻辑,如果一个文件不存在,就返回 404, 方便问题定件
- 使用第一个请求的 mime-type
### 0.1.2
- 添加 content-type Header

@@ -30,0 +36,0 @@

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