-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmobify-source.ts
More file actions
97 lines (86 loc) · 3.07 KB
/
mobify-source.ts
File metadata and controls
97 lines (86 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* Mobify (~/.mobify) configuration source for MRT API key.
*
* @internal This module is internal to the SDK. Use ConfigResolver instead.
*/
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import type {ConfigSource, ConfigLoadResult, ResolveConfigOptions} from '../types.js';
import {getLogger} from '../../logging/logger.js';
/**
* Mobify config file structure (~/.mobify)
*/
interface MobifyConfigFile {
username?: string;
api_key?: string;
}
/**
* Configuration source that loads MRT API key from ~/.mobify file.
*
* The mobify config file is a JSON file located at ~/.mobify containing:
* ```json
* {
* "username": "user@example.com",
* "api_key": "your-api-key"
* }
* ```
*
* When a cloudOrigin is provided in options, looks for ~/.mobify--[hostname] instead.
*
* @internal
*/
export class MobifySource implements ConfigSource {
readonly name = 'MobifySource';
readonly priority = 0;
load(options: ResolveConfigOptions): ConfigLoadResult | undefined {
const logger = getLogger();
// Use explicit credentialsFile if provided, otherwise use default path
const mobifyPath = options.credentialsFile ?? this.getMobifyPath(options.cloudOrigin);
logger.trace({location: mobifyPath}, '[MobifySource] Checking for credentials file');
if (!fs.existsSync(mobifyPath)) {
logger.trace({location: mobifyPath}, '[MobifySource] No credentials file found');
return undefined;
}
try {
const content = fs.readFileSync(mobifyPath, 'utf8');
const config = JSON.parse(content) as MobifyConfigFile;
if (!config.api_key) {
logger.trace({location: mobifyPath}, '[MobifySource] Credentials file found but no api_key present');
return undefined;
}
logger.trace({location: mobifyPath, fields: ['mrtApiKey']}, '[MobifySource] Loaded credentials');
return {
config: {mrtApiKey: config.api_key},
location: mobifyPath,
};
} catch (error) {
// Invalid JSON or read error - log at trace level and re-throw
// The resolver will catch this and create a SOURCE_ERROR warning
const message = error instanceof Error ? error.message : String(error);
logger.trace({location: mobifyPath, error: message}, '[MobifySource] Failed to parse credentials file');
throw error;
}
}
/**
* Determines the mobify config file path based on cloud origin.
*/
private getMobifyPath(cloudOrigin?: string): string {
if (cloudOrigin) {
// Extract hostname from origin URL for the config file suffix
try {
const url = new URL(cloudOrigin);
return path.join(os.homedir(), `.mobify--${url.hostname}`);
} catch {
// If URL parsing fails, use the origin as-is
return path.join(os.homedir(), `.mobify--${cloudOrigin}`);
}
}
return path.join(os.homedir(), '.mobify');
}
}