express-file-cluster 0.10.0 → 0.11.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 (194) hide show
  1. package/dist/auth/index.cjs +7 -6
  2. package/dist/auth/index.cjs.map +1 -1
  3. package/dist/auth/index.d.cts +3 -1
  4. package/dist/auth/index.d.ts +3 -1
  5. package/dist/auth/index.js +5 -5
  6. package/dist/{chunk-73XJUT2S.js → chunk-2IDRC7RK.js} +163 -5
  7. package/dist/chunk-2IDRC7RK.js.map +1 -0
  8. package/dist/{chunk-QDEDOHP7.js → chunk-336IU55W.js} +9 -42
  9. package/dist/chunk-336IU55W.js.map +1 -0
  10. package/dist/{chunk-R52CUEGT.js → chunk-3SWI46YK.js} +2 -2
  11. package/dist/{chunk-7KO4DJRJ.cjs → chunk-6WTMRZS7.cjs} +14 -14
  12. package/dist/{chunk-7KO4DJRJ.cjs.map → chunk-6WTMRZS7.cjs.map} +1 -1
  13. package/dist/chunk-AGOYTFRO.cjs +42 -0
  14. package/dist/chunk-AGOYTFRO.cjs.map +1 -0
  15. package/dist/{chunk-RAB44UGX.js → chunk-B6MB6HEG.js} +1 -1
  16. package/dist/chunk-CGY7JU4Y.cjs +8 -0
  17. package/dist/chunk-CGY7JU4Y.cjs.map +1 -0
  18. package/dist/chunk-DGYPIIJE.cjs +845 -0
  19. package/dist/chunk-DGYPIIJE.cjs.map +1 -0
  20. package/dist/{chunk-EGSSZUVO.js → chunk-DN4TCGYB.js} +38 -11
  21. package/dist/chunk-DN4TCGYB.js.map +1 -0
  22. package/dist/{chunk-YK4ZM3CA.cjs → chunk-DTMDJGRW.cjs} +15 -15
  23. package/dist/{chunk-YK4ZM3CA.cjs.map → chunk-DTMDJGRW.cjs.map} +1 -1
  24. package/dist/{chunk-R4XO4SVM.js → chunk-EJDZZ4KX.js} +4 -4
  25. package/dist/{chunk-NKLOE4AK.cjs → chunk-GWMW6XY7.cjs} +20 -53
  26. package/dist/chunk-GWMW6XY7.cjs.map +1 -0
  27. package/dist/{chunk-GBI77W5T.cjs → chunk-H2PVWCWC.cjs} +1 -1
  28. package/dist/{chunk-GBI77W5T.cjs.map → chunk-H2PVWCWC.cjs.map} +1 -1
  29. package/dist/{chunk-KFVFP2Q7.cjs → chunk-IEF6AEK3.cjs} +70 -55
  30. package/dist/chunk-IEF6AEK3.cjs.map +1 -0
  31. package/dist/{chunk-PGJQK36V.js → chunk-JBOMTZT5.js} +7 -5
  32. package/dist/{chunk-PGJQK36V.js.map → chunk-JBOMTZT5.js.map} +1 -1
  33. package/dist/chunk-LECHRAU3.js +845 -0
  34. package/dist/chunk-LECHRAU3.js.map +1 -0
  35. package/dist/{chunk-4DOHQU6I.cjs → chunk-M53GODCX.cjs} +39 -12
  36. package/dist/chunk-M53GODCX.cjs.map +1 -0
  37. package/dist/{chunk-NI2BNGGV.js → chunk-O2E7H2XA.js} +2 -2
  38. package/dist/{chunk-ATVD2HGC.js → chunk-OM2XJJWX.js} +4 -4
  39. package/dist/{chunk-DUADUR6R.cjs → chunk-QSIP7YLW.cjs} +172 -14
  40. package/dist/chunk-QSIP7YLW.cjs.map +1 -0
  41. package/dist/{chunk-7KC2H76D.js → chunk-R6GPQZLI.js} +62 -47
  42. package/dist/chunk-R6GPQZLI.js.map +1 -0
  43. package/dist/{chunk-WBLUGOAG.cjs → chunk-RPL2IFWK.cjs} +8 -5
  44. package/dist/chunk-RPL2IFWK.cjs.map +1 -0
  45. package/dist/{chunk-HBCY7ILD.cjs → chunk-SOU3PBJC.cjs} +97 -95
  46. package/dist/chunk-SOU3PBJC.cjs.map +1 -0
  47. package/dist/{chunk-DCJOWFVD.cjs → chunk-WHBSVYUY.cjs} +3 -3
  48. package/dist/{chunk-DCJOWFVD.cjs.map → chunk-WHBSVYUY.cjs.map} +1 -1
  49. package/dist/chunk-XPY5TQDK.js +42 -0
  50. package/dist/chunk-XPY5TQDK.js.map +1 -0
  51. package/dist/cli/db-runner.cjs +81 -42
  52. package/dist/cli/db-runner.cjs.map +1 -1
  53. package/dist/cli/db-runner.js +54 -16
  54. package/dist/cli/db-runner.js.map +1 -1
  55. package/dist/cli/index.cjs +23 -25
  56. package/dist/cli/index.cjs.map +1 -1
  57. package/dist/cli/index.js +12 -17
  58. package/dist/cli/index.js.map +1 -1
  59. package/dist/docs/api-reference/auth.md +23 -0
  60. package/dist/docs/api-reference/compose.md +1 -1
  61. package/dist/docs/api-reference/db.md +8 -0
  62. package/dist/docs/api-reference/ignite.md +8 -1
  63. package/dist/docs/api-reference/openapi.md +36 -0
  64. package/dist/docs/api-reference/testing.md +28 -0
  65. package/dist/docs/api-reference/websockets.md +23 -0
  66. package/dist/docs/cli/add.md +2 -2
  67. package/dist/docs/core-concepts/clustering.md +2 -0
  68. package/dist/docs/core-concepts/file-based-routing.md +4 -0
  69. package/dist/docs/core-concepts/middleware.md +2 -2
  70. package/dist/docs/getting-started/index.md +4 -0
  71. package/dist/docs/guides/authentication.md +33 -0
  72. package/dist/docs/guides/caching.md +2 -0
  73. package/dist/docs/guides/database.md +15 -0
  74. package/dist/docs/guides/environment-variables.md +6 -0
  75. package/dist/docs/guides/feature-matrix.md +9 -0
  76. package/dist/docs/guides/generated-portals.md +3 -1
  77. package/dist/docs/guides/logging.md +17 -0
  78. package/dist/docs/guides/migrations.md +4 -2
  79. package/dist/docs/guides/openapi.md +57 -0
  80. package/dist/docs/guides/testing.md +31 -0
  81. package/dist/docs/guides/validation.md +2 -0
  82. package/dist/docs/guides/websockets.md +60 -0
  83. package/dist/docs/index.md +4 -1
  84. package/dist/docs/skills/SKILL.md +8 -2
  85. package/dist/docs/skills/api-surface.md +30 -4
  86. package/dist/docs/skills/auth-internals.md +14 -1
  87. package/dist/docs/skills/pre-flight.md +8 -0
  88. package/dist/docs/skills/routing-rules.md +5 -1
  89. package/dist/docs/skills/what-not-to-invent.md +11 -1
  90. package/dist/docs/versioning.md +1 -1
  91. package/dist/index.cjs +100 -267
  92. package/dist/index.cjs.map +1 -1
  93. package/dist/index.d.cts +28 -3
  94. package/dist/index.d.ts +28 -3
  95. package/dist/index.js +52 -221
  96. package/dist/index.js.map +1 -1
  97. package/dist/openapi-cjs.cjs +5 -0
  98. package/dist/pgboss-backend-PQW6I3VF.js +14 -0
  99. package/dist/pgboss-backend-QVXISTLP.cjs +15 -0
  100. package/dist/pgboss-backend-QVXISTLP.cjs.map +1 -0
  101. package/dist/registry/index.json +27 -3
  102. package/dist/registry/js-mongo/account-security.json +1 -1
  103. package/dist/registry/js-mongo/admin-users.json +1 -1
  104. package/dist/registry/js-mongo/auth.json +1 -1
  105. package/dist/registry/js-mongo/auth@apple.json +1 -1
  106. package/dist/registry/js-mongo/auth@google.json +1 -1
  107. package/dist/registry/js-mongo/forgot-password.json +1 -1
  108. package/dist/registry/js-mongo/user-profile.json +1 -1
  109. package/dist/registry/js-mongo-collection/account-security.json +1 -1
  110. package/dist/registry/js-mongo-collection/admin-users.json +1 -1
  111. package/dist/registry/js-mongo-collection/auth.json +1 -1
  112. package/dist/registry/js-mongo-collection/auth@apple.json +1 -1
  113. package/dist/registry/js-mongo-collection/auth@google.json +1 -1
  114. package/dist/registry/js-mongo-collection/forgot-password.json +1 -1
  115. package/dist/registry/js-mongo-collection/user-profile.json +1 -1
  116. package/dist/registry/js-pg-model/account-security.json +1 -1
  117. package/dist/registry/js-pg-model/admin-users.json +1 -1
  118. package/dist/registry/js-pg-model/auth.json +1 -1
  119. package/dist/registry/js-pg-model/auth@apple.json +1 -1
  120. package/dist/registry/js-pg-model/auth@google.json +1 -1
  121. package/dist/registry/js-pg-model/forgot-password.json +1 -1
  122. package/dist/registry/js-pg-model/user-profile.json +1 -1
  123. package/dist/registry/js-pg-table/account-security.json +1 -1
  124. package/dist/registry/js-pg-table/admin-users.json +1 -1
  125. package/dist/registry/js-pg-table/auth.json +1 -1
  126. package/dist/registry/js-pg-table/auth@apple.json +1 -1
  127. package/dist/registry/js-pg-table/auth@google.json +1 -1
  128. package/dist/registry/js-pg-table/forgot-password.json +1 -1
  129. package/dist/registry/js-pg-table/user-profile.json +1 -1
  130. package/dist/registry/ts-mongo/account-security.json +1 -1
  131. package/dist/registry/ts-mongo/admin-users.json +1 -1
  132. package/dist/registry/ts-mongo/auth.json +1 -1
  133. package/dist/registry/ts-mongo/auth@apple.json +1 -1
  134. package/dist/registry/ts-mongo/auth@google.json +1 -1
  135. package/dist/registry/ts-mongo/forgot-password.json +1 -1
  136. package/dist/registry/ts-mongo/user-profile.json +1 -1
  137. package/dist/registry/ts-mongo-collection/account-security.json +1 -1
  138. package/dist/registry/ts-mongo-collection/admin-users.json +1 -1
  139. package/dist/registry/ts-mongo-collection/auth.json +1 -1
  140. package/dist/registry/ts-mongo-collection/auth@apple.json +1 -1
  141. package/dist/registry/ts-mongo-collection/auth@google.json +1 -1
  142. package/dist/registry/ts-mongo-collection/forgot-password.json +1 -1
  143. package/dist/registry/ts-mongo-collection/user-profile.json +1 -1
  144. package/dist/registry/ts-pg-model/account-security.json +1 -1
  145. package/dist/registry/ts-pg-model/admin-users.json +1 -1
  146. package/dist/registry/ts-pg-model/auth.json +1 -1
  147. package/dist/registry/ts-pg-model/auth@apple.json +1 -1
  148. package/dist/registry/ts-pg-model/auth@google.json +1 -1
  149. package/dist/registry/ts-pg-model/forgot-password.json +1 -1
  150. package/dist/registry/ts-pg-model/user-profile.json +1 -1
  151. package/dist/registry/ts-pg-table/account-security.json +1 -1
  152. package/dist/registry/ts-pg-table/admin-users.json +1 -1
  153. package/dist/registry/ts-pg-table/auth.json +1 -1
  154. package/dist/registry/ts-pg-table/auth@apple.json +1 -1
  155. package/dist/registry/ts-pg-table/auth@google.json +1 -1
  156. package/dist/registry/ts-pg-table/forgot-password.json +1 -1
  157. package/dist/registry/ts-pg-table/user-profile.json +1 -1
  158. package/dist/tasks/index.cjs +3 -2
  159. package/dist/tasks/index.cjs.map +1 -1
  160. package/dist/tasks/index.d.cts +3 -1
  161. package/dist/tasks/index.d.ts +3 -1
  162. package/dist/tasks/index.js +1 -1
  163. package/dist/testing-cjs.cjs +5 -0
  164. package/dist/testing.cjs +65 -0
  165. package/dist/testing.cjs.map +1 -0
  166. package/dist/testing.d.cts +30 -0
  167. package/dist/testing.d.ts +30 -0
  168. package/dist/testing.js +64 -0
  169. package/dist/testing.js.map +1 -0
  170. package/dist/thread-runner.cjs +4 -3
  171. package/dist/thread-runner.cjs.map +1 -1
  172. package/dist/thread-runner.js +2 -2
  173. package/dist/{types-Bnxvj3l0.d.ts → types-D3QmZgeX.d.cts} +88 -2
  174. package/dist/{types-Bnxvj3l0.d.cts → types-D3QmZgeX.d.ts} +88 -2
  175. package/package.json +11 -4
  176. package/dist/chunk-4DOHQU6I.cjs.map +0 -1
  177. package/dist/chunk-73XJUT2S.js.map +0 -1
  178. package/dist/chunk-7KC2H76D.js.map +0 -1
  179. package/dist/chunk-DUADUR6R.cjs.map +0 -1
  180. package/dist/chunk-EGSSZUVO.js.map +0 -1
  181. package/dist/chunk-HBCY7ILD.cjs.map +0 -1
  182. package/dist/chunk-KFVFP2Q7.cjs.map +0 -1
  183. package/dist/chunk-NKLOE4AK.cjs.map +0 -1
  184. package/dist/chunk-QDEDOHP7.js.map +0 -1
  185. package/dist/chunk-WBLUGOAG.cjs.map +0 -1
  186. package/dist/pgboss-backend-FW42SBSM.js +0 -14
  187. package/dist/pgboss-backend-RVHUBJTU.cjs +0 -14
  188. package/dist/pgboss-backend-RVHUBJTU.cjs.map +0 -1
  189. /package/dist/{chunk-R52CUEGT.js.map → chunk-3SWI46YK.js.map} +0 -0
  190. /package/dist/{chunk-RAB44UGX.js.map → chunk-B6MB6HEG.js.map} +0 -0
  191. /package/dist/{chunk-R4XO4SVM.js.map → chunk-EJDZZ4KX.js.map} +0 -0
  192. /package/dist/{chunk-NI2BNGGV.js.map → chunk-O2E7H2XA.js.map} +0 -0
  193. /package/dist/{chunk-ATVD2HGC.js.map → chunk-OM2XJJWX.js.map} +0 -0
  194. /package/dist/{pgboss-backend-FW42SBSM.js.map → pgboss-backend-PQW6I3VF.js.map} +0 -0
package/dist/index.js CHANGED
@@ -1,3 +1,12 @@
1
+ import {
2
+ attachWebSockets,
3
+ closeWebSockets,
4
+ createHttpApp,
5
+ generateOpenAPI,
6
+ mountErrorHandler,
7
+ mountOpenAPI,
8
+ mountRoutes
9
+ } from "./chunk-LECHRAU3.js";
1
10
  import {
2
11
  cache,
3
12
  closeCache,
@@ -6,14 +15,14 @@ import {
6
15
  isCacheConfigured,
7
16
  rawIncr,
8
17
  servePrimaryCache
9
- } from "./chunk-7KC2H76D.js";
18
+ } from "./chunk-R6GPQZLI.js";
10
19
  import {
11
20
  scanDir
12
- } from "./chunk-NI2BNGGV.js";
21
+ } from "./chunk-O2E7H2XA.js";
13
22
  import {
14
23
  closeBullMQ,
15
24
  initBullMQ
16
- } from "./chunk-ATVD2HGC.js";
25
+ } from "./chunk-OM2XJJWX.js";
17
26
  import {
18
27
  connectMongo,
19
28
  db,
@@ -23,23 +32,25 @@ import {
23
32
  getDbClient,
24
33
  setDbClient,
25
34
  transaction
26
- } from "./chunk-PGJQK36V.js";
35
+ } from "./chunk-JBOMTZT5.js";
27
36
  import {
28
- HttpError,
29
37
  ModelValidationError,
30
- isHttpError,
31
38
  setAutoSync,
32
39
  setDbEngine
33
- } from "./chunk-QDEDOHP7.js";
40
+ } from "./chunk-336IU55W.js";
41
+ import {
42
+ HttpError,
43
+ isHttpError
44
+ } from "./chunk-XPY5TQDK.js";
34
45
  import {
35
46
  closePgBoss,
36
47
  initPgBoss
37
- } from "./chunk-R4XO4SVM.js";
38
- import "./chunk-R52CUEGT.js";
48
+ } from "./chunk-EJDZZ4KX.js";
49
+ import "./chunk-3SWI46YK.js";
39
50
  import {
40
51
  listTasks,
41
52
  registerTask
42
- } from "./chunk-RAB44UGX.js";
53
+ } from "./chunk-B6MB6HEG.js";
43
54
  import {
44
55
  checkConnectionBudget,
45
56
  configureLogger,
@@ -47,153 +58,19 @@ import {
47
58
  connectPostgres,
48
59
  getCurrentUser,
49
60
  getLogger,
61
+ getQueryMetrics,
50
62
  logger,
51
- requestLogger,
52
- runWithContext
53
- } from "./chunk-73XJUT2S.js";
63
+ resetQueryMetrics
64
+ } from "./chunk-2IDRC7RK.js";
54
65
 
55
66
  // src/index.ts
67
+ import http from "http";
56
68
  import fs2 from "fs";
57
69
  import express from "express";
58
- import cors from "cors";
59
- import cookieParser from "cookie-parser";
60
70
  import cluster2 from "cluster";
61
71
  import os from "os";
62
72
  import path2 from "path";
63
73
 
64
- // src/router/validate.ts
65
- var LOCATIONS = ["params", "query", "body"];
66
- function isRequestSchema(value) {
67
- return value != null && typeof value === "object" && LOCATIONS.some((loc) => loc in value);
68
- }
69
- function schemaFor(schema, method) {
70
- if (!schema) return void 0;
71
- const byMethod = schema[method];
72
- if (isRequestSchema(byMethod)) return byMethod;
73
- return isRequestSchema(schema) ? schema : void 0;
74
- }
75
- function pathToString(path3) {
76
- if (!path3) return "";
77
- return path3.map((p) => typeof p === "object" && p !== null && "key" in p ? p.key : p).map(String).join(".");
78
- }
79
- async function run(validator, input) {
80
- const std = validator["~standard"];
81
- if (std && typeof std.validate === "function") {
82
- return await std.validate(input);
83
- }
84
- const v = validator;
85
- const parse = v.safeParseAsync ?? v.safeParse;
86
- if (typeof parse !== "function") {
87
- throw new Error("[EFC] Route schema entries must be Standard Schema validators (e.g. zod)");
88
- }
89
- const r = await parse.call(validator, input);
90
- return r.success ? { value: r.data } : { issues: r.error?.issues ?? [{ message: "Invalid" }] };
91
- }
92
- function validateRequest(schema) {
93
- return (req, res, next) => {
94
- void (async () => {
95
- const issues = [];
96
- const parsed = {};
97
- for (const location of LOCATIONS) {
98
- const validator = schema[location];
99
- if (!validator) continue;
100
- const result = await run(validator, req[location]);
101
- if (result.issues) {
102
- for (const issue of result.issues) {
103
- issues.push({ location, path: pathToString(issue.path), message: issue.message });
104
- }
105
- } else {
106
- parsed[location] = result.value;
107
- }
108
- }
109
- if (issues.length > 0) {
110
- res.status(400).json({ error: "Validation failed", statusCode: 400, issues });
111
- return;
112
- }
113
- for (const location of LOCATIONS) {
114
- if (location in parsed) {
115
- Object.defineProperty(req, location, {
116
- value: parsed[location],
117
- writable: true,
118
- configurable: true,
119
- enumerable: true
120
- });
121
- }
122
- }
123
- next();
124
- })().catch(next);
125
- };
126
- }
127
-
128
- // src/router/mount.ts
129
- var HTTP_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"];
130
- function asyncWrap(handler) {
131
- return (req, res, next) => {
132
- Promise.resolve(handler(req, res, next)).catch(next);
133
- };
134
- }
135
- function namedParams(route) {
136
- const catchAlls = new Set([...route.urlPath.matchAll(/:([A-Za-z0-9_$]+)\*/g)].map((m) => m[1]));
137
- return (req, _res, next) => {
138
- const positional = req.params;
139
- const named = {};
140
- route.params.forEach((name, i) => {
141
- const value = positional[String(i)];
142
- if (value === void 0 || value === "") {
143
- named[name] = catchAlls.has(name) ? [] : void 0;
144
- } else if (catchAlls.has(name)) {
145
- named[name] = value.split("/").filter(Boolean).map(decodeURIComponent);
146
- } else {
147
- named[name] = decodeURIComponent(value);
148
- }
149
- });
150
- req.params = named;
151
- next();
152
- };
153
- }
154
- async function mountRoutes(app, routes) {
155
- const mounted = [];
156
- for (const route of routes) {
157
- const mod = await import(route.filePath);
158
- const routeMiddlewares = Array.isArray(mod["middlewares"]) ? mod["middlewares"] : [];
159
- const raw = mod["meta"];
160
- const meta = raw != null && typeof raw === "object" && !Array.isArray(raw) ? raw : void 0;
161
- const schema = mod["schema"] != null && typeof mod["schema"] === "object" ? mod["schema"] : void 0;
162
- const routePath = route.pattern ?? route.urlPath;
163
- const paramMapper = route.pattern ? [namedParams(route)] : [];
164
- const implemented = [];
165
- const unimplemented = [];
166
- for (const method of HTTP_METHODS) {
167
- if (typeof mod[method] === "function") {
168
- implemented.push(method);
169
- const methodSchema = schemaFor(schema, method);
170
- const validation = methodSchema ? [validateRequest(methodSchema)] : [];
171
- app[method.toLowerCase()](
172
- routePath,
173
- ...paramMapper,
174
- ...routeMiddlewares,
175
- ...validation,
176
- asyncWrap(mod[method])
177
- );
178
- } else {
179
- unimplemented.push(method);
180
- }
181
- }
182
- if (implemented.length > 0 && unimplemented.length > 0) {
183
- const allowHeader = implemented.join(", ");
184
- app.all(routePath, (req, res, next) => {
185
- if (unimplemented.includes(req.method)) {
186
- res.set("Allow", allowHeader).status(405).json({ error: "Method Not Allowed" });
187
- } else {
188
- next();
189
- }
190
- });
191
- }
192
- mounted.push({ ...route, methods: implemented, ...meta !== void 0 && { meta } });
193
- }
194
- return mounted;
195
- }
196
-
197
74
  // src/cluster/index.ts
198
75
  import cluster from "cluster";
199
76
  var isShuttingDown = false;
@@ -277,7 +154,7 @@ function generateDashboard(routes, basePath, port, projectName, projectVersion)
277
154
  const safeRoutes = JSON.stringify(
278
155
  routes.map((r) => ({
279
156
  urlPath: r.urlPath,
280
- methods: r.methods,
157
+ methods: r.websocket ? [...r.methods, "WS"] : r.methods,
281
158
  params: r.params,
282
159
  meta: r.meta ?? null
283
160
  }))
@@ -966,7 +843,9 @@ function compose(...handlers) {
966
843
  return;
967
844
  }
968
845
  try {
969
- Promise.resolve(handler(req, res, () => dispatch(i + 1))).catch(next);
846
+ Promise.resolve(
847
+ handler(req, res, (err) => err ? next(err) : dispatch(i + 1))
848
+ ).catch(next);
970
849
  } catch (err) {
971
850
  next(err);
972
851
  }
@@ -983,14 +862,7 @@ function detectDatabase(url) {
983
862
  return void 0;
984
863
  }
985
864
  async function ignite(config) {
986
- const {
987
- port: _port,
988
- workers,
989
- globalMiddlewares = [],
990
- onWorkerReady,
991
- onWorkerCrash,
992
- onError
993
- } = config;
865
+ const { port: _port, workers, onWorkerReady, onWorkerCrash, onError } = config;
994
866
  function resolveConventionDir(name) {
995
867
  const candidates = [
996
868
  ...process.argv[1] ? [path2.join(path2.dirname(process.argv[1]), name)] : [],
@@ -1041,40 +913,7 @@ async function ignite(config) {
1041
913
  });
1042
914
  return;
1043
915
  }
1044
- const app = express();
1045
- if (config.trustProxy !== void 0) app.set("trust proxy", config.trustProxy);
1046
- app.set(
1047
- "json replacer",
1048
- (_key, value) => typeof value === "bigint" ? value.toString() : value
1049
- );
1050
- app.use((_req, _res, next) => {
1051
- runWithContext({}, next);
1052
- });
1053
- app.use(requestLogger());
1054
- if (config.requestTimeout) {
1055
- const ms = config.requestTimeout;
1056
- app.use((_req, res, next) => {
1057
- const timer = setTimeout(() => {
1058
- if (!res.headersSent) res.status(408).json({ error: "Request Timeout", statusCode: 408 });
1059
- }, ms);
1060
- const clear = () => clearTimeout(timer);
1061
- res.once("finish", clear);
1062
- res.once("close", clear);
1063
- next();
1064
- });
1065
- }
1066
- const corsOption = config.cors ?? true;
1067
- if (corsOption !== false) {
1068
- const origin = typeof corsOption === "object" && corsOption.origin !== void 0 ? corsOption.origin : true;
1069
- const corsOpts = typeof corsOption === "object" ? { ...corsOption, origin } : { origin };
1070
- app.use(cors(corsOpts));
1071
- }
1072
- app.use(express.json());
1073
- app.use(express.urlencoded({ extended: true }));
1074
- app.use(cookieParser());
1075
- for (const mw of globalMiddlewares) {
1076
- app.use(mw);
1077
- }
916
+ const app = createHttpApp(config);
1078
917
  if (database) setDbEngine(database);
1079
918
  setAutoSync(config.autoSync ?? true);
1080
919
  const poolWorkers = clusterEnabled ? workers ?? os.cpus().length : 1;
@@ -1139,6 +978,15 @@ async function ignite(config) {
1139
978
  const routes = scanDir(apiDir);
1140
979
  const apiRouter = express.Router();
1141
980
  const mounted = await mountRoutes(apiRouter, routes);
981
+ if (config.openapi) {
982
+ const meta = readProjectMeta();
983
+ const options = config.openapi === true ? {} : config.openapi;
984
+ await mountOpenAPI(app, mounted, {
985
+ info: { title: meta.name, version: meta.version },
986
+ ...options,
987
+ basePath
988
+ });
989
+ }
1142
990
  app.use(basePath, apiRouter);
1143
991
  if (config.dashboard !== false && process.env["NODE_ENV"] === "development") {
1144
992
  app.get("/", (_req, res) => {
@@ -1147,35 +995,11 @@ async function ignite(config) {
1147
995
  res.send(generateDashboard(mounted, basePath, port, name, version));
1148
996
  });
1149
997
  }
1150
- if (onError) {
1151
- app.use(onError);
1152
- } else {
1153
- app.use(
1154
- (err, _req, res, _next) => {
1155
- if (res.headersSent) {
1156
- getLogger().warn({ err }, "Error after the response was sent");
1157
- return;
1158
- }
1159
- const status = err?.statusCode;
1160
- const duckTyped = err instanceof Error && typeof status === "number" && status >= 400 && status < 500 ? err : null;
1161
- if (isHttpError(err)) {
1162
- const issues = err.issues;
1163
- res.status(err.statusCode).json({
1164
- error: err.message,
1165
- statusCode: err.statusCode,
1166
- ...issues !== void 0 && { issues }
1167
- });
1168
- } else if (duckTyped) {
1169
- res.status(duckTyped.statusCode).json({ error: duckTyped.message, statusCode: duckTyped.statusCode });
1170
- } else {
1171
- getLogger().error({ err }, "Unhandled error");
1172
- res.status(500).json({ error: "Internal Server Error", statusCode: 500 });
1173
- }
1174
- }
1175
- );
1176
- }
998
+ mountErrorHandler(app, onError);
1177
999
  return new Promise((resolve, reject) => {
1178
- const server = app.listen(port);
1000
+ const server = http.createServer(app);
1001
+ if (config.websocket !== false && mounted.some((route) => route.websocket))
1002
+ attachWebSockets(server, app, config.websocket);
1179
1003
  server.once("listening", () => {
1180
1004
  const wid = cluster2.worker?.id ?? "primary";
1181
1005
  const addr = server.address();
@@ -1183,6 +1007,7 @@ async function ignite(config) {
1183
1007
  resolve(server);
1184
1008
  });
1185
1009
  server.once("error", reject);
1010
+ server.listen(port);
1186
1011
  });
1187
1012
  }
1188
1013
  function gracefulShutdown(server, timeoutMs = 1e4) {
@@ -1192,10 +1017,11 @@ function gracefulShutdown(server, timeoutMs = 1e4) {
1192
1017
  shuttingDown = true;
1193
1018
  getLogger().info(`${signal} received \u2014 closing server gracefully\u2026`);
1194
1019
  if (server) {
1020
+ const websocketDrain = closeWebSockets(server, Math.max(1, Math.min(1e3, timeoutMs / 2)));
1195
1021
  server.closeIdleConnections();
1196
1022
  server.close(() => {
1197
1023
  getLogger().info("Server closed");
1198
- Promise.all([closeBullMQ(), closePgBoss(), closeCache()]).catch((err) => getLogger().error({ err }, "Task backend close failed")).finally(() => process.exit(0));
1024
+ Promise.all([websocketDrain, closeBullMQ(), closePgBoss(), closeCache()]).catch((err) => getLogger().error({ err }, "Task backend close failed")).finally(() => process.exit(0));
1199
1025
  });
1200
1026
  setTimeout(() => {
1201
1027
  getLogger().error("Forced exit after timeout");
@@ -1215,21 +1041,26 @@ function gracefulShutdown(server, timeoutMs = 1e4) {
1215
1041
  export {
1216
1042
  HttpError,
1217
1043
  ModelValidationError,
1044
+ attachWebSockets,
1218
1045
  cache,
1219
1046
  cacheResponse,
1047
+ closeWebSockets,
1220
1048
  compose,
1221
1049
  db,
1222
1050
  defineCollection,
1223
1051
  defineModel,
1224
1052
  defineTable,
1053
+ generateOpenAPI,
1225
1054
  getDbClient,
1226
1055
  getLogger,
1056
+ getQueryMetrics,
1227
1057
  gracefulShutdown,
1228
1058
  ignite,
1229
1059
  isHttpError,
1230
1060
  listTasks,
1231
1061
  logger,
1232
1062
  rateLimit,
1063
+ resetQueryMetrics,
1233
1064
  scanDir,
1234
1065
  setDbClient,
1235
1066
  transaction,