@ankhorage/projects 0.4.0 → 0.6.0

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
@@ -3,7 +3,7 @@
3
3
 
4
4
  # @ankhorage/projects
5
5
 
6
- ![license: MIT](././paradox/badges/license.svg) ![npm: v0.4.0](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: configured](././paradox/badges/tests.svg) ![paradox: canonical](././paradox/badges/docs.svg)
6
+ ![license: MIT](././paradox/badges/license.svg) ![npm: v0.6.0](././paradox/badges/npm.svg) ![runtime: bun](././paradox/badges/runtime.svg) ![typescript: strict](././paradox/badges/typescript.svg) ![eslint: checked](././paradox/badges/eslint.svg) ![prettier: checked](././paradox/badges/prettier.svg) ![build: checked](././paradox/badges/build.svg) ![tests: configured](././paradox/badges/tests.svg) ![paradox: canonical](././paradox/badges/docs.svg)
7
7
 
8
8
  Standalone project identity and repository actions.
9
9
 
@@ -16,3 +16,4 @@ Standalone project identity and repository actions.
16
16
  - [Module relationships](././paradox/diagrams/module-relationships.mmd)
17
17
  - [Export graph](././paradox/diagrams/export-graph.mmd)
18
18
  - [createProjectRepositoryFileSystemAsync sequence](././paradox/diagrams/sequences/create-project-repository-file-system-async.mmd)
19
+ - [createTrustedZefixHost sequence](././paradox/diagrams/sequences/create-trusted-zefix-host.mmd)
@@ -89,4 +89,24 @@ export declare const CAPABILITIES: readonly [{
89
89
  };
90
90
  readonly label: "Delete project";
91
91
  readonly description: "Delete one persisted project by identity.";
92
+ }, {
93
+ readonly id: "projects.zefix.search";
94
+ readonly owner: "@ankhorage/projects";
95
+ readonly access: readonly ["read"];
96
+ readonly binding: {
97
+ readonly kind: "action";
98
+ readonly bindableAs: readonly ["target"];
99
+ };
100
+ readonly label: "Search Zefix companies";
101
+ readonly description: "Search curated Zefix company data through a trusted project host.";
102
+ }, {
103
+ readonly id: "projects.zefix.get-by-uid";
104
+ readonly owner: "@ankhorage/projects";
105
+ readonly access: readonly ["read"];
106
+ readonly binding: {
107
+ readonly kind: "action";
108
+ readonly bindableAs: readonly ["target"];
109
+ };
110
+ readonly label: "Read a Zefix company by UID";
111
+ readonly description: "Read curated Zefix company data through a trusted project host.";
92
112
  }];
@@ -72,4 +72,20 @@ export const CAPABILITIES = [
72
72
  label: 'Delete project',
73
73
  description: 'Delete one persisted project by identity.',
74
74
  },
75
+ {
76
+ id: 'projects.zefix.search',
77
+ owner: '@ankhorage/projects',
78
+ access: ['read'],
79
+ binding: { kind: 'action', bindableAs: ['target'] },
80
+ label: 'Search Zefix companies',
81
+ description: 'Search curated Zefix company data through a trusted project host.',
82
+ },
83
+ {
84
+ id: 'projects.zefix.get-by-uid',
85
+ owner: '@ankhorage/projects',
86
+ access: ['read'],
87
+ binding: { kind: 'action', bindableAs: ['target'] },
88
+ label: 'Read a Zefix company by UID',
89
+ description: 'Read curated Zefix company data through a trusted project host.',
90
+ },
75
91
  ];
@@ -0,0 +1,11 @@
1
+ import type { NavigatorGenerationBindings } from '@ankhorage/contracts/navigator';
2
+ /*** Compose generated Runtime dependencies and bindings with caller-owned Navigator extensions. */
3
+ export declare function composeGeneratedRuntimeMaterialization(input: Readonly<{
4
+ readonly callerBindings: NavigatorGenerationBindings;
5
+ readonly packageSource: string;
6
+ readonly runtimeBindings: NavigatorGenerationBindings;
7
+ readonly runtimeDependencies: Readonly<Record<string, string>>;
8
+ }>): Readonly<{
9
+ navigatorBindings: NavigatorGenerationBindings;
10
+ packageSource: string;
11
+ }>;
@@ -0,0 +1,27 @@
1
+ /*** Compose generated Runtime dependencies and bindings with caller-owned Navigator extensions. */
2
+ export function composeGeneratedRuntimeMaterialization(input) {
3
+ assertNoConflictingRuntimeBindings(input.callerBindings, input.runtimeBindings);
4
+ const packageJson = JSON.parse(input.packageSource);
5
+ return {
6
+ navigatorBindings: {
7
+ guards: { ...input.callerBindings.guards, ...input.runtimeBindings.guards },
8
+ screens: { ...input.callerBindings.screens, ...input.runtimeBindings.screens },
9
+ },
10
+ packageSource: `${JSON.stringify({
11
+ ...packageJson,
12
+ dependencies: { ...packageJson.dependencies, ...input.runtimeDependencies },
13
+ }, null, 2)}\n`,
14
+ };
15
+ }
16
+ /*** Reject a caller attempt to replace an immutable Runtime-owned manifest screen binding. */
17
+ function assertNoConflictingRuntimeBindings(callerBindings, runtimeBindings) {
18
+ for (const [screenId, runtimeBinding] of Object.entries(runtimeBindings.screens)) {
19
+ const callerEntry = Object.entries(callerBindings.screens).find(([id]) => id === screenId);
20
+ const callerBinding = callerEntry?.[1];
21
+ if (callerBinding !== undefined &&
22
+ (callerBinding.module !== runtimeBinding.module ||
23
+ callerBinding.exportName !== runtimeBinding.exportName)) {
24
+ throw new Error(`Navigator binding conflicts with generated Runtime screen '${screenId}'.`);
25
+ }
26
+ }
27
+ }
@@ -1,9 +1,10 @@
1
1
  import { lstat, mkdir, mkdtemp, readdir, readFile, realpath, rm, writeFile, } from 'node:fs/promises';
2
2
  import { basename, dirname, relative, resolve } from 'node:path';
3
- import { resolveExpoRuntimeGeneratedAppScaffold } from '@ankhorage/expo-runtime/generated-app';
3
+ import { resolveExpoRuntimeGeneratedAppScaffold, resolveExpoRuntimeGeneratedAppSources, } from '@ankhorage/expo-runtime/generated-app';
4
4
  import { resolveExpoRuntimePlan } from '@ankhorage/expo-runtime/planning';
5
5
  import { createNavigatorPlan, generateNavigator } from '@ankhorage/navigator';
6
6
  import { materializeTemplateRevision } from '@ankhorage/templates';
7
+ import { composeGeneratedRuntimeMaterialization } from './composeGeneratedRuntimeMaterialization.js';
7
8
  /*** Create the public, caller-targeted Expo materializer for stored immutable project revisions. */
8
9
  export function createExpoProjectMaterializer(input) {
9
10
  return {
@@ -36,18 +37,37 @@ async function materializeExpoProjectAsync(input, project, revision) {
36
37
  function createGeneratedFiles(manifest, navigatorInput) {
37
38
  const runtimePlan = ownerApis.resolveExpoRuntimePlan(manifest);
38
39
  assertNoErrors(runtimePlan.diagnostics, 'Expo Runtime');
40
+ const runtime = ownerApis.resolveExpoRuntimeGeneratedAppSources(manifest, runtimePlan, {
41
+ manifestModuleSpecifier: '../ankh.manifest',
42
+ screenModuleSpecifier: '../generated/runtimeManifestScreens',
43
+ });
44
+ assertNoErrors(runtime.diagnostics, 'Expo Runtime');
45
+ const scaffold = ownerApis.resolveExpoRuntimeGeneratedAppScaffold(manifest, runtimePlan);
46
+ const packageSource = composeGeneratedRuntimeMaterialization({
47
+ callerBindings: navigatorInput.bindings,
48
+ packageSource: scaffold.files.find(({ relativePath }) => relativePath === 'package.json')?.source ?? '',
49
+ runtimeBindings: runtime.navigatorBindings,
50
+ runtimeDependencies: runtime.dependencies,
51
+ });
39
52
  const navigatorPlan = ownerApis.createNavigatorPlan(manifest.navigator, {
40
53
  platform: navigatorInput.platform,
41
54
  expoRouterVersion: navigatorInput.expoRouterVersion,
42
55
  });
43
- const navigator = ownerApis.generateNavigator(navigatorPlan, navigatorInput.bindings);
56
+ const navigator = ownerApis.generateNavigator(navigatorPlan, packageSource.navigatorBindings);
44
57
  assertNoErrors(navigator.diagnostics, 'Navigator');
45
- const runtime = ownerApis.resolveExpoRuntimeGeneratedAppScaffold(manifest, runtimePlan);
46
58
  const files = [
59
+ ...scaffold.files.map((file) => ({
60
+ relativePath: file.relativePath,
61
+ source: file.relativePath === 'package.json' ? packageSource.packageSource : file.source,
62
+ })),
47
63
  ...runtime.files.map((file) => ({
48
64
  relativePath: file.relativePath,
49
65
  source: file.source,
50
66
  })),
67
+ {
68
+ relativePath: 'src/ankh.manifest.ts',
69
+ source: `export default ${JSON.stringify(manifest, null, 2)} as const;\n`,
70
+ },
51
71
  ...navigator.files.map((file) => ({
52
72
  relativePath: file.path,
53
73
  source: file.contents,
@@ -73,6 +93,7 @@ const ownerApis = {
73
93
  createNavigatorPlan: createNavigatorPlan,
74
94
  generateNavigator: generateNavigator,
75
95
  resolveExpoRuntimePlan: resolveExpoRuntimePlan,
96
+ resolveExpoRuntimeGeneratedAppSources: resolveExpoRuntimeGeneratedAppSources,
76
97
  resolveExpoRuntimeGeneratedAppScaffold: resolveExpoRuntimeGeneratedAppScaffold,
77
98
  };
78
99
  /*** Reject unavailable owner emitters rather than reproducing their source in Projects. */
@@ -81,6 +102,7 @@ function assertReleasedOwnerApis() {
81
102
  typeof ownerApis.createNavigatorPlan !== 'function' ||
82
103
  typeof ownerApis.generateNavigator !== 'function' ||
83
104
  typeof ownerApis.resolveExpoRuntimePlan !== 'function' ||
105
+ typeof ownerApis.resolveExpoRuntimeGeneratedAppSources !== 'function' ||
84
106
  typeof ownerApis.resolveExpoRuntimeGeneratedAppScaffold !== 'function') {
85
107
  throw new Error('Required released owner materialization API is unavailable.');
86
108
  }
@@ -0,0 +1,30 @@
1
+ import { type ApiRequest, type ComposeAuthorizedApiRuntimeOptions } from '@ankhorage/api';
2
+ import { type FastifyInstance, type RegisterFastifyApiOptions } from '@ankhorage/api-fastify';
3
+ import type { InternalRestApiDefinition } from '@ankhorage/contracts/data';
4
+ import { type EndpointTestFetch } from '@ankhorage/data-sources';
5
+ import type { SecretStoreAdapter } from '@ankhorage/secrets/port';
6
+ type TrustedZefixOperation = 'company.search' | 'company.get-by-uid';
7
+ export type CreateTrustedZefixHostInput = Readonly<{
8
+ appId: string;
9
+ projectId: string;
10
+ environment: string;
11
+ definition: InternalRestApiDefinition;
12
+ operations: Readonly<Record<string, TrustedZefixOperation>>;
13
+ authenticateCallerAsync: (request: ApiRequest) => Promise<unknown>;
14
+ authorizeCallerAsync: (input: Readonly<{
15
+ caller: unknown;
16
+ appId: string;
17
+ projectId: string;
18
+ environment: string;
19
+ operationId: string;
20
+ }>) => Promise<boolean>;
21
+ secretStore: SecretStoreAdapter;
22
+ fetch?: EndpointTestFetch;
23
+ }>;
24
+ export type TrustedZefixHost = Readonly<{
25
+ composition: ComposeAuthorizedApiRuntimeOptions;
26
+ registerFastifyAsync: (server: FastifyInstance, options?: RegisterFastifyApiOptions) => Promise<unknown>;
27
+ }>;
28
+ /*** Compose declared Zefix operations into an authenticated, project-scoped trusted host. */
29
+ export declare function createTrustedZefixHost(input: CreateTrustedZefixHostInput): TrustedZefixHost;
30
+ export {};
@@ -0,0 +1,156 @@
1
+ import {} from '@ankhorage/api';
2
+ import { registerAuthorizedFastifyApiAsync, } from '@ankhorage/api-fastify';
3
+ import { createZefixPublicRestApi, mapZefixCompanyResult, } from '@ankhorage/data-sources';
4
+ import { createRuntimeApiCapabilityExecutor } from '@ankhorage/runtime/api-capabilities';
5
+ import { isRecord } from '@ankhorage/utility/object';
6
+ import { CAPABILITIES } from '../../../capabilities/index.js';
7
+ /*** Compose declared Zefix operations into an authenticated, project-scoped trusted host. */
8
+ export function createTrustedZefixHost(input) {
9
+ assertHostIdentity(input);
10
+ const executor = createRuntimeApiCapabilityExecutor({
11
+ apis: { zefix: createZefixPublicRestApi() },
12
+ credentialResolver: createZefixCredentialResolver(input),
13
+ ...(input.fetch === undefined ? {} : { fetch: input.fetch }),
14
+ });
15
+ const composition = {
16
+ definition: input.definition,
17
+ context: { appId: input.appId, projectId: input.projectId },
18
+ authorizeAsync: async ({ binding, request }) => authorizeRequestAsync(input, binding.operationId, request),
19
+ resolveHandler: ({ binding, declaration, context }) => {
20
+ const operation = input.operations[binding.operationId];
21
+ const capability = resolveCapability(operation);
22
+ const { method } = binding.operation;
23
+ if (!operation ||
24
+ declaration.capability !== capability?.id ||
25
+ binding.operation.intent !== 'read' ||
26
+ method === undefined ||
27
+ !isCanonicalOperationMethod(operation, method))
28
+ return undefined;
29
+ return {
30
+ capability,
31
+ approval: {
32
+ operationId: binding.operationId,
33
+ intent: binding.operation.intent,
34
+ method,
35
+ capabilityId: capability.id,
36
+ owner: capability.owner,
37
+ scope: context,
38
+ },
39
+ handler: async (request) => executeZefixOperationAsync(executor, operation, request),
40
+ };
41
+ },
42
+ };
43
+ return {
44
+ composition,
45
+ registerFastifyAsync: (server, options) => registerAuthorizedFastifyApiAsync(server, composition, options),
46
+ };
47
+ }
48
+ /*** Bind each exposed host declaration to the one canonical Zefix HTTP method. */
49
+ function isCanonicalOperationMethod(operation, method) {
50
+ return ((operation === 'company.search' && method === 'POST') ||
51
+ (operation === 'company.get-by-uid' && method === 'GET'));
52
+ }
53
+ /*** Deny before the operation handler can resolve a Vault secret or contact Zefix. */
54
+ async function authorizeRequestAsync(input, operationId, request) {
55
+ try {
56
+ const caller = await input.authenticateCallerAsync(request);
57
+ return caller === undefined
58
+ ? false
59
+ : input.authorizeCallerAsync({
60
+ caller,
61
+ appId: input.appId,
62
+ projectId: input.projectId,
63
+ environment: input.environment,
64
+ operationId,
65
+ });
66
+ }
67
+ catch {
68
+ return false;
69
+ }
70
+ }
71
+ /*** Resolve Basic credentials only for the canonical Zefix reference and immutable server scope. */
72
+ function createZefixCredentialResolver(input) {
73
+ return async (credential) => {
74
+ if (credential.id !== 'zefix-credentials' || credential.kind !== 'basic')
75
+ return undefined;
76
+ const result = await input.secretStore.resolve({
77
+ scope: { projectId: input.projectId, environment: input.environment },
78
+ ref: 'zefix-credentials',
79
+ });
80
+ const username = result.ok ? result.data.username : undefined;
81
+ const password = result.ok ? result.data.password : undefined;
82
+ return typeof username === 'string' && typeof password === 'string'
83
+ ? { headers: { authorization: `Basic ${btoa(`${username}:${password}`)}` } }
84
+ : undefined;
85
+ };
86
+ }
87
+ /*** Execute only an approved Zefix operation and return the curated company projection. */
88
+ async function executeZefixOperationAsync(executor, operation, request) {
89
+ const input = operation === 'company.search' ? request.body : { id: request.params.id };
90
+ const values = isHostBindingRecord(input) ? input : undefined;
91
+ const result = await executor({
92
+ capability: `api.zefix.${operation}`,
93
+ ...(values === undefined ? {} : { input: values }),
94
+ });
95
+ if (!result.ok)
96
+ return zefixUnavailableResponse();
97
+ const companies = curateZefixResult(result.data);
98
+ return companies === undefined ? zefixUnavailableResponse() : { body: companies };
99
+ }
100
+ /*** Strip provider wire payloads before any Zefix result crosses the trusted host boundary. */
101
+ function curateZefixResult(value) {
102
+ if (!Array.isArray(value))
103
+ return undefined;
104
+ const mapped = value.map((candidate) => {
105
+ const company = asZefixCompany(candidate);
106
+ return company ? mapZefixCompanyResult(company) : undefined;
107
+ });
108
+ return mapped.every((result) => result !== undefined)
109
+ ? mapped.map((result) => result.company)
110
+ : undefined;
111
+ }
112
+ /*** Return the one credential-safe failure representation for Vault or provider failures. */
113
+ function zefixUnavailableResponse() {
114
+ return { status: 502, body: { error: { code: 'zefix_unavailable' } } };
115
+ }
116
+ /*** Narrow unknown endpoint data to the optional-field Zefix short-company DTO. */
117
+ function asZefixCompany(value) {
118
+ return isRecord(value) &&
119
+ optionalString(value.name) &&
120
+ optionalString(value.uid) &&
121
+ optionalString(value.chid) &&
122
+ optionalString(value.legalSeat)
123
+ ? value
124
+ : undefined;
125
+ }
126
+ /*** Accept missing or string wire fields while rejecting non-serializable provider data. */
127
+ function optionalString(value) {
128
+ return value === undefined || typeof value === 'string';
129
+ }
130
+ /*** Admit only serializable operation input before passing it to Runtime's binding executor. */
131
+ function isHostBindingRecord(value) {
132
+ return isRecord(value) && Object.values(value).every(isHostBindingValue);
133
+ }
134
+ /*** Recursively reject host objects and functions from untrusted request input. */
135
+ function isHostBindingValue(value) {
136
+ return (value === null ||
137
+ typeof value === 'string' ||
138
+ typeof value === 'number' ||
139
+ typeof value === 'boolean' ||
140
+ (Array.isArray(value) && value.every(isHostBindingValue)) ||
141
+ (isRecord(value) && Object.values(value).every(isHostBindingValue)));
142
+ }
143
+ /*** Resolve the one Projects-owned capability permitted for each declared Zefix operation. */
144
+ function resolveCapability(operation) {
145
+ const id = operation === 'company.search'
146
+ ? 'projects.zefix.search'
147
+ : operation === 'company.get-by-uid'
148
+ ? 'projects.zefix.get-by-uid'
149
+ : undefined;
150
+ return CAPABILITIES.find((candidate) => candidate.id === id);
151
+ }
152
+ /*** Reject incomplete immutable host configuration before Fastify routes are registered. */
153
+ function assertHostIdentity(input) {
154
+ if (!input.appId.trim() || !input.projectId.trim() || !input.environment.trim())
155
+ throw new Error('Trusted Zefix host requires immutable app, project, and environment identities.');
156
+ }
package/dist/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { projects } from './features/projects/application/projects.js';
3
3
  export { createExpoProjectMaterializer, type CreateExpoProjectMaterializerInput, type ExpoProjectMaterializationResult, type ExpoProjectMaterializer, } from './features/projects/composition/createExpoProjectMaterializer.js';
4
4
  export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
5
5
  export type { ProjectMaterializer, ProjectRepository, } from './features/projects/ports/ProjectRepository.js';
6
+ export { createTrustedZefixHost, type CreateTrustedZefixHostInput, type TrustedZefixHost, } from './features/trusted-zefix-host/composition/createTrustedZefixHost.js';
6
7
  export type { CreateProjectInput, MaterializeProjectRevisionInput, Project, ProjectManifest, ProjectManifestDraft, ProjectManifestRevision, ReloadProjectManifestInput, SaveProjectManifestInput, UpdateProjectInput, } from './types/projects.js';
package/dist/index.js CHANGED
@@ -2,3 +2,4 @@ export { createProjectRepositoryFileSystemAsync } from './features/projects/adap
2
2
  export { projects } from './features/projects/application/projects.js';
3
3
  export { createExpoProjectMaterializer, } from './features/projects/composition/createExpoProjectMaterializer.js';
4
4
  export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
5
+ export { createTrustedZefixHost, } from './features/trusted-zefix-host/composition/createTrustedZefixHost.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ankhorage/projects",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Standalone project identity and repository actions.",
5
5
  "keywords": [
6
6
  "ankhorage",
@@ -62,10 +62,14 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "@ankhorage/api": "^0.5.1",
65
+ "@ankhorage/api-fastify": "^0.4.0",
65
66
  "@ankhorage/contracts": "^26.11.1",
66
- "@ankhorage/expo-runtime": "^7.4.3",
67
+ "@ankhorage/data-sources": "^2.4.0",
68
+ "@ankhorage/expo-runtime": "^7.5.0",
67
69
  "@ankhorage/navigator": "^3.5.22",
68
70
  "@ankhorage/permissions": "^0.2.64",
71
+ "@ankhorage/runtime": "^5.6.4",
72
+ "@ankhorage/secrets": "^0.3.2",
69
73
  "@ankhorage/templates": "^12.6.30",
70
74
  "@ankhorage/utility": "^1.10.0"
71
75
  },
@@ -206,6 +210,36 @@
206
210
  },
207
211
  "label": "Delete project",
208
212
  "description": "Delete one persisted project by identity."
213
+ },
214
+ {
215
+ "id": "projects.zefix.search",
216
+ "owner": "@ankhorage/projects",
217
+ "access": [
218
+ "read"
219
+ ],
220
+ "binding": {
221
+ "kind": "action",
222
+ "bindableAs": [
223
+ "target"
224
+ ]
225
+ },
226
+ "label": "Search Zefix companies",
227
+ "description": "Search curated Zefix company data through a trusted project host."
228
+ },
229
+ {
230
+ "id": "projects.zefix.get-by-uid",
231
+ "owner": "@ankhorage/projects",
232
+ "access": [
233
+ "read"
234
+ ],
235
+ "binding": {
236
+ "kind": "action",
237
+ "bindableAs": [
238
+ "target"
239
+ ]
240
+ },
241
+ "label": "Read a Zefix company by UID",
242
+ "description": "Read curated Zefix company data through a trusted project host."
209
243
  }
210
244
  ]
211
245
  }