@agentic-kit/db-tools 0.15.1 → 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 +4 -4
- package/blueprint/blueprint-schema.js +3 -1
- package/context.d.ts +9 -9
- package/context.js +92 -54
- package/db-probe.js +2 -2
- package/esm/blueprint/blueprint-schema.js +3 -1
- package/esm/context.d.ts +9 -9
- package/esm/context.js +91 -54
- package/esm/db-probe.js +2 -2
- package/esm/failure.d.ts +1 -1
- package/esm/failure.js +2 -2
- package/esm/gate.js +1 -1
- package/esm/graphql-http.d.ts +3 -2
- package/esm/graphql-http.js +26 -7
- package/esm/host.d.ts +20 -7
- package/esm/host.js +7 -2
- package/esm/index.d.ts +175 -14
- package/esm/index.js +5 -15
- package/esm/project-file.d.ts +72 -0
- package/esm/project-file.js +161 -0
- package/esm/provision-database/provision.d.ts +8 -3
- package/esm/provision-database/provision.js +63 -94
- package/esm/ready.d.ts +7 -3
- package/esm/ready.js +3 -3
- package/esm/records/rows.d.ts +1 -0
- package/esm/records/rows.js +9 -6
- package/esm/tools/access.js +1 -1
- package/esm/tools/create-api-key.js +2 -2
- package/esm/tools/delete-records.d.ts +1 -1
- package/esm/tools/delete-records.js +7 -7
- package/esm/tools/index.d.ts +195 -2
- package/esm/tools/index.js +25 -15
- package/esm/tools/mutations.js +7 -6
- package/esm/tools/provision-database.js +4 -4
- package/esm/tools/run-codegen.d.ts +2 -2
- package/esm/tools/run-codegen.js +10 -11
- package/esm/tools/update-records.js +1 -1
- package/failure.d.ts +1 -1
- package/failure.js +2 -2
- package/gate.js +1 -1
- package/graphql-http.d.ts +3 -2
- package/graphql-http.js +26 -7
- package/host.d.ts +20 -7
- package/host.js +7 -2
- package/index.d.ts +175 -14
- package/index.js +9 -45
- package/package.json +19 -16
- package/project-file.d.ts +72 -0
- package/project-file.js +178 -0
- package/provision-database/provision.d.ts +8 -3
- package/provision-database/provision.js +66 -96
- package/ready.d.ts +7 -3
- package/ready.js +3 -3
- package/records/rows.d.ts +1 -0
- package/records/rows.js +8 -5
- package/tools/access.js +1 -1
- package/tools/create-api-key.js +2 -2
- package/tools/delete-records.d.ts +1 -1
- package/tools/delete-records.js +8 -8
- package/tools/index.d.ts +195 -2
- package/tools/index.js +53 -43
- package/tools/mutations.js +6 -5
- package/tools/provision-database.js +5 -5
- package/tools/run-codegen.d.ts +2 -2
- package/tools/run-codegen.js +8 -9
- package/tools/update-records.js +1 -1
- package/esm/provision-database/env-file.d.ts +0 -62
- package/esm/provision-database/env-file.js +0 -170
- package/esm/provision-database/pending-ticket.d.ts +0 -33
- package/esm/provision-database/pending-ticket.js +0 -63
- package/provision-database/env-file.d.ts +0 -62
- package/provision-database/env-file.js +0 -179
- package/provision-database/pending-ticket.d.ts +0 -33
- package/provision-database/pending-ticket.js +0 -72
package/tools/run-codegen.js
CHANGED
|
@@ -44,8 +44,8 @@ function appStepsOf(app, scaffolded) {
|
|
|
44
44
|
];
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
47
|
-
* The endpoints codegen reads, from the API endpoint the session trusts: never from an API_ENDPOINT pin
|
|
48
|
-
*
|
|
47
|
+
* The endpoints codegen reads, from the API endpoint the session trusts: never from an injected API_ENDPOINT pin, since
|
|
48
|
+
* the schema at the endpoint decides the code that lands in the project.
|
|
49
49
|
*/
|
|
50
50
|
const planesOf = (context) => (0, endpoints_1.derivePlaneEndpoints)(context.controlApiEndpoint, context.databaseName);
|
|
51
51
|
// The app data plane endpoint codegen reads: the per-database api host.
|
|
@@ -60,8 +60,8 @@ function noApp(context, message, hint) {
|
|
|
60
60
|
/**
|
|
61
61
|
* What run_codegen checks before it changes anything: the project's database
|
|
62
62
|
* on the data plane (codegen reads the app's own API, as the Preview does), by
|
|
63
|
-
* name, and the app in packages/app. Its API endpoint is the
|
|
64
|
-
*
|
|
63
|
+
* name, and the app in packages/app. Its API endpoint is the one the data
|
|
64
|
+
* plane resolves. A host that prepares no app
|
|
65
65
|
* (`prewarmApp: false`) needs an app there, installed when it is composed.
|
|
66
66
|
*/
|
|
67
67
|
async function runCodegenReady(cwd) {
|
|
@@ -77,9 +77,8 @@ async function runCodegenReady(cwd) {
|
|
|
77
77
|
kind: 'failed',
|
|
78
78
|
failure: {
|
|
79
79
|
code: 'missing-credentials',
|
|
80
|
-
message: '
|
|
81
|
-
hint:
|
|
82
|
-
calls: [(0, failure_1.suggest)('run_codegen', {}, 'Generate the SDK once DATABASE_NAME is in .env')],
|
|
80
|
+
message: 'The project context names no database, and the backend reported no name for it.',
|
|
81
|
+
hint: 'Give the context the database name, or bind this directory to its database again, then call again.',
|
|
83
82
|
},
|
|
84
83
|
};
|
|
85
84
|
}
|
|
@@ -276,11 +275,11 @@ function createRunCodegenTool(exec = app_workspace_1.run) {
|
|
|
276
275
|
if (generatedSubmodules.length === 0) {
|
|
277
276
|
return fail({
|
|
278
277
|
code: 'failed',
|
|
279
|
-
message: `Codegen ran, but no app submodules (hooks/orm/types) were generated — the database may have no entity tables yet. Normalized ${barrelsRewritten
|
|
278
|
+
message: `Codegen ran, but no app submodules (hooks/orm/types) were generated — the database may have no entity tables yet. Normalized ${(0, harness_1.plural)(barrelsRewritten, 'barrel file')}.`,
|
|
280
279
|
hint: 'Create tables first, then generate the SDK again.',
|
|
281
280
|
}, { barrelsRewritten, generatedSubmodules, ...stepDetails() });
|
|
282
281
|
}
|
|
283
|
-
const message = `Codegen complete. Generated app submodules: ${generatedSubmodules.join(', ')}. Normalized ${barrelsRewritten
|
|
282
|
+
const message = `Codegen complete. Generated app submodules: ${generatedSubmodules.join(', ')}. Normalized ${(0, harness_1.plural)(barrelsRewritten, 'barrel file')}.`;
|
|
284
283
|
return {
|
|
285
284
|
content: [{ type: 'text', text: message }],
|
|
286
285
|
details: {
|
package/tools/update-records.js
CHANGED
|
@@ -46,7 +46,7 @@ exports.updateRecordsTool = {
|
|
|
46
46
|
description: "Set columns on rows that are already in the project's app database, each row named by its primary key. Use it to link rows, for example a key column of a row add_records made to a sample row seeded when the app was built.",
|
|
47
47
|
promptSnippet: 'update_records: set columns on existing rows by primary key, such as linking rows. Gated.',
|
|
48
48
|
parameters: UpdateRecordsZod,
|
|
49
|
-
effect: '
|
|
49
|
+
effect: 'destructive',
|
|
50
50
|
async execute(params, ctx) {
|
|
51
51
|
const ready = await updateRecordsReady(params, ctx.cwd);
|
|
52
52
|
if (ready.kind === 'failed')
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export type EnvVars = Record<string, string>;
|
|
2
|
-
/** Parse .env text: quoted values, `export` prefixes and comments; last definition wins. */
|
|
3
|
-
export declare function readEnv(content: string): EnvVars;
|
|
4
|
-
/**
|
|
5
|
-
* Upsert `vars` into the existing `.env` `content`. An existing key is
|
|
6
|
-
* replaced in place (keeping an `export` prefix), and any later definition of
|
|
7
|
-
* it is dropped so the new value is the one that wins; new keys are appended.
|
|
8
|
-
* Returns the new file body, trailing-newline-terminated.
|
|
9
|
-
*/
|
|
10
|
-
export declare function mergeEnv(content: string, vars: EnvVars): string;
|
|
11
|
-
/**
|
|
12
|
-
* Drop every definition of `keys`. Comments and other entries pass through,
|
|
13
|
-
* and content without those keys comes back unchanged.
|
|
14
|
-
*/
|
|
15
|
-
export declare function removeEnvKeys(content: string, keys: readonly string[]): string;
|
|
16
|
-
/**
|
|
17
|
-
* The provision request provision_database is still waiting on, recorded when
|
|
18
|
-
* it stops waiting so the next call resumes that ticket. It's resumed only
|
|
19
|
-
* for the same database name, backend and account. Absent whenever no request
|
|
20
|
-
* is pending.
|
|
21
|
-
*/
|
|
22
|
-
export declare const PENDING_PROVISION_KEYS: {
|
|
23
|
-
readonly ticketId: "PROVISION_TICKET_ID";
|
|
24
|
-
readonly databaseName: "PROVISION_TICKET_DATABASE_NAME";
|
|
25
|
-
readonly apiEndpoint: "PROVISION_TICKET_API_ENDPOINT";
|
|
26
|
-
readonly userId: "PROVISION_TICKET_USER_ID";
|
|
27
|
-
readonly attempts: "PROVISION_TICKET_ATTEMPTS";
|
|
28
|
-
};
|
|
29
|
-
/** The backend's own check on a database name, which is 3 to 63 characters long. */
|
|
30
|
-
export declare const DATABASE_NAME_PATTERN: RegExp;
|
|
31
|
-
/**
|
|
32
|
-
* The binding keys whose value in `env` is not of its form. The .env of a cloned project is not the person's text, so
|
|
33
|
-
* such a value is never used, sent or shown: the caller refuses with the keys alone.
|
|
34
|
-
*/
|
|
35
|
-
export declare const invalidBindingKeys: (env: Record<string, string | undefined>) => string[];
|
|
36
|
-
/** The binding keys provision_database owns in a project's .env. */
|
|
37
|
-
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
38
|
-
/**
|
|
39
|
-
* Keys archived as comments on reprovision — the binding identity: which
|
|
40
|
-
* database, under whom, on which backend.
|
|
41
|
-
*/
|
|
42
|
-
export declare const ARCHIVED_BINDING_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
43
|
-
/**
|
|
44
|
-
* Build the database binding written to a project's .env. The
|
|
45
|
-
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
46
|
-
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
47
|
-
* falling back to localhost defaults when the app targets a hosted backend.
|
|
48
|
-
*/
|
|
49
|
-
export declare function provisionEnvVars(args: {
|
|
50
|
-
databaseId: string;
|
|
51
|
-
databaseName: string;
|
|
52
|
-
ownerId: string;
|
|
53
|
-
apiEndpoint: string;
|
|
54
|
-
modulesEndpoint: string;
|
|
55
|
-
}): EnvVars;
|
|
56
|
-
/**
|
|
57
|
-
* Comment out the given keys in place (`# archived <date>: KEY=value`) so a
|
|
58
|
-
* reprovision keeps the previous binding readable in the file instead of
|
|
59
|
-
* silently overwriting it. Every other line — including existing comments and
|
|
60
|
-
* already-archived entries — passes through untouched.
|
|
61
|
-
*/
|
|
62
|
-
export declare function archiveBindingKeys(content: string, keys: readonly string[], archivedOn: string): string;
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
// Pure .env edits — used by provision_database to persist provisioning
|
|
2
|
-
// credentials without clobbering existing keys.
|
|
3
|
-
// Kept side-effect-free so it is unit-testable (the tool does the fs write).
|
|
4
|
-
//
|
|
5
|
-
// Reads go through the same parser context.ts uses (quotes, `export`,
|
|
6
|
-
// comments, the last definition wins), and edits find a key the way that
|
|
7
|
-
// parser does, so a key written as `export KEY="value"` or spread over several
|
|
8
|
-
// lines by a quoted value is still the key being replaced or removed.
|
|
9
|
-
import { parseDotenv } from '12factor-env/dotenv';
|
|
10
|
-
/** Parse .env text: quoted values, `export` prefixes and comments; last definition wins. */
|
|
11
|
-
export function readEnv(content) {
|
|
12
|
-
return parseDotenv(content);
|
|
13
|
-
}
|
|
14
|
-
const ENTRY_RE = /^(\s*)(export\s+)?([\w.-]+)\s*=\s*(.*)$/;
|
|
15
|
-
const QUOTES = new Set(['"', "'", '`']);
|
|
16
|
-
// Group lines into entries: a key with the lines its value spans (a quoted
|
|
17
|
-
// value left open continues to the line that closes it), or a non-entry line
|
|
18
|
-
// (comment, blank, anything else) with a null key.
|
|
19
|
-
function envEntries(content) {
|
|
20
|
-
const lines = content.split('\n');
|
|
21
|
-
const entries = [];
|
|
22
|
-
for (let i = 0; i < lines.length; i++) {
|
|
23
|
-
const line = lines[i];
|
|
24
|
-
const match = line.trimStart().startsWith('#') ? null : ENTRY_RE.exec(line);
|
|
25
|
-
if (!match) {
|
|
26
|
-
entries.push({ key: null, exported: false, lines: [line] });
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
const value = match[4];
|
|
30
|
-
const span = [line];
|
|
31
|
-
if (QUOTES.has(value[0]) && !value.slice(1).includes(value[0])) {
|
|
32
|
-
while (i + 1 < lines.length) {
|
|
33
|
-
span.push(lines[++i]);
|
|
34
|
-
if (lines[i].includes(value[0]))
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
entries.push({ key: match[3], exported: !!match[2], lines: span });
|
|
39
|
-
}
|
|
40
|
-
return entries;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Upsert `vars` into the existing `.env` `content`. An existing key is
|
|
44
|
-
* replaced in place (keeping an `export` prefix), and any later definition of
|
|
45
|
-
* it is dropped so the new value is the one that wins; new keys are appended.
|
|
46
|
-
* Returns the new file body, trailing-newline-terminated.
|
|
47
|
-
*/
|
|
48
|
-
export function mergeEnv(content, vars) {
|
|
49
|
-
const remaining = new Map(Object.entries(vars));
|
|
50
|
-
const written = new Set();
|
|
51
|
-
const out = [];
|
|
52
|
-
for (const entry of envEntries(content)) {
|
|
53
|
-
if (entry.key !== null && remaining.has(entry.key)) {
|
|
54
|
-
out.push(`${entry.exported ? 'export ' : ''}${entry.key}=${remaining.get(entry.key)}`);
|
|
55
|
-
remaining.delete(entry.key);
|
|
56
|
-
written.add(entry.key);
|
|
57
|
-
}
|
|
58
|
-
else if (entry.key === null || !written.has(entry.key)) {
|
|
59
|
-
out.push(...entry.lines);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
let text = out.join('\n');
|
|
63
|
-
for (const [key, value] of remaining)
|
|
64
|
-
text += `\n${key}=${value}`;
|
|
65
|
-
return text.trim() + '\n';
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Drop every definition of `keys`. Comments and other entries pass through,
|
|
69
|
-
* and content without those keys comes back unchanged.
|
|
70
|
-
*/
|
|
71
|
-
export function removeEnvKeys(content, keys) {
|
|
72
|
-
const keySet = new Set(keys);
|
|
73
|
-
const entries = envEntries(content);
|
|
74
|
-
const kept = entries.filter((entry) => entry.key === null || !keySet.has(entry.key));
|
|
75
|
-
return kept.length === entries.length ? content : kept.flatMap((entry) => entry.lines).join('\n');
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* The provision request provision_database is still waiting on, recorded when
|
|
79
|
-
* it stops waiting so the next call resumes that ticket. It's resumed only
|
|
80
|
-
* for the same database name, backend and account. Absent whenever no request
|
|
81
|
-
* is pending.
|
|
82
|
-
*/
|
|
83
|
-
export const PENDING_PROVISION_KEYS = {
|
|
84
|
-
ticketId: 'PROVISION_TICKET_ID',
|
|
85
|
-
databaseName: 'PROVISION_TICKET_DATABASE_NAME',
|
|
86
|
-
apiEndpoint: 'PROVISION_TICKET_API_ENDPOINT',
|
|
87
|
-
userId: 'PROVISION_TICKET_USER_ID',
|
|
88
|
-
attempts: 'PROVISION_TICKET_ATTEMPTS',
|
|
89
|
-
};
|
|
90
|
-
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
91
|
-
/** The backend's own check on a database name, which is 3 to 63 characters long. */
|
|
92
|
-
export const DATABASE_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
93
|
-
const isDatabaseName = (value) => value.length >= 3 && value.length <= 63 && DATABASE_NAME_PATTERN.test(value);
|
|
94
|
-
function isEndpoint(value) {
|
|
95
|
-
try {
|
|
96
|
-
return ['http:', 'https:'].includes(new URL(value).protocol);
|
|
97
|
-
}
|
|
98
|
-
catch {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* The form of each binding value the tools read from a project's .env. The ticket's endpoint and account need none:
|
|
104
|
-
* they are used only when they equal the host's own, and its attempt count is read as a number.
|
|
105
|
-
*/
|
|
106
|
-
const BINDING_FORMS = {
|
|
107
|
-
DATABASE_ID: (value) => UUID.test(value),
|
|
108
|
-
OWNER_ID: (value) => UUID.test(value),
|
|
109
|
-
DATABASE_NAME: isDatabaseName,
|
|
110
|
-
API_ENDPOINT: isEndpoint,
|
|
111
|
-
MODULES_ENDPOINT: isEndpoint,
|
|
112
|
-
[PENDING_PROVISION_KEYS.ticketId]: (value) => UUID.test(value),
|
|
113
|
-
[PENDING_PROVISION_KEYS.databaseName]: isDatabaseName,
|
|
114
|
-
};
|
|
115
|
-
/**
|
|
116
|
-
* The binding keys whose value in `env` is not of its form. The .env of a cloned project is not the person's text, so
|
|
117
|
-
* such a value is never used, sent or shown: the caller refuses with the keys alone.
|
|
118
|
-
*/
|
|
119
|
-
export const invalidBindingKeys = (env) => Object.entries(BINDING_FORMS).flatMap(([key, valid]) => (env[key] && !valid(env[key]) ? [key] : []));
|
|
120
|
-
/** The binding keys provision_database owns in a project's .env. */
|
|
121
|
-
export const PROVISION_ENV_KEYS = [
|
|
122
|
-
'DATABASE_ID',
|
|
123
|
-
'DATABASE_NAME',
|
|
124
|
-
'OWNER_ID',
|
|
125
|
-
'PGDATABASE',
|
|
126
|
-
'API_ENDPOINT',
|
|
127
|
-
'MODULES_ENDPOINT',
|
|
128
|
-
];
|
|
129
|
-
/**
|
|
130
|
-
* Keys archived as comments on reprovision — the binding identity: which
|
|
131
|
-
* database, under whom, on which backend.
|
|
132
|
-
*/
|
|
133
|
-
export const ARCHIVED_BINDING_KEYS = [
|
|
134
|
-
'DATABASE_ID',
|
|
135
|
-
'DATABASE_NAME',
|
|
136
|
-
'OWNER_ID',
|
|
137
|
-
'PGDATABASE',
|
|
138
|
-
'API_ENDPOINT',
|
|
139
|
-
'MODULES_ENDPOINT',
|
|
140
|
-
];
|
|
141
|
-
/**
|
|
142
|
-
* Build the database binding written to a project's .env. The
|
|
143
|
-
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
144
|
-
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
145
|
-
* falling back to localhost defaults when the app targets a hosted backend.
|
|
146
|
-
*/
|
|
147
|
-
export function provisionEnvVars(args) {
|
|
148
|
-
return {
|
|
149
|
-
DATABASE_ID: args.databaseId,
|
|
150
|
-
DATABASE_NAME: args.databaseName,
|
|
151
|
-
OWNER_ID: args.ownerId,
|
|
152
|
-
PGDATABASE: args.databaseName,
|
|
153
|
-
API_ENDPOINT: args.apiEndpoint,
|
|
154
|
-
MODULES_ENDPOINT: args.modulesEndpoint,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Comment out the given keys in place (`# archived <date>: KEY=value`) so a
|
|
159
|
-
* reprovision keeps the previous binding readable in the file instead of
|
|
160
|
-
* silently overwriting it. Every other line — including existing comments and
|
|
161
|
-
* already-archived entries — passes through untouched.
|
|
162
|
-
*/
|
|
163
|
-
export function archiveBindingKeys(content, keys, archivedOn) {
|
|
164
|
-
const keySet = new Set(keys);
|
|
165
|
-
return envEntries(content)
|
|
166
|
-
.flatMap((entry) => entry.key !== null && keySet.has(entry.key)
|
|
167
|
-
? entry.lines.map((line) => `# archived ${archivedOn}: ${line.trim()}`)
|
|
168
|
-
: entry.lines)
|
|
169
|
-
.join('\n');
|
|
170
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type EnvVars } from './env-file';
|
|
2
|
-
/**
|
|
3
|
-
* Pending results recorded for one ticket before provision_database stops
|
|
4
|
-
* following it. At the default 240 s deadline that is about 20 minutes.
|
|
5
|
-
*/
|
|
6
|
-
export declare const MAX_PENDING_ATTEMPTS = 5;
|
|
7
|
-
export type PendingTicket = {
|
|
8
|
-
ticketId: string;
|
|
9
|
-
databaseName: string;
|
|
10
|
-
apiEndpoint: string;
|
|
11
|
-
userId: string;
|
|
12
|
-
/** Pending results recorded for this ticket so far. */
|
|
13
|
-
attempts: number;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Tickets the host told provisioning to stop following (`abandonPending`),
|
|
17
|
-
* comma-separated. A lookup by name skips them, so an abandoned provision is
|
|
18
|
-
* never resumed.
|
|
19
|
-
*/
|
|
20
|
-
export declare const ABANDONED_TICKETS_KEY = "PROVISION_TICKET_ABANDONED";
|
|
21
|
-
/**
|
|
22
|
-
* The recorded ticket, with the name, backend and account it was requested
|
|
23
|
-
* for. Only a ticket from this backend and account is ours to poll, and one
|
|
24
|
-
* for another name is a different database.
|
|
25
|
-
*/
|
|
26
|
-
export declare function recordedTicket(env: EnvVars): PendingTicket | undefined;
|
|
27
|
-
export declare function recordPendingTicket(content: string, ticket: PendingTicket): string;
|
|
28
|
-
export declare function clearPendingTicket(content: string): string;
|
|
29
|
-
/** Comment the record out (`# archived <date>: …`), keeping it readable. */
|
|
30
|
-
export declare function archivePendingTicket(content: string, archivedOn: string): string;
|
|
31
|
-
export declare function abandonedTickets(env: EnvVars): Set<string>;
|
|
32
|
-
/** Add ticket ids to the abandoned list. */
|
|
33
|
-
export declare function recordAbandoned(content: string, ticketIds: readonly string[]): string;
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// The provision request provision_database stopped waiting on, as recorded in
|
|
2
|
-
// a project's .env. requestDatabase never resumes by name, so the ticket is
|
|
3
|
-
// the only way back to a provision that is still running.
|
|
4
|
-
import { archiveBindingKeys, mergeEnv, PENDING_PROVISION_KEYS, readEnv, removeEnvKeys, } from './env-file';
|
|
5
|
-
/**
|
|
6
|
-
* Pending results recorded for one ticket before provision_database stops
|
|
7
|
-
* following it. At the default 240 s deadline that is about 20 minutes.
|
|
8
|
-
*/
|
|
9
|
-
export const MAX_PENDING_ATTEMPTS = 5;
|
|
10
|
-
const KEYS = Object.values(PENDING_PROVISION_KEYS);
|
|
11
|
-
/**
|
|
12
|
-
* Tickets the host told provisioning to stop following (`abandonPending`),
|
|
13
|
-
* comma-separated. A lookup by name skips them, so an abandoned provision is
|
|
14
|
-
* never resumed.
|
|
15
|
-
*/
|
|
16
|
-
export const ABANDONED_TICKETS_KEY = 'PROVISION_TICKET_ABANDONED';
|
|
17
|
-
/** The newest abandoned ids kept; older ones are long finished or failed. */
|
|
18
|
-
const MAX_ABANDONED = 10;
|
|
19
|
-
/**
|
|
20
|
-
* The recorded ticket, with the name, backend and account it was requested
|
|
21
|
-
* for. Only a ticket from this backend and account is ours to poll, and one
|
|
22
|
-
* for another name is a different database.
|
|
23
|
-
*/
|
|
24
|
-
export function recordedTicket(env) {
|
|
25
|
-
const ticketId = env[PENDING_PROVISION_KEYS.ticketId];
|
|
26
|
-
if (!ticketId)
|
|
27
|
-
return undefined;
|
|
28
|
-
const attempts = Number.parseInt(env[PENDING_PROVISION_KEYS.attempts] ?? '', 10);
|
|
29
|
-
return {
|
|
30
|
-
ticketId,
|
|
31
|
-
databaseName: env[PENDING_PROVISION_KEYS.databaseName] ?? '',
|
|
32
|
-
apiEndpoint: env[PENDING_PROVISION_KEYS.apiEndpoint] ?? '',
|
|
33
|
-
userId: env[PENDING_PROVISION_KEYS.userId] ?? '',
|
|
34
|
-
attempts: Number.isFinite(attempts) && attempts > 0 ? attempts : 0,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
export function recordPendingTicket(content, ticket) {
|
|
38
|
-
return mergeEnv(content, {
|
|
39
|
-
[PENDING_PROVISION_KEYS.ticketId]: ticket.ticketId,
|
|
40
|
-
[PENDING_PROVISION_KEYS.databaseName]: ticket.databaseName,
|
|
41
|
-
[PENDING_PROVISION_KEYS.apiEndpoint]: ticket.apiEndpoint,
|
|
42
|
-
[PENDING_PROVISION_KEYS.userId]: ticket.userId,
|
|
43
|
-
[PENDING_PROVISION_KEYS.attempts]: String(ticket.attempts),
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
export function clearPendingTicket(content) {
|
|
47
|
-
return removeEnvKeys(content, KEYS);
|
|
48
|
-
}
|
|
49
|
-
/** Comment the record out (`# archived <date>: …`), keeping it readable. */
|
|
50
|
-
export function archivePendingTicket(content, archivedOn) {
|
|
51
|
-
return archiveBindingKeys(content, KEYS, archivedOn);
|
|
52
|
-
}
|
|
53
|
-
export function abandonedTickets(env) {
|
|
54
|
-
return new Set((env[ABANDONED_TICKETS_KEY] ?? '')
|
|
55
|
-
.split(',')
|
|
56
|
-
.map((id) => id.trim())
|
|
57
|
-
.filter(Boolean));
|
|
58
|
-
}
|
|
59
|
-
/** Add ticket ids to the abandoned list. */
|
|
60
|
-
export function recordAbandoned(content, ticketIds) {
|
|
61
|
-
const ids = [...new Set([...abandonedTickets(readEnv(content)), ...ticketIds])].slice(-MAX_ABANDONED);
|
|
62
|
-
return mergeEnv(content, { [ABANDONED_TICKETS_KEY]: ids.join(',') });
|
|
63
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
export type EnvVars = Record<string, string>;
|
|
2
|
-
/** Parse .env text: quoted values, `export` prefixes and comments; last definition wins. */
|
|
3
|
-
export declare function readEnv(content: string): EnvVars;
|
|
4
|
-
/**
|
|
5
|
-
* Upsert `vars` into the existing `.env` `content`. An existing key is
|
|
6
|
-
* replaced in place (keeping an `export` prefix), and any later definition of
|
|
7
|
-
* it is dropped so the new value is the one that wins; new keys are appended.
|
|
8
|
-
* Returns the new file body, trailing-newline-terminated.
|
|
9
|
-
*/
|
|
10
|
-
export declare function mergeEnv(content: string, vars: EnvVars): string;
|
|
11
|
-
/**
|
|
12
|
-
* Drop every definition of `keys`. Comments and other entries pass through,
|
|
13
|
-
* and content without those keys comes back unchanged.
|
|
14
|
-
*/
|
|
15
|
-
export declare function removeEnvKeys(content: string, keys: readonly string[]): string;
|
|
16
|
-
/**
|
|
17
|
-
* The provision request provision_database is still waiting on, recorded when
|
|
18
|
-
* it stops waiting so the next call resumes that ticket. It's resumed only
|
|
19
|
-
* for the same database name, backend and account. Absent whenever no request
|
|
20
|
-
* is pending.
|
|
21
|
-
*/
|
|
22
|
-
export declare const PENDING_PROVISION_KEYS: {
|
|
23
|
-
readonly ticketId: "PROVISION_TICKET_ID";
|
|
24
|
-
readonly databaseName: "PROVISION_TICKET_DATABASE_NAME";
|
|
25
|
-
readonly apiEndpoint: "PROVISION_TICKET_API_ENDPOINT";
|
|
26
|
-
readonly userId: "PROVISION_TICKET_USER_ID";
|
|
27
|
-
readonly attempts: "PROVISION_TICKET_ATTEMPTS";
|
|
28
|
-
};
|
|
29
|
-
/** The backend's own check on a database name, which is 3 to 63 characters long. */
|
|
30
|
-
export declare const DATABASE_NAME_PATTERN: RegExp;
|
|
31
|
-
/**
|
|
32
|
-
* The binding keys whose value in `env` is not of its form. The .env of a cloned project is not the person's text, so
|
|
33
|
-
* such a value is never used, sent or shown: the caller refuses with the keys alone.
|
|
34
|
-
*/
|
|
35
|
-
export declare const invalidBindingKeys: (env: Record<string, string | undefined>) => string[];
|
|
36
|
-
/** The binding keys provision_database owns in a project's .env. */
|
|
37
|
-
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
38
|
-
/**
|
|
39
|
-
* Keys archived as comments on reprovision — the binding identity: which
|
|
40
|
-
* database, under whom, on which backend.
|
|
41
|
-
*/
|
|
42
|
-
export declare const ARCHIVED_BINDING_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
43
|
-
/**
|
|
44
|
-
* Build the database binding written to a project's .env. The
|
|
45
|
-
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
46
|
-
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
47
|
-
* falling back to localhost defaults when the app targets a hosted backend.
|
|
48
|
-
*/
|
|
49
|
-
export declare function provisionEnvVars(args: {
|
|
50
|
-
databaseId: string;
|
|
51
|
-
databaseName: string;
|
|
52
|
-
ownerId: string;
|
|
53
|
-
apiEndpoint: string;
|
|
54
|
-
modulesEndpoint: string;
|
|
55
|
-
}): EnvVars;
|
|
56
|
-
/**
|
|
57
|
-
* Comment out the given keys in place (`# archived <date>: KEY=value`) so a
|
|
58
|
-
* reprovision keeps the previous binding readable in the file instead of
|
|
59
|
-
* silently overwriting it. Every other line — including existing comments and
|
|
60
|
-
* already-archived entries — passes through untouched.
|
|
61
|
-
*/
|
|
62
|
-
export declare function archiveBindingKeys(content: string, keys: readonly string[], archivedOn: string): string;
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// Pure .env edits — used by provision_database to persist provisioning
|
|
3
|
-
// credentials without clobbering existing keys.
|
|
4
|
-
// Kept side-effect-free so it is unit-testable (the tool does the fs write).
|
|
5
|
-
//
|
|
6
|
-
// Reads go through the same parser context.ts uses (quotes, `export`,
|
|
7
|
-
// comments, the last definition wins), and edits find a key the way that
|
|
8
|
-
// parser does, so a key written as `export KEY="value"` or spread over several
|
|
9
|
-
// lines by a quoted value is still the key being replaced or removed.
|
|
10
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.ARCHIVED_BINDING_KEYS = exports.PROVISION_ENV_KEYS = exports.invalidBindingKeys = exports.DATABASE_NAME_PATTERN = exports.PENDING_PROVISION_KEYS = void 0;
|
|
12
|
-
exports.readEnv = readEnv;
|
|
13
|
-
exports.mergeEnv = mergeEnv;
|
|
14
|
-
exports.removeEnvKeys = removeEnvKeys;
|
|
15
|
-
exports.provisionEnvVars = provisionEnvVars;
|
|
16
|
-
exports.archiveBindingKeys = archiveBindingKeys;
|
|
17
|
-
const dotenv_1 = require("12factor-env/dotenv");
|
|
18
|
-
/** Parse .env text: quoted values, `export` prefixes and comments; last definition wins. */
|
|
19
|
-
function readEnv(content) {
|
|
20
|
-
return (0, dotenv_1.parseDotenv)(content);
|
|
21
|
-
}
|
|
22
|
-
const ENTRY_RE = /^(\s*)(export\s+)?([\w.-]+)\s*=\s*(.*)$/;
|
|
23
|
-
const QUOTES = new Set(['"', "'", '`']);
|
|
24
|
-
// Group lines into entries: a key with the lines its value spans (a quoted
|
|
25
|
-
// value left open continues to the line that closes it), or a non-entry line
|
|
26
|
-
// (comment, blank, anything else) with a null key.
|
|
27
|
-
function envEntries(content) {
|
|
28
|
-
const lines = content.split('\n');
|
|
29
|
-
const entries = [];
|
|
30
|
-
for (let i = 0; i < lines.length; i++) {
|
|
31
|
-
const line = lines[i];
|
|
32
|
-
const match = line.trimStart().startsWith('#') ? null : ENTRY_RE.exec(line);
|
|
33
|
-
if (!match) {
|
|
34
|
-
entries.push({ key: null, exported: false, lines: [line] });
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
const value = match[4];
|
|
38
|
-
const span = [line];
|
|
39
|
-
if (QUOTES.has(value[0]) && !value.slice(1).includes(value[0])) {
|
|
40
|
-
while (i + 1 < lines.length) {
|
|
41
|
-
span.push(lines[++i]);
|
|
42
|
-
if (lines[i].includes(value[0]))
|
|
43
|
-
break;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
entries.push({ key: match[3], exported: !!match[2], lines: span });
|
|
47
|
-
}
|
|
48
|
-
return entries;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Upsert `vars` into the existing `.env` `content`. An existing key is
|
|
52
|
-
* replaced in place (keeping an `export` prefix), and any later definition of
|
|
53
|
-
* it is dropped so the new value is the one that wins; new keys are appended.
|
|
54
|
-
* Returns the new file body, trailing-newline-terminated.
|
|
55
|
-
*/
|
|
56
|
-
function mergeEnv(content, vars) {
|
|
57
|
-
const remaining = new Map(Object.entries(vars));
|
|
58
|
-
const written = new Set();
|
|
59
|
-
const out = [];
|
|
60
|
-
for (const entry of envEntries(content)) {
|
|
61
|
-
if (entry.key !== null && remaining.has(entry.key)) {
|
|
62
|
-
out.push(`${entry.exported ? 'export ' : ''}${entry.key}=${remaining.get(entry.key)}`);
|
|
63
|
-
remaining.delete(entry.key);
|
|
64
|
-
written.add(entry.key);
|
|
65
|
-
}
|
|
66
|
-
else if (entry.key === null || !written.has(entry.key)) {
|
|
67
|
-
out.push(...entry.lines);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
let text = out.join('\n');
|
|
71
|
-
for (const [key, value] of remaining)
|
|
72
|
-
text += `\n${key}=${value}`;
|
|
73
|
-
return text.trim() + '\n';
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Drop every definition of `keys`. Comments and other entries pass through,
|
|
77
|
-
* and content without those keys comes back unchanged.
|
|
78
|
-
*/
|
|
79
|
-
function removeEnvKeys(content, keys) {
|
|
80
|
-
const keySet = new Set(keys);
|
|
81
|
-
const entries = envEntries(content);
|
|
82
|
-
const kept = entries.filter((entry) => entry.key === null || !keySet.has(entry.key));
|
|
83
|
-
return kept.length === entries.length ? content : kept.flatMap((entry) => entry.lines).join('\n');
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* The provision request provision_database is still waiting on, recorded when
|
|
87
|
-
* it stops waiting so the next call resumes that ticket. It's resumed only
|
|
88
|
-
* for the same database name, backend and account. Absent whenever no request
|
|
89
|
-
* is pending.
|
|
90
|
-
*/
|
|
91
|
-
exports.PENDING_PROVISION_KEYS = {
|
|
92
|
-
ticketId: 'PROVISION_TICKET_ID',
|
|
93
|
-
databaseName: 'PROVISION_TICKET_DATABASE_NAME',
|
|
94
|
-
apiEndpoint: 'PROVISION_TICKET_API_ENDPOINT',
|
|
95
|
-
userId: 'PROVISION_TICKET_USER_ID',
|
|
96
|
-
attempts: 'PROVISION_TICKET_ATTEMPTS',
|
|
97
|
-
};
|
|
98
|
-
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
99
|
-
/** The backend's own check on a database name, which is 3 to 63 characters long. */
|
|
100
|
-
exports.DATABASE_NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
101
|
-
const isDatabaseName = (value) => value.length >= 3 && value.length <= 63 && exports.DATABASE_NAME_PATTERN.test(value);
|
|
102
|
-
function isEndpoint(value) {
|
|
103
|
-
try {
|
|
104
|
-
return ['http:', 'https:'].includes(new URL(value).protocol);
|
|
105
|
-
}
|
|
106
|
-
catch {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* The form of each binding value the tools read from a project's .env. The ticket's endpoint and account need none:
|
|
112
|
-
* they are used only when they equal the host's own, and its attempt count is read as a number.
|
|
113
|
-
*/
|
|
114
|
-
const BINDING_FORMS = {
|
|
115
|
-
DATABASE_ID: (value) => UUID.test(value),
|
|
116
|
-
OWNER_ID: (value) => UUID.test(value),
|
|
117
|
-
DATABASE_NAME: isDatabaseName,
|
|
118
|
-
API_ENDPOINT: isEndpoint,
|
|
119
|
-
MODULES_ENDPOINT: isEndpoint,
|
|
120
|
-
[exports.PENDING_PROVISION_KEYS.ticketId]: (value) => UUID.test(value),
|
|
121
|
-
[exports.PENDING_PROVISION_KEYS.databaseName]: isDatabaseName,
|
|
122
|
-
};
|
|
123
|
-
/**
|
|
124
|
-
* The binding keys whose value in `env` is not of its form. The .env of a cloned project is not the person's text, so
|
|
125
|
-
* such a value is never used, sent or shown: the caller refuses with the keys alone.
|
|
126
|
-
*/
|
|
127
|
-
const invalidBindingKeys = (env) => Object.entries(BINDING_FORMS).flatMap(([key, valid]) => (env[key] && !valid(env[key]) ? [key] : []));
|
|
128
|
-
exports.invalidBindingKeys = invalidBindingKeys;
|
|
129
|
-
/** The binding keys provision_database owns in a project's .env. */
|
|
130
|
-
exports.PROVISION_ENV_KEYS = [
|
|
131
|
-
'DATABASE_ID',
|
|
132
|
-
'DATABASE_NAME',
|
|
133
|
-
'OWNER_ID',
|
|
134
|
-
'PGDATABASE',
|
|
135
|
-
'API_ENDPOINT',
|
|
136
|
-
'MODULES_ENDPOINT',
|
|
137
|
-
];
|
|
138
|
-
/**
|
|
139
|
-
* Keys archived as comments on reprovision — the binding identity: which
|
|
140
|
-
* database, under whom, on which backend.
|
|
141
|
-
*/
|
|
142
|
-
exports.ARCHIVED_BINDING_KEYS = [
|
|
143
|
-
'DATABASE_ID',
|
|
144
|
-
'DATABASE_NAME',
|
|
145
|
-
'OWNER_ID',
|
|
146
|
-
'PGDATABASE',
|
|
147
|
-
'API_ENDPOINT',
|
|
148
|
-
'MODULES_ENDPOINT',
|
|
149
|
-
];
|
|
150
|
-
/**
|
|
151
|
-
* Build the database binding written to a project's .env. The
|
|
152
|
-
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
153
|
-
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
154
|
-
* falling back to localhost defaults when the app targets a hosted backend.
|
|
155
|
-
*/
|
|
156
|
-
function provisionEnvVars(args) {
|
|
157
|
-
return {
|
|
158
|
-
DATABASE_ID: args.databaseId,
|
|
159
|
-
DATABASE_NAME: args.databaseName,
|
|
160
|
-
OWNER_ID: args.ownerId,
|
|
161
|
-
PGDATABASE: args.databaseName,
|
|
162
|
-
API_ENDPOINT: args.apiEndpoint,
|
|
163
|
-
MODULES_ENDPOINT: args.modulesEndpoint,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Comment out the given keys in place (`# archived <date>: KEY=value`) so a
|
|
168
|
-
* reprovision keeps the previous binding readable in the file instead of
|
|
169
|
-
* silently overwriting it. Every other line — including existing comments and
|
|
170
|
-
* already-archived entries — passes through untouched.
|
|
171
|
-
*/
|
|
172
|
-
function archiveBindingKeys(content, keys, archivedOn) {
|
|
173
|
-
const keySet = new Set(keys);
|
|
174
|
-
return envEntries(content)
|
|
175
|
-
.flatMap((entry) => entry.key !== null && keySet.has(entry.key)
|
|
176
|
-
? entry.lines.map((line) => `# archived ${archivedOn}: ${line.trim()}`)
|
|
177
|
-
: entry.lines)
|
|
178
|
-
.join('\n');
|
|
179
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { type EnvVars } from './env-file';
|
|
2
|
-
/**
|
|
3
|
-
* Pending results recorded for one ticket before provision_database stops
|
|
4
|
-
* following it. At the default 240 s deadline that is about 20 minutes.
|
|
5
|
-
*/
|
|
6
|
-
export declare const MAX_PENDING_ATTEMPTS = 5;
|
|
7
|
-
export type PendingTicket = {
|
|
8
|
-
ticketId: string;
|
|
9
|
-
databaseName: string;
|
|
10
|
-
apiEndpoint: string;
|
|
11
|
-
userId: string;
|
|
12
|
-
/** Pending results recorded for this ticket so far. */
|
|
13
|
-
attempts: number;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Tickets the host told provisioning to stop following (`abandonPending`),
|
|
17
|
-
* comma-separated. A lookup by name skips them, so an abandoned provision is
|
|
18
|
-
* never resumed.
|
|
19
|
-
*/
|
|
20
|
-
export declare const ABANDONED_TICKETS_KEY = "PROVISION_TICKET_ABANDONED";
|
|
21
|
-
/**
|
|
22
|
-
* The recorded ticket, with the name, backend and account it was requested
|
|
23
|
-
* for. Only a ticket from this backend and account is ours to poll, and one
|
|
24
|
-
* for another name is a different database.
|
|
25
|
-
*/
|
|
26
|
-
export declare function recordedTicket(env: EnvVars): PendingTicket | undefined;
|
|
27
|
-
export declare function recordPendingTicket(content: string, ticket: PendingTicket): string;
|
|
28
|
-
export declare function clearPendingTicket(content: string): string;
|
|
29
|
-
/** Comment the record out (`# archived <date>: …`), keeping it readable. */
|
|
30
|
-
export declare function archivePendingTicket(content: string, archivedOn: string): string;
|
|
31
|
-
export declare function abandonedTickets(env: EnvVars): Set<string>;
|
|
32
|
-
/** Add ticket ids to the abandoned list. */
|
|
33
|
-
export declare function recordAbandoned(content: string, ticketIds: readonly string[]): string;
|