mikroserve 0.0.4 → 0.0.6
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 +42 -5
- package/lib/MikroServe.d.mts +1 -1
- package/lib/MikroServe.d.ts +1 -1
- package/lib/MikroServe.js +8 -5
- package/lib/MikroServe.mjs +3 -3
- package/lib/chunk-6HESV5Q6.mjs +9 -0
- package/lib/{chunk-6UXWR6LD.mjs → chunk-E3RGQ7QF.mjs} +6 -3
- package/lib/{chunk-RP67R3W4.mjs → chunk-NSHBEU32.mjs} +3 -3
- package/lib/index.d.mts +0 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +8 -21
- package/lib/index.mjs +3 -19
- package/lib/interfaces/index.d.mts +1 -0
- package/lib/interfaces/index.d.ts +1 -0
- package/lib/utils/configDefaults.js +3 -3
- package/lib/utils/configDefaults.mjs +2 -2
- package/lib/utils/getTruthyValue.d.mts +6 -0
- package/lib/utils/getTruthyValue.d.ts +6 -0
- package/lib/utils/{getTruthyEnvValue.js → getTruthyValue.js} +7 -7
- package/lib/utils/getTruthyValue.mjs +6 -0
- package/package.json +9 -3
- package/lib/chunk-CQPU7577.mjs +0 -9
- package/lib/utils/getTruthyEnvValue.d.mts +0 -6
- package/lib/utils/getTruthyEnvValue.d.ts +0 -6
- package/lib/utils/getTruthyEnvValue.mjs +0 -6
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
- Supports custom middlewares
|
|
14
14
|
- Out-of-the-box CORS support
|
|
15
15
|
- Built-in customizable rate limiter
|
|
16
|
-
- Tiny (~
|
|
16
|
+
- Tiny (~4.8kb gzipped)
|
|
17
17
|
- Only a single dependency: [MikroConf](https://github.com/mikaelvesavuori/mikroconf)
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
@@ -46,22 +46,23 @@ api.start();
|
|
|
46
46
|
// The response should be "Hello world!"
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
### Bigger example
|
|
50
50
|
|
|
51
51
|
```typescript
|
|
52
52
|
import { MikroServe } from 'mikroserve';
|
|
53
53
|
|
|
54
|
-
// Create a new
|
|
54
|
+
// Create a new API instance
|
|
55
55
|
const api = new MikroServe({
|
|
56
|
+
// These are the default values
|
|
56
57
|
port: 3000,
|
|
57
58
|
host: '0.0.0.0',
|
|
58
59
|
useHttps: false,
|
|
59
60
|
sslCert: '',
|
|
60
61
|
sslKey: '',
|
|
61
62
|
sslCa: '',
|
|
62
|
-
debug:
|
|
63
|
+
debug: false,
|
|
63
64
|
rateLimit: {
|
|
64
|
-
requestsPerMinute:
|
|
65
|
+
requestsPerMinute: 100,
|
|
65
66
|
enabled: true
|
|
66
67
|
},
|
|
67
68
|
allowedDomains: ['*']
|
|
@@ -165,6 +166,42 @@ api.get('/error', () => {
|
|
|
165
166
|
api.start();
|
|
166
167
|
```
|
|
167
168
|
|
|
169
|
+
## Configuration
|
|
170
|
+
|
|
171
|
+
All of the settings already presented in the above examples can be provided in multiple ways.
|
|
172
|
+
|
|
173
|
+
- They can be provided via the CLI, e.g. `node app.js --port 1234`.
|
|
174
|
+
- Certain values can be provided via environment variables.
|
|
175
|
+
- Port: `process.env.PORT` - number
|
|
176
|
+
- Host: `process.env.HOST` - string
|
|
177
|
+
- Debug: `process.env.DEBUG` - boolean
|
|
178
|
+
- Programmatically/directly via scripting, e.g. `new MikroServe({ port: 1234 })`.
|
|
179
|
+
- They can be placed in a configuration file named `mikroserve.config.json` (plain JSON), which will be automatically applied on load.
|
|
180
|
+
|
|
181
|
+
### Options
|
|
182
|
+
|
|
183
|
+
| CLI argument | CLI value | JSON (config file) value | Environment variable |
|
|
184
|
+
|--------------|-----------------------------|-----------------------------|----------------------|
|
|
185
|
+
| --port | `<number>` | port | PORT |
|
|
186
|
+
| --host | `<string>` | host | HOST |
|
|
187
|
+
| --https | none (is flag) | useHttps | |
|
|
188
|
+
| --cert | `<string>` | sslCert | |
|
|
189
|
+
| --key | `<string>` | sslKey | |
|
|
190
|
+
| --ca | `<string>` | sslCa | |
|
|
191
|
+
| --debug | none (is flag) | debug | DEBUG |
|
|
192
|
+
| --ratelimit | none (is flag) | rateLimit.enabled | |
|
|
193
|
+
| --rps | `<number>` | rateLimit.requestsPerMinute | |
|
|
194
|
+
| --allowed | `<comma-separated strings>` | allowedDomains | |
|
|
195
|
+
|
|
196
|
+
### Order of application
|
|
197
|
+
|
|
198
|
+
As per [MikroConf](https://github.com/mikaelvesavuori/mikroconf) behavior, the configuration sources are applied in this order:
|
|
199
|
+
|
|
200
|
+
1. Command line arguments (highest priority)
|
|
201
|
+
2. Programmatically provided config
|
|
202
|
+
3. Config file (JSON)
|
|
203
|
+
4. Default values (lowest priority)
|
|
204
|
+
|
|
168
205
|
## Create self-signed HTTPS certificates
|
|
169
206
|
|
|
170
207
|
On Mac and Linux, run:
|
package/lib/MikroServe.d.mts
CHANGED
|
@@ -6,9 +6,9 @@ import { MikroServeOptions, Middleware, RouteHandler } from './interfaces/index.
|
|
|
6
6
|
* @description MikroServe manages HTTP server operations with routing.
|
|
7
7
|
*/
|
|
8
8
|
declare class MikroServe {
|
|
9
|
+
private config;
|
|
9
10
|
private rateLimiter;
|
|
10
11
|
private router;
|
|
11
|
-
private config;
|
|
12
12
|
/**
|
|
13
13
|
* @description Creates a new MikroServe instance.
|
|
14
14
|
*/
|
package/lib/MikroServe.d.ts
CHANGED
|
@@ -6,9 +6,9 @@ import { MikroServeOptions, Middleware, RouteHandler } from './interfaces/index.
|
|
|
6
6
|
* @description MikroServe manages HTTP server operations with routing.
|
|
7
7
|
*/
|
|
8
8
|
declare class MikroServe {
|
|
9
|
+
private config;
|
|
9
10
|
private rateLimiter;
|
|
10
11
|
private router;
|
|
11
|
-
private config;
|
|
12
12
|
/**
|
|
13
13
|
* @description Creates a new MikroServe instance.
|
|
14
14
|
*/
|
package/lib/MikroServe.js
CHANGED
|
@@ -281,8 +281,8 @@ var Router = class {
|
|
|
281
281
|
}
|
|
282
282
|
};
|
|
283
283
|
|
|
284
|
-
// src/utils/
|
|
285
|
-
function
|
|
284
|
+
// src/utils/getTruthyValue.ts
|
|
285
|
+
function getTruthyValue(value) {
|
|
286
286
|
if (value === "true" || value === true) return true;
|
|
287
287
|
return false;
|
|
288
288
|
}
|
|
@@ -296,7 +296,7 @@ var configDefaults = () => {
|
|
|
296
296
|
sslCert: "",
|
|
297
297
|
sslKey: "",
|
|
298
298
|
sslCa: "",
|
|
299
|
-
debug:
|
|
299
|
+
debug: getTruthyValue(process.env.DEBUG) || false,
|
|
300
300
|
rateLimit: {
|
|
301
301
|
enabled: true,
|
|
302
302
|
requestsPerMinute: 100
|
|
@@ -307,9 +307,9 @@ var configDefaults = () => {
|
|
|
307
307
|
|
|
308
308
|
// src/MikroServe.ts
|
|
309
309
|
var MikroServe = class {
|
|
310
|
+
config;
|
|
310
311
|
rateLimiter;
|
|
311
312
|
router;
|
|
312
|
-
config;
|
|
313
313
|
/**
|
|
314
314
|
* @description Creates a new MikroServe instance.
|
|
315
315
|
*/
|
|
@@ -494,7 +494,10 @@ var MikroServe = class {
|
|
|
494
494
|
});
|
|
495
495
|
}
|
|
496
496
|
const result = await this.router.handle(req, res);
|
|
497
|
-
if (result)
|
|
497
|
+
if (result) {
|
|
498
|
+
if (result._handled) return;
|
|
499
|
+
return this.respond(res, result);
|
|
500
|
+
}
|
|
498
501
|
return this.respond(res, {
|
|
499
502
|
statusCode: 404,
|
|
500
503
|
body: {
|
package/lib/MikroServe.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MikroServe
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-E3RGQ7QF.mjs";
|
|
4
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
5
5
|
import "./chunk-KJT4SET2.mjs";
|
|
6
|
-
import "./chunk-
|
|
7
|
-
import "./chunk-
|
|
6
|
+
import "./chunk-NSHBEU32.mjs";
|
|
7
|
+
import "./chunk-6HESV5Q6.mjs";
|
|
8
8
|
export {
|
|
9
9
|
MikroServe
|
|
10
10
|
};
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "./chunk-KJT4SET2.mjs";
|
|
7
7
|
import {
|
|
8
8
|
configDefaults
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-NSHBEU32.mjs";
|
|
10
10
|
|
|
11
11
|
// src/MikroServe.ts
|
|
12
12
|
import { readFileSync } from "node:fs";
|
|
@@ -14,9 +14,9 @@ import http from "node:http";
|
|
|
14
14
|
import https from "node:https";
|
|
15
15
|
import { MikroConf, parsers } from "mikroconf";
|
|
16
16
|
var MikroServe = class {
|
|
17
|
+
config;
|
|
17
18
|
rateLimiter;
|
|
18
19
|
router;
|
|
19
|
-
config;
|
|
20
20
|
/**
|
|
21
21
|
* @description Creates a new MikroServe instance.
|
|
22
22
|
*/
|
|
@@ -201,7 +201,10 @@ var MikroServe = class {
|
|
|
201
201
|
});
|
|
202
202
|
}
|
|
203
203
|
const result = await this.router.handle(req, res);
|
|
204
|
-
if (result)
|
|
204
|
+
if (result) {
|
|
205
|
+
if (result._handled) return;
|
|
206
|
+
return this.respond(res, result);
|
|
207
|
+
}
|
|
205
208
|
return this.respond(res, {
|
|
206
209
|
statusCode: 404,
|
|
207
210
|
body: {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
getTruthyValue
|
|
3
|
+
} from "./chunk-6HESV5Q6.mjs";
|
|
4
4
|
|
|
5
5
|
// src/utils/configDefaults.ts
|
|
6
6
|
var configDefaults = () => {
|
|
@@ -11,7 +11,7 @@ var configDefaults = () => {
|
|
|
11
11
|
sslCert: "",
|
|
12
12
|
sslKey: "",
|
|
13
13
|
sslCa: "",
|
|
14
|
-
debug:
|
|
14
|
+
debug: getTruthyValue(process.env.DEBUG) || false,
|
|
15
15
|
rateLimit: {
|
|
16
16
|
enabled: true,
|
|
17
17
|
requestsPerMinute: 100
|
package/lib/index.d.mts
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
"use strict";
|
|
3
2
|
var __create = Object.create;
|
|
4
3
|
var __defProp = Object.defineProperty;
|
|
@@ -284,8 +283,8 @@ var Router = class {
|
|
|
284
283
|
}
|
|
285
284
|
};
|
|
286
285
|
|
|
287
|
-
// src/utils/
|
|
288
|
-
function
|
|
286
|
+
// src/utils/getTruthyValue.ts
|
|
287
|
+
function getTruthyValue(value) {
|
|
289
288
|
if (value === "true" || value === true) return true;
|
|
290
289
|
return false;
|
|
291
290
|
}
|
|
@@ -299,7 +298,7 @@ var configDefaults = () => {
|
|
|
299
298
|
sslCert: "",
|
|
300
299
|
sslKey: "",
|
|
301
300
|
sslCa: "",
|
|
302
|
-
debug:
|
|
301
|
+
debug: getTruthyValue(process.env.DEBUG) || false,
|
|
303
302
|
rateLimit: {
|
|
304
303
|
enabled: true,
|
|
305
304
|
requestsPerMinute: 100
|
|
@@ -310,9 +309,9 @@ var configDefaults = () => {
|
|
|
310
309
|
|
|
311
310
|
// src/MikroServe.ts
|
|
312
311
|
var MikroServe = class {
|
|
312
|
+
config;
|
|
313
313
|
rateLimiter;
|
|
314
314
|
router;
|
|
315
|
-
config;
|
|
316
315
|
/**
|
|
317
316
|
* @description Creates a new MikroServe instance.
|
|
318
317
|
*/
|
|
@@ -497,7 +496,10 @@ var MikroServe = class {
|
|
|
497
496
|
});
|
|
498
497
|
}
|
|
499
498
|
const result = await this.router.handle(req, res);
|
|
500
|
-
if (result)
|
|
499
|
+
if (result) {
|
|
500
|
+
if (result._handled) return;
|
|
501
|
+
return this.respond(res, result);
|
|
502
|
+
}
|
|
501
503
|
return this.respond(res, {
|
|
502
504
|
statusCode: 404,
|
|
503
505
|
body: {
|
|
@@ -642,21 +644,6 @@ var MikroServe = class {
|
|
|
642
644
|
process.on("unhandledRejection", shutdown);
|
|
643
645
|
}
|
|
644
646
|
};
|
|
645
|
-
|
|
646
|
-
// src/index.ts
|
|
647
|
-
async function main() {
|
|
648
|
-
const isRunFromCommandLine = process.argv[1]?.includes("node_modules/.bin/mikroserve");
|
|
649
|
-
const force = process.argv[1]?.includes("mikroauth/src/index.ts") && (process.argv[2] || "") === "--force";
|
|
650
|
-
if (isRunFromCommandLine || force && isRunFromCommandLine) {
|
|
651
|
-
console.log("\u{1F680} Welcome to MikroServe! \u2728");
|
|
652
|
-
try {
|
|
653
|
-
new MikroServe();
|
|
654
|
-
} catch (error) {
|
|
655
|
-
console.error(error);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
main();
|
|
660
647
|
// Annotate the CommonJS export names for ESM import in node:
|
|
661
648
|
0 && (module.exports = {
|
|
662
649
|
MikroServe
|
package/lib/index.mjs
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
1
|
import {
|
|
3
2
|
MikroServe
|
|
4
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-E3RGQ7QF.mjs";
|
|
5
4
|
import "./chunk-ZFBBESGU.mjs";
|
|
6
5
|
import "./chunk-KJT4SET2.mjs";
|
|
7
|
-
import "./chunk-
|
|
8
|
-
import "./chunk-
|
|
9
|
-
|
|
10
|
-
// src/index.ts
|
|
11
|
-
async function main() {
|
|
12
|
-
const isRunFromCommandLine = process.argv[1]?.includes("node_modules/.bin/mikroserve");
|
|
13
|
-
const force = process.argv[1]?.includes("mikroauth/src/index.ts") && (process.argv[2] || "") === "--force";
|
|
14
|
-
if (isRunFromCommandLine || force && isRunFromCommandLine) {
|
|
15
|
-
console.log("\u{1F680} Welcome to MikroServe! \u2728");
|
|
16
|
-
try {
|
|
17
|
-
new MikroServe();
|
|
18
|
-
} catch (error) {
|
|
19
|
-
console.error(error);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
main();
|
|
6
|
+
import "./chunk-NSHBEU32.mjs";
|
|
7
|
+
import "./chunk-6HESV5Q6.mjs";
|
|
24
8
|
export {
|
|
25
9
|
MikroServe
|
|
26
10
|
};
|
|
@@ -25,8 +25,8 @@ __export(configDefaults_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(configDefaults_exports);
|
|
27
27
|
|
|
28
|
-
// src/utils/
|
|
29
|
-
function
|
|
28
|
+
// src/utils/getTruthyValue.ts
|
|
29
|
+
function getTruthyValue(value) {
|
|
30
30
|
if (value === "true" || value === true) return true;
|
|
31
31
|
return false;
|
|
32
32
|
}
|
|
@@ -40,7 +40,7 @@ var configDefaults = () => {
|
|
|
40
40
|
sslCert: "",
|
|
41
41
|
sslKey: "",
|
|
42
42
|
sslCa: "",
|
|
43
|
-
debug:
|
|
43
|
+
debug: getTruthyValue(process.env.DEBUG) || false,
|
|
44
44
|
rateLimit: {
|
|
45
45
|
enabled: true,
|
|
46
46
|
requestsPerMinute: 100
|
|
@@ -17,17 +17,17 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/utils/
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
23
|
-
|
|
20
|
+
// src/utils/getTruthyValue.ts
|
|
21
|
+
var getTruthyValue_exports = {};
|
|
22
|
+
__export(getTruthyValue_exports, {
|
|
23
|
+
getTruthyValue: () => getTruthyValue
|
|
24
24
|
});
|
|
25
|
-
module.exports = __toCommonJS(
|
|
26
|
-
function
|
|
25
|
+
module.exports = __toCommonJS(getTruthyValue_exports);
|
|
26
|
+
function getTruthyValue(value) {
|
|
27
27
|
if (value === "true" || value === true) return true;
|
|
28
28
|
return false;
|
|
29
29
|
}
|
|
30
30
|
// Annotate the CommonJS export names for ESM import in node:
|
|
31
31
|
0 && (module.exports = {
|
|
32
|
-
|
|
32
|
+
getTruthyValue
|
|
33
33
|
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mikroserve",
|
|
3
3
|
"description": "Minimalistic, ready-to-use API, built on Node.js primitives.",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.6",
|
|
5
5
|
"author": "Mikael Vesavuori",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"keywords": [
|
|
7
|
+
"keywords": [
|
|
8
|
+
"node",
|
|
9
|
+
"node-server",
|
|
10
|
+
"api",
|
|
11
|
+
"api-server",
|
|
12
|
+
"https"
|
|
13
|
+
],
|
|
8
14
|
"main": "lib/index.js",
|
|
9
15
|
"repository": {
|
|
10
16
|
"type": "git",
|
|
@@ -29,7 +35,7 @@
|
|
|
29
35
|
"mikroserve": "lib/index.js"
|
|
30
36
|
},
|
|
31
37
|
"scripts": {
|
|
32
|
-
"start": "npx tsx src/index.ts
|
|
38
|
+
"start": "npx tsx src/index.ts",
|
|
33
39
|
"test": "npm run test:licenses && npm run test:types && npm run lint && npm run test:unit",
|
|
34
40
|
"test:types": "npx type-coverage --at-least 85 --strict --ignore-files \"tests/**/*.ts\" --ignore-files \"*.ts\" --ignore-files \"src/application/errors/*.ts\" --ignore-files \"testdata/*.ts\"",
|
|
35
41
|
"test:licenses": "npx license-compliance --direct --allow 'MIT;ISC;0BSD;BSD-2-Clause;BSD-3-Clause;Apache-2.0;Unlicense;CC0-1.0'",
|
package/lib/chunk-CQPU7577.mjs
DELETED