@adonisjs/http-server 7.0.0-0 → 7.0.0-1
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/factories/http_context.d.ts +25 -0
- package/build/factories/http_context.js +51 -0
- package/build/factories/http_server.d.ts +8 -0
- package/build/factories/http_server.js +26 -0
- package/build/factories/main.d.ts +6 -149
- package/build/factories/main.js +14 -331
- package/build/factories/qs_parser_factory.d.ts +20 -0
- package/build/factories/qs_parser_factory.js +44 -0
- package/build/factories/request.d.ts +29 -0
- package/build/factories/request.js +73 -0
- package/build/factories/response.d.ts +29 -0
- package/build/factories/response.js +77 -0
- package/build/factories/router.d.ts +23 -0
- package/build/factories/router.js +45 -0
- package/build/factories/server_factory.d.ts +29 -0
- package/build/factories/server_factory.js +65 -0
- package/build/index.d.ts +14 -272
- package/build/index.js +22 -308
- package/build/src/cookies/client.d.ts +37 -0
- package/build/src/cookies/client.js +84 -0
- package/build/src/cookies/drivers/encrypted.d.ts +16 -0
- package/build/src/cookies/drivers/encrypted.js +36 -0
- package/build/src/cookies/drivers/plain.d.ts +15 -0
- package/build/src/cookies/drivers/plain.js +33 -0
- package/build/src/cookies/drivers/signed.d.ts +16 -0
- package/build/src/cookies/drivers/signed.js +36 -0
- package/build/src/cookies/parser.d.ts +37 -0
- package/build/src/cookies/parser.js +167 -0
- package/build/src/cookies/serializer.d.ts +33 -0
- package/build/src/cookies/serializer.js +79 -0
- package/build/src/debug.d.ts +3 -0
- package/build/src/debug.js +10 -0
- package/build/src/define_config.d.ts +9 -0
- package/build/src/define_config.js +68 -0
- package/build/src/define_middleware.d.ts +11 -0
- package/build/src/define_middleware.js +35 -0
- package/build/src/exception_handler.d.ts +113 -0
- package/build/src/exception_handler.js +306 -0
- package/build/src/exceptions.d.ts +84 -0
- package/build/src/exceptions.js +38 -0
- package/build/src/helpers.d.ts +23 -0
- package/build/src/helpers.js +105 -0
- package/build/src/http_context/local_storage.d.ts +12 -0
- package/build/src/http_context/local_storage.js +39 -0
- package/build/src/http_context/main.d.ts +58 -0
- package/build/src/http_context/main.js +105 -0
- package/build/src/qs.d.ts +11 -0
- package/build/src/qs.js +25 -0
- package/build/src/redirect.d.ts +42 -0
- package/build/src/redirect.js +140 -0
- package/build/src/request.d.ts +565 -0
- package/build/src/request.js +865 -0
- package/build/src/response.d.ts +620 -0
- package/build/src/response.js +1208 -0
- package/build/src/router/brisk.d.ts +42 -0
- package/build/src/router/brisk.js +85 -0
- package/build/src/router/executor.d.ts +9 -0
- package/build/src/router/executor.js +30 -0
- package/build/src/router/factories/use_return_value.d.ts +6 -0
- package/build/src/router/factories/use_return_value.js +22 -0
- package/build/src/router/group.d.ts +65 -0
- package/build/src/router/group.js +207 -0
- package/build/src/router/lookup_store/main.d.ts +49 -0
- package/build/src/router/lookup_store/main.js +86 -0
- package/build/src/router/lookup_store/route_finder.d.ts +21 -0
- package/build/src/router/lookup_store/route_finder.js +49 -0
- package/build/src/router/lookup_store/url_builder.d.ts +52 -0
- package/build/src/router/lookup_store/url_builder.js +209 -0
- package/build/src/router/main.d.ts +128 -0
- package/build/src/router/main.js +316 -0
- package/build/src/router/matchers.d.ts +27 -0
- package/build/src/router/matchers.js +36 -0
- package/build/src/router/parser.d.ts +5 -0
- package/build/src/router/parser.js +17 -0
- package/build/src/router/resource.d.ts +54 -0
- package/build/src/router/resource.js +216 -0
- package/build/src/router/route.d.ts +92 -0
- package/build/src/router/route.js +293 -0
- package/build/src/router/store.d.ts +66 -0
- package/build/src/router/store.js +195 -0
- package/build/src/server/factories/final_handler.d.ts +10 -0
- package/build/src/server/factories/final_handler.js +30 -0
- package/build/src/server/factories/middleware_handler.d.ts +8 -0
- package/build/src/server/factories/middleware_handler.js +16 -0
- package/build/src/server/factories/write_response.d.ts +6 -0
- package/build/src/server/factories/write_response.js +24 -0
- package/build/{main-29eaaee4.d.ts → src/server/main.d.ts} +18 -13
- package/build/src/server/main.js +292 -0
- package/build/src/types/base.d.ts +19 -0
- package/build/src/types/base.js +9 -0
- package/build/src/types/main.d.ts +7 -14
- package/build/src/types/main.js +15 -0
- package/build/src/types/middleware.d.ts +35 -0
- package/build/src/types/middleware.js +9 -0
- package/build/src/types/qs.d.ts +68 -0
- package/build/src/types/qs.js +9 -0
- package/build/src/types/request.d.ts +39 -0
- package/build/src/types/request.js +9 -0
- package/build/src/types/response.d.ts +45 -0
- package/build/src/types/response.js +9 -0
- package/build/src/types/route.d.ts +166 -0
- package/build/src/types/route.js +9 -0
- package/build/src/types/server.d.ts +72 -0
- package/build/src/types/server.js +9 -0
- package/package.json +6 -3
- package/build/chunk-XX72ATFY.js +0 -4388
- package/build/main-e5b46c83.d.ts +0 -2210
package/build/index.js
CHANGED
|
@@ -1,308 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
} from
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
import is from "@sindresorhus/is";
|
|
24
|
-
import Macroable from "@poppinss/macroable";
|
|
25
|
-
var ExceptionHandler = class extends Macroable {
|
|
26
|
-
/**
|
|
27
|
-
* Computed from the status pages property
|
|
28
|
-
*/
|
|
29
|
-
#expandedStatusPages;
|
|
30
|
-
/**
|
|
31
|
-
* Whether or not to render debug info. When set to true, the errors
|
|
32
|
-
* will have the complete error stack.
|
|
33
|
-
*/
|
|
34
|
-
debug = process.env.NODE_ENV !== "production";
|
|
35
|
-
/**
|
|
36
|
-
* Whether or not to render status pages. When set to true, the unhandled
|
|
37
|
-
* errors with matching status codes will be rendered using a status
|
|
38
|
-
* page.
|
|
39
|
-
*/
|
|
40
|
-
renderStatusPages = process.env.NODE_ENV === "production";
|
|
41
|
-
/**
|
|
42
|
-
* A collection of error status code range and the view to render.
|
|
43
|
-
*/
|
|
44
|
-
statusPages = {};
|
|
45
|
-
/**
|
|
46
|
-
* Enable/disable errors reporting
|
|
47
|
-
*/
|
|
48
|
-
reportErrors = true;
|
|
49
|
-
/**
|
|
50
|
-
* An array of exception classes to ignore when
|
|
51
|
-
* reporting an error
|
|
52
|
-
*/
|
|
53
|
-
ignoreExceptions = [
|
|
54
|
-
E_HTTP_EXCEPTION,
|
|
55
|
-
E_ROUTE_NOT_FOUND,
|
|
56
|
-
E_CANNOT_LOOKUP_ROUTE,
|
|
57
|
-
E_HTTP_REQUEST_ABORTED
|
|
58
|
-
];
|
|
59
|
-
/**
|
|
60
|
-
* An array of HTTP status codes to ignore when reporting
|
|
61
|
-
* an error
|
|
62
|
-
*/
|
|
63
|
-
ignoreStatuses = [400, 422, 401];
|
|
64
|
-
/**
|
|
65
|
-
* An array of error codes to ignore when reporting
|
|
66
|
-
* an error
|
|
67
|
-
*/
|
|
68
|
-
ignoreCodes = [];
|
|
69
|
-
/**
|
|
70
|
-
* Expands status pages
|
|
71
|
-
*/
|
|
72
|
-
#expandStatusPages() {
|
|
73
|
-
if (!this.#expandedStatusPages) {
|
|
74
|
-
this.#expandedStatusPages = Object.keys(this.statusPages).reduce(
|
|
75
|
-
(result, range) => {
|
|
76
|
-
const renderer = this.statusPages[range];
|
|
77
|
-
result = Object.assign(result, parseRange(range, renderer));
|
|
78
|
-
return result;
|
|
79
|
-
},
|
|
80
|
-
{}
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
return this.#expandedStatusPages;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Forcefully tweaking the type of the error object to
|
|
87
|
-
* have known properties.
|
|
88
|
-
*/
|
|
89
|
-
#toHttpError(error) {
|
|
90
|
-
const httpError = is.object(error) ? error : new Error(String(error));
|
|
91
|
-
httpError.message = httpError.message || "Internal server error";
|
|
92
|
-
httpError.status = httpError.status || 500;
|
|
93
|
-
return httpError;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Error reporting context
|
|
97
|
-
*/
|
|
98
|
-
context(ctx) {
|
|
99
|
-
const requestId = ctx.request.id();
|
|
100
|
-
return requestId ? {
|
|
101
|
-
"x-request-id": requestId
|
|
102
|
-
} : {};
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Returns the log level for an error based upon the error
|
|
106
|
-
* status code.
|
|
107
|
-
*/
|
|
108
|
-
getErrorLogLevel(error) {
|
|
109
|
-
if (error.status >= 500) {
|
|
110
|
-
return "error";
|
|
111
|
-
}
|
|
112
|
-
if (error.status >= 400) {
|
|
113
|
-
return "warn";
|
|
114
|
-
}
|
|
115
|
-
return "info";
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* A boolean to control if errors should be rendered with
|
|
119
|
-
* all the available debugging info.
|
|
120
|
-
*/
|
|
121
|
-
isDebuggingEnabled(_) {
|
|
122
|
-
return this.debug;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Returns a boolean by checking if an error should be reported.
|
|
126
|
-
*/
|
|
127
|
-
shouldReport(error) {
|
|
128
|
-
if (this.reportErrors === false) {
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
if (this.ignoreStatuses.includes(error.status)) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
if (error.code && this.ignoreCodes.includes(error.code)) {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
if (this.ignoreExceptions.find((exception) => error instanceof exception)) {
|
|
138
|
-
return false;
|
|
139
|
-
}
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Renders an error to JSON response
|
|
144
|
-
*/
|
|
145
|
-
async renderErrorAsJSON(error, ctx) {
|
|
146
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
147
|
-
const { default: Youch } = await import("youch");
|
|
148
|
-
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
149
|
-
ctx.response.status(error.status).send(json.error);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
ctx.response.status(error.status).send({ message: error.message });
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Renders an error to JSON API response
|
|
156
|
-
*/
|
|
157
|
-
async renderErrorAsJSONAPI(error, ctx) {
|
|
158
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
159
|
-
const { default: Youch } = await import("youch");
|
|
160
|
-
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
161
|
-
ctx.response.status(error.status).send(json.error);
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
ctx.response.status(error.status).send({
|
|
165
|
-
errors: [
|
|
166
|
-
{
|
|
167
|
-
title: error.message,
|
|
168
|
-
code: error.code,
|
|
169
|
-
status: error.status
|
|
170
|
-
}
|
|
171
|
-
]
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Renders an error to HTML response
|
|
176
|
-
*/
|
|
177
|
-
async renderErrorAsHTML(error, ctx) {
|
|
178
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
179
|
-
const { default: Youch } = await import("youch");
|
|
180
|
-
const html = await new Youch(error, ctx.request.request).toHTML({
|
|
181
|
-
cspNonce: "nonce" in ctx.response ? ctx.response.nonce : void 0
|
|
182
|
-
});
|
|
183
|
-
ctx.response.status(error.status).send(html);
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
ctx.response.status(error.status).send(`<p> ${error.message} </p>`);
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Renders the validation error message to a JSON
|
|
190
|
-
* response
|
|
191
|
-
*/
|
|
192
|
-
async renderValidationErrorAsJSON(error, ctx) {
|
|
193
|
-
ctx.response.status(error.status).send({
|
|
194
|
-
errors: error.messages
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Renders the validation error message as per JSON API
|
|
199
|
-
* spec
|
|
200
|
-
*/
|
|
201
|
-
async renderValidationErrorAsJSONAPI(error, ctx) {
|
|
202
|
-
ctx.response.status(error.status).send({
|
|
203
|
-
errors: error.messages.map((message) => {
|
|
204
|
-
return {
|
|
205
|
-
title: message.message,
|
|
206
|
-
code: message.rule,
|
|
207
|
-
source: {
|
|
208
|
-
pointer: message.field
|
|
209
|
-
},
|
|
210
|
-
meta: message.meta
|
|
211
|
-
};
|
|
212
|
-
})
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Renders the validation error as an HTML string
|
|
217
|
-
*/
|
|
218
|
-
async renderValidationErrorAsHTML(error, ctx) {
|
|
219
|
-
ctx.response.status(error.status).type("html").send(
|
|
220
|
-
error.messages.map((message) => {
|
|
221
|
-
return `${message.field} - ${message.message}`;
|
|
222
|
-
}).join("<br />")
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Renders the error to response
|
|
227
|
-
*/
|
|
228
|
-
renderError(error, ctx) {
|
|
229
|
-
switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
|
|
230
|
-
case "application/vnd.api+json":
|
|
231
|
-
return this.renderErrorAsJSONAPI(error, ctx);
|
|
232
|
-
case "json":
|
|
233
|
-
return this.renderErrorAsJSON(error, ctx);
|
|
234
|
-
case "html":
|
|
235
|
-
default:
|
|
236
|
-
return this.renderErrorAsHTML(error, ctx);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Renders the validation error to response
|
|
241
|
-
*/
|
|
242
|
-
renderValidationError(error, ctx) {
|
|
243
|
-
switch (ctx.request.accepts(["html", "application/vnd.api+json", "json"])) {
|
|
244
|
-
case "application/vnd.api+json":
|
|
245
|
-
return this.renderValidationErrorAsJSONAPI(error, ctx);
|
|
246
|
-
case "json":
|
|
247
|
-
return this.renderValidationErrorAsJSON(error, ctx);
|
|
248
|
-
case "html":
|
|
249
|
-
default:
|
|
250
|
-
return this.renderValidationErrorAsHTML(error, ctx);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Reports an error during an HTTP request
|
|
255
|
-
*/
|
|
256
|
-
async report(error, ctx) {
|
|
257
|
-
const httpError = this.#toHttpError(error);
|
|
258
|
-
if (!this.shouldReport(httpError)) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
if (typeof httpError.report === "function") {
|
|
262
|
-
httpError.report(httpError, ctx);
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
const level = this.getErrorLogLevel(httpError);
|
|
266
|
-
ctx.logger.log(
|
|
267
|
-
level,
|
|
268
|
-
{
|
|
269
|
-
...level === "error" || level === "fatal" ? { err: httpError } : {},
|
|
270
|
-
...this.context(ctx)
|
|
271
|
-
},
|
|
272
|
-
httpError.message
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Handles the error during the HTTP request.
|
|
277
|
-
*/
|
|
278
|
-
async handle(error, ctx) {
|
|
279
|
-
const httpError = this.#toHttpError(error);
|
|
280
|
-
if (typeof httpError.handle === "function") {
|
|
281
|
-
return httpError.handle(httpError, ctx);
|
|
282
|
-
}
|
|
283
|
-
if (httpError.code === "E_VALIDATION_ERROR" && "messages" in httpError) {
|
|
284
|
-
return this.renderValidationError(httpError, ctx);
|
|
285
|
-
}
|
|
286
|
-
const statusPages = this.#expandStatusPages();
|
|
287
|
-
if (this.renderStatusPages && statusPages[httpError.status]) {
|
|
288
|
-
return statusPages[httpError.status](httpError, ctx);
|
|
289
|
-
}
|
|
290
|
-
return this.renderError(httpError, ctx);
|
|
291
|
-
}
|
|
292
|
-
};
|
|
293
|
-
export {
|
|
294
|
-
BriskRoute,
|
|
295
|
-
CookieClient,
|
|
296
|
-
ExceptionHandler,
|
|
297
|
-
HttpContext,
|
|
298
|
-
Redirect,
|
|
299
|
-
Request,
|
|
300
|
-
Response,
|
|
301
|
-
Route,
|
|
302
|
-
RouteGroup,
|
|
303
|
-
RouteResource,
|
|
304
|
-
Router,
|
|
305
|
-
Server,
|
|
306
|
-
defineConfig,
|
|
307
|
-
exceptions_exports as errors
|
|
308
|
-
};
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
export { Request } from './src/request.js';
|
|
10
|
+
export { Response } from './src/response.js';
|
|
11
|
+
export { Redirect } from './src/redirect.js';
|
|
12
|
+
export { Server } from './src/server/main.js';
|
|
13
|
+
export { Router } from './src/router/main.js';
|
|
14
|
+
export { Route } from './src/router/route.js';
|
|
15
|
+
export * as errors from './src/exceptions.js';
|
|
16
|
+
export { BriskRoute } from './src/router/brisk.js';
|
|
17
|
+
export { RouteGroup } from './src/router/group.js';
|
|
18
|
+
export { defineConfig } from './src/define_config.js';
|
|
19
|
+
export { CookieClient } from './src/cookies/client.js';
|
|
20
|
+
export { HttpContext } from './src/http_context/main.js';
|
|
21
|
+
export { RouteResource } from './src/router/resource.js';
|
|
22
|
+
export { ExceptionHandler } from './src/exception_handler.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
2
|
+
/**
|
|
3
|
+
* Cookie client exposes the API to parse/set AdonisJS cookies
|
|
4
|
+
* as a client.
|
|
5
|
+
*/
|
|
6
|
+
export declare class CookieClient {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(encryption: Encryption);
|
|
9
|
+
/**
|
|
10
|
+
* Encrypt a key value pair to be sent in the cookie header
|
|
11
|
+
*/
|
|
12
|
+
encrypt(key: string, value: any): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Sign a key value pair to be sent in the cookie header
|
|
15
|
+
*/
|
|
16
|
+
sign(key: string, value: any): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Encode a key value pair to be sent in the cookie header
|
|
19
|
+
*/
|
|
20
|
+
encode(_: string, value: any): string | null;
|
|
21
|
+
/**
|
|
22
|
+
* Unsign a signed cookie value
|
|
23
|
+
*/
|
|
24
|
+
unsign(key: string, value: string): any;
|
|
25
|
+
/**
|
|
26
|
+
* Decrypt an encrypted cookie value
|
|
27
|
+
*/
|
|
28
|
+
decrypt(key: string, value: string): any;
|
|
29
|
+
/**
|
|
30
|
+
* Decode an encoded cookie value
|
|
31
|
+
*/
|
|
32
|
+
decode(_: string, value: string): any;
|
|
33
|
+
/**
|
|
34
|
+
* Parse response cookie
|
|
35
|
+
*/
|
|
36
|
+
parse(key: string, value: any): any;
|
|
37
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import * as plainCookiesDriver from './drivers/plain.js';
|
|
10
|
+
import * as signedCookiesDriver from './drivers/signed.js';
|
|
11
|
+
import * as encryptedCookiesDriver from './drivers/encrypted.js';
|
|
12
|
+
/**
|
|
13
|
+
* Cookie client exposes the API to parse/set AdonisJS cookies
|
|
14
|
+
* as a client.
|
|
15
|
+
*/
|
|
16
|
+
export class CookieClient {
|
|
17
|
+
#encryption;
|
|
18
|
+
constructor(encryption) {
|
|
19
|
+
this.#encryption = encryption;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Encrypt a key value pair to be sent in the cookie header
|
|
23
|
+
*/
|
|
24
|
+
encrypt(key, value) {
|
|
25
|
+
return encryptedCookiesDriver.pack(key, value, this.#encryption);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Sign a key value pair to be sent in the cookie header
|
|
29
|
+
*/
|
|
30
|
+
sign(key, value) {
|
|
31
|
+
return signedCookiesDriver.pack(key, value, this.#encryption);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Encode a key value pair to be sent in the cookie header
|
|
35
|
+
*/
|
|
36
|
+
encode(_, value) {
|
|
37
|
+
return plainCookiesDriver.pack(value);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Unsign a signed cookie value
|
|
41
|
+
*/
|
|
42
|
+
unsign(key, value) {
|
|
43
|
+
return signedCookiesDriver.canUnpack(value)
|
|
44
|
+
? signedCookiesDriver.unpack(key, value, this.#encryption)
|
|
45
|
+
: null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Decrypt an encrypted cookie value
|
|
49
|
+
*/
|
|
50
|
+
decrypt(key, value) {
|
|
51
|
+
return encryptedCookiesDriver.canUnpack(value)
|
|
52
|
+
? encryptedCookiesDriver.unpack(key, value, this.#encryption)
|
|
53
|
+
: null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Decode an encoded cookie value
|
|
57
|
+
*/
|
|
58
|
+
decode(_, value) {
|
|
59
|
+
return plainCookiesDriver.canUnpack(value) ? plainCookiesDriver.unpack(value) : null;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Parse response cookie
|
|
63
|
+
*/
|
|
64
|
+
parse(key, value) {
|
|
65
|
+
/**
|
|
66
|
+
* Unsign signed cookie
|
|
67
|
+
*/
|
|
68
|
+
if (signedCookiesDriver.canUnpack(value)) {
|
|
69
|
+
return signedCookiesDriver.unpack(key, value, this.#encryption);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Decrypted encrypted cookie
|
|
73
|
+
*/
|
|
74
|
+
if (encryptedCookiesDriver.canUnpack(value)) {
|
|
75
|
+
return encryptedCookiesDriver.unpack(key, value, this.#encryption);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Decode encoded cookie
|
|
79
|
+
*/
|
|
80
|
+
if (plainCookiesDriver.canUnpack(value)) {
|
|
81
|
+
return plainCookiesDriver.unpack(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
2
|
+
/**
|
|
3
|
+
* Encrypt a value to be set as cookie
|
|
4
|
+
*/
|
|
5
|
+
export declare function pack(key: string, value: any, encryption: Encryption): null | string;
|
|
6
|
+
/**
|
|
7
|
+
* Returns a boolean, if the unpack method from this module can attempt
|
|
8
|
+
* to unpack encrypted value.
|
|
9
|
+
*/
|
|
10
|
+
export declare function canUnpack(encryptedValue: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Attempts to unpack the encrypted cookie value. Returns null, when fails to do so.
|
|
13
|
+
* Only call this method, when `canUnpack` returns true, otherwise runtime
|
|
14
|
+
* exceptions can be raised.
|
|
15
|
+
*/
|
|
16
|
+
export declare function unpack(key: string, encryptedValue: string, encryption: Encryption): null | any;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Encrypt a value to be set as cookie
|
|
11
|
+
*/
|
|
12
|
+
export function pack(key, value, encryption) {
|
|
13
|
+
if (value === undefined || value === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return `e:${encryption.encrypt(value, undefined, key)}`;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Returns a boolean, if the unpack method from this module can attempt
|
|
20
|
+
* to unpack encrypted value.
|
|
21
|
+
*/
|
|
22
|
+
export function canUnpack(encryptedValue) {
|
|
23
|
+
return typeof encryptedValue === 'string' && encryptedValue.substring(0, 2) === 'e:';
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Attempts to unpack the encrypted cookie value. Returns null, when fails to do so.
|
|
27
|
+
* Only call this method, when `canUnpack` returns true, otherwise runtime
|
|
28
|
+
* exceptions can be raised.
|
|
29
|
+
*/
|
|
30
|
+
export function unpack(key, encryptedValue, encryption) {
|
|
31
|
+
const value = encryptedValue.slice(2);
|
|
32
|
+
if (!value) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return encryption.decrypt(value, key);
|
|
36
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Encodes a value into a base64 url encoded string to
|
|
3
|
+
* be set as cookie
|
|
4
|
+
*/
|
|
5
|
+
export declare function pack(value: any): null | string;
|
|
6
|
+
/**
|
|
7
|
+
* Returns true when this `unpack` method of this module can attempt
|
|
8
|
+
* to unpack the encode value.
|
|
9
|
+
*/
|
|
10
|
+
export declare function canUnpack(encodedValue: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Attempts to unpack the value by decoding it. Make sure to call, `canUnpack`
|
|
13
|
+
* before calling this method
|
|
14
|
+
*/
|
|
15
|
+
export declare function unpack(encodedValue: string): null | any;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { base64, MessageBuilder } from '@poppinss/utils';
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a value into a base64 url encoded string to
|
|
12
|
+
* be set as cookie
|
|
13
|
+
*/
|
|
14
|
+
export function pack(value) {
|
|
15
|
+
if (value === undefined || value === null) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return base64.urlEncode(new MessageBuilder().build(value));
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Returns true when this `unpack` method of this module can attempt
|
|
22
|
+
* to unpack the encode value.
|
|
23
|
+
*/
|
|
24
|
+
export function canUnpack(encodedValue) {
|
|
25
|
+
return typeof encodedValue === 'string';
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Attempts to unpack the value by decoding it. Make sure to call, `canUnpack`
|
|
29
|
+
* before calling this method
|
|
30
|
+
*/
|
|
31
|
+
export function unpack(encodedValue) {
|
|
32
|
+
return new MessageBuilder().verify(base64.urlDecode(encodedValue, 'utf-8', false));
|
|
33
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
2
|
+
/**
|
|
3
|
+
* Signs a value to be shared as a cookie. The signed output has a
|
|
4
|
+
* hash to verify tampering with the original value
|
|
5
|
+
*/
|
|
6
|
+
export declare function pack(key: string, value: any, encryption: Encryption): null | string;
|
|
7
|
+
/**
|
|
8
|
+
* Returns a boolean, if the unpack method from this module can attempt
|
|
9
|
+
* to unpack the signed value.
|
|
10
|
+
*/
|
|
11
|
+
export declare function canUnpack(signedValue: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Attempts to unpack the signed value. Make sure to call `canUnpack` before
|
|
14
|
+
* calling this method.
|
|
15
|
+
*/
|
|
16
|
+
export declare function unpack(key: string, signedValue: string, encryption: Encryption): null | any;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Signs a value to be shared as a cookie. The signed output has a
|
|
11
|
+
* hash to verify tampering with the original value
|
|
12
|
+
*/
|
|
13
|
+
export function pack(key, value, encryption) {
|
|
14
|
+
if (value === undefined || value === null) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return `s:${encryption.verifier.sign(value, undefined, key)}`;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Returns a boolean, if the unpack method from this module can attempt
|
|
21
|
+
* to unpack the signed value.
|
|
22
|
+
*/
|
|
23
|
+
export function canUnpack(signedValue) {
|
|
24
|
+
return typeof signedValue === 'string' && signedValue.substring(0, 2) === 's:';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Attempts to unpack the signed value. Make sure to call `canUnpack` before
|
|
28
|
+
* calling this method.
|
|
29
|
+
*/
|
|
30
|
+
export function unpack(key, signedValue, encryption) {
|
|
31
|
+
const value = signedValue.slice(2);
|
|
32
|
+
if (!value) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return encryption.verifier.unsign(value, key);
|
|
36
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
2
|
+
/**
|
|
3
|
+
* Cookie parser parses the HTTP `cookie` header and collects all cookies
|
|
4
|
+
* inside an object of `key-value` pair, but doesn't attempt to decrypt
|
|
5
|
+
* or unsign or decode the individual values.
|
|
6
|
+
*
|
|
7
|
+
* The cookie values are lazily decrypted, or unsigned to avoid unncessary
|
|
8
|
+
* processing, which infact can be used as a means to burden the server
|
|
9
|
+
* by sending too many cookies which even doesn't belongs to the
|
|
10
|
+
* server.
|
|
11
|
+
*/
|
|
12
|
+
export declare class CookieParser {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(cookieHeader: string, encryption: Encryption);
|
|
15
|
+
/**
|
|
16
|
+
* Attempts to decode a cookie by the name. When calling this method,
|
|
17
|
+
* you are assuming that the cookie was just encoded in the first
|
|
18
|
+
* place and not signed or encrypted.
|
|
19
|
+
*/
|
|
20
|
+
decode(key: string, encoded?: boolean): any | null;
|
|
21
|
+
/**
|
|
22
|
+
* Attempts to unsign a cookie by the name. When calling this method,
|
|
23
|
+
* you are assuming that the cookie was signed in the first place.
|
|
24
|
+
*/
|
|
25
|
+
unsign(key: string): null | any;
|
|
26
|
+
/**
|
|
27
|
+
* Attempts to decrypt a cookie by the name. When calling this method,
|
|
28
|
+
* you are assuming that the cookie was encrypted in the first place.
|
|
29
|
+
*/
|
|
30
|
+
decrypt(key: string): null | any;
|
|
31
|
+
/**
|
|
32
|
+
* Returns an object of cookies key-value pair. Do note, the
|
|
33
|
+
* cookies are not decoded, unsigned or decrypted inside this
|
|
34
|
+
* list.
|
|
35
|
+
*/
|
|
36
|
+
list(): Record<string, any>;
|
|
37
|
+
}
|