Skip to content

Commit ec003a8

Browse files
authored
Update-redis v3 -> v4 (#155)
* curry redis client * bump with legacy mode * remove moment
1 parent 03bf885 commit ec003a8

File tree

12 files changed

+177
-142
lines changed

12 files changed

+177
-142
lines changed

lib/middleware/negotiate-content.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const moment = require("moment");
21
const XML = require("xmlbuilder");
32
const YAML = require("yamljs");
43

@@ -41,7 +40,6 @@ module.exports = (req, res, next) => {
4140
}
4241

4342
function HTMLResponse() {
44-
req.app.locals.moment = moment;
4543
XML.create(res.bodyXmlObj, { allowSurrogateChars: true });
4644
res.render(res.view || "default", {
4745
req: req,

lib/routes/bins.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const debug = require("debug")("mockbin");
22
const express = require("express");
33
const mw = require("../middleware");
4-
const redis = require("redis");
4+
const { createClient } = require("redis");
55
const routes = require("./bins/");
66
const URL = require("url").URL;
77

@@ -12,21 +12,23 @@ module.exports = function bins(dsnStr) {
1212
this.dsn = dsn;
1313

1414
// connect to redis
15-
this.client = redis.createClient({
15+
const client = createClient({
16+
legacyMode: true,
1617
host: dsn.hostname,
1718
port: dsn.port,
1819
no_ready_check: true,
1920
});
2021

2122
// Disable client's AUTH command.
22-
this.client.auth = null;
23+
client.auth = null;
2324
if (dsn.username) {
24-
this.client.send_command("AUTH", [dsn.username, dsn.password]);
25+
client.send_command("AUTH", [dsn.username, dsn.password]);
2526
}
2627

27-
this.client.on("error", (err) => {
28+
client.on("error", (err) => {
2829
console.log("redis error:", err);
2930
});
31+
client.connect();
3032

3133
const router = express.Router();
3234

@@ -40,18 +42,34 @@ module.exports = function bins(dsnStr) {
4042
];
4143

4244
const endpoints = [
43-
{ action: "get", path: "/create", route: routes.form.bind(this) },
44-
{ action: "post", path: "/create", route: routes.create.bind(this) },
45-
{ action: "get", path: "/view/:uuid*", route: routes.view.bind(this) },
46-
{ action: "get", path: "/sample/:uuid*", route: routes.sample.bind(this) },
47-
{ action: "get", path: "/log/:uuid*", route: routes.log.bind(this) },
45+
{ action: "get", path: "/create", route: routes.form },
46+
{
47+
action: "post",
48+
path: "/create",
49+
route: routes.create(client),
50+
},
51+
{
52+
action: "get",
53+
path: "/view/:uuid*",
54+
route: routes.view(client),
55+
},
56+
{
57+
action: "get",
58+
path: "/sample/:uuid*",
59+
route: routes.sample(client),
60+
},
61+
{
62+
action: "get",
63+
path: "/log/:uuid*",
64+
route: routes.log(client),
65+
},
4866
{
4967
action: "delete",
5068
path: "/delete/:uuid*",
51-
route: routes.delete.bind(this),
69+
route: routes.delete(client),
5270
},
53-
{ action: "put", path: "/upsert/:uuid*", route: routes.update.bind(this) },
54-
{ action: "all", path: "/:uuid*", route: routes.run.bind(this) },
71+
{ action: "put", path: "/upsert/:uuid*", route: routes.update(client) },
72+
{ action: "all", path: "/:uuid*", route: routes.run(client) },
5573
];
5674

5775
endpoints.forEach((endpoint) => {

lib/routes/bins/create.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const util = require("util");
33
const uuid = require("uuid");
44
const validate = require("har-validator");
55

6-
module.exports = async function (req, res, next) {
6+
module.exports = (client) => (req, res, next) => {
77
let mock = req.jsonBody;
88

99
// check for full HAR
@@ -41,18 +41,16 @@ module.exports = async function (req, res, next) {
4141

4242
mock.content.size = 0;
4343

44-
await validate
44+
validate
4545
.response(mock)
46-
.then(
47-
function () {
48-
const id = uuid.v4();
46+
.then(function () {
47+
const id = uuid.v4();
4948

50-
this.client.set(`bin:${id}`, JSON.stringify(mock));
49+
client.set(`bin:${id}`, JSON.stringify(mock));
5150

52-
res.view = "redirect";
53-
res.status(201).location(util.format("/bin/%s", id)).body = id;
54-
}.bind(this),
55-
)
51+
res.view = "redirect";
52+
res.status(201).location(util.format("/bin/%s", id)).body = id;
53+
})
5654

5755
.catch((err) => {
5856
res.body = {

lib/routes/bins/delete.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const debug = require("debug")("mockbin");
22

3-
module.exports = function (req, res, next) {
3+
module.exports = (client) => (req, res, next) => {
44
const compoundId = req.params.uuid + req.params[0];
5-
this.client.del(`bin:${compoundId}`, (err) => {
5+
client.del(`bin:${compoundId}`, (err) => {
66
if (err) {
77
debug(err);
88

@@ -11,7 +11,7 @@ module.exports = function (req, res, next) {
1111
next();
1212
});
1313

14-
this.client.del(`log:${compoundId}`, (err) => {
14+
client.del(`log:${compoundId}`, (err) => {
1515
if (err) {
1616
debug(err);
1717

lib/routes/bins/log.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const debug = require("debug")("mockbin");
22
const pkg = require("../../../package.json");
33

4-
module.exports = function (req, res, next) {
4+
module.exports = (client) => (req, res, next) => {
55
res.view = "bin/log";
66
const compoundId = req.params.uuid + req.params[0];
7-
this.client.lrange(`log:${compoundId}`, 0, -1, (err, history) => {
7+
client.lrange(`log:${compoundId}`, 0, -1, (err, history) => {
88
if (err) {
99
debug(err);
1010

lib/routes/bins/run.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
const debug = require("debug")("mockbin");
22

3-
module.exports = function (req, res, next) {
3+
module.exports = (client) => (req, res, next) => {
44
// compoundId allows us to provide paths in the id to resolve to a specific bin
55
const compoundId = req.params.uuid + req.params[0];
6-
this.client.get(
7-
`bin:${compoundId}`,
8-
function (err, value) {
9-
if (err) {
10-
debug(err);
11-
12-
throw err;
6+
client.get(`bin:${compoundId}`, function (err, value) {
7+
if (err) {
8+
debug(err);
9+
10+
throw err;
11+
}
12+
13+
if (value) {
14+
const har = JSON.parse(value);
15+
16+
// log interaction & send the appropriate response based on HAR
17+
client.rpush(`log:${compoundId}`, JSON.stringify(req.har.log.entries[0]));
18+
client.ltrim(`log:${compoundId}`, 0, 100);
19+
20+
// headers
21+
har.headers.map((header) => {
22+
res.set(header.name, header.value);
23+
});
24+
25+
// cookies
26+
har.cookies.map((cookie) => {
27+
res.cookie(cookie.name, cookie.value);
28+
});
29+
30+
// status
31+
res.httpVersion = har.httpVersion.split("/")[1];
32+
res.statusCode = har.status || 200;
33+
res.statusMessage = har.statusText || "OK";
34+
35+
// special condition
36+
if (har.redirectURL !== "") {
37+
res.location(har.redirectURL);
1338
}
1439

15-
if (value) {
16-
const har = JSON.parse(value);
17-
18-
// log interaction & send the appropriate response based on HAR
19-
this.client.rpush(
20-
`log:${compoundId}`,
21-
JSON.stringify(req.har.log.entries[0]),
22-
);
23-
this.client.ltrim(`log:${compoundId}`, 0, 100);
24-
25-
// headers
26-
har.headers.map((header) => {
27-
res.set(header.name, header.value);
28-
});
29-
30-
// cookies
31-
har.cookies.map((cookie) => {
32-
res.cookie(cookie.name, cookie.value);
33-
});
34-
35-
// status
36-
res.httpVersion = har.httpVersion.split("/")[1];
37-
res.statusCode = har.status || 200;
38-
res.statusMessage = har.statusText || "OK";
39-
40-
// special condition
41-
if (har.redirectURL !== "") {
42-
res.location(har.redirectURL);
43-
}
44-
45-
return res.send(har.content.text ? har.content.text : null);
46-
}
40+
return res.send(har.content.text ? har.content.text : null);
41+
}
4742

48-
next();
49-
}.bind(this),
50-
);
43+
next();
44+
});
5145
};

lib/routes/bins/sample.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const debug = require("debug")("mockbin");
22
const util = require("util");
33

4-
module.exports = function (req, res, next) {
5-
this.client.get(`bin:${req.params.uuid}`, (err, value) => {
4+
module.exports = (client) => (req, res, next) => {
5+
client.get(`bin:${req.params.uuid}`, (err, value) => {
66
if (err) {
77
debug(err);
88

lib/routes/bins/update.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const debug = require("debug")("mockbin");
22
const validate = require("har-validator");
33
const path = require("path");
44

5-
module.exports = function (req, res, next) {
5+
module.exports = (client) => (req, res, next) => {
66
const id = req.params.uuid;
77
const path = req.params[0];
88
const compoundId = id + path;
@@ -38,14 +38,12 @@ module.exports = function (req, res, next) {
3838

3939
validate
4040
.response(mock)
41-
.then(
42-
function () {
43-
this.client.set(`bin:${compoundId}`, JSON.stringify(mock));
44-
45-
res.view = "redirect";
46-
res.status(200).location(`/bin/${compoundId}`).body = id;
47-
}.bind(this),
48-
)
41+
.then(function () {
42+
client.set(`bin:${compoundId}`, JSON.stringify(mock));
43+
44+
res.view = "redirect";
45+
res.status(200).location(`/bin/${compoundId}`).body = id;
46+
})
4947
.catch((err) => {
5048
res.body = {
5149
errors: err.message,

lib/routes/bins/view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const debug = require("debug")("mockbin");
22

33
module.exports = function (req, res, next) {
4-
this.client.get(`bin:${req.params.uuid + req.params[0]}`, (err, value) => {
4+
client.get(`bin:${req.params.uuid + req.params[0]}`, (err, value) => {
55
if (err) {
66
debug(err);
77

0 commit comments

Comments
 (0)