Merge ingest-api, collector-worker, admin-ui, database/error_reporting, deploy/lxc, docker-compose, and deployment docs into the dedicated repository. Paths updated for repo root (ER_REPO_ROOT, systemd WorkingDirectory). Root README documents layout. Co-Authored-By: Paperclip <[email protected]>
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import type { IngestEventInput } from "../types/ingest.js";
|
|
|
|
export type FetchHttpEventsResult =
|
|
| { ok: true; events: IngestEventInput[] }
|
|
| { ok: false; error: string };
|
|
|
|
function _isRecord(value: unknown): value is Record<string, unknown> {
|
|
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
function _isValidEvent(value: unknown): value is IngestEventInput {
|
|
if (!_isRecord(value)) {
|
|
return false;
|
|
}
|
|
if (value.schema_version !== 1) {
|
|
return false;
|
|
}
|
|
if (typeof value.occurred_at !== "string" || value.occurred_at.trim() === "") {
|
|
return false;
|
|
}
|
|
if (typeof value.level !== "string" || value.level.trim() === "") {
|
|
return false;
|
|
}
|
|
if (typeof value.message !== "string" || value.message.trim() === "") {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export async function fetchHttpPollEvents(input: {
|
|
endpointUri: string;
|
|
timeoutMs: number;
|
|
}): Promise<FetchHttpEventsResult> {
|
|
const _controller = new AbortController();
|
|
const _timer = setTimeout(() => _controller.abort(), input.timeoutMs);
|
|
try {
|
|
const _res = await fetch(input.endpointUri, {
|
|
method: "GET",
|
|
headers: { Accept: "application/json" },
|
|
signal: _controller.signal,
|
|
});
|
|
if (!_res.ok) {
|
|
return { ok: false, error: `http_status_${_res.status}` };
|
|
}
|
|
const _text = await _res.text();
|
|
let _json: unknown;
|
|
try {
|
|
_json = JSON.parse(_text) as unknown;
|
|
} catch {
|
|
return { ok: false, error: "invalid_json" };
|
|
}
|
|
if (!_isRecord(_json)) {
|
|
return { ok: false, error: "expected_json_object" };
|
|
}
|
|
const _eventsRaw = _json.events;
|
|
if (!Array.isArray(_eventsRaw)) {
|
|
return { ok: false, error: "missing_events_array" };
|
|
}
|
|
if (_eventsRaw.length > 50) {
|
|
return { ok: false, error: "too_many_events_max_50" };
|
|
}
|
|
const _events: IngestEventInput[] = [];
|
|
for (const _item of _eventsRaw) {
|
|
if (!_isValidEvent(_item)) {
|
|
return { ok: false, error: "invalid_event_shape" };
|
|
}
|
|
_events.push(_item);
|
|
}
|
|
return { ok: true, events: _events };
|
|
} catch (_err) {
|
|
if (_err instanceof Error && _err.name === "AbortError") {
|
|
return { ok: false, error: "fetch_timeout" };
|
|
}
|
|
const _msg = _err instanceof Error ? _err.message : "fetch_failed";
|
|
return { ok: false, error: _msg };
|
|
} finally {
|
|
clearTimeout(_timer);
|
|
}
|
|
}
|