@lithia-js/core 1.0.0-canary.0

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 (91) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +13 -0
  3. package/LICENSE +21 -0
  4. package/README.md +60 -0
  5. package/dist/config.d.ts +101 -0
  6. package/dist/config.js +113 -0
  7. package/dist/config.js.map +1 -0
  8. package/dist/context/event-context.d.ts +53 -0
  9. package/dist/context/event-context.js +42 -0
  10. package/dist/context/event-context.js.map +1 -0
  11. package/dist/context/index.d.ts +16 -0
  12. package/dist/context/index.js +29 -0
  13. package/dist/context/index.js.map +1 -0
  14. package/dist/context/lithia-context.d.ts +47 -0
  15. package/dist/context/lithia-context.js +43 -0
  16. package/dist/context/lithia-context.js.map +1 -0
  17. package/dist/context/route-context.d.ts +74 -0
  18. package/dist/context/route-context.js +42 -0
  19. package/dist/context/route-context.js.map +1 -0
  20. package/dist/env.d.ts +1 -0
  21. package/dist/env.js +32 -0
  22. package/dist/env.js.map +1 -0
  23. package/dist/errors.d.ts +51 -0
  24. package/dist/errors.js +80 -0
  25. package/dist/errors.js.map +1 -0
  26. package/dist/hooks/dependency-hooks.d.ts +105 -0
  27. package/dist/hooks/dependency-hooks.js +96 -0
  28. package/dist/hooks/dependency-hooks.js.map +1 -0
  29. package/dist/hooks/event-hooks.d.ts +61 -0
  30. package/dist/hooks/event-hooks.js +70 -0
  31. package/dist/hooks/event-hooks.js.map +1 -0
  32. package/dist/hooks/index.d.ts +41 -0
  33. package/dist/hooks/index.js +59 -0
  34. package/dist/hooks/index.js.map +1 -0
  35. package/dist/hooks/route-hooks.d.ts +154 -0
  36. package/dist/hooks/route-hooks.js +174 -0
  37. package/dist/hooks/route-hooks.js.map +1 -0
  38. package/dist/lib.d.ts +10 -0
  39. package/dist/lib.js +30 -0
  40. package/dist/lib.js.map +1 -0
  41. package/dist/lithia.d.ts +447 -0
  42. package/dist/lithia.js +649 -0
  43. package/dist/lithia.js.map +1 -0
  44. package/dist/logger.d.ts +11 -0
  45. package/dist/logger.js +55 -0
  46. package/dist/logger.js.map +1 -0
  47. package/dist/module-loader.d.ts +12 -0
  48. package/dist/module-loader.js +78 -0
  49. package/dist/module-loader.js.map +1 -0
  50. package/dist/server/event-processor.d.ts +195 -0
  51. package/dist/server/event-processor.js +253 -0
  52. package/dist/server/event-processor.js.map +1 -0
  53. package/dist/server/http-server.d.ts +196 -0
  54. package/dist/server/http-server.js +295 -0
  55. package/dist/server/http-server.js.map +1 -0
  56. package/dist/server/middlewares/validation.d.ts +12 -0
  57. package/dist/server/middlewares/validation.js +34 -0
  58. package/dist/server/middlewares/validation.js.map +1 -0
  59. package/dist/server/request-processor.d.ts +400 -0
  60. package/dist/server/request-processor.js +652 -0
  61. package/dist/server/request-processor.js.map +1 -0
  62. package/dist/server/request.d.ts +73 -0
  63. package/dist/server/request.js +207 -0
  64. package/dist/server/request.js.map +1 -0
  65. package/dist/server/response.d.ts +69 -0
  66. package/dist/server/response.js +173 -0
  67. package/dist/server/response.js.map +1 -0
  68. package/package.json +46 -0
  69. package/src/config.ts +212 -0
  70. package/src/context/event-context.ts +66 -0
  71. package/src/context/index.ts +32 -0
  72. package/src/context/lithia-context.ts +59 -0
  73. package/src/context/route-context.ts +89 -0
  74. package/src/env.ts +31 -0
  75. package/src/errors.ts +96 -0
  76. package/src/hooks/dependency-hooks.ts +122 -0
  77. package/src/hooks/event-hooks.ts +69 -0
  78. package/src/hooks/index.ts +58 -0
  79. package/src/hooks/route-hooks.ts +177 -0
  80. package/src/lib.ts +27 -0
  81. package/src/lithia.ts +777 -0
  82. package/src/logger.ts +66 -0
  83. package/src/module-loader.ts +45 -0
  84. package/src/server/event-processor.ts +344 -0
  85. package/src/server/http-server.ts +371 -0
  86. package/src/server/middlewares/validation.ts +46 -0
  87. package/src/server/request-processor.ts +860 -0
  88. package/src/server/request.ts +247 -0
  89. package/src/server/response.ts +204 -0
  90. package/tsconfig.build.tsbuildinfo +1 -0
  91. package/tsconfig.json +8 -0
@@ -0,0 +1,4 @@
1
+
2
+ > @lithia-js/core@1.0.0-canary.0 build /home/runner/work/lithia/lithia/packages/core
3
+ > tsc -p tsconfig.build.json
4
+
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @lithia-js/core
2
+
3
+ ## 1.0.0-canary.0
4
+
5
+ ### Major Changes
6
+
7
+ - e9a0904: Lithia v1 canary release. Now with separated modules and napi bindings for build performance
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [e9a0904]
12
+ - @lithia-js/native@1.0.0-canary.0
13
+ - @lithia-js/utils@1.0.0-canary.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 lithiajs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ <div align="center">
2
+ <a href="https://github.com/lithia-framework/lithia">
3
+ <img alt="Lithia logo" src="https://raw.githubusercontent.com/lithia-framework/lithia/canary/.github/assets/logo.svg" height="128">
4
+ </a>
5
+ <h1>Lithia</h1>
6
+ <p><strong>The Node.js framework that makes API development feel like magic</strong></p>
7
+
8
+ <a href="https://www.npmjs.com/package/lithia"><img alt="NPM version" src="https://img.shields.io/npm/v/lithia.svg?style=for-the-badge&labelColor=000000"></a>
9
+ <a href="https://github.com/lithia-framework/lithia/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/npm/l/lithia.svg?style=for-the-badge&labelColor=000000"></a>
10
+ <a href="https://opencollective.com/lithiajs"><img alt="Support Lithia" src="https://img.shields.io/badge/Support%20Lithia-blueviolet.svg?style=for-the-badge&logo=OpenCollective&labelColor=000000&logoWidth=20"></a>
11
+
12
+ </div>
13
+
14
+ ## Getting Started
15
+
16
+ Lithia is a next-generation Node.js framework that enables you to build powerful APIs with file-based routing, built-in WebSocket support, and a beautiful development interface.
17
+
18
+ - Visit our [Learn Lithia](https://lithiajs.com/docs) guide to get started.
19
+ - Check out the [Examples](https://github.com/lithia-framework/lithia/tree/main/examples) to see Lithia in action.
20
+
21
+ ## Documentation
22
+
23
+ Visit [https://lithiajs.com/docs](https://lithiajs.com/docs) to view the full documentation.
24
+
25
+ ## Community
26
+
27
+ The Lithia community can be found on [GitHub Discussions](https://github.com/lithia-framework/lithia/discussions) where you can ask questions, voice ideas, and share your projects with other people.
28
+
29
+ ## Contributing
30
+
31
+ Contributions to Lithia are welcome and highly appreciated. However, before you jump right into it, we would like you to review our [Contribution Guidelines](CONTRIBUTING.md) to make sure you have a smooth experience contributing to Lithia.
32
+
33
+ ---
34
+
35
+ ## Support the Project
36
+
37
+ If Lithia makes your life easier, consider supporting it:
38
+
39
+ - **Star** this repository
40
+ - **Share** on social media
41
+ - **Sponsor** via [OpenCollective](https://opencollective.com/lithiajs)
42
+ - **Report bugs** and suggest improvements
43
+
44
+ ---
45
+
46
+ ## License
47
+
48
+ Lithia is [MIT licensed](LICENSE). Free for personal and commercial use.
49
+
50
+ ---
51
+
52
+ <div align="center">
53
+ <p><strong>Built with ❤️ by the Lithia community</strong></p>
54
+ <p>
55
+ <a href="https://github.com/lithia-framework/lithia">GitHub</a> •
56
+ <a href="https://lithiajs.com">Documentation</a> •
57
+ <a href="https://opencollective.com/lithiajs">OpenCollective</a> •
58
+ <a href="https://github.com/lithia-framework/lithia/discussions">Discussions</a>
59
+ </p>
60
+ </div>
@@ -0,0 +1,101 @@
1
+ import type { DeepPartial } from "@lithia-js/utils";
2
+ import { type C12InputConfig, type WatchConfigOptions } from "c12";
3
+ /** Result type for lifecycle hooks — may be synchronous or async. */
4
+ export type HookResult = void | Promise<void>;
5
+ /** Available lifecycle hooks supported by the runtime. */
6
+ export interface LithiaHooks {
7
+ /** Called before the HTTP server starts. */
8
+ "before:start": () => HookResult;
9
+ /** Called after the HTTP server has started. */
10
+ "after:start": () => HookResult;
11
+ }
12
+ /** Shape of the runtime configuration used by Lithia. */
13
+ export interface LithiaOptions {
14
+ debug: boolean;
15
+ http: {
16
+ port: number;
17
+ host: string;
18
+ maxBodySize?: number;
19
+ ssl?: {
20
+ key: string;
21
+ cert: string;
22
+ passphrase?: string;
23
+ };
24
+ cors: {
25
+ origin?: string[];
26
+ methods?: string[];
27
+ allowedHeaders?: string[];
28
+ exposedHeaders?: string[];
29
+ credentials?: boolean;
30
+ maxAge?: number;
31
+ };
32
+ mimeTypes?: Record<string, string>;
33
+ };
34
+ static?: {
35
+ root: string;
36
+ prefix?: string;
37
+ };
38
+ studio: {
39
+ enabled: boolean;
40
+ };
41
+ logging: {
42
+ /** Enable request logging. Critical errors (5xx) are always logged. */
43
+ requests: boolean;
44
+ /** Enable event logging. Critical errors are always logged. */
45
+ events: boolean;
46
+ };
47
+ hooks: {
48
+ [K in keyof LithiaHooks]: LithiaHooks[K];
49
+ };
50
+ }
51
+ /** Partial configuration accepted by `defineConfig` and the config loader. */
52
+ export interface LithiaConfig extends DeepPartial<LithiaOptions>, C12InputConfig<LithiaConfig> {
53
+ }
54
+ /** Default runtime configuration used when no overrides are provided. */
55
+ export declare const DEFAULT_CONFIG: LithiaConfig;
56
+ type LoadConfigOptions = {
57
+ watch?: boolean;
58
+ c12?: WatchConfigOptions;
59
+ overrides?: LithiaConfig;
60
+ };
61
+ /** Error thrown when configuration validation fails. */
62
+ export declare class ConfigValidationError extends Error {
63
+ readonly field?: string | undefined;
64
+ constructor(message: string, field?: string | undefined);
65
+ }
66
+ /** Context passed to `watchConfig` callbacks describing the updated config. */
67
+ export interface ConfigUpdateContext {
68
+ /** Returns a list of diffs between old and new config. */
69
+ getDiff: () => Array<{
70
+ key: string;
71
+ type: string;
72
+ newValue: unknown;
73
+ oldValue: unknown;
74
+ }>;
75
+ /** The new fully materialized config. */
76
+ newConfig: LithiaOptions;
77
+ /** The previous config prior to the update. */
78
+ oldConfig: LithiaOptions;
79
+ }
80
+ /** Provider responsible for loading and optionally watching the runtime config. */
81
+ export declare class ConfigProvider {
82
+ /**
83
+ * Load the configuration, applying optional overrides.
84
+ *
85
+ * This delegates to `c12` for reading configuration files and defaults
86
+ * and validates the resulting `LithiaOptions` before returning them.
87
+ */
88
+ loadConfig(overrides?: LithiaConfig, opts?: LoadConfigOptions): Promise<LithiaOptions>;
89
+ /**
90
+ * Watch the configuration for changes and invoke `onChange` when updates occur.
91
+ *
92
+ * Returns a handle with a `close()` method to stop watching.
93
+ */
94
+ watchConfig(onChange: (ctx: ConfigUpdateContext) => void | Promise<void>, overrides?: LithiaConfig, opts?: LoadConfigOptions): Promise<{
95
+ close: () => void;
96
+ }>;
97
+ private validateConfig;
98
+ }
99
+ /** Utility helper used by users to define their config with IDE type hints. */
100
+ export declare function defineConfig(config: LithiaConfig): LithiaConfig;
101
+ export {};
package/dist/config.js ADDED
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigProvider = exports.ConfigValidationError = exports.DEFAULT_CONFIG = void 0;
4
+ exports.defineConfig = defineConfig;
5
+ const c12_1 = require("c12");
6
+ const klona_1 = require("klona");
7
+ /** Default runtime configuration used when no overrides are provided. */
8
+ exports.DEFAULT_CONFIG = {
9
+ debug: false,
10
+ http: {
11
+ port: 3000,
12
+ host: "localhost",
13
+ maxBodySize: 1024 * 1024,
14
+ cors: {
15
+ origin: ["*"],
16
+ methods: ["*"],
17
+ allowedHeaders: ["*"],
18
+ exposedHeaders: ["X-Powered-By"],
19
+ credentials: false,
20
+ maxAge: 86400,
21
+ },
22
+ },
23
+ logging: {
24
+ requests: true,
25
+ events: true,
26
+ },
27
+ hooks: {},
28
+ studio: {
29
+ enabled: false,
30
+ },
31
+ };
32
+ /** Error thrown when configuration validation fails. */
33
+ class ConfigValidationError extends Error {
34
+ field;
35
+ constructor(message, field) {
36
+ super(message);
37
+ this.field = field;
38
+ this.name = "ConfigValidationError";
39
+ }
40
+ }
41
+ exports.ConfigValidationError = ConfigValidationError;
42
+ /** Provider responsible for loading and optionally watching the runtime config. */
43
+ class ConfigProvider {
44
+ /**
45
+ * Load the configuration, applying optional overrides.
46
+ *
47
+ * This delegates to `c12` for reading configuration files and defaults
48
+ * and validates the resulting `LithiaOptions` before returning them.
49
+ */
50
+ async loadConfig(overrides = {}, opts = {}) {
51
+ overrides = (0, klona_1.klona)(overrides);
52
+ const configOptions = {
53
+ name: "lithia",
54
+ configFile: "lithia.config",
55
+ cwd: process.cwd(),
56
+ dotenv: true,
57
+ overrides,
58
+ defaults: exports.DEFAULT_CONFIG,
59
+ ...opts.c12,
60
+ };
61
+ const loadedConfig = await (0, c12_1.loadConfig)(configOptions);
62
+ const options = (0, klona_1.klona)(loadedConfig.config);
63
+ // overrides are already applied by c12; no need to re-assign here
64
+ this.validateConfig(options);
65
+ return options;
66
+ }
67
+ /**
68
+ * Watch the configuration for changes and invoke `onChange` when updates occur.
69
+ *
70
+ * Returns a handle with a `close()` method to stop watching.
71
+ */
72
+ async watchConfig(onChange, overrides = {}, opts = {}) {
73
+ overrides = (0, klona_1.klona)(overrides);
74
+ const configOptions = {
75
+ name: "lithia",
76
+ configFile: "lithia.config",
77
+ cwd: process.cwd(),
78
+ dotenv: true,
79
+ overrides,
80
+ defaults: exports.DEFAULT_CONFIG,
81
+ ...opts.c12,
82
+ watch: true,
83
+ onUpdate: async (context) => {
84
+ const newOptions = (0, klona_1.klona)(context.newConfig.config);
85
+ this.validateConfig(newOptions);
86
+ await onChange({
87
+ getDiff: context.getDiff,
88
+ newConfig: newOptions,
89
+ oldConfig: context.oldConfig.config,
90
+ });
91
+ },
92
+ };
93
+ const handle = await (0, c12_1.watchConfig)(configOptions);
94
+ return {
95
+ close: () => {
96
+ if (typeof handle?.close === "function") {
97
+ handle.close();
98
+ }
99
+ },
100
+ };
101
+ }
102
+ validateConfig(config) {
103
+ if (config.http.port < 1 || config.http.port > 65535) {
104
+ throw new ConfigValidationError(`HTTP port must be between 1 and 65535, got ${config.http.port}`, "http.port");
105
+ }
106
+ }
107
+ }
108
+ exports.ConfigProvider = ConfigProvider;
109
+ /** Utility helper used by users to define their config with IDE type hints. */
110
+ function defineConfig(config) {
111
+ return config;
112
+ }
113
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Event context module for Socket.IO events.
3
+ *
4
+ * Provides context specific to Socket.IO event handling, including access
5
+ * to event data passed from the client.
6
+ *
7
+ * @module context/event-context
8
+ */
9
+ import { AsyncLocalStorage } from "node:async_hooks";
10
+ import type { Socket } from "socket.io";
11
+ /**
12
+ * Socket.IO event handler context.
13
+ *
14
+ * Contains event-specific information available to event handlers
15
+ * during Socket.IO event processing.
16
+ */
17
+ export type EventContext = {
18
+ /**
19
+ * Data payload sent with the event.
20
+ *
21
+ * Contains the data passed from the client when emitting the event.
22
+ * The structure depends on what the client sends.
23
+ */
24
+ data: any;
25
+ /**
26
+ * The Socket.IO socket instance.
27
+ *
28
+ * Represents the client connection that triggered the event.
29
+ */
30
+ socket: Socket;
31
+ };
32
+ /**
33
+ * AsyncLocalStorage instance for event context.
34
+ *
35
+ * Uses Node.js AsyncLocalStorage to provide implicit context propagation
36
+ * for Socket.IO event handling across async boundaries.
37
+ *
38
+ * @see https://nodejs.org/api/async_hooks.html#class-asynclocalstorage
39
+ */
40
+ export declare const eventContext: AsyncLocalStorage<EventContext>;
41
+ /**
42
+ * Gets the current event context.
43
+ *
44
+ * @returns The current EventContext
45
+ * @throws {Error} If called outside of a Socket.IO event handler
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const ctx = getEventContext();
50
+ * console.log('Event data:', ctx.data);
51
+ * ```
52
+ */
53
+ export declare function getEventContext(): EventContext;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ /**
3
+ * Event context module for Socket.IO events.
4
+ *
5
+ * Provides context specific to Socket.IO event handling, including access
6
+ * to event data passed from the client.
7
+ *
8
+ * @module context/event-context
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.eventContext = void 0;
12
+ exports.getEventContext = getEventContext;
13
+ const node_async_hooks_1 = require("node:async_hooks");
14
+ /**
15
+ * AsyncLocalStorage instance for event context.
16
+ *
17
+ * Uses Node.js AsyncLocalStorage to provide implicit context propagation
18
+ * for Socket.IO event handling across async boundaries.
19
+ *
20
+ * @see https://nodejs.org/api/async_hooks.html#class-asynclocalstorage
21
+ */
22
+ exports.eventContext = new node_async_hooks_1.AsyncLocalStorage();
23
+ /**
24
+ * Gets the current event context.
25
+ *
26
+ * @returns The current EventContext
27
+ * @throws {Error} If called outside of a Socket.IO event handler
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const ctx = getEventContext();
32
+ * console.log('Event data:', ctx.data);
33
+ * ```
34
+ */
35
+ function getEventContext() {
36
+ const ctx = exports.eventContext.getStore();
37
+ if (!ctx) {
38
+ throw new Error("Lithia event context not found. Are you calling a hook outside of an event handler?");
39
+ }
40
+ return ctx;
41
+ }
42
+ //# sourceMappingURL=event-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-context.js","sourceRoot":"","sources":["../../src/context/event-context.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAkDH,0CAQC;AAxDD,uDAAqD;AA0BrD;;;;;;;GAOG;AACU,QAAA,YAAY,GAAG,IAAI,oCAAiB,EAAgB,CAAC;AAElE;;;;;;;;;;;GAWG;AACH,SAAgB,eAAe;IAC9B,MAAM,GAAG,GAAG,oBAAY,CAAC,QAAQ,EAAE,CAAC;IACpC,IAAI,CAAC,GAAG,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CACd,qFAAqF,CACrF,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Context management for Lithia.
3
+ *
4
+ * This module provides three separate context types using AsyncLocalStorage:
5
+ * - **LithiaContext**: Global application context with DI container
6
+ * - **RouteContext**: HTTP request-specific context
7
+ * - **EventContext**: Socket.IO event-specific context
8
+ *
9
+ * Each context is isolated and provides different information based on
10
+ * the execution environment (HTTP request vs Socket.IO event).
11
+ *
12
+ * @module context
13
+ */
14
+ export { type EventContext, eventContext, getEventContext, } from "./event-context";
15
+ export { getLithiaContext, type LithiaContext, lithiaContext, } from "./lithia-context";
16
+ export { getRouteContext, type RouteContext, routeContext, } from "./route-context";
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ /**
3
+ * Context management for Lithia.
4
+ *
5
+ * This module provides three separate context types using AsyncLocalStorage:
6
+ * - **LithiaContext**: Global application context with DI container
7
+ * - **RouteContext**: HTTP request-specific context
8
+ * - **EventContext**: Socket.IO event-specific context
9
+ *
10
+ * Each context is isolated and provides different information based on
11
+ * the execution environment (HTTP request vs Socket.IO event).
12
+ *
13
+ * @module context
14
+ */
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.routeContext = exports.getRouteContext = exports.lithiaContext = exports.getLithiaContext = exports.getEventContext = exports.eventContext = void 0;
17
+ // Socket.IO event context
18
+ var event_context_1 = require("./event-context");
19
+ Object.defineProperty(exports, "eventContext", { enumerable: true, get: function () { return event_context_1.eventContext; } });
20
+ Object.defineProperty(exports, "getEventContext", { enumerable: true, get: function () { return event_context_1.getEventContext; } });
21
+ // Lithia global context
22
+ var lithia_context_1 = require("./lithia-context");
23
+ Object.defineProperty(exports, "getLithiaContext", { enumerable: true, get: function () { return lithia_context_1.getLithiaContext; } });
24
+ Object.defineProperty(exports, "lithiaContext", { enumerable: true, get: function () { return lithia_context_1.lithiaContext; } });
25
+ // HTTP route context
26
+ var route_context_1 = require("./route-context");
27
+ Object.defineProperty(exports, "getRouteContext", { enumerable: true, get: function () { return route_context_1.getRouteContext; } });
28
+ Object.defineProperty(exports, "routeContext", { enumerable: true, get: function () { return route_context_1.routeContext; } });
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AAEH,0BAA0B;AAC1B,iDAIyB;AAFxB,6GAAA,YAAY,OAAA;AACZ,gHAAA,eAAe,OAAA;AAEhB,wBAAwB;AACxB,mDAI0B;AAHzB,kHAAA,gBAAgB,OAAA;AAEhB,+GAAA,aAAa,OAAA;AAEd,qBAAqB;AACrB,iDAIyB;AAHxB,gHAAA,eAAe,OAAA;AAEf,6GAAA,YAAY,OAAA"}
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Lithia global context module.
3
+ *
4
+ * Provides the main application-level context that holds the global
5
+ * dependency injection container. This context is available throughout
6
+ * the entire request/event lifecycle.
7
+ *
8
+ * @module context/lithia-context
9
+ */
10
+ import { AsyncLocalStorage } from "node:async_hooks";
11
+ /**
12
+ * Global Lithia application context.
13
+ *
14
+ * Holds the dependency injection container that is shared across
15
+ * the entire application and accessible in all request/event handlers.
16
+ */
17
+ export interface LithiaContext {
18
+ /**
19
+ * Global dependency injection container.
20
+ *
21
+ * Stores dependencies registered via `provide()` that can be injected
22
+ * into handlers using `inject()` or `injectOptional()`.
23
+ */
24
+ dependencies: Map<any, any>;
25
+ }
26
+ /**
27
+ * AsyncLocalStorage instance for Lithia application context.
28
+ *
29
+ * Uses Node.js AsyncLocalStorage to provide implicit context propagation
30
+ * across async boundaries without explicit parameter passing.
31
+ *
32
+ * @see https://nodejs.org/api/async_hooks.html#class-asynclocalstorage
33
+ */
34
+ export declare const lithiaContext: AsyncLocalStorage<LithiaContext>;
35
+ /**
36
+ * Gets the current Lithia context.
37
+ *
38
+ * @returns The current LithiaContext
39
+ * @throws {Error} If called outside of a request or event handler context
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const ctx = getLithiaContext();
44
+ * const db = ctx.dependencies.get(dbKey);
45
+ * ```
46
+ */
47
+ export declare function getLithiaContext(): LithiaContext;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ /**
3
+ * Lithia global context module.
4
+ *
5
+ * Provides the main application-level context that holds the global
6
+ * dependency injection container. This context is available throughout
7
+ * the entire request/event lifecycle.
8
+ *
9
+ * @module context/lithia-context
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.lithiaContext = void 0;
13
+ exports.getLithiaContext = getLithiaContext;
14
+ const node_async_hooks_1 = require("node:async_hooks");
15
+ /**
16
+ * AsyncLocalStorage instance for Lithia application context.
17
+ *
18
+ * Uses Node.js AsyncLocalStorage to provide implicit context propagation
19
+ * across async boundaries without explicit parameter passing.
20
+ *
21
+ * @see https://nodejs.org/api/async_hooks.html#class-asynclocalstorage
22
+ */
23
+ exports.lithiaContext = new node_async_hooks_1.AsyncLocalStorage();
24
+ /**
25
+ * Gets the current Lithia context.
26
+ *
27
+ * @returns The current LithiaContext
28
+ * @throws {Error} If called outside of a request or event handler context
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const ctx = getLithiaContext();
33
+ * const db = ctx.dependencies.get(dbKey);
34
+ * ```
35
+ */
36
+ function getLithiaContext() {
37
+ const ctx = exports.lithiaContext.getStore();
38
+ if (!ctx) {
39
+ throw new Error("Lithia context not found. Are you accessing dependencies outside of a request or event handler?");
40
+ }
41
+ return ctx;
42
+ }
43
+ //# sourceMappingURL=lithia-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lithia-context.js","sourceRoot":"","sources":["../../src/context/lithia-context.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AA0CH,4CAQC;AAhDD,uDAAqD;AAkBrD;;;;;;;GAOG;AACU,QAAA,aAAa,GAAG,IAAI,oCAAiB,EAAiB,CAAC;AAEpE;;;;;;;;;;;GAWG;AACH,SAAgB,gBAAgB;IAC/B,MAAM,GAAG,GAAG,qBAAa,CAAC,QAAQ,EAAE,CAAC;IACrC,IAAI,CAAC,GAAG,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CACd,iGAAiG,CACjG,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC"}
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Route context module for HTTP requests.
3
+ *
4
+ * Provides context specific to HTTP route handling, including access to
5
+ * the current request, response, and matched route information.
6
+ *
7
+ * @module context/route-context
8
+ */
9
+ import { AsyncLocalStorage } from "node:async_hooks";
10
+ import type { Route } from "@lithia-js/native";
11
+ import type { Server } from "socket.io";
12
+ import type { LithiaRequest } from "../server/request";
13
+ import type { LithiaResponse } from "../server/response";
14
+ /**
15
+ * HTTP route handler context.
16
+ *
17
+ * Contains request-specific information available to route handlers
18
+ * and middlewares during HTTP request processing.
19
+ */
20
+ export interface RouteContext {
21
+ /**
22
+ * The current HTTP request object.
23
+ *
24
+ * Provides access to request data like params, query, headers, and body.
25
+ */
26
+ req: LithiaRequest;
27
+ /**
28
+ * The current HTTP response object.
29
+ *
30
+ * Used to send responses back to the client.
31
+ */
32
+ res: LithiaResponse;
33
+ /**
34
+ * The matched route definition.
35
+ *
36
+ * Contains metadata about the current route including path, method,
37
+ * and handler information. May be undefined before route resolution
38
+ * or in 404 handlers.
39
+ */
40
+ route?: Route;
41
+ /**
42
+ * The Socket.IO server instance.
43
+ *
44
+ * Provides access to the Socket.IO server, allowing you to emit events
45
+ * to all connected clients, manage rooms, or access server-level features
46
+ * from within HTTP route handlers.
47
+ *
48
+ * This is useful for scenarios where an HTTP request needs to trigger
49
+ * real-time updates to connected Socket.IO clients.
50
+ */
51
+ socketServer: Server;
52
+ }
53
+ /**
54
+ * AsyncLocalStorage instance for route context.
55
+ *
56
+ * Uses Node.js AsyncLocalStorage to provide implicit context propagation
57
+ * for HTTP request handling across async boundaries.
58
+ *
59
+ * @see https://nodejs.org/api/async_hooks.html#class-asynclocalstorage
60
+ */
61
+ export declare const routeContext: AsyncLocalStorage<RouteContext>;
62
+ /**
63
+ * Gets the current route context.
64
+ *
65
+ * @returns The current RouteContext
66
+ * @throws {Error} If called outside of an HTTP request handler
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const ctx = getRouteContext();
71
+ * console.log(ctx.req.method, ctx.req.url);
72
+ * ```
73
+ */
74
+ export declare function getRouteContext(): RouteContext;