@ankhorage/projects 0.7.3 → 0.9.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 +20 -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.d.ts +4 -6
- package/dist/features/projects/composition/createExpoProjectMaterializer.js +29 -36
- package/dist/features/projects/composition/createNavigatorMaterializationFiles.d.ts +14 -0
- package/dist/features/projects/composition/createNavigatorMaterializationFiles.js +19 -0
- 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/features/trusted-zefix-host/composition/createTrustedZefixHost.d.ts +1 -2
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixHost.js +3 -59
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixWorkerEntry.d.ts +36 -0
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixWorkerEntry.js +105 -0
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixWorkerHandler.d.ts +35 -0
- package/dist/features/trusted-zefix-host/composition/createTrustedZefixWorkerHandler.js +92 -0
- package/dist/features/trusted-zefix-host/composition/zefixOperations.d.ts +20 -0
- package/dist/features/trusted-zefix-host/composition/zefixOperations.js +57 -0
- package/dist/features/worker-materialization/composition/composeWorkerMaterialization.d.ts +13 -0
- package/dist/features/worker-materialization/composition/composeWorkerMaterialization.js +90 -0
- package/dist/features/worker-materialization/composition/createWorkerMaterializationFile.d.ts +13 -0
- package/dist/features/worker-materialization/composition/createWorkerMaterializationFile.js +48 -0
- package/dist/features/worker-materialization/constants/worker.d.ts +6 -0
- package/dist/features/worker-materialization/constants/worker.js +6 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +16 -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/dist/worker/index.d.ts +3 -0
- package/dist/worker/index.js +3 -0
- package/package.json +511 -9
package/dist/features/trusted-social-manager-host/adapters/createSocialManagerMediaStoreAsync.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { constants, lstat, mkdir, open, rename, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { createKeyedAsyncMutex } from '@ankhorage/utility/concurrency';
|
|
5
|
+
import { isMissingPathError, writeJsonFileAtomic } from '@ankhorage/utility/node/fs';
|
|
6
|
+
import { isRecord } from '@ankhorage/utility/object';
|
|
7
|
+
const maximumByteLength = 2_000_000_000;
|
|
8
|
+
/***
|
|
9
|
+
* Persist immutable caller-owned Social media for one private local host directory.
|
|
10
|
+
* Media identifiers contain both a server-derived scope hash and content hash, so they
|
|
11
|
+
* remain stable across restarts without making mutable filenames part of publication state.
|
|
12
|
+
*/
|
|
13
|
+
export async function createSocialManagerMediaStoreAsync(directory) {
|
|
14
|
+
const root = join(directory, 'social-manager-media');
|
|
15
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
16
|
+
await requirePrivateDirectoryAsync(root, 'Social Manager media directory');
|
|
17
|
+
const mutations = createKeyedAsyncMutex();
|
|
18
|
+
return {
|
|
19
|
+
uploadAsync: async (caller, upload) => {
|
|
20
|
+
const key = callerKey(caller);
|
|
21
|
+
return mutations.run(key, () => uploadAsync(root, key, caller, upload));
|
|
22
|
+
},
|
|
23
|
+
listAsync: async (caller) => (await readDocumentAsync(root, callerKey(caller))).media.map(toMedia),
|
|
24
|
+
readAsync: async (caller, mediaId) => findMedia(await readDocumentAsync(root, callerKey(caller)), mediaId)
|
|
25
|
+
.map(toMedia)
|
|
26
|
+
.at(0),
|
|
27
|
+
loadAsync: async (caller, mediaId) => loadAsync(root, callerKey(caller), mediaId),
|
|
28
|
+
readForPublicationAsync: async ({ tenantId, actorId, mediaId }) => readForPublicationAsync(root, tenantId, actorId, mediaId),
|
|
29
|
+
removeAsync: async (caller, mediaId, isReferencedAsync) => {
|
|
30
|
+
const key = callerKey(caller);
|
|
31
|
+
return mutations.run(key, () => removeAsync(root, key, mediaId, isReferencedAsync));
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/*** Store a new content-addressed object or return the existing immutable record. */
|
|
36
|
+
async function uploadAsync(root, key, caller, upload) {
|
|
37
|
+
const content = readUpload(upload);
|
|
38
|
+
const document = await readDocumentAsync(root, key);
|
|
39
|
+
const mediaId = createMediaId(key, content.bytes);
|
|
40
|
+
const existing = findMedia(document, mediaId).at(0);
|
|
41
|
+
if (existing) {
|
|
42
|
+
if (existing.contentType !== content.contentType || existing.kind !== content.kind)
|
|
43
|
+
throw new Error('Social Manager media content metadata is immutable.');
|
|
44
|
+
return toMedia(existing);
|
|
45
|
+
}
|
|
46
|
+
const record = {
|
|
47
|
+
...content,
|
|
48
|
+
mediaId,
|
|
49
|
+
createdAt: new Date().toISOString(),
|
|
50
|
+
...caller,
|
|
51
|
+
};
|
|
52
|
+
const directory = mediaDirectory(root, key);
|
|
53
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
54
|
+
await requirePrivateDirectoryAsync(directory, 'Social Manager media scope directory');
|
|
55
|
+
await writeContentAtomicAsync(join(directory, `${mediaId}.bin`), content.bytes);
|
|
56
|
+
await saveDocumentAsync(root, key, { ...document, media: [...document.media, record] });
|
|
57
|
+
return toMedia(record);
|
|
58
|
+
}
|
|
59
|
+
/*** Load exact caller-scoped immutable bytes after validating the persisted metadata. */
|
|
60
|
+
async function loadAsync(root, key, mediaId) {
|
|
61
|
+
const record = findMedia(await readDocumentAsync(root, key), mediaId).at(0);
|
|
62
|
+
if (!record)
|
|
63
|
+
return undefined;
|
|
64
|
+
const bytes = await readContentAsync(join(mediaDirectory(root, key), `${mediaId}.bin`));
|
|
65
|
+
if (!bytes || createMediaId(key, bytes) !== mediaId || bytes.byteLength !== record.byteLength)
|
|
66
|
+
throw new Error('Social Manager media content is unavailable or has changed.');
|
|
67
|
+
return { media: toMedia(record), bytes };
|
|
68
|
+
}
|
|
69
|
+
/*** Resolve publication metadata only when the opaque reference still names its original actor. */
|
|
70
|
+
async function readForPublicationAsync(root, tenantId, actorId, mediaId) {
|
|
71
|
+
const key = scopeKeyFromMediaId(mediaId);
|
|
72
|
+
if (!key)
|
|
73
|
+
return undefined;
|
|
74
|
+
const record = findMedia(await readDocumentAsync(root, key), mediaId).at(0);
|
|
75
|
+
return record?.tenantId === tenantId && record.actorId === actorId ? toMedia(record) : undefined;
|
|
76
|
+
}
|
|
77
|
+
/*** Delete bytes only after the caller has proved no immutable draft revision retains the reference. */
|
|
78
|
+
async function removeAsync(root, key, mediaId, isReferencedAsync) {
|
|
79
|
+
const document = await readDocumentAsync(root, key);
|
|
80
|
+
const record = findMedia(document, mediaId).at(0);
|
|
81
|
+
if (!record)
|
|
82
|
+
return 'not-found';
|
|
83
|
+
if (await isReferencedAsync(toMedia(record)))
|
|
84
|
+
return 'referenced';
|
|
85
|
+
await saveDocumentAsync(root, key, {
|
|
86
|
+
...document,
|
|
87
|
+
media: document.media.filter((candidate) => candidate.mediaId !== mediaId),
|
|
88
|
+
});
|
|
89
|
+
await unlink(join(mediaDirectory(root, key), `${mediaId}.bin`)).catch((error) => {
|
|
90
|
+
if (!isMissingPathError(error))
|
|
91
|
+
throw error;
|
|
92
|
+
});
|
|
93
|
+
return 'removed';
|
|
94
|
+
}
|
|
95
|
+
/*** Validate the bounded binary upload shape before it can become durable application state. */
|
|
96
|
+
function readUpload(upload) {
|
|
97
|
+
const kind = mediaKind(upload.contentType);
|
|
98
|
+
if (!kind ||
|
|
99
|
+
!(upload.bytes instanceof Uint8Array) ||
|
|
100
|
+
upload.bytes.byteLength === 0 ||
|
|
101
|
+
upload.bytes.byteLength > maximumByteLength) {
|
|
102
|
+
throw new Error('Invalid Social Manager media upload.');
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
kind,
|
|
106
|
+
contentType: upload.contentType,
|
|
107
|
+
byteLength: upload.bytes.byteLength,
|
|
108
|
+
bytes: upload.bytes,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/*** Classify only media forms accepted by at least one configured Social provider. */
|
|
112
|
+
function mediaKind(contentType) {
|
|
113
|
+
return ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].includes(contentType)
|
|
114
|
+
? 'image'
|
|
115
|
+
: ['video/mp4', 'video/quicktime'].includes(contentType)
|
|
116
|
+
? 'video'
|
|
117
|
+
: undefined;
|
|
118
|
+
}
|
|
119
|
+
/*** Derive a stable opaque reference from the trusted scope and immutable content bytes. */
|
|
120
|
+
function createMediaId(key, bytes) {
|
|
121
|
+
return `media-${key}-${createHash('sha256').update(bytes).digest('hex')}`;
|
|
122
|
+
}
|
|
123
|
+
/*** Accept only references generated by this adapter before using their embedded scope key. */
|
|
124
|
+
function scopeKeyFromMediaId(mediaId) {
|
|
125
|
+
const match = /^media-([a-f0-9]{64})-[a-f0-9]{64}$/.exec(mediaId);
|
|
126
|
+
return match?.[1];
|
|
127
|
+
}
|
|
128
|
+
/*** Keep scope filenames independent from all caller-controlled path segments. */
|
|
129
|
+
function callerKey(caller) {
|
|
130
|
+
const values = [caller.tenantId, caller.projectId, caller.environmentId, caller.actorId];
|
|
131
|
+
if (values.some((value) => !value.trim()))
|
|
132
|
+
throw new Error('Social Manager scope is incomplete.');
|
|
133
|
+
return createHash('sha256').update(JSON.stringify(values)).digest('hex');
|
|
134
|
+
}
|
|
135
|
+
/*** Read a private scope document and fail closed on malformed immutable media records. */
|
|
136
|
+
async function readDocumentAsync(root, key) {
|
|
137
|
+
const path = join(mediaDirectory(root, key), 'media.json');
|
|
138
|
+
try {
|
|
139
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
140
|
+
try {
|
|
141
|
+
if (!(await handle.stat()).isFile())
|
|
142
|
+
throw new Error('Invalid Social Manager media document.');
|
|
143
|
+
const value = JSON.parse(await handle.readFile('utf8'));
|
|
144
|
+
if (!isDocument(value, key))
|
|
145
|
+
throw new Error('Invalid Social Manager media document.');
|
|
146
|
+
return value;
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
await handle.close();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (error) {
|
|
153
|
+
if (isMissingPathError(error))
|
|
154
|
+
return { version: 1, scope: key, media: [] };
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/*** Atomically retain all immutable media metadata for one trusted scope. */
|
|
159
|
+
async function saveDocumentAsync(root, key, document) {
|
|
160
|
+
await writeJsonFileAtomic(join(mediaDirectory(root, key), 'media.json'), document);
|
|
161
|
+
}
|
|
162
|
+
/*** Write bytes through a private temporary path so a restart never observes a partial object. */
|
|
163
|
+
async function writeContentAtomicAsync(path, bytes) {
|
|
164
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
165
|
+
await writeFile(temporary, bytes, { mode: 0o600, flag: 'wx' });
|
|
166
|
+
await rename(temporary, path).catch(async (error) => {
|
|
167
|
+
await unlink(temporary).catch(() => undefined);
|
|
168
|
+
throw error;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/*** Read only a regular no-follow content object from the private server-owned directory. */
|
|
172
|
+
async function readContentAsync(path) {
|
|
173
|
+
try {
|
|
174
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
175
|
+
try {
|
|
176
|
+
if (!(await handle.stat()).isFile())
|
|
177
|
+
throw new Error('Invalid Social Manager media object.');
|
|
178
|
+
return new Uint8Array(await handle.readFile());
|
|
179
|
+
}
|
|
180
|
+
finally {
|
|
181
|
+
await handle.close();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
if (isMissingPathError(error))
|
|
186
|
+
return undefined;
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/*** Require directories to remain private before storing credentials-adjacent media content. */
|
|
191
|
+
async function requirePrivateDirectoryAsync(path, description) {
|
|
192
|
+
const stat = await lstat(path);
|
|
193
|
+
if (!stat.isDirectory() || (stat.mode & 0o077) !== 0)
|
|
194
|
+
throw new Error(`${description} must be private.`);
|
|
195
|
+
}
|
|
196
|
+
/*** Match one opaque identifier without accepting prefix or path traversal variants. */
|
|
197
|
+
function findMedia(document, mediaId) {
|
|
198
|
+
return isMediaId(mediaId) ? document.media.filter((media) => media.mediaId === mediaId) : [];
|
|
199
|
+
}
|
|
200
|
+
/*** Project durable metadata without disclosing scope ownership details. */
|
|
201
|
+
function toMedia(media) {
|
|
202
|
+
const { mediaId, kind, contentType, byteLength, createdAt } = media;
|
|
203
|
+
return { mediaId, kind, contentType, byteLength, createdAt };
|
|
204
|
+
}
|
|
205
|
+
/*** Validate the persisted envelope and every immutable media record before returning it. */
|
|
206
|
+
function isDocument(value, key) {
|
|
207
|
+
return (isRecord(value) &&
|
|
208
|
+
value.version === 1 &&
|
|
209
|
+
value.scope === key &&
|
|
210
|
+
Array.isArray(value.media) &&
|
|
211
|
+
value.media.every((media) => isStoredMedia(media, key)));
|
|
212
|
+
}
|
|
213
|
+
/*** Validate immutable media metadata and bind its identifier to the persisted scope key. */
|
|
214
|
+
function isStoredMedia(value, key) {
|
|
215
|
+
return (isRecord(value) &&
|
|
216
|
+
isMediaId(value.mediaId) &&
|
|
217
|
+
scopeKeyFromMediaId(value.mediaId) === key &&
|
|
218
|
+
isMediaMetadata(value) &&
|
|
219
|
+
typeof value.tenantId === 'string' &&
|
|
220
|
+
typeof value.projectId === 'string' &&
|
|
221
|
+
typeof value.environmentId === 'string' &&
|
|
222
|
+
typeof value.actorId === 'string');
|
|
223
|
+
}
|
|
224
|
+
/*** Validate the immutable provider-neutral portion of one persisted media record. */
|
|
225
|
+
function isMediaMetadata(value) {
|
|
226
|
+
return ((value.kind === 'image' || value.kind === 'video') &&
|
|
227
|
+
typeof value.contentType === 'string' &&
|
|
228
|
+
mediaKind(value.contentType) === value.kind &&
|
|
229
|
+
typeof value.byteLength === 'number' &&
|
|
230
|
+
Number.isSafeInteger(value.byteLength) &&
|
|
231
|
+
value.byteLength > 0 &&
|
|
232
|
+
value.byteLength <= maximumByteLength &&
|
|
233
|
+
typeof value.createdAt === 'string');
|
|
234
|
+
}
|
|
235
|
+
/*** Keep object filenames confined to opaque hashes generated by this adapter. */
|
|
236
|
+
function isMediaId(value) {
|
|
237
|
+
return typeof value === 'string' && /^media-[a-f0-9]{64}-[a-f0-9]{64}$/.test(value);
|
|
238
|
+
}
|
|
239
|
+
/*** Map server-derived scope hashes into the one private media directory tree. */
|
|
240
|
+
function mediaDirectory(root, key) {
|
|
241
|
+
return join(root, key);
|
|
242
|
+
}
|
package/dist/features/trusted-social-manager-host/adapters/createSocialOperationStoresAsync.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { FacebookPagesOperationStore, InstagramProfessionalOperationStore, LinkedinOperationStore, PinterestOperationStore } from '@ankhorage/social';
|
|
2
|
+
export type SocialOperationStores = Readonly<{
|
|
3
|
+
facebookPages: FacebookPagesOperationStore;
|
|
4
|
+
instagramProfessional: InstagramProfessionalOperationStore;
|
|
5
|
+
linkedin: LinkedinOperationStore;
|
|
6
|
+
pinterest: PinterestOperationStore;
|
|
7
|
+
}>;
|
|
8
|
+
/***
|
|
9
|
+
* Create filesystem-backed Social provider operation stores for one trusted local host.
|
|
10
|
+
* The directory is explicitly single-writer: atomic writes and this mutex coordinate one
|
|
11
|
+
* adapter instance, while multiple processes require an external lock.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createSocialOperationStoresAsync(directory: string): Promise<SocialOperationStores>;
|
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
|
+
};
|