@entifix/service-shell 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/dist/auth/failures.d.ts +0 -12
- package/dist/auth/failures.d.ts.map +1 -1
- package/dist/auth/failures.js +0 -10
- package/dist/auth/failures.js.map +1 -1
- package/dist/auth/index.d.ts +0 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +0 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/require-principal.d.ts +0 -16
- package/dist/auth/require-principal.d.ts.map +1 -1
- package/dist/auth/require-principal.js +1 -38
- package/dist/auth/require-principal.js.map +1 -1
- package/dist/auth/require-service-crossing.d.ts +2 -73
- package/dist/auth/require-service-crossing.d.ts.map +1 -1
- package/dist/auth/require-service-crossing.js +2 -117
- package/dist/auth/require-service-crossing.js.map +1 -1
- package/dist/auth/shared-token.d.ts +0 -15
- package/dist/auth/shared-token.d.ts.map +1 -1
- package/dist/auth/shared-token.js +0 -25
- package/dist/auth/shared-token.js.map +1 -1
- package/dist/http-api/build-api.d.ts.map +1 -1
- package/dist/http-api/build-api.js +1 -1
- package/dist/http-api/build-api.js.map +1 -1
- package/dist/http-api/entity-api.d.ts.map +1 -1
- package/dist/http-api/entity-api.js +6 -2
- package/dist/http-api/entity-api.js.map +1 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -4
- package/dist/index.js.map +1 -1
- package/dist/load-remote-configuration.d.ts +4 -4
- package/dist/load-remote-configuration.d.ts.map +1 -1
- package/dist/load-remote-configuration.js +7 -14
- package/dist/load-remote-configuration.js.map +1 -1
- package/dist/observability.d.ts +2 -2
- package/dist/observability.d.ts.map +1 -1
- package/dist/observability.js.map +1 -1
- package/dist/push/index.d.ts +9 -0
- package/dist/push/index.d.ts.map +1 -0
- package/dist/push/index.js +9 -0
- package/dist/push/index.js.map +1 -0
- package/dist/push/server-push.d.ts +82 -0
- package/dist/push/server-push.d.ts.map +1 -0
- package/dist/push/server-push.js +48 -0
- package/dist/push/server-push.js.map +1 -0
- package/dist/push/sse.d.ts +62 -0
- package/dist/push/sse.d.ts.map +1 -0
- package/dist/push/sse.js +120 -0
- package/dist/push/sse.js.map +1 -0
- package/dist/respond.d.ts +1 -7
- package/dist/respond.d.ts.map +1 -1
- package/dist/respond.js +1 -8
- package/dist/respond.js.map +1 -1
- package/package.json +23 -13
- package/dist/auth/require-organization.d.ts +0 -32
- package/dist/auth/require-organization.d.ts.map +0 -1
- package/dist/auth/require-organization.js +0 -52
- package/dist/auth/require-organization.js.map +0 -1
- package/dist/entity-facet-routes.d.ts +0 -44
- package/dist/entity-facet-routes.d.ts.map +0 -1
- package/dist/entity-facet-routes.js +0 -70
- package/dist/entity-facet-routes.js.map +0 -1
- package/dist/entity-metadata-route.d.ts +0 -25
- package/dist/entity-metadata-route.d.ts.map +0 -1
- package/dist/entity-metadata-route.js +0 -102
- package/dist/entity-metadata-route.js.map +0 -1
- package/dist/entity-route-guard.d.ts +0 -62
- package/dist/entity-route-guard.d.ts.map +0 -1
- package/dist/entity-route-guard.js +0 -35
- package/dist/entity-route-guard.js.map +0 -1
- package/dist/entity-routes.d.ts +0 -76
- package/dist/entity-routes.d.ts.map +0 -1
- package/dist/entity-routes.js +0 -267
- package/dist/entity-routes.js.map +0 -1
- package/dist/route-responses.d.ts +0 -44
- package/dist/route-responses.d.ts.map +0 -1
- package/dist/route-responses.js +0 -104
- package/dist/route-responses.js.map +0 -1
- package/dist/sse.d.ts +0 -43
- package/dist/sse.d.ts.map +0 -1
- package/dist/sse.js +0 -78
- package/dist/sse.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/observability.ts"],"sourcesContent":["import * as NodeSdk from '@effect/opentelemetry/NodeSdk';\nimport type { ConfigurationClient, EntifixBuildError } from '@entifix/core';\nimport {\n type Attributes,\n createLogger,\n type Logger as ToolingLogger,\n type LogLevel,\n type LogRecord,\n type LogSink,\n makeOtlpHttpLogSink,\n makeStdoutJsonSink,\n} from '@entifix/tooling/logging';\nimport { context } from '@opentelemetry/api';\nimport { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport {\n AggregationTemporality,\n InMemoryMetricExporter,\n type MetricReader,\n PeriodicExportingMetricReader,\n type ResourceMetrics,\n} from '@opentelemetry/sdk-metrics';\nimport {\n BatchSpanProcessor,\n InMemorySpanExporter,\n type ReadableSpan,\n SimpleSpanProcessor,\n type SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\nimport { Effect, Layer, Logger, References } from 'effect';\nimport type * as EffectLogLevel from 'effect/LogLevel';\n\n/** Resolved observability settings (from config-service `logging.*`/`otel.*`). */\nexport interface ObservabilityConfig {\n readonly serviceName: string;\n readonly level: LogLevel;\n /** `otlp` in dev (ship straight to otel-lgtm), `stdout` in a cluster (filelog). */\n readonly sink: 'stdout' | 'otlp';\n /**\n * OTLP base URL; `/v1/traces`, `/v1/logs` and `/v1/metrics` are appended.\n *\n * **Optional, and that is the boot guarantee**: with no endpoint the service\n * still starts — logs fall back to the stdout sink and neither exporter is\n * built. It used to be a required `getString`, so a fleet whose\n * `otel.endpoint` row was missing failed to boot at all rather than running\n * without telemetry, which is the wrong trade for a signal that is not on the\n * request path.\n */\n readonly otelEndpoint?: string | undefined;\n /**\n * How often the metric reader exports, in milliseconds. Config rather than a\n * constant because it is a real operational dial (a shorter interval buys\n * resolution with cardinality-independent request volume), and optional\n * because a fleet whose seed predates the key should not fail to boot over an\n * export interval. (Optional for that reason alone: config-service reconciles\n * `SEED_ROWS` per row on every boot, so a *new* key does reach an existing\n * Postgres — `ON CONFLICT DO NOTHING` declines to overwrite an operator's\n * changed *value*, which is the case that needs a `dev:reset`.)\n */\n readonly metricIntervalMs?: number | undefined;\n}\n\n/** Export interval used when `otel.metricIntervalMs` is unset. */\nconst DEFAULT_METRIC_INTERVAL_MS = 60_000;\n\n/** The in-memory reader's interval — long enough that only `forceFlush` collects. */\nconst ONE_HOUR_MS = 3_600_000;\n\n/**\n * `@effect/opentelemetry` manages its own span tree but does not register an\n * OpenTelemetry context manager, so `@opentelemetry/api`'s active-span lookup\n * (which the tooling logger uses to stamp `trace_id`) would stay empty. Register\n * the AsyncLocalStorage manager once so the active span propagates and logs\n * correlate to their traces. Idempotent — `setGlobalContextManager` no-ops if a\n * manager is already registered (e.g. across test boots in one process).\n */\nlet contextManagerReady = false;\nconst ensureContextManager = (): void => {\n if (!contextManagerReady) {\n context.setGlobalContextManager(\n new AsyncLocalStorageContextManager().enable(),\n );\n contextManagerReady = true;\n }\n};\n\n/**\n * The OTLP base URL, or `undefined` when there is nowhere to export.\n *\n * A **blank** endpoint counts as absent, not as a base URL. config-service's\n * operator CRUD writes empty strings, so `''` is reachable in practice — and\n * taken literally it builds exporters aimed at the relative `/v1/traces` and\n * `/v1/metrics`, which resolve against nothing, fail every export, and look\n * from the call site exactly like a service that is simply quiet.\n */\nconst resolveEndpoint = (raw: string | undefined): string | undefined => {\n const trimmed = raw?.trim();\n return trimmed === undefined || trimmed === '' ? undefined : trimmed;\n};\n\nconst makeSink = (\n config: ObservabilityConfig,\n endpoint: string | undefined,\n): LogSink =>\n config.sink === 'otlp' && endpoint !== undefined\n ? makeOtlpHttpLogSink({\n endpoint,\n serviceName: config.serviceName,\n // Flush on a short interval too, so a quiet service's logs reach the\n // Collector promptly instead of waiting for a full batch.\n flushIntervalMs: 2000,\n onError: error =>\n process.stderr.write(\n `[otlp-log-sink] export failed: ${String(error)}\\n`,\n ),\n })\n : makeStdoutJsonSink();\n\n/**\n * Effect's `LogLevel` mapped onto the four levels the tooling logger has.\n *\n * ⚠️ **Match the spelling `effect/LogLevel` declares, and let the compiler\n * hold you to it.** Effect 4's level is the string union `\"All\" | \"Fatal\" |\n * \"Error\" | \"Warn\" | \"Info\" | \"Debug\" | \"Trace\" | \"None\"`. Under Effect 3 this\n * switch read the level's `label`, which was upper case (`\"ERROR\"`), against\n * mixed-case spellings (`'Error'`, `'Warning'`) typed as a plain `string` — so\n * **no case could ever match** and every log in the service fell through to\n * `default` and was emitted at `info`. Measured in Loki: an `Effect.logError`\n * arrived as `severity_text: INFO`, `severity_number: 9`, which makes an error\n * invisible to every level-based alert and dashboard while still appearing in\n * the log — the failure mode that reads as \"we have no errors\". Typing the\n * parameter as Effect's own union is what makes a misspelt case a compile\n * error now: `'Warning'` would not compile.\n */\nconst toToolingLevel = (level: EffectLogLevel.LogLevel): LogLevel => {\n switch (level) {\n case 'Trace':\n case 'Debug':\n return 'debug';\n case 'Warn':\n return 'warn';\n case 'Error':\n case 'Fatal':\n return 'error';\n default:\n return 'info';\n }\n};\n\n/**\n * `Effect.annotateLogs`' entries flattened into tooling `Attributes`.\n *\n * The annotations are a record read off the fiber, and the attribute values are\n * a closed scalar set, so anything else is stringified rather than dropped — an\n * annotation that reaches a sink as `[object Object]` is still more use than one\n * that silently is not there.\n */\nconst toAttributes = (\n annotations: Readonly<Record<string, unknown>>,\n): Attributes =>\n Object.fromEntries(\n Object.entries(annotations).map(([key, value]) => [\n key,\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === null\n ? value\n : String(value),\n ]),\n );\n\nconst messageToString = (message: unknown): string =>\n Array.isArray(message)\n ? message.map(part => String(part)).join(' ')\n : String(message);\n\n/**\n * An Effect `Logger` that forwards every Effect log (including the request logs\n * `HttpRouter.serve` writes) to the tooling logger — so the whole service logs\n * through one structured, trace-correlated pipeline.\n *\n * **Annotations are forwarded**, and used not to be: they were destructured\n * away, so every `Effect.annotateLogs` in every service was discarded before it\n * reached a sink. That made the annotated fields the outbox relay and the\n * transaction engine attach — the tenant database, the event id, the\n * transaction id, the rollback's own error — unqueryable, while the comments\n * beside them said they were structured fields in Loki. Measured: the records\n * arrived carrying the message and nothing else. Effect 4 no longer hands them\n * to a logger at all; they live on the fiber, as\n * `References.CurrentLogAnnotations`, and are read from there.\n *\n * `error` is called through its own arm because the tooling `Logger` gives it a\n * different signature — `(message, error?, attributes?)` — so passing the\n * attributes positionally as the second argument would file them as the *cause*\n * and lose them again.\n */\nconst makeEffectLogger = (\n logger: ToolingLogger,\n): Logger.Logger<unknown, void> =>\n Logger.make(({ logLevel, message, fiber }) => {\n const level = toToolingLevel(logLevel);\n const text = messageToString(message);\n const attributes = toAttributes(\n fiber.getRef(References.CurrentLogAnnotations),\n );\n\n if (level === 'error') {\n logger.error(text, undefined, attributes);\n return;\n }\n logger[level](text, attributes);\n });\n\n/** Lower-level inputs to {@link makeObservabilityLayerWith}. */\nexport interface ObservabilityLayerOptions {\n readonly serviceName: string;\n readonly level: LogLevel;\n /** Where log records go (a real sink in prod, an in-memory double in tests). */\n readonly sink: LogSink;\n /**\n * How spans are exported (batch→OTLP in prod, in-memory in tests).\n * Omitted when there is no OTLP endpoint to export to.\n */\n readonly spanProcessor?: SpanProcessor | undefined;\n /**\n * How metrics are exported (periodic→OTLP in prod, in-memory in tests).\n *\n * Injectable for the same reason `spanProcessor` is: a test drives the exact\n * wiring that ships, so — unlike a double that reimplements the composition —\n * it can see `Metrics.layer` failing to register a producer at all.\n */\n readonly metricReader?: MetricReader | undefined;\n}\n\n/**\n * The observability composition over explicit sink + span processor: replace\n * Effect's default logger with the tooling-backed one, and stand up the OTel\n * tracer so requests produce spans. Keeping sink/processor injectable lets a\n * test drive the exact same wiring with in-memory exporters (see\n * {@link makeInMemoryObservabilityLayer}), so the correlation the e2e asserts is\n * the correlation that ships.\n */\nexport const makeObservabilityLayerWith = (\n options: ObservabilityLayerOptions,\n) => {\n ensureContextManager();\n const toolingLogger = createLogger({\n service: options.serviceName,\n level: options.level,\n sink: options.sink,\n });\n // Replace Effect's loggers with the tooling logger. `Logger.layer` sets the\n // fiber's current loggers outright rather than adding to them, and neither\n // the service boot (`runMain`) nor the e2e boot (`Effect.runFork`) installs a\n // logger of its own, so this single layer takes effect in either runtime.\n const effectLogger = makeEffectLogger(toolingLogger);\n const LoggingLayer = Logger.layer([effectLogger]);\n // One `NodeSdk.layer` for both signals: it builds the OTel `Resource` once,\n // so the spans and the metrics carry the same `service.name` rather than two\n // resources that can disagree. `NodeSdk` degrades an absent `spanProcessor`\n // or `metricReader` to `Layer.empty` on its own (its `isNonEmpty` guard), so\n // the no-endpoint case needs no branch here — it simply exports nothing.\n //\n // The metric half needs no producer of its own: `NodeSdk` wires the reader\n // through `Metrics.layer`, whose `MetricProducer` reads **Effect's own metric\n // registry**. So `Metric.counter(…)` at any call site is exported, and a\n // domain counter stays one line where it is incremented.\n const TelemetryLayer = NodeSdk.layer(() => ({\n resource: { serviceName: options.serviceName },\n spanProcessor: options.spanProcessor,\n metricReader: options.metricReader,\n }));\n return Layer.merge(LoggingLayer, TelemetryLayer);\n};\n\n/**\n * The production/dev observability layer: log level + sink and OTLP endpoint\n * come from config-service. Merged into the service `AppLayer`.\n */\nexport const makeObservabilityLayer = (config: ObservabilityConfig) => {\n const endpoint = resolveEndpoint(config.otelEndpoint);\n return makeObservabilityLayerWith({\n serviceName: config.serviceName,\n level: config.level,\n sink: makeSink(config, endpoint),\n spanProcessor:\n endpoint === undefined\n ? undefined\n : new BatchSpanProcessor(\n new OTLPTraceExporter({ url: `${endpoint}/v1/traces` }),\n ),\n metricReader:\n endpoint === undefined\n ? undefined\n : new PeriodicExportingMetricReader({\n exporter: new OTLPMetricExporter({\n url: `${endpoint}/v1/metrics`,\n }),\n exportIntervalMillis:\n config.metricIntervalMs ?? DEFAULT_METRIC_INTERVAL_MS,\n }),\n });\n};\n\n/** Captured telemetry from {@link makeInMemoryObservabilityLayer}. */\nexport interface InMemoryObservability {\n readonly layer: ReturnType<typeof makeObservabilityLayerWith>;\n /** Records the (replaced) logger emitted — the e2e asserts on these. */\n readonly logRecords: readonly LogRecord[];\n /** The spans exported so far (used to confirm requests produced traces). */\n readonly getSpans: () => ReadonlyArray<ReadableSpan>;\n /**\n * Forces a metric collection and returns what the exporter then holds.\n *\n * A push reader only exports on its interval, so a test that merely read the\n * exporter would find it empty and could not tell \"the producer is not\n * registered\" from \"the interval has not elapsed\".\n */\n readonly collectMetrics: () => Promise<ReadonlyArray<ResourceMetrics>>;\n}\n\n/**\n * Test-support: the real observability wiring with in-memory exporters, so an\n * e2e can boot the service and assert that logs are structured and carry the\n * `trace_id` of the request span that produced them.\n */\nexport const makeInMemoryObservabilityLayer = (\n serviceName: string,\n): InMemoryObservability => {\n const logRecords: LogRecord[] = [];\n const spanExporter = new InMemorySpanExporter();\n const metricExporter = new InMemoryMetricExporter(\n AggregationTemporality.CUMULATIVE,\n );\n // A long interval, because the test drives collection explicitly through\n // `collectMetrics`. A short one would export on a timer as well and make the\n // assertions depend on how long the surrounding effect happened to take.\n const metricReader = new PeriodicExportingMetricReader({\n exporter: metricExporter,\n exportIntervalMillis: ONE_HOUR_MS,\n });\n const layer = makeObservabilityLayerWith({\n serviceName,\n level: 'debug',\n sink: { emit: record => logRecords.push(record) },\n spanProcessor: new SimpleSpanProcessor(spanExporter),\n metricReader,\n });\n return {\n layer,\n logRecords,\n getSpans: () => spanExporter.getFinishedSpans(),\n collectMetrics: async () => {\n await metricReader.forceFlush();\n return metricExporter.getMetrics();\n },\n };\n};\n\n/**\n * The observability layer for a service, read from its own configuration.\n *\n * This is the whole per-service wiring: a composition root yields it and merges\n * the result into its `AppLayer`. It exists because the four reads below were\n * byte-identical in every service that had them, sitting directly above a\n * `makeObservabilityLayer` call that mapped them one-for-one — the same\n * duplication the module itself carried, one level up.\n *\n * `logging.level` and `logging.sink` are **required**: the rows are seeded for\n * every service and reach an existing table on config-service's next boot (the\n * seed reconciles per row and only declines to overwrite a value that is already\n * there), so a missing one is a real misconfiguration and should be loud.\n *\n * `otel.endpoint` and `otel.metricIntervalMs` are **optional**, for two\n * different reasons. The endpoint is what makes telemetry a *degradable*\n * dependency — with none, the service boots and serves, logs to stdout and\n * exports nothing, rather than an unreachable telemetry destination taking the\n * service down. The interval is a dial with a sane default, and requiring it\n * would fail the boot on any fleet whose seed predates it.\n *\n * Takes the {@link ConfigurationClient} port rather than a concrete client, so\n * config-service can pass the store it builds from its **own** SQL rows: it is\n * the one service that cannot fetch its configuration over HTTP, because it is\n * the thing being fetched from.\n */\nexport const observabilityFromConfiguration = (\n store: ConfigurationClient,\n serviceName: string,\n): Effect.Effect<\n ReturnType<typeof makeObservabilityLayer>,\n EntifixBuildError\n> =>\n Effect.gen(function* () {\n const level = yield* store.in('logging').getString('level');\n const sink = yield* store.in('logging').getString('sink');\n const otelEndpoint = yield* store.in('otel').getOptionalString('endpoint');\n const metricIntervalMs = yield* store\n .in('otel')\n .getOptionalNumber('metricIntervalMs');\n\n return makeObservabilityLayer({\n serviceName,\n level: level as LogLevel,\n // Anything but the explicit `stdout` is the OTLP sink, which is what makes\n // a mistyped value degrade to shipping rather than to silence.\n sink: sink === 'stdout' ? 'stdout' : 'otlp',\n otelEndpoint,\n metricIntervalMs,\n });\n });\n"],"names":["NodeSdk","createLogger","makeOtlpHttpLogSink","makeStdoutJsonSink","context","AsyncLocalStorageContextManager","OTLPMetricExporter","OTLPTraceExporter","AggregationTemporality","InMemoryMetricExporter","PeriodicExportingMetricReader","BatchSpanProcessor","InMemorySpanExporter","SimpleSpanProcessor","Effect","Layer","Logger","References","DEFAULT_METRIC_INTERVAL_MS","ONE_HOUR_MS","contextManagerReady","ensureContextManager","setGlobalContextManager","enable","resolveEndpoint","raw","trimmed","trim","undefined","makeSink","config","endpoint","sink","serviceName","flushIntervalMs","onError","error","process","stderr","write","String","toToolingLevel","level","toAttributes","annotations","Object","fromEntries","entries","map","key","value","messageToString","message","Array","isArray","part","join","makeEffectLogger","logger","make","logLevel","fiber","text","attributes","getRef","CurrentLogAnnotations","makeObservabilityLayerWith","options","toolingLogger","service","effectLogger","LoggingLayer","layer","TelemetryLayer","resource","spanProcessor","metricReader","merge","makeObservabilityLayer","otelEndpoint","url","exporter","exportIntervalMillis","metricIntervalMs","makeInMemoryObservabilityLayer","logRecords","spanExporter","metricExporter","CUMULATIVE","emit","record","push","getSpans","getFinishedSpans","collectMetrics","forceFlush","getMetrics","observabilityFromConfiguration","store","gen","in","getString","getOptionalString","getOptionalNumber"],"mappings":"AAAA,YAAYA,aAAa,gCAAgC;AAEzD,SAEEC,YAAY,EAKZC,mBAAmB,EACnBC,kBAAkB,QACb,2BAA2B;AAClC,SAASC,OAAO,QAAQ,qBAAqB;AAC7C,SAASC,+BAA+B,QAAQ,qCAAqC;AACrF,SAASC,kBAAkB,QAAQ,4CAA4C;AAC/E,SAASC,iBAAiB,QAAQ,0CAA0C;AAC5E,SACEC,sBAAsB,EACtBC,sBAAsB,EAEtBC,6BAA6B,QAExB,6BAA6B;AACpC,SACEC,kBAAkB,EAClBC,oBAAoB,EAEpBC,mBAAmB,QAEd,gCAAgC;AACvC,SAASC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,UAAU,QAAQ,SAAS;AAiC3D,gEAAgE,GAChE,MAAMC,6BAA6B;AAEnC,mFAAmF,GACnF,MAAMC,cAAc;AAEpB;;;;;;;CAOC,GACD,IAAIC,sBAAsB;AAC1B,MAAMC,uBAAuB;IAC3B,IAAI,CAACD,qBAAqB;QACxBhB,QAAQkB,uBAAuB,CAC7B,IAAIjB,kCAAkCkB,MAAM;QAE9CH,sBAAsB;IACxB;AACF;AAEA;;;;;;;;CAQC,GACD,MAAMI,kBAAkB,CAACC;IACvB,MAAMC,UAAUD,uBAAAA,IAAKE,IAAI;IACzB,OAAOD,YAAYE,aAAaF,YAAY,KAAKE,YAAYF;AAC/D;AAEA,MAAMG,WAAW,CACfC,QACAC,WAEAD,OAAOE,IAAI,KAAK,UAAUD,aAAaH,YACnC1B,oBAAoB;QAClB6B;QACAE,aAAaH,OAAOG,WAAW;QAC/B,qEAAqE;QACrE,0DAA0D;QAC1DC,iBAAiB;QACjBC,SAASC,CAAAA,QACPC,QAAQC,MAAM,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AuBgF,UAAU;IAEnC,yEAAyE;IACzE,6EAA6E;IAC7E,yEAAyE;IACzE,MAAMZ,eAAe,IAAIlE,8BAA8B;QACrDuE,UAAUM;QACVL,sBAAsB/D;IACxB;IACA,MAAMqD,QAAQN,2BAA2B;QACvCjC;QACAS,OAAO;QACPV,MAAM;YAAEyD,MAAMC,CAAAA,SAAUL,WAAWM,IAAI,CAACD;QAAQ;QAChDf,eAAe,IAAI9D,oBAAoByE;QACvCV;IACF;IACA,OAAO;QACLJ;QACAa;QACAO,UAAU,IAAMN,aAAaO,gBAAgB;QAC7CC,gBAAgB;YACd,MAAMlB,aAAamB,UAAU;YAC7B,OAAOR,eAAeS,UAAU;QAClC;IACF;AACF,EAAE;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GACD,OAAO,MAAMC,iCAAiC,CAC5CC,OACAjE,cAKAnB,OAAOqF,GAAG,CAAC;QACT,MAAMzD,QAAQ,OAAOwD,MAAME,EAAE,CAAC,WAAWC,SAAS,CAAC;QACnD,MAAMrE,OAAO,OAAOkE,MAAME,EAAE,CAAC,WAAWC,SAAS,CAAC;QAClD,MAAMtB,eAAe,OAAOmB,MAAME,EAAE,CAAC,QAAQE,iBAAiB,CAAC;QAC/D,MAAMnB,mBAAmB,OAAOe,MAC7BE,EAAE,CAAC,QACHG,iBAAiB,CAAC;QAErB,OAAOzB,uBAAuB;YAC5B7C;YACAS,OAAOA;YACP,2EAA2E;YAC3E,+DAA+D;YAC/DV,MAAMA,SAAS,WAAW,WAAW;YACrC+C;YACAI;QACF;IACF,GAAG"}
|
|
1
|
+
{"version":3,"sources":["../src/observability.ts"],"sourcesContent":["import * as NodeSdk from '@effect/opentelemetry/NodeSdk';\nimport type { ConfigurationClient, Unavailable } from '@entifix/core';\nimport {\n type Attributes,\n createLogger,\n type Logger as ToolingLogger,\n type LogLevel,\n type LogRecord,\n type LogSink,\n makeOtlpHttpLogSink,\n makeStdoutJsonSink,\n} from '@entifix/tooling/logging';\nimport { context } from '@opentelemetry/api';\nimport { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';\nimport { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-http';\nimport { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';\nimport {\n AggregationTemporality,\n InMemoryMetricExporter,\n type MetricReader,\n PeriodicExportingMetricReader,\n type ResourceMetrics,\n} from '@opentelemetry/sdk-metrics';\nimport {\n BatchSpanProcessor,\n InMemorySpanExporter,\n type ReadableSpan,\n SimpleSpanProcessor,\n type SpanProcessor,\n} from '@opentelemetry/sdk-trace-base';\nimport { Effect, Layer, Logger, References } from 'effect';\nimport type * as EffectLogLevel from 'effect/LogLevel';\n\n/** Resolved observability settings (from config-service `logging.*`/`otel.*`). */\nexport interface ObservabilityConfig {\n readonly serviceName: string;\n readonly level: LogLevel;\n /** `otlp` in dev (ship straight to otel-lgtm), `stdout` in a cluster (filelog). */\n readonly sink: 'stdout' | 'otlp';\n /**\n * OTLP base URL; `/v1/traces`, `/v1/logs` and `/v1/metrics` are appended.\n *\n * **Optional, and that is the boot guarantee**: with no endpoint the service\n * still starts — logs fall back to the stdout sink and neither exporter is\n * built. It used to be a required `getString`, so a fleet whose\n * `otel.endpoint` row was missing failed to boot at all rather than running\n * without telemetry, which is the wrong trade for a signal that is not on the\n * request path.\n */\n readonly otelEndpoint?: string | undefined;\n /**\n * How often the metric reader exports, in milliseconds. Config rather than a\n * constant because it is a real operational dial (a shorter interval buys\n * resolution with cardinality-independent request volume), and optional\n * because a fleet whose seed predates the key should not fail to boot over an\n * export interval. (Optional for that reason alone: config-service reconciles\n * `SEED_ROWS` per row on every boot, so a *new* key does reach an existing\n * Postgres — `ON CONFLICT DO NOTHING` declines to overwrite an operator's\n * changed *value*, which is the case that needs a `dev:reset`.)\n */\n readonly metricIntervalMs?: number | undefined;\n}\n\n/** Export interval used when `otel.metricIntervalMs` is unset. */\nconst DEFAULT_METRIC_INTERVAL_MS = 60_000;\n\n/** The in-memory reader's interval — long enough that only `forceFlush` collects. */\nconst ONE_HOUR_MS = 3_600_000;\n\n/**\n * `@effect/opentelemetry` manages its own span tree but does not register an\n * OpenTelemetry context manager, so `@opentelemetry/api`'s active-span lookup\n * (which the tooling logger uses to stamp `trace_id`) would stay empty. Register\n * the AsyncLocalStorage manager once so the active span propagates and logs\n * correlate to their traces. Idempotent — `setGlobalContextManager` no-ops if a\n * manager is already registered (e.g. across test boots in one process).\n */\nlet contextManagerReady = false;\nconst ensureContextManager = (): void => {\n if (!contextManagerReady) {\n context.setGlobalContextManager(\n new AsyncLocalStorageContextManager().enable(),\n );\n contextManagerReady = true;\n }\n};\n\n/**\n * The OTLP base URL, or `undefined` when there is nowhere to export.\n *\n * A **blank** endpoint counts as absent, not as a base URL. config-service's\n * operator CRUD writes empty strings, so `''` is reachable in practice — and\n * taken literally it builds exporters aimed at the relative `/v1/traces` and\n * `/v1/metrics`, which resolve against nothing, fail every export, and look\n * from the call site exactly like a service that is simply quiet.\n */\nconst resolveEndpoint = (raw: string | undefined): string | undefined => {\n const trimmed = raw?.trim();\n return trimmed === undefined || trimmed === '' ? undefined : trimmed;\n};\n\nconst makeSink = (\n config: ObservabilityConfig,\n endpoint: string | undefined,\n): LogSink =>\n config.sink === 'otlp' && endpoint !== undefined\n ? makeOtlpHttpLogSink({\n endpoint,\n serviceName: config.serviceName,\n // Flush on a short interval too, so a quiet service's logs reach the\n // Collector promptly instead of waiting for a full batch.\n flushIntervalMs: 2000,\n onError: error =>\n process.stderr.write(\n `[otlp-log-sink] export failed: ${String(error)}\\n`,\n ),\n })\n : makeStdoutJsonSink();\n\n/**\n * Effect's `LogLevel` mapped onto the four levels the tooling logger has.\n *\n * ⚠️ **Match the spelling `effect/LogLevel` declares, and let the compiler\n * hold you to it.** Effect 4's level is the string union `\"All\" | \"Fatal\" |\n * \"Error\" | \"Warn\" | \"Info\" | \"Debug\" | \"Trace\" | \"None\"`. Under Effect 3 this\n * switch read the level's `label`, which was upper case (`\"ERROR\"`), against\n * mixed-case spellings (`'Error'`, `'Warning'`) typed as a plain `string` — so\n * **no case could ever match** and every log in the service fell through to\n * `default` and was emitted at `info`. Measured in Loki: an `Effect.logError`\n * arrived as `severity_text: INFO`, `severity_number: 9`, which makes an error\n * invisible to every level-based alert and dashboard while still appearing in\n * the log — the failure mode that reads as \"we have no errors\". Typing the\n * parameter as Effect's own union is what makes a misspelt case a compile\n * error now: `'Warning'` would not compile.\n */\nconst toToolingLevel = (level: EffectLogLevel.LogLevel): LogLevel => {\n switch (level) {\n case 'Trace':\n case 'Debug':\n return 'debug';\n case 'Warn':\n return 'warn';\n case 'Error':\n case 'Fatal':\n return 'error';\n default:\n return 'info';\n }\n};\n\n/**\n * `Effect.annotateLogs`' entries flattened into tooling `Attributes`.\n *\n * The annotations are a record read off the fiber, and the attribute values are\n * a closed scalar set, so anything else is stringified rather than dropped — an\n * annotation that reaches a sink as `[object Object]` is still more use than one\n * that silently is not there.\n */\nconst toAttributes = (\n annotations: Readonly<Record<string, unknown>>,\n): Attributes =>\n Object.fromEntries(\n Object.entries(annotations).map(([key, value]) => [\n key,\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === null\n ? value\n : String(value),\n ]),\n );\n\nconst messageToString = (message: unknown): string =>\n Array.isArray(message)\n ? message.map(part => String(part)).join(' ')\n : String(message);\n\n/**\n * An Effect `Logger` that forwards every Effect log (including the request logs\n * `HttpRouter.serve` writes) to the tooling logger — so the whole service logs\n * through one structured, trace-correlated pipeline.\n *\n * **Annotations are forwarded**, and used not to be: they were destructured\n * away, so every `Effect.annotateLogs` in every service was discarded before it\n * reached a sink. That made the annotated fields the outbox relay and the\n * transaction engine attach — the tenant database, the event id, the\n * transaction id, the rollback's own error — unqueryable, while the comments\n * beside them said they were structured fields in Loki. Measured: the records\n * arrived carrying the message and nothing else. Effect 4 no longer hands them\n * to a logger at all; they live on the fiber, as\n * `References.CurrentLogAnnotations`, and are read from there.\n *\n * `error` is called through its own arm because the tooling `Logger` gives it a\n * different signature — `(message, error?, attributes?)` — so passing the\n * attributes positionally as the second argument would file them as the *cause*\n * and lose them again.\n */\nconst makeEffectLogger = (\n logger: ToolingLogger,\n): Logger.Logger<unknown, void> =>\n Logger.make(({ logLevel, message, fiber }) => {\n const level = toToolingLevel(logLevel);\n const text = messageToString(message);\n const attributes = toAttributes(\n fiber.getRef(References.CurrentLogAnnotations),\n );\n\n if (level === 'error') {\n logger.error(text, undefined, attributes);\n return;\n }\n logger[level](text, attributes);\n });\n\n/** Lower-level inputs to {@link makeObservabilityLayerWith}. */\nexport interface ObservabilityLayerOptions {\n readonly serviceName: string;\n readonly level: LogLevel;\n /** Where log records go (a real sink in prod, an in-memory double in tests). */\n readonly sink: LogSink;\n /**\n * How spans are exported (batch→OTLP in prod, in-memory in tests).\n * Omitted when there is no OTLP endpoint to export to.\n */\n readonly spanProcessor?: SpanProcessor | undefined;\n /**\n * How metrics are exported (periodic→OTLP in prod, in-memory in tests).\n *\n * Injectable for the same reason `spanProcessor` is: a test drives the exact\n * wiring that ships, so — unlike a double that reimplements the composition —\n * it can see `Metrics.layer` failing to register a producer at all.\n */\n readonly metricReader?: MetricReader | undefined;\n}\n\n/**\n * The observability composition over explicit sink + span processor: replace\n * Effect's default logger with the tooling-backed one, and stand up the OTel\n * tracer so requests produce spans. Keeping sink/processor injectable lets a\n * test drive the exact same wiring with in-memory exporters (see\n * {@link makeInMemoryObservabilityLayer}), so the correlation the e2e asserts is\n * the correlation that ships.\n */\nexport const makeObservabilityLayerWith = (\n options: ObservabilityLayerOptions,\n) => {\n ensureContextManager();\n const toolingLogger = createLogger({\n service: options.serviceName,\n level: options.level,\n sink: options.sink,\n });\n // Replace Effect's loggers with the tooling logger. `Logger.layer` sets the\n // fiber's current loggers outright rather than adding to them, and neither\n // the service boot (`runMain`) nor the e2e boot (`Effect.runFork`) installs a\n // logger of its own, so this single layer takes effect in either runtime.\n const effectLogger = makeEffectLogger(toolingLogger);\n const LoggingLayer = Logger.layer([effectLogger]);\n // One `NodeSdk.layer` for both signals: it builds the OTel `Resource` once,\n // so the spans and the metrics carry the same `service.name` rather than two\n // resources that can disagree. `NodeSdk` degrades an absent `spanProcessor`\n // or `metricReader` to `Layer.empty` on its own (its `isNonEmpty` guard), so\n // the no-endpoint case needs no branch here — it simply exports nothing.\n //\n // The metric half needs no producer of its own: `NodeSdk` wires the reader\n // through `Metrics.layer`, whose `MetricProducer` reads **Effect's own metric\n // registry**. So `Metric.counter(…)` at any call site is exported, and a\n // domain counter stays one line where it is incremented.\n const TelemetryLayer = NodeSdk.layer(() => ({\n resource: { serviceName: options.serviceName },\n spanProcessor: options.spanProcessor,\n metricReader: options.metricReader,\n }));\n return Layer.merge(LoggingLayer, TelemetryLayer);\n};\n\n/**\n * The production/dev observability layer: log level + sink and OTLP endpoint\n * come from config-service. Merged into the service `AppLayer`.\n */\nexport const makeObservabilityLayer = (config: ObservabilityConfig) => {\n const endpoint = resolveEndpoint(config.otelEndpoint);\n return makeObservabilityLayerWith({\n serviceName: config.serviceName,\n level: config.level,\n sink: makeSink(config, endpoint),\n spanProcessor:\n endpoint === undefined\n ? undefined\n : new BatchSpanProcessor(\n new OTLPTraceExporter({ url: `${endpoint}/v1/traces` }),\n ),\n metricReader:\n endpoint === undefined\n ? undefined\n : new PeriodicExportingMetricReader({\n exporter: new OTLPMetricExporter({\n url: `${endpoint}/v1/metrics`,\n }),\n exportIntervalMillis:\n config.metricIntervalMs ?? DEFAULT_METRIC_INTERVAL_MS,\n }),\n });\n};\n\n/** Captured telemetry from {@link makeInMemoryObservabilityLayer}. */\nexport interface InMemoryObservability {\n readonly layer: ReturnType<typeof makeObservabilityLayerWith>;\n /** Records the (replaced) logger emitted — the e2e asserts on these. */\n readonly logRecords: readonly LogRecord[];\n /** The spans exported so far (used to confirm requests produced traces). */\n readonly getSpans: () => ReadonlyArray<ReadableSpan>;\n /**\n * Forces a metric collection and returns what the exporter then holds.\n *\n * A push reader only exports on its interval, so a test that merely read the\n * exporter would find it empty and could not tell \"the producer is not\n * registered\" from \"the interval has not elapsed\".\n */\n readonly collectMetrics: () => Promise<ReadonlyArray<ResourceMetrics>>;\n}\n\n/**\n * Test-support: the real observability wiring with in-memory exporters, so an\n * e2e can boot the service and assert that logs are structured and carry the\n * `trace_id` of the request span that produced them.\n */\nexport const makeInMemoryObservabilityLayer = (\n serviceName: string,\n): InMemoryObservability => {\n const logRecords: LogRecord[] = [];\n const spanExporter = new InMemorySpanExporter();\n const metricExporter = new InMemoryMetricExporter(\n AggregationTemporality.CUMULATIVE,\n );\n // A long interval, because the test drives collection explicitly through\n // `collectMetrics`. A short one would export on a timer as well and make the\n // assertions depend on how long the surrounding effect happened to take.\n const metricReader = new PeriodicExportingMetricReader({\n exporter: metricExporter,\n exportIntervalMillis: ONE_HOUR_MS,\n });\n const layer = makeObservabilityLayerWith({\n serviceName,\n level: 'debug',\n sink: { emit: record => logRecords.push(record) },\n spanProcessor: new SimpleSpanProcessor(spanExporter),\n metricReader,\n });\n return {\n layer,\n logRecords,\n getSpans: () => spanExporter.getFinishedSpans(),\n collectMetrics: async () => {\n await metricReader.forceFlush();\n return metricExporter.getMetrics();\n },\n };\n};\n\n/**\n * The observability layer for a service, read from its own configuration.\n *\n * This is the whole per-service wiring: a composition root yields it and merges\n * the result into its `AppLayer`. It exists because the four reads below were\n * byte-identical in every service that had them, sitting directly above a\n * `makeObservabilityLayer` call that mapped them one-for-one — the same\n * duplication the module itself carried, one level up.\n *\n * `logging.level` and `logging.sink` are **required**: the rows are seeded for\n * every service and reach an existing table on config-service's next boot (the\n * seed reconciles per row and only declines to overwrite a value that is already\n * there), so a missing one is a real misconfiguration and should be loud.\n *\n * `otel.endpoint` and `otel.metricIntervalMs` are **optional**, for two\n * different reasons. The endpoint is what makes telemetry a *degradable*\n * dependency — with none, the service boots and serves, logs to stdout and\n * exports nothing, rather than an unreachable telemetry destination taking the\n * service down. The interval is a dial with a sane default, and requiring it\n * would fail the boot on any fleet whose seed predates it.\n *\n * Takes the {@link ConfigurationClient} port rather than a concrete client, so\n * config-service can pass the store it builds from its **own** SQL rows: it is\n * the one service that cannot fetch its configuration over HTTP, because it is\n * the thing being fetched from.\n */\nexport const observabilityFromConfiguration = (\n store: ConfigurationClient,\n serviceName: string,\n): Effect.Effect<ReturnType<typeof makeObservabilityLayer>, Unavailable> =>\n Effect.gen(function* () {\n const level = yield* store.in('logging').getString('level');\n const sink = yield* store.in('logging').getString('sink');\n const otelEndpoint = yield* store.in('otel').getOptionalString('endpoint');\n const metricIntervalMs = yield* store\n .in('otel')\n .getOptionalNumber('metricIntervalMs');\n\n return makeObservabilityLayer({\n serviceName,\n level: level as LogLevel,\n // Anything but the explicit `stdout` is the OTLP sink, which is what makes\n // a mistyped value degrade to shipping rather than to silence.\n sink: sink === 'stdout' ? 'stdout' : 'otlp',\n otelEndpoint,\n metricIntervalMs,\n });\n });\n"],"names":["NodeSdk","createLogger","makeOtlpHttpLogSink","makeStdoutJsonSink","context","AsyncLocalStorageContextManager","OTLPMetricExporter","OTLPTraceExporter","AggregationTemporality","InMemoryMetricExporter","PeriodicExportingMetricReader","BatchSpanProcessor","InMemorySpanExporter","SimpleSpanProcessor","Effect","Layer","Logger","References","DEFAULT_METRIC_INTERVAL_MS","ONE_HOUR_MS","contextManagerReady","ensureContextManager","setGlobalContextManager","enable","resolveEndpoint","raw","trimmed","trim","undefined","makeSink","config","endpoint","sink","serviceName","flushIntervalMs","onError","error","process","stderr","write","String","toToolingLevel","level","toAttributes","annotations","Object","fromEntries","entries","map","key","value","messageToString","message","Array","isArray","part","join","makeEffectLogger","logger","make","logLevel","fiber","text","attributes","getRef","CurrentLogAnnotations","makeObservabilityLayerWith","options","toolingLogger","service","effectLogger","LoggingLayer","layer","TelemetryLayer","resource","spanProcessor","metricReader","merge","makeObservabilityLayer","otelEndpoint","url","exporter","exportIntervalMillis","metricIntervalMs","makeInMemoryObservabilityLayer","logRecords","spanExporter","metricExporter","CUMULATIVE","emit","record","push","getSpans","getFinishedSpans","collectMetrics","forceFlush","getMetrics","observabilityFromConfiguration","store","gen","in","getString","getOptionalString","getOptionalNumber"],"mappings":"AAAA,YAAYA,aAAa,gCAAgC;AAEzD,SAEEC,YAAY,EAKZC,mBAAmB,EACnBC,kBAAkB,QACb,2BAA2B;AAClC,SAASC,OAAO,QAAQ,qBAAqB;AAC7C,SAASC,+BAA+B,QAAQ,qCAAqC;AACrF,SAASC,kBAAkB,QAAQ,4CAA4C;AAC/E,SAASC,iBAAiB,QAAQ,0CAA0C;AAC5E,SACEC,sBAAsB,EACtBC,sBAAsB,EAEtBC,6BAA6B,QAExB,6BAA6B;AACpC,SACEC,kBAAkB,EAClBC,oBAAoB,EAEpBC,mBAAmB,QAEd,gCAAgC;AACvC,SAASC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,UAAU,QAAQ,SAAS;AAiC3D,gEAAgE,GAChE,MAAMC,6BAA6B;AAEnC,mFAAmF,GACnF,MAAMC,cAAc;AAEpB;;;;;;;CAOC,GACD,IAAIC,sBAAsB;AAC1B,MAAMC,uBAAuB;IAC3B,IAAI,CAACD,qBAAqB;QACxBhB,QAAQkB,uBAAuB,CAC7B,IAAIjB,kCAAkCkB,MAAM;QAE9CH,sBAAsB;IACxB;AACF;AAEA;;;;;;;;CAQC,GACD,MAAMI,kBAAkB,CAACC;IACvB,MAAMC,UAAUD,uBAAAA,IAAKE,IAAI;IACzB,OAAOD,YAAYE,aAAaF,YAAY,KAAKE,YAAYF;AAC/D;AAEA,MAAMG,WAAW,CACfC,QACAC,WAEAD,OAAOE,IAAI,KAAK,UAAUD,aAAaH,YACnC1B,oBAAoB;QAClB6B;QACAE,aAAaH,OAAOG,WAAW;QAC/B,qEAAqE;QACrE,0DAA0D;QAC1DC,iBAAiB;QACjBC,SAASC,CAAAA,QACPC,QAAQC,MAAM,CAACC,KAAK,CAClB,CAAC,+BAA+B,EAAEC,OAAOJ,OAAO,EAAE,CAAC;IAEzD,KACAjC;AAEN;;;;;;;;;;;;;;;CA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AAUM;QACVL,sBAAsB/D;IACxB;IACA,MAAMqD,QAAQN,2BAA2B;QACvCjC;QACAS,OAAO;QACPV,MAAM;YAAEyD,MAAMC,CAAAA,SAAUL,WAAWM,IAAI,CAACD;QAAQ;QAChDf,eAAe,IAAI9D,oBAAoByE;QACvCV;IACF;IACA,OAAO;QACLJ;QACAa;QACAO,UAAU,IAAMN,aAAaO,gBAAgB;QAC7CC,gBAAgB;YACd,MAAMlB,aAAamB,UAAU;YAC7B,OAAOR,eAAeS,UAAU;QAClC;IACF;AACF,EAAE;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;CAyBC,GACD,OAAO,MAAMC,iCAAiC,CAC5CC,OACAjE,cAEAnB,OAAOqF,GAAG,CAAC;QACT,MAAMzD,QAAQ,OAAOwD,MAAME,EAAE,CAAC,WAAWC,SAAS,CAAC;QACnD,MAAMrE,OAAO,OAAOkE,MAAME,EAAE,CAAC,WAAWC,SAAS,CAAC;QAClD,MAAMtB,eAAe,OAAOmB,MAAME,EAAE,CAAC,QAAQE,iBAAiB,CAAC;QAC/D,MAAMnB,mBAAmB,OAAOe,MAC7BE,EAAE,CAAC,QACHG,iBAAiB,CAAC;QAErB,OAAOzB,uBAAuB;YAC5B7C;YACAS,OAAOA;YACP,2EAA2E;YAC3E,+DAA+D;YAC/DV,MAAMA,SAAS,WAAW,WAAW;YACrC+C;YACAI;QACF;IACF,GAAG"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server push: the port a host publishes to and a connection reads from
|
|
3
|
+
* ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).
|
|
4
|
+
*
|
|
5
|
+
* A subpath rather than part of `.`, so a host can ban it from every package
|
|
6
|
+
* that must not know a browser exists. The SSE adapter is at `./push/sse`.
|
|
7
|
+
*/
|
|
8
|
+
export * from './server-push.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/push/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server push: the port a host publishes to and a connection reads from
|
|
3
|
+
* ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).
|
|
4
|
+
*
|
|
5
|
+
* A subpath rather than part of `.`, so a host can ban it from every package
|
|
6
|
+
* that must not know a browser exists. The SSE adapter is at `./push/sse`.
|
|
7
|
+
*/ export * from './server-push.js';
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/push/index.ts"],"sourcesContent":["/**\n * Server push: the port a host publishes to and a connection reads from\n * ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).\n *\n * A subpath rather than part of `.`, so a host can ban it from every package\n * that must not know a browser exists. The SSE adapter is at `./push/sse`.\n */\nexport * from './server-push.js';\n"],"names":[],"mappings":"AAAA;;;;;;CAMC,GACD,cAAc,mBAAmB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type AudienceRecipient, type DomainEvent } from '@entifix/core';
|
|
2
|
+
import { Context, Effect, Metric, type Scope, Stream } from 'effect';
|
|
3
|
+
/**
|
|
4
|
+
* One open push connection: who holds it, what it asked for, and where it
|
|
5
|
+
* left off.
|
|
6
|
+
*
|
|
7
|
+
* Every field comes from the request that opened it — the topics and the
|
|
8
|
+
* cursor from its URL and `Last-Event-ID`, the recipient from the verified
|
|
9
|
+
* session or the flow capabilities it presented. Nothing about a connection is
|
|
10
|
+
* kept anywhere else, so any replica can answer its reconnect
|
|
11
|
+
* ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).
|
|
12
|
+
*/
|
|
13
|
+
export interface PushConnection {
|
|
14
|
+
readonly recipient: AudienceRecipient;
|
|
15
|
+
/** `entity:<key>`, `record:<key>:<id>` or `flow:<id>`. */
|
|
16
|
+
readonly topics: readonly string[];
|
|
17
|
+
/** The `Last-Event-ID` the client reconnected with, if any. */
|
|
18
|
+
readonly cursor?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* What a connection receives: an event, with the topics of this connection it
|
|
22
|
+
* matched, or a request to re-read some of its topics because what it missed
|
|
23
|
+
* cannot be replayed.
|
|
24
|
+
*
|
|
25
|
+
* The matched topics travel with the event so the browser routes it to the
|
|
26
|
+
* listeners that asked, without knowing the host's topic vocabulary.
|
|
27
|
+
*/
|
|
28
|
+
export type PushMessage = {
|
|
29
|
+
readonly _tag: 'event';
|
|
30
|
+
readonly event: DomainEvent;
|
|
31
|
+
readonly topics: readonly string[];
|
|
32
|
+
} | {
|
|
33
|
+
readonly _tag: 'resync';
|
|
34
|
+
readonly topics: readonly string[];
|
|
35
|
+
};
|
|
36
|
+
/** The server half of push: what a publisher feeds and what a connection reads. */
|
|
37
|
+
export interface ServerPush {
|
|
38
|
+
/** Offers one event to every open connection that subscribed and is admitted. */
|
|
39
|
+
readonly publish: (event: DomainEvent) => Effect.Effect<void>;
|
|
40
|
+
/**
|
|
41
|
+
* The messages for one connection: the resume hook's catch-up first, then
|
|
42
|
+
* every live event matching its topics and its audience, until the
|
|
43
|
+
* connection's scope closes.
|
|
44
|
+
*/
|
|
45
|
+
readonly open: (connection: PushConnection) => Stream.Stream<PushMessage>;
|
|
46
|
+
}
|
|
47
|
+
declare const ServerPushTag_base: Context.ServiceClass<ServerPushTag, "@entifix/service-shell/ServerPush", ServerPush>;
|
|
48
|
+
export declare class ServerPushTag extends ServerPushTag_base {
|
|
49
|
+
}
|
|
50
|
+
export interface ServerPushOptions {
|
|
51
|
+
/**
|
|
52
|
+
* The topics an event belongs to. An event in no topic reaches nobody.
|
|
53
|
+
*
|
|
54
|
+
* Host vocabulary: an entity change might be `entity:<key>` and
|
|
55
|
+
* `record:<key>:<id>`, a flow outcome `flow:<id>`.
|
|
56
|
+
*/
|
|
57
|
+
readonly topicsOf: (event: DomainEvent) => readonly string[];
|
|
58
|
+
/**
|
|
59
|
+
* Whether a connection may receive an event it subscribed to. Defaults to
|
|
60
|
+
* the event's own audience, which fails closed: an event naming no audience
|
|
61
|
+
* reaches no connection.
|
|
62
|
+
*/
|
|
63
|
+
readonly admits?: (event: DomainEvent, connection: PushConnection) => boolean;
|
|
64
|
+
/**
|
|
65
|
+
* What a reconnecting connection missed, answered from a read model the host
|
|
66
|
+
* already has. Omitted, a connection with a cursor is told to re-read every
|
|
67
|
+
* topic it holds.
|
|
68
|
+
*/
|
|
69
|
+
readonly resume?: (cursor: string, topics: readonly string[]) => Stream.Stream<PushMessage>;
|
|
70
|
+
}
|
|
71
|
+
/** Events a connection subscribed to but its audience did not admit. */
|
|
72
|
+
export declare const pushFiltered: Metric.Counter<number>;
|
|
73
|
+
/**
|
|
74
|
+
* The in-process hub every connection on this replica reads from.
|
|
75
|
+
*
|
|
76
|
+
* One per process, fed by the host's broadcast subscription. It holds no
|
|
77
|
+
* backlog: a connection that was not open when an event was published learns
|
|
78
|
+
* of it through `resume`, never from here.
|
|
79
|
+
*/
|
|
80
|
+
export declare const makeServerPush: ({ topicsOf, admits, resume, }: ServerPushOptions) => Effect.Effect<ServerPush, never, Scope.Scope>;
|
|
81
|
+
export {};
|
|
82
|
+
//# sourceMappingURL=server-push.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-push.d.ts","sourceRoot":"","sources":["../../src/push/server-push.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,WAAW,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAU,KAAK,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAE7E;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc;IAC7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jC;AAED,wEAAwE;AACxE,eAAO,MAAM,YAAY,wBAGvB,CAAC;AAUH;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,+BAI5B,iBAAiB,KAAG,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,CA4C/D,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { audienceAdmits } from '@entifix/core';
|
|
2
|
+
import { Context, Effect, Metric, PubSub, Stream } from 'effect';
|
|
3
|
+
export class ServerPushTag extends Context.Service()('@entifix/service-shell/ServerPush') {
|
|
4
|
+
}
|
|
5
|
+
/** Events a connection subscribed to but its audience did not admit. */ export const pushFiltered = Metric.counter('entifix_push_filtered_total', {
|
|
6
|
+
description: 'Pushed events withheld from a subscribed connection by its audience.'
|
|
7
|
+
});
|
|
8
|
+
const defaultAdmits = (event, connection)=>audienceAdmits(event.audience, connection.recipient);
|
|
9
|
+
const resyncAll = (_cursor, topics)=>Stream.make({
|
|
10
|
+
_tag: 'resync',
|
|
11
|
+
topics
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* The in-process hub every connection on this replica reads from.
|
|
15
|
+
*
|
|
16
|
+
* One per process, fed by the host's broadcast subscription. It holds no
|
|
17
|
+
* backlog: a connection that was not open when an event was published learns
|
|
18
|
+
* of it through `resume`, never from here.
|
|
19
|
+
*/ export const makeServerPush = ({ topicsOf, admits = defaultAdmits, resume = resyncAll })=>Effect.gen(function*() {
|
|
20
|
+
const hub = yield* Effect.acquireRelease(PubSub.unbounded(), PubSub.shutdown);
|
|
21
|
+
// The event as this connection receives it, or `undefined` when it asked
|
|
22
|
+
// for none of the event's topics or its audience does not admit it.
|
|
23
|
+
const delivered = (connection)=>{
|
|
24
|
+
const wanted = new Set(connection.topics);
|
|
25
|
+
return (event)=>{
|
|
26
|
+
const topics = topicsOf(event).filter((topic)=>wanted.has(topic));
|
|
27
|
+
if (topics.length === 0) return Effect.succeed(undefined);
|
|
28
|
+
if (admits(event, connection)) return Effect.succeed({
|
|
29
|
+
_tag: 'event',
|
|
30
|
+
event,
|
|
31
|
+
topics
|
|
32
|
+
});
|
|
33
|
+
return Effect.as(Metric.update(pushFiltered, 1), undefined);
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
publish: (event)=>Effect.asVoid(PubSub.publish(hub, event)),
|
|
38
|
+
// Subscribed before the catch-up runs, so an event published while
|
|
39
|
+
// `resume` is still reading is queued rather than falling in the gap
|
|
40
|
+
// between the two.
|
|
41
|
+
open: (connection)=>Stream.unwrap(Effect.map(PubSub.subscribe(hub), (subscription)=>{
|
|
42
|
+
const live = Stream.filter(Stream.mapEffect(Stream.fromSubscription(subscription), delivered(connection)), (message)=>message !== undefined);
|
|
43
|
+
return connection.cursor === undefined ? live : Stream.concat(resume(connection.cursor, connection.topics), live);
|
|
44
|
+
}))
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
//# sourceMappingURL=server-push.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/push/server-push.ts"],"sourcesContent":["import {\n audienceAdmits,\n type AudienceRecipient,\n type DomainEvent,\n} from '@entifix/core';\nimport { Context, Effect, Metric, PubSub, type Scope, Stream } from 'effect';\n\n/**\n * One open push connection: who holds it, what it asked for, and where it\n * left off.\n *\n * Every field comes from the request that opened it — the topics and the\n * cursor from its URL and `Last-Event-ID`, the recipient from the verified\n * session or the flow capabilities it presented. Nothing about a connection is\n * kept anywhere else, so any replica can answer its reconnect\n * ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).\n */\nexport interface PushConnection {\n readonly recipient: AudienceRecipient;\n /** `entity:<key>`, `record:<key>:<id>` or `flow:<id>`. */\n readonly topics: readonly string[];\n /** The `Last-Event-ID` the client reconnected with, if any. */\n readonly cursor?: string;\n}\n\n/**\n * What a connection receives: an event, with the topics of this connection it\n * matched, or a request to re-read some of its topics because what it missed\n * cannot be replayed.\n *\n * The matched topics travel with the event so the browser routes it to the\n * listeners that asked, without knowing the host's topic vocabulary.\n */\nexport type PushMessage =\n | {\n readonly _tag: 'event';\n readonly event: DomainEvent;\n readonly topics: readonly string[];\n }\n | { readonly _tag: 'resync'; readonly topics: readonly string[] };\n\n/** The server half of push: what a publisher feeds and what a connection reads. */\nexport interface ServerPush {\n /** Offers one event to every open connection that subscribed and is admitted. */\n readonly publish: (event: DomainEvent) => Effect.Effect<void>;\n /**\n * The messages for one connection: the resume hook's catch-up first, then\n * every live event matching its topics and its audience, until the\n * connection's scope closes.\n */\n readonly open: (connection: PushConnection) => Stream.Stream<PushMessage>;\n}\n\nexport class ServerPushTag extends Context.Service<ServerPushTag, ServerPush>()(\n '@entifix/service-shell/ServerPush',\n) {}\n\nexport interface ServerPushOptions {\n /**\n * The topics an event belongs to. An event in no topic reaches nobody.\n *\n * Host vocabulary: an entity change might be `entity:<key>` and\n * `record:<key>:<id>`, a flow outcome `flow:<id>`.\n */\n readonly topicsOf: (event: DomainEvent) => readonly string[];\n /**\n * Whether a connection may receive an event it subscribed to. Defaults to\n * the event's own audience, which fails closed: an event naming no audience\n * reaches no connection.\n */\n readonly admits?: (event: DomainEvent, connection: PushConnection) => boolean;\n /**\n * What a reconnecting connection missed, answered from a read model the host\n * already has. Omitted, a connection with a cursor is told to re-read every\n * topic it holds.\n */\n readonly resume?: (\n cursor: string,\n topics: readonly string[],\n ) => Stream.Stream<PushMessage>;\n}\n\n/** Events a connection subscribed to but its audience did not admit. */\nexport const pushFiltered = Metric.counter('entifix_push_filtered_total', {\n description:\n 'Pushed events withheld from a subscribed connection by its audience.',\n});\n\nconst defaultAdmits = (event: DomainEvent, connection: PushConnection) =>\n audienceAdmits(event.audience, connection.recipient);\n\nconst resyncAll = (\n _cursor: string,\n topics: readonly string[],\n): Stream.Stream<PushMessage> => Stream.make({ _tag: 'resync', topics });\n\n/**\n * The in-process hub every connection on this replica reads from.\n *\n * One per process, fed by the host's broadcast subscription. It holds no\n * backlog: a connection that was not open when an event was published learns\n * of it through `resume`, never from here.\n */\nexport const makeServerPush = ({\n topicsOf,\n admits = defaultAdmits,\n resume = resyncAll,\n}: ServerPushOptions): Effect.Effect<ServerPush, never, Scope.Scope> =>\n Effect.gen(function* () {\n const hub = yield* Effect.acquireRelease(\n PubSub.unbounded<DomainEvent>(),\n PubSub.shutdown,\n );\n\n // The event as this connection receives it, or `undefined` when it asked\n // for none of the event's topics or its audience does not admit it.\n const delivered = (connection: PushConnection) => {\n const wanted = new Set(connection.topics);\n return (event: DomainEvent): Effect.Effect<PushMessage | undefined> => {\n const topics = topicsOf(event).filter(topic => wanted.has(topic));\n if (topics.length === 0) return Effect.succeed(undefined);\n if (admits(event, connection))\n return Effect.succeed({ _tag: 'event', event, topics });\n return Effect.as(Metric.update(pushFiltered, 1), undefined);\n };\n };\n\n return {\n publish: event => Effect.asVoid(PubSub.publish(hub, event)),\n // Subscribed before the catch-up runs, so an event published while\n // `resume` is still reading is queued rather than falling in the gap\n // between the two.\n open: connection =>\n Stream.unwrap(\n Effect.map(PubSub.subscribe(hub), subscription => {\n const live = Stream.filter(\n Stream.mapEffect(\n Stream.fromSubscription(subscription),\n delivered(connection),\n ),\n (message): message is PushMessage => message !== undefined,\n );\n return connection.cursor === undefined\n ? live\n : Stream.concat(\n resume(connection.cursor, connection.topics),\n live,\n );\n }),\n ),\n };\n });\n"],"names":["audienceAdmits","Context","Effect","Metric","PubSub","Stream","ServerPushTag","Service","pushFiltered","counter","description","defaultAdmits","event","connection","audience","recipient","resyncAll","_cursor","topics","make","_tag","makeServerPush","topicsOf","admits","resume","gen","hub","acquireRelease","unbounded","shutdown","delivered","wanted","Set","filter","topic","has","length","succeed","undefined","as","update","publish","asVoid","open","unwrap","map","subscribe","subscription","live","mapEffect","fromSubscription","message","cursor","concat"],"mappings":"AAAA,SACEA,cAAc,QAGT,gBAAgB;AACvB,SAASC,OAAO,EAAEC,MAAM,EAAEC,MAAM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AAO2C,GAAG,CAACzC,OAAO0C,SAAS,CAACpB,MAAMqB,CAAAA;oBAChC,MAAMC,OAAO3C,OAAO4B,MAAM,CACxB5B,OAAO4C,SAAS,CACd5C,OAAO6C,gBAAgB,CAACH,eACxBjB,UAAUjB,cAEZ,CAACsC,UAAoCA,YAAYb;oBAEnD,OAAOzB,WAAWuC,MAAM,KAAKd,YACzBU,OACA3C,OAAOgD,MAAM,CACX7B,OAAOX,WAAWuC,MAAM,EAAEvC,WAAWK,MAAM,GAC3C8B;gBAER;QAEN;IACF,GAAG"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Duration, Effect, Metric, type Scope } from 'effect';
|
|
2
|
+
import { HttpServerResponse } from 'effect/http';
|
|
3
|
+
import { type PushConnection, ServerPushTag } from './server-push.js';
|
|
4
|
+
/**
|
|
5
|
+
* How often an idle push connection sends a comment frame.
|
|
6
|
+
*
|
|
7
|
+
* 15 s sits under the common 30 s idle timeout with room for one lost frame.
|
|
8
|
+
* An edge in front of the stream must hold an idle connection for more than
|
|
9
|
+
* twice this, or it closes healthy streams
|
|
10
|
+
* ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).
|
|
11
|
+
*/
|
|
12
|
+
export declare const PUSH_HEARTBEAT: Duration.Duration;
|
|
13
|
+
export interface SsePushDials {
|
|
14
|
+
/**
|
|
15
|
+
* The range a connection's `retry:` is drawn from, in milliseconds.
|
|
16
|
+
*
|
|
17
|
+
* Drawn per connection, so a redeploy that closes every stream at once does
|
|
18
|
+
* not have every browser reconnect in the same instant.
|
|
19
|
+
*/
|
|
20
|
+
readonly retryMs: {
|
|
21
|
+
readonly min: number;
|
|
22
|
+
readonly max: number;
|
|
23
|
+
};
|
|
24
|
+
/** How long before the token's `exp` the connection asks for a reauth. */
|
|
25
|
+
readonly reauthMarginMs: number;
|
|
26
|
+
/**
|
|
27
|
+
* How long a shutdown waits for its drained connections to send their last
|
|
28
|
+
* frame, in milliseconds. Defaults to 5 000.
|
|
29
|
+
*/
|
|
30
|
+
readonly drainWindowMs?: number;
|
|
31
|
+
/** Injectable for tests; `[0, 1)`, defaults to `Math.random`. */
|
|
32
|
+
readonly random?: () => number;
|
|
33
|
+
}
|
|
34
|
+
export interface SsePushConnection extends PushConnection {
|
|
35
|
+
/**
|
|
36
|
+
* Unix seconds at which the credential that opened the stream expires — the
|
|
37
|
+
* verified token's `exp`. Omitted for a connection with no expiring
|
|
38
|
+
* credential, such as one opened with flow capabilities only.
|
|
39
|
+
*/
|
|
40
|
+
readonly expiresAt?: number;
|
|
41
|
+
}
|
|
42
|
+
export interface SsePush {
|
|
43
|
+
/** Serves one connection as `text/event-stream`. */
|
|
44
|
+
readonly respond: (connection: SsePushConnection) => HttpServerResponse.HttpServerResponse;
|
|
45
|
+
}
|
|
46
|
+
/** Open push connections on this process. */
|
|
47
|
+
export declare const pushConnections: Metric.Gauge<number>;
|
|
48
|
+
export declare const pushOpened: Metric.Counter<number>;
|
|
49
|
+
export declare const pushClosed: Metric.Counter<number>;
|
|
50
|
+
export declare const pushDrained: Metric.Counter<number>;
|
|
51
|
+
export declare const pushReauth: Metric.Counter<number>;
|
|
52
|
+
export declare const pushUnencodable: Metric.Counter<number>;
|
|
53
|
+
/**
|
|
54
|
+
* The SSE adapter of the push port.
|
|
55
|
+
*
|
|
56
|
+
* Every connection opens with its own randomized `retry:`, sends a comment
|
|
57
|
+
* every {@link PUSH_HEARTBEAT}, asks for a reauth at `exp − margin` and closes
|
|
58
|
+
* cleanly instead of failing a reconnect with a 401, and is drained — a fresh
|
|
59
|
+
* `retry:` and then EOF — when this adapter's scope closes.
|
|
60
|
+
*/
|
|
61
|
+
export declare const makeSsePush: ({ retryMs, reauthMarginMs, drainWindowMs, random, }: SsePushDials) => Effect.Effect<SsePush, never, ServerPushTag | Scope.Scope>;
|
|
62
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceR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AM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,aAAa,GAAG,KAAK,CAAC,KAAK,CAyIvE,CAAC"}
|
package/dist/push/sse.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { encodeEvent } from '@entifix/core';
|
|
2
|
+
import { Clock, Deferred, Duration, Effect, Exit, Metric, Ref, Stream } from 'effect';
|
|
3
|
+
import { HttpServerResponse } from 'effect/http';
|
|
4
|
+
import { ServerPushTag } from './server-push.js';
|
|
5
|
+
/**
|
|
6
|
+
* How often an idle push connection sends a comment frame.
|
|
7
|
+
*
|
|
8
|
+
* 15 s sits under the common 30 s idle timeout with room for one lost frame.
|
|
9
|
+
* An edge in front of the stream must hold an idle connection for more than
|
|
10
|
+
* twice this, or it closes healthy streams
|
|
11
|
+
* ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).
|
|
12
|
+
*/ export const PUSH_HEARTBEAT = Duration.seconds(15);
|
|
13
|
+
/** Open push connections on this process. */ export const pushConnections = Metric.gauge('entifix_push_connections', {
|
|
14
|
+
description: 'Open server-push connections.'
|
|
15
|
+
});
|
|
16
|
+
export const pushOpened = Metric.counter('entifix_push_opened_total', {
|
|
17
|
+
description: 'Server-push connections opened.'
|
|
18
|
+
});
|
|
19
|
+
export const pushClosed = Metric.counter('entifix_push_closed_total', {
|
|
20
|
+
description: 'Server-push connections closed, for any reason.'
|
|
21
|
+
});
|
|
22
|
+
export const pushDrained = Metric.counter('entifix_push_drained_total', {
|
|
23
|
+
description: 'Server-push connections closed by a drain on shutdown.'
|
|
24
|
+
});
|
|
25
|
+
export const pushReauth = Metric.counter('entifix_push_reauth_total', {
|
|
26
|
+
description: 'Server-push connections asked to reauthenticate before exp.'
|
|
27
|
+
});
|
|
28
|
+
export const pushUnencodable = Metric.counter('entifix_push_unencodable_total', {
|
|
29
|
+
description: 'Pushed events dropped because they do not encode.'
|
|
30
|
+
});
|
|
31
|
+
const encoder = new TextEncoder();
|
|
32
|
+
// `data` is `{ topics, envelope }`: the topics of this connection the event
|
|
33
|
+
// matched, and the event as its registered codec encodes it.
|
|
34
|
+
const eventFrame = (event, topics)=>Effect.exit(encodeEvent(event)).pipe(Effect.flatMap((encoded)=>Exit.isSuccess(encoded) ? Effect.succeed(`id: ${event.id}\ndata: ${JSON.stringify({
|
|
35
|
+
topics,
|
|
36
|
+
envelope: encoded.value
|
|
37
|
+
})}\n\n`) : Effect.as(Metric.update(pushUnencodable, 1), undefined)));
|
|
38
|
+
const frameOf = (message)=>message._tag === 'event' ? eventFrame(message.event, message.topics) : Effect.succeed(`event: resync\ndata: ${JSON.stringify({
|
|
39
|
+
topics: message.topics
|
|
40
|
+
})}\n\n`);
|
|
41
|
+
/**
|
|
42
|
+
* The SSE adapter of the push port.
|
|
43
|
+
*
|
|
44
|
+
* Every connection opens with its own randomized `retry:`, sends a comment
|
|
45
|
+
* every {@link PUSH_HEARTBEAT}, asks for a reauth at `exp − margin` and closes
|
|
46
|
+
* cleanly instead of failing a reconnect with a 401, and is drained — a fresh
|
|
47
|
+
* `retry:` and then EOF — when this adapter's scope closes.
|
|
48
|
+
*/ export const makeSsePush = ({ retryMs, reauthMarginMs, drainWindowMs = 5000, random = Math.random })=>Effect.gen(function*() {
|
|
49
|
+
const push = yield* ServerPushTag;
|
|
50
|
+
// Each open connection's drain signal, and what it completes once its last
|
|
51
|
+
// frame is out.
|
|
52
|
+
const live = new Map();
|
|
53
|
+
const retry = ()=>Math.round(retryMs.min + random() * (retryMs.max - retryMs.min));
|
|
54
|
+
// Drain on shutdown: every connection still open is told to come back
|
|
55
|
+
// later, at its own randomized interval, and then ends. The finalizer waits
|
|
56
|
+
// for those last frames — up to the window — because the hub the
|
|
57
|
+
// connections read from closes right after it.
|
|
58
|
+
yield* Effect.addFinalizer(()=>{
|
|
59
|
+
const open = Array.from(live);
|
|
60
|
+
return Effect.forEach(open, ([drain])=>Deferred.succeed(drain, undefined), {
|
|
61
|
+
discard: true
|
|
62
|
+
}).pipe(Effect.andThen(Effect.forEach(open, ([, done])=>Deferred.await(done), {
|
|
63
|
+
discard: true
|
|
64
|
+
})), Effect.timeoutOption(Duration.millis(drainWindowMs)));
|
|
65
|
+
});
|
|
66
|
+
const frames = (connection)=>Stream.unwrap(Effect.gen(function*() {
|
|
67
|
+
const drain = yield* Deferred.make();
|
|
68
|
+
const done = yield* Deferred.make();
|
|
69
|
+
const ending = yield* Ref.make(undefined);
|
|
70
|
+
const now = yield* Clock.currentTimeMillis;
|
|
71
|
+
live.set(drain, done);
|
|
72
|
+
yield* Metric.update(pushOpened, 1);
|
|
73
|
+
yield* Metric.update(pushConnections, live.size);
|
|
74
|
+
const drained = Effect.as(Deferred.await(drain), 'drain');
|
|
75
|
+
const closing = connection.expiresAt === undefined ? drained : Effect.raceFirst(Effect.as(Effect.sleep(Duration.millis(Math.max(0, connection.expiresAt * 1000 - reauthMarginMs - now))), 'reauth'), drained);
|
|
76
|
+
const messages = Stream.filter(Stream.mapEffect(push.open(connection), frameOf), (frame)=>frame !== undefined);
|
|
77
|
+
const heartbeat = Stream.fromEffectRepeat(Effect.as(Effect.sleep(PUSH_HEARTBEAT), ': heartbeat\n\n'));
|
|
78
|
+
// The opening frame is sent at once: a chunked response writes its
|
|
79
|
+
// headers with its first chunk, so a quiet stream would otherwise
|
|
80
|
+
// leave the browser in `CONNECTING`.
|
|
81
|
+
const body = Stream.concat(Stream.make(`retry: ${retry()}\n: open\n\n`), Stream.merge(messages, heartbeat, {
|
|
82
|
+
haltStrategy: 'left'
|
|
83
|
+
}));
|
|
84
|
+
const bounded = Stream.interruptWhen(body, Effect.flatMap(closing, (reason)=>Ref.set(ending, reason)));
|
|
85
|
+
const last = Stream.unwrap(Effect.map(Ref.get(ending), (reason)=>{
|
|
86
|
+
switch(reason){
|
|
87
|
+
case 'reauth':
|
|
88
|
+
return Stream.fromEffect(Effect.as(Metric.update(pushReauth, 1), 'event: reauth\ndata: {}\n\n'));
|
|
89
|
+
case 'drain':
|
|
90
|
+
return Stream.fromEffect(Effect.as(Metric.update(pushDrained, 1), `retry: ${retry()}\n\n`));
|
|
91
|
+
default:
|
|
92
|
+
return Stream.empty;
|
|
93
|
+
}
|
|
94
|
+
}));
|
|
95
|
+
return Stream.ensuring(Stream.concat(bounded, last), Effect.suspend(()=>{
|
|
96
|
+
live.delete(drain);
|
|
97
|
+
return Effect.all([
|
|
98
|
+
Metric.update(pushClosed, 1),
|
|
99
|
+
Metric.update(pushConnections, live.size),
|
|
100
|
+
Deferred.succeed(done, undefined)
|
|
101
|
+
]);
|
|
102
|
+
}));
|
|
103
|
+
}));
|
|
104
|
+
return {
|
|
105
|
+
respond: (connection)=>HttpServerResponse.stream(Stream.map(frames(connection), (frame)=>encoder.encode(frame)), {
|
|
106
|
+
contentType: 'text/event-stream',
|
|
107
|
+
headers: {
|
|
108
|
+
// Scoped to one principal: a cached stream is served to whoever
|
|
109
|
+
// asks next.
|
|
110
|
+
'cache-control': 'no-store',
|
|
111
|
+
connection: 'keep-alive',
|
|
112
|
+
// A buffering proxy would hold every frame until the stream
|
|
113
|
+
// ends — that is, forever.
|
|
114
|
+
'x-accel-buffering': 'no'
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/push/sse.ts"],"sourcesContent":["import { type DomainEvent, encodeEvent } from '@entifix/core';\nimport {\n Clock,\n Deferred,\n Duration,\n Effect,\n Exit,\n Metric,\n Ref,\n type Scope,\n Stream,\n} from 'effect';\nimport { HttpServerResponse } from 'effect/http';\n\nimport {\n type PushConnection,\n type PushMessage,\n ServerPushTag,\n} from './server-push.js';\n\n/**\n * How often an idle push connection sends a comment frame.\n *\n * 15 s sits under the common 30 s idle timeout with room for one lost frame.\n * An edge in front of the stream must hold an idle connection for more than\n * twice this, or it closes healthy streams\n * ([ADR 0021](../../../../../docs/adr/0021-server-push-and-the-multiplexed-channel.md)).\n */\nexport const PUSH_HEARTBEAT = Duration.seconds(15);\n\nexport interface SsePushDials {\n /**\n * The range a connection's `retry:` is drawn from, in milliseconds.\n *\n * Drawn per connection, so a redeploy that closes every stream at once does\n * not have every browser reconnect in the same instant.\n */\n readonly retryMs: { readonly min: number; readonly max: number };\n /** How long before the token's `exp` the connection asks for a reauth. */\n readonly reauthMarginMs: number;\n /**\n * How long a shutdown waits for its drained connections to send their last\n * frame, in milliseconds. Defaults to 5 000.\n */\n readonly drainWindowMs?: number;\n /** Injectable for tests; `[0, 1)`, defaults to `Math.random`. */\n readonly random?: () => number;\n}\n\nexport interface SsePushConnection extends PushConnection {\n /**\n * Unix seconds at which the credential that opened the stream expires — the\n * verified token's `exp`. Omitted for a connection with no expiring\n * credential, such as one opened with flow capabilities only.\n */\n readonly expiresAt?: number;\n}\n\nexport interface SsePush {\n /** Serves one connection as `text/event-stream`. */\n readonly respond: (\n connection: SsePushConnection,\n ) => HttpServerResponse.HttpServerResponse;\n}\n\n/** Open push connections on this process. */\nexport const pushConnections = Metric.gauge('entifix_push_connections', {\n description: 'Open server-push connections.',\n});\nexport const pushOpened = Metric.counter('entifix_push_opened_total', {\n description: 'Server-push connections opened.',\n});\nexport const pushClosed = Metric.counter('entifix_push_closed_total', {\n description: 'Server-push connections closed, for any reason.',\n});\nexport const pushDrained = Metric.counter('entifix_push_drained_total', {\n description: 'Server-push connections closed by a drain on shutdown.',\n});\nexport const pushReauth = Metric.counter('entifix_push_reauth_total', {\n description: 'Server-push connections asked to reauthenticate before exp.',\n});\nexport const pushUnencodable = Metric.counter(\n 'entifix_push_unencodable_total',\n { description: 'Pushed events dropped because they do not encode.' },\n);\n\nconst encoder = new TextEncoder();\n\n// `data` is `{ topics, envelope }`: the topics of this connection the event\n// matched, and the event as its registered codec encodes it.\nconst eventFrame = (\n event: DomainEvent,\n topics: readonly string[],\n): Effect.Effect<string | undefined> =>\n Effect.exit(encodeEvent(event)).pipe(\n Effect.flatMap(encoded =>\n Exit.isSuccess(encoded)\n ? Effect.succeed(\n `id: ${event.id}\\ndata: ${JSON.stringify({ topics, envelope: encoded.value })}\\n\\n`,\n )\n : Effect.as(Metric.update(pushUnencodable, 1), undefined),\n ),\n );\n\nconst frameOf = (message: PushMessage): Effect.Effect<string | undefined> =>\n message._tag === 'event'\n ? eventFrame(message.event, message.topics)\n : Effect.succeed(\n `event: resync\\ndata: ${JSON.stringify({ topics: message.topics })}\\n\\n`,\n );\n\ntype Ending = 'reauth' | 'drain';\n\n/**\n * The SSE adapter of the push port.\n *\n * Every connection opens with its own randomized `retry:`, sends a comment\n * every {@link PUSH_HEARTBEAT}, asks for a reauth at `exp − margin` and closes\n * cleanly instead of failing a reconnect with a 401, and is drained — a fresh\n * `retry:` and then EOF — when this adapter's scope closes.\n */\nexport const makeSsePush = ({\n retryMs,\n reauthMarginMs,\n drainWindowMs = 5_000,\n random = Math.random,\n}: SsePushDials): Effect.Effect<SsePush, never, ServerPushTag | Scope.Scope> =>\n Effect.gen(function* () {\n const push = yield* ServerPushTag;\n // Each open connection's drain signal, and what it completes once its last\n // frame is out.\n const live = new Map<Deferred.Deferred<void>, Deferred.Deferred<void>>();\n\n const retry = () =>\n Math.round(retryMs.min + random() * (retryMs.max - retryMs.min));\n\n // Drain on shutdown: every connection still open is told to come back\n // later, at its own randomized interval, and then ends. The finalizer waits\n // for those last frames — up to the window — because the hub the\n // connections read from closes right after it.\n yield* Effect.addFinalizer(() => {\n const open = Array.from(live);\n return Effect.forEach(\n open,\n ([drain]) => Deferred.succeed(drain, undefined),\n { discard: true },\n ).pipe(\n Effect.andThen(\n Effect.forEach(open, ([, done]) => Deferred.await(done), {\n discard: true,\n }),\n ),\n Effect.timeoutOption(Duration.millis(drainWindowMs)),\n );\n });\n\n const frames = (connection: SsePushConnection) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const drain = yield* Deferred.make<void>();\n const done = yield* Deferred.make<void>();\n const ending = yield* Ref.make<Ending | undefined>(undefined);\n const now = yield* Clock.currentTimeMillis;\n\n live.set(drain, done);\n yield* Metric.update(pushOpened, 1);\n yield* Metric.update(pushConnections, live.size);\n\n const drained = Effect.as(Deferred.await(drain), 'drain' as const);\n const closing: Effect.Effect<Ending> =\n connection.expiresAt === undefined\n ? drained\n : Effect.raceFirst(\n Effect.as(\n Effect.sleep(\n Duration.millis(\n Math.max(\n 0,\n connection.expiresAt * 1000 - reauthMarginMs - now,\n ),\n ),\n ),\n 'reauth' as const,\n ),\n drained,\n );\n\n const messages = Stream.filter(\n Stream.mapEffect(push.open(connection), frameOf),\n (frame): frame is string => frame !== undefined,\n );\n const heartbeat = Stream.fromEffectRepeat(\n Effect.as(Effect.sleep(PUSH_HEARTBEAT), ': heartbeat\\n\\n'),\n );\n\n // The opening frame is sent at once: a chunked response writes its\n // headers with its first chunk, so a quiet stream would otherwise\n // leave the browser in `CONNECTING`.\n const body = Stream.concat(\n Stream.make(`retry: ${retry()}\\n: open\\n\\n`),\n Stream.merge(messages, heartbeat, { haltStrategy: 'left' }),\n );\n\n const bounded = Stream.interruptWhen(\n body,\n Effect.flatMap(closing, reason => Ref.set(ending, reason)),\n );\n\n const last = Stream.unwrap(\n Effect.map(Ref.get(ending), reason => {\n switch (reason) {\n case 'reauth':\n return Stream.fromEffect(\n Effect.as(\n Metric.update(pushReauth, 1),\n 'event: reauth\\ndata: {}\\n\\n',\n ),\n );\n case 'drain':\n return Stream.fromEffect(\n Effect.as(\n Metric.update(pushDrained, 1),\n `retry: ${retry()}\\n\\n`,\n ),\n );\n default:\n return Stream.empty;\n }\n }),\n );\n\n return Stream.ensuring(\n Stream.concat(bounded, last),\n Effect.suspend(() => {\n live.delete(drain);\n return Effect.all([\n Metric.update(pushClosed, 1),\n Metric.update(pushConnections, live.size),\n Deferred.succeed(done, undefined),\n ]);\n }),\n );\n }),\n );\n\n return {\n respond: connection =>\n HttpServerResponse.stream(\n Stream.map(frames(connection), frame => encoder.encode(frame)),\n {\n contentType: 'text/event-stream',\n headers: {\n // Scoped to one principal: a cached stream is served to whoever\n // asks next.\n 'cache-control': 'no-store',\n connection: 'keep-alive',\n // A buffering proxy would hold every frame until the stream\n // ends — that is, forever.\n 'x-accel-buffering': 'no',\n },\n },\n ),\n };\n });\n"],"names":["encodeEvent","Clock","Deferred","Duration","Effect","Exit","Metric","Ref","Stream","HttpServerResponse","ServerPushTag","PUSH_HEARTBEAT","seconds","pushConnections","gauge","description","pushOpened","counter","pushClosed","pushDrained","pushReauth","pushUnencodable","encoder","TextEncoder","eventFrame","event","topics","exit","pipe","flatMap","encoded","isSuccess","succeed","id","JSON","stringify","envelope","value","as","update","undefined","frameOf","message","_tag","makeSsePush","retryMs","reauthMarginMs","drainWindowMs","random","Math","gen","push","live","Map","retry","round","min","max","addFinalizer","open","Array","from","forEach","drain","discard","andThen","done","await","timeoutOption","millis","frames","connection","unwrap","make","ending","now","currentTimeMillis","set","size","drained","closing","expiresAt","raceFirst","sleep","messages","filter","mapEffect","frame","heartbeat","fromEffectRepeat","body","concat","merge","haltStrategy","bounded","interruptWhen","reason","last","map","get","fromEffect","empty","ensuring","suspend","delete","all","respond","stream","encode","contentType","headers"],"mappings":"AAAA,SAA2BA,WAAW,QAAQ,gBAAgB;AAC9D,SACEC,KAAK,EACLC,QAAQ,EACRC,QAAQ,EACRC,MAAM,EACNC,IAAI,EACJC,MAAM,EACNC,GAAG,EAEHC,MAAM,QACD,SAAS;AAChB,SAASC,kBAAkB,QAAQ,cAAc;AAEjD,SAGEC,aAAa,QACR,mBAAmB;AAE1B;;;;;;;CAOC,GACD,OAAO,MAAMC,iBAAiBR,SAASS,OAAO,CAAC,IAAI;AAqCnD,2CAA2C,GAC3C,OAAO,MAAMC,kBAAkBP,OAAOQ,KAAK,CAAC,4BAA4B;IACtEC,aAAa;AACf,GAAG;AACH,OAAO,MAAMC,aAAaV,OAAOW,OAAO,CAAC,6BAA6B;IACpEF,aAAa;AACf,GAAG;AACH,OAAO,MAAMG,aAAaZ,O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M,CAAC1B,iBAAiBuC,KAAK0B,IAAI;wBACxC5E,SAAS8B,OAAO,CAACkC,MAAM1B;qBACxB;gBACH;YAEJ;QAGJ,OAAO;YACLkE,SAASnC,CAAAA,aACP9D,mBAAmBkG,MAAM,CACvBnG,OAAO0F,GAAG,CAAC5B,OAAOC,aAAagB,CAAAA,QAASjE,QAAQsF,MAAM,CAACrB,SACvD;oBACEsB,aAAa;oBACbC,SAAS;wBACP,gEAAgE;wBAChE,aAAa;wBACb,iBAAiB;wBACjBvC,YAAY;wBACZ,4DAA4D;wBAC5D,2BAA2B;wBAC3B,qBAAqB;oBACvB;gBACF;QAEN;IACF,GAAG"}
|
package/dist/respond.d.ts
CHANGED
|
@@ -57,16 +57,10 @@ export declare const problemResponse: (failure: EntifixFailure, policy?: Failure
|
|
|
57
57
|
* away has nobody to answer.
|
|
58
58
|
*/
|
|
59
59
|
export declare const respond: <E extends RouteFailure, R>(effect: Effect.Effect<HttpServerResponse.HttpServerResponse, E, R>, policy?: FailurePolicy) => Effect.Effect<HttpServerResponse.HttpServerResponse, never, R>;
|
|
60
|
-
/**
|
|
61
|
-
* Mounts a set of routes with every handler wrapped in {@link respond}, once,
|
|
62
|
-
* at the composition root. A route whose handler fails with anything but a
|
|
63
|
-
* {@link RouteFailure} does not compile here.
|
|
64
|
-
*/
|
|
65
|
-
export declare const mountRoutes: <const Routes extends ReadonlyArray<HttpRouter.Route<RouteFailure, any>>>(routes: Routes, policy?: FailurePolicy) => import("effect/Layer").Layer<never, never, HttpRouter.HttpRouter | HttpRouter.Request.From<"Requires", Exclude<HttpRouter.Route.Context<{ readonly [K in keyof Routes]: HttpRouter.Route<never, HttpRouter.Route.Context<Routes[K]>>; }[number]>, HttpRouter.Provided>> | HttpRouter.Request.From<"Error", HttpRouter.Route.Error<{ readonly [K in keyof Routes]: HttpRouter.Route<never, HttpRouter.Route.Context<Routes[K]>>; }[number]>>>;
|
|
66
60
|
/**
|
|
67
61
|
* The router's last net. A path no route matches answers a `404` problem
|
|
68
62
|
* rather than an empty body, and a defect that escaped every route — one not
|
|
69
|
-
*
|
|
63
|
+
* wrapped in {@link respond}, or one thrown while a response was
|
|
70
64
|
* being written — answers the same opaque `500` problem. It catches those two
|
|
71
65
|
* and nothing else, never the whole cause, so an interruption still
|
|
72
66
|
* interrupts.
|
package/dist/respond.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"respond.d.ts","sourceRoot":"","sources":["../src/respond.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,cAAc,EACnB,KAAK,eAAe,EAMrB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAgB,MAAM,EAAU,MAAM,QAAQ,CAAC;AACtD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EACL,UAAU,EAGV,kBAAkB,EACnB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG,QAAQ,CAAC,aAAa,CAAC;AAEnE;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC,CAAC;IACnE,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IACrD,QAAQ,CAAC,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC,CAAC;IACvE,QAAQ,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;CAC1D;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,EAAE,aA+BlC,CAAC;AAEF,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC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AED,wEAAwE;AACxE,eAAO,MAAM,wBAAwB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAOvE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,GAC9B,SAAS,gBAAgB,KACxB,MAAM,CAAC,MAAM,CAAC,IAAI,CAUpB,CAAC"}
|
package/dist/respond.js
CHANGED
|
@@ -131,17 +131,10 @@ const answerFailure = (failure, policy)=>Effect.gen(function*() {
|
|
|
131
131
|
* an opaque `500`. Interruption is left alone — a request whose client went
|
|
132
132
|
* away has nobody to answer.
|
|
133
133
|
*/ export const respond = (effect, policy = defaultFailurePolicy)=>withRequestDeadline(effect).pipe(Effect.catch((failure)=>isFailure(failure) ? answerFailure(failure, policy) : answerDefect(failure)), Effect.catchDefect(answerDefect));
|
|
134
|
-
/**
|
|
135
|
-
* Mounts a set of routes with every handler wrapped in {@link respond}, once,
|
|
136
|
-
* at the composition root. A route whose handler fails with anything but a
|
|
137
|
-
* {@link RouteFailure} does not compile here.
|
|
138
|
-
*/ export const mountRoutes = (routes, policy = defaultFailurePolicy)=>HttpRouter.addAll(routes.map((route)=>HttpRouter.route(route.method, route.path, respond(route.handler, policy), {
|
|
139
|
-
uninterruptible: route.uninterruptible
|
|
140
|
-
})));
|
|
141
134
|
/**
|
|
142
135
|
* The router's last net. A path no route matches answers a `404` problem
|
|
143
136
|
* rather than an empty body, and a defect that escaped every route — one not
|
|
144
|
-
*
|
|
137
|
+
* wrapped in {@link respond}, or one thrown while a response was
|
|
145
138
|
* being written — answers the same opaque `500` problem. It catches those two
|
|
146
139
|
* and nothing else, never the whole cause, so an interruption still
|
|
147
140
|
* interrupts.
|