UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

138 lines 4.51 kB
import { redactValue } from "../ports/redaction.js"; function isObject(value) { return typeof value === "object" && value !== null; } /** * Return whether a value is a provider instrumentation helper. * * Checks key presence with `in` before reading so probing proxy-backed * objects, such as test-context port guards that throw on unknown reads, * stays safe. */ export function isProviderInstrumentation(value) { return (isObject(value) && "record" in value && "custom" in value && "isEnabled" in value && typeof value.record === "function" && typeof value.custom === "function" && typeof value.isEnabled === "function"); } /** * Return whether a value is a provider instrumentation port. * * Checks key presence with `in` before reading so probing proxy-backed * objects stays safe. */ export function isProviderInstrumentationPort(value) { return (isObject(value) && "record" in value && typeof value.record === "function"); } function resolveProviderInstrumentationValue(value) { if (isProviderInstrumentation(value)) return value.port; if (isProviderInstrumentationPort(value)) return value; return undefined; } /** * Resolve an instrumentation port from a direct port, helper, or context-like * object. */ export function resolveProviderInstrumentationPort(target) { if (!target) return undefined; const directPort = resolveProviderInstrumentationValue(target); if (directPort) return directPort; if (!isObject(target)) return undefined; return (resolveProviderInstrumentationValue("instrumentation" in target ? target.instrumentation : undefined) ?? resolveProviderInstrumentationValue("devtools" in target ? target.devtools : undefined)); } function withProviderDetails(providerName, details) { if (details === undefined) { return { providerName }; } if (isObject(details) && !Array.isArray(details)) { return { ...details, providerName }; } return { providerName, value: details }; } function withInstrumentationProviderDetails(event, providerName) { if (event.type === "provider") return event; return { ...event, details: withProviderDetails(providerName, event.details), }; } /** * Create a provider instrumentation helper that handles watcher checks, * default watcher assignment, redaction, and sink failures. */ export function createProviderInstrumentation(target, options) { if (isProviderInstrumentation(target)) { return target; } const port = resolveProviderInstrumentationPort(target); function isEnabled(watcher = options.watcher) { if (!port) return false; if (!watcher) return true; try { return port.isWatcherEnabled?.(watcher) ?? true; } catch { return false; } } function record(event) { if (!port) return undefined; const watcher = event.watcher ?? options.watcher; if (watcher && !isEnabled(watcher)) return undefined; const eventWithWatcher = options.watcher && event.watcher === undefined ? { ...event, watcher: options.watcher } : event; const eventWithProvider = withInstrumentationProviderDetails(eventWithWatcher, options.providerName); let redacted; try { redacted = options.redact ? options.redact(redactValue(eventWithProvider)) : redactValue(eventWithProvider); } catch { return undefined; } try { const result = port.record(redacted); if (result !== null && (typeof result === "object" || typeof result === "function") && "then" in result && typeof result.then === "function") { return Promise.resolve(result).catch(() => undefined); } return result; } catch { return undefined; } } function custom(event) { return record({ ...event, type: "custom", watcher: event.watcher ?? options.watcher, details: withProviderDetails(options.providerName, event.details), }); } return { port, isEnabled, record, custom, }; } //# sourceMappingURL=instrumentation.js.map