@vernali/core 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/README.md +34 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/applicationCore.d.ts +34 -0
- package/dist/lib/applicationCore.d.ts.map +1 -0
- package/dist/lib/applicationCore.js +92 -0
- package/dist/lib/applicationCore.js.map +1 -0
- package/dist/lib/bodyParser.d.ts +4 -0
- package/dist/lib/bodyParser.d.ts.map +1 -0
- package/dist/lib/bodyParser.js +17 -0
- package/dist/lib/bodyParser.js.map +1 -0
- package/dist/lib/context.d.ts +28 -0
- package/dist/lib/context.d.ts.map +1 -0
- package/dist/lib/context.js +110 -0
- package/dist/lib/context.js.map +1 -0
- package/dist/lib/router/compose.d.ts +3 -0
- package/dist/lib/router/compose.d.ts.map +1 -0
- package/dist/lib/router/compose.js +21 -0
- package/dist/lib/router/compose.js.map +1 -0
- package/dist/lib/router/index.d.ts +3 -0
- package/dist/lib/router/index.d.ts.map +1 -0
- package/dist/lib/router/index.js +3 -0
- package/dist/lib/router/index.js.map +1 -0
- package/dist/lib/router/layer.d.ts +24 -0
- package/dist/lib/router/layer.d.ts.map +1 -0
- package/dist/lib/router/layer.js +150 -0
- package/dist/lib/router/layer.js.map +1 -0
- package/dist/lib/router/router.d.ts +14 -0
- package/dist/lib/router/router.d.ts.map +1 -0
- package/dist/lib/router/router.js +133 -0
- package/dist/lib/router/router.js.map +1 -0
- package/dist/types/core.d.ts +18 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/core.js +2 -0
- package/dist/types/core.js.map +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# vernali-core
|
|
2
|
+
|
|
3
|
+
Core functionality for the Vernali framework.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install vernali-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { ApplicationCore, Router } from 'vernali-core'
|
|
15
|
+
|
|
16
|
+
const app = new ApplicationCore()
|
|
17
|
+
const router = new Router()
|
|
18
|
+
|
|
19
|
+
router.get('/hello', (ctx) => {
|
|
20
|
+
ctx.send('Hello, World!\n')
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
app.use('/api', router)
|
|
24
|
+
|
|
25
|
+
app.createServer().listen(3000)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Scripts
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm run build
|
|
32
|
+
npm run type-check
|
|
33
|
+
npm pack --dry-run
|
|
34
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ApplicationCore } from './lib/applicationCore.js';
|
|
2
|
+
export { Context } from './lib/context.js';
|
|
3
|
+
export { Router } from './lib/router/index.js';
|
|
4
|
+
export { bodyParser } from './lib/bodyParser.js';
|
|
5
|
+
export type { RenderOptions } from './lib/context.js';
|
|
6
|
+
export type { Context as CoreContext } from './types/core.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAEhD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AACrD,YAAY,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,iBAAiB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import http, { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
import { Context, type RenderOptions } from './context.js';
|
|
3
|
+
import { Context as ContextType } from '../types/core.js';
|
|
4
|
+
import { Router } from './router/index.js';
|
|
5
|
+
declare class ApplicationCore {
|
|
6
|
+
router: Router;
|
|
7
|
+
renderOptions: RenderOptions;
|
|
8
|
+
constructor();
|
|
9
|
+
createContext(req: IncomingMessage, res: ServerResponse): Context;
|
|
10
|
+
setViews(engine: string, views: string): this;
|
|
11
|
+
loadContext(ctx: ContextType): Promise<void>;
|
|
12
|
+
dispatch(ctx: ContextType): Promise<void>;
|
|
13
|
+
handleNotFound(ctx: ContextType): Promise<void>;
|
|
14
|
+
handleError(err: any, ctx: ContextType): Promise<void>;
|
|
15
|
+
handleRequest(req: IncomingMessage, res: ServerResponse, handlers?: {
|
|
16
|
+
dispatch?: (ctx: ContextType) => Promise<void>;
|
|
17
|
+
onNotFound?: (ctx: ContextType) => Promise<void>;
|
|
18
|
+
onError?: (err: any, ctx: ContextType) => Promise<void>;
|
|
19
|
+
}): Promise<void>;
|
|
20
|
+
createServer(handlers?: {
|
|
21
|
+
dispatch?: (ctx: ContextType) => Promise<void>;
|
|
22
|
+
onNotFound?: (ctx: ContextType) => Promise<void>;
|
|
23
|
+
onError?: (err: any, ctx: ContextType) => Promise<void>;
|
|
24
|
+
}): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
25
|
+
use(...args: [any, ...any[]]): this;
|
|
26
|
+
get(path: string | RegExp, ...handlers: any[]): this;
|
|
27
|
+
post(path: string | RegExp, ...handlers: any[]): this;
|
|
28
|
+
put(path: string | RegExp, ...handlers: any[]): this;
|
|
29
|
+
delete(path: string | RegExp, ...handlers: any[]): this;
|
|
30
|
+
patch(path: string | RegExp, ...handlers: any[]): this;
|
|
31
|
+
all(path: string | RegExp, ...handlers: any[]): this;
|
|
32
|
+
}
|
|
33
|
+
export { ApplicationCore };
|
|
34
|
+
//# sourceMappingURL=applicationCore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applicationCore.d.ts","sourceRoot":"","sources":["../../src/lib/applicationCore.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAE5D,OAAO,EAAE,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAA;AAC1D,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAE1C,cAAM,eAAe;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,aAAa,EAAE,aAAa,CAAA;;IAO5B,aAAa,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc;IAIvD,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAUhC,WAAW,CAAC,GAAG,EAAE,WAAW;IAM5B,QAAQ,CAAC,GAAG,EAAE,WAAW;IAIzB,cAAc,CAAC,GAAG,EAAE,WAAW;IAM/B,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW;IAYtC,aAAa,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,EAAE,QAAQ,GAAE;QACvE,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,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1
|
+
import http from 'http';
|
|
2
|
+
import { bodyParser } from './bodyParser.js';
|
|
3
|
+
import { Context } from './context.js';
|
|
4
|
+
import { Router } from './router/index.js';
|
|
5
|
+
class ApplicationCore {
|
|
6
|
+
router;
|
|
7
|
+
renderOptions;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.router = new Router();
|
|
10
|
+
this.renderOptions = {};
|
|
11
|
+
}
|
|
12
|
+
createContext(req, res) {
|
|
13
|
+
return new Context(req, res, this.renderOptions);
|
|
14
|
+
}
|
|
15
|
+
setViews(engine, views) {
|
|
16
|
+
this.renderOptions = {
|
|
17
|
+
...this.renderOptions,
|
|
18
|
+
engine,
|
|
19
|
+
views
|
|
20
|
+
};
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
async loadContext(ctx) {
|
|
24
|
+
if (['POST', 'PUT', 'PATCH'].includes(ctx.req.method)) {
|
|
25
|
+
ctx.body = await bodyParser(ctx.req);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async dispatch(ctx) {
|
|
29
|
+
await this.router.handle(ctx);
|
|
30
|
+
}
|
|
31
|
+
async handleNotFound(ctx) {
|
|
32
|
+
ctx.res.statusCode = 404;
|
|
33
|
+
ctx.res.setHeader('Content-Type', 'text/plain');
|
|
34
|
+
ctx.res.end('Not Found\n');
|
|
35
|
+
}
|
|
36
|
+
async handleError(err, ctx) {
|
|
37
|
+
console.error(err);
|
|
38
|
+
if (ctx.res.writableEnded) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
ctx.res.statusCode = 500;
|
|
42
|
+
ctx.res.setHeader('Content-Type', 'text/plain');
|
|
43
|
+
ctx.res.end('Internal Server Error\n');
|
|
44
|
+
}
|
|
45
|
+
async handleRequest(req, res, handlers = {}) {
|
|
46
|
+
const ctx = this.createContext(req, res);
|
|
47
|
+
const { dispatch = this.dispatch.bind(this), onNotFound = this.handleNotFound.bind(this), onError = this.handleError.bind(this) } = handlers;
|
|
48
|
+
try {
|
|
49
|
+
await this.loadContext(ctx);
|
|
50
|
+
await dispatch(ctx);
|
|
51
|
+
if (!res.writableEnded) {
|
|
52
|
+
await onNotFound(ctx);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
await onError(err, ctx);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
createServer(handlers = {}) {
|
|
60
|
+
return http.createServer((req, res) => this.handleRequest(req, res, handlers));
|
|
61
|
+
}
|
|
62
|
+
use(...args) {
|
|
63
|
+
this.router.use(...args);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
get(path, ...handlers) {
|
|
67
|
+
this.router.get(path, ...handlers);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
post(path, ...handlers) {
|
|
71
|
+
this.router.post(path, ...handlers);
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
put(path, ...handlers) {
|
|
75
|
+
this.router.put(path, ...handlers);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
delete(path, ...handlers) {
|
|
79
|
+
this.router.delete(path, ...handlers);
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
patch(path, ...handlers) {
|
|
83
|
+
this.router.patch(path, ...handlers);
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
all(path, ...handlers) {
|
|
87
|
+
this.router.all(path, ...handlers);
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export { ApplicationCore };
|
|
92
|
+
//# sourceMappingURL=applicationCore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"applicationCore.js","sourceRoot":"","sources":["../../src/lib/applicationCore.ts"],"names":[],"mappings":"AAAA,OAAO,IAAyC,MAAM,MAAM,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,OAAO,EAAE,OAAO,EAAsB,MAAM,cAAc,CAAA;AAE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,CAAA;AAEtC,QAAA,MAAM,UAAU,GAAI,KAAK,eAAe,KAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAiBrE,CAAA;AAED,OAAO,EACL,UAAU,EACX,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const bodyParser = (req) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
let body = '';
|
|
4
|
+
req.on('data', chunk => {
|
|
5
|
+
body += chunk.toString();
|
|
6
|
+
});
|
|
7
|
+
req.on('end', () => {
|
|
8
|
+
const parsedBody = body ? JSON.parse(body) : {};
|
|
9
|
+
resolve(parsedBody);
|
|
10
|
+
});
|
|
11
|
+
req.on('error', error => {
|
|
12
|
+
reject(error);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
export { bodyParser };
|
|
17
|
+
//# sourceMappingURL=bodyParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bodyParser.js","sourceRoot":"","sources":["../../src/lib/bodyParser.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,GAAG,CAAC,GAAoB,EAAgC,EAAE;IACxE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,IAAI,GAAG,EAAE,CAAA;QAEb,GAAG,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE;YACrB,IAAI,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAA;QAC1B,CAAC,CAAC,CAAA;QAEF,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YACjB,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;YAC/C,OAAO,CAAC,UAAU,CAAC,CAAA;QACrB,CAAC,CAAC,CAAA;QAEF,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE;YACtB,MAAM,CAAC,KAAK,CAAC,CAAA;QACf,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,OAAO,EACL,UAAU,EACX,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
import { Context as ContextType } from '../types/core.js';
|
|
3
|
+
type ViewData = Record<string, any>;
|
|
4
|
+
type RenderOptions = {
|
|
5
|
+
engine?: string;
|
|
6
|
+
views?: string;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
declare class Context implements ContextType {
|
|
10
|
+
req: IncomingMessage;
|
|
11
|
+
res: ServerResponse;
|
|
12
|
+
body?: Record<string, any> | undefined;
|
|
13
|
+
state?: Record<string, any> | undefined;
|
|
14
|
+
originalUrl: string;
|
|
15
|
+
baseUrl: string;
|
|
16
|
+
path: string;
|
|
17
|
+
params: Record<string, string>;
|
|
18
|
+
query: Record<string, string>;
|
|
19
|
+
searchParams: URLSearchParams;
|
|
20
|
+
renderOptions: RenderOptions;
|
|
21
|
+
constructor(req: IncomingMessage, res: ServerResponse, renderOptions?: RenderOptions);
|
|
22
|
+
json(data: Record<string, any>): void;
|
|
23
|
+
send(data: string): void;
|
|
24
|
+
render(view: string, data?: ViewData, options?: RenderOptions): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export { Context };
|
|
27
|
+
export type { RenderOptions };
|
|
28
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/lib/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAGtD,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEzD,KAAK,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AACnC,KAAK,aAAa,GAAG;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CACnB,CAAA;AA6ED,cAAM,OAAQ,YAAW,WAAW;IAClC,GAAG,EAAE,eAAe,CAAA;IACpB,GAAG,EAAE,cAAc,CAAA;IAGnB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAA;IACtC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAA;IACvC,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B,YAAY,EAAE,eAAe,CAAA;IAC7B,aAAa,EAAE,aAAa,CAAA;gBAEhB,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,EAAE,aAAa,GAAE,aAAkB;IAgBxF,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAK9B,IAAI,CAAC,IAAI,EAAE,MAAM;IAKX,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,GAAE,QAAa,EAAE,OAAO,GAAE,aAAkB;CAS5E;AAED,OAAO,EACL,OAAO,EACR,CAAA;AAED,YAAY,EACV,aAAa,EACd,CAAA"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
const getValue = (data, key) => {
|
|
4
|
+
return key.split('.').reduce((value, pathSegment) => {
|
|
5
|
+
if (value == null) {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
return value[pathSegment];
|
|
9
|
+
}, data);
|
|
10
|
+
};
|
|
11
|
+
const escapeHtml = (value) => {
|
|
12
|
+
return String(value)
|
|
13
|
+
.replace(/&/g, '&')
|
|
14
|
+
.replace(/</g, '<')
|
|
15
|
+
.replace(/>/g, '>')
|
|
16
|
+
.replace(/"/g, '"')
|
|
17
|
+
.replace(/'/g, ''');
|
|
18
|
+
};
|
|
19
|
+
const renderTemplate = (template, data) => {
|
|
20
|
+
return template.replace(/\{\{\s*([\w.]+)\s*\}\}/g, (_, key) => {
|
|
21
|
+
const value = getValue(data, key);
|
|
22
|
+
return value == null ? '' : escapeHtml(value);
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const getViewEngine = (view, options) => {
|
|
26
|
+
const extension = path.extname(view).slice(1);
|
|
27
|
+
return ((options.engine ?? extension) || 'html').toLowerCase();
|
|
28
|
+
};
|
|
29
|
+
const resolveViewPath = (view, engine, options) => {
|
|
30
|
+
const root = options.views ? path.resolve(options.views) : process.cwd();
|
|
31
|
+
const viewPath = path.isAbsolute(view)
|
|
32
|
+
? view
|
|
33
|
+
: path.resolve(root, view);
|
|
34
|
+
if (path.extname(viewPath) || engine === 'html') {
|
|
35
|
+
return viewPath;
|
|
36
|
+
}
|
|
37
|
+
return `${viewPath}.${engine}`;
|
|
38
|
+
};
|
|
39
|
+
const importRenderEngine = async (engine) => {
|
|
40
|
+
try {
|
|
41
|
+
const module = await import(engine);
|
|
42
|
+
return module.default ?? module;
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
if (err.code === 'ERR_MODULE_NOT_FOUND' || err.code === 'MODULE_NOT_FOUND') {
|
|
46
|
+
throw new Error(`Render engine "${engine}" is not installed`);
|
|
47
|
+
}
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const renderWithEngine = async (engine, viewPath, data, options) => {
|
|
52
|
+
if (engine === 'html') {
|
|
53
|
+
return renderTemplate(await readFile(viewPath, 'utf8'), data);
|
|
54
|
+
}
|
|
55
|
+
if (engine === 'ejs') {
|
|
56
|
+
const ejs = await importRenderEngine('ejs');
|
|
57
|
+
return ejs.renderFile(viewPath, data, options);
|
|
58
|
+
}
|
|
59
|
+
if (engine === 'pug') {
|
|
60
|
+
const pug = await importRenderEngine('pug');
|
|
61
|
+
return pug.renderFile(viewPath, { ...options, ...data });
|
|
62
|
+
}
|
|
63
|
+
throw new Error(`Unsupported render engine "${engine}"`);
|
|
64
|
+
};
|
|
65
|
+
class Context {
|
|
66
|
+
req;
|
|
67
|
+
res;
|
|
68
|
+
// Assign req and res in the constructor
|
|
69
|
+
body;
|
|
70
|
+
state;
|
|
71
|
+
originalUrl;
|
|
72
|
+
baseUrl;
|
|
73
|
+
path;
|
|
74
|
+
params;
|
|
75
|
+
query;
|
|
76
|
+
searchParams;
|
|
77
|
+
renderOptions;
|
|
78
|
+
constructor(req, res, renderOptions = {}) {
|
|
79
|
+
const url = new URL(req.url ?? '', 'http://localhost');
|
|
80
|
+
this.req = req;
|
|
81
|
+
this.res = res;
|
|
82
|
+
this.body = {};
|
|
83
|
+
this.state = {};
|
|
84
|
+
this.originalUrl = req.url ?? '';
|
|
85
|
+
this.baseUrl = '';
|
|
86
|
+
this.path = url.pathname;
|
|
87
|
+
this.params = {};
|
|
88
|
+
this.query = Object.fromEntries(url.searchParams);
|
|
89
|
+
this.searchParams = url.searchParams;
|
|
90
|
+
this.renderOptions = renderOptions;
|
|
91
|
+
}
|
|
92
|
+
json(data) {
|
|
93
|
+
this.res.setHeader('Content-Type', 'application/json');
|
|
94
|
+
this.res.end(JSON.stringify(data));
|
|
95
|
+
}
|
|
96
|
+
send(data) {
|
|
97
|
+
this.res.setHeader('Content-Type', 'text/plain');
|
|
98
|
+
this.res.end(data);
|
|
99
|
+
}
|
|
100
|
+
async render(view, data = {}, options = {}) {
|
|
101
|
+
const renderOptions = { ...this.renderOptions, ...options };
|
|
102
|
+
const engine = getViewEngine(view, renderOptions);
|
|
103
|
+
const viewPath = resolveViewPath(view, engine, renderOptions);
|
|
104
|
+
const html = await renderWithEngine(engine, viewPath, data, renderOptions);
|
|
105
|
+
this.res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
106
|
+
this.res.end(html);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export { Context };
|
|
110
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/lib/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,IAAI,MAAM,MAAM,CAAA;AAUvB,MAAM,QAAQ,GAAG,CAAC,IAAc,EAAE,GAAW,EAAE,EAAE;IAC/C,OAAO,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAClD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;YAClB,OAAO,SAAS,CAAA;QAClB,CAAC;QAED,OAAO,KAAK,CAAC,WAAW,CAAC,CAAA;IAC3B,CAAC,EAAE,IAAI,CAAC,CAAA;AACV,CAAC,CAAA;AAED,MAAM,UAAU,GAAG,CAAC,KAAc,EAAE,EAAE;IACpC,OAAO,MAAM,CAAC,KAAK,CAAC;SACjB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC;SACtB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;SACrB,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;SACvB,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AAC3B,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,CAAC,QAAgB,EAAE,IAAc,EAAE,EAAE;IAC1D,OAAO,QAAQ,CAAC,OAAO,CAAC,yBAAyB,EAAE,CAAC,CAAC,EAAE,GA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AG,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,GAAG,OAAO,EAAE,CAAA;QAC3D,MAAM,MAAM,GAAG,aAAa,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;QACjD,MAAM,QAAQ,GAAG,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,CAAC,CAAA;QAC7D,MAAM,IAAI,GAAG,MAAM,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE,aAAa,CAAC,CAAA;QAE1E,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,0BAA0B,CAAC,CAAA;QAC9D,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;CACF;AAED,OAAO,EACL,OAAO,EACR,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose.d.ts","sourceRoot":"","sources":["../../../src/lib/router/compose.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,OAAO,GAAI,UAAU,GAAG,EAAE,MAChB,KAAK,GAAG,EAAE,MAAM,MAAM,OAAO,CAAC,IAAI,CAAC,kBAuBlD,CAAA;AAED,OAAO,EACL,OAAO,EACR,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const compose = (handlers) => {
|
|
2
|
+
return async (ctx, next) => {
|
|
3
|
+
let index = -1;
|
|
4
|
+
const dispatch = async (handlerIndex) => {
|
|
5
|
+
if (handlerIndex <= index) {
|
|
6
|
+
throw new Error('next() called multiple times');
|
|
7
|
+
}
|
|
8
|
+
index = handlerIndex;
|
|
9
|
+
const handler = handlerIndex === handlers.length
|
|
10
|
+
? next
|
|
11
|
+
: handlers[handlerIndex];
|
|
12
|
+
if (!handler) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
await handler(ctx, () => dispatch(handlerIndex + 1));
|
|
16
|
+
};
|
|
17
|
+
await dispatch(0);
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export { compose };
|
|
21
|
+
//# sourceMappingURL=compose.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compose.js","sourceRoot":"","sources":["../../../src/lib/router/compose.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG,CAAC,QAAe,EAAE,EAAE;IAClC,OAAO,KAAK,EAAE,GAAQ,EAAE,IAAyB,EAAE,EAAE;QACnD,IAAI,KAAK,GAAG,CAAC,CAAC,CAAA;QAEd,MAAM,QAAQ,GAAG,KAAK,EAAE,YAAoB,EAAiB,EAAE;YAC7D,IAAI,YAAY,IAAI,KAAK,EAAE,CAAC;gBAC1B,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;YACjD,CAAC;YAED,KAAK,GAAG,YAAY,CAAA;YAEpB,MAAM,OAAO,GAAG,YAAY,KAAK,QAAQ,CAAC,MAAM;gBAC9C,CAAC,CAAC,IAAI;gBACN,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAA;YAE1B,IAAI,CAAC,OAAO,EAAE,CAAC;gBACb,OAAM;YACR,CAAC;YAED,MAAM,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC,CAAA;QACtD,CAAC,CAAA;QAED,MAAM,QAAQ,CAAC,CAAC,CAAC,CAAA;IACnB,CAAC,CAAA;AACH,CAAC,CAAA;AAED,OAAO,EACL,OAAO,EACR,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,EACL,MAAM,EACP,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/lib/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,EACL,MAAM,EACP,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Router } from './router.js';
|
|
2
|
+
type MatchResult = {
|
|
3
|
+
matched: boolean;
|
|
4
|
+
consumedPath: string;
|
|
5
|
+
params: Record<string, string>;
|
|
6
|
+
};
|
|
7
|
+
declare const normalizePath: (path?: string | RegExp) => string | RegExp;
|
|
8
|
+
declare class Layer {
|
|
9
|
+
path: string | RegExp;
|
|
10
|
+
method: string | null;
|
|
11
|
+
handlers: any[];
|
|
12
|
+
router: Router | null;
|
|
13
|
+
isRoute: boolean;
|
|
14
|
+
constructor({ path, method, handlers, router, isRoute }: {
|
|
15
|
+
path?: string | RegExp;
|
|
16
|
+
method?: string | null;
|
|
17
|
+
handlers?: any[];
|
|
18
|
+
router?: Router | null;
|
|
19
|
+
isRoute?: boolean;
|
|
20
|
+
});
|
|
21
|
+
matches(pathname: string, method: string | undefined): MatchResult | null;
|
|
22
|
+
}
|
|
23
|
+
export { Layer, normalizePath };
|
|
24
|
+
//# sourceMappingURL=layer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer.d.ts","sourceRoot":"","sources":["../../../src/lib/router/layer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEzC,KAAK,WAAW,GAAG;IACjB,OAAO,EAAE,OAAO,CAAA;IAChB,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC/B,CAAA;AAUD,QAAA,MAAM,aAAa,GAAI,OAAM,MAAM,GAAG,MAAY,oBAWjD,CAAA;AAoED,cAAM,KAAK;IACT,IAAI,EAAE,MAAM,GAAG,MAAM,CAAA;IACrB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,QAAQ,EAAE,GAAG,EAAE,CAAA;IACf,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;IACrB,OAAO,EAAE,OAAO,CAAA;gBAEJ,EACV,IAAU,EACV,MAAa,EACb,QAAa,EACb,MAAa,EACb,OAAe,EAChB,EAAE;QACD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QACvB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACvB,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC;QACjB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACvB,OAAO,CAAC,EAAE,OAAO,CAAA;KAClB;IAQD,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,WAAW,GAAG,IAAI;CAgF1E;AAED,OAAO,EACL,KAAK,EACL,aAAa,EACd,CAAA"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
const trimTrailingSlash = (value) => {
|
|
2
|
+
if (value.length > 1 && value.endsWith('/')) {
|
|
3
|
+
return value.slice(0, -1);
|
|
4
|
+
}
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
const normalizePath = (path = '/') => {
|
|
8
|
+
if (path instanceof RegExp || path === '*') {
|
|
9
|
+
return path;
|
|
10
|
+
}
|
|
11
|
+
if (!path || path === '/') {
|
|
12
|
+
return '/';
|
|
13
|
+
}
|
|
14
|
+
const normalized = path.startsWith('/') ? path : `/${path}`;
|
|
15
|
+
return trimTrailingSlash(normalized);
|
|
16
|
+
};
|
|
17
|
+
const execRegExp = (pattern, pathname) => {
|
|
18
|
+
pattern.lastIndex = 0;
|
|
19
|
+
return pattern.exec(pathname);
|
|
20
|
+
};
|
|
21
|
+
const splitPath = (path) => {
|
|
22
|
+
return path.split('/').filter(Boolean);
|
|
23
|
+
};
|
|
24
|
+
const safeDecode = (value) => {
|
|
25
|
+
try {
|
|
26
|
+
return decodeURIComponent(value);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const matchParameterizedPath = (routePath, pathname, isRoute) => {
|
|
33
|
+
if (!routePath.includes(':')) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const routeSegments = splitPath(routePath);
|
|
37
|
+
const pathSegments = splitPath(trimTrailingSlash(pathname));
|
|
38
|
+
if (isRoute && routeSegments.length !== pathSegments.length) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
if (!isRoute && routeSegments.length > pathSegments.length) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const params = {};
|
|
45
|
+
for (let index = 0; index < routeSegments.length; index++) {
|
|
46
|
+
const routeSegment = routeSegments[index];
|
|
47
|
+
const pathSegment = pathSegments[index];
|
|
48
|
+
if (!pathSegment) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
if (routeSegment.startsWith(':')) {
|
|
52
|
+
const paramName = routeSegment.slice(1);
|
|
53
|
+
if (!paramName) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
params[paramName] = safeDecode(pathSegment);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (routeSegment !== pathSegment) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
matched: true,
|
|
65
|
+
consumedPath: `/${pathSegments.slice(0, routeSegments.length).join('/')}`,
|
|
66
|
+
params
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
class Layer {
|
|
70
|
+
path;
|
|
71
|
+
method;
|
|
72
|
+
handlers;
|
|
73
|
+
router;
|
|
74
|
+
isRoute;
|
|
75
|
+
constructor({ path = '/', method = null, handlers = [], router = null, isRoute = false }) {
|
|
76
|
+
this.path = normalizePath(path);
|
|
77
|
+
this.method = method;
|
|
78
|
+
this.handlers = handlers;
|
|
79
|
+
this.router = router;
|
|
80
|
+
this.isRoute = isRoute;
|
|
81
|
+
}
|
|
82
|
+
matches(pathname, method) {
|
|
83
|
+
if (this.isRoute) {
|
|
84
|
+
const methodMatches = this.method === null || this.method === method;
|
|
85
|
+
if (!methodMatches) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
if (this.path === '*') {
|
|
89
|
+
return {
|
|
90
|
+
matched: true,
|
|
91
|
+
consumedPath: pathname,
|
|
92
|
+
params: {}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (this.path instanceof RegExp) {
|
|
96
|
+
const match = execRegExp(this.path, pathname);
|
|
97
|
+
return match
|
|
98
|
+
? {
|
|
99
|
+
matched: true,
|
|
100
|
+
consumedPath: match[0],
|
|
101
|
+
params: {}
|
|
102
|
+
}
|
|
103
|
+
: null;
|
|
104
|
+
}
|
|
105
|
+
const parameterizedMatch = matchParameterizedPath(this.path, pathname, true);
|
|
106
|
+
if (parameterizedMatch) {
|
|
107
|
+
return parameterizedMatch;
|
|
108
|
+
}
|
|
109
|
+
return this.path === pathname
|
|
110
|
+
? {
|
|
111
|
+
matched: true,
|
|
112
|
+
consumedPath: this.path,
|
|
113
|
+
params: {}
|
|
114
|
+
}
|
|
115
|
+
: null;
|
|
116
|
+
}
|
|
117
|
+
if (this.path === '/' || this.path === '*') {
|
|
118
|
+
return {
|
|
119
|
+
matched: true,
|
|
120
|
+
consumedPath: '',
|
|
121
|
+
params: {}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (this.path instanceof RegExp) {
|
|
125
|
+
const match = execRegExp(this.path, pathname);
|
|
126
|
+
if (!match || match.index !== 0) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
matched: true,
|
|
131
|
+
consumedPath: trimTrailingSlash(match[0]),
|
|
132
|
+
params: {}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const parameterizedMatch = matchParameterizedPath(this.path, pathname, false);
|
|
136
|
+
if (parameterizedMatch) {
|
|
137
|
+
return parameterizedMatch;
|
|
138
|
+
}
|
|
139
|
+
if (pathname === this.path || pathname.startsWith(`${this.path}/`)) {
|
|
140
|
+
return {
|
|
141
|
+
matched: true,
|
|
142
|
+
consumedPath: this.path,
|
|
143
|
+
params: {}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
export { Layer, normalizePath };
|
|
150
|
+
//# sourceMappingURL=layer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layer.js","sourceRoot":"","sources":["../../../src/lib/router/layer.ts"],"names":[],"mappings":"AAQA,MAAM,iBAAiB,GAAG,CAAC,KAAa,EAAE,EAAE;IAC1C,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC5C,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAC3B,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,aAAa,GAAG,CAAC,OAAwB,GAAG,EAAE,EAAE;IACpD,IAAI,IAAI,YAAY,MAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;QAC3C,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IAAI,CAAC,IAAI,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;QAC1B,OAAO,GAAG,CAAA;IACZ,CAAC;IAED,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAA;IAC3D,OAAO,iBAAiB,CAAC,UAAU,CAAC,CAAA;AACtC,CAAC,CAAA;AAED,MAAM,UAAU,GAAG,CAAC,OAAe,EAAE,QAAgB,EAAE,EAAE;IACvD,OAAO,CAAC,SAAS,GAAG,CAAC,CAAA;IACrB,OAAO,OAAO,CAAC,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AAI,EAAE,QAAQ,CAAC,CAAA;YAE7C,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,KAAK,CAAC,EAAE,CAAC;gBAChC,OAAO,IAAI,CAAA;YACb,CAAC;YAED,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,YAAY,EAAE,iBAAiB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACzC,MAAM,EAAE,EAAE;aACX,CAAA;QACH,CAAC;QAED,MAAM,kBAAkB,GAAG,sBAAsB,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAA;QAE7E,IAAI,kBAAkB,EAAE,CAAC;YACvB,OAAO,kBAAkB,CAAA;QAC3B,CAAC;QAED,IAAI,QAAQ,KAAK,IAAI,CAAC,IAAI,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;YACnE,OAAO;gBACL,OAAO,EAAE,IAAI;gBACb,YAAY,EAAE,IAAI,CAAC,IAAI;gBACvB,MAAM,EAAE,EAAE;aACX,CAAA;QACH,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AAED,OAAO,EACL,KAAK,EACL,aAAa,EACd,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Context } from '../../types/core.js';
|
|
2
|
+
import { Layer } from './layer.js';
|
|
3
|
+
declare class Router {
|
|
4
|
+
#private;
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
stack: Layer[];
|
|
7
|
+
constructor();
|
|
8
|
+
use(pathOrHandler: string | RegExp | Router, ...handlers: any[]): this;
|
|
9
|
+
all(path: string | RegExp, ...handlers: any[]): this;
|
|
10
|
+
handle(ctx: Context, next?: () => Promise<void>): Promise<void>;
|
|
11
|
+
register(method: string | null, path: string | RegExp, ...handlers: any[]): this;
|
|
12
|
+
}
|
|
13
|
+
export { Router };
|
|
14
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../../src/lib/router/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAE7C,OAAO,EAAE,KAAK,EAAiB,MAAM,YAAY,CAAA;AAIjD,cAAM,MAAM;;IACV,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;IAClB,KAAK,EAAE,KAAK,EAAE,CAAA;;IAMd,GAAG,CAAC,aAAa,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,GAAG,QAAQ,EAAE,GAAG,EAAE;IAqB/D,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,QAAQ,EAAE,GAAG,EAAE;IAI7C,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAI/C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,QAAQ,EAAE,GAAG,EAAE;CAiH1E;AAQD,OAAO,EACL,MAAM,EACP,CAAA"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { compose } from './compose.js';
|
|
2
|
+
import { Layer, normalizePath } from './layer.js';
|
|
3
|
+
const METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'];
|
|
4
|
+
class Router {
|
|
5
|
+
stack;
|
|
6
|
+
constructor() {
|
|
7
|
+
this.stack = [];
|
|
8
|
+
}
|
|
9
|
+
use(pathOrHandler, ...handlers) {
|
|
10
|
+
const { path, items } = this.#resolveUseArguments(pathOrHandler, handlers);
|
|
11
|
+
for (const item of items) {
|
|
12
|
+
if (item instanceof Router) {
|
|
13
|
+
this.stack.push(new Layer({
|
|
14
|
+
path: path,
|
|
15
|
+
router: item
|
|
16
|
+
}));
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
this.stack.push(new Layer({
|
|
20
|
+
path,
|
|
21
|
+
handlers: [item]
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
all(path, ...handlers) {
|
|
27
|
+
return this.register(null, path, ...handlers);
|
|
28
|
+
}
|
|
29
|
+
handle(ctx, next) {
|
|
30
|
+
return this.#dispatch(ctx, next);
|
|
31
|
+
}
|
|
32
|
+
register(method, path, ...handlers) {
|
|
33
|
+
const normalizedPath = normalizePath(path);
|
|
34
|
+
const flatHandlers = handlers.flat();
|
|
35
|
+
if (flatHandlers.length === 0) {
|
|
36
|
+
const printablePath = normalizedPath instanceof RegExp
|
|
37
|
+
? normalizedPath.toString()
|
|
38
|
+
: normalizedPath;
|
|
39
|
+
throw new Error(`No handlers provided for ${method ?? 'ALL'} ${printablePath}`);
|
|
40
|
+
}
|
|
41
|
+
this.stack.push(new Layer({
|
|
42
|
+
path: normalizedPath,
|
|
43
|
+
method,
|
|
44
|
+
handlers: flatHandlers,
|
|
45
|
+
isRoute: true
|
|
46
|
+
}));
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
async #dispatch(ctx, next) {
|
|
50
|
+
const pathname = ctx.path;
|
|
51
|
+
const method = ctx.req.method;
|
|
52
|
+
let index = 0;
|
|
53
|
+
const dispatch = async () => {
|
|
54
|
+
while (index < this.stack.length) {
|
|
55
|
+
const layer = this.stack[index++];
|
|
56
|
+
const match = layer.matches(pathname, method);
|
|
57
|
+
if (!match) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (layer.router) {
|
|
61
|
+
const matchedBase = match.consumedPath || '';
|
|
62
|
+
const previousBaseUrl = ctx.baseUrl;
|
|
63
|
+
const previousPath = ctx.path;
|
|
64
|
+
const previousParams = ctx.params;
|
|
65
|
+
ctx.baseUrl = `${ctx.baseUrl}${matchedBase}` || '/';
|
|
66
|
+
ctx.path = pathname.slice(matchedBase.length) || '/';
|
|
67
|
+
ctx.params = { ...ctx.params, ...match.params };
|
|
68
|
+
try {
|
|
69
|
+
let routerMatched = false;
|
|
70
|
+
await layer.router.handle(ctx, async () => {
|
|
71
|
+
routerMatched = true;
|
|
72
|
+
ctx.baseUrl = previousBaseUrl;
|
|
73
|
+
ctx.path = previousPath;
|
|
74
|
+
ctx.params = previousParams;
|
|
75
|
+
await dispatch();
|
|
76
|
+
});
|
|
77
|
+
if (!routerMatched) {
|
|
78
|
+
ctx.baseUrl = previousBaseUrl;
|
|
79
|
+
ctx.path = previousPath;
|
|
80
|
+
ctx.params = previousParams;
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
ctx.baseUrl = previousBaseUrl;
|
|
87
|
+
ctx.path = previousPath;
|
|
88
|
+
ctx.params = previousParams;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const fn = compose(layer.handlers);
|
|
92
|
+
let finished = false;
|
|
93
|
+
const previousParams = ctx.params;
|
|
94
|
+
ctx.params = { ...ctx.params, ...match.params };
|
|
95
|
+
try {
|
|
96
|
+
await fn(ctx, async () => {
|
|
97
|
+
finished = true;
|
|
98
|
+
await dispatch();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
ctx.params = previousParams;
|
|
103
|
+
}
|
|
104
|
+
if (!finished || ctx.res.writableEnded) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (next) {
|
|
109
|
+
await next();
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
await dispatch();
|
|
113
|
+
}
|
|
114
|
+
#resolveUseArguments(pathOrHandler, handlers) {
|
|
115
|
+
if (typeof pathOrHandler === 'string' || pathOrHandler instanceof RegExp) {
|
|
116
|
+
return {
|
|
117
|
+
path: normalizePath(pathOrHandler),
|
|
118
|
+
items: handlers.flat()
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
path: '/',
|
|
123
|
+
items: [pathOrHandler, ...handlers].flat()
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
for (const method of METHODS) {
|
|
128
|
+
Router.prototype[method.toLowerCase()] = function register(path, ...handlers) {
|
|
129
|
+
return this.register(method, path, ...handlers);
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export { Router };
|
|
133
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../../src/lib/router/router.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAEjD,MAAM,OAAO,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAA;AAE5E,MAAM,MAAM;IAEV,KAAK,CAAS;IAEd;QACE,IAAI,CAAC,KAAK,GAAG,EAAE,CAAA;IACjB,CAAC;IAED,GAAG,CAAC,aAAuC,EAAE,GAAG,QAAe;QAC7D,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,oBAAoB,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAA;QAE1E,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,IAAI,YAAY,MAAM,EAAE,CAAC;gBAC3B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC;oBACxB,IAAI,EAAE,IAAc;oBACpB,MAAM,EAAE,IAAI;iBACb,CAAC,CAAC,CAAA;gBACH,SAAQ;YACV,CAAC;YAED,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC;gBACxB,IAAI;gBACJ,QAAQ,EAA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aAAa,CAAC,aAAa,CAAC;gBAClC,KAAK,EAAE,QAAQ,CAAC,IAAI,EAAE;aACvB,CAAA;QACH,CAAC;QAED,OAAO;YACL,IAAI,EAAE,GAAG;YACT,KAAK,EAAE,CAAC,aAAa,EAAE,GAAG,QAAQ,CAAC,CAAC,IAAI,EAAE;SAC3C,CAAA;IACH,CAAC;CACF;AAED,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;IAC7B,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,GAAG,SAAS,QAAQ,CAAC,IAAqB,EAAE,GAAG,QAAe;QAClG,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,CAAC,CAAA;IACjD,CAAC,CAAA;AACH,CAAC;AAED,OAAO,EACL,MAAM,EACP,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
interface Context {
|
|
3
|
+
req: IncomingMessage;
|
|
4
|
+
res: ServerResponse;
|
|
5
|
+
body?: Record<string, any>;
|
|
6
|
+
state?: Record<string, any>;
|
|
7
|
+
originalUrl: string;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
path: string;
|
|
10
|
+
params: Record<string, string>;
|
|
11
|
+
query: Record<string, string>;
|
|
12
|
+
searchParams: URLSearchParams;
|
|
13
|
+
json(data: Record<string, any>): void;
|
|
14
|
+
send(data: string): void;
|
|
15
|
+
render(view: string, data?: Record<string, any>, options?: Record<string, any>): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export { Context };
|
|
18
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/types/core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAEtD,UAAU,OAAO;IACf,GAAG,EAAE,eAAe,CAAA;IACpB,GAAG,EAAE,cAAc,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC3B,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B,YAAY,EAAE,eAAe,CAAA;IAE7B,IAAI,CAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;IACtC,IAAI,CAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,MAAM,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAChG;AAED,OAAO,EAAE,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../src/types/core.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vernali/core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Core functionality for Vernali framework. POC for now, will be expanded in the future.",
|
|
5
|
+
"homepage": "https://github.com/YaelDJ/vernali-core.git",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/YaelDJ/vernali-core.git",
|
|
8
|
+
"email": "dejesusyael1987@gmail.com"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/YaelDJ/vernali-core.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"author": "Yael De Jesus",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./router": {
|
|
23
|
+
"types": "./dist/lib/router/index.d.ts",
|
|
24
|
+
"import": "./dist/lib/router/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "nodemon",
|
|
34
|
+
"clean": "rm -rf dist/",
|
|
35
|
+
"build": "npm run clean && tsc --project tsconfig.json",
|
|
36
|
+
"prepare": "npm run build",
|
|
37
|
+
"type-check": "tsc --noEmit",
|
|
38
|
+
"lint": "standard --parser @typescript-eslint/parser \"src/**/*.{js,ts}\"",
|
|
39
|
+
"lint:fix": "standard --parser @typescript-eslint/parser \"src/**/*.{js,ts}\" --fix"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^25.5.0",
|
|
43
|
+
"nodemon": "^3.1.14",
|
|
44
|
+
"ts-node": "^10.9.2",
|
|
45
|
+
"typescript": "^6.0.2"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"ejs": "^3.1.10",
|
|
49
|
+
"pug": "^3.0.3"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"ejs": {
|
|
53
|
+
"optional": true
|
|
54
|
+
},
|
|
55
|
+
"pug": {
|
|
56
|
+
"optional": true
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|