@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/LICENSE.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Apache License 2.0
|
|
2
|
+
|
|
3
|
+
Apache License
|
|
4
|
+
Version 2.0, January 2004
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
24
|
+
|
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
26
|
+
exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
29
|
+
including but not limited to software source code, documentation
|
|
30
|
+
source, and configuration files.
|
|
31
|
+
|
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
|
33
|
+
transformation or translation of a Source form, including but
|
|
34
|
+
not limited to compiled object code, generated documentation,
|
|
35
|
+
and conversions to other media types.
|
|
36
|
+
|
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
38
|
+
Object form, made available under the License, as indicated by a
|
|
39
|
+
copyright notice that is included in or attached to the work
|
|
40
|
+
(an example is provided in the Appendix below).
|
|
41
|
+
|
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
48
|
+
the Work and Derivative Works thereof.
|
|
49
|
+
|
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
|
51
|
+
the original version of the Work and any modifications or additions
|
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
63
|
+
|
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
66
|
+
subsequently incorporated within the Work.
|
|
67
|
+
|
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
80
|
+
where such license applies only to those patent claims licensable
|
|
81
|
+
by such Contributor that are necessarily infringed by their
|
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
84
|
+
institute patent litigation against any entity (including a
|
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
87
|
+
or contributory patent infringement, then any patent licenses
|
|
88
|
+
granted to You under this License for that Work shall terminate
|
|
89
|
+
as of the date such litigation is filed.
|
|
90
|
+
|
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
93
|
+
modifications, and in Source or Object form, provided that You
|
|
94
|
+
meet the following conditions:
|
|
95
|
+
|
|
96
|
+
(a) You must give any other recipients of the Work or
|
|
97
|
+
Derivative Works a copy of this License; and
|
|
98
|
+
|
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
|
100
|
+
stating that You changed the files; and
|
|
101
|
+
|
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
|
104
|
+
attribution notices from the Source form of the Work,
|
|
105
|
+
excluding those notices that do not pertain to any part of
|
|
106
|
+
the Derivative Works; and
|
|
107
|
+
|
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
|
110
|
+
include a readable copy of the attribution notices contained
|
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
|
113
|
+
of the following places: within a NOTICE text file distributed
|
|
114
|
+
as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
|
116
|
+
within a display generated by the Derivative Works, if and
|
|
117
|
+
wherever such third-party notices normally appear. The contents
|
|
118
|
+
of the NOTICE file are for informational purposes only and
|
|
119
|
+
do not modify the License. You may add Your own attribution
|
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
122
|
+
that such additional attribution notices cannot be construed
|
|
123
|
+
as modifying the License.
|
|
124
|
+
|
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
|
126
|
+
may provide additional or different license terms and conditions
|
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate license agreement you may have executed
|
|
138
|
+
with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for reasonable and customary use in describing the
|
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
170
|
+
or other liability obligations and/or rights consistent with this
|
|
171
|
+
License. However, in accepting such obligations, You may act only
|
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
176
|
+
of your accepting any such warranty or additional liability.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { buildFunctionBundle } from "./lib/function-bundle.js";
|
|
2
|
+
import { PullConfigOptions, PulledBranchConfig, PulledPreview, pullConfig } from "./lib/pull-config.js";
|
|
3
|
+
import { PushConfigOptions, PushConfirmContext, pushConfig } from "./lib/push-config.js";
|
|
4
|
+
import { ApplyOptions, ConfigOperationOptions, apply, inspect, plan } from "./lib/operations.js";
|
|
5
|
+
import { AppliedChange, Config, ConfigLoadError, ConfigValidationError, ConflictReport, ErrorCode, LoadConfigOptions, MissingContextError, NeonApi, PlatformError, PushAbortedError, PushConflictError, PushResult, createNeonApiFromOptions, createRealNeonApi, defineConfig, loadConfigFromFile } from "./v1.js";
|
|
6
|
+
export { AppliedChange, ApplyOptions, Config, ConfigLoadError, ConfigOperationOptions, ConfigValidationError, ConflictReport, ErrorCode, LoadConfigOptions, MissingContextError, NeonApi, PlatformError, PullConfigOptions, PulledBranchConfig, PulledPreview, PushAbortedError, PushConfigOptions, PushConfirmContext, PushConflictError, PushResult, apply, buildFunctionBundle, createNeonApiFromOptions, createRealNeonApi, defineConfig, inspect, loadConfigFromFile, plan, pullConfig, pushConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { buildFunctionBundle } from "./lib/function-bundle.js";
|
|
2
|
+
import { pullConfig } from "./lib/pull-config.js";
|
|
3
|
+
import { pushConfig } from "./lib/push-config.js";
|
|
4
|
+
import { apply, inspect, plan } from "./lib/operations.js";
|
|
5
|
+
import { ConfigLoadError, ConfigValidationError, ErrorCode, MissingContextError, PlatformError, PushAbortedError, PushConflictError, createNeonApiFromOptions, createRealNeonApi, defineConfig, loadConfigFromFile } from "./v1.js";
|
|
6
|
+
export { ConfigLoadError, ConfigValidationError, ErrorCode, MissingContextError, PlatformError, PushAbortedError, PushConflictError, apply, buildFunctionBundle, createNeonApiFromOptions, createRealNeonApi, defineConfig, inspect, loadConfigFromFile, plan, pullConfig, pushConfig };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ResolvedFunctionConfig } from "@neondatabase/config";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/function-bundle.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Build the deployable bundle (a ZIP archive of the esbuild-bundled source) for a function.
|
|
7
|
+
*
|
|
8
|
+
* This is the **imperative shell** step of function deploys, and the reason it lives in
|
|
9
|
+
* `@neondatabase/config-runtime` rather than `@neondatabase/config`: it pulls in `esbuild`
|
|
10
|
+
* (a native binary) and `fflate`. Keeping it out of `@neondatabase/config` means a `neon.ts`
|
|
11
|
+
* that only imports `defineConfig` never drags esbuild into the user's dependency tree or
|
|
12
|
+
* bundle. Deploy-side consumers (the neonctl CLI, CI) import this package and get esbuild as
|
|
13
|
+
* a normal, auto-installed dependency.
|
|
14
|
+
*
|
|
15
|
+
* esbuild and fflate are loaded with a dynamic `import()` (not a static top-level import) so
|
|
16
|
+
* that nothing in this package's static graph names esbuild until a deploy actually runs —
|
|
17
|
+
* a second layer of protection on top of the package split.
|
|
18
|
+
*
|
|
19
|
+
* Mirrors: `esbuild <source> --bundle --outfile=out.js --sourcemap --minify`, then zips the
|
|
20
|
+
* emitted files into the archive the Functions deploy endpoint expects.
|
|
21
|
+
*/
|
|
22
|
+
declare function buildFunctionBundle(fn: ResolvedFunctionConfig): Promise<Uint8Array>;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { buildFunctionBundle };
|
|
25
|
+
//# sourceMappingURL=function-bundle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-bundle.d.ts","names":[],"sources":["../../src/lib/function-bundle.ts"],"mappings":";;;;;;AAwBA;;;;;AAEU;;;;;;;;;;iBAFY,mBAAA,KACjB,yBACF,QAAQ"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ErrorCode, PlatformError } from "@neondatabase/config";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
//#region src/lib/function-bundle.ts
|
|
4
|
+
/**
|
|
5
|
+
* Build the deployable bundle (a ZIP archive of the esbuild-bundled source) for a function.
|
|
6
|
+
*
|
|
7
|
+
* This is the **imperative shell** step of function deploys, and the reason it lives in
|
|
8
|
+
* `@neondatabase/config-runtime` rather than `@neondatabase/config`: it pulls in `esbuild`
|
|
9
|
+
* (a native binary) and `fflate`. Keeping it out of `@neondatabase/config` means a `neon.ts`
|
|
10
|
+
* that only imports `defineConfig` never drags esbuild into the user's dependency tree or
|
|
11
|
+
* bundle. Deploy-side consumers (the neonctl CLI, CI) import this package and get esbuild as
|
|
12
|
+
* a normal, auto-installed dependency.
|
|
13
|
+
*
|
|
14
|
+
* esbuild and fflate are loaded with a dynamic `import()` (not a static top-level import) so
|
|
15
|
+
* that nothing in this package's static graph names esbuild until a deploy actually runs —
|
|
16
|
+
* a second layer of protection on top of the package split.
|
|
17
|
+
*
|
|
18
|
+
* Mirrors: `esbuild <source> --bundle --outfile=out.js --sourcemap --minify`, then zips the
|
|
19
|
+
* emitted files into the archive the Functions deploy endpoint expects.
|
|
20
|
+
*/
|
|
21
|
+
async function buildFunctionBundle(fn) {
|
|
22
|
+
const esbuild = await loadEsbuild();
|
|
23
|
+
let result;
|
|
24
|
+
try {
|
|
25
|
+
result = await esbuild.build({
|
|
26
|
+
entryPoints: [fn.source],
|
|
27
|
+
bundle: true,
|
|
28
|
+
write: false,
|
|
29
|
+
outfile: "out.js",
|
|
30
|
+
sourcemap: true,
|
|
31
|
+
minify: true,
|
|
32
|
+
format: "esm",
|
|
33
|
+
platform: "node",
|
|
34
|
+
packages: "external",
|
|
35
|
+
logLevel: "silent"
|
|
36
|
+
});
|
|
37
|
+
} catch (cause) {
|
|
38
|
+
throw new PlatformError(ErrorCode.InvalidConfig, [`Failed to bundle function "${fn.slug}" from ${fn.source}.`, cause?.message ?? String(cause)].join(" "), { cause });
|
|
39
|
+
}
|
|
40
|
+
const entries = {};
|
|
41
|
+
for (const file of result.outputFiles ?? []) entries[basename(file.path)] = file.contents;
|
|
42
|
+
return zipBundle(entries);
|
|
43
|
+
}
|
|
44
|
+
async function zipBundle(entries) {
|
|
45
|
+
const { zipSync } = await loadFflate();
|
|
46
|
+
return zipSync(entries, { level: 6 });
|
|
47
|
+
}
|
|
48
|
+
async function loadEsbuild() {
|
|
49
|
+
try {
|
|
50
|
+
return await import("esbuild");
|
|
51
|
+
} catch (cause) {
|
|
52
|
+
throw new PlatformError(ErrorCode.InvalidConfig, ["Deploying Neon Functions requires `esbuild`, which could not be loaded.", "It is a dependency of @neondatabase/config-runtime — reinstall your dependencies (`pnpm install` / `npm install`)."].join(" "), { cause });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async function loadFflate() {
|
|
56
|
+
try {
|
|
57
|
+
return await import("fflate");
|
|
58
|
+
} catch (cause) {
|
|
59
|
+
throw new PlatformError(ErrorCode.InvalidConfig, ["Deploying Neon Functions requires `fflate`, which could not be loaded.", "It is a dependency of @neondatabase/config-runtime — reinstall your dependencies (`pnpm install` / `npm install`)."].join(" "), { cause });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { buildFunctionBundle };
|
|
64
|
+
|
|
65
|
+
//# sourceMappingURL=function-bundle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-bundle.js","names":[],"sources":["../../src/lib/function-bundle.ts"],"sourcesContent":["import { basename } from \"node:path\";\nimport {\n\tErrorCode,\n\tPlatformError,\n\ttype ResolvedFunctionConfig,\n} from \"@neondatabase/config\";\n\n/**\n * Build the deployable bundle (a ZIP archive of the esbuild-bundled source) for a function.\n *\n * This is the **imperative shell** step of function deploys, and the reason it lives in\n * `@neondatabase/config-runtime` rather than `@neondatabase/config`: it pulls in `esbuild`\n * (a native binary) and `fflate`. Keeping it out of `@neondatabase/config` means a `neon.ts`\n * that only imports `defineConfig` never drags esbuild into the user's dependency tree or\n * bundle. Deploy-side consumers (the neonctl CLI, CI) import this package and get esbuild as\n * a normal, auto-installed dependency.\n *\n * esbuild and fflate are loaded with a dynamic `import()` (not a static top-level import) so\n * that nothing in this package's static graph names esbuild until a deploy actually runs —\n * a second layer of protection on top of the package split.\n *\n * Mirrors: `esbuild <source> --bundle --outfile=out.js --sourcemap --minify`, then zips the\n * emitted files into the archive the Functions deploy endpoint expects.\n */\nexport async function buildFunctionBundle(\n\tfn: ResolvedFunctionConfig,\n): Promise<Uint8Array> {\n\tconst esbuild = await loadEsbuild();\n\n\tlet result: Awaited<ReturnType<typeof esbuild.build>>;\n\ttry {\n\t\tresult = await esbuild.build({\n\t\t\tentryPoints: [fn.source],\n\t\t\tbundle: true,\n\t\t\twrite: false,\n\t\t\t// Set an explicit outfile so the emitted files are named `out.js` / `out.js.map`\n\t\t\t// (with `write: false` and no outfile, esbuild labels the buffer `<stdout>`).\n\t\t\toutfile: \"out.js\",\n\t\t\tsourcemap: true,\n\t\t\tminify: true,\n\t\t\tformat: \"esm\",\n\t\t\tplatform: \"node\",\n\t\t\t// The Functions runtime provides Node built-ins; don't try to bundle them.\n\t\t\tpackages: \"external\",\n\t\t\tlogLevel: \"silent\",\n\t\t});\n\t} catch (cause) {\n\t\tthrow new PlatformError(\n\t\t\tErrorCode.InvalidConfig,\n\t\t\t[\n\t\t\t\t`Failed to bundle function \"${fn.slug}\" from ${fn.source}.`,\n\t\t\t\t(cause as Error)?.message ?? String(cause),\n\t\t\t].join(\" \"),\n\t\t\t{ cause },\n\t\t);\n\t}\n\n\tconst entries: Record<string, Uint8Array> = {};\n\t// `write: false` guarantees `outputFiles`, but the type is optional — guard for safety.\n\tfor (const file of result.outputFiles ?? []) {\n\t\t// esbuild returns absolute output paths; archive them under their basename\n\t\t// (`out.js`, `out.js.map`) so the bundle layout is stable regardless of cwd.\n\t\tentries[basename(file.path)] = file.contents;\n\t}\n\n\treturn zipBundle(entries);\n}\n\nasync function zipBundle(\n\tentries: Record<string, Uint8Array>,\n): Promise<Uint8Array> {\n\tconst { zipSync } = await loadFflate();\n\treturn zipSync(entries, { level: 6 });\n}\n\nasync function loadEsbuild(): Promise<typeof import(\"esbuild\")> {\n\ttry {\n\t\treturn await import(\"esbuild\");\n\t} catch (cause) {\n\t\tthrow new PlatformError(\n\t\t\tErrorCode.InvalidConfig,\n\t\t\t[\n\t\t\t\t\"Deploying Neon Functions requires `esbuild`, which could not be loaded.\",\n\t\t\t\t\"It is a dependency of @neondatabase/config-runtime — reinstall your dependencies (`pnpm install` / `npm install`).\",\n\t\t\t].join(\" \"),\n\t\t\t{ cause },\n\t\t);\n\t}\n}\n\nasync function loadFflate(): Promise<typeof import(\"fflate\")> {\n\ttry {\n\t\treturn await import(\"fflate\");\n\t} catch (cause) {\n\t\tthrow new PlatformError(\n\t\t\tErrorCode.InvalidConfig,\n\t\t\t[\n\t\t\t\t\"Deploying Neon Functions requires `fflate`, which could not be loaded.\",\n\t\t\t\t\"It is a dependency of @neondatabase/config-runtime — reinstall your dependencies (`pnpm install` / `npm install`).\",\n\t\t\t].join(\" \"),\n\t\t\t{ cause },\n\t\t);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAwBA,eAAsB,oBACrB,IACsB;CACtB,MAAM,UAAU,MAAM,YAAY;CAElC,IAAI;CACJ,IAAI;EACH,SAAS,MAAM,QAAQ,MAAM;GAC5B,aAAa,CAAC,GAAG,MAAM;GACvB,QAAQ;GACR,OAAO;GAGP,SAAS;GACT,WAAW;GACX,QAAQ;GACR,QAAQ;GACR,UAAU;GAEV,UAAU;GACV,UAAU;EACX,CAAC;CACF,SAAS,OAAO;EACf,MAAM,IAAI,cACT,UAAU,eACV,CACC,8BAA8B,GAAG,KAAK,SAAS,GAAG,OAAO,IACxD,OAAiB,WAAW,OAAO,KAAK,CAC1C,EAAE,KAAK,GAAG,GACV,EAAE,MAAM,CACT;CACD;CAEA,MAAM,UAAsC,CAAC;CAE7C,KAAK,MAAM,QAAQ,OAAO,eAAe,CAAC,GAGzC,QAAQ,SAAS,KAAK,IAAI,KAAK,KAAK;CAGrC,OAAO,UAAU,OAAO;AACzB;AAEA,eAAe,UACd,SACsB;CACtB,MAAM,EAAE,YAAY,MAAM,WAAW;CACrC,OAAO,QAAQ,SAAS,EAAE,OAAO,EAAE,CAAC;AACrC;AAEA,eAAe,cAAiD;CAC/D,IAAI;EACH,OAAO,MAAM,OAAO;CACrB,SAAS,OAAO;EACf,MAAM,IAAI,cACT,UAAU,eACV,CACC,2EACA,oHACD,EAAE,KAAK,GAAG,GACV,EAAE,MAAM,CACT;CACD;AACD;AAEA,eAAe,aAA+C;CAC7D,IAAI;EACH,OAAO,MAAM,OAAO;CACrB,SAAS,OAAO;EACf,MAAM,IAAI,cACT,UAAU,eACV,CACC,0EACA,oHACD,EAAE,KAAK,GAAG,GACV,EAAE,MAAM,CACT;CACD;AACD"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { PulledBranchConfig } from "./pull-config.js";
|
|
2
|
+
import { PushConfigOptions } from "./push-config.js";
|
|
3
|
+
import { Config, PushResult } from "@neondatabase/config";
|
|
4
|
+
|
|
5
|
+
//#region src/lib/operations.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Where to run the operation and how to authenticate. Filesystem- and env-agnostic: the
|
|
9
|
+
* `projectId` and `branchId` are always passed explicitly by the caller (e.g. neonctl
|
|
10
|
+
* resolves them from `.neon` / `NEON_*` and forwards them here).
|
|
11
|
+
*/
|
|
12
|
+
interface ConfigOperationOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Neon project id. **Required** — the management API addresses branches through their
|
|
15
|
+
* project, so operations cannot run without it.
|
|
16
|
+
*/
|
|
17
|
+
projectId: string;
|
|
18
|
+
/**
|
|
19
|
+
* Neon branch id (`br-…`). **Required.** Must already exist on the project; resolve
|
|
20
|
+
* branch names to ids before calling.
|
|
21
|
+
*/
|
|
22
|
+
branchId: string;
|
|
23
|
+
/** Neon API key. Falls back to `NEON_API_KEY` / neonctl credentials. */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
/** Inject a custom NeonApi adapter (primarily for tests). */
|
|
26
|
+
api?: PushConfigOptions["api"];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Options accepted by {@link apply} on top of {@link ConfigOperationOptions}.
|
|
30
|
+
*/
|
|
31
|
+
interface ApplyOptions extends ConfigOperationOptions {
|
|
32
|
+
/**
|
|
33
|
+
* Auto-confirm overriding existing remote settings (TTL, `protected`, compute
|
|
34
|
+
* settings) on the selected branch. Without it, drift is reported as a conflict.
|
|
35
|
+
*/
|
|
36
|
+
updateExisting?: boolean;
|
|
37
|
+
/** Auto-confirm applying to a branch marked `protected` on Neon. */
|
|
38
|
+
allowProtectedBranch?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Read a branch's live Neon state as a plain object (project + branch metadata and the
|
|
42
|
+
* reverse-engineered `BranchConfig`). Network read only — never mutates.
|
|
43
|
+
*
|
|
44
|
+
* `projectId` and `branchId` are **required** (both in `options`).
|
|
45
|
+
*/
|
|
46
|
+
declare function inspect(options: ConfigOperationOptions): Promise<PulledBranchConfig>;
|
|
47
|
+
/**
|
|
48
|
+
* Compute what {@link apply} would do for the given branch without mutating anything
|
|
49
|
+
* (dry-run plan). Returns the full {@link PushResult} with the planned changes in
|
|
50
|
+
* `applied` and any blocking drift in `conflicts` — the Neon equivalent of
|
|
51
|
+
* `terraform plan`.
|
|
52
|
+
*
|
|
53
|
+
* `projectId` and `branchId` are **required** (both in `options`).
|
|
54
|
+
*/
|
|
55
|
+
declare function plan(config: Config, options: ConfigOperationOptions): Promise<PushResult>;
|
|
56
|
+
/**
|
|
57
|
+
* Apply a `neon.ts` policy to the given Neon branch and return the {@link PushResult}
|
|
58
|
+
* describing what changed — the Neon equivalent of `terraform apply`.
|
|
59
|
+
*
|
|
60
|
+
* `projectId` and `branchId` are **required** (both in `options`). Pass `updateExisting`
|
|
61
|
+
* to auto-confirm overriding existing remote settings and `allowProtectedBranch` to
|
|
62
|
+
* auto-confirm applying to a protected branch; otherwise drift is reported as a
|
|
63
|
+
* `PushConflictError`.
|
|
64
|
+
*
|
|
65
|
+
* Never creates projects or branches — both must already exist.
|
|
66
|
+
*/
|
|
67
|
+
declare function apply(config: Config, options: ApplyOptions): Promise<PushResult>;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { ApplyOptions, ConfigOperationOptions, apply, inspect, plan };
|
|
70
|
+
//# sourceMappingURL=operations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.d.ts","names":[],"sources":["../../src/lib/operations.ts"],"mappings":";;;;;;;;AASA;AAoBA;AAgBA;AAA6B,UApCZ,sBAAA,CAoCY;;;;AAEnB;EAiBY,SAAI,EAAA,MAAA;EAAA;;;;UAGvB,EAAA,MAAA;EAAO;EAuBY,MAAA,CAAA,EAAK,MAAA;EAAA;KAClB,CAAA,EApEF,iBAoEE,CAAA,KAAA,CAAA;;;;AAEC;UAhEO,YAAA,SAAqB;;;;;;;;;;;;;;;iBAgBhB,OAAA,UACZ,yBACP,QAAQ;;;;;;;;;iBAiBW,IAAA,SACb,iBACC,yBACP,QAAQ;;;;;;;;;;;;iBAuBW,KAAA,SACb,iBACC,eACP,QAAQ"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { pullConfig } from "./pull-config.js";
|
|
2
|
+
import { pushConfig } from "./push-config.js";
|
|
3
|
+
//#region src/lib/operations.ts
|
|
4
|
+
/**
|
|
5
|
+
* Read a branch's live Neon state as a plain object (project + branch metadata and the
|
|
6
|
+
* reverse-engineered `BranchConfig`). Network read only — never mutates.
|
|
7
|
+
*
|
|
8
|
+
* `projectId` and `branchId` are **required** (both in `options`).
|
|
9
|
+
*/
|
|
10
|
+
async function inspect(options) {
|
|
11
|
+
return pullConfig({
|
|
12
|
+
projectId: options.projectId,
|
|
13
|
+
branchId: options.branchId,
|
|
14
|
+
...options.api ? { api: options.api } : {},
|
|
15
|
+
...options.apiKey ? { apiKey: options.apiKey } : {}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Compute what {@link apply} would do for the given branch without mutating anything
|
|
20
|
+
* (dry-run plan). Returns the full {@link PushResult} with the planned changes in
|
|
21
|
+
* `applied` and any blocking drift in `conflicts` — the Neon equivalent of
|
|
22
|
+
* `terraform plan`.
|
|
23
|
+
*
|
|
24
|
+
* `projectId` and `branchId` are **required** (both in `options`).
|
|
25
|
+
*/
|
|
26
|
+
async function plan(config, options) {
|
|
27
|
+
return pushConfig(config, {
|
|
28
|
+
projectId: options.projectId,
|
|
29
|
+
branchId: options.branchId,
|
|
30
|
+
dryRun: true,
|
|
31
|
+
updateExisting: true,
|
|
32
|
+
...options.api ? { api: options.api } : {},
|
|
33
|
+
...options.apiKey ? { apiKey: options.apiKey } : {}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Apply a `neon.ts` policy to the given Neon branch and return the {@link PushResult}
|
|
38
|
+
* describing what changed — the Neon equivalent of `terraform apply`.
|
|
39
|
+
*
|
|
40
|
+
* `projectId` and `branchId` are **required** (both in `options`). Pass `updateExisting`
|
|
41
|
+
* to auto-confirm overriding existing remote settings and `allowProtectedBranch` to
|
|
42
|
+
* auto-confirm applying to a protected branch; otherwise drift is reported as a
|
|
43
|
+
* `PushConflictError`.
|
|
44
|
+
*
|
|
45
|
+
* Never creates projects or branches — both must already exist.
|
|
46
|
+
*/
|
|
47
|
+
async function apply(config, options) {
|
|
48
|
+
return pushConfig(config, {
|
|
49
|
+
projectId: options.projectId,
|
|
50
|
+
branchId: options.branchId,
|
|
51
|
+
...options.api ? { api: options.api } : {},
|
|
52
|
+
...options.apiKey ? { apiKey: options.apiKey } : {},
|
|
53
|
+
...options.updateExisting ? { updateExisting: true } : {},
|
|
54
|
+
...options.allowProtectedBranch ? { allowProtectedBranch: true } : {}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { apply, inspect, plan };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=operations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operations.js","names":[],"sources":["../../src/lib/operations.ts"],"sourcesContent":["import type { Config, PushResult } from \"@neondatabase/config\";\nimport { type PulledBranchConfig, pullConfig } from \"./pull-config.js\";\nimport { type PushConfigOptions, pushConfig } from \"./push-config.js\";\n\n/**\n * Where to run the operation and how to authenticate. Filesystem- and env-agnostic: the\n * `projectId` and `branchId` are always passed explicitly by the caller (e.g. neonctl\n * resolves them from `.neon` / `NEON_*` and forwards them here).\n */\nexport interface ConfigOperationOptions {\n\t/**\n\t * Neon project id. **Required** — the management API addresses branches through their\n\t * project, so operations cannot run without it.\n\t */\n\tprojectId: string;\n\t/**\n\t * Neon branch id (`br-…`). **Required.** Must already exist on the project; resolve\n\t * branch names to ids before calling.\n\t */\n\tbranchId: string;\n\t/** Neon API key. Falls back to `NEON_API_KEY` / neonctl credentials. */\n\tapiKey?: string;\n\t/** Inject a custom NeonApi adapter (primarily for tests). */\n\tapi?: PushConfigOptions[\"api\"];\n}\n\n/**\n * Options accepted by {@link apply} on top of {@link ConfigOperationOptions}.\n */\nexport interface ApplyOptions extends ConfigOperationOptions {\n\t/**\n\t * Auto-confirm overriding existing remote settings (TTL, `protected`, compute\n\t * settings) on the selected branch. Without it, drift is reported as a conflict.\n\t */\n\tupdateExisting?: boolean;\n\t/** Auto-confirm applying to a branch marked `protected` on Neon. */\n\tallowProtectedBranch?: boolean;\n}\n\n/**\n * Read a branch's live Neon state as a plain object (project + branch metadata and the\n * reverse-engineered `BranchConfig`). Network read only — never mutates.\n *\n * `projectId` and `branchId` are **required** (both in `options`).\n */\nexport async function inspect(\n\toptions: ConfigOperationOptions,\n): Promise<PulledBranchConfig> {\n\treturn pullConfig({\n\t\tprojectId: options.projectId,\n\t\tbranchId: options.branchId,\n\t\t...(options.api ? { api: options.api } : {}),\n\t\t...(options.apiKey ? { apiKey: options.apiKey } : {}),\n\t});\n}\n\n/**\n * Compute what {@link apply} would do for the given branch without mutating anything\n * (dry-run plan). Returns the full {@link PushResult} with the planned changes in\n * `applied` and any blocking drift in `conflicts` — the Neon equivalent of\n * `terraform plan`.\n *\n * `projectId` and `branchId` are **required** (both in `options`).\n */\nexport async function plan(\n\tconfig: Config,\n\toptions: ConfigOperationOptions,\n): Promise<PushResult> {\n\treturn pushConfig(config, {\n\t\tprojectId: options.projectId,\n\t\tbranchId: options.branchId,\n\t\tdryRun: true,\n\t\t// Surface the full would-apply list as plan steps without mutating anything.\n\t\tupdateExisting: true,\n\t\t...(options.api ? { api: options.api } : {}),\n\t\t...(options.apiKey ? { apiKey: options.apiKey } : {}),\n\t});\n}\n\n/**\n * Apply a `neon.ts` policy to the given Neon branch and return the {@link PushResult}\n * describing what changed — the Neon equivalent of `terraform apply`.\n *\n * `projectId` and `branchId` are **required** (both in `options`). Pass `updateExisting`\n * to auto-confirm overriding existing remote settings and `allowProtectedBranch` to\n * auto-confirm applying to a protected branch; otherwise drift is reported as a\n * `PushConflictError`.\n *\n * Never creates projects or branches — both must already exist.\n */\nexport async function apply(\n\tconfig: Config,\n\toptions: ApplyOptions,\n): Promise<PushResult> {\n\treturn pushConfig(config, {\n\t\tprojectId: options.projectId,\n\t\tbranchId: options.branchId,\n\t\t...(options.api ? { api: options.api } : {}),\n\t\t...(options.apiKey ? { apiKey: options.apiKey } : {}),\n\t\t...(options.updateExisting ? { updateExisting: true } : {}),\n\t\t...(options.allowProtectedBranch ? { allowProtectedBranch: true } : {}),\n\t});\n}\n"],"mappings":";;;;;;;;;AA6CA,eAAsB,QACrB,SAC8B;CAC9B,OAAO,WAAW;EACjB,WAAW,QAAQ;EACnB,UAAU,QAAQ;EAClB,GAAI,QAAQ,MAAM,EAAE,KAAK,QAAQ,IAAI,IAAI,CAAC;EAC1C,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC;CACpD,CAAC;AACF;;;;;;;;;AAUA,eAAsB,KACrB,QACA,SACsB;CACtB,OAAO,WAAW,QAAQ;EACzB,WAAW,QAAQ;EACnB,UAAU,QAAQ;EAClB,QAAQ;EAER,gBAAgB;EAChB,GAAI,QAAQ,MAAM,EAAE,KAAK,QAAQ,IAAI,IAAI,CAAC;EAC1C,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC;CACpD,CAAC;AACF;;;;;;;;;;;;AAaA,eAAsB,MACrB,QACA,SACsB;CACtB,OAAO,WAAW,QAAQ;EACzB,WAAW,QAAQ;EACnB,UAAU,QAAQ;EAClB,GAAI,QAAQ,MAAM,EAAE,KAAK,QAAQ,IAAI,IAAI,CAAC;EAC1C,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAO,IAAI,CAAC;EACnD,GAAI,QAAQ,iBAAiB,EAAE,gBAAgB,KAAK,IAAI,CAAC;EACzD,GAAI,QAAQ,uBAAuB,EAAE,sBAAsB,KAAK,IAAI,CAAC;CACtE,CAAC;AACF"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { BranchConfig, BucketConfig, NeonApi, NeonBranchSnapshot, NeonBucketSnapshot, NeonEndpointSnapshot, NeonFunctionSnapshot, NeonProjectSnapshot } from "@neondatabase/config";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/pull-config.d.ts
|
|
4
|
+
interface PullConfigOptions {
|
|
5
|
+
/** Neon project id (`<project>`). Required — the API addresses branches by project. */
|
|
6
|
+
projectId: string;
|
|
7
|
+
/** Neon branch id (`br-…`). Required. Resolve names to ids before calling. */
|
|
8
|
+
branchId: string;
|
|
9
|
+
/** Neon API key. Falls back to `NEON_API_KEY` / neonctl credentials. */
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
/** Inject a custom NeonApi adapter (primarily for tests). */
|
|
12
|
+
api?: NeonApi;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Live Preview-feature state read back from a branch. Surfaced alongside `config` rather
|
|
16
|
+
* than inside it because functions cannot round-trip: the remote only knows the deployed
|
|
17
|
+
* bundle, not the local `source` path a {@link FunctionConfig} requires, so a pulled
|
|
18
|
+
* function is reported as `{ slug, name }` (no `source`).
|
|
19
|
+
*/
|
|
20
|
+
interface PulledPreview {
|
|
21
|
+
buckets: BucketConfig[];
|
|
22
|
+
functions: Array<{
|
|
23
|
+
slug: string;
|
|
24
|
+
name: string;
|
|
25
|
+
}>;
|
|
26
|
+
aiGatewayEnabled: boolean;
|
|
27
|
+
}
|
|
28
|
+
interface PulledBranchConfig {
|
|
29
|
+
project: {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
region: string;
|
|
33
|
+
pgVersion: number;
|
|
34
|
+
orgId?: string;
|
|
35
|
+
};
|
|
36
|
+
branch: {
|
|
37
|
+
id: string;
|
|
38
|
+
name: string;
|
|
39
|
+
parent?: string;
|
|
40
|
+
isDefault: boolean;
|
|
41
|
+
protected: boolean;
|
|
42
|
+
expiresAt?: string;
|
|
43
|
+
};
|
|
44
|
+
config: BranchConfig;
|
|
45
|
+
/**
|
|
46
|
+
* Live Preview-feature state, when the branch has any buckets/functions or an enabled
|
|
47
|
+
* AI Gateway. Omitted entirely when there is nothing to report.
|
|
48
|
+
*/
|
|
49
|
+
preview?: PulledPreview;
|
|
50
|
+
}
|
|
51
|
+
declare function pullConfig(options: PullConfigOptions): Promise<PulledBranchConfig>;
|
|
52
|
+
declare function buildPulledBranchConfig(project: NeonProjectSnapshot, branch: NeonBranchSnapshot, branches: NeonBranchSnapshot[], endpoint: NeonEndpointSnapshot | undefined, previewState?: {
|
|
53
|
+
buckets: NeonBucketSnapshot[];
|
|
54
|
+
functions: NeonFunctionSnapshot[];
|
|
55
|
+
aiGatewayEnabled: boolean;
|
|
56
|
+
}): PulledBranchConfig;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { PullConfigOptions, PulledBranchConfig, PulledPreview, buildPulledBranchConfig, pullConfig };
|
|
59
|
+
//# sourceMappingURL=pull-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pull-config.d.ts","names":[],"sources":["../../src/lib/pull-config.ts"],"mappings":";;;UAeiB,iBAAA;;EAAA,SAAA,EAAA,MAAA;EAiBA;EAAa,QAAA,EAAA,MAAA;;QAElB,CAAA,EAAA,MAAA;EAAK;EAIA,GAAA,CAAA,EAfV,OAeU;;;;AAqBO;AAGxB;;;AAEW,UAhCM,aAAA,CAgCN;SAAR,EA/BO,YA+BP,EAAA;EAAO,SAAA,EA9BE,KA8BF,CAAA;IA8BM,IAAA,EAAA,MAAA;IAAuB,IAAA,EAAA,MAAA;;kBAE9B,EAAA,OAAA;;AAEE,UA5DM,kBAAA,CA4DN;SAEA,EAAA;IACE,EAAA,EAAA,MAAA;IAGV,IAAA,EAAA,MAAA;IAAkB,MAAA,EAAA,MAAA;;;;;;;;;;;;UAlDZ;;;;;YAKE;;iBAGW,UAAA,UACZ,oBACP,QAAQ;iBA8BK,uBAAA,UACN,6BACD,8BACE,gCACA;WAEA;aACE;;IAGV"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ErrorCode, PlatformError, createNeonApiFromOptions } from "@neondatabase/config";
|
|
2
|
+
//#region src/lib/pull-config.ts
|
|
3
|
+
async function pullConfig(options) {
|
|
4
|
+
const api = options.api ?? createApiFromOptions(options);
|
|
5
|
+
const projectId = options.projectId;
|
|
6
|
+
const project = await api.getProject(projectId);
|
|
7
|
+
const [branches, endpoints] = await Promise.all([api.listBranches(projectId), api.listEndpoints(projectId)]);
|
|
8
|
+
const branch = resolveBranch(options.branchId, branches);
|
|
9
|
+
const endpoint = endpoints.find((ep) => ep.type === "read_write" && ep.branchId === branch.id);
|
|
10
|
+
const [buckets, functions, aiGatewayEnabled] = await Promise.all([
|
|
11
|
+
api.listBranchBuckets(projectId, branch.id),
|
|
12
|
+
api.listBranchFunctions(projectId, branch.id),
|
|
13
|
+
api.getAiGatewayEnabled(projectId, branch.id)
|
|
14
|
+
]);
|
|
15
|
+
return buildPulledBranchConfig(project, branch, branches, endpoint, {
|
|
16
|
+
buckets,
|
|
17
|
+
functions,
|
|
18
|
+
aiGatewayEnabled
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function createApiFromOptions(options) {
|
|
22
|
+
return createNeonApiFromOptions("pullConfig", { ...options.apiKey ? { apiKey: options.apiKey } : {} });
|
|
23
|
+
}
|
|
24
|
+
function buildPulledBranchConfig(project, branch, branches, endpoint, previewState) {
|
|
25
|
+
const parent = branch.parentId ? branches.find((b) => b.id === branch.parentId) : void 0;
|
|
26
|
+
const config = {};
|
|
27
|
+
if (parent) config.parent = parent.name;
|
|
28
|
+
if (branch.expiresAt) config.ttl = branch.expiresAt;
|
|
29
|
+
if (branch.protected) config.protected = true;
|
|
30
|
+
if (endpoint) {
|
|
31
|
+
const compute = endpointToComputeSettings(endpoint, project);
|
|
32
|
+
if (compute) config.postgres = { computeSettings: compute };
|
|
33
|
+
}
|
|
34
|
+
const result = {
|
|
35
|
+
project: {
|
|
36
|
+
id: project.id,
|
|
37
|
+
name: project.name,
|
|
38
|
+
region: project.regionId,
|
|
39
|
+
pgVersion: project.pgVersion,
|
|
40
|
+
...project.orgId ? { orgId: project.orgId } : {}
|
|
41
|
+
},
|
|
42
|
+
branch: {
|
|
43
|
+
id: branch.id,
|
|
44
|
+
name: branch.name,
|
|
45
|
+
...parent ? { parent: parent.name } : {},
|
|
46
|
+
isDefault: branch.isDefault,
|
|
47
|
+
protected: branch.protected,
|
|
48
|
+
...branch.expiresAt ? { expiresAt: branch.expiresAt } : {}
|
|
49
|
+
},
|
|
50
|
+
config
|
|
51
|
+
};
|
|
52
|
+
const preview = previewState ? buildPulledPreview(previewState) : void 0;
|
|
53
|
+
if (preview) result.preview = preview;
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Reverse-engineer the {@link PulledPreview} from remote snapshots. Returns `undefined` when
|
|
58
|
+
* the branch has no Preview features so the field can be omitted entirely.
|
|
59
|
+
*/
|
|
60
|
+
function buildPulledPreview(state) {
|
|
61
|
+
if (state.buckets.length === 0 && state.functions.length === 0 && !state.aiGatewayEnabled) return;
|
|
62
|
+
return {
|
|
63
|
+
buckets: state.buckets.map((b) => ({
|
|
64
|
+
name: b.name,
|
|
65
|
+
access: b.accessLevel
|
|
66
|
+
})),
|
|
67
|
+
functions: state.functions.map((f) => ({
|
|
68
|
+
slug: f.slug,
|
|
69
|
+
name: f.name
|
|
70
|
+
})),
|
|
71
|
+
aiGatewayEnabled: state.aiGatewayEnabled
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function resolveBranch(branchId, branches) {
|
|
75
|
+
const match = branches.find((b) => b.id === branchId);
|
|
76
|
+
if (match) return match;
|
|
77
|
+
throw new PlatformError(ErrorCode.BranchNotFound, [`pullConfig: branch id ${JSON.stringify(branchId)} not found on project.`, `Available branches: ${branches.map((b) => `${b.name} (${b.id})`).join(", ") || "(none)"}.`].join(" "), { details: {
|
|
78
|
+
branchId,
|
|
79
|
+
available: branches.map((b) => b.id)
|
|
80
|
+
} });
|
|
81
|
+
}
|
|
82
|
+
function endpointToComputeSettings(endpoint, project) {
|
|
83
|
+
const defaults = project.defaultEndpointSettings;
|
|
84
|
+
const out = {};
|
|
85
|
+
if (endpoint.autoscalingLimitMinCu !== void 0 && endpoint.autoscalingLimitMinCu !== defaults?.autoscalingLimitMinCu) out.autoscalingLimitMinCu = endpoint.autoscalingLimitMinCu;
|
|
86
|
+
if (endpoint.autoscalingLimitMaxCu !== void 0 && endpoint.autoscalingLimitMaxCu !== defaults?.autoscalingLimitMaxCu) out.autoscalingLimitMaxCu = endpoint.autoscalingLimitMaxCu;
|
|
87
|
+
if (endpoint.suspendTimeout !== void 0 && endpoint.suspendTimeout !== defaults?.suspendTimeout) out.suspendTimeout = endpoint.suspendTimeout;
|
|
88
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
export { buildPulledBranchConfig, pullConfig };
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=pull-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pull-config.js","names":[],"sources":["../../src/lib/pull-config.ts"],"sourcesContent":["import {\n\ttype BranchConfig,\n\ttype BucketConfig,\n\ttype ComputeSettings,\n\tcreateNeonApiFromOptions,\n\tErrorCode,\n\ttype NeonApi,\n\ttype NeonBranchSnapshot,\n\ttype NeonBucketSnapshot,\n\ttype NeonEndpointSnapshot,\n\ttype NeonFunctionSnapshot,\n\ttype NeonProjectSnapshot,\n\tPlatformError,\n} from \"@neondatabase/config\";\n\nexport interface PullConfigOptions {\n\t/** Neon project id (`<project>`). Required — the API addresses branches by project. */\n\tprojectId: string;\n\t/** Neon branch id (`br-…`). Required. Resolve names to ids before calling. */\n\tbranchId: string;\n\t/** Neon API key. Falls back to `NEON_API_KEY` / neonctl credentials. */\n\tapiKey?: string;\n\t/** Inject a custom NeonApi adapter (primarily for tests). */\n\tapi?: NeonApi;\n}\n\n/**\n * Live Preview-feature state read back from a branch. Surfaced alongside `config` rather\n * than inside it because functions cannot round-trip: the remote only knows the deployed\n * bundle, not the local `source` path a {@link FunctionConfig} requires, so a pulled\n * function is reported as `{ slug, name }` (no `source`).\n */\nexport interface PulledPreview {\n\tbuckets: BucketConfig[];\n\tfunctions: Array<{ slug: string; name: string }>;\n\taiGatewayEnabled: boolean;\n}\n\nexport interface PulledBranchConfig {\n\tproject: {\n\t\tid: string;\n\t\tname: string;\n\t\tregion: string;\n\t\tpgVersion: number;\n\t\torgId?: string;\n\t};\n\tbranch: {\n\t\tid: string;\n\t\tname: string;\n\t\tparent?: string;\n\t\tisDefault: boolean;\n\t\tprotected: boolean;\n\t\texpiresAt?: string;\n\t};\n\tconfig: BranchConfig;\n\t/**\n\t * Live Preview-feature state, when the branch has any buckets/functions or an enabled\n\t * AI Gateway. Omitted entirely when there is nothing to report.\n\t */\n\tpreview?: PulledPreview;\n}\n\nexport async function pullConfig(\n\toptions: PullConfigOptions,\n): Promise<PulledBranchConfig> {\n\tconst api = options.api ?? createApiFromOptions(options);\n\tconst projectId = options.projectId;\n\tconst project = await api.getProject(projectId);\n\tconst [branches, endpoints] = await Promise.all([\n\t\tapi.listBranches(projectId),\n\t\tapi.listEndpoints(projectId),\n\t]);\n\tconst branch = resolveBranch(options.branchId, branches);\n\tconst endpoint = endpoints.find(\n\t\t(ep) => ep.type === \"read_write\" && ep.branchId === branch.id,\n\t);\n\tconst [buckets, functions, aiGatewayEnabled] = await Promise.all([\n\t\tapi.listBranchBuckets(projectId, branch.id),\n\t\tapi.listBranchFunctions(projectId, branch.id),\n\t\tapi.getAiGatewayEnabled(projectId, branch.id),\n\t]);\n\treturn buildPulledBranchConfig(project, branch, branches, endpoint, {\n\t\tbuckets,\n\t\tfunctions,\n\t\taiGatewayEnabled,\n\t});\n}\n\nfunction createApiFromOptions(options: PullConfigOptions): NeonApi {\n\treturn createNeonApiFromOptions(\"pullConfig\", {\n\t\t...(options.apiKey ? { apiKey: options.apiKey } : {}),\n\t});\n}\n\nexport function buildPulledBranchConfig(\n\tproject: NeonProjectSnapshot,\n\tbranch: NeonBranchSnapshot,\n\tbranches: NeonBranchSnapshot[],\n\tendpoint: NeonEndpointSnapshot | undefined,\n\tpreviewState?: {\n\t\tbuckets: NeonBucketSnapshot[];\n\t\tfunctions: NeonFunctionSnapshot[];\n\t\taiGatewayEnabled: boolean;\n\t},\n): PulledBranchConfig {\n\tconst parent = branch.parentId\n\t\t? branches.find((b) => b.id === branch.parentId)\n\t\t: undefined;\n\tconst config: BranchConfig = {};\n\tif (parent) config.parent = parent.name;\n\tif (branch.expiresAt) config.ttl = branch.expiresAt;\n\tif (branch.protected) config.protected = true;\n\tif (endpoint) {\n\t\tconst compute = endpointToComputeSettings(endpoint, project);\n\t\tif (compute) config.postgres = { computeSettings: compute };\n\t}\n\tconst result: PulledBranchConfig = {\n\t\tproject: {\n\t\t\tid: project.id,\n\t\t\tname: project.name,\n\t\t\tregion: project.regionId,\n\t\t\tpgVersion: project.pgVersion,\n\t\t\t...(project.orgId ? { orgId: project.orgId } : {}),\n\t\t},\n\t\tbranch: {\n\t\t\tid: branch.id,\n\t\t\tname: branch.name,\n\t\t\t...(parent ? { parent: parent.name } : {}),\n\t\t\tisDefault: branch.isDefault,\n\t\t\tprotected: branch.protected,\n\t\t\t...(branch.expiresAt ? { expiresAt: branch.expiresAt } : {}),\n\t\t},\n\t\tconfig,\n\t};\n\tconst preview = previewState ? buildPulledPreview(previewState) : undefined;\n\tif (preview) result.preview = preview;\n\treturn result;\n}\n\n/**\n * Reverse-engineer the {@link PulledPreview} from remote snapshots. Returns `undefined` when\n * the branch has no Preview features so the field can be omitted entirely.\n */\nfunction buildPulledPreview(state: {\n\tbuckets: NeonBucketSnapshot[];\n\tfunctions: NeonFunctionSnapshot[];\n\taiGatewayEnabled: boolean;\n}): PulledPreview | undefined {\n\tif (\n\t\tstate.buckets.length === 0 &&\n\t\tstate.functions.length === 0 &&\n\t\t!state.aiGatewayEnabled\n\t) {\n\t\treturn undefined;\n\t}\n\treturn {\n\t\tbuckets: state.buckets.map((b) => ({\n\t\t\tname: b.name,\n\t\t\taccess: b.accessLevel,\n\t\t})),\n\t\tfunctions: state.functions.map((f) => ({\n\t\t\tslug: f.slug,\n\t\t\tname: f.name,\n\t\t})),\n\t\taiGatewayEnabled: state.aiGatewayEnabled,\n\t};\n}\n\nfunction resolveBranch(\n\tbranchId: string,\n\tbranches: NeonBranchSnapshot[],\n): NeonBranchSnapshot {\n\tconst match = branches.find((b) => b.id === branchId);\n\tif (match) return match;\n\tthrow new PlatformError(\n\t\tErrorCode.BranchNotFound,\n\t\t[\n\t\t\t`pullConfig: branch id ${JSON.stringify(branchId)} not found on project.`,\n\t\t\t`Available branches: ${branches.map((b) => `${b.name} (${b.id})`).join(\", \") || \"(none)\"}.`,\n\t\t].join(\" \"),\n\t\t{\n\t\t\tdetails: {\n\t\t\t\tbranchId,\n\t\t\t\tavailable: branches.map((b) => b.id),\n\t\t\t},\n\t\t},\n\t);\n}\n\nfunction endpointToComputeSettings(\n\tendpoint: NeonEndpointSnapshot,\n\tproject: NeonProjectSnapshot,\n): ComputeSettings | undefined {\n\tconst defaults = project.defaultEndpointSettings;\n\tconst out: ComputeSettings = {};\n\tif (\n\t\tendpoint.autoscalingLimitMinCu !== undefined &&\n\t\tendpoint.autoscalingLimitMinCu !== defaults?.autoscalingLimitMinCu\n\t) {\n\t\tout.autoscalingLimitMinCu = endpoint.autoscalingLimitMinCu;\n\t}\n\tif (\n\t\tendpoint.autoscalingLimitMaxCu !== undefined &&\n\t\tendpoint.autoscalingLimitMaxCu !== defaults?.autoscalingLimitMaxCu\n\t) {\n\t\tout.autoscalingLimitMaxCu = endpoint.autoscalingLimitMaxCu;\n\t}\n\tif (\n\t\tendpoint.suspendTimeout !== undefined &&\n\t\tendpoint.suspendTimeout !== defaults?.suspendTimeout\n\t) {\n\t\tout.suspendTimeout = endpoint.suspendTimeout;\n\t}\n\treturn Object.keys(out).length > 0 ? out : undefined;\n}\n"],"mappings":"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"}
|