Skip to content

Commit 5b34c4f

Browse files
committed
feat: migrate s3 plugin to serverless v4
1 parent 80f0d8f commit 5b34c4f

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const {get, isUndefined, omitBy, pick} = require('lodash/fp');
22

3-
const log = require('@serverless/utils/log').log;
4-
53
const S3 = require('./s3');
64

75
const OFFLINE_OPTION = 'serverless-offline';
@@ -17,15 +15,21 @@ const defaultOptions = {
1715
const omitUndefined = omitBy(isUndefined);
1816

1917
class ServerlessOfflineS3 {
20-
constructor(serverless, cliOptions) {
18+
constructor(serverless, cliOptions, {log} = {}) {
2119
this.cliOptions = null;
2220
this.options = null;
2321
this.s3 = null;
2422
this.lambda = null;
2523
this.serverless = null;
24+
this.log = null;
2625

2726
this.cliOptions = cliOptions;
2827
this.serverless = serverless;
28+
this.log = log || {
29+
debug: console.debug.bind(console),
30+
notice: console.log.bind(console),
31+
warning: console.warn.bind(console)
32+
};
2933

3034
this.hooks = {
3135
'offline:start:init': this.start.bind(this),
@@ -52,7 +56,7 @@ class ServerlessOfflineS3 {
5256

5357
await Promise.all(eventModules);
5458

55-
this.serverless.cli.log(
59+
this.log.notice(
5660
`Starting Offline S3 at stage ${this.options.stage} (${this.options.endPoint}/${this.options.region})`
5761
);
5862
}
@@ -68,7 +72,7 @@ class ServerlessOfflineS3 {
6872

6973
signals.map(signal =>
7074
process.on(signal, async () => {
71-
this.serverless.cli.log(`Got ${signal} signal. Offline Halting...`);
75+
this.log.notice(`Got ${signal} signal. Offline Halting...`);
7276

7377
await this.end();
7478
})
@@ -85,7 +89,7 @@ class ServerlessOfflineS3 {
8589
return;
8690
}
8791

88-
this.serverless.cli.log('Halting offline server');
92+
this.log.notice('Halting offline server');
8993

9094
const eventModules = [];
9195

@@ -114,7 +118,7 @@ class ServerlessOfflineS3 {
114118
async _createS3(events, skipStart) {
115119
const resources = this._getResources();
116120

117-
this.s3 = new S3(this.lambda, resources, this.options);
121+
this.s3 = new S3(this.lambda, resources, this.options, this.log);
118122

119123
await this.s3.create(events);
120124

@@ -140,7 +144,7 @@ class ServerlessOfflineS3 {
140144
omitUndefined(this.cliOptions)
141145
);
142146

143-
log.debug('options:', this.options);
147+
this.log.debug('s3 options:', this.options);
144148
}
145149

146150
_getEvents() {

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

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const Minio = require('minio');
22
const {assign, toNumber} = require('lodash/fp');
33

4-
const log = require('@serverless/utils/log').log;
5-
64
const S3EventDefinition = require('./s3-event-definition');
75
const S3Event = require('./s3-event');
86

@@ -11,15 +9,23 @@ const delay = timeout =>
119
setTimeout(resolve, timeout);
1210
});
1311

12+
const defaultLog = {
13+
debug: console.debug.bind(console),
14+
notice: console.log.bind(console),
15+
warning: console.warn.bind(console)
16+
};
17+
1418
class S3 {
15-
constructor(lambda, resources, options) {
19+
constructor(lambda, resources, options, log = defaultLog) {
1620
this.lambda = null;
1721
this.resources = null;
1822
this.options = null;
23+
this.log = null;
1924

2025
this.lambda = lambda;
2126
this.resources = resources;
2227
this.options = options;
28+
this.log = log || defaultLog;
2329

2430
const s3Endpoint = this.options.endpoint ? new URL(this.options.endpoint) : {};
2531
this.client = new Minio.Client(
@@ -34,6 +40,14 @@ class S3 {
3440
this.listeners = [];
3541
}
3642

43+
_safeLog(level, message) {
44+
if (this.log && typeof this.log[level] === 'function') {
45+
this.log[level](message);
46+
} else if (console[level]) {
47+
console[level](message);
48+
}
49+
}
50+
3751
create(events) {
3852
this.events = events;
3953
return Promise.all(
@@ -48,35 +62,45 @@ class S3 {
4862
return Promise.all(
4963
this.events.map(async ({functionKey, s3}) => {
5064
const {event, bucket, rules} = s3;
51-
await this._waitFor(bucket);
65+
this.log.debug(`Setting up listener for bucket: ${bucket}, event: ${event}`);
5266

67+
await this._waitFor(bucket);
5368
const eventRules = rules || [];
5469
const prefix = (eventRules.find(rule => rule.prefix) || {prefix: '*'}).prefix;
5570
const suffix = (eventRules.find(rule => rule.suffix) || {suffix: '*'}).suffix;
56-
5771
const listener = this.client.listenBucketNotification(bucket, prefix, suffix, [event]);
5872

5973
listener.on('notification', async record => {
6074
if (record) {
6175
try {
76+
this.log.debug(
77+
`Received S3 notification for bucket ${bucket}: ${JSON.stringify(record)}`
78+
);
6279
const lambdaFunction = this.lambda.get(functionKey);
6380

6481
const s3Notification = new S3Event(record);
6582
lambdaFunction.setEvent(s3Notification);
6683

6784
await lambdaFunction.runHandler();
6885
} catch (err) {
69-
log.warn(err.stack);
86+
this.log.warning(
87+
`Error processing S3 notification for bucket ${bucket}: ${err.stack}`
88+
);
7089
}
7190
}
7291
});
7392

93+
listener.on('error', err => {
94+
this.log.warning(`Error in S3 listener for bucket ${bucket}: ${err.message}`);
95+
});
96+
7497
this.listeners = [...this.listeners, listener];
98+
this.log.debug(`Listener set up successfully for bucket: ${bucket}`);
7599
})
76100
);
77101
}
78102

79-
stop(timeout) {
103+
stop(_timeout) {
80104
this.listeners.forEach(listener => listener.stop());
81105
this.listeners = [];
82106
}
@@ -114,7 +138,7 @@ class S3 {
114138

115139
await lambdaFunction.runHandler();
116140
} catch (err) {
117-
log.warn(err.stack);
141+
this._safeLog('warning', err.stack);
118142
}
119143
}
120144
});

0 commit comments

Comments
 (0)