Skip to content

@saflib/node


index ​

Classes ​

ClassDescription
AssertionError-
AssertionExpiredError-
AssertionKeysConfigError-
AssertionMalformedError-
AssertionSignatureError-
AssertionTtlExceededError-
AssertionUnknownKeyError-

Interfaces ​

InterfaceDescription
AuthAuth object passed in with every authenticated request.
ErrorCollectorParamParameters provided to error collectors.
ErrorReportOptionsSubset of properties given to Sentry or similar error reporting services. https://docs.sentry.io/platforms/javascript/guides/node/apis/#captureException
IdentityAssertion-
LoggerOptionsContext to give for a logger, which doesn't include properties that are global.
SafContextStatic, serializable context about what's currently going on. These should always be available in backend systems.
SafContextWithAuthConvenience type for when the context needs to have auth.
SafReportersClients for reporting various sorts of telemetry. They're expected to be instantiated with a SafContext, so that context is included.

Type Aliases ​

Type AliasDescription
ErrorCollectorCollectors take errors reported to them and propagate them to telemetry services such as Sentry.
ErrorLevelsTaken from Sentry, excluding "log" which seems superfluous and vague
ErrorReporterThe function that application logic has access to for reporting errors.
LoggerContextContext to give for a logger, which doesn't include properties that are global.
SubsystemNameList of allowed subsystem names to be used as context for logging and such.

Variables ​

VariableDescription
defaultErrorReporterDefault ErrorReporter; call addErrorCollector with this to use it.
HTTP_ACCESS_DURATION_WIDTHFixed 6-char duration ( 325ms / 72.2s).
HTTP_ACCESS_LARGE_BYTESResponse body at or above this size (bytes) is highlighted when ANSI styling is on.
HTTP_ACCESS_SIZE_WIDTHFixed 6-char response size; kb when > 1024 bytes.
HTTP_ACCESS_SLOW_MSAccess-log duration above this (ms) is highlighted when ANSI styling is on.
HTTP_CHANNEL_CLIENT_PLAINBrowser/client traffic — heavy bar, arrow leans left (inbound from the edge). Unicode: BLACK LEFT-POINTING POINTER + HEAVY HORIZONTAL.
HTTP_CHANNEL_INTERNAL_PLAINInternal background traffic — light bar, arrow leans right (outbound dispatch). Unicode: LIGHT HORIZONTAL + RIGHT-POINTING SMALL TRIANGLE.
HTTP_CHANNEL_MARKER_WIDTHFixed visual width for the HTTP channel marker column.
metricHistogramDefaultBuckets-
SAF_INTERNAL_ASSERTION_KEYS_NAME-
safContextStorageStorage for SafContext.
safReportersStorageAsyncLocalStorage for SafReporters.
testContextContext provided during testing.

Functions ​

FunctionDescription
addErrorCollectorAdds a callback for when errors are reported by the application.
addTransportFor production, when the application starts, it should add any transports using this function. Then all SAF-based applications will log to winston and they'll propagate to loggers such as Loki.
collectSystemMetricsCall when the application starts. Calls prom-client's collectDefaultMetrics function under the hood.
configureInternalAssertionKeysLoads HMAC assertion keys from the secret store when not set in env (prod). Dev sets SAF_INTERNAL_ASSERTION_KEYS in env; job tests stub env directly. Prod-local with INFISICAL_TOKEN=mock gets the placeholder "mock", which signAssertion / verifyAssertion accept as a fixed local key.
createLoggerCreates a child logger with the specified request ID. Any servers or processors should use this to create a unique logger for each request or job or what have you. However, if not "instantiating" the request, you should use the request ID provided by the caller, such as in the proto envelope, so that requests which span processes can be correlated.
createSilentLoggerCreate a logger that doesn't print anything.
formatCompactTimestampShared compact log timestamp: MM-DD HH:mm (UTC when TZ=UTC).
formatHttpAccessLine-
formatHttpDurationMsFixed 6-char duration for access-log columns. Under 10s: 325ms. At/above 10s: 72.2s / 999s so long requests are never left-truncated (e.g. 72233ms must not become 2233ms).
formatHttpMethodFixed 5-char HTTP method so paths align (GET vs PATCH).
formatHttpResponseSizeFixed 6-char response size; kb when > 1024 bytes.
formatHttpStatusFixed 3-char HTTP status for access-log alignment.
generateRequestIdGenerates a request ID. Only necessary for "requests" which are not initiated by proxy servers, such as for cron or async jobs.
getErrorCollectors-
getSafContextConvenience function for getting SafContext store. Errors if not found, returns testContext if in test mode.
getSafContextWithAuthConvenience function for getting SafContext store with auth. Errors if either the store is not found, or auth is not included.
getSafReportersConvenience method for getting the SafReporters from the storage. Errors if not found.
getServiceNameGetter for service name.
httpAccessAnsiEnabled-
httpChannelIndicator-
makeSubsystemErrorReporterDuring setup, subsystems should use this to create their own set of reporters. "Operation name" should be the name of the function.
makeSubsystemReportersCreates a new SafReporters object for a given subsystem and operation.
runWithActingUserRun fn with auth.userId set on the current SafContext. Use when a request is anonymous (webhooks, jobs) but should be attributed to the user who created the related resource for logging, analytics, and error reporting.
setServiceNameSets the service name. Should be called as soon as the process starts. This is provided in SafContext and to instrumentation.
signAssertionSigns an identity assertion.
verifyAssertionVerifies an identity assertion token against any configured key.

References ​

getGitHashes ​

Re-exports getGitHashes


GitHashes ​

Re-exports GitHashes


NodeEnvSchema ​

Re-exports NodeEnvSchema


typedEnv ​

Re-exports typedEnv