fluxion-ts 0.0.4 → 0.0.5

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 (2) hide show
  1. package/README.md +173 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -9,3 +9,176 @@
9
9
  <img src="https://raw.githubusercontent.com/baendlorel/fluxion/refs/heads/main/assets/fluxion.svg" width="240px" alt="fluxion logo" />
10
10
  </a>
11
11
  </p>
12
+
13
+ Fluxion is a filesystem-routing dynamic server for Node.js.
14
+
15
+ - Use `.mjs` files directly as route handlers
16
+ - Run handlers inside worker runtime isolation
17
+ - Inject any npm module into handler `context` via `modules`
18
+ - If a handler returns a value, Fluxion auto-responds with `200 + JSON`
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ npm install fluxion
24
+ ```
25
+
26
+ ## Quick Start
27
+
28
+ ### 1) Start the server
29
+
30
+ Create `server.mjs`:
31
+
32
+ ```js
33
+ import { fluxion } from 'fluxion';
34
+
35
+ fluxion({
36
+ dir: './dynamicDirectory',
37
+ host: '127.0.0.1',
38
+ port: 3000,
39
+ });
40
+ ```
41
+
42
+ ### 2) Create a route handler
43
+
44
+ Create `dynamicDirectory/hello.mjs`:
45
+
46
+ ```js
47
+ export default function handler(_req, _res, context) {
48
+ return {
49
+ message: 'hello fluxion',
50
+ workerId: context.worker.id,
51
+ };
52
+ }
53
+ ```
54
+
55
+ Run:
56
+
57
+ ```bash
58
+ node server.mjs
59
+ ```
60
+
61
+ Test:
62
+
63
+ ```bash
64
+ curl http://127.0.0.1:3000/hello
65
+ ```
66
+
67
+ You will get a JSON response with status `200`.
68
+
69
+ ## Routing Rules
70
+
71
+ - `dynamicDirectory/index.mjs` -> `/`
72
+ - `dynamicDirectory/user.mjs` -> `/user`
73
+ - `dynamicDirectory/user/index.mjs` -> `/user`
74
+ - Non-`.mjs` files are served as static files (`GET/HEAD`)
75
+ - Directories/files starting with `_` are private and not routable
76
+
77
+ ## Handler Styles
78
+
79
+ ### Function export
80
+
81
+ ```js
82
+ export default function handler(req, res, context) {
83
+ return { ok: true };
84
+ }
85
+ ```
86
+
87
+ ### Object export (with modules)
88
+
89
+ ```js
90
+ export default {
91
+ modules: [
92
+ {
93
+ module: 'node:crypto',
94
+ injectKey: 'crypto',
95
+ factory: (cryptoModule) => cryptoModule,
96
+ },
97
+ ],
98
+ handler(_req, _res, context) {
99
+ return {
100
+ hash: context.crypto.createHash('sha1').update('abc').digest('hex'),
101
+ };
102
+ },
103
+ };
104
+ ```
105
+
106
+ ## Automatic JSON Response
107
+
108
+ If a handler return value is not `undefined` and you do not manually call `res.end()`, Fluxion will automatically:
109
+
110
+ - set status to `200`
111
+ - set `content-type` to `application/json; charset=utf-8` (if missing)
112
+ - serialize the return value with `JSON.stringify(...)`
113
+
114
+ Recommended pattern per handler:
115
+
116
+ 1. Return data directly (recommended)
117
+ 2. Or fully control `res` manually (streaming, file download, etc.)
118
+
119
+ ## Module Injection (Recommended)
120
+
121
+ Fluxion does not bundle database drivers. Install app dependencies yourself.
122
+
123
+ For example, to use MySQL in handlers:
124
+
125
+ ```bash
126
+ npm install mysql2
127
+ ```
128
+
129
+ ```js
130
+ export default {
131
+ modules: [
132
+ {
133
+ module: 'mysql2/promise',
134
+ injectKey: 'mydb',
135
+ options: {
136
+ host: '127.0.0.1',
137
+ user: 'root',
138
+ password: '***',
139
+ database: 'demo',
140
+ },
141
+ factory: (mysql2, options) => mysql2.createPool(options),
142
+ },
143
+ ],
144
+ async handler(_req, _res, context) {
145
+ const [rows] = await context.mydb.query('select 1 as ok');
146
+ return rows;
147
+ },
148
+ };
149
+ ```
150
+
151
+ ### `modules` fields
152
+
153
+ - `module`: module id used by dynamic `import()`
154
+ - `injectKey`: target key in `context[injectKey]`
155
+ - `options`: custom config passed into `factory`
156
+ - `factory`: `(importedModule, options, runtime) => injectedValue`
157
+
158
+ `factory` runs inside the worker and is restored from source text. Keep it self-contained (do not depend on outer closures).
159
+
160
+ Each worker keeps its own module instances. On worker shutdown, Fluxion will attempt `dispose/close/end/destroy` if present.
161
+
162
+ ## Common Options
163
+
164
+ Main `fluxion({...})` options:
165
+
166
+ - `dir`: dynamic directory (handler root)
167
+ - `host`: listen host
168
+ - `port`: listen port
169
+ - `maxRequestBytes`: max request body size (returns 413 when exceeded)
170
+ - `logger`: `one-line` / `json-line` / custom function
171
+
172
+ ## Important
173
+
174
+ Legacy handler-level `db` declarations are removed:
175
+
176
+ ```js
177
+ export default {
178
+ // db: ['main'] // no longer supported
179
+ modules: [],
180
+ handler() {},
181
+ };
182
+ ```
183
+
184
+ Use `modules` for dependency injection.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fluxion-ts",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"