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