Skip to content

Commit

Permalink
Merge pull request #275 from jamhall/optional_close_cb
Browse files Browse the repository at this point in the history
Avoid monkeypatching the close method on the returned server instance
  • Loading branch information
leontastic authored Jul 27, 2018
2 parents e761780 + 1d05986 commit 0a579f0
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const async = require("async");
const fs = require("fs-extra");
const { defaults } = require("lodash");
const https = require("https");
Expand All @@ -14,18 +13,12 @@ class S3rver {
this.options = defaults({}, options, S3rver.defaultOptions);
}

resetFs(callback) {
resetFs() {
const { directory } = this.options;
fs.readdir(directory, (err, buckets) => {
if (err) return callback(err);
async.eachSeries(
buckets,
(bucket, callback) => {
fs.remove(path.join(directory, bucket), callback);
},
callback
);
});
const buckets = fs.readdirSync(directory);
for (const bucket of buckets) {
fs.removeSync(path.join(directory, bucket));
}
}

callback() {
Expand All @@ -47,21 +40,14 @@ class S3rver {
this.options.directory
);
})
.on("error", err => {
done(err);
});
server.close = callback => {
const { close } = Object.getPrototypeOf(server);
return close.call(server, () => {
.on("close", () => {
app.logger.unhandleExceptions();
app.logger.close();
if (this.options.removeBucketsOnClose) {
this.resetFs(callback);
} else {
callback();
this.resetFs();
}
});
};
})
.on("error", done);
server.s3Event = app.s3Event;
return server;
}
Expand Down

0 comments on commit 0a579f0

Please sign in to comment.