anhedral 0.13.0 → 0.16.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 +23 -229
- package/assets/patches/audit-deps.mjs +12 -2
- package/assets/stack/ai-schema.ts.txt +5 -0
- package/assets/stack/ai.ts.txt +30 -0
- package/assets/stack/api-index.ts.txt +15 -0
- package/assets/stack/auth-features.ts.txt +4 -0
- package/assets/stack/auth-journal.json.txt +13 -0
- package/assets/stack/auth-migration.sql.txt +53 -0
- package/assets/stack/auth-routes.ts.txt +11 -0
- package/assets/stack/auth-schema.ts.txt +64 -0
- package/assets/stack/auth-snapshot.json.txt +346 -0
- package/assets/stack/auth.ts.txt +32 -0
- package/assets/stack/billing-mode.ts.txt +8 -0
- package/assets/stack/billing-schema.ts.txt +11 -0
- package/assets/stack/desktop.rs.txt +21 -0
- package/assets/stack/entitlement-guard.ts.txt +32 -0
- package/assets/stack/entitlements.ts.txt +8 -0
- package/assets/stack/events-schema.ts.txt +12 -0
- package/assets/stack/events.ts.txt +59 -0
- package/assets/stack/mobile-auth.ts.txt +25 -0
- package/assets/stack/mobile-billing.tsx.txt +63 -0
- package/assets/stack/mobile-session.tsx.txt +83 -0
- package/assets/stack/native-button.tsx.txt +108 -0
- package/assets/stack/native-global.css.txt +4 -0
- package/assets/stack/native-lib-utils.ts.txt +5 -0
- package/assets/stack/native-metro.config.js.txt +6 -0
- package/assets/stack/native-nativewind-env.d.ts.txt +1 -0
- package/assets/stack/native-tailwind.config.js.txt +73 -0
- package/assets/stack/native-text.tsx.txt +89 -0
- package/assets/stack/purchases.ts.txt +65 -0
- package/assets/stack/revenuecat.ts.txt +88 -0
- package/assets/stack/rtc-schema.ts.txt +10 -0
- package/assets/stack/rtc.ts.txt +122 -0
- package/assets/stack/security.ts.txt +31 -0
- package/assets/stack/storage.ts.txt +36 -0
- package/assets/stack/stripe.ts.txt +126 -0
- package/assets/stack/web-sign-in.tsx.txt +51 -0
- package/assets/ui/LICENSE.react-native-reusables +21 -0
- package/assets/ui/LICENSE.shadcn +21 -0
- package/assets/ui/UPSTREAM.json +39 -0
- package/assets/ui/catalog.json +891 -0
- package/assets/ui/extensions/registry.json +6 -0
- package/assets/ui/native/_controls.tsx +521 -0
- package/assets/ui/native/_core.tsx +118 -0
- package/assets/ui/native/_data.tsx +200 -0
- package/assets/ui/native/_display.tsx +453 -0
- package/assets/ui/native/_feedback.tsx +242 -0
- package/assets/ui/native/_legacy-dialog.tsx +38 -0
- package/assets/ui/native/_motion.tsx +520 -0
- package/assets/ui/native/_overlay.tsx +402 -0
- package/assets/ui/native/_selection.tsx +366 -0
- package/assets/ui/native/accordion.tsx +1 -0
- package/assets/ui/native/alert-dialog.tsx +11 -0
- package/assets/ui/native/alert.tsx +1 -0
- package/assets/ui/native/aspect-ratio.tsx +1 -0
- package/assets/ui/native/attachment.tsx +8 -0
- package/assets/ui/native/avatar.tsx +8 -0
- package/assets/ui/native/badge.tsx +1 -0
- package/assets/ui/native/breadcrumb.tsx +9 -0
- package/assets/ui/native/bubble.tsx +1 -0
- package/assets/ui/native/button-group.tsx +1 -0
- package/assets/ui/native/button.tsx +86 -0
- package/assets/ui/native/calendar.tsx +170 -0
- package/assets/ui/native/card.tsx +44 -0
- package/assets/ui/native/carousel.tsx +1 -0
- package/assets/ui/native/chart.tsx +193 -0
- package/assets/ui/native/checkbox.tsx +1 -0
- package/assets/ui/native/code-block.tsx +137 -0
- package/assets/ui/native/collapsible.tsx +1 -0
- package/assets/ui/native/combobox.tsx +1 -0
- package/assets/ui/native/command.tsx +11 -0
- package/assets/ui/native/context-menu.tsx +16 -0
- package/assets/ui/native/data-table.tsx +1 -0
- package/assets/ui/native/date-picker.tsx +43 -0
- package/assets/ui/native/dialog.tsx +15 -0
- package/assets/ui/native/direction.tsx +1 -0
- package/assets/ui/native/drawer.tsx +10 -0
- package/assets/ui/native/dropdown-menu.tsx +16 -0
- package/assets/ui/native/empty.tsx +8 -0
- package/assets/ui/native/field.tsx +13 -0
- package/assets/ui/native/hover-card.tsx +1 -0
- package/assets/ui/native/input-group.tsx +8 -0
- package/assets/ui/native/input-otp.tsx +1 -0
- package/assets/ui/native/input.tsx +28 -0
- package/assets/ui/native/item.tsx +12 -0
- package/assets/ui/native/kbd.tsx +1 -0
- package/assets/ui/native/label.tsx +4 -0
- package/assets/ui/native/layout.tsx +26 -0
- package/assets/ui/native/marker.tsx +1 -0
- package/assets/ui/native/menubar.tsx +17 -0
- package/assets/ui/native/message-scroller.tsx +6 -0
- package/assets/ui/native/message.tsx +10 -0
- package/assets/ui/native/native-select.tsx +1 -0
- package/assets/ui/native/navigation-menu.tsx +10 -0
- package/assets/ui/native/pagination.tsx +9 -0
- package/assets/ui/native/popover.tsx +1 -0
- package/assets/ui/native/progress.tsx +1 -0
- package/assets/ui/native/questionnaire.tsx +130 -0
- package/assets/ui/native/radio-group.tsx +1 -0
- package/assets/ui/native/registry.json +1103 -0
- package/assets/ui/native/resizable.tsx +1 -0
- package/assets/ui/native/scroll-area.tsx +1 -0
- package/assets/ui/native/select.tsx +11 -0
- package/assets/ui/native/separator.tsx +1 -0
- package/assets/ui/native/sheet.tsx +10 -0
- package/assets/ui/native/sidebar.tsx +22 -0
- package/assets/ui/native/skeleton.tsx +1 -0
- package/assets/ui/native/slider.tsx +1 -0
- package/assets/ui/native/spinner.tsx +1 -0
- package/assets/ui/native/switch.tsx +1 -0
- package/assets/ui/native/table.tsx +10 -0
- package/assets/ui/native/tabs.tsx +1 -0
- package/assets/ui/native/text.tsx +20 -0
- package/assets/ui/native/textarea.tsx +1 -0
- package/assets/ui/native/theme-switcher.tsx +22 -0
- package/assets/ui/native/toast.tsx +11 -0
- package/assets/ui/native/toggle-group.tsx +1 -0
- package/assets/ui/native/toggle.tsx +1 -0
- package/assets/ui/native/tooltip.tsx +1 -0
- package/assets/ui/native/trace.tsx +20 -0
- package/assets/ui/native/typography.tsx +15 -0
- package/assets/ui/theme-storage.ts +25 -0
- package/assets/ui/theme.tsx +66 -0
- package/assets/ui/web/button.tsx +59 -0
- package/assets/ui/web/input.tsx +21 -0
- package/assets/ui/web/label.tsx +19 -0
- package/dist/bin.js +7 -12
- package/dist/capabilities.d.ts +4 -33
- package/dist/capabilities.js +2 -84
- package/dist/capability-catalog.d.ts +83 -0
- package/dist/capability-catalog.js +121 -0
- package/dist/capability-command.d.ts +3 -0
- package/dist/capability-command.js +188 -0
- package/dist/capability-editor.d.ts +17 -0
- package/dist/capability-editor.js +97 -0
- package/dist/capability-recipes.d.ts +4 -0
- package/dist/capability-recipes.js +520 -0
- package/dist/capability-references.d.ts +4 -0
- package/dist/capability-references.js +7 -0
- package/dist/cli-commands.d.ts +1 -2
- package/dist/cli-commands.js +10 -104
- package/dist/cli-output.d.ts +1 -2
- package/dist/cli-output.js +6 -251
- package/dist/command-schema.js +40 -374
- package/dist/dependencies.d.ts +34 -62
- package/dist/dependencies.js +112 -199
- package/dist/index.d.ts +1 -45
- package/dist/index.js +1 -30
- package/dist/platforms/mobile.js +29 -139
- package/dist/platforms/web.d.ts +3 -0
- package/dist/platforms/web.js +251 -0
- package/dist/project-config.d.ts +2 -13
- package/dist/project-config.js +10 -83
- package/dist/registry/context.js +0 -1
- package/dist/registry/options.js +6 -40
- package/dist/registry/resolve.d.ts +3 -4
- package/dist/registry/resolve.js +18 -108
- package/dist/registry/types.d.ts +1 -7
- package/dist/registry/usage.js +3 -9
- package/dist/runtime-policy.d.ts +0 -2
- package/dist/runtime-policy.js +0 -16
- package/dist/scaffold/backend.d.ts +0 -4
- package/dist/scaffold/backend.js +1 -189
- package/dist/scaffold/bootstrap.d.ts +2 -3
- package/dist/scaffold/bootstrap.js +37 -43
- package/dist/scaffold/compose.js +2 -14
- package/dist/scaffold/database.d.ts +1 -1
- package/dist/scaffold/database.js +19 -126
- package/dist/scaffold/documentation.d.ts +0 -1
- package/dist/scaffold/documentation.js +82 -84
- package/dist/scaffold/expo-runtime.d.ts +2 -2
- package/dist/scaffold/expo-runtime.js +9 -101
- package/dist/scaffold/native-ui.d.ts +2 -0
- package/dist/scaffold/native-ui.js +74 -0
- package/dist/scaffold/stack-documentation.d.ts +0 -1
- package/dist/scaffold/stack-documentation.js +3 -49
- package/dist/scaffold/storage.d.ts +2 -0
- package/dist/scaffold/storage.js +21 -0
- package/dist/scaffold/styling.d.ts +1 -1
- package/dist/scaffold/styling.js +20 -28
- package/dist/scaffold/workspace.js +63 -199
- package/dist/stack-registry.d.ts +2 -58
- package/dist/stack-registry.js +22 -446
- package/dist/standard.js +7 -24
- package/dist/ui-catalog.d.ts +26 -0
- package/dist/ui-catalog.js +31 -0
- package/dist/ui-command.d.ts +3 -0
- package/dist/ui-command.js +219 -0
- package/dist/ui-project.d.ts +22 -0
- package/dist/ui-project.js +110 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/anhedral-stack.md +4 -119
- package/docs/capabilities.md +110 -0
- package/docs/native-ui.md +102 -0
- package/docs/software-factory.md +16 -577
- package/docs/stack-standard.md +11 -307
- package/docs/ui.md +33 -0
- package/docs/verification.md +511 -0
- package/package.json +26 -27
- package/dist/access.d.ts +0 -10
- package/dist/access.js +0 -93
- package/dist/auth-schema-snapshot.d.ts +0 -346
- package/dist/auth-schema-snapshot.js +0 -347
- package/dist/auth-schema.d.ts +0 -1
- package/dist/auth-schema.js +0 -57
- package/dist/capability-contract.d.ts +0 -47
- package/dist/capability-contract.js +0 -168
- package/dist/catalog-model.d.ts +0 -24
- package/dist/catalog-model.js +0 -1
- package/dist/catalog-registration.d.ts +0 -16
- package/dist/catalog-registration.js +0 -22
- package/dist/catalog-snapshot.d.ts +0 -5
- package/dist/catalog-snapshot.js +0 -97
- package/dist/cli-delivery.d.ts +0 -3
- package/dist/cli-delivery.js +0 -172
- package/dist/cloudflare.d.ts +0 -25
- package/dist/cloudflare.js +0 -226
- package/dist/delivery-evidence.d.ts +0 -62
- package/dist/delivery-evidence.js +0 -186
- package/dist/delivery.d.ts +0 -88
- package/dist/delivery.js +0 -570
- package/dist/inspector-model.d.ts +0 -123
- package/dist/inspector-model.js +0 -1
- package/dist/integration-service-readers.d.ts +0 -13
- package/dist/integration-service-readers.js +0 -173
- package/dist/lifecycle-model.d.ts +0 -15
- package/dist/lifecycle-model.js +0 -20
- package/dist/lifecycle.d.ts +0 -11
- package/dist/lifecycle.js +0 -91
- package/dist/management.d.ts +0 -3
- package/dist/management.js +0 -47
- package/dist/mobile-delivery.d.ts +0 -77
- package/dist/mobile-delivery.js +0 -366
- package/dist/neon-service-data.d.ts +0 -8
- package/dist/neon-service-data.js +0 -117
- package/dist/platforms/desktop.d.ts +0 -2
- package/dist/platforms/desktop.js +0 -415
- package/dist/platforms/extension.d.ts +0 -2
- package/dist/platforms/extension.js +0 -328
- package/dist/project-catalog.d.ts +0 -41
- package/dist/project-catalog.js +0 -288
- package/dist/project-inspector.d.ts +0 -4
- package/dist/project-inspector.js +0 -702
- package/dist/project-lifecycle.d.ts +0 -25
- package/dist/project-lifecycle.js +0 -57
- package/dist/project-state.d.ts +0 -49
- package/dist/project-state.js +0 -563
- package/dist/provider-auth.d.ts +0 -41
- package/dist/provider-auth.js +0 -215
- package/dist/provider-readiness.d.ts +0 -30
- package/dist/provider-readiness.js +0 -104
- package/dist/providers/adapters.d.ts +0 -17
- package/dist/providers/adapters.js +0 -278
- package/dist/providers/api.d.ts +0 -29
- package/dist/providers/api.js +0 -253
- package/dist/providers/config.d.ts +0 -15
- package/dist/providers/config.js +0 -182
- package/dist/providers/definitions.d.ts +0 -318
- package/dist/providers/definitions.js +0 -127
- package/dist/providers/provision.d.ts +0 -24
- package/dist/providers/provision.js +0 -76
- package/dist/providers/receipts.d.ts +0 -20
- package/dist/providers/receipts.js +0 -66
- package/dist/providers/recovery-verification.d.ts +0 -7
- package/dist/providers/recovery-verification.js +0 -112
- package/dist/providers/recovery.d.ts +0 -30
- package/dist/providers/recovery.js +0 -34
- package/dist/providers/runtime.d.ts +0 -10
- package/dist/providers/runtime.js +0 -265
- package/dist/providers/schema.d.ts +0 -28
- package/dist/providers/schema.js +0 -49
- package/dist/providers/state.d.ts +0 -55
- package/dist/providers/state.js +0 -289
- package/dist/provision.d.ts +0 -55
- package/dist/provision.js +0 -284
- package/dist/r2-service-data.d.ts +0 -2
- package/dist/r2-service-data.js +0 -136
- package/dist/readiness.d.ts +0 -23
- package/dist/readiness.js +0 -122
- package/dist/registry/add.d.ts +0 -24
- package/dist/registry/add.js +0 -270
- package/dist/registry/bootstrap.d.ts +0 -2
- package/dist/registry/bootstrap.js +0 -14
- package/dist/registry/commands.d.ts +0 -3
- package/dist/registry/commands.js +0 -85
- package/dist/registry/presets.d.ts +0 -4
- package/dist/registry/presets.js +0 -41
- package/dist/registry/preview.d.ts +0 -11
- package/dist/registry/preview.js +0 -91
- package/dist/registry/receipt.d.ts +0 -3
- package/dist/registry/receipt.js +0 -68
- package/dist/repository.d.ts +0 -24
- package/dist/repository.js +0 -96
- package/dist/scaffold/auth.d.ts +0 -3
- package/dist/scaffold/auth.js +0 -151
- package/dist/scaffold/billing-config.d.ts +0 -1
- package/dist/scaffold/billing-config.js +0 -73
- package/dist/scaffold/billing-contracts.d.ts +0 -2
- package/dist/scaffold/billing-contracts.js +0 -48
- package/dist/scaffold/billing-providers.d.ts +0 -1
- package/dist/scaffold/billing-providers.js +0 -249
- package/dist/scaffold/billing-schema.d.ts +0 -1
- package/dist/scaffold/billing-schema.js +0 -34
- package/dist/scaffold/billing-security.d.ts +0 -1
- package/dist/scaffold/billing-security.js +0 -46
- package/dist/scaffold/billing-server.d.ts +0 -1
- package/dist/scaffold/billing-server.js +0 -139
- package/dist/scaffold/billing-store.d.ts +0 -1
- package/dist/scaffold/billing-store.js +0 -72
- package/dist/scaffold/billing.d.ts +0 -2
- package/dist/scaffold/billing.js +0 -216
- package/dist/scaffold/cloudflare-services.d.ts +0 -8
- package/dist/scaffold/cloudflare-services.js +0 -198
- package/dist/scaffold/expo-auth.d.ts +0 -3
- package/dist/scaffold/expo-auth.js +0 -192
- package/dist/scaffold/expo-billing.d.ts +0 -3
- package/dist/scaffold/expo-billing.js +0 -292
- package/dist/scaffold/integrations.d.ts +0 -4
- package/dist/scaffold/integrations.js +0 -12
- package/dist/scaffold/lint.d.ts +0 -4
- package/dist/scaffold/lint.js +0 -56
- package/dist/scaffold/neon-realtime.d.ts +0 -2
- package/dist/scaffold/neon-realtime.js +0 -97
- package/dist/scaffold/services.d.ts +0 -5
- package/dist/scaffold/services.js +0 -108
- package/dist/scaffold/stream.d.ts +0 -2
- package/dist/scaffold/stream.js +0 -114
- package/dist/scaffold/void.d.ts +0 -4
- package/dist/scaffold/void.js +0 -264
- package/dist/scaffold/web.d.ts +0 -2
- package/dist/scaffold/web.js +0 -130
- package/dist/service-deletion.d.ts +0 -29
- package/dist/service-deletion.js +0 -195
- package/dist/service-descriptor.d.ts +0 -10
- package/dist/service-descriptor.js +0 -78
- package/dist/service-model.d.ts +0 -49
- package/dist/service-model.js +0 -8
- package/dist/service-operations.d.ts +0 -29
- package/dist/service-operations.js +0 -78
- package/dist/service-readers.d.ts +0 -8
- package/dist/service-readers.js +0 -247
- package/dist/services-command.d.ts +0 -39
- package/dist/services-command.js +0 -102
- package/dist/ship.d.ts +0 -348
- package/dist/ship.js +0 -239
- package/dist/vercel-project.d.ts +0 -101
- package/dist/vercel-project.js +0 -305
- package/dist/vercel.d.ts +0 -67
- package/dist/vercel.js +0 -581
- package/dist/vinext.d.ts +0 -2
- package/dist/vinext.js +0 -86
- package/dist/void-execution.d.ts +0 -10
- package/dist/void-execution.js +0 -94
- package/dist/void-migrations.d.ts +0 -6
- package/dist/void-migrations.js +0 -110
- package/dist/void-state.d.ts +0 -16
- package/dist/void-state.js +0 -65
- package/dist/void.d.ts +0 -3
- package/dist/void.js +0 -5
package/dist/providers/state.js
DELETED
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
import path from 'node:path';
|
|
2
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
3
|
-
import { existsSync, lstatSync, mkdirSync, realpathSync } from 'node:fs';
|
|
4
|
-
import { readBoundedFile, MAX_PROJECT_JSON_BYTES } from '../bounded-file.js';
|
|
5
|
-
import { atomicJson } from '../project-state.js';
|
|
6
|
-
import { digest, object, ProviderRequestError } from './api.js';
|
|
7
|
-
import { verifyRecoveredResource } from './recovery-verification.js';
|
|
8
|
-
import { splitReceipt, validReceipt, validCredentials, restoreReceipt, } from './receipts.js';
|
|
9
|
-
import { INTEGRATION_PROVIDERS, } from './config.js';
|
|
10
|
-
function validDescriptor(value) {
|
|
11
|
-
return (record(value) &&
|
|
12
|
-
Object.keys(value).every((key) => ['key', 'route'].includes(key)) &&
|
|
13
|
-
typeof value.key === 'string' &&
|
|
14
|
-
/^(project|application|domain|web-app|ios-app|android-app|sending-key|webhook|service-account|client-key)$/.test(value.key) &&
|
|
15
|
-
typeof value.route === 'string' &&
|
|
16
|
-
value.route.length <= 1024 &&
|
|
17
|
-
/^\/[a-zA-Z0-9_/-]+$/.test(value.route) &&
|
|
18
|
-
!value.route.includes('//'));
|
|
19
|
-
}
|
|
20
|
-
function record(value) {
|
|
21
|
-
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
|
22
|
-
}
|
|
23
|
-
/** Validate saved operations before trusting their replay protection or resource values. */
|
|
24
|
-
function validState(value) {
|
|
25
|
-
if (!record(value) ||
|
|
26
|
-
value.schemaVersion !== 2 ||
|
|
27
|
-
Object.keys(value).some((key) => !['schemaVersion', 'providers'].includes(key)) ||
|
|
28
|
-
!record(value.providers))
|
|
29
|
-
return false;
|
|
30
|
-
return Object.entries(value.providers).every(([id, provider]) => {
|
|
31
|
-
if (!INTEGRATION_PROVIDERS.includes(id) ||
|
|
32
|
-
!record(provider) ||
|
|
33
|
-
Object.keys(provider).some((key) => !['config', 'operations'].includes(key)) ||
|
|
34
|
-
typeof provider.config !== 'string' ||
|
|
35
|
-
!/^[a-f0-9]{64}$/.test(provider.config) ||
|
|
36
|
-
!record(provider.operations))
|
|
37
|
-
return false;
|
|
38
|
-
return Object.entries(provider.operations).every(([key, operation]) => {
|
|
39
|
-
if (!/^[a-f0-9]{64}$/.test(key) ||
|
|
40
|
-
!record(operation) ||
|
|
41
|
-
Object.keys(operation).some((field) => !['status', 'value', 'credentialFields', 'descriptor'].includes(field)))
|
|
42
|
-
return false;
|
|
43
|
-
if (!validDescriptor(operation.descriptor))
|
|
44
|
-
return false;
|
|
45
|
-
if (operation.status === 'pending')
|
|
46
|
-
return !Object.hasOwn(operation, 'value') && !Object.hasOwn(operation, 'credentialFields');
|
|
47
|
-
if (operation.status !== 'complete' || !record(operation.value))
|
|
48
|
-
return false;
|
|
49
|
-
return (validReceipt(id, operation.value) &&
|
|
50
|
-
(operation.credentialFields === undefined ||
|
|
51
|
-
(Array.isArray(operation.credentialFields) &&
|
|
52
|
-
new Set(operation.credentialFields).size === operation.credentialFields.length &&
|
|
53
|
-
operation.credentialFields.every((field) => typeof field === 'string' &&
|
|
54
|
-
validCredentials(id, { [field]: 'validation-placeholder' })))));
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
export async function privateStateDirectory(root, run) {
|
|
59
|
-
root = realpathSync(root);
|
|
60
|
-
const directory = path.join(root, '.anhedral');
|
|
61
|
-
if (existsSync(directory) &&
|
|
62
|
-
(lstatSync(directory).isSymbolicLink() || !lstatSync(directory).isDirectory()))
|
|
63
|
-
throw new Error('Provider state directory must be a regular directory.');
|
|
64
|
-
const environment = { ...process.env, CI: '1', NO_COLOR: '1' };
|
|
65
|
-
if (realpathSync((await run('git', ['rev-parse', '--show-toplevel'], root, environment)).trim()) !==
|
|
66
|
-
root)
|
|
67
|
-
throw new Error('Provider configuration requires its own application repository.');
|
|
68
|
-
for (const file of [
|
|
69
|
-
'.anhedral/providers.json',
|
|
70
|
-
'.anhedral/provider-credentials.json',
|
|
71
|
-
'.anhedral/runtime.json',
|
|
72
|
-
]) {
|
|
73
|
-
if ((await run('git', ['ls-files', '--', file], root, environment)).trim())
|
|
74
|
-
throw new Error('Provider state must not be tracked.');
|
|
75
|
-
if ((await run('git', ['check-ignore', '--no-index', '--', file], root, environment)).trim() !==
|
|
76
|
-
file)
|
|
77
|
-
throw new Error('Ignore .anhedral/ before provisioning providers.');
|
|
78
|
-
}
|
|
79
|
-
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
80
|
-
if (realpathSync(directory) !== directory)
|
|
81
|
-
throw new Error('Provider state directory escapes the project.');
|
|
82
|
-
return directory;
|
|
83
|
-
}
|
|
84
|
-
function validCredentialsState(value) {
|
|
85
|
-
return (record(value) &&
|
|
86
|
-
value.schemaVersion === 1 &&
|
|
87
|
-
!Object.keys(value).some((key) => !['schemaVersion', 'providers'].includes(key)) &&
|
|
88
|
-
record(value.providers) &&
|
|
89
|
-
Object.entries(value.providers).every(([id, operations]) => INTEGRATION_PROVIDERS.includes(id) &&
|
|
90
|
-
record(operations) &&
|
|
91
|
-
Object.entries(operations).every(([key, entries]) => /^[a-f0-9]{64}$/.test(key) && validCredentials(id, entries))));
|
|
92
|
-
}
|
|
93
|
-
function privateJson(file) {
|
|
94
|
-
const stat = lstatSync(file);
|
|
95
|
-
if (!stat.isFile() || stat.isSymbolicLink())
|
|
96
|
-
throw new Error('Provider state must be a regular private file.');
|
|
97
|
-
if (process.platform !== 'win32' && stat.mode & 0o077)
|
|
98
|
-
throw new Error('Provider state contains credentials; restrict its permissions to 0600.');
|
|
99
|
-
try {
|
|
100
|
-
return JSON.parse(readBoundedFile(file, MAX_PROJECT_JSON_BYTES).toString('utf8'));
|
|
101
|
-
}
|
|
102
|
-
catch {
|
|
103
|
-
throw new Error('Invalid provider state.');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
export function readProviderStateStatus(directory) {
|
|
107
|
-
const file = path.join(directory, 'providers.json');
|
|
108
|
-
if (!existsSync(file))
|
|
109
|
-
return { schemaVersion: 2, providers: [] };
|
|
110
|
-
if (lstatSync(directory).isSymbolicLink() || !lstatSync(directory).isDirectory())
|
|
111
|
-
throw new Error('Provider state directory must be a regular private directory.');
|
|
112
|
-
const state = privateJson(file);
|
|
113
|
-
if (!validState(state))
|
|
114
|
-
throw new Error('Invalid provider state.');
|
|
115
|
-
return {
|
|
116
|
-
schemaVersion: 2,
|
|
117
|
-
providers: Object.entries(state.providers).map(([id, provider]) => ({
|
|
118
|
-
provider: id,
|
|
119
|
-
operations: Object.entries(provider.operations).map(([operation, saved]) => ({
|
|
120
|
-
operation,
|
|
121
|
-
status: saved.status,
|
|
122
|
-
...(saved.value ? { resources: saved.value } : {}),
|
|
123
|
-
descriptor: saved.descriptor,
|
|
124
|
-
recovery: ['sending-key', 'webhook', 'service-account'].includes(saved.descriptor.key)
|
|
125
|
-
? 'requires-one-time-credential'
|
|
126
|
-
: ['project', 'application', 'domain', 'web-app', 'ios-app', 'android-app'].includes(saved.descriptor.key)
|
|
127
|
-
? 'verified-resource'
|
|
128
|
-
: 'unsupported-operation',
|
|
129
|
-
})),
|
|
130
|
-
})),
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
/** Store credentials before completing a minimal receipt. A crash leaves a pending
|
|
134
|
-
* operation, never permission to replay a provider mutation with an unknown outcome. */
|
|
135
|
-
export class ProvisioningState {
|
|
136
|
-
write;
|
|
137
|
-
file;
|
|
138
|
-
credentialsFile;
|
|
139
|
-
state;
|
|
140
|
-
credentials;
|
|
141
|
-
constructor(directory, write = atomicJson) {
|
|
142
|
-
this.write = write;
|
|
143
|
-
if (lstatSync(directory).isSymbolicLink() || !lstatSync(directory).isDirectory())
|
|
144
|
-
throw new Error('Provider state directory must be a regular private directory.');
|
|
145
|
-
directory = realpathSync(directory);
|
|
146
|
-
this.file = path.join(directory, 'providers.json');
|
|
147
|
-
this.credentialsFile = path.join(directory, 'provider-credentials.json');
|
|
148
|
-
const credentials = existsSync(this.credentialsFile)
|
|
149
|
-
? privateJson(this.credentialsFile)
|
|
150
|
-
: { schemaVersion: 1, providers: {} };
|
|
151
|
-
if (!validCredentialsState(credentials))
|
|
152
|
-
throw new Error('Invalid provider credential state.');
|
|
153
|
-
this.credentials = credentials;
|
|
154
|
-
const state = existsSync(this.file)
|
|
155
|
-
? privateJson(this.file)
|
|
156
|
-
: { schemaVersion: 2, providers: {} };
|
|
157
|
-
if (!validState(state))
|
|
158
|
-
throw new Error('Invalid provider state.');
|
|
159
|
-
this.state = state;
|
|
160
|
-
this.assertCredentials();
|
|
161
|
-
}
|
|
162
|
-
assertCredentials() {
|
|
163
|
-
for (const [id, provider] of Object.entries(this.state.providers)) {
|
|
164
|
-
for (const [operation, saved] of Object.entries(provider.operations)) {
|
|
165
|
-
const credentials = this.credentials.providers[id]?.[operation] ?? {};
|
|
166
|
-
if (saved.credentialFields?.some((field) => !credentials[field]))
|
|
167
|
-
throw new Error('Provider creation credentials are missing. Restore private state before retrying.');
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
retainCredentials(id, operation, entries) {
|
|
172
|
-
if (!Object.keys(entries).length)
|
|
173
|
-
return;
|
|
174
|
-
const provider = (this.credentials.providers[id] ??= {});
|
|
175
|
-
const previous = provider[operation];
|
|
176
|
-
// Compare the private credential records directly; credential values must not
|
|
177
|
-
// flow through the SHA-256 helper used for public operation fingerprints.
|
|
178
|
-
if (previous && !isDeepStrictEqual(previous, entries))
|
|
179
|
-
throw new Error('Provider credential state conflicts with the saved creation result.');
|
|
180
|
-
provider[operation] = entries;
|
|
181
|
-
}
|
|
182
|
-
/** No configurations, credentials, provider payloads or mutation bodies in status. */
|
|
183
|
-
status() {
|
|
184
|
-
return {
|
|
185
|
-
schemaVersion: 2,
|
|
186
|
-
providers: Object.entries(this.state.providers).map(([id, provider]) => ({
|
|
187
|
-
provider: id,
|
|
188
|
-
operations: Object.entries(provider.operations).map(([operation, saved]) => ({
|
|
189
|
-
operation,
|
|
190
|
-
status: saved.status,
|
|
191
|
-
resources: saved.value ?? {},
|
|
192
|
-
})),
|
|
193
|
-
})),
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
context(id, settings) {
|
|
197
|
-
const hash = digest(settings);
|
|
198
|
-
const saved = this.state.providers[id];
|
|
199
|
-
if (saved && saved.config !== hash)
|
|
200
|
-
throw new Error(`${id}: desired configuration changed. Reconcile saved resource IDs before changing provider state.`);
|
|
201
|
-
if (!saved)
|
|
202
|
-
this.state.providers[id] = { config: hash, operations: {} };
|
|
203
|
-
}
|
|
204
|
-
async recoverResource(id, operation, resource, settings, api, beforeCommit = () => { }) {
|
|
205
|
-
this.context(id, settings);
|
|
206
|
-
const provider = this.state.providers[id];
|
|
207
|
-
const saved = provider.operations[operation];
|
|
208
|
-
if (!saved || saved.status !== 'pending')
|
|
209
|
-
throw new Error('Only an explicitly identified pending provider operation can be reconciled.');
|
|
210
|
-
const receipt = await verifyRecoveredResource(id, saved.descriptor, resource, settings, api);
|
|
211
|
-
beforeCommit();
|
|
212
|
-
const complete = {
|
|
213
|
-
status: 'complete',
|
|
214
|
-
descriptor: saved.descriptor,
|
|
215
|
-
value: receipt,
|
|
216
|
-
};
|
|
217
|
-
provider.operations[operation] = complete;
|
|
218
|
-
try {
|
|
219
|
-
this.write(this.file, this.state);
|
|
220
|
-
}
|
|
221
|
-
catch (error) {
|
|
222
|
-
provider.operations[operation] = saved;
|
|
223
|
-
throw error;
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
provider: id,
|
|
227
|
-
operation,
|
|
228
|
-
status: 'complete',
|
|
229
|
-
resources: receipt,
|
|
230
|
-
mutation: 'local-receipt-only',
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
async create(id, key, api, route, body) {
|
|
234
|
-
const state = this.state.providers[id];
|
|
235
|
-
if (!state)
|
|
236
|
-
throw new Error('Select a provider configuration before creating resources.');
|
|
237
|
-
const operation = digest({ key, route, body });
|
|
238
|
-
const saved = state.operations[operation];
|
|
239
|
-
if (saved?.status === 'complete')
|
|
240
|
-
return restoreReceipt(saved.value, this.credentials.providers[id]?.[operation] ?? {});
|
|
241
|
-
if (saved)
|
|
242
|
-
throw new Error(`${id}: interrupted ${key} creation has an unknown outcome. Inspect the provider and reconcile the saved operation before retrying.`);
|
|
243
|
-
const descriptor = { key, route };
|
|
244
|
-
if (!validDescriptor(descriptor))
|
|
245
|
-
throw new Error('Unsupported provider operation descriptor.');
|
|
246
|
-
state.operations[operation] = { status: 'pending', descriptor };
|
|
247
|
-
this.write(this.file, this.state);
|
|
248
|
-
let value;
|
|
249
|
-
try {
|
|
250
|
-
value = object(await api(route, 'POST', body, operation));
|
|
251
|
-
}
|
|
252
|
-
catch (error) {
|
|
253
|
-
if (error instanceof ProviderRequestError && error.rejected) {
|
|
254
|
-
delete state.operations[operation];
|
|
255
|
-
this.write(this.file, this.state);
|
|
256
|
-
}
|
|
257
|
-
throw error;
|
|
258
|
-
}
|
|
259
|
-
const { receipt, credentials } = splitReceipt(id, value);
|
|
260
|
-
const expected = id === 'resend' && key === 'sending-key'
|
|
261
|
-
? 'token'
|
|
262
|
-
: id === 'stripe' && key === 'webhook'
|
|
263
|
-
? 'secret'
|
|
264
|
-
: id === 'openai' && key === 'service-account'
|
|
265
|
-
? 'api_key'
|
|
266
|
-
: undefined;
|
|
267
|
-
if (expected && !credentials[expected])
|
|
268
|
-
throw new Error('Provider omitted the one-time creation credential. Inspect the pending operation before retrying.');
|
|
269
|
-
this.retainCredentials(id, operation, credentials);
|
|
270
|
-
if (Object.keys(credentials).length)
|
|
271
|
-
this.write(this.credentialsFile, this.credentials);
|
|
272
|
-
state.operations[operation] = {
|
|
273
|
-
status: 'complete',
|
|
274
|
-
descriptor,
|
|
275
|
-
value: receipt,
|
|
276
|
-
...(Object.keys(credentials).length
|
|
277
|
-
? { credentialFields: Object.keys(credentials) }
|
|
278
|
-
: {}),
|
|
279
|
-
};
|
|
280
|
-
try {
|
|
281
|
-
this.write(this.file, this.state);
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
state.operations[operation] = { status: 'pending', descriptor };
|
|
285
|
-
throw error;
|
|
286
|
-
}
|
|
287
|
-
return restoreReceipt(receipt, credentials);
|
|
288
|
-
}
|
|
289
|
-
}
|
package/dist/provision.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { type DeliveryRunner } from './project-state.js';
|
|
2
|
-
import { type CloudflareRequest } from './cloudflare.js';
|
|
3
|
-
interface Resource {
|
|
4
|
-
kind: 'd1' | 'r2' | 'kv' | 'queue' | 'stream';
|
|
5
|
-
name: string;
|
|
6
|
-
id?: string;
|
|
7
|
-
target: string;
|
|
8
|
-
field?: (string | number)[];
|
|
9
|
-
}
|
|
10
|
-
export declare function planProvisioning(root: string): {
|
|
11
|
-
projectId: string;
|
|
12
|
-
accountId: string | null;
|
|
13
|
-
resources: Resource[];
|
|
14
|
-
externalIntegrations: string[];
|
|
15
|
-
};
|
|
16
|
-
export declare function provisionApplication(root: string, dryRun?: boolean, run?: DeliveryRunner, suppliedRequest?: CloudflareRequest): Promise<{
|
|
17
|
-
schemaVersion: number;
|
|
18
|
-
projectId: string;
|
|
19
|
-
accountId: string | null;
|
|
20
|
-
checkedAt: string;
|
|
21
|
-
resources: {
|
|
22
|
-
kind: string;
|
|
23
|
-
name: string;
|
|
24
|
-
id: string;
|
|
25
|
-
reused: boolean;
|
|
26
|
-
createdByProject: boolean;
|
|
27
|
-
}[];
|
|
28
|
-
state: string;
|
|
29
|
-
externalIntegrations: string[];
|
|
30
|
-
} | {
|
|
31
|
-
mutation: boolean;
|
|
32
|
-
projectId: string;
|
|
33
|
-
accountId: string | null;
|
|
34
|
-
resources: Resource[];
|
|
35
|
-
externalIntegrations: string[];
|
|
36
|
-
} | {
|
|
37
|
-
mutation: boolean;
|
|
38
|
-
writer: "void";
|
|
39
|
-
deferredToDeployment: boolean;
|
|
40
|
-
projectId: string;
|
|
41
|
-
accountId: string | null;
|
|
42
|
-
resources: Resource[];
|
|
43
|
-
externalIntegrations: string[];
|
|
44
|
-
}>;
|
|
45
|
-
/** Reconcile committed identities against the customer account without creating resources or rewriting configuration. */
|
|
46
|
-
export declare function inspectApplicationResources(root: string, request: CloudflareRequest): Promise<{
|
|
47
|
-
resources: {
|
|
48
|
-
kind: string;
|
|
49
|
-
name: string;
|
|
50
|
-
id: string;
|
|
51
|
-
target: string;
|
|
52
|
-
}[];
|
|
53
|
-
externalIntegrations: string[];
|
|
54
|
-
}>;
|
|
55
|
-
export {};
|
package/dist/provision.js
DELETED
|
@@ -1,284 +0,0 @@
|
|
|
1
|
-
import { assertNoDeletedServices } from './service-deletion.js';
|
|
2
|
-
import { existsSync, readFileSync, realpathSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { readBoundedFile } from './bounded-file.js';
|
|
5
|
-
import { applyEdits, modify } from 'jsonc-parser';
|
|
6
|
-
import { atomicJson, authenticate, defaultRunner, environment, readApplication, readConfig, targetDirectory, withApplicationLock, } from './project-state.js';
|
|
7
|
-
import { cloudflareRequest, listCloudflare } from './cloudflare.js';
|
|
8
|
-
const resourceTypes = {
|
|
9
|
-
d1: { endpoint: '/d1/database', nameField: 'name', idField: 'uuid' },
|
|
10
|
-
r2: { endpoint: '/r2/buckets', nameField: 'name', idField: 'name' },
|
|
11
|
-
kv: { endpoint: '/storage/kv/namespaces', nameField: 'title', idField: 'id' },
|
|
12
|
-
queue: { endpoint: '/queues', nameField: 'queue_name', idField: 'queue_id' },
|
|
13
|
-
stream: { endpoint: '/pipelines/v1/streams', nameField: 'name', idField: 'id' },
|
|
14
|
-
};
|
|
15
|
-
async function listResources(request, kind) {
|
|
16
|
-
const { endpoint } = resourceTypes[kind];
|
|
17
|
-
if (kind === 'stream') {
|
|
18
|
-
const streams = [];
|
|
19
|
-
let bytes = 0;
|
|
20
|
-
for (let page = 0; page < 1000; page++) {
|
|
21
|
-
const response = await request('GET', `${endpoint}?page=${page}&per_page=100`);
|
|
22
|
-
if (!Array.isArray(response.result))
|
|
23
|
-
throw new Error('Unsupported stream listing.');
|
|
24
|
-
if (response.result.some((item) => !item || typeof item !== 'object' || Array.isArray(item)))
|
|
25
|
-
throw new Error('Unsupported stream resource.');
|
|
26
|
-
streams.push(...response.result);
|
|
27
|
-
bytes += Buffer.byteLength(JSON.stringify(response.result));
|
|
28
|
-
if (streams.length > 10_000 || bytes > 10 * 1024 * 1024)
|
|
29
|
-
throw new Error('Stream listing exceeded collection limit; select resource IDs explicitly.');
|
|
30
|
-
if (response.result.length < 100)
|
|
31
|
-
return streams;
|
|
32
|
-
}
|
|
33
|
-
throw new Error('Stream listing exceeded pagination limit.');
|
|
34
|
-
}
|
|
35
|
-
if (kind !== 'r2')
|
|
36
|
-
return listCloudflare(request, endpoint);
|
|
37
|
-
const value = (await request('GET', endpoint)).result;
|
|
38
|
-
if (!Array.isArray(value?.buckets) || value.truncated)
|
|
39
|
-
throw new Error('Unsupported or truncated R2 bucket listing; select resources explicitly with provider tooling.');
|
|
40
|
-
return value.buckets;
|
|
41
|
-
}
|
|
42
|
-
async function assertPrivateStream(request, id) {
|
|
43
|
-
if (!/^[a-f0-9]{32}$/i.test(id))
|
|
44
|
-
throw new Error('Invalid Basin stream ID.');
|
|
45
|
-
const stream = (await request('GET', `${resourceTypes.stream.endpoint}/${id}`)).result;
|
|
46
|
-
if (stream?.http?.enabled !== false ||
|
|
47
|
-
stream.worker_binding?.enabled !== true ||
|
|
48
|
-
stream.format?.type !== 'json')
|
|
49
|
-
throw new Error('Basin stream must use a private Worker binding with JSON events and HTTP ingestion disabled.');
|
|
50
|
-
}
|
|
51
|
-
/** Provisioning and recovery use the same independently verified private-storage policy. */
|
|
52
|
-
async function assertPrivateBucket(request, name) {
|
|
53
|
-
const endpoint = `${resourceTypes.r2.endpoint}/${encodeURIComponent(name)}/domains`;
|
|
54
|
-
const managed = (await request('GET', `${endpoint}/managed`)).result;
|
|
55
|
-
const custom = (await request('GET', `${endpoint}/custom`)).result;
|
|
56
|
-
if (managed?.enabled !== false ||
|
|
57
|
-
!Array.isArray(custom?.domains) ||
|
|
58
|
-
custom.domains.some((domain) => domain.enabled !== false))
|
|
59
|
-
throw new Error('Selected R2 bucket has public or unknown domain access; resolve it before using private storage.');
|
|
60
|
-
}
|
|
61
|
-
function resourceName(value) {
|
|
62
|
-
if (typeof value !== 'string' || !/^[a-z0-9][a-z0-9-]{0,62}$/.test(value))
|
|
63
|
-
throw new Error('Resources require explicit lowercase names of at most 63 characters.');
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
export function planProvisioning(root) {
|
|
67
|
-
const project = readApplication(root);
|
|
68
|
-
const resources = [];
|
|
69
|
-
for (const target of project.targets) {
|
|
70
|
-
const config = readConfig(targetDirectory(root, target));
|
|
71
|
-
if ((config.env && Object.keys(config.env).length) ||
|
|
72
|
-
(config.account_id && config.account_id !== project.accountId))
|
|
73
|
-
throw new Error('Reconcile target account/environment before provisioning.');
|
|
74
|
-
for (const [kind, field] of [
|
|
75
|
-
['d1', 'd1_databases'],
|
|
76
|
-
['r2', 'r2_buckets'],
|
|
77
|
-
['kv', 'kv_namespaces'],
|
|
78
|
-
]) {
|
|
79
|
-
const entries = config[field];
|
|
80
|
-
if (entries === undefined)
|
|
81
|
-
continue;
|
|
82
|
-
if (!Array.isArray(entries))
|
|
83
|
-
throw new Error(`Invalid ${field} configuration.`);
|
|
84
|
-
entries.forEach((entry, index) => {
|
|
85
|
-
if (!entry || typeof entry.binding !== 'string' || entry.jurisdiction)
|
|
86
|
-
throw new Error('Use an explicit provider workflow for jurisdiction-specific resources.');
|
|
87
|
-
const name = resourceName(kind === 'd1'
|
|
88
|
-
? entry.database_name
|
|
89
|
-
: kind === 'r2'
|
|
90
|
-
? entry.bucket_name
|
|
91
|
-
: (entry.anhedral_name ?? `${project.name}-${entry.binding.toLowerCase()}`));
|
|
92
|
-
const id = kind === 'd1' ? entry.database_id : kind === 'kv' ? entry.id : undefined;
|
|
93
|
-
resources.push({
|
|
94
|
-
kind,
|
|
95
|
-
name,
|
|
96
|
-
target: target.id,
|
|
97
|
-
id: typeof id === 'string' && !id.startsWith('REPLACE_WITH_') ? id : undefined,
|
|
98
|
-
field: kind === 'r2' ? undefined : [field, index, kind === 'd1' ? 'database_id' : 'id'],
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
const queues = config.queues;
|
|
103
|
-
for (const entry of [...(queues?.producers ?? []), ...(queues?.consumers ?? [])]) {
|
|
104
|
-
resources.push({ kind: 'queue', name: resourceName(entry.queue), target: target.id });
|
|
105
|
-
if ('dead_letter_queue' in entry && entry.dead_letter_queue)
|
|
106
|
-
resources.push({
|
|
107
|
-
kind: 'queue',
|
|
108
|
-
name: resourceName(entry.dead_letter_queue),
|
|
109
|
-
target: target.id,
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
if (config.pipelines !== undefined) {
|
|
113
|
-
if (!Array.isArray(config.pipelines))
|
|
114
|
-
throw new Error('Invalid Basin stream bindings.');
|
|
115
|
-
config.pipelines.forEach((entry, index) => {
|
|
116
|
-
if (!entry ||
|
|
117
|
-
typeof entry.binding !== 'string' ||
|
|
118
|
-
typeof entry.stream !== 'string' ||
|
|
119
|
-
entry.pipeline)
|
|
120
|
-
throw new Error('Use explicit Basin stream bindings.');
|
|
121
|
-
resources.push({
|
|
122
|
-
kind: 'stream',
|
|
123
|
-
name: resourceName(`${project.name}-${entry.binding.toLowerCase()}`),
|
|
124
|
-
target: target.id,
|
|
125
|
-
id: entry.stream.startsWith('REPLACE_WITH_') ? undefined : entry.stream,
|
|
126
|
-
field: ['pipelines', index, 'stream'],
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return {
|
|
132
|
-
projectId: project.id,
|
|
133
|
-
accountId: project.accountId,
|
|
134
|
-
resources,
|
|
135
|
-
externalIntegrations: ['hyperdrive'].filter((key) => project.targets.some((target) => key in readConfig(targetDirectory(root, target)))),
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
export async function provisionApplication(root, dryRun = false, run = defaultRunner, suppliedRequest) {
|
|
139
|
-
root = realpathSync(root);
|
|
140
|
-
const plan = planProvisioning(root);
|
|
141
|
-
assertNoDeletedServices(root, plan.resources);
|
|
142
|
-
if (dryRun)
|
|
143
|
-
return { ...plan, mutation: false };
|
|
144
|
-
if (readApplication(root).deliveryEngine === 'void')
|
|
145
|
-
return { ...plan, mutation: false, writer: 'void', deferredToDeployment: true };
|
|
146
|
-
return withApplicationLock(root, async () => {
|
|
147
|
-
const project = readApplication(root);
|
|
148
|
-
const plan = planProvisioning(root);
|
|
149
|
-
assertNoDeletedServices(root, plan.resources);
|
|
150
|
-
const configured = new Map(project.targets.map((target) => {
|
|
151
|
-
const file = path.join(targetDirectory(root, target), 'wrangler.jsonc');
|
|
152
|
-
return [file, readFileSync(file, 'utf8')];
|
|
153
|
-
}));
|
|
154
|
-
const assertConfiguration = () => {
|
|
155
|
-
if (JSON.stringify(readApplication(root)) !== JSON.stringify(project) ||
|
|
156
|
-
[...configured].some(([file, content]) => readFileSync(file, 'utf8') !== content))
|
|
157
|
-
throw new Error('Configuration changed during provisioning; inspect resource receipts before retrying.');
|
|
158
|
-
};
|
|
159
|
-
await authenticate(root, project, run);
|
|
160
|
-
const request = suppliedRequest ??
|
|
161
|
-
(await cloudflareRequest(project, targetDirectory(root, project.targets[0]), run, environment(project)));
|
|
162
|
-
const previousFile = path.join(root, '.anhedral/resources.json');
|
|
163
|
-
let previousResources = [];
|
|
164
|
-
if (existsSync(previousFile)) {
|
|
165
|
-
const previous = JSON.parse(readBoundedFile(previousFile, 256 * 1024).toString('utf8'));
|
|
166
|
-
if (previous.schemaVersion !== 1 ||
|
|
167
|
-
previous.projectId !== project.id ||
|
|
168
|
-
previous.accountId !== project.accountId ||
|
|
169
|
-
!Array.isArray(previous.resources) ||
|
|
170
|
-
previous.resources.length > 500 ||
|
|
171
|
-
previous.resources.some((entry) => !entry ||
|
|
172
|
-
typeof entry !== 'object' ||
|
|
173
|
-
Array.isArray(entry) ||
|
|
174
|
-
!['d1', 'r2', 'kv', 'queue', 'stream'].includes(String(entry.kind)) ||
|
|
175
|
-
['name', 'id'].some((field) => typeof entry[field] !== 'string') ||
|
|
176
|
-
typeof entry.reused !== 'boolean'))
|
|
177
|
-
throw new Error('Previous resource ownership cannot be verified.');
|
|
178
|
-
previousResources = previous.resources;
|
|
179
|
-
}
|
|
180
|
-
const receipts = [];
|
|
181
|
-
const resources = new Map();
|
|
182
|
-
for (const resource of plan.resources) {
|
|
183
|
-
assertConfiguration();
|
|
184
|
-
const key = `${resource.kind}:${resource.name}`;
|
|
185
|
-
let receipt = resources.get(key);
|
|
186
|
-
if (!receipt) {
|
|
187
|
-
const { endpoint, nameField, idField } = resourceTypes[resource.kind];
|
|
188
|
-
const existing = await listResources(request, resource.kind);
|
|
189
|
-
const matches = existing.filter((item) => item[nameField] === resource.name);
|
|
190
|
-
if (matches.length > 1)
|
|
191
|
-
throw new Error('Ambiguous provider resource name.');
|
|
192
|
-
let value = matches[0];
|
|
193
|
-
const reused = !!value;
|
|
194
|
-
if (resource.id && (!value || value[idField] !== resource.id))
|
|
195
|
-
throw new Error('Configured resource ID does not match its account/name; no replacement was created.');
|
|
196
|
-
assertConfiguration();
|
|
197
|
-
if (!value)
|
|
198
|
-
value = (await request('POST', endpoint, {
|
|
199
|
-
[nameField]: resource.name,
|
|
200
|
-
...(resource.kind === 'stream'
|
|
201
|
-
? {
|
|
202
|
-
http: { enabled: false, authentication: true },
|
|
203
|
-
worker_binding: { enabled: true },
|
|
204
|
-
format: { type: 'json', unstructured: true },
|
|
205
|
-
}
|
|
206
|
-
: {}),
|
|
207
|
-
})).result;
|
|
208
|
-
if (!value || typeof value[idField] !== 'string' || !value[idField])
|
|
209
|
-
throw new Error('Provider did not return a resource identity; inspect provider state before retrying.');
|
|
210
|
-
receipt = {
|
|
211
|
-
kind: resource.kind,
|
|
212
|
-
name: resource.name,
|
|
213
|
-
id: value[idField],
|
|
214
|
-
reused,
|
|
215
|
-
createdByProject: !reused ||
|
|
216
|
-
previousResources.some((previous) => previous.kind === resource.kind &&
|
|
217
|
-
previous.name === resource.name &&
|
|
218
|
-
previous.id === value[idField] &&
|
|
219
|
-
(previous.createdByProject === true || previous.reused === false)),
|
|
220
|
-
};
|
|
221
|
-
resources.set(key, receipt);
|
|
222
|
-
receipts.push(receipt);
|
|
223
|
-
// Save identities immediately: a subsequent operation can fail without losing creation evidence.
|
|
224
|
-
atomicJson(path.join(root, '.anhedral/resources.json'), {
|
|
225
|
-
schemaVersion: 1,
|
|
226
|
-
projectId: project.id,
|
|
227
|
-
accountId: project.accountId,
|
|
228
|
-
checkedAt: new Date().toISOString(),
|
|
229
|
-
resources: receipts,
|
|
230
|
-
state: 'reconciling',
|
|
231
|
-
});
|
|
232
|
-
if (resource.kind === 'r2')
|
|
233
|
-
await assertPrivateBucket(request, resource.name);
|
|
234
|
-
if (resource.kind === 'stream')
|
|
235
|
-
await assertPrivateStream(request, receipt.id);
|
|
236
|
-
}
|
|
237
|
-
assertConfiguration();
|
|
238
|
-
if (resource.id && receipt.id !== resource.id)
|
|
239
|
-
throw new Error('Targets disagree about a shared resource ID.');
|
|
240
|
-
if (resource.field) {
|
|
241
|
-
const target = project.targets.find((item) => item.id === resource.target);
|
|
242
|
-
const file = path.join(targetDirectory(root, target), 'wrangler.jsonc');
|
|
243
|
-
const before = readFileSync(file, 'utf8');
|
|
244
|
-
const after = applyEdits(before, modify(before, resource.field, receipt.id, {
|
|
245
|
-
formattingOptions: { insertSpaces: true, tabSize: 2 },
|
|
246
|
-
}));
|
|
247
|
-
writeFileSync(file, after);
|
|
248
|
-
configured.set(file, after);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
const result = {
|
|
252
|
-
schemaVersion: 1,
|
|
253
|
-
projectId: project.id,
|
|
254
|
-
accountId: project.accountId,
|
|
255
|
-
checkedAt: new Date().toISOString(),
|
|
256
|
-
resources: receipts,
|
|
257
|
-
state: 'reconciled',
|
|
258
|
-
externalIntegrations: plan.externalIntegrations,
|
|
259
|
-
};
|
|
260
|
-
atomicJson(path.join(root, '.anhedral/resources.json'), result);
|
|
261
|
-
return result;
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
/** Reconcile committed identities against the customer account without creating resources or rewriting configuration. */
|
|
265
|
-
export async function inspectApplicationResources(root, request) {
|
|
266
|
-
const plan = planProvisioning(root);
|
|
267
|
-
const receipts = [];
|
|
268
|
-
for (const resource of plan.resources) {
|
|
269
|
-
const { nameField, idField } = resourceTypes[resource.kind];
|
|
270
|
-
const existing = await listResources(request, resource.kind);
|
|
271
|
-
const matches = existing.filter((item) => item[nameField] === resource.name);
|
|
272
|
-
if (matches.length !== 1 || typeof matches[0][idField] !== 'string' || !matches[0][idField])
|
|
273
|
-
throw new Error('Committed resource is missing or ambiguous in the registered account.');
|
|
274
|
-
const id = matches[0][idField];
|
|
275
|
-
if ((resource.field && !resource.id) || (resource.id && resource.id !== id))
|
|
276
|
-
throw new Error('Committed resource identity differs from the registered account; recovery will not replace it.');
|
|
277
|
-
if (resource.kind === 'r2')
|
|
278
|
-
await assertPrivateBucket(request, resource.name);
|
|
279
|
-
if (resource.kind === 'stream')
|
|
280
|
-
await assertPrivateStream(request, id);
|
|
281
|
-
receipts.push({ kind: resource.kind, name: resource.name, id, target: resource.target });
|
|
282
|
-
}
|
|
283
|
-
return { resources: receipts, externalIntegrations: plan.externalIntegrations };
|
|
284
|
-
}
|