@arkstack/driver-h3 0.12.10 → 0.12.11
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/dist/{index-CDLqKB-5.d.ts → index-DIJtwwZP.d.ts} +7 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1144 -14
- package/dist/middlewares/index.d.ts +27 -3
- package/dist/middlewares/index.js +2 -2
- package/dist/{middlewares-DWmfAXws.js → middlewares-CGBJHsH7.js} +25 -2
- package/dist/types-DWUchLdg.d.ts +10 -0
- package/dist/types.d.ts +2 -16
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import { t as staticAssetHandler } from "./middlewares-
|
|
1
|
+
import { t as staticAssetHandler } from "./middlewares-CGBJHsH7.js";
|
|
2
2
|
import { Arkstack, ArkstackKitDriver } from "@arkstack/contract";
|
|
3
3
|
import { H3, HTTPResponse, serve, toResponse } from "h3";
|
|
4
|
-
import { ErrorHandler, Logger,
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { ErrorHandler, Logger, isClass, renderError } from "@arkstack/common";
|
|
5
|
+
import { Request } from "clear-router";
|
|
6
|
+
import { definePlugin } from "clear-router/core";
|
|
7
|
+
import { definePlugin as definePlugin$1 } from "kanun";
|
|
8
|
+
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
9
|
+
import { DB } from "arkormx";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
7
12
|
import { Router as Router$1 } from "clear-router/h3";
|
|
8
13
|
import { clearRouterH3Plugin } from "@resora/plugin-clear-router";
|
|
9
14
|
import { registerPlugin } from "resora";
|
|
10
15
|
//#region src/error-handler.ts
|
|
11
16
|
const webMiddlewareKey = Symbol.for("arkstack:http:web");
|
|
12
|
-
const isRecord = (value) => {
|
|
17
|
+
const isRecord$1 = (value) => {
|
|
13
18
|
return typeof value === "object" && value !== null;
|
|
14
19
|
};
|
|
15
20
|
const isWebRequest = (value) => {
|
|
16
|
-
if (!isRecord(value)) return false;
|
|
21
|
+
if (!isRecord$1(value)) return false;
|
|
17
22
|
return value[webMiddlewareKey] === true || value.arkstackWeb === true || isWebRequest(value.context) || isWebRequest(value.req);
|
|
18
23
|
};
|
|
19
24
|
const redirectBackTarget = (event) => {
|
|
@@ -60,20 +65,1144 @@ const defaultErrorHandler = async (err, event) => {
|
|
|
60
65
|
});
|
|
61
66
|
};
|
|
62
67
|
//#endregion
|
|
68
|
+
//#region ../http/dist/redirect-Bwrh6IHq.js
|
|
69
|
+
const unwrapRequestSource = (source) => {
|
|
70
|
+
if (source.headers) return source;
|
|
71
|
+
if (source.req) return source.req;
|
|
72
|
+
if (source.request) return source.request;
|
|
73
|
+
return source;
|
|
74
|
+
};
|
|
75
|
+
const normalizeHeaders = (headers) => {
|
|
76
|
+
const normalized = {};
|
|
77
|
+
if (!headers) return normalized;
|
|
78
|
+
if (isHeaders(headers)) {
|
|
79
|
+
headers.forEach((value, key) => {
|
|
80
|
+
normalized[key.toLowerCase()] = value;
|
|
81
|
+
});
|
|
82
|
+
return normalized;
|
|
83
|
+
}
|
|
84
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
85
|
+
const normalizedValue = normalizeHeaderValue(value);
|
|
86
|
+
if (typeof normalizedValue === "string") normalized[key.toLowerCase()] = normalizedValue;
|
|
87
|
+
}
|
|
88
|
+
return normalized;
|
|
89
|
+
};
|
|
90
|
+
const normalizeHeaderValue = (value) => {
|
|
91
|
+
if (Array.isArray(value)) return value.join(", ");
|
|
92
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
93
|
+
return value ?? void 0;
|
|
94
|
+
};
|
|
95
|
+
const isHeaders = (value) => typeof Headers !== "undefined" && value instanceof Headers;
|
|
96
|
+
const isRecord = (value) => {
|
|
97
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Resolve Middleware
|
|
101
|
+
*
|
|
102
|
+
* @param middleware
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
const resolveMiddleware = (middleware) => {
|
|
106
|
+
if (!middleware || typeof middleware === "function" && !isClass(middleware)) return middleware;
|
|
107
|
+
if (middleware && typeof middleware === "object" && !isClass(middleware) && "handle" in middleware) return middleware.handle.bind(middleware);
|
|
108
|
+
const instance = isClass(middleware) ? new middleware() : middleware;
|
|
109
|
+
if (instance && typeof instance.handle === "function") return instance.handle.bind(instance);
|
|
110
|
+
return middleware;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Represents an HTTP request, providing a consistent interface for accessing request data.
|
|
114
|
+
*
|
|
115
|
+
* @author 3m1n3nc3
|
|
116
|
+
*/
|
|
117
|
+
var Request$1 = class Request$1 extends Request {
|
|
118
|
+
headers;
|
|
119
|
+
ip;
|
|
120
|
+
source;
|
|
121
|
+
user;
|
|
122
|
+
authToken;
|
|
123
|
+
constructor(options = {}) {
|
|
124
|
+
super(options);
|
|
125
|
+
this.headers = normalizeHeaders(options.headers);
|
|
126
|
+
if (this.method) this.method = options.method;
|
|
127
|
+
if (this.url) this.url = options.url;
|
|
128
|
+
if (this.path) this.path = options.path;
|
|
129
|
+
this.ip = options.ip ?? null;
|
|
130
|
+
this.user = options.user;
|
|
131
|
+
this.authToken = options.authToken;
|
|
132
|
+
this.source = options.source;
|
|
133
|
+
globalThis.request = (key) => key ? this.input(key) : this;
|
|
134
|
+
}
|
|
135
|
+
static from(source) {
|
|
136
|
+
if (!source) return;
|
|
137
|
+
if (source instanceof Request$1) return source;
|
|
138
|
+
const request = unwrapRequestSource(source);
|
|
139
|
+
return new Request$1({
|
|
140
|
+
headers: request.headers,
|
|
141
|
+
method: request.method,
|
|
142
|
+
url: request.originalUrl ?? request.url,
|
|
143
|
+
path: request.path,
|
|
144
|
+
ip: request.ip ?? null,
|
|
145
|
+
user: request.user,
|
|
146
|
+
authToken: request.authToken,
|
|
147
|
+
source
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
header(name) {
|
|
151
|
+
return this.headers[name.toLowerCase()];
|
|
152
|
+
}
|
|
153
|
+
bearerToken() {
|
|
154
|
+
const authorization = this.header("authorization");
|
|
155
|
+
if (!authorization?.startsWith("Bearer ")) return null;
|
|
156
|
+
return authorization.substring(7);
|
|
157
|
+
}
|
|
158
|
+
setUser(user) {
|
|
159
|
+
this.user = user;
|
|
160
|
+
if (isRecord(this.source)) this.source.user = user;
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var FlashBag = class {
|
|
165
|
+
bag = {};
|
|
166
|
+
sweepKeys = /* @__PURE__ */ new Set();
|
|
167
|
+
constructor(items) {
|
|
168
|
+
this.bag = { ...items || {} };
|
|
169
|
+
this.sweepKeys = new Set(Object.keys(this.bag));
|
|
170
|
+
}
|
|
171
|
+
put(key, value) {
|
|
172
|
+
this.bag[key] = value;
|
|
173
|
+
this.sweepKeys.delete(key);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
set(key, value) {
|
|
177
|
+
return this.put(key, value);
|
|
178
|
+
}
|
|
179
|
+
get(key, defaultValue) {
|
|
180
|
+
return key in this.bag ? this.bag[key] : defaultValue;
|
|
181
|
+
}
|
|
182
|
+
has(key) {
|
|
183
|
+
if (Array.isArray(key)) return key.every((item) => this.has(item));
|
|
184
|
+
if (key) return key in this.bag;
|
|
185
|
+
return this.any();
|
|
186
|
+
}
|
|
187
|
+
any() {
|
|
188
|
+
return Object.keys(this.bag).length > 0;
|
|
189
|
+
}
|
|
190
|
+
isEmpty() {
|
|
191
|
+
return !this.any();
|
|
192
|
+
}
|
|
193
|
+
isNotEmpty() {
|
|
194
|
+
return this.any();
|
|
195
|
+
}
|
|
196
|
+
keys() {
|
|
197
|
+
return Object.keys(this.bag);
|
|
198
|
+
}
|
|
199
|
+
all() {
|
|
200
|
+
return { ...this.bag };
|
|
201
|
+
}
|
|
202
|
+
clear(key) {
|
|
203
|
+
if (Array.isArray(key)) {
|
|
204
|
+
for (const item of key) {
|
|
205
|
+
delete this.bag[item];
|
|
206
|
+
this.sweepKeys.delete(item);
|
|
207
|
+
}
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
if (key) {
|
|
211
|
+
delete this.bag[key];
|
|
212
|
+
this.sweepKeys.delete(key);
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
this.bag = {};
|
|
216
|
+
this.sweepKeys.clear();
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
forget(key) {
|
|
220
|
+
return this.clear(key);
|
|
221
|
+
}
|
|
222
|
+
markForSweep(keys = this.keys()) {
|
|
223
|
+
this.sweepKeys = new Set(keys);
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
sweep() {
|
|
227
|
+
for (const key of this.sweepKeys) delete this.bag[key];
|
|
228
|
+
this.sweepKeys = new Set(Object.keys(this.bag));
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
toJSON() {
|
|
232
|
+
return this.all();
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const sessionKey = Symbol.for("arkstack:http:session");
|
|
236
|
+
const asMessageRecord = (value) => {
|
|
237
|
+
if (!isRecord(value)) return;
|
|
238
|
+
return value;
|
|
239
|
+
};
|
|
240
|
+
const callRecordMethod = (source, method) => {
|
|
241
|
+
if (typeof source[method] !== "function") return;
|
|
242
|
+
return asMessageRecord(source[method]());
|
|
243
|
+
};
|
|
244
|
+
const resolveMessageRecord = (source) => {
|
|
245
|
+
if (!isRecord(source)) return;
|
|
246
|
+
if (typeof source.getMessageBag === "function") {
|
|
247
|
+
const bag = source.getMessageBag();
|
|
248
|
+
if (bag && bag !== source) {
|
|
249
|
+
const messages = resolveMessageRecord(bag);
|
|
250
|
+
if (messages) return messages;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (typeof source.errors === "function") {
|
|
254
|
+
const errors = source.errors();
|
|
255
|
+
const messages = resolveMessageRecord(errors) || asMessageRecord(errors);
|
|
256
|
+
if (messages) return messages;
|
|
257
|
+
}
|
|
258
|
+
return callRecordMethod(source, "getMessages") || callRecordMethod(source, "messagesRaw") || callRecordMethod(source, "toArray") || resolveMessageRecord(source.errors) || asMessageRecord(source.errors);
|
|
259
|
+
};
|
|
260
|
+
const getValidationIssueField = (issue) => {
|
|
261
|
+
if (typeof issue.field === "string") return issue.field;
|
|
262
|
+
if (typeof issue.attribute === "string") return issue.attribute;
|
|
263
|
+
if (typeof issue.key === "string") return issue.key;
|
|
264
|
+
if (typeof issue.path === "string") return issue.path;
|
|
265
|
+
if (Array.isArray(issue.path)) return issue.path.join(".") || "_";
|
|
266
|
+
return "_";
|
|
267
|
+
};
|
|
268
|
+
const toMessages = (value) => {
|
|
269
|
+
if (Array.isArray(value)) return value.flatMap((item) => toMessages(item));
|
|
270
|
+
if (value instanceof Error) return [value.message];
|
|
271
|
+
if (isRecord(value) && typeof value.message === "string") return [value.message];
|
|
272
|
+
if (value === null || typeof value === "undefined") return [];
|
|
273
|
+
return [String(value)];
|
|
274
|
+
};
|
|
275
|
+
const getPath = (source, key, defaultValue) => {
|
|
276
|
+
const value = key.split(".").reduce((current, part) => {
|
|
277
|
+
if (!isRecord(current) && !Array.isArray(current)) return;
|
|
278
|
+
return current[part];
|
|
279
|
+
}, source);
|
|
280
|
+
return typeof value === "undefined" ? defaultValue : value;
|
|
281
|
+
};
|
|
282
|
+
var ErrorBag = class ErrorBag extends FlashBag {
|
|
283
|
+
constructor(errors) {
|
|
284
|
+
super();
|
|
285
|
+
if (errors) {
|
|
286
|
+
this.merge(errors);
|
|
287
|
+
this.markForSweep();
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
add(field, message) {
|
|
291
|
+
const key = field || "_";
|
|
292
|
+
const messages = toMessages(message);
|
|
293
|
+
if (!messages.length) return this;
|
|
294
|
+
this.put(key, [...this.bag[key] || [], ...messages]);
|
|
295
|
+
return this;
|
|
296
|
+
}
|
|
297
|
+
addIf(condition, field, message) {
|
|
298
|
+
if (condition) this.add(field, message);
|
|
299
|
+
return this;
|
|
300
|
+
}
|
|
301
|
+
merge(errors) {
|
|
302
|
+
const incoming = resolveMessageRecord(errors) || (isRecord(errors) ? errors : void 0);
|
|
303
|
+
if (!incoming) return this.validation(errors);
|
|
304
|
+
for (const [field, messages] of Object.entries(incoming)) this.add(field, messages);
|
|
305
|
+
return this;
|
|
306
|
+
}
|
|
307
|
+
validation(error) {
|
|
308
|
+
if (!error) return this;
|
|
309
|
+
if (error instanceof ErrorBag) return this.merge(error);
|
|
310
|
+
const messages = resolveMessageRecord(error);
|
|
311
|
+
if (messages) return this.merge(messages);
|
|
312
|
+
if (Array.isArray(error)) {
|
|
313
|
+
for (const item of error) if (isRecord(item) && "message" in item) this.add(getValidationIssueField(item), item.message);
|
|
314
|
+
else this.add("_", item);
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
if (isRecord(error)) {
|
|
318
|
+
if (typeof error.errors === "function") return this.validation(error.errors());
|
|
319
|
+
if (error.errors) return this.validation(error.errors);
|
|
320
|
+
if (Array.isArray(error.issues)) return this.validation(error.issues);
|
|
321
|
+
if ("message" in error) return this.add(getValidationIssueField(error), error.message);
|
|
322
|
+
return this.merge(error);
|
|
323
|
+
}
|
|
324
|
+
if (error instanceof Error) return this.add("_", error.message);
|
|
325
|
+
return this.add("_", error);
|
|
326
|
+
}
|
|
327
|
+
keys() {
|
|
328
|
+
return Object.keys(this.bag);
|
|
329
|
+
}
|
|
330
|
+
get(field = "_") {
|
|
331
|
+
return [...this.bag[field] || []];
|
|
332
|
+
}
|
|
333
|
+
first(field) {
|
|
334
|
+
if (field) return this.bag[field]?.[0] || "";
|
|
335
|
+
return this.all()[0] || "";
|
|
336
|
+
}
|
|
337
|
+
has(field) {
|
|
338
|
+
if (Array.isArray(field)) return field.every((key) => this.has(key));
|
|
339
|
+
if (field) return (this.bag[field]?.length || 0) > 0;
|
|
340
|
+
return this.any();
|
|
341
|
+
}
|
|
342
|
+
hasAny(fields) {
|
|
343
|
+
return (Array.isArray(fields) ? fields : [fields]).some((key) => this.has(key));
|
|
344
|
+
}
|
|
345
|
+
missing(fields) {
|
|
346
|
+
return (Array.isArray(fields) ? fields : [fields]).every((key) => !this.has(key));
|
|
347
|
+
}
|
|
348
|
+
any() {
|
|
349
|
+
return Object.values(this.bag).some((messages) => messages.length > 0);
|
|
350
|
+
}
|
|
351
|
+
isEmpty() {
|
|
352
|
+
return !this.any();
|
|
353
|
+
}
|
|
354
|
+
isNotEmpty() {
|
|
355
|
+
return this.any();
|
|
356
|
+
}
|
|
357
|
+
count() {
|
|
358
|
+
return Object.values(this.bag).reduce((total, messages) => total + messages.length, 0);
|
|
359
|
+
}
|
|
360
|
+
all() {
|
|
361
|
+
return Object.values(this.bag).flat();
|
|
362
|
+
}
|
|
363
|
+
unique() {
|
|
364
|
+
return [...new Set(this.all())];
|
|
365
|
+
}
|
|
366
|
+
clear(field) {
|
|
367
|
+
super.clear(field);
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
forget(field) {
|
|
371
|
+
return this.clear(field);
|
|
372
|
+
}
|
|
373
|
+
messagesRaw() {
|
|
374
|
+
return this.toJSON();
|
|
375
|
+
}
|
|
376
|
+
getMessages() {
|
|
377
|
+
return this.messagesRaw();
|
|
378
|
+
}
|
|
379
|
+
getMessageBag() {
|
|
380
|
+
return this;
|
|
381
|
+
}
|
|
382
|
+
toArray() {
|
|
383
|
+
return this.toJSON();
|
|
384
|
+
}
|
|
385
|
+
toJSON() {
|
|
386
|
+
return Object.entries(this.bag).reduce((errors, [field, messages]) => {
|
|
387
|
+
errors[field] = [...messages];
|
|
388
|
+
return errors;
|
|
389
|
+
}, {});
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
var Session = class Session {
|
|
393
|
+
errors;
|
|
394
|
+
flashBag;
|
|
395
|
+
id;
|
|
396
|
+
data;
|
|
397
|
+
persistent;
|
|
398
|
+
saveQueue = Promise.resolve();
|
|
399
|
+
constructor(initial, persistent) {
|
|
400
|
+
const current = initial instanceof Session ? initial : void 0;
|
|
401
|
+
const state = current ? current.snapshot() : initial && ("data" in initial || "errors" in initial || "flash" in initial) ? initial : { data: initial };
|
|
402
|
+
this.id = persistent?.id ?? current?.id;
|
|
403
|
+
this.persistent = persistent ?? current?.persistent;
|
|
404
|
+
this.saveQueue = current?.saveQueue ?? this.saveQueue;
|
|
405
|
+
this.data = current ? current.data : { ...state.data || {} };
|
|
406
|
+
this.errors = current ? current.errors : state.errors instanceof ErrorBag ? state.errors : new ErrorBag(state.errors);
|
|
407
|
+
this.flashBag = current ? current.flashBag : state.flash instanceof FlashBag ? state.flash : new FlashBag(state.flash);
|
|
408
|
+
const helper = ((key) => key ? this.get(key) : this);
|
|
409
|
+
Object.assign(helper, {
|
|
410
|
+
get: this.get.bind(this),
|
|
411
|
+
put: this.put.bind(this),
|
|
412
|
+
set: this.set.bind(this),
|
|
413
|
+
has: this.has.bind(this),
|
|
414
|
+
forget: this.forget.bind(this),
|
|
415
|
+
clear: this.clear.bind(this),
|
|
416
|
+
all: this.all.bind(this),
|
|
417
|
+
flash: this.flash.bind(this),
|
|
418
|
+
getFlash: this.getFlash.bind(this),
|
|
419
|
+
hasErrors: this.hasErrors.bind(this),
|
|
420
|
+
clearErrors: this.clearErrors.bind(this),
|
|
421
|
+
errors: this.errors,
|
|
422
|
+
flashBag: this.flashBag
|
|
423
|
+
});
|
|
424
|
+
globalThis.session = helper;
|
|
425
|
+
}
|
|
426
|
+
snapshot() {
|
|
427
|
+
return {
|
|
428
|
+
data: this.all(),
|
|
429
|
+
errors: this.errors.toJSON(),
|
|
430
|
+
flash: this.flashBag.toJSON()
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
queuePersist() {
|
|
434
|
+
this.save();
|
|
435
|
+
}
|
|
436
|
+
async save() {
|
|
437
|
+
const payload = this.snapshot();
|
|
438
|
+
const previous = this.saveQueue.catch(() => void 0);
|
|
439
|
+
this.saveQueue = previous.then(async () => {
|
|
440
|
+
await this.persistent?.save(payload);
|
|
441
|
+
});
|
|
442
|
+
await this.saveQueue;
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
async destroy() {
|
|
446
|
+
this.data = {};
|
|
447
|
+
this.errors.clear();
|
|
448
|
+
this.flashBag.clear();
|
|
449
|
+
await this.persistent?.destroy?.();
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Get an item from the session bag
|
|
454
|
+
*
|
|
455
|
+
* @param key
|
|
456
|
+
* @param defaultValue
|
|
457
|
+
* @returns
|
|
458
|
+
*/
|
|
459
|
+
get(key, defaultValue) {
|
|
460
|
+
return key in this.data ? this.data[key] : defaultValue;
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Add an item to the session bag
|
|
464
|
+
*
|
|
465
|
+
* @param key
|
|
466
|
+
* @param defaultValue
|
|
467
|
+
* @returns
|
|
468
|
+
*/
|
|
469
|
+
put(key, value) {
|
|
470
|
+
this.data[key] = value;
|
|
471
|
+
this.queuePersist();
|
|
472
|
+
return this;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Add an item to the session bag
|
|
476
|
+
*
|
|
477
|
+
* @param key
|
|
478
|
+
* @param defaultValue
|
|
479
|
+
* @returns
|
|
480
|
+
*/
|
|
481
|
+
set(key, value) {
|
|
482
|
+
return this.put(key, value);
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Check if an item exist in the session bag
|
|
486
|
+
*
|
|
487
|
+
* @param key
|
|
488
|
+
* @returns
|
|
489
|
+
*/
|
|
490
|
+
has(key) {
|
|
491
|
+
return key in this.data;
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Remove an item from the session bag
|
|
495
|
+
*
|
|
496
|
+
* @param key
|
|
497
|
+
* @returns
|
|
498
|
+
*/
|
|
499
|
+
forget(key) {
|
|
500
|
+
delete this.data[key];
|
|
501
|
+
this.queuePersist();
|
|
502
|
+
return this;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Clear the session bag
|
|
506
|
+
*
|
|
507
|
+
* @returns
|
|
508
|
+
*/
|
|
509
|
+
clear() {
|
|
510
|
+
this.data = {};
|
|
511
|
+
this.errors.clear();
|
|
512
|
+
this.flashBag.clear();
|
|
513
|
+
this.queuePersist();
|
|
514
|
+
return this;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Get all items in the session bag
|
|
518
|
+
*
|
|
519
|
+
* @returns
|
|
520
|
+
*/
|
|
521
|
+
all() {
|
|
522
|
+
return { ...this.data };
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Add a flash item for the next request
|
|
526
|
+
*
|
|
527
|
+
* @param key
|
|
528
|
+
* @param value
|
|
529
|
+
* @returns
|
|
530
|
+
*/
|
|
531
|
+
flash(key, value) {
|
|
532
|
+
this.flashBag.put(key, value);
|
|
533
|
+
this.queuePersist();
|
|
534
|
+
return this;
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Get a flash item
|
|
538
|
+
*
|
|
539
|
+
* @param key
|
|
540
|
+
* @param defaultValue
|
|
541
|
+
* @returns
|
|
542
|
+
*/
|
|
543
|
+
getFlash(key, defaultValue) {
|
|
544
|
+
return this.flashBag.get(key, defaultValue);
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Sweep flashed data that was loaded for this request
|
|
548
|
+
*
|
|
549
|
+
* @returns
|
|
550
|
+
*/
|
|
551
|
+
async sweepFlash() {
|
|
552
|
+
this.errors.sweep();
|
|
553
|
+
this.flashBag.sweep();
|
|
554
|
+
await this.save();
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Add an error to the session error bag
|
|
559
|
+
*
|
|
560
|
+
* @param field
|
|
561
|
+
* @param message
|
|
562
|
+
* @returns
|
|
563
|
+
*/
|
|
564
|
+
addError(field, message) {
|
|
565
|
+
this.errors.add(field, message);
|
|
566
|
+
this.queuePersist();
|
|
567
|
+
return this;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Add multiple errors to the session error bag
|
|
571
|
+
*
|
|
572
|
+
* @param errors
|
|
573
|
+
* @returns
|
|
574
|
+
*/
|
|
575
|
+
addErrors(errors) {
|
|
576
|
+
this.errors.merge(errors);
|
|
577
|
+
this.queuePersist();
|
|
578
|
+
return this;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Add a validation error to the session error bag
|
|
582
|
+
*
|
|
583
|
+
* @param error
|
|
584
|
+
* @returns
|
|
585
|
+
*/
|
|
586
|
+
addValidationErrors(error) {
|
|
587
|
+
this.errors.validation(error);
|
|
588
|
+
this.queuePersist();
|
|
589
|
+
return this;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Check if the session error bag has any errors
|
|
593
|
+
*
|
|
594
|
+
* @param field
|
|
595
|
+
* @returns
|
|
596
|
+
*/
|
|
597
|
+
hasErrors(field) {
|
|
598
|
+
return this.errors.has(field);
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Clear all errors in the session error bag
|
|
602
|
+
*
|
|
603
|
+
* @param field
|
|
604
|
+
* @returns
|
|
605
|
+
*/
|
|
606
|
+
clearErrors(field) {
|
|
607
|
+
this.errors.clear(field);
|
|
608
|
+
this.queuePersist();
|
|
609
|
+
return this;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Parse session for views
|
|
613
|
+
*
|
|
614
|
+
* @returns
|
|
615
|
+
*/
|
|
616
|
+
forView() {
|
|
617
|
+
return {
|
|
618
|
+
...this.all(),
|
|
619
|
+
errors: this.errors,
|
|
620
|
+
flash: this.flashBag
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* Return session as json
|
|
625
|
+
*
|
|
626
|
+
* @returns
|
|
627
|
+
*/
|
|
628
|
+
toJSON() {
|
|
629
|
+
return {
|
|
630
|
+
...this.all(),
|
|
631
|
+
errors: this.errors.toJSON(),
|
|
632
|
+
flash: this.flashBag.toJSON()
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
const requestInput = () => {
|
|
637
|
+
const request = globalThis.request?.();
|
|
638
|
+
if (request instanceof Request$1) {
|
|
639
|
+
if (isRecord(request.body)) return request.body;
|
|
640
|
+
const source = isRecord(request.source) ? request.source : void 0;
|
|
641
|
+
if (source && typeof source.getBody === "function") return source.getBody() ?? {};
|
|
642
|
+
if (isRecord(source?.body)) return source.body;
|
|
643
|
+
}
|
|
644
|
+
if (isRecord(request) && typeof request.getBody === "function") return request.getBody() ?? {};
|
|
645
|
+
return isRecord(request?.body) ? request.body : {};
|
|
646
|
+
};
|
|
647
|
+
const old = (key, defaultValue) => {
|
|
648
|
+
const input = requestInput();
|
|
649
|
+
if (!key) return input;
|
|
650
|
+
return getPath(input, key, defaultValue);
|
|
651
|
+
};
|
|
652
|
+
const sweepRegisteredKey = Symbol.for("arkstack:http:flash-sweep-registered");
|
|
653
|
+
const attachSessionProperty = (target, session) => {
|
|
654
|
+
target.httpSession = session;
|
|
655
|
+
if (!("session" in target) || target.session instanceof Session) target.session = session;
|
|
656
|
+
};
|
|
657
|
+
const responseSource = (target) => {
|
|
658
|
+
return target.res ?? target.ctx?.res ?? target.response?.source ?? target.clearResponse?.source ?? target.context?.res ?? target.context?.response?.source;
|
|
659
|
+
};
|
|
660
|
+
const registerResponseFlashSweep = (target, session) => {
|
|
661
|
+
if (!isRecord(target)) return;
|
|
662
|
+
const current = session ?? getSession(target);
|
|
663
|
+
const res = responseSource(target);
|
|
664
|
+
if (!(current instanceof Session) || !isRecord(res) || typeof res.end !== "function" || res[sweepRegisteredKey]) return;
|
|
665
|
+
res[sweepRegisteredKey] = true;
|
|
666
|
+
const end = res.end.bind(res);
|
|
667
|
+
res.end = (...args) => {
|
|
668
|
+
current.sweepFlash().catch(() => void 0).finally(() => end(...args));
|
|
669
|
+
return res;
|
|
670
|
+
};
|
|
671
|
+
};
|
|
672
|
+
const attachViewState = (target, session) => {
|
|
673
|
+
attachSessionProperty(target, session);
|
|
674
|
+
target.errors = session.errors;
|
|
675
|
+
if (isRecord(target.req)) {
|
|
676
|
+
attachSessionProperty(target.req, session);
|
|
677
|
+
target.req.errors = session.errors;
|
|
678
|
+
target.req.old = old;
|
|
679
|
+
}
|
|
680
|
+
if (isRecord(target.res)) target.res.locals = {
|
|
681
|
+
...target.res.locals || {},
|
|
682
|
+
session,
|
|
683
|
+
errors: session.errors,
|
|
684
|
+
flash: session.flashBag,
|
|
685
|
+
old
|
|
686
|
+
};
|
|
687
|
+
if (isRecord(target.response?.source)) target.response.source.locals = {
|
|
688
|
+
...target.response.source.locals || {},
|
|
689
|
+
session,
|
|
690
|
+
errors: session.errors,
|
|
691
|
+
flash: session.flashBag,
|
|
692
|
+
old
|
|
693
|
+
};
|
|
694
|
+
if (isRecord(target.context)) {
|
|
695
|
+
attachSessionProperty(target.context, session);
|
|
696
|
+
target.context.errors = session.errors;
|
|
697
|
+
target.context.flash = session.flashBag;
|
|
698
|
+
target.context.old = old;
|
|
699
|
+
}
|
|
700
|
+
if (isRecord(target.state)) {
|
|
701
|
+
attachSessionProperty(target.state, session);
|
|
702
|
+
target.state.errors = session.errors;
|
|
703
|
+
target.state.flash = session.flashBag;
|
|
704
|
+
target.state.old = old;
|
|
705
|
+
}
|
|
706
|
+
if (typeof target.set === "function") {
|
|
707
|
+
target.set("session", session);
|
|
708
|
+
target.set("errors", session.errors);
|
|
709
|
+
target.set("flash", session.flashBag);
|
|
710
|
+
target.set("old", old);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
/**
|
|
714
|
+
* Ensure a valid session exists
|
|
715
|
+
*
|
|
716
|
+
* @param ctx
|
|
717
|
+
* @param initial
|
|
718
|
+
* @returns
|
|
719
|
+
*/
|
|
720
|
+
const ensureSession = (ctx, initial, persistent) => {
|
|
721
|
+
if (!isRecord(ctx)) return new Session(initial, persistent);
|
|
722
|
+
const existing = ctx[sessionKey] ?? (ctx.session instanceof Session ? ctx.session : void 0) ?? (isRecord(ctx.req) && ctx.req.httpSession instanceof Session ? ctx.req.httpSession : void 0);
|
|
723
|
+
const session = existing instanceof Session ? existing : new Session(initial, persistent);
|
|
724
|
+
ctx[sessionKey] = session;
|
|
725
|
+
attachViewState(ctx, session);
|
|
726
|
+
return session;
|
|
727
|
+
};
|
|
728
|
+
/**
|
|
729
|
+
* Get the current session
|
|
730
|
+
*
|
|
731
|
+
* @param ctx
|
|
732
|
+
* @returns
|
|
733
|
+
*/
|
|
734
|
+
const getSession = (ctx) => {
|
|
735
|
+
if (!isRecord(ctx)) return;
|
|
736
|
+
const session = ctx[sessionKey] ?? ctx.session;
|
|
737
|
+
return session instanceof Session ? session : void 0;
|
|
738
|
+
};
|
|
739
|
+
const generateSessionId = () => randomBytes(32).toString("base64url");
|
|
740
|
+
const signValue = (value, secret) => createHmac("sha256", secret).update(value).digest("base64url");
|
|
741
|
+
const encodeSignedValue = (value, secret) => `${value}.${signValue(value, secret)}`;
|
|
742
|
+
const decodeSignedValue = (value, secret) => {
|
|
743
|
+
if (!value) return void 0;
|
|
744
|
+
const index = value.lastIndexOf(".");
|
|
745
|
+
if (index < 1) return void 0;
|
|
746
|
+
const payload = value.slice(0, index);
|
|
747
|
+
const signature = value.slice(index + 1);
|
|
748
|
+
const expected = signValue(payload, secret);
|
|
749
|
+
const signatureBuffer = Buffer.from(signature);
|
|
750
|
+
const expectedBuffer = Buffer.from(expected);
|
|
751
|
+
if (signatureBuffer.length !== expectedBuffer.length) return void 0;
|
|
752
|
+
return timingSafeEqual(signatureBuffer, expectedBuffer) ? payload : void 0;
|
|
753
|
+
};
|
|
754
|
+
const decodeJson = (value) => {
|
|
755
|
+
if (!value) return void 0;
|
|
756
|
+
try {
|
|
757
|
+
return JSON.parse(Buffer.from(value, "base64url").toString("utf8"));
|
|
758
|
+
} catch {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
const parseCookies = (header) => {
|
|
763
|
+
return (Array.isArray(header) ? header.join("; ") : header || "").split(";").reduce((cookies, part) => {
|
|
764
|
+
const index = part.indexOf("=");
|
|
765
|
+
if (index < 0) return cookies;
|
|
766
|
+
const key = part.slice(0, index).trim();
|
|
767
|
+
const value = part.slice(index + 1).trim();
|
|
768
|
+
if (key) cookies[key] = decodeURIComponent(value);
|
|
769
|
+
return cookies;
|
|
770
|
+
}, {});
|
|
771
|
+
};
|
|
772
|
+
const getCookie = (context, name) => {
|
|
773
|
+
const ctx = isRecord(context.ctx) ? context.ctx : context;
|
|
774
|
+
const headers = (context.request || ctx.clearRequest || ctx.req || ctx.request)?.headers || ctx.headers || ctx.req?.headers || ctx.request?.headers;
|
|
775
|
+
return parseCookies(typeof headers?.get === "function" ? headers.get("cookie") : headers?.cookie)[name];
|
|
776
|
+
};
|
|
777
|
+
const serializeCookie = (name, value, options = {}) => {
|
|
778
|
+
const parts = [`${name}=${encodeURIComponent(value)}`];
|
|
779
|
+
if (typeof options.maxAge === "number") parts.push(`Max-Age=${Math.max(0, Math.floor(options.maxAge))}`);
|
|
780
|
+
if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);
|
|
781
|
+
parts.push(`Path=${options.path || "/"}`);
|
|
782
|
+
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
783
|
+
if (options.httpOnly !== false) parts.push("HttpOnly");
|
|
784
|
+
if (options.secure) parts.push("Secure");
|
|
785
|
+
if (options.sameSite) parts.push(`SameSite=${options.sameSite}`);
|
|
786
|
+
return parts.join("; ");
|
|
787
|
+
};
|
|
788
|
+
const splitSetCookieHeader = (value) => {
|
|
789
|
+
return value.split(/,\s*(?=[^;,\s]+=)/).filter(Boolean);
|
|
790
|
+
};
|
|
791
|
+
const withoutCookie = (current, cookieName) => {
|
|
792
|
+
return (Array.isArray(current) ? current.flatMap((item) => splitSetCookieHeader(String(item))) : typeof current === "string" ? splitSetCookieHeader(current) : []).filter((cookie) => !cookie.trim().startsWith(`${cookieName}=`));
|
|
793
|
+
};
|
|
794
|
+
const upsertHeaderValue = (target, headerName, cookieName, value) => {
|
|
795
|
+
if (!target) return false;
|
|
796
|
+
if (typeof target.setHeader === "function") {
|
|
797
|
+
const next = [...withoutCookie(typeof target.getHeader === "function" ? target.getHeader(headerName) : void 0, cookieName), value];
|
|
798
|
+
target.setHeader(headerName, next);
|
|
799
|
+
return true;
|
|
800
|
+
}
|
|
801
|
+
if (target.headers && typeof target.headers.set === "function") {
|
|
802
|
+
const next = [...withoutCookie(target.headers.get(headerName), cookieName), value];
|
|
803
|
+
target.headers.set(headerName, next.join(", "));
|
|
804
|
+
return true;
|
|
805
|
+
}
|
|
806
|
+
if (typeof target.appendHeader === "function") {
|
|
807
|
+
target.appendHeader(headerName, value);
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
810
|
+
if (typeof target.append === "function") {
|
|
811
|
+
target.append(headerName, value);
|
|
812
|
+
return true;
|
|
813
|
+
}
|
|
814
|
+
return false;
|
|
815
|
+
};
|
|
816
|
+
const setCookie = (context, name, value, options = {}) => {
|
|
817
|
+
const ctx = isRecord(context.ctx) ? context.ctx : context;
|
|
818
|
+
const cookie = serializeCookie(name, value, options);
|
|
819
|
+
const response = context.response || ctx.clearResponse;
|
|
820
|
+
if (response?.headers && typeof response.headers.set === "function") {
|
|
821
|
+
const next = [...withoutCookie(response.headers.get("set-cookie"), name), cookie];
|
|
822
|
+
response.headers.set("set-cookie", next.join(", "));
|
|
823
|
+
}
|
|
824
|
+
upsertHeaderValue(response?.source, "Set-Cookie", name, cookie) || upsertHeaderValue(ctx.res, "Set-Cookie", name, cookie) || upsertHeaderValue(ctx.response, "Set-Cookie", name, cookie) || upsertHeaderValue(ctx.response?.source, "Set-Cookie", name, cookie) || upsertHeaderValue(ctx.event?.res, "Set-Cookie", name, cookie);
|
|
825
|
+
return cookie;
|
|
826
|
+
};
|
|
827
|
+
const byteLength = (value) => Buffer.byteLength(value, "utf8");
|
|
828
|
+
const serializeValue = (value) => {
|
|
829
|
+
if (value === null || typeof value === "undefined") return "N;";
|
|
830
|
+
if (typeof value === "boolean") return `b:${value ? 1 : 0};`;
|
|
831
|
+
if (typeof value === "number") return Number.isInteger(value) ? `i:${value};` : `d:${value};`;
|
|
832
|
+
if (typeof value === "string") return `s:${byteLength(value)}:"${value}";`;
|
|
833
|
+
if (Array.isArray(value)) return serializeEntries(value.map((item, index) => [index, item]));
|
|
834
|
+
if (typeof value === "object") return serializeEntries(Object.entries(value));
|
|
835
|
+
return serializeValue(String(value));
|
|
836
|
+
};
|
|
837
|
+
const serializeEntries = (entries) => {
|
|
838
|
+
return `a:${entries.length}:{${entries.map(([key, value]) => serializeValue(key) + serializeValue(value)).join("")}}`;
|
|
839
|
+
};
|
|
840
|
+
var Parser = class {
|
|
841
|
+
source;
|
|
842
|
+
offset = 0;
|
|
843
|
+
constructor(source) {
|
|
844
|
+
this.source = source;
|
|
845
|
+
}
|
|
846
|
+
parse() {
|
|
847
|
+
const type = this.source[this.offset];
|
|
848
|
+
this.offset += type === "N" ? 1 : 2;
|
|
849
|
+
switch (type) {
|
|
850
|
+
case "N":
|
|
851
|
+
this.expect(";");
|
|
852
|
+
return null;
|
|
853
|
+
case "b": return this.readUntil(";") === "1";
|
|
854
|
+
case "i": return Number.parseInt(this.readUntil(";"), 10);
|
|
855
|
+
case "d": return Number.parseFloat(this.readUntil(";"));
|
|
856
|
+
case "s": return this.parseString();
|
|
857
|
+
case "a": return this.parseArray();
|
|
858
|
+
default: throw new Error(`Unsupported serialized session value: ${type}`);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
parseString() {
|
|
862
|
+
const length = Number.parseInt(this.readUntil(":"), 10);
|
|
863
|
+
this.expect("\"");
|
|
864
|
+
let end = this.offset;
|
|
865
|
+
let bytes = 0;
|
|
866
|
+
while (end < this.source.length && bytes < length) {
|
|
867
|
+
const char = this.source[end];
|
|
868
|
+
bytes += Buffer.byteLength(char, "utf8");
|
|
869
|
+
end += 1;
|
|
870
|
+
}
|
|
871
|
+
const value = this.source.slice(this.offset, end);
|
|
872
|
+
this.offset = end;
|
|
873
|
+
this.expect("\"");
|
|
874
|
+
this.expect(";");
|
|
875
|
+
return value;
|
|
876
|
+
}
|
|
877
|
+
parseArray() {
|
|
878
|
+
const length = Number.parseInt(this.readUntil(":"), 10);
|
|
879
|
+
this.expect("{");
|
|
880
|
+
const entries = [];
|
|
881
|
+
let sequential = true;
|
|
882
|
+
for (let index = 0; index < length; index += 1) {
|
|
883
|
+
const key = this.parse();
|
|
884
|
+
const value = this.parse();
|
|
885
|
+
entries.push([key, value]);
|
|
886
|
+
if (key !== index) sequential = false;
|
|
887
|
+
}
|
|
888
|
+
this.expect("}");
|
|
889
|
+
if (sequential) return entries.map(([, value]) => value);
|
|
890
|
+
return entries.reduce((record, [key, value]) => {
|
|
891
|
+
record[String(key)] = value;
|
|
892
|
+
return record;
|
|
893
|
+
}, {});
|
|
894
|
+
}
|
|
895
|
+
readUntil(token) {
|
|
896
|
+
const index = this.source.indexOf(token, this.offset);
|
|
897
|
+
if (index < 0) throw new Error("Invalid serialized session payload");
|
|
898
|
+
const value = this.source.slice(this.offset, index);
|
|
899
|
+
this.offset = index + token.length;
|
|
900
|
+
return value;
|
|
901
|
+
}
|
|
902
|
+
expect(token) {
|
|
903
|
+
if (this.source.slice(this.offset, this.offset + token.length) !== token) throw new Error("Invalid serialized session payload");
|
|
904
|
+
this.offset += token.length;
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
const encodeSessionPayload = (payload) => {
|
|
908
|
+
return serializeValue(payload);
|
|
909
|
+
};
|
|
910
|
+
const normalizeSessionPayload = (payload) => {
|
|
911
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return payload;
|
|
912
|
+
const record = payload;
|
|
913
|
+
for (const key of [
|
|
914
|
+
"data",
|
|
915
|
+
"errors",
|
|
916
|
+
"flash"
|
|
917
|
+
]) if (Array.isArray(record[key]) && record[key].length === 0) record[key] = {};
|
|
918
|
+
return record;
|
|
919
|
+
};
|
|
920
|
+
const decodeSessionPayload = (value) => {
|
|
921
|
+
if (!value) return;
|
|
922
|
+
try {
|
|
923
|
+
return normalizeSessionPayload(new Parser(value).parse());
|
|
924
|
+
} catch {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
const keyFromSecret = (secret) => {
|
|
929
|
+
if (secret.startsWith("base64:")) {
|
|
930
|
+
const decoded = Buffer.from(secret.slice(7), "base64");
|
|
931
|
+
if (decoded.length === 32) return decoded;
|
|
932
|
+
}
|
|
933
|
+
const raw = Buffer.from(secret, "base64");
|
|
934
|
+
if (raw.length === 32) return raw;
|
|
935
|
+
return createHash("sha256").update(secret).digest();
|
|
936
|
+
};
|
|
937
|
+
const macFor = (iv, value, key) => {
|
|
938
|
+
return createHmac("sha256", key).update(iv + value).digest("hex");
|
|
939
|
+
};
|
|
940
|
+
const encryptSessionValue = (value, secret) => {
|
|
941
|
+
const key = keyFromSecret(secret);
|
|
942
|
+
const iv = randomBytes(16);
|
|
943
|
+
const ivValue = iv.toString("base64");
|
|
944
|
+
const cipher = createCipheriv("aes-256-cbc", key, iv);
|
|
945
|
+
const encrypted = Buffer.concat([cipher.update(value, "utf8"), cipher.final()]).toString("base64");
|
|
946
|
+
const payload = {
|
|
947
|
+
iv: ivValue,
|
|
948
|
+
value: encrypted,
|
|
949
|
+
mac: macFor(ivValue, encrypted, key),
|
|
950
|
+
tag: ""
|
|
951
|
+
};
|
|
952
|
+
return JSON.stringify(payload);
|
|
953
|
+
};
|
|
954
|
+
const decryptSessionValue = (payload, secret) => {
|
|
955
|
+
if (!payload) return;
|
|
956
|
+
try {
|
|
957
|
+
const decoded = JSON.parse(payload.startsWith("{") ? payload : Buffer.from(payload, "base64").toString("utf8"));
|
|
958
|
+
if (!decoded.iv || !decoded.value || !decoded.mac) return;
|
|
959
|
+
const key = keyFromSecret(secret);
|
|
960
|
+
const expected = macFor(decoded.iv, decoded.value, key);
|
|
961
|
+
const actualBuffer = Buffer.from(decoded.mac);
|
|
962
|
+
const expectedBuffer = Buffer.from(expected);
|
|
963
|
+
if (actualBuffer.length !== expectedBuffer.length || !timingSafeEqual(actualBuffer, expectedBuffer)) return;
|
|
964
|
+
const decipher = createDecipheriv("aes-256-cbc", key, Buffer.from(decoded.iv, "base64"));
|
|
965
|
+
return Buffer.concat([decipher.update(Buffer.from(decoded.value, "base64")), decipher.final()]).toString("utf8");
|
|
966
|
+
} catch {
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
};
|
|
970
|
+
const defaultSecret = () => String(process.env.SESSION_SECRET || process.env.APP_KEY || "arkstack-session-secret");
|
|
971
|
+
const defaultcookie_options = (ttl) => ({
|
|
972
|
+
httpOnly: true,
|
|
973
|
+
sameSite: "Lax",
|
|
974
|
+
secure: process.env.NODE_ENV === "production",
|
|
975
|
+
path: "/",
|
|
976
|
+
maxAge: ttl
|
|
977
|
+
});
|
|
978
|
+
var BaseSessionDriver = class {
|
|
979
|
+
cookie;
|
|
980
|
+
secret;
|
|
981
|
+
ttl;
|
|
982
|
+
cookie_options;
|
|
983
|
+
constructor(options = {}) {
|
|
984
|
+
this.cookie = options.cookie || "arkstack_session";
|
|
985
|
+
this.secret = options.secret || defaultSecret();
|
|
986
|
+
this.ttl = options.ttl;
|
|
987
|
+
this.cookie_options = {
|
|
988
|
+
...defaultcookie_options(options.ttl),
|
|
989
|
+
...options.cookie_options || {}
|
|
990
|
+
};
|
|
991
|
+
}
|
|
992
|
+
readSessionId(context) {
|
|
993
|
+
return decodeSignedValue(getCookie(context, this.cookie), this.secret);
|
|
994
|
+
}
|
|
995
|
+
encryptPayload(value) {
|
|
996
|
+
return encryptSessionValue(value, this.secret);
|
|
997
|
+
}
|
|
998
|
+
decryptPayload(value) {
|
|
999
|
+
return decryptSessionValue(value, this.secret);
|
|
1000
|
+
}
|
|
1001
|
+
writeSessionId(context, id) {
|
|
1002
|
+
setCookie(context, this.cookie, encodeSignedValue(id, this.secret), this.cookie_options);
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
var CookieSessionDriver = class extends BaseSessionDriver {
|
|
1006
|
+
async start(context) {
|
|
1007
|
+
const cookie = getCookie(context, this.cookie);
|
|
1008
|
+
const decoded = this.decryptPayload(cookie) ?? decodeSignedValue(cookie, this.secret);
|
|
1009
|
+
const payload = decodeSessionPayload(decoded) ?? decodeJson(decoded);
|
|
1010
|
+
const id = payload?.id || generateSessionId();
|
|
1011
|
+
const state = payload ? {
|
|
1012
|
+
data: payload.data,
|
|
1013
|
+
errors: payload.errors,
|
|
1014
|
+
flash: payload.flash
|
|
1015
|
+
} : void 0;
|
|
1016
|
+
const save = async (next) => {
|
|
1017
|
+
setCookie(context, this.cookie, this.encryptPayload(encodeSessionPayload({
|
|
1018
|
+
id,
|
|
1019
|
+
...next
|
|
1020
|
+
})), this.cookie_options);
|
|
1021
|
+
};
|
|
1022
|
+
const destroy = async () => {
|
|
1023
|
+
setCookie(context, this.cookie, "", {
|
|
1024
|
+
...this.cookie_options,
|
|
1025
|
+
maxAge: 0,
|
|
1026
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
1027
|
+
});
|
|
1028
|
+
};
|
|
1029
|
+
return {
|
|
1030
|
+
id,
|
|
1031
|
+
state,
|
|
1032
|
+
save,
|
|
1033
|
+
destroy
|
|
1034
|
+
};
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
var DatabaseSessionDriver = class extends BaseSessionDriver {
|
|
1038
|
+
tableName;
|
|
1039
|
+
constructor(options = {}) {
|
|
1040
|
+
super(options);
|
|
1041
|
+
this.tableName = options.table || "sessions";
|
|
1042
|
+
}
|
|
1043
|
+
table() {
|
|
1044
|
+
return DB.table(this.tableName);
|
|
1045
|
+
}
|
|
1046
|
+
async start(context) {
|
|
1047
|
+
const id = this.readSessionId(context) || generateSessionId();
|
|
1048
|
+
this.writeSessionId(context, id);
|
|
1049
|
+
const row = await this.table().where({ id }).first();
|
|
1050
|
+
const state = isRecord(row) && typeof row.payload === "string" ? decodeSessionPayload(this.decryptPayload(row.payload) ?? row.payload) ?? decodeJson(this.decryptPayload(row.payload) ?? row.payload) : isRecord(row?.payload) ? row.payload : void 0;
|
|
1051
|
+
const save = async (payload) => {
|
|
1052
|
+
const now = /* @__PURE__ */ new Date();
|
|
1053
|
+
const values = {
|
|
1054
|
+
id,
|
|
1055
|
+
payload: this.encryptPayload(encodeSessionPayload(payload)),
|
|
1056
|
+
updatedAt: now,
|
|
1057
|
+
expiresAt: this.ttl ? new Date(now.getTime() + this.ttl * 1e3) : null
|
|
1058
|
+
};
|
|
1059
|
+
if (await this.table().where({ id }).first()) await this.table().where({ id }).update(values);
|
|
1060
|
+
else await this.table().insert({
|
|
1061
|
+
...values,
|
|
1062
|
+
createdAt: now
|
|
1063
|
+
});
|
|
1064
|
+
this.writeSessionId(context, id);
|
|
1065
|
+
};
|
|
1066
|
+
const destroy = async () => {
|
|
1067
|
+
await this.table().where({ id }).delete();
|
|
1068
|
+
setCookie(context, this.cookie, "", {
|
|
1069
|
+
...this.cookie_options,
|
|
1070
|
+
maxAge: 0,
|
|
1071
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
1072
|
+
});
|
|
1073
|
+
};
|
|
1074
|
+
return {
|
|
1075
|
+
id,
|
|
1076
|
+
state,
|
|
1077
|
+
save,
|
|
1078
|
+
destroy
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
var FileSessionDriver = class extends BaseSessionDriver {
|
|
1083
|
+
directory;
|
|
1084
|
+
constructor(options = {}) {
|
|
1085
|
+
super(options);
|
|
1086
|
+
this.directory = options.directory || join(Arkstack.rootDir(), "storage", "framework", "sessions");
|
|
1087
|
+
}
|
|
1088
|
+
path(id) {
|
|
1089
|
+
return join(this.directory, id);
|
|
1090
|
+
}
|
|
1091
|
+
async start(context) {
|
|
1092
|
+
const id = this.readSessionId(context) || generateSessionId();
|
|
1093
|
+
this.writeSessionId(context, id);
|
|
1094
|
+
let state;
|
|
1095
|
+
try {
|
|
1096
|
+
const contents = await readFile(this.path(id), "utf8");
|
|
1097
|
+
const payload = this.decryptPayload(contents) ?? contents;
|
|
1098
|
+
state = decodeSessionPayload(payload) ?? JSON.parse(payload);
|
|
1099
|
+
} catch {
|
|
1100
|
+
state = void 0;
|
|
1101
|
+
}
|
|
1102
|
+
const save = async (payload) => {
|
|
1103
|
+
const path = this.path(id);
|
|
1104
|
+
await mkdir(dirname(path), { recursive: true });
|
|
1105
|
+
await writeFile(path, this.encryptPayload(encodeSessionPayload(payload)), "utf8");
|
|
1106
|
+
this.writeSessionId(context, id);
|
|
1107
|
+
};
|
|
1108
|
+
const destroy = async () => {
|
|
1109
|
+
await rm(this.path(id), { force: true });
|
|
1110
|
+
setCookie(context, this.cookie, "", {
|
|
1111
|
+
...this.cookie_options,
|
|
1112
|
+
maxAge: 0,
|
|
1113
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
1114
|
+
});
|
|
1115
|
+
};
|
|
1116
|
+
return {
|
|
1117
|
+
id,
|
|
1118
|
+
state,
|
|
1119
|
+
save,
|
|
1120
|
+
destroy
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
};
|
|
1124
|
+
let configuredDriver;
|
|
1125
|
+
const readAppSessionConfig = () => {
|
|
1126
|
+
try {
|
|
1127
|
+
if (!globalThis.config) return;
|
|
1128
|
+
return {
|
|
1129
|
+
driver: config("session.driver", "cookie"),
|
|
1130
|
+
cookie: config("session.cookie", "arkstack_session"),
|
|
1131
|
+
secret: config("session.secret"),
|
|
1132
|
+
ttl: config("session.ttl", 3600 * 24 * 7),
|
|
1133
|
+
cookie_options: {
|
|
1134
|
+
path: config("session.path", "/"),
|
|
1135
|
+
httpOnly: config("session.http_only", true),
|
|
1136
|
+
secure: config("session.secure", true),
|
|
1137
|
+
sameSite: config("session.same_site", "Lax")
|
|
1138
|
+
},
|
|
1139
|
+
file: { directory: config("session.directory") },
|
|
1140
|
+
database: { table: config("session.table", "sessions") }
|
|
1141
|
+
};
|
|
1142
|
+
} catch {
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
const createSessionDriver = (config = {}) => {
|
|
1147
|
+
if (config.driver && typeof config.driver !== "string") return config.driver;
|
|
1148
|
+
const common = {
|
|
1149
|
+
cookie: config.cookie,
|
|
1150
|
+
secret: config.secret,
|
|
1151
|
+
ttl: config.ttl,
|
|
1152
|
+
cookie_options: config.cookie_options
|
|
1153
|
+
};
|
|
1154
|
+
switch (config.driver || "cookie") {
|
|
1155
|
+
case "file": return new FileSessionDriver({
|
|
1156
|
+
...common,
|
|
1157
|
+
directory: config.file?.directory
|
|
1158
|
+
});
|
|
1159
|
+
case "database": return new DatabaseSessionDriver({
|
|
1160
|
+
...common,
|
|
1161
|
+
table: config.database?.table
|
|
1162
|
+
});
|
|
1163
|
+
default: return new CookieSessionDriver(common);
|
|
1164
|
+
}
|
|
1165
|
+
};
|
|
1166
|
+
const getSessionDriver = () => {
|
|
1167
|
+
if (!configuredDriver) configuredDriver = createSessionDriver(readAppSessionConfig());
|
|
1168
|
+
return configuredDriver;
|
|
1169
|
+
};
|
|
1170
|
+
definePlugin({
|
|
1171
|
+
name: "arkstack-http",
|
|
1172
|
+
setup: ({ bind, useHttpContext }) => {
|
|
1173
|
+
bind(Session, ({ ctx }) => ensureSession(ctx));
|
|
1174
|
+
useHttpContext(async (context) => {
|
|
1175
|
+
globalThis.request = (key) => key ? context.request.input(key) : context.request;
|
|
1176
|
+
const persistent = await getSessionDriver().start(context);
|
|
1177
|
+
const session = ensureSession(context.ctx, persistent.state, persistent);
|
|
1178
|
+
context.httpSession = session;
|
|
1179
|
+
if (!("session" in context) || context.session instanceof Session) context.session = session;
|
|
1180
|
+
context.errors = session.errors;
|
|
1181
|
+
attachViewState(context, session);
|
|
1182
|
+
registerResponseFlashSweep(context, session);
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
});
|
|
1186
|
+
definePlugin$1({
|
|
1187
|
+
name: "kanun-session-plugin",
|
|
1188
|
+
install({ onValidationError }) {
|
|
1189
|
+
onValidationError((validator) => {
|
|
1190
|
+
const currentSession = globalThis.session?.();
|
|
1191
|
+
if (currentSession instanceof Session) currentSession.addValidationErrors(validator);
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
});
|
|
1195
|
+
//#endregion
|
|
63
1196
|
//#region src/Router.ts
|
|
64
1197
|
registerPlugin(clearRouterH3Plugin);
|
|
65
1198
|
Router$1.configure({ inferParamName: true });
|
|
66
1199
|
var Router = class extends Router$1 {
|
|
67
1200
|
static async bind(app) {
|
|
68
1201
|
try {
|
|
69
|
-
|
|
70
|
-
await importFile(join(Arkstack.rootDir(), "src/routes/api.ts"));
|
|
71
|
-
});
|
|
1202
|
+
await Router$1.group("/api", join(Arkstack.rootDir(), "src/routes/api.ts"));
|
|
72
1203
|
} catch {}
|
|
73
1204
|
try {
|
|
74
|
-
|
|
75
|
-
await importFile(join(Arkstack.rootDir(), "src/routes/web.ts"));
|
|
76
|
-
});
|
|
1205
|
+
await Router$1.group("/", join(Arkstack.rootDir(), "src/routes/web.ts"));
|
|
77
1206
|
} catch {}
|
|
78
1207
|
return Router$1.apply(app);
|
|
79
1208
|
}
|
|
@@ -148,12 +1277,13 @@ var H3Driver = class extends ArkstackKitDriver {
|
|
|
148
1277
|
const mw = Array.isArray(middleware) ? middleware[0] : middleware;
|
|
149
1278
|
const conf = Array.isArray(middleware) && middleware[1] ? middleware[1] : {};
|
|
150
1279
|
if (typeof mw === "function") {
|
|
151
|
-
app.use(mw, conf);
|
|
1280
|
+
app.use(resolveMiddleware(mw), conf);
|
|
152
1281
|
return;
|
|
153
1282
|
}
|
|
154
1283
|
for (const [pos, entries] of Object.entries(middleware)) for (const entry of entries) {
|
|
155
|
-
const
|
|
1284
|
+
const instance = Array.isArray(entry) ? entry[0] : entry;
|
|
156
1285
|
const conf = Array.isArray(entry) && entry[1] ? entry[1] : {};
|
|
1286
|
+
const mw = resolveMiddleware(instance);
|
|
157
1287
|
if (pos === "after") app.use(async (evt, next) => {
|
|
158
1288
|
evt[Symbol.for("h3.internal.event.res")] = new H3EventResponse(await toResponse(await next(), evt));
|
|
159
1289
|
await mw(evt, next);
|