routup 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -8
- package/dist/dispatcher/adapters/index.d.ts +3 -0
- package/dist/dispatcher/adapters/node/index.d.ts +1 -0
- package/dist/dispatcher/adapters/node/module.d.ts +6 -0
- package/dist/dispatcher/adapters/raw/module.d.ts +4 -0
- package/dist/dispatcher/adapters/raw/type.d.ts +18 -0
- package/dist/dispatcher/adapters/web/index.d.ts +2 -0
- package/dist/dispatcher/adapters/web/module.d.ts +5 -0
- package/dist/dispatcher/adapters/web/type.d.ts +3 -0
- package/dist/dispatcher/index.d.ts +3 -0
- package/dist/dispatcher/type.d.ts +30 -0
- package/dist/dispatcher/utils.d.ts +4 -0
- package/dist/error.d.ts +1 -0
- package/dist/index.cjs +1075 -538
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.mjs +1054 -509
- package/dist/index.mjs.map +1 -1
- package/dist/layer/module.d.ts +5 -4
- package/dist/layer/type.d.ts +1 -2
- package/dist/layer/utils.d.ts +1 -1
- package/dist/path/index.d.ts +1 -0
- package/dist/path/matcher.d.ts +1 -2
- package/dist/path/type.d.ts +1 -0
- package/dist/path/utils.d.ts +2 -0
- package/dist/{helpers/request → request/helpers}/body.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/cache.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/cookie.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/env.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/header-accept-charset.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/header-accept-language.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/header-accept.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/header-content-type.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/header.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/hostname.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/index.d.ts +1 -0
- package/dist/{helpers/request → request/helpers}/ip.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/mount-path.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/negotiator.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/params.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/path.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/protocol.d.ts +1 -1
- package/dist/{helpers/request → request/helpers}/query.d.ts +1 -1
- package/dist/request/helpers/router.d.ts +3 -0
- package/dist/request/index.d.ts +3 -0
- package/dist/request/module.d.ts +4 -0
- package/dist/request/types.d.ts +9 -0
- package/dist/{helpers/response → response/helpers}/cache.d.ts +1 -1
- package/dist/response/helpers/gone.d.ts +2 -0
- package/dist/{helpers/response → response/helpers}/header-attachment.d.ts +1 -1
- package/dist/{helpers/response → response/helpers}/header-content-type.d.ts +1 -1
- package/dist/{helpers/response → response/helpers}/header.d.ts +1 -1
- package/dist/{helpers/response → response/helpers}/index.d.ts +3 -0
- package/dist/{helpers/response → response/helpers}/send-accepted.d.ts +2 -2
- package/dist/{helpers/response → response/helpers}/send-created.d.ts +2 -2
- package/dist/response/helpers/send-file.d.ts +17 -0
- package/dist/{helpers/response → response/helpers}/send-format.d.ts +1 -1
- package/dist/response/helpers/send-redirect.d.ts +2 -0
- package/dist/response/helpers/send-stream.d.ts +2 -0
- package/dist/response/helpers/send-web-blob.d.ts +2 -0
- package/dist/response/helpers/send-web-response.d.ts +2 -0
- package/dist/response/helpers/send.d.ts +2 -0
- package/dist/response/helpers/utils.d.ts +2 -0
- package/dist/response/index.d.ts +2 -0
- package/dist/response/module.d.ts +2 -0
- package/dist/route/module.d.ts +6 -5
- package/dist/route/type.d.ts +1 -1
- package/dist/route/utils.d.ts +1 -1
- package/dist/router/index.d.ts +0 -1
- package/dist/router/module.d.ts +13 -32
- package/dist/router/utils.d.ts +1 -0
- package/dist/router-options/index.d.ts +2 -0
- package/dist/router-options/module.d.ts +4 -0
- package/dist/router-options/transform.d.ts +2 -0
- package/dist/router-options/type.d.ts +50 -0
- package/dist/types.d.ts +19 -0
- package/dist/utils/cookie.d.ts +1 -0
- package/dist/utils/etag/module.d.ts +4 -3
- package/dist/utils/etag/type.d.ts +1 -1
- package/dist/utils/header.d.ts +3 -0
- package/dist/utils/index.d.ts +4 -1
- package/dist/utils/path.d.ts +5 -2
- package/dist/utils/stream.d.ts +8 -0
- package/dist/utils/web.d.ts +3 -0
- package/package.json +17 -16
- package/dist/config/module.d.ts +0 -8
- package/dist/config/type.d.ts +0 -34
- package/dist/handler/index.d.ts +0 -1
- package/dist/handler/utils.d.ts +0 -2
- package/dist/helpers/index.d.ts +0 -2
- package/dist/helpers/response/send-file.d.ts +0 -9
- package/dist/helpers/response/send-redirect.d.ts +0 -2
- package/dist/helpers/response/send-stream.d.ts +0 -4
- package/dist/helpers/response/send.d.ts +0 -2
- package/dist/helpers/response/utils.d.ts +0 -3
- package/dist/router/type.d.ts +0 -24
- package/dist/type.d.ts +0 -24
- package/dist/utils/request.d.ts +0 -2
- /package/dist/{config → dispatcher/adapters/raw}/index.d.ts +0 -0
- /package/dist/{helpers/request → request/helpers}/header-accept-encoding.d.ts +0 -0
package/README.md
CHANGED
|
@@ -12,11 +12,15 @@
|
|
|
12
12
|
[](https://snyk.io/test/github/Tada5hi/routup)
|
|
13
13
|
[](https://conventionalcommits.org)
|
|
14
14
|
|
|
15
|
-
**Routup** is a lightweight,
|
|
15
|
+
**Routup** is a lightweight, runtime agnostic and extendable routing library.
|
|
16
16
|
It uses node's vanilla request and response interfaces, which are injected into route handlers aka middlewares as function argument.
|
|
17
|
-
|
|
18
17
|
Helpers provide additional functionalities to transform and interact with the request and manipulate the response upstream.
|
|
19
18
|
|
|
19
|
+
Use the same routing framework for each project, regardless of the used runtime environment (Node.Js, Bun, ... ) 🎉.
|
|
20
|
+
Besides, it is even **228%** faster than Express ([more](#benchmarks)).
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
20
24
|
**Table of Contents**
|
|
21
25
|
|
|
22
26
|
- [Installation](#installation)
|
|
@@ -24,6 +28,7 @@ Helpers provide additional functionalities to transform and interact with the re
|
|
|
24
28
|
- [Documentation](#documentation)
|
|
25
29
|
- [Usage](#usage)
|
|
26
30
|
- [Plugins](#plugins)
|
|
31
|
+
- [Benchmarks](#benchmarks)
|
|
27
32
|
- [Contributing](#contributing)
|
|
28
33
|
- [License](#license)
|
|
29
34
|
|
|
@@ -35,12 +40,12 @@ npm install routup --save
|
|
|
35
40
|
|
|
36
41
|
## Features
|
|
37
42
|
|
|
38
|
-
- 🚀
|
|
43
|
+
- 🚀 runtime agnostic (Node.JS, Bun, Deno, ...)
|
|
39
44
|
- 🧰 response & request composables/helpers
|
|
40
45
|
- 💼 extendable & compact
|
|
41
46
|
- 🛫 named route parameters
|
|
42
47
|
- 📁 nestable routers
|
|
43
|
-
- 🤝️ define one or many (error-) middlewares
|
|
48
|
+
- 🤝️ define one or many (error-) middlewares (inc. express middlewares)
|
|
44
49
|
- ✨ promise support for route- & middleware-handlers
|
|
45
50
|
- 👕 TypeScript fully supported
|
|
46
51
|
- 🤏 Minimalistic to fit into any solution with minimum overhead
|
|
@@ -52,16 +57,75 @@ To read the docs, visit [https://routup.net](https://routup.net)
|
|
|
52
57
|
|
|
53
58
|
## Usage
|
|
54
59
|
|
|
60
|
+
**`NodeJs`**
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import {createServer} from 'node:http';
|
|
64
|
+
import {
|
|
65
|
+
createNodeDispatcher,
|
|
66
|
+
Router,
|
|
67
|
+
send
|
|
68
|
+
} from 'routup';
|
|
69
|
+
|
|
70
|
+
const router = new Router();
|
|
71
|
+
|
|
72
|
+
router.get('/', () => 'Hello World');
|
|
73
|
+
|
|
74
|
+
const server = createServer(createNodeDispatcher(router));
|
|
75
|
+
server.listen(3000)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**`Bun`**
|
|
79
|
+
|
|
55
80
|
```typescript
|
|
56
|
-
import {
|
|
81
|
+
import {
|
|
82
|
+
createWebDispatcher,
|
|
83
|
+
Router,
|
|
84
|
+
send
|
|
85
|
+
} from 'routup';
|
|
57
86
|
|
|
58
87
|
const router = new Router();
|
|
59
88
|
|
|
60
|
-
router.get('/', (
|
|
61
|
-
|
|
89
|
+
router.get('/', () => 'Hello World');
|
|
90
|
+
|
|
91
|
+
const dispatch = createWebDispatcher(router);
|
|
92
|
+
|
|
93
|
+
Bun.serve({
|
|
94
|
+
async fetch(request) {
|
|
95
|
+
return dispatch(request);
|
|
96
|
+
},
|
|
97
|
+
port: 3000,
|
|
62
98
|
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**`Deno`**
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import {
|
|
105
|
+
createWebDispatcher,
|
|
106
|
+
Router,
|
|
107
|
+
send
|
|
108
|
+
} from 'routup';
|
|
63
109
|
|
|
64
|
-
router
|
|
110
|
+
const router = new Router();
|
|
111
|
+
|
|
112
|
+
router.get('/', () => 'Hello World');
|
|
113
|
+
|
|
114
|
+
const dispatch = createWebDispatcher(router);
|
|
115
|
+
|
|
116
|
+
const server = Deno.listen({
|
|
117
|
+
port: 3000
|
|
118
|
+
});
|
|
119
|
+
for await (const conn of server) {
|
|
120
|
+
const httpConn = Deno.serveHttp(conn);
|
|
121
|
+
|
|
122
|
+
for await (const requestEvent of httpConn) {
|
|
123
|
+
const response = await dispatch(
|
|
124
|
+
requestEvent.request
|
|
125
|
+
);
|
|
126
|
+
requestEvent.respondWith(response);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
65
129
|
```
|
|
66
130
|
|
|
67
131
|
## Plugins
|
|
@@ -81,6 +145,26 @@ typically http framework functions, which are not integrated in the main package
|
|
|
81
145
|
| [rate-limit-redis](https://www.npmjs.com/package/@routup/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
|
|
82
146
|
| [static](https://www.npmjs.com/package/@routup/static) | Serve static files from a directory. |
|
|
83
147
|
| [swagger](https://www.npmjs.com/package/@routup/swagger) | Serve generated docs from URL or based on a JSON file. |
|
|
148
|
+
|
|
149
|
+
## Benchmarks
|
|
150
|
+
|
|
151
|
+
* CPUs: `24`
|
|
152
|
+
* RAM: `63.9GB`
|
|
153
|
+
* Node: `v18.16.0`
|
|
154
|
+
* Date: `Wed Sep 13 2023 15:11:58 GMT+0200 (Mitteleuropäische Sommerzeit) `
|
|
155
|
+
* Method: `autocannon -c 100 -d 40 -p 10 localhost:3000` (two rounds; one to warm-up, one to measure)
|
|
156
|
+
|
|
157
|
+
| Package | Requests/s | Latency (ms) | Throughput/MB |
|
|
158
|
+
|:-----------|:-----------:|--------------:|----------------:|
|
|
159
|
+
| http | 61062 | 15.87 | 10.89 |
|
|
160
|
+
| fastify | 59679 | 16.26 | 10.70 |
|
|
161
|
+
| koa | 45763 | 21.35 | 8.16 |
|
|
162
|
+
| **routup** | 43881 | 22.29 | 8.87 |
|
|
163
|
+
| hapi | 41374 | 23.67 | 7.38 |
|
|
164
|
+
| express | 13376 | 74.18 | 2.39 |
|
|
165
|
+
|
|
166
|
+
Benchmarks were generated using autocannon.
|
|
167
|
+
To recreate the results, this can be done using the [benchmarks'](https://github.com/routup/benchmarks) repository.
|
|
84
168
|
## Contributing
|
|
85
169
|
|
|
86
170
|
Before starting to work on a pull request, it is important to review the guidelines for
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './module';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { RequestListener } from 'node:http';
|
|
3
|
+
import type { Router } from '../../../router';
|
|
4
|
+
import type { Request, Response } from '../../../types';
|
|
5
|
+
export declare function dispatchNodeRequest(router: Router, req: Request, res: Response): Promise<void>;
|
|
6
|
+
export declare function createNodeDispatcher(router: Router): RequestListener;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Router } from '../../../router';
|
|
2
|
+
import type { DispatchRawRequestOptions, RawRequest, RawResponse } from './type';
|
|
3
|
+
export declare function dispatchRawRequest(router: Router, request: RawRequest, options?: DispatchRawRequestOptions): Promise<RawResponse>;
|
|
4
|
+
export declare function createRawDispatcher(router: Router): (request: RawRequest) => Promise<RawResponse>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RequestBody, RequestHeaders } from '../../../request';
|
|
2
|
+
export type RawRequest = {
|
|
3
|
+
method: string;
|
|
4
|
+
path: string;
|
|
5
|
+
headers?: RequestHeaders;
|
|
6
|
+
body?: RequestBody;
|
|
7
|
+
};
|
|
8
|
+
export type RawResponseHeader = string | string[];
|
|
9
|
+
export type RawResponseHeaders = Record<string, RawResponseHeader>;
|
|
10
|
+
export type RawResponse = {
|
|
11
|
+
status: number;
|
|
12
|
+
statusMessage?: string;
|
|
13
|
+
headers: RawResponseHeaders;
|
|
14
|
+
body?: ArrayBuffer;
|
|
15
|
+
};
|
|
16
|
+
export type DispatchRawRequestOptions = {
|
|
17
|
+
throwOnError?: boolean;
|
|
18
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Router } from '../../../router';
|
|
2
|
+
import type { WebRequest } from '../../../types';
|
|
3
|
+
import type { DispatchWebRequestOptions } from './type';
|
|
4
|
+
export declare function dispatchWebRequest(router: Router, request: WebRequest, options?: DispatchWebRequestOptions): Promise<Response>;
|
|
5
|
+
export declare function createWebDispatcher(router: Router): (request: WebRequest) => Promise<Response>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Request, Response } from '../types';
|
|
2
|
+
export interface Dispatcher {
|
|
3
|
+
dispatch(event: DispatcherEvent, meta: DispatcherMeta): Promise<boolean>;
|
|
4
|
+
}
|
|
5
|
+
export type DispatcherMeta = {
|
|
6
|
+
/**
|
|
7
|
+
* Params collected on path.
|
|
8
|
+
*/
|
|
9
|
+
params?: Record<string, any>;
|
|
10
|
+
/**
|
|
11
|
+
* Path to check for the current instance.
|
|
12
|
+
*/
|
|
13
|
+
path?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The relative path on which the router is hung.
|
|
16
|
+
*/
|
|
17
|
+
mountPath?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The error which occurred during a previous handler.
|
|
20
|
+
*/
|
|
21
|
+
error?: Error;
|
|
22
|
+
/**
|
|
23
|
+
* Ids of chained router instances.
|
|
24
|
+
*/
|
|
25
|
+
routerIds?: number[];
|
|
26
|
+
};
|
|
27
|
+
export type DispatcherEvent = {
|
|
28
|
+
req: Request;
|
|
29
|
+
res: Response;
|
|
30
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DispatcherMeta } from './type';
|
|
2
|
+
export declare function cloneDispatcherMeta(input?: DispatcherMeta): DispatcherMeta;
|
|
3
|
+
export declare function cloneDispatcherMetaParams(input?: Record<string, any>): Record<string, any>;
|
|
4
|
+
export declare function mergeDispatcherMetaParams(t1?: Record<string, any>, t2?: Record<string, any>): Record<string, any>;
|
package/dist/error.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createError(input: Error | Record<string, any>): Error;
|