{"size": 2538, "ext": "js", "max_stars_repo_path": "src/server.js", "max_stars_repo_name": "bryanchriswhite/fuzz-rest-api", "max_stars_repo_stars_event_min_datetime": "2018-04-21T21:46:08.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-23T20:19:59.000Z", "max_issues_repo_path": "src/server.js", "max_issues_repo_name": "bryanchriswhite/fuzz-rest-api", "max_issues_repo_issues_event_min_datetime": "2019-02-24T19:44:33.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-07T19:19:45.000Z", "max_forks_repo_path": "src/server.js", "max_forks_repo_name": "bryanchriswhite/fuzz-rest-api", "max_forks_repo_forks_event_min_datetime": "2019-04-12T14:08:30.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-12T11:53:14.000Z"}
{"max_stars_count": 40, "max_issues_count": 2, "max_forks_count": 7, "avg_line_length": 33.3947368421, "max_line_length": 112, "alphanum_fraction": 0.6016548463}
const express = require('express');
const wrap = require('async-middleware').wrap;
const app = express();
const bodyParser = require('body-parser');
const sqlite = require('sqlite');

// DB warming up code
let db = null;
const warmupDB = async dbName => {
  console.log(`Warming up database ${dbName}`);
  if (db !== null) return;
  db = await sqlite.open(dbName, { Promise });
  await db.run('CREATE TABLE IF NOT EXISTS Users (id INTEGER PRIMARY KEY, username TEXT, password TEXT)');
  const userdatas = await db.all(`SELECT * FROM Users`);
  if (userdatas.length === 0) await db.run("INSERT INTO Users (username, password) VALUES ('admin', 'admin')");
};

// Configure routes
const router = express.Router();
router.post(
  // DB injection no escape on received POST arguments
  '/login',
  wrap(async (req, res) => {
    const params = req.body;
    console.log(`POST - /login - ${JSON.stringify({ username: params.username, password: params.password })}`);
    const userdata = await db.get(
      `SELECT * FROM Users WHERE username='${params.username}' AND password='${params.password}'`
    );
    res.json({ status: userdata ? 'success' : 'failure' });
  })
);
router.get(
  // DB injection no escape on urls params
  '/profile/:uid',
  wrap(async (req, res) => {
    const uid = req.params.uid;
    console.log(`GET  - /profile/${uid}`);
    const userdata = await db.get(`SELECT * FROM Users WHERE id=${uid}`);
    res.json({ query: { uid }, profile: userdata });
  })
);
router.post(
  // Much deaper payload
  '/comment',
  wrap(async (req, res) => {
    // payload:
    // {
    //   user: { login: 'toto' },
    //   comment: { message: 'lorem ipsum', postId: 5, commentId: 8, public: false, details: ["", 5] }
    // }
    const params = req.body;
    console.log(`POST - /comment - ${JSON.stringify({ ...params })}`);
    if (
      params.user == null ||
      params.user.login == null ||
      params.comment == null ||
      typeof params.comment.postId !== 'number'
    ) {
      res.json({ answer: 'bad request' });
      return;
    }
    if (typeof params.comment.commentId === 'string') throw new Error('Supposed it failed on this case');
    res.json({});
  })
);

// Lauching the server
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/api', router);
warmupDB('db.sqlite').then(() => {
  const port = process.env.PORT || 8080;
  app.listen(port);
  console.log(`Listening on port ${port}`);
});
