@xrystal/core 3.55.59 → 3.55.62

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 CHANGED
@@ -263,7 +263,7 @@ bun run dev
263
263
 
264
264
  **TR:** `x/environments/*` yalnızca lokal dev + Telepresence. Image'a gömme. Cluster'da Secret inject; runtime dosya okumaz.
265
265
 
266
- **Infra preflight:** Before Kafka/Redis/Mongo connect, TCP checks from tmp.yml + env. **Default fail-fast** (structured report, boot aborts on CONFIG/UNREACHABLE). Opt out locally: `STRICT_INFRA_PREFLIGHT=0`.
266
+ **Infra preflight:** Before Kafka/Redis/Mongo connect, parallel TCP (~500ms) from tmp.yml + env. **Default fail-fast** (structured report with UTC time, runtime, probe stats; exit 1 without stack). Lenient: `STRICT_INFRA_PREFLIGHT=0`. Disable TCP: `INFRA_PREFLIGHT_TCP=0`. Timeout: `INFRA_PREFLIGHT_TIMEOUT_MS`.
267
267
 
268
268
  **Docker / Compose:** Image `CMD ["bun", "xry", "start"]` (dev: `xry dev`). Runtime skips `x/environments` when `/.dockerenv` or k8s is detected (optional override: `XRY_RUNTIME=container|docker|k8s`). Compose: `env_file:` on `x/docker/docker-compose*.yml` — not `COPY` env into the image.
269
269
  | `xry test` | Run `x/tests` | `x/tests` çalıştır |
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.55.59",
4
+ "version": "3.55.62",
5
5
  "description": "Runtime framework and CLI for API services, frontends, and multi-repo workspaces",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -19,8 +19,28 @@ type PreflightRow = {
19
19
  result: 'OK' | 'UNREACHABLE' | 'CONFIG' | 'INVALID';
20
20
  detail?: string;
21
21
  };
22
+ export type PreflightReportMeta = {
23
+ atUtc: string;
24
+ runtime: string;
25
+ host: string;
26
+ namespace?: string;
27
+ coreVersion: string;
28
+ probeTimeoutMs: number;
29
+ tcpProbe: boolean;
30
+ durationMs: number;
31
+ };
32
+ export declare function buildPreflightReportMeta(options: {
33
+ probeTimeoutMs: number;
34
+ tcpProbe: boolean;
35
+ startedAtMs: number;
36
+ }): PreflightReportMeta;
37
+ /** Strict fail: one report on stderr, exit 1 — no Error stack (avoids duplicate lines + Bun SIGTERM noise in k8s). */
38
+ export declare function abortInfraPreflight(report: string): never;
39
+ export declare function resolveProbeTimeoutMs(): number;
22
40
  /** Default: fail-fast. Set STRICT_INFRA_PREFLIGHT=0|false|no|off to warn only (local edge cases). */
23
41
  export declare function isStrictInfraPreflight(): boolean;
42
+ /** Default: same fast TCP probe everywhere (local, Docker, Kubernetes). Opt out: INFRA_PREFLIGHT_TCP=0. */
43
+ export declare function shouldTcpProbeInfraPreflight(): boolean;
24
44
  export declare function collectInfraEndpoints(plan: LoaderBootPlan, config: IConfig): InfraEndpoint[];
25
45
  export declare function collectInfraConfigGaps(plan: LoaderBootPlan, config: IConfig): PreflightRow[];
26
46
  export declare function probeInfraEndpoints(endpoints: InfraEndpoint[]): Promise<InfraProbeResult[]>;
@@ -5,8 +5,47 @@ import { InfraLoaderEnum } from '../../../utils/models/enums/index.js';
5
5
  import { isInfraLoaderEnabled, resolveLoaderBootPlan, } from '../../../utils/helpers/tmp/loader-boot-plan.js';
6
6
  import { resolveTmpRuntimeConfigs } from '../../../utils/helpers/tmp/tmp-schema.js';
7
7
  import { getTmpSnapshot } from '../../../utils/helpers/tmp/tmp-loader.js';
8
- const PROBE_TIMEOUT_MS = 2500;
8
+ const DEFAULT_PROBE_TIMEOUT_MS = 500;
9
9
  const RULE = '─'.repeat(72);
10
+ function resolveRuntimeLabel() {
11
+ if (process.env.KUBERNETES_SERVICE_HOST)
12
+ return 'kubernetes';
13
+ try {
14
+ if (fs.existsSync('/.dockerenv'))
15
+ return 'docker';
16
+ }
17
+ catch {
18
+ /* ignore */
19
+ }
20
+ return 'local';
21
+ }
22
+ export function buildPreflightReportMeta(options) {
23
+ const ns = (process.env.KUBERNETES_NAMESPACE ?? process.env.POD_NAMESPACE ?? '').trim();
24
+ return {
25
+ atUtc: new Date().toISOString(),
26
+ runtime: resolveRuntimeLabel(),
27
+ host: (process.env.HOSTNAME ?? process.env.HOST ?? 'unknown').trim() || 'unknown',
28
+ namespace: ns || undefined,
29
+ coreVersion: (process.env.npm_package_version ?? 'unknown').trim() || 'unknown',
30
+ probeTimeoutMs: options.probeTimeoutMs,
31
+ tcpProbe: options.tcpProbe,
32
+ durationMs: Math.max(0, Date.now() - options.startedAtMs),
33
+ };
34
+ }
35
+ /** Strict fail: one report on stderr, exit 1 — no Error stack (avoids duplicate lines + Bun SIGTERM noise in k8s). */
36
+ export function abortInfraPreflight(report) {
37
+ console.error(report);
38
+ process.exit(1);
39
+ }
40
+ export function resolveProbeTimeoutMs() {
41
+ const raw = (process.env.INFRA_PREFLIGHT_TIMEOUT_MS ?? '').trim();
42
+ if (!raw)
43
+ return DEFAULT_PROBE_TIMEOUT_MS;
44
+ const n = Number.parseInt(raw, 10);
45
+ if (!Number.isFinite(n) || n < 100)
46
+ return DEFAULT_PROBE_TIMEOUT_MS;
47
+ return Math.min(n, 5000);
48
+ }
10
49
  /** Default: fail-fast. Set STRICT_INFRA_PREFLIGHT=0|false|no|off to warn only (local edge cases). */
11
50
  export function isStrictInfraPreflight() {
12
51
  const raw = (process.env.STRICT_INFRA_PREFLIGHT ?? '').trim().toLowerCase();
@@ -16,9 +55,19 @@ export function isStrictInfraPreflight() {
16
55
  return false;
17
56
  return true;
18
57
  }
58
+ /** Default: same fast TCP probe everywhere (local, Docker, Kubernetes). Opt out: INFRA_PREFLIGHT_TCP=0. */
59
+ export function shouldTcpProbeInfraPreflight() {
60
+ const raw = (process.env.INFRA_PREFLIGHT_TCP ?? '').trim().toLowerCase();
61
+ if (raw === '0' || raw === 'false' || raw === 'no' || raw === 'off')
62
+ return false;
63
+ return true;
64
+ }
19
65
  function runtimeContextHint() {
20
66
  if (process.env.KUBERNETES_SERVICE_HOST) {
21
- return 'Kubernetes: verify Service DNS, namespace, and Secret envFrom keys.';
67
+ if (!shouldTcpProbeInfraPreflight()) {
68
+ return 'Kubernetes: TCP preflight disabled (INFRA_PREFLIGHT_TCP=0). Config-only check.';
69
+ }
70
+ return 'Kubernetes: pod must reach bare-metal hosts in env (often Tailscale IP). Fix UFW/routes if UNREACHABLE.';
22
71
  }
23
72
  try {
24
73
  if (fs.existsSync('/.dockerenv')) {
@@ -82,7 +131,8 @@ function parseKafkaEndpoints(brokers) {
82
131
  };
83
132
  });
84
133
  }
85
- function probeTcp(host, port, timeoutMs = PROBE_TIMEOUT_MS) {
134
+ function probeTcp(host, port, timeoutMs) {
135
+ const ms = timeoutMs ?? resolveProbeTimeoutMs();
86
136
  if (host === '(srv)' || host === '_srv_') {
87
137
  return Promise.resolve({ ok: true });
88
138
  }
@@ -96,7 +146,7 @@ function probeTcp(host, port, timeoutMs = PROBE_TIMEOUT_MS) {
96
146
  socket.destroy();
97
147
  resolve({ ok, error });
98
148
  };
99
- socket.setTimeout(timeoutMs);
149
+ socket.setTimeout(ms);
100
150
  socket.on('connect', () => finish(true));
101
151
  socket.on('timeout', () => finish(false, 'connection timed out'));
102
152
  socket.on('error', (err) => finish(false, err.code ?? err.message));
@@ -181,17 +231,25 @@ export function collectInfraConfigGaps(plan, config) {
181
231
  return rows;
182
232
  }
183
233
  export async function probeInfraEndpoints(endpoints) {
184
- const unique = new Map();
234
+ const timeoutMs = resolveProbeTimeoutMs();
235
+ const sockets = new Map();
185
236
  for (const ep of endpoints) {
186
237
  if (ep.host.startsWith('('))
187
238
  continue;
188
- unique.set(`${ep.service}|${ep.host}|${ep.port}`, ep);
239
+ sockets.set(`${ep.host}:${ep.port}`, { host: ep.host, port: ep.port });
189
240
  }
190
- const results = [];
191
- await Promise.all([...unique.values()].map(async (endpoint) => {
192
- const { ok, error } = await probeTcp(endpoint.host, endpoint.port);
193
- results.push({ endpoint, ok, error });
241
+ const socketStatus = new Map();
242
+ await Promise.all([...sockets.entries()].map(async ([key, { host, port }]) => {
243
+ socketStatus.set(key, await probeTcp(host, port, timeoutMs));
194
244
  }));
245
+ const results = [];
246
+ for (const endpoint of endpoints) {
247
+ if (endpoint.host.startsWith('('))
248
+ continue;
249
+ const key = `${endpoint.host}:${endpoint.port}`;
250
+ const status = socketStatus.get(key) ?? { ok: false, error: 'not probed' };
251
+ results.push({ endpoint, ok: status.ok, error: status.error });
252
+ }
195
253
  return results;
196
254
  }
197
255
  function pad(value, width) {
@@ -199,10 +257,23 @@ function pad(value, width) {
199
257
  return value.slice(0, width);
200
258
  return value + ' '.repeat(width - value.length);
201
259
  }
202
- function formatReport(outcome, rows, context, action) {
260
+ function formatReportMeta(meta) {
261
+ const lines = [
262
+ `At ${meta.atUtc}`,
263
+ `Runtime ${meta.runtime}`,
264
+ `Host ${meta.host}`,
265
+ ];
266
+ if (meta.namespace)
267
+ lines.push(`Namespace ${meta.namespace}`);
268
+ lines.push(`Core @xrystal/core ${meta.coreVersion}`, `Probe timeout ${meta.probeTimeoutMs}ms · TCP ${meta.tcpProbe ? 'on' : 'off'} · elapsed ${meta.durationMs}ms`);
269
+ return lines;
270
+ }
271
+ function formatReport(outcome, rows, context, meta, action) {
203
272
  const lines = [
204
273
  'Infrastructure preflight',
205
274
  RULE,
275
+ ...formatReportMeta(meta),
276
+ RULE,
206
277
  outcome,
207
278
  RULE,
208
279
  `${pad('Service', 14)} ${pad('Env', 22)} ${pad('Target', 22)} Result`,
@@ -224,10 +295,14 @@ function buildFailureAction(strict) {
224
295
  return 'Boot continues (lenient mode). Fix connectivity before relying on infra loaders.';
225
296
  }
226
297
  export async function runInfraPreflight(config, plan) {
298
+ const startedAtMs = Date.now();
227
299
  const strict = isStrictInfraPreflight();
300
+ const probeTimeoutMs = resolveProbeTimeoutMs();
301
+ const tcpProbe = shouldTcpProbeInfraPreflight();
228
302
  const bootPlan = plan ?? resolveLoaderBootPlan(resolveTmpRuntimeConfigs(getTmpSnapshot()));
229
303
  const rows = [...collectInfraConfigGaps(bootPlan, config)];
230
304
  const endpoints = collectInfraEndpoints(bootPlan, config);
305
+ const meta = () => buildPreflightReportMeta({ probeTimeoutMs, tcpProbe, startedAtMs });
231
306
  for (const ep of endpoints.filter((e) => e.host.startsWith('('))) {
232
307
  rows.push({
233
308
  service: ep.service,
@@ -237,6 +312,19 @@ export async function runInfraPreflight(config, plan) {
237
312
  detail: 'could not parse host',
238
313
  });
239
314
  }
315
+ const configFailed = rows.filter((r) => r.result !== 'OK');
316
+ if (!shouldTcpProbeInfraPreflight()) {
317
+ if (configFailed.length === 0) {
318
+ console.log('Infrastructure preflight: config OK (TCP disabled via INFRA_PREFLIGHT_TCP=0).');
319
+ return;
320
+ }
321
+ const outcome = `Outcome FAILED | ${configFailed.length} config issue(s), TCP disabled`;
322
+ const report = formatReport(outcome, rows, runtimeContextHint(), meta(), buildFailureAction(strict));
323
+ if (strict)
324
+ abortInfraPreflight(report);
325
+ console.error(report);
326
+ return;
327
+ }
240
328
  const probeTargets = endpoints.filter((ep) => !ep.host.startsWith('('));
241
329
  const results = probeTargets.length ? await probeInfraEndpoints(probeTargets) : [];
242
330
  for (const { endpoint, ok, error } of results) {
@@ -254,13 +342,13 @@ export async function runInfraPreflight(config, plan) {
254
342
  const passed = rows.filter((r) => r.result === 'OK');
255
343
  if (failed.length === 0) {
256
344
  const names = [...new Set(passed.map((r) => r.service))].join(', ');
257
- console.log(`Infrastructure preflight: passed (${passed.length} checks: ${names})`);
345
+ const m = meta();
346
+ console.log(`Infrastructure preflight: passed (${passed.length} checks: ${names}, ${m.durationMs}ms)`);
258
347
  return;
259
348
  }
260
349
  const outcome = `Outcome FAILED | ${failed.length} issue(s), ${passed.length} passed`;
261
- const report = formatReport(outcome, rows, runtimeContextHint(), buildFailureAction(strict));
350
+ const report = formatReport(outcome, rows, runtimeContextHint(), meta(), buildFailureAction(strict));
351
+ if (strict)
352
+ abortInfraPreflight(report);
262
353
  console.error(report);
263
- if (strict) {
264
- throw new Error(`Infrastructure preflight failed.\n${report}`);
265
- }
266
354
  }