Skip to content

Commit

Permalink
Merge pull request #22 from smartive/feat/node-14
Browse files Browse the repository at this point in the history
feat: Upgrade to Node.js 14 and add prettier
  • Loading branch information
nickredmark authored May 4, 2021
2 parents 973a127 + 82b29ec commit cf273f9
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 43 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run prettier
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"@smartive/prettier-config"
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
FROM node:10
FROM node:14-alpine

WORKDIR /app

COPY package.json package-lock.json ./

RUN npm ci
RUN npm ci --only=production

COPY index.js .

EXPOSE 25
EXPOSE 1080

ENTRYPOINT [ "npm", "run" ]
CMD [ "start" ]
CMD npm start
75 changes: 38 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const express = require("express");
const bodyParser = require("body-parser");
const cors = require('cors')
const { SMTPServer } = require("smtp-server");
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { SMTPServer } = require('smtp-server');

const app = express();

const PORT = process.env.PORT || "1080";
const SMTP_PORT = process.env.SMTP_PORT || "25";
const HOST = process.env.HOST || "0.0.0.0";
const PORT = process.env.PORT || '1080';
const SMTP_PORT = process.env.SMTP_PORT || '25';
const HOST = process.env.HOST || '0.0.0.0';

let routes = [];
let calls = [];
Expand All @@ -30,46 +30,46 @@ Structure of route:
*/

app.use(cors({ origin: `*`}))
app.use(cors({ origin: '*' }));
app.use(bodyParser.json());
app.use(bodyParser.text());

const smtpServer = new SMTPServer({
authOptional: true,
onData: async (stream, _session, callback) => {
console.log("Got mail");
console.log('Got mail');
const mail = await streamToString(stream);

if (nextMailListeners.length) {
console.log("Notifying mail listeners", nextMailListeners.length);
console.log('Notifying mail listeners', nextMailListeners.length);
nextMailListeners.forEach((listener) => listener(mail));
nextMailListeners = [];
} else {
console.log("Adding mail to queue");
console.log('Adding mail to queue');
mails.push(mail);
}

callback();
},
onAuth: (_auth, _session, cb) => {
console.log("Mail auth");
cb(null, { user: "dummy" });
console.log('Mail auth');
cb(null, { user: 'dummy' });
},
});

smtpServer.listen(SMTP_PORT, HOST);

const route = express.Router();
app.use(process.env.MOCK_PATH || "/mock", route);
app.use(process.env.MOCK_PATH || '/mock', route);

const streamToString = (readStream) =>
new Promise((res) => {
const chunks = [];
readStream.on("data", (chunk) => chunks.push(chunk));
readStream.on("end", () => res(Buffer.concat(chunks)));
readStream.on('data', (chunk) => chunks.push(chunk));
readStream.on('end', () => res(Buffer.concat(chunks)));
});

route.post("/mock", (req, res) => {
route.post('/mock', (req, res) => {
console.log(`Mocking route:`, req.body);
routes = routes.filter(
(route) => !(route.request.match === req.body.request.match && route.request.bodyMatch === req.body.request.bodyMatch)
Expand All @@ -78,8 +78,8 @@ route.post("/mock", (req, res) => {
res.sendStatus(204);
});

route.post("/reset", (_req, res) => {
console.log("resetting");
route.post('/reset', (_req, res) => {
console.log('resetting');
routes = [];
calls = [];
nextCallListeners = [];
Expand All @@ -88,48 +88,48 @@ route.post("/reset", (_req, res) => {
res.sendStatus(204);
});

route.post("/reset/calls", (_req, res) => {
console.log("resetting calls");
route.post('/reset/calls', (_req, res) => {
console.log('resetting calls');
calls = [];
nextCallListeners = [];
mails = [];
nextMailListeners = [];
res.sendStatus(204);
});

route.get("/calls", (_req, res) => {
route.get('/calls', (_req, res) => {
res.send(calls);
});

route.get("/calls/next", (_req, res) => {
route.get('/calls/next', (_req, res) => {
if (calls.length) {
console.log("sending a call");
console.log('sending a call');
res.send(calls.shift());
} else {
console.log("registering a call listener");
console.log('registering a call listener');
nextCallListeners.push((call) => res.send(call));
}
});

route.get("/mails", (_req, res) => {
route.get('/mails', (_req, res) => {
res.send(mails);
});

route.get("/mails/next", (_req, res) => {
route.get('/mails/next', (_req, res) => {
if (mails.length) {
console.log("sending a mail");
console.log('sending a mail');
res.send(mails.shift());
} else {
console.log("registering a mail listener", nextMailListeners.length);
console.log('registering a mail listener', nextMailListeners.length);
nextMailListeners.push((mail) => res.send(mail));
}
});

route.get("/routes", (_req, res) => {
route.get('/routes', (_req, res) => {
res.send(routes);
});

app.all("*", (req, res) => {
app.all('*', (req, res) => {
const call = {
method: req.method,
headers: req.headers,
Expand All @@ -143,13 +143,16 @@ app.all("*", (req, res) => {
calls.push(call);
}

const stringifiedBody = JSON.stringify(req.body, null, 2)
const stringifiedBody = JSON.stringify(req.body, null, 2);
for (const route of routes) {
if (new RegExp(`^${route.request.match}$`).test(req.url) && (!route.request.bodyMatch || new RegExp(`^${route.request.bodyMatch}$`, 'm').test(stringifiedBody))) {
if (
new RegExp(`^${route.request.match}$`).test(req.url) &&
(!route.request.bodyMatch || new RegExp(`^${route.request.bodyMatch}$`, 'm').test(stringifiedBody))
) {
console.log(`Call to ${req.url} matched ${route.request.match} ${route.request.bodyMatch || ''}`);
const response = route.response;
res.status(response.status || 200);
res.setHeader("Content-Type", response.contentType || "application/json");
res.setHeader('Content-Type', response.contentType || 'application/json');
const body = response.bodies ? response.bodies.shift() : response.body;
res.send(response.contentType ? body : JSON.stringify(body));
if (response.bodies && response.bodies.length === 0) {
Expand All @@ -168,6 +171,4 @@ app.all("*", (req, res) => {
});
});

app.listen(PORT, HOST, () =>
console.log(`Smart mockserver running at ${HOST}:${PORT}`)
);
app.listen(PORT, HOST, () => console.log(`Smart mockserver running at ${HOST}:${PORT}`));
Loading

0 comments on commit cf273f9

Please sign in to comment.