@adonisjs/http-server 5.12.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 +88 -86
  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 -440
  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 -46
  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 -414
  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 -143
  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
@@ -0,0 +1,587 @@
1
+ import etag from 'etag';
2
+ import vary from 'vary';
3
+ import fresh from 'fresh';
4
+ import mime from 'mime-types';
5
+ import destroy from 'destroy';
6
+ import { extname } from 'node:path';
7
+ import onFinished from 'on-finished';
8
+ import { promisify } from 'node:util';
9
+ import json from '@poppinss/utils/json';
10
+ import { Macroable } from '@poppinss/macroable';
11
+ import { createReadStream, stat } from 'node:fs';
12
+ import { RuntimeException } from '@poppinss/utils';
13
+ import contentDisposition from 'content-disposition';
14
+ import { Redirect } from './redirect.js';
15
+ import { CookieSerializer } from './cookies/serializer.js';
16
+ import { AbortException } from './exceptions/abort_exception.js';
17
+ const statFn = promisify(stat);
18
+ const CACHEABLE_HTTP_METHODS = ['GET', 'HEAD'];
19
+ export class Response extends Macroable {
20
+ request;
21
+ response;
22
+ #qs;
23
+ #headers = {};
24
+ #hasExplicitStatus = false;
25
+ #cookieSerializer;
26
+ #router;
27
+ #config;
28
+ get hasLazyBody() {
29
+ return !!(this.lazyBody.content || this.lazyBody.fileToStream || this.lazyBody.stream);
30
+ }
31
+ get isStreamResponse() {
32
+ return !!(this.lazyBody.stream || this.lazyBody.fileToStream);
33
+ }
34
+ lazyBody = {};
35
+ ctx;
36
+ constructor(request, response, encryption, config, router, qs) {
37
+ super();
38
+ this.request = request;
39
+ this.response = response;
40
+ this.#qs = qs;
41
+ this.#config = config;
42
+ this.#router = router;
43
+ this.#cookieSerializer = new CookieSerializer(encryption);
44
+ }
45
+ get finished() {
46
+ return this.response.writableFinished;
47
+ }
48
+ get headersSent() {
49
+ return this.response.headersSent;
50
+ }
51
+ get isPending() {
52
+ return !this.headersSent && !this.finished;
53
+ }
54
+ #castHeaderValue(value) {
55
+ return Array.isArray(value) ? value.map(String) : String(value);
56
+ }
57
+ #endResponse(body, statusCode) {
58
+ this.flushHeaders(statusCode);
59
+ const res = this.response;
60
+ res.end(body, null, null);
61
+ }
62
+ #getDataType(content) {
63
+ if (Buffer.isBuffer(content)) {
64
+ return 'buffer';
65
+ }
66
+ if (content instanceof Date) {
67
+ return 'date';
68
+ }
69
+ if (content instanceof RegExp) {
70
+ return 'regexp';
71
+ }
72
+ const dataType = typeof content;
73
+ if (dataType === 'number' ||
74
+ dataType === 'boolean' ||
75
+ dataType === 'string' ||
76
+ dataType === 'bigint') {
77
+ return dataType;
78
+ }
79
+ if (dataType === 'object') {
80
+ return 'object';
81
+ }
82
+ throw new RuntimeException(`Cannot serialize "${dataType}" to HTTP response`);
83
+ }
84
+ writeBody(content, generateEtag, jsonpCallbackName) {
85
+ const hasEmptyBody = content === null || content === undefined || content === '';
86
+ if (hasEmptyBody) {
87
+ this.safeStatus(204);
88
+ }
89
+ const statusCode = this.response.statusCode;
90
+ if (statusCode && (statusCode < 200 || statusCode === 204 || statusCode === 304)) {
91
+ this.removeHeader('Content-Type');
92
+ this.removeHeader('Content-Length');
93
+ this.removeHeader('Transfer-Encoding');
94
+ this.#endResponse();
95
+ return;
96
+ }
97
+ if (hasEmptyBody) {
98
+ this.removeHeader('Content-Length');
99
+ this.#endResponse();
100
+ return;
101
+ }
102
+ const dataType = this.#getDataType(content);
103
+ if (dataType === 'object') {
104
+ content = json.safeStringify(content);
105
+ }
106
+ else if (dataType === 'number' ||
107
+ dataType === 'boolean' ||
108
+ dataType === 'bigint' ||
109
+ dataType === 'regexp') {
110
+ content = String(content);
111
+ }
112
+ else if (dataType === 'date') {
113
+ content = content.toISOString();
114
+ }
115
+ if (jsonpCallbackName) {
116
+ content = content.replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029');
117
+ content = `/**/ typeof ${jsonpCallbackName} === 'function' && ${jsonpCallbackName}(${content});`;
118
+ }
119
+ if (generateEtag) {
120
+ this.setEtag(content);
121
+ }
122
+ if (generateEtag && this.fresh()) {
123
+ this.removeHeader('Content-Type');
124
+ this.removeHeader('Content-Length');
125
+ this.removeHeader('Transfer-Encoding');
126
+ this.#endResponse(null, 304);
127
+ return;
128
+ }
129
+ this.header('Content-Length', Buffer.byteLength(content));
130
+ if (jsonpCallbackName) {
131
+ this.header('X-Content-Type-Options', 'nosniff');
132
+ this.safeHeader('Content-Type', 'text/javascript; charset=utf-8');
133
+ }
134
+ else {
135
+ switch (dataType) {
136
+ case 'string':
137
+ const type = /^\s*</.test(content) ? 'text/html' : 'text/plain';
138
+ this.safeHeader('Content-Type', `${type}; charset=utf-8`);
139
+ break;
140
+ case 'number':
141
+ case 'boolean':
142
+ case 'date':
143
+ case 'bigint':
144
+ case 'regexp':
145
+ this.safeHeader('Content-Type', 'text/plain; charset=utf-8');
146
+ break;
147
+ case 'buffer':
148
+ this.safeHeader('Content-Type', 'application/octet-stream; charset=utf-8');
149
+ break;
150
+ case 'object':
151
+ this.safeHeader('Content-Type', 'application/json; charset=utf-8');
152
+ break;
153
+ }
154
+ }
155
+ this.#endResponse(content);
156
+ }
157
+ streamBody(body, errorCallback) {
158
+ return new Promise((resolve) => {
159
+ let finished = false;
160
+ body.on('error', (error) => {
161
+ if (finished) {
162
+ return;
163
+ }
164
+ finished = true;
165
+ destroy(body);
166
+ this.type('text');
167
+ if (typeof errorCallback === 'function') {
168
+ this.#endResponse(...errorCallback(error));
169
+ }
170
+ else {
171
+ this.#endResponse(error.code === 'ENOENT' ? 'File not found' : 'Cannot process file', error.code === 'ENOENT' ? 404 : 500);
172
+ resolve();
173
+ }
174
+ });
175
+ body.on('end', resolve);
176
+ onFinished(this.response, () => {
177
+ finished = true;
178
+ destroy(body);
179
+ });
180
+ this.flushHeaders();
181
+ body.pipe(this.response);
182
+ });
183
+ }
184
+ async streamFileForDownload(filePath, generateEtag, errorCallback) {
185
+ try {
186
+ const stats = await statFn(filePath);
187
+ if (!stats || !stats.isFile()) {
188
+ throw new TypeError('response.download only accepts path to a file');
189
+ }
190
+ this.header('Last-Modified', stats.mtime.toUTCString());
191
+ this.type(extname(filePath));
192
+ if (generateEtag) {
193
+ this.setEtag(stats, true);
194
+ }
195
+ if (this.request.method === 'HEAD') {
196
+ this.#endResponse(null, generateEtag && this.fresh() ? 304 : 200);
197
+ return;
198
+ }
199
+ if (generateEtag && this.fresh()) {
200
+ this.#endResponse(null, 304);
201
+ return;
202
+ }
203
+ this.header('Content-length', stats.size);
204
+ return this.streamBody(createReadStream(filePath), errorCallback);
205
+ }
206
+ catch (error) {
207
+ this.type('text');
208
+ this.removeHeader('Etag');
209
+ if (typeof errorCallback === 'function') {
210
+ this.#endResponse(...errorCallback(error));
211
+ }
212
+ else {
213
+ this.#endResponse(error.code === 'ENOENT' ? 'File not found' : 'Cannot process file', error.code === 'ENOENT' ? 404 : 500);
214
+ }
215
+ }
216
+ }
217
+ flushHeaders(statusCode) {
218
+ this.response.writeHead(statusCode || this.response.statusCode, this.#headers);
219
+ return this;
220
+ }
221
+ getHeader(key) {
222
+ const value = this.#headers[key.toLowerCase()];
223
+ return value === undefined ? this.response.getHeader(key) : value;
224
+ }
225
+ getHeaders() {
226
+ return {
227
+ ...this.response.getHeaders(),
228
+ ...this.#headers,
229
+ };
230
+ }
231
+ header(key, value) {
232
+ if (value === null || value === undefined) {
233
+ return this;
234
+ }
235
+ this.#headers[key.toLowerCase()] = this.#castHeaderValue(value);
236
+ return this;
237
+ }
238
+ append(key, value) {
239
+ if (value === null || value === undefined) {
240
+ return this;
241
+ }
242
+ key = key.toLowerCase();
243
+ let existingHeader = this.getHeader(key);
244
+ let casted = this.#castHeaderValue(value);
245
+ if (!existingHeader) {
246
+ this.#headers[key] = casted;
247
+ return this;
248
+ }
249
+ existingHeader = this.#castHeaderValue(existingHeader);
250
+ casted = Array.isArray(existingHeader)
251
+ ? existingHeader.concat(casted)
252
+ : [existingHeader].concat(casted);
253
+ this.#headers[key] = casted;
254
+ return this;
255
+ }
256
+ safeHeader(key, value) {
257
+ if (!this.getHeader(key)) {
258
+ this.header(key, value);
259
+ }
260
+ return this;
261
+ }
262
+ removeHeader(key) {
263
+ key = key.toLowerCase();
264
+ if (this.#headers[key]) {
265
+ delete this.#headers[key.toLowerCase()];
266
+ }
267
+ return this;
268
+ }
269
+ getStatus() {
270
+ return this.response.statusCode;
271
+ }
272
+ status(code) {
273
+ this.#hasExplicitStatus = true;
274
+ this.response.statusCode = code;
275
+ return this;
276
+ }
277
+ safeStatus(code) {
278
+ if (this.#hasExplicitStatus) {
279
+ return this;
280
+ }
281
+ this.response.statusCode = code;
282
+ return this;
283
+ }
284
+ type(type, charset) {
285
+ type = charset ? `${type}; charset=${charset}` : type;
286
+ this.header('Content-Type', mime.contentType(type));
287
+ return this;
288
+ }
289
+ vary(field) {
290
+ vary(this.response, field);
291
+ return this;
292
+ }
293
+ setEtag(body, weak = false) {
294
+ this.header('Etag', etag(body, { weak }));
295
+ return this;
296
+ }
297
+ fresh() {
298
+ if (this.request.method && !CACHEABLE_HTTP_METHODS.includes(this.request.method)) {
299
+ return false;
300
+ }
301
+ const status = this.response.statusCode;
302
+ if ((status >= 200 && status < 300) || status === 304) {
303
+ return fresh(this.request.headers, this.#headers);
304
+ }
305
+ return false;
306
+ }
307
+ getBody() {
308
+ if (this.lazyBody.content) {
309
+ return this.lazyBody.content[0];
310
+ }
311
+ return null;
312
+ }
313
+ send(body, generateEtag = this.#config.etag) {
314
+ this.lazyBody.content = [body, generateEtag];
315
+ }
316
+ json(body, generateEtag = this.#config.etag) {
317
+ return this.send(body, generateEtag);
318
+ }
319
+ jsonp(body, callbackName = this.#config.jsonpCallbackName, generateEtag = this.#config.etag) {
320
+ this.lazyBody.content = [body, generateEtag, callbackName];
321
+ }
322
+ stream(body, errorCallback) {
323
+ if (typeof body.pipe !== 'function' || !body.readable || typeof body.read !== 'function') {
324
+ throw new TypeError('response.stream accepts a readable stream only');
325
+ }
326
+ this.lazyBody.stream = [body, errorCallback];
327
+ }
328
+ download(filePath, generateEtag = this.#config.etag, errorCallback) {
329
+ this.lazyBody.fileToStream = [filePath, generateEtag, errorCallback];
330
+ }
331
+ attachment(filePath, name, disposition, generateEtag, errorCallback) {
332
+ name = name || filePath;
333
+ this.header('Content-Disposition', contentDisposition(name, { type: disposition }));
334
+ return this.download(filePath, generateEtag, errorCallback);
335
+ }
336
+ location(url) {
337
+ this.header('Location', url);
338
+ return this;
339
+ }
340
+ redirect(path, forwardQueryString = false, statusCode = 302) {
341
+ const handler = new Redirect(this.request, this, this.#router, this.#qs);
342
+ if (forwardQueryString) {
343
+ handler.withQs();
344
+ }
345
+ if (path === 'back') {
346
+ return handler.status(statusCode).back();
347
+ }
348
+ if (path) {
349
+ return handler.status(statusCode).toPath(path);
350
+ }
351
+ return handler;
352
+ }
353
+ abort(body, status) {
354
+ throw AbortException.invoke(body, status || 400);
355
+ }
356
+ abortIf(condition, body, status) {
357
+ if (condition) {
358
+ this.abort(body, status);
359
+ }
360
+ }
361
+ abortUnless(condition, body, status) {
362
+ if (!condition) {
363
+ this.abort(body, status);
364
+ }
365
+ }
366
+ cookie(key, value, options) {
367
+ options = Object.assign({}, this.#config.cookie, options);
368
+ const serialized = this.#cookieSerializer.sign(key, value, options);
369
+ if (!serialized) {
370
+ return this;
371
+ }
372
+ this.append('set-cookie', serialized);
373
+ return this;
374
+ }
375
+ encryptedCookie(key, value, options) {
376
+ options = Object.assign({}, this.#config.cookie, options);
377
+ const serialized = this.#cookieSerializer.encrypt(key, value, options);
378
+ if (!serialized) {
379
+ return this;
380
+ }
381
+ this.append('set-cookie', serialized);
382
+ return this;
383
+ }
384
+ plainCookie(key, value, options) {
385
+ options = Object.assign({}, this.#config.cookie, options);
386
+ const serialized = this.#cookieSerializer.encode(key, value, options);
387
+ if (!serialized) {
388
+ return this;
389
+ }
390
+ this.append('set-cookie', serialized);
391
+ return this;
392
+ }
393
+ clearCookie(key, options) {
394
+ options = Object.assign({}, this.#config.cookie, options);
395
+ options.expires = new Date(1);
396
+ options.maxAge = -1;
397
+ const serialized = this.#cookieSerializer.encode(key, '', { ...options, encode: false });
398
+ this.append('set-cookie', serialized);
399
+ return this;
400
+ }
401
+ finish() {
402
+ if (!this.isPending) {
403
+ return;
404
+ }
405
+ if (this.lazyBody.content) {
406
+ this.writeBody(...this.lazyBody.content);
407
+ return;
408
+ }
409
+ if (this.lazyBody.stream) {
410
+ this.streamBody(...this.lazyBody.stream);
411
+ return;
412
+ }
413
+ if (this.lazyBody.fileToStream) {
414
+ this.streamFileForDownload(...this.lazyBody.fileToStream);
415
+ return;
416
+ }
417
+ this.#endResponse();
418
+ }
419
+ continue() {
420
+ this.status(100);
421
+ return this.send(null, false);
422
+ }
423
+ switchingProtocols() {
424
+ this.status(101);
425
+ return this.send(null, false);
426
+ }
427
+ ok(body, generateEtag) {
428
+ this.status(200);
429
+ return this.send(body, generateEtag);
430
+ }
431
+ created(body, generateEtag) {
432
+ this.status(201);
433
+ return this.send(body, generateEtag);
434
+ }
435
+ accepted(body, generateEtag) {
436
+ this.status(202);
437
+ return this.send(body, generateEtag);
438
+ }
439
+ nonAuthoritativeInformation(body, generateEtag) {
440
+ this.status(203);
441
+ return this.send(body, generateEtag);
442
+ }
443
+ noContent() {
444
+ this.status(204);
445
+ return this.send(null, false);
446
+ }
447
+ resetContent() {
448
+ this.status(205);
449
+ return this.send(null, false);
450
+ }
451
+ partialContent(body, generateEtag) {
452
+ this.status(206);
453
+ return this.send(body, generateEtag);
454
+ }
455
+ multipleChoices(body, generateEtag) {
456
+ this.status(300);
457
+ return this.send(body, generateEtag);
458
+ }
459
+ movedPermanently(body, generateEtag) {
460
+ this.status(301);
461
+ return this.send(body, generateEtag);
462
+ }
463
+ movedTemporarily(body, generateEtag) {
464
+ this.status(302);
465
+ return this.send(body, generateEtag);
466
+ }
467
+ seeOther(body, generateEtag) {
468
+ this.status(303);
469
+ return this.send(body, generateEtag);
470
+ }
471
+ notModified(body, generateEtag) {
472
+ this.status(304);
473
+ return this.send(body, generateEtag);
474
+ }
475
+ useProxy(body, generateEtag) {
476
+ this.status(305);
477
+ return this.send(body, generateEtag);
478
+ }
479
+ temporaryRedirect(body, generateEtag) {
480
+ this.status(307);
481
+ return this.send(body, generateEtag);
482
+ }
483
+ badRequest(body, generateEtag) {
484
+ this.status(400);
485
+ return this.send(body, generateEtag);
486
+ }
487
+ unauthorized(body, generateEtag) {
488
+ this.status(401);
489
+ return this.send(body, generateEtag);
490
+ }
491
+ paymentRequired(body, generateEtag) {
492
+ this.status(402);
493
+ return this.send(body, generateEtag);
494
+ }
495
+ forbidden(body, generateEtag) {
496
+ this.status(403);
497
+ return this.send(body, generateEtag);
498
+ }
499
+ notFound(body, generateEtag) {
500
+ this.status(404);
501
+ return this.send(body, generateEtag);
502
+ }
503
+ methodNotAllowed(body, generateEtag) {
504
+ this.status(405);
505
+ return this.send(body, generateEtag);
506
+ }
507
+ notAcceptable(body, generateEtag) {
508
+ this.status(406);
509
+ return this.send(body, generateEtag);
510
+ }
511
+ proxyAuthenticationRequired(body, generateEtag) {
512
+ this.status(407);
513
+ return this.send(body, generateEtag);
514
+ }
515
+ requestTimeout(body, generateEtag) {
516
+ this.status(408);
517
+ return this.send(body, generateEtag);
518
+ }
519
+ conflict(body, generateEtag) {
520
+ this.status(409);
521
+ return this.send(body, generateEtag);
522
+ }
523
+ gone(body, generateEtag) {
524
+ this.status(410);
525
+ return this.send(body, generateEtag);
526
+ }
527
+ lengthRequired(body, generateEtag) {
528
+ this.status(411);
529
+ return this.send(body, generateEtag);
530
+ }
531
+ preconditionFailed(body, generateEtag) {
532
+ this.status(412);
533
+ return this.send(body, generateEtag);
534
+ }
535
+ requestEntityTooLarge(body, generateEtag) {
536
+ this.status(413);
537
+ return this.send(body, generateEtag);
538
+ }
539
+ requestUriTooLong(body, generateEtag) {
540
+ this.status(414);
541
+ return this.send(body, generateEtag);
542
+ }
543
+ unsupportedMediaType(body, generateEtag) {
544
+ this.status(415);
545
+ return this.send(body, generateEtag);
546
+ }
547
+ requestedRangeNotSatisfiable(body, generateEtag) {
548
+ this.status(416);
549
+ return this.send(body, generateEtag);
550
+ }
551
+ expectationFailed(body, generateEtag) {
552
+ this.status(417);
553
+ return this.send(body, generateEtag);
554
+ }
555
+ unprocessableEntity(body, generateEtag) {
556
+ this.status(422);
557
+ return this.send(body, generateEtag);
558
+ }
559
+ tooManyRequests(body, generateEtag) {
560
+ this.status(429);
561
+ return this.send(body, generateEtag);
562
+ }
563
+ internalServerError(body, generateEtag) {
564
+ this.status(500);
565
+ return this.send(body, generateEtag);
566
+ }
567
+ notImplemented(body, generateEtag) {
568
+ this.status(501);
569
+ return this.send(body, generateEtag);
570
+ }
571
+ badGateway(body, generateEtag) {
572
+ this.status(502);
573
+ return this.send(body, generateEtag);
574
+ }
575
+ serviceUnavailable(body, generateEtag) {
576
+ this.status(503);
577
+ return this.send(body, generateEtag);
578
+ }
579
+ gatewayTimeout(body, generateEtag) {
580
+ this.status(504);
581
+ return this.send(body, generateEtag);
582
+ }
583
+ httpVersionNotSupported(body, generateEtag) {
584
+ this.status(505);
585
+ return this.send(body, generateEtag);
586
+ }
587
+ }
@@ -0,0 +1,18 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ import type { Application } from '@adonisjs/application';
3
+ import { Route } from './route.js';
4
+ import type { LazyImport } from '../types/base.js';
5
+ import type { MiddlewareStore } from '../middleware/store.js';
6
+ import type { MiddlewareAsClass } from '../types/middleware.js';
7
+ import type { MakeUrlOptions, RouteFn, RouteMatchers } from '../types/route.js';
8
+ export declare class BriskRoute<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any> extends Macroable {
9
+ #private;
10
+ route: null | Route<NamedMiddleware>;
11
+ constructor(app: Application, middlewareStore: MiddlewareStore<NamedMiddleware>, options: {
12
+ pattern: string;
13
+ globalMatchers: RouteMatchers;
14
+ });
15
+ setHandler(handler: RouteFn): Route;
16
+ redirect(identifier: string, params?: any[] | Record<string, any>, options?: MakeUrlOptions): Route;
17
+ redirectToPath(url: string): Route;
18
+ }
@@ -0,0 +1,35 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ import { Route } from './route.js';
3
+ export class BriskRoute extends Macroable {
4
+ #pattern;
5
+ #globalMatchers;
6
+ #app;
7
+ #middlewareStore;
8
+ route = null;
9
+ constructor(app, middlewareStore, options) {
10
+ super();
11
+ this.#app = app;
12
+ this.#middlewareStore = middlewareStore;
13
+ this.#pattern = options.pattern;
14
+ this.#globalMatchers = options.globalMatchers;
15
+ }
16
+ setHandler(handler) {
17
+ this.route = new Route(this.#app, this.#middlewareStore, {
18
+ pattern: this.#pattern,
19
+ globalMatchers: this.#globalMatchers,
20
+ methods: ['GET', 'HEAD'],
21
+ handler: handler,
22
+ });
23
+ return this.route;
24
+ }
25
+ redirect(identifier, params, options) {
26
+ return this.setHandler(async (ctx) => {
27
+ return ctx.response.redirect().toRoute(identifier, params || ctx.params, options);
28
+ });
29
+ }
30
+ redirectToPath(url) {
31
+ return this.setHandler(async (ctx) => {
32
+ return ctx.response.redirect().toPath(url);
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,4 @@
1
+ import type { ContainerResolver } from '@adonisjs/fold';
2
+ import type { StoreRouteNode } from '../types/route.js';
3
+ import type { HttpContext } from '../http_context/main.js';
4
+ export declare function execute(route: StoreRouteNode, resolver: ContainerResolver, ctx: HttpContext): Promise<void>;
@@ -0,0 +1,16 @@
1
+ export function execute(route, resolver, ctx) {
2
+ return route.middleware
3
+ .runner()
4
+ .finalHandler(() => {
5
+ if (typeof route.handler === 'function') {
6
+ return route.handler(ctx);
7
+ }
8
+ return route.handler.handle(resolver, ctx);
9
+ })
10
+ .run((middleware, next) => {
11
+ if (typeof middleware === 'function') {
12
+ return middleware(ctx, next);
13
+ }
14
+ return middleware.handle(resolver, ctx, next, middleware.args);
15
+ });
16
+ }
@@ -0,0 +1,19 @@
1
+ import { Macroable } from '@poppinss/macroable';
2
+ import type { MiddlewareStore } from '../middleware/store.js';
3
+ import type { LazyImport, UnWrapLazyImport } from '../types/base.js';
4
+ import type { RouteMatcher } from '../types/route.js';
5
+ import type { GetMiddlewareArgs, MiddlewareAsClass, MiddlewareFn } from '../types/middleware.js';
6
+ import { Route } from './route.js';
7
+ import { BriskRoute } from './brisk.js';
8
+ import { RouteResource } from './resource.js';
9
+ export declare class RouteGroup<NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any> extends Macroable {
10
+ #private;
11
+ routes: (Route | RouteGroup | RouteResource | BriskRoute)[];
12
+ constructor(routes: (Route | RouteGroup | RouteResource | BriskRoute)[], middlewareStore: MiddlewareStore<NamedMiddleware>);
13
+ where(param: string, matcher: RouteMatcher | string | RegExp): this;
14
+ prefix(prefix: string): this;
15
+ domain(domain: string): this;
16
+ as(name: string): this;
17
+ middleware<Name extends keyof NamedMiddleware>(middleware: Name, ...args: GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[Name]>>): this;
18
+ middleware(middleware: MiddlewareFn): this;
19
+ }