objective-http 1.2.5 → 1.3.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/README.md +76 -39
- package/package.json +1 -1
- package/src/js/bun/client/index.js +2 -2
- package/src/js/bun/client/request/index.js +3 -0
- package/src/js/bun/client/response/index.js +3 -0
- package/src/js/bun/server/index.js +2 -2
- package/src/js/bun/server/request/index.js +3 -0
- package/src/js/bun/server/response/index.js +3 -0
- package/src/js/client/index.js +2 -2
- package/src/js/client/request/OutputRequest.js +31 -19
- package/src/js/client/request/index.js +3 -0
- package/src/js/client/response/InputResponse.js +33 -21
- package/src/js/client/response/index.js +3 -0
- package/src/js/server/endpoint/LoggedEndpoint.js +8 -1
- package/src/js/server/endpoint/index.js +5 -0
- package/src/js/server/index.js +3 -9
- package/src/js/server/request/LoggedInputRequest.js +8 -1
- package/src/js/server/request/index.js +5 -0
- package/src/js/server/response/LoggedOutputResponse.js +12 -1
- package/src/js/server/response/index.js +5 -0
package/README.md
CHANGED
|
@@ -13,23 +13,29 @@ const createServerFunction = require('node:http').createServer;
|
|
|
13
13
|
const {
|
|
14
14
|
Server,
|
|
15
15
|
LoggedServer,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
endpoint: {
|
|
17
|
+
Endpoint,
|
|
18
|
+
LoggedEndpoint,
|
|
19
|
+
Endpoints
|
|
20
|
+
},
|
|
21
|
+
request: {
|
|
22
|
+
InputRequest,
|
|
23
|
+
JsonInputRequest,
|
|
24
|
+
LoggedInputRequest,
|
|
25
|
+
},
|
|
26
|
+
response: {
|
|
27
|
+
OutputResponse,
|
|
28
|
+
JsonOutputResponse,
|
|
29
|
+
LoggedOutputResponse
|
|
30
|
+
}
|
|
25
31
|
} = require('objective-http').server;
|
|
26
32
|
|
|
27
33
|
new LoggedServer(
|
|
28
34
|
new Server(
|
|
29
35
|
new Endpoints([
|
|
30
|
-
new
|
|
31
|
-
new
|
|
32
|
-
new
|
|
36
|
+
new LoggedEndpoint(new MyFirstEndpoint(), console),
|
|
37
|
+
new LoggedEndpoint(new MySecondEndpoint(), console),
|
|
38
|
+
new LoggedEndpoint(new MyThirdEndpoint(), console)
|
|
33
39
|
]),
|
|
34
40
|
{port: server_port},
|
|
35
41
|
new LoggedInputRequest(new JsonInputRequest(new InputRequest()), console),
|
|
@@ -77,8 +83,12 @@ class MyEndpoint {
|
|
|
77
83
|
const requestFunction = require('node:http').request;
|
|
78
84
|
|
|
79
85
|
const {
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
request: {
|
|
87
|
+
OutputRequest
|
|
88
|
+
},
|
|
89
|
+
response: {
|
|
90
|
+
InputResponse
|
|
91
|
+
}
|
|
82
92
|
} = require('objective-http').client;
|
|
83
93
|
|
|
84
94
|
|
|
@@ -127,23 +137,29 @@ const createServerFunction = require('node:http').createServer;
|
|
|
127
137
|
const {
|
|
128
138
|
Server,
|
|
129
139
|
LoggedServer,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
140
|
+
endpoint: {
|
|
141
|
+
Endpoint,
|
|
142
|
+
LoggedEndpoint,
|
|
143
|
+
Endpoints
|
|
144
|
+
},
|
|
145
|
+
request: {
|
|
146
|
+
InputRequest,
|
|
147
|
+
JsonInputRequest,
|
|
148
|
+
LoggedInputRequest,
|
|
149
|
+
},
|
|
150
|
+
response: {
|
|
151
|
+
OutputResponse,
|
|
152
|
+
JsonOutputResponse,
|
|
153
|
+
LoggedOutputResponse
|
|
154
|
+
}
|
|
139
155
|
} = require('objective-http').server;
|
|
140
156
|
|
|
141
157
|
new LoggedServer(
|
|
142
158
|
new Server(
|
|
143
159
|
new Endpoints([
|
|
144
|
-
new
|
|
145
|
-
new
|
|
146
|
-
new
|
|
160
|
+
new LoggedEndpoint(new MyFirstEndpoint(), console),
|
|
161
|
+
new LoggedEndpoint(new MySecondEndpoint(), console),
|
|
162
|
+
new LoggedEndpoint(new MyThirdEndpoint(), console)
|
|
147
163
|
]),
|
|
148
164
|
{port: server_port},
|
|
149
165
|
new LoggedInputRequest(new JsonInputRequest(new InputRequest()), console),
|
|
@@ -163,15 +179,28 @@ const createServerFunction = require('objective-http').bun.bunttp.createServer;
|
|
|
163
179
|
const {
|
|
164
180
|
Server,
|
|
165
181
|
LoggedServer,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
182
|
+
endpoint: {
|
|
183
|
+
Endpoint,
|
|
184
|
+
LoggedEndpoint,
|
|
185
|
+
Endpoints
|
|
186
|
+
},
|
|
187
|
+
request: {
|
|
188
|
+
JsonInputRequest,
|
|
189
|
+
LoggedInputRequest,
|
|
190
|
+
},
|
|
191
|
+
response: {
|
|
192
|
+
JsonOutputResponse,
|
|
193
|
+
LoggedOutputResponse
|
|
194
|
+
}
|
|
195
|
+
} = require('objective-http').server;
|
|
196
|
+
|
|
197
|
+
const {
|
|
198
|
+
request: {
|
|
199
|
+
InputRequest
|
|
200
|
+
},
|
|
201
|
+
response: {
|
|
202
|
+
OutputResponse
|
|
203
|
+
}
|
|
175
204
|
} = require('objective-http').bun.server;
|
|
176
205
|
```
|
|
177
206
|
|
|
@@ -184,8 +213,12 @@ It should work with `node` and `bun`:
|
|
|
184
213
|
const requestFunction = require('node:http').request;
|
|
185
214
|
|
|
186
215
|
const {
|
|
187
|
-
|
|
188
|
-
|
|
216
|
+
request: {
|
|
217
|
+
OutputRequest
|
|
218
|
+
},
|
|
219
|
+
response: {
|
|
220
|
+
InputResponse
|
|
221
|
+
}
|
|
189
222
|
} = require('objective-http').client;
|
|
190
223
|
|
|
191
224
|
await (new OutputRequest(new InputResponse(), requestFunction)
|
|
@@ -203,7 +236,11 @@ In order for the code to be executed only by `bun`, you need to make changes to
|
|
|
203
236
|
const requestFunction = require('objective-http').bun.bunttp.request;
|
|
204
237
|
|
|
205
238
|
const {
|
|
206
|
-
|
|
207
|
-
|
|
239
|
+
request: {
|
|
240
|
+
OutputRequest
|
|
241
|
+
},
|
|
242
|
+
response: {
|
|
243
|
+
InputResponse
|
|
244
|
+
}
|
|
208
245
|
} = require('objective-http').bun.client;
|
|
209
246
|
```
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"objective-http","version":"1.
|
|
1
|
+
{"name":"objective-http","version":"1.3.0","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"}
|
package/src/js/client/index.js
CHANGED
|
@@ -13,17 +13,42 @@ module.exports = class OutputRequest {
|
|
|
13
13
|
return new OutputRequest(response, http, {method: 'GET', ...options});
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
send() {
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
async send() {
|
|
17
|
+
try {
|
|
18
|
+
return await new Promise((resolve, reject) => {
|
|
19
19
|
this.#sendRequestOutputStream(
|
|
20
20
|
this.#configureRequestOutputStream(this.#requestFunction, this.#response, this.#options, resolve, reject),
|
|
21
|
-
this.#options
|
|
21
|
+
this.#options,
|
|
22
|
+
reject);
|
|
23
|
+
});
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
} catch (e) {
|
|
26
|
+
if (e.cause == null) {
|
|
24
27
|
throw new Error(e.message, {cause: 'INVALID_REQUEST'});
|
|
25
28
|
}
|
|
26
|
-
|
|
29
|
+
|
|
30
|
+
throw e;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#sendRequestOutputStream(requestOutputStream, options, reject) {
|
|
35
|
+
try {
|
|
36
|
+
requestOutputStream.once('error', e => reject(e));
|
|
37
|
+
|
|
38
|
+
if (this.#needToByWritten(options)) {
|
|
39
|
+
requestOutputStream.write(options.body);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
requestOutputStream.end();
|
|
43
|
+
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#needToByWritten(options) {
|
|
50
|
+
return ['POST', 'PUT'].some(method => method === options.method.toString().toUpperCase())
|
|
51
|
+
&& (options.body != null && typeof options.body === 'string');
|
|
27
52
|
}
|
|
28
53
|
|
|
29
54
|
#configureRequestOutputStream(requestFunction, response, options, resolve, reject) {
|
|
@@ -53,17 +78,4 @@ module.exports = class OutputRequest {
|
|
|
53
78
|
reject(e);
|
|
54
79
|
}
|
|
55
80
|
}
|
|
56
|
-
|
|
57
|
-
#sendRequestOutputStream(requestOutputStream, options) {
|
|
58
|
-
if (this.#needToByWritten(options)) {
|
|
59
|
-
requestOutputStream.write(options.body);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
requestOutputStream.end();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
#needToByWritten(options) {
|
|
66
|
-
return ['POST', 'PUT'].some(method => method === options.method.toString().toUpperCase())
|
|
67
|
-
&& (options.body != null && typeof options.body === 'string');
|
|
68
|
-
}
|
|
69
81
|
};
|
|
@@ -11,27 +11,6 @@ module.exports = class InputResponse {
|
|
|
11
11
|
return new InputResponse(inputStream, options);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
flush() {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
try {
|
|
17
|
-
this.#inputStream.once('error', (e) => reject(new Error(e.message, {cause: 'INVALID_RESPONSE'})));
|
|
18
|
-
|
|
19
|
-
let chunks = [];
|
|
20
|
-
this.#inputStream.on('data', (chunk) => chunks.push(chunk));
|
|
21
|
-
this.#inputStream.on('end', () => resolve(new InputResponse(this.#inputStream,
|
|
22
|
-
{
|
|
23
|
-
statusCode: this.#inputStream.statusCode,
|
|
24
|
-
headers: new Headers(this.#inputStream.headers),
|
|
25
|
-
body: Buffer.concat(chunks)
|
|
26
|
-
}
|
|
27
|
-
)));
|
|
28
|
-
|
|
29
|
-
} catch (e) {
|
|
30
|
-
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
14
|
statusCode() {
|
|
36
15
|
return this.#options.statusCode;
|
|
37
16
|
}
|
|
@@ -43,4 +22,37 @@ module.exports = class InputResponse {
|
|
|
43
22
|
body() {
|
|
44
23
|
return this.#options.body;
|
|
45
24
|
}
|
|
25
|
+
|
|
26
|
+
async flush() {
|
|
27
|
+
try {
|
|
28
|
+
return await new Promise((resolve, reject) => {
|
|
29
|
+
this.#flushResponseInputStream(this.#inputStream, resolve, reject);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
} catch (e) {
|
|
33
|
+
throw new Error(e.message, {cause: 'INVALID_RESPONSE'});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#flushResponseInputStream(inputStream, resolve, reject) {
|
|
38
|
+
try {
|
|
39
|
+
inputStream.once('error', (e) => reject(e));
|
|
40
|
+
|
|
41
|
+
let chunks = [];
|
|
42
|
+
inputStream.on('data', (chunk) => chunks.push(chunk));
|
|
43
|
+
inputStream.on('end', () => resolve(
|
|
44
|
+
new InputResponse(
|
|
45
|
+
inputStream,
|
|
46
|
+
{
|
|
47
|
+
statusCode: inputStream.statusCode,
|
|
48
|
+
headers: new Headers(inputStream.headers),
|
|
49
|
+
body: Buffer.concat(chunks)
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
));
|
|
53
|
+
|
|
54
|
+
} catch (e) {
|
|
55
|
+
reject(e);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
46
58
|
};
|
|
@@ -18,6 +18,13 @@ module.exports = class LoggedEndpoint {
|
|
|
18
18
|
async handle(request) {
|
|
19
19
|
this.#logger.debug(`HttpEndpoint's handling [${request.route().method}] ${request.route().path}`);
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
try {
|
|
22
|
+
return await this.#origin.handle(request);
|
|
23
|
+
|
|
24
|
+
} catch (e) {
|
|
25
|
+
this.#logger.error(`HttpEndpoint's handling [${request.route().method}] ${request.route().path} error: ${e.message}`, e);
|
|
26
|
+
|
|
27
|
+
throw e;
|
|
28
|
+
}
|
|
22
29
|
}
|
|
23
30
|
}
|
package/src/js/server/index.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
Server: require('./Server'),
|
|
3
3
|
LoggedServer: require('./LoggedServer'),
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
InputRequest: require('./request/InputRequest'),
|
|
8
|
-
JsonInputRequest: require('./request/JsonInputRequest'),
|
|
9
|
-
LoggedInputRequest: require('./request/LoggedInputRequest'),
|
|
10
|
-
OutputResponse: require('./response/OutputResponse'),
|
|
11
|
-
JsonOutputResponse: require('./response/JsonOutputResponse'),
|
|
12
|
-
LoggedOutputResponse: require('./response/LoggedOutputResponse')
|
|
4
|
+
endpoint: require('./endpoint'),
|
|
5
|
+
request: require('./request'),
|
|
6
|
+
response: require('./response')
|
|
13
7
|
}
|
|
@@ -16,7 +16,14 @@ module.exports = class LoggedInputRequest {
|
|
|
16
16
|
async flush() {
|
|
17
17
|
this.#logger.debug(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} ${JSON.stringify(this.#inputStream.headers)}`);
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
try {
|
|
20
|
+
return new LoggedInputRequest(await this.#origin.flush(), this.#logger);
|
|
21
|
+
|
|
22
|
+
} catch (e) {
|
|
23
|
+
this.#logger.error(`HttpRequest: [${this.#inputStream.method}] ${this.#inputStream.url} error: ${e.message}`, e);
|
|
24
|
+
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
route() {
|
|
@@ -16,10 +16,21 @@ module.exports = class LoggedOutputResponse {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
flush() {
|
|
19
|
-
const outputStream = this.#
|
|
19
|
+
const outputStream = this.#loggedFlush();
|
|
20
20
|
|
|
21
21
|
this.#logger.debug(`HttpResponse: [${outputStream.req.method}] ${outputStream.req.url} - ${outputStream.statusCode}`);
|
|
22
22
|
|
|
23
23
|
return outputStream;
|
|
24
24
|
}
|
|
25
|
+
|
|
26
|
+
#loggedFlush() {
|
|
27
|
+
try {
|
|
28
|
+
return this.#origin.flush();
|
|
29
|
+
|
|
30
|
+
} catch (e) {
|
|
31
|
+
this.#logger.error(`HttpResponse error: ${e.message}`, e);
|
|
32
|
+
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
25
36
|
}
|