Skip to content

Fixes to comply with the nodejs-application #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
16 changes: 8 additions & 8 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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));
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
"prettier": "^3.0.0",
"typescript": "^5.1.6"
}
}
}
2 changes: 1 addition & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down