|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | +/** |
| 7 | + * package.json configuration source. |
| 8 | + * |
| 9 | + * Reads configuration from the `b2c` key in package.json. |
| 10 | + * Only loads from cwd (project root), not from parent directories. |
| 11 | + * |
| 12 | + * @internal This module is internal to the SDK. Use ConfigResolver instead. |
| 13 | + */ |
| 14 | +import * as fs from 'node:fs'; |
| 15 | +import * as path from 'node:path'; |
| 16 | +import type {ConfigSource, ConfigLoadResult, ResolveConfigOptions, NormalizedConfig} from '../types.js'; |
| 17 | +import {getPopulatedFields} from '../mapping.js'; |
| 18 | +import {getLogger} from '../../logging/logger.js'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Fields allowed to be configured in package.json. |
| 22 | + * These are non-sensitive, non-instance-specific configuration. |
| 23 | + */ |
| 24 | +const ALLOWED_FIELDS: (keyof NormalizedConfig)[] = [ |
| 25 | + 'shortCode', |
| 26 | + 'clientId', |
| 27 | + 'mrtProject', |
| 28 | + 'mrtOrigin', |
| 29 | + 'accountManagerHost', |
| 30 | +]; |
| 31 | + |
| 32 | +/** |
| 33 | + * Structure of the b2c config in package.json |
| 34 | + */ |
| 35 | +interface PackageJsonB2CConfig { |
| 36 | + shortCode?: string; |
| 37 | + clientId?: string; |
| 38 | + mrtProject?: string; |
| 39 | + mrtOrigin?: string; |
| 40 | + accountManagerHost?: string; |
| 41 | + [key: string]: unknown; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Configuration source that loads from package.json `b2c` key. |
| 46 | + * |
| 47 | + * This source has the lowest priority (1000) and only provides |
| 48 | + * non-sensitive, project-level defaults. |
| 49 | + * |
| 50 | + * @internal |
| 51 | + */ |
| 52 | +export class PackageJsonSource implements ConfigSource { |
| 53 | + readonly name = 'PackageJsonSource'; |
| 54 | + readonly priority = 1000; |
| 55 | + |
| 56 | + load(options: ResolveConfigOptions): ConfigLoadResult | undefined { |
| 57 | + const logger = getLogger(); |
| 58 | + |
| 59 | + // Only look in cwd (or startDir if provided) |
| 60 | + const searchDir = options.startDir ?? process.cwd(); |
| 61 | + const packageJsonPath = path.join(searchDir, 'package.json'); |
| 62 | + |
| 63 | + logger.trace({location: packageJsonPath}, '[PackageJsonSource] Checking for package.json'); |
| 64 | + |
| 65 | + if (!fs.existsSync(packageJsonPath)) { |
| 66 | + logger.trace('[PackageJsonSource] No package.json found'); |
| 67 | + return undefined; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + const content = fs.readFileSync(packageJsonPath, 'utf8'); |
| 72 | + const packageJson = JSON.parse(content) as {b2c?: PackageJsonB2CConfig}; |
| 73 | + |
| 74 | + if (!packageJson.b2c) { |
| 75 | + logger.trace('[PackageJsonSource] No b2c key in package.json'); |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + |
| 79 | + const b2cConfig = packageJson.b2c; |
| 80 | + const config: NormalizedConfig = {}; |
| 81 | + |
| 82 | + // Only copy allowed fields |
| 83 | + for (const field of ALLOWED_FIELDS) { |
| 84 | + const value = b2cConfig[field]; |
| 85 | + if (value !== undefined) { |
| 86 | + (config as Record<string, unknown>)[field] = value; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Warn about disallowed fields |
| 91 | + const disallowedFields = Object.keys(b2cConfig).filter( |
| 92 | + (key) => !ALLOWED_FIELDS.includes(key as keyof NormalizedConfig), |
| 93 | + ); |
| 94 | + if (disallowedFields.length > 0) { |
| 95 | + logger.warn( |
| 96 | + {disallowedFields}, |
| 97 | + '[PackageJsonSource] Ignoring sensitive/instance-specific fields in package.json b2c config', |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const fields = getPopulatedFields(config); |
| 102 | + if (fields.length === 0) { |
| 103 | + logger.trace('[PackageJsonSource] b2c key present but no allowed fields populated'); |
| 104 | + return undefined; |
| 105 | + } |
| 106 | + |
| 107 | + logger.trace({location: packageJsonPath, fields}, '[PackageJsonSource] Loaded config'); |
| 108 | + |
| 109 | + return {config, location: packageJsonPath}; |
| 110 | + } catch (error) { |
| 111 | + const message = error instanceof Error ? error.message : String(error); |
| 112 | + logger.trace({location: packageJsonPath, error: message}, '[PackageJsonSource] Failed to parse package.json'); |
| 113 | + return undefined; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments