@dokebi/cli 0.4.29 → 0.4.31

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.
@@ -69,22 +69,36 @@ export async function productionDiagnostics(root, env = process.env) {
69
69
  add('NODE_ENV_NOT_PRODUCTION', 'error', 'NODE_ENV', 'NODE_ENV is not production here, so this check (and an app started like this) runs with development behaviour: run it as the service runs, NODE_ENV=production npx dokebi doctor --production (the systemd unit and compose file set it)');
70
70
  const { target, source } = await readSettings(root);
71
71
  const secretsFile = env[SECRETS_VARIABLE] || undefined;
72
- let options;
73
- try {
74
- options = (await applySecrets(parseConfig(source), secretsFile)).options;
75
- }
76
- catch (error) {
72
+ const failed = (error, file) => {
77
73
  if (!(error instanceof ConfigError))
78
74
  throw error;
79
- // The app would refuse to start with the same code; the rest is checked on the file alone.
80
- add(error.code, 'error', target.file, `${error.message}. ${error.fix}`, 'dokebi-config', {
75
+ // The app would refuse to start with the same code.
76
+ add(error.code, 'error', file, `${error.message}. ${error.fix}`, 'dokebi-config', {
81
77
  issues: error.issues,
82
78
  });
83
- // A settings file that does not parse has nothing more to check; a secrets problem leaves
84
- // the settings file itself readable.
85
- if (!error.code.startsWith('CONFIG_SECRETS') && !/secrets file/.test(error.message))
86
- return found;
87
- options = parseConfig(source).options;
79
+ };
80
+ let document;
81
+ try {
82
+ document = parseConfig(source);
83
+ }
84
+ catch (error) {
85
+ // A settings file that does not parse has nothing more to check.
86
+ failed(error, target.file);
87
+ return found;
88
+ }
89
+ let options;
90
+ let secretsFailed = false;
91
+ try {
92
+ options = (await applySecrets(document, secretsFile)).options;
93
+ }
94
+ catch (error) {
95
+ secretsFailed = true;
96
+ // A secrets problem leaves the settings file itself readable: the rest is checked on it
97
+ // alone. The list's own failures name the settings; the file's (unreadable, not YAML, a
98
+ // `secrets` list in it) name the secrets file (R8-g5a-4).
99
+ const code = error instanceof ConfigError ? error.code : '';
100
+ failed(error, code.startsWith('CONFIG_SECRETS') || secretsFile === undefined ? target.file : secretsFile);
101
+ options = document.options;
88
102
  }
89
103
  const state = readState(parseObject(await readFile(join(root, '.dokebi/state.json'), 'utf8'), 'state'));
90
104
  const installed = new Set(state.plugins.map((plugin) => optionsKey(plugin)));
@@ -127,7 +141,8 @@ export async function productionDiagnostics(root, env = process.env) {
127
141
  const placeholders = placeholderPaths(options);
128
142
  if (placeholders.length)
129
143
  add('PLACEHOLDER_LEFT', 'error', target.file, `these options still hold a <placeholder> of the sample: ${placeholders.join(', ')}; fill them in the settings (or the secrets file)`, null, { paths: placeholders });
130
- if (!found.some((item) => item.code.startsWith('CONFIG_SECRETS'))) {
144
+ // The secrets list is checked once: config secrets would refuse the same file again.
145
+ if (!secretsFailed) {
131
146
  try {
132
147
  const secrets = await configSecrets(root, env);
133
148
  for (const warning of secrets.warnings ?? [])
package/dist/project.d.ts CHANGED
@@ -78,6 +78,7 @@ export type GeneratedSecret = {
78
78
  readonly variable: string;
79
79
  readonly file: typeof ENV_FILE;
80
80
  };
81
+ /** Previous secrets `config secret --rotate` keeps after the new one in a list (AI-M05). */
81
82
  export declare const ROTATE_KEEP = 2;
82
83
  /** What `config secret` did at one option: names and counts only, never a value. */
83
84
  export interface SecretOutcome {
package/dist/project.js CHANGED
@@ -48,9 +48,11 @@ import { appViewValue, NO_VIEW, projectView } from './view-choice.js';
48
48
  import { installedPath, packageFile, workspacePackages, workspaceRootOf, } from './workspace.js';
49
49
  const GENERATED = 'src/dokebi.generated.ts';
50
50
  const EXAMPLE = 'dokebi.example.yaml';
51
- /** Previous secrets `config secret --rotate` keeps after the new one in a list (AI-M05). */
52
51
  /** An option key that holds an address (`url`, `uri`, `databaseUrl`, `endpointURI`). */
53
52
  const URL_KEY = /^(url|uri)$|(Url|URL|Uri|URI)$/;
53
+ /** A key a provider issues (`apiKey` of @dokebi/ai, D-0186): never a random string. */
54
+ const ISSUED_KEY = /^(apiKey|api_key)$|ApiKey$/;
55
+ /** Previous secrets `config secret --rotate` keeps after the new one in a list (AI-M05). */
54
56
  export const ROTATE_KEEP = 2;
55
57
  /** The generated registration differs from what the CLI wrote (D-0116: a sentence and a fix). */
56
58
  function modifiedRegistration() {
@@ -772,9 +774,10 @@ export class Project {
772
774
  },
773
775
  phase: 'plan',
774
776
  });
775
- if (options.adapter !== undefined && added.id !== 'dokebi-db')
776
- throw new CliError('USAGE', '--adapter applies to @dokebi/db only');
777
- if (options.startup !== undefined && options.adapter === undefined)
777
+ // D-0186: --adapter picks a catalog variant of the plugin being added (the db adapter, the
778
+ // ai provider SDK); only @dokebi/db also writes an adapter file and a connection.
779
+ if (options.startup !== undefined &&
780
+ (options.adapter === undefined || added.id !== 'dokebi-db'))
778
781
  throw new CliError('USAGE', '--startup applies to @dokebi/db with --adapter only');
779
782
  const hostsApps = added.hosts?.includes('apps') === true;
780
783
  if (options.firstApp === false && !hostsApps)
@@ -800,6 +803,13 @@ export class Project {
800
803
  }
801
804
  if (options.adapter === undefined)
802
805
  return { changed: [], preserved: [] };
806
+ if (added.id !== 'dokebi-db') {
807
+ // Another plugin with adapters (@dokebi/ai): the variant's driver only.
808
+ const { variant } = await this.#adapterVariant(staging, options.adapter, added.packageName);
809
+ await this.#installDriver(session, staging, variant, dependencyMap(session.pkg));
810
+ await this.#replacePackages(session, staging, session.pkg);
811
+ return await this.#finish(session, { changed: tx.paths, preserved: [], ...(warnings.length ? { warnings } : {}) }, dryRun);
812
+ }
803
813
  // An installed db gets its adapter now (adapter file, connection, driver).
804
814
  const adapted = await this.#dbAdapter(session, staging, options.adapter, {
805
815
  configSource: session.configSource,
@@ -837,7 +847,7 @@ export class Project {
837
847
  // The adapter's driver joins the same staging install before the checks run.
838
848
  const adapter = options.adapter === undefined
839
849
  ? undefined
840
- : await this.#adapterVariant(staging, options.adapter);
850
+ : await this.#adapterVariant(staging, options.adapter, added.packageName);
841
851
  if (adapter)
842
852
  await this.#installDriver(session, staging, adapter.variant, dependencies);
843
853
  const installed = await this.#installedDependencies(staging);
@@ -1077,33 +1087,38 @@ export class Project {
1077
1087
  delete installed[name];
1078
1088
  return { installed, ids, required };
1079
1089
  }
1080
- /** The db variant (from the staged @dokebi/db, else the CLI snapshot) for `--adapter`. */
1081
- async #adapterVariant(base, adapter) {
1090
+ /**
1091
+ * The variant of `--adapter` (from the staged package, else the CLI snapshot): a db adapter,
1092
+ * or the provider SDK of @dokebi/ai (D-0186). A plugin without adapters refuses the flag.
1093
+ */
1094
+ async #adapterVariant(base, adapter, packageName = '@dokebi/db') {
1082
1095
  let variants = [];
1083
1096
  try {
1084
- const pkg = parseObject(await readFile(await packageFile(base, '@dokebi/db', 'package.json'), 'utf8'), '@dokebi/db');
1097
+ const pkg = parseObject(await readFile(await packageFile(base, packageName, 'package.json'), 'utf8'), packageName);
1085
1098
  variants = pkg.dokebiCatalog?.variants ?? [];
1086
1099
  }
1087
1100
  catch (error) {
1088
1101
  if (!(error instanceof CliError) && !isMissing(error))
1089
1102
  throw error;
1090
- debug('Installed @dokebi/db has no readable catalog; using the CLI snapshot', {
1103
+ debug('Installed package has no readable catalog; using the CLI snapshot', {
1104
+ package: packageName,
1091
1105
  error: describeError(error),
1092
1106
  });
1093
1107
  }
1094
1108
  if (!variants.length)
1095
1109
  variants =
1096
- (await loadCatalog()).packages.find((item) => item.package === '@dokebi/db')?.variants ??
1097
- [];
1098
- const variant = variants.find((item) => item.adapter === adapter);
1099
- if (!variant)
1100
- throw new CliError('UNKNOWN_ADAPTER', `No database adapter "${adapter}"`, {
1101
- details: {
1102
- adapter,
1103
- adapters: variants.map((item) => item.adapter).filter(Boolean),
1104
- },
1110
+ (await loadCatalog()).packages.find((item) => item.package === packageName)?.variants ?? [];
1111
+ const adapters = variants.map((item) => item.adapter).filter(Boolean);
1112
+ if (!adapters.length)
1113
+ throw new CliError('USAGE', `--adapter applies to a plugin with adapters, not ${packageName}`, {
1114
+ details: { package: packageName, plugins: ['@dokebi/db', '@dokebi/ai'] },
1105
1115
  phase: 'arguments',
1106
1116
  });
1117
+ const variant = variants.find((item) => item.adapter === adapter);
1118
+ if (!variant)
1119
+ throw new CliError('UNKNOWN_ADAPTER', packageName === '@dokebi/db'
1120
+ ? `No database adapter "${adapter}"`
1121
+ : `No adapter "${adapter}" in ${packageName}`, { details: { adapter, package: packageName, adapters }, phase: 'arguments' });
1107
1122
  return { name: adapter, variant };
1108
1123
  }
1109
1124
  /** Adds the variant's driver (and its types) to the staged project and installs them. */
@@ -1125,7 +1140,7 @@ export class Project {
1125
1140
  }
1126
1141
  if (!changed)
1127
1142
  return;
1128
- debug('Installing the database driver', {
1143
+ debug('Installing the adapter driver', {
1129
1144
  driver: variant.driver?.package ?? null,
1130
1145
  types: variant.types?.package ?? null,
1131
1146
  });
@@ -2764,6 +2779,14 @@ export class Project {
2764
2779
  },
2765
2780
  phase: 'arguments',
2766
2781
  });
2782
+ if (value === undefined && ISSUED_KEY.test(path.at(-1) ?? ''))
2783
+ throw new CliError('INVALID_SET', `${where} is a key the provider issues, not a generated secret`, {
2784
+ details: {
2785
+ path: where,
2786
+ fix: `npx dokebi config secret ${where} --stdin (type it without echo, or < <file with the key>)`,
2787
+ },
2788
+ phase: 'arguments',
2789
+ });
2767
2790
  const validator = new PluginOptionsValidator({ allErrors: true });
2768
2791
  const rotated = current !== undefined && Array.isArray(current)
2769
2792
  ? await this.#rotatedList(session.root, validator, plugin, session.configSource, path, current, ...(value === undefined ? [] : [value]))
@@ -15,19 +15,25 @@ const HSTS_MIN = 15_552_000;
15
15
  function fetchOnce(url, timeoutMs, ca) {
16
16
  return new Promise((resolve, reject) => {
17
17
  const send = url.protocol === 'https:' ? httpsRequest : httpRequest;
18
+ // The whole answer, not only each pause, stays within the limit (a server that drips bytes).
19
+ const deadline = setTimeout(() => request.destroy(new Error(`no answer within ${timeoutMs} ms`)), timeoutMs);
18
20
  const request = send(url, {
19
21
  method: 'GET',
20
22
  headers: { 'user-agent': 'dokebi-verify-remote', accept: '*/*' },
21
23
  timeout: timeoutMs,
22
24
  ...(ca === undefined ? {} : { ca }),
23
25
  }, (response) => {
24
- // Only the status and headers matter; the body is read to the end and dropped.
25
- response.resume();
26
- response.once('end', () => resolve({ status: response.statusCode ?? 0, headers: response.headers }));
27
- response.once('error', reject);
26
+ // Only the status and headers matter: the body is never read (an event stream or a
27
+ // download at the URL would keep the check waiting), the connection is closed.
28
+ clearTimeout(deadline);
29
+ resolve({ status: response.statusCode ?? 0, headers: response.headers });
30
+ response.destroy();
28
31
  });
29
32
  request.once('timeout', () => request.destroy(new Error(`no answer within ${timeoutMs} ms`)));
30
- request.once('error', reject);
33
+ request.once('error', (error) => {
34
+ clearTimeout(deadline);
35
+ reject(error);
36
+ });
31
37
  request.end();
32
38
  });
33
39
  }
package/dist/server.d.ts CHANGED
@@ -108,7 +108,9 @@ export declare function checkProject(root: string): Promise<string>;
108
108
  * R1 T4 review: why a start failed, from the app log of this start only (after its last start
109
109
  * mark): the first record with an error, as `<code>: <first line>` and, when the error has
110
110
  * causes, `… <its last Caused by line>`. A START_FAILED said only "exited with code 1", and its
111
- * tail began with an earlier start's lines. Undefined when no record says.
111
+ * tail began with an earlier start's lines. A record below `error` that carries an error (a
112
+ * `warn` DB_CHECK_FAILED of a connection whose check failed) is named only when no record at
113
+ * `error` (or without a level) says (audit R8-g5b-1). Undefined when no record says.
112
114
  */
113
115
  export declare function startFailureCause(lines: readonly string[]): string | undefined;
114
116
  export interface LogOptions {
package/dist/server.js CHANGED
@@ -673,14 +673,19 @@ async function tail(file, lines, current) {
673
673
  }
674
674
  /** The line the log writer writes before each start (log-writer.ts). */
675
675
  const START_MARK = '--- dokebi start ';
676
+ /** Record levels below `error`: an error such a record carries did not stop the start. */
677
+ const LOWER_LEVELS = new Set(['trace', 'debug', 'info', 'warn']);
676
678
  /**
677
679
  * R1 T4 review: why a start failed, from the app log of this start only (after its last start
678
680
  * mark): the first record with an error, as `<code>: <first line>` and, when the error has
679
681
  * causes, `… <its last Caused by line>`. A START_FAILED said only "exited with code 1", and its
680
- * tail began with an earlier start's lines. Undefined when no record says.
682
+ * tail began with an earlier start's lines. A record below `error` that carries an error (a
683
+ * `warn` DB_CHECK_FAILED of a connection whose check failed) is named only when no record at
684
+ * `error` (or without a level) says (audit R8-g5b-1). Undefined when no record says.
681
685
  */
682
686
  export function startFailureCause(lines) {
683
687
  const from = lines.findLastIndex((line) => line.startsWith(START_MARK));
688
+ let lower;
684
689
  for (const line of lines.slice(from + 1)) {
685
690
  // Records are JSON lines; a stack line or a plain console line is not one.
686
691
  if (!line.startsWith('{'))
@@ -703,9 +708,12 @@ export function startFailureCause(lines) {
703
708
  const causes = text.split('\n').filter((each) => each.startsWith('Caused by: '));
704
709
  const deepest = causes.at(-1)?.slice('Caused by: '.length);
705
710
  const code = typeof record.code === 'string' ? `${record.code}: ` : '';
706
- return `${code}${first}${deepest ? ` … ${deepest}` : ''}`.slice(0, 600);
711
+ const said = `${code}${first}${deepest ? ` … ${deepest}` : ''}`.slice(0, 600);
712
+ if (!LOWER_LEVELS.has(String(record.level)))
713
+ return said;
714
+ lower ??= said;
707
715
  }
708
- return undefined;
716
+ return lower;
709
717
  }
710
718
  /** Rejects rotation settings outside LOG_LIMITS; bytes below 1 MB are allowed for tests. */
711
719
  function logSettings(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/cli",
3
- "version": "0.4.29",
3
+ "version": "0.4.31",
4
4
  "description": "Project scaffolding, plugin catalog, recipes, install and verification CLI for Dokebi.",
5
5
  "keywords": [
6
6
  "dokebi",