Skip to content

Commit

Permalink
Refactored working with latest node (#118)
Browse files Browse the repository at this point in the history
* Refactored working with latest node

* Resolved formatting issue with

* Resolved defect with content type

* bump and refresh lock

---------

Co-authored-by: jackkav <[email protected]>
  • Loading branch information
JamesGouldSonos and jackkav authored Jan 3, 2024
1 parent a31208c commit babb88d
Show file tree
Hide file tree
Showing 20 changed files with 2,590 additions and 2,465 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"asi": true,
"node": true
"node": true,
"esversion": 8
}
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM node:16-alpine
FROM node:18-alpine

# Create app directory
WORKDIR /mockbin

COPY package*.json ./
RUN npm ci --only=production
RUN npm update -g npm

COPY package.json ./
RUN npm i --omit=dev
COPY . .

ENV MOCKBIN_REDIS "redis://redis:6379"
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:
app:
build: .
environment:
REALM: prod
REDIS_URL: "//redis:6379"
links:
- redis
ports:
- "8080:8080"

redis:
image: redis
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')
var express = require('express')
var mw = require('./middleware')
var routes = require('./routes')
Expand Down
5 changes: 4 additions & 1 deletion lib/middleware/body-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = function (req, res, next) {

// parse Content-Type
var contentType = req.headers['content-type']
var type = contentType ? typer.parse(contentType) : null

// @jgould-sonos
// 8 June 2022 - Updated to avoid error with unable to handle semicolon in content type
var type = contentType ? typer.parse(contentType.split(';')[0]) : null

if (type) {
req.contentType = [[type.type, type.subtype].join('/'), type.suffix].join('+').replace(/\+$/, '')
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')

module.exports = function (err, req, res, next) {
debug(err)
Expand Down
108 changes: 63 additions & 45 deletions lib/routes/bins.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
'use strict'

var debug = require('debug-log')('mockbin')
var express = require('express')
var mw = require('../middleware')
var redis = require('redis')
var routes = require('./bins/')
var URL = require('url').URL
var debug = require("debug")("mockbin");
var express = require("express");
var mw = require("../middleware");
var redis = require("redis");
var routes = require("./bins/");
var URL = require("url").URL;

module.exports = function bins(dsnStr) {
// parse redis dsn
var dsn = new URL(dsnStr)

// connect to redis
this.client = redis.createClient(dsn.port, dsn.hostname, {
auth_pass: dsn.auth ? dsn.auth.split(':').pop() : false
})

this.client.on('error', function (err) {
debug('redis error:', err)
})

var router = express.Router()

var defaults = [mw.forwarded, mw.errorHandler, mw.bodyParser, null, mw.cors, mw.negotiateContent]

var endpoints = [
{ action: 'get', path: '/create', route: routes.form.bind(this) },
{ action: 'post', path: '/create', route: routes.create.bind(this) },
{ action: 'get', path: '/:uuid/view', route: routes.view.bind(this) },
{ action: 'get', path: '/:uuid/sample', route: routes.sample.bind(this) },
{ action: 'get', path: '/:uuid/log', route: routes.log.bind(this) },
{ action: 'delete', path: '/:uuid/delete', route: routes.delete.bind(this) },
{ action: 'put', path: '/:uuid', route: routes.update.bind(this) },
{ action: 'all', path: '/:uuid*', route: routes.run.bind(this) }
]

endpoints.forEach(function (endpoint) {
// add route to middleware
defaults.splice(3, 1, endpoint.route)

// assign router to action at path
router[endpoint.action].apply(router, [endpoint.path].concat(defaults))
})

return router
}
// parse redis dsn
var dsn = new URL(dsnStr);

this.dsn = dsn;

// connect to redis
this.client = redis.createClient(
{
host: dsn.hostname,
port: dsn.port,
no_ready_check: true
})

// Disable client's AUTH command.
this.client.auth = null
this.client.send_command('AUTH', [dsn.username, dsn.password])

this.client.on("error", (err) => {
debug("redis error:", err);
});

var router = express.Router();

var defaults = [
mw.forwarded,
mw.errorHandler,
mw.bodyParser,
null,
mw.cors,
mw.negotiateContent,
];

var endpoints = [
{ action: "get", path: "/create", route: routes.form.bind(this) },
{ action: "post", path: "/create", route: routes.create.bind(this) },
{ action: "get", path: "/:uuid/view", route: routes.view.bind(this) },
{ action: "get", path: "/:uuid/sample", route: routes.sample.bind(this) },
{ action: "get", path: "/:uuid/log", route: routes.log.bind(this) },
{
action: "delete",
path: "/:uuid/delete",
route: routes.delete.bind(this),
},
{ action: "put", path: "/:uuid", route: routes.update.bind(this) },
{ action: "all", path: "/:uuid*", route: routes.run.bind(this) },
];

endpoints.forEach((endpoint) => {
// add route to middleware
defaults.splice(3, 1, endpoint.route);

// assign router to action at path
router[endpoint.action].apply(router, [endpoint.path].concat(defaults));
});

return router;
};
8 changes: 4 additions & 4 deletions lib/routes/bins/create.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')
var util = require('util')
var uuid = require('node-uuid')
var uuid = require('uuid')
var validate = require('har-validator')

module.exports = function (req, res, next) {
module.exports = async function (req, res, next) {
var mock = req.jsonBody

// check for full HAR
Expand Down Expand Up @@ -43,7 +43,7 @@ module.exports = function (req, res, next) {

mock.content.size = 0

validate.response(mock)
await validate.response(mock)
.then(function () {
var id = uuid.v4()

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/bins/log.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')
var pkg = require('../../../package.json')

module.exports = function (req, res, next) {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/bins/run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')

module.exports = function (req, res, next) {
this.client.get('bin:' + req.params.uuid, function (err, value) {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/bins/sample.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')
var util = require('util')

module.exports = function (req, res, next) {
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/bins/view.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var debug = require('debug-log')('mockbin')
var debug = require('debug')('mockbin')

module.exports = function (req, res, next) {
this.client.get('bin:' + req.params.uuid, function (err, value) {
Expand Down
Loading

0 comments on commit babb88d

Please sign in to comment.