Skip to content

Commit 15f9480

Browse files
committed
feat: migrate sqs plugin to serverless v4
1 parent 5b34c4f commit 15f9480

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packages/serverless-offline-sqs/src/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ const {
1212
toPairs
1313
} = require('lodash/fp');
1414

15-
const log = require('@serverless/utils/log').log;
16-
1715
const SQS = require('./sqs');
1816

1917
const OFFLINE_OPTION = 'serverless-offline';
@@ -32,15 +30,21 @@ const defaultOptions = {
3230
const omitUndefined = omitBy(isUndefined);
3331

3432
class ServerlessOfflineSQS {
35-
constructor(serverless, cliOptions) {
33+
constructor(serverless, cliOptions, {log} = {}) {
3634
this.cliOptions = null;
3735
this.options = null;
36+
this.log = null;
3837
this.sqs = null;
3938
this.lambda = null;
4039
this.serverless = null;
4140

4241
this.cliOptions = cliOptions;
4342
this.serverless = serverless;
43+
this.log = log || {
44+
debug: console.debug.bind(console),
45+
notice: console.log.bind(console),
46+
warning: console.warn.bind(console)
47+
};
4448

4549
this.hooks = {
4650
'offline:start:init': this.start.bind(this),
@@ -67,9 +71,7 @@ class ServerlessOfflineSQS {
6771

6872
await Promise.all(eventModules);
6973

70-
this.serverless.cli.log(
71-
`Starting Offline SQS at stage ${this.options.stage} (${this.options.region})`
72-
);
74+
this.log.notice(`Starting Offline SQS at stage ${this.options.stage} (${this.options.region})`);
7375
}
7476

7577
ready() {
@@ -83,7 +85,7 @@ class ServerlessOfflineSQS {
8385

8486
signals.map(signal =>
8587
process.on(signal, async () => {
86-
this.serverless.cli.log(`Got ${signal} signal. Offline Halting...`);
88+
this.log.notice(`Got ${signal} signal. Offline Halting...`);
8789

8890
await this.end();
8991
})
@@ -100,7 +102,7 @@ class ServerlessOfflineSQS {
100102
return;
101103
}
102104

103-
this.serverless.cli.log('Halting offline server');
105+
this.log.notice('Halting offline server');
104106

105107
const eventModules = [];
106108

@@ -129,7 +131,7 @@ class ServerlessOfflineSQS {
129131
async _createSqs(events, skipStart) {
130132
const resources = this._getResources();
131133

132-
this.sqs = new SQS(this.lambda, resources, this.options);
134+
this.sqs = new SQS(this.lambda, resources, this.options, this.log);
133135

134136
await this.sqs.create(events);
135137

@@ -155,7 +157,7 @@ class ServerlessOfflineSQS {
155157
omitUndefined(this.cliOptions)
156158
);
157159

158-
log.debug('options:', this.options);
160+
this.log.debug('sqs options:', this.options);
159161
}
160162

161163
_getEvents() {

packages/serverless-offline-sqs/src/sqs.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
toString,
1212
values
1313
} = require('lodash/fp');
14-
const log = require('@serverless/utils/log').log;
1514
const {default: PQueue} = require('p-queue');
1615
const SQSEventDefinition = require('./sqs-event-definition');
1716
const SQSEvent = require('./sqs-event');
@@ -21,21 +20,37 @@ const delay = timeout =>
2120
setTimeout(resolve, timeout);
2221
});
2322

23+
const defaultLog = {
24+
debug: console.debug.bind(console),
25+
notice: console.log.bind(console),
26+
warning: console.warn.bind(console)
27+
};
28+
2429
class SQS {
25-
constructor(lambda, resources, options) {
30+
constructor(lambda, resources, options, log = defaultLog) {
2631
this.lambda = null;
2732
this.resources = null;
2833
this.options = null;
34+
this.log = null;
2935

3036
this.lambda = lambda;
3137
this.resources = resources;
3238
this.options = options;
39+
this.log = log || defaultLog;
3340

3441
this.client = new SQSClient(this.options);
3542

3643
this.queue = new PQueue({autoStart: false});
3744
}
3845

46+
_safeLog(level, message) {
47+
if (this.log && typeof this.log[level] === 'function') {
48+
this.log[level](message);
49+
} else if (console[level]) {
50+
console[level](message);
51+
}
52+
}
53+
3954
create(events) {
4055
return Promise.all(events.map(({functionKey, sqs}) => this._create(functionKey, sqs)));
4156
}
@@ -138,7 +153,7 @@ class SQS {
138153
)
139154
);
140155
} catch (err) {
141-
log.warning(err.stack);
156+
this._safeLog('warning', err.stack);
142157
}
143158
}
144159

@@ -170,7 +185,7 @@ class SQS {
170185
} catch (err) {
171186
if (remainingTry > 0 && err.name === 'AWS.SimpleQueueService.NonExistentQueue')
172187
return this._createQueue({queueName}, remainingTry - 1);
173-
log.warning(err.stack);
188+
this._safeLog('warning', err.stack);
174189
}
175190
}
176191
}

0 commit comments

Comments
 (0)