mikroserve 0.0.7 → 0.0.8
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 +5 -3
- package/lib/MikroServe.js +64 -37
- package/lib/MikroServe.mjs +3 -2
- package/lib/Router.js +20 -0
- package/lib/Router.mjs +1 -1
- package/lib/{chunk-KJT4SET2.mjs → chunk-GUYBTPZH.mjs} +20 -0
- package/lib/{chunk-J2WPHNMW.mjs → chunk-SLBFEKEH.mjs} +10 -38
- package/lib/chunk-YOHL3T54.mjs +43 -0
- package/lib/config.d.mts +37 -0
- package/lib/config.d.ts +37 -0
- package/lib/config.js +88 -0
- package/lib/config.mjs +7 -0
- package/lib/index.js +64 -37
- package/lib/index.mjs +3 -2
- package/lib/interfaces/index.d.mts +2 -0
- package/lib/interfaces/index.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
|
|
15
15
|
- Native Node.js [http](https://nodejs.org/api/http.html)/[https](https://nodejs.org/api/https.html) implementation, meaning maximum performance
|
|
16
16
|
- [Hono](https://hono.dev)-style API semantics for GET, POST, PATCH, PUT, DELETE operations
|
|
17
|
-
- Supports being exposed over
|
|
17
|
+
- Supports being exposed over HTTP, HTTPS, and HTTP2
|
|
18
18
|
- Supports custom middlewares
|
|
19
19
|
- Out-of-the-box CORS support
|
|
20
20
|
- Built-in customizable rate limiter
|
|
21
|
-
- Tiny (
|
|
21
|
+
- Tiny (5kb gzipped)
|
|
22
22
|
- Only a single dependency: [MikroConf](https://github.com/mikaelvesavuori/mikroconf)
|
|
23
23
|
|
|
24
24
|
## Installation
|
|
@@ -47,7 +47,8 @@ api.get('/', (c: Context) => c.text('Hello world!'));
|
|
|
47
47
|
api.start();
|
|
48
48
|
|
|
49
49
|
// The API is ready – go ahead and curl it in your command line of choice
|
|
50
|
-
// curl 0.0.0.0:3000
|
|
50
|
+
// HTTP: curl 0.0.0.0:3000
|
|
51
|
+
// HTTPS or HTTP2: curl -k 0.0.0.0:3000
|
|
51
52
|
// The response should be "Hello world!"
|
|
52
53
|
```
|
|
53
54
|
|
|
@@ -62,6 +63,7 @@ const api = new MikroServe({
|
|
|
62
63
|
port: 3000,
|
|
63
64
|
host: '0.0.0.0',
|
|
64
65
|
useHttps: false,
|
|
66
|
+
useHttp2: false,
|
|
65
67
|
sslCert: '',
|
|
66
68
|
sslKey: '',
|
|
67
69
|
sslCa: '',
|
package/lib/MikroServe.js
CHANGED
|
@@ -37,7 +37,7 @@ var import_node_fs = require("fs");
|
|
|
37
37
|
var import_node_http = __toESM(require("http"));
|
|
38
38
|
var import_node_http2 = __toESM(require("http2"));
|
|
39
39
|
var import_node_https = __toESM(require("https"));
|
|
40
|
-
var
|
|
40
|
+
var import_mikroconf2 = require("mikroconf");
|
|
41
41
|
|
|
42
42
|
// src/RateLimiter.ts
|
|
43
43
|
var RateLimiter = class {
|
|
@@ -206,6 +206,16 @@ var Router = class {
|
|
|
206
206
|
path,
|
|
207
207
|
state: {},
|
|
208
208
|
// Add the missing state property
|
|
209
|
+
raw: () => res,
|
|
210
|
+
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
211
|
+
statusCode: status,
|
|
212
|
+
body: content,
|
|
213
|
+
headers: {
|
|
214
|
+
"Content-Type": contentType,
|
|
215
|
+
"Content-Length": content.length.toString()
|
|
216
|
+
},
|
|
217
|
+
isRaw: true
|
|
218
|
+
}),
|
|
209
219
|
text: (content, status = 200) => ({
|
|
210
220
|
statusCode: status,
|
|
211
221
|
body: content,
|
|
@@ -233,6 +243,16 @@ var Router = class {
|
|
|
233
243
|
}),
|
|
234
244
|
status: function(code) {
|
|
235
245
|
return {
|
|
246
|
+
raw: () => res,
|
|
247
|
+
binary: (content, contentType = "application/octet-stream") => ({
|
|
248
|
+
statusCode: code,
|
|
249
|
+
body: content,
|
|
250
|
+
headers: {
|
|
251
|
+
"Content-Type": contentType,
|
|
252
|
+
"Content-Length": content.length.toString()
|
|
253
|
+
},
|
|
254
|
+
isRaw: true
|
|
255
|
+
}),
|
|
236
256
|
text: (content) => ({
|
|
237
257
|
statusCode: code,
|
|
238
258
|
body: content,
|
|
@@ -282,6 +302,9 @@ var Router = class {
|
|
|
282
302
|
}
|
|
283
303
|
};
|
|
284
304
|
|
|
305
|
+
// src/config.ts
|
|
306
|
+
var import_mikroconf = require("mikroconf");
|
|
307
|
+
|
|
285
308
|
// src/utils/configDefaults.ts
|
|
286
309
|
var configDefaults = () => {
|
|
287
310
|
return {
|
|
@@ -305,6 +328,41 @@ function getTruthyValue(value) {
|
|
|
305
328
|
return false;
|
|
306
329
|
}
|
|
307
330
|
|
|
331
|
+
// src/config.ts
|
|
332
|
+
var defaults = configDefaults();
|
|
333
|
+
var baseConfig = (options) => ({
|
|
334
|
+
configFilePath: "mikroserve.config.json",
|
|
335
|
+
args: process.argv,
|
|
336
|
+
options: [
|
|
337
|
+
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
338
|
+
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
339
|
+
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
340
|
+
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
341
|
+
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
342
|
+
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
343
|
+
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
344
|
+
{
|
|
345
|
+
flag: "--ratelimit",
|
|
346
|
+
path: "rateLimit.enabled",
|
|
347
|
+
defaultValue: defaults.rateLimit.enabled,
|
|
348
|
+
isFlag: true
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
flag: "--rps",
|
|
352
|
+
path: "rateLimit.requestsPerMinute",
|
|
353
|
+
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
flag: "--allowed",
|
|
357
|
+
path: "allowedDomains",
|
|
358
|
+
defaultValue: defaults.allowedDomains,
|
|
359
|
+
parser: import_mikroconf.parsers.array
|
|
360
|
+
},
|
|
361
|
+
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
362
|
+
],
|
|
363
|
+
config: options
|
|
364
|
+
});
|
|
365
|
+
|
|
308
366
|
// src/MikroServe.ts
|
|
309
367
|
var MikroServe = class {
|
|
310
368
|
config;
|
|
@@ -314,43 +372,11 @@ var MikroServe = class {
|
|
|
314
372
|
* @description Creates a new MikroServe instance.
|
|
315
373
|
*/
|
|
316
374
|
constructor(options) {
|
|
317
|
-
const
|
|
318
|
-
const config = new import_mikroconf.MikroConf({
|
|
319
|
-
configFilePath: "mikroserve.config.json",
|
|
320
|
-
args: process.argv,
|
|
321
|
-
options: [
|
|
322
|
-
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
323
|
-
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
324
|
-
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
325
|
-
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
326
|
-
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
327
|
-
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
328
|
-
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
329
|
-
{
|
|
330
|
-
flag: "--ratelimit",
|
|
331
|
-
path: "rateLimit.enabled",
|
|
332
|
-
defaultValue: defaults.rateLimit.enabled,
|
|
333
|
-
isFlag: true
|
|
334
|
-
},
|
|
335
|
-
{
|
|
336
|
-
flag: "--rps",
|
|
337
|
-
path: "rateLimit.requestsPerMinute",
|
|
338
|
-
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
339
|
-
},
|
|
340
|
-
{
|
|
341
|
-
flag: "--allowed",
|
|
342
|
-
path: "allowedDomains",
|
|
343
|
-
defaultValue: defaults.allowedDomains,
|
|
344
|
-
parser: import_mikroconf.parsers.array
|
|
345
|
-
},
|
|
346
|
-
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
347
|
-
],
|
|
348
|
-
config: options
|
|
349
|
-
}).get();
|
|
375
|
+
const config = new import_mikroconf2.MikroConf(baseConfig(options || {})).get();
|
|
350
376
|
if (config.debug) console.log("Using configuration:", config);
|
|
351
377
|
this.config = config;
|
|
352
378
|
this.router = new Router();
|
|
353
|
-
const requestsPerMinute = config.rateLimit.requestsPerMinute ||
|
|
379
|
+
const requestsPerMinute = config.rateLimit.requestsPerMinute || configDefaults().rateLimit.requestsPerMinute;
|
|
354
380
|
this.rateLimiter = new RateLimiter(requestsPerMinute, 60);
|
|
355
381
|
if (config.rateLimit.enabled === true) this.use(this.rateLimitMiddleware.bind(this));
|
|
356
382
|
}
|
|
@@ -631,9 +657,8 @@ var MikroServe = class {
|
|
|
631
657
|
"Content-Security-Policy": "default-src 'self'; script-src 'self'; object-src 'none'",
|
|
632
658
|
"X-XSS-Protection": "1; mode=block"
|
|
633
659
|
};
|
|
634
|
-
if (isHttps || this.config.useHttp2)
|
|
660
|
+
if (isHttps || this.config.useHttp2)
|
|
635
661
|
securityHeaders["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
|
|
636
|
-
}
|
|
637
662
|
if (res instanceof import_node_http.default.ServerResponse) {
|
|
638
663
|
Object.entries(securityHeaders).forEach(([name, value]) => {
|
|
639
664
|
res.setHeader(name, value);
|
|
@@ -658,12 +683,14 @@ var MikroServe = class {
|
|
|
658
683
|
if (hasWriteHead(res)) {
|
|
659
684
|
res.writeHead(response.statusCode, headers);
|
|
660
685
|
if (response.body === null || response.body === void 0) res.end();
|
|
686
|
+
else if (response.isRaw) res.end(response.body);
|
|
661
687
|
else if (typeof response.body === "string") res.end(response.body);
|
|
662
688
|
else res.end(JSON.stringify(response.body));
|
|
663
689
|
} else {
|
|
664
690
|
console.warn("Unexpected response object type without writeHead/end methods");
|
|
665
691
|
res.writeHead?.(response.statusCode, headers);
|
|
666
692
|
if (response.body === null || response.body === void 0) res.end?.();
|
|
693
|
+
else if (response.isRaw) res.end?.(response.body);
|
|
667
694
|
else if (typeof response.body === "string") res.end?.(response.body);
|
|
668
695
|
else res.end?.(JSON.stringify(response.body));
|
|
669
696
|
}
|
package/lib/MikroServe.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MikroServe
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SLBFEKEH.mjs";
|
|
4
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
5
|
-
import "./chunk-
|
|
5
|
+
import "./chunk-GUYBTPZH.mjs";
|
|
6
|
+
import "./chunk-YOHL3T54.mjs";
|
|
6
7
|
import "./chunk-JJX5XRNB.mjs";
|
|
7
8
|
export {
|
|
8
9
|
MikroServe
|
package/lib/Router.js
CHANGED
|
@@ -143,6 +143,16 @@ var Router = class {
|
|
|
143
143
|
path,
|
|
144
144
|
state: {},
|
|
145
145
|
// Add the missing state property
|
|
146
|
+
raw: () => res,
|
|
147
|
+
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
148
|
+
statusCode: status,
|
|
149
|
+
body: content,
|
|
150
|
+
headers: {
|
|
151
|
+
"Content-Type": contentType,
|
|
152
|
+
"Content-Length": content.length.toString()
|
|
153
|
+
},
|
|
154
|
+
isRaw: true
|
|
155
|
+
}),
|
|
146
156
|
text: (content, status = 200) => ({
|
|
147
157
|
statusCode: status,
|
|
148
158
|
body: content,
|
|
@@ -170,6 +180,16 @@ var Router = class {
|
|
|
170
180
|
}),
|
|
171
181
|
status: function(code) {
|
|
172
182
|
return {
|
|
183
|
+
raw: () => res,
|
|
184
|
+
binary: (content, contentType = "application/octet-stream") => ({
|
|
185
|
+
statusCode: code,
|
|
186
|
+
body: content,
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": contentType,
|
|
189
|
+
"Content-Length": content.length.toString()
|
|
190
|
+
},
|
|
191
|
+
isRaw: true
|
|
192
|
+
}),
|
|
173
193
|
text: (content) => ({
|
|
174
194
|
statusCode: code,
|
|
175
195
|
body: content,
|
package/lib/Router.mjs
CHANGED
|
@@ -119,6 +119,16 @@ var Router = class {
|
|
|
119
119
|
path,
|
|
120
120
|
state: {},
|
|
121
121
|
// Add the missing state property
|
|
122
|
+
raw: () => res,
|
|
123
|
+
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
124
|
+
statusCode: status,
|
|
125
|
+
body: content,
|
|
126
|
+
headers: {
|
|
127
|
+
"Content-Type": contentType,
|
|
128
|
+
"Content-Length": content.length.toString()
|
|
129
|
+
},
|
|
130
|
+
isRaw: true
|
|
131
|
+
}),
|
|
122
132
|
text: (content, status = 200) => ({
|
|
123
133
|
statusCode: status,
|
|
124
134
|
body: content,
|
|
@@ -146,6 +156,16 @@ var Router = class {
|
|
|
146
156
|
}),
|
|
147
157
|
status: function(code) {
|
|
148
158
|
return {
|
|
159
|
+
raw: () => res,
|
|
160
|
+
binary: (content, contentType = "application/octet-stream") => ({
|
|
161
|
+
statusCode: code,
|
|
162
|
+
body: content,
|
|
163
|
+
headers: {
|
|
164
|
+
"Content-Type": contentType,
|
|
165
|
+
"Content-Length": content.length.toString()
|
|
166
|
+
},
|
|
167
|
+
isRaw: true
|
|
168
|
+
}),
|
|
149
169
|
text: (content) => ({
|
|
150
170
|
statusCode: code,
|
|
151
171
|
body: content,
|
|
@@ -3,7 +3,10 @@ import {
|
|
|
3
3
|
} from "./chunk-ZFBBESGU.mjs";
|
|
4
4
|
import {
|
|
5
5
|
Router
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-GUYBTPZH.mjs";
|
|
7
|
+
import {
|
|
8
|
+
baseConfig
|
|
9
|
+
} from "./chunk-YOHL3T54.mjs";
|
|
7
10
|
import {
|
|
8
11
|
configDefaults
|
|
9
12
|
} from "./chunk-JJX5XRNB.mjs";
|
|
@@ -13,7 +16,7 @@ import { readFileSync } from "node:fs";
|
|
|
13
16
|
import http from "node:http";
|
|
14
17
|
import http2 from "node:http2";
|
|
15
18
|
import https from "node:https";
|
|
16
|
-
import { MikroConf
|
|
19
|
+
import { MikroConf } from "mikroconf";
|
|
17
20
|
var MikroServe = class {
|
|
18
21
|
config;
|
|
19
22
|
rateLimiter;
|
|
@@ -22,43 +25,11 @@ var MikroServe = class {
|
|
|
22
25
|
* @description Creates a new MikroServe instance.
|
|
23
26
|
*/
|
|
24
27
|
constructor(options) {
|
|
25
|
-
const
|
|
26
|
-
const config = new MikroConf({
|
|
27
|
-
configFilePath: "mikroserve.config.json",
|
|
28
|
-
args: process.argv,
|
|
29
|
-
options: [
|
|
30
|
-
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
31
|
-
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
32
|
-
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
33
|
-
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
34
|
-
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
35
|
-
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
36
|
-
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
37
|
-
{
|
|
38
|
-
flag: "--ratelimit",
|
|
39
|
-
path: "rateLimit.enabled",
|
|
40
|
-
defaultValue: defaults.rateLimit.enabled,
|
|
41
|
-
isFlag: true
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
flag: "--rps",
|
|
45
|
-
path: "rateLimit.requestsPerMinute",
|
|
46
|
-
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
flag: "--allowed",
|
|
50
|
-
path: "allowedDomains",
|
|
51
|
-
defaultValue: defaults.allowedDomains,
|
|
52
|
-
parser: parsers.array
|
|
53
|
-
},
|
|
54
|
-
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
55
|
-
],
|
|
56
|
-
config: options
|
|
57
|
-
}).get();
|
|
28
|
+
const config = new MikroConf(baseConfig(options || {})).get();
|
|
58
29
|
if (config.debug) console.log("Using configuration:", config);
|
|
59
30
|
this.config = config;
|
|
60
31
|
this.router = new Router();
|
|
61
|
-
const requestsPerMinute = config.rateLimit.requestsPerMinute ||
|
|
32
|
+
const requestsPerMinute = config.rateLimit.requestsPerMinute || configDefaults().rateLimit.requestsPerMinute;
|
|
62
33
|
this.rateLimiter = new RateLimiter(requestsPerMinute, 60);
|
|
63
34
|
if (config.rateLimit.enabled === true) this.use(this.rateLimitMiddleware.bind(this));
|
|
64
35
|
}
|
|
@@ -339,9 +310,8 @@ var MikroServe = class {
|
|
|
339
310
|
"Content-Security-Policy": "default-src 'self'; script-src 'self'; object-src 'none'",
|
|
340
311
|
"X-XSS-Protection": "1; mode=block"
|
|
341
312
|
};
|
|
342
|
-
if (isHttps || this.config.useHttp2)
|
|
313
|
+
if (isHttps || this.config.useHttp2)
|
|
343
314
|
securityHeaders["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
|
|
344
|
-
}
|
|
345
315
|
if (res instanceof http.ServerResponse) {
|
|
346
316
|
Object.entries(securityHeaders).forEach(([name, value]) => {
|
|
347
317
|
res.setHeader(name, value);
|
|
@@ -366,12 +336,14 @@ var MikroServe = class {
|
|
|
366
336
|
if (hasWriteHead(res)) {
|
|
367
337
|
res.writeHead(response.statusCode, headers);
|
|
368
338
|
if (response.body === null || response.body === void 0) res.end();
|
|
339
|
+
else if (response.isRaw) res.end(response.body);
|
|
369
340
|
else if (typeof response.body === "string") res.end(response.body);
|
|
370
341
|
else res.end(JSON.stringify(response.body));
|
|
371
342
|
} else {
|
|
372
343
|
console.warn("Unexpected response object type without writeHead/end methods");
|
|
373
344
|
res.writeHead?.(response.statusCode, headers);
|
|
374
345
|
if (response.body === null || response.body === void 0) res.end?.();
|
|
346
|
+
else if (response.isRaw) res.end?.(response.body);
|
|
375
347
|
else if (typeof response.body === "string") res.end?.(response.body);
|
|
376
348
|
else res.end?.(JSON.stringify(response.body));
|
|
377
349
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
configDefaults
|
|
3
|
+
} from "./chunk-JJX5XRNB.mjs";
|
|
4
|
+
|
|
5
|
+
// src/config.ts
|
|
6
|
+
import { parsers } from "mikroconf";
|
|
7
|
+
var defaults = configDefaults();
|
|
8
|
+
var baseConfig = (options) => ({
|
|
9
|
+
configFilePath: "mikroserve.config.json",
|
|
10
|
+
args: process.argv,
|
|
11
|
+
options: [
|
|
12
|
+
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
13
|
+
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
14
|
+
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
15
|
+
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
16
|
+
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
17
|
+
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
18
|
+
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
19
|
+
{
|
|
20
|
+
flag: "--ratelimit",
|
|
21
|
+
path: "rateLimit.enabled",
|
|
22
|
+
defaultValue: defaults.rateLimit.enabled,
|
|
23
|
+
isFlag: true
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
flag: "--rps",
|
|
27
|
+
path: "rateLimit.requestsPerMinute",
|
|
28
|
+
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
flag: "--allowed",
|
|
32
|
+
path: "allowedDomains",
|
|
33
|
+
defaultValue: defaults.allowedDomains,
|
|
34
|
+
parser: parsers.array
|
|
35
|
+
},
|
|
36
|
+
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
37
|
+
],
|
|
38
|
+
config: options
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export {
|
|
42
|
+
baseConfig
|
|
43
|
+
};
|
package/lib/config.d.mts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MikroServeOptions, MikroServeConfiguration } from './interfaces/index.mjs';
|
|
2
|
+
import 'node:http';
|
|
3
|
+
import 'node:http2';
|
|
4
|
+
import 'node:https';
|
|
5
|
+
|
|
6
|
+
declare const baseConfig: (options: MikroServeOptions) => {
|
|
7
|
+
configFilePath: string;
|
|
8
|
+
args: string[];
|
|
9
|
+
options: ({
|
|
10
|
+
flag: string;
|
|
11
|
+
path: string;
|
|
12
|
+
defaultValue: number;
|
|
13
|
+
isFlag?: undefined;
|
|
14
|
+
parser?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
flag: string;
|
|
17
|
+
path: string;
|
|
18
|
+
defaultValue: string;
|
|
19
|
+
isFlag?: undefined;
|
|
20
|
+
parser?: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
flag: string;
|
|
23
|
+
path: string;
|
|
24
|
+
defaultValue: boolean;
|
|
25
|
+
isFlag: boolean;
|
|
26
|
+
parser?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
flag: string;
|
|
29
|
+
path: string;
|
|
30
|
+
defaultValue: string[];
|
|
31
|
+
parser: (value: string) => string[];
|
|
32
|
+
isFlag?: undefined;
|
|
33
|
+
})[];
|
|
34
|
+
config: Partial<MikroServeConfiguration>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { baseConfig };
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MikroServeOptions, MikroServeConfiguration } from './interfaces/index.js';
|
|
2
|
+
import 'node:http';
|
|
3
|
+
import 'node:http2';
|
|
4
|
+
import 'node:https';
|
|
5
|
+
|
|
6
|
+
declare const baseConfig: (options: MikroServeOptions) => {
|
|
7
|
+
configFilePath: string;
|
|
8
|
+
args: string[];
|
|
9
|
+
options: ({
|
|
10
|
+
flag: string;
|
|
11
|
+
path: string;
|
|
12
|
+
defaultValue: number;
|
|
13
|
+
isFlag?: undefined;
|
|
14
|
+
parser?: undefined;
|
|
15
|
+
} | {
|
|
16
|
+
flag: string;
|
|
17
|
+
path: string;
|
|
18
|
+
defaultValue: string;
|
|
19
|
+
isFlag?: undefined;
|
|
20
|
+
parser?: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
flag: string;
|
|
23
|
+
path: string;
|
|
24
|
+
defaultValue: boolean;
|
|
25
|
+
isFlag: boolean;
|
|
26
|
+
parser?: undefined;
|
|
27
|
+
} | {
|
|
28
|
+
flag: string;
|
|
29
|
+
path: string;
|
|
30
|
+
defaultValue: string[];
|
|
31
|
+
parser: (value: string) => string[];
|
|
32
|
+
isFlag?: undefined;
|
|
33
|
+
})[];
|
|
34
|
+
config: Partial<MikroServeConfiguration>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export { baseConfig };
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/config.ts
|
|
21
|
+
var config_exports = {};
|
|
22
|
+
__export(config_exports, {
|
|
23
|
+
baseConfig: () => baseConfig
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(config_exports);
|
|
26
|
+
var import_mikroconf = require("mikroconf");
|
|
27
|
+
|
|
28
|
+
// src/utils/configDefaults.ts
|
|
29
|
+
var configDefaults = () => {
|
|
30
|
+
return {
|
|
31
|
+
port: Number(process.env.PORT) || 3e3,
|
|
32
|
+
host: process.env.HOST || "0.0.0.0",
|
|
33
|
+
useHttps: false,
|
|
34
|
+
useHttp2: false,
|
|
35
|
+
sslCert: "",
|
|
36
|
+
sslKey: "",
|
|
37
|
+
sslCa: "",
|
|
38
|
+
debug: getTruthyValue(process.env.DEBUG) || false,
|
|
39
|
+
rateLimit: {
|
|
40
|
+
enabled: true,
|
|
41
|
+
requestsPerMinute: 100
|
|
42
|
+
},
|
|
43
|
+
allowedDomains: ["*"]
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
function getTruthyValue(value) {
|
|
47
|
+
if (value === "true" || value === true) return true;
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/config.ts
|
|
52
|
+
var defaults = configDefaults();
|
|
53
|
+
var baseConfig = (options) => ({
|
|
54
|
+
configFilePath: "mikroserve.config.json",
|
|
55
|
+
args: process.argv,
|
|
56
|
+
options: [
|
|
57
|
+
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
58
|
+
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
59
|
+
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
60
|
+
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
61
|
+
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
62
|
+
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
63
|
+
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
64
|
+
{
|
|
65
|
+
flag: "--ratelimit",
|
|
66
|
+
path: "rateLimit.enabled",
|
|
67
|
+
defaultValue: defaults.rateLimit.enabled,
|
|
68
|
+
isFlag: true
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
flag: "--rps",
|
|
72
|
+
path: "rateLimit.requestsPerMinute",
|
|
73
|
+
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
flag: "--allowed",
|
|
77
|
+
path: "allowedDomains",
|
|
78
|
+
defaultValue: defaults.allowedDomains,
|
|
79
|
+
parser: import_mikroconf.parsers.array
|
|
80
|
+
},
|
|
81
|
+
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
82
|
+
],
|
|
83
|
+
config: options
|
|
84
|
+
});
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
baseConfig
|
|
88
|
+
});
|
package/lib/config.mjs
ADDED
package/lib/index.js
CHANGED
|
@@ -39,7 +39,7 @@ var import_node_fs = require("fs");
|
|
|
39
39
|
var import_node_http = __toESM(require("http"));
|
|
40
40
|
var import_node_http2 = __toESM(require("http2"));
|
|
41
41
|
var import_node_https = __toESM(require("https"));
|
|
42
|
-
var
|
|
42
|
+
var import_mikroconf2 = require("mikroconf");
|
|
43
43
|
|
|
44
44
|
// src/RateLimiter.ts
|
|
45
45
|
var RateLimiter = class {
|
|
@@ -208,6 +208,16 @@ var Router = class {
|
|
|
208
208
|
path,
|
|
209
209
|
state: {},
|
|
210
210
|
// Add the missing state property
|
|
211
|
+
raw: () => res,
|
|
212
|
+
binary: (content, contentType = "application/octet-stream", status = 200) => ({
|
|
213
|
+
statusCode: status,
|
|
214
|
+
body: content,
|
|
215
|
+
headers: {
|
|
216
|
+
"Content-Type": contentType,
|
|
217
|
+
"Content-Length": content.length.toString()
|
|
218
|
+
},
|
|
219
|
+
isRaw: true
|
|
220
|
+
}),
|
|
211
221
|
text: (content, status = 200) => ({
|
|
212
222
|
statusCode: status,
|
|
213
223
|
body: content,
|
|
@@ -235,6 +245,16 @@ var Router = class {
|
|
|
235
245
|
}),
|
|
236
246
|
status: function(code) {
|
|
237
247
|
return {
|
|
248
|
+
raw: () => res,
|
|
249
|
+
binary: (content, contentType = "application/octet-stream") => ({
|
|
250
|
+
statusCode: code,
|
|
251
|
+
body: content,
|
|
252
|
+
headers: {
|
|
253
|
+
"Content-Type": contentType,
|
|
254
|
+
"Content-Length": content.length.toString()
|
|
255
|
+
},
|
|
256
|
+
isRaw: true
|
|
257
|
+
}),
|
|
238
258
|
text: (content) => ({
|
|
239
259
|
statusCode: code,
|
|
240
260
|
body: content,
|
|
@@ -284,6 +304,9 @@ var Router = class {
|
|
|
284
304
|
}
|
|
285
305
|
};
|
|
286
306
|
|
|
307
|
+
// src/config.ts
|
|
308
|
+
var import_mikroconf = require("mikroconf");
|
|
309
|
+
|
|
287
310
|
// src/utils/configDefaults.ts
|
|
288
311
|
var configDefaults = () => {
|
|
289
312
|
return {
|
|
@@ -307,6 +330,41 @@ function getTruthyValue(value) {
|
|
|
307
330
|
return false;
|
|
308
331
|
}
|
|
309
332
|
|
|
333
|
+
// src/config.ts
|
|
334
|
+
var defaults = configDefaults();
|
|
335
|
+
var baseConfig = (options) => ({
|
|
336
|
+
configFilePath: "mikroserve.config.json",
|
|
337
|
+
args: process.argv,
|
|
338
|
+
options: [
|
|
339
|
+
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
340
|
+
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
341
|
+
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
342
|
+
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
343
|
+
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
344
|
+
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
345
|
+
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
346
|
+
{
|
|
347
|
+
flag: "--ratelimit",
|
|
348
|
+
path: "rateLimit.enabled",
|
|
349
|
+
defaultValue: defaults.rateLimit.enabled,
|
|
350
|
+
isFlag: true
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
flag: "--rps",
|
|
354
|
+
path: "rateLimit.requestsPerMinute",
|
|
355
|
+
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
flag: "--allowed",
|
|
359
|
+
path: "allowedDomains",
|
|
360
|
+
defaultValue: defaults.allowedDomains,
|
|
361
|
+
parser: import_mikroconf.parsers.array
|
|
362
|
+
},
|
|
363
|
+
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
364
|
+
],
|
|
365
|
+
config: options
|
|
366
|
+
});
|
|
367
|
+
|
|
310
368
|
// src/MikroServe.ts
|
|
311
369
|
var MikroServe = class {
|
|
312
370
|
config;
|
|
@@ -316,43 +374,11 @@ var MikroServe = class {
|
|
|
316
374
|
* @description Creates a new MikroServe instance.
|
|
317
375
|
*/
|
|
318
376
|
constructor(options) {
|
|
319
|
-
const
|
|
320
|
-
const config = new import_mikroconf.MikroConf({
|
|
321
|
-
configFilePath: "mikroserve.config.json",
|
|
322
|
-
args: process.argv,
|
|
323
|
-
options: [
|
|
324
|
-
{ flag: "--port", path: "port", defaultValue: defaults.port },
|
|
325
|
-
{ flag: "--host", path: "host", defaultValue: defaults.host },
|
|
326
|
-
{ flag: "--https", path: "useHttps", defaultValue: defaults.useHttps, isFlag: true },
|
|
327
|
-
{ flag: "--http2", path: "useHttp2", defaultValue: defaults.useHttp2, isFlag: true },
|
|
328
|
-
{ flag: "--cert", path: "sslCert", defaultValue: defaults.sslCert },
|
|
329
|
-
{ flag: "--key", path: "sslKey", defaultValue: defaults.sslKey },
|
|
330
|
-
{ flag: "--ca", path: "sslCa", defaultValue: defaults.sslCa },
|
|
331
|
-
{
|
|
332
|
-
flag: "--ratelimit",
|
|
333
|
-
path: "rateLimit.enabled",
|
|
334
|
-
defaultValue: defaults.rateLimit.enabled,
|
|
335
|
-
isFlag: true
|
|
336
|
-
},
|
|
337
|
-
{
|
|
338
|
-
flag: "--rps",
|
|
339
|
-
path: "rateLimit.requestsPerMinute",
|
|
340
|
-
defaultValue: defaults.rateLimit.requestsPerMinute
|
|
341
|
-
},
|
|
342
|
-
{
|
|
343
|
-
flag: "--allowed",
|
|
344
|
-
path: "allowedDomains",
|
|
345
|
-
defaultValue: defaults.allowedDomains,
|
|
346
|
-
parser: import_mikroconf.parsers.array
|
|
347
|
-
},
|
|
348
|
-
{ flag: "--debug", path: "debug", defaultValue: defaults.debug, isFlag: true }
|
|
349
|
-
],
|
|
350
|
-
config: options
|
|
351
|
-
}).get();
|
|
377
|
+
const config = new import_mikroconf2.MikroConf(baseConfig(options || {})).get();
|
|
352
378
|
if (config.debug) console.log("Using configuration:", config);
|
|
353
379
|
this.config = config;
|
|
354
380
|
this.router = new Router();
|
|
355
|
-
const requestsPerMinute = config.rateLimit.requestsPerMinute ||
|
|
381
|
+
const requestsPerMinute = config.rateLimit.requestsPerMinute || configDefaults().rateLimit.requestsPerMinute;
|
|
356
382
|
this.rateLimiter = new RateLimiter(requestsPerMinute, 60);
|
|
357
383
|
if (config.rateLimit.enabled === true) this.use(this.rateLimitMiddleware.bind(this));
|
|
358
384
|
}
|
|
@@ -633,9 +659,8 @@ var MikroServe = class {
|
|
|
633
659
|
"Content-Security-Policy": "default-src 'self'; script-src 'self'; object-src 'none'",
|
|
634
660
|
"X-XSS-Protection": "1; mode=block"
|
|
635
661
|
};
|
|
636
|
-
if (isHttps || this.config.useHttp2)
|
|
662
|
+
if (isHttps || this.config.useHttp2)
|
|
637
663
|
securityHeaders["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
|
|
638
|
-
}
|
|
639
664
|
if (res instanceof import_node_http.default.ServerResponse) {
|
|
640
665
|
Object.entries(securityHeaders).forEach(([name, value]) => {
|
|
641
666
|
res.setHeader(name, value);
|
|
@@ -660,12 +685,14 @@ var MikroServe = class {
|
|
|
660
685
|
if (hasWriteHead(res)) {
|
|
661
686
|
res.writeHead(response.statusCode, headers);
|
|
662
687
|
if (response.body === null || response.body === void 0) res.end();
|
|
688
|
+
else if (response.isRaw) res.end(response.body);
|
|
663
689
|
else if (typeof response.body === "string") res.end(response.body);
|
|
664
690
|
else res.end(JSON.stringify(response.body));
|
|
665
691
|
} else {
|
|
666
692
|
console.warn("Unexpected response object type without writeHead/end methods");
|
|
667
693
|
res.writeHead?.(response.statusCode, headers);
|
|
668
694
|
if (response.body === null || response.body === void 0) res.end?.();
|
|
695
|
+
else if (response.isRaw) res.end?.(response.body);
|
|
669
696
|
else if (typeof response.body === "string") res.end?.(response.body);
|
|
670
697
|
else res.end?.(JSON.stringify(response.body));
|
|
671
698
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MikroServe
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SLBFEKEH.mjs";
|
|
4
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
5
|
-
import "./chunk-
|
|
5
|
+
import "./chunk-GUYBTPZH.mjs";
|
|
6
|
+
import "./chunk-YOHL3T54.mjs";
|
|
6
7
|
import "./chunk-JJX5XRNB.mjs";
|
|
7
8
|
export {
|
|
8
9
|
MikroServe
|
|
@@ -78,6 +78,8 @@ type MikroServeOptions = Partial<MikroServeConfiguration>;
|
|
|
78
78
|
* @description Response utilities for handlers.
|
|
79
79
|
*/
|
|
80
80
|
interface ResponseHelpers {
|
|
81
|
+
raw: () => ResponseType;
|
|
82
|
+
binary: (content: Buffer, contentType?: string) => HandlerResponse;
|
|
81
83
|
text(content: string, status?: number): HandlerResponse;
|
|
82
84
|
json(data: any, status?: number): HandlerResponse;
|
|
83
85
|
html(content: string, status?: number): HandlerResponse;
|
|
@@ -78,6 +78,8 @@ type MikroServeOptions = Partial<MikroServeConfiguration>;
|
|
|
78
78
|
* @description Response utilities for handlers.
|
|
79
79
|
*/
|
|
80
80
|
interface ResponseHelpers {
|
|
81
|
+
raw: () => ResponseType;
|
|
82
|
+
binary: (content: Buffer, contentType?: string) => HandlerResponse;
|
|
81
83
|
text(content: string, status?: number): HandlerResponse;
|
|
82
84
|
json(data: any, status?: number): HandlerResponse;
|
|
83
85
|
html(content: string, status?: number): HandlerResponse;
|