diff --git a/lib/common.js b/lib/common.js index 33a02f4..06e4fc6 100644 --- a/lib/common.js +++ b/lib/common.js @@ -81,9 +81,26 @@ const receiveBody = async (req) => { return Buffer.concat(buffers).toString(); }; +const CRC_LEN = 4; + +const generateToken = (secret, characters, tokenLength) => { + const length = tokenLength - CRC_LEN; + if (length < 0 || secret === '' || characters === '') return ''; + const base = characters.length; + const uint8 = new Uint8Array(length); + for (let i = 0; i < length; i++) { + const index = crypto.randomInt(0, base); + uint8[i] = characters.charCodeAt(index); + } + const key = String.fromCharCode.apply(null, uint8); + const md5 = crypto.createHash('md5').update(key + secret); + return key + md5.digest('hex').substring(0, CRC_LEN); +}; + module.exports = Object.freeze({ hashPassword, validatePassword, jsonParse, receiveBody, + generateToken, }); diff --git a/lib/db.js b/lib/db.js index 302df4b..1b96bc0 100644 --- a/lib/db.js +++ b/lib/db.js @@ -8,11 +8,11 @@ const crud = (pool) => (table) => ({ return result.rows; }, - async read(id, fields = ['*']) { + async read(id, search = 'id', fields = ['*']) { const names = fields.join(', '); - const sql = `SELECT ${names} FROM ${table}`; + const sql = `SELECT ${names} FROM "${table}"`; if (!id) return pool.query(sql); - return pool.query(`${sql} WHERE id = $1`, [id]); + return pool.query(`${sql} WHERE "${search}" = $1`, [id]); }, async create({ ...record }) { @@ -40,15 +40,15 @@ const crud = (pool) => (table) => ({ updates[i] = `${key} = $${++i}`; } const delta = updates.join(', '); - const sql = `UPDATE ${table} SET ${delta} WHERE id = $${++i}`; + const sql = `UPDATE "${table}" SET ${delta} WHERE id = $${++i}`; data.push(id); return pool.query(sql, data); }, - async delete(id) { - const sql = 'DELETE FROM ${table} WHERE id = $1'; - return pool.query(sql, [id]); + async delete(id, field) { + const sql = `DELETE FROM "${table}" WHERE "${field}" = $1`; + return await pool.query(sql, [id]); }, }); -module.exports = { crud, pg }; +module.exports = (options) => crud(new pg.Pool(options)); diff --git a/main.js b/main.js index 59d9ce4..c39a247 100644 --- a/main.js +++ b/main.js @@ -25,7 +25,7 @@ const sandbox = vm.createContext({ console, common }); const domainPath = path.join(appPath, './domain'); const domain = await loadDir(domainPath, sandbox); - sandbox.db = require('./lib/db.js'); + sandbox.db = require('./lib/db.js')(config.database); const apiPath = path.join(appPath, './api'); const api = await loadDir(apiPath, sandbox, true); diff --git a/package-lock.json b/package-lock.json index b117fec..f18f059 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5145,4 +5145,4 @@ "dev": true } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index e2b6207..48559c8 100644 --- a/package.json +++ b/package.json @@ -28,4 +28,4 @@ "prettier": "^3.0.0", "typescript": "^5.1.6" } -} +} \ No newline at end of file diff --git a/src/server.js b/src/server.js index 419c12b..56339c7 100644 --- a/src/server.js +++ b/src/server.js @@ -176,7 +176,7 @@ class Server { }*/ this.console.log(`${client.ip}\t${packet.method}`); proc(context) - .method(packet.args) + .method(...packet.args) .then((result) => { if (result?.constructor?.name === 'Error') { const { code, httpCode = 200 } = result;