neon 3.0.0 → 3.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.
Files changed (198) hide show
  1. package/README.md +54 -0
  2. package/dist/_shared/auth_selection.js +76 -79
  3. package/dist/_shared/credentials.js +166 -187
  4. package/dist/_shared/env-core/env.js +354 -517
  5. package/dist/_shared/env-core/reuse-secrets.js +159 -203
  6. package/dist/_shared/paths.js +129 -126
  7. package/dist/_shared/profiles.js +192 -242
  8. package/dist/_shared/secure_file.js +36 -38
  9. package/dist/_virtual/_rolldown/runtime.js +13 -0
  10. package/dist/analytics.js +163 -207
  11. package/dist/api.js +815 -758
  12. package/dist/auth.js +121 -141
  13. package/dist/auth_context.js +39 -53
  14. package/dist/cli.js +4 -7
  15. package/dist/commands/api.js +220 -250
  16. package/dist/commands/api_keys.js +251 -314
  17. package/dist/commands/auth.js +283 -328
  18. package/dist/commands/bootstrap.js +372 -437
  19. package/dist/commands/branches.js +304 -455
  20. package/dist/commands/bucket.js +374 -514
  21. package/dist/commands/checkout.js +213 -298
  22. package/dist/commands/config.js +573 -690
  23. package/dist/commands/connection_string.js +137 -165
  24. package/dist/commands/data_api.js +238 -260
  25. package/dist/commands/databases.js +67 -76
  26. package/dist/commands/deploy.js +31 -25
  27. package/dist/commands/dev.js +639 -719
  28. package/dist/commands/diff.js +156 -200
  29. package/dist/commands/env.js +243 -303
  30. package/dist/commands/functions.js +275 -355
  31. package/dist/commands/index.js +70 -65
  32. package/dist/commands/init.js +84 -119
  33. package/dist/commands/inspect.js +55 -55
  34. package/dist/commands/ip_allow.js +88 -120
  35. package/dist/commands/link.js +874 -1019
  36. package/dist/commands/logs.js +291 -0
  37. package/dist/commands/neon_auth.js +725 -933
  38. package/dist/commands/operations.js +34 -25
  39. package/dist/commands/orgs.js +28 -18
  40. package/dist/commands/profile.js +614 -845
  41. package/dist/commands/projects.js +313 -373
  42. package/dist/commands/psql.js +60 -58
  43. package/dist/commands/roles.js +55 -58
  44. package/dist/commands/schema_diff.js +87 -131
  45. package/dist/commands/set_context.js +34 -26
  46. package/dist/commands/snapshots.js +288 -413
  47. package/dist/commands/status.js +41 -37
  48. package/dist/commands/user.js +21 -10
  49. package/dist/commands/vpc_endpoints.js +85 -113
  50. package/dist/config.js +7 -6
  51. package/dist/config_format.js +50 -66
  52. package/dist/config_template.js +128 -157
  53. package/dist/context.js +183 -235
  54. package/dist/current_branch_fast_path.js +40 -49
  55. package/dist/dev/env.js +313 -394
  56. package/dist/dev/functions.js +54 -68
  57. package/dist/dev/inputs.js +46 -58
  58. package/dist/dev/runtime.js +135 -164
  59. package/dist/dev/websocket.js +766 -959
  60. package/dist/env.js +27 -33
  61. package/dist/env_file.js +118 -132
  62. package/dist/env_services.js +36 -38
  63. package/dist/errors.js +57 -68
  64. package/dist/functions_api.js +45 -43
  65. package/dist/help.js +189 -140
  66. package/dist/index.js +182 -257
  67. package/dist/init/agents.js +137 -118
  68. package/dist/init/auth.js +58 -68
  69. package/dist/init/bootstrap.js +325 -396
  70. package/dist/init/build_config.js +4 -2
  71. package/dist/init/detect_agent.js +56 -101
  72. package/dist/init/editors.js +35 -52
  73. package/dist/init/enrich_output.js +51 -66
  74. package/dist/init/extension.js +134 -171
  75. package/dist/init/inspect.js +179 -266
  76. package/dist/init/interactive.js +510 -622
  77. package/dist/init/neonctl.js +117 -168
  78. package/dist/init/orchestrate.js +157 -173
  79. package/dist/init/phases/auth.js +188 -202
  80. package/dist/init/phases/cleanup.js +23 -23
  81. package/dist/init/phases/db.js +251 -277
  82. package/dist/init/phases/getting_started.js +213 -223
  83. package/dist/init/phases/mcp.js +174 -224
  84. package/dist/init/phases/migrations.js +247 -248
  85. package/dist/init/phases/neon_auth.js +114 -133
  86. package/dist/init/phases/setup.js +546 -703
  87. package/dist/init/phases/skills.js +75 -86
  88. package/dist/init/phases/status.js +72 -67
  89. package/dist/init/resolve_context.js +102 -99
  90. package/dist/init/route_command.js +91 -98
  91. package/dist/init/skills.js +174 -218
  92. package/dist/init/vsix.js +77 -99
  93. package/dist/log.js +17 -16
  94. package/dist/neon_services.js +104 -129
  95. package/dist/parameters.gen.js +481 -471
  96. package/dist/pkg.js +17 -19
  97. package/dist/profile_keys.js +44 -47
  98. package/dist/psql/cli.js +44 -47
  99. package/dist/psql/command/cmd_cond.js +231 -406
  100. package/dist/psql/command/cmd_connect.js +557 -764
  101. package/dist/psql/command/cmd_copy.js +727 -983
  102. package/dist/psql/command/cmd_describe.js +1499 -1688
  103. package/dist/psql/command/cmd_format.js +733 -905
  104. package/dist/psql/command/cmd_io.js +1293 -2082
  105. package/dist/psql/command/cmd_lo.js +297 -359
  106. package/dist/psql/command/cmd_meta.js +727 -878
  107. package/dist/psql/command/cmd_misc.js +138 -172
  108. package/dist/psql/command/cmd_pipeline.js +547 -1099
  109. package/dist/psql/command/cmd_restrict.js +119 -155
  110. package/dist/psql/command/cmd_show.js +529 -688
  111. package/dist/psql/command/dispatch.js +261 -325
  112. package/dist/psql/command/inputQueue.js +35 -33
  113. package/dist/psql/command/shared.js +49 -63
  114. package/dist/psql/complete/filenames.js +90 -133
  115. package/dist/psql/complete/index.js +59 -97
  116. package/dist/psql/complete/matcher.js +236 -300
  117. package/dist/psql/complete/psqlVars.js +218 -223
  118. package/dist/psql/complete/queries.js +159 -177
  119. package/dist/psql/complete/rules.js +1493 -2299
  120. package/dist/psql/core/common.js +762 -1180
  121. package/dist/psql/core/help.js +456 -546
  122. package/dist/psql/core/mainloop.js +692 -1302
  123. package/dist/psql/core/prompt.js +391 -408
  124. package/dist/psql/core/settings.js +429 -644
  125. package/dist/psql/core/sqlHelp.js +480 -554
  126. package/dist/psql/core/startup.js +626 -815
  127. package/dist/psql/core/syncVars.js +67 -110
  128. package/dist/psql/core/variables.js +156 -278
  129. package/dist/psql/describe/formatters.js +884 -1285
  130. package/dist/psql/describe/processNamePattern.js +173 -260
  131. package/dist/psql/describe/queries.js +1368 -2403
  132. package/dist/psql/describe/versionGate.js +32 -41
  133. package/dist/psql/index.js +1414 -1927
  134. package/dist/psql/io/history.js +232 -271
  135. package/dist/psql/io/input.js +103 -108
  136. package/dist/psql/io/lineEditor/buffer.js +238 -319
  137. package/dist/psql/io/lineEditor/complete.js +135 -213
  138. package/dist/psql/io/lineEditor/filename.js +139 -148
  139. package/dist/psql/io/lineEditor/index.js +653 -870
  140. package/dist/psql/io/lineEditor/keymap.js +544 -702
  141. package/dist/psql/io/lineEditor/vt100.js +294 -341
  142. package/dist/psql/io/pgpass.js +158 -187
  143. package/dist/psql/io/pgservice.js +146 -183
  144. package/dist/psql/io/psqlrc.js +328 -403
  145. package/dist/psql/print/aligned.js +1020 -1683
  146. package/dist/psql/print/asciidoc.js +180 -214
  147. package/dist/psql/print/crosstab.js +281 -442
  148. package/dist/psql/print/csv.js +48 -70
  149. package/dist/psql/print/html.js +195 -226
  150. package/dist/psql/print/json.js +75 -88
  151. package/dist/psql/print/latex.js +291 -364
  152. package/dist/psql/print/pager.js +171 -242
  153. package/dist/psql/print/troff.js +194 -226
  154. package/dist/psql/print/unaligned.js +69 -95
  155. package/dist/psql/print/units.js +167 -169
  156. package/dist/psql/scanner/slash.js +428 -483
  157. package/dist/psql/scanner/sql.js +445 -889
  158. package/dist/psql/scanner/stringutils.js +309 -379
  159. package/dist/psql/types/index.js +2 -7
  160. package/dist/psql/types/scanner.js +25 -22
  161. package/dist/psql/wire/connection.js +2042 -2803
  162. package/dist/psql/wire/copy.js +84 -100
  163. package/dist/psql/wire/notify.js +39 -59
  164. package/dist/psql/wire/pipeline.js +305 -518
  165. package/dist/psql/wire/protocol.js +349 -417
  166. package/dist/psql/wire/sasl.js +180 -265
  167. package/dist/psql/wire/tls.js +400 -561
  168. package/dist/storage_api.js +115 -129
  169. package/dist/test_utils/fixtures.js +94 -113
  170. package/dist/test_utils/oauth_server.js +10 -7
  171. package/dist/test_utils/project_dir.js +33 -0
  172. package/dist/utils/ai_gateway_notice.js +131 -162
  173. package/dist/utils/api_enums.js +21 -28
  174. package/dist/utils/auth.js +10 -4
  175. package/dist/utils/branch_notice.js +20 -19
  176. package/dist/utils/branch_picker.js +83 -89
  177. package/dist/utils/cli_name.js +15 -12
  178. package/dist/utils/compute_units.js +20 -27
  179. package/dist/utils/config_diff.js +127 -158
  180. package/dist/utils/enrichers.js +95 -148
  181. package/dist/utils/esbuild.js +130 -189
  182. package/dist/utils/flags.js +35 -47
  183. package/dist/utils/formats.js +8 -15
  184. package/dist/utils/git_diff.js +69 -80
  185. package/dist/utils/inspect_db.js +101 -143
  186. package/dist/utils/inspect_queries.js +179 -142
  187. package/dist/utils/middlewares.js +37 -44
  188. package/dist/utils/openapi.js +87 -99
  189. package/dist/utils/package_manager.js +312 -110
  190. package/dist/utils/point_in_time.js +49 -53
  191. package/dist/utils/psql.js +89 -106
  192. package/dist/utils/service_picker.js +55 -58
  193. package/dist/utils/string.js +5 -5
  194. package/dist/utils/ui.js +38 -55
  195. package/dist/utils/write_sync.js +26 -35
  196. package/dist/utils/zip.js +4 -3
  197. package/dist/writer.js +67 -87
  198. package/package.json +7 -5
@@ -1,276 +1,226 @@
1
- /**
2
- * # Profiles — several Neon accounts in one config directory
3
- *
4
- * A profile is **a pointer to a credentials file**. Nothing more. That constraint is what
5
- * keeps the feature small: there is no mirror, no per-profile directory tree, no persistent
6
- * "active profile" state to fall out of sync, and no migration.
7
- *
8
- * ```
9
- * ~/.config/neon/
10
- * ├── credentials.json # this IS the DEFAULT profile, not a copy of it
11
- * ├── credentials.work.json # created by `neon auth --profile work`
12
- * └── profiles.json # created only once a second profile exists
13
- * ```
14
- *
15
- * `profiles.json` maps a name to a path, and the path may point anywhere — which is what
16
- * makes adopting an existing directory a one-line edit rather than an import command:
17
- *
18
- * ```json
19
- * {
20
- * "version": 1,
21
- * "profiles": {
22
- * "DEFAULT": { "credentials": "credentials.json" },
23
- * "work": {
24
- * "credentials": "../neonctl-databricks/credentials.json",
25
- * "label": "someone@example.com"
26
- * }
27
- * }
28
- * }
29
- * ```
30
- *
31
- * ## Selection
32
- *
33
- * `--profile` → `NEON_PROFILE` → `DEFAULT`. Per invocation, like `AWS_PROFILE`; there is no
34
- * `profile use` command, so nothing persists that could disagree with what you typed.
35
- *
36
- * ## Compatibility
37
- *
38
- * An install with no `profiles.json` is already a valid `DEFAULT`-only state: `DEFAULT`
39
- * resolves to `credentials.json` in the config directory (including an existing one in the
40
- * legacy `neonctl` directory — see `./paths.ts`). Nothing is created until a second
41
- * profile is, and nothing is ever moved.
42
- */
1
+ import { writeSecretFile } from "./secure_file.js";
2
+ import { credentialsPath, defaultDir, resolveConfigFile } from "./paths.js";
43
3
  import { existsSync, readFileSync } from "node:fs";
44
4
  import { isAbsolute, relative, resolve } from "node:path";
45
- import { credentialsPath, defaultDir, resolveConfigFile } from "./paths.js";
46
- import { writeSecretFile } from "./secure_file.js";
47
- export const PROFILES_FILE = "profiles.json";
5
+ //#region src/_shared/profiles.ts
6
+ /**
7
+ * # Profiles — several Neon accounts in one config directory
8
+ *
9
+ * A profile is **a pointer to a credentials file**. Nothing more. That constraint is what
10
+ * keeps the feature small: there is no mirror, no per-profile directory tree, no persistent
11
+ * "active profile" state to fall out of sync, and no migration.
12
+ *
13
+ * ```
14
+ * ~/.config/neon/
15
+ * ├── credentials.json # this IS the DEFAULT profile, not a copy of it
16
+ * ├── credentials.work.json # created by `neon auth --profile work`
17
+ * └── profiles.json # created only once a second profile exists
18
+ * ```
19
+ *
20
+ * `profiles.json` maps a name to a path, and the path may point anywhere — which is what
21
+ * makes adopting an existing directory a one-line edit rather than an import command:
22
+ *
23
+ * ```json
24
+ * {
25
+ * "version": 1,
26
+ * "profiles": {
27
+ * "DEFAULT": { "credentials": "credentials.json" },
28
+ * "work": {
29
+ * "credentials": "../neonctl-databricks/credentials.json",
30
+ * "label": "someone@example.com"
31
+ * }
32
+ * }
33
+ * }
34
+ * ```
35
+ *
36
+ * ## Selection
37
+ *
38
+ * `--profile` → `NEON_PROFILE` → `DEFAULT`. Per invocation, like `AWS_PROFILE`; there is no
39
+ * `profile use` command, so nothing persists that could disagree with what you typed.
40
+ *
41
+ * ## Compatibility
42
+ *
43
+ * An install with no `profiles.json` is already a valid `DEFAULT`-only state: `DEFAULT`
44
+ * resolves to `credentials.json` in the config directory (including an existing one in the
45
+ * legacy `neonctl` directory — see `./paths.ts`). Nothing is created until a second
46
+ * profile is, and nothing is ever moved.
47
+ */
48
+ const PROFILES_FILE = "profiles.json";
48
49
  /** The implicit profile. Backed by plain `credentials.json`, with or without a profiles file. */
49
- export const DEFAULT_PROFILE = "DEFAULT";
50
+ const DEFAULT_PROFILE = "DEFAULT";
50
51
  /** Profile names become part of a filename, so keep them boring. */
51
52
  const NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
52
53
  /** Which profile this invocation should use: `--profile` → `NEON_PROFILE` → `DEFAULT`. */
53
- export const selectProfileName = (flag, env = process.env) => nonEmpty(flag) ?? nonEmpty(env.NEON_PROFILE) ?? DEFAULT_PROFILE;
54
- export const assertValidProfileName = (name) => {
55
- if (!NAME_PATTERN.test(name)) {
56
- throw new Error(`Invalid profile name "${name}". Use letters, digits, dot, dash or underscore, starting with a letter or digit.`);
57
- }
54
+ const selectProfileName = (flag, env = process.env) => nonEmpty(flag) ?? nonEmpty(env.NEON_PROFILE) ?? "DEFAULT";
55
+ const assertValidProfileName = (name) => {
56
+ if (!NAME_PATTERN.test(name)) throw new Error(`Invalid profile name "${name}". Use letters, digits, dot, dash or underscore, starting with a letter or digit.`);
58
57
  };
59
58
  /** Where `profiles.json` lives for this config directory (whether or not it exists yet). */
60
- export const profilesFilePath = (dir) => resolveConfigFile(PROFILES_FILE, dir === defaultDir ? {} : { dir }).path;
59
+ const profilesFilePath = (dir) => resolveConfigFile(PROFILES_FILE, dir === defaultDir ? {} : { dir }).path;
61
60
  /**
62
- * Read and classify `profiles.json` without deciding what to do about it.
63
- *
64
- * Entry keys and shapes are validated here rather than at each use. A key is a profile name,
65
- * and a name that `assertValidProfileName` would reject cannot have been written by this CLI —
66
- * it would travel into error messages as a recovery command nobody can run, and into a
67
- * `credentials.<name>.json` filename.
68
- */
69
- export const inspectProfiles = (dir) => {
70
- const path = profilesFilePath(dir);
71
- if (!existsSync(path))
72
- return { kind: "absent" };
73
- const broken = (why) => ({
74
- kind: "unusable",
75
- reason: `${path} could not be read as a profiles file: ${why}`,
76
- });
77
- // Reading and parsing are separate failures with separate answers. Sharing one catch
78
- // reported `EACCES` as "not valid JSON", which sends the user to edit a file that is
79
- // perfectly valid and that they cannot open.
80
- let contents;
81
- try {
82
- contents = readFileSync(path, "utf8");
83
- }
84
- catch (err) {
85
- const code = err.code;
86
- return broken(code ? `reading it failed with ${code}` : "reading it failed");
87
- }
88
- let parsed;
89
- try {
90
- parsed = JSON.parse(contents);
91
- }
92
- catch {
93
- return broken("it is not valid JSON");
94
- }
95
- if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
96
- return broken("it does not contain an object");
97
- const profiles = parsed.profiles;
98
- if (profiles === null ||
99
- typeof profiles !== "object" ||
100
- Array.isArray(profiles))
101
- return broken("it has no `profiles` object");
102
- for (const [name, entry] of Object.entries(profiles)) {
103
- if (!NAME_PATTERN.test(name))
104
- return broken(`"${name}" is not a valid profile name`);
105
- if (entry === null ||
106
- typeof entry !== "object" ||
107
- typeof entry.credentials !== "string" ||
108
- entry.credentials.trim() === "") {
109
- return broken(`profile "${name}" has no \`credentials\` path`);
110
- }
111
- }
112
- return { kind: "ok", file: { version: 1, profiles } };
61
+ * Read and classify `profiles.json` without deciding what to do about it.
62
+ *
63
+ * Entry keys and shapes are validated here rather than at each use. A key is a profile name,
64
+ * and a name that `assertValidProfileName` would reject cannot have been written by this CLI —
65
+ * it would travel into error messages as a recovery command nobody can run, and into a
66
+ * `credentials.<name>.json` filename.
67
+ */
68
+ const inspectProfiles = (dir) => {
69
+ const path = profilesFilePath(dir);
70
+ if (!existsSync(path)) return { kind: "absent" };
71
+ const broken = (why) => ({
72
+ kind: "unusable",
73
+ reason: `${path} could not be read as a profiles file: ${why}`
74
+ });
75
+ let contents;
76
+ try {
77
+ contents = readFileSync(path, "utf8");
78
+ } catch (err) {
79
+ const code = err.code;
80
+ return broken(code ? `reading it failed with ${code}` : "reading it failed");
81
+ }
82
+ let parsed;
83
+ try {
84
+ parsed = JSON.parse(contents);
85
+ } catch {
86
+ return broken("it is not valid JSON");
87
+ }
88
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return broken("it does not contain an object");
89
+ const profiles = parsed.profiles;
90
+ if (profiles === null || typeof profiles !== "object" || Array.isArray(profiles)) return broken("it has no `profiles` object");
91
+ for (const [name, entry] of Object.entries(profiles)) {
92
+ if (!NAME_PATTERN.test(name)) return broken(`"${name}" is not a valid profile name`);
93
+ if (entry === null || typeof entry !== "object" || typeof entry.credentials !== "string" || entry.credentials.trim() === "") return broken(`profile "${name}" has no \`credentials\` path`);
94
+ }
95
+ return {
96
+ kind: "ok",
97
+ file: {
98
+ version: 1,
99
+ profiles
100
+ }
101
+ };
113
102
  };
114
103
  /**
115
- * Read `profiles.json`, or `null` when there is nothing usable there.
116
- *
117
- * A malformed file is reported through `onWarn` and treated as absent, because for a *read* the
118
- * worst case is a named profile turning up missing, which is recoverable — whereas throwing
119
- * would lock the user out of `neon auth` itself. Writing is the opposite: see
120
- * {@link upsertProfile}, which refuses rather than rebuilding a file it cannot read.
121
- */
122
- export const readProfiles = (dir,
123
- /** Called with the reason a profiles file was ignored. The consumer owns how it reports. */
124
- onWarn = () => { }) => {
125
- const read = inspectProfiles(dir);
126
- if (read.kind === "ok")
127
- return read.file;
128
- if (read.kind === "unusable")
129
- onWarn(read.reason);
130
- return null;
104
+ * Read `profiles.json`, or `null` when there is nothing usable there.
105
+ *
106
+ * A malformed file is reported through `onWarn` and treated as absent, because for a *read* the
107
+ * worst case is a named profile turning up missing, which is recoverable — whereas throwing
108
+ * would lock the user out of `neon auth` itself. Writing is the opposite: see
109
+ * {@link upsertProfile}, which refuses rather than rebuilding a file it cannot read.
110
+ */
111
+ const readProfiles = (dir, onWarn = () => {}) => {
112
+ const read = inspectProfiles(dir);
113
+ if (read.kind === "ok") return read.file;
114
+ if (read.kind === "unusable") onWarn(read.reason);
115
+ return null;
131
116
  };
132
117
  /**
133
- * Refuse to act on a named profile when the file that defines it cannot be read.
134
- *
135
- * Call this **before** anything that writes a credential, opens a browser, or spends an API
136
- * call. {@link upsertProfile} refuses too, but it runs last: by then `create` has already
137
- * overwritten `credentials.<name>.json` and revoked the key it replaced, and `neon auth
138
- * --profile` has already signed in over it — a refusal that arrives after the destruction it
139
- * exists to prevent. The path resolution itself is the unsound part, since with the metadata
140
- * unreadable the conventional filename is a guess about which account that file belongs to.
141
- *
142
- * `DEFAULT` is exempt: it is defined by the absence of metadata rather than by an entry, so
143
- * signing in normally must keep working while a broken `profiles.json` is repaired.
144
- */
145
- export const assertProfilesUsable = (dir, name) => {
146
- if (name === DEFAULT_PROFILE)
147
- return;
148
- const read = inspectProfiles(dir);
149
- if (read.kind === "unusable") {
150
- throw new Error(`${read.reason}. Fix or delete the file before working with profile "${name}" — it is the only record of where each account's credentials live.`);
151
- }
118
+ * Refuse to act on a named profile when the file that defines it cannot be read.
119
+ *
120
+ * Call this **before** anything that writes a credential, opens a browser, or spends an API
121
+ * call. {@link upsertProfile} refuses too, but it runs last: by then `create` has already
122
+ * overwritten `credentials.<name>.json` and revoked the key it replaced, and `neon auth
123
+ * --profile` has already signed in over it — a refusal that arrives after the destruction it
124
+ * exists to prevent. The path resolution itself is the unsound part, since with the metadata
125
+ * unreadable the conventional filename is a guess about which account that file belongs to.
126
+ *
127
+ * `DEFAULT` is exempt: it is defined by the absence of metadata rather than by an entry, so
128
+ * signing in normally must keep working while a broken `profiles.json` is repaired.
129
+ */
130
+ const assertProfilesUsable = (dir, name) => {
131
+ if (name === "DEFAULT") return;
132
+ const read = inspectProfiles(dir);
133
+ if (read.kind === "unusable") throw new Error(`${read.reason}. Fix or delete the file before working with profile "${name}" — it is the only record of where each account's credentials live.`);
152
134
  };
153
135
  /** Resolve a profile to an absolute credentials path. Throws when a named profile is unknown. */
154
- export const resolveProfile = (dir, name) => {
155
- const read = inspectProfiles(dir);
156
- // A broken file must not be reported as `Unknown profile "work"`. That names the wrong
157
- // problem, and the user goes looking for a profile they can see in the file in front of them.
158
- if (read.kind === "unusable" && name !== DEFAULT_PROFILE) {
159
- throw new Error(`${read.reason}. Fix or delete the file — every named profile is defined in it.`);
160
- }
161
- const file = read.kind === "ok" ? read.file : null;
162
- const entry = file?.profiles[name];
163
- if (entry) {
164
- return {
165
- name,
166
- credentialsPath: resolveEntryPath(dir, entry.credentials),
167
- ...(entry.label ? { label: entry.label } : {}),
168
- ...(entry.userId ? { userId: entry.userId } : {}),
169
- declared: true,
170
- };
171
- }
172
- // DEFAULT works with no profiles.json at all, and keeps working when one exists but
173
- // doesn't mention it — that is the pre-profiles behaviour, unchanged.
174
- if (name === DEFAULT_PROFILE) {
175
- return {
176
- name,
177
- credentialsPath: credentialsPath(dir),
178
- declared: false,
179
- };
180
- }
181
- const known = file
182
- ? Object.keys(file.profiles).join(", ")
183
- : DEFAULT_PROFILE;
184
- throw new Error(`Unknown profile "${name}". Known profiles: ${known}. Create it with \`neon profile create ${name}\`.`);
136
+ const resolveProfile = (dir, name) => {
137
+ const read = inspectProfiles(dir);
138
+ if (read.kind === "unusable" && name !== "DEFAULT") throw new Error(`${read.reason}. Fix or delete the file — every named profile is defined in it.`);
139
+ const file = read.kind === "ok" ? read.file : null;
140
+ const entry = file?.profiles[name];
141
+ if (entry) return {
142
+ name,
143
+ credentialsPath: resolveEntryPath(dir, entry.credentials),
144
+ ...entry.label ? { label: entry.label } : {},
145
+ ...entry.userId ? { userId: entry.userId } : {},
146
+ declared: true
147
+ };
148
+ if (name === "DEFAULT") return {
149
+ name,
150
+ credentialsPath: credentialsPath(dir),
151
+ declared: false
152
+ };
153
+ const known = file ? Object.keys(file.profiles).join(", ") : DEFAULT_PROFILE;
154
+ throw new Error(`Unknown profile "${name}". Known profiles: ${known}. Create it with \`neon profile create ${name}\`.`);
185
155
  };
186
156
  /** Default location for a new named profile's credentials file. */
187
- export const newProfileCredentialsPath = (dir, name) => resolve(dir, `credentials.${name}.json`);
157
+ const newProfileCredentialsPath = (dir, name) => resolve(dir, `credentials.${name}.json`);
188
158
  /**
189
- * Record a profile, creating `profiles.json` if this is the first named one.
190
- *
191
- * When the file is created, `DEFAULT` is written explicitly and pointed at wherever
192
- * `credentials.json` actually is. That matters for an install predating the directory
193
- * rename: `profiles.json` is created in `neon/` while the credentials are still in
194
- * `neonctl/`, so `DEFAULT` is recorded as `../neonctl/credentials.json` rather than a
195
- * relative name that would resolve to a file that isn't there.
196
- */
197
- export const upsertProfile = (dir, name, entry) => {
198
- assertValidProfileName(name);
199
- const path = profilesFilePath(dir);
200
- const read = inspectProfiles(dir);
201
- // Refusing is the point. Treating a broken file as absent here rebuilt it from a single
202
- // `DEFAULT` entry and dropped every named profile in it — silent data loss, in the file
203
- // that is the only record of where each account's credentials live. The credentials
204
- // themselves survive, so fixing the file by hand recovers everything.
205
- if (read.kind === "unusable") {
206
- throw new Error(`${read.reason}. Refusing to rewrite it, because doing so would discard the profiles it defines. Fix or delete the file, then re-run.`);
207
- }
208
- const file = read.kind === "ok"
209
- ? read.file
210
- : {
211
- version: 1,
212
- profiles: {
213
- [DEFAULT_PROFILE]: {
214
- credentials: relativeToProfiles(path, credentialsPath(dir)),
215
- },
216
- },
217
- };
218
- file.profiles[name] = {
219
- credentials: relativeToProfiles(path, entry.credentials),
220
- ...(entry.label ? { label: entry.label } : {}),
221
- ...(entry.userId ? { userId: entry.userId } : {}),
222
- };
223
- writeProfiles(path, file);
159
+ * Record a profile, creating `profiles.json` if this is the first named one.
160
+ *
161
+ * When the file is created, `DEFAULT` is written explicitly and pointed at wherever
162
+ * `credentials.json` actually is. That matters for an install predating the directory
163
+ * rename: `profiles.json` is created in `neon/` while the credentials are still in
164
+ * `neonctl/`, so `DEFAULT` is recorded as `../neonctl/credentials.json` rather than a
165
+ * relative name that would resolve to a file that isn't there.
166
+ */
167
+ const upsertProfile = (dir, name, entry) => {
168
+ assertValidProfileName(name);
169
+ const path = profilesFilePath(dir);
170
+ const read = inspectProfiles(dir);
171
+ if (read.kind === "unusable") throw new Error(`${read.reason}. Refusing to rewrite it, because doing so would discard the profiles it defines. Fix or delete the file, then re-run.`);
172
+ const file = read.kind === "ok" ? read.file : {
173
+ version: 1,
174
+ profiles: { [DEFAULT_PROFILE]: { credentials: relativeToProfiles(path, credentialsPath(dir)) } }
175
+ };
176
+ file.profiles[name] = {
177
+ credentials: relativeToProfiles(path, entry.credentials),
178
+ ...entry.label ? { label: entry.label } : {},
179
+ ...entry.userId ? { userId: entry.userId } : {}
180
+ };
181
+ writeProfiles(path, file);
224
182
  };
225
183
  /** Remove an entry. Returns false when it wasn't there. */
226
- export const removeProfileEntry = (dir, name) => {
227
- const path = profilesFilePath(dir);
228
- const file = readProfiles(dir);
229
- if (!file?.profiles[name])
230
- return false;
231
- delete file.profiles[name];
232
- writeProfiles(path, file);
233
- return true;
184
+ const removeProfileEntry = (dir, name) => {
185
+ const path = profilesFilePath(dir);
186
+ const file = readProfiles(dir);
187
+ if (!file?.profiles[name]) return false;
188
+ delete file.profiles[name];
189
+ writeProfiles(path, file);
190
+ return true;
234
191
  };
235
192
  /**
236
- * True when only `DEFAULT` is left, so `profiles.json` no longer earns its place. Mirrors
237
- * lazy creation: a single-account install has no profiles file, before or after.
238
- */
239
- export const onlyDefaultRemains = (file) => {
240
- const names = Object.keys(file.profiles);
241
- return (names.length === 0 ||
242
- (names.length === 1 && names[0] === DEFAULT_PROFILE));
193
+ * True when only `DEFAULT` is left, so `profiles.json` no longer earns its place. Mirrors
194
+ * lazy creation: a single-account install has no profiles file, before or after.
195
+ */
196
+ const onlyDefaultRemains = (file) => {
197
+ const names = Object.keys(file.profiles);
198
+ return names.length === 0 || names.length === 1 && names[0] === "DEFAULT";
243
199
  };
244
- export const listProfiles = (dir) => {
245
- const read = inspectProfiles(dir);
246
- // Listing is the command run to find out what is there, so a broken file is the answer
247
- // rather than an obstacle. Showing only `DEFAULT` would state, as fact, that the profiles
248
- // in that file do not exist.
249
- if (read.kind === "unusable") {
250
- throw new Error(`${read.reason}. Fix or delete the file — every named profile is defined in it.`);
251
- }
252
- const file = read.kind === "ok" ? read.file : null;
253
- if (!file)
254
- return [resolveProfile(dir, DEFAULT_PROFILE)];
255
- const names = Object.keys(file.profiles);
256
- if (!names.includes(DEFAULT_PROFILE))
257
- names.unshift(DEFAULT_PROFILE);
258
- return names.map((name) => resolveProfile(dir, name));
200
+ const listProfiles = (dir) => {
201
+ const read = inspectProfiles(dir);
202
+ if (read.kind === "unusable") throw new Error(`${read.reason}. Fix or delete the file — every named profile is defined in it.`);
203
+ const file = read.kind === "ok" ? read.file : null;
204
+ if (!file) return [resolveProfile(dir, DEFAULT_PROFILE)];
205
+ const names = Object.keys(file.profiles);
206
+ if (!names.includes("DEFAULT")) names.unshift(DEFAULT_PROFILE);
207
+ return names.map((name) => resolveProfile(dir, name));
259
208
  };
260
209
  const writeProfiles = (path, file) => {
261
- writeSecretFile(path, `${JSON.stringify(file, null, 2)}\n`);
210
+ writeSecretFile(path, `${JSON.stringify(file, null, 2)}\n`);
262
211
  };
263
212
  const resolveEntryPath = (dir, entry) => isAbsolute(entry) ? entry : resolve(profilesDir(dir), entry);
264
213
  /** `profiles.json` may sit in the legacy directory, so entries resolve against its own dir. */
265
214
  const profilesDir = (dir) => resolve(profilesFilePath(dir), "..");
266
215
  /** Keep entries relative when they sit near `profiles.json`; absolute paths stay absolute. */
267
216
  const relativeToProfiles = (profilesPath, target) => {
268
- const rel = relative(resolve(profilesPath, ".."), target);
269
- return rel && !isAbsolute(rel) ? rel : target;
217
+ const rel = relative(resolve(profilesPath, ".."), target);
218
+ return rel && !isAbsolute(rel) ? rel : target;
270
219
  };
271
220
  function nonEmpty(value) {
272
- if (typeof value !== "string")
273
- return undefined;
274
- const trimmed = value.trim();
275
- return trimmed === "" ? undefined : trimmed;
221
+ if (typeof value !== "string") return void 0;
222
+ const trimmed = value.trim();
223
+ return trimmed === "" ? void 0 : trimmed;
276
224
  }
225
+ //#endregion
226
+ export { DEFAULT_PROFILE, PROFILES_FILE, assertProfilesUsable, assertValidProfileName, inspectProfiles, listProfiles, newProfileCredentialsPath, onlyDefaultRemains, profilesFilePath, readProfiles, removeProfileEntry, resolveProfile, selectProfileName, upsertProfile };
@@ -1,43 +1,41 @@
1
1
  import { renameSync, unlinkSync, writeFileSync } from "node:fs";
2
2
  import { basename, dirname, join } from "node:path";
3
+ //#region src/_shared/secure_file.ts
3
4
  /** Owner read/write. A credential needs those two and nothing else. */
4
- export const SECRET_FILE_MODE = 0o600;
5
+ const SECRET_FILE_MODE = 384;
5
6
  /**
6
- * Write a secret to disk owner-only, by creating a temporary file in the same directory and
7
- * renaming it over the target.
8
- *
9
- * The rename is what makes this correct rather than merely tidy. `writeFileSync`'s `mode`
10
- * applies only when it *creates* the file, so writing over an existing credentials file
11
- * leaves whatever permissions it already had — a file created `0700` by an older release
12
- * stays `0700` forever, and one created before a umask change stays world-readable. Renaming
13
- * a fresh inode into place means every write lands at {@link SECRET_FILE_MODE}, so the
14
- * permissions repair themselves instead of being inherited.
15
- *
16
- * It also closes the window where a reader could see the file at default permissions: the
17
- * temporary file is created `0600` *before* it holds the secret's final name, and `rename`
18
- * is atomic within a directory, so there is no moment at which the target is readable by
19
- * anyone else and no moment at which it is half-written.
20
- *
21
- * The temporary name carries the pid so two processes writing at once cannot collide on it.
22
- */
23
- export const writeSecretFile = (path, contents) => {
24
- const directory = dirname(path);
25
- const temporary = join(directory, `.${basename(path)}.${process.pid}.${Date.now()}.tmp`);
26
- try {
27
- writeFileSync(temporary, contents, {
28
- encoding: "utf8",
29
- mode: SECRET_FILE_MODE,
30
- });
31
- renameSync(temporary, path);
32
- }
33
- catch (err) {
34
- // Never leave the secret behind under a temporary name the caller doesn't know about.
35
- try {
36
- unlinkSync(temporary);
37
- }
38
- catch {
39
- // The temp file was never created, or is already gone. Report the original error.
40
- }
41
- throw err;
42
- }
7
+ * Write a secret to disk owner-only, by creating a temporary file in the same directory and
8
+ * renaming it over the target.
9
+ *
10
+ * The rename is what makes this correct rather than merely tidy. `writeFileSync`'s `mode`
11
+ * applies only when it *creates* the file, so writing over an existing credentials file
12
+ * leaves whatever permissions it already had — a file created `0700` by an older release
13
+ * stays `0700` forever, and one created before a umask change stays world-readable. Renaming
14
+ * a fresh inode into place means every write lands at {@link SECRET_FILE_MODE}, so the
15
+ * permissions repair themselves instead of being inherited.
16
+ *
17
+ * It also closes the window where a reader could see the file at default permissions: the
18
+ * temporary file is created `0600` *before* it holds the secret's final name, and `rename`
19
+ * is atomic within a directory, so there is no moment at which the target is readable by
20
+ * anyone else and no moment at which it is half-written.
21
+ *
22
+ * The temporary name carries the pid so two processes writing at once cannot collide on it.
23
+ */
24
+ const writeSecretFile = (path, contents) => {
25
+ const directory = dirname(path);
26
+ const temporary = join(directory, `.${basename(path)}.${process.pid}.${Date.now()}.tmp`);
27
+ try {
28
+ writeFileSync(temporary, contents, {
29
+ encoding: "utf8",
30
+ mode: 384
31
+ });
32
+ renameSync(temporary, path);
33
+ } catch (err) {
34
+ try {
35
+ unlinkSync(temporary);
36
+ } catch {}
37
+ throw err;
38
+ }
43
39
  };
40
+ //#endregion
41
+ export { SECRET_FILE_MODE, writeSecretFile };
@@ -0,0 +1,13 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, no_symbols) => {
4
+ let target = {};
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return target;
11
+ };
12
+ //#endregion
13
+ export { __exportAll };