kempo-server 2.2.0 → 3.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.
- package/CONFIG.md +295 -187
- package/README.md +5 -4
- package/SPA.md +14 -14
- package/dist/defaultConfig.js +1 -1
- package/dist/index.js +1 -1
- package/dist/render.js +2 -0
- package/dist/router.js +1 -1
- package/dist/serveFile.js +1 -1
- package/dist/templating/index.js +1 -0
- package/dist/templating/parse.js +1 -0
- package/docs/caching.html +103 -17
- package/docs/cli-utils.html +102 -16
- package/docs/configuration.html +104 -17
- package/docs/examples.html +104 -17
- package/docs/fs-utils.html +102 -16
- package/docs/getting-started.html +104 -17
- package/docs/index.html +176 -81
- package/docs/middleware.html +104 -17
- package/docs/request-response.html +104 -17
- package/docs/routing.html +104 -17
- package/docs/templating.html +292 -0
- package/docs-src/.config.js +11 -0
- package/docs-src/caching.page.html +220 -0
- package/docs-src/cli-utils.page.html +71 -0
- package/docs-src/configuration.page.html +310 -0
- package/docs-src/default.template.html +35 -0
- package/docs-src/examples.page.html +192 -0
- package/docs-src/fs-utils.page.html +102 -0
- package/docs-src/getting-started.page.html +63 -0
- package/docs-src/index.page.html +79 -0
- package/docs-src/middleware.page.html +133 -0
- package/docs-src/nav.fragment.html +73 -0
- package/docs-src/request-response.page.html +96 -0
- package/docs-src/routing.page.html +73 -0
- package/docs-src/templating.page.html +188 -0
- package/llms.txt +97 -31
- package/package.json +5 -2
- package/scripts/build.js +22 -1
- package/scripts/render.js +58 -0
- package/src/defaultConfig.js +14 -2
- package/src/index.js +1 -1
- package/src/router.js +69 -10
- package/src/serveFile.js +27 -0
- package/src/templating/index.js +132 -0
- package/src/templating/parse.js +285 -0
- package/tests/cacheConfig.node-test.js +2 -2
- package/tests/config-flag.node-test.js +61 -25
- package/tests/customRoute-outside-root.node-test.js +1 -1
- package/tests/router-wildcard.node-test.js +47 -2
- package/tests/templating-parse.node-test.js +243 -0
- package/tests/templating-render.node-test.js +188 -0
- package/tests/utils/test-scenario.js +4 -4
- package/docs/.config.json.example +0 -29
- package/docs/api/_admin/cache/DELETE.js +0 -28
- package/docs/api/_admin/cache/GET.js +0 -53
- package/docs/api/user/[id]/GET.js +0 -15
- package/docs/api/user/[id]/[info]/DELETE.js +0 -12
- package/docs/api/user/[id]/[info]/GET.js +0 -17
- package/docs/api/user/[id]/[info]/POST.js +0 -18
- package/docs/api/user/[id]/[info]/PUT.js +0 -19
- package/docs/init.js +0 -2
- package/docs/nav.inc.html +0 -70
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
<page pageName="Module Caching" title="Module Caching - Kempo Server">
|
|
2
|
+
<content>
|
|
3
|
+
<p>Kempo Server includes an intelligent module caching system that dramatically improves performance by caching JavaScript route modules in memory.</p>
|
|
4
|
+
|
|
5
|
+
<h2>How It Works</h2>
|
|
6
|
+
<p>The cache system combines multiple strategies to optimize performance while preventing memory issues:</p>
|
|
7
|
+
<ul>
|
|
8
|
+
<li><strong>LRU (Least Recently Used)</strong> - Evicts oldest modules when cache fills</li>
|
|
9
|
+
<li><strong>Time-based expiration</strong> - Modules expire after configurable TTL</li>
|
|
10
|
+
<li><strong>Memory monitoring</strong> - Automatically clears cache if memory usage gets too high</li>
|
|
11
|
+
<li><strong>File watching</strong> - Instantly invalidates cache when files change (development)</li>
|
|
12
|
+
</ul>
|
|
13
|
+
|
|
14
|
+
<h2>Basic Configuration</h2>
|
|
15
|
+
<p>Add a <code>cache</code> object to your <code>.config.json</code> file:</p>
|
|
16
|
+
<pre><code class="hljs json">{
|
|
17
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
18
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
|
|
19
|
+
<span class="hljs-attr">"maxSize"</span>: <span class="hljs-number">100</span>,
|
|
20
|
+
<span class="hljs-attr">"maxMemoryMB"</span>: <span class="hljs-number">50</span>,
|
|
21
|
+
<span class="hljs-attr">"ttlMs"</span>: <span class="hljs-number">300000</span>,
|
|
22
|
+
<span class="hljs-attr">"watchFiles"</span>: <span class="hljs-literal">true</span>
|
|
23
|
+
}
|
|
24
|
+
}</code></pre>
|
|
25
|
+
|
|
26
|
+
<h2>Configuration Options</h2>
|
|
27
|
+
<table>
|
|
28
|
+
<thead>
|
|
29
|
+
<tr>
|
|
30
|
+
<th>Option</th>
|
|
31
|
+
<th>Type</th>
|
|
32
|
+
<th>Default</th>
|
|
33
|
+
<th>Description</th>
|
|
34
|
+
</tr>
|
|
35
|
+
</thead>
|
|
36
|
+
<tbody>
|
|
37
|
+
<tr>
|
|
38
|
+
<td><code>enabled</code></td>
|
|
39
|
+
<td>boolean</td>
|
|
40
|
+
<td><code>true</code></td>
|
|
41
|
+
<td>Enable/disable the entire caching system</td>
|
|
42
|
+
</tr>
|
|
43
|
+
<tr>
|
|
44
|
+
<td><code>maxSize</code></td>
|
|
45
|
+
<td>number</td>
|
|
46
|
+
<td><code>100</code></td>
|
|
47
|
+
<td>Maximum number of modules to cache</td>
|
|
48
|
+
</tr>
|
|
49
|
+
<tr>
|
|
50
|
+
<td><code>maxMemoryMB</code></td>
|
|
51
|
+
<td>number</td>
|
|
52
|
+
<td><code>50</code></td>
|
|
53
|
+
<td>Memory limit for cache in megabytes</td>
|
|
54
|
+
</tr>
|
|
55
|
+
<tr>
|
|
56
|
+
<td><code>ttlMs</code></td>
|
|
57
|
+
<td>number</td>
|
|
58
|
+
<td><code>300000</code></td>
|
|
59
|
+
<td>Time to live in milliseconds (5 minutes)</td>
|
|
60
|
+
</tr>
|
|
61
|
+
<tr>
|
|
62
|
+
<td><code>maxHeapUsagePercent</code></td>
|
|
63
|
+
<td>number</td>
|
|
64
|
+
<td><code>70</code></td>
|
|
65
|
+
<td>Clear cache when heap usage exceeds this percentage</td>
|
|
66
|
+
</tr>
|
|
67
|
+
<tr>
|
|
68
|
+
<td><code>memoryCheckInterval</code></td>
|
|
69
|
+
<td>number</td>
|
|
70
|
+
<td><code>30000</code></td>
|
|
71
|
+
<td>How often to check memory usage (milliseconds)</td>
|
|
72
|
+
</tr>
|
|
73
|
+
<tr>
|
|
74
|
+
<td><code>watchFiles</code></td>
|
|
75
|
+
<td>boolean</td>
|
|
76
|
+
<td><code>true</code></td>
|
|
77
|
+
<td>Auto-invalidate cache when files change</td>
|
|
78
|
+
</tr>
|
|
79
|
+
<tr>
|
|
80
|
+
<td><code>enableMemoryMonitoring</code></td>
|
|
81
|
+
<td>boolean</td>
|
|
82
|
+
<td><code>true</code></td>
|
|
83
|
+
<td>Enable automatic memory monitoring</td>
|
|
84
|
+
</tr>
|
|
85
|
+
</tbody>
|
|
86
|
+
</table>
|
|
87
|
+
|
|
88
|
+
<h2>Environment-Specific Configuration</h2>
|
|
89
|
+
<p>Use separate configuration files for different environments instead of nested objects:</p>
|
|
90
|
+
|
|
91
|
+
<h3>Development Configuration</h3>
|
|
92
|
+
<p>Create <code>dev.config.json</code> with settings optimized for development:</p>
|
|
93
|
+
<pre><code class="hljs json">{
|
|
94
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
95
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
|
|
96
|
+
<span class="hljs-attr">"maxSize"</span>: <span class="hljs-number">50</span>,
|
|
97
|
+
<span class="hljs-attr">"maxMemoryMB"</span>: <span class="hljs-number">25</span>,
|
|
98
|
+
<span class="hljs-attr">"ttlMs"</span>: <span class="hljs-number">300000</span>,
|
|
99
|
+
<span class="hljs-attr">"watchFiles"</span>: <span class="hljs-literal">true</span>
|
|
100
|
+
}
|
|
101
|
+
}</code></pre>
|
|
102
|
+
<pre><code class="hljs bash">node src/index.js --config dev.config.json</code></pre>
|
|
103
|
+
|
|
104
|
+
<h3>Production Configuration</h3>
|
|
105
|
+
<p>Create <code>prod.config.json</code> with settings optimized for production:</p>
|
|
106
|
+
<pre><code class="hljs json">{
|
|
107
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
108
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
|
|
109
|
+
<span class="hljs-attr">"maxSize"</span>: <span class="hljs-number">1000</span>,
|
|
110
|
+
<span class="hljs-attr">"maxMemoryMB"</span>: <span class="hljs-number">200</span>,
|
|
111
|
+
<span class="hljs-attr">"ttlMs"</span>: <span class="hljs-number">3600000</span>,
|
|
112
|
+
<span class="hljs-attr">"watchFiles"</span>: <span class="hljs-literal">false</span>
|
|
113
|
+
}
|
|
114
|
+
}</code></pre>
|
|
115
|
+
<pre><code class="hljs bash">node src/index.js --config prod.config.json</code></pre>
|
|
116
|
+
|
|
117
|
+
<h2>Cache Monitoring</h2>
|
|
118
|
+
<p>Monitor cache performance and manage cached modules at runtime:</p>
|
|
119
|
+
|
|
120
|
+
<h3>View Cache Statistics</h3>
|
|
121
|
+
<pre><code class="hljs bash">curl http://localhost:3000/_admin/cache</code></pre>
|
|
122
|
+
<p>Returns detailed statistics including:</p>
|
|
123
|
+
<ul>
|
|
124
|
+
<li>Cache size and memory usage</li>
|
|
125
|
+
<li>Hit/miss rates</li>
|
|
126
|
+
<li>Node.js memory usage</li>
|
|
127
|
+
<li>List of cached files</li>
|
|
128
|
+
</ul>
|
|
129
|
+
|
|
130
|
+
<h3>Clear Cache</h3>
|
|
131
|
+
<pre><code class="hljs bash">curl -X DELETE http://localhost:3000/_admin/cache</code></pre>
|
|
132
|
+
<p>Immediately clears all cached modules.</p>
|
|
133
|
+
|
|
134
|
+
<h2>Performance Tuning</h2>
|
|
135
|
+
|
|
136
|
+
<h3>Memory Settings</h3>
|
|
137
|
+
<ul>
|
|
138
|
+
<li>Set <code>maxMemoryMB</code> to 10-20% of available server RAM</li>
|
|
139
|
+
<li>Use lower <code>maxHeapUsagePercent</code> (60-70%) for memory-constrained environments</li>
|
|
140
|
+
<li>Monitor actual usage with <code>/_admin/cache</code> endpoint</li>
|
|
141
|
+
</ul>
|
|
142
|
+
|
|
143
|
+
<h3>Cache Size</h3>
|
|
144
|
+
<ul>
|
|
145
|
+
<li>Start with <code>maxSize</code> = 10x your typical concurrent routes</li>
|
|
146
|
+
<li>Increase if you have many route files and sufficient memory</li>
|
|
147
|
+
<li>Decrease for microservices with few routes</li>
|
|
148
|
+
</ul>
|
|
149
|
+
|
|
150
|
+
<h3>TTL Settings</h3>
|
|
151
|
+
<ul>
|
|
152
|
+
<li><strong>Development:</strong> Short TTL (5-10 minutes) for quick iteration</li>
|
|
153
|
+
<li><strong>Production:</strong> Longer TTL (30-60 minutes) for stability</li>
|
|
154
|
+
<li><strong>High-change environments:</strong> Shorter TTL or rely on file watching</li>
|
|
155
|
+
</ul>
|
|
156
|
+
|
|
157
|
+
<h3>File Watching</h3>
|
|
158
|
+
<ul>
|
|
159
|
+
<li><strong>Enable</strong> (<code>watchFiles: true</code>) in development for instant invalidation</li>
|
|
160
|
+
<li><strong>Disable</strong> (<code>watchFiles: false</code>) in production to reduce overhead</li>
|
|
161
|
+
</ul>
|
|
162
|
+
|
|
163
|
+
<h2>Configuration Examples</h2>
|
|
164
|
+
|
|
165
|
+
<h3>Minimal Configuration</h3>
|
|
166
|
+
<p>Just enable caching with defaults:</p>
|
|
167
|
+
<pre><code class="hljs json">{
|
|
168
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
169
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>
|
|
170
|
+
}
|
|
171
|
+
}</code></pre>
|
|
172
|
+
|
|
173
|
+
<h3>High-Traffic Configuration</h3>
|
|
174
|
+
<p>For servers with lots of routes and traffic:</p>
|
|
175
|
+
<pre><code class="hljs json">{
|
|
176
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
177
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
|
|
178
|
+
<span class="hljs-attr">"maxSize"</span>: <span class="hljs-number">2000</span>,
|
|
179
|
+
<span class="hljs-attr">"maxMemoryMB"</span>: <span class="hljs-number">500</span>,
|
|
180
|
+
<span class="hljs-attr">"ttlMs"</span>: <span class="hljs-number">7200000</span>,
|
|
181
|
+
<span class="hljs-attr">"watchFiles"</span>: <span class="hljs-literal">false</span>
|
|
182
|
+
}
|
|
183
|
+
}</code></pre>
|
|
184
|
+
|
|
185
|
+
<h3>Memory-Constrained Configuration</h3>
|
|
186
|
+
<p>For servers with limited RAM:</p>
|
|
187
|
+
<pre><code class="hljs json">{
|
|
188
|
+
<span class="hljs-attr">"cache"</span>: {
|
|
189
|
+
<span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">true</span>,
|
|
190
|
+
<span class="hljs-attr">"maxSize"</span>: <span class="hljs-number">25</span>,
|
|
191
|
+
<span class="hljs-attr">"maxMemoryMB"</span>: <span class="hljs-number">10</span>,
|
|
192
|
+
<span class="hljs-attr">"ttlMs"</span>: <span class="hljs-number">120000</span>,
|
|
193
|
+
<span class="hljs-attr">"maxHeapUsagePercent"</span>: <span class="hljs-number">60</span>
|
|
194
|
+
}
|
|
195
|
+
}</code></pre>
|
|
196
|
+
|
|
197
|
+
<h2>Troubleshooting</h2>
|
|
198
|
+
|
|
199
|
+
<h3>Cache Not Working</h3>
|
|
200
|
+
<ul>
|
|
201
|
+
<li>Verify <code>cache.enabled</code> is <code>true</code></li>
|
|
202
|
+
<li>Check file permissions for watching</li>
|
|
203
|
+
<li>Review server logs for cache statistics</li>
|
|
204
|
+
</ul>
|
|
205
|
+
|
|
206
|
+
<h3>Memory Issues</h3>
|
|
207
|
+
<ul>
|
|
208
|
+
<li>Reduce <code>maxMemoryMB</code> and <code>maxSize</code></li>
|
|
209
|
+
<li>Lower <code>maxHeapUsagePercent</code></li>
|
|
210
|
+
<li>Enable more aggressive memory monitoring</li>
|
|
211
|
+
</ul>
|
|
212
|
+
|
|
213
|
+
<h3>Performance Issues</h3>
|
|
214
|
+
<ul>
|
|
215
|
+
<li>Increase cache limits if you have available memory</li>
|
|
216
|
+
<li>Extend <code>ttlMs</code> for stable route files</li>
|
|
217
|
+
<li>Monitor hit rates with admin endpoints</li>
|
|
218
|
+
</ul>
|
|
219
|
+
</content>
|
|
220
|
+
</page>
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
<page pageName="CLI Utilities" title="CLI Utilities - Kempo Server">
|
|
2
|
+
<content>
|
|
3
|
+
<p>The CLI utilities provide simple command-line argument parsing functionality for Node.js applications.</p>
|
|
4
|
+
|
|
5
|
+
<h2>Installation</h2>
|
|
6
|
+
<p>Import the utilities from the kempo-server package:</p>
|
|
7
|
+
<pre><code class="hljs javascript">import { getArgs } from 'kempo-server/utils/cli';</code></pre>
|
|
8
|
+
|
|
9
|
+
<h2>getArgs(mapping, argv)</h2>
|
|
10
|
+
<p>Parses command-line arguments into a key-value object.</p>
|
|
11
|
+
|
|
12
|
+
<h3>Parameters</h3>
|
|
13
|
+
<ul>
|
|
14
|
+
<li><code>mapping</code> (object, optional) - Maps short flag names to full names</li>
|
|
15
|
+
<li><code>argv</code> (array, optional) - Array of arguments to parse (defaults to <code>process.argv</code>)</li>
|
|
16
|
+
</ul>
|
|
17
|
+
|
|
18
|
+
<h3>Returns</h3>
|
|
19
|
+
<p>An object where keys are argument names and values are parsed argument values.</p>
|
|
20
|
+
|
|
21
|
+
<h3>Features</h3>
|
|
22
|
+
<ul>
|
|
23
|
+
<li>Supports both space-separated (<code>--flag value</code>) and equals-separated (<code>--flag=value</code>) formats</li>
|
|
24
|
+
<li>Automatically converts <code>"true"</code> and <code>"false"</code> strings to boolean values</li>
|
|
25
|
+
<li>Supports short flags with mapping (e.g., <code>-p</code> maps to <code>port</code>)</li>
|
|
26
|
+
<li>Handles multiple values for the same flag</li>
|
|
27
|
+
<li>Boolean flags without values are set to <code>true</code></li>
|
|
28
|
+
</ul>
|
|
29
|
+
|
|
30
|
+
<h3>Examples</h3>
|
|
31
|
+
|
|
32
|
+
<h4>Basic Usage</h4>
|
|
33
|
+
<pre><code class="hljs bash">node app.js --port 8080 --host localhost --verbose</code></pre>
|
|
34
|
+
<pre><code class="hljs javascript">const args = getArgs();
|
|
35
|
+
// Result: { port: '8080', host: 'localhost', verbose: true }</code></pre>
|
|
36
|
+
|
|
37
|
+
<h4>With Short Flag Mapping</h4>
|
|
38
|
+
<pre><code class="hljs bash">node app.js -p 8080 -h localhost -v</code></pre>
|
|
39
|
+
<pre><code class="hljs javascript">const args = getArgs({
|
|
40
|
+
p: 'port',
|
|
41
|
+
h: 'host',
|
|
42
|
+
v: 'verbose'
|
|
43
|
+
});
|
|
44
|
+
// Result: { port: '8080', host: 'localhost', verbose: true }</code></pre>
|
|
45
|
+
|
|
46
|
+
<h4>Equals-Separated Values</h4>
|
|
47
|
+
<pre><code class="hljs bash">node app.js --port=8080 --enabled=true --disabled=false --name=John</code></pre>
|
|
48
|
+
<pre><code class="hljs javascript">const args = getArgs();
|
|
49
|
+
// Result: { port: '8080', enabled: true, disabled: false, name: 'John' }</code></pre>
|
|
50
|
+
|
|
51
|
+
<h4>Mixed Formats</h4>
|
|
52
|
+
<pre><code class="hljs bash">node app.js --port=8080 -h localhost --verbose --config=prod.json</code></pre>
|
|
53
|
+
<pre><code class="hljs javascript">const args = getArgs({ h: 'host' });
|
|
54
|
+
// Result: { port: '8080', host: 'localhost', verbose: true, config: 'prod.json' }</code></pre>
|
|
55
|
+
|
|
56
|
+
<h4>Values with Equals Signs</h4>
|
|
57
|
+
<pre><code class="hljs bash">node app.js --url=https://example.com?param=value</code></pre>
|
|
58
|
+
<pre><code class="hljs javascript">const args = getArgs();
|
|
59
|
+
// Result: { url: 'https://example.com?param=value' }</code></pre>
|
|
60
|
+
|
|
61
|
+
<h2>Other Functions</h2>
|
|
62
|
+
<p>The CLI utilities module also exports other functions for running child processes and prompting users:</p>
|
|
63
|
+
<ul>
|
|
64
|
+
<li><code>runChildProcess(command)</code> - Run a shell command</li>
|
|
65
|
+
<li><code>runChildNodeProcess(scriptPath, argsObj)</code> - Run a Node.js script with arguments</li>
|
|
66
|
+
<li><code>runChildNodeProcessScript(scriptPath)</code> - Run a Node.js script</li>
|
|
67
|
+
<li><code>promptUser(query)</code> - Prompt user for input</li>
|
|
68
|
+
<li><code>promptYN(query, defaultValue)</code> - Prompt for yes/no with default</li>
|
|
69
|
+
</ul>
|
|
70
|
+
</content>
|
|
71
|
+
</page>
|