Skip to content

Commit

Permalink
Get local port form Router
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Canales committed Nov 13, 2023
1 parent 7786667 commit 050f687
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
22 changes: 22 additions & 0 deletions lib/helpers/get-local-service-port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const RouterFetcher = require('@janiscommerce/router-fetcher');

/**
* Get the local port of the service
* @param {string} serviceCode JANIS service code
* @returns {string}
*/
module.exports = async serviceCode => {

const routerFetcher = new RouterFetcher();
const { servers } = await routerFetcher.getSchema(serviceCode);

if(!servers)
return;

for(const server of servers) {
if(server.variables?.environment?.default === 'local')
return server.url.match(/(?<=:).+?(?=\/)/g)[1];
}
};
20 changes: 4 additions & 16 deletions lib/invoker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { ApiSession } = require('@janiscommerce/api-session');
const Settings = require('@janiscommerce/settings');

const LambdaError = require('./lambda-error');

Expand All @@ -14,6 +13,7 @@ const { isObjectNotEmpty } = require('./helpers/is-object');

const isLocalEnv = require('./helpers/is-local-env');
const getApiLambdaFunctionName = require('./helpers/get-api-lambda-function-name');
const getLocalServicePort = require('./helpers/get-local-service-port');

/**
* @typedef InvokeResponse
Expand All @@ -39,18 +39,6 @@ module.exports = class Invoker {
return 'Event';
}

/**
* @private
* @static
*/
static get localServicePorts() {

if(!this._localServicePorts)
this._localServicePorts = Settings.get('localServicePorts') || {};

return this._localServicePorts;
}

/**
* Invoke a Lambda Function
* @static
Expand Down Expand Up @@ -313,15 +301,15 @@ module.exports = class Invoker {
return serviceAccountId;
}

static getServiceLambdaInstance(serviceAccountId, serviceCode) {
static async getServiceLambdaInstance(serviceAccountId, serviceCode) {

if(!isLocalEnv())
return LambdaInstance.getInstanceWithRole(serviceAccountId);

const servicePort = this.localServicePorts[serviceCode];
const servicePort = await getLocalServicePort(serviceCode);

if(!servicePort)
throw new LambdaError(`No local service port found for service code ${serviceCode} in settings`, LambdaError.codes.NO_LOCAL_SERVICE_PORT);
throw new LambdaError(`No local service port found for service code ${serviceCode}`, LambdaError.codes.NO_LOCAL_SERVICE_PORT);

return LambdaInstance.getInstanceForLocalService(servicePort, serviceCode);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
"@janiscommerce/api-session": "^3.3.1",
"@janiscommerce/aws-secrets-manager": "^1.0.2",
"@janiscommerce/events": "^0.2.0",
"@janiscommerce/router-fetcher": "^2.1.2",
"@janiscommerce/s3": "^2.0.0",
"@janiscommerce/settings": "^1.0.1",
"lllog": "^1.1.2",
"lodash": "^4.17.21",
"nanoid": "^3.3.4"
Expand Down
51 changes: 32 additions & 19 deletions tests/invoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');
const sinon = require('sinon');

const { ApiSession } = require('@janiscommerce/api-session');
const Settings = require('@janiscommerce/settings');
const RouterFetcher = require('@janiscommerce/router-fetcher');

const { Invoker, LambdaError } = require('../lib/index');
const { LambdaWrapper } = require('../lib/helpers/aws-wrappers');
Expand All @@ -28,12 +28,29 @@ describe('Invoker', () => {
'some-service': fakeServiceAccountId
};

const localServicePorts = {
'some-service': 1234
};

let oldEnv;

const routerFetcherSchema = {
servers: [
{
variables: {
environment: {
default: 'prod'
}
},
url: 'http://test.lambda/api'
},
{
variables: {
environment: {
default: 'local'
}
},
url: 'http://test.lambda:3001/api'
}
]
};

beforeEach(() => {

sinon.restore();
Expand Down Expand Up @@ -826,9 +843,7 @@ describe('Invoker', () => {

process.env.JANIS_ENV = 'local';

sinon.stub(Settings, 'get')
.withArgs('localServicePorts')
.returns(localServicePorts);
sinon.stub(RouterFetcher.prototype, 'getSchema').resolves(routerFetcherSchema);

sinon.stub(LambdaWrapper.prototype, 'invoke').resolves(invokeAsyncResponse);

Expand All @@ -847,15 +862,15 @@ describe('Invoker', () => {
FunctionName: 'JanisSomeServiceService-local-FakeLambda',
InvocationType: 'RequestResponse'
});

sinon.assert.calledOnceWithExactly(RouterFetcher.prototype.getSchema, 'some-service');
});

it('Should reject when can\'t find the local service port in local env', async () => {

process.env.JANIS_ENV = 'local';

Invoker._localServicePorts = {}; // eslint-disable-line no-underscore-dangle

sinon.spy(Settings, 'get');
sinon.stub(RouterFetcher.prototype, 'getSchema').resolves({});
sinon.spy(LambdaWrapper.prototype, 'invoke');
sinon.spy(SecretFetcher, 'fetch');

Expand All @@ -864,7 +879,7 @@ describe('Invoker', () => {
code: LambdaError.codes.NO_LOCAL_SERVICE_PORT
});

sinon.assert.notCalled(Settings.get);
sinon.assert.calledOnceWithExactly(RouterFetcher.prototype.getSchema, 'some-service');
sinon.assert.notCalled(SecretFetcher.fetch);
sinon.assert.notCalled(LambdaWrapper.prototype.invoke);
});
Expand Down Expand Up @@ -1242,9 +1257,7 @@ describe('Invoker', () => {

process.env.JANIS_ENV = 'local';

sinon.stub(Settings, 'get')
.withArgs('localServicePorts')
.returns(localServicePorts);
sinon.stub(RouterFetcher.prototype, 'getSchema').resolves(routerFetcherSchema);

sinon.stub(LambdaWrapper.prototype, 'invoke').resolves(invokeAsyncResponse);

Expand All @@ -1258,7 +1271,7 @@ describe('Invoker', () => {
statusCode: invokeAsyncResponse.StatusCode,
payload: {}
});

sinon.assert.calledOnceWithExactly(RouterFetcher.prototype.getSchema, 'some-service');
sinon.assert.calledOnceWithExactly(LambdaWrapper.prototype.invoke, {
FunctionName: 'JanisSomeServiceService-local-FakeLambda',
InvocationType: 'RequestResponse',
Expand All @@ -1270,9 +1283,7 @@ describe('Invoker', () => {

process.env.JANIS_ENV = 'local';

sinon.stub(Settings, 'get')
.withArgs('localServicePorts')
.returns();
sinon.stub(RouterFetcher.prototype, 'getSchema').resolves({ servers: [routerFetcherSchema.servers[0]] });

sinon.spy(LambdaWrapper.prototype, 'invoke');
sinon.spy(SecretFetcher, 'fetch');
Expand All @@ -1282,6 +1293,8 @@ describe('Invoker', () => {
code: LambdaError.codes.NO_LOCAL_SERVICE_PORT
});

sinon.assert.calledOnceWithExactly(RouterFetcher.prototype.getSchema, 'some-service');

sinon.assert.notCalled(SecretFetcher.fetch);
sinon.assert.notCalled(LambdaWrapper.prototype.invoke);
});
Expand Down

0 comments on commit 050f687

Please sign in to comment.