mppx 0.17.4 → 0.17.5

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.
@@ -0,0 +1,48 @@
1
+ /** Where a Stripe secret key came from, in resolution order. */
2
+ export type KeySource = 'flag' | 'env' | 'stripe-cli';
3
+ /** A resolved Stripe API key and its source. */
4
+ export type ResolvedKey = {
5
+ key: string;
6
+ source: KeySource;
7
+ };
8
+ /**
9
+ * Resolves the Stripe key for `mppx stripe address`: `--secret-key`, then `STRIPE_SECRET_KEY`,
10
+ * then the Stripe CLI's saved login for the current project.
11
+ */
12
+ export declare function resolveKey(options: resolveKey.Options): ResolvedKey | undefined;
13
+ export declare namespace resolveKey {
14
+ type Options = readStripeCliKey.Options & {
15
+ /** Value of `--secret-key`. */
16
+ secretKey?: string | undefined;
17
+ };
18
+ }
19
+ /**
20
+ * Reads the API key the Stripe CLI saved with `stripe login`, without running the CLI.
21
+ *
22
+ * Test mode keys live in `<config>/stripe/config.toml`. Live mode keys live in the OS keyring
23
+ * (service `StripeCLI`, account `<project>.live_mode_api_key`), or in `credentials.json` next to
24
+ * the config when no keyring was available.
25
+ */
26
+ export declare function readStripeCliKey(options: readStripeCliKey.Options): string | undefined;
27
+ export declare namespace readStripeCliKey {
28
+ type Options = {
29
+ /** Environment to read (default: `process.env`). */
30
+ env?: Record<string, string | undefined> | undefined;
31
+ /** Runs a system command and returns its stdout; replaced in tests. */
32
+ exec?: ((file: string, args: readonly string[]) => string) | undefined;
33
+ /** Read the live mode key instead of the test mode key. */
34
+ live?: boolean | undefined;
35
+ /** OS to read the keyring of (default: `process.platform`). */
36
+ platform?: NodeJS.Platform | undefined;
37
+ /** Stripe CLI project (`--project-name`); falls back to `STRIPE_PROJECT_NAME`, then `default`. */
38
+ projectName?: string | undefined;
39
+ };
40
+ }
41
+ /** Sets `key=value` in a dotenv file, replacing an existing assignment or appending one. */
42
+ export declare function writeEnvValue(file: string, key: string, value: string): void;
43
+ /**
44
+ * Gives a bare `--env` on `mppx stripe address` its default path, since incur flags can't take an
45
+ * optional value: `--env` last or followed by another flag becomes `--env .env`.
46
+ */
47
+ export declare function normalizeArgv(argv: readonly string[]): string[];
48
+ //# sourceMappingURL=stripe.d.ts.map
@@ -0,0 +1,157 @@
1
+ import { execFileSync } from 'node:child_process';
2
+ import * as fs from 'node:fs';
3
+ import * as os from 'node:os';
4
+ import * as path from 'node:path';
5
+ /** The Stripe CLI's keyring service name (stripe-cli pkg/config KeyManagementService). */
6
+ const keyringService = 'StripeCLI';
7
+ /**
8
+ * Resolves the Stripe key for `mppx stripe address`: `--secret-key`, then `STRIPE_SECRET_KEY`,
9
+ * then the Stripe CLI's saved login for the current project.
10
+ */
11
+ export function resolveKey(options) {
12
+ const env = options.env ?? process.env;
13
+ const flag = options.secretKey?.trim();
14
+ if (flag)
15
+ return { key: flag, source: 'flag' };
16
+ const fromEnv = env.STRIPE_SECRET_KEY?.trim();
17
+ if (fromEnv)
18
+ return { key: fromEnv, source: 'env' };
19
+ const fromCli = readStripeCliKey(options);
20
+ if (fromCli)
21
+ return { key: fromCli, source: 'stripe-cli' };
22
+ return undefined;
23
+ }
24
+ /**
25
+ * Reads the API key the Stripe CLI saved with `stripe login`, without running the CLI.
26
+ *
27
+ * Test mode keys live in `<config>/stripe/config.toml`. Live mode keys live in the OS keyring
28
+ * (service `StripeCLI`, account `<project>.live_mode_api_key`), or in `credentials.json` next to
29
+ * the config when no keyring was available.
30
+ */
31
+ export function readStripeCliKey(options) {
32
+ const env = options.env ?? process.env;
33
+ const directory = path.join(env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config'), 'stripe');
34
+ const project = options.projectName || env.STRIPE_PROJECT_NAME || 'default';
35
+ const profile = readProfile(path.join(directory, 'config.toml'), project);
36
+ if (!options.live)
37
+ return profile.test_mode_api_key || profile.secret_key || profile.api_key || undefined;
38
+ // Older CLIs kept the live key in config.toml; newer ones leave a redacted copy there.
39
+ const inConfig = profile.live_mode_api_key;
40
+ if (inConfig && !inConfig.includes('*'))
41
+ return inConfig;
42
+ const account = `${project}.live_mode_api_key`;
43
+ const fromFile = readJson(path.join(directory, 'credentials.json'))?.[account];
44
+ if (typeof fromFile === 'string' && fromFile)
45
+ return fromFile;
46
+ return readKeyring(account, options);
47
+ }
48
+ /** Reads a keyring entry with the OS's own tool (macOS `security`, Linux `secret-tool`). */
49
+ function readKeyring(account, options) {
50
+ const platform = options.platform ?? process.platform;
51
+ const exec = options.exec ?? execSystem;
52
+ const command = (() => {
53
+ if (platform === 'darwin')
54
+ return [
55
+ 'security',
56
+ ['find-generic-password', '-s', keyringService, '-a', account, '-w'],
57
+ ];
58
+ if (platform === 'linux')
59
+ return ['secret-tool', ['lookup', 'service', keyringService, 'username', account]];
60
+ return undefined;
61
+ })();
62
+ if (!command)
63
+ return undefined;
64
+ try {
65
+ const value = exec(command[0], command[1]).trim();
66
+ // go-keyring stores macOS values base64-encoded behind this prefix.
67
+ const prefix = 'go-keyring-base64:';
68
+ if (value.startsWith(prefix))
69
+ return Buffer.from(value.slice(prefix.length), 'base64').toString('utf8') || undefined;
70
+ return value || undefined;
71
+ }
72
+ catch {
73
+ return undefined;
74
+ }
75
+ }
76
+ function execSystem(file, args) {
77
+ return execFileSync(file, args, {
78
+ encoding: 'utf8',
79
+ stdio: ['ignore', 'pipe', 'ignore'],
80
+ timeout: 3_000,
81
+ });
82
+ }
83
+ function readJson(file) {
84
+ try {
85
+ return JSON.parse(fs.readFileSync(file, 'utf8'));
86
+ }
87
+ catch {
88
+ return undefined;
89
+ }
90
+ }
91
+ /**
92
+ * Reads one profile's string values from the Stripe CLI's config.toml: the `[<project>]` table, or
93
+ * `[profiles.<project>]` in the CLI's newer layout. Handles only the flat string keys the CLI writes.
94
+ */
95
+ function readProfile(file, project) {
96
+ let text;
97
+ try {
98
+ text = fs.readFileSync(file, 'utf8');
99
+ }
100
+ catch {
101
+ return {};
102
+ }
103
+ const tables = new Set([project, `profiles.${project}`]);
104
+ const values = {};
105
+ let current;
106
+ for (const raw of text.split(/\r?\n/)) {
107
+ const line = raw.trim();
108
+ if (!line || line.startsWith('#'))
109
+ continue;
110
+ const table = /^\[\s*([^\]]+?)\s*\]$/.exec(line);
111
+ if (table) {
112
+ current = table[1].replace(/["']/g, '').replace(/\s*\.\s*/g, '.');
113
+ continue;
114
+ }
115
+ if (!current || !tables.has(current))
116
+ continue;
117
+ const pair = /^([A-Za-z0-9_-]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|'([^']*)')/.exec(line);
118
+ if (pair)
119
+ values[pair[1]] = pair[2] !== undefined ? pair[2].replace(/\\(.)/g, '$1') : pair[3];
120
+ }
121
+ return values;
122
+ }
123
+ /** Sets `key=value` in a dotenv file, replacing an existing assignment or appending one. */
124
+ export function writeEnvValue(file, key, value) {
125
+ const line = `${key}=${value}`;
126
+ if (!fs.existsSync(file))
127
+ return fs.writeFileSync(file, `${line}\n`, { mode: 0o600 });
128
+ const existing = fs.readFileSync(file, 'utf8');
129
+ const pattern = new RegExp(`^(?:export\\s+)?${key}\\s*=.*$`, 'm');
130
+ if (pattern.test(existing))
131
+ return fs.writeFileSync(file, existing.replace(pattern, () => line));
132
+ const separator = existing === '' || existing.endsWith('\n') ? '' : '\n';
133
+ fs.writeFileSync(file, `${existing}${separator}${line}\n`);
134
+ }
135
+ /**
136
+ * Gives a bare `--env` on `mppx stripe address` its default path, since incur flags can't take an
137
+ * optional value: `--env` last or followed by another flag becomes `--env .env`.
138
+ */
139
+ export function normalizeArgv(argv) {
140
+ const command = argv.findIndex((token, i) => token === 'stripe' && argv[i + 1] === 'address');
141
+ if (command === -1)
142
+ return [...argv];
143
+ const result = argv.slice(0, command + 2);
144
+ for (let i = command + 2; i < argv.length; i++) {
145
+ const token = argv[i];
146
+ result.push(token);
147
+ if (token === '--') {
148
+ result.push(...argv.slice(i + 1));
149
+ break;
150
+ }
151
+ const next = argv[i + 1];
152
+ if (token === '--env' && (next === undefined || next.startsWith('-')))
153
+ result.push('.env');
154
+ }
155
+ return result;
156
+ }
157
+ //# sourceMappingURL=stripe.js.map
@@ -1,5 +1,5 @@
1
1
  /** Current mppx package version. */
2
- export declare const version = "0.17.4";
2
+ export declare const version = "0.17.5";
3
3
  /** Canonical SDK identifier for this mppx release. */
4
- export declare const sdkIdentifier = "mppx/0.17.4";
4
+ export declare const sdkIdentifier = "mppx/0.17.5";
5
5
  //# sourceMappingURL=version.d.ts.map
@@ -1,6 +1,6 @@
1
1
  // Generated by scripts/sync-version.ts.
2
2
  /** Current mppx package version. */
3
- export const version = '0.17.4';
3
+ export const version = '0.17.5';
4
4
  /** Canonical SDK identifier for this mppx release. */
5
5
  export const sdkIdentifier = `mppx/${version}`;
6
6
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mppx",
3
3
  "type": "module",
4
- "version": "0.17.4",
4
+ "version": "0.17.5",
5
5
  "main": "./dist/index.js",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/wevm/mppx#readme",