@h3ravel/session 0.1.0-alpha.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,774 @@
1
+ /// <reference path="./app.globals.d.ts" />
2
+ import * as _h3ravel_contracts0 from "@h3ravel/contracts";
3
+ import { IApplication, IHttpContext, ISessionDriver, ISessionManager, SessionDriverBuilder, SessionDriverOption } from "@h3ravel/contracts";
4
+ import { Command } from "@h3ravel/musket";
5
+ import { ServiceProvider } from "@h3ravel/support";
6
+
7
+ //#region src/adapters.d.ts
8
+ /**
9
+ * FileDriver builder
10
+ * constructor(sessionId: string, sessionDir?: string, cwd?: string)
11
+ */
12
+ declare const fileBuilder: SessionDriverBuilder;
13
+ /**
14
+ * DatabaseDriver builder
15
+ * constructor(sessionId: string, table?: string)
16
+ */
17
+ declare const dbBuilder: SessionDriverBuilder;
18
+ /**
19
+ * MemoryDriver builder
20
+ * constructor(sessionId: string)
21
+ */
22
+ declare const memoryBuilder: SessionDriverBuilder;
23
+ /**
24
+ * RedisDriver builder
25
+ * constructor(sessionId: string, redisClient?: RedisClient, prefix?: string)
26
+ */
27
+ declare const redisBuilder: SessionDriverBuilder;
28
+ //#endregion
29
+ //#region src/Commands/MakeSessionTableCommand.d.ts
30
+ declare class MakeSessionTableCommand extends Command {
31
+ /**
32
+ * The name and signature of the console command.
33
+ *
34
+ * @var string
35
+ */
36
+ protected signature: string;
37
+ /**
38
+ * The console command description.
39
+ *
40
+ * @var string
41
+ */
42
+ protected description: string;
43
+ handle(): Promise<void>;
44
+ }
45
+ //#endregion
46
+ //#region src/Encryption.d.ts
47
+ declare class Encryption {
48
+ private key;
49
+ constructor();
50
+ /**
51
+ * Encrypt session data using AES-256-CBC and the APP_KEY.
52
+ */
53
+ encrypt(value: any): string;
54
+ /**
55
+ * Decrypt session data.
56
+ */
57
+ decrypt(value: any): any;
58
+ }
59
+ //#endregion
60
+ //#region src/FlashBag.d.ts
61
+ /**
62
+ * FlashBag
63
+ *
64
+ * Manages flash data for session management, handling temporary data
65
+ * that persists for one request cycle.
66
+ */
67
+ declare class FlashBag {
68
+ /**
69
+ * Storage for flash data
70
+ *
71
+ * Structure:
72
+ * {
73
+ * new: { key1: value1, key2: value2 },
74
+ * old: { key3: value3, key4: value4 }
75
+ * }
76
+ */
77
+ private flashData;
78
+ /**
79
+ * Flash a value for the next request
80
+ *
81
+ * @param key Key to store in flash
82
+ * @param value Value to be flashed
83
+ */
84
+ flash(key: string, value: any): void;
85
+ /**
86
+ * Store a temporary value for the current request only
87
+ *
88
+ * @param key Key to store
89
+ * @param value Value to store
90
+ */
91
+ now(key: string, value: any): void;
92
+ /**
93
+ * Reflash all current flash data for another request cycle
94
+ */
95
+ reflash(): void;
96
+ /**
97
+ * Keep only specific flash keys for the next request
98
+ *
99
+ * @param keys Keys to keep
100
+ */
101
+ keep(keys: string[]): void;
102
+ /**
103
+ * Age flash data at the end of the request
104
+ *
105
+ * - Removes old flash data
106
+ * - Moves new flash data to old
107
+ * - Clears new flash data
108
+ */
109
+ ageFlashData(): void;
110
+ /**
111
+ * Get a flash value
112
+ *
113
+ * @param key Key to retrieve
114
+ * @param defaultValue Default value if key doesn't exist
115
+ * @returns Flash value or default
116
+ */
117
+ get(key: string, defaultValue?: any): any;
118
+ /**
119
+ * Check if a flash key exists
120
+ *
121
+ * @param key Key to check
122
+ * @returns Boolean indicating existence
123
+ */
124
+ has(key: string): boolean;
125
+ /**
126
+ * Get all flash data
127
+ *
128
+ * @returns Combined flash data
129
+ */
130
+ all(): Record<string, any>;
131
+ /**
132
+ * Get all flash data keys
133
+ *
134
+ * @returns Combined flash data
135
+ */
136
+ keys(): string[];
137
+ /**
138
+ * Get the raww flash data
139
+ *
140
+ * @returns raw flash data
141
+ */
142
+ raw(): Record<string, any>;
143
+ /**
144
+ * Clear all flash data
145
+ */
146
+ clear(): void;
147
+ }
148
+ //#endregion
149
+ //#region src/drivers/Driver.d.ts
150
+ /**
151
+ * Driver
152
+ *
153
+ * Base Session driver.
154
+ */
155
+ declare abstract class Driver extends ISessionDriver {
156
+ protected encryptor: Encryption;
157
+ protected sessionId: string;
158
+ flashBag: FlashBag;
159
+ /**
160
+ * Invalidate session completely and regenerate empty session.
161
+ */
162
+ abstract invalidate(): void;
163
+ /**
164
+ * Fetch current payload
165
+ *
166
+ * @returns
167
+ */
168
+ protected abstract fetchPayload<T extends Record<string, any>>(loadFlash?: boolean): T | Promise<T>;
169
+ /**
170
+ * Save updated payload
171
+ *
172
+ * @param payload
173
+ */
174
+ protected abstract savePayload(payload: Record<string, any>): void | Promise<void>;
175
+ /**
176
+ * Save the raw session payload (session + flash)
177
+ */
178
+ private saveRawPayload;
179
+ /**
180
+ * Retrieve all data from the session including flash
181
+ *
182
+ * @returns
183
+ */
184
+ getAll<T = any>(): T | Promise<T>;
185
+ /**
186
+ * Retrieve a value from the session
187
+ *
188
+ * @param key
189
+ * @param defaultValue
190
+ * @returns
191
+ */
192
+ get<T = any>(key: string, defaultValue?: any): T | Promise<T>;
193
+ /**
194
+ * Store a value in the session
195
+ *
196
+ * @param key
197
+ * @param value
198
+ */
199
+ set(value: Record<string, any>): void | Promise<void>;
200
+ /**
201
+ * Store multiple key/value pairs
202
+ *
203
+ * @param values
204
+ */
205
+ put(key: string, value: any): void | Promise<void>;
206
+ /**
207
+ * Append a value to an array key
208
+ *
209
+ * @param key
210
+ * @param value
211
+ */
212
+ push(key: string, value: any): void | Promise<void>;
213
+ /**
214
+ * Remove a key from the session
215
+ *
216
+ * @param key
217
+ */
218
+ forget(key: string): void | Promise<void>;
219
+ /**
220
+ * Retrieve all session data
221
+ *
222
+ * @returns
223
+ */
224
+ all<T extends Record<string, any>>(): T | Promise<T>;
225
+ /**
226
+ * Determine if a key exists (even if null).
227
+ *
228
+ * @param key
229
+ * @returns
230
+ */
231
+ exists(key: string): boolean | Promise<boolean>;
232
+ /**
233
+ * Determine if a key has a non-null value.
234
+ *
235
+ * @param key
236
+ * @returns
237
+ */
238
+ has(key: string): boolean | Promise<boolean>;
239
+ /**
240
+ * Get only specific keys.
241
+ *
242
+ * @param keys
243
+ * @returns
244
+ */
245
+ only<T extends Record<string, any>>(keys: string[]): T | Promise<T>;
246
+ /**
247
+ * Return all keys except the specified ones.
248
+ *
249
+ * @param keys
250
+ * @returns
251
+ */
252
+ except<T extends Record<string, any>>(keys: string[]): T | Promise<T>;
253
+ /**
254
+ * Return and delete a key from the session.
255
+ *
256
+ * @param key
257
+ * @param defaultValue
258
+ * @returns
259
+ */
260
+ pull<T = any>(key: string, defaultValue?: any): T | Promise<T>;
261
+ /**
262
+ * Increment a numeric value by amount (default 1).
263
+ *
264
+ * @param key
265
+ * @param amount
266
+ * @returns
267
+ */
268
+ increment(key: string, amount?: number): number | Promise<number>;
269
+ /**
270
+ * Decrement a numeric value by amount (default 1).
271
+ *
272
+ * @param key
273
+ * @param amount
274
+ * @returns
275
+ */
276
+ decrement(key: string, amount?: number): number | Promise<number>;
277
+ /**
278
+ * Flash a value for next request only.
279
+ *
280
+ * @param key
281
+ * @param value
282
+ */
283
+ flash(key: string, value: any): void | Promise<void>;
284
+ /**
285
+ * Reflash all flash data for one more cycle.
286
+ *
287
+ * @returns
288
+ */
289
+ reflash(): void | Promise<void>;
290
+ /**
291
+ * Keep only selected flash data.
292
+ *
293
+ * @param keys
294
+ * @returns
295
+ */
296
+ keep(keys: string[]): void | Promise<void>;
297
+ /**
298
+ * Store a temporary value (flash) for this request only (not persisted)
299
+ *
300
+ * @param key
301
+ * @param value
302
+ */
303
+ now(key: string, value: any): void | Promise<void>;
304
+ /**
305
+ * Regenerate session ID and persist data under new ID.
306
+ */
307
+ regenerate(): void | Promise<void>;
308
+ /**
309
+ * Age flash data at the end of the request lifecycle.
310
+ */
311
+ ageFlashData(): void | Promise<void>;
312
+ /**
313
+ * Determine if an item is not present in the session.
314
+ *
315
+ * @param key
316
+ * @returns
317
+ */
318
+ missing(key: string): boolean | Promise<boolean>;
319
+ /**
320
+ * Flush all session data
321
+ */
322
+ flush(): void | Promise<void>;
323
+ }
324
+ //#endregion
325
+ //#region src/drivers/DatabaseDriver.d.ts
326
+ /**
327
+ * DatabaseDriver
328
+ *
329
+ * Stores sessions in a database table. Each session ID maps to a row.
330
+ * The `payload` column contains all session key/value pairs as JSON.
331
+ */
332
+ declare class DatabaseDriver extends Driver implements ISessionDriver {
333
+ protected sessionId: string;
334
+ private table;
335
+ /**
336
+ *
337
+ * @param sessionId The current session ID
338
+ * @param table
339
+ */
340
+ constructor(sessionId: string, table?: string);
341
+ /**
342
+ * Get the query builder for this table
343
+ */
344
+ private query;
345
+ /**
346
+ * Fetch the session payload
347
+ */
348
+ protected fetchPayload<T extends Record<string, any>>(): Promise<T>;
349
+ /**
350
+ * Save the session payload back to DB
351
+ */
352
+ protected savePayload(payload: Record<string, any>): Promise<void>;
353
+ /**
354
+ * Retrieve all data from the session including flash
355
+ */
356
+ getAll<T = any>(): Promise<T>;
357
+ /**
358
+ * Get a value from the session
359
+ */
360
+ get<T = any>(key: string, defaultValue?: any): Promise<T>;
361
+ /**
362
+ * Set one or multiple session values
363
+ */
364
+ set(values: Record<string, any>): Promise<void>;
365
+ /**
366
+ * Store a single key/value pair
367
+ */
368
+ put(key: string, value: any): Promise<void>;
369
+ /**
370
+ * Append a value to an array key
371
+ */
372
+ push(key: string, value: any): Promise<void>;
373
+ /**
374
+ * Forget a session key
375
+ */
376
+ forget(key: string): Promise<void>;
377
+ /**
378
+ * Retrieve all session data (excluding flash)
379
+ */
380
+ all<T extends Record<string, any>>(): Promise<T>;
381
+ /**
382
+ * Determine if a key exists (even if null)
383
+ */
384
+ exists(key: string): Promise<boolean>;
385
+ /**
386
+ * Determine if a key has a non-null value
387
+ */
388
+ has(key: string): Promise<boolean>;
389
+ /**
390
+ * Get only specific keys
391
+ */
392
+ only<T extends Record<string, any>>(keys: string[]): Promise<T>;
393
+ /**
394
+ * Return all except specific keys
395
+ */
396
+ except<T extends Record<string, any>>(keys: string[]): Promise<T>;
397
+ /**
398
+ * Retrieve and delete a value
399
+ */
400
+ pull(key: string, defaultValue?: any): Promise<any>;
401
+ /**
402
+ * Increment a numeric value
403
+ */
404
+ increment(key: string, amount?: number): Promise<number>;
405
+ /**
406
+ * Decrement a numeric value
407
+ */
408
+ decrement(key: string, amount?: number): Promise<number>;
409
+ /**
410
+ * Flash a value for next request only
411
+ */
412
+ flash(key: string, value: any): Promise<void>;
413
+ /**
414
+ * Reflash all flash data for one more cycle
415
+ */
416
+ reflash(): Promise<void>;
417
+ /**
418
+ * Keep only specific flash keys
419
+ */
420
+ keep(keys: string[]): Promise<void>;
421
+ /**
422
+ * Store a temporary value (flash) for this request only (not persisted)
423
+ */
424
+ now(key: string, value: any): Promise<void>;
425
+ /**
426
+ * Regenerate session ID with same data
427
+ */
428
+ regenerate(): Promise<void>;
429
+ /**
430
+ * Check if a key is missing
431
+ */
432
+ missing(key: string): Promise<boolean>;
433
+ /**
434
+ * Flush all session data
435
+ */
436
+ flush(): Promise<void>;
437
+ /**
438
+ * Invalidate the session and regenerate
439
+ */
440
+ invalidate(): Promise<void>;
441
+ /**
442
+ * Age flash data at the end of the request lifecycle.
443
+ */
444
+ ageFlashData(): Promise<void>;
445
+ }
446
+ //#endregion
447
+ //#region src/drivers/FileDriver.d.ts
448
+ /**
449
+ * FileDriver
450
+ *
451
+ * Stores session data as encrypted JSON files.
452
+ * Each session is stored in its own file named after the session ID.
453
+ * Ideal for local development or low-scale deployments.
454
+ */
455
+ declare class FileDriver extends Driver implements ISessionDriver {
456
+ protected sessionId: string;
457
+ private sessionDir;
458
+ private cwd;
459
+ constructor(sessionId: string, sessionDir?: string, cwd?: string);
460
+ /**
461
+ * Ensures the session file exists and is initialized.
462
+ */
463
+ private ensureSessionFile;
464
+ /**
465
+ * Get the absolute path for the current session file.
466
+ */
467
+ private sessionFilePath;
468
+ /**
469
+ * Read raw decrypted payload (including _flash).
470
+ */
471
+ private readRawPayload;
472
+ /**
473
+ * Fetch decrypted payload and strip out flash metadata.
474
+ */
475
+ protected fetchPayload<T extends Record<string, any>>(): T;
476
+ /**
477
+ * Write and encrypt session data to file.
478
+ * Always persists flash state.
479
+ *
480
+ * @param data
481
+ */
482
+ protected savePayload(payload: Record<string, any>): void;
483
+ /**
484
+ * Completely invalidate the current session and regenerate a new one.
485
+ */
486
+ invalidate(): void;
487
+ }
488
+ //#endregion
489
+ //#region src/drivers/MemoryDriver.d.ts
490
+ /**
491
+ * MemoryDriver
492
+ *
493
+ * Lightweight, ephemeral session storage.
494
+ * Intended for tests, local development, or short-lived apps.
495
+ */
496
+ declare class MemoryDriver extends Driver implements ISessionDriver {
497
+ protected sessionId: string;
498
+ private static store;
499
+ constructor(sessionId: string);
500
+ /**
501
+ * Fetch and return session payload.
502
+ *
503
+ * @returns Decrypted and usable payload
504
+ */
505
+ protected fetchPayload<T extends Record<string, any>>(): T;
506
+ /**
507
+ * Persist session payload and flash bag state.
508
+ *
509
+ * @param data
510
+ */
511
+ protected savePayload(payload: Record<string, any>): void;
512
+ /**
513
+ * Invalidate current session and regenerate new session ID.
514
+ */
515
+ invalidate(): void;
516
+ }
517
+ //#endregion
518
+ //#region src/drivers/RedisDriver.d.ts
519
+ /**
520
+ * RedisDriver (placeholder)
521
+ */
522
+ declare class RedisDriver extends Driver implements ISessionDriver {
523
+ /**
524
+ * The current session ID
525
+ */
526
+ protected sessionId: string;
527
+ protected redisClient?: "RedisClient" | undefined;
528
+ protected prefix?: string | undefined;
529
+ private static store;
530
+ constructor(
531
+ /**
532
+ * The current session ID
533
+ */
534
+ sessionId: string, redisClient?: "RedisClient" | undefined, prefix?: string | undefined);
535
+ /**
536
+ * Fetch and return session payload.
537
+ *
538
+ * @returns Decrypted and usable payload
539
+ */
540
+ protected fetchPayload<T extends Record<string, any>>(): T;
541
+ /**
542
+ * Persist session payload and flash bag state.
543
+ *
544
+ * @param data
545
+ */
546
+ protected savePayload(_payload: Record<string, any>): void;
547
+ /**
548
+ * Invalidate current session and regenerate new session ID.
549
+ */
550
+ invalidate(): void;
551
+ }
552
+ //#endregion
553
+ //#region src/Providers/SessionServiceProvider.d.ts
554
+ declare class SessionServiceProvider extends ServiceProvider {
555
+ static priority: number;
556
+ static order: string;
557
+ register(): void;
558
+ }
559
+ //#endregion
560
+ //#region src/SessionManager.d.ts
561
+ /**
562
+ * SessionManager
563
+ *
564
+ * Handles session initialization, ID generation, and encryption.
565
+ * Each request gets a unique session namespace tied to its ID.
566
+ */
567
+ declare class SessionManager extends ISessionManager {
568
+ private app;
569
+ private ctx;
570
+ private driver;
571
+ private appKey;
572
+ private sessionId;
573
+ private request;
574
+ flashBag: FlashBag;
575
+ /**
576
+ * @param ctx - incoming request http context
577
+ * @param driverName - registered driver key ('file' | 'database' | 'memory' | 'redis')
578
+ * @param driverOptions - optional bag for driver-specific options
579
+ */
580
+ constructor(app?: IApplication, driverName?: 'file' | 'memory' | 'database' | 'redis', driverOptions?: SessionDriverOption);
581
+ constructor(app?: IHttpContext | IApplication, driverName?: 'file' | 'memory' | 'database' | 'redis', driverOptions?: SessionDriverOption);
582
+ /**
583
+ * Initialize the Session Manager
584
+ *
585
+ * @param ctx
586
+ * @returns
587
+ */
588
+ static init(app: IApplication): SessionManager;
589
+ /**
590
+ * Generate a secure session ID unique to the user device.
591
+ */
592
+ private generateSessionId;
593
+ /**
594
+ * Resolve the session ID from cookie, header, or create a new one.
595
+ */
596
+ private resolveSessionId;
597
+ /**
598
+ * Access the current session ID.
599
+ */
600
+ id(): string;
601
+ /**
602
+ * Get the current session driver
603
+ */
604
+ getDriver(): ISessionDriver;
605
+ /**
606
+ * Retrieve a value from the session
607
+ *
608
+ * @param key
609
+ * @param defaultValue
610
+ * @returns
611
+ */
612
+ get(key: string, defaultValue?: any): Promise<any> | any;
613
+ /**
614
+ * Store a value in the session
615
+ *
616
+ * @param key
617
+ * @param value
618
+ */
619
+ set(value: Record<string, any>): Promise<void> | void;
620
+ /**
621
+ * Store multiple key/value pairs
622
+ *
623
+ * @param values
624
+ */
625
+ put(key: string, value: any): void | Promise<void>;
626
+ /**
627
+ * Append a value to an array key
628
+ *
629
+ * @param key
630
+ * @param value
631
+ */
632
+ push(key: string, value: any): void | Promise<void>;
633
+ /**
634
+ * Remove a key from the session
635
+ *
636
+ * @param key
637
+ */
638
+ forget(key: string): void | Promise<void>;
639
+ /**
640
+ * Retrieve all session data
641
+ *
642
+ * @returns
643
+ */
644
+ all(): Record<string, any> | Promise<Record<string, any>>;
645
+ /**
646
+ * Determine if a key exists (even if null).
647
+ *
648
+ * @param key
649
+ * @returns
650
+ */
651
+ exists(key: string): Promise<boolean> | boolean;
652
+ /**
653
+ * Determine if a key has a non-null value.
654
+ *
655
+ * @param key
656
+ * @returns
657
+ */
658
+ has(key: string): Promise<boolean> | boolean;
659
+ /**
660
+ * Get only specific keys.
661
+ *
662
+ * @param keys
663
+ * @returns
664
+ */
665
+ only(keys: string[]): Record<string, any> | Promise<Record<string, any>>;
666
+ /**
667
+ * Return all keys except the specified ones.
668
+ *
669
+ * @param keys
670
+ * @returns
671
+ */
672
+ except(keys: string[]): Record<string, any> | Promise<Record<string, any>>;
673
+ /**
674
+ * Return and delete a key from the session.
675
+ *
676
+ * @param key
677
+ * @param defaultValue
678
+ * @returns
679
+ */
680
+ pull(key: string, defaultValue?: any): any;
681
+ /**
682
+ * Increment a numeric value by amount (default 1).
683
+ *
684
+ * @param key
685
+ * @param amount
686
+ * @returns
687
+ */
688
+ increment(key: string, amount?: number): Promise<number> | number;
689
+ /**
690
+ * Decrement a numeric value by amount (default 1).
691
+ *
692
+ * @param key
693
+ * @param amount
694
+ * @returns
695
+ */
696
+ decrement(key: string, amount?: number): number | Promise<number>;
697
+ /**
698
+ * Flash a value for next request only.
699
+ *
700
+ * @param key
701
+ * @param value
702
+ */
703
+ flash(key: string, value: any): void | Promise<void>;
704
+ /**
705
+ * Reflash all flash data for one more cycle.
706
+ *
707
+ * @returns
708
+ */
709
+ reflash(): void | Promise<void>;
710
+ /**
711
+ * Keep only selected flash data.
712
+ *
713
+ * @param keys
714
+ * @returns
715
+ */
716
+ keep(keys: string[]): void | Promise<void>;
717
+ /**
718
+ * Store data only for current request cycle (not persisted).
719
+ *
720
+ * @param key
721
+ * @param value
722
+ */
723
+ now(key: string, value: any): void | Promise<void>;
724
+ /**
725
+ * Regenerate session ID and persist data under new ID.
726
+ */
727
+ regenerate(): void | Promise<void>;
728
+ /**
729
+ * Determine if an item is not present in the session.
730
+ *
731
+ * @param key
732
+ * @returns
733
+ */
734
+ missing(key: string): Promise<boolean> | boolean;
735
+ /**
736
+ * Flush all session data
737
+ */
738
+ flush(): void | Promise<void>;
739
+ /**
740
+ * Invalidate the session completely and regenerate ID.
741
+ *
742
+ * @returns
743
+ */
744
+ invalidate(): void | Promise<void>;
745
+ /**
746
+ * Age flash data at the end of the request lifecycle.
747
+ *
748
+ * @returns
749
+ */
750
+ ageFlashData(): void | Promise<void>;
751
+ }
752
+ //#endregion
753
+ //#region src/SessionStore.d.ts
754
+ /**
755
+ * SessionStore (Driver registry)
756
+ *
757
+ * Register driver builders under a name and then create instances using:
758
+ * SessionStore.make('file', sessionId, options)
759
+ */
760
+ declare class SessionStore {
761
+ private static registry;
762
+ /**
763
+ * Register a driver builder under a key (e.g. 'file', 'database', 'memory').
764
+ */
765
+ static register(name: 'file' | 'memory' | 'database' | 'redis', builder: SessionDriverBuilder): void;
766
+ /**
767
+ * Create a driver instance for the given sessionId using the named builder.
768
+ *
769
+ * If driver not found, throws. Options is a simple key/value bag passed to the builder.
770
+ */
771
+ static make(name: 'file' | 'memory' | 'database' | 'redis', sessionId: string, options?: SessionDriverOption): _h3ravel_contracts0.ISessionDriver;
772
+ }
773
+ //#endregion
774
+ export { DatabaseDriver, Driver, Encryption, FileDriver, FlashBag, MakeSessionTableCommand, MemoryDriver, RedisDriver, SessionManager, SessionServiceProvider, SessionStore, dbBuilder, fileBuilder, memoryBuilder, redisBuilder };