Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agentage/observability",
"version": "1.2.0",
"version": "1.2.1",
"description": "Shared observability kit for agentage services: OTLP trace bootstrap (node --import), pino logger preset with trace correlation, one-call error capture, and the estate /health envelope.",
"type": "module",
"license": "MIT",
Expand Down
5 changes: 4 additions & 1 deletion src/serve-health.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
import { healthResponse, type ChecksInput, type HealthSourceOptions } from './health.js';

/** The two paths the estate probes: the container HEALTHCHECK and the edge-routed one. */
Expand Down Expand Up @@ -59,6 +59,9 @@ export function serveHealth(
send(res, response, await response.text(), method === 'HEAD');
};

// Resolved at call time, not import time: the root barrel must stay loadable on the
// Next edge runtime, where `node:http` is absent (instrumentation.ts is bundled there).
const { createServer } = process.getBuiltinModule('node:http');
const server = createServer((req, res) => {
// A throwing check must answer 500, not kill the worker it is reporting on.
void handle(req, res).catch(() => {
Expand Down
44 changes: 44 additions & 0 deletions test/unit/edge-safe-barrel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { readFileSync } from 'node:fs';
import { dirname, join, normalize } from 'node:path';
import { describe, expect, it } from 'vitest';

// Next bundles instrumentation.ts for the edge runtime, where only a few node: builtins
// exist. Anything the root barrel reaches must therefore load without the rest of them.
const EDGE_BUILTINS = new Set(['node:async_hooks', 'node:events']);
const SRC = join(__dirname, '..', '..', 'src');

const staticImports = (file: string): string[] =>
[
...readFileSync(file, 'utf8').matchAll(
/^(?:import|export)\s+(?!type\s)[^'"]*from\s+['"]([^'"]+)['"]/gm
),
].map((m) => m[1]);

const reachable = (entry: string): Map<string, string[]> => {
const seen = new Map<string, string[]>();
const queue = [entry];
while (queue.length > 0) {
const file = queue.shift()!;
if (seen.has(file)) continue;
const specs = staticImports(file);
seen.set(file, specs);
for (const spec of specs) {
if (spec.startsWith('.'))
queue.push(normalize(join(dirname(file), spec.replace(/\.js$/, '.ts'))));
}
}
return seen;
};

describe('root barrel is edge-safe', () => {
it('reaches no node: builtin the Next edge runtime lacks', () => {
const offenders: string[] = [];
for (const [file, specs] of reachable(join(SRC, 'index.ts'))) {
for (const spec of specs) {
if (spec.startsWith('node:') && !EDGE_BUILTINS.has(spec))
offenders.push(`${file} -> ${spec}`);
}
}
expect(offenders).toEqual([]);
});
});
Loading