@everymundo/em-eureka
Advanced tools
Comparing version 0.1.1 to 1.0.0
70
index.js
@@ -8,18 +8,37 @@ 'use strict'; | ||
const generateAppName = () => { | ||
const appPackageJson = require(`${process.cwd()}/package.json`); | ||
const { name, version } = appPackageJson; | ||
const majorVersionNumber = version.split('.')[0]; | ||
const appName = `${name}-V${majorVersionNumber}`; | ||
return appName; | ||
}; | ||
const generateInstanceId = (appName, ipAddr) => { | ||
const instanceId = `${appName}:${ipAddr}`; | ||
return instanceId; | ||
}; | ||
const coalesce = (...args) => args.reduce((a, b) => a || b); | ||
const createConfig = ({ instanceId, app, hostName, ipAddr, port, securePort, vipAddress, dataCenterInfoName, eureka = {} }) => { | ||
assert(app && /^\w[-\w]{2,}$/.test(app), `Invalid app name [${app}]`); | ||
assert(port && /^[-\w]{3,}$/.test(app), `Invalid port [${port}]`); | ||
assert(port ? /^\d+$/.test(port) : true, `Invalid port [${port}]`); | ||
const appName = coalesce(app, generateAppName()); | ||
ipAddr = coalesce(ipAddr, ip.address()); | ||
const eurekaConfig = { | ||
instance: { | ||
instanceId, | ||
app, | ||
hostName: coalesce(hostName, 'localhost'), | ||
ipAddr: coalesce(ipAddr, ip.address()), | ||
instanceId: coalesce(instanceId, generateInstanceId(appName, ipAddr)), | ||
app: appName, | ||
hostName: coalesce(hostName, 'localhost'), | ||
ipAddr, | ||
// statusPageUrl, | ||
port: { $: coalesce(Math.abs(port), 9080), '@enabled': 'true' }, | ||
securePort: { $: coalesce(Math.abs(securePort), 9443), '@enabled': (securePort !== undefined).toString() }, | ||
vipAddress: coalesce(vipAddress, ipAddr, 'localhost'), | ||
vipAddress: coalesce(vipAddress, ipAddr), | ||
dataCenterInfo: { | ||
@@ -33,10 +52,6 @@ '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', | ||
port: coalesce(Math.abs(eureka.port), 8080), | ||
servicePath: '/eureka/v2/apps/', | ||
servicePath: coalesce(eureka.servicePath, '/eureka/v2/apps/'), | ||
}, | ||
}; | ||
if (!eurekaConfig.instance.instanceId) { | ||
eurekaConfig.instance.instanceId = `${eurekaConfig.instance.ipAddr}:${eurekaConfig.instance.port.$}`; | ||
} | ||
return eurekaConfig; | ||
@@ -68,4 +83,18 @@ }; | ||
// emCli.create({app:'myAppName', hostName:, ipAddr, port, vipAddress, eureka}) | ||
/* eslint-disable no-shadow */ | ||
const getEurekaFromConfigService = async () => { | ||
const url = require('url'); | ||
// expects the Config Service URL to be set on process.env.SPRING_CLOUD_CONFIG_URI | ||
const { getConfigsAsync } = require('@everymundo/config-service-client'); | ||
const { parsed } = await getConfigsAsync(); | ||
const { serviceUrl } = parsed.eureka.client; | ||
const eurekaUrls = Object.keys(serviceUrl).map(_ => serviceUrl[_]); | ||
const { hostname, port/* , pathname */ } = url.parse(eurekaUrls[0]); | ||
const config = { host: hostname, port }; | ||
return config; | ||
}; | ||
const createClient = ({ config, app, hostName, ipAddr, port, vipAddress, eureka, instanceId }, start = true) => { | ||
@@ -79,4 +108,9 @@ const xConfig = createConfig({ app, hostName, ipAddr, port, vipAddress, eureka, instanceId }); | ||
}; | ||
/* eslint-enable no-shadow */ | ||
const asyncClientFromConfigService = async ({ config, app, hostName, ipAddr, port, vipAddress, instanceId }, start) => { | ||
const eureka = await getEurekaFromConfigService(); | ||
return createClient({ config, app, hostName, ipAddr, port, vipAddress, eureka, instanceId }, start); | ||
}; | ||
module.exports = { | ||
@@ -87,2 +121,8 @@ EmEureka: { | ||
}, | ||
createConfig, | ||
createClient, | ||
asyncClientFromConfigService, | ||
getEurekaFromConfigService, | ||
generateInstanceId, | ||
generateAppName, | ||
}; |
{ | ||
"name": "@everymundo/em-eureka", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "wrapper around eureka-js-client for everymundo microservice apps", | ||
@@ -18,10 +18,11 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@everymundo/cleanrequire": "^1.0.0", | ||
"@everymundo/cleanrequire": "^1.1.1", | ||
"chai": "^4.1.2", | ||
"istanbul": "^1.1.0-alpha.1", | ||
"mocha": "^5.0.0", | ||
"sinon": "^4.2.2" | ||
"mocha": "^5.0.4", | ||
"sinon": "^4.4.2" | ||
}, | ||
"dependencies": { | ||
"eureka-js-client": "^4.4.0", | ||
"@everymundo/config-service-client": "^1.0.0", | ||
"eureka-js-client": "^4.4.1", | ||
"ip": "^1.1.5", | ||
@@ -28,0 +29,0 @@ "request-promise": "^4.2.2", |
'require strict'; | ||
/* eslint-disable no-unused-expressions */ | ||
const | ||
@@ -9,3 +11,3 @@ sinon = require('sinon'), | ||
describe('cluster.js', () => { | ||
describe('index.js', () => { | ||
const eurekaJsClient = require('eureka-js-client'); | ||
@@ -15,7 +17,12 @@ const loadResilient = require('../load-resilient'); | ||
let box; | ||
beforeEach(() => { box = sinon.sandbox.create(); }); | ||
// retores the sandbox | ||
afterEach(() => { box.restore(); }); | ||
describe('#createClient', () => { | ||
let startCalled; | ||
let instancesById; | ||
function FakeEureka() { | ||
this.start = () => startCalled(); | ||
this.start = sinon.spy(() => {}); | ||
this.getInstancesByAppId = () => instancesById; | ||
@@ -29,9 +36,6 @@ } | ||
let box; | ||
beforeEach(() => { | ||
// creates sinon sandbox | ||
box = sinon.sandbox.create(); | ||
box.stub(eurekaJsClient, 'Eureka').value(FakeEureka); | ||
box.stub(loadResilient, 'Resilient').callsFake(FakeResilient); | ||
startCalled = () => { }; | ||
setServersCalled = () => { }; | ||
@@ -48,9 +52,4 @@ instancesById = [{ | ||
// retores the sandbox | ||
afterEach(() => { box.restore(); }); | ||
it('should be a function', () => { | ||
const { EmEureka } = require('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = require('../index'); | ||
@@ -61,6 +60,5 @@ expect(createClient).to.be.instanceof(Function); | ||
it('should return an instance of Eureka', () => { | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = cleanrequire('../index'); | ||
const res = createClient({app: 'valid', port: 80}); | ||
const res = createClient({app: 'valid', port: 80}, false); | ||
@@ -72,6 +70,5 @@ expect(res).to.be.instanceof(FakeEureka); | ||
it('should return an instance of Eureka with a method named app', () => { | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = require('../index'); | ||
const res = createClient({ app: 'valid', port: 80 }); | ||
const res = createClient({ app: 'valid', port: 80 }, false); | ||
@@ -86,6 +83,5 @@ expect(res).to.be.instanceof(FakeEureka); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = require('../index'); | ||
const client = createClient({ app: 'valid', port: 80 }); | ||
const client = createClient({ app: 'valid', port: 80 }, false); | ||
@@ -99,6 +95,5 @@ const func = () => client.app('INVALID'); | ||
setServersCalled = sinon.spy(() => {}); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = cleanrequire('../index'); | ||
const res = createClient({ app: 'valid', port: 80 }); | ||
const res = createClient({ app: 'valid', port: 80 }, false); | ||
@@ -118,6 +113,5 @@ expect(res).to.have.property('app'); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = cleanrequire('../index'); | ||
const client = createClient({ app: 'valid', port: 80 }); | ||
const client = createClient({ app: 'valid', port: 80 }, false); | ||
@@ -135,7 +129,7 @@ const resilient = client.app('appName'); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = cleanrequire('../index'); | ||
const expected = {timestamp: Date.now()}; | ||
const client = createClient({ app: 'valid', port: 80 }); | ||
const client = createClient({ app: 'valid', port: 80 }, false); | ||
@@ -154,21 +148,17 @@ expect(client).to.have.property('app'); | ||
it('should call start when not told otherwise', () => { | ||
startCalled = sinon.spy(() => {}); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = require('../index'); | ||
const res = createClient({app: 'valid', port: 80}); | ||
const eurekaCli = createClient({app: 'valid', port: 80}); | ||
expect(res).to.be.instanceof(FakeEureka); | ||
expect(startCalled).to.have.property('calledOnce', true); | ||
expect(eurekaCli).to.be.instanceof(FakeEureka); | ||
expect(eurekaCli.start).to.have.property('calledOnce', true); | ||
}); | ||
it('should NOT call start when told not to', () => { | ||
startCalled = sinon.spy(() => { }); | ||
const { EmEureka } = cleanrequire('../index'); | ||
const { createClient } = EmEureka; | ||
const { createClient } = require('../index'); | ||
const res = createClient({app: 'valid', port: 80}, false); | ||
const eurekaCli = createClient({app: 'valid', port: 80}, false); | ||
expect(res).to.be.instanceof(FakeEureka); | ||
expect(startCalled).to.have.property('calledOnce', false); | ||
expect(eurekaCli).to.be.instanceof(FakeEureka); | ||
expect(eurekaCli.start).to.have.property('calledOnce', false); | ||
}); | ||
@@ -178,6 +168,4 @@ }); | ||
describe('#createConfig', () => { | ||
const { EmEureka } = require('../index'); | ||
it('should be a function', () => { | ||
const { createConfig } = EmEureka; | ||
const { createConfig } = require('../index'); | ||
@@ -187,20 +175,36 @@ expect(createConfig).to.be.instanceof(Function); | ||
it('should throw AssertionError', () => { | ||
const { createConfig } = EmEureka; | ||
context('* port parameter', () => { | ||
context('When not passing one', () => { | ||
it('should not throw an AssertionError', () => { | ||
const { createConfig } = require('../index'); | ||
const func = () => createConfig({instanceId: 1}); | ||
const func = () => createConfig({}); | ||
expect(func).to.throw(AssertionError, 'Invalid app name'); | ||
}); | ||
expect(func).to.not.throw(AssertionError); | ||
}); | ||
}); | ||
it('should throw AssertionError', () => { | ||
const { createConfig } = EmEureka; | ||
context('When passing a valid one', () => { | ||
it('should not throw an AssertionError', () => { | ||
const { createConfig } = require('../index'); | ||
const func = () => createConfig({instanceId: 1, app: 'name'}); | ||
const func = () => createConfig({instanceId: 1}); | ||
expect(func).to.throw(AssertionError); | ||
expect(func).to.not.throw(AssertionError); | ||
}); | ||
}); | ||
context('When passing a INVALID one', () => { | ||
it('should throw AssertionError', () => { | ||
const { createConfig } = require('../index'); | ||
const func = () => createConfig({port: 'NaN'}); | ||
expect(func).to.throw(AssertionError, 'Invalid port'); | ||
}); | ||
}); | ||
}); | ||
it('should return an object', () => { | ||
const { createConfig } = EmEureka; | ||
it('should return the correct object', () => { | ||
const { createConfig } = require('../index'); | ||
@@ -223,3 +227,3 @@ const res = createConfig({instanceId: 1, app:'valid', port: 10000}); | ||
}, | ||
vipAddress: 'localhost', | ||
vipAddress: ip, | ||
dataCenterInfo: { | ||
@@ -240,5 +244,50 @@ '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', | ||
context('When no argument are passed', () => { | ||
beforeEach(() => { | ||
box.stub(process, 'cwd').callsFake(() => `${__dirname}/support`); | ||
}); | ||
it('should return the correct object', () => { | ||
const { createConfig } = require('../index'); | ||
const {name, version} = require('./support/package.json'); | ||
expect(name).to.equal('my-test-project', 'Test package.json name mismatch'); | ||
expect(version).to.equal('5.4.3', 'Test package.json version mismatch'); | ||
const res = createConfig({}); | ||
// console.log({res}); | ||
const expected = { | ||
instance: { | ||
instanceId: `my-test-project-V5:${ip}`, | ||
app: 'my-test-project-V5', | ||
hostName: 'localhost', | ||
ipAddr: ip, | ||
port: { | ||
$: 9080, | ||
'@enabled': 'true', | ||
}, | ||
securePort: { | ||
$: 9443, | ||
'@enabled': 'false', | ||
}, | ||
vipAddress: ip, | ||
dataCenterInfo: { | ||
'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo', | ||
name: 'MyOwn', | ||
}, | ||
}, | ||
eureka: { | ||
host: 'localhost', | ||
port: 8080, | ||
servicePath: '/eureka/v2/apps/', | ||
}, | ||
}; | ||
expect(res).to.deep.equal(expected); | ||
}); | ||
}); | ||
context('Whenever instanceId is undefined', () => { | ||
it('should return an object', () => { | ||
const { createConfig } = EmEureka; | ||
const { createConfig } = require('../index'); | ||
@@ -250,3 +299,3 @@ const res = createConfig({ instanceId: undefined, ipAddr:'127.0.0.1', app:'valid', port: 10000}); | ||
instance: { | ||
instanceId: '127.0.0.1:10000', | ||
instanceId: 'valid:127.0.0.1', | ||
app: 'valid', | ||
@@ -282,5 +331,11 @@ hostName: 'localhost', | ||
it('should return an object with securePort.@enabled === true', () => { | ||
const { createConfig } = EmEureka; | ||
const { createConfig } = require('../index'); | ||
const res = createConfig({ instanceId: undefined, ipAddr:'127.0.0.1', app:'valid', port: 10000}); | ||
const | ||
instanceId = undefined, | ||
ipAddr = '127.0.0.1', | ||
app = 'my-app-V1', | ||
port = 10000; | ||
const res = createConfig({ instanceId, ipAddr, app, port }, true, 'FUCK'); | ||
// console.log(JSON.stringify({res}, null, 2)); | ||
@@ -290,4 +345,4 @@ expect(res).to.be.instanceof(Object); | ||
instance: { | ||
instanceId: '127.0.0.1:10000', | ||
app: 'valid', | ||
instanceId: `${app}:${ipAddr}`, | ||
app, | ||
hostName: 'localhost', | ||
@@ -320,2 +375,102 @@ ipAddr: '127.0.0.1', | ||
}); | ||
describe('#getEurekaFromConfigService', () => { | ||
const cfgClientLib = require('@everymundo/config-service-client'); | ||
const fareConfig = { parsed: { eureka: { client: { serviceUrl: { any: 'http://eureka.test.com:1000/eureka' } } } } }; | ||
beforeEach(() => { | ||
box.stub(cfgClientLib, 'getConfigsAsync').callsFake(() => fareConfig); | ||
}); | ||
it('should return proper {host, port} object', () => { | ||
const { getEurekaFromConfigService } = require('../index'); | ||
return getEurekaFromConfigService() | ||
.then((config) => { | ||
const expected = { host: 'eureka.test.com', port: '1000' }; | ||
expect(config).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
describe('#generateInstanceId', () => { | ||
const fakeIp = '10.11.12.13'; | ||
const ipLib = require('ip'); | ||
beforeEach(() => { | ||
box.stub(ipLib, 'address').callsFake(() => fakeIp); | ||
}); | ||
const { generateInstanceId } = require('../index'); | ||
it('should create a correct instanceId based on the projects package.json', () => { | ||
const res = generateInstanceId('my-test-project-V5', fakeIp); | ||
const expected = `my-test-project-V5:${fakeIp}`; | ||
expect(res).to.equal(expected); | ||
}); | ||
}); | ||
describe('#generateAppName', () => { | ||
beforeEach(() => { | ||
box.stub(process, 'cwd').callsFake(() => `${__dirname}/support`); | ||
}); | ||
const { generateAppName } = require('../index'); | ||
it('should create a correct appName based on the projects package.json', () => { | ||
const res = generateAppName(); | ||
const expected = 'my-test-project-V5'; | ||
expect(res).to.equal(expected); | ||
}); | ||
it('should call process.cwd', () => { | ||
generateAppName(); | ||
expect(process.cwd).to.have.property('calledOnce', true); | ||
}); | ||
}); | ||
describe('#getEurekaFromConfigService', () => { | ||
const cfgClientLib = require('@everymundo/config-service-client'); | ||
const fareConfig = {parsed: {eureka:{ client: {serviceUrl: {any: 'http://eureka.test.com:1000/eureka'}}}}}; | ||
beforeEach(() => { | ||
box.stub(cfgClientLib, 'getConfigsAsync').callsFake(() => fareConfig); | ||
}); | ||
it('should return proper {host, port} object', () => { | ||
const { getEurekaFromConfigService } = require('../index'); | ||
return getEurekaFromConfigService() | ||
.then((config) => { | ||
const expected = {host: 'eureka.test.com', port: '1000'}; | ||
expect(config).to.deep.equal(expected); | ||
}); | ||
}); | ||
}); | ||
describe('#asyncClientFromConfigService', () => { | ||
const cfgClientLib = require('@everymundo/config-service-client'); | ||
const fareConfig = {parsed: {eureka:{ client: {serviceUrl: {any: 'http://eureka.test.com:1000/eureka'}}}}}; | ||
beforeEach(() => { | ||
box.stub(cfgClientLib, 'getConfigsAsync').callsFake(() => fareConfig); | ||
}); | ||
it('should return proper {host, port} object', () => { | ||
const { Eureka } = cleanrequire('eureka-js-client'); | ||
const { asyncClientFromConfigService } = cleanrequire('../index'); | ||
return asyncClientFromConfigService({}, false) | ||
.then((eurekaCli) => { | ||
expect(eurekaCli).to.be.instanceof(Eureka); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
20395
7
453
1
5
2
+ Added@everymundo/config-service-client@1.2.0(transitive)
+ Added@everymundo/generate-microservice-name@1.1.2(transitive)
+ Added@everymundo/global-root-dir@1.1.1(transitive)
+ Added@everymundo/json-utils@1.1.1(transitive)
+ Added@everymundo/promise-data-to@1.4.2(transitive)
+ Added@everymundo/simple-logr@1.2.2(transitive)
Updatedeureka-js-client@^4.4.1