Function Signature
Parameters
The environment variable name that points to the secret file path
Returns
Promise<Date | undefined> - A Date object from the secret file, or undefined if not found
How It Works
This is the optional variant ofsecretDate. It reads secrets from the filesystem without throwing errors when the file is missing:
- Checks if the environment variable
keycontains a file path - If no path is set, defaults to
/run/secrets/{key} - Reads the file contents and parses it as a Date
- Returns
undefinedif the secret file doesn’t exist (instead of throwing) - Still throws an error if the file exists but contains an invalid date
Difference from secretDate
- maybeSecretDate: Returns
undefinedwhen the secret file is missing - secretDate: Throws an error when the secret file is missing (unless a fallback is provided)
maybeSecretDate for truly optional date secrets where absence is a valid state.
Example
Error Handling
Returnsundefined if:
- The secret file doesn’t exist
- The file is empty
- The file exists but cannot be parsed as a valid Date
