@neondatabase/config-runtime 0.0.0 → 0.1.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/LICENSE.md +178 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/lib/function-bundle.d.ts +25 -0
- package/dist/lib/function-bundle.d.ts.map +1 -0
- package/dist/lib/function-bundle.js +65 -0
- package/dist/lib/function-bundle.js.map +1 -0
- package/dist/lib/operations.d.ts +70 -0
- package/dist/lib/operations.d.ts.map +1 -0
- package/dist/lib/operations.js +60 -0
- package/dist/lib/operations.js.map +1 -0
- package/dist/lib/pull-config.d.ts +59 -0
- package/dist/lib/pull-config.d.ts.map +1 -0
- package/dist/lib/pull-config.js +93 -0
- package/dist/lib/pull-config.js.map +1 -0
- package/dist/lib/push-config.d.ts +104 -0
- package/dist/lib/push-config.d.ts.map +1 -0
- package/dist/lib/push-config.js +389 -0
- package/dist/lib/push-config.js.map +1 -0
- package/dist/v1.d.ts +6 -0
- package/dist/v1.js +6 -0
- package/package.json +69 -22
- package/e2e/conflict.e2e.test.ts +0 -34
- package/e2e/helpers.ts +0 -204
- package/e2e/lifecycle.e2e.test.ts +0 -50
- package/e2e/load-env.ts +0 -29
- package/e2e/setup.ts +0 -24
- package/src/index.ts +0 -5
- package/src/lib/fake-neon-api.ts +0 -782
- package/src/lib/function-bundle.test.ts +0 -60
- package/src/lib/function-bundle.ts +0 -104
- package/src/lib/operations.test.ts +0 -150
- package/src/lib/operations.ts +0 -103
- package/src/lib/pull-config.test.ts +0 -51
- package/src/lib/pull-config.ts +0 -215
- package/src/lib/push-config.test.ts +0 -421
- package/src/lib/push-config.ts +0 -619
- package/src/v1.test.ts +0 -30
- package/src/v1.ts +0 -74
- package/tsconfig.json +0 -4
- package/tsdown.config.ts +0 -22
- package/vitest.config.ts +0 -19
- package/vitest.e2e.config.ts +0 -29
package/e2e/conflict.e2e.test.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect } from "vitest";
|
|
2
|
-
import { defineConfig, pushConfig } from "../src/v1.js";
|
|
3
|
-
import {
|
|
4
|
-
bootstrapProject,
|
|
5
|
-
DEFAULT_REGION,
|
|
6
|
-
detectApiKeyScope,
|
|
7
|
-
e2eTest,
|
|
8
|
-
makeRealApi,
|
|
9
|
-
uniqueProjectName,
|
|
10
|
-
} from "./helpers.js";
|
|
11
|
-
|
|
12
|
-
describe("e2e — branch-scoped push conflicts", () => {
|
|
13
|
-
e2eTest(
|
|
14
|
-
"reports branch drift without updateExisting",
|
|
15
|
-
async ({ track }) => {
|
|
16
|
-
const scope = await detectApiKeyScope();
|
|
17
|
-
if (scope.kind !== "org-or-user") return;
|
|
18
|
-
const api = makeRealApi();
|
|
19
|
-
const projectId = await bootstrapProject(api, {
|
|
20
|
-
name: uniqueProjectName("conflict"),
|
|
21
|
-
region: DEFAULT_REGION,
|
|
22
|
-
});
|
|
23
|
-
track(projectId);
|
|
24
|
-
const branchId = (await api.listBranches(projectId)).find(
|
|
25
|
-
(b) => b.isDefault,
|
|
26
|
-
)?.id;
|
|
27
|
-
if (!branchId) throw new Error("missing default branch");
|
|
28
|
-
const config = defineConfig(() => ({ protected: true }));
|
|
29
|
-
await expect(
|
|
30
|
-
pushConfig(config, { api, projectId, branchId }),
|
|
31
|
-
).rejects.toMatchObject({ code: "PLATFORM_PUSH_CONFLICT" });
|
|
32
|
-
},
|
|
33
|
-
);
|
|
34
|
-
});
|
package/e2e/helpers.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { createApiClient } from "@neondatabase/api-client";
|
|
3
|
-
import { createRealNeonApi, type NeonApi } from "@neondatabase/config";
|
|
4
|
-
import { test } from "vitest";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Every e2e-created project is named `neon-ts-e2e-<uuid>`. Tests can register
|
|
8
|
-
* `track(id)` to opt into the per-test cleanup hook. The suite-level
|
|
9
|
-
* {@link sweepOrphans} additionally deletes leftovers from a previous failed run.
|
|
10
|
-
*/
|
|
11
|
-
const PROJECT_PREFIX = "neon-ts-e2e-";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Default Neon region used by every e2e test that creates a project. Override per-test
|
|
15
|
-
* by passing `region` to `defineConfig`.
|
|
16
|
-
*/
|
|
17
|
-
export const DEFAULT_REGION = "aws-us-east-2";
|
|
18
|
-
|
|
19
|
-
/** Generate a project name guaranteed not to collide with anything else in the org. */
|
|
20
|
-
export function uniqueProjectName(suffix?: string): string {
|
|
21
|
-
const id = randomUUID().slice(0, 8);
|
|
22
|
-
return suffix
|
|
23
|
-
? `${PROJECT_PREFIX}${id}-${suffix}`
|
|
24
|
-
: `${PROJECT_PREFIX}${id}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function requireApiKey(): string {
|
|
28
|
-
const key = process.env.NEON_API_KEY;
|
|
29
|
-
if (!key || key.trim() === "") {
|
|
30
|
-
throw new Error(
|
|
31
|
-
"NEON_API_KEY is not set. Create packages/config/.env (see .env.example) before running test:e2e.",
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
return key;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** The same real NeonApi adapter the SDK uses internally — exercised end-to-end. */
|
|
38
|
-
export function makeRealApi(): NeonApi {
|
|
39
|
-
return createRealNeonApi({ apiKey: requireApiKey() });
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Create a real Neon project via the NeonApi adapter directly. `pushConfig` no longer
|
|
44
|
-
* provisions projects (callers are expected to run `neonctl link` first), so every e2e
|
|
45
|
-
* test that needs a fresh project to push against goes through this helper instead.
|
|
46
|
-
*/
|
|
47
|
-
export async function bootstrapProject(
|
|
48
|
-
api: NeonApi,
|
|
49
|
-
args: { name: string; region: string },
|
|
50
|
-
): Promise<string> {
|
|
51
|
-
const created = await api.createProject({
|
|
52
|
-
name: args.name,
|
|
53
|
-
regionId: args.region,
|
|
54
|
-
});
|
|
55
|
-
return created.id;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Lower-level Neon client. Used by cleanup and a few setup helpers. */
|
|
59
|
-
function makeRawClient(): ReturnType<typeof createApiClient> {
|
|
60
|
-
return createApiClient({ apiKey: requireApiKey() });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Discriminates the key currently configured. Project-scoped keys can't list projects;
|
|
65
|
-
* org/user-scoped keys can.
|
|
66
|
-
*/
|
|
67
|
-
export type ApiKeyScope =
|
|
68
|
-
| { kind: "org-or-user"; canCreate: true }
|
|
69
|
-
| { kind: "project"; projectId: string; canCreate: false };
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Probe the configured API key to find out what it can do. Memoised because we only need
|
|
73
|
-
* to do this once per process.
|
|
74
|
-
*/
|
|
75
|
-
let cachedScope: ApiKeyScope | undefined;
|
|
76
|
-
export async function detectApiKeyScope(): Promise<ApiKeyScope> {
|
|
77
|
-
if (cachedScope) return cachedScope;
|
|
78
|
-
const client = makeRawClient();
|
|
79
|
-
try {
|
|
80
|
-
await client.listProjects({ limit: 1 });
|
|
81
|
-
cachedScope = { kind: "org-or-user", canCreate: true };
|
|
82
|
-
return cachedScope;
|
|
83
|
-
} catch (err) {
|
|
84
|
-
const status = (err as { response?: { status?: number } } | undefined)
|
|
85
|
-
?.response?.status;
|
|
86
|
-
if (status !== 401 && status !== 403) throw err;
|
|
87
|
-
}
|
|
88
|
-
const fixedProjectId = process.env.NEON_PROJECT_ID;
|
|
89
|
-
if (!fixedProjectId || fixedProjectId.trim() === "") {
|
|
90
|
-
throw new Error(
|
|
91
|
-
"API key cannot list projects (looks project-scoped) and NEON_PROJECT_ID is not set. " +
|
|
92
|
-
"Set NEON_PROJECT_ID in packages/config/.env to target a fixed project for the bounded e2e subset.",
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
cachedScope = {
|
|
96
|
-
kind: "project",
|
|
97
|
-
projectId: fixedProjectId,
|
|
98
|
-
canCreate: false,
|
|
99
|
-
};
|
|
100
|
-
return cachedScope;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Delete a project, ignoring "already gone" errors so cleanup is idempotent. Refuses to
|
|
105
|
-
* delete anything that isn't prefixed with {@link PROJECT_PREFIX} so a mis-typed id can
|
|
106
|
-
* never wipe an unrelated project.
|
|
107
|
-
*/
|
|
108
|
-
async function deleteProject(projectId: string): Promise<void> {
|
|
109
|
-
const client = makeRawClient();
|
|
110
|
-
const project = await client.getProject(projectId).catch((err: unknown) => {
|
|
111
|
-
const status = (err as { response?: { status?: number } } | undefined)
|
|
112
|
-
?.response?.status;
|
|
113
|
-
if (status === 404 || status === 410) return null;
|
|
114
|
-
throw err;
|
|
115
|
-
});
|
|
116
|
-
if (!project) return;
|
|
117
|
-
if (!project.data.project.name.startsWith(PROJECT_PREFIX)) {
|
|
118
|
-
throw new Error(
|
|
119
|
-
`Refusing to delete project ${projectId} ("${project.data.project.name}"): does not match the e2e prefix.`,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
// Retry on 423 (locked while a previous mutation is in flight) so cleanup is robust.
|
|
123
|
-
const maxAttempts = 12;
|
|
124
|
-
let delay = 500;
|
|
125
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
126
|
-
try {
|
|
127
|
-
await client.deleteProject(projectId);
|
|
128
|
-
return;
|
|
129
|
-
} catch (err) {
|
|
130
|
-
const status = (
|
|
131
|
-
err as { response?: { status?: number } } | undefined
|
|
132
|
-
)?.response?.status;
|
|
133
|
-
if (status === 404 || status === 410) return;
|
|
134
|
-
if (status !== 423 || attempt === maxAttempts) throw err;
|
|
135
|
-
await sleep(delay);
|
|
136
|
-
delay = Math.min(delay * 2, 5_000);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* List every project whose name starts with {@link PROJECT_PREFIX} and delete them.
|
|
143
|
-
* Called once at suite start to mop up orphans from a previous failed run.
|
|
144
|
-
*/
|
|
145
|
-
export async function sweepOrphans(): Promise<{ swept: string[] }> {
|
|
146
|
-
const scope = await detectApiKeyScope();
|
|
147
|
-
if (scope.kind === "project") return { swept: [] };
|
|
148
|
-
const client = makeRawClient();
|
|
149
|
-
const swept: string[] = [];
|
|
150
|
-
let cursor: string | undefined;
|
|
151
|
-
while (true) {
|
|
152
|
-
const res = await client.listProjects({
|
|
153
|
-
limit: 100,
|
|
154
|
-
...(cursor ? { cursor } : {}),
|
|
155
|
-
});
|
|
156
|
-
for (const project of res.data.projects) {
|
|
157
|
-
if (project.name.startsWith(PROJECT_PREFIX)) {
|
|
158
|
-
await deleteProject(project.id);
|
|
159
|
-
swept.push(project.id);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const next = (res.data as { pagination?: { next?: string } }).pagination
|
|
163
|
-
?.next;
|
|
164
|
-
if (!next || next === cursor) break;
|
|
165
|
-
cursor = next;
|
|
166
|
-
}
|
|
167
|
-
return { swept };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* A vitest `test.extend` fixture that tracks every project id a test creates and deletes
|
|
172
|
-
* each one in the cleanup phase, even if the test failed mid-way. Use `track(id)` to
|
|
173
|
-
* register ids — cleanup runs regardless of outcome.
|
|
174
|
-
*/
|
|
175
|
-
export const e2eTest = test.extend<{
|
|
176
|
-
track: (projectId: string) => void;
|
|
177
|
-
}>({
|
|
178
|
-
// biome-ignore lint/correctness/noEmptyPattern: vitest's fixture API requires this exact shape.
|
|
179
|
-
track: async ({}, use) => {
|
|
180
|
-
const created: string[] = [];
|
|
181
|
-
await use((projectId: string) => {
|
|
182
|
-
created.push(projectId);
|
|
183
|
-
});
|
|
184
|
-
for (const projectId of created) {
|
|
185
|
-
try {
|
|
186
|
-
await deleteProject(projectId);
|
|
187
|
-
} catch (err) {
|
|
188
|
-
// Surface, but don't fail on cleanup errors — the orphan sweep on the next
|
|
189
|
-
// run will mop up anything we miss.
|
|
190
|
-
console.error(
|
|
191
|
-
`[e2e cleanup] failed to delete ${projectId}: ${(err as Error).message}`,
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Some Neon operations are eventually consistent (notably branch creation finishing
|
|
200
|
-
* `init` → `ready`). A small wait avoids racing on subsequent reads.
|
|
201
|
-
*/
|
|
202
|
-
function sleep(ms: number): Promise<void> {
|
|
203
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
204
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { describe, expect } from "vitest";
|
|
2
|
-
import { defineConfig, pushConfig } from "../src/v1.js";
|
|
3
|
-
import {
|
|
4
|
-
bootstrapProject,
|
|
5
|
-
DEFAULT_REGION,
|
|
6
|
-
detectApiKeyScope,
|
|
7
|
-
e2eTest,
|
|
8
|
-
makeRealApi,
|
|
9
|
-
uniqueProjectName,
|
|
10
|
-
} from "./helpers.js";
|
|
11
|
-
|
|
12
|
-
describe("e2e — branch policy lifecycle", () => {
|
|
13
|
-
e2eTest(
|
|
14
|
-
"creates a dev branch and pushes main policy",
|
|
15
|
-
async ({ track }) => {
|
|
16
|
-
const scope = await detectApiKeyScope();
|
|
17
|
-
if (scope.kind !== "org-or-user") return;
|
|
18
|
-
const api = makeRealApi();
|
|
19
|
-
const projectId = await bootstrapProject(api, {
|
|
20
|
-
name: uniqueProjectName("life"),
|
|
21
|
-
region: DEFAULT_REGION,
|
|
22
|
-
});
|
|
23
|
-
track(projectId);
|
|
24
|
-
const main = (await api.listBranches(projectId)).find(
|
|
25
|
-
(b) => b.isDefault,
|
|
26
|
-
);
|
|
27
|
-
if (!main) throw new Error("missing default branch");
|
|
28
|
-
const config = defineConfig((b) =>
|
|
29
|
-
b.name === main.name
|
|
30
|
-
? { protected: true }
|
|
31
|
-
: { parent: main.name, ttl: "1h" },
|
|
32
|
-
);
|
|
33
|
-
const created = await api.createBranch(projectId, {
|
|
34
|
-
name: "dev",
|
|
35
|
-
parentId: main.id,
|
|
36
|
-
});
|
|
37
|
-
expect(created.branch.name).toBe("dev");
|
|
38
|
-
await pushConfig(config, {
|
|
39
|
-
api,
|
|
40
|
-
projectId,
|
|
41
|
-
branchId: main.id,
|
|
42
|
-
updateExisting: true,
|
|
43
|
-
});
|
|
44
|
-
const reread = (await api.listBranches(projectId)).find(
|
|
45
|
-
(b) => b.id === main.id,
|
|
46
|
-
);
|
|
47
|
-
expect(reread?.protected).toBe(true);
|
|
48
|
-
},
|
|
49
|
-
);
|
|
50
|
-
});
|
package/e2e/load-env.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// Loaded by vitest.e2e.config.ts `setupFiles`; not imported statically.
|
|
2
|
-
// fallow-ignore-file unused-file
|
|
3
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
-
import { dirname, resolve } from "node:path";
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Vitest setup file — loads `packages/config/.env` into `process.env` so e2e tests can
|
|
9
|
-
* read `NEON_API_KEY` (and friends). Node 22 has `--env-file` but it's per-process; doing
|
|
10
|
-
* it here keeps the test command short (`pnpm test:e2e`).
|
|
11
|
-
*
|
|
12
|
-
* Lines starting with `#` are treated as comments; everything else is parsed as
|
|
13
|
-
* `KEY=value` (no quoting / interpolation — we keep this minimal on purpose).
|
|
14
|
-
*/
|
|
15
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
16
|
-
const envPath = resolve(here, "..", ".env");
|
|
17
|
-
|
|
18
|
-
if (existsSync(envPath)) {
|
|
19
|
-
const raw = readFileSync(envPath, "utf-8");
|
|
20
|
-
for (const rawLine of raw.split("\n")) {
|
|
21
|
-
const line = rawLine.trim();
|
|
22
|
-
if (line === "" || line.startsWith("#")) continue;
|
|
23
|
-
const eq = line.indexOf("=");
|
|
24
|
-
if (eq <= 0) continue;
|
|
25
|
-
const key = line.slice(0, eq).trim();
|
|
26
|
-
const value = line.slice(eq + 1).trim();
|
|
27
|
-
if (process.env[key] === undefined) process.env[key] = value;
|
|
28
|
-
}
|
|
29
|
-
}
|
package/e2e/setup.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// Loaded by vitest.e2e.config.ts `setupFiles`; not imported statically.
|
|
2
|
-
// fallow-ignore-file unused-file
|
|
3
|
-
import { beforeAll } from "vitest";
|
|
4
|
-
import { detectApiKeyScope, sweepOrphans } from "./helpers.js";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Suite-level setup. Runs once before any e2e test:
|
|
8
|
-
* 1. Probes the configured API key to detect its scope. We do this here so a misconfigured
|
|
9
|
-
* environment fails fast with a clear message rather than surfacing as cryptic 403s
|
|
10
|
-
* inside individual tests.
|
|
11
|
-
* 2. When the key is org/user-scoped, sweep any orphaned `neon-ts-e2e-*` projects
|
|
12
|
-
* left over from a previous run.
|
|
13
|
-
*/
|
|
14
|
-
beforeAll(async () => {
|
|
15
|
-
const scope = await detectApiKeyScope();
|
|
16
|
-
if (scope.kind === "org-or-user") {
|
|
17
|
-
const { swept } = await sweepOrphans();
|
|
18
|
-
if (swept.length > 0) {
|
|
19
|
-
console.warn(
|
|
20
|
-
`[e2e setup] swept ${swept.length} orphaned project(s) from a previous run.`,
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
});
|