diff --git a/hl-tests/64/proxy.js b/hl-tests/64/proxy.js index 3befec6..84ad31b 100644 --- a/hl-tests/64/proxy.js +++ b/hl-tests/64/proxy.js @@ -49,7 +49,5 @@ http res.writeHead(200); res.write(req.url); res.end(); - - console.log(req.host); }) .listen(3000); diff --git a/index.js b/index.mjs similarity index 100% rename from index.js rename to index.mjs diff --git a/lib/docker.mjs b/lib/docker.mjs index c19e36c..594ebdb 100644 --- a/lib/docker.mjs +++ b/lib/docker.mjs @@ -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*") @@ -163,4 +163,3 @@ function containerPort(dolphin, containerId) { }); } -module.exports = DockerModule; diff --git a/lib/etcd-backend.mjs b/lib/etcd-backend.mjs index 7677d6f..98b0823 100644 --- a/lib/etcd-backend.mjs +++ b/lib/etcd-backend.mjs @@ -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); } @@ -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 { @@ -86,5 +86,3 @@ function ETCDModule(redbird, options) { }); } } - -module.exports = ETCDModule; diff --git a/lib/letsencrypt.js b/lib/letsencrypt.mjs similarity index 97% rename from lib/letsencrypt.js rename to lib/letsencrypt.mjs index 0e30efb..f0de0a1 100644 --- a/lib/letsencrypt.js +++ b/lib/letsencrypt.mjs @@ -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 }; diff --git a/lib/proxy.mjs b/lib/proxy.mjs index eb26c5c..48c02e8 100755 --- a/lib/proxy.mjs +++ b/lib/proxy.mjs @@ -1,19 +1,22 @@ /*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 { LRUCache } from 'lru-cache'; + +const routeCache = new LRUCache({ max: 5000 }); + +import tls from 'tls'; const ONE_DAY = 60 * 60 * 24 * 1000; const ONE_MONTH = ONE_DAY * 30; @@ -44,8 +47,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(); } @@ -247,7 +250,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]); @@ -450,7 +453,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; @@ -728,15 +731,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'); + } }; // @@ -769,7 +775,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); @@ -812,7 +818,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; @@ -821,13 +827,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 = {}; diff --git a/package.json b/package.json index aa8d57b..6f2a385 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "le-challenge-fs": "^2.0.9", "le-store-certbot": "^2.2.3", "lodash": "^4.17.15", - "lru-cache": "^5.1.1", + "lru-cache": "^11.0.1", "node-etcd": "^7.0.0", "object-hash": "^1.3.1", "pino": "^9.4.0", diff --git a/test/custom_resolver.spec.js b/test/custom_resolver.spec.js index 0d21ece..61875fa 100644 --- a/test/custom_resolver.spec.js +++ b/test/custom_resolver.spec.js @@ -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 = { diff --git a/test/hostheader.spec.js b/test/hostheader.spec.js index e46c753..9230e96 100644 --- a/test/hostheader.spec.js +++ b/test/hostheader.spec.js @@ -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'; diff --git a/test/onrequest.spec.js b/test/onrequest.spec.js index 60a4a54..0daeec4 100644 --- a/test/onrequest.spec.js +++ b/test/onrequest.spec.js @@ -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'; diff --git a/test/pathnames.spec.js b/test/pathnames.spec.js index 1ec5e34..1db9886 100644 --- a/test/pathnames.spec.js +++ b/test/pathnames.spec.js @@ -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; diff --git a/test/register.spec.js b/test/register.spec.js index b5e295b..2358f2e 100644 --- a/test/register.spec.js +++ b/test/register.spec.js @@ -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 = { diff --git a/yarn.lock b/yarn.lock index 4787e05..3c36000 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1971,12 +1971,10 @@ 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" +lru-cache@^11.0.1: + version "11.0.1" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-11.0.1.tgz#3a732fbfedb82c5ba7bca6564ad3f42afcb6e147" + integrity sha512-CgeuL5uom6j/ZVrg7G/+1IXqRY8JXX4Hghfy5YE0EhoYQWvndP1kufu58cmZLNIDKnRhZrXfdS9urVWx98AipQ== magic-string@^0.30.10: version "0.30.11" @@ -2124,7 +2122,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" @@ -3215,8 +3213,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==