Skip to content

Commit

Permalink
chore: remove bluebird dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Sep 10, 2024
1 parent 2a96a2a commit f6b43a3
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 134 deletions.
139 changes: 66 additions & 73 deletions lib/proxy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ const http = require('http'),
LRUCache = require('lru-cache'),
routeCache = new LRUCache({ max: 5000 }),
safe = require('safetimeout'),
letsencrypt = require('./letsencrypt.js'),
Promise = require('bluebird');
letsencrypt = require('./letsencrypt.js');

const ONE_DAY = 60 * 60 * 24 * 1000;
const ONE_MONTH = ONE_DAY * 30;
Expand Down Expand Up @@ -185,27 +184,23 @@ export function Redbird(opts) {
}

Redbird.prototype.setupHttpProxy = function (proxy, websocketsUpgrade, log, opts) {
const _this = this;
const httpServerModule = opts.serverModule || http;
const server = (this.server = httpServerModule.createServer(function (req, res) {
const src = _this._getSource(req);
_this
._getTarget(src, req, res)
.bind(_this)
.then(function (target) {
if (target) {
if (shouldRedirectToHttps(_this.certs, src, target, _this)) {
redirectToHttps(req, res, target, opts.ssl, log);
} else {
proxy.web(req, res, {
target: target,
secure: !proxy.options || proxy.options.secure !== false,
});
}
const server = (this.server = httpServerModule.createServer((req, res) => {
const src = this._getSource(req);
this._getTarget(src, req, res).then((target) => {
if (target) {
if (shouldRedirectToHttps(this.certs, src, target, this)) {
redirectToHttps(req, res, target, opts.ssl, log);
} else {
respondNotFound(req, res);
proxy.web(req, res, {
target: target,
secure: !proxy.options || proxy.options.secure !== false,
});
}
});
} else {
respondNotFound(req, res);
}
});
}));

//
Expand Down Expand Up @@ -628,69 +623,67 @@ Redbird.buildRoute = function (route) {
Redbird.prototype._getTarget = function (src, req, res) {
const url = req.url;

return this.resolve(src, url, req)
.bind(this)
.then(function (route) {
if (!route) {
this.log && this.log.warn({ src: src, url: url }, 'no valid route found for given source');
return;
}

const pathname = route.path;
if (pathname.length > 1) {
//
// remove prefix from src
//
req._url = url; // save original url
req.url = url.substr(pathname.length) || '';
}
return this.resolve(src, url, req).then((route) => {
if (!route) {
this.log && this.log.warn({ src: src, url: url }, 'no valid route found for given source');
return;
}

const pathname = route.path;
if (pathname.length > 1) {
//
// Perform Round-Robin on the available targets
// TODO: if target errors with EHOSTUNREACH we should skip this
// target and try with another.
// remove prefix from src
//
const urls = route.urls;
const j = route.rr;
route.rr = (j + 1) % urls.length; // get and update Round-robin index.
const target = route.urls[j];
req._url = url; // save original url
req.url = url.substr(pathname.length) || '';
}

//
// Fix request url if targetname specified.
//
if (target.pathname) {
if (req.url) {
req.url = path.posix.join(target.pathname, req.url);
} else {
req.url = target.pathname;
}
}
//
// Perform Round-Robin on the available targets
// TODO: if target errors with EHOSTUNREACH we should skip this
// target and try with another.
//
const urls = route.urls;
const j = route.rr;
route.rr = (j + 1) % urls.length; // get and update Round-robin index.
const target = route.urls[j];

//
// Host headers are passed through from the source by default
// Often we want to use the host header of the target instead
//
if (target.useTargetHostHeader === true) {
req.host = target.host;
//
// Fix request url if targetname specified.
//
if (target.pathname) {
if (req.url) {
req.url = path.posix.join(target.pathname, req.url);
} else {
req.url = target.pathname;
}
}

if (route.opts.onRequest) {
const resultFromRequestHandler = route.opts.onRequest(req, res, target);
if (resultFromRequestHandler !== undefined) {
this.log &&
this.log.info(
'Proxying %s received result from onRequest handler, returning.',
src + url
);
return resultFromRequestHandler;
}
//
// Host headers are passed through from the source by default
// Often we want to use the host header of the target instead
//
if (target.useTargetHostHeader === true) {
req.host = target.host;
}

if (route.opts.onRequest) {
const resultFromRequestHandler = route.opts.onRequest(req, res, target);
if (resultFromRequestHandler !== undefined) {
this.log &&
this.log.info(
'Proxying %s received result from onRequest handler, returning.',
src + url
);
return resultFromRequestHandler;
}
}

this.log &&
this.log.info('Proxying %s to %s', src + url, path.posix.join(target.host, req.url));
this.log &&
this.log.info('Proxying %s to %s', src + url, path.posix.join(target.host, req.url));

return target;
});
return target;
});
};

Redbird.prototype._getSource = function (req) {
Expand Down
3 changes: 0 additions & 3 deletions lib/redis-backend.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';

var redis = require('redis');
var Promise = require('bluebird');
var _ = require('lodash');

Promise.promisifyAll(redis);

/**
Instantiates a Redis Redbird backend.
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@
},
"homepage": "https://github.com/OptimalBits/redbird",
"dependencies": {
"bluebird": "^2.11.0 || ^3.7.2",
"dolphin": "*",
"greenlock": "^2.8.8",
"greenlock": "^2.8.9",
"http-proxy": "^1.18.0",
"le-challenge-fs": "^2.0.9",
"le-store-certbot": "^2.2.3",
"lodash": "^4.17.15",
"lru-cache": "^5.1.1",
"node-etcd": "^7.0.0",
"object-hash": "^1.3.1",
"pino": "^5.15.0",
"pino": "^9.4.0",
"safetimeout": "^0.1.2",
"spdy": "^4.0.1",
"spdy": "^4.0.2",
"valid-url": "^1.0.9"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit f6b43a3

Please sign in to comment.