-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Adamant-im/dev
v1.9.0: ComServer, FameEX support
- Loading branch information
Showing
40 changed files
with
4,906 additions
and
2,407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/trade/settings | ||
/trade/tests | ||
/trade/co_test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,78 @@ | ||
module.exports = (db) => { | ||
return class { | ||
constructor(data = {}, isSave) { | ||
class Model { | ||
constructor(data = {}, shouldSave) { | ||
this.db = db; | ||
|
||
Object.assign(this, data); | ||
if (isSave) { | ||
|
||
if (shouldSave) { | ||
this.save(); | ||
} | ||
} | ||
static get db() { | ||
return db; | ||
} | ||
static find(a) { // return Array | ||
return new Promise((resolve, reject) => { | ||
this.db.find(a).toArray((err, data) => { | ||
resolve(data.map((d)=>new this(d))); | ||
}); | ||
}); | ||
static async find(req) { | ||
const data = await db.find(req).toArray(); | ||
|
||
return data.map((d) => new this(d)); | ||
} | ||
static aggregate(a) { // return Array | ||
return new Promise((resolve, reject) => { | ||
this.db.aggregate(a).toArray((err, data) => { | ||
resolve(data.map((d)=>new this(d))); | ||
}); | ||
}); | ||
static async aggregate(req) { | ||
const data = await db.aggregate(req).toArray(); | ||
|
||
return data.map((d) => new this(d)); | ||
} | ||
static findOne(a) { | ||
return new Promise((resolve, reject) => { | ||
db.findOne(a).then((doc, b) => { | ||
if (!doc) { | ||
resolve(doc); | ||
} else { | ||
resolve(new this(doc)); | ||
} | ||
}); | ||
}); | ||
static async findOne(req) { | ||
const doc = await db.findOne(req); | ||
|
||
return doc ? new this(doc) : doc; | ||
} | ||
static deleteOne(a) { | ||
return new Promise((resolve, reject) => { | ||
delete a.db; | ||
db.deleteOne(a).then((res) => { | ||
resolve(res.deletedCount); | ||
}); | ||
}); | ||
static async deleteOne(req) { | ||
delete req.db; | ||
|
||
const { deletedCount } = await db.deleteOne(req); | ||
|
||
return deletedCount; | ||
} | ||
static count(a) { | ||
return new Promise((resolve, reject) => { | ||
db.count(a).then((count) => { | ||
resolve(count); | ||
}); | ||
}); | ||
static async count(req) { | ||
const count = await db.count(req); | ||
|
||
return count; | ||
} | ||
_data() { | ||
const data = {}; | ||
for (const field in this) { | ||
if (!['db', '_id'].includes(field)) { | ||
data[field] = this[field]; | ||
|
||
for (const fieldName in this) { | ||
if (Object.prototype.hasOwnProperty.call(this, fieldName)) { | ||
if (!['db', '_id'].includes(fieldName)) { | ||
data[fieldName] = this[fieldName]; | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
async update(obj, isSave) { | ||
async update(obj, shouldSave) { | ||
Object.assign(this, obj); | ||
if (isSave) { | ||
|
||
if (shouldSave) { | ||
await this.save(); | ||
} | ||
} | ||
save() { | ||
return new Promise((resolve, reject) => { | ||
if (!this._id) { | ||
db.insertOne(this._data(), (err, res) => { | ||
this._id = res.insertedId; | ||
resolve(this._id); | ||
}); | ||
} else { | ||
db.updateOne({ _id: this._id }, { | ||
$set: this._data(), | ||
}, { upsert: true }).then(() => { | ||
resolve(this._id); | ||
}); | ||
} | ||
}); | ||
async save() { | ||
if (!this._id) { | ||
const res = await db.insertOne(this._data()); | ||
this._id = res.insertedId; | ||
return this._id; | ||
} else { | ||
await db.updateOne({ _id: this._id }, { | ||
$set: this._data(), | ||
}, { upsert: true }); | ||
|
||
return this._id; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
return Model; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const crypto = require('crypto'); | ||
const config = require('../modules/config/reader'); | ||
|
||
const iv = crypto.randomBytes(16); | ||
const secretKey = crypto | ||
.createHash('sha256') | ||
.update(String(config.com_server_secret_key)) | ||
.digest('base64') | ||
.substr(0, 32); | ||
|
||
const encrypt = (text) => { | ||
const cipher = crypto.createCipheriv('aes-256-ctr', secretKey, iv); | ||
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]); | ||
|
||
return { | ||
iv: iv.toString('hex'), | ||
content: encrypted.toString('hex'), | ||
}; | ||
}; | ||
|
||
const decrypt = (hash) => { | ||
const decipher = crypto.createDecipheriv('aes-256-ctr', secretKey, Buffer.from(hash.iv, 'hex')); | ||
const decrypted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')), decipher.final()]); | ||
|
||
return decrypted.toString(); | ||
}; | ||
|
||
module.exports = { encrypt, decrypt }; |
Oops, something went wrong.