< Summary - Envilder CLI

Information
Class: src/envilder/core/domain/ChildEnvironment.ts
Assembly: Default
File(s): src/envilder/core/domain/ChildEnvironment.ts
Tag: 561_41292037308
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/domain/ChildEnvironment.ts

#LineLine coverage
 1export type ChildEnvironment = {
 2  /** Complete environment for the child process. */
 3  env: Record<string, string>;
 4  /** Names of inherited variables replaced by a resolved one. */
 5  overridden: string[];
 6};
 7
 8/**
 9 * Composes the environment of an exec child: the inherited environment plus
 10 * the resolved secrets, where the map file wins on conflicts.
 11 *
 12 * On Windows environment names are case-insensitive, so an inherited `Path`
 13 * and a mapped `PATH` are the same variable and the child must get only one.
 14 * Values are copied as-is: no escaping, quoting or trimming.
 15 */
 16export function buildChildEnvironment(
 17  inherited: Readonly<Record<string, string | undefined>>,
 18  resolved: Readonly<Record<string, string>>,
 19  platform: string,
 20): ChildEnvironment {
 21  const normalize =
 6122    platform === 'win32'
 1223      ? (name: string) => name.toUpperCase()
 397824      : (name: string) => name;
 6125  const resolvedNames = new Set(Object.keys(resolved).map(normalize));
 26
 27  // Null prototype: an inherited variable named `__proto__` must stay an own
 28  // property instead of going through the Object.prototype setter.
 6129  const env: Record<string, string> = Object.create(null);
 6130  const overridden: string[] = [];
 6131  for (const [name, value] of Object.entries(inherited)) {
 388232    if (value === undefined) {
 133      continue;
 34    }
 388135    if (resolvedNames.has(normalize(name))) {
 836      overridden.push(name);
 837      continue;
 38    }
 387339    env[name] = value;
 40  }
 6141  for (const [name, value] of Object.entries(resolved)) {
 10942    env[name] = value;
 43  }
 44
 6145  return { env, overridden };
 46}
 47
 48/**
 49 * Groups of variable names that the platform treats as one variable. On
 50 * Windows names are case-insensitive, so `Path` and `PATH` collide; on other
 51 * platforms there are never collisions.
 52 */
 53export function findCaseCollisions(
 54  names: readonly string[],
 55  platform: string,
 56): string[][] {
 7657  if (platform !== 'win32') {
 7358    return [];
 59  }
 360  const groups = new Map<string, string[]>();
 361  for (const name of names) {
 562    const key = name.toUpperCase();
 563    groups.set(key, [...(groups.get(key) ?? []), name]);
 64  }
 365  return [...groups.values()].filter((group) => group.length > 1);
 66}