@adonisjs/http-server 5.11.0 → 6.0.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.
Files changed (169) hide show
  1. package/README.md +15 -38
  2. package/build/index.d.ts +16 -0
  3. package/build/index.js +16 -0
  4. package/build/src/cookies/client.d.ts +12 -0
  5. package/build/src/cookies/client.js +42 -0
  6. package/build/src/cookies/drivers/encrypted.d.ts +4 -0
  7. package/build/src/cookies/drivers/encrypted.js +16 -0
  8. package/build/src/cookies/drivers/plain.d.ts +3 -0
  9. package/build/src/cookies/drivers/plain.js +13 -0
  10. package/build/src/cookies/drivers/signed.d.ts +4 -0
  11. package/build/src/cookies/drivers/signed.js +16 -0
  12. package/build/src/cookies/parser.d.ts +9 -0
  13. package/build/src/cookies/parser.js +69 -0
  14. package/build/src/cookies/serializer.d.ts +11 -0
  15. package/build/src/cookies/serializer.js +39 -0
  16. package/build/src/debug.d.ts +3 -0
  17. package/build/src/debug.js +2 -0
  18. package/build/src/define_config.d.ts +2 -0
  19. package/build/src/define_config.js +40 -0
  20. package/build/src/define_middleware.d.ts +4 -0
  21. package/build/src/define_middleware.js +6 -0
  22. package/build/src/exceptions/abort_exception.d.ts +5 -0
  23. package/build/src/exceptions/abort_exception.js +6 -0
  24. package/build/src/exceptions/cannot_lookup_route.d.ts +5 -0
  25. package/build/src/exceptions/cannot_lookup_route.js +5 -0
  26. package/build/src/exceptions/http_exception.d.ts +6 -0
  27. package/build/src/exceptions/http_exception.js +20 -0
  28. package/build/src/exceptions/route_not_found.d.ts +5 -0
  29. package/build/src/exceptions/route_not_found.js +5 -0
  30. package/build/src/helpers.d.ts +6 -67
  31. package/build/src/helpers.js +13 -168
  32. package/build/src/http_context/local_storage.d.ts +9 -0
  33. package/build/src/http_context/local_storage.js +14 -0
  34. package/build/src/http_context/main.d.ts +20 -0
  35. package/build/src/http_context/main.js +49 -0
  36. package/build/src/middleware/store.d.ts +11 -0
  37. package/build/src/middleware/store.js +33 -0
  38. package/build/src/qs.d.ts +7 -0
  39. package/build/src/qs.js +13 -0
  40. package/build/src/redirect.d.ts +18 -0
  41. package/build/src/redirect.js +80 -0
  42. package/build/src/request.d.ts +81 -0
  43. package/build/src/request.js +315 -0
  44. package/build/src/response.d.ts +108 -0
  45. package/build/src/response.js +587 -0
  46. package/build/src/router/brisk.d.ts +18 -0
  47. package/build/src/router/brisk.js +35 -0
  48. package/build/src/router/executor.d.ts +4 -0
  49. package/build/src/router/executor.js +16 -0
  50. package/build/src/router/group.d.ts +19 -0
  51. package/build/src/router/group.js +117 -0
  52. package/build/src/router/lookup_store/main.d.ts +17 -0
  53. package/build/src/router/lookup_store/main.js +37 -0
  54. package/build/src/router/lookup_store/route_finder.d.ts +8 -0
  55. package/build/src/router/lookup_store/route_finder.js +28 -0
  56. package/build/src/router/lookup_store/url_builder.d.ts +16 -0
  57. package/build/src/router/lookup_store/url_builder.js +111 -0
  58. package/build/src/router/main.d.ts +36 -0
  59. package/build/src/router/main.js +161 -0
  60. package/build/src/router/matchers.d.ts +14 -0
  61. package/build/src/router/matchers.js +15 -0
  62. package/build/src/router/resource.d.ts +27 -0
  63. package/build/src/router/resource.js +124 -0
  64. package/build/src/router/route.d.ts +28 -0
  65. package/build/src/router/route.js +135 -0
  66. package/build/src/router/store.d.ts +12 -0
  67. package/build/src/router/store.js +87 -0
  68. package/build/src/server/factories/final_handler.d.ts +4 -0
  69. package/build/src/server/factories/final_handler.js +17 -0
  70. package/build/src/server/factories/middleware_handler.d.ts +5 -0
  71. package/build/src/server/factories/middleware_handler.js +5 -0
  72. package/build/src/server/factories/use_return_value.d.ts +2 -0
  73. package/build/src/server/factories/use_return_value.js +9 -0
  74. package/build/src/server/factories/write_response.d.ts +2 -0
  75. package/build/src/server/factories/write_response.js +12 -0
  76. package/build/src/server/main.d.ts +23 -0
  77. package/build/src/server/main.js +124 -0
  78. package/build/src/types/base.d.ts +5 -0
  79. package/build/src/types/base.js +1 -0
  80. package/build/src/types/middleware.d.ts +14 -0
  81. package/build/src/types/middleware.js +1 -0
  82. package/build/src/types/qs.d.ts +15 -0
  83. package/build/src/types/qs.js +1 -0
  84. package/build/src/types/request.d.ts +7 -0
  85. package/build/src/types/request.js +1 -0
  86. package/build/src/types/response.d.ts +18 -0
  87. package/build/src/types/response.js +1 -0
  88. package/build/src/types/route.d.ts +74 -0
  89. package/build/src/types/route.js +1 -0
  90. package/build/src/types/server.d.ts +13 -0
  91. package/build/src/types/server.js +1 -0
  92. package/package.json +98 -96
  93. package/build/adonis-typings/container.d.ts +0 -24
  94. package/build/adonis-typings/container.js +0 -8
  95. package/build/adonis-typings/context.d.ts +0 -73
  96. package/build/adonis-typings/context.js +0 -8
  97. package/build/adonis-typings/cookie-client.d.ts +0 -34
  98. package/build/adonis-typings/cookie-client.js +0 -8
  99. package/build/adonis-typings/http-server.d.ts +0 -98
  100. package/build/adonis-typings/http-server.js +0 -8
  101. package/build/adonis-typings/index.d.ts +0 -16
  102. package/build/adonis-typings/index.js +0 -16
  103. package/build/adonis-typings/middleware.d.ts +0 -95
  104. package/build/adonis-typings/middleware.js +0 -8
  105. package/build/adonis-typings/request.d.ts +0 -556
  106. package/build/adonis-typings/request.js +0 -8
  107. package/build/adonis-typings/response.d.ts +0 -438
  108. package/build/adonis-typings/response.js +0 -8
  109. package/build/adonis-typings/route.d.ts +0 -559
  110. package/build/adonis-typings/route.js +0 -8
  111. package/build/exceptions.json +0 -110
  112. package/build/providers/HttpServerProvider.d.ts +0 -46
  113. package/build/providers/HttpServerProvider.js +0 -100
  114. package/build/src/Cookie/Client/index.d.ts +0 -39
  115. package/build/src/Cookie/Client/index.js +0 -108
  116. package/build/src/Cookie/Drivers/Encrypted.d.ts +0 -24
  117. package/build/src/Cookie/Drivers/Encrypted.js +0 -42
  118. package/build/src/Cookie/Drivers/Plain.d.ts +0 -23
  119. package/build/src/Cookie/Drivers/Plain.js +0 -40
  120. package/build/src/Cookie/Drivers/Signed.d.ts +0 -24
  121. package/build/src/Cookie/Drivers/Signed.js +0 -42
  122. package/build/src/Cookie/Parser/index.d.ts +0 -61
  123. package/build/src/Cookie/Parser/index.js +0 -174
  124. package/build/src/Cookie/Serializer/index.d.ts +0 -44
  125. package/build/src/Cookie/Serializer/index.js +0 -88
  126. package/build/src/Exceptions/HttpException.d.ts +0 -20
  127. package/build/src/Exceptions/HttpException.js +0 -36
  128. package/build/src/Exceptions/RouterException.d.ts +0 -36
  129. package/build/src/Exceptions/RouterException.js +0 -76
  130. package/build/src/HttpContext/LocalStorage/index.d.ts +0 -24
  131. package/build/src/HttpContext/LocalStorage/index.js +0 -28
  132. package/build/src/HttpContext/index.d.ts +0 -90
  133. package/build/src/HttpContext/index.js +0 -181
  134. package/build/src/MiddlewareStore/index.d.ts +0 -92
  135. package/build/src/MiddlewareStore/index.js +0 -133
  136. package/build/src/Redirect/index.d.ts +0 -71
  137. package/build/src/Redirect/index.js +0 -139
  138. package/build/src/Request/index.d.ts +0 -619
  139. package/build/src/Request/index.js +0 -862
  140. package/build/src/Response/index.d.ts +0 -412
  141. package/build/src/Response/index.js +0 -1010
  142. package/build/src/Router/BriskRoute.d.ts +0 -53
  143. package/build/src/Router/BriskRoute.js +0 -74
  144. package/build/src/Router/Group.d.ts +0 -101
  145. package/build/src/Router/Group.js +0 -165
  146. package/build/src/Router/LookupStore.d.ts +0 -122
  147. package/build/src/Router/LookupStore.js +0 -264
  148. package/build/src/Router/Matchers.d.ts +0 -31
  149. package/build/src/Router/Matchers.js +0 -43
  150. package/build/src/Router/Resource.d.ts +0 -95
  151. package/build/src/Router/Resource.js +0 -182
  152. package/build/src/Router/Route.d.ts +0 -138
  153. package/build/src/Router/Route.js +0 -204
  154. package/build/src/Router/Store.d.ts +0 -93
  155. package/build/src/Router/Store.js +0 -211
  156. package/build/src/Router/index.d.ts +0 -142
  157. package/build/src/Router/index.js +0 -333
  158. package/build/src/Server/ExceptionManager/index.d.ts +0 -49
  159. package/build/src/Server/ExceptionManager/index.js +0 -96
  160. package/build/src/Server/Hooks/index.d.ts +0 -43
  161. package/build/src/Server/Hooks/index.js +0 -77
  162. package/build/src/Server/PreCompiler/index.d.ts +0 -60
  163. package/build/src/Server/PreCompiler/index.js +0 -141
  164. package/build/src/Server/RequestHandler/index.d.ts +0 -39
  165. package/build/src/Server/RequestHandler/index.js +0 -87
  166. package/build/src/Server/index.d.ts +0 -90
  167. package/build/src/Server/index.js +0 -175
  168. package/build/standalone.d.ts +0 -14
  169. package/build/standalone.js +0 -23
@@ -1,113 +1,37 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/http-server
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.statFn = exports.normalizeMakeSignedUrlOptions = exports.normalizeMakeUrlOptions = exports.trustProxy = exports.useReturnValue = exports.processPattern = exports.toRoutesJSON = exports.dropSlash = void 0;
15
- /// <reference path="../adonis-typings/index.ts" />
16
- const tmp_cache_1 = __importDefault(require("tmp-cache"));
17
- const fs_1 = require("fs");
18
- const Group_1 = require("./Router/Group");
19
- const BriskRoute_1 = require("./Router/BriskRoute");
20
- const Resource_1 = require("./Router/Resource");
21
- const RouterException_1 = require("./Exceptions/RouterException");
22
- const proxyCache = new tmp_cache_1.default({ max: 200 });
23
- /**
24
- * Makes input string consistent by having only the starting
25
- * slash
26
- */
27
- function dropSlash(input) {
1
+ import Cache from 'tmp-cache';
2
+ import { BriskRoute } from './router/brisk.js';
3
+ import { RouteGroup } from './router/group.js';
4
+ import { RouteResource } from './router/resource.js';
5
+ const proxyCache = new Cache({ max: 200 });
6
+ export function dropSlash(input) {
28
7
  if (input === '/') {
29
8
  return '/';
30
9
  }
31
10
  return `/${input.replace(/^\//, '').replace(/\/$/, '')}`;
32
11
  }
33
- exports.dropSlash = dropSlash;
34
- /**
35
- * Converts and array of routes or route groups or route resource to a flat
36
- * list of route defination.
37
- */
38
- function toRoutesJSON(routes) {
12
+ export function toRoutesJSON(routes) {
39
13
  return routes.reduce((list, route) => {
40
- if (route instanceof Group_1.RouteGroup) {
14
+ if (route instanceof RouteGroup) {
41
15
  list = list.concat(toRoutesJSON(route.routes));
42
16
  return list;
43
17
  }
44
- if (route instanceof Resource_1.RouteResource) {
18
+ if (route instanceof RouteResource) {
45
19
  list = list.concat(toRoutesJSON(route.routes));
46
20
  return list;
47
21
  }
48
- if (route instanceof BriskRoute_1.BriskRoute) {
49
- if (route.route) {
22
+ if (route instanceof BriskRoute) {
23
+ if (route.route && !route.route.isDeleted()) {
50
24
  list.push(route.route.toJSON());
51
25
  }
52
26
  return list;
53
27
  }
54
- if (!route.deleted) {
28
+ if (!route.isDeleted()) {
55
29
  list.push(route.toJSON());
56
30
  }
57
31
  return list;
58
32
  }, []);
59
33
  }
60
- exports.toRoutesJSON = toRoutesJSON;
61
- /**
62
- * Makes url for a route pattern and params and querystring.
63
- */
64
- function processPattern(pattern, data) {
65
- let url = pattern;
66
- if (url.indexOf(':') > -1) {
67
- /*
68
- * Split pattern when route has dynamic segments
69
- */
70
- const tokens = url.split('/');
71
- /*
72
- * Lookup over the route tokens and replace them the params values
73
- */
74
- url = tokens
75
- .map((token) => {
76
- if (!token.startsWith(':')) {
77
- return token;
78
- }
79
- const isOptional = token.endsWith('?');
80
- const paramName = token.replace(/^:/, '').replace(/\?$/, '');
81
- const param = data[paramName];
82
- /*
83
- * A required param is always required to make the complete URL
84
- */
85
- if (!param && !isOptional) {
86
- throw RouterException_1.RouterException.cannotMakeRoute(paramName, pattern);
87
- }
88
- return param;
89
- })
90
- .join('/');
91
- }
92
- return url;
93
- }
94
- exports.processPattern = processPattern;
95
- /**
96
- * Returns a boolean telling if the return value must be used as
97
- * the response body or not
98
- */
99
- function useReturnValue(returnValue, ctx) {
100
- return (returnValue !== undefined && // Return value is explicitly defined
101
- returnValue !== ctx.response && // Return value is not the instance of response object
102
- !ctx.response.hasLazyBody // Lazy body is not set
103
- );
104
- }
105
- exports.useReturnValue = useReturnValue;
106
- /**
107
- * Since finding the trusted proxy based upon the remote address
108
- * is an expensive function, we cache its result
109
- */
110
- function trustProxy(remoteAddress, proxyFn) {
34
+ export function trustProxy(remoteAddress, proxyFn) {
111
35
  if (proxyCache.has(remoteAddress)) {
112
36
  return proxyCache.get(remoteAddress);
113
37
  }
@@ -115,82 +39,3 @@ function trustProxy(remoteAddress, proxyFn) {
115
39
  proxyCache.set(remoteAddress, result);
116
40
  return result;
117
41
  }
118
- exports.trustProxy = trustProxy;
119
- /**
120
- * Invokes a callback if any of the defined keys is part of the values object
121
- */
122
- function onIntersect(values, keys, callback) {
123
- if (Array.isArray(values)) {
124
- return;
125
- }
126
- const valueKeys = Object.keys(values);
127
- const matchingKey = keys.find((key) => valueKeys.includes(key));
128
- if (matchingKey) {
129
- callback(matchingKey);
130
- }
131
- }
132
- /**
133
- * Normalizes the makeURL options to work with the new API and the old
134
- * one as well
135
- */
136
- function normalizeMakeUrlOptions(params, options) {
137
- /**
138
- * Params used to be options earlier. So we are checking a few properties of it
139
- */
140
- params = params || {};
141
- options = options || {};
142
- const normalizedParams = params['params'] ? params['params'] : params;
143
- const qs = options.qs || params['qs'];
144
- const domain = options.domain;
145
- const prefixUrl = options.prefixUrl;
146
- const disableRouteLookup = options.disableRouteLookup || false;
147
- /**
148
- * Using legacy options
149
- */
150
- onIntersect(params, ['prefixDomain', 'domainParams', 'qs', 'params'], () => {
151
- process.emitWarning('DeprecationWarning', 'You are using legacy the API of the "Route.makeUrl". We recommend reading the docs and use the latest API');
152
- });
153
- return { params: normalizedParams, qs, domain, prefixUrl, disableRouteLookup };
154
- }
155
- exports.normalizeMakeUrlOptions = normalizeMakeUrlOptions;
156
- /**
157
- * Normalizes the make signed url options by allowing params to appear on
158
- * top level object with option to nest inside `params` property.
159
- */
160
- function normalizeMakeSignedUrlOptions(params, options) {
161
- /**
162
- * Params used to be options earlier. So we are checking a few properties of it
163
- */
164
- params = params || {};
165
- options = options || {};
166
- const normalizedParams = params['params'] ? params['params'] : params;
167
- const qs = options.qs || params['qs'];
168
- const expiresIn = options.expiresIn || params['expiresIn'];
169
- const purpose = options.purpose || params['purpose'];
170
- const domain = options.domain;
171
- const prefixUrl = options.prefixUrl;
172
- const disableRouteLookup = options.disableRouteLookup || false;
173
- /**
174
- * Using legacy options
175
- */
176
- onIntersect(params, ['prefixDomain', 'domainParams', 'qs', 'params', 'purpose', 'expiresIn'], () => {
177
- process.emitWarning('DeprecationWarning', 'You are using legacy the API of the "Route.makeSignedUrl". We recommend reading the docs and use the latest API');
178
- });
179
- return { params: normalizedParams, qs, domain, prefixUrl, expiresIn, purpose, disableRouteLookup };
180
- }
181
- exports.normalizeMakeSignedUrlOptions = normalizeMakeSignedUrlOptions;
182
- /**
183
- * Wraps `fs.stat` to promise interface.
184
- */
185
- function statFn(filePath) {
186
- return new Promise((resolve, reject) => {
187
- (0, fs_1.stat)(filePath, (error, stats) => {
188
- if (error) {
189
- reject(error);
190
- return;
191
- }
192
- resolve(stats);
193
- });
194
- });
195
- }
196
- exports.statFn = statFn;
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { AsyncLocalStorage } from 'node:async_hooks';
3
+ import type { HttpContext } from './main.js';
4
+ export declare const asyncLocalStorage: {
5
+ isEnabled: boolean;
6
+ storage: null | AsyncLocalStorage<HttpContext>;
7
+ create(): AsyncLocalStorage<HttpContext>;
8
+ destroy(): void;
9
+ };
@@ -0,0 +1,14 @@
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
+ export const asyncLocalStorage = {
3
+ isEnabled: false,
4
+ storage: null,
5
+ create() {
6
+ this.isEnabled = true;
7
+ this.storage = new AsyncLocalStorage();
8
+ return this.storage;
9
+ },
10
+ destroy() {
11
+ this.isEnabled = false;
12
+ this.storage = null;
13
+ },
14
+ };
@@ -0,0 +1,20 @@
1
+ import type { Logger } from '@adonisjs/logger';
2
+ import { Macroable } from '@poppinss/macroable';
3
+ import type { Request } from '../request.js';
4
+ import type { Response } from '../response.js';
5
+ import type { StoreRouteNode } from '../types/route.js';
6
+ export declare class HttpContext extends Macroable {
7
+ request: Request;
8
+ response: Response;
9
+ logger: Logger;
10
+ static get usingAsyncLocalStorage(): boolean;
11
+ static get(): HttpContext | null;
12
+ static getOrFail(): HttpContext;
13
+ static runOutsideContext<T>(callback: (...args: any[]) => T, ...args: any[]): T;
14
+ route?: StoreRouteNode;
15
+ routeKey?: string;
16
+ params: Record<string, any>;
17
+ subdomains: Record<string, any>;
18
+ constructor(request: Request, response: Response, logger: Logger);
19
+ inspect(): string;
20
+ }
@@ -0,0 +1,49 @@
1
+ import { inspect } from 'node:util';
2
+ import { Macroable } from '@poppinss/macroable';
3
+ import { RuntimeException } from '@poppinss/utils';
4
+ import { asyncLocalStorage } from './local_storage.js';
5
+ export class HttpContext extends Macroable {
6
+ request;
7
+ response;
8
+ logger;
9
+ static get usingAsyncLocalStorage() {
10
+ return asyncLocalStorage.isEnabled;
11
+ }
12
+ static get() {
13
+ if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
14
+ return null;
15
+ }
16
+ return asyncLocalStorage.storage.getStore() || null;
17
+ }
18
+ static getOrFail() {
19
+ if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
20
+ throw new RuntimeException('HTTP context is not available. Enable "useAsyncLocalStorage" inside "config/app.ts" file');
21
+ }
22
+ const store = this.get();
23
+ if (!store) {
24
+ throw new RuntimeException('Http context is not available outside of an HTTP request');
25
+ }
26
+ return store;
27
+ }
28
+ static runOutsideContext(callback, ...args) {
29
+ if (!asyncLocalStorage.storage) {
30
+ return callback(...args);
31
+ }
32
+ return asyncLocalStorage.storage.exit(callback, ...args);
33
+ }
34
+ route;
35
+ routeKey;
36
+ params = {};
37
+ subdomains = {};
38
+ constructor(request, response, logger) {
39
+ super();
40
+ this.request = request;
41
+ this.response = response;
42
+ this.logger = logger;
43
+ this.request.ctx = this;
44
+ this.response.ctx = this;
45
+ }
46
+ inspect() {
47
+ return inspect(this, false, 1, true);
48
+ }
49
+ }
@@ -0,0 +1,11 @@
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
+ }
@@ -0,0 +1,33 @@
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
+ }
@@ -0,0 +1,7 @@
1
+ import { QSParserConfig } from './types/qs.js';
2
+ export declare class Qs {
3
+ #private;
4
+ constructor(config: QSParserConfig);
5
+ parse(value: string): import("qs").ParsedQs;
6
+ stringify(value: any): string;
7
+ }
@@ -0,0 +1,13 @@
1
+ import { parse, stringify } from 'qs';
2
+ export class Qs {
3
+ #config;
4
+ constructor(config) {
5
+ this.#config = config;
6
+ }
7
+ parse(value) {
8
+ return parse(value, this.#config.parse);
9
+ }
10
+ stringify(value) {
11
+ return stringify(value, this.#config.stringify);
12
+ }
13
+ }
@@ -0,0 +1,18 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import type { IncomingMessage } from 'node:http';
3
+ import type { Qs } from './qs.js';
4
+ import type { Response } from './response.js';
5
+ import type { Router } from './router/main.js';
6
+ import type { MakeUrlOptions } from './types/route.js';
7
+ export declare class Redirect {
8
+ #private;
9
+ constructor(request: IncomingMessage, response: Response, router: Router, qs: Qs);
10
+ status(statusCode: number): this;
11
+ clearQs(): this;
12
+ withQs(): this;
13
+ withQs(values: Record<string, any>): this;
14
+ withQs(name: string, value: any): this;
15
+ back(): void;
16
+ toRoute(routeIdentifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): void;
17
+ toPath(url: string): void;
18
+ }
@@ -0,0 +1,80 @@
1
+ import { parse } from 'node:url';
2
+ import encodeUrl from 'encodeurl';
3
+ import debug from './debug.js';
4
+ export class Redirect {
5
+ #forwardQueryString = false;
6
+ #statusCode = 302;
7
+ #queryString = {};
8
+ #request;
9
+ #response;
10
+ #router;
11
+ #qs;
12
+ constructor(request, response, router, qs) {
13
+ this.#request = request;
14
+ this.#response = response;
15
+ this.#router = router;
16
+ this.#qs = qs;
17
+ }
18
+ #sendResponse(url, query) {
19
+ const stringified = this.#qs.stringify(query);
20
+ url = stringified ? `${url}?${stringified}` : url;
21
+ debug('redirecting to url "%s"', url);
22
+ this.#response.location(encodeUrl(url));
23
+ this.#response.safeStatus(this.#statusCode);
24
+ this.#response.type('text/plain; charset=utf-8');
25
+ this.#response.send(`Redirecting to ${url}`);
26
+ }
27
+ #getReferrerUrl() {
28
+ let url = this.#request.headers['referer'] || this.#request.headers['referrer'] || '/';
29
+ return Array.isArray(url) ? url[0] : url;
30
+ }
31
+ status(statusCode) {
32
+ this.#statusCode = statusCode;
33
+ return this;
34
+ }
35
+ clearQs() {
36
+ this.#forwardQueryString = false;
37
+ this.#queryString = {};
38
+ return this;
39
+ }
40
+ withQs(name, value) {
41
+ if (typeof name === 'undefined') {
42
+ this.#forwardQueryString = true;
43
+ return this;
44
+ }
45
+ if (typeof name === 'string') {
46
+ this.#queryString[name] = value;
47
+ return this;
48
+ }
49
+ Object.assign(this.#queryString, name);
50
+ return this;
51
+ }
52
+ back() {
53
+ let query = {};
54
+ const referrerUrl = this.#getReferrerUrl();
55
+ const url = parse(referrerUrl);
56
+ debug('referrer url "%s"', referrerUrl);
57
+ debug('referrer base url "%s"', url.pathname);
58
+ if (this.#forwardQueryString) {
59
+ query = this.#qs.parse(url.query || '');
60
+ }
61
+ Object.assign(query, this.#queryString);
62
+ this.#sendResponse(url.pathname || '', query);
63
+ }
64
+ toRoute(routeIdentifier, params, options) {
65
+ if (options && options.qs) {
66
+ this.withQs(options.qs);
67
+ options.qs = undefined;
68
+ }
69
+ const url = this.#router.makeUrl(routeIdentifier, params, options);
70
+ return this.toPath(url);
71
+ }
72
+ toPath(url) {
73
+ let query = {};
74
+ if (this.#forwardQueryString) {
75
+ query = this.#qs.parse(parse(this.#request.url).query || '');
76
+ }
77
+ Object.assign(query, this.#queryString);
78
+ this.#sendResponse(url, query);
79
+ }
80
+ }
@@ -0,0 +1,81 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ /// <reference types="node" resolution-mode="require"/>
3
+ import { Macroable } from '@poppinss/macroable';
4
+ import type Encryption from '@adonisjs/encryption';
5
+ import { UrlWithStringQuery } from 'node:url';
6
+ import { ServerResponse, IncomingMessage, IncomingHttpHeaders } from 'node:http';
7
+ import type { Qs } from './qs.js';
8
+ import { RequestConfig } from './types/request.js';
9
+ import type { HttpContext } from './http_context/main.js';
10
+ export declare class Request extends Macroable {
11
+ #private;
12
+ request: IncomingMessage;
13
+ response: ServerResponse;
14
+ parsedUrl: UrlWithStringQuery;
15
+ ctx?: HttpContext;
16
+ constructor(request: IncomingMessage, response: ServerResponse, encryption: Encryption, config: RequestConfig, qsParser: Qs);
17
+ id(): string | undefined;
18
+ setInitialBody(body: Record<string, any>): void;
19
+ updateBody(body: Record<string, any>): void;
20
+ updateRawBody(rawBody: string): void;
21
+ updateQs(data: Record<string, any>): void;
22
+ params(): Record<string, any>;
23
+ qs(): Record<string, any>;
24
+ body(): Record<string, any>;
25
+ all(): Record<string, any>;
26
+ original(): Record<string, any>;
27
+ raw(): string | null;
28
+ input(key: string, defaultValue?: any): any;
29
+ param(key: string, defaultValue?: any): any;
30
+ except(keys: string[]): Record<string, any>;
31
+ only<T extends string>(keys: T[]): {
32
+ [K in T]: any;
33
+ };
34
+ intended(): string;
35
+ method(): string;
36
+ headers(): IncomingHttpHeaders;
37
+ header(key: string, defaultValue?: any): string | undefined;
38
+ ip(): string;
39
+ ips(): string[];
40
+ protocol(): string;
41
+ secure(): boolean;
42
+ host(): string | null;
43
+ hostname(): string | null;
44
+ subdomains(): string[];
45
+ ajax(): boolean;
46
+ pjax(): boolean;
47
+ url(includeQueryString?: boolean): string;
48
+ completeUrl(includeQueryString?: boolean): string;
49
+ matchesRoute(routeIdentifier: string | string[]): boolean;
50
+ is(types: string[]): string | null;
51
+ accepts<T extends string>(types: T[]): T | null;
52
+ types(): string[];
53
+ language<T extends string>(languages: T[]): T | null;
54
+ languages(): string[];
55
+ charset<T extends string>(charsets: T[]): T | null;
56
+ charsets(): string[];
57
+ encoding<T extends string>(encodings: T[]): T | null;
58
+ encodings(): string[];
59
+ hasBody(): boolean;
60
+ fresh(): boolean;
61
+ stale(): boolean;
62
+ cookiesList(): Record<string, any>;
63
+ cookie(key: string, defaultValue?: string): any;
64
+ encryptedCookie(key: string, defaultValue?: string): any;
65
+ plainCookie(key: string, defaultValue?: string, encoded?: boolean): any;
66
+ hasValidSignature(purpose?: string): boolean;
67
+ toJSON(): {
68
+ id: string | undefined;
69
+ url: string;
70
+ query: string | null;
71
+ body: Record<string, any>;
72
+ params: Record<string, any>;
73
+ headers: IncomingHttpHeaders;
74
+ method: string;
75
+ protocol: string;
76
+ cookies: Record<string, any>;
77
+ hostname: string | null;
78
+ ip: string;
79
+ subdomains: Record<string, any>;
80
+ };
81
+ }