routup 3.0.0 → 3.1.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.
Files changed (62) hide show
  1. package/README.md +10 -8
  2. package/dist/adapters/node/module.d.ts +7 -0
  3. package/dist/{utils → adapters/raw}/header.d.ts +1 -1
  4. package/dist/{layer → adapters/raw}/index.d.ts +1 -1
  5. package/dist/adapters/raw/module.d.ts +4 -0
  6. package/dist/{dispatcher/adapters → adapters}/raw/type.d.ts +1 -1
  7. package/dist/adapters/web/module.d.ts +4 -0
  8. package/dist/constants.d.ts +7 -7
  9. package/dist/dispatcher/event/dispatch.d.ts +4 -0
  10. package/dist/dispatcher/event/error.d.ts +5 -0
  11. package/dist/dispatcher/event/index.d.ts +5 -0
  12. package/dist/dispatcher/event/is.d.ts +3 -0
  13. package/dist/dispatcher/event/module.d.ts +56 -0
  14. package/dist/dispatcher/event/types.d.ts +9 -0
  15. package/dist/dispatcher/index.d.ts +1 -2
  16. package/dist/dispatcher/type.d.ts +2 -30
  17. package/dist/error/create.d.ts +3 -3
  18. package/dist/error/is.d.ts +2 -2
  19. package/dist/error/module.d.ts +1 -1
  20. package/dist/handler/constants.d.ts +1 -0
  21. package/dist/handler/core/define.d.ts +4 -3
  22. package/dist/handler/core/types.d.ts +3 -3
  23. package/dist/handler/error/define.d.ts +4 -3
  24. package/dist/handler/error/types.d.ts +5 -5
  25. package/dist/handler/index.d.ts +1 -0
  26. package/dist/handler/is.d.ts +3 -1
  27. package/dist/handler/module.d.ts +23 -0
  28. package/dist/handler/types-base.d.ts +6 -2
  29. package/dist/handler/types.d.ts +3 -4
  30. package/dist/hook/constants.d.ts +8 -0
  31. package/dist/hook/index.d.ts +3 -0
  32. package/dist/hook/module.d.ts +19 -0
  33. package/dist/hook/types.d.ts +5 -0
  34. package/dist/index.cjs +629 -377
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.ts +1 -1
  37. package/dist/index.mjs +620 -370
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/response/helpers/send-web-blob.d.ts +1 -1
  40. package/dist/response/helpers/send-web-response.d.ts +1 -1
  41. package/dist/router/constants.d.ts +8 -0
  42. package/dist/router/index.d.ts +0 -1
  43. package/dist/router/module.d.ts +56 -24
  44. package/dist/router/types.d.ts +7 -0
  45. package/dist/types.d.ts +1 -0
  46. package/dist/utils/index.d.ts +2 -1
  47. package/dist/utils/method.d.ts +3 -0
  48. package/dist/utils/next.d.ts +2 -0
  49. package/package.json +6 -6
  50. package/dist/dispatcher/adapters/node/module.d.ts +0 -7
  51. package/dist/dispatcher/adapters/raw/module.d.ts +0 -4
  52. package/dist/dispatcher/adapters/web/index.d.ts +0 -2
  53. package/dist/dispatcher/adapters/web/module.d.ts +0 -5
  54. package/dist/dispatcher/utils.d.ts +0 -5
  55. package/dist/layer/constants.d.ts +0 -1
  56. package/dist/layer/module.d.ts +0 -17
  57. package/dist/layer/type.d.ts +0 -8
  58. package/dist/layer/utils.d.ts +0 -2
  59. /package/dist/{dispatcher/adapters → adapters}/index.d.ts +0 -0
  60. /package/dist/{dispatcher/adapters → adapters}/node/index.d.ts +0 -0
  61. /package/dist/{dispatcher/adapters/raw → adapters/web}/index.d.ts +0 -0
  62. /package/dist/{dispatcher/adapters → adapters}/web/type.d.ts +0 -0
@@ -1,3 +1,3 @@
1
1
  import type { WebBlob } from '../../types';
2
2
  import type { Response } from '../types';
3
- export declare function sendWebBlob(res: Response, blob: WebBlob): Promise<unknown>;
3
+ export declare function sendWebBlob(res: Response, blob: WebBlob): Promise<void>;
@@ -1,3 +1,3 @@
1
1
  import type { WebResponse } from '../../types';
2
2
  import type { Response } from '../types';
3
- export declare function sendWebResponse(res: Response, webResponse: WebResponse): Promise<unknown>;
3
+ export declare function sendWebResponse(res: Response, webResponse: WebResponse): Promise<void>;
@@ -1 +1,9 @@
1
1
  export declare const RouterSymbol: unique symbol;
2
+ export declare enum RouterPipelineStep {
3
+ START = 0,
4
+ LOOKUP = 1,
5
+ CHILD_BEFORE = 2,
6
+ CHILD_DISPATCH = 3,
7
+ CHILD_AFTER = 4,
8
+ FINISH = 5
9
+ }
@@ -1,2 +1 @@
1
1
  export * from './module';
2
- export { isRouterInstance } from './utils';
@@ -1,11 +1,14 @@
1
1
  import { MethodName } from '../constants';
2
- import type { Dispatcher, DispatcherEvent, DispatcherMeta } from '../dispatcher';
3
- import type { Handler } from '../handler';
4
- import { Layer } from '../layer';
2
+ import type { DispatchEvent, Dispatcher } from '../dispatcher';
3
+ import type { HandlerConfig } from '../handler';
4
+ import { Handler } from '../handler';
5
+ import type { HookDefaultListener, HookErrorListener, HookListener, HookUnsubscribeFn } from '../hook';
6
+ import { HookManager, HookName } from '../hook';
5
7
  import type { Path } from '../path';
6
8
  import { PathMatcher } from '../path';
7
- import type { RouterOptionsInput } from '../router-options';
8
9
  import type { Plugin, PluginInstallContext } from '../plugin';
10
+ import type { RouterOptionsInput } from '../router-options';
11
+ import type { RouterPipelineContext } from './types';
9
12
  export declare class Router implements Dispatcher {
10
13
  readonly '@instanceof': symbol;
11
14
  /**
@@ -21,37 +24,66 @@ export declare class Router implements Dispatcher {
21
24
  *
22
25
  * @protected
23
26
  */
24
- protected stack: (Router | Layer)[];
27
+ protected stack: (Router | Handler)[];
25
28
  /**
26
29
  * Path matcher for the current mount path.
27
30
  *
28
31
  * @protected
29
32
  */
30
33
  protected pathMatcher: PathMatcher | undefined;
34
+ /**
35
+ * A hook manager.
36
+ *
37
+ * @protected
38
+ */
39
+ protected hookManager: HookManager;
31
40
  constructor(options?: RouterOptionsInput);
32
- setPath(value?: Path): void;
33
41
  matchPath(path: string): boolean;
34
- dispatch(event: DispatcherEvent, meta: DispatcherMeta): Promise<boolean>;
35
- delete(...handlers: Handler[]): this;
36
- delete(path: Path, ...handlers: Handler[]): this;
37
- get(...handlers: Handler[]): this;
38
- get(path: Path, ...handlers: Handler[]): this;
39
- post(...handlers: Handler[]): this;
40
- post(path: Path, ...handlers: Handler[]): this;
41
- put(...handlers: Handler[]): this;
42
- put(path: Path, ...handlers: Handler[]): this;
43
- patch(...handlers: Handler[]): this;
44
- patch(path: Path, ...handlers: Handler[]): this;
45
- head(...handlers: Handler[]): this;
46
- head(path: Path, ...handlers: Handler[]): this;
47
- options(...handlers: Handler[]): this;
48
- options(path: Path, ...handlers: Handler[]): this;
49
- protected useForMethod(method: `${MethodName}`, ...input: (Path | Handler)[]): void;
42
+ setPath(value?: Path): void;
43
+ protected executePipelineStep(context: RouterPipelineContext): Promise<void>;
44
+ protected executePipelineStepStart(context: RouterPipelineContext): Promise<void>;
45
+ protected executePipelineStepLookup(context: RouterPipelineContext): Promise<void>;
46
+ protected executePipelineStepChildBefore(context: RouterPipelineContext): Promise<void>;
47
+ protected executePipelineStepChildAfter(context: RouterPipelineContext): Promise<void>;
48
+ protected executePipelineStepChildDispatch(context: RouterPipelineContext): Promise<void>;
49
+ protected executePipelineStepFinish(context: RouterPipelineContext): Promise<void>;
50
+ dispatch(event: DispatchEvent): Promise<void>;
51
+ delete(...handlers: (Handler | HandlerConfig)[]): this;
52
+ delete(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
53
+ get(...handlers: (Handler | HandlerConfig)[]): this;
54
+ get(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
55
+ post(...handlers: (Handler | HandlerConfig)[]): this;
56
+ post(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
57
+ put(...handlers: (Handler | HandlerConfig)[]): this;
58
+ put(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
59
+ patch(...handlers: (Handler | HandlerConfig)[]): this;
60
+ patch(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
61
+ head(...handlers: (Handler | HandlerConfig)[]): this;
62
+ head(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
63
+ options(...handlers: (Handler | HandlerConfig)[]): this;
64
+ options(path: Path, ...handlers: (Handler | HandlerConfig)[]): this;
65
+ protected useForMethod(method: MethodName, ...input: (Path | Handler | HandlerConfig)[]): void;
50
66
  use(router: Router): this;
51
- use(handler: Handler): this;
67
+ use(handler: Handler | HandlerConfig): this;
52
68
  use(plugin: Plugin): this;
53
69
  use(path: Path, router: Router): this;
54
- use(path: Path, handler: Handler): this;
70
+ use(path: Path, handler: Handler | HandlerConfig): this;
55
71
  use(path: Path, plugin: Plugin): this;
56
72
  protected install(plugin: Plugin, context?: PluginInstallContext): this;
73
+ /**
74
+ * Add a hook listener.
75
+ *
76
+ * @param name
77
+ * @param fn
78
+ */
79
+ on(name: `${HookName.DISPATCH_START}` | `${HookName.DISPATCH_END}` | `${HookName.CHILD_DISPATCH_BEFORE}` | `${HookName.CHILD_DISPATCH_AFTER}`, fn: HookDefaultListener): HookUnsubscribeFn;
80
+ on(name: `${HookName.CHILD_MATCH}`, fn: HookErrorListener): HookUnsubscribeFn;
81
+ on(name: `${HookName.ERROR}`, fn: HookErrorListener): HookUnsubscribeFn;
82
+ /**
83
+ * Remove single or all hook listeners.
84
+ *
85
+ * @param name
86
+ */
87
+ off(name: `${HookName}`): this;
88
+ off(name: `${HookName}`, fn: HookListener): this;
57
89
  }
@@ -0,0 +1,7 @@
1
+ import type { DispatchEvent } from '../dispatcher';
2
+ import type { RouterPipelineStep } from './constants';
3
+ export type RouterPipelineContext = {
4
+ step: RouterPipelineStep;
5
+ event: DispatchEvent;
6
+ stackIndex: number;
7
+ };
package/dist/types.d.ts CHANGED
@@ -8,3 +8,4 @@ export type WebReadableStream = globalThis.ReadableStream | ReadableStream;
8
8
  export type WebResponse = globalThis.Response;
9
9
  export type WebRequest = globalThis.Request;
10
10
  export type WebBlob = globalThis.Blob;
11
+ export type Next = (err?: Error) => void;
@@ -1,9 +1,10 @@
1
1
  export * from './cookie';
2
- export * from './header';
3
2
  export * from './etag';
4
3
  export * from './trust-proxy';
5
4
  export * from './is-instance';
6
5
  export * from './mime';
6
+ export * from './method';
7
+ export * from './next';
7
8
  export * from './object';
8
9
  export * from './path';
9
10
  export * from './promise';
@@ -0,0 +1,3 @@
1
+ import type { MethodName } from '../constants';
2
+ export declare function toMethodName(input: string | undefined): MethodName | undefined;
3
+ export declare function toMethodName(input: string | undefined, alt: MethodName): MethodName;
@@ -0,0 +1,2 @@
1
+ import type { Next } from '../types';
2
+ export declare const nextPlaceholder: Next;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "routup",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Routup is a minimalistic http based routing framework.",
5
5
  "exports": {
6
6
  "./package.json": "./package.json",
@@ -50,7 +50,7 @@
50
50
  },
51
51
  "homepage": "https://github.com/routup/routup#readme",
52
52
  "dependencies": {
53
- "@ebec/http": "^2.2.2",
53
+ "@ebec/http": "^2.3.0",
54
54
  "buffer": "^6.0.3",
55
55
  "mime-explorer": "^1.0.0",
56
56
  "negotiator": "^0.6.3",
@@ -61,9 +61,9 @@
61
61
  "uncrypto": "^0.1.3"
62
62
  },
63
63
  "devDependencies": {
64
- "@rollup/plugin-commonjs": "^25.0.4",
65
- "@rollup/plugin-node-resolve": "^15.2.1",
66
- "@swc/core": "^1.3.91",
64
+ "@rollup/plugin-commonjs": "^25.0.7",
65
+ "@rollup/plugin-node-resolve": "^15.2.3",
66
+ "@swc/core": "^1.3.93",
67
67
  "@swc/jest": "^0.2.29",
68
68
  "@tada5hi/commitlint-config": "^1.1.2",
69
69
  "@tada5hi/eslint-config-typescript": "^1.2.5",
@@ -78,7 +78,7 @@
78
78
  "cross-env": "^7.0.3",
79
79
  "jest": "^29.7.0",
80
80
  "rimraf": "^5.0.5",
81
- "rollup": "^3.29.4",
81
+ "rollup": "^4.1.4",
82
82
  "semantic-release": "^22.0.5",
83
83
  "supertest": "^6.3.3",
84
84
  "typescript": "5.2.2"
@@ -1,7 +0,0 @@
1
- /// <reference types="node" />
2
- import type { RequestListener } from 'node:http';
3
- import type { Request } from '../../../request';
4
- import type { Response } from '../../../response';
5
- import type { Router } from '../../../router';
6
- export declare function dispatchNodeRequest(router: Router, req: Request, res: Response): Promise<void>;
7
- export declare function createNodeDispatcher(router: Router): RequestListener;
@@ -1,4 +0,0 @@
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>;
@@ -1,2 +0,0 @@
1
- export * from './module';
2
- export * from './type';
@@ -1,5 +0,0 @@
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>;
@@ -1,5 +0,0 @@
1
- import type { DispatcherMeta } from './type';
2
- export declare function buildDispatcherMeta(input: Partial<DispatcherMeta>): DispatcherMeta;
3
- export declare function cloneDispatcherMeta(input: DispatcherMeta): DispatcherMeta;
4
- export declare function cloneDispatcherMetaParams(input?: Record<string, any>): Record<string, any>;
5
- export declare function mergeDispatcherMetaParams(t1?: Record<string, any>, t2?: Record<string, any>): Record<string, any>;
@@ -1 +0,0 @@
1
- export declare const LayerSymbol: unique symbol;
@@ -1,17 +0,0 @@
1
- import type { Dispatcher, DispatcherEvent, DispatcherMeta } from '../dispatcher';
2
- import type { Handler } from '../handler';
3
- import { PathMatcher } from '../path';
4
- import type { Response } from '../response';
5
- export declare class Layer implements Dispatcher {
6
- readonly '@instanceof': symbol;
7
- protected handler: Handler;
8
- protected pathMatcher: PathMatcher | undefined;
9
- constructor(handler: Handler);
10
- get type(): "error" | "core";
11
- get path(): import("../path").Path | undefined;
12
- get method(): string | undefined;
13
- dispatch(event: DispatcherEvent, meta: DispatcherMeta): Promise<boolean>;
14
- protected sendOutput(res: Response, input: unknown): Promise<any>;
15
- matchPath(path: string): boolean;
16
- matchMethod(method: string): boolean;
17
- }
@@ -1,8 +0,0 @@
1
- import type { MethodName } from '../constants';
2
- import type { HandlerFn } from '../handler';
3
- import type { Path } from '../path';
4
- export type LayerOptions = {
5
- handler: HandlerFn;
6
- method?: `${MethodName}`;
7
- path?: Path;
8
- };
@@ -1,2 +0,0 @@
1
- import type { Layer } from './module';
2
- export declare function isLayerInstance(input: unknown): input is Layer;