@adonisjs/http-server 6.0.2-0 → 6.1.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/build/index.d.ts +0 -1
- package/build/index.js +0 -1
- package/build/src/define_middleware.d.ts +6 -4
- package/build/src/define_middleware.js +14 -3
- package/build/src/http_context/main.d.ts +3 -1
- package/build/src/http_context/main.js +3 -1
- package/build/src/request.d.ts +1 -1
- package/build/src/request.js +1 -1
- package/build/src/router/brisk.d.ts +4 -6
- package/build/src/router/brisk.js +4 -4
- package/build/src/router/group.d.ts +5 -7
- package/build/src/router/group.js +6 -11
- package/build/src/router/main.d.ts +21 -17
- package/build/src/router/main.js +17 -8
- package/build/src/router/resource.d.ts +5 -7
- package/build/src/router/resource.js +4 -4
- package/build/src/router/route.d.ts +5 -7
- package/build/src/router/route.js +9 -12
- package/build/src/server/main.d.ts +7 -5
- package/build/src/server/main.js +32 -31
- package/build/src/types/middleware.d.ts +5 -1
- package/package.json +6 -7
- package/build/src/middleware/store.d.ts +0 -11
- package/build/src/middleware/store.js +0 -33
package/build/index.d.ts
CHANGED
|
@@ -13,4 +13,3 @@ export { HttpException } from './src/exceptions/http_exception.js';
|
|
|
13
13
|
export { AbortException } from './src/exceptions/abort_exception.js';
|
|
14
14
|
export { RouteNotFoundException } from './src/exceptions/route_not_found.js';
|
|
15
15
|
export { CannotLookupRouteException } from './src/exceptions/cannot_lookup_route.js';
|
|
16
|
-
export { defineMiddleware, defineNamedMiddleware } from './src/define_middleware.js';
|
package/build/index.js
CHANGED
|
@@ -13,4 +13,3 @@ export { HttpException } from './src/exceptions/http_exception.js';
|
|
|
13
13
|
export { AbortException } from './src/exceptions/abort_exception.js';
|
|
14
14
|
export { RouteNotFoundException } from './src/exceptions/route_not_found.js';
|
|
15
15
|
export { CannotLookupRouteException } from './src/exceptions/cannot_lookup_route.js';
|
|
16
|
-
export { defineMiddleware, defineNamedMiddleware } from './src/define_middleware.js';
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { LazyImport } from './types/base.js';
|
|
2
|
-
import type { MiddlewareAsClass } from './types/middleware.js';
|
|
3
|
-
export declare function
|
|
4
|
-
|
|
1
|
+
import type { LazyImport, UnWrapLazyImport } from './types/base.js';
|
|
2
|
+
import type { GetMiddlewareArgs, MiddlewareAsClass, ParsedGlobalMiddleware } from './types/middleware.js';
|
|
3
|
+
export declare function defineNamedMiddleware<NamedMiddleware extends Record<string | number | symbol, LazyImport<MiddlewareAsClass>>>(collection: NamedMiddleware): { [K in keyof NamedMiddleware]: <Args extends GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[K]>>>(...args: Args) => {
|
|
4
|
+
name: K;
|
|
5
|
+
args: Args[0];
|
|
6
|
+
} & ParsedGlobalMiddleware; };
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { moduleImporter } from '@adonisjs/fold';
|
|
2
|
+
function middlewareReferenceBuilder(name, middleware) {
|
|
3
|
+
const handler = moduleImporter(middleware, 'handle').toHandleMethod();
|
|
4
|
+
return function (...args) {
|
|
5
|
+
return {
|
|
6
|
+
name,
|
|
7
|
+
args: args[0],
|
|
8
|
+
...handler,
|
|
9
|
+
};
|
|
10
|
+
};
|
|
3
11
|
}
|
|
4
12
|
export function defineNamedMiddleware(collection) {
|
|
5
|
-
return collection
|
|
13
|
+
return Object.keys(collection).reduce((result, key) => {
|
|
14
|
+
result[key] = middlewareReferenceBuilder(key, collection[key]);
|
|
15
|
+
return result;
|
|
16
|
+
}, {});
|
|
6
17
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@adonisjs/logger';
|
|
2
2
|
import { Macroable } from '@poppinss/macroable';
|
|
3
|
+
import { ContainerResolver } from '@adonisjs/fold';
|
|
3
4
|
import type { Request } from '../request.js';
|
|
4
5
|
import type { Response } from '../response.js';
|
|
5
6
|
import type { StoreRouteNode } from '../types/route.js';
|
|
@@ -7,6 +8,7 @@ export declare class HttpContext extends Macroable {
|
|
|
7
8
|
request: Request;
|
|
8
9
|
response: Response;
|
|
9
10
|
logger: Logger;
|
|
11
|
+
containerResolver: ContainerResolver<any>;
|
|
10
12
|
static get usingAsyncLocalStorage(): boolean;
|
|
11
13
|
static get(): HttpContext | null;
|
|
12
14
|
static getOrFail(): HttpContext;
|
|
@@ -15,6 +17,6 @@ export declare class HttpContext extends Macroable {
|
|
|
15
17
|
routeKey?: string;
|
|
16
18
|
params: Record<string, any>;
|
|
17
19
|
subdomains: Record<string, any>;
|
|
18
|
-
constructor(request: Request, response: Response, logger: Logger);
|
|
20
|
+
constructor(request: Request, response: Response, logger: Logger, containerResolver: ContainerResolver<any>);
|
|
19
21
|
inspect(): string;
|
|
20
22
|
}
|
|
@@ -6,6 +6,7 @@ export class HttpContext extends Macroable {
|
|
|
6
6
|
request;
|
|
7
7
|
response;
|
|
8
8
|
logger;
|
|
9
|
+
containerResolver;
|
|
9
10
|
static get usingAsyncLocalStorage() {
|
|
10
11
|
return asyncLocalStorage.isEnabled;
|
|
11
12
|
}
|
|
@@ -35,11 +36,12 @@ export class HttpContext extends Macroable {
|
|
|
35
36
|
routeKey;
|
|
36
37
|
params = {};
|
|
37
38
|
subdomains = {};
|
|
38
|
-
constructor(request, response, logger) {
|
|
39
|
+
constructor(request, response, logger, containerResolver) {
|
|
39
40
|
super();
|
|
40
41
|
this.request = request;
|
|
41
42
|
this.response = response;
|
|
42
43
|
this.logger = logger;
|
|
44
|
+
this.containerResolver = containerResolver;
|
|
43
45
|
this.request.ctx = this;
|
|
44
46
|
this.response.ctx = this;
|
|
45
47
|
}
|
package/build/src/request.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
import { Macroable } from '@poppinss/macroable';
|
|
4
|
-
import type { Encryption } from '@adonisjs/encryption';
|
|
5
4
|
import { UrlWithStringQuery } from 'node:url';
|
|
5
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
6
6
|
import { ServerResponse, IncomingMessage, IncomingHttpHeaders } from 'node:http';
|
|
7
7
|
import type { Qs } from './qs.js';
|
|
8
8
|
import { RequestConfig } from './types/request.js';
|
package/build/src/request.js
CHANGED
|
@@ -4,8 +4,8 @@ import typeIs from 'type-is';
|
|
|
4
4
|
import accepts from 'accepts';
|
|
5
5
|
import { isIP } from 'node:net';
|
|
6
6
|
import proxyaddr from 'proxy-addr';
|
|
7
|
-
import { safeEqual } from '@poppinss/utils';
|
|
8
7
|
import lodash from '@poppinss/utils/lodash';
|
|
8
|
+
import { safeEqual } from '@poppinss/utils';
|
|
9
9
|
import { Macroable } from '@poppinss/macroable';
|
|
10
10
|
import { parse } from 'node:url';
|
|
11
11
|
import { trustProxy } from './helpers.js';
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { Macroable } from '@poppinss/macroable';
|
|
2
2
|
import type { Application } from '@adonisjs/application';
|
|
3
3
|
import { Route } from './route.js';
|
|
4
|
-
import type {
|
|
5
|
-
import type { MiddlewareStore } from '../middleware/store.js';
|
|
6
|
-
import type { MiddlewareAsClass } from '../types/middleware.js';
|
|
4
|
+
import type { ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
7
5
|
import type { MakeUrlOptions, RouteFn, RouteMatchers } from '../types/route.js';
|
|
8
|
-
export declare class BriskRoute
|
|
6
|
+
export declare class BriskRoute extends Macroable {
|
|
9
7
|
#private;
|
|
10
|
-
route: null | Route
|
|
11
|
-
constructor(app: Application<any, any
|
|
8
|
+
route: null | Route;
|
|
9
|
+
constructor(app: Application<any, any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
|
|
12
10
|
pattern: string;
|
|
13
11
|
globalMatchers: RouteMatchers;
|
|
14
12
|
});
|
|
@@ -4,17 +4,17 @@ export class BriskRoute extends Macroable {
|
|
|
4
4
|
#pattern;
|
|
5
5
|
#globalMatchers;
|
|
6
6
|
#app;
|
|
7
|
-
#
|
|
7
|
+
#routerMiddleware;
|
|
8
8
|
route = null;
|
|
9
|
-
constructor(app,
|
|
9
|
+
constructor(app, routerMiddleware, options) {
|
|
10
10
|
super();
|
|
11
11
|
this.#app = app;
|
|
12
|
-
this.#
|
|
12
|
+
this.#routerMiddleware = routerMiddleware;
|
|
13
13
|
this.#pattern = options.pattern;
|
|
14
14
|
this.#globalMatchers = options.globalMatchers;
|
|
15
15
|
}
|
|
16
16
|
setHandler(handler) {
|
|
17
|
-
this.route = new Route(this.#app, this.#
|
|
17
|
+
this.route = new Route(this.#app, this.#routerMiddleware, {
|
|
18
18
|
pattern: this.#pattern,
|
|
19
19
|
globalMatchers: this.#globalMatchers,
|
|
20
20
|
methods: ['GET', 'HEAD'],
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { Macroable } from '@poppinss/macroable';
|
|
2
|
-
import type { MiddlewareStore } from '../middleware/store.js';
|
|
3
|
-
import type { LazyImport, UnWrapLazyImport } from '../types/base.js';
|
|
4
2
|
import type { RouteMatcher } from '../types/route.js';
|
|
5
|
-
import type {
|
|
3
|
+
import type { MiddlewareFn, ParsedNamedMiddleware } from '../types/middleware.js';
|
|
6
4
|
import { Route } from './route.js';
|
|
7
5
|
import { BriskRoute } from './brisk.js';
|
|
8
6
|
import { RouteResource } from './resource.js';
|
|
9
|
-
export declare class RouteGroup
|
|
7
|
+
export declare class RouteGroup extends Macroable {
|
|
10
8
|
#private;
|
|
11
9
|
routes: (Route | RouteGroup | RouteResource | BriskRoute)[];
|
|
12
|
-
constructor(routes: (Route | RouteGroup | RouteResource | BriskRoute)[]
|
|
10
|
+
constructor(routes: (Route | RouteGroup | RouteResource | BriskRoute)[]);
|
|
13
11
|
where(param: string, matcher: RouteMatcher | string | RegExp): this;
|
|
14
12
|
prefix(prefix: string): this;
|
|
15
13
|
domain(domain: string): this;
|
|
16
14
|
as(name: string): this;
|
|
17
|
-
|
|
18
|
-
middleware(middleware: MiddlewareFn): this;
|
|
15
|
+
use(middleware: MiddlewareFn | ParsedNamedMiddleware): this;
|
|
16
|
+
middleware(middleware: MiddlewareFn | ParsedNamedMiddleware): this;
|
|
19
17
|
}
|
|
@@ -4,11 +4,9 @@ import { RouteResource } from './resource.js';
|
|
|
4
4
|
export class RouteGroup extends Macroable {
|
|
5
5
|
routes;
|
|
6
6
|
#middleware = [];
|
|
7
|
-
|
|
8
|
-
constructor(routes, middlewareStore) {
|
|
7
|
+
constructor(routes) {
|
|
9
8
|
super();
|
|
10
9
|
this.routes = routes;
|
|
11
|
-
this.#middlewareStore = middlewareStore;
|
|
12
10
|
}
|
|
13
11
|
#shareMiddlewareStackWithRoutes(route) {
|
|
14
12
|
if (route instanceof RouteGroup) {
|
|
@@ -101,17 +99,14 @@ export class RouteGroup extends Macroable {
|
|
|
101
99
|
this.routes.forEach((route) => this.#updateRouteName(route, name));
|
|
102
100
|
return this;
|
|
103
101
|
}
|
|
104
|
-
|
|
102
|
+
use(middleware) {
|
|
105
103
|
if (!this.#middleware.length) {
|
|
106
104
|
this.routes.forEach((route) => this.#shareMiddlewareStackWithRoutes(route));
|
|
107
105
|
}
|
|
108
|
-
|
|
109
|
-
this.#middleware.push(this.#middlewareStore.get(middleware, args));
|
|
110
|
-
return this;
|
|
111
|
-
}
|
|
112
|
-
if (typeof middleware === 'function') {
|
|
113
|
-
this.#middleware.push(middleware);
|
|
114
|
-
}
|
|
106
|
+
this.#middleware.push(middleware);
|
|
115
107
|
return this;
|
|
116
108
|
}
|
|
109
|
+
middleware(middleware) {
|
|
110
|
+
return this.use(middleware);
|
|
111
|
+
}
|
|
117
112
|
}
|
|
@@ -5,29 +5,33 @@ import { Route } from './route.js';
|
|
|
5
5
|
import { RouteGroup } from './group.js';
|
|
6
6
|
import { BriskRoute } from './brisk.js';
|
|
7
7
|
import { RouteResource } from './resource.js';
|
|
8
|
+
import type { LazyImport } from '../types/base.js';
|
|
8
9
|
import { LookupStore } from './lookup_store/main.js';
|
|
9
10
|
import { RouteMatchers as Matchers } from './matchers.js';
|
|
10
|
-
import type {
|
|
11
|
-
import { MiddlewareStore } from '../middleware/store.js';
|
|
12
|
-
import type { MiddlewareAsClass } from '../types/middleware.js';
|
|
11
|
+
import type { MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
13
12
|
import type { RouteFn, MatchedRoute, RouteMatcher, MakeUrlOptions, MakeSignedUrlOptions } from '../types/route.js';
|
|
14
|
-
export declare class Router
|
|
13
|
+
export declare class Router extends LookupStore {
|
|
15
14
|
#private;
|
|
16
|
-
routes: (Route
|
|
15
|
+
routes: (Route | RouteResource | RouteGroup | BriskRoute)[];
|
|
17
16
|
usingDomains: boolean;
|
|
18
17
|
matchers: Matchers;
|
|
19
|
-
constructor(app: Application<any, any
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
constructor(app: Application<any, any>, encryption: Encryption, qsParser: Qs);
|
|
19
|
+
use(middleware: LazyImport<MiddlewareAsClass>[]): this;
|
|
20
|
+
named<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>>>(collection: NamedMiddleware): { [K in keyof NamedMiddleware]: <Args extends import("../types/middleware.js").GetMiddlewareArgs<import("../types/base.js").UnWrapLazyImport<NamedMiddleware[K]>>>(...args: Args) => {
|
|
21
|
+
name: K;
|
|
22
|
+
args: Args[0];
|
|
23
|
+
} & ParsedGlobalMiddleware; };
|
|
24
|
+
route(pattern: string, methods: string[], handler: string | RouteFn): Route;
|
|
25
|
+
any(pattern: string, handler: string | RouteFn): Route;
|
|
26
|
+
get(pattern: string, handler: string | RouteFn): Route;
|
|
27
|
+
post(pattern: string, handler: string | RouteFn): Route;
|
|
28
|
+
put(pattern: string, handler: string | RouteFn): Route;
|
|
29
|
+
patch(pattern: string, handler: string | RouteFn): Route;
|
|
30
|
+
delete(pattern: string, handler: string | RouteFn): Route;
|
|
31
|
+
group(callback: () => void): RouteGroup;
|
|
32
|
+
resource(resource: string, controller: string): RouteResource;
|
|
33
|
+
shallowResource(resource: string, controller: string): RouteResource;
|
|
34
|
+
on(pattern: string): BriskRoute;
|
|
31
35
|
where(param: string, matcher: RouteMatcher | string | RegExp): this;
|
|
32
36
|
commit(): void;
|
|
33
37
|
match(url: string, method: string, hostname?: string | null): null | MatchedRoute;
|
package/build/src/router/main.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import is from '@sindresorhus/is';
|
|
2
|
+
import { moduleImporter } from '@adonisjs/fold';
|
|
2
3
|
import { RuntimeException } from '@poppinss/utils';
|
|
3
4
|
import { Route } from './route.js';
|
|
4
5
|
import { RouteGroup } from './group.js';
|
|
@@ -8,19 +9,19 @@ import { toRoutesJSON } from '../helpers.js';
|
|
|
8
9
|
import { RouteResource } from './resource.js';
|
|
9
10
|
import { LookupStore } from './lookup_store/main.js';
|
|
10
11
|
import { RouteMatchers as Matchers } from './matchers.js';
|
|
12
|
+
import { defineNamedMiddleware } from '../define_middleware.js';
|
|
11
13
|
export class Router extends LookupStore {
|
|
12
14
|
#app;
|
|
13
15
|
#store = new RoutesStore();
|
|
14
16
|
#globalMatchers = {};
|
|
15
|
-
#
|
|
17
|
+
#middleware = [];
|
|
16
18
|
#openedGroups = [];
|
|
17
19
|
routes = [];
|
|
18
20
|
usingDomains = false;
|
|
19
21
|
matchers = new Matchers();
|
|
20
|
-
constructor(app, encryption,
|
|
22
|
+
constructor(app, encryption, qsParser) {
|
|
21
23
|
super(encryption, qsParser);
|
|
22
24
|
this.#app = app;
|
|
23
|
-
this.#middlewareStore = middlewareStore;
|
|
24
25
|
}
|
|
25
26
|
#pushToRoutes(entity) {
|
|
26
27
|
const openedGroup = this.#openedGroups[this.#openedGroups.length - 1];
|
|
@@ -30,8 +31,15 @@ export class Router extends LookupStore {
|
|
|
30
31
|
}
|
|
31
32
|
this.routes.push(entity);
|
|
32
33
|
}
|
|
34
|
+
use(middleware) {
|
|
35
|
+
middleware.forEach((one) => this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod()));
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
named(collection) {
|
|
39
|
+
return defineNamedMiddleware(collection);
|
|
40
|
+
}
|
|
33
41
|
route(pattern, methods, handler) {
|
|
34
|
-
const route = new Route(this.#app, this.#
|
|
42
|
+
const route = new Route(this.#app, this.#middleware, {
|
|
35
43
|
pattern,
|
|
36
44
|
methods,
|
|
37
45
|
handler,
|
|
@@ -59,7 +67,7 @@ export class Router extends LookupStore {
|
|
|
59
67
|
return this.route(pattern, ['DELETE'], handler);
|
|
60
68
|
}
|
|
61
69
|
group(callback) {
|
|
62
|
-
const group = new RouteGroup([]
|
|
70
|
+
const group = new RouteGroup([]);
|
|
63
71
|
this.#pushToRoutes(group);
|
|
64
72
|
this.#openedGroups.push(group);
|
|
65
73
|
callback();
|
|
@@ -67,7 +75,7 @@ export class Router extends LookupStore {
|
|
|
67
75
|
return group;
|
|
68
76
|
}
|
|
69
77
|
resource(resource, controller) {
|
|
70
|
-
const resourceInstance = new RouteResource(this.#app, this.#
|
|
78
|
+
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
71
79
|
resource,
|
|
72
80
|
controller,
|
|
73
81
|
shallow: false,
|
|
@@ -77,7 +85,7 @@ export class Router extends LookupStore {
|
|
|
77
85
|
return resourceInstance;
|
|
78
86
|
}
|
|
79
87
|
shallowResource(resource, controller) {
|
|
80
|
-
const resourceInstance = new RouteResource(this.#app, this.#
|
|
88
|
+
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
81
89
|
resource,
|
|
82
90
|
controller,
|
|
83
91
|
shallow: true,
|
|
@@ -87,7 +95,7 @@ export class Router extends LookupStore {
|
|
|
87
95
|
return resourceInstance;
|
|
88
96
|
}
|
|
89
97
|
on(pattern) {
|
|
90
|
-
const briskRoute = new BriskRoute(this.#app, this.#
|
|
98
|
+
const briskRoute = new BriskRoute(this.#app, this.#middleware, {
|
|
91
99
|
pattern,
|
|
92
100
|
globalMatchers: this.#globalMatchers,
|
|
93
101
|
});
|
|
@@ -126,6 +134,7 @@ export class Router extends LookupStore {
|
|
|
126
134
|
this.usingDomains = this.#store.usingDomains;
|
|
127
135
|
this.routes = [];
|
|
128
136
|
this.#globalMatchers = {};
|
|
137
|
+
this.#middleware = [];
|
|
129
138
|
}
|
|
130
139
|
match(url, method, hostname) {
|
|
131
140
|
const matchingDomain = this.#store.matchDomain(hostname);
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { Macroable } from '@poppinss/macroable';
|
|
2
2
|
import type { Application } from '@adonisjs/application';
|
|
3
3
|
import { Route } from './route.js';
|
|
4
|
-
import type {
|
|
5
|
-
import type { MiddlewareStore } from '../middleware/store.js';
|
|
6
|
-
import type { MiddlewareAsClass } from '../types/middleware.js';
|
|
4
|
+
import type { ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
7
5
|
import type { ResourceActionNames, RouteMatcher, RouteMatchers } from '../types/route.js';
|
|
8
|
-
export declare class RouteResource
|
|
6
|
+
export declare class RouteResource extends Macroable {
|
|
9
7
|
#private;
|
|
10
8
|
routes: Route[];
|
|
11
|
-
constructor(app: Application<any, any
|
|
9
|
+
constructor(app: Application<any, any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
|
|
12
10
|
resource: string;
|
|
13
11
|
controller: string;
|
|
14
12
|
globalMatchers: RouteMatchers;
|
|
@@ -18,8 +16,8 @@ export declare class RouteResource<NamedMiddleware extends Record<string, LazyIm
|
|
|
18
16
|
except(names: ResourceActionNames[]): this;
|
|
19
17
|
apiOnly(): this;
|
|
20
18
|
where(key: string, matcher: RouteMatcher | string | RegExp): this;
|
|
21
|
-
tap(callback: (route: Route
|
|
22
|
-
tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route
|
|
19
|
+
tap(callback: (route: Route) => void): this;
|
|
20
|
+
tap(actions: ResourceActionNames | ResourceActionNames[], callback: (route: Route) => void): this;
|
|
23
21
|
params(resources: {
|
|
24
22
|
[resource: string]: string;
|
|
25
23
|
}): this;
|
|
@@ -8,16 +8,16 @@ export class RouteResource extends Macroable {
|
|
|
8
8
|
#shallow = false;
|
|
9
9
|
#globalMatchers;
|
|
10
10
|
#app;
|
|
11
|
-
#
|
|
11
|
+
#routerMiddleware;
|
|
12
12
|
#params = {};
|
|
13
13
|
#routesBaseName;
|
|
14
14
|
routes = [];
|
|
15
|
-
constructor(app,
|
|
15
|
+
constructor(app, routerMiddleware, options) {
|
|
16
16
|
super();
|
|
17
17
|
this.#validateResourceName(options.resource);
|
|
18
18
|
this.#app = app;
|
|
19
19
|
this.#shallow = options.shallow;
|
|
20
|
-
this.#
|
|
20
|
+
this.#routerMiddleware = routerMiddleware;
|
|
21
21
|
this.#controller = options.controller;
|
|
22
22
|
this.#globalMatchers = options.globalMatchers;
|
|
23
23
|
this.#resource = this.#normalizeResourceName(options.resource);
|
|
@@ -39,7 +39,7 @@ export class RouteResource extends Macroable {
|
|
|
39
39
|
.join('.');
|
|
40
40
|
}
|
|
41
41
|
#createRoute(pattern, methods, action) {
|
|
42
|
-
const route = new Route(this.#app, this.#
|
|
42
|
+
const route = new Route(this.#app, this.#routerMiddleware, {
|
|
43
43
|
pattern,
|
|
44
44
|
methods,
|
|
45
45
|
handler: `${this.#controller}.${action}`,
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { Macroable } from '@poppinss/macroable';
|
|
2
2
|
import type { Application } from '@adonisjs/application';
|
|
3
|
-
import type {
|
|
4
|
-
import type { LazyImport, UnWrapLazyImport } from '../types/base.js';
|
|
5
|
-
import type { GetMiddlewareArgs, MiddlewareAsClass, MiddlewareFn } from '../types/middleware.js';
|
|
3
|
+
import type { MiddlewareFn, ParsedNamedMiddleware, ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
6
4
|
import type { RouteFn, RouteJSON, RouteMatcher, RouteMatchers, StoreRouteMiddleware } from '../types/route.js';
|
|
7
|
-
export declare class Route
|
|
5
|
+
export declare class Route extends Macroable {
|
|
8
6
|
#private;
|
|
9
|
-
constructor(app: Application<any, any
|
|
7
|
+
constructor(app: Application<any, any>, routerMiddleware: ParsedGlobalMiddleware[], options: {
|
|
10
8
|
pattern: string;
|
|
11
9
|
methods: string[];
|
|
12
10
|
handler: RouteFn | string;
|
|
@@ -15,8 +13,8 @@ export declare class Route<NamedMiddleware extends Record<string, LazyImport<Mid
|
|
|
15
13
|
where(param: string, matcher: RouteMatcher | string | RegExp): this;
|
|
16
14
|
prefix(prefix: string): this;
|
|
17
15
|
domain(domain: string, overwrite?: boolean): this;
|
|
18
|
-
|
|
19
|
-
middleware(middleware: MiddlewareFn): this;
|
|
16
|
+
use(middleware: MiddlewareFn | ParsedNamedMiddleware): this;
|
|
17
|
+
middleware(middleware: MiddlewareFn | ParsedNamedMiddleware): this;
|
|
20
18
|
as(name: string, prepend?: boolean): this;
|
|
21
19
|
isDeleted(): boolean;
|
|
22
20
|
markAsDeleted(): void;
|
|
@@ -13,15 +13,15 @@ export class Route extends Macroable {
|
|
|
13
13
|
#handler;
|
|
14
14
|
#globalMatchers;
|
|
15
15
|
#app;
|
|
16
|
-
#
|
|
16
|
+
#routerMiddleware;
|
|
17
17
|
#routeDomain = 'root';
|
|
18
18
|
#matchers = {};
|
|
19
19
|
#prefixes = [];
|
|
20
20
|
#middleware = [];
|
|
21
|
-
constructor(app,
|
|
21
|
+
constructor(app, routerMiddleware, options) {
|
|
22
22
|
super();
|
|
23
23
|
this.#app = app;
|
|
24
|
-
this.#
|
|
24
|
+
this.#routerMiddleware = routerMiddleware;
|
|
25
25
|
this.#pattern = options.pattern;
|
|
26
26
|
this.#methods = options.methods;
|
|
27
27
|
this.#handler = this.#resolveRouteHandle(options.handler);
|
|
@@ -73,16 +73,13 @@ export class Route extends Macroable {
|
|
|
73
73
|
}
|
|
74
74
|
return this;
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
this.#middleware.push([this.#middlewareStore.get(middleware, args)]);
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
if (typeof middleware === 'function') {
|
|
82
|
-
this.#middleware.push([middleware]);
|
|
83
|
-
}
|
|
76
|
+
use(middleware) {
|
|
77
|
+
this.#middleware.push([middleware]);
|
|
84
78
|
return this;
|
|
85
79
|
}
|
|
80
|
+
middleware(middleware) {
|
|
81
|
+
return this.use(middleware);
|
|
82
|
+
}
|
|
86
83
|
as(name, prepend = false) {
|
|
87
84
|
if (prepend) {
|
|
88
85
|
if (!this.#name) {
|
|
@@ -115,7 +112,7 @@ export class Route extends Macroable {
|
|
|
115
112
|
}
|
|
116
113
|
#getMiddlewareForStore() {
|
|
117
114
|
const middleware = new Middleware();
|
|
118
|
-
this.#
|
|
115
|
+
this.#routerMiddleware.forEach((one) => middleware.add(one));
|
|
119
116
|
this.#middleware.flat().forEach((one) => middleware.add(one));
|
|
120
117
|
return middleware;
|
|
121
118
|
}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
import type { Encryption } from '@adonisjs/encryption';
|
|
4
|
-
import type { Application } from '@adonisjs/application';
|
|
5
4
|
import type { Server as HttpsServer } from 'node:https';
|
|
5
|
+
import type { Application } from '@adonisjs/application';
|
|
6
6
|
import type { ServerResponse, IncomingMessage, Server as HttpServer } from 'node:http';
|
|
7
7
|
import type { LazyImport } from '../types/base.js';
|
|
8
8
|
import type { MiddlewareAsClass } from '../types/middleware.js';
|
|
9
9
|
import type { ErrorHandlerAsAClass, ServerConfig } from '../types/server.js';
|
|
10
10
|
import { Router } from '../router/main.js';
|
|
11
|
-
|
|
11
|
+
import { HttpContext } from '../http_context/main.js';
|
|
12
|
+
export declare class Server {
|
|
12
13
|
#private;
|
|
13
14
|
get usingAsyncLocalStorage(): boolean;
|
|
14
|
-
constructor(app: Application<any, any
|
|
15
|
-
use(
|
|
15
|
+
constructor(app: Application<any, any>, encryption: Encryption, config: ServerConfig);
|
|
16
|
+
use(middleware: LazyImport<MiddlewareAsClass>[]): this;
|
|
16
17
|
errorHandler(handler: LazyImport<ErrorHandlerAsAClass>): this;
|
|
17
18
|
boot(): Promise<void>;
|
|
18
19
|
setNodeServer(server: HttpServer | HttpsServer): void;
|
|
19
20
|
getNodeServer(): HttpServer<typeof IncomingMessage, typeof ServerResponse> | HttpsServer<typeof IncomingMessage, typeof ServerResponse> | undefined;
|
|
20
|
-
getRouter(): Router
|
|
21
|
+
getRouter(): Router;
|
|
22
|
+
onRequest(callback: (ctx: HttpContext) => void): this;
|
|
21
23
|
handle(req: IncomingMessage, res: ServerResponse): Promise<any>;
|
|
22
24
|
}
|
package/build/src/server/main.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import Middleware from '@poppinss/middleware';
|
|
2
|
-
import {
|
|
2
|
+
import { moduleImporter } from '@adonisjs/fold';
|
|
3
3
|
import { Qs } from '../qs.js';
|
|
4
4
|
import debug from '../debug.js';
|
|
5
5
|
import { Request } from '../request.js';
|
|
6
6
|
import { Response } from '../response.js';
|
|
7
7
|
import { Router } from '../router/main.js';
|
|
8
8
|
import { HttpContext } from '../http_context/main.js';
|
|
9
|
-
import { MiddlewareStore } from '../middleware/store.js';
|
|
10
9
|
import { finalHandler } from './factories/final_handler.js';
|
|
11
10
|
import { writeResponse } from './factories/write_response.js';
|
|
12
11
|
import { useReturnValue } from './factories/use_return_value.js';
|
|
13
12
|
import { asyncLocalStorage } from '../http_context/local_storage.js';
|
|
14
13
|
import { middlewareHandler } from './factories/middleware_handler.js';
|
|
15
14
|
export class Server {
|
|
15
|
+
#requestHooks = new Set();
|
|
16
16
|
#errorHandler;
|
|
17
17
|
#resolvedErrorHandler = {
|
|
18
18
|
handle(error, ctx) {
|
|
@@ -25,43 +25,45 @@ export class Server {
|
|
|
25
25
|
#qsParser;
|
|
26
26
|
#serverMiddlewareStack;
|
|
27
27
|
#router;
|
|
28
|
-
#
|
|
28
|
+
#nodeHttpServer;
|
|
29
|
+
#middleware = [];
|
|
29
30
|
get usingAsyncLocalStorage() {
|
|
30
31
|
return asyncLocalStorage.isEnabled;
|
|
31
32
|
}
|
|
32
33
|
constructor(app, encryption, config) {
|
|
33
34
|
this.#app = app;
|
|
34
|
-
this.#encryption = encryption;
|
|
35
35
|
this.#config = config;
|
|
36
|
+
this.#encryption = encryption;
|
|
36
37
|
this.#qsParser = new Qs(this.#config.qs);
|
|
38
|
+
this.#router = new Router(this.#app, this.#encryption, this.#qsParser);
|
|
37
39
|
this.#createAsyncLocalStore();
|
|
38
40
|
debug('server config: %O', this.#config);
|
|
39
41
|
}
|
|
40
42
|
#createAsyncLocalStore() {
|
|
41
43
|
if (this.#config.useAsyncLocalStorage) {
|
|
42
|
-
debug('creating
|
|
44
|
+
debug('creating ALS store for HTTP context');
|
|
43
45
|
asyncLocalStorage.create();
|
|
44
46
|
}
|
|
45
47
|
else {
|
|
46
48
|
asyncLocalStorage.destroy();
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
|
-
#
|
|
50
|
-
this.#router = new Router(this.#app, this.#encryption, middlewareStore, this.#qsParser);
|
|
51
|
-
}
|
|
52
|
-
#createServerMiddlewareStack(middlewareStore) {
|
|
51
|
+
#createServerMiddlewareStack() {
|
|
53
52
|
this.#serverMiddlewareStack = new Middleware();
|
|
54
|
-
|
|
53
|
+
this.#middleware.forEach((middleware) => this.#serverMiddlewareStack.add(middleware));
|
|
55
54
|
this.#serverMiddlewareStack.freeze();
|
|
55
|
+
this.#middleware = [];
|
|
56
56
|
}
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
#handleRequest(ctx, resolver) {
|
|
58
|
+
return this.#serverMiddlewareStack.runner()
|
|
59
|
+
.finalHandler(finalHandler(this.#router, resolver, ctx))
|
|
60
|
+
.run(middlewareHandler(resolver, ctx))
|
|
61
|
+
.then(useReturnValue(ctx))
|
|
62
|
+
.catch((error) => this.#resolvedErrorHandler.handle(error, ctx))
|
|
63
|
+
.finally(writeResponse(ctx));
|
|
61
64
|
}
|
|
62
|
-
use(
|
|
63
|
-
this.#
|
|
64
|
-
this.#createServerMiddlewareStack(new MiddlewareStore(serverMiddleware));
|
|
65
|
+
use(middleware) {
|
|
66
|
+
middleware.forEach((one) => this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod()));
|
|
65
67
|
return this;
|
|
66
68
|
}
|
|
67
69
|
errorHandler(handler) {
|
|
@@ -70,9 +72,7 @@ export class Server {
|
|
|
70
72
|
}
|
|
71
73
|
async boot() {
|
|
72
74
|
debug('booting HTTP server');
|
|
73
|
-
|
|
74
|
-
throw new RuntimeException('Cannot boot HTTP server. Register middleware using "server.use" first');
|
|
75
|
-
}
|
|
75
|
+
this.#createServerMiddlewareStack();
|
|
76
76
|
this.#router.commit();
|
|
77
77
|
if (this.#errorHandler) {
|
|
78
78
|
if (debug.enabled) {
|
|
@@ -82,26 +82,27 @@ export class Server {
|
|
|
82
82
|
this.#resolvedErrorHandler = await this.#app.container.make(moduleExports.default);
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
#handleRequest(ctx, resolver) {
|
|
86
|
-
return this.#serverMiddlewareStack.runner()
|
|
87
|
-
.finalHandler(finalHandler(this.#router, resolver, ctx))
|
|
88
|
-
.run(middlewareHandler(resolver, ctx))
|
|
89
|
-
.then(useReturnValue(ctx))
|
|
90
|
-
.catch((error) => this.#resolvedErrorHandler.handle(error, ctx))
|
|
91
|
-
.finally(writeResponse(ctx));
|
|
92
|
-
}
|
|
93
85
|
setNodeServer(server) {
|
|
94
|
-
this.#
|
|
86
|
+
this.#nodeHttpServer = server;
|
|
95
87
|
}
|
|
96
88
|
getNodeServer() {
|
|
97
|
-
return this.#
|
|
89
|
+
return this.#nodeHttpServer;
|
|
98
90
|
}
|
|
99
91
|
getRouter() {
|
|
100
92
|
return this.#router;
|
|
101
93
|
}
|
|
94
|
+
onRequest(callback) {
|
|
95
|
+
this.#requestHooks.add(callback);
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
102
98
|
handle(req, res) {
|
|
103
|
-
const ctx = this.#createContext(req, res);
|
|
104
99
|
const resolver = this.#app.container.createResolver();
|
|
100
|
+
const request = new Request(req, res, this.#encryption, this.#config, this.#qsParser);
|
|
101
|
+
const response = new Response(req, res, this.#encryption, this.#config, this.#router, this.#qsParser);
|
|
102
|
+
const ctx = new HttpContext(request, response, this.#app.logger.child({}), resolver);
|
|
103
|
+
for (let hook of this.#requestHooks) {
|
|
104
|
+
hook(ctx);
|
|
105
|
+
}
|
|
105
106
|
if (this.usingAsyncLocalStorage) {
|
|
106
107
|
return asyncLocalStorage.storage.run(ctx, () => this.#handleRequest(ctx, resolver));
|
|
107
108
|
}
|
|
@@ -5,9 +5,13 @@ export type MiddlewareAsClass = Constructor<{
|
|
|
5
5
|
handle: (ctx: HttpContext, next: NextFn, args?: any) => any;
|
|
6
6
|
}>;
|
|
7
7
|
type HasUndefined<T> = T extends NonNullable<T> ? true : false;
|
|
8
|
-
export type GetMiddlewareArgs<Middleware extends MiddlewareAsClass> = HasUndefined<Parameters<InstanceType<Middleware>['handle']>[2]> extends true ? [Parameters<InstanceType<Middleware>['handle']>[2]] : [Parameters<InstanceType<Middleware>['handle']>[2]?];
|
|
8
|
+
export type GetMiddlewareArgs<Middleware extends MiddlewareAsClass> = Parameters<InstanceType<Middleware>['handle']>[2] extends undefined ? [] : HasUndefined<Parameters<InstanceType<Middleware>['handle']>[2]> extends true ? [Parameters<InstanceType<Middleware>['handle']>[2]] : [Parameters<InstanceType<Middleware>['handle']>[2]?];
|
|
9
9
|
export type MiddlewareFn = (ctx: HttpContext, next: NextFn) => any;
|
|
10
10
|
export type ParsedGlobalMiddleware = {
|
|
11
11
|
handle: (resolver: ContainerResolver<any>, ...args: [ctx: HttpContext, next: NextFn, params?: any]) => any;
|
|
12
12
|
};
|
|
13
|
+
export type ParsedNamedMiddleware = ParsedGlobalMiddleware & {
|
|
14
|
+
name: string;
|
|
15
|
+
args: any;
|
|
16
|
+
};
|
|
13
17
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/http-server",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0-0",
|
|
4
4
|
"description": "AdonisJS HTTP server with support packed with Routing and Cookies",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -37,11 +37,10 @@
|
|
|
37
37
|
"author": "virk,adonisjs",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@adonisjs/application": "^6.
|
|
41
|
-
"@adonisjs/config": "^4.1.
|
|
40
|
+
"@adonisjs/application": "^6.3.1-0",
|
|
41
|
+
"@adonisjs/config": "^4.1.2-0",
|
|
42
42
|
"@adonisjs/encryption": "^5.0.2-0",
|
|
43
|
-
"@adonisjs/
|
|
44
|
-
"@adonisjs/fold": "^9.5.0-0",
|
|
43
|
+
"@adonisjs/fold": "^9.6.0-0",
|
|
45
44
|
"@adonisjs/logger": "^5.1.0-0",
|
|
46
45
|
"@commitlint/cli": "^17.3.0",
|
|
47
46
|
"@commitlint/config-conventional": "^17.3.0",
|
|
@@ -114,9 +113,9 @@
|
|
|
114
113
|
"vary": "^1.1.2"
|
|
115
114
|
},
|
|
116
115
|
"peerDependencies": {
|
|
117
|
-
"@adonisjs/application": "^6.
|
|
116
|
+
"@adonisjs/application": "^6.3.1-0",
|
|
118
117
|
"@adonisjs/encryption": "^5.0.2-0",
|
|
119
|
-
"@adonisjs/fold": "^9.
|
|
118
|
+
"@adonisjs/fold": "^9.6.0-0"
|
|
120
119
|
},
|
|
121
120
|
"repository": {
|
|
122
121
|
"type": "git",
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { LazyImport, UnWrapLazyImport } from '../types/base.js';
|
|
2
|
-
import type { GetMiddlewareArgs, MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js';
|
|
3
|
-
export declare class MiddlewareStore<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>>> {
|
|
4
|
-
#private;
|
|
5
|
-
constructor(middleware: LazyImport<MiddlewareAsClass>[], namedMiddleware?: NamedMiddleware);
|
|
6
|
-
get<Name extends keyof NamedMiddleware, Args extends GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[Name]>>>(name: Name, ...args: Args): {
|
|
7
|
-
name: Name;
|
|
8
|
-
args: any;
|
|
9
|
-
} & ParsedGlobalMiddleware;
|
|
10
|
-
list(): ParsedGlobalMiddleware[];
|
|
11
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { moduleImporter } from '@adonisjs/fold';
|
|
2
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
3
|
-
export class MiddlewareStore {
|
|
4
|
-
#namedMiddleware;
|
|
5
|
-
#middleware;
|
|
6
|
-
#resolvedMiddleware = new Map();
|
|
7
|
-
constructor(middleware, namedMiddleware) {
|
|
8
|
-
this.#middleware = middleware.map((one) => moduleImporter(one, 'handle').toHandleMethod());
|
|
9
|
-
this.#namedMiddleware = namedMiddleware;
|
|
10
|
-
}
|
|
11
|
-
get(name, ...args) {
|
|
12
|
-
if (this.#resolvedMiddleware.has(name)) {
|
|
13
|
-
return {
|
|
14
|
-
name,
|
|
15
|
-
args: args[0],
|
|
16
|
-
...this.#resolvedMiddleware.get(name),
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
if (!this.#namedMiddleware || !this.#namedMiddleware[name]) {
|
|
20
|
-
throw new RuntimeException(`Cannot resolve "${String(name)}" middleware. Make sure the middleware is registered before using it`);
|
|
21
|
-
}
|
|
22
|
-
const handler = moduleImporter(this.#namedMiddleware[name], 'handle').toHandleMethod();
|
|
23
|
-
this.#resolvedMiddleware.set(name, handler);
|
|
24
|
-
return {
|
|
25
|
-
name,
|
|
26
|
-
args: args[0],
|
|
27
|
-
...handler,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
list() {
|
|
31
|
-
return this.#middleware;
|
|
32
|
-
}
|
|
33
|
-
}
|