Skip to content
Closed
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
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
],
"quotes": [
"error",
"single"
"single",
{
"allowTemplateLiterals": true
}
],
"semi": [
"error",
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ node_js:
services:
- postgresql
before_script:
- psql -f db/install.sql -U postgres
- PGPASSWORD=marcus psql -d application -f db/structure.sql -U marcus
- PGPASSWORD=marcus psql -d application -f db/data.sql -U marcus
- psql -f application/db/install.sql -U postgres
- PGPASSWORD=marcus psql -d application -f application/db/structure.sql -U marcus
- PGPASSWORD=marcus psql -d application -f application/db/data.sql -U marcus
script:
- npm test
1 change: 0 additions & 1 deletion api/introspection.js

This file was deleted.

9 changes: 0 additions & 9 deletions api/resmon.js

This file was deleted.

17 changes: 17 additions & 0 deletions application/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"parserOptions": {
"sourceType": "module"
},
"rules": {
"id-denylist": [
2,
"application",
"node",
"npm",
"api",
"lib",
"domain",
"config"
]
}
}
11 changes: 11 additions & 0 deletions application/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"globals": {
"application": "readonly",
"config": "readonly",
"context": "readonly",
"node": "readonly",
"npm": "readonly",
"lib": "readonly",
"domain": "readonly"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
async ({ countryId }) => {
const fields = ['Id', 'Name'];
const where = { countryId };
const data = await application.db.select('City', fields, where);
const data = await domain.database.example.select('City', fields, where);
return { result: 'success', data };
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
async () => {
const fields = ['Id', 'Name'];
const data = await application.db.select('Country', fields);
const data = await domain.database.example.select('Country', fields);
return { result: 'success', data };
};
1 change: 1 addition & 0 deletions application/api/example.1/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
async () => new Error('Hello!', 54321);
3 changes: 3 additions & 0 deletions application/api/example.1/exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
async () => {
throw new Error('Hello', 12345);
};
1 change: 1 addition & 0 deletions application/api/example.1/getUndefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
async () => undefined;
7 changes: 7 additions & 0 deletions application/api/example.1/remoteMethod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
({
access: 'public',
method: async ({ ...args }) => {
console.debug({ remoteMethod: args });
return { result: 'success' };
}
});
9 changes: 9 additions & 0 deletions application/api/example.1/resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async () => {
const loadavg = node.os.loadavg();
const stats = lib.resmon.getStatistics();
const { heapTotal, heapUsed, external, contexts } = stats;
const total = lib.utils.bytesToSize(heapTotal);
const used = lib.utils.bytesToSize(heapUsed);
const ext = lib.utils.bytesToSize(external);
return { total, used, ext, contexts, loadavg };
};
7 changes: 7 additions & 0 deletions application/api/example.1/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async () => {
setInterval(() => {
const stats = lib.resmon.getStatistics();
context.client.emit('example/resmon', stats);
}, config.resmon.interval);
return { subscribed: 'resmon' };
};
9 changes: 9 additions & 0 deletions application/api/example.1/uploadFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
async ({ name, data }) => {
const buffer = Buffer.from(data, 'base64');
const tmpPath = 'application/tmp';
const filePath = node.path.join(tmpPath, name);
if (filePath.startsWith(tmpPath)) {
await node.fsp.writeFile(filePath, buffer);
}
return { uploaded: data.length };
};
3 changes: 3 additions & 0 deletions application/api/example.1/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
async ({ delay }) => new Promise(resolve => {
setTimeout(resolve, delay, 'done');
});
7 changes: 7 additions & 0 deletions application/api/example.1/webHook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
({
access: 'public',
method: async ({ ...args }) => {
console.debug({ webHook: args });
return { result: 'success' };
}
});
4 changes: 4 additions & 0 deletions application/api/system.1/introspect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
access: 'public',
method: application.introspect
});
File renamed without changes.
5 changes: 5 additions & 0 deletions application/cert/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cd "$(dirname "$0")"
openssl genrsa -out key.pem 3072
openssl req -new -out self.pem -key key.pem -subj '/CN=localhost'
openssl req -text -noout -in self.pem
openssl x509 -req -days 1024 -in self.pem -signkey key.pem -out cert.pem -extfile generate.ext
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion config/server.js → application/config/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
({
host: '127.0.0.1',
ports: [8000, 8001, 8002, 8003],
balancer: 8000,
protocol: 'http',
ports: [8001, 8002],
timeout: 5000,
concurrency: 1000,
queue: {
size: 2000,
timeout: 3000,
},
workers: {
pool: 2,
timeout: 3000,
},
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions domain/.eslintrc.json → application/domain/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
},
"globals": {
"application": "readonly",
"context": "readonly",
"api": "readonly"
"config": "readonly",
"node": "readonly",
"npm": "readonly",
"lib": "readonly",
"domain": "readonly"
}
}
4 changes: 4 additions & 0 deletions application/domain/database/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(async () => {
console.debug('Connect to pg');
domain.database.example = new lib.pg.Database(config.database);
});
16 changes: 16 additions & 0 deletions application/lib/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"rules": {
"strict": [
"error",
"never"
]
},
"globals": {
"application": "readonly",
"config": "readonly",
"node": "readonly",
"npm": "readonly",
"lib": "readonly",
"domain": "readonly"
}
}
13 changes: 13 additions & 0 deletions application/lib/example/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
({
parameters: {
a: 'number',
b: 'number',
},

method({ a, b }) {
console.log({ a, b });
return a + b;
},

returns: 'number',
});
15 changes: 15 additions & 0 deletions application/lib/example/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
({
values: new Map(),

set({ key, val }) {
console.log({ set: { key, val } });
return this.values.set(key, val);
},

get({ key }) {
console.log({ get: key });
const res = this.values.get(key);
console.log({ return: res });
return res;
}
});
3 changes: 3 additions & 0 deletions application/lib/example/doSomething.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.doSomething');
});
10 changes: 10 additions & 0 deletions application/lib/example/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
({
privateField: 100,

async method() {
console.log('Start example plugin');
this.parent.cache.set({ key: 'keyName', val: this.privateField });
const res = lib.example.cache.get({ key: 'keyName' });
console.log({ res, cache: this.parent.cache.values });
}
});
3 changes: 3 additions & 0 deletions application/lib/example/stop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(async () => {
console.debug('Stop example plugin');
});
13 changes: 13 additions & 0 deletions application/lib/example/storage/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
({
values: new Map(),

method({ key, val }) {
console.log({ key, val });
if (val) {
return this.values.set(key, val);
}
const res = this.values.get(key);
console.log({ return: { res } });
return res;
}
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule1/method1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule1.method1');
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule1/method2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule1.method2');
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule2/method1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule2.method1');
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule2/method2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule2.method2');
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule2/nested1/method1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule2.nested1.method1');
});
3 changes: 3 additions & 0 deletions application/lib/example/submodule3/nested2/method1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
console.debug('Call method: example.submodule3.nested2.method1');
});
58 changes: 58 additions & 0 deletions application/lib/pg/Database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(class Database {
constructor(options) {
this.pool = new npm.pg.Pool(options);
}

query(sql, values) {
const data = values ? values.join(',') : '';
console.debug(`${sql}\t[${data}]`);
return this.pool.query(sql, values);
}

insert(table, record) {
const keys = Object.keys(record);
const nums = new Array(keys.length);
const data = new Array(keys.length);
let i = 0;
for (const key of keys) {
data[i] = record[key];
nums[i] = `$${++i}`;
}
const fields = keys.join(', ');
const params = nums.join(', ');
const sql = `INSERT INTO ${table} (${fields}) VALUES (${params})`;
return this.query(sql, data);
}

async select(table, fields = ['*'], conditions = null) {
const keys = fields.join(', ');
const sql = `SELECT ${keys} FROM ${table}`;
let whereClause = '';
let args = [];
if (conditions) {
const whereData = lib.pg.utils.where(conditions);
whereClause = ' WHERE ' + whereData.clause;
args = whereData.args;
}
const res = await this.query(sql + whereClause, args);
return res.rows;
}

delete(table, conditions = null) {
const { clause, args } = lib.pg.utils.where(conditions);
const sql = `DELETE FROM ${table} WHERE ${clause}`;
return this.query(sql, args);
}

update(table, delta = null, conditions = null) {
const upd = lib.pg.utils.updates(delta);
const cond = lib.pg.utils.where(conditions, upd.args.length + 1);
const sql = `UPDATE ${table} SET ${upd.clause} WHERE ${cond.clause}`;
const args = [...upd.args, ...cond.args];
return this.query(sql, args);
}

close() {
this.pool.end();
}
});
3 changes: 3 additions & 0 deletions application/lib/pg/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
OPERATORS: ['>=', '<=', '<>', '>', '<']
});
12 changes: 12 additions & 0 deletions application/lib/pg/updates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
((delta, firstArgIndex = 1) => {
const clause = [];
const args = [];
let i = firstArgIndex;
const keys = Object.keys(delta);
for (const key of keys) {
const value = delta[key].toString();
clause.push(`${key} = $${i++}`);
args.push(value);
}
return { clause: clause.join(', '), args };
});
26 changes: 26 additions & 0 deletions application/lib/pg/where.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
((conditions, firstArgIndex = 1) => {
const clause = [];
const args = [];
let i = firstArgIndex;
const keys = Object.keys(conditions);
for (const key of keys) {
let operator = '=';
let value = conditions[key];
if (typeof value === 'string') {
for (const op of lib.pg.constants.OPERATORS) {
const len = op.length;
if (value.startsWith(op)) {
operator = op;
value = value.substring(len);
}
}
if (value.includes('*') || value.includes('?')) {
operator = 'LIKE';
value = value.replace(/\*/g, '%').replace(/\?/g, '_');
}
}
clause.push(`${key} ${operator} $${i++}`);
args.push(value);
}
return { clause: clause.join(' AND '), args };
});
7 changes: 7 additions & 0 deletions application/lib/resmon/getStatistics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(() => {
const { heapTotal, heapUsed, external } = node.process.memoryUsage();
const hs = node.v8.getHeapStatistics();
const contexts = hs.number_of_native_contexts;
const detached = hs.number_of_detached_contexts;
return { heapTotal, heapUsed, external, contexts, detached };
});
Loading