@deepraxis/sdk 0.1.1-dev.877a000 → 0.1.1-dev.8ab3c22

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.
@@ -1,9 +1,12 @@
1
1
  /**
2
- * Default credential chain for code running on a Praxis computer (ticket-5367, ADR-098 §2):
3
- * the PRAXIS_API_TOKEN environment variable, then the login file of the environment,
4
- * `~/.praxis/cli/<env>.json` — the one file `praxis login` writes for a person and the platform writes
5
- * for a managed login. Resolved at call time so a token the platform renewed in the file is picked up
6
- * by an already-running process.
2
+ * Default credential chain (ticket-5367, ADR-098 §2, ADR-157), the same rule as the `praxis` CLI:
3
+ * 1. the PRAXIS_API_TOKEN environment variable;
4
+ * 2. PRAXIS_CWD set: `$PRAXIS_CWD/.praxis/credentials/<env>.json` and nothing else — no file there means no token,
5
+ * never the home's login and never a parent directory's;
6
+ * 3. otherwise the home: `~/.praxis/credentials/<env>.json`, and while that file is absent the legacy
7
+ * `~/.praxis/cli/<env>.json` that `praxis login` wrote before ADR-157 and the platform still writes for a
8
+ * managed login.
9
+ * Resolved at call time so a token the platform renewed in the file is picked up by an already-running process.
7
10
  */
8
11
  export interface ResolvedCredentials {
9
12
  token?: string;
@@ -17,7 +20,14 @@ type ReadFile = (path: string) => string | null;
17
20
  * applies. A PRAXIS_ENV that is not a plain name is no environment at all and is ignored. Pure.
18
21
  */
19
22
  export declare function credentialsEnv(env: Env): string;
20
- /** The one login file, the same path the CLI reads for this environment. Null without a home directory. Pure. */
23
+ /** The credential file of one directory: `<dir>/.praxis/credentials/<env>.json`. Pure. */
24
+ export declare function credentialsFileIn(dir: string, env: Env): string;
25
+ /**
26
+ * The credential files this process reads, in order (ADR-157): a PRAXIS_CWD directory is the only one when named;
27
+ * otherwise the home's file and then its legacy login file. Empty without a home directory. Pure.
28
+ */
29
+ export declare function credentialsPaths(env: Env): string[];
30
+ /** The first file this process would read (the one `praxis login` writes). Null without a home directory. Pure. */
21
31
  export declare function credentialsPath(env: Env): string | null;
22
32
  export declare function resolveDefaultCredentials(env?: Env, readFile?: ReadFile): ResolvedCredentials;
23
33
  export {};
@@ -27,18 +27,37 @@ export function credentialsEnv(env) {
27
27
  }
28
28
  return 'production';
29
29
  }
30
- /** The one login file, the same path the CLI reads for this environment. Null without a home directory. Pure. */
31
- export function credentialsPath(env) {
30
+ const trimSlashes = (path) => path.replace(/[/\\]+$/, '') || path;
31
+ /** The credential file of one directory: `<dir>/.praxis/credentials/<env>.json`. Pure. */
32
+ export function credentialsFileIn(dir, env) {
33
+ return `${trimSlashes(dir)}/.praxis/credentials/${credentialsEnv(env)}.json`;
34
+ }
35
+ /**
36
+ * The credential files this process reads, in order (ADR-157): a PRAXIS_CWD directory is the only one when named;
37
+ * otherwise the home's file and then its legacy login file. Empty without a home directory. Pure.
38
+ */
39
+ export function credentialsPaths(env) {
40
+ const named = nonEmpty(env.PRAXIS_CWD);
41
+ if (named)
42
+ return [credentialsFileIn(named, env)];
32
43
  const home = nonEmpty(env.HOME) ?? nonEmpty(env.USERPROFILE);
33
- return home ? `${home.replace(/[/\\]+$/, '')}/.praxis/cli/${credentialsEnv(env)}.json` : null;
44
+ return home ? [credentialsFileIn(home, env), `${trimSlashes(home)}/.praxis/cli/${credentialsEnv(env)}.json`] : [];
45
+ }
46
+ /** The first file this process would read (the one `praxis login` writes). Null without a home directory. Pure. */
47
+ export function credentialsPath(env) {
48
+ return credentialsPaths(env)[0] ?? null;
34
49
  }
35
50
  export function resolveDefaultCredentials(env = globalThis.process?.env ?? {}, readFile = defaultReadFile) {
36
51
  const envToken = nonEmpty(env.PRAXIS_API_TOKEN);
37
52
  const envUrl = nonEmpty(env.PRAXIS_API_URL);
38
53
  if (envToken)
39
54
  return { token: envToken, ...(envUrl ? { apiUrl: envUrl } : {}) };
40
- const path = credentialsPath(env);
41
- const text = path ? readFile(path) : null;
55
+ let text = null;
56
+ for (const path of credentialsPaths(env)) {
57
+ text = readFile(path);
58
+ if (text)
59
+ break;
60
+ }
42
61
  if (!text)
43
62
  return envUrl ? { apiUrl: envUrl } : {};
44
63
  try {