@xrystal/core 3.32.1 → 3.37.4

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 (264) hide show
  1. package/README.md +676 -136
  2. package/package.json +27 -24
  3. package/tsconfig.base.json +15 -0
  4. package/x/bin/commands/create.mjs +34 -5
  5. package/x/bin/commands/doctor.mjs +115 -25
  6. package/x/bin/commands/hooks.mjs +55 -0
  7. package/x/bin/commands/project.mjs +75 -42
  8. package/x/bin/commands/release.mjs +8 -3
  9. package/x/bin/commands/sync-toolchain.mjs +43 -0
  10. package/x/bin/commands/tmp.mjs +13 -4
  11. package/x/bin/helpers/.generated/.generated-from +1 -0
  12. package/x/bin/helpers/.generated/tmp-schema.mjs +192 -0
  13. package/x/bin/helpers/banner.mjs +66 -12
  14. package/x/bin/helpers/build-config.mjs +342 -0
  15. package/x/bin/helpers/cli-help.mjs +164 -0
  16. package/x/bin/helpers/create-target.mjs +18 -2
  17. package/x/bin/helpers/project-artifacts.mjs +142 -0
  18. package/x/bin/helpers/runtime-info.mjs +172 -0
  19. package/x/bin/helpers/tmp-config.mjs +11 -10
  20. package/x/bin/helpers/tmp-layout.mjs +10 -16
  21. package/x/bin/helpers/tmp-schema.mjs +9 -0
  22. package/x/bin/helpers/toolchain-config.mjs +233 -0
  23. package/x/bin/main-cli.js +31 -3
  24. package/x/bin/scripts/clean-source-types.mjs +19 -0
  25. package/x/bin/scripts/fix-dist-imports.mjs +80 -0
  26. package/x/bin/scripts/install-git-hooks.mjs +86 -0
  27. package/x/bin/scripts/minify-dist.mjs +50 -0
  28. package/x/bin/scripts/prepare-npm-publish.mjs +39 -0
  29. package/x/bin/scripts/publish-guard.mjs +113 -0
  30. package/x/bin/scripts/verify-npm-pack.mjs +12 -0
  31. package/x/bin/scripts/verify-publish-config.mjs +10 -0
  32. package/x/bin/scripts/verify-version-bump.mjs +27 -0
  33. package/x/bin/scripts/version-check.mjs +173 -0
  34. package/x/dist/index.js +1 -0
  35. package/x/dist/loader/clients/index.js +1 -0
  36. package/x/dist/loader/configs/index.js +1 -0
  37. package/{source → x/dist}/loader/controller/index.d.ts +20 -17
  38. package/x/dist/loader/controller/index.js +1 -0
  39. package/x/dist/loader/dto/index.js +1 -0
  40. package/x/dist/loader/events/index.js +1 -0
  41. package/x/dist/loader/index.d.ts +11 -0
  42. package/x/dist/loader/index.js +1 -0
  43. package/{source → x/dist}/loader/kafka/index.d.ts +1 -0
  44. package/x/dist/loader/kafka/index.js +1 -0
  45. package/x/dist/loader/localizations/index.js +1 -0
  46. package/{source → x/dist}/loader/logger/index.d.ts +1 -0
  47. package/x/dist/loader/logger/index.js +2 -0
  48. package/x/dist/loader/service/index.js +1 -0
  49. package/x/dist/loader/system/index.js +1 -0
  50. package/x/dist/project/index.d.ts +12 -0
  51. package/x/dist/project/index.js +1 -0
  52. package/x/dist/utils/constants/defaults.json +44 -0
  53. package/x/dist/utils/constants/index.js +1 -0
  54. package/x/dist/utils/helpers/configs/index.js +1 -0
  55. package/x/dist/utils/helpers/context/controller-store.js +1 -0
  56. package/x/dist/utils/helpers/context/index.js +1 -0
  57. package/{source → x/dist}/utils/helpers/core/index.d.ts +1 -1
  58. package/x/dist/utils/helpers/core/index.js +1 -0
  59. package/x/dist/utils/helpers/core/paths.js +1 -0
  60. package/x/dist/utils/helpers/core/tmp-layout.js +1 -0
  61. package/x/dist/utils/helpers/date/index.js +1 -0
  62. package/x/dist/utils/helpers/dispatch/index.js +1 -0
  63. package/x/dist/utils/helpers/dto/index.js +1 -0
  64. package/x/dist/utils/helpers/filters/index.js +1 -0
  65. package/x/dist/utils/helpers/framework/index.js +1 -0
  66. package/x/dist/utils/helpers/handlers/adapters.js +1 -0
  67. package/x/dist/utils/helpers/handlers/execute.js +1 -0
  68. package/x/dist/utils/helpers/handlers/index.js +1 -0
  69. package/x/dist/utils/helpers/hash/crypto.js +1 -0
  70. package/x/dist/utils/helpers/id/index.js +1 -0
  71. package/x/dist/utils/helpers/index.js +1 -0
  72. package/x/dist/utils/helpers/ip/index.js +1 -0
  73. package/x/dist/utils/helpers/is/index.js +1 -0
  74. package/x/dist/utils/helpers/locales/index.js +1 -0
  75. package/x/dist/utils/helpers/math/index.js +1 -0
  76. package/{source → x/dist}/utils/helpers/middleware/index.d.ts +2 -2
  77. package/x/dist/utils/helpers/middleware/index.js +1 -0
  78. package/x/dist/utils/helpers/objects/index.js +1 -0
  79. package/x/dist/utils/helpers/path/index.js +1 -0
  80. package/{source → x/dist}/utils/helpers/protocol/index.d.ts +2 -10
  81. package/x/dist/utils/helpers/protocol/index.js +1 -0
  82. package/x/dist/utils/helpers/protocol/options.js +1 -0
  83. package/x/dist/utils/helpers/regex/checkSpecialRegexControl.js +1 -0
  84. package/x/dist/utils/helpers/runtime/dto-access.js +1 -0
  85. package/x/dist/utils/helpers/runtime/dto-types.js +1 -0
  86. package/{source → x/dist}/utils/helpers/runtime/index.d.ts +1 -0
  87. package/x/dist/utils/helpers/runtime/index.js +1 -0
  88. package/x/dist/utils/helpers/runtime/shutdown.d.ts +3 -0
  89. package/x/dist/utils/helpers/runtime/shutdown.js +1 -0
  90. package/x/dist/utils/helpers/state/index.js +1 -0
  91. package/x/dist/utils/helpers/state/props-from-tmp.js +1 -0
  92. package/x/dist/utils/helpers/state/request-key-guard.js +1 -0
  93. package/x/dist/utils/helpers/state/scopes.js +1 -0
  94. package/x/dist/utils/helpers/string/index.js +1 -0
  95. package/x/dist/utils/helpers/tests/tmp-schema.test.js +1 -0
  96. package/x/dist/utils/helpers/tests/tmp-watch.test.d.ts +1 -0
  97. package/x/dist/utils/helpers/tests/tmp-watch.test.js +1 -0
  98. package/x/dist/utils/helpers/timer/index.js +1 -0
  99. package/x/dist/utils/helpers/tmp/index.d.ts +5 -0
  100. package/x/dist/utils/helpers/tmp/index.js +1 -0
  101. package/x/dist/utils/helpers/tmp/loader-tmp-sync.js +1 -0
  102. package/{source → x/dist}/utils/helpers/tmp/runtime-tmp.d.ts +2 -6
  103. package/x/dist/utils/helpers/tmp/runtime-tmp.js +1 -0
  104. package/{source → x/dist}/utils/helpers/tmp/tmp-loader.d.ts +1 -0
  105. package/x/dist/utils/helpers/tmp/tmp-loader.js +1 -0
  106. package/x/dist/utils/helpers/tmp/tmp-schema.d.ts +47 -0
  107. package/x/dist/utils/helpers/tmp/tmp-schema.js +1 -0
  108. package/x/dist/utils/helpers/tmp/tmp-watch.d.ts +1 -0
  109. package/x/dist/utils/helpers/tmp/tmp-watch.js +1 -0
  110. package/x/dist/utils/helpers/validates/index.js +1 -0
  111. package/x/dist/utils/index.js +1 -0
  112. package/x/dist/utils/models/classes/class.clients.js +1 -0
  113. package/x/dist/utils/models/classes/class.interfaces.js +1 -0
  114. package/x/dist/utils/models/classes/class.service-locator.js +1 -0
  115. package/{source → x/dist}/utils/models/classes/class.tmp-file-loader.d.ts +3 -2
  116. package/x/dist/utils/models/classes/class.tmp-file-loader.js +1 -0
  117. package/x/dist/utils/models/classes/class.x.js +1 -0
  118. package/x/dist/utils/models/enums/index.js +1 -0
  119. package/x/dist/utils/models/index.js +1 -0
  120. package/x/dist/utils/models/types/index.js +1 -0
  121. package/x/dist/utils/package-root.js +1 -0
  122. package/x/tmp.yml +51 -42
  123. package/source/index.js +0 -11
  124. package/source/loader/clients/index.js +0 -22
  125. package/source/loader/configs/index.js +0 -88
  126. package/source/loader/controller/index.js +0 -236
  127. package/source/loader/dto/index.js +0 -92
  128. package/source/loader/events/index.js +0 -44
  129. package/source/loader/index.d.ts +0 -11
  130. package/source/loader/index.js +0 -11
  131. package/source/loader/kafka/index.js +0 -137
  132. package/source/loader/localizations/index.js +0 -46
  133. package/source/loader/logger/index.js +0 -217
  134. package/source/loader/service/index.js +0 -7
  135. package/source/loader/system/index.js +0 -25
  136. package/source/project/index.d.ts +0 -8
  137. package/source/project/index.js +0 -70
  138. package/source/utils/constants/defaults.json +0 -15
  139. package/source/utils/constants/defaults.mjs +0 -10
  140. package/source/utils/constants/index.js +0 -22
  141. package/source/utils/helpers/configs/index.js +0 -1
  142. package/source/utils/helpers/context/controller-store.js +0 -12
  143. package/source/utils/helpers/context/index.js +0 -24
  144. package/source/utils/helpers/core/index.js +0 -58
  145. package/source/utils/helpers/core/paths.js +0 -78
  146. package/source/utils/helpers/core/tmp-layout.js +0 -241
  147. package/source/utils/helpers/date/index.js +0 -48
  148. package/source/utils/helpers/dispatch/index.js +0 -53
  149. package/source/utils/helpers/dto/index.js +0 -10
  150. package/source/utils/helpers/filters/index.js +0 -44
  151. package/source/utils/helpers/framework/index.js +0 -17
  152. package/source/utils/helpers/handlers/adapters.js +0 -40
  153. package/source/utils/helpers/handlers/execute.js +0 -82
  154. package/source/utils/helpers/handlers/index.js +0 -31
  155. package/source/utils/helpers/hash/crypto.js +0 -22
  156. package/source/utils/helpers/id/index.js +0 -24
  157. package/source/utils/helpers/index.js +0 -26
  158. package/source/utils/helpers/ip/index.js +0 -21
  159. package/source/utils/helpers/is/index.js +0 -35
  160. package/source/utils/helpers/locales/index.js +0 -82
  161. package/source/utils/helpers/math/index.js +0 -14
  162. package/source/utils/helpers/middleware/index.js +0 -36
  163. package/source/utils/helpers/objects/index.js +0 -55
  164. package/source/utils/helpers/path/index.js +0 -4
  165. package/source/utils/helpers/protocol/index.js +0 -99
  166. package/source/utils/helpers/protocol/options.js +0 -13
  167. package/source/utils/helpers/regex/checkSpecialRegexControl.js +0 -3
  168. package/source/utils/helpers/runtime/dto-access.js +0 -5
  169. package/source/utils/helpers/runtime/index.js +0 -1
  170. package/source/utils/helpers/state/index.js +0 -185
  171. package/source/utils/helpers/state/props-from-tmp.js +0 -44
  172. package/source/utils/helpers/state/request-key-guard.js +0 -42
  173. package/source/utils/helpers/state/scopes.js +0 -69
  174. package/source/utils/helpers/string/index.js +0 -9
  175. package/source/utils/helpers/timer/index.js +0 -5
  176. package/source/utils/helpers/tmp/index.d.ts +0 -3
  177. package/source/utils/helpers/tmp/index.js +0 -3
  178. package/source/utils/helpers/tmp/loader-tmp-sync.js +0 -13
  179. package/source/utils/helpers/tmp/runtime-tmp.js +0 -30
  180. package/source/utils/helpers/tmp/tmp-loader.js +0 -48
  181. package/source/utils/helpers/validates/index.js +0 -20
  182. package/source/utils/index.js +0 -3
  183. package/source/utils/models/classes/class.clients.js +0 -142
  184. package/source/utils/models/classes/class.interfaces.js +0 -10
  185. package/source/utils/models/classes/class.service-locator.js +0 -16
  186. package/source/utils/models/classes/class.tmp-file-loader.js +0 -38
  187. package/source/utils/models/classes/class.x.js +0 -180
  188. package/source/utils/models/enums/index.js +0 -148
  189. package/source/utils/models/index.js +0 -9
  190. package/source/utils/models/types/index.js +0 -2
  191. package/source/utils/models/types/shims.d.ts +0 -123
  192. package/source/utils/package-root.js +0 -5
  193. package/x/tests/cli.test.ts +0 -85
  194. package/x/tests/context.test.ts +0 -25
  195. package/x/tests/dispatch.test.ts +0 -84
  196. package/x/tests/fixtures/locales/en/translation.json +0 -8
  197. package/x/tests/fixtures/locales/tr/translation.json +0 -8
  198. package/x/tests/frameworks.integration.test.ts +0 -185
  199. package/x/tests/helpers/i18n-setup.ts +0 -52
  200. package/x/tests/helpers/middleware.ts +0 -23
  201. package/x/tests/helpers/register-test-dto.ts +0 -52
  202. package/x/tests/i18n.integration.test.ts +0 -77
  203. package/x/tests/i18n.test.ts +0 -96
  204. package/x/tests/middleware.test.ts +0 -83
  205. package/x/tests/project-layout.test.ts +0 -65
  206. package/x/tests/protocol.test.ts +0 -75
  207. package/x/tests/realtime.examples.test.ts +0 -198
  208. package/x/tests/runtime-tmp.test.ts +0 -49
  209. package/x/tests/socket.integration.test.ts +0 -174
  210. package/x/tests/state.test.ts +0 -183
  211. package/x/tests/tmp-layout-guard.test.ts +0 -40
  212. /package/{source → x/dist}/index.d.ts +0 -0
  213. /package/{source → x/dist}/loader/clients/index.d.ts +0 -0
  214. /package/{source → x/dist}/loader/configs/index.d.ts +0 -0
  215. /package/{source → x/dist}/loader/dto/index.d.ts +0 -0
  216. /package/{source → x/dist}/loader/events/index.d.ts +0 -0
  217. /package/{source → x/dist}/loader/localizations/index.d.ts +0 -0
  218. /package/{source → x/dist}/loader/service/index.d.ts +0 -0
  219. /package/{source → x/dist}/loader/system/index.d.ts +0 -0
  220. /package/{source → x/dist}/utils/constants/index.d.ts +0 -0
  221. /package/{source → x/dist}/utils/helpers/configs/index.d.ts +0 -0
  222. /package/{source → x/dist}/utils/helpers/context/controller-store.d.ts +0 -0
  223. /package/{source → x/dist}/utils/helpers/context/index.d.ts +0 -0
  224. /package/{source → x/dist}/utils/helpers/core/paths.d.ts +0 -0
  225. /package/{source → x/dist}/utils/helpers/core/tmp-layout.d.ts +0 -0
  226. /package/{source → x/dist}/utils/helpers/date/index.d.ts +0 -0
  227. /package/{source → x/dist}/utils/helpers/dispatch/index.d.ts +0 -0
  228. /package/{source → x/dist}/utils/helpers/dto/index.d.ts +0 -0
  229. /package/{source → x/dist}/utils/helpers/filters/index.d.ts +0 -0
  230. /package/{source → x/dist}/utils/helpers/framework/index.d.ts +0 -0
  231. /package/{source → x/dist}/utils/helpers/handlers/adapters.d.ts +0 -0
  232. /package/{source → x/dist}/utils/helpers/handlers/execute.d.ts +0 -0
  233. /package/{source → x/dist}/utils/helpers/handlers/index.d.ts +0 -0
  234. /package/{source → x/dist}/utils/helpers/hash/crypto.d.ts +0 -0
  235. /package/{source → x/dist}/utils/helpers/id/index.d.ts +0 -0
  236. /package/{source → x/dist}/utils/helpers/index.d.ts +0 -0
  237. /package/{source → x/dist}/utils/helpers/ip/index.d.ts +0 -0
  238. /package/{source → x/dist}/utils/helpers/is/index.d.ts +0 -0
  239. /package/{source → x/dist}/utils/helpers/locales/index.d.ts +0 -0
  240. /package/{source → x/dist}/utils/helpers/math/index.d.ts +0 -0
  241. /package/{source → x/dist}/utils/helpers/objects/index.d.ts +0 -0
  242. /package/{source → x/dist}/utils/helpers/path/index.d.ts +0 -0
  243. /package/{source → x/dist}/utils/helpers/protocol/options.d.ts +0 -0
  244. /package/{source → x/dist}/utils/helpers/regex/checkSpecialRegexControl.d.ts +0 -0
  245. /package/{source → x/dist}/utils/helpers/runtime/dto-access.d.ts +0 -0
  246. /package/{source → x/dist}/utils/helpers/runtime/dto-types.d.ts +0 -0
  247. /package/{source → x/dist}/utils/helpers/state/index.d.ts +0 -0
  248. /package/{source → x/dist}/utils/helpers/state/props-from-tmp.d.ts +0 -0
  249. /package/{source → x/dist}/utils/helpers/state/request-key-guard.d.ts +0 -0
  250. /package/{source → x/dist}/utils/helpers/state/scopes.d.ts +0 -0
  251. /package/{source → x/dist}/utils/helpers/string/index.d.ts +0 -0
  252. /package/{source/utils/helpers/runtime/dto-types.js → x/dist/utils/helpers/tests/tmp-schema.test.d.ts} +0 -0
  253. /package/{source → x/dist}/utils/helpers/timer/index.d.ts +0 -0
  254. /package/{source → x/dist}/utils/helpers/tmp/loader-tmp-sync.d.ts +0 -0
  255. /package/{source → x/dist}/utils/helpers/validates/index.d.ts +0 -0
  256. /package/{source → x/dist}/utils/index.d.ts +0 -0
  257. /package/{source → x/dist}/utils/models/classes/class.clients.d.ts +0 -0
  258. /package/{source → x/dist}/utils/models/classes/class.interfaces.d.ts +0 -0
  259. /package/{source → x/dist}/utils/models/classes/class.service-locator.d.ts +0 -0
  260. /package/{source → x/dist}/utils/models/classes/class.x.d.ts +0 -0
  261. /package/{source → x/dist}/utils/models/enums/index.d.ts +0 -0
  262. /package/{source → x/dist}/utils/models/index.d.ts +0 -0
  263. /package/{source → x/dist}/utils/models/types/index.d.ts +0 -0
  264. /package/{source → x/dist}/utils/package-root.d.ts +0 -0
@@ -1,44 +0,0 @@
1
- export const G_PROP_NOT_ALLOWED = '[xrystal] G prop not declared in tmp.yml vars — set autoCreateProps: true to allow runtime keys';
2
- const emptyVars = () => ({});
3
- function readGStoreNode(configs) {
4
- const loaders = configs.loaders;
5
- const fromSystem = loaders?.system;
6
- return fromSystem?.gStore;
7
- }
8
- function readBootstrap(options) {
9
- if (typeof options.bootstrap === 'boolean')
10
- return options.bootstrap;
11
- return true;
12
- }
13
- function readAllowAutoCreateProps(options) {
14
- if (typeof options.autoCreateProps === 'boolean')
15
- return options.autoCreateProps;
16
- return true;
17
- }
18
- function readWarnOnRequestKeys(options) {
19
- if (typeof options.warnOnRequestKeys === 'boolean')
20
- return options.warnOnRequestKeys;
21
- return true;
22
- }
23
- export function resolveGStoreBoot(tmp = {}) {
24
- const configs = (tmp.configs ?? tmp);
25
- const gStore = readGStoreNode(configs);
26
- if (!gStore) {
27
- return { bootstrap: true, allowAutoCreateProps: true, warnOnRequestKeys: true, vars: emptyVars() };
28
- }
29
- const options = (gStore.options ?? {});
30
- const varsRaw = gStore.vars;
31
- const vars = varsRaw && typeof varsRaw === 'object' && !Array.isArray(varsRaw)
32
- ? { ...varsRaw }
33
- : emptyVars();
34
- return {
35
- bootstrap: readBootstrap(options),
36
- allowAutoCreateProps: readAllowAutoCreateProps(options),
37
- warnOnRequestKeys: readWarnOnRequestKeys(options),
38
- vars,
39
- };
40
- }
41
- export function propsFromTmp(tmp = {}) {
42
- const boot = resolveGStoreBoot(tmp);
43
- return boot.bootstrap ? boot.vars : emptyVars();
44
- }
@@ -1,42 +0,0 @@
1
- import { getRunContext } from '../../../utils/helpers/context';
2
- import { getControllerCtx } from '../../../utils/helpers/context/controller-store';
3
- export const G_REQUEST_KEY_WARN = '[xrystal] G: key looks request-scoped — use run/controller context, not global G';
4
- const EXACT = new Set([
5
- 'requestid',
6
- 'reqid',
7
- 'correlationid',
8
- 'cookies',
9
- 'cookie',
10
- 'headers',
11
- 'header',
12
- 'body',
13
- 'query',
14
- 'queries',
15
- 'params',
16
- 'locals',
17
- 'session',
18
- 'sessionid',
19
- ]);
20
- export function looksLikeRequestScopedKey(key) {
21
- const trimmed = key.trim();
22
- if (!trimmed)
23
- return false;
24
- const lower = trimmed.toLowerCase();
25
- if (EXACT.has(lower))
26
- return true;
27
- if (lower.endsWith('requestid') || lower.endsWith('correlationid'))
28
- return true;
29
- if (lower === 'req' || lower === 'res')
30
- return true;
31
- return false;
32
- }
33
- export function warnIfRequestScopedKey(key, enabled) {
34
- if (!enabled || !looksLikeRequestScopedKey(key))
35
- return;
36
- const activeScope = getRunContext()
37
- ? 'run'
38
- : getControllerCtx()
39
- ? 'controller'
40
- : 'outside-request';
41
- console.warn(`${G_REQUEST_KEY_WARN} (key: ${key}, scope: ${activeScope})`);
42
- }
@@ -1,69 +0,0 @@
1
- import { GStoreScopeEnum } from '../../../utils/models/enums';
2
- import { getRunContext, patchRunContext } from '../../../utils/helpers/context';
3
- import { getControllerCtx } from '../../../utils/helpers/context/controller-store';
4
- export function summarizeController(ctx) {
5
- if (!ctx)
6
- return null;
7
- return {
8
- protocol: ctx.protocol,
9
- framework: ctx.framework,
10
- requestId: ctx.requestId,
11
- metadata: ctx.metadata,
12
- hasReq: !!ctx.req,
13
- hasRes: !!ctx.res,
14
- hasSocket: !!ctx.socket,
15
- };
16
- }
17
- export function runContextValues(ctx) {
18
- if (!ctx)
19
- return null;
20
- return { ...ctx };
21
- }
22
- export function buildStoresSnapshot(globalValues) {
23
- const run = getRunContext();
24
- const controller = summarizeController(getControllerCtx());
25
- return {
26
- [GStoreScopeEnum.GLOBAL]: {
27
- name: GStoreScopeEnum.GLOBAL,
28
- active: true,
29
- keys: Object.keys(globalValues),
30
- values: { ...globalValues },
31
- },
32
- [GStoreScopeEnum.RUN]: {
33
- name: GStoreScopeEnum.RUN,
34
- active: !!run,
35
- keys: run ? Object.keys(run) : [],
36
- values: runContextValues(run),
37
- },
38
- [GStoreScopeEnum.CONTROLLER]: {
39
- name: GStoreScopeEnum.CONTROLLER,
40
- active: !!controller,
41
- keys: controller ? Object.keys(controller) : [],
42
- values: controller,
43
- },
44
- };
45
- }
46
- export function patchControllerScope(patch) {
47
- const ctx = getControllerCtx();
48
- if (!ctx)
49
- return false;
50
- if (patch.requestId !== undefined)
51
- ctx.requestId = patch.requestId;
52
- if (patch.metadata) {
53
- ctx.metadata = {
54
- ...ctx.metadata,
55
- ...patch.metadata,
56
- locals: {
57
- ...(ctx.metadata?.locals ?? {}),
58
- ...(patch.metadata.locals ?? {}),
59
- },
60
- };
61
- }
62
- return true;
63
- }
64
- export function patchRunScope(patch) {
65
- if (!getRunContext())
66
- return false;
67
- patchRunContext(patch);
68
- return true;
69
- }
@@ -1,9 +0,0 @@
1
- export function cleanStringForNormalize(input) {
2
- if (typeof input !== 'string')
3
- return '';
4
- const cleaned = input
5
- .normalize('NFD')
6
- .replace(/\u0307/g, '')
7
- .normalize('NFC');
8
- return cleaned;
9
- }
@@ -1,5 +0,0 @@
1
- export async function delay({ duration, }) {
2
- return new Promise((resolve, reject) => {
3
- setTimeout(() => resolve(delay), duration);
4
- });
5
- }
@@ -1,3 +0,0 @@
1
- export { setRuntimeTmpConfigs, getRuntimeTmpConfigs, onRuntimeTmpChange, notifyRuntimeTmpChange, clearRuntimeTmpConfigs, type RuntimeTmpConfigs, } from '../../../utils/helpers/tmp/runtime-tmp';
2
- export { getTmpLoader, getTmpLoaderPath, getTmpSnapshot, onTmpReload, resetTmpLoaderForTests, } from '../../../utils/helpers/tmp/tmp-loader';
3
- export { bindLoaderTmpSync, type ITmpSyncLoader, } from '../../../utils/helpers/tmp/loader-tmp-sync';
@@ -1,3 +0,0 @@
1
- export { setRuntimeTmpConfigs, getRuntimeTmpConfigs, onRuntimeTmpChange, notifyRuntimeTmpChange, clearRuntimeTmpConfigs, } from '../../../utils/helpers/tmp/runtime-tmp';
2
- export { getTmpLoader, getTmpLoaderPath, getTmpSnapshot, onTmpReload, resetTmpLoaderForTests, } from '../../../utils/helpers/tmp/tmp-loader';
3
- export { bindLoaderTmpSync, } from '../../../utils/helpers/tmp/loader-tmp-sync';
@@ -1,13 +0,0 @@
1
- import { onRuntimeTmpChange } from '../../../utils/helpers/tmp/runtime-tmp';
2
- export function bindLoaderTmpSync(loader) {
3
- const tag = loader.constructor.name;
4
- onRuntimeTmpChange(async () => {
5
- try {
6
- await loader.syncFromTmp();
7
- }
8
- catch (error) {
9
- const message = error instanceof Error ? error.message : String(error);
10
- console.warn(`[xrystal] ${tag} tmp reload failed: ${message}`);
11
- }
12
- });
13
- }
@@ -1,30 +0,0 @@
1
- let runtimeConfigs;
2
- const listeners = [];
3
- export function setRuntimeTmpConfigs(configs) {
4
- runtimeConfigs = configs;
5
- }
6
- export function getRuntimeTmpConfigs() {
7
- return runtimeConfigs;
8
- }
9
- export function onRuntimeTmpChange(listener) {
10
- listeners.push(listener);
11
- return () => {
12
- const index = listeners.indexOf(listener);
13
- if (index >= 0)
14
- listeners.splice(index, 1);
15
- };
16
- }
17
- export async function notifyRuntimeTmpChange(configs) {
18
- if (configs)
19
- runtimeConfigs = configs;
20
- const snapshot = runtimeConfigs;
21
- if (!snapshot)
22
- return;
23
- for (const listener of listeners) {
24
- await listener(snapshot);
25
- }
26
- }
27
- export function clearRuntimeTmpConfigs() {
28
- runtimeConfigs = undefined;
29
- listeners.length = 0;
30
- }
@@ -1,48 +0,0 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { Constants } from '../../../utils/constants';
4
- import { assertCoreTmpGuard, coreCanonicalTmpPath, isCorePackageRoot, } from '../../../utils/helpers/core/tmp-layout';
5
- import { findFileRecursively } from '../../../utils/helpers/core/paths';
6
- import { TmpFileLoader } from '../../../utils/models/classes/class.tmp-file-loader';
7
- let loaderInstance = null;
8
- let loaderPath = null;
9
- function resolveTmpPath(root = process.cwd(), tmpFileName = Constants.tmpBaseName, ext = Constants.tmpExtension) {
10
- const resolvedRoot = path.resolve(root);
11
- if (isCorePackageRoot(resolvedRoot)) {
12
- const canonical = coreCanonicalTmpPath(resolvedRoot);
13
- if (!fs.existsSync(canonical)) {
14
- throw new Error(`${Constants.packageRootFolder}/tmp.yml not found. Run xcore tmp or ensureCoreTmpEntry() before starting @xrystal/core`);
15
- }
16
- assertCoreTmpGuard(resolvedRoot, canonical);
17
- return canonical;
18
- }
19
- const filePath = findFileRecursively(resolvedRoot, tmpFileName, ext);
20
- if (!filePath) {
21
- throw new Error(`${tmpFileName}${ext.startsWith('.') ? ext : `.${ext}`} file not found`);
22
- }
23
- return filePath;
24
- }
25
- export function getTmpLoaderPath() {
26
- if (!loaderPath)
27
- loaderPath = resolveTmpPath();
28
- return loaderPath;
29
- }
30
- export function getTmpLoader() {
31
- if (!loaderInstance) {
32
- loaderInstance = new TmpFileLoader({ filePath: getTmpLoaderPath() });
33
- }
34
- return loaderInstance;
35
- }
36
- export function getTmpSnapshot() {
37
- return getTmpLoader().getResolvedTmpFile();
38
- }
39
- export function onTmpReload(listener) {
40
- const loader = getTmpLoader();
41
- const handler = () => listener(loader.getResolvedTmpFile());
42
- loader.on('reload', handler);
43
- return () => loader.off('reload', handler);
44
- }
45
- export function resetTmpLoaderForTests() {
46
- loaderInstance = null;
47
- loaderPath = null;
48
- }
@@ -1,20 +0,0 @@
1
- export const validateScopes = ({ scopes, permissions, resources }) => {
2
- const parsedScopes = scopes.split(' ');
3
- function dublicateControl(parsedScopes) {
4
- const uniqueScopes = new Set(parsedScopes);
5
- if (uniqueScopes.size !== parsedScopes.length) {
6
- return true;
7
- }
8
- return false;
9
- }
10
- if (dublicateControl(parsedScopes)) {
11
- return false;
12
- }
13
- const isInvalid = parsedScopes.some((scope) => {
14
- const [resource, permission] = scope.split(':');
15
- const isValidResource = resources.includes(resource);
16
- const isValidPermission = permissions.includes(permission);
17
- return !isValidResource || !isValidPermission;
18
- });
19
- return !isInvalid;
20
- };
@@ -1,3 +0,0 @@
1
- export * from '../utils/constants';
2
- export * from '../utils/helpers';
3
- export * from '../utils/models';
@@ -1,142 +0,0 @@
1
- import { createHash, createHmac } from 'node:crypto';
2
- import { Configs, Logger } from '../../../loader';
3
- import { LoggerLayerEnum, x } from '../../../utils';
4
- import { getRequestId } from '../../../utils/helpers/context';
5
- export class ClientStore {
6
- static _store = {};
7
- static get(clientName) { return this._store[clientName] || {}; }
8
- static set(clientName, data) {
9
- this._store[clientName] = { ...this._store[clientName], ...data };
10
- }
11
- }
12
- export class Client {
13
- logger = x.get(Logger);
14
- configs = x.get(Configs);
15
- clientName;
16
- baseURL;
17
- version = null;
18
- timeout = 15000;
19
- debug;
20
- authConfigs;
21
- breaker = {
22
- failures: 0,
23
- lastFailure: 0,
24
- state: 'CLOSED',
25
- threshold: 5,
26
- cooldown: 30000
27
- };
28
- constructor({ clientName, baseURL, version, timeout, auth, debug }) {
29
- this.clientName = clientName;
30
- this.baseURL = baseURL;
31
- this.version = version || null;
32
- this.authConfigs = auth;
33
- this.timeout = timeout || 15000;
34
- const configDebug = Number(this.configs.all.debug) >= LoggerLayerEnum.DEBUG;
35
- this.debug = debug ?? configDebug;
36
- if (auth?.token)
37
- ClientStore.set(clientName, { accessToken: auth.token });
38
- }
39
- resolvePath(obj, path) {
40
- return path.split('.').reduce((prev, curr) => prev ? prev[curr] : undefined, obj);
41
- }
42
- static cryptoHashGenerate = ({ algorithm, input, digest = 'hex' }) => {
43
- return createHash(algorithm).update(input).digest(digest);
44
- };
45
- }
46
- export class BaseApiClient extends Client {
47
- constructor(config) { super(config); }
48
- async request(path, options = {}) {
49
- if (this.breaker.state === 'OPEN') {
50
- if (Date.now() - this.breaker.lastFailure > this.breaker.cooldown) {
51
- this.breaker.state = 'HALF_OPEN';
52
- }
53
- else {
54
- throw new Error(`[CircuitBreaker] ${this.clientName} is OPEN`);
55
- }
56
- }
57
- const maxRetries = options.retries ?? 3;
58
- let attempt = 0;
59
- while (attempt < maxRetries) {
60
- try {
61
- const response = await this._execute(path, { ...options });
62
- if (response.ok || response.status === 401) {
63
- this.breaker.failures = 0;
64
- this.breaker.state = 'CLOSED';
65
- return response;
66
- }
67
- if ([502, 503, 504].includes(response.status)) {
68
- throw new Error(`Server Error: ${response.status}`);
69
- }
70
- return response;
71
- }
72
- catch (error) {
73
- attempt++;
74
- this.breaker.failures++;
75
- this.breaker.lastFailure = Date.now();
76
- if (this.breaker.failures >= this.breaker.threshold)
77
- this.breaker.state = 'OPEN';
78
- if (attempt >= maxRetries)
79
- throw error;
80
- const backoff = Math.pow(2, attempt) * 500;
81
- await new Promise(res => setTimeout(res, backoff));
82
- }
83
- }
84
- throw new Error(`${this.clientName} failed after ${maxRetries} attempts`);
85
- }
86
- async _execute(path, options) {
87
- const base = this.baseURL.replace(/\/$/, '');
88
- const activeVersion = options.version !== undefined ? options.version : this.version;
89
- const ver = activeVersion ? `/${activeVersion.replace(/^\//, '')}` : '';
90
- const url = `${base}${ver}${path}`;
91
- const correlationId = getRequestId();
92
- const headers = new Headers(options.headers || {});
93
- const isUrlEncoded = options.body instanceof URLSearchParams;
94
- const isFormData = options.body instanceof FormData;
95
- if (!isUrlEncoded && !isFormData) {
96
- headers.set('Content-Type', 'application/json');
97
- }
98
- if (correlationId)
99
- headers.set('x-correlation-id', correlationId);
100
- const systemSecret = this.configs.all.internalSecret || process.env.INTERNAL_SECRET;
101
- if (systemSecret) {
102
- const timestamp = Date.now().toString();
103
- const signature = createHmac('sha256', systemSecret)
104
- .update(timestamp + this.clientName)
105
- .digest('hex');
106
- headers.set('x-internal-signature', signature);
107
- headers.set('x-internal-timestamp', timestamp);
108
- headers.set('x-internal-client', this.clientName);
109
- }
110
- const isDebugMode = options.debug ?? this.debug;
111
- const { version, retries, debug, ...fetchOptions } = options;
112
- let processedBody = fetchOptions.body;
113
- if (processedBody && typeof processedBody === 'object' && !isUrlEncoded && !isFormData) {
114
- processedBody = JSON.stringify(processedBody);
115
- }
116
- if (isDebugMode) {
117
- const logData = {
118
- method: fetchOptions.method || 'GET',
119
- url,
120
- body: processedBody && typeof processedBody === 'string' ? JSON.parse(processedBody) : processedBody
121
- };
122
- console.log(`[DEBUG] ${this.clientName}`, logData);
123
- this.logger.winston.info(`${this.clientName} Request`, logData);
124
- }
125
- const controller = new AbortController();
126
- const timeoutId = setTimeout(() => controller.abort(), this.timeout);
127
- try {
128
- const response = await fetch(url, {
129
- ...fetchOptions,
130
- body: processedBody,
131
- headers,
132
- signal: controller.signal
133
- });
134
- clearTimeout(timeoutId);
135
- return response;
136
- }
137
- catch (error) {
138
- clearTimeout(timeoutId);
139
- throw error;
140
- }
141
- }
142
- }
@@ -1,10 +0,0 @@
1
- export class ILifeCycle {
2
- async onInit(props = {}) {
3
- }
4
- onAfterInit(_context) {
5
- }
6
- onDispose(_context) {
7
- }
8
- }
9
- export class IProvide extends ILifeCycle {
10
- }
@@ -1,16 +0,0 @@
1
- // => // X Service Locator
2
- export class Locator {
3
- _context = new Map();
4
- constructor({}) {
5
- }
6
- set = ({ service, reference, args }) => {
7
- this._context.set(service, new reference(...(args || [])));
8
- };
9
- get = (service) => {
10
- if (!this._context.has(service)) {
11
- throw new Error(`${service} not found in context.`);
12
- }
13
- return this._context.get(service);
14
- };
15
- }
16
- export default new Locator({});
@@ -1,38 +0,0 @@
1
- import { EventEmitter } from "node:events";
2
- import { resolve } from "node:path";
3
- import { readFileSync, watchFile } from "node:fs";
4
- import { parse } from "yaml";
5
- import { resolveObjWithHandlebars } from "../../../utils/helpers";
6
- export class TmpFileLoader extends EventEmitter {
7
- #tmpFileJson = {};
8
- #filePath;
9
- constructor({ filePath }) {
10
- super();
11
- this.#filePath = resolve(filePath);
12
- this.#loadSync();
13
- this.#initWatcher();
14
- }
15
- #loadSync() {
16
- try {
17
- const content = readFileSync(this.#filePath, "utf8");
18
- this.#tmpFileJson = parse(content);
19
- this.emit("reload", this.#tmpFileJson);
20
- }
21
- catch (err) {
22
- console.error(`[TmpFileLoader] Error: ${err}`);
23
- }
24
- }
25
- #initWatcher() {
26
- watchFile(this.#filePath, { interval: 500 }, (curr, prev) => {
27
- if (curr.mtimeMs !== prev.mtimeMs) {
28
- this.#loadSync();
29
- }
30
- });
31
- }
32
- getResolvedTmpFile() {
33
- const data = Object.keys(this.#tmpFileJson).length > 0
34
- ? this.#tmpFileJson
35
- : (this.#loadSync(), this.#tmpFileJson);
36
- return resolveObjWithHandlebars(data, data);
37
- }
38
- }