< Summary - Envilder Node.js SDK

Information
Class: src/sdks/nodejs/src/domain/environment-variable-name.ts
Assembly: Default
File(s): src/sdks/nodejs/src/domain/environment-variable-name.ts
Tag: 558_41035824231
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 52
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/sdks/nodejs/src/domain/environment-variable-name.ts

#LineLine coverage
 1/**
 2 * Which strings are usable as environment variable names, per the map-file
 3 * naming constraint in ADR-0008: an accepted name must survive a `.env`
 4 * round-trip as itself and nothing else. Anything outside `^[A-Za-z0-9_./-]+$`
 5 * is either read back as a different assignment (`=`, CR, LF, U+2028, U+2029)
 6 * or silently lost (a space, `#`, a tab, a non-Latin letter). `/` stays
 7 * valid: it is the hierarchy separator map files have always used
 8 * (`Database/Password`).
 9 *
 10 * `__proto__` is inside the allowlist but every reader that accumulates pairs
 11 * into a plain object drops it, so it is rejected as well.
 12 */
 413const READABLE_NAME = /^[A-Za-z0-9_./-]+$/;
 14
 415const UNSUPPORTED_NAMES = new Set(['__proto__']);
 16
 17export function isValidEnvironmentVariableName(name: string): boolean {
 6518  return READABLE_NAME.test(name) && !UNSUPPORTED_NAMES.has(name);
 19}
 20
 21/**
 22 * Builds the rejection message. The name is untrusted input, so it is quoted
 23 * rather than interpolated: a name carrying line terminators cannot break the
 24 * message across lines. Only the name is echoed, never the mapped value.
 25 */
 26export function invalidEnvironmentVariableNameMessage(name: string): string {
 3527  if (name.trim() === '') {
 428    return 'Environment variable name cannot be empty';
 29  }
 30
 3131  if (UNSUPPORTED_NAMES.has(name)) {
 232    return (
 33      `Unsupported environment variable name ${quoteName(name)}: ` +
 34      'a .env parser cannot read this name back, so it would be written ' +
 35      'but never resolved'
 36    );
 37  }
 38
 2939  return (
 40    `Invalid environment variable name ${quoteName(name)}: names may only ` +
 41    'contain letters, digits, underscore, dot, hyphen and slash, so that ' +
 42    'they can be read back from a .env file'
 43  );
 44}
 45
 46function quoteName(name: string): string {
 47  // JSON.stringify escapes CR, LF and the other control characters, but leaves
 48  // U+2028/U+2029 literal even though they terminate a line in JavaScript.
 3149  return JSON.stringify(name)
 50    .replace(/\u{2028}/gu, '\\u2028')
 51    .replace(/\u{2029}/gu, '\\u2029');
 52}