@mantiq/core 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +19 -0
  2. package/package.json +65 -0
  3. package/src/application/Application.ts +241 -0
  4. package/src/cache/CacheManager.ts +180 -0
  5. package/src/cache/FileCacheStore.ts +113 -0
  6. package/src/cache/MemcachedCacheStore.ts +115 -0
  7. package/src/cache/MemoryCacheStore.ts +62 -0
  8. package/src/cache/NullCacheStore.ts +39 -0
  9. package/src/cache/RedisCacheStore.ts +125 -0
  10. package/src/cache/events.ts +52 -0
  11. package/src/config/ConfigRepository.ts +115 -0
  12. package/src/config/env.ts +26 -0
  13. package/src/container/Container.ts +198 -0
  14. package/src/container/ContextualBindingBuilder.ts +21 -0
  15. package/src/contracts/Cache.ts +49 -0
  16. package/src/contracts/Config.ts +24 -0
  17. package/src/contracts/Container.ts +68 -0
  18. package/src/contracts/DriverManager.ts +16 -0
  19. package/src/contracts/Encrypter.ts +32 -0
  20. package/src/contracts/EventDispatcher.ts +32 -0
  21. package/src/contracts/ExceptionHandler.ts +20 -0
  22. package/src/contracts/Hasher.ts +19 -0
  23. package/src/contracts/Middleware.ts +23 -0
  24. package/src/contracts/Request.ts +54 -0
  25. package/src/contracts/Response.ts +19 -0
  26. package/src/contracts/Router.ts +62 -0
  27. package/src/contracts/ServiceProvider.ts +31 -0
  28. package/src/contracts/Session.ts +47 -0
  29. package/src/encryption/Encrypter.ts +197 -0
  30. package/src/encryption/errors.ts +30 -0
  31. package/src/errors/ConfigKeyNotFoundError.ts +7 -0
  32. package/src/errors/ContainerResolutionError.ts +13 -0
  33. package/src/errors/ForbiddenError.ts +7 -0
  34. package/src/errors/HttpError.ts +16 -0
  35. package/src/errors/MantiqError.ts +16 -0
  36. package/src/errors/NotFoundError.ts +7 -0
  37. package/src/errors/TokenMismatchError.ts +10 -0
  38. package/src/errors/TooManyRequestsError.ts +10 -0
  39. package/src/errors/UnauthorizedError.ts +7 -0
  40. package/src/errors/ValidationError.ts +10 -0
  41. package/src/exceptions/DevErrorPage.ts +564 -0
  42. package/src/exceptions/Handler.ts +118 -0
  43. package/src/hashing/Argon2Hasher.ts +46 -0
  44. package/src/hashing/BcryptHasher.ts +36 -0
  45. package/src/hashing/HashManager.ts +80 -0
  46. package/src/helpers/abort.ts +46 -0
  47. package/src/helpers/app.ts +17 -0
  48. package/src/helpers/cache.ts +12 -0
  49. package/src/helpers/config.ts +15 -0
  50. package/src/helpers/encrypt.ts +22 -0
  51. package/src/helpers/env.ts +1 -0
  52. package/src/helpers/hash.ts +20 -0
  53. package/src/helpers/response.ts +69 -0
  54. package/src/helpers/route.ts +24 -0
  55. package/src/helpers/session.ts +11 -0
  56. package/src/http/Cookie.ts +26 -0
  57. package/src/http/Kernel.ts +252 -0
  58. package/src/http/Request.ts +249 -0
  59. package/src/http/Response.ts +112 -0
  60. package/src/http/UploadedFile.ts +56 -0
  61. package/src/index.ts +97 -0
  62. package/src/macroable/Macroable.ts +174 -0
  63. package/src/middleware/Cors.ts +91 -0
  64. package/src/middleware/EncryptCookies.ts +101 -0
  65. package/src/middleware/Pipeline.ts +66 -0
  66. package/src/middleware/StartSession.ts +90 -0
  67. package/src/middleware/TrimStrings.ts +32 -0
  68. package/src/middleware/VerifyCsrfToken.ts +130 -0
  69. package/src/providers/CoreServiceProvider.ts +97 -0
  70. package/src/routing/ResourceRegistrar.ts +64 -0
  71. package/src/routing/Route.ts +40 -0
  72. package/src/routing/RouteCollection.ts +50 -0
  73. package/src/routing/RouteMatcher.ts +92 -0
  74. package/src/routing/Router.ts +280 -0
  75. package/src/routing/events.ts +19 -0
  76. package/src/session/SessionManager.ts +75 -0
  77. package/src/session/Store.ts +192 -0
  78. package/src/session/handlers/CookieSessionHandler.ts +42 -0
  79. package/src/session/handlers/FileSessionHandler.ts +79 -0
  80. package/src/session/handlers/MemorySessionHandler.ts +35 -0
  81. package/src/websocket/WebSocketContext.ts +20 -0
  82. package/src/websocket/WebSocketKernel.ts +60 -0
@@ -0,0 +1,35 @@
1
+ import type { SessionHandler } from '../../contracts/Session.ts'
2
+
3
+ interface SessionEntry {
4
+ data: string
5
+ lastActivity: number
6
+ }
7
+
8
+ /**
9
+ * In-memory session handler. Fast, but sessions are lost on restart.
10
+ * Good for development and testing.
11
+ */
12
+ export class MemorySessionHandler implements SessionHandler {
13
+ private sessions = new Map<string, SessionEntry>()
14
+
15
+ async read(sessionId: string): Promise<string> {
16
+ return this.sessions.get(sessionId)?.data ?? ''
17
+ }
18
+
19
+ async write(sessionId: string, data: string): Promise<void> {
20
+ this.sessions.set(sessionId, { data, lastActivity: Date.now() })
21
+ }
22
+
23
+ async destroy(sessionId: string): Promise<void> {
24
+ this.sessions.delete(sessionId)
25
+ }
26
+
27
+ async gc(maxLifetimeSeconds: number): Promise<void> {
28
+ const cutoff = Date.now() - maxLifetimeSeconds * 1000
29
+ for (const [id, entry] of this.sessions) {
30
+ if (entry.lastActivity < cutoff) {
31
+ this.sessions.delete(id)
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,20 @@
1
+ import type { MantiqRequest } from '../contracts/Request.ts'
2
+
3
+ export interface WebSocketContext {
4
+ userId?: string | number
5
+ channels: Set<string>
6
+ metadata: Record<string, any>
7
+ }
8
+
9
+ export interface WebSocketHandler {
10
+ /**
11
+ * Called before the WebSocket upgrade.
12
+ * Return a context object to allow the connection, or null to reject it.
13
+ */
14
+ onUpgrade(request: MantiqRequest): Promise<WebSocketContext | null>
15
+
16
+ open(ws: any): void | Promise<void>
17
+ message(ws: any, message: string | Buffer): void | Promise<void>
18
+ close(ws: any, code: number, reason: string): void | Promise<void>
19
+ drain(ws: any): void | Promise<void>
20
+ }
@@ -0,0 +1,60 @@
1
+ import type { WebSocketHandler } from './WebSocketContext.ts'
2
+ import { MantiqRequest } from '../http/Request.ts'
3
+
4
+ /**
5
+ * Handles WebSocket upgrade detection and lifecycle delegation.
6
+ *
7
+ * Core only provides the infrastructure — @mantiq/realtime registers
8
+ * its handler via registerHandler(). Without it, all upgrades return 426.
9
+ */
10
+ export class WebSocketKernel {
11
+ private handler: WebSocketHandler | null = null
12
+
13
+ /**
14
+ * Called by @mantiq/realtime to register its WebSocket handler.
15
+ */
16
+ registerHandler(handler: WebSocketHandler): void {
17
+ this.handler = handler
18
+ }
19
+
20
+ /**
21
+ * Called by HttpKernel when an upgrade request is detected.
22
+ */
23
+ async handleUpgrade(request: Request, server: any): Promise<Response> {
24
+ if (!this.handler) {
25
+ return new Response('WebSocket not available. Install @mantiq/realtime.', {
26
+ status: 426,
27
+ headers: { Upgrade: 'websocket' },
28
+ })
29
+ }
30
+
31
+ const mantiqRequest = MantiqRequest.fromBun(request)
32
+ const context = await this.handler.onUpgrade(mantiqRequest)
33
+
34
+ if (!context) {
35
+ return new Response('Unauthorized', { status: 401 })
36
+ }
37
+
38
+ const upgraded = server.upgrade(request, { data: context })
39
+ if (!upgraded) {
40
+ return new Response('WebSocket upgrade failed', { status: 500 })
41
+ }
42
+
43
+ // Bun handles the response after a successful upgrade
44
+ return undefined as unknown as Response
45
+ }
46
+
47
+ /**
48
+ * Returns the Bun WebSocket handlers object for Bun.serve().
49
+ * If no handler is registered, provides no-op stubs.
50
+ */
51
+ getBunHandlers(): object {
52
+ const h = this.handler
53
+ return {
54
+ open: (ws: any) => h?.open(ws),
55
+ message: (ws: any, msg: any) => h?.message(ws, msg),
56
+ close: (ws: any, code: number, reason: string) => h?.close(ws, code, reason),
57
+ drain: (ws: any) => h?.drain(ws),
58
+ }
59
+ }
60
+ }