keryx 0.42.1 → 0.42.3
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/README.md +10 -0
- package/initializers/swagger.ts +13 -0
- package/package.json +1 -1
- package/util/cli.ts +3 -1
- package/util/swaggerSchemaGenerator.ts +10 -3
package/README.md
CHANGED
|
@@ -252,6 +252,16 @@ If you're coming from ActionHero, the biggest changes are: unified controllers (
|
|
|
252
252
|
|
|
253
253
|
Each application has its own `Dockerfile`, and a `docker-compose.yml` runs them together. You probably won't use this exact setup in production, but it shows how the pieces fit together.
|
|
254
254
|
|
|
255
|
+
**Run `keryx build` at image build time.** It pre-generates the OpenAPI response schemas into `.cache/swagger-schemas.json` so the server doesn't have to derive them from your action return types on every boot:
|
|
256
|
+
|
|
257
|
+
```dockerfile
|
|
258
|
+
COPY . .
|
|
259
|
+
RUN cd backend && bun keryx.ts build
|
|
260
|
+
CMD ["bun", "keryx.ts", "start"]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
See the [deployment guide](https://keryxjs.com/guide/deployment#build-step) for details.
|
|
264
|
+
|
|
255
265
|
## Documentation
|
|
256
266
|
|
|
257
267
|
Full docs at [keryxjs.com](https://keryxjs.com), including:
|
package/initializers/swagger.ts
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
computeActionsHash,
|
|
6
6
|
generateSwaggerSchemas,
|
|
7
7
|
loadCachedSchemas,
|
|
8
|
+
SCHEMA_CACHE_RELATIVE_PATH,
|
|
8
9
|
writeSchemasCache,
|
|
9
10
|
} from "../util/swaggerSchemaGenerator";
|
|
10
11
|
|
|
@@ -34,6 +35,18 @@ export class SwaggerInitializer extends Initializer {
|
|
|
34
35
|
return { responseSchemas: cached.responseSchemas };
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
// No usable cache, so we fall through to ts-morph. This is by far the most
|
|
39
|
+
// expensive thing that happens at boot (it can peak over 1 GB of RSS on a
|
|
40
|
+
// large app) and it is the usual cause of an unexplained OOM kill during
|
|
41
|
+
// startup on a memory-capped host. Warn *before* doing the work — a process
|
|
42
|
+
// that cannot afford the allocation never reaches a message logged after it.
|
|
43
|
+
const cacheState = cached
|
|
44
|
+
? `swagger schema cache at ${SCHEMA_CACHE_RELATIVE_PATH} is stale`
|
|
45
|
+
: `no swagger schema cache found at ${SCHEMA_CACHE_RELATIVE_PATH}`;
|
|
46
|
+
logger.warn(
|
|
47
|
+
`${cacheState}; generating response schemas via ts-morph. This takes seconds and can peak over 1 GB of RSS, which will OOM a memory-capped host. Run \`keryx build\` at build time to pre-generate them and skip this step — see \`keryx build --help\`.`,
|
|
48
|
+
);
|
|
49
|
+
|
|
37
50
|
// Generate schemas via ts-morph
|
|
38
51
|
let responseSchemas: Record<string, JSONSchema> = {};
|
|
39
52
|
try {
|
package/package.json
CHANGED
package/util/cli.ts
CHANGED
|
@@ -107,7 +107,9 @@ export async function buildProgram(opts: {
|
|
|
107
107
|
.description(
|
|
108
108
|
"Run build-time code generation. Analyzes Action return types via ts-morph\n" +
|
|
109
109
|
"and writes .cache/swagger-schemas.json so the server can skip this step at\n" +
|
|
110
|
-
"startup.
|
|
110
|
+
"startup. Strongly recommended for memory-constrained environments (e.g.,\n" +
|
|
111
|
+
"Docker): the analysis can peak over 1 GB of RSS and will OOM a small\n" +
|
|
112
|
+
"container if it runs at boot instead. Needs no database and no Redis.\n\n" +
|
|
111
113
|
"Example Dockerfile usage:\n" +
|
|
112
114
|
" COPY . .\n" +
|
|
113
115
|
" RUN bun keryx.ts build\n" +
|
|
@@ -2,6 +2,13 @@ import { mkdir } from "fs/promises";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import type { Type } from "ts-morph";
|
|
4
4
|
|
|
5
|
+
/** Directory (relative to the app root) holding build-time generated artifacts. */
|
|
6
|
+
export const SCHEMA_CACHE_DIR = ".cache";
|
|
7
|
+
/** File name of the pre-generated swagger response schema cache. */
|
|
8
|
+
export const SCHEMA_CACHE_FILE = "swagger-schemas.json";
|
|
9
|
+
/** Human-readable path to the schema cache, for log and doc messages. */
|
|
10
|
+
export const SCHEMA_CACHE_RELATIVE_PATH = `${SCHEMA_CACHE_DIR}/${SCHEMA_CACHE_FILE}`;
|
|
11
|
+
|
|
5
12
|
export type JSONSchema = {
|
|
6
13
|
type?: string;
|
|
7
14
|
properties?: Record<string, JSONSchema>;
|
|
@@ -202,7 +209,7 @@ export async function loadCachedSchemas(rootDir: string): Promise<{
|
|
|
202
209
|
hash: string;
|
|
203
210
|
responseSchemas: Record<string, JSONSchema>;
|
|
204
211
|
} | null> {
|
|
205
|
-
const cacheFile = path.join(rootDir,
|
|
212
|
+
const cacheFile = path.join(rootDir, SCHEMA_CACHE_DIR, SCHEMA_CACHE_FILE);
|
|
206
213
|
const cacheFileHandle = Bun.file(cacheFile);
|
|
207
214
|
if (await cacheFileHandle.exists()) {
|
|
208
215
|
try {
|
|
@@ -325,10 +332,10 @@ export async function writeSchemasCache(
|
|
|
325
332
|
rootDir: string,
|
|
326
333
|
data: { hash: string; responseSchemas: Record<string, JSONSchema> },
|
|
327
334
|
): Promise<void> {
|
|
328
|
-
const cacheDir = path.join(rootDir,
|
|
335
|
+
const cacheDir = path.join(rootDir, SCHEMA_CACHE_DIR);
|
|
329
336
|
await mkdir(cacheDir, { recursive: true });
|
|
330
337
|
await Bun.write(
|
|
331
|
-
path.join(cacheDir,
|
|
338
|
+
path.join(cacheDir, SCHEMA_CACHE_FILE),
|
|
332
339
|
JSON.stringify(data, null, 2),
|
|
333
340
|
);
|
|
334
341
|
}
|