@ankhorage/projects 0.7.2 → 0.8.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 +19 -12
- package/dist/capabilities/index.d.ts +330 -0
- package/dist/capabilities/index.js +194 -0
- package/dist/capabilities/socialManagerMediaAnalytics.d.ts +92 -0
- package/dist/capabilities/socialManagerMediaAnalytics.js +75 -0
- package/dist/features/projects/composition/composeGeneratedRuntimeMaterialization.d.ts +1 -0
- package/dist/features/projects/composition/composeGeneratedRuntimeMaterialization.js +13 -5
- package/dist/features/projects/composition/createExpoProjectMaterializer.js +27 -23
- package/dist/features/projects/composition/createRootNavigatorGenerationOptions.d.ts +9 -0
- package/dist/features/projects/composition/createRootNavigatorGenerationOptions.js +10 -0
- package/dist/features/projects/composition/createSocialManagerServerFiles.d.ts +6 -0
- package/dist/features/projects/composition/createSocialManagerServerFiles.js +16 -0
- package/dist/features/trusted-social-manager-host/adapters/createConnectionFileSystemRepositories.d.ts +16 -0
- package/dist/features/trusted-social-manager-host/adapters/createConnectionFileSystemRepositories.js +201 -0
- package/dist/features/trusted-social-manager-host/adapters/createConnectionFileSystemStoreAsync.d.ts +25 -0
- package/dist/features/trusted-social-manager-host/adapters/createConnectionFileSystemStoreAsync.js +72 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerDraftStoreAsync.d.ts +7 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerDraftStoreAsync.js +160 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerFlowSecurityAsync.d.ts +19 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerFlowSecurityAsync.js +176 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerMediaPreflight.d.ts +4 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerMediaPreflight.js +37 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerMediaStoreAsync.d.ts +7 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialManagerMediaStoreAsync.js +242 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialOperationStoresAsync.d.ts +13 -0
- package/dist/features/trusted-social-manager-host/adapters/createSocialOperationStoresAsync.js +244 -0
- package/dist/features/trusted-social-manager-host/adapters/projectSocialManagerConnection.d.ts +53 -0
- package/dist/features/trusted-social-manager-host/adapters/projectSocialManagerConnection.js +42 -0
- package/dist/features/trusted-social-manager-host/application/createSocialManagerPublicationRuntime.d.ts +10 -0
- package/dist/features/trusted-social-manager-host/application/createSocialManagerPublicationRuntime.js +240 -0
- package/dist/features/trusted-social-manager-host/application/resolveSocialManagerPublication.d.ts +22 -0
- package/dist/features/trusted-social-manager-host/application/resolveSocialManagerPublication.js +97 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerCapabilityOperations.d.ts +22 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerCapabilityOperations.js +224 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerConnectionOperations.d.ts +20 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerConnectionOperations.js +152 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerLocalProviderFixtures.d.ts +22 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerLocalProviderFixtures.js +271 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerMediaOperations.d.ts +14 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerMediaOperations.js +72 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerProvider.d.ts +20 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerProvider.js +20 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerPublicationOperations.d.ts +15 -0
- package/dist/features/trusted-social-manager-host/composition/createSocialManagerPublicationOperations.js +185 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerFileSystemPersistenceAsync.d.ts +14 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerFileSystemPersistenceAsync.js +17 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerHost.d.ts +51 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerHost.js +188 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerLocalHostAsync.d.ts +18 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerLocalHostAsync.js +177 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerLocalServerAsync.d.ts +26 -0
- package/dist/features/trusted-social-manager-host/composition/createTrustedSocialManagerLocalServerAsync.js +66 -0
- package/dist/features/trusted-social-manager-host/domain/publicationConfig.d.ts +18 -0
- package/dist/features/trusted-social-manager-host/domain/publicationConfig.js +77 -0
- package/dist/features/trusted-social-manager-host/domain/readSocialManagerConnectionRequest.d.ts +54 -0
- package/dist/features/trusted-social-manager-host/domain/readSocialManagerConnectionRequest.js +77 -0
- package/dist/features/trusted-social-manager-host/domain/readSocialManagerDraftContent.d.ts +3 -0
- package/dist/features/trusted-social-manager-host/domain/readSocialManagerDraftContent.js +47 -0
- package/dist/features/trusted-social-manager-host/domain/socialManagerConnectionIdentity.d.ts +17 -0
- package/dist/features/trusted-social-manager-host/domain/socialManagerConnectionIdentity.js +17 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +14 -0
- package/dist/types/socialManager.d.ts +77 -0
- package/dist/types/socialManager.js +1 -0
- package/dist/types/socialManagerPublication.d.ts +27 -0
- package/dist/types/socialManagerPublication.js +1 -0
- package/package.json +507 -9
package/dist/features/trusted-social-manager-host/adapters/createSocialOperationStoresAsync.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { constants, lstat, mkdir, open } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { isMissingPathError, writeJsonFileAtomic } from '@ankhorage/utility/node/fs';
|
|
5
|
+
import { isRecord } from '@ankhorage/utility/object';
|
|
6
|
+
/***
|
|
7
|
+
* Create filesystem-backed Social provider operation stores for one trusted local host.
|
|
8
|
+
* The directory is explicitly single-writer: atomic writes and this mutex coordinate one
|
|
9
|
+
* adapter instance, while multiple processes require an external lock.
|
|
10
|
+
*/
|
|
11
|
+
export async function createSocialOperationStoresAsync(directory) {
|
|
12
|
+
const root = join(directory, 'social-operations');
|
|
13
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
14
|
+
const stat = await lstat(root);
|
|
15
|
+
if (!stat.isDirectory() || (stat.mode & 0o077) !== 0) {
|
|
16
|
+
throw new Error('Social operation directory must be private.');
|
|
17
|
+
}
|
|
18
|
+
const persistence = createOperationPersistence(root, createSerialAsyncMutex());
|
|
19
|
+
return createSocialOperationStores(persistence);
|
|
20
|
+
}
|
|
21
|
+
/*** Bind each published provider store contract to the shared durable operation document. */
|
|
22
|
+
function createSocialOperationStores(persistence) {
|
|
23
|
+
return {
|
|
24
|
+
facebookPages: {
|
|
25
|
+
getAsync: (command) => persistence.getAsync('facebookPages', command),
|
|
26
|
+
saveAsync: async (operation) => {
|
|
27
|
+
const existing = await persistence.updateAsync('facebookPages', operation, operation.state === 'started');
|
|
28
|
+
if (existing)
|
|
29
|
+
throw new Error('Facebook operation is already started.');
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
instagramProfessional: {
|
|
33
|
+
getAsync: (command) => persistence.getAsync('instagramProfessional', command),
|
|
34
|
+
saveAsync: async (operation) => {
|
|
35
|
+
await persistence.updateAsync('instagramProfessional', operation, false);
|
|
36
|
+
},
|
|
37
|
+
claimAsync: (operation) => persistence.updateAsync('instagramProfessional', operation, true),
|
|
38
|
+
},
|
|
39
|
+
linkedin: {
|
|
40
|
+
getAsync: (command) => persistence.getAsync('linkedin', command),
|
|
41
|
+
claimAsync: async (operation) => {
|
|
42
|
+
const existing = await persistence.updateAsync('linkedin', operation, true);
|
|
43
|
+
return existing
|
|
44
|
+
? { state: 'existing', operation: existing }
|
|
45
|
+
: { state: 'claimed' };
|
|
46
|
+
},
|
|
47
|
+
saveAsync: async (operation) => {
|
|
48
|
+
await persistence.updateAsync('linkedin', operation, false);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
pinterest: {
|
|
52
|
+
getAsync: (command) => persistence.getAsync('pinterest', command),
|
|
53
|
+
claimAsync: async (operation) => {
|
|
54
|
+
const existing = await persistence.updateAsync('pinterest', operation, true);
|
|
55
|
+
return existing
|
|
56
|
+
? { state: 'existing', operation: existing }
|
|
57
|
+
: { state: 'claimed' };
|
|
58
|
+
},
|
|
59
|
+
saveAsync: async (operation) => {
|
|
60
|
+
await persistence.updateAsync('pinterest', operation, false);
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/*** Coordinate read and atomic replacement of one private operation document. */
|
|
66
|
+
function createOperationPersistence(root, mutations) {
|
|
67
|
+
return {
|
|
68
|
+
getAsync: async (provider, command) => {
|
|
69
|
+
const operation = operationsFor(await readDocumentAsync(root), provider).find((candidate) => operationKey(candidate.command) === operationKey(command));
|
|
70
|
+
return (operation ?? null);
|
|
71
|
+
},
|
|
72
|
+
updateAsync: (provider, operation, claim) => mutations.runAsync(async () => {
|
|
73
|
+
const document = await readDocumentAsync(root);
|
|
74
|
+
const current = operationsFor(document, provider);
|
|
75
|
+
const key = operationKey(operation.command);
|
|
76
|
+
const existing = current.find((candidate) => operationKey(candidate.command) === key);
|
|
77
|
+
if (existing && !isSameIdentity(existing, operation))
|
|
78
|
+
throw new Error('Social operation identity collision.');
|
|
79
|
+
if (claim && existing && existing.state !== 'rejected')
|
|
80
|
+
return existing;
|
|
81
|
+
const operations = existing
|
|
82
|
+
? current.map((candidate) => operationKey(candidate.command) === key ? operation : candidate)
|
|
83
|
+
: [...current, operation];
|
|
84
|
+
await saveDocumentAsync(root, replaceOperations(document, provider, operations));
|
|
85
|
+
return null;
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/*** Select one statically known provider partition without caller-controlled object indexing. */
|
|
90
|
+
function operationsFor(document, provider) {
|
|
91
|
+
switch (provider) {
|
|
92
|
+
case 'facebookPages':
|
|
93
|
+
return document.operations.facebookPages;
|
|
94
|
+
case 'instagramProfessional':
|
|
95
|
+
return document.operations.instagramProfessional;
|
|
96
|
+
case 'linkedin':
|
|
97
|
+
return document.operations.linkedin;
|
|
98
|
+
case 'pinterest':
|
|
99
|
+
return document.operations.pinterest;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/*** Replace only one provider partition while preserving all other provider operation records. */
|
|
103
|
+
function replaceOperations(document, provider, operations) {
|
|
104
|
+
const current = document.operations;
|
|
105
|
+
switch (provider) {
|
|
106
|
+
case 'facebookPages':
|
|
107
|
+
return { ...document, operations: { ...current, facebookPages: operations } };
|
|
108
|
+
case 'instagramProfessional':
|
|
109
|
+
return { ...document, operations: { ...current, instagramProfessional: operations } };
|
|
110
|
+
case 'linkedin':
|
|
111
|
+
return { ...document, operations: { ...current, linkedin: operations } };
|
|
112
|
+
case 'pinterest':
|
|
113
|
+
return { ...document, operations: { ...current, pinterest: operations } };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/*** Read only a private regular document and reject malformed persisted state. */
|
|
117
|
+
async function readDocumentAsync(root) {
|
|
118
|
+
const path = join(root, 'operations.json');
|
|
119
|
+
try {
|
|
120
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
121
|
+
try {
|
|
122
|
+
if (!(await handle.stat()).isFile())
|
|
123
|
+
throw new Error('Social operation file must be regular.');
|
|
124
|
+
const document = JSON.parse(await handle.readFile('utf8'));
|
|
125
|
+
if (!isStoredDocument(document))
|
|
126
|
+
throw new Error('Social operation document is invalid.');
|
|
127
|
+
return document;
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
await handle.close();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (isMissingPathError(error))
|
|
135
|
+
return { version: 1, operations: emptyOperations() };
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/*** Persist all provider records together so claims are never partially visible. */
|
|
140
|
+
async function saveDocumentAsync(root, document) {
|
|
141
|
+
await writeJsonFileAtomic(join(root, 'operations.json'), document);
|
|
142
|
+
}
|
|
143
|
+
/*** Derive a path-independent key from the complete untrusted Social command identity. */
|
|
144
|
+
function operationKey(command) {
|
|
145
|
+
return createHash('sha256').update(JSON.stringify(command)).digest('hex');
|
|
146
|
+
}
|
|
147
|
+
/*** Refuse a same-command key from changing its trusted authorization binding. */
|
|
148
|
+
function isSameIdentity(left, right) {
|
|
149
|
+
return (operationKey(left.command) === operationKey(right.command) &&
|
|
150
|
+
left.authorizationRef === right.authorizationRef);
|
|
151
|
+
}
|
|
152
|
+
/*** Construct the full provider partition for a newly initialized operation document. */
|
|
153
|
+
function emptyOperations() {
|
|
154
|
+
return { facebookPages: [], instagramProfessional: [], linkedin: [], pinterest: [] };
|
|
155
|
+
}
|
|
156
|
+
/*** Validate the persisted envelope before opaque provider operation values reach callers. */
|
|
157
|
+
function isStoredDocument(value) {
|
|
158
|
+
if (!isRecord(value) || value.version !== 1 || !isRecord(value.operations))
|
|
159
|
+
return false;
|
|
160
|
+
const { operations } = value;
|
|
161
|
+
return (Array.isArray(operations.facebookPages) &&
|
|
162
|
+
operations.facebookPages.every((operation) => isStoredOperation('facebookPages', operation)) &&
|
|
163
|
+
Array.isArray(operations.instagramProfessional) &&
|
|
164
|
+
operations.instagramProfessional.every((operation) => isStoredOperation('instagramProfessional', operation)) &&
|
|
165
|
+
Array.isArray(operations.linkedin) &&
|
|
166
|
+
operations.linkedin.every((operation) => isStoredOperation('linkedin', operation)) &&
|
|
167
|
+
Array.isArray(operations.pinterest) &&
|
|
168
|
+
operations.pinterest.every((operation) => isStoredOperation('pinterest', operation)));
|
|
169
|
+
}
|
|
170
|
+
/*** Require every retained operation to include a complete command and authorization binding. */
|
|
171
|
+
function isStoredOperation(provider, value) {
|
|
172
|
+
if (!isRecord(value) ||
|
|
173
|
+
typeof value.state !== 'string' ||
|
|
174
|
+
typeof value.authorizationRef !== 'string' ||
|
|
175
|
+
value.authorizationRef.length === 0 ||
|
|
176
|
+
!isCommand(value.command)) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
return provider === 'instagramProfessional'
|
|
180
|
+
? isInstagramOperation(value)
|
|
181
|
+
: isReceiptOperation(value, provider);
|
|
182
|
+
}
|
|
183
|
+
/*** Validate the stable command identity used as the durable operation key. */
|
|
184
|
+
function isCommand(value) {
|
|
185
|
+
return (isRecord(value) &&
|
|
186
|
+
typeof value.operationKey === 'string' &&
|
|
187
|
+
typeof value.tenantId === 'string' &&
|
|
188
|
+
typeof value.actorId === 'string' &&
|
|
189
|
+
isRecord(value.target) &&
|
|
190
|
+
typeof value.target.provider === 'string' &&
|
|
191
|
+
typeof value.target.accountId === 'string' &&
|
|
192
|
+
typeof value.target.accountType === 'string' &&
|
|
193
|
+
typeof value.target.destinationId === 'string' &&
|
|
194
|
+
typeof value.target.destinationType === 'string' &&
|
|
195
|
+
isRecord(value.action) &&
|
|
196
|
+
typeof value.action.kind === 'string');
|
|
197
|
+
}
|
|
198
|
+
/*** Retain only documented Instagram stages and their stage-specific durable details. */
|
|
199
|
+
function isInstagramOperation(value) {
|
|
200
|
+
return (value.state === 'comment-started' ||
|
|
201
|
+
(value.state === 'creating-children' &&
|
|
202
|
+
Array.isArray(value.childContainerIds) &&
|
|
203
|
+
value.childContainerIds.every((id) => typeof id === 'string')) ||
|
|
204
|
+
((value.state === 'container-created' || value.state === 'publication-started') &&
|
|
205
|
+
typeof value.containerId === 'string') ||
|
|
206
|
+
(value.state === 'completed' && isReceipt(value.receipt)));
|
|
207
|
+
}
|
|
208
|
+
/*** Retain only documented simple provider stages and require complete completion receipts. */
|
|
209
|
+
function isReceiptOperation(value, provider) {
|
|
210
|
+
const states = provider === 'facebookPages'
|
|
211
|
+
? ['started', 'completed', 'rejected']
|
|
212
|
+
: ['started', 'completed', 'rejected'];
|
|
213
|
+
return (states.includes(value.state) &&
|
|
214
|
+
(value.state !== 'completed' || isReceipt(value.receipt)));
|
|
215
|
+
}
|
|
216
|
+
/*** Require a completion receipt to retain its stable operation, authorization, and target bindings. */
|
|
217
|
+
function isReceipt(value) {
|
|
218
|
+
return (isRecord(value) &&
|
|
219
|
+
typeof value.receiptId === 'string' &&
|
|
220
|
+
typeof value.operationKey === 'string' &&
|
|
221
|
+
typeof value.tenantId === 'string' &&
|
|
222
|
+
typeof value.actorId === 'string' &&
|
|
223
|
+
typeof value.authorizationRef === 'string' &&
|
|
224
|
+
isRecord(value.target) &&
|
|
225
|
+
typeof value.target.provider === 'string' &&
|
|
226
|
+
typeof value.target.accountId === 'string' &&
|
|
227
|
+
typeof value.target.accountType === 'string' &&
|
|
228
|
+
typeof value.target.destinationId === 'string' &&
|
|
229
|
+
typeof value.target.destinationType === 'string');
|
|
230
|
+
}
|
|
231
|
+
function createSerialAsyncMutex() {
|
|
232
|
+
let tail = Promise.resolve();
|
|
233
|
+
return {
|
|
234
|
+
runAsync: async (operation) => {
|
|
235
|
+
const previous = tail;
|
|
236
|
+
let release = () => undefined;
|
|
237
|
+
tail = new Promise((resolve) => {
|
|
238
|
+
release = resolve;
|
|
239
|
+
});
|
|
240
|
+
await previous;
|
|
241
|
+
return operation().finally(release);
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
package/dist/features/trusted-social-manager-host/adapters/projectSocialManagerConnection.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { AuthorizationHealthSnapshot, ManagedConnection } from '@ankhorage/connection';
|
|
2
|
+
/*** Project a caller-owned connection without exposing authorization references or credentials. */
|
|
3
|
+
export declare function projectSocialManagerConnection(connection: ManagedConnection): {
|
|
4
|
+
connectionId: string;
|
|
5
|
+
provider: import("@ankhorage/connection").DelegatedAuthorizationProvider;
|
|
6
|
+
accountId: string;
|
|
7
|
+
status: "connected" | "reconnect-required" | "disconnected";
|
|
8
|
+
scopes: readonly string[];
|
|
9
|
+
revision: string;
|
|
10
|
+
};
|
|
11
|
+
/*** Ensure a trusted health adapter cannot substitute a different durable authorization binding. */
|
|
12
|
+
export declare function isSocialManagerConnectionHealth(connection: ManagedConnection, health: AuthorizationHealthSnapshot): boolean;
|
|
13
|
+
/*** Project one successful connection use-case result through the host response envelope. */
|
|
14
|
+
export declare function socialManagerConnectionSuccess(status: number, body: unknown): {
|
|
15
|
+
status: number;
|
|
16
|
+
body: unknown;
|
|
17
|
+
};
|
|
18
|
+
/*** Return a stable validation error without reflecting untrusted connection input. */
|
|
19
|
+
export declare function socialManagerConnectionInvalid(): {
|
|
20
|
+
status: number;
|
|
21
|
+
body: {
|
|
22
|
+
error: {
|
|
23
|
+
code: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/*** Hide connection ownership and existence behind one caller-safe response. */
|
|
28
|
+
export declare function socialManagerConnectionMissing(code: string): {
|
|
29
|
+
status: number;
|
|
30
|
+
body: {
|
|
31
|
+
error: {
|
|
32
|
+
code: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/*** Keep a trusted provider failure distinct from malformed connection input. */
|
|
37
|
+
export declare function socialManagerConnectionUnavailable(): {
|
|
38
|
+
status: number;
|
|
39
|
+
body: {
|
|
40
|
+
error: {
|
|
41
|
+
code: string;
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/*** Preserve the authorization middleware denial if a handler is invoked without it. */
|
|
46
|
+
export declare function socialManagerConnectionForbidden(): {
|
|
47
|
+
status: number;
|
|
48
|
+
body: {
|
|
49
|
+
error: {
|
|
50
|
+
code: string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/*** Project a caller-owned connection without exposing authorization references or credentials. */
|
|
2
|
+
export function projectSocialManagerConnection(connection) {
|
|
3
|
+
return {
|
|
4
|
+
connectionId: connection.connectionId,
|
|
5
|
+
provider: connection.provider,
|
|
6
|
+
accountId: connection.accountId,
|
|
7
|
+
status: connection.status,
|
|
8
|
+
scopes: connection.grantedScopes,
|
|
9
|
+
revision: connection.revision,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/*** Ensure a trusted health adapter cannot substitute a different durable authorization binding. */
|
|
13
|
+
export function isSocialManagerConnectionHealth(connection, health) {
|
|
14
|
+
return (health.tenantId === connection.tenantId &&
|
|
15
|
+
health.projectId === connection.projectId &&
|
|
16
|
+
health.environmentId === connection.environmentId &&
|
|
17
|
+
health.actorId === connection.actorId &&
|
|
18
|
+
health.provider === connection.provider &&
|
|
19
|
+
health.connectionId === connection.connectionId &&
|
|
20
|
+
health.accountId === connection.accountId &&
|
|
21
|
+
health.authorizationId === connection.authorizationId);
|
|
22
|
+
}
|
|
23
|
+
/*** Project one successful connection use-case result through the host response envelope. */
|
|
24
|
+
export function socialManagerConnectionSuccess(status, body) {
|
|
25
|
+
return { status, body };
|
|
26
|
+
}
|
|
27
|
+
/*** Return a stable validation error without reflecting untrusted connection input. */
|
|
28
|
+
export function socialManagerConnectionInvalid() {
|
|
29
|
+
return { status: 400, body: { error: { code: 'invalid_connection_request' } } };
|
|
30
|
+
}
|
|
31
|
+
/*** Hide connection ownership and existence behind one caller-safe response. */
|
|
32
|
+
export function socialManagerConnectionMissing(code) {
|
|
33
|
+
return { status: 404, body: { error: { code } } };
|
|
34
|
+
}
|
|
35
|
+
/*** Keep a trusted provider failure distinct from malformed connection input. */
|
|
36
|
+
export function socialManagerConnectionUnavailable() {
|
|
37
|
+
return { status: 502, body: { error: { code: 'connection_unavailable' } } };
|
|
38
|
+
}
|
|
39
|
+
/*** Preserve the authorization middleware denial if a handler is invoked without it. */
|
|
40
|
+
export function socialManagerConnectionForbidden() {
|
|
41
|
+
return { status: 403, body: { error: { code: 'forbidden' } } };
|
|
42
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type FlowRuntime } from '@ankhorage/flow';
|
|
2
|
+
import type { SocialManagerPublicationDependencies, SocialManagerPublicationRequest } from '../../../types/socialManagerPublication.js';
|
|
3
|
+
/*** Compose the released Flow engine with one protected Social operation and immutable draft references. */
|
|
4
|
+
export declare function createSocialManagerPublicationRuntime(input: SocialManagerPublicationDependencies): {
|
|
5
|
+
readonly runtime: FlowRuntime;
|
|
6
|
+
readonly scheduleAsync: (request: SocialManagerPublicationRequest) => Promise<{
|
|
7
|
+
readonly run: Awaited<ReturnType<FlowRuntime['triggerAsync']>>;
|
|
8
|
+
readonly approvalId: string;
|
|
9
|
+
}>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { createFlowNodeOperationResolver, createFlowRuntime, } from '@ankhorage/flow';
|
|
3
|
+
import { executeSocialActionAsync, } from '@ankhorage/social';
|
|
4
|
+
import { createKeyedAsyncMutex } from '@ankhorage/utility/concurrency';
|
|
5
|
+
import { assertCallerScope, readPublicationConfig, sameTarget, } from '../domain/publicationConfig.js';
|
|
6
|
+
import { readDestinationAsync, requireBindingAsync, resolveCommandAsync, } from './resolveSocialManagerPublication.js';
|
|
7
|
+
const capabilityId = 'projects.social.publication.execute';
|
|
8
|
+
const providerId = '@ankhorage/projects';
|
|
9
|
+
/*** Compose the released Flow engine with one protected Social operation and immutable draft references. */
|
|
10
|
+
export function createSocialManagerPublicationRuntime(input) {
|
|
11
|
+
const operation = createPublicationOperation(input);
|
|
12
|
+
const mutex = createKeyedAsyncMutex();
|
|
13
|
+
const runtime = createFlowRuntime({
|
|
14
|
+
...input.flow,
|
|
15
|
+
nodeRegistry: createFlowNodeOperationResolver({
|
|
16
|
+
capabilityRegistry: {
|
|
17
|
+
getCapability: (node) => node.capability.providerId === providerId && node.capability.capabilityId === capabilityId
|
|
18
|
+
? {
|
|
19
|
+
id: capabilityId,
|
|
20
|
+
owner: providerId,
|
|
21
|
+
access: ['invoke'],
|
|
22
|
+
binding: { kind: 'action', bindableAs: ['target'] },
|
|
23
|
+
}
|
|
24
|
+
: undefined,
|
|
25
|
+
},
|
|
26
|
+
capabilityResolver: { getExecutor: () => operation },
|
|
27
|
+
}),
|
|
28
|
+
});
|
|
29
|
+
return {
|
|
30
|
+
runtime,
|
|
31
|
+
scheduleAsync: (request) => mutex.run(publicationId(request), () => scheduleAsync(input, runtime, request)),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/*** Persist one exact, independently approvable destination run; Flow owns scheduling and execution. */
|
|
35
|
+
async function scheduleAsync(input, runtime, request) {
|
|
36
|
+
const scheduledRequest = {
|
|
37
|
+
...request,
|
|
38
|
+
runAt: request.runAt ?? (input.flow.clock?.now() ?? new Date()).toISOString(),
|
|
39
|
+
};
|
|
40
|
+
const { caller, draftId, revision, destinationIndex, runAt } = scheduledRequest;
|
|
41
|
+
assertCallerScope(input.scope, caller);
|
|
42
|
+
assertScheduleInput(scheduledRequest);
|
|
43
|
+
const destination = await readDestinationAsync(input, scheduledRequest);
|
|
44
|
+
const binding = await requireBindingAsync(input, caller, destination);
|
|
45
|
+
const config = {
|
|
46
|
+
caller,
|
|
47
|
+
draftId,
|
|
48
|
+
revision,
|
|
49
|
+
destinationIndex,
|
|
50
|
+
runAt,
|
|
51
|
+
connectionRevision: binding.connection.revision,
|
|
52
|
+
target: binding.target,
|
|
53
|
+
};
|
|
54
|
+
const flow = createPublicationDefinition(config);
|
|
55
|
+
const scope = { tenantId: caller.tenantId, projectId: caller.projectId };
|
|
56
|
+
const existing = await runtime.getWorkflowAsync(scope, flow.id, flow.version);
|
|
57
|
+
if (!existing)
|
|
58
|
+
await runtime.createDraftAsync(scope, flow);
|
|
59
|
+
else if (JSON.stringify(existing.definition) !== JSON.stringify(flow))
|
|
60
|
+
throw new Error('Social publication configuration changed.');
|
|
61
|
+
if (!existing?.publishedAt)
|
|
62
|
+
await runtime.publishAsync(scope, flow.id, flow.version);
|
|
63
|
+
const run = await runtime.triggerAsync({
|
|
64
|
+
scope,
|
|
65
|
+
flowId: flow.id,
|
|
66
|
+
triggerId: 'publish',
|
|
67
|
+
idempotencyKey: flow.id,
|
|
68
|
+
runAt,
|
|
69
|
+
});
|
|
70
|
+
const approvalId = await ensureApprovalAsync(input, runtime, scheduledRequest, run.id);
|
|
71
|
+
return { run, approvalId };
|
|
72
|
+
}
|
|
73
|
+
/*** Create a pending approval only once, without resetting a previously resolved decision. */
|
|
74
|
+
async function ensureApprovalAsync(input, runtime, request, runId) {
|
|
75
|
+
const { caller } = request;
|
|
76
|
+
const scope = { tenantId: caller.tenantId, projectId: caller.projectId };
|
|
77
|
+
const approvalId = `social-approval:${runId}`;
|
|
78
|
+
if (!(await input.flow.store.getApprovalAsync(scope, approvalId)))
|
|
79
|
+
await runtime.requestApprovalAsync({
|
|
80
|
+
scope,
|
|
81
|
+
approval: {
|
|
82
|
+
id: approvalId,
|
|
83
|
+
runId,
|
|
84
|
+
status: 'pending',
|
|
85
|
+
requestedBy: caller.actorId,
|
|
86
|
+
requestedAt: (input.flow.clock?.now() ?? new Date()).toISOString(),
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return approvalId;
|
|
90
|
+
}
|
|
91
|
+
/*** Validate one exact destination revision and canonical UTC instant. */
|
|
92
|
+
function assertScheduleInput(request) {
|
|
93
|
+
const { caller, revision, destinationIndex, runAt } = request;
|
|
94
|
+
if (!caller.actorId.trim())
|
|
95
|
+
throw new Error('Social publication requires a caller.');
|
|
96
|
+
if (!Number.isSafeInteger(revision) ||
|
|
97
|
+
revision < 1 ||
|
|
98
|
+
!Number.isSafeInteger(destinationIndex) ||
|
|
99
|
+
destinationIndex < 0 ||
|
|
100
|
+
(runAt !== undefined &&
|
|
101
|
+
(!Number.isFinite(Date.parse(runAt)) || new Date(runAt).toISOString() !== runAt))) {
|
|
102
|
+
throw new Error('Invalid Social Manager publication request.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/*** Derive a stable intent identity so a repeated schedule request cannot create another run. */
|
|
106
|
+
function publicationId(request) {
|
|
107
|
+
const { caller, draftId, revision, destinationIndex, runAt } = request;
|
|
108
|
+
return ('social-' +
|
|
109
|
+
createHash('sha256')
|
|
110
|
+
.update(JSON.stringify([
|
|
111
|
+
caller.tenantId,
|
|
112
|
+
caller.projectId,
|
|
113
|
+
caller.environmentId,
|
|
114
|
+
caller.actorId,
|
|
115
|
+
draftId,
|
|
116
|
+
revision,
|
|
117
|
+
destinationIndex,
|
|
118
|
+
runAt ?? null,
|
|
119
|
+
]))
|
|
120
|
+
.digest('hex'));
|
|
121
|
+
}
|
|
122
|
+
/*** Construct a server-owned graph that pins the approved revision and provider destination. */
|
|
123
|
+
function createPublicationDefinition(config) {
|
|
124
|
+
return {
|
|
125
|
+
id: publicationId(config),
|
|
126
|
+
version: 1,
|
|
127
|
+
title: 'Social publication',
|
|
128
|
+
triggers: [{ id: 'publish', kind: 'manual' }],
|
|
129
|
+
edges: [],
|
|
130
|
+
nodes: [
|
|
131
|
+
{
|
|
132
|
+
id: 'publication',
|
|
133
|
+
capability: { providerId, capabilityId },
|
|
134
|
+
config: {
|
|
135
|
+
publication: {
|
|
136
|
+
kind: 'inline',
|
|
137
|
+
value: {
|
|
138
|
+
caller: { ...config.caller },
|
|
139
|
+
draftId: config.draftId,
|
|
140
|
+
revision: config.revision,
|
|
141
|
+
destinationIndex: config.destinationIndex,
|
|
142
|
+
runAt: config.runAt,
|
|
143
|
+
connectionRevision: config.connectionRevision,
|
|
144
|
+
target: { ...config.target },
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
ports: [{ id: 'receipt', direction: 'output' }],
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/*** Register an externally effective node; no internal operation can bypass the Flow security permit. */
|
|
154
|
+
function createPublicationOperation(input) {
|
|
155
|
+
return {
|
|
156
|
+
effect: {
|
|
157
|
+
kind: 'protected',
|
|
158
|
+
createSecurityOperation: ({ node, effect }) => {
|
|
159
|
+
const { caller } = readPublicationConfig(node, input.scope);
|
|
160
|
+
return {
|
|
161
|
+
actor: {
|
|
162
|
+
id: caller.actorId,
|
|
163
|
+
tenantId: caller.tenantId,
|
|
164
|
+
projectId: caller.projectId,
|
|
165
|
+
environment: caller.environmentId,
|
|
166
|
+
},
|
|
167
|
+
capability: capabilityId,
|
|
168
|
+
operationId: effect.idempotencyKey,
|
|
169
|
+
idempotencyKey: effect.idempotencyKey,
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
executeAsync: async ({ node, effect, signal }) => {
|
|
174
|
+
signal.throwIfAborted();
|
|
175
|
+
await requireApprovalAsync(input, node, effect.idempotencyKey);
|
|
176
|
+
const resolved = await resolveCommandAsync(input, node, effect.idempotencyKey);
|
|
177
|
+
signal.throwIfAborted();
|
|
178
|
+
const decision = await executeSocialActionAsync(resolved.command, {
|
|
179
|
+
authorizeAsync: () => Promise.resolve(resolved.grant),
|
|
180
|
+
}, input.mediaPreflight, resolved.provider);
|
|
181
|
+
if (!decision.dispatched)
|
|
182
|
+
throw new Error(`Social publication: ${decision.reason}.`);
|
|
183
|
+
return receiptOutput(decision.receipt);
|
|
184
|
+
},
|
|
185
|
+
reconcileAsync: async ({ node, effect }) => {
|
|
186
|
+
const resolved = await resolveCommandAsync(input, node, effect.idempotencyKey);
|
|
187
|
+
const result = await resolved.provider.reconcileAsync(resolved.command, resolved.grant.authorizationRef);
|
|
188
|
+
if (result.state !== 'found')
|
|
189
|
+
return 'unknown';
|
|
190
|
+
if (!matchesReceipt(result.receipt, resolved.command, resolved.grant.authorizationRef))
|
|
191
|
+
throw new Error('Social publication receipt does not match the authorized operation.');
|
|
192
|
+
return receiptOutput(result.receipt);
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/*** Independently bind the approval to the actual durable run before dispatching. */
|
|
197
|
+
async function requireApprovalAsync(input, node, operationKey) {
|
|
198
|
+
const suffix = ':step:publication';
|
|
199
|
+
if (!operationKey.endsWith(suffix))
|
|
200
|
+
throw new Error('Invalid Social publication operation identity.');
|
|
201
|
+
const runId = operationKey.slice(0, -suffix.length);
|
|
202
|
+
const config = readPublicationConfig(node, input.scope);
|
|
203
|
+
const scope = { tenantId: config.caller.tenantId, projectId: config.caller.projectId };
|
|
204
|
+
const run = await input.flow.store.getRunAsync(scope, runId);
|
|
205
|
+
const approval = await input.flow.store.getApprovalAsync(scope, 'social-approval:' + runId);
|
|
206
|
+
if (run?.flow.id !== publicationId(config) ||
|
|
207
|
+
run.flow.version !== 1 ||
|
|
208
|
+
approval?.runId !== runId ||
|
|
209
|
+
approval.status !== 'approved' ||
|
|
210
|
+
(approval.expiresAt !== undefined &&
|
|
211
|
+
Date.parse(approval.expiresAt) <= (input.flow.clock?.now() ?? new Date()).getTime()))
|
|
212
|
+
throw new Error('Social publication has no current exact approval.');
|
|
213
|
+
}
|
|
214
|
+
/*** Reject a stale or cross-authorization reconciliation receipt. */
|
|
215
|
+
function matchesReceipt(receipt, command, authorizationRef) {
|
|
216
|
+
return (!!receipt.receiptId &&
|
|
217
|
+
receipt.operationKey === command.operationKey &&
|
|
218
|
+
receipt.actorId === command.actorId &&
|
|
219
|
+
receipt.tenantId === command.tenantId &&
|
|
220
|
+
receipt.authorizationRef === authorizationRef &&
|
|
221
|
+
sameTarget(receipt.target, command.target));
|
|
222
|
+
}
|
|
223
|
+
/*** Project an authoritative receipt without disclosing its opaque authorization reference. */
|
|
224
|
+
function receiptOutput(receipt) {
|
|
225
|
+
return {
|
|
226
|
+
output: {
|
|
227
|
+
receipt: {
|
|
228
|
+
kind: 'inline',
|
|
229
|
+
value: {
|
|
230
|
+
receiptId: receipt.receiptId,
|
|
231
|
+
operationKey: receipt.operationKey,
|
|
232
|
+
provider: receipt.target.provider,
|
|
233
|
+
destinationId: receipt.target.destinationId,
|
|
234
|
+
...(receipt.resourceId === undefined ? {} : { resourceId: receipt.resourceId }),
|
|
235
|
+
...(receipt.resourceUrl === undefined ? {} : { resourceUrl: receipt.resourceUrl }),
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
package/dist/features/trusted-social-manager-host/application/resolveSocialManagerPublication.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { FlowNode } from '@ankhorage/contracts/flow';
|
|
2
|
+
import type { SocialActionCommand } from '@ankhorage/social';
|
|
3
|
+
import type { SocialManagerCaller, SocialManagerDestination } from '../../../types/socialManager.js';
|
|
4
|
+
import type { SocialManagerPublicationBinding, SocialManagerPublicationDependencies, SocialManagerPublicationRequest } from '../../../types/socialManagerPublication.js';
|
|
5
|
+
/*** Resolve the immutable destination and current authorization before execution or reconciliation. */
|
|
6
|
+
export declare function resolveCommandAsync(input: SocialManagerPublicationDependencies, node: FlowNode, operationKey: string): Promise<{
|
|
7
|
+
command: SocialActionCommand;
|
|
8
|
+
grant: {
|
|
9
|
+
readonly authorized: true;
|
|
10
|
+
readonly authorizationRef: string;
|
|
11
|
+
readonly operationKey: string;
|
|
12
|
+
readonly tenantId: string;
|
|
13
|
+
readonly actorId: string;
|
|
14
|
+
readonly target: import("@ankhorage/social").SocialTargetRef;
|
|
15
|
+
readonly scopes: readonly string[];
|
|
16
|
+
};
|
|
17
|
+
provider: import("@ankhorage/social").SocialProviderPort;
|
|
18
|
+
}>;
|
|
19
|
+
/*** Revalidate the connection's current exact target and the provider's present publish requirements. */
|
|
20
|
+
export declare function requireBindingAsync(input: SocialManagerPublicationDependencies, caller: SocialManagerCaller, destination: SocialManagerDestination): Promise<SocialManagerPublicationBinding>;
|
|
21
|
+
/*** Read a destination from its immutable revision rather than the mutable current draft. */
|
|
22
|
+
export declare function readDestinationAsync(input: SocialManagerPublicationDependencies, request: SocialManagerPublicationRequest): Promise<SocialManagerDestination>;
|