@radatek/microserver 2.0.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/microserver.d.ts +72 -35
- package/dist/microserver.js +349 -243
- package/microserver.ts +3803 -0
- package/package.json +9 -4
- package/readme.md +50 -47
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radatek/microserver",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "HTTP MicroServer",
|
|
5
5
|
"author": "Darius Kisonas",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,14 +11,19 @@
|
|
|
11
11
|
"httpserver"
|
|
12
12
|
],
|
|
13
13
|
"type": "module",
|
|
14
|
-
"main": "
|
|
15
|
-
"module": "./dist/microserver.js",
|
|
16
|
-
"types": "./dist/microserver.d.ts",
|
|
14
|
+
"main": "microserver.ts",
|
|
17
15
|
"files": [
|
|
18
16
|
"dist"
|
|
19
17
|
],
|
|
20
18
|
"repository": {
|
|
21
19
|
"type": "git",
|
|
22
20
|
"url": "git+https://github.com/radateklt/microserver.git"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/node": "^22.19.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "pnpm --package typescript dlx tsc -d -m nodenext -t es2021 microserver.ts --outDir dist && sed -Ei 's/\\\"use strict\\\";\\n\\n//g' dist/microserver.js && sed -Ei ':a;N;$!ba;s/\\n +private _[^\\n]+//g;s/export \\{\\};//g' dist/microserver.d.ts",
|
|
27
|
+
"test": "node test.ts"
|
|
23
28
|
}
|
|
24
29
|
}
|
package/readme.md
CHANGED
|
@@ -1,35 +1,36 @@
|
|
|
1
1
|
## HTTP MicroServer
|
|
2
2
|
|
|
3
|
-
Lightweight all-in-one http web server
|
|
3
|
+
Lightweight all-in-one http web server without dependencies
|
|
4
4
|
|
|
5
5
|
Features:
|
|
6
6
|
- fast REST API router
|
|
7
7
|
- form/json body decoder
|
|
8
8
|
- file upload
|
|
9
|
+
- websockets
|
|
9
10
|
- authentication
|
|
10
11
|
- plain/hashed passwords
|
|
11
12
|
- virtual hosts
|
|
12
13
|
- static files
|
|
14
|
+
- rewrite
|
|
15
|
+
- redirect
|
|
13
16
|
- reverse proxy routes
|
|
14
17
|
- trust ip for reverse proxy
|
|
15
18
|
- json file storage with autosave
|
|
16
|
-
- websockets (single file module from ws package)
|
|
17
19
|
- tls with automatic certificate reload
|
|
18
20
|
- data model with validation and mongodb interface
|
|
19
|
-
- simple file/memory storage
|
|
21
|
+
- simple file/memory storage
|
|
20
22
|
- promises as middleware
|
|
21
23
|
- controller class
|
|
22
24
|
- access rights per route
|
|
23
25
|
- access rights per model field
|
|
24
|
-
- rewrite
|
|
25
|
-
- redirect
|
|
26
|
-
- express like interface
|
|
27
26
|
|
|
28
27
|
### Usage examples:
|
|
29
28
|
|
|
30
29
|
Simple router:
|
|
31
30
|
|
|
32
31
|
```ts
|
|
32
|
+
import { MicroServer, AccessDenied } from '@radatek/microserver'
|
|
33
|
+
|
|
33
34
|
const server = new MicroServer({
|
|
34
35
|
listen: 8080,
|
|
35
36
|
auth: {
|
|
@@ -47,7 +48,7 @@ server.use('POST /api/login',
|
|
|
47
48
|
(req: ServerRequset, res: ServerResponse) =>
|
|
48
49
|
{
|
|
49
50
|
const user = await req.auth.login(req.body.user, req.body.password)
|
|
50
|
-
return user ? {user} :
|
|
51
|
+
return user ? {user} : new AccessDenied()
|
|
51
52
|
})
|
|
52
53
|
server.use('GET /api/protected', 'acl:auth',
|
|
53
54
|
(req: ServerRequset, res: ServerResponse) =>
|
|
@@ -55,49 +56,14 @@ server.use('GET /api/protected', 'acl:auth',
|
|
|
55
56
|
server.use('static', {root:'public'})
|
|
56
57
|
```
|
|
57
58
|
|
|
58
|
-
Using
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
class RestApi extends Controller {
|
|
62
|
-
static acl = '' // default acl
|
|
63
|
-
|
|
64
|
-
gethello(id) {
|
|
65
|
-
return {message:'Hello ' + id + '!'}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async postlogin() {
|
|
69
|
-
const user = await this.auth.login(this.body.user, this.body.password)
|
|
70
|
-
return user ? {user} : 403
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
static 'acl:protected' = 'user'
|
|
74
|
-
static 'url:protected' = 'GET /protected'
|
|
75
|
-
protected() {
|
|
76
|
-
return {message:'Protected'}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const server = new MicroServer({
|
|
81
|
-
listen: 8080,
|
|
82
|
-
auth: {
|
|
83
|
-
users: {
|
|
84
|
-
usr: {
|
|
85
|
-
password: 'secret',
|
|
86
|
-
acl: {user: true}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
server.use('/api', RestApi)
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
`Model.handler` automatically detects usage for standard functions: get,insert,update,delete
|
|
59
|
+
Using data schema:
|
|
95
60
|
|
|
96
61
|
```js
|
|
97
|
-
import { MicroServer, Model, MicroCollection, FileStore } from '@
|
|
62
|
+
import { MicroServer, Model, MicroCollection, FileStore } from '@radatek/microserver'
|
|
98
63
|
|
|
99
64
|
const usersCollection = new MicroCollection({ store: new FileStore({ dir: 'data' }), name: 'users' })
|
|
100
|
-
//
|
|
65
|
+
// or using MicroDB collection
|
|
66
|
+
const usersCollection = await db.collection('users')
|
|
101
67
|
|
|
102
68
|
const userProfile = new Model({
|
|
103
69
|
_id: 'string',
|
|
@@ -115,7 +81,7 @@ const server = new MicroServer({
|
|
|
115
81
|
}
|
|
116
82
|
})
|
|
117
83
|
|
|
118
|
-
userProfile.insert({name: 'admin', password: 'secret', role: 'admin', acl: {'user/*': true}})
|
|
84
|
+
await userProfile.insert({name: 'admin', password: 'secret', role: 'admin', acl: {'user/*': true}})
|
|
119
85
|
|
|
120
86
|
server.use('POST /login', async (req) => {
|
|
121
87
|
const user = await req.auth.login(req.body.user, req.body.password)
|
|
@@ -134,3 +100,40 @@ server.use('PUT /admin/user/:id', 'acl:user/update', userProfile)
|
|
|
134
100
|
// delete user if has acl 'user/update'
|
|
135
101
|
server.use('DELETE /admin/user/:id', 'acl:user/delete', userProfile)
|
|
136
102
|
```
|
|
103
|
+
|
|
104
|
+
Using controller:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const server = new MicroServer({
|
|
108
|
+
listen: 8080,
|
|
109
|
+
auth: {
|
|
110
|
+
users: {
|
|
111
|
+
usr: {
|
|
112
|
+
password: 'secret',
|
|
113
|
+
acl: {user: true}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
class RestApi extends Controller {
|
|
120
|
+
static acl = '' // default acl
|
|
121
|
+
|
|
122
|
+
gethello(id) {
|
|
123
|
+
return {message:'Hello ' + id + '!'}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async postlogin() {
|
|
127
|
+
const user = await this.auth.login(this.body.user, this.body.password)
|
|
128
|
+
return user ? {user} : 403
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
static 'acl:protected' = 'user'
|
|
132
|
+
static 'url:protected' = 'GET /protected'
|
|
133
|
+
protected() {
|
|
134
|
+
return {message:'Protected'}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
server.use('/api', RestApi)
|
|
139
|
+
```
|