Skip to content

Commit

Permalink
filter out non mongodb options
Browse files Browse the repository at this point in the history
  • Loading branch information
cayasso committed Nov 29, 2018
1 parent 001dc30 commit 1bc6243
Showing 1 changed file with 101 additions and 89 deletions.
190 changes: 101 additions & 89 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
'use strict';
'use strict'

/**
* Module dependencies.
*/

import { MongoClient } from 'mongodb';
import uri from 'mongodb-uri';
import thunky from 'thunky';
import zlib from 'zlib';
import { MongoClient } from 'mongodb'
import uri from 'mongodb-uri'
import thunky from 'thunky'
import zlib from 'zlib'

/**
* Module constants.
*/

const noop = () => {};
const noop = () => {}

const OPTIONS_LIST = [
'port',
'host',
'username',
'password',
'database',
'collection',
'compression',
'engine',
'Promise',
'delimiter',
'prefix',
'ttl',
'count',
'hosts'
]

export default class MongoStore {

/**
* MongoStore constructor.
*
Expand All @@ -25,61 +41,58 @@ export default class MongoStore {
*/

constructor(conn, options = {}) {

let store = this;
let store = this

if ('object' === typeof conn) {
if ('function' !== typeof conn.collection) {
options = conn;
options = conn
if (Object.keys(options).length === 0) {
conn = null;
conn = null
} else if (options.client) {
this.client = options.client
} else {
options.database = options.database || options.db;
options.hosts = options.hosts || [{
port: options.port || 27017,
host: options.host || '127.0.0.1'
}];
conn = uri.format(options);
options.database = options.database || options.db
options.hosts = options.hosts || [
{
port: options.port || 27017,
host: options.host || '127.0.0.1'
}
]
conn = uri.format(options)
}
} else {
this.client = conn;
this.client = conn
}
}

conn = conn || 'mongodb://127.0.0.1:27017';
var coll = this.coll = options.collection || 'cacheman';
this.compression = options.compression || false;
conn = conn || 'mongodb://127.0.0.1:27017'
var coll = (this.coll = options.collection || 'cacheman')
this.compression = options.compression || false
this.ready = thunky((cb) => {

function createIndex(err, db) {
db.ensureIndex(coll, { 'expireAt': 1 }, { expireAfterSeconds: 0 }, err => {
cb(err, db);
});
db.ensureIndex(coll, { expireAt: 1 }, { expireAfterSeconds: 0 }, (err) => {
cb(err, db)
})
}

if ('string' === typeof conn) {
const {
port, host, username, password,
database, collection, compression, engine,
Promise, delimiter, prefix, ttl, count, hosts,
..._options
} = options;

MongoClient.connect(conn, _options, (err, db) => {
if (err) return cb(err);
createIndex(null, this.client = db);
db.ensureIndex(coll, { 'expireAt': 1 }, { expireAfterSeconds: 0 }, err => {
cb(err, db);
});
});
const mongoOptions = OPTIONS_LIST.reduce((opt, key) => {
delete opt[key]
return opt
}, Object.assign({}, options))

MongoClient.connect(conn, mongoOptions, (err, db) => {
if (err) return cb(err)
createIndex(null, (this.client = db))
db.ensureIndex(coll, { expireAt: 1 }, { expireAfterSeconds: 0 }, (err) => {
cb(err, db)
})
})
} else {
if (this.client) return createIndex(null, this.client);
cb(new Error('Invalid mongo connection.'));
if (this.client) return createIndex(null, this.client)
cb(new Error('Invalid mongo connection.'))
}
});

})
}

/**
Expand All @@ -92,23 +105,23 @@ export default class MongoStore {

get(key, fn = noop) {
this.ready((err, db) => {
if (err) return fn(err);
if (err) return fn(err)
db.collection(this.coll).findOne({ key: key }, (err, data) => {
if (err) return fn(err);
if (!data) return fn(null, null);
if (err) return fn(err)
if (!data) return fn(null, null)
//Mongo's TTL might have a delay, to fully respect the TTL, it is best to validate it in get.
if (data.expireAt.getTime() < Date.now()) {
this.del(key);
return fn(null, null);
this.del(key)
return fn(null, null)
}
try {
if (data.compressed) return decompress(data.value, fn);
fn(null, data.value);
if (data.compressed) return decompress(data.value, fn)
fn(null, data.value)
} catch (err) {
fn(err);
fn(err)
}
});
});
})
})
}

/**
Expand All @@ -122,45 +135,44 @@ export default class MongoStore {
*/

set(key, val, ttl, fn = noop) {

if ('function' === typeof ttl) {
fn = ttl;
ttl = null;
fn = ttl
ttl = null
}

let data;
let store = this;
let query = { key: key };
let options = { upsert: true, safe: true };
let data
let store = this
let query = { key: key }
let options = { upsert: true, safe: true }

try {
data = {
key: key,
value: val,
expireAt: new Date(Date.now() + ((ttl || 60) * 1000))
};
expireAt: new Date(Date.now() + (ttl || 60) * 1000)
}
} catch (err) {
return fn(err);
return fn(err)
}

this.ready((err, db) => {
if (err) return fn(err);
if (err) return fn(err)
if (!this.compression) {
update(data);
update(data)
} else {
compress(data, function compressData(err, data) {
if (err) return fn(err);
update(data);
});
if (err) return fn(err)
update(data)
})
}
function update(data) {
db.collection(store.coll).update(query, data, options, (err, data) => {
if (err) return fn(err);
if (!data) return fn(null, null);
fn(null, val);
});
if (err) return fn(err)
if (!data) return fn(null, null)
fn(null, val)
})
}
});
})
}

/**
Expand All @@ -173,9 +185,9 @@ export default class MongoStore {

del(key, fn = noop) {
this.ready((err, db) => {
if (err) return fn(err);
db.collection(this.coll).remove({ key: key }, { safe: true }, fn);
});
if (err) return fn(err)
db.collection(this.coll).remove({ key: key }, { safe: true }, fn)
})
}

/**
Expand All @@ -187,9 +199,9 @@ export default class MongoStore {

clear(fn = noop) {
this.ready((err, db) => {
if (err) return fn(err);
db.collection(this.coll).remove({}, { safe: true }, fn);
});
if (err) return fn(err)
db.collection(this.coll).remove({}, { safe: true }, fn)
})
}
}

Expand All @@ -207,18 +219,18 @@ export default class MongoStore {

function compress(data, fn) {
// Data is not of a "compressable" type (currently only Buffer)
if (!Buffer.isBuffer(data.value)) return fn(null, data);
if (!Buffer.isBuffer(data.value)) return fn(null, data)

zlib.gzip(data.value, (err, val) => {
// If compression was successful, then use the compressed data.
// Otherwise, save the original data.
if (!err) {
data.value = val;
data.compressed = true;
data.value = val
data.compressed = true
}
fn(err, data);
});
};
fn(err, data)
})
}

/**
* Decompress data value.
Expand All @@ -228,7 +240,7 @@ function compress(data, fn) {
* @api public
*/

function decompress(value, fn){
let v = (value.buffer && Buffer.isBuffer(value.buffer)) ? value.buffer : value;
zlib.gunzip(v, fn);
};
function decompress(value, fn) {
let v = value.buffer && Buffer.isBuffer(value.buffer) ? value.buffer : value
zlib.gunzip(v, fn)
}

0 comments on commit 1bc6243

Please sign in to comment.