@xrystal/core 3.55.12 → 3.55.16

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
@@ -9,9 +9,9 @@
9
9
 
10
10
  ## Platform services / Platform servisleri
11
11
 
12
- **EN:** Consumer microservices in `../services/` — pin `@xrystal/core` to the latest published minor (currently **3.52.5**).
12
+ **EN:** Consumer microservices in `../services/` — table below is the **pin matrix** (service pkg + `@xrystal/core`; bump rows when you ship). Apply pins with **`xry sync`** (not manual `bun add` per repo).
13
13
 
14
- **TR:** `../services/` altındaki tüketen microservice'ler — `@xrystal/core` sürümünü npm latest ile hizala (şu an **3.52.5**).
14
+ **TR:** `../services/` altındaki microservice'ler — tablo **pin matrisi** (servis sürümü + `@xrystal/core`; release sonrası satırları güncelle). Pin uygulama: **`xry sync`** (her repo için elle `bun add` yazma).
15
15
 
16
16
  | Repo folder | `tmp.yml` name | Role | Service pkg | `@xrystal/core` |
17
17
  |-------------|----------------|------|-------------|-----------------|
@@ -31,13 +31,7 @@ import { resolveRedisSocketOptions } from '@xrystal/core/loader/redis'
31
31
  import { resolveMongoConnectOptions } from '@xrystal/core/loader/mongo'
32
32
  ```
33
33
 
34
- **Update all services / tüm servisleri güncelle:**
35
-
36
- ```bash
37
- cd ../services/api-<name>-svc
38
- bun add @xrystal/core@3.52.5
39
- bun run build
40
- ```
34
+ **After `@xrystal/core` publish / core yayın sonrası:** workspace root → `xry sync` (all linked `api/*` pins to CLI version). One service → `cd` that repo → `xry sync`. Build/tag images = each service’s own release flow (see monorepo release order in infra).
41
35
 
42
36
  ---
43
37
 
@@ -876,4 +870,7 @@ class ChatController extends Controller {
876
870
 
877
871
  **EN:** This repo is the `@xrystal/core` source. Bump `package.json` → commit → `xry tag` (runs prepare + pushes `vX.Y.Z`; GitHub Publish workflow releases to npm). Consumers only need `bun add @xrystal/core`.
878
872
 
873
+
874
+ **npm CI (`E401` / invalid token):** GitHub → repo **Settings → Environments → NPM** → secret **`TOKEN`** = [npm Granular Access Token](https://www.npmjs.com/settings/~youruser/tokens) with **Publish** on package `@xrystal/core` (or whole `@xrystal` scope). Classic automation tokens that bypass 2FA are being restricted — prefer **Granular** + **Publish**. After rotate: re-run failed **Publish Core Library NPM** workflow or push empty commit / re-tag. Stage flow: workflow runs `npm stage publish` → approve on npmjs.com (`npm stage list` / Approve with 2FA).
875
+
879
876
  **TR:** Bu repo `@xrystal/core` kaynağıdır. `package.json` bump → commit → `xry tag` (prepare + `vX.Y.Z` push; GitHub Publish → npm). Tüketici için sadece `bun add @xrystal/core` yeter.
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.12",
4
+ "version": "3.55.16",
5
5
  "description": "Runtime framework and CLI for API services, frontends, and multi-repo workspaces",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -123,4 +123,4 @@
123
123
  "typescript": "~5.9.3",
124
124
  "typescript-eslint": "^8.62.1"
125
125
  }
126
- }
126
+ }
@@ -48,11 +48,30 @@ export function listInfraRootCandidates(start = process.cwd()) {
48
48
  return out
49
49
  }
50
50
 
51
- /** Resolve a script under -infrastructure-/scripts/helpers. */
51
+ function findHelperScriptUnderRoot(root, name) {
52
+ const base = path.join(root, HELPERS_DIR)
53
+ const direct = path.join(base, name)
54
+ if (fs.existsSync(direct)) return direct
55
+
56
+ let entries
57
+ try {
58
+ entries = fs.readdirSync(base, { withFileTypes: true })
59
+ } catch {
60
+ return null
61
+ }
62
+ for (const ent of entries) {
63
+ if (!ent.isDirectory()) continue
64
+ const nested = path.join(base, ent.name, name)
65
+ if (fs.existsSync(nested)) return nested
66
+ }
67
+ return null
68
+ }
69
+
70
+ /** Resolve a script under -infrastructure-/scripts/helpers (incl. domain subdirs). */
52
71
  export function resolveInfraHelperScript(name, { cwd = process.cwd() } = {}) {
53
72
  for (const root of listInfraRootCandidates(cwd)) {
54
- const script = path.join(root, HELPERS_DIR, name)
55
- if (fs.existsSync(script)) return script
73
+ const script = findHelperScriptUnderRoot(root, name)
74
+ if (script) return script
56
75
  }
57
76
  return null
58
77
  }
@@ -27,6 +27,9 @@ export interface IConfig {
27
27
  kafkaLogsTopic: string;
28
28
  isRedisPassive: boolean;
29
29
  redisUri: string;
30
+ /** false = force standalone URL; default Sentinel when URI has comma hosts or :26379 */
31
+ isRedisSentinel: boolean;
32
+ redisSentinelMaster: string;
30
33
  isMongoPassive: boolean;
31
34
  mongodbUri: string;
32
35
  isRegistryEnabled: boolean;
@@ -67,6 +67,8 @@ export default class Configs {
67
67
  kafkaLogsTopic: this.kafkaLogsTopic,
68
68
  isRedisPassive: process.env.IS_REDIS_PASSIVE === 'true' ? true : false,
69
69
  redisUri: process.env.REDIS_URI ?? '',
70
+ isRedisSentinel: process.env.IS_REDIS_SENTINEL === 'false' ? false : true,
71
+ redisSentinelMaster: process.env.REDIS_SENTINEL_MASTER ?? 'mymaster',
70
72
  isMongoPassive: process.env.IS_MONGODB_PASSIVE === 'true' ? true : false,
71
73
  mongodbUri: process.env.MONGODB_URI ?? '',
72
74
  isRegistryEnabled: process.env.IS_REGISTRY_ENABLED === 'true',
@@ -0,0 +1,7 @@
1
+ import { type RedisClientType } from 'redis';
2
+ export type ConnectRedisOptions = {
3
+ uri: string;
4
+ sentinelMaster?: string;
5
+ isRedisSentinel?: boolean;
6
+ };
7
+ export declare function connectRedisClient(options: ConnectRedisOptions): Promise<RedisClientType>;
@@ -0,0 +1,57 @@
1
+ import { createClient, createSentinel } from 'redis';
2
+ import { parseRedisUri, standaloneRedisUrl } from './parse-uri.js';
3
+ import { resolveRedisSocketOptions } from './resolve-socket.js';
4
+ function parseSentinelRootNodes(csv) {
5
+ return csv
6
+ .split(',')
7
+ .map((part) => part.trim())
8
+ .filter(Boolean)
9
+ .map((part) => {
10
+ const colon = part.lastIndexOf(':');
11
+ if (colon === -1) {
12
+ return { host: part, port: 26379 };
13
+ }
14
+ const host = part.slice(0, colon);
15
+ const port = Number.parseInt(part.slice(colon + 1), 10);
16
+ return { host, port: Number.isFinite(port) ? port : 26379 };
17
+ })
18
+ .filter((n) => n.host.length > 0);
19
+ }
20
+ export async function connectRedisClient(options) {
21
+ const uri = options.uri?.trim();
22
+ if (!uri) {
23
+ throw new Error('Redis URI is required');
24
+ }
25
+ const parsed = parseRedisUri(uri);
26
+ const socketOpts = resolveRedisSocketOptions(uri);
27
+ const sentinelMaster = options.sentinelMaster?.trim() ||
28
+ process.env.REDIS_SENTINEL_MASTER?.trim() ||
29
+ 'mymaster';
30
+ const envSentinelHosts = process.env.REDIS_SENTINEL_HOSTS?.trim();
31
+ const preferSentinel = options.isRedisSentinel !== false &&
32
+ (envSentinelHosts ? true : parsed.useSentinel);
33
+ if (preferSentinel) {
34
+ const rootNodes = envSentinelHosts
35
+ ? parseSentinelRootNodes(envSentinelHosts)
36
+ : parsed.endpoints.map((e) => ({ host: e.host, port: e.port }));
37
+ if (rootNodes.length === 0) {
38
+ throw new Error('Redis Sentinel mode requires at least one sentinel endpoint');
39
+ }
40
+ return (await createSentinel({
41
+ name: sentinelMaster,
42
+ sentinelRootNodes: rootNodes,
43
+ nodeClientOptions: {
44
+ ...parsed.creds,
45
+ socket: socketOpts,
46
+ },
47
+ sentinelClientOptions: {
48
+ socket: socketOpts,
49
+ },
50
+ }).connect());
51
+ }
52
+ const standaloneUrl = standaloneRedisUrl(uri, parsed.endpoints);
53
+ return (await createClient({
54
+ url: standaloneUrl,
55
+ socket: socketOpts,
56
+ }).connect());
57
+ }
@@ -1,5 +1,7 @@
1
1
  export { resolveRedisSocketOptions } from './resolve-socket';
2
- import { type RedisClientType } from 'redis';
2
+ export { parseRedisUri, standaloneRedisUrl } from './parse-uri';
3
+ export { connectRedisClient, type ConnectRedisOptions } from './connect';
4
+ import type { RedisClientType } from 'redis';
3
5
  import Configs from '../../loader/configs';
4
6
  import System from '../../loader/system';
5
7
  import { IProvide } from '../../utils/models/classes/class.interfaces';
@@ -1,20 +1,10 @@
1
1
  export { resolveRedisSocketOptions } from './resolve-socket.js';
2
- import { createClient, createSentinel } from 'redis';
2
+ export { parseRedisUri, standaloneRedisUrl } from './parse-uri.js';
3
+ export { connectRedisClient } from './connect.js';
3
4
  import { bindLoaderTmpSync } from '../../utils/helpers/tmp/loader-tmp-sync.js';
4
5
  import { readInfraLoaderEnabled } from '../../utils/helpers/loader-options/index.js';
5
6
  import { InfraLoaderEnum } from '../../utils/models/enums/index.js';
6
- import { resolveRedisSocketOptions } from './resolve-socket.js';
7
- function parseSentinelRootNodes(csv) {
8
- return csv
9
- .split(',')
10
- .map((part) => part.trim())
11
- .filter(Boolean)
12
- .map((part) => {
13
- const [host, port] = part.split(':');
14
- return { host: host.trim(), port: Number.parseInt(port || '26379', 10) };
15
- })
16
- .filter((n) => n.host.length > 0);
17
- }
7
+ import { connectRedisClient } from './connect.js';
18
8
  export default class RedisForCore {
19
9
  #client = null;
20
10
  #system;
@@ -37,7 +27,7 @@ export default class RedisForCore {
37
27
  syncFromTmp = async () => {
38
28
  await this.teardown();
39
29
  const loaders = this.#resolveDependsLoaders();
40
- if (!readInfraLoaderEnabled(loaders, InfraLoaderEnum.REDIS, true)) {
30
+ if (!readInfraLoaderEnabled(loaders, InfraLoaderEnum.REDIS, false)) {
41
31
  return;
42
32
  }
43
33
  if (this.#configs.all.isRedisPassive) {
@@ -47,33 +37,11 @@ export default class RedisForCore {
47
37
  if (!uri) {
48
38
  return;
49
39
  }
50
- const socketOpts = resolveRedisSocketOptions(uri);
51
- const sentinelHosts = process.env.REDIS_SENTINEL_HOSTS?.trim();
52
- const sentinelMaster = process.env.REDIS_SENTINEL_MASTER?.trim() || 'mymaster';
53
- let client;
54
- if (sentinelHosts) {
55
- const rootNodes = parseSentinelRootNodes(sentinelHosts);
56
- if (rootNodes.length === 0) {
57
- throw new Error('REDIS_SENTINEL_HOSTS is set but empty after parse');
58
- }
59
- client = (await createSentinel({
60
- name: sentinelMaster,
61
- sentinelRootNodes: rootNodes,
62
- nodeClientOptions: {
63
- url: uri,
64
- socket: socketOpts,
65
- },
66
- sentinelClientOptions: {
67
- socket: socketOpts,
68
- },
69
- }).connect());
70
- }
71
- else {
72
- client = (await createClient({
73
- url: uri,
74
- socket: socketOpts,
75
- }).connect());
76
- }
40
+ const client = await connectRedisClient({
41
+ uri,
42
+ sentinelMaster: this.#configs.all.redisSentinelMaster,
43
+ isRedisSentinel: this.#configs.all.isRedisSentinel,
44
+ });
77
45
  client.on('error', (err) => {
78
46
  console.error(`Redis ${err}`);
79
47
  });
@@ -0,0 +1,16 @@
1
+ export type RedisEndpoint = {
2
+ host: string;
3
+ port: number;
4
+ };
5
+ export type ParsedRedisUri = {
6
+ creds: {
7
+ username?: string;
8
+ password?: string;
9
+ };
10
+ endpoints: RedisEndpoint[];
11
+ /** HA comma hosts or sentinel port → use createSentinel */
12
+ useSentinel: boolean;
13
+ };
14
+ /** Comma-host rediss:// URIs are not valid for `new URL()` — parse manually. */
15
+ export declare function parseRedisUri(uri: string): ParsedRedisUri;
16
+ export declare function standaloneRedisUrl(uri: string, endpoints: RedisEndpoint[]): string;
@@ -0,0 +1,92 @@
1
+ const DEFAULT_SENTINEL_PORT = 26379;
2
+ const DEFAULT_DATA_PORT = 6379;
3
+ function decodeCred(part) {
4
+ if (!part) {
5
+ return undefined;
6
+ }
7
+ try {
8
+ return decodeURIComponent(part);
9
+ }
10
+ catch {
11
+ return part;
12
+ }
13
+ }
14
+ function splitAuthAndHosts(netloc) {
15
+ const at = netloc.lastIndexOf('@');
16
+ if (at === -1) {
17
+ return { auth: '', hostsCsv: netloc };
18
+ }
19
+ return { auth: netloc.slice(0, at), hostsCsv: netloc.slice(at + 1) };
20
+ }
21
+ function parseAuth(auth) {
22
+ if (!auth) {
23
+ return {};
24
+ }
25
+ const colon = auth.indexOf(':');
26
+ if (colon === -1) {
27
+ return { username: decodeCred(auth) };
28
+ }
29
+ return {
30
+ username: decodeCred(auth.slice(0, colon)),
31
+ password: decodeCred(auth.slice(colon + 1)),
32
+ };
33
+ }
34
+ function parseHostsCsv(hostsCsv) {
35
+ return hostsCsv
36
+ .split(',')
37
+ .map((part) => part.trim())
38
+ .filter(Boolean)
39
+ .map((part) => {
40
+ const colon = part.lastIndexOf(':');
41
+ if (colon === -1) {
42
+ return { host: part, port: DEFAULT_DATA_PORT };
43
+ }
44
+ const host = part.slice(0, colon);
45
+ const port = Number.parseInt(part.slice(colon + 1), 10);
46
+ return { host, port: Number.isFinite(port) ? port : DEFAULT_DATA_PORT };
47
+ })
48
+ .filter((e) => e.host.length > 0);
49
+ }
50
+ function isSentinelEndpoints(endpoints) {
51
+ if (endpoints.length > 1) {
52
+ return true;
53
+ }
54
+ return endpoints.length === 1 && endpoints[0].port === DEFAULT_SENTINEL_PORT;
55
+ }
56
+ /** Comma-host rediss:// URIs are not valid for `new URL()` — parse manually. */
57
+ export function parseRedisUri(uri) {
58
+ const trimmed = uri.trim();
59
+ const schemeEnd = trimmed.indexOf('://');
60
+ if (schemeEnd === -1) {
61
+ throw new Error('Invalid Redis URI: missing scheme');
62
+ }
63
+ const rest = trimmed.slice(schemeEnd + 3);
64
+ const pathStart = rest.search(/[/?#]/);
65
+ const authority = pathStart === -1 ? rest : rest.slice(0, pathStart);
66
+ const { auth, hostsCsv } = splitAuthAndHosts(authority);
67
+ const endpoints = parseHostsCsv(hostsCsv);
68
+ if (endpoints.length === 0) {
69
+ throw new Error('Invalid Redis URI: no hosts');
70
+ }
71
+ return {
72
+ creds: parseAuth(auth),
73
+ endpoints,
74
+ useSentinel: isSentinelEndpoints(endpoints),
75
+ };
76
+ }
77
+ export function standaloneRedisUrl(uri, endpoints) {
78
+ if (endpoints.length !== 1) {
79
+ throw new Error('standalone Redis URL requires exactly one endpoint');
80
+ }
81
+ const trimmed = uri.trim();
82
+ const schemeEnd = trimmed.indexOf('://');
83
+ const scheme = trimmed.slice(0, schemeEnd + 3);
84
+ const rest = trimmed.slice(schemeEnd + 3);
85
+ const pathStart = rest.search(/[/?#]/);
86
+ const suffix = pathStart === -1 ? '' : rest.slice(pathStart);
87
+ const { auth } = splitAuthAndHosts(pathStart === -1 ? rest : rest.slice(0, pathStart));
88
+ const ep = endpoints[0];
89
+ const hostPart = ep.host.includes(':') ? `[${ep.host}]` : ep.host;
90
+ const netloc = auth ? `${auth}@${hostPart}:${ep.port}` : `${hostPart}:${ep.port}`;
91
+ return `${scheme}${netloc}${suffix}`;
92
+ }