objective-http 1.2.0 → 1.2.2
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/README.md
CHANGED
|
@@ -9,11 +9,9 @@ Your endpoints must implement `Endpoint` class interface (`route()` and `async h
|
|
|
9
9
|
|
|
10
10
|
```javascript
|
|
11
11
|
const http = require('node:http');
|
|
12
|
-
const cluster = require('node:cluster');
|
|
13
12
|
|
|
14
13
|
const {
|
|
15
14
|
Server,
|
|
16
|
-
ClusteredServer,
|
|
17
15
|
LoggedServer,
|
|
18
16
|
InputRequest,
|
|
19
17
|
JsonInputRequest,
|
|
@@ -26,26 +24,52 @@ const {
|
|
|
26
24
|
Endpoints
|
|
27
25
|
} = require('objective-http').server;
|
|
28
26
|
|
|
29
|
-
new
|
|
30
|
-
new
|
|
31
|
-
new
|
|
32
|
-
new
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
http
|
|
41
|
-
),
|
|
42
|
-
console
|
|
27
|
+
new LoggedServer(
|
|
28
|
+
new Server(
|
|
29
|
+
new Endpoints([
|
|
30
|
+
new MyFirstEndpoint(new LoggedEndpoint(new Endpoint(), console)),
|
|
31
|
+
new MySecondEndpoint(new LoggedEndpoint(new Endpoint(), console)),
|
|
32
|
+
new MyThirdEndpoint(new LoggedEndpoint(new Endpoint(), console))
|
|
33
|
+
]),
|
|
34
|
+
{port: server_port},
|
|
35
|
+
new LoggedInputRequest(new JsonInputRequest(new InputRequest()), console),
|
|
36
|
+
new LoggedOutputResponse(new JsonOutputResponse(new OutputResponse()), console),
|
|
37
|
+
http
|
|
43
38
|
),
|
|
44
|
-
|
|
45
|
-
{workers: workers_count}
|
|
39
|
+
console
|
|
46
40
|
).start();
|
|
47
41
|
```
|
|
48
42
|
|
|
43
|
+
`MyEndpoint` class example:
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
class MyEndpoint {
|
|
47
|
+
route() {
|
|
48
|
+
return {
|
|
49
|
+
method: 'GET',
|
|
50
|
+
path: '/test'
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async handle(request) {
|
|
55
|
+
try {
|
|
56
|
+
const processResult = await someProcess();
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
statusCode: 200,
|
|
60
|
+
body: processResult.toString()
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
} catch (e) {
|
|
64
|
+
return {
|
|
65
|
+
statusCode: 404,
|
|
66
|
+
body: 'process does not found anything'
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
```
|
|
49
73
|
|
|
50
74
|
## Client
|
|
51
75
|
|
|
@@ -90,8 +114,7 @@ console.log(response.body().toString());
|
|
|
90
114
|
|
|
91
115
|
`server` and `client` packages support Bun by default.
|
|
92
116
|
But there ara special `bun` package with native [Bun API](https://bun.sh/docs/runtime/bun-apis) implementation (like `Bun.serve()`).
|
|
93
|
-
And you should replace `node:http` package with `objective-http.bun.bunttp` in your `Server` configuration.
|
|
94
|
-
[Don't use](https://bun.sh/docs/runtime/nodejs-apis#node-cluster) `ClusteredServer` with `Bun`!!!
|
|
117
|
+
And you should replace `node:http` package with `objective-http.bun.bunttp` in your `Server` configuration.
|
|
95
118
|
|
|
96
119
|
|
|
97
120
|
### Server Bun usage
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"objective-http","version":"1.2.
|
|
1
|
+
{"name":"objective-http","version":"1.2.2","description":"Proxy classes for creating a http server","keywords":["web","web-server","http","http-server","oop"],"author":{"name":"volatilization","email":"volatilization@yandex.ru"},"repository":{"url":"git+https://github.com/volatilization/objective-http.git"},"license":"LGPL-3.0-only","main":"src/js/index.js"}
|
|
@@ -14,9 +14,7 @@ module.exports = class InputResponse {
|
|
|
14
14
|
flush() {
|
|
15
15
|
try {
|
|
16
16
|
return new Promise((resolve, reject) => {
|
|
17
|
-
this.#inputStream.once('error', (e) =>
|
|
18
|
-
reject(new Error(e.message, {cause: 'INVALID_RESPONSE'}))
|
|
19
|
-
);
|
|
17
|
+
this.#inputStream.once('error', (e) => reject(new Error(e.message, {cause: 'INVALID_RESPONSE'})));
|
|
20
18
|
|
|
21
19
|
let chunks = [];
|
|
22
20
|
this.#inputStream.on('data', (chunk) => chunks.push(chunk));
|
|
@@ -30,7 +28,7 @@ module.exports = class InputResponse {
|
|
|
30
28
|
});
|
|
31
29
|
|
|
32
30
|
} catch (e) {
|
|
33
|
-
throw new Error(e.message, {cause: 'INVALID_RESPONSE'})
|
|
31
|
+
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
|
package/src/js/server/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
Server: require('./Server'),
|
|
3
3
|
LoggedServer: require('./LoggedServer'),
|
|
4
|
-
ClusteredServer: require('./ClusteredServer'),
|
|
5
4
|
Endpoint: require('./endpoint/Endpoint'),
|
|
6
5
|
LoggedEndpoint: require('./endpoint/LoggedEndpoint'),
|
|
7
6
|
Endpoints: require('./endpoint/Endpoints'),
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
module.exports = class ClusteredServer {
|
|
2
|
-
#origin;
|
|
3
|
-
#cluster;
|
|
4
|
-
#options;
|
|
5
|
-
|
|
6
|
-
constructor(origin, options, cluster) {
|
|
7
|
-
this.#origin = origin;
|
|
8
|
-
this.#options = options;
|
|
9
|
-
this.#cluster = cluster;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
async start() {
|
|
13
|
-
if (this.#cluster.isPrimary) {
|
|
14
|
-
for (let i = 0; i < this.#options.workers; i++) {
|
|
15
|
-
this.#cluster.fork();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
} else {
|
|
19
|
-
return new ClusteredServer(await this.#origin.start(), this.#cluster, this.#options);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async stop() {
|
|
24
|
-
return new ClusteredServer(await this.#origin.stop(), this.#cluster, this.#options);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
options() {
|
|
28
|
-
return {...this.#origin.options(), ...this.#options};
|
|
29
|
-
}
|
|
30
|
-
}
|