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

@mutable/proxy

Package Overview
Dependencies
Maintainers
7
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mutable/proxy - npm Package Compare versions

Comparing version 2.0.4 to 2.1.0

static/401.html

6

package.json
{
"name": "@mutable/proxy",
"version": "2.0.4",
"version": "2.1.0",
"description": "Proxy",

@@ -8,2 +8,6 @@ "author": "pellepelle3",

"main": "src/index.js",
"repository": {
"type": "git",
"url": "git@github.com:mutable/node-proxy.git"
},
"dependencies": {

@@ -10,0 +14,0 @@ "@mutable/meta": "^1.3.0",

84

src/proxy.js

@@ -14,2 +14,3 @@ const fs = require('fs');

const defaultPage404 = fs.readFileSync(path.join(__dirname, '../static/404.html')).toString();
const defaultPage401 = fs.readFileSync(path.join(__dirname, '../static/401.html')).toString();

@@ -78,3 +79,5 @@ function TimeTrack(req, label) {

class Proxy {
constructor(config) {
constructor(config, checkAuth) {
if (checkAuth && typeof checkAuth === 'function') this._checkAuth = checkAuth;
if (config) this.config = config;
this._proxy = httpProxy.createProxyServer({ ws: true, xfwd: false });

@@ -90,2 +93,3 @@ this._proxy.on('error', (err, req, res) => this._onError(err, req, res));

this._page404 = config.page404 || defaultPage404;
this._page401 = config.page401 || defaultPage401;
this._routes.updateConfig(config);

@@ -123,7 +127,16 @@ }

_proxyWebSockets(req, socket, head, opt, cb) {
async _proxyWebSockets(req, socket, head, opt, cb) {
const url = Url.parse(opt.target);
req.url = url.path;
opt.target = Url.format({ protocol: url.protocol, host: url.host });
DebugPrint('_proxyWeb', opt.target + req.url);
DebugPrint('_proxyWebSockets', opt.target + req.url);
if (this._checkAuth) {
try {
await this._checkAuth(req, opt, this.config);
} catch (e) {
DebugPrint('_proxyWebSockets', e);
}
}
try {

@@ -134,3 +147,3 @@ SetXHeaders(req);

this._proxy.ws(req, socket, head, opt, (err) => {
return this._proxy.ws(req, socket, head, opt, (err) => {
if (err && err.code && err.code === 'ECONNREFUSED' && typeof cb === 'function') cb();

@@ -140,36 +153,45 @@ });

console.error('error:: _proxyWebSockets: ', e);
return e;
}
}
_proxyWeb(req, res, opt) {
if (IsObject(opt)) {
const url = Url.parse(opt.target);
req.headers.host = req.headers.host || '';
req.url = url.path;
url.pathname = '';
opt.target = Url.format({ protocol: url.protocol, host: url.host });
DebugPrint('_proxyWeb', opt.target + req.url);
async _proxyWeb(req, res, opt) {
if (!IsObject(opt)) return this._checkRoutes(req, res);
if (opt.redirect) {
res.writeHead(opt.statusCode || 302, {
location: Url.resolve(opt.target, req.url),
});
res.end();
} else if (opt.content) {
SendWeb(res, opt.content, opt.statusCode || 200);
} else {
const url = Url.parse(opt.target);
req.headers.host = req.headers.host || '';
req.url = url.path;
url.pathname = '';
opt.target = Url.format({ protocol: url.protocol, host: url.host });
DebugPrint('_proxyWeb', opt.target + req.url);
if (opt.redirect) {
res.writeHead(opt.statusCode || 302, {
location: Url.resolve(opt.target, req.url),
});
res.end();
} else if (opt.content) {
SendWeb(res, opt.content, opt.statusCode || 200);
} else {
if (this._checkAuth) {
try {
SetXHeaders(req);
if (opt.changeHost) req.headers.host = url.host;
DebugPrint('headers', req.headers);
TimeTrack(req, '_proxyWeb');
this._proxy.web(req, res, opt);
await this._checkAuth(req, opt, this.config);
} catch (e) {
TimeTrack(req, '_proxyWeb catch');
this._routePage404(res);
DebugPrint('_proxyWeb', e);
this._routePage401(req, res);
}
}
} else {
this._checkRoutes(req, res);
try {
SetXHeaders(req);
if (opt.changeHost) req.headers.host = url.host;
DebugPrint('headers', req.headers);
TimeTrack(req, '_proxyWeb');
this._proxy.web(req, res, opt);
} catch (e) {
TimeTrack(req, '_proxyWeb catch');
this._routePage404(res);
}
}
return res;
}

@@ -181,2 +203,6 @@

_routePage401(res) {
SendWeb(res, this._page401 || '401', 401);
}
_onError(err, req, res) {

@@ -183,0 +209,0 @@ TimeTrack(req, '_onError');

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