mikroserve 0.0.1
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/LICENSE +7 -0
- package/README.md +3 -0
- package/lib/MikroServe.d.mts +106 -0
- package/lib/MikroServe.d.ts +106 -0
- package/lib/MikroServe.js +621 -0
- package/lib/MikroServe.mjs +8 -0
- package/lib/RateLimiter.d.mts +16 -0
- package/lib/RateLimiter.d.ts +16 -0
- package/lib/RateLimiter.js +73 -0
- package/lib/RateLimiter.mjs +6 -0
- package/lib/Router.d.mts +64 -0
- package/lib/Router.d.ts +64 -0
- package/lib/Router.js +224 -0
- package/lib/Router.mjs +6 -0
- package/lib/chunk-GGGGATKH.mjs +349 -0
- package/lib/chunk-KJT4SET2.mjs +200 -0
- package/lib/chunk-ZFBBESGU.mjs +49 -0
- package/lib/index.d.mts +4 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +623 -0
- package/lib/index.mjs +8 -0
- package/lib/interfaces/index.d.mts +264 -0
- package/lib/interfaces/index.d.ts +264 -0
- package/lib/interfaces/index.js +18 -0
- package/lib/interfaces/index.mjs +0 -0
- package/package.json +58 -0
|
@@ -0,0 +1,73 @@
|
|
|
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/RateLimiter.ts
|
|
21
|
+
var RateLimiter_exports = {};
|
|
22
|
+
__export(RateLimiter_exports, {
|
|
23
|
+
RateLimiter: () => RateLimiter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(RateLimiter_exports);
|
|
26
|
+
var RateLimiter = class {
|
|
27
|
+
requests = /* @__PURE__ */ new Map();
|
|
28
|
+
limit;
|
|
29
|
+
windowMs;
|
|
30
|
+
constructor(limit = 100, windowSeconds = 60) {
|
|
31
|
+
this.limit = limit;
|
|
32
|
+
this.windowMs = windowSeconds * 1e3;
|
|
33
|
+
setInterval(() => this.cleanup(), this.windowMs);
|
|
34
|
+
}
|
|
35
|
+
getLimit() {
|
|
36
|
+
return this.limit;
|
|
37
|
+
}
|
|
38
|
+
isAllowed(ip) {
|
|
39
|
+
const now = Date.now();
|
|
40
|
+
const key = ip || "unknown";
|
|
41
|
+
let entry = this.requests.get(key);
|
|
42
|
+
if (!entry || entry.resetTime < now) {
|
|
43
|
+
entry = { count: 0, resetTime: now + this.windowMs };
|
|
44
|
+
this.requests.set(key, entry);
|
|
45
|
+
}
|
|
46
|
+
entry.count++;
|
|
47
|
+
return entry.count <= this.limit;
|
|
48
|
+
}
|
|
49
|
+
getRemainingRequests(ip) {
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
const key = ip || "unknown";
|
|
52
|
+
const entry = this.requests.get(key);
|
|
53
|
+
if (!entry || entry.resetTime < now) return this.limit;
|
|
54
|
+
return Math.max(0, this.limit - entry.count);
|
|
55
|
+
}
|
|
56
|
+
getResetTime(ip) {
|
|
57
|
+
const now = Date.now();
|
|
58
|
+
const key = ip || "unknown";
|
|
59
|
+
const entry = this.requests.get(key);
|
|
60
|
+
if (!entry || entry.resetTime < now) return Math.floor((now + this.windowMs) / 1e3);
|
|
61
|
+
return Math.floor(entry.resetTime / 1e3);
|
|
62
|
+
}
|
|
63
|
+
cleanup() {
|
|
64
|
+
const now = Date.now();
|
|
65
|
+
for (const [key, entry] of this.requests.entries()) {
|
|
66
|
+
if (entry.resetTime < now) this.requests.delete(key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
RateLimiter
|
|
73
|
+
});
|
package/lib/Router.d.mts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { Middleware, RouteHandler, Route, HandlerResponse } from './interfaces/index.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Router class to manage routes and middleware
|
|
6
|
+
*/
|
|
7
|
+
declare class Router {
|
|
8
|
+
private routes;
|
|
9
|
+
private globalMiddlewares;
|
|
10
|
+
private pathPatterns;
|
|
11
|
+
/**
|
|
12
|
+
* Add a global middleware
|
|
13
|
+
*/
|
|
14
|
+
use(middleware: Middleware): this;
|
|
15
|
+
/**
|
|
16
|
+
* Register a route with specified method
|
|
17
|
+
*/
|
|
18
|
+
private register;
|
|
19
|
+
/**
|
|
20
|
+
* Register a GET route
|
|
21
|
+
*/
|
|
22
|
+
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
23
|
+
/**
|
|
24
|
+
* Register a POST route
|
|
25
|
+
*/
|
|
26
|
+
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
27
|
+
/**
|
|
28
|
+
* Register a PUT route
|
|
29
|
+
*/
|
|
30
|
+
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
31
|
+
/**
|
|
32
|
+
* Register a DELETE route
|
|
33
|
+
*/
|
|
34
|
+
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
35
|
+
/**
|
|
36
|
+
* Register a PATCH route
|
|
37
|
+
*/
|
|
38
|
+
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
39
|
+
/**
|
|
40
|
+
* Register an OPTIONS route
|
|
41
|
+
*/
|
|
42
|
+
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
43
|
+
/**
|
|
44
|
+
* Match a request to a route
|
|
45
|
+
*/
|
|
46
|
+
match(method: string, path: string): {
|
|
47
|
+
route: Route;
|
|
48
|
+
params: Record<string, string>;
|
|
49
|
+
} | null;
|
|
50
|
+
/**
|
|
51
|
+
* Create a regex pattern for path matching
|
|
52
|
+
*/
|
|
53
|
+
private createPathPattern;
|
|
54
|
+
/**
|
|
55
|
+
* Handle a request and find the matching route
|
|
56
|
+
*/
|
|
57
|
+
handle(req: http.IncomingMessage, res: http.ServerResponse): Promise<HandlerResponse | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Execute middleware chain and final handler
|
|
60
|
+
*/
|
|
61
|
+
private executeMiddlewareChain;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { Router };
|
package/lib/Router.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import { Middleware, RouteHandler, Route, HandlerResponse } from './interfaces/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Router class to manage routes and middleware
|
|
6
|
+
*/
|
|
7
|
+
declare class Router {
|
|
8
|
+
private routes;
|
|
9
|
+
private globalMiddlewares;
|
|
10
|
+
private pathPatterns;
|
|
11
|
+
/**
|
|
12
|
+
* Add a global middleware
|
|
13
|
+
*/
|
|
14
|
+
use(middleware: Middleware): this;
|
|
15
|
+
/**
|
|
16
|
+
* Register a route with specified method
|
|
17
|
+
*/
|
|
18
|
+
private register;
|
|
19
|
+
/**
|
|
20
|
+
* Register a GET route
|
|
21
|
+
*/
|
|
22
|
+
get(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
23
|
+
/**
|
|
24
|
+
* Register a POST route
|
|
25
|
+
*/
|
|
26
|
+
post(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
27
|
+
/**
|
|
28
|
+
* Register a PUT route
|
|
29
|
+
*/
|
|
30
|
+
put(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
31
|
+
/**
|
|
32
|
+
* Register a DELETE route
|
|
33
|
+
*/
|
|
34
|
+
delete(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
35
|
+
/**
|
|
36
|
+
* Register a PATCH route
|
|
37
|
+
*/
|
|
38
|
+
patch(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
39
|
+
/**
|
|
40
|
+
* Register an OPTIONS route
|
|
41
|
+
*/
|
|
42
|
+
options(path: string, ...handlers: (RouteHandler | Middleware)[]): this;
|
|
43
|
+
/**
|
|
44
|
+
* Match a request to a route
|
|
45
|
+
*/
|
|
46
|
+
match(method: string, path: string): {
|
|
47
|
+
route: Route;
|
|
48
|
+
params: Record<string, string>;
|
|
49
|
+
} | null;
|
|
50
|
+
/**
|
|
51
|
+
* Create a regex pattern for path matching
|
|
52
|
+
*/
|
|
53
|
+
private createPathPattern;
|
|
54
|
+
/**
|
|
55
|
+
* Handle a request and find the matching route
|
|
56
|
+
*/
|
|
57
|
+
handle(req: http.IncomingMessage, res: http.ServerResponse): Promise<HandlerResponse | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Execute middleware chain and final handler
|
|
60
|
+
*/
|
|
61
|
+
private executeMiddlewareChain;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { Router };
|
package/lib/Router.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
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/Router.ts
|
|
21
|
+
var Router_exports = {};
|
|
22
|
+
__export(Router_exports, {
|
|
23
|
+
Router: () => Router
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(Router_exports);
|
|
26
|
+
var import_node_url = require("url");
|
|
27
|
+
var Router = class {
|
|
28
|
+
routes = [];
|
|
29
|
+
globalMiddlewares = [];
|
|
30
|
+
pathPatterns = /* @__PURE__ */ new Map();
|
|
31
|
+
/**
|
|
32
|
+
* Add a global middleware
|
|
33
|
+
*/
|
|
34
|
+
use(middleware) {
|
|
35
|
+
this.globalMiddlewares.push(middleware);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Register a route with specified method
|
|
40
|
+
*/
|
|
41
|
+
register(method, path, handler, middlewares = []) {
|
|
42
|
+
this.routes.push({ method, path, handler, middlewares });
|
|
43
|
+
this.pathPatterns.set(path, this.createPathPattern(path));
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Register a GET route
|
|
48
|
+
*/
|
|
49
|
+
get(path, ...handlers) {
|
|
50
|
+
const handler = handlers.pop();
|
|
51
|
+
return this.register("GET", path, handler, handlers);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Register a POST route
|
|
55
|
+
*/
|
|
56
|
+
post(path, ...handlers) {
|
|
57
|
+
const handler = handlers.pop();
|
|
58
|
+
return this.register("POST", path, handler, handlers);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Register a PUT route
|
|
62
|
+
*/
|
|
63
|
+
put(path, ...handlers) {
|
|
64
|
+
const handler = handlers.pop();
|
|
65
|
+
return this.register("PUT", path, handler, handlers);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Register a DELETE route
|
|
69
|
+
*/
|
|
70
|
+
delete(path, ...handlers) {
|
|
71
|
+
const handler = handlers.pop();
|
|
72
|
+
return this.register("DELETE", path, handler, handlers);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Register a PATCH route
|
|
76
|
+
*/
|
|
77
|
+
patch(path, ...handlers) {
|
|
78
|
+
const handler = handlers.pop();
|
|
79
|
+
return this.register("PATCH", path, handler, handlers);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Register an OPTIONS route
|
|
83
|
+
*/
|
|
84
|
+
options(path, ...handlers) {
|
|
85
|
+
const handler = handlers.pop();
|
|
86
|
+
return this.register("OPTIONS", path, handler, handlers);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Match a request to a route
|
|
90
|
+
*/
|
|
91
|
+
match(method, path) {
|
|
92
|
+
for (const route of this.routes) {
|
|
93
|
+
if (route.method !== method) continue;
|
|
94
|
+
const pathPattern = this.pathPatterns.get(route.path);
|
|
95
|
+
if (!pathPattern) continue;
|
|
96
|
+
const match = pathPattern.pattern.exec(path);
|
|
97
|
+
if (!match) continue;
|
|
98
|
+
const params = {};
|
|
99
|
+
pathPattern.paramNames.forEach((name, index) => {
|
|
100
|
+
params[name] = match[index + 1] || "";
|
|
101
|
+
});
|
|
102
|
+
return { route, params };
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a regex pattern for path matching
|
|
108
|
+
*/
|
|
109
|
+
createPathPattern(path) {
|
|
110
|
+
const paramNames = [];
|
|
111
|
+
const pattern = path.replace(/\/:[^/]+/g, (match) => {
|
|
112
|
+
const paramName = match.slice(2);
|
|
113
|
+
paramNames.push(paramName);
|
|
114
|
+
return "/([^/]+)";
|
|
115
|
+
}).replace(/\/$/, "/?");
|
|
116
|
+
return {
|
|
117
|
+
pattern: new RegExp(`^${pattern}$`),
|
|
118
|
+
paramNames
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Handle a request and find the matching route
|
|
123
|
+
*/
|
|
124
|
+
async handle(req, res) {
|
|
125
|
+
const method = req.method || "GET";
|
|
126
|
+
const url = new import_node_url.URL(req.url || "/", `http://${req.headers.host}`);
|
|
127
|
+
const path = url.pathname;
|
|
128
|
+
const matched = this.match(method, path);
|
|
129
|
+
if (!matched) return null;
|
|
130
|
+
const { route, params } = matched;
|
|
131
|
+
const query = {};
|
|
132
|
+
url.searchParams.forEach((value, key) => {
|
|
133
|
+
query[key] = value;
|
|
134
|
+
});
|
|
135
|
+
const context = {
|
|
136
|
+
req,
|
|
137
|
+
res,
|
|
138
|
+
params,
|
|
139
|
+
query,
|
|
140
|
+
// @ts-ignore
|
|
141
|
+
body: req.body || {},
|
|
142
|
+
headers: req.headers,
|
|
143
|
+
path,
|
|
144
|
+
state: {},
|
|
145
|
+
// Add the missing state property
|
|
146
|
+
text: (content, status = 200) => ({
|
|
147
|
+
statusCode: status,
|
|
148
|
+
body: content,
|
|
149
|
+
headers: { "Content-Type": "text/plain" }
|
|
150
|
+
}),
|
|
151
|
+
form: (content, status = 200) => ({
|
|
152
|
+
statusCode: status,
|
|
153
|
+
body: content,
|
|
154
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
155
|
+
}),
|
|
156
|
+
json: (content, status = 200) => ({
|
|
157
|
+
statusCode: status,
|
|
158
|
+
body: content,
|
|
159
|
+
headers: { "Content-Type": "application/json" }
|
|
160
|
+
}),
|
|
161
|
+
html: (content, status = 200) => ({
|
|
162
|
+
statusCode: status,
|
|
163
|
+
body: content,
|
|
164
|
+
headers: { "Content-Type": "text/html" }
|
|
165
|
+
}),
|
|
166
|
+
redirect: (url2, status = 302) => ({
|
|
167
|
+
statusCode: status,
|
|
168
|
+
body: null,
|
|
169
|
+
headers: { Location: url2 }
|
|
170
|
+
}),
|
|
171
|
+
status: function(code) {
|
|
172
|
+
return {
|
|
173
|
+
text: (content) => ({
|
|
174
|
+
statusCode: code,
|
|
175
|
+
body: content,
|
|
176
|
+
headers: { "Content-Type": "text/plain" }
|
|
177
|
+
}),
|
|
178
|
+
json: (data) => ({
|
|
179
|
+
statusCode: code,
|
|
180
|
+
body: data,
|
|
181
|
+
headers: { "Content-Type": "application/json" }
|
|
182
|
+
}),
|
|
183
|
+
html: (content) => ({
|
|
184
|
+
statusCode: code,
|
|
185
|
+
body: content,
|
|
186
|
+
headers: { "Content-Type": "text/html" }
|
|
187
|
+
}),
|
|
188
|
+
form: (content) => ({
|
|
189
|
+
// Make sure form method is included here
|
|
190
|
+
statusCode: code,
|
|
191
|
+
body: content,
|
|
192
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" }
|
|
193
|
+
}),
|
|
194
|
+
redirect: (url2, redirectStatus = 302) => ({
|
|
195
|
+
statusCode: redirectStatus,
|
|
196
|
+
body: null,
|
|
197
|
+
headers: { Location: url2 }
|
|
198
|
+
}),
|
|
199
|
+
status: (updatedCode) => this.status(updatedCode)
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
const middlewares = [...this.globalMiddlewares, ...route.middlewares];
|
|
204
|
+
return this.executeMiddlewareChain(context, middlewares, route.handler);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Execute middleware chain and final handler
|
|
208
|
+
*/
|
|
209
|
+
async executeMiddlewareChain(context, middlewares, finalHandler) {
|
|
210
|
+
let currentIndex = 0;
|
|
211
|
+
const next = async () => {
|
|
212
|
+
if (currentIndex < middlewares.length) {
|
|
213
|
+
const middleware = middlewares[currentIndex++];
|
|
214
|
+
return middleware(context, next);
|
|
215
|
+
}
|
|
216
|
+
return finalHandler(context);
|
|
217
|
+
};
|
|
218
|
+
return next();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
222
|
+
0 && (module.exports = {
|
|
223
|
+
Router
|
|
224
|
+
});
|