@xrystal/core 3.55.59 → 3.55.63
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
|
|
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
package/x/bin/main-cli.js
CHANGED
|
@@ -98,7 +98,11 @@ registerTelepresenceCommand(cli, cliContext)
|
|
|
98
98
|
registerProjectCommands(cli, cliContext)
|
|
99
99
|
|
|
100
100
|
cli.parseAsync(process.argv).catch((error) => {
|
|
101
|
-
|
|
101
|
+
const message = error instanceof Error && error.message ? error.message : 'Command failed.'
|
|
102
|
+
if (error instanceof Error && error.name === 'InfraPreflightFailed') {
|
|
103
|
+
process.exit(1)
|
|
104
|
+
}
|
|
105
|
+
console.error(chalk.red(`\n✗ ${message}\n`))
|
|
102
106
|
process.exit(1)
|
|
103
107
|
})
|
|
104
108
|
|
package/x/dist/project/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import { getTmpSnapshot } from '../utils/helpers/tmp/tmp-loader.js';
|
|
|
7
7
|
import { isInfraLoaderEnabled, resolveLoaderBootPlan } from '../utils/helpers/tmp/loader-boot-plan.js';
|
|
8
8
|
import { InfraLoaderEnum } from '../utils/models/enums/index.js';
|
|
9
9
|
import { resolveTmpRuntimeConfigs } from '../utils/helpers/tmp/tmp-schema.js';
|
|
10
|
+
import { InfraPreflightFailed } from '../utils/helpers/infra-preflight/index.js';
|
|
10
11
|
export { ensureCoreTmpEntry, isCorePackageRoot } from '../utils/helpers/core/tmp-layout.js';
|
|
11
12
|
export { getTmpSnapshot } from '../utils/helpers/tmp/tmp-loader.js';
|
|
12
13
|
export { resolveLoaderBootPlan } from '../utils/helpers/tmp/loader-boot-plan.js';
|
|
@@ -52,6 +53,9 @@ const coreLoader = async (_params = {}) => {
|
|
|
52
53
|
return core;
|
|
53
54
|
}
|
|
54
55
|
catch (error) {
|
|
56
|
+
if (error instanceof InfraPreflightFailed) {
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
55
59
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
56
60
|
console.error(`[${Constants.packageName} core failure]: ${errorMessage}`);
|
|
57
61
|
throw error;
|
|
@@ -19,8 +19,34 @@ 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
|
+
runtimeDetect: string;
|
|
26
|
+
host: string;
|
|
27
|
+
namespace?: string;
|
|
28
|
+
coreVersion: string;
|
|
29
|
+
serviceVersion?: string;
|
|
30
|
+
probeTimeoutMs: number;
|
|
31
|
+
tcpProbe: boolean;
|
|
32
|
+
durationMs: number;
|
|
33
|
+
};
|
|
34
|
+
export declare function buildPreflightReportMeta(options: {
|
|
35
|
+
probeTimeoutMs: number;
|
|
36
|
+
tcpProbe: boolean;
|
|
37
|
+
startedAtMs: number;
|
|
38
|
+
}): PreflightReportMeta;
|
|
39
|
+
/** Thrown after report is printed — boot handler exits without extra CLI noise. */
|
|
40
|
+
export declare class InfraPreflightFailed extends Error {
|
|
41
|
+
constructor();
|
|
42
|
+
}
|
|
43
|
+
/** Strict fail: one report on stderr, then InfraPreflightFailed (no duplicate stack). */
|
|
44
|
+
export declare function abortInfraPreflight(report: string): never;
|
|
45
|
+
export declare function resolveProbeTimeoutMs(): number;
|
|
22
46
|
/** Default: fail-fast. Set STRICT_INFRA_PREFLIGHT=0|false|no|off to warn only (local edge cases). */
|
|
23
47
|
export declare function isStrictInfraPreflight(): boolean;
|
|
48
|
+
/** Default: same fast TCP probe everywhere (local, Docker, Kubernetes). Opt out: INFRA_PREFLIGHT_TCP=0. */
|
|
49
|
+
export declare function shouldTcpProbeInfraPreflight(): boolean;
|
|
24
50
|
export declare function collectInfraEndpoints(plan: LoaderBootPlan, config: IConfig): InfraEndpoint[];
|
|
25
51
|
export declare function collectInfraConfigGaps(plan: LoaderBootPlan, config: IConfig): PreflightRow[];
|
|
26
52
|
export declare function probeInfraEndpoints(endpoints: InfraEndpoint[]): Promise<InfraProbeResult[]>;
|
|
@@ -1,12 +1,80 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import net from 'node:net';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
3
5
|
import { parseRedisUri } from '../../../loader/redis/parse-uri.js';
|
|
4
6
|
import { InfraLoaderEnum } from '../../../utils/models/enums/index.js';
|
|
5
7
|
import { isInfraLoaderEnabled, resolveLoaderBootPlan, } from '../../../utils/helpers/tmp/loader-boot-plan.js';
|
|
6
8
|
import { resolveTmpRuntimeConfigs } from '../../../utils/helpers/tmp/tmp-schema.js';
|
|
7
9
|
import { getTmpSnapshot } from '../../../utils/helpers/tmp/tmp-loader.js';
|
|
8
|
-
const
|
|
10
|
+
const DEFAULT_PROBE_TIMEOUT_MS = 500;
|
|
9
11
|
const RULE = '─'.repeat(72);
|
|
12
|
+
function resolveInstalledCoreVersion() {
|
|
13
|
+
try {
|
|
14
|
+
const pkgPath = require.resolve('@xrystal/core/package.json');
|
|
15
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
|
|
16
|
+
return pkg.version?.trim() || 'unknown';
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return 'unknown';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function resolveRuntimeLabel() {
|
|
23
|
+
if (process.env.KUBERNETES_SERVICE_HOST) {
|
|
24
|
+
return {
|
|
25
|
+
label: 'kubernetes',
|
|
26
|
+
detect: 'KUBERNETES_SERVICE_HOST (kubelet-injected)',
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
if (fs.existsSync('/.dockerenv')) {
|
|
31
|
+
return { label: 'docker', detect: '/.dockerenv' };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
/* ignore */
|
|
36
|
+
}
|
|
37
|
+
return { label: 'local', detect: 'default' };
|
|
38
|
+
}
|
|
39
|
+
export function buildPreflightReportMeta(options) {
|
|
40
|
+
const ns = (process.env.KUBERNETES_NAMESPACE ?? process.env.POD_NAMESPACE ?? '').trim();
|
|
41
|
+
const runtime = resolveRuntimeLabel();
|
|
42
|
+
const svcVer = (process.env.npm_package_version ?? '').trim();
|
|
43
|
+
const svcName = (process.env.npm_package_name ?? '').trim();
|
|
44
|
+
return {
|
|
45
|
+
atUtc: new Date().toISOString(),
|
|
46
|
+
runtime: runtime.label,
|
|
47
|
+
runtimeDetect: runtime.detect,
|
|
48
|
+
host: (process.env.HOSTNAME ?? process.env.HOST ?? 'unknown').trim() || 'unknown',
|
|
49
|
+
namespace: ns || undefined,
|
|
50
|
+
coreVersion: resolveInstalledCoreVersion(),
|
|
51
|
+
serviceVersion: svcVer ? (svcName ? `${svcName} ${svcVer}` : svcVer) : undefined,
|
|
52
|
+
probeTimeoutMs: options.probeTimeoutMs,
|
|
53
|
+
tcpProbe: options.tcpProbe,
|
|
54
|
+
durationMs: Math.max(0, Date.now() - options.startedAtMs),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Thrown after report is printed — boot handler exits without extra CLI noise. */
|
|
58
|
+
export class InfraPreflightFailed extends Error {
|
|
59
|
+
constructor() {
|
|
60
|
+
super('Infrastructure preflight failed');
|
|
61
|
+
this.name = 'InfraPreflightFailed';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Strict fail: one report on stderr, then InfraPreflightFailed (no duplicate stack). */
|
|
65
|
+
export function abortInfraPreflight(report) {
|
|
66
|
+
console.error(report);
|
|
67
|
+
throw new InfraPreflightFailed();
|
|
68
|
+
}
|
|
69
|
+
export function resolveProbeTimeoutMs() {
|
|
70
|
+
const raw = (process.env.INFRA_PREFLIGHT_TIMEOUT_MS ?? '').trim();
|
|
71
|
+
if (!raw)
|
|
72
|
+
return DEFAULT_PROBE_TIMEOUT_MS;
|
|
73
|
+
const n = Number.parseInt(raw, 10);
|
|
74
|
+
if (!Number.isFinite(n) || n < 100)
|
|
75
|
+
return DEFAULT_PROBE_TIMEOUT_MS;
|
|
76
|
+
return Math.min(n, 5000);
|
|
77
|
+
}
|
|
10
78
|
/** Default: fail-fast. Set STRICT_INFRA_PREFLIGHT=0|false|no|off to warn only (local edge cases). */
|
|
11
79
|
export function isStrictInfraPreflight() {
|
|
12
80
|
const raw = (process.env.STRICT_INFRA_PREFLIGHT ?? '').trim().toLowerCase();
|
|
@@ -16,9 +84,19 @@ export function isStrictInfraPreflight() {
|
|
|
16
84
|
return false;
|
|
17
85
|
return true;
|
|
18
86
|
}
|
|
87
|
+
/** Default: same fast TCP probe everywhere (local, Docker, Kubernetes). Opt out: INFRA_PREFLIGHT_TCP=0. */
|
|
88
|
+
export function shouldTcpProbeInfraPreflight() {
|
|
89
|
+
const raw = (process.env.INFRA_PREFLIGHT_TCP ?? '').trim().toLowerCase();
|
|
90
|
+
if (raw === '0' || raw === 'false' || raw === 'no' || raw === 'off')
|
|
91
|
+
return false;
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
19
94
|
function runtimeContextHint() {
|
|
20
95
|
if (process.env.KUBERNETES_SERVICE_HOST) {
|
|
21
|
-
|
|
96
|
+
if (!shouldTcpProbeInfraPreflight()) {
|
|
97
|
+
return 'Kubernetes: TCP preflight disabled (INFRA_PREFLIGHT_TCP=0). Config-only check.';
|
|
98
|
+
}
|
|
99
|
+
return 'Kubernetes: pod must reach bare-metal hosts in env (often Tailscale IP). Fix UFW/routes if UNREACHABLE.';
|
|
22
100
|
}
|
|
23
101
|
try {
|
|
24
102
|
if (fs.existsSync('/.dockerenv')) {
|
|
@@ -82,23 +160,24 @@ function parseKafkaEndpoints(brokers) {
|
|
|
82
160
|
};
|
|
83
161
|
});
|
|
84
162
|
}
|
|
85
|
-
function probeTcp(host, port, timeoutMs
|
|
163
|
+
function probeTcp(host, port, timeoutMs) {
|
|
164
|
+
const ms = timeoutMs ?? resolveProbeTimeoutMs();
|
|
86
165
|
if (host === '(srv)' || host === '_srv_') {
|
|
87
166
|
return Promise.resolve({ ok: true });
|
|
88
167
|
}
|
|
89
168
|
return new Promise((resolve) => {
|
|
90
|
-
const socket = net.connect({ host, port });
|
|
91
169
|
let settled = false;
|
|
92
170
|
const finish = (ok, error) => {
|
|
93
171
|
if (settled)
|
|
94
172
|
return;
|
|
95
173
|
settled = true;
|
|
174
|
+
clearTimeout(hardTimer);
|
|
96
175
|
socket.destroy();
|
|
97
176
|
resolve({ ok, error });
|
|
98
177
|
};
|
|
99
|
-
socket.
|
|
178
|
+
const socket = net.connect({ host, port });
|
|
179
|
+
const hardTimer = setTimeout(() => finish(false, 'connection timed out'), ms);
|
|
100
180
|
socket.on('connect', () => finish(true));
|
|
101
|
-
socket.on('timeout', () => finish(false, 'connection timed out'));
|
|
102
181
|
socket.on('error', (err) => finish(false, err.code ?? err.message));
|
|
103
182
|
});
|
|
104
183
|
}
|
|
@@ -181,17 +260,25 @@ export function collectInfraConfigGaps(plan, config) {
|
|
|
181
260
|
return rows;
|
|
182
261
|
}
|
|
183
262
|
export async function probeInfraEndpoints(endpoints) {
|
|
184
|
-
const
|
|
263
|
+
const timeoutMs = resolveProbeTimeoutMs();
|
|
264
|
+
const sockets = new Map();
|
|
185
265
|
for (const ep of endpoints) {
|
|
186
266
|
if (ep.host.startsWith('('))
|
|
187
267
|
continue;
|
|
188
|
-
|
|
268
|
+
sockets.set(`${ep.host}:${ep.port}`, { host: ep.host, port: ep.port });
|
|
189
269
|
}
|
|
190
|
-
const
|
|
191
|
-
await Promise.all([...
|
|
192
|
-
|
|
193
|
-
results.push({ endpoint, ok, error });
|
|
270
|
+
const socketStatus = new Map();
|
|
271
|
+
await Promise.all([...sockets.entries()].map(async ([key, { host, port }]) => {
|
|
272
|
+
socketStatus.set(key, await probeTcp(host, port, timeoutMs));
|
|
194
273
|
}));
|
|
274
|
+
const results = [];
|
|
275
|
+
for (const endpoint of endpoints) {
|
|
276
|
+
if (endpoint.host.startsWith('('))
|
|
277
|
+
continue;
|
|
278
|
+
const key = `${endpoint.host}:${endpoint.port}`;
|
|
279
|
+
const status = socketStatus.get(key) ?? { ok: false, error: 'not probed' };
|
|
280
|
+
results.push({ endpoint, ok: status.ok, error: status.error });
|
|
281
|
+
}
|
|
195
282
|
return results;
|
|
196
283
|
}
|
|
197
284
|
function pad(value, width) {
|
|
@@ -199,10 +286,26 @@ function pad(value, width) {
|
|
|
199
286
|
return value.slice(0, width);
|
|
200
287
|
return value + ' '.repeat(width - value.length);
|
|
201
288
|
}
|
|
202
|
-
function
|
|
289
|
+
function formatReportMeta(meta) {
|
|
290
|
+
const lines = [
|
|
291
|
+
`At ${meta.atUtc}`,
|
|
292
|
+
`Runtime ${meta.runtime} (${meta.runtimeDetect})`,
|
|
293
|
+
`Host ${meta.host}`,
|
|
294
|
+
];
|
|
295
|
+
if (meta.namespace)
|
|
296
|
+
lines.push(`Namespace ${meta.namespace}`);
|
|
297
|
+
lines.push(`Core @xrystal/core ${meta.coreVersion}`);
|
|
298
|
+
if (meta.serviceVersion)
|
|
299
|
+
lines.push(`Service ${meta.serviceVersion}`);
|
|
300
|
+
lines.push(`Probe timeout ${meta.probeTimeoutMs}ms · TCP ${meta.tcpProbe ? 'on' : 'off'} · elapsed ${meta.durationMs}ms`);
|
|
301
|
+
return lines;
|
|
302
|
+
}
|
|
303
|
+
function formatReport(outcome, rows, context, meta, action) {
|
|
203
304
|
const lines = [
|
|
204
305
|
'Infrastructure preflight',
|
|
205
306
|
RULE,
|
|
307
|
+
...formatReportMeta(meta),
|
|
308
|
+
RULE,
|
|
206
309
|
outcome,
|
|
207
310
|
RULE,
|
|
208
311
|
`${pad('Service', 14)} ${pad('Env', 22)} ${pad('Target', 22)} Result`,
|
|
@@ -224,10 +327,14 @@ function buildFailureAction(strict) {
|
|
|
224
327
|
return 'Boot continues (lenient mode). Fix connectivity before relying on infra loaders.';
|
|
225
328
|
}
|
|
226
329
|
export async function runInfraPreflight(config, plan) {
|
|
330
|
+
const startedAtMs = Date.now();
|
|
227
331
|
const strict = isStrictInfraPreflight();
|
|
332
|
+
const probeTimeoutMs = resolveProbeTimeoutMs();
|
|
333
|
+
const tcpProbe = shouldTcpProbeInfraPreflight();
|
|
228
334
|
const bootPlan = plan ?? resolveLoaderBootPlan(resolveTmpRuntimeConfigs(getTmpSnapshot()));
|
|
229
335
|
const rows = [...collectInfraConfigGaps(bootPlan, config)];
|
|
230
336
|
const endpoints = collectInfraEndpoints(bootPlan, config);
|
|
337
|
+
const meta = () => buildPreflightReportMeta({ probeTimeoutMs, tcpProbe, startedAtMs });
|
|
231
338
|
for (const ep of endpoints.filter((e) => e.host.startsWith('('))) {
|
|
232
339
|
rows.push({
|
|
233
340
|
service: ep.service,
|
|
@@ -237,6 +344,19 @@ export async function runInfraPreflight(config, plan) {
|
|
|
237
344
|
detail: 'could not parse host',
|
|
238
345
|
});
|
|
239
346
|
}
|
|
347
|
+
const configFailed = rows.filter((r) => r.result !== 'OK');
|
|
348
|
+
if (!shouldTcpProbeInfraPreflight()) {
|
|
349
|
+
if (configFailed.length === 0) {
|
|
350
|
+
console.log('Infrastructure preflight: config OK (TCP disabled via INFRA_PREFLIGHT_TCP=0).');
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const outcome = `Outcome FAILED | ${configFailed.length} config issue(s), TCP disabled`;
|
|
354
|
+
const report = formatReport(outcome, rows, runtimeContextHint(), meta(), buildFailureAction(strict));
|
|
355
|
+
if (strict)
|
|
356
|
+
abortInfraPreflight(report);
|
|
357
|
+
console.error(report);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
240
360
|
const probeTargets = endpoints.filter((ep) => !ep.host.startsWith('('));
|
|
241
361
|
const results = probeTargets.length ? await probeInfraEndpoints(probeTargets) : [];
|
|
242
362
|
for (const { endpoint, ok, error } of results) {
|
|
@@ -254,13 +374,13 @@ export async function runInfraPreflight(config, plan) {
|
|
|
254
374
|
const passed = rows.filter((r) => r.result === 'OK');
|
|
255
375
|
if (failed.length === 0) {
|
|
256
376
|
const names = [...new Set(passed.map((r) => r.service))].join(', ');
|
|
257
|
-
|
|
377
|
+
const m = meta();
|
|
378
|
+
console.log(`Infrastructure preflight: passed (${passed.length} checks: ${names}, ${m.durationMs}ms)`);
|
|
258
379
|
return;
|
|
259
380
|
}
|
|
260
381
|
const outcome = `Outcome FAILED | ${failed.length} issue(s), ${passed.length} passed`;
|
|
261
|
-
const report = formatReport(outcome, rows, runtimeContextHint(), buildFailureAction(strict));
|
|
382
|
+
const report = formatReport(outcome, rows, runtimeContextHint(), meta(), buildFailureAction(strict));
|
|
383
|
+
if (strict)
|
|
384
|
+
abortInfraPreflight(report);
|
|
262
385
|
console.error(report);
|
|
263
|
-
if (strict) {
|
|
264
|
-
throw new Error(`Infrastructure preflight failed.\n${report}`);
|
|
265
|
-
}
|
|
266
386
|
}
|