Skip to content
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

Client tests #225

Open
wants to merge 80 commits into
base: client-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
954dbb1
Add redis instance
MarhiievHE May 4, 2023
c2b132c
fix redis
MarhiievHE May 4, 2023
744b2b1
update client test
MarhiievHE May 4, 2023
ca20fb5
npm run fmt
MarhiievHE May 4, 2023
63e16c8
fixup! fix redis
MarhiievHE May 4, 2023
730622f
fixup! update client test
MarhiievHE May 4, 2023
f13bccc
fix result message
MarhiievHE May 4, 2023
d3a6082
Apply suggestions from code review
MarhiievHE May 5, 2023
01b18d4
fixup! Apply suggestions from code review
MarhiievHE May 5, 2023
336cca8
Apply suggestions from code review
tshemsedinov May 5, 2023
5204859
Update test/client.js
tshemsedinov May 5, 2023
0842251
add redis config
MarhiievHE May 5, 2023
7306f03
update compose
MarhiievHE May 5, 2023
2c2549e
update base image to node v 20
MarhiievHE May 5, 2023
267a5e5
Removed unnecessary console.logs
MarhiievHE May 5, 2023
67317da
testing with pg and Redis containers
MarhiievHE May 8, 2023
e34937c
remove debug
MarhiievHE May 8, 2023
bd5aa68
Handle startup error to prevent app termination
tshemsedinov Apr 6, 2023
7f0096f
Update impress and metacom
tshemsedinov Apr 21, 2023
65ed136
Apply prettier fixes
tshemsedinov Apr 21, 2023
eed4337
Update browser-side metacom client
tshemsedinov Apr 21, 2023
5a23b3f
Convert package_lock.json to lockfileVersion 2
tshemsedinov May 3, 2023
e5a917a
Update dependencies
tshemsedinov May 3, 2023
cd3e68e
Drop node.js 14 support, add 20
tshemsedinov May 3, 2023
4aadf35
Create config to enable/disable examples
tshemsedinov May 6, 2023
20ba5b3
update test logic
MarhiievHE May 18, 2023
a218fc6
add break test on error
MarhiievHE May 18, 2023
a9c8962
Add an API startup wait
MarhiievHE May 18, 2023
0ffedd1
debug
MarhiievHE May 18, 2023
8898a50
increase test timeout
MarhiievHE May 18, 2023
0ab34ef
update uploader
MarhiievHE May 19, 2023
771ded4
increase start delay
MarhiievHE May 20, 2023
5ecfdf6
add delay
MarhiievHE May 20, 2023
cbf2009
update action
MarhiievHE May 20, 2023
476b3b0
fix get
MarhiievHE May 20, 2023
4f6922e
metacom master Not for landing!
MarhiievHE May 20, 2023
eaf30e3
add client test
MarhiievHE May 20, 2023
d1990b1
Rename ./db folder to ./database
tshemsedinov May 31, 2023
85d7385
fix: upgrade pg from 8.10.0 to 8.11.0 (#230)
snyk-bot Jun 7, 2023
696f36c
Add instructions to renew certificates with certbot
tshemsedinov May 31, 2023
ef0b6e5
Update application/cert folder structure
tshemsedinov Jun 5, 2023
5511f43
Change `application/cert` structure
tshemsedinov Jun 8, 2023
a893de6
Update to latest metarhia libraries
tshemsedinov Jun 8, 2023
5c1d5e3
Fix cache configuration
tshemsedinov Jun 12, 2023
d360471
Fix cache configuration
tshemsedinov Jun 12, 2023
df94712
Update Metarhia to 3.0
tshemsedinov Jun 30, 2023
7047f81
Error handling approach in Metarhia 3
tshemsedinov Jun 23, 2023
8702c90
Update dependencies (#237)
tshemsedinov Jul 23, 2023
3a06d60
Update dependencies
tshemsedinov Jul 31, 2023
6c98310
Remove autocreated directory
tshemsedinov Jul 31, 2023
4c17230
Version 3.0.0
tshemsedinov Jul 31, 2023
2b89e0f
Increase Node.js version in Dockerfile
naliferov Aug 1, 2023
9889ec9
fix README. Change Node ver, add line about docker
naliferov Aug 1, 2023
3632d4a
Change repo title in readme
tshemsedinov Aug 4, 2023
eb11e4f
Fix the path to the local file in require
MarhiievHE Aug 9, 2023
4ec38c4
increase start delay
MarhiievHE Aug 9, 2023
8fcb886
add TODO
MarhiievHE Aug 21, 2023
46261a7
Update impress to 3.0.3 and other dependencies
tshemsedinov Aug 17, 2023
124fcf7
Add package files to prettier ignore
tshemsedinov Aug 17, 2023
c90646e
Update static client example
tshemsedinov Aug 20, 2023
661cdf4
Update client metacom to 3.0.5
tshemsedinov Sep 9, 2023
d84fd18
Update dependencies
tshemsedinov Sep 9, 2023
9391e39
Remove spec recorder
tshemsedinov Sep 9, 2023
60a7962
Add virtual folder example
tshemsedinov Sep 12, 2023
c71812c
Add 404 template
tshemsedinov Sep 12, 2023
387ccff
Change relative urls to absolute
tshemsedinov Sep 12, 2023
596dc00
Update content to support new Cache format
tshemsedinov Sep 12, 2023
8350292
Update dependencies
tshemsedinov Sep 13, 2023
1b4e23c
Change eslint config for static
tshemsedinov Sep 13, 2023
bc8e2e7
fix prettier
MarhiievHE Sep 14, 2023
eacafc1
Add test mode
MarhiievHE Sep 14, 2023
dd31d8c
Switch to node test runner and assert
MarhiievHE Sep 14, 2023
fb8ee4a
Switch to metavm for are loading configs
MarhiievHE Sep 15, 2023
f9cfcbd
fix db init scripts path
MarhiievHE Sep 15, 2023
5de3b2b
update testing
MarhiievHE Sep 15, 2023
6e59324
update test
MarhiievHE Sep 15, 2023
9a129ae
update test
MarhiievHE Sep 15, 2023
fee12c7
remove test-api container
MarhiievHE Sep 15, 2023
d2fa9dd
used once function instead of Promice
MarhiievHE Sep 21, 2023
dc051cb
Add an example of subscribing and unsubscribing in API using Set
MarhiievHE Sep 21, 2023
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
22 changes: 20 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Testing CI

on: pull_request
on:
- pull_request
- push # not for landing

jobs:
build:
Expand All @@ -9,24 +11,40 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 19
- 20
os:
- ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Start db docker containers
run: |
docker-compose -f test-db-docker-compose.yml up -d
sleep 10 # wait for database to be ready

- name: Use Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- run: npm ci

- run: npm test

- name: Client test
run: |
node server.js > /dev/null&
sleep 4 # wait for api to be ready
node test/system.js
node --test test/client.js
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
application/tasks/
package-lock.json
package.json
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mhart/alpine-node:14
FROM node:20-alpine
WORKDIR /usr/server
COPY . .
RUN npm ci --only=production
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Metarhia Example Server for Node.js
# Metarhia application example for Node.js

[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/metarhia/Example/blob/master/LICENSE)

Expand Down Expand Up @@ -49,15 +49,16 @@ let's start with

## Usage

- You need node.js v14 or later
- You need node.js v16 or later
- Fork and clone this repository (optionally subscribe to repo changes)
- Run `npm i` to install dependencies and generate RSA certificate
- Remove unneeded dependencies if your project doesn't require them
- Add your license to `LICENSE` file but don't remove starter kit license
- Start your project modifying this starter kit
- If you have Docker and Docker Compose installed to run the project, use the command: `docker-compose up`
- Before running server initialize the DB:
- First of all, make sure you have PostgreSQL installed (prefer 12.x).
- Run database initialization script: `db/setup.sh`
- Run database initialization script: `database/setup.sh`
- Run project: `node server.js` and stop with Ctrl+C
- Ask questions in https://t.me/nodeua and post issues on
[github](https://github.com/HowProgrammingWorks/NodejsStarterKit/issues)
Expand Down
3 changes: 2 additions & 1 deletion application/api/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"db": "readonly",
"bus": "readonly",
"domain": "readonly",
"metarhia": "readonly"
"metarhia": "readonly",
"DomainError": "readonly"
}
}
1 change: 0 additions & 1 deletion application/api/chat.1/send.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
async ({ room, message }) => {
domain.chat.send(room, message);
return 'ok';
};
1 change: 0 additions & 1 deletion application/api/chat.1/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ async ({ room }) => {
context.client.on('close', () => {
clients.delete(context.client);
});
return 'ok';
};
8 changes: 8 additions & 0 deletions application/api/console.1/content.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api.console.content {
type Code = 'ENOTFOUND' | 'EPARSE';

class CustomError extends DomainError {
constructor(code?: Code);
toJSON(): object;
}
}
10 changes: 7 additions & 3 deletions application/api/console.1/content.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
({
access: 'public',

async method({ name }) {
// Try type: new api.console.content.CustomError('EPARSE');
const filePath = `/content/${name}.md`;
const buffer = application.resources.get(filePath);
if (!buffer) return new Error('Content is not found');
return { text: buffer.toString() };
const file = application.resources.get(filePath);
if (!file) return new Error('Content is not found');
return { text: file.data.toString() };
},

CustomError: class CustomError extends Error {},
});
8 changes: 8 additions & 0 deletions application/api/example.1/add.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api.example.add {
type Code = 'EARGA' | 'EARGB';

class CustomError extends DomainError {
constructor(code?: Code);
toJSON(): object;
}
}
11 changes: 11 additions & 0 deletions application/api/example.1/add.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
({
access: 'public',

parameters: {
a: 'number',
b: 'number',
},

method: async ({ a, b }) => {
if (a < 0) return new DomainError('EARGA');
if (b > 500) return new DomainError('EARGB');
if (Number.isNaN(a)) throw Error('Not a number: a');
if (Number.isNaN(b)) throw Error('Not a number: b');
const result = a + b;
return result;
},

returns: 'number',

errors: {
EARGA: 'Invalid argument: "a" expected to be > 0',
EARGB: 'Invalid argument: "b" expected to be < 500',
},
});
6 changes: 4 additions & 2 deletions application/api/example.1/getClientInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
method: async () => {
const { uuid, session, client } = context;
const { ip } = client;
const { token, accountId } = session;
return { result: { ip, token, accountId, uuid } };
const { token } = session;
const accountId = session.state.accountId;
const result = { ip, token, accountId, uuid };
return { result };
},
});
2 changes: 1 addition & 1 deletion application/api/example.1/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
router({ method, args, verb, headers }) {
const ip = context.client.ip;
console.log({ method, args, ip, verb, headers });
return {success:true, method, args, ip, verb, headers };
return { success: true, method, args, ip, verb, headers };
},
});
2 changes: 1 addition & 1 deletion application/api/example.1/redisGet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
({
access: 'public',
method: async ({ key }) => {
const result = await lib.redis.get(key);
const result = await db.redis.get(key);
return { result };
},
});
2 changes: 1 addition & 1 deletion application/api/example.1/redisSet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
({
access: 'public',
method: async ({ key, value }) => {
const result = await lib.redis.set(key, value);
const result = await db.redis.set(key, value);
return { result };
},
});
19 changes: 12 additions & 7 deletions application/api/example.1/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
async () => {
setInterval(async () => {
const stats = await lib.resmon.getStatistics();
context.client.emit('example/resmon', stats);
}, config.resmon.interval);
return { subscribed: 'resmon' };
};
({
access: 'public',

method: async () => {
const { client } = context;
domain.resmon.subscribe(client);
context.client.on('close', () => {
domain.resmon.unsubscribe(client);
});
return { subscribed: 'resmon' };
},
});
9 changes: 9 additions & 0 deletions application/api/example.1/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
access: 'public',

method: async () => {
const { client } = context;
domain.resmon.unsubscribe(client);
return { unsubscribed: 'resmon' };
},
});
2 changes: 1 addition & 1 deletion application/api/example.1/uploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ async ({ name, data }) => {
if (filePath.startsWith(tmpPath)) {
await node.fsp.writeFile(filePath, buffer);
}
return { uploaded: data.length };
return { uploaded: data.data.length };
};
3 changes: 3 additions & 0 deletions application/api/files.1/upload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
async ({ streamId, name }) => {
const filePath = `./application/resources/${name}`;
const toBool = [() => true, () => false];
const exists = await node.fsp.access(filePath).then(...toBool);
if (exists) return { result: `File ${name} already exist on server` };
// Get incoming stream by streamId sent from client
const readable = context.client.getStream(streamId);
// Create nodejs stream to write file on server
Expand Down
2 changes: 1 addition & 1 deletion application/api/hook.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
router({ method, args, verb, headers }) {
const ip = context.client.ip;
console.log({ method, args, ip, verb, headers });
return {};
return { success: true, args };
},
});
24 changes: 0 additions & 24 deletions application/cert/README.md

This file was deleted.

24 changes: 24 additions & 0 deletions application/cert/default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generate certificates

## Certbot (for production)

- Let's Encrypt is a free certificate authority: https://letsencrypt.org/
- Use Certbot (free tool for automatically using Let’s Encrypt certificates on
manually-administrated websites to enable HTTPS): https://certbot.eff.org/

```
dnf -y install certbot
certbot certonly --standalone -d www.domain.com -d domain.com -m [email protected] --agree-tos --no-eff-email
yes | cp /etc/letsencrypt/live/domain.com/fullchain.pem ~/domain.com/application/cert/cert.pem
yes | cp /etc/letsencrypt/live/domain.com/privkey.pem ~/domain.com/application/cert/key.pem
```

Or use impress web server for challenge exchange:

```
certbot certonly --webroot -w ~/domain.com/application/static -d www.domain.com -d domain.com -m [email protected] --agree-tos --no-eff-email
```

## Self-signed (for testing)

- Run `./generate.sh` in this directory
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ subjectAltName = @alt_names

[alt_names]
DNS.1 = example.com
DNS.2 = hello.example.com
DNS.3 = hello1.example.com
DNS.4 = hello2.example.com
IP.1 = 127.0.0.1
IP.2 = ::1
File renamed without changes.
4 changes: 2 additions & 2 deletions application/config/cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
({
size: '50mb',
maxFileSize: '10mb',
size: '50 mb',
maxFileSize: '10 mb',
});
3 changes: 0 additions & 3 deletions application/config/example.js

This file was deleted.

7 changes: 7 additions & 0 deletions application/config/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
({
bus: false,
scheduler: false,
metacom: false,
invoke: false,
plugin: false,
});
9 changes: 9 additions & 0 deletions application/config/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
({
apiRedis: {
socket: {
host: process.env.REDIS_HOST ?? '127.0.0.1',
port: process.env.REDIS_PORT ?? 6379,
},
password: process.env.PASS,
},
});
2 changes: 1 addition & 1 deletion application/config/resmon.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
({
interval: 30000,
active: true,
active: false,
});
1 change: 0 additions & 1 deletion application/db/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"npm": "readonly",
"lib": "readonly",
"db": "readonly",
"bus": "readonly",
"domain": "readonly",
"metarhia": "readonly"
}
Expand Down
8 changes: 1 addition & 7 deletions application/db/redis/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
(key) =>
new Promise((resolve, reject) => {
lib.redis.client.get(key, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
(key) => db.redis.client.get(key);
8 changes: 1 addition & 7 deletions application/db/redis/set.js
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
(key, value) =>
new Promise((resolve, reject) => {
lib.redis.client.set(key, value, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
async (key, value) => db.redis.client.set(key, value);
4 changes: 2 additions & 2 deletions application/db/redis/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ async () => {
if (application.worker.id === 'W1') {
console.debug('Connect to redis');
}
const client = npm.redis.createClient();
const client = npm.redis.createClient(config.redis.apiRedis);
db.redis.client = client;
client.on('error', () => {
if (application.worker.id === 'W1') {
console.warn('No redis service detected, so quit client');
}
client.quit();
});
//await client.connect();
await client.connect();
};
Loading