<commit_before>'use strict';

import express from 'express';
import http from 'http';
import { MongoClient, ObjectID } from 'mongodb';
import { escape } from 'querystring';
import nepq from 'nepq';
import uuid from 'node-uuid';
import compression from 'compression';
import config from './config';

var connectionUri = (() => {
  let { user, pwd, host, port, maxPoolSize } = config.database;
  return `mongodb://${(user && pwd) ? `${user}:${escape(pwd)}@` : ''}${host || 'localhost'}:${port || 27017}/?maxPoolSize=${maxPoolSize}`;
})();

function objectId(id) {
  let _id = null;
  try {
    _id = ObjectID.createFromHexString(id);
  } catch (e) { }
  return _id;
}

function json(s) {
  let r = {};
  try {
    r = JSON.parse(s);
  } catch (e) { }
  return r;
}
/*
interface User {
  _id: mongodb.ObjectID;
  username: string;
  password: <PASSWORD>;
  role: mongodb.ObjectID;
}

interface Token {
  _id: mongodb.ObjectID;
  token: string;
  user: mongodb.ObjectID;
  timestamp: number;
  expire: number;
}

interface Role {
  _id: mongodb.ObjectID;
  name: string;
  auth: any;
}
*/

var app = express();

app.use(compression(config.compression));

app.set('x-powered-by', false);

var db;

MongoClient.connect(connectionUri, (err, database) => {
  if (err) throw err;

  db = database;

  let port = config.port || 8000;

  http.createServer(app).listen(port, () => {
    console.log(`Server listening on port ${port}`);
  });
});

var nq = nepq();

function resp(err, r) {
  if (err) {
    nq.res.status(520).json(err);
  } else {
    nq.send(r);
  }
}

function ns() {
  let q = nq.request;
  let d = q.namespace.shift();
  let c = q.namespace.join('.');
  c = c ? `${c}.${q.name}` : q.name;
  return [ d, c ];
}

function auth() {
  let q = nq.request;
  // TODO: auth logic
  return true;
}

nq.on('auth', null, 'login', q => {
  console.log(q);
  let _db = q.namespace.shift();
  db.db(_db).collection('nepq.user').findOne({
    username: q.param.username,
    password: <PASSWORD>
  }, (err, r) => {
    if (err) return resp(err);
    if (!r) return resp(401);

    // create token
    let token = uuid.v4();
    db.db(_db).collection('nepq.token').insert({
      token: token,
      user: r._id,
      created: Date.now(),
      expire: Date.now() + (q.param.remember ? config.auth.expire.remember : config.auth.expire.time)
    }, (err) => {
      if (err) return resp(500);
      nq.response({ token: token });
    });
  });
});

function extendTime(token) {
  // TODO: add config.suth.expire.time to token
}

nq.on('auth', null, 'logout', q => {
  let _db = q.namespace.shift();

});

nq.on('create', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();
  db.db(d).collection(c).insertMany(q.param, { w: 1 }, resp);
});

nq.on('$create', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();
  db.db(d).collection(c).insertOne(q.param, { w: 1 }, resp);
});

nq.on('read', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();

  let x = q.param;
  let opt = {};

  if (q.param.length >= 2) {
    x = q.param[0];
    opt = q.param[1];
  }

  opt = {
    limit: opt.limit || 0,<commit_msg>fake auth<commit_after>'use strict';

import express from 'express';
import http from 'http';
import { MongoClient, ObjectID } from 'mongodb';
import { escape } from 'querystring';
import nepq from 'nepq';
import uuid from 'node-uuid';
import compression from 'compression';
import config from './config';

var connectionUri = (() => {
  let { user, pwd, host, port, maxPoolSize } = config.database;
  return `mongodb://${(user && pwd) ? `${user}:${escape(pwd)}@` : ''}${host || 'localhost'}:${port || 27017}/?maxPoolSize=${maxPoolSize}`;
})();

function objectId(id) {
  let _id = null;
  try {
    _id = ObjectID.createFromHexString(id);
  } catch (e) { }
  return _id;
}

function json(s) {
  let r = {};
  try {
    r = JSON.parse(s);
  } catch (e) { }
  return r;
}
/*
interface User {
  _id: mongodb.ObjectID;
  username: string;
  password: <PASSWORD>;
  role: mongodb.ObjectID;
}

interface Token {
  _id: mongodb.ObjectID;
  token: string;
  user: mongodb.ObjectID;
  timestamp: number;
  expire: number;
}

interface Role {
  _id: mongodb.ObjectID;
  name: string;
  auth: any;
}
*/

var app = express();

app.use(compression(config.compression));

app.set('x-powered-by', false);

var db;

MongoClient.connect(connectionUri, (err, database) => {
  if (err) throw err;

  db = database;

  let port = config.port || 8000;

  http.createServer(app).listen(port, () => {
    console.log(`Server listening on port ${port}`);
  });
});

var nq = nepq();

function resp(err, r) {
  if (err) {
    nq.res.status(520).json(err);
  } else {
    nq.send(r);
  }
}

function ns() {
  let q = nq.request;
  let d = q.namespace.shift();
  let c = q.namespace.join('.');
  c = c ? `${c}.${q.name}` : q.name;
  return [ d, c ];
}

function auth() {
  let q = nq.request;
  // TODO: auth logic
  return true;
}

nq.on('auth', null, 'login', q => {
  let [ d, c ] = ns();
  nq.send({token: "<PASSWORD>"});
  /*nq.res.status(401).json({
    name: 'NepDB',
    message: 'Unauthorized'
  });*/
  /*
  let _db = q.namespace.shift();
  db.db(_db).collection('nepq.user').findOne({
    username: q.param.username,
    password: q.param.password
  }, (err, r) => {
    if (err) return resp(err);
    if (!r) return resp(401);

    // create token
    let token = uuid.v4();
    db.db(_db).collection('nepq.token').insert({
      token: token,
      user: r._id,
      created: Date.now(),
      expire: Date.now() + (q.param.remember ? config.auth.expire.remember : config.auth.expire.time)
    }, (err) => {
      if (err) return resp(500);
      nq.response({ token: token });
    });
  });*/
});

function extendTime(token) {
  // TODO: add config.suth.expire.time to token
}

nq.on('auth', null, 'logout', q => {
  let [ d, c ] = ns();
  nq.send();
});

nq.on('create', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();
  db.db(d).collection(c).insertMany(q.param, { w: 1 }, resp);
});

nq.on('$create', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();
  db.db(d).collection(c).insertOne(q.param, { w: 1 }, resp);
});

nq.on('read', null, null, q => {
  if (!auth()) return;
  let [ d, c ] = ns();

  let x = q.param;
  let opt = {};

  if (q.param.length >= 2) {
    x = q.param[0];
    opt = q.param[1];
  }

  opt = {
    limit: opt.limit || 0,
{'fraction_non_alphanumeric': 0.20552865844135562, 'fraction_numerical': 0.008277369982820552, 'mean_word_length': 3.0686149936467597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6383883', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2459, 'n_words': 590}