@principal-ai/subsystems-core 0.51.6 → 0.51.7
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/infra/c4.d.ts +59 -0
- package/dist/infra/c4.d.ts.map +1 -0
- package/dist/infra/c4.js +101 -0
- package/dist/infra/c4.js.map +1 -0
- package/dist/infra/index.d.ts +13 -0
- package/dist/infra/index.d.ts.map +1 -0
- package/dist/infra/index.js +22 -0
- package/dist/infra/index.js.map +1 -0
- package/dist/infra/jsonc.d.ts +10 -0
- package/dist/infra/jsonc.d.ts.map +1 -0
- package/dist/infra/jsonc.js +77 -0
- package/dist/infra/jsonc.js.map +1 -0
- package/dist/infra/terraform.d.ts +41 -0
- package/dist/infra/terraform.d.ts.map +1 -0
- package/dist/infra/terraform.js +208 -0
- package/dist/infra/terraform.js.map +1 -0
- package/dist/infra/types.d.ts +107 -0
- package/dist/infra/types.d.ts.map +1 -0
- package/dist/infra/types.js +47 -0
- package/dist/infra/types.js.map +1 -0
- package/dist/infra/wrangler.d.ts +24 -0
- package/dist/infra/wrangler.d.ts.map +1 -0
- package/dist/infra/wrangler.js +116 -0
- package/dist/infra/wrangler.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +4 -0
- package/src/infra/c4.ts +141 -0
- package/src/infra/index.ts +21 -0
- package/src/infra/infra.test.ts +300 -0
- package/src/infra/jsonc.ts +70 -0
- package/src/infra/terraform.ts +251 -0
- package/src/infra/types.ts +136 -0
- package/src/infra/wrangler.ts +148 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Infrastructure-as-code read side — what a config file *declares* and what a
|
|
3
|
+
* live account was *observed* to be running.
|
|
4
|
+
*
|
|
5
|
+
* Two facts, kept apart on purpose:
|
|
6
|
+
*
|
|
7
|
+
* - **Configured** ({@link InfraManifest}) is a static parse of a file at a
|
|
8
|
+
* commit. No credentials, always available, verifiable like source.
|
|
9
|
+
* - **Deployed** ({@link InfraDeploymentObservation}) is an observation of
|
|
10
|
+
* the live account. It needs credentials, carries a timestamp, and goes
|
|
11
|
+
* stale. Nothing here fetches one; the tooling that does is separate and
|
|
12
|
+
* only has to produce this shape.
|
|
13
|
+
*
|
|
14
|
+
* Everything in this folder is pure and browser-safe: extractors take text or
|
|
15
|
+
* parsed JSON, never a path.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The tool whose file declared a resource. */
|
|
19
|
+
export type InfraTool = 'wrangler' | 'terraform';
|
|
20
|
+
|
|
21
|
+
/** Where a declaration lives — the infra analogue of `file` + `symbol`. */
|
|
22
|
+
export interface InfraResourceSource {
|
|
23
|
+
tool: InfraTool;
|
|
24
|
+
/** Repo-root-relative path of the file the caller parsed. */
|
|
25
|
+
file: string;
|
|
26
|
+
/**
|
|
27
|
+
* Location inside that file. A JSON pointer for wrangler
|
|
28
|
+
* (`/r2_buckets/0`), a resource address for terraform
|
|
29
|
+
* (`cloudflare_r2_bucket.blobs`).
|
|
30
|
+
*/
|
|
31
|
+
pointer: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One declared piece of infrastructure. */
|
|
35
|
+
export interface InfraResource {
|
|
36
|
+
/**
|
|
37
|
+
* Stable identity, `<provider>:<resourceType>:<identifier>` — e.g.
|
|
38
|
+
* `cloudflare:worker:topic-share`. Chosen so the same resource declared by
|
|
39
|
+
* two tools yields the same key; observations join on it.
|
|
40
|
+
*/
|
|
41
|
+
key: string;
|
|
42
|
+
/** Hosting provider, lowercase: `cloudflare`. */
|
|
43
|
+
provider: string;
|
|
44
|
+
/** Provider-neutral-ish type: `worker`, `r2_bucket`, `d1_database`, `kv_namespace`, `queue`. */
|
|
45
|
+
resourceType: string;
|
|
46
|
+
/** The provider's own name or id for the resource. */
|
|
47
|
+
name: string;
|
|
48
|
+
/** What to show a reader. Falls back to the binding name when the provider name is an opaque id. */
|
|
49
|
+
label: string;
|
|
50
|
+
source: InfraResourceSource;
|
|
51
|
+
/** Small declared facts worth showing (`crons`, `assets`, `id`). Never secrets or env values. */
|
|
52
|
+
attributes?: Record<string, string>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A declared access path from one resource to another — a Worker's binding. */
|
|
56
|
+
export interface InfraBinding {
|
|
57
|
+
/** Key of the resource that holds the binding (the Worker). */
|
|
58
|
+
from: string;
|
|
59
|
+
/** Key of the resource it reaches. May name a resource no parsed file declares. */
|
|
60
|
+
to: string;
|
|
61
|
+
/** The name code uses, e.g. `TOPIC_BLOBS` for `env.TOPIC_BLOBS`. */
|
|
62
|
+
binding: string;
|
|
63
|
+
source: InfraResourceSource;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Everything one or more config files declare. */
|
|
67
|
+
export interface InfraManifest {
|
|
68
|
+
resources: InfraResource[];
|
|
69
|
+
bindings: InfraBinding[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Whether a resource was found live.
|
|
74
|
+
*
|
|
75
|
+
* `unknown` means the check could not be made (no permission, network
|
|
76
|
+
* failure). It is deliberately not `not-found`: a failed check must never read
|
|
77
|
+
* as "not deployed".
|
|
78
|
+
*/
|
|
79
|
+
export type InfraDeploymentStatus = 'deployed' | 'not-found' | 'unknown';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One observation of one resource in a live account.
|
|
83
|
+
*
|
|
84
|
+
* Produced by credentialed tooling outside this package. Carries no
|
|
85
|
+
* credential, token, or environment value.
|
|
86
|
+
*/
|
|
87
|
+
export interface InfraDeploymentObservation {
|
|
88
|
+
/** Joins to {@link InfraResource.key}. */
|
|
89
|
+
resourceKey: string;
|
|
90
|
+
status: InfraDeploymentStatus;
|
|
91
|
+
/** Provider id, e.g. a D1 database id. */
|
|
92
|
+
resourceId?: string;
|
|
93
|
+
/** Where the running thing answers — workers.dev, routes, custom domains. */
|
|
94
|
+
liveUrls?: string[];
|
|
95
|
+
/** The provider dashboard page for this resource. */
|
|
96
|
+
consoleUrl?: string;
|
|
97
|
+
/** Deployed version or deployment id, when the provider exposes one. */
|
|
98
|
+
version?: string;
|
|
99
|
+
/** When that version went live (ISO). */
|
|
100
|
+
deployedAt?: string;
|
|
101
|
+
/** When we looked (ISO). Required so a view can show staleness. */
|
|
102
|
+
observedAt: string;
|
|
103
|
+
/** Who said so: `cloudflare-api`, `terraform-state`, `authored`. */
|
|
104
|
+
source: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Build a resource key. Exported so observers produce the same string. */
|
|
108
|
+
export function infraResourceKey(
|
|
109
|
+
provider: string,
|
|
110
|
+
resourceType: string,
|
|
111
|
+
identifier: string,
|
|
112
|
+
): string {
|
|
113
|
+
return `${provider}:${resourceType}:${identifier}`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Merge manifests from several files. The first declaration of a key wins, so
|
|
118
|
+
* pass the more authoritative file first; bindings are de-duplicated on
|
|
119
|
+
* `from` + `to` + `binding`.
|
|
120
|
+
*/
|
|
121
|
+
export function mergeInfraManifests(
|
|
122
|
+
manifests: readonly InfraManifest[],
|
|
123
|
+
): InfraManifest {
|
|
124
|
+
const resources = new Map<string, InfraResource>();
|
|
125
|
+
const bindings = new Map<string, InfraBinding>();
|
|
126
|
+
for (const manifest of manifests) {
|
|
127
|
+
for (const resource of manifest.resources) {
|
|
128
|
+
if (!resources.has(resource.key)) resources.set(resource.key, resource);
|
|
129
|
+
}
|
|
130
|
+
for (const binding of manifest.bindings) {
|
|
131
|
+
const id = `${binding.from}\u0000${binding.to}\u0000${binding.binding}`;
|
|
132
|
+
if (!bindings.has(id)) bindings.set(id, binding);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { resources: [...resources.values()], bindings: [...bindings.values()] };
|
|
136
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrangler config → {@link InfraManifest}.
|
|
3
|
+
*
|
|
4
|
+
* A `wrangler.jsonc` declares one Worker and the resources it binds. That maps
|
|
5
|
+
* cleanly: the Worker is a resource, each bound store is a resource, and each
|
|
6
|
+
* binding is the access path between them — named exactly as code names it
|
|
7
|
+
* (`env.TOPIC_BLOBS`), which is what makes the binding a join to trail steps.
|
|
8
|
+
*
|
|
9
|
+
* Top-level config only. Named environments (`env.staging`) are not read yet.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { parseJsonc } from './jsonc';
|
|
13
|
+
import {
|
|
14
|
+
infraResourceKey,
|
|
15
|
+
type InfraBinding,
|
|
16
|
+
type InfraManifest,
|
|
17
|
+
type InfraResource,
|
|
18
|
+
type InfraResourceSource,
|
|
19
|
+
} from './types';
|
|
20
|
+
|
|
21
|
+
const PROVIDER = 'cloudflare';
|
|
22
|
+
|
|
23
|
+
type Json = Record<string, unknown>;
|
|
24
|
+
|
|
25
|
+
function isObject(value: unknown): value is Json {
|
|
26
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function str(value: unknown): string | undefined {
|
|
30
|
+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* One row per binding family. `identifier` is what the resource key is built
|
|
35
|
+
* from, so it must be the field another tool would also know the resource by.
|
|
36
|
+
*/
|
|
37
|
+
interface BindingFamily {
|
|
38
|
+
/** Path to the array in the config, e.g. `['queues', 'producers']`. */
|
|
39
|
+
path: readonly string[];
|
|
40
|
+
resourceType: string;
|
|
41
|
+
/** Field holding the code-side binding name. */
|
|
42
|
+
bindingField: string;
|
|
43
|
+
/** Field holding the provider name/id the key is built from. */
|
|
44
|
+
identifierField: string;
|
|
45
|
+
/** Extra identifier worth keeping as an attribute (e.g. the D1 id). */
|
|
46
|
+
idField?: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const BINDING_FAMILIES: readonly BindingFamily[] = [
|
|
50
|
+
{ path: ['kv_namespaces'], resourceType: 'kv_namespace', bindingField: 'binding', identifierField: 'id' },
|
|
51
|
+
{ path: ['r2_buckets'], resourceType: 'r2_bucket', bindingField: 'binding', identifierField: 'bucket_name' },
|
|
52
|
+
{
|
|
53
|
+
path: ['d1_databases'],
|
|
54
|
+
resourceType: 'd1_database',
|
|
55
|
+
bindingField: 'binding',
|
|
56
|
+
identifierField: 'database_name',
|
|
57
|
+
idField: 'database_id',
|
|
58
|
+
},
|
|
59
|
+
{ path: ['queues', 'producers'], resourceType: 'queue', bindingField: 'binding', identifierField: 'queue' },
|
|
60
|
+
{ path: ['services'], resourceType: 'worker', bindingField: 'binding', identifierField: 'service' },
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
/** A KV namespace is declared by opaque id only, so the binding names it. */
|
|
64
|
+
const OPAQUE_IDENTIFIER = new Set(['kv_namespace']);
|
|
65
|
+
|
|
66
|
+
export interface ExtractWranglerOptions {
|
|
67
|
+
/** Repo-root-relative path of the config, recorded on every source. */
|
|
68
|
+
file: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Read a wrangler config. Accepts JSONC text or an already-parsed object, so a
|
|
73
|
+
* caller holding a `wrangler.toml` can parse it and pass the result.
|
|
74
|
+
*
|
|
75
|
+
* Returns an empty manifest when the config names no Worker. Malformed JSONC
|
|
76
|
+
* text throws `SyntaxError`.
|
|
77
|
+
*/
|
|
78
|
+
export function extractWranglerManifest(
|
|
79
|
+
config: string | unknown,
|
|
80
|
+
options: ExtractWranglerOptions,
|
|
81
|
+
): InfraManifest {
|
|
82
|
+
const root = typeof config === 'string' ? parseJsonc(config) : config;
|
|
83
|
+
if (!isObject(root)) return { resources: [], bindings: [] };
|
|
84
|
+
const workerName = str(root.name);
|
|
85
|
+
if (!workerName) return { resources: [], bindings: [] };
|
|
86
|
+
|
|
87
|
+
const source = (pointer: string): InfraResourceSource => ({
|
|
88
|
+
tool: 'wrangler',
|
|
89
|
+
file: options.file,
|
|
90
|
+
pointer,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const workerKey = infraResourceKey(PROVIDER, 'worker', workerName);
|
|
94
|
+
const workerAttributes: Record<string, string> = {};
|
|
95
|
+
const main = str(root.main);
|
|
96
|
+
if (main) workerAttributes.main = main;
|
|
97
|
+
if (isObject(root.assets)) workerAttributes.assets = 'true';
|
|
98
|
+
const crons = isObject(root.triggers) ? root.triggers.crons : undefined;
|
|
99
|
+
if (Array.isArray(crons) && crons.length > 0) {
|
|
100
|
+
workerAttributes.crons = crons.filter((c) => typeof c === 'string').join(', ');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const resources: InfraResource[] = [
|
|
104
|
+
{
|
|
105
|
+
key: workerKey,
|
|
106
|
+
provider: PROVIDER,
|
|
107
|
+
resourceType: 'worker',
|
|
108
|
+
name: workerName,
|
|
109
|
+
label: workerName,
|
|
110
|
+
source: source(''),
|
|
111
|
+
...(Object.keys(workerAttributes).length > 0 ? { attributes: workerAttributes } : {}),
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
const bindings: InfraBinding[] = [];
|
|
115
|
+
|
|
116
|
+
for (const family of BINDING_FAMILIES) {
|
|
117
|
+
let entries: unknown = root;
|
|
118
|
+
for (const segment of family.path) {
|
|
119
|
+
entries = isObject(entries) ? entries[segment] : undefined;
|
|
120
|
+
}
|
|
121
|
+
if (!Array.isArray(entries)) continue;
|
|
122
|
+
entries.forEach((entry, index) => {
|
|
123
|
+
if (!isObject(entry)) return;
|
|
124
|
+
const binding = str(entry[family.bindingField]);
|
|
125
|
+
const identifier = str(entry[family.identifierField]);
|
|
126
|
+
if (!binding || !identifier) return;
|
|
127
|
+
const pointer = `/${family.path.join('/')}/${index}`;
|
|
128
|
+
const key = infraResourceKey(PROVIDER, family.resourceType, identifier);
|
|
129
|
+
const id = family.idField ? str(entry[family.idField]) : undefined;
|
|
130
|
+
// A service binding points at another Worker, which its own config
|
|
131
|
+
// declares; emit the access path but not a second declaration of it.
|
|
132
|
+
if (family.resourceType !== 'worker') {
|
|
133
|
+
resources.push({
|
|
134
|
+
key,
|
|
135
|
+
provider: PROVIDER,
|
|
136
|
+
resourceType: family.resourceType,
|
|
137
|
+
name: identifier,
|
|
138
|
+
label: OPAQUE_IDENTIFIER.has(family.resourceType) ? binding : identifier,
|
|
139
|
+
source: source(pointer),
|
|
140
|
+
...(id ? { attributes: { id } } : {}),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
bindings.push({ from: workerKey, to: key, binding, source: source(pointer) });
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { resources, bindings };
|
|
148
|
+
}
|