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

@everymundo/em-eureka

Package Overview
Dependencies
Maintainers
19
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@everymundo/em-eureka - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

lib/get-docker-instance-id.js

61

index.js

@@ -8,4 +8,6 @@ 'use strict';

const { getMicroserviceName } = require('@everymundo/generate-microservice-name');
const logr = require('@everymundo/simple-logr');
const { awsMetadataToEureka } = require('./lib/aws-metadata-to-eureka');
const { coalesce } = require('./lib/coalesce');
const { getDockerInstanceId } = require('./lib/get-docker-instance-id');

@@ -30,3 +32,4 @@ const createDataCenterInfo = (awsIdentityInfo) => {

const generateInstanceId = (appName, ipAddr) => {
const instanceId = `${appName}:${ipAddr}`;
const dockerId = getDockerInstanceId();
const instanceId = `${appName}:${ipAddr}:${dockerId}`;

@@ -36,3 +39,17 @@ return instanceId;

const createConfig = ({ instanceId, app, hostName, ipAddr, port, securePort, vipAddress, dataCenterInfo, eureka = {}}) => {
const { parseEureka } = require('./lib/parse-eureka');
const createConfig = ({
instanceId,
app,
hostName,
ipAddr,
port,
securePort,
vipAddress,
dataCenterInfo,
statusPageUrl,
healthCheckUrl,
eureka = {},
}) => {
assert(port ? /^\d+$/.test(port) : true, `Invalid port [${port}]`);

@@ -44,2 +61,4 @@

port = coalesce(Math.abs(port), 9080);
const eurekaConfig = {

@@ -51,5 +70,5 @@ instance: {

ipAddr,
// statusPageUrl: 'http://__HOST__:8080/info',
// healthCheckUrl: 'http://__HOST__:8077/healthcheck',
port: { $: coalesce(Math.abs(port), 9080), '@enabled': 'true' },
statusPageUrl: coalesce(statusPageUrl, `http://${ipAddr}:${Math.abs(port)}/info`),
healthCheckUrl: coalesce(healthCheckUrl, `http://${ipAddr}:${Math.abs(port)}/healthcheck`),
port: { $: port, '@enabled': 'true' },
securePort: { $: coalesce(Math.abs(securePort), 9443), '@enabled': (securePort !== undefined).toString() },

@@ -59,7 +78,9 @@ vipAddress: coalesce(vipAddress, ipAddr),

},
eureka: {
host: coalesce(eureka.host, 'localhost'),
port: coalesce(Math.abs(eureka.port), 8080),
servicePath: coalesce(eureka.servicePath, '/eureka/v2/apps/'),
},
// eureka: {
// host: coalesce(eureka.host, 'localhost'),
// port: coalesce(Math.abs(eureka.port), 8080),
// serviceUrls: eureka.serviceUrls,
// servicePath: coalesce(eureka.servicePath, '/eureka/v2/apps/'),
// },
eureka: parseEureka(eureka),
};

@@ -94,3 +115,3 @@

const getEurekaFromConfigService = async (eureka = {}) => {
const url = require('url');
// const url = require('url');
// expects the Config Service URL to be set on process.env.SPRING_CLOUD_CONFIG_URI

@@ -101,7 +122,10 @@ const { getConfigsAsync } = require('@everymundo/config-service-client');

const { serviceUrl } = parsed.eureka.client;
const eurekaUrls = Object.keys(serviceUrl).map(_ => serviceUrl[_]);
const availabilityZones = Object.keys(serviceUrl);
const serviceUrls = {};
const { hostname, port/* , pathname */ } = url.parse(eurekaUrls[0]);
availabilityZones.forEach((zone) => {
serviceUrls[zone] = [serviceUrl[zone]];
});
const config = { host: hostname, port, ...eureka };
const config = { serviceUrls, availabilityZones, ...eureka };

@@ -124,5 +148,10 @@ return config;

if (!dataCenterInfo) {
const {getIdentityDocument} = require('@everymundo/aws-instance-identity');
const { getIdentityDocument } = require('@everymundo/aws-instance-identity');
const awsIdentityInfo = await getIdentityDocument();
let awsIdentityInfo;
try {
awsIdentityInfo = await getIdentityDocument();
} catch (e) {
logr.error(asyncClientFromConfigService.name, e);
}

@@ -129,0 +158,0 @@ dataCenterInfo = createDataCenterInfo(awsIdentityInfo);

{
"name": "@everymundo/em-eureka",
"version": "1.2.1",
"version": "1.3.0",
"description": "wrapper around eureka-js-client for everymundo microservice apps",

@@ -32,5 +32,6 @@ "main": "index.js",

"dependencies": {
"@everymundo/aws-instance-identity": "0.0.2",
"@everymundo/aws-instance-identity": "^0.1.1",
"@everymundo/config-service-client": "^1.1.1",
"@everymundo/generate-microservice-name": "^1.0.1",
"@everymundo/simple-logr": "^1.1.1",
"eureka-js-client": "^4.4.1",

@@ -37,0 +38,0 @@ "ip": "^1.1.5",

@@ -157,2 +157,10 @@ 'require strict';

describe('#createConfig', () => {
const fakeDockerId = '1011121314151617';
const dockerLib = require('../lib/get-docker-instance-id');
beforeEach(() => {
box.stub(dockerLib, 'getDockerInstanceId').callsFake(() => fakeDockerId);
});
it('should be a function', () => {

@@ -198,4 +206,4 @@ const { createConfig } = require('../index');

const { createConfig } = require('../index');
const res = createConfig({instanceId: 1, app:'valid', port: 10000});
const port = 10000;
const res = createConfig({instanceId: 1, app:'valid', port});
// console.log({res});

@@ -208,4 +216,6 @@ const expected = {

ipAddr: ip,
statusPageUrl: `http://${ip}:${port}/info`,
healthCheckUrl: `http://${ip}:${port}/healthcheck`,
port: {
$: 10000,
$: port,
'@enabled': 'true',

@@ -243,6 +253,8 @@ },

instance: {
instanceId: `em-eureka-v${majorV}:${ip}`,
instanceId: `em-eureka-v${majorV}:${ip}:${fakeDockerId}`,
app: `em-eureka-v${majorV}`,
hostName: 'localhost',
ipAddr: ip,
statusPageUrl: `http://${ip}:9080/info`,
healthCheckUrl: `http://${ip}:9080/healthcheck`,
port: {

@@ -277,3 +289,6 @@ $: 9080,

const res = createConfig({ instanceId: undefined, ipAddr:'127.0.0.1', app:'valid', port: 10000});
const ipAddr = '127.0.0.1';
const port = 10000;
const res = createConfig({ instanceId: undefined, ipAddr, app:'valid', port});
// console.log(JSON.stringify({res}, null, 2));

@@ -283,8 +298,10 @@ expect(res).to.be.instanceof(Object);

instance: {
instanceId: 'valid:127.0.0.1',
instanceId: `valid:127.0.0.1:${fakeDockerId}`,
app: 'valid',
hostName: 'localhost',
ipAddr: '127.0.0.1',
ipAddr,
statusPageUrl: `http://${ipAddr}:${port}/info`,
healthCheckUrl: `http://${ipAddr}:${port}/healthcheck`,
port: {
$: 10000,
$: port,
'@enabled': 'true',

@@ -315,3 +332,3 @@ },

it('should return an object with securePort.@enabled === true', () => {
const { createConfig } = require('../index');
const { createConfig } = cleanrequire('../index');

@@ -329,8 +346,10 @@ const

instance: {
instanceId: `${app}:${ipAddr}`,
instanceId: `${app}:${ipAddr}:${fakeDockerId}`,
app,
hostName: 'localhost',
ipAddr: '127.0.0.1',
ipAddr,
statusPageUrl: `http://${ipAddr}:${port}/info`,
healthCheckUrl: `http://${ipAddr}:${port}/healthcheck`,
port: {
$: 10000,
$: port,
'@enabled': 'true',

@@ -342,3 +361,3 @@ },

},
vipAddress: '127.0.0.1',
vipAddress: ipAddr,
dataCenterInfo: {

@@ -374,3 +393,9 @@ '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',

.then((config) => {
const expected = { host: 'eureka.test.com', port: '1000' };
// const expected = { host: 'eureka.test.com', port: '1000' };
const expected = {
availabilityZones: ['any'],
serviceUrls: {
any: ['http://eureka.test.com:1000/eureka'],
},
};

@@ -384,14 +409,17 @@ expect(config).to.deep.equal(expected);

const fakeIp = '10.11.12.13';
const fakeDockerId = '1011121314151617';
const ipLib = require('ip');
const dockerLib = require('../lib/get-docker-instance-id');
beforeEach(() => {
box.stub(dockerLib, 'getDockerInstanceId').callsFake(() => fakeDockerId);
box.stub(ipLib, 'address').callsFake(() => fakeIp);
});
const { generateInstanceId } = require('../index');
it('should create a correct instanceId based on the projects package.json', () => {
const { generateInstanceId } = cleanrequire('../index');
const res = generateInstanceId('my-test-project-V5', fakeIp);
const expected = `my-test-project-V5:${fakeIp}`;
const expected = `my-test-project-V5:${fakeIp}:${fakeDockerId}`;

@@ -462,4 +490,7 @@ expect(res).to.equal(expected);

.then((config) => {
const expected = {host: 'eureka.test.com', port: '1000'};
// const expected = {host: 'eureka.test.com', port: '1000'};
const expected = {
availabilityZones: ['any'],
serviceUrls: { any: ['http://eureka.test.com:1000/eureka'] },
};
expect(config).to.deep.equal(expected);

@@ -477,39 +508,67 @@ });

box.stub(cfgClientLib, 'getConfigsAsync').callsFake(() => fareConfig);
box.stub(awsIdentityLib, 'getIdentityDocument').callsFake(() => { });
});
context('without passing dataCenterInfo [default]', () => {
it('should return proper eurekaClient', () => {
const { Eureka } = cleanrequire('eureka-js-client');
const { asyncClientFromConfigService, createDataCenterInfo } = cleanrequire('../index');
context('getIdentityDocument returns empty', () => {
beforeEach(() => {
box.stub(awsIdentityLib, 'getIdentityDocument').callsFake(() => { });
});
return asyncClientFromConfigService({}, false)
.then((eurekaCli) => {
expect(eurekaCli).to.be.instanceof(Eureka);
context('without passing dataCenterInfo [default]', () => {
it('should return proper eurekaClient', () => {
const { Eureka } = cleanrequire('eureka-js-client');
const { asyncClientFromConfigService, createDataCenterInfo } = cleanrequire('../index');
const dataCenterInfo = createDataCenterInfo();
const { instance } = eurekaCli.config;
expect(instance).to.have.property('dataCenterInfo');
expect(instance.dataCenterInfo).to.deep.equal(dataCenterInfo);
});
return asyncClientFromConfigService({}, false)
.then((eurekaCli) => {
expect(eurekaCli).to.be.instanceof(Eureka);
const dataCenterInfo = createDataCenterInfo();
const { instance } = eurekaCli.config;
expect(instance).to.have.property('dataCenterInfo');
expect(instance.dataCenterInfo).to.deep.equal(dataCenterInfo);
});
});
});
context('passing dataCenterInfo', () => {
it('should return proper eurekaClient', () => {
const { Eureka } = cleanrequire('eureka-js-client');
const { asyncClientFromConfigService } = cleanrequire('../index');
const dataCenterInfo = {
'@class': 'some.class',
name: 'Test',
};
return asyncClientFromConfigService({dataCenterInfo}, false)
.then((eurekaCli) => {
expect(eurekaCli).to.be.instanceof(Eureka);
const { instance } = eurekaCli.config;
expect(instance).to.have.property('dataCenterInfo');
expect(instance.dataCenterInfo).to.deep.equal(dataCenterInfo);
});
});
});
});
context('passing dataCenterInfo', () => {
it('should return proper eurekaClient', () => {
const { Eureka } = cleanrequire('eureka-js-client');
const { asyncClientFromConfigService } = cleanrequire('../index');
context('getIdentityDocument throws error', () => {
beforeEach(() => {
box.stub(awsIdentityLib, 'getIdentityDocument').callsFake(() => { throw new Error('TestError'); });
});
const dataCenterInfo = {
'@class': 'some.class',
name: 'Test',
};
context('without passing dataCenterInfo [default]', () => {
it('should return proper eurekaClient', () => {
const { Eureka } = cleanrequire('eureka-js-client');
const { asyncClientFromConfigService, createDataCenterInfo } = cleanrequire('../index');
return asyncClientFromConfigService({dataCenterInfo}, false)
.then((eurekaCli) => {
expect(eurekaCli).to.be.instanceof(Eureka);
const { instance } = eurekaCli.config;
expect(instance).to.have.property('dataCenterInfo');
expect(instance.dataCenterInfo).to.deep.equal(dataCenterInfo);
});
return asyncClientFromConfigService({}, false)
.then((eurekaCli) => {
expect(eurekaCli).to.be.instanceof(Eureka);
const dataCenterInfo = createDataCenterInfo();
const { instance } = eurekaCli.config;
expect(instance).to.have.property('dataCenterInfo');
expect(instance.dataCenterInfo).to.deep.equal(dataCenterInfo);
});
});
});

@@ -516,0 +575,0 @@ });

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