@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,107 @@
|
|
|
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
|
+
/** The tool whose file declared a resource. */
|
|
18
|
+
export type InfraTool = 'wrangler' | 'terraform';
|
|
19
|
+
/** Where a declaration lives — the infra analogue of `file` + `symbol`. */
|
|
20
|
+
export interface InfraResourceSource {
|
|
21
|
+
tool: InfraTool;
|
|
22
|
+
/** Repo-root-relative path of the file the caller parsed. */
|
|
23
|
+
file: string;
|
|
24
|
+
/**
|
|
25
|
+
* Location inside that file. A JSON pointer for wrangler
|
|
26
|
+
* (`/r2_buckets/0`), a resource address for terraform
|
|
27
|
+
* (`cloudflare_r2_bucket.blobs`).
|
|
28
|
+
*/
|
|
29
|
+
pointer: string;
|
|
30
|
+
}
|
|
31
|
+
/** One declared piece of infrastructure. */
|
|
32
|
+
export interface InfraResource {
|
|
33
|
+
/**
|
|
34
|
+
* Stable identity, `<provider>:<resourceType>:<identifier>` — e.g.
|
|
35
|
+
* `cloudflare:worker:topic-share`. Chosen so the same resource declared by
|
|
36
|
+
* two tools yields the same key; observations join on it.
|
|
37
|
+
*/
|
|
38
|
+
key: string;
|
|
39
|
+
/** Hosting provider, lowercase: `cloudflare`. */
|
|
40
|
+
provider: string;
|
|
41
|
+
/** Provider-neutral-ish type: `worker`, `r2_bucket`, `d1_database`, `kv_namespace`, `queue`. */
|
|
42
|
+
resourceType: string;
|
|
43
|
+
/** The provider's own name or id for the resource. */
|
|
44
|
+
name: string;
|
|
45
|
+
/** What to show a reader. Falls back to the binding name when the provider name is an opaque id. */
|
|
46
|
+
label: string;
|
|
47
|
+
source: InfraResourceSource;
|
|
48
|
+
/** Small declared facts worth showing (`crons`, `assets`, `id`). Never secrets or env values. */
|
|
49
|
+
attributes?: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
/** A declared access path from one resource to another — a Worker's binding. */
|
|
52
|
+
export interface InfraBinding {
|
|
53
|
+
/** Key of the resource that holds the binding (the Worker). */
|
|
54
|
+
from: string;
|
|
55
|
+
/** Key of the resource it reaches. May name a resource no parsed file declares. */
|
|
56
|
+
to: string;
|
|
57
|
+
/** The name code uses, e.g. `TOPIC_BLOBS` for `env.TOPIC_BLOBS`. */
|
|
58
|
+
binding: string;
|
|
59
|
+
source: InfraResourceSource;
|
|
60
|
+
}
|
|
61
|
+
/** Everything one or more config files declare. */
|
|
62
|
+
export interface InfraManifest {
|
|
63
|
+
resources: InfraResource[];
|
|
64
|
+
bindings: InfraBinding[];
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Whether a resource was found live.
|
|
68
|
+
*
|
|
69
|
+
* `unknown` means the check could not be made (no permission, network
|
|
70
|
+
* failure). It is deliberately not `not-found`: a failed check must never read
|
|
71
|
+
* as "not deployed".
|
|
72
|
+
*/
|
|
73
|
+
export type InfraDeploymentStatus = 'deployed' | 'not-found' | 'unknown';
|
|
74
|
+
/**
|
|
75
|
+
* One observation of one resource in a live account.
|
|
76
|
+
*
|
|
77
|
+
* Produced by credentialed tooling outside this package. Carries no
|
|
78
|
+
* credential, token, or environment value.
|
|
79
|
+
*/
|
|
80
|
+
export interface InfraDeploymentObservation {
|
|
81
|
+
/** Joins to {@link InfraResource.key}. */
|
|
82
|
+
resourceKey: string;
|
|
83
|
+
status: InfraDeploymentStatus;
|
|
84
|
+
/** Provider id, e.g. a D1 database id. */
|
|
85
|
+
resourceId?: string;
|
|
86
|
+
/** Where the running thing answers — workers.dev, routes, custom domains. */
|
|
87
|
+
liveUrls?: string[];
|
|
88
|
+
/** The provider dashboard page for this resource. */
|
|
89
|
+
consoleUrl?: string;
|
|
90
|
+
/** Deployed version or deployment id, when the provider exposes one. */
|
|
91
|
+
version?: string;
|
|
92
|
+
/** When that version went live (ISO). */
|
|
93
|
+
deployedAt?: string;
|
|
94
|
+
/** When we looked (ISO). Required so a view can show staleness. */
|
|
95
|
+
observedAt: string;
|
|
96
|
+
/** Who said so: `cloudflare-api`, `terraform-state`, `authored`. */
|
|
97
|
+
source: string;
|
|
98
|
+
}
|
|
99
|
+
/** Build a resource key. Exported so observers produce the same string. */
|
|
100
|
+
export declare function infraResourceKey(provider: string, resourceType: string, identifier: string): string;
|
|
101
|
+
/**
|
|
102
|
+
* Merge manifests from several files. The first declaration of a key wins, so
|
|
103
|
+
* pass the more authoritative file first; bindings are de-duplicated on
|
|
104
|
+
* `from` + `to` + `binding`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function mergeInfraManifests(manifests: readonly InfraManifest[]): InfraManifest;
|
|
107
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/infra/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,+CAA+C;AAC/C,MAAM,MAAM,SAAS,GAAG,UAAU,GAAG,WAAW,CAAC;AAEjD,2EAA2E;AAC3E,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,SAAS,CAAC;IAChB,6DAA6D;IAC7D,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,4CAA4C;AAC5C,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ,iDAAiD;IACjD,QAAQ,EAAE,MAAM,CAAC;IACjB,gGAAgG;IAChG,YAAY,EAAE,MAAM,CAAC;IACrB,sDAAsD;IACtD,IAAI,EAAE,MAAM,CAAC;IACb,oGAAoG;IACpG,KAAK,EAAE,MAAM,CAAC;IACd,MA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
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Infrastructure-as-code read side — what a config file *declares* and what a
|
|
4
|
+
* live account was *observed* to be running.
|
|
5
|
+
*
|
|
6
|
+
* Two facts, kept apart on purpose:
|
|
7
|
+
*
|
|
8
|
+
* - **Configured** ({@link InfraManifest}) is a static parse of a file at a
|
|
9
|
+
* commit. No credentials, always available, verifiable like source.
|
|
10
|
+
* - **Deployed** ({@link InfraDeploymentObservation}) is an observation of
|
|
11
|
+
* the live account. It needs credentials, carries a timestamp, and goes
|
|
12
|
+
* stale. Nothing here fetches one; the tooling that does is separate and
|
|
13
|
+
* only has to produce this shape.
|
|
14
|
+
*
|
|
15
|
+
* Everything in this folder is pure and browser-safe: extractors take text or
|
|
16
|
+
* parsed JSON, never a path.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.mergeInfraManifests = exports.infraResourceKey = void 0;
|
|
20
|
+
/** Build a resource key. Exported so observers produce the same string. */
|
|
21
|
+
function infraResourceKey(provider, resourceType, identifier) {
|
|
22
|
+
return `${provider}:${resourceType}:${identifier}`;
|
|
23
|
+
}
|
|
24
|
+
exports.infraResourceKey = infraResourceKey;
|
|
25
|
+
/**
|
|
26
|
+
* Merge manifests from several files. The first declaration of a key wins, so
|
|
27
|
+
* pass the more authoritative file first; bindings are de-duplicated on
|
|
28
|
+
* `from` + `to` + `binding`.
|
|
29
|
+
*/
|
|
30
|
+
function mergeInfraManifests(manifests) {
|
|
31
|
+
const resources = new Map();
|
|
32
|
+
const bindings = new Map();
|
|
33
|
+
for (const manifest of manifests) {
|
|
34
|
+
for (const resource of manifest.resources) {
|
|
35
|
+
if (!resources.has(resource.key))
|
|
36
|
+
resources.set(resource.key, resource);
|
|
37
|
+
}
|
|
38
|
+
for (const binding of manifest.bindings) {
|
|
39
|
+
const id = `${binding.from}\u0000${binding.to}\u0000${binding.binding}`;
|
|
40
|
+
if (!bindings.has(id))
|
|
41
|
+
bindings.set(id, binding);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return { resources: [...resources.values()], bindings: [...bindings.values()] };
|
|
45
|
+
}
|
|
46
|
+
exports.mergeInfraManifests = mergeInfraManifests;
|
|
47
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/infra/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;GAeG;;;AA2FH,2EAA2E;AAC3E,SAAgB,gBAAgB,CAC9B,QAAgB,EAChB,YAAoB,EACpB,UAAkB;IAElB,OAAO,GAAG,QAAQ,IAAI,YAAY,IAAI,UAAU,EAAE,CAAC;AACrD,CAAC;AAND,4CAMC;AAED;;;;GAIG;AACH,SAAgB,mBAAmB,CACjC,SAAmC;IAEnC,MAAM,SAAS,GAAG,IAAI,GAAG,EAAyB,CAAC;IACnD,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;IACjD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE;QAChC,KAAK,MAAM,QAAQ,IAAI,QAAQ,CAAC,SAAS,EAAE;YACzC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC;gBAAE,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;SACzE;QACD,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,QAAQ,EAAE;YACvC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,SAAS,OAAO,CAAC,EAAE,SAAS,OAAO,CAAC,OAAO,EAAE,CAAC;YACxE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;SAClD;KACF;IACD,OAAO,EAAE,SAAS,EAAE,CAAC,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;AAClF,CAAC;AAfD,kDAeC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
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
|
+
import { type InfraManifest } from './types';
|
|
12
|
+
export interface ExtractWranglerOptions {
|
|
13
|
+
/** Repo-root-relative path of the config, recorded on every source. */
|
|
14
|
+
file: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Read a wrangler config. Accepts JSONC text or an already-parsed object, so a
|
|
18
|
+
* caller holding a `wrangler.toml` can parse it and pass the result.
|
|
19
|
+
*
|
|
20
|
+
* Returns an empty manifest when the config names no Worker. Malformed JSONC
|
|
21
|
+
* text throws `SyntaxError`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function extractWranglerManifest(config: string | unknown, options: ExtractWranglerOptions): InfraManifest;
|
|
24
|
+
//# sourceMappingURL=wrangler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrangler.d.ts","sourceRoot":"","sources":["../../src/infra/wrangler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAGL,KAAK,aAAa,EAGnB,MAAM,SAAS,CAAC;AA+CjB,MAAM,WAAW,sBAAsB;IACrC,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,GAAG,OAAO,EACxB,OAAO,EAAE,sBAAsB,GAC9B,aAAa,CAmEf"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Wrangler config → {@link InfraManifest}.
|
|
4
|
+
*
|
|
5
|
+
* A `wrangler.jsonc` declares one Worker and the resources it binds. That maps
|
|
6
|
+
* cleanly: the Worker is a resource, each bound store is a resource, and each
|
|
7
|
+
* binding is the access path between them — named exactly as code names it
|
|
8
|
+
* (`env.TOPIC_BLOBS`), which is what makes the binding a join to trail steps.
|
|
9
|
+
*
|
|
10
|
+
* Top-level config only. Named environments (`env.staging`) are not read yet.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.extractWranglerManifest = void 0;
|
|
14
|
+
const jsonc_1 = require("./jsonc");
|
|
15
|
+
const types_1 = require("./types");
|
|
16
|
+
const PROVIDER = 'cloudflare';
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
19
|
+
}
|
|
20
|
+
function str(value) {
|
|
21
|
+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
22
|
+
}
|
|
23
|
+
const BINDING_FAMILIES = [
|
|
24
|
+
{ path: ['kv_namespaces'], resourceType: 'kv_namespace', bindingField: 'binding', identifierField: 'id' },
|
|
25
|
+
{ path: ['r2_buckets'], resourceType: 'r2_bucket', bindingField: 'binding', identifierField: 'bucket_name' },
|
|
26
|
+
{
|
|
27
|
+
path: ['d1_databases'],
|
|
28
|
+
resourceType: 'd1_database',
|
|
29
|
+
bindingField: 'binding',
|
|
30
|
+
identifierField: 'database_name',
|
|
31
|
+
idField: 'database_id',
|
|
32
|
+
},
|
|
33
|
+
{ path: ['queues', 'producers'], resourceType: 'queue', bindingField: 'binding', identifierField: 'queue' },
|
|
34
|
+
{ path: ['services'], resourceType: 'worker', bindingField: 'binding', identifierField: 'service' },
|
|
35
|
+
];
|
|
36
|
+
/** A KV namespace is declared by opaque id only, so the binding names it. */
|
|
37
|
+
const OPAQUE_IDENTIFIER = new Set(['kv_namespace']);
|
|
38
|
+
/**
|
|
39
|
+
* Read a wrangler config. Accepts JSONC text or an already-parsed object, so a
|
|
40
|
+
* caller holding a `wrangler.toml` can parse it and pass the result.
|
|
41
|
+
*
|
|
42
|
+
* Returns an empty manifest when the config names no Worker. Malformed JSONC
|
|
43
|
+
* text throws `SyntaxError`.
|
|
44
|
+
*/
|
|
45
|
+
function extractWranglerManifest(config, options) {
|
|
46
|
+
const root = typeof config === 'string' ? (0, jsonc_1.parseJsonc)(config) : config;
|
|
47
|
+
if (!isObject(root))
|
|
48
|
+
return { resources: [], bindings: [] };
|
|
49
|
+
const workerName = str(root.name);
|
|
50
|
+
if (!workerName)
|
|
51
|
+
return { resources: [], bindings: [] };
|
|
52
|
+
const source = (pointer) => ({
|
|
53
|
+
tool: 'wrangler',
|
|
54
|
+
file: options.file,
|
|
55
|
+
pointer,
|
|
56
|
+
});
|
|
57
|
+
const workerKey = (0, types_1.infraResourceKey)(PROVIDER, 'worker', workerName);
|
|
58
|
+
const workerAttributes = {};
|
|
59
|
+
const main = str(root.main);
|
|
60
|
+
if (main)
|
|
61
|
+
workerAttributes.main = main;
|
|
62
|
+
if (isObject(root.assets))
|
|
63
|
+
workerAttributes.assets = 'true';
|
|
64
|
+
const crons = isObject(root.triggers) ? root.triggers.crons : undefined;
|
|
65
|
+
if (Array.isArray(crons) && crons.length > 0) {
|
|
66
|
+
workerAttributes.crons = crons.filter((c) => typeof c === 'string').join(', ');
|
|
67
|
+
}
|
|
68
|
+
const resources = [
|
|
69
|
+
{
|
|
70
|
+
key: workerKey,
|
|
71
|
+
provider: PROVIDER,
|
|
72
|
+
resourceType: 'worker',
|
|
73
|
+
name: workerName,
|
|
74
|
+
label: workerName,
|
|
75
|
+
source: source(''),
|
|
76
|
+
...(Object.keys(workerAttributes).length > 0 ? { attributes: workerAttributes } : {}),
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
const bindings = [];
|
|
80
|
+
for (const family of BINDING_FAMILIES) {
|
|
81
|
+
let entries = root;
|
|
82
|
+
for (const segment of family.path) {
|
|
83
|
+
entries = isObject(entries) ? entries[segment] : undefined;
|
|
84
|
+
}
|
|
85
|
+
if (!Array.isArray(entries))
|
|
86
|
+
continue;
|
|
87
|
+
entries.forEach((entry, index) => {
|
|
88
|
+
if (!isObject(entry))
|
|
89
|
+
return;
|
|
90
|
+
const binding = str(entry[family.bindingField]);
|
|
91
|
+
const identifier = str(entry[family.identifierField]);
|
|
92
|
+
if (!binding || !identifier)
|
|
93
|
+
return;
|
|
94
|
+
const pointer = `/${family.path.join('/')}/${index}`;
|
|
95
|
+
const key = (0, types_1.infraResourceKey)(PROVIDER, family.resourceType, identifier);
|
|
96
|
+
const id = family.idField ? str(entry[family.idField]) : undefined;
|
|
97
|
+
// A service binding points at another Worker, which its own config
|
|
98
|
+
// declares; emit the access path but not a second declaration of it.
|
|
99
|
+
if (family.resourceType !== 'worker') {
|
|
100
|
+
resources.push({
|
|
101
|
+
key,
|
|
102
|
+
provider: PROVIDER,
|
|
103
|
+
resourceType: family.resourceType,
|
|
104
|
+
name: identifier,
|
|
105
|
+
label: OPAQUE_IDENTIFIER.has(family.resourceType) ? binding : identifier,
|
|
106
|
+
source: source(pointer),
|
|
107
|
+
...(id ? { attributes: { id } } : {}),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
bindings.push({ from: workerKey, to: key, binding, source: source(pointer) });
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return { resources, bindings };
|
|
114
|
+
}
|
|
115
|
+
exports.extractWranglerManifest = extractWranglerManifest;
|
|
116
|
+
//# sourceMappingURL=wrangler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrangler.js","sourceRoot":"","sources":["../../src/infra/wrangler.ts"],"names":[],"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AAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,EAAE,CAAC;YACrD,MAAM,GAAG,GAAG,IAAA,wBAAgB,EAAC,QAAQ,EAAE,MAAM,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;YACxE,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;YACnE,mEAAmE;YACnE,qEAAqE;YACrE,IAAI,MAAM,CAAC,YAAY,KAAK,QAAQ,EAAE;gBACpC,SAAS,CAAC,IAAI,CAAC;oBACb,GAAG;oBACH,QAAQ,EAAE,QAAQ;oBAClB,YAAY,EAAE,MAAM,CAAC,YAAY;oBACjC,IAAI,EAAE,UAAU;oBAChB,KAAK,EAAE,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU;oBACxE,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC;oBACvB,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;iBACtC,CAAC,CAAC;aACJ;YACD,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAChF,CAAC,CAAC,CAAC;KACJ;IAED,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAC;AACjC,CAAC;AAtED,0DAsEC"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -24,6 +24,10 @@ export { formatTopicReferenceLink, parseTopicReferenceLink } from './storage/top
|
|
|
24
24
|
export { toSharedTopic } from './storage/topic-share';
|
|
25
25
|
export type { SharedTopic, SharedTopicAsset, SharedTopicProjection } from './storage/topic-share';
|
|
26
26
|
|
|
27
|
+
// Infrastructure-as-code read side: what wrangler / terraform declare, and the
|
|
28
|
+
// shape a live-deployment observation takes. Pure — text and JSON in.
|
|
29
|
+
export * from './infra';
|
|
30
|
+
|
|
27
31
|
// Cross-field validation (the rules the JSON Schema can't express). Structural
|
|
28
32
|
// (schema) validation is enforced per surface against
|
|
29
33
|
// `schemas/subsystem-model.schema.json`.
|
package/src/infra/c4.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link InfraManifest} → proposed C4 containers and edges.
|
|
3
|
+
*
|
|
4
|
+
* The full C4 model lives in the viewer package, which depends on this one, so
|
|
5
|
+
* the shapes here are structural: they are assignable to the viewer's
|
|
6
|
+
* `C4Container` / `C4Edge` without this package importing them (the same
|
|
7
|
+
* arrangement as `audit/c4.ts`).
|
|
8
|
+
*
|
|
9
|
+
* Everything produced is `proposed`. A config file is evidence that a
|
|
10
|
+
* deployable unit exists, not a reviewer's acceptance of it as architecture.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { InfraManifest, InfraResource, InfraResourceSource } from './types';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The configured anchor a container carries — which declared resource it is.
|
|
17
|
+
* Holds no live fact: whether it is running is a separate observation, joined
|
|
18
|
+
* on `resourceKey`.
|
|
19
|
+
*/
|
|
20
|
+
export interface InfraC4Deployment {
|
|
21
|
+
resourceKey: string;
|
|
22
|
+
provider: string;
|
|
23
|
+
resourceType: string;
|
|
24
|
+
name: string;
|
|
25
|
+
source: InfraResourceSource;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface InfraC4Container {
|
|
29
|
+
id: string;
|
|
30
|
+
kind: 'container';
|
|
31
|
+
containerKind: 'application' | 'data-store';
|
|
32
|
+
label: string;
|
|
33
|
+
technology: string;
|
|
34
|
+
state: 'proposed';
|
|
35
|
+
rationale: string;
|
|
36
|
+
deployment: InfraC4Deployment;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface InfraC4Edge {
|
|
40
|
+
id: string;
|
|
41
|
+
source: string;
|
|
42
|
+
target: string;
|
|
43
|
+
label: string;
|
|
44
|
+
draft: true;
|
|
45
|
+
mechanisms: string[];
|
|
46
|
+
count: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const TECHNOLOGY: Record<string, string> = {
|
|
50
|
+
'cloudflare:worker': 'Cloudflare Worker',
|
|
51
|
+
'cloudflare:r2_bucket': 'Cloudflare R2',
|
|
52
|
+
'cloudflare:d1_database': 'Cloudflare D1',
|
|
53
|
+
'cloudflare:kv_namespace': 'Cloudflare KV',
|
|
54
|
+
'cloudflare:queue': 'Cloudflare Queue',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** Resource types that run code. Everything else is something code reads or writes. */
|
|
58
|
+
const APPLICATION_TYPES = new Set(['worker']);
|
|
59
|
+
|
|
60
|
+
/** Short suffix that keeps a store's id distinct from a same-named Worker. */
|
|
61
|
+
const ID_SUFFIX: Record<string, string> = {
|
|
62
|
+
r2_bucket: 'r2',
|
|
63
|
+
d1_database: 'd1',
|
|
64
|
+
kv_namespace: 'kv',
|
|
65
|
+
queue: 'queue',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
function slug(text: string): string {
|
|
69
|
+
return text
|
|
70
|
+
.toLowerCase()
|
|
71
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
72
|
+
.replace(/^-+|-+$/g, '');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The stable C4 element id for a declared resource. */
|
|
76
|
+
export function infraContainerId(resource: Pick<InfraResource, 'resourceType' | 'label'>): string {
|
|
77
|
+
const suffix = ID_SUFFIX[resource.resourceType];
|
|
78
|
+
const base = slug(resource.label) || 'resource';
|
|
79
|
+
return `container:${suffix ? `${base}-${suffix}` : base}`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The configured anchor for one resource. */
|
|
83
|
+
export function infraDeploymentRef(resource: InfraResource): InfraC4Deployment {
|
|
84
|
+
return {
|
|
85
|
+
resourceKey: resource.key,
|
|
86
|
+
provider: resource.provider,
|
|
87
|
+
resourceType: resource.resourceType,
|
|
88
|
+
name: resource.name,
|
|
89
|
+
source: resource.source,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Propose one container per declared resource and one edge per binding.
|
|
95
|
+
*
|
|
96
|
+
* A binding whose target no parsed file declares (a service binding to a
|
|
97
|
+
* Worker configured elsewhere) is dropped rather than drawn to nothing; merge
|
|
98
|
+
* the other config's manifest in first to keep it.
|
|
99
|
+
*/
|
|
100
|
+
export function infraToC4(manifest: InfraManifest): {
|
|
101
|
+
nodes: InfraC4Container[];
|
|
102
|
+
edges: InfraC4Edge[];
|
|
103
|
+
} {
|
|
104
|
+
const idByKey = new Map<string, string>();
|
|
105
|
+
const nodes = manifest.resources.map((resource): InfraC4Container => {
|
|
106
|
+
const id = infraContainerId(resource);
|
|
107
|
+
idByKey.set(resource.key, id);
|
|
108
|
+
const where = resource.source.pointer
|
|
109
|
+
? `${resource.source.file} at ${resource.source.pointer}`
|
|
110
|
+
: resource.source.file;
|
|
111
|
+
return {
|
|
112
|
+
id,
|
|
113
|
+
kind: 'container',
|
|
114
|
+
containerKind: APPLICATION_TYPES.has(resource.resourceType) ? 'application' : 'data-store',
|
|
115
|
+
label: resource.label,
|
|
116
|
+
technology:
|
|
117
|
+
TECHNOLOGY[`${resource.provider}:${resource.resourceType}`] ??
|
|
118
|
+
`${resource.provider} ${resource.resourceType}`,
|
|
119
|
+
state: 'proposed',
|
|
120
|
+
rationale: `Declared by ${resource.source.tool} in ${where}.`,
|
|
121
|
+
deployment: infraDeploymentRef(resource),
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const edges: InfraC4Edge[] = [];
|
|
126
|
+
for (const binding of manifest.bindings) {
|
|
127
|
+
const source = idByKey.get(binding.from);
|
|
128
|
+
const target = idByKey.get(binding.to);
|
|
129
|
+
if (!source || !target) continue;
|
|
130
|
+
edges.push({
|
|
131
|
+
id: `edge:${source}->${target}:${binding.binding}`,
|
|
132
|
+
source,
|
|
133
|
+
target,
|
|
134
|
+
label: `env.${binding.binding}`,
|
|
135
|
+
draft: true,
|
|
136
|
+
mechanisms: [],
|
|
137
|
+
count: 0,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return { nodes, edges };
|
|
141
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Infrastructure-as-code read side. Pure and browser-safe.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { infraResourceKey, mergeInfraManifests } from './types';
|
|
6
|
+
export type {
|
|
7
|
+
InfraBinding,
|
|
8
|
+
InfraDeploymentObservation,
|
|
9
|
+
InfraDeploymentStatus,
|
|
10
|
+
InfraManifest,
|
|
11
|
+
InfraResource,
|
|
12
|
+
InfraResourceSource,
|
|
13
|
+
InfraTool,
|
|
14
|
+
} from './types';
|
|
15
|
+
export { parseJsonc, stripJsonc } from './jsonc';
|
|
16
|
+
export { extractWranglerManifest } from './wrangler';
|
|
17
|
+
export type { ExtractWranglerOptions } from './wrangler';
|
|
18
|
+
export { extractTerraformManifest, terraformStateObservations } from './terraform';
|
|
19
|
+
export type { ExtractTerraformOptions } from './terraform';
|
|
20
|
+
export { infraContainerId, infraDeploymentRef, infraToC4 } from './c4';
|
|
21
|
+
export type { InfraC4Container, InfraC4Deployment, InfraC4Edge } from './c4';
|