Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless offline 6 #125

Closed
wants to merge 13 commits into from
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v8.15.1
10.15
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ language: node_js
sudo: required

node_js:
- "14"
- "12"
- "10"
- "8"

services:
- docker
Expand Down
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"eslint.format.enable": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
6 changes: 3 additions & 3 deletions packages/serverless-offline-sqs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-offline-sqs",
"version": "3.1.3",
"version": "4.0.0",
"description": "Emulate AWS λ and SQS locally when developing your Serverless project",
"main": "src",
"repository": {
Expand All @@ -21,15 +21,15 @@
],
"license": "MIT",
"peerDependencies": {
"serverless-offline": "^5.12.1"
"serverless-offline": "^6.1.0"
},
"dependencies": {
"aws-sdk": "^2.642.0",
"figures": "^3.2.0",
"lodash": "^4.17.15"
},
"devDependencies": {
"serverless-offline": "^5.12.1"
"serverless-offline": "^6.1.0"
},
"keywords": [
"sqs",
Expand Down
75 changes: 34 additions & 41 deletions packages/serverless-offline-sqs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const {join} = require('path');
const figures = require('figures');
const SQS = require('aws-sdk/clients/sqs');
const {
Expand All @@ -24,8 +23,6 @@ const {
toPairs,
values
} = require('lodash/fp');
const functionHelper = require('serverless-offline/src/functionHelper');
const LambdaContext = require('serverless-offline/src/LambdaContext');

const fromCallback = fun =>
new Promise((resolve, reject) => {
Expand Down Expand Up @@ -56,6 +53,14 @@ class ServerlessOfflineSQS {
'before:offline:start:end': this.offlineStartEnd.bind(this)
};

this.serverlessOfflinePlugin = this.serverless.pluginManager.plugins.find(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, this doesn't find anything even though I do have serverless-offline-sqs plugin installed... Are you sure this is the best way to find it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find any better way to hook into the serverless-offline plugin. The plugin order matters. Make sure that - serverless-offline appears before - serverless-offline-sqs in your serverless.yml

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that was it - after changing the order I can approve that it works on my side. (tested with serverless-offline versions 6.1.0 and 6.1.4)

plugin => plugin.commands && plugin.commands.offline
);

if (!this.serverlessOfflinePlugin) {
throw new Error('Missing serverless-offline plugin');
}

this.streams = [];
}

Expand Down Expand Up @@ -120,43 +125,13 @@ class ServerlessOfflineSQS {
return queueName;
}

eventHandler(queueEvent, functionName, messages, cb) {
if (!messages) return cb();
async eventHandler(queueEvent, functionName, messages) {
if (!messages) return;

const streamName = this.getQueueName(queueEvent);
this.serverless.cli.log(`${streamName} (λ: ${functionName})`);

const config = this.getConfig();
const {location = '.'} = config;

const __function = this.service.getFunction(functionName);

const {env} = process;
const functionEnv = assignAll([
{AWS_REGION: get('service.provider.region', this)},
env,
get('service.provider.environment', this),
get('environment', __function)
]);
process.env = functionEnv;

const serviceRuntime = this.service.provider.runtime;
const servicePath = join(this.serverless.config.servicePath, location);

const funOptions = functionHelper.getFunctionOptions(
__function,
functionName,
servicePath,
serviceRuntime
);
const handler = functionHelper.createHandler(funOptions, config);

const lambdaContext = new LambdaContext(__function, this.service.provider, (err, data) => {
this.serverless.cli.log(
`[${err ? figures.cross : figures.tick}] ${functionName} ${JSON.stringify(data) || ''}`
);
cb(err, data);
});

const awsRegion = config.region || 'us-west-2';
const awsAccountId = config.accountId || '000000000000';
Expand Down Expand Up @@ -188,12 +163,30 @@ class ServerlessOfflineSQS {
)
};

const x = handler(event, lambdaContext, lambdaContext.done);
if (x && typeof x.then === 'function' && typeof x.catch === 'function')
x.then(lambdaContext.succeed).catch(lambdaContext.fail);
else if (x instanceof Error) lambdaContext.fail(x);
const lambdaFunction = this.getLambdaFunction(functionName);
lambdaFunction.setEvent(event);

let err = null;
let data = null;
try {
data = await lambdaFunction.runHandler();
} catch (executionError) {
err = executionError;
throw err;
} finally {
this.serverless.cli.log(
`${streamName} (λ: ${functionName}) [${
err ? figures.cross : figures.tick
}] ${JSON.stringify(data) || ''}`
);
}
}

process.env = env;
getLambdaFunction(functionName) {
// TODO: Find a better way to hook into serverless-offline Lambda
const lambda = this.serverlessOfflinePlugin.__private_5_lambda;
const lambdaFunction = lambda.get(functionName);
return lambdaFunction;
}

async createQueueReadable(functionName, queueEvent) {
Expand Down Expand Up @@ -226,7 +219,7 @@ class ServerlessOfflineSQS {

if (Messages) {
try {
await fromCallback(cb => this.eventHandler(queueEvent, functionName, Messages, cb));
await this.eventHandler(queueEvent, functionName, Messages);

await fromCallback(cb =>
client.deleteMessageBatch(
Expand Down
4 changes: 2 additions & 2 deletions tests/serverless-plugins-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
"figures": "^3.0.0",
"lodash": "^4.17.15",
"serverless": "^1.53.0",
"serverless-offline": "^5.11.0",
"serverless-offline": "^6.1.0",
"serverless-offline-dynamodb-streams": "^3.0.2",
"serverless-offline-kinesis": "^3.0.2",
"serverless-offline-sqs": "^3.1.3",
"serverless-offline-sqs": "^4.0.0",
"serverless-offline-ssm-provider": "^2.0.1",
"signal-exit": "^3.0.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ provider:
runtime: nodejs8.10

plugins:
localPath: '../../packages'
localPath: "../../packages"
modules:
- serverless-offline-ssm-provider
- serverless-offline-sqs
- serverless-offline
- serverless-offline-sqs

functions:
autoCreatedHandler:
Expand Down Expand Up @@ -49,7 +49,7 @@ resources:
maxReceiveCount: 12
MessageRetentionPeriod: 1209600
ContentBasedDeduplication: true
Policy: {Ref: "whatever the policy, i'll ignore it"}
Policy: { Ref: "whatever the policy, i'll ignore it" }

custom:
serverless-offline:
Expand Down
6 changes: 3 additions & 3 deletions tests/serverless-plugins-integration/serverless.sqs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ provider:
runtime: nodejs8.10

plugins:
localPath: '../../packages'
localPath: "../../packages"
modules:
- serverless-offline-ssm-provider
- serverless-offline-sqs
- serverless-offline
- serverless-offline-sqs

functions:
myPromiseHandler:
Expand Down Expand Up @@ -48,4 +48,4 @@ resources:
custom:
serverless-offline:
port: 3333
serverless-offline-sqs: ${file(./custom.yml):serverless-offline-sqs}
serverless-offline-sqs: ${file(./custom.yml):serverless-offline-sqs}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ serverless.stdout.pipe(
write(chunk, enc, cb) {
const output = chunk.toString();

if (/Offline \[HTTP] listening on/.test(output)) {
if (/Offline.+listening on/.test(output)) {
putItems();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/serverless-plugins-integration/test-kinesis.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ serverless.stdout.pipe(
write(chunk, enc, cb) {
const output = chunk.toString();

if (/Offline \[HTTP] listening on/.test(output)) {
if (/Offline.+listening on/.test(output)) {
putRecords();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ serverless.stdout.pipe(
write(chunk, enc, cb) {
const output = chunk.toString();

if (/Offline \[HTTP] listening on/.test(output)) {
if (/Offline.+listening on/.test(output)) {
sendMessages()
.then(() => console.log('sucessfully send messages'))
.catch(err => {
Expand Down
2 changes: 1 addition & 1 deletion tests/serverless-plugins-integration/test-sqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ serverless.stdout.pipe(
write(chunk, enc, cb) {
const output = chunk.toString();

if (/Offline \[HTTP] listening on/.test(output)) {
if (/Offline.+listening on/.test(output)) {
sendMessages();
}

Expand Down