fromEnvironment static method

DVPreviewRuntime? fromEnvironment(
  1. Map<String, String> environment
)

The preview this process is, or null when it is not one.

Reads what DVPreviewDeployment.variables writes. A preview whose settings cannot be read is refused rather than served under a guess: the guess that looks like working is the one that admits everybody.

Implementation

static DVPreviewRuntime? fromEnvironment(Map<String, String> environment) {
  if (environment['DARTVEL_ENVIRONMENT'] != dvPreviewEnvironment) return null;
  final String? name = environment['DARTVEL_PREVIEW'];
  if (name == null || name.isEmpty) {
    throw const FormatException(
      'DARTVEL_ENVIRONMENT is preview and DARTVEL_PREVIEW names no preview',
    );
  }
  final String? rawVisibility = environment['DARTVEL_PREVIEW_VISIBILITY'];
  final DVPreviewVisibility visibility = DVPreviewVisibility.values.firstWhere(
    (DVPreviewVisibility v) => v.name == rawVisibility,
    orElse: () => throw FormatException(
      'DARTVEL_PREVIEW_VISIBILITY must be one of '
      '${DVPreviewVisibility.values.map((DVPreviewVisibility v) => v.name).join(' | ')}, '
      'got $rawVisibility',
    ),
  );
  final String? digest = environment['DARTVEL_PREVIEW_LINK_DIGEST'];
  if (visibility == DVPreviewVisibility.link &&
      (digest == null || !RegExp(r'^[0-9a-f]+$').hasMatch(digest))) {
    throw const FormatException(
      'a link preview needs DARTVEL_PREVIEW_LINK_DIGEST, the digest of its '
      'token; without it no link could be checked',
    );
  }
  final String rawSchedules = environment['DARTVEL_PREVIEW_SCHEDULES'] ?? '';
  return DVPreviewRuntime(
    name: name,
    visibility: visibility,
    linkDigest: digest,
    productionOrigin: environment['DARTVEL_PRODUCTION_ORIGIN'],
    schedules: <String>{
      for (final String s in rawSchedules.split(','))
        if (s.trim().isNotEmpty) s.trim(),
    },
  );
}