@jskit-ai/kernel 0.1.159 → 0.1.161

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 (102) hide show
  1. package/_testable/index.js +1 -3
  2. package/client/componentRegistry.js +44 -0
  3. package/client/componentRegistry.test.js +16 -0
  4. package/client/moduleBootstrap.js +54 -97
  5. package/client/moduleBootstrap.test.js +47 -32
  6. package/client/vite/clientBootstrapPlugin.test.js +2 -2
  7. package/internal/node/installedPackages.js +2 -7
  8. package/internal/node/installedPackages.test.js +26 -5
  9. package/package.json +3 -5
  10. package/server/actions/actionCatalogue.js +162 -0
  11. package/server/actions/actionEvents.js +66 -0
  12. package/server/actions/actionProvider.js +23 -0
  13. package/server/actions/index.js +3 -6
  14. package/server/features/defineFeature.js +131 -0
  15. package/server/features/defineFeature.test.js +238 -0
  16. package/server/features/index.js +1 -0
  17. package/server/http/HttpProvider.js +23 -0
  18. package/server/http/_testable/index.js +0 -1
  19. package/server/http/capabilityHttpRuntime.js +123 -0
  20. package/server/http/capabilityHttpRuntime.test.js +161 -0
  21. package/server/http/directRequestActionExecutor.js +78 -0
  22. package/server/http/index.js +2 -1
  23. package/server/http/lib/index.js +1 -13
  24. package/server/http/lib/routeRegistration.js +9 -25
  25. package/server/platform/index.js +2 -2
  26. package/server/platform/installedRuntime.js +122 -0
  27. package/server/platform/installedRuntime.test.js +103 -0
  28. package/server/platform/providerRuntime/capabilityProviderLoader.js +100 -0
  29. package/server/platform/providerRuntime/capabilityProviderLoader.test.js +80 -0
  30. package/server/platform/providerRuntime/packageCatalog.js +4 -1
  31. package/server/platform/surfaceRuntime.js +1 -15
  32. package/server/runtime/BootstrapProvider.js +69 -0
  33. package/server/runtime/EventProvider.js +74 -0
  34. package/server/runtime/EventProvider.test.js +32 -0
  35. package/server/runtime/entityChangeEvents.js +7 -7
  36. package/server/runtime/entityChangeEvents.test.js +2 -2
  37. package/server/runtime/fastifyBootstrap.js +0 -40
  38. package/server/runtime/index.js +2 -3
  39. package/server/surface/index.js +0 -1
  40. package/shared/actions/actionDefinitions.js +20 -0
  41. package/shared/actions/pipeline.js +22 -0
  42. package/shared/actions/policies.js +1 -1
  43. package/shared/actions/registry.js +3 -0
  44. package/shared/capabilities/defineProvider.js +99 -0
  45. package/shared/capabilities/index.js +2 -0
  46. package/shared/capabilities/runtime.js +243 -0
  47. package/shared/capabilities/runtime.test.js +170 -0
  48. package/test/barrelExposure.test.js +36 -13
  49. package/test/exportsContract.test.js +8 -3
  50. package/server/actions/ActionRuntimeServiceProvider.js +0 -304
  51. package/server/actions/ActionRuntimeServiceProvider.test.js +0 -558
  52. package/server/container/ContainerCoreServiceProvider.js +0 -27
  53. package/server/container/index.js +0 -10
  54. package/server/http/HttpFastifyServiceProvider.js +0 -25
  55. package/server/http/lib/httpRuntime.js +0 -83
  56. package/server/http/lib/kernel.js +0 -15
  57. package/server/http/lib/kernel.test.js +0 -1565
  58. package/server/http/lib/requestActionExecutor.js +0 -265
  59. package/server/http/lib/requestScope.js +0 -57
  60. package/server/kernel/KernelCoreServiceProvider.js +0 -27
  61. package/server/kernel/index.js +0 -10
  62. package/server/platform/PlatformServerRuntimeServiceProvider.js +0 -30
  63. package/server/platform/providerRuntime/providerLoader.js +0 -283
  64. package/server/platform/providerRuntime.js +0 -146
  65. package/server/platform/providerRuntime.test.js +0 -214
  66. package/server/platform/runtime.js +0 -40
  67. package/server/registries/actionSurfaceSourceRegistry.js +0 -148
  68. package/server/registries/bootstrapPayloadContributorRegistry.js +0 -51
  69. package/server/registries/domainEventListenerRegistry.js +0 -58
  70. package/server/registries/index.js +0 -32
  71. package/server/registries/primitives.js +0 -63
  72. package/server/registries/routeVisibilityResolverRegistry.js +0 -84
  73. package/server/registries/serviceRegistrationRegistry.js +0 -488
  74. package/server/runtime/ServerRuntimeCoreServiceProvider.js +0 -64
  75. package/server/runtime/ServerRuntimeCoreServiceProvider.test.js +0 -53
  76. package/server/runtime/apiRoutePolicyParity.test.js +0 -109
  77. package/server/runtime/apiRouteRegistration.js +0 -65
  78. package/server/runtime/bootBootstrapRoutes.js +0 -29
  79. package/server/runtime/bootBootstrapRoutes.test.js +0 -69
  80. package/server/runtime/bootstrapContributors.test.js +0 -166
  81. package/server/runtime/composition.js +0 -142
  82. package/server/runtime/domainEvents.test.js +0 -114
  83. package/server/runtime/domainRules.js +0 -50
  84. package/server/runtime/domainRules.test.js +0 -87
  85. package/server/runtime/runtimeAssembly.js +0 -113
  86. package/server/runtime/runtimeKernel.js +0 -55
  87. package/server/runtime/serviceRegistration.test.js +0 -251
  88. package/server/support/SupportCoreServiceProvider.js +0 -27
  89. package/server/surface/SurfaceRoutingServiceProvider.js +0 -27
  90. package/shared/actions/index.js +0 -2
  91. package/shared/runtime/application.js +0 -324
  92. package/shared/runtime/container.js +0 -261
  93. package/shared/runtime/containerErrors.js +0 -22
  94. package/shared/runtime/index.js +0 -18
  95. package/shared/runtime/kernelErrors.js +0 -25
  96. package/shared/runtime/kernelErrors.test.js +0 -120
  97. package/shared/runtime/serviceProvider.js +0 -13
  98. package/shared/support/containerToken.js +0 -8
  99. package/shared/support/generatedUiContract.js +0 -542
  100. package/shared/support/generatedUiContract.test.js +0 -218
  101. package/shared/support/tokens.js +0 -1
  102. package/shared/support/tokens.test.js +0 -17
@@ -1,146 +0,0 @@
1
- import { ActionRuntimeServiceProvider } from "../actions/ActionRuntimeServiceProvider.js";
2
- import { ServerRuntimeCoreServiceProvider } from "../runtime/ServerRuntimeCoreServiceProvider.js";
3
- import { createApplication } from "../kernel/index.js";
4
- import { createHttpRuntime } from "../http/lib/kernel.js";
5
- import {
6
- collectGlobalUiPaths,
7
- resolveInstalledJskitPackages,
8
- resolvePackageLoadOrder,
9
- validatePackageCapabilities
10
- } from "./providerRuntime/packageCatalog.js";
11
- import { loadPackageProviders, registerProviderClass } from "./providerRuntime/providerLoader.js";
12
-
13
- const KERNEL_BUILTIN_CAPABILITY_PROVIDERS = Object.freeze({
14
- "runtime.actions": Object.freeze(["@jskit-ai/kernel"])
15
- });
16
-
17
- async function createProviderRuntimeApp({
18
- profile = "",
19
- providers = [],
20
- env = {},
21
- logger = console,
22
- fastify = null
23
- } = {}) {
24
- const app = createApplication({
25
- profile
26
- });
27
-
28
- app.instance("jskit.env", env && typeof env === "object" ? { ...env } : {});
29
- app.instance("jskit.logger", logger || console);
30
-
31
- let httpRuntime = null;
32
- if (fastify && typeof fastify.route === "function") {
33
- httpRuntime = createHttpRuntime({
34
- app,
35
- fastify
36
- });
37
- }
38
-
39
- await app.start({ providers });
40
-
41
- if (fastify && typeof fastify.addHook === "function") {
42
- let didShutdown = false;
43
- fastify.addHook("onClose", async () => {
44
- if (didShutdown) {
45
- return;
46
- }
47
-
48
- didShutdown = true;
49
- await app.shutdown();
50
- });
51
- }
52
-
53
- const routeRegistration = httpRuntime ? httpRuntime.registerRoutes() : { routeCount: 0 };
54
- return Object.freeze({
55
- app,
56
- routeCount: routeRegistration.routeCount,
57
- routeRegistration,
58
- diagnostics: app.getDiagnostics()
59
- });
60
- }
61
-
62
- async function createProviderRuntimeFromApp({
63
- appRoot,
64
- profile = "",
65
- env = {},
66
- logger = console,
67
- fastify = null
68
- } = {}) {
69
- if (!appRoot || typeof appRoot !== "string") {
70
- throw new TypeError("createProviderRuntimeFromApp requires appRoot.");
71
- }
72
-
73
- const installedPackages = await resolveInstalledJskitPackages({
74
- appRoot
75
- });
76
- validatePackageCapabilities(installedPackages, {
77
- builtinProvidersByCapability: KERNEL_BUILTIN_CAPABILITY_PROVIDERS
78
- });
79
- const orderedPackages = resolvePackageLoadOrder(installedPackages);
80
- const catalog = Object.freeze({
81
- packageOrder: Object.freeze(orderedPackages.map((entry) => entry.packageId)),
82
- globalUiPaths: collectGlobalUiPaths(orderedPackages),
83
- packages: Object.freeze(orderedPackages)
84
- });
85
-
86
- const orderedProviderClasses = [];
87
- const providerPackageIds = [];
88
- const seenProviderIds = new Map();
89
-
90
- for (const packageEntry of catalog.packages) {
91
- const packageProviders = await loadPackageProviders({ packageEntry });
92
- if (packageProviders.length < 1) {
93
- continue;
94
- }
95
- providerPackageIds.push(packageEntry.packageId);
96
-
97
- for (const providerClass of packageProviders) {
98
- registerProviderClass({
99
- providerClass,
100
- sourceId: packageEntry.packageId,
101
- seenProviderIds,
102
- orderedProviderClasses
103
- });
104
- }
105
- }
106
-
107
- if (!seenProviderIds.has(ActionRuntimeServiceProvider.id)) {
108
- registerProviderClass({
109
- providerClass: ActionRuntimeServiceProvider,
110
- sourceId: "@jskit-ai/kernel",
111
- seenProviderIds,
112
- orderedProviderClasses
113
- });
114
- }
115
-
116
- if (!seenProviderIds.has(ServerRuntimeCoreServiceProvider.id)) {
117
- registerProviderClass({
118
- providerClass: ServerRuntimeCoreServiceProvider,
119
- sourceId: "@jskit-ai/kernel",
120
- seenProviderIds,
121
- orderedProviderClasses
122
- });
123
- }
124
-
125
- const envRecord = env && typeof env === "object" ? { ...env } : {};
126
- const loggerInstance = logger || console;
127
-
128
- const providerRuntime = await createProviderRuntimeApp({
129
- profile,
130
- providers: orderedProviderClasses,
131
- env: envRecord,
132
- logger: loggerInstance,
133
- fastify
134
- });
135
-
136
- return Object.freeze({
137
- ...providerRuntime,
138
- routeCount: providerRuntime.routeCount,
139
- packageOrder: catalog.packageOrder,
140
- globalUiPaths: catalog.globalUiPaths,
141
- providerPackageOrder: Object.freeze(providerPackageIds),
142
- appLocalProviderOrder: Object.freeze([])
143
- });
144
- }
145
-
146
- export { createProviderRuntimeApp, createProviderRuntimeFromApp };
@@ -1,214 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import path from "node:path";
5
- import test from "node:test";
6
-
7
- import { createProviderRuntimeFromApp } from "./providerRuntime.js";
8
-
9
- async function createTestAppRoot(prefix) {
10
- const appRoot = await mkdtemp(path.join(tmpdir(), prefix));
11
- await writeFile(
12
- path.join(appRoot, "package.json"),
13
- `${JSON.stringify({ name: "fixture-app", private: true, type: "module" }, null, 2)}\n`,
14
- "utf8"
15
- );
16
- return appRoot;
17
- }
18
-
19
- async function declareLocalPackage(appRoot, packageId, packagePath, jskit = {}) {
20
- await writeFile(
21
- path.join(appRoot, "package.json"),
22
- `${JSON.stringify({
23
- name: "fixture-app",
24
- private: true,
25
- type: "module",
26
- dependencies: {
27
- [packageId]: `file:${packagePath}`
28
- }
29
- }, null, 2)}\n`,
30
- "utf8"
31
- );
32
- const packageRoot = path.join(appRoot, packagePath);
33
- await mkdir(packageRoot, { recursive: true });
34
- await writeFile(
35
- path.join(packageRoot, "package.json"),
36
- `${JSON.stringify({
37
- name: packageId,
38
- version: "0.1.0",
39
- description: "Local example package",
40
- type: "module",
41
- jskit
42
- }, null, 2)}\n`,
43
- "utf8"
44
- );
45
- }
46
-
47
- test("createProviderRuntimeFromApp discovers package providers from package metadata", async () => {
48
- const appRoot = await createTestAppRoot("kernel-provider-runtime-discover-");
49
- try {
50
- await mkdir(path.join(appRoot, "packages", "local-example", "src", "server", "providers"), { recursive: true });
51
- await declareLocalPackage(appRoot, "@local/example", "packages/local-example", {
52
- capabilities: { provides: [], requires: [] },
53
- runtime: {
54
- server: {
55
- providers: [
56
- { discover: { dir: "src/server/providers", pattern: "*Provider.js" } }
57
- ]
58
- }
59
- }
60
- });
61
- await writeFile(
62
- path.join(appRoot, "packages", "local-example", "src", "server", "providers", "AlphaProvider.js"),
63
- [
64
- "export default class AlphaProvider {",
65
- " static id = \"example.alpha\";",
66
- " register(app) {",
67
- " app.instance(\"example.alpha.value\", 42);",
68
- " }",
69
- " boot() {}",
70
- "}"
71
- ].join("\n"),
72
- "utf8"
73
- );
74
- await writeFile(
75
- path.join(appRoot, "packages", "local-example", "src", "server", "providers", "ignored.js"),
76
- "export const value = 1;\n",
77
- "utf8"
78
- );
79
-
80
- const runtime = await createProviderRuntimeFromApp({
81
- appRoot,
82
- profile: "app"
83
- });
84
-
85
- assert.deepEqual(runtime.packageOrder, ["@local/example"]);
86
- assert.deepEqual(runtime.providerPackageOrder, ["@local/example"]);
87
- assert.equal(runtime.appLocalProviderOrder.length, 0);
88
- assert.deepEqual(runtime.diagnostics.providerOrder, ["example.alpha", "runtime.actions", "runtime.server"]);
89
- assert.equal(runtime.app.make("example.alpha.value"), 42);
90
- assert.equal(typeof runtime.app.make("actionExecutor")?.execute, "function");
91
- } finally {
92
- await rm(appRoot, { recursive: true, force: true });
93
- }
94
- });
95
-
96
- test("createProviderRuntimeFromApp ignores app-local src/server/providers folder", async () => {
97
- const appRoot = await createTestAppRoot("kernel-provider-runtime-app-local-");
98
- try {
99
- await mkdir(path.join(appRoot, "src", "server", "providers"), { recursive: true });
100
- await writeFile(
101
- path.join(appRoot, "src", "server", "providers", "IgnoredProvider.js"),
102
- [
103
- "export default class IgnoredProvider {",
104
- " static id = \"ignored.app.local\";",
105
- " register(app) {",
106
- " app.instance(\"ignored.value\", true);",
107
- " }",
108
- " boot() {}",
109
- "}"
110
- ].join("\n"),
111
- "utf8"
112
- );
113
-
114
- const runtime = await createProviderRuntimeFromApp({
115
- appRoot,
116
- profile: "app"
117
- });
118
-
119
- assert.deepEqual(runtime.packageOrder, []);
120
- assert.deepEqual(runtime.providerPackageOrder, []);
121
- assert.equal(runtime.appLocalProviderOrder.length, 0);
122
- assert.deepEqual(runtime.diagnostics.providerOrder, ["runtime.actions", "runtime.server"]);
123
- assert.equal(runtime.app.has("ignored.value"), false);
124
- } finally {
125
- await rm(appRoot, { recursive: true, force: true });
126
- }
127
- });
128
-
129
- test("createProviderRuntimeFromApp resolves package metadata from a file dependency", async () => {
130
- const appRoot = await createTestAppRoot("kernel-provider-runtime-local-package-");
131
- try {
132
- await mkdir(path.join(appRoot, "packages", "local-example"), { recursive: true });
133
- await declareLocalPackage(appRoot, "@local/example", "packages/local-example", {
134
- capabilities: { provides: [], requires: [] },
135
- runtime: { server: { providers: [] } }
136
- });
137
-
138
- const runtime = await createProviderRuntimeFromApp({
139
- appRoot,
140
- profile: "app"
141
- });
142
-
143
- assert.deepEqual(runtime.packageOrder, ["@local/example"]);
144
- assert.deepEqual(runtime.providerPackageOrder, []);
145
- } finally {
146
- await rm(appRoot, { recursive: true, force: true });
147
- }
148
- });
149
-
150
- test("createProviderRuntimeFromApp wires fastify onClose to provider shutdown exactly once", async () => {
151
- const appRoot = await createTestAppRoot("kernel-provider-runtime-fastify-close-");
152
- try {
153
- await mkdir(path.join(appRoot, "packages", "local-example", "src", "server", "providers"), { recursive: true });
154
- await declareLocalPackage(appRoot, "@local/example", "packages/local-example", {
155
- capabilities: { provides: [], requires: [] },
156
- runtime: {
157
- server: {
158
- providers: [
159
- { discover: { dir: "src/server/providers", pattern: "*Provider.js" } }
160
- ]
161
- }
162
- }
163
- });
164
- await writeFile(
165
- path.join(appRoot, "packages", "local-example", "src", "server", "providers", "CloseAwareProvider.js"),
166
- [
167
- "export default class CloseAwareProvider {",
168
- " static id = \"example.close-aware\";",
169
- " register(app) {",
170
- " app.instance(\"example.close.state\", { shutdownCalls: 0 });",
171
- " }",
172
- " async boot() {}",
173
- " async shutdown(app) {",
174
- " app.make(\"example.close.state\").shutdownCalls += 1;",
175
- " }",
176
- "}"
177
- ].join("\n"),
178
- "utf8"
179
- );
180
-
181
- const hooks = [];
182
- const fastify = {
183
- route() {},
184
- setErrorHandler() {},
185
- addContentTypeParser() {},
186
- hasContentTypeParser() {
187
- return false;
188
- },
189
- getDefaultJsonParser() {
190
- return (_request, body, done) => done(null, body);
191
- },
192
- addHook(name, handler) {
193
- hooks.push({ name, handler });
194
- }
195
- };
196
-
197
- const runtime = await createProviderRuntimeFromApp({
198
- appRoot,
199
- profile: "app",
200
- fastify
201
- });
202
-
203
- const closeHook = hooks.find((entry) => entry.name === "onClose");
204
- assert.ok(closeHook);
205
- assert.equal(runtime.app.make("example.close.state").shutdownCalls, 0);
206
-
207
- await closeHook.handler();
208
- await closeHook.handler();
209
-
210
- assert.equal(runtime.app.make("example.close.state").shutdownCalls, 1);
211
- } finally {
212
- await rm(appRoot, { recursive: true, force: true });
213
- }
214
- });
@@ -1,40 +0,0 @@
1
- import { createRuntimeAssembly } from "../runtime/runtimeAssembly.js";
2
-
3
- function createPlatformRuntimeBundle({
4
- repositoryDefinitions = [],
5
- serviceDefinitions = [],
6
- controllerDefinitions = [],
7
- runtimeServiceIds = []
8
- } = {}) {
9
- return Object.freeze({
10
- repositoryDefinitions,
11
- serviceDefinitions,
12
- controllerDefinitions,
13
- runtimeServiceIds
14
- });
15
- }
16
-
17
- function createServerRuntime({ bundles = [], dependencies = {} } = {}) {
18
- return createRuntimeAssembly({
19
- bundles,
20
- dependencies
21
- });
22
- }
23
-
24
- function createServerRuntimeWithPlatformBundle({
25
- platformBundle,
26
- appFeatureBundle,
27
- dependencies = {}
28
- } = {}) {
29
- if (!platformBundle || typeof platformBundle !== "object") {
30
- throw new Error("platformBundle is required.");
31
- }
32
-
33
- const bundles = appFeatureBundle ? [platformBundle, appFeatureBundle] : [platformBundle];
34
- return createServerRuntime({
35
- bundles,
36
- dependencies
37
- });
38
- }
39
-
40
- export { createPlatformRuntimeBundle, createServerRuntime, createServerRuntimeWithPlatformBundle };
@@ -1,148 +0,0 @@
1
- import { assertTaggableApp } from "./primitives.js";
2
-
3
- const ACTION_SURFACE_SOURCE_NAME_PATTERN = /^[a-z][a-z0-9_.-]*$/;
4
-
5
- function normalizeSurfaceIdValue(value) {
6
- return String(value || "").trim().toLowerCase();
7
- }
8
-
9
- function normalizeSurfaceSourceName(sourceName, { context = "action surface source" } = {}) {
10
- const normalizedSourceName = normalizeSurfaceIdValue(sourceName);
11
- if (!ACTION_SURFACE_SOURCE_NAME_PATTERN.test(normalizedSourceName)) {
12
- throw new Error(`${context} must match ${ACTION_SURFACE_SOURCE_NAME_PATTERN.toString()}.`);
13
- }
14
-
15
- return normalizedSourceName;
16
- }
17
-
18
- function resolveSurfaceRuntime(scope) {
19
- if (!scope || typeof scope.has !== "function" || typeof scope.make !== "function") {
20
- throw new Error("Action definition materialization requires scope.has()/make().");
21
- }
22
-
23
- if (!scope.has("jskit.surface.runtime")) {
24
- throw new Error("Action definition surfacesFrom requires jskit.surface.runtime.");
25
- }
26
-
27
- return scope.make("jskit.surface.runtime");
28
- }
29
-
30
- function resolveEnabledSurfaceIds(surfaceRuntime) {
31
- return Array.isArray(surfaceRuntime?.listEnabledSurfaceIds?.()) ? surfaceRuntime.listEnabledSurfaceIds() : [];
32
- }
33
-
34
- function normalizeSurfaceIdList(surfaceIds, { context = "action.surfacesFrom", surfaceRuntime = null } = {}) {
35
- const sourceSurfaceIds = Array.isArray(surfaceIds) ? surfaceIds : [];
36
- const enabledSurfaceIds = new Set(
37
- resolveEnabledSurfaceIds(surfaceRuntime).map((entry) => normalizeSurfaceIdValue(entry)).filter(Boolean)
38
- );
39
- const seen = new Set();
40
- const normalized = [];
41
-
42
- for (const sourceSurfaceId of sourceSurfaceIds) {
43
- const normalizedSurfaceId = normalizeSurfaceIdValue(sourceSurfaceId);
44
- if (!normalizedSurfaceId || seen.has(normalizedSurfaceId)) {
45
- continue;
46
- }
47
-
48
- if (!enabledSurfaceIds.has(normalizedSurfaceId)) {
49
- throw new Error(`${context} resolved non-enabled surface "${normalizedSurfaceId}".`);
50
- }
51
-
52
- seen.add(normalizedSurfaceId);
53
- normalized.push(normalizedSurfaceId);
54
- }
55
-
56
- return Object.freeze(normalized);
57
- }
58
-
59
- function createActionSurfaceSourceRegistry() {
60
- const sourceResolvers = new Map();
61
-
62
- const register = (sourceName, resolver, { allowOverride = false } = {}) => {
63
- const normalizedSourceName = normalizeSurfaceSourceName(sourceName);
64
- if (typeof resolver !== "function") {
65
- throw new Error(`action surface source "${normalizedSourceName}" resolver must be a function.`);
66
- }
67
- if (!allowOverride && sourceResolvers.has(normalizedSourceName)) {
68
- throw new Error(`action surface source "${normalizedSourceName}" is already registered.`);
69
- }
70
- sourceResolvers.set(normalizedSourceName, resolver);
71
- };
72
-
73
- register("enabled", ({ surfaceRuntime }) => resolveEnabledSurfaceIds(surfaceRuntime));
74
-
75
- return Object.freeze({
76
- register,
77
- resolve(scope, sourceName, { context = "action.surfacesFrom" } = {}) {
78
- const normalizedSourceName = normalizeSurfaceSourceName(sourceName, { context });
79
- const resolver = sourceResolvers.get(normalizedSourceName);
80
- if (typeof resolver !== "function") {
81
- throw new Error(
82
- `${context} references unknown surface source "${normalizedSourceName}". Register it via app.actionSurfaceSource().`
83
- );
84
- }
85
-
86
- const surfaceRuntime = resolveSurfaceRuntime(scope);
87
- const resolvedSurfaceIds = resolver({
88
- scope,
89
- sourceName: normalizedSourceName,
90
- surfaceRuntime
91
- });
92
-
93
- return normalizeSurfaceIdList(resolvedSurfaceIds, {
94
- context,
95
- surfaceRuntime
96
- });
97
- }
98
- });
99
- }
100
-
101
- function resolveActionSurfaceSourceRegistry(scope) {
102
- if (!scope || typeof scope.has !== "function" || typeof scope.make !== "function") {
103
- throw new Error("Action surface source resolution requires scope.has()/make().");
104
- }
105
- if (!scope.has("jskit.runtime.actions.surfaceSourceRegistry")) {
106
- throw new Error("Action surface source registry is not registered.");
107
- }
108
- return scope.make("jskit.runtime.actions.surfaceSourceRegistry");
109
- }
110
-
111
- function resolveActionSurfaceSourceIds(scope, sourceName, { context = "action.surfacesFrom" } = {}) {
112
- const sourceRegistry = resolveActionSurfaceSourceRegistry(scope);
113
- return sourceRegistry.resolve(scope, sourceName, { context });
114
- }
115
-
116
- function installActionSurfaceSourceRegistrationApi(app) {
117
- if (typeof app.actionSurfaceSource === "function") {
118
- return;
119
- }
120
-
121
- const registerActionSurfaceSource = function registerActionSurfaceSource(sourceName, resolver) {
122
- const sourceRegistry = resolveActionSurfaceSourceRegistry(this);
123
- sourceRegistry.register(sourceName, resolver);
124
- return this;
125
- };
126
-
127
- Object.defineProperty(app, "actionSurfaceSource", {
128
- configurable: true,
129
- writable: true,
130
- value: registerActionSurfaceSource
131
- });
132
- }
133
-
134
- function ensureActionSurfaceSourceRegistry(app) {
135
- assertTaggableApp(app, {
136
- context: "ensureActionSurfaceSourceRegistry"
137
- });
138
- if (typeof app.has !== "function") {
139
- throw new Error("ensureActionSurfaceSourceRegistry requires app.has().");
140
- }
141
-
142
- if (!app.has("jskit.runtime.actions.surfaceSourceRegistry")) {
143
- app.singleton("jskit.runtime.actions.surfaceSourceRegistry", () => createActionSurfaceSourceRegistry());
144
- }
145
- installActionSurfaceSourceRegistrationApi(app);
146
- }
147
-
148
- export { ensureActionSurfaceSourceRegistry, resolveActionSurfaceSourceIds };
@@ -1,51 +0,0 @@
1
- import { normalizeObject } from "../../shared/support/normalize.js";
2
- import { normalizeContributorEntry, registerTaggedSingleton, resolveTaggedEntries } from "./primitives.js";
3
-
4
- function registerBootstrapPayloadContributor(app, token, factory) {
5
- registerTaggedSingleton(app, token, factory, "jskit.runtime.bootstrap.payloadContributors", {
6
- context: "registerBootstrapPayloadContributor"
7
- });
8
- }
9
-
10
- function resolveBootstrapPayloadContributors(scope) {
11
- return resolveTaggedEntries(scope, "jskit.runtime.bootstrap.payloadContributors")
12
- .map((entry, index) => ({
13
- contributor: normalizeContributorEntry(entry),
14
- index
15
- }))
16
- .filter((entry) => Boolean(entry.contributor))
17
- .sort((left, right) => {
18
- const leftOrder = Number.isFinite(left.contributor?.order) ? Number(left.contributor.order) : 0;
19
- const rightOrder = Number.isFinite(right.contributor?.order) ? Number(right.contributor.order) : 0;
20
- if (leftOrder !== rightOrder) {
21
- return leftOrder - rightOrder;
22
- }
23
-
24
- return left.index - right.index;
25
- })
26
- .map((entry) => entry.contributor);
27
- }
28
-
29
- async function resolveBootstrapPayload(scope, context = {}) {
30
- const contributors = resolveBootstrapPayloadContributors(scope);
31
- let payload = {};
32
-
33
- for (const contributor of contributors) {
34
- const contribution = await contributor.contribute({
35
- ...normalizeObject(context),
36
- payload: Object.freeze({ ...payload })
37
- });
38
- payload = {
39
- ...payload,
40
- ...normalizeObject(contribution)
41
- };
42
- }
43
-
44
- return payload;
45
- }
46
-
47
- export {
48
- registerBootstrapPayloadContributor,
49
- resolveBootstrapPayloadContributors,
50
- resolveBootstrapPayload
51
- };
@@ -1,58 +0,0 @@
1
- import { normalizeObject } from "../../shared/support/normalize.js";
2
- import { registerTaggedSingleton, resolveTaggedEntries } from "./primitives.js";
3
-
4
- function normalizeDomainEventListener(entry) {
5
- if (typeof entry === "function") {
6
- return {
7
- listenerId: String(entry.name || "anonymous"),
8
- matches: null,
9
- handle: entry
10
- };
11
- }
12
-
13
- if (entry && typeof entry === "object" && typeof entry.handle === "function") {
14
- return {
15
- ...entry,
16
- listenerId: String(entry.listenerId || "anonymous"),
17
- matches: typeof entry.matches === "function" ? entry.matches : null
18
- };
19
- }
20
-
21
- return null;
22
- }
23
-
24
- function registerDomainEventListener(app, token, factory) {
25
- registerTaggedSingleton(app, token, factory, "jskit.runtime.domainEvent.listeners", {
26
- context: "registerDomainEventListener"
27
- });
28
- }
29
-
30
- function resolveDomainEventListeners(scope) {
31
- return resolveTaggedEntries(scope, "jskit.runtime.domainEvent.listeners")
32
- .map((entry) => normalizeDomainEventListener(entry))
33
- .filter(Boolean);
34
- }
35
-
36
- function createDomainEvents(scope) {
37
- return Object.freeze({
38
- async publish(event = {}) {
39
- const payload = normalizeObject(event);
40
- const listeners = resolveDomainEventListeners(scope);
41
-
42
- for (const listener of listeners) {
43
- if (listener.matches && listener.matches(payload) !== true) {
44
- continue;
45
- }
46
- await listener.handle(payload);
47
- }
48
-
49
- return null;
50
- }
51
- });
52
- }
53
-
54
- export {
55
- registerDomainEventListener,
56
- resolveDomainEventListeners,
57
- createDomainEvents
58
- };
@@ -1,32 +0,0 @@
1
- export {
2
- registerBootstrapPayloadContributor,
3
- resolveBootstrapPayloadContributors,
4
- resolveBootstrapPayload
5
- } from "./bootstrapPayloadContributorRegistry.js";
6
- export {
7
- registerDomainEventListener,
8
- resolveDomainEventListeners,
9
- createDomainEvents
10
- } from "./domainEventListenerRegistry.js";
11
- export {
12
- normalizeServiceRegistration,
13
- materializeServiceRegistration,
14
- registerServiceRegistration,
15
- resolveServiceRegistrations,
16
- installServiceRegistrationApi
17
- } from "./serviceRegistrationRegistry.js";
18
- export {
19
- resolveRouteVisibilityResolvers,
20
- registerRouteVisibilityResolver,
21
- resolveRouteVisibilityContext
22
- } from "./routeVisibilityResolverRegistry.js";
23
- export {
24
- ensureActionSurfaceSourceRegistry,
25
- resolveActionSurfaceSourceIds
26
- } from "./actionSurfaceSourceRegistry.js";
27
- export {
28
- normalizeNestedEntries,
29
- assertTaggableApp,
30
- registerTaggedSingleton,
31
- resolveTaggedEntries
32
- } from "./primitives.js";