Skip to content

Commit

Permalink
chore: more cleanups and modularization
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Sep 11, 2024
1 parent f6b43a3 commit 2fc3ccd
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 55 deletions.
2 changes: 0 additions & 2 deletions hl-tests/64/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,5 @@ http
res.writeHead(200);
res.write(req.url);
res.end();

console.log(req.host);
})
.listen(3000);
File renamed without changes.
5 changes: 2 additions & 3 deletions lib/docker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
This module handles automatic regitration and de-registration of
services running on docker containers.
*/
var Dolphin = require('dolphin');
import Dolphin from 'dolphin';

function DockerModule(redbird, url) {
export function DockerModule(redbird, url) {
if (!(this instanceof DockerModule)) {
//TODO: should it return a new instance per redbird proxy?
//because every time we run -> docker(redbird).register("localhost", "tomcat*")
Expand Down Expand Up @@ -163,4 +163,3 @@ function containerPort(dolphin, containerId) {
});
}

module.exports = DockerModule;
8 changes: 3 additions & 5 deletions lib/etcd-backend.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Redbird ETCD Module
This module handles automatic proxy registration via etcd
*/
var Etcd = require('node-etcd');
import Etcd from 'node-etcd';

function ETCDModule(redbird, options) {
export function ETCDModule(redbird, options) {
if (!(this instanceof ETCDModule)) {
return new ETCDModule(redbird, options);
}
Expand Down Expand Up @@ -64,7 +64,7 @@ function ETCDModule(redbird, options) {
} else if (body.node.key && body.node.value && IsJsonString(body.node.value)) {
var config = JSON.parse(body.node.value);
if (typeof config.docker !== 'undefined') {
require('..')
require('../index.mjs')
.docker(_this.redbird)
.register(body.node.key, body.node.value.docker, body.node.value);
} else {
Expand All @@ -86,5 +86,3 @@ function ETCDModule(redbird, options) {
});
}
}

module.exports = ETCDModule;
6 changes: 4 additions & 2 deletions lib/letsencrypt.js → lib/letsencrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,7 @@ function getCertificates(domain, email, production, renew, logger) {
});
}

module.exports.init = init;
module.exports.getCertificates = getCertificates;
//module.exports.init = init;
//module.exports.getCertificates = getCertificates;

export { init, getCertificates };
51 changes: 27 additions & 24 deletions lib/proxy.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*eslint-env node */
'use strict';

const http = require('http'),
httpProxy = require('http-proxy'),
validUrl = require('valid-url'),
parseUrl = require('url').parse,
path = require('path'),
_ = require('lodash'),
pino = require('pino'),
cluster = require('cluster'),
hash = require('object-hash'),
LRUCache = require('lru-cache'),
routeCache = new LRUCache({ max: 5000 }),
safe = require('safetimeout'),
letsencrypt = require('./letsencrypt.js');
import http from 'http';
import httpProxy from 'http-proxy';
import validUrl from 'valid-url';
import { parse as parseUrl } from 'url';
import path from 'path';
import _ from 'lodash';
import pino from 'pino';
import cluster from 'cluster';
import hash from 'object-hash';
import safe from 'safetimeout';
import * as letsencrypt from './letsencrypt.mjs';

import tls from 'tls';

const ONE_DAY = 60 * 60 * 24 * 1000;
const ONE_MONTH = ONE_DAY * 30;
Expand Down Expand Up @@ -44,8 +44,8 @@ export function Redbird(opts) {
throw Error('cluster setting must be an integer less than 32');
}

if (opts.cluster && cluster.isMaster) {
for (const i = 0; i < opts.cluster; i++) {
if (opts.cluster && cluster.isPrimary) {
for (let i = 0; i < opts.cluster; i++) {
cluster.fork();
}

Expand Down Expand Up @@ -247,7 +247,7 @@ Redbird.prototype.setupHttpsProxy = function (proxy, websocketsUpgrade, log, ssl

const certs = this.certs;

const ssl = {
let ssl = {
SNICallback: function (hostname, cb) {
if (cb) {
cb(null, certs[hostname]);
Expand Down Expand Up @@ -450,7 +450,7 @@ Redbird.prototype.updateCertificates = function (domain, email, production, rene
//
// TODO: cluster friendly
//
const renewTime = certs.expiresAt - Date.now() - renewWithin;
let renewTime = certs.expiresAt - Date.now() - renewWithin;
renewTime =
renewTime > 0 ? renewTime : _this.opts.letsencrypt.minRenewTime || 60 * 60 * 1000;

Expand Down Expand Up @@ -728,15 +728,18 @@ Redbird.prototype.close = function () {
}
*/

const respondNotFound = function (req, res) {
let respondNotFound = function (req, res) {
res.statusCode = 404;
res.write('Not Found');
res.end();
};

Redbird.prototype.notFound = function (callback) {
if (typeof callback == 'function') respondNotFound = callback;
else throw Error('notFound callback is not a function');
if (typeof callback == 'function') {
respondNotFound = callback;
} else {
throw Error('notFound callback is not a function');
}
};

//
Expand Down Expand Up @@ -769,7 +772,7 @@ function prepareUrl(url) {
url = setHttp(url);

if (!validUrl.isHttpUri(url) && !validUrl.isHttpsUri(url)) {
throw Error('uri is not a valid http uri ' + url);
throw Error(`uri is not a valid http uri ${url}`);
}

url = parseUrl(url);
Expand Down Expand Up @@ -812,7 +815,7 @@ function unbundleCert(bundle) {
const ca = [];
const cert = [];

for (const i = 0, len = chain.length; i < len; i++) {
for (let i = 0, len = chain.length; i < len; i++) {
const line = chain[i].trim();
if (!(line.length !== 0)) {
continue;
Expand All @@ -821,13 +824,13 @@ function unbundleCert(bundle) {
if (line.match(/-END CERTIFICATE-/)) {
const joined = cert.join('\n');
ca.push(joined);
cert = [];
//cert = [];
cert.length = 0;
}
}
return ca;
}

const tls = require('tls');
function createCredentialContext(key, cert, ca) {
const opts = {};

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"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": "^9.4.0",
Expand Down
2 changes: 1 addition & 1 deletion test/custom_resolver.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { Redbird } from '../index.mjs'; // Adjust the import path if necessary
import { expect } from 'chai';

const opts = {
Expand Down
2 changes: 1 addition & 1 deletion test/hostheader.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { Redbird } from '../index.mjs'; // Adjust the import path if necessary
import { expect } from 'chai';
import { createServer } from 'http';
import fetch from 'node-fetch';
Expand Down
2 changes: 1 addition & 1 deletion test/onrequest.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { Redbird } from '../index.mjs'; // Adjust the import path if necessary
import { expect } from 'chai';
import fetch from 'node-fetch';
import { createServer } from 'http';
Expand Down
2 changes: 1 addition & 1 deletion test/pathnames.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { createServer, get } from 'http';
import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { Redbird } from '../index.mjs'; // Adjust the import path if necessary
import { expect } from 'chai';

const TEST_PORT = 54673;
Expand Down
2 changes: 1 addition & 1 deletion test/register.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import { describe, it, expect } from 'vitest';
import { Redbird } from '../'; // Adjust the import path if necessary
import { Redbird } from '../index.mjs'; // Adjust the import path if necessary
import { expect } from 'chai';

const opts = {
Expand Down
14 changes: 1 addition & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1971,13 +1971,6 @@ lru-cache@2:
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz"
integrity sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz"
integrity sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==
dependencies:
yallist "^3.0.2"

magic-string@^0.30.10:
version "0.30.11"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.30.11.tgz#301a6f93b3e8c2cb13ac1a7a673492c0dfd12954"
Expand Down Expand Up @@ -2124,7 +2117,7 @@ node-domexception@^1.0.0:

node-etcd@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/node-etcd/-/node-etcd-7.0.0.tgz"
resolved "https://registry.yarnpkg.com/node-etcd/-/node-etcd-7.0.0.tgz#87416d56d44104049822ca242e47d01728a34ed3"
integrity sha512-kGnYVoxdDuUU2ojCt0GnZhR2wMRZWyJvq0OsWX+adExUbiX0z7D+8//nlv9Gnve1dIvNEQ/mvM+72aSKnWVp5Q==
dependencies:
deasync "^0.1.13"
Expand Down Expand Up @@ -3215,8 +3208,3 @@ xml-escape@~1.0.0:
version "4.0.1"
resolved "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz"
integrity sha1-pcbVMr5lbiPbgg77lDofBJmNY68=

yallist@^3.0.2:
version "3.1.1"
resolved "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz"
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==

0 comments on commit 2fc3ccd

Please sign in to comment.