@hasna/logs 0.3.23 → 0.3.25
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/bun.lock +7 -6
- package/dist/cli/index.js +688 -3
- package/dist/{http-0wsh40x1.js → http-zm3ph78w.js} +1 -1
- package/dist/{index-hjzbctgt.js → index-5qznfyah.js} +2 -2
- package/dist/{index-2sbhn1ye.js → index-kezb178p.js} +2 -2
- package/dist/{index-zmayq5kj.js → index-p1vgwwsz.js} +1 -1
- package/dist/{index-vmr85wa1.js → index-pen6t0yc.js} +4334 -3119
- package/dist/mcp/index.js +3 -3
- package/dist/server/index.js +2 -2
- package/package.json +3 -2
- package/src/cli/index.ts +2 -0
- package/dist/export-yjaar93b.js +0 -10
- package/dist/health-9792c1rc.js +0 -8
- package/dist/health-egdb00st.js +0 -8
- package/dist/index-14dvwcf1.js +0 -45
- package/dist/index-1f2ghyhm.js +0 -540
- package/dist/index-4ba0sabv.js +0 -1241
- package/dist/index-4hj4sakk.js +0 -1241
- package/dist/index-5qwba140.js +0 -1241
- package/dist/index-5tvnhvgr.js +0 -536
- package/dist/index-6y8pmes4.js +0 -45
- package/dist/index-6zrkek5y.js +0 -9454
- package/dist/index-7qhh666n.js +0 -1241
- package/dist/index-86j0hn03.js +0 -540
- package/dist/index-exeq2gs6.js +0 -79
- package/dist/index-fzmz9aqs.js +0 -1241
- package/dist/index-g8dczzvv.js +0 -30
- package/dist/index-rbrsvsyh.js +0 -88
- package/dist/index-t97ttm0a.js +0 -543
- package/dist/index-wbsq8qjd.js +0 -1241
- package/dist/index-xjn8gam3.js +0 -39
- package/dist/index-yb8yd4j6.js +0 -39
- package/dist/jobs-02z4fzsn.js +0 -22
- package/dist/query-6s5gqkck.js +0 -15
- package/dist/query-shjjj67k.js +0 -14
- package/dist/query-tcg3bm9s.js +0 -14
package/dist/cli/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
import {
|
|
4
4
|
runJob
|
|
5
|
-
} from "../index-
|
|
5
|
+
} from "../index-kezb178p.js";
|
|
6
6
|
import {
|
|
7
7
|
PACKAGE_VERSION,
|
|
8
8
|
createPage,
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
listProjects,
|
|
14
14
|
resolveProjectId,
|
|
15
15
|
summarizeLogs
|
|
16
|
-
} from "../index-
|
|
16
|
+
} from "../index-pen6t0yc.js";
|
|
17
17
|
import {
|
|
18
18
|
createJob,
|
|
19
19
|
listJobs
|
|
@@ -1001,7 +1001,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
|
|
|
1001
1001
|
this._exitCallback = (err) => {
|
|
1002
1002
|
if (err.code !== "commander.executeSubCommandAsync") {
|
|
1003
1003
|
throw err;
|
|
1004
|
-
}
|
|
1004
|
+
}
|
|
1005
1005
|
};
|
|
1006
1006
|
}
|
|
1007
1007
|
return this;
|
|
@@ -2122,6 +2122,690 @@ var require_commander = __commonJS((exports) => {
|
|
|
2122
2122
|
exports.InvalidOptionArgumentError = InvalidArgumentError;
|
|
2123
2123
|
});
|
|
2124
2124
|
|
|
2125
|
+
// node_modules/@hasna/events/dist/commander.js
|
|
2126
|
+
import { chmod, mkdir, readFile, rename, writeFile } from "fs/promises";
|
|
2127
|
+
import { existsSync } from "fs";
|
|
2128
|
+
import { homedir } from "os";
|
|
2129
|
+
import { join } from "path";
|
|
2130
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
2131
|
+
import { randomUUID } from "crypto";
|
|
2132
|
+
import { spawn } from "child_process";
|
|
2133
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
2134
|
+
function getPathValue(input, path) {
|
|
2135
|
+
return path.split(".").reduce((value, part) => {
|
|
2136
|
+
if (value && typeof value === "object" && part in value) {
|
|
2137
|
+
return value[part];
|
|
2138
|
+
}
|
|
2139
|
+
return;
|
|
2140
|
+
}, input);
|
|
2141
|
+
}
|
|
2142
|
+
function wildcardToRegExp(pattern) {
|
|
2143
|
+
const escaped = pattern.replace(/[|\\{}()[\]^$+?.]/g, "\\$&").replace(/\*/g, ".*");
|
|
2144
|
+
return new RegExp(`^${escaped}$`);
|
|
2145
|
+
}
|
|
2146
|
+
function matchString(value, matcher) {
|
|
2147
|
+
if (matcher === undefined)
|
|
2148
|
+
return true;
|
|
2149
|
+
if (value === undefined)
|
|
2150
|
+
return false;
|
|
2151
|
+
const matchers = Array.isArray(matcher) ? matcher : [matcher];
|
|
2152
|
+
return matchers.some((item) => wildcardToRegExp(item).test(value));
|
|
2153
|
+
}
|
|
2154
|
+
function matchRecord(input, matcher) {
|
|
2155
|
+
if (!matcher)
|
|
2156
|
+
return true;
|
|
2157
|
+
return Object.entries(matcher).every(([path, expected]) => {
|
|
2158
|
+
const actual = getPathValue(input, path);
|
|
2159
|
+
if (typeof expected === "string" || Array.isArray(expected)) {
|
|
2160
|
+
return matchString(actual === undefined ? undefined : String(actual), expected);
|
|
2161
|
+
}
|
|
2162
|
+
return actual === expected;
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
function eventMatchesFilter(event, filter) {
|
|
2166
|
+
return matchString(event.source, filter.source) && matchString(event.type, filter.type) && matchString(event.subject, filter.subject) && matchString(event.severity, filter.severity) && matchRecord(event.data, filter.data) && matchRecord(event.metadata, filter.metadata);
|
|
2167
|
+
}
|
|
2168
|
+
function channelMatchesEvent(channel, event) {
|
|
2169
|
+
if (!channel.enabled)
|
|
2170
|
+
return false;
|
|
2171
|
+
if (!channel.filters || channel.filters.length === 0)
|
|
2172
|
+
return true;
|
|
2173
|
+
return channel.filters.some((filter) => eventMatchesFilter(event, filter));
|
|
2174
|
+
}
|
|
2175
|
+
var HASNA_EVENTS_DIR_ENV = "HASNA_EVENTS_DIR";
|
|
2176
|
+
var HASNA_EVENTS_HOME_ENV = "HASNA_EVENTS_HOME";
|
|
2177
|
+
function getEventsDataDir(override) {
|
|
2178
|
+
return override || process.env[HASNA_EVENTS_DIR_ENV] || process.env[HASNA_EVENTS_HOME_ENV] || join(homedir(), ".hasna", "events");
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
class JsonEventsStore {
|
|
2182
|
+
dataDir;
|
|
2183
|
+
channelsPath;
|
|
2184
|
+
eventsPath;
|
|
2185
|
+
deliveriesPath;
|
|
2186
|
+
constructor(dataDir = getEventsDataDir()) {
|
|
2187
|
+
this.dataDir = dataDir;
|
|
2188
|
+
this.channelsPath = join(dataDir, "channels.json");
|
|
2189
|
+
this.eventsPath = join(dataDir, "events.json");
|
|
2190
|
+
this.deliveriesPath = join(dataDir, "deliveries.json");
|
|
2191
|
+
}
|
|
2192
|
+
async init() {
|
|
2193
|
+
await mkdir(this.dataDir, { recursive: true, mode: 448 });
|
|
2194
|
+
await chmod(this.dataDir, 448).catch(() => {
|
|
2195
|
+
return;
|
|
2196
|
+
});
|
|
2197
|
+
await this.ensureArrayFile(this.channelsPath);
|
|
2198
|
+
await this.ensureArrayFile(this.eventsPath);
|
|
2199
|
+
await this.ensureArrayFile(this.deliveriesPath);
|
|
2200
|
+
}
|
|
2201
|
+
async addChannel(channel) {
|
|
2202
|
+
await this.init();
|
|
2203
|
+
const channels = await this.readJson(this.channelsPath, []);
|
|
2204
|
+
const index = channels.findIndex((item) => item.id === channel.id);
|
|
2205
|
+
if (index >= 0) {
|
|
2206
|
+
channels[index] = { ...channel, createdAt: channels[index].createdAt, updatedAt: new Date().toISOString() };
|
|
2207
|
+
} else {
|
|
2208
|
+
channels.push(channel);
|
|
2209
|
+
}
|
|
2210
|
+
await this.writeJson(this.channelsPath, channels);
|
|
2211
|
+
return index >= 0 ? channels[index] : channel;
|
|
2212
|
+
}
|
|
2213
|
+
async listChannels() {
|
|
2214
|
+
await this.init();
|
|
2215
|
+
return this.readJson(this.channelsPath, []);
|
|
2216
|
+
}
|
|
2217
|
+
async getChannel(id) {
|
|
2218
|
+
const channels = await this.listChannels();
|
|
2219
|
+
return channels.find((channel) => channel.id === id);
|
|
2220
|
+
}
|
|
2221
|
+
async removeChannel(id) {
|
|
2222
|
+
await this.init();
|
|
2223
|
+
const channels = await this.readJson(this.channelsPath, []);
|
|
2224
|
+
const next = channels.filter((channel) => channel.id !== id);
|
|
2225
|
+
await this.writeJson(this.channelsPath, next);
|
|
2226
|
+
return next.length !== channels.length;
|
|
2227
|
+
}
|
|
2228
|
+
async appendEvent(event) {
|
|
2229
|
+
await this.init();
|
|
2230
|
+
const events = await this.readJson(this.eventsPath, []);
|
|
2231
|
+
events.push(event);
|
|
2232
|
+
await this.writeJson(this.eventsPath, events);
|
|
2233
|
+
return event;
|
|
2234
|
+
}
|
|
2235
|
+
async listEvents() {
|
|
2236
|
+
await this.init();
|
|
2237
|
+
return this.readJson(this.eventsPath, []);
|
|
2238
|
+
}
|
|
2239
|
+
async findEventByIdentity(identity) {
|
|
2240
|
+
const events = await this.listEvents();
|
|
2241
|
+
return events.find((event) => identity.id !== undefined && event.id === identity.id || identity.dedupeKey !== undefined && event.dedupeKey === identity.dedupeKey);
|
|
2242
|
+
}
|
|
2243
|
+
async appendDelivery(result) {
|
|
2244
|
+
await this.init();
|
|
2245
|
+
const deliveries = await this.readJson(this.deliveriesPath, []);
|
|
2246
|
+
deliveries.push(result);
|
|
2247
|
+
await this.writeJson(this.deliveriesPath, deliveries);
|
|
2248
|
+
return result;
|
|
2249
|
+
}
|
|
2250
|
+
async listDeliveries() {
|
|
2251
|
+
await this.init();
|
|
2252
|
+
return this.readJson(this.deliveriesPath, []);
|
|
2253
|
+
}
|
|
2254
|
+
async exportData() {
|
|
2255
|
+
return {
|
|
2256
|
+
channels: await this.listChannels(),
|
|
2257
|
+
events: await this.listEvents(),
|
|
2258
|
+
deliveries: await this.listDeliveries()
|
|
2259
|
+
};
|
|
2260
|
+
}
|
|
2261
|
+
async ensureArrayFile(path) {
|
|
2262
|
+
if (!existsSync(path)) {
|
|
2263
|
+
await writeFile(path, `[]
|
|
2264
|
+
`, { encoding: "utf-8", mode: 384 });
|
|
2265
|
+
}
|
|
2266
|
+
await chmod(path, 384).catch(() => {
|
|
2267
|
+
return;
|
|
2268
|
+
});
|
|
2269
|
+
}
|
|
2270
|
+
async readJson(path, fallback) {
|
|
2271
|
+
try {
|
|
2272
|
+
const raw = await readFile(path, "utf-8");
|
|
2273
|
+
if (!raw.trim())
|
|
2274
|
+
return fallback;
|
|
2275
|
+
return JSON.parse(raw);
|
|
2276
|
+
} catch (error) {
|
|
2277
|
+
if (error.code === "ENOENT")
|
|
2278
|
+
return fallback;
|
|
2279
|
+
throw error;
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
async writeJson(path, value) {
|
|
2283
|
+
const tempPath = `${path}.${process.pid}.${Date.now()}.tmp`;
|
|
2284
|
+
await writeFile(tempPath, `${JSON.stringify(value, null, 2)}
|
|
2285
|
+
`, { encoding: "utf-8", mode: 384 });
|
|
2286
|
+
await rename(tempPath, path);
|
|
2287
|
+
await chmod(path, 384).catch(() => {
|
|
2288
|
+
return;
|
|
2289
|
+
});
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
var DEFAULT_SIGNATURE_TOLERANCE_MS = 5 * 60 * 1000;
|
|
2293
|
+
function buildSignatureBase(timestamp, body) {
|
|
2294
|
+
return `${timestamp}.${body}`;
|
|
2295
|
+
}
|
|
2296
|
+
function signPayload(secret, timestamp, body) {
|
|
2297
|
+
const digest = createHmac("sha256", secret).update(buildSignatureBase(timestamp, body)).digest("hex");
|
|
2298
|
+
return `sha256=${digest}`;
|
|
2299
|
+
}
|
|
2300
|
+
function now() {
|
|
2301
|
+
return new Date().toISOString();
|
|
2302
|
+
}
|
|
2303
|
+
function truncate(value, max = 4096) {
|
|
2304
|
+
return value.length > max ? `${value.slice(0, max)}...` : value;
|
|
2305
|
+
}
|
|
2306
|
+
function buildWebhookRequest(event, channel) {
|
|
2307
|
+
if (!channel.webhook)
|
|
2308
|
+
throw new Error(`Channel ${channel.id} has no webhook config`);
|
|
2309
|
+
const body = JSON.stringify(event);
|
|
2310
|
+
const timestamp = event.time;
|
|
2311
|
+
const headers = {
|
|
2312
|
+
"Content-Type": "application/json",
|
|
2313
|
+
"User-Agent": "@hasna/events",
|
|
2314
|
+
"X-Hasna-Event-Id": event.id,
|
|
2315
|
+
"X-Hasna-Event-Type": event.type,
|
|
2316
|
+
"X-Hasna-Timestamp": timestamp,
|
|
2317
|
+
...channel.webhook.headers
|
|
2318
|
+
};
|
|
2319
|
+
if (channel.webhook.secret) {
|
|
2320
|
+
headers["X-Hasna-Signature"] = signPayload(channel.webhook.secret, timestamp, body);
|
|
2321
|
+
}
|
|
2322
|
+
return { body, headers };
|
|
2323
|
+
}
|
|
2324
|
+
async function dispatchWebhook(event, channel, options = {}) {
|
|
2325
|
+
if (!channel.webhook)
|
|
2326
|
+
throw new Error(`Channel ${channel.id} has no webhook config`);
|
|
2327
|
+
const startedAt = now();
|
|
2328
|
+
const { body, headers } = buildWebhookRequest(event, channel);
|
|
2329
|
+
const controller = new AbortController;
|
|
2330
|
+
const timeout = setTimeout(() => controller.abort(), channel.webhook.timeoutMs ?? 15000);
|
|
2331
|
+
try {
|
|
2332
|
+
const response = await (options.fetchImpl ?? fetch)(channel.webhook.url, {
|
|
2333
|
+
method: "POST",
|
|
2334
|
+
headers,
|
|
2335
|
+
body,
|
|
2336
|
+
signal: controller.signal
|
|
2337
|
+
});
|
|
2338
|
+
const responseBody = truncate(await response.text());
|
|
2339
|
+
return {
|
|
2340
|
+
attempt: 1,
|
|
2341
|
+
status: response.ok ? "success" : "failed",
|
|
2342
|
+
startedAt,
|
|
2343
|
+
completedAt: now(),
|
|
2344
|
+
responseStatus: response.status,
|
|
2345
|
+
responseBody,
|
|
2346
|
+
error: response.ok ? undefined : `Webhook returned HTTP ${response.status}`
|
|
2347
|
+
};
|
|
2348
|
+
} catch (error) {
|
|
2349
|
+
return {
|
|
2350
|
+
attempt: 1,
|
|
2351
|
+
status: "failed",
|
|
2352
|
+
startedAt,
|
|
2353
|
+
completedAt: now(),
|
|
2354
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2355
|
+
};
|
|
2356
|
+
} finally {
|
|
2357
|
+
clearTimeout(timeout);
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
async function dispatchCommand(event, channel) {
|
|
2361
|
+
if (!channel.command)
|
|
2362
|
+
throw new Error(`Channel ${channel.id} has no command config`);
|
|
2363
|
+
const startedAt = now();
|
|
2364
|
+
const eventJson = JSON.stringify(event);
|
|
2365
|
+
const env = {
|
|
2366
|
+
...process.env,
|
|
2367
|
+
...channel.command.env,
|
|
2368
|
+
HASNA_CHANNEL_ID: channel.id,
|
|
2369
|
+
HASNA_EVENT_ID: event.id,
|
|
2370
|
+
HASNA_EVENT_TYPE: event.type,
|
|
2371
|
+
HASNA_EVENT_SOURCE: event.source,
|
|
2372
|
+
HASNA_EVENT_SUBJECT: event.subject ?? "",
|
|
2373
|
+
HASNA_EVENT_SEVERITY: event.severity,
|
|
2374
|
+
HASNA_EVENT_TIME: event.time,
|
|
2375
|
+
HASNA_EVENT_DEDUPE_KEY: event.dedupeKey ?? "",
|
|
2376
|
+
HASNA_EVENT_SCHEMA_VERSION: event.schemaVersion,
|
|
2377
|
+
HASNA_EVENT_JSON: eventJson
|
|
2378
|
+
};
|
|
2379
|
+
return new Promise((resolve) => {
|
|
2380
|
+
const child = spawn(channel.command.command, channel.command.args ?? [], {
|
|
2381
|
+
cwd: channel.command.cwd,
|
|
2382
|
+
env,
|
|
2383
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
2384
|
+
});
|
|
2385
|
+
let stdout = "";
|
|
2386
|
+
let stderr = "";
|
|
2387
|
+
const timeout = setTimeout(() => child.kill("SIGTERM"), channel.command.timeoutMs ?? 15000);
|
|
2388
|
+
child.stdin.end(eventJson);
|
|
2389
|
+
child.stdout.on("data", (chunk) => {
|
|
2390
|
+
stdout += chunk.toString();
|
|
2391
|
+
});
|
|
2392
|
+
child.stderr.on("data", (chunk) => {
|
|
2393
|
+
stderr += chunk.toString();
|
|
2394
|
+
});
|
|
2395
|
+
child.on("error", (error) => {
|
|
2396
|
+
clearTimeout(timeout);
|
|
2397
|
+
resolve({
|
|
2398
|
+
attempt: 1,
|
|
2399
|
+
status: "failed",
|
|
2400
|
+
startedAt,
|
|
2401
|
+
completedAt: now(),
|
|
2402
|
+
stdout: truncate(stdout),
|
|
2403
|
+
stderr: truncate(stderr),
|
|
2404
|
+
error: error.message
|
|
2405
|
+
});
|
|
2406
|
+
});
|
|
2407
|
+
child.on("close", (code, signal) => {
|
|
2408
|
+
clearTimeout(timeout);
|
|
2409
|
+
const success = code === 0;
|
|
2410
|
+
resolve({
|
|
2411
|
+
attempt: 1,
|
|
2412
|
+
status: success ? "success" : "failed",
|
|
2413
|
+
startedAt,
|
|
2414
|
+
completedAt: now(),
|
|
2415
|
+
stdout: truncate(stdout),
|
|
2416
|
+
stderr: truncate(stderr),
|
|
2417
|
+
error: success ? undefined : `Command exited with ${signal ? `signal ${signal}` : `code ${code}`}`
|
|
2418
|
+
});
|
|
2419
|
+
});
|
|
2420
|
+
});
|
|
2421
|
+
}
|
|
2422
|
+
async function dispatchChannel(event, channel, options = {}) {
|
|
2423
|
+
if (channel.transport === "webhook")
|
|
2424
|
+
return dispatchWebhook(event, channel, options);
|
|
2425
|
+
if (channel.transport === "command")
|
|
2426
|
+
return dispatchCommand(event, channel);
|
|
2427
|
+
return {
|
|
2428
|
+
attempt: 1,
|
|
2429
|
+
status: "skipped",
|
|
2430
|
+
startedAt: now(),
|
|
2431
|
+
completedAt: now(),
|
|
2432
|
+
error: `Unsupported transport: ${channel.transport}`
|
|
2433
|
+
};
|
|
2434
|
+
}
|
|
2435
|
+
function createDeliveryResult(event, channel, attempts) {
|
|
2436
|
+
const status = attempts.some((attempt) => attempt.status === "success") ? "success" : attempts.every((attempt) => attempt.status === "skipped") ? "skipped" : "failed";
|
|
2437
|
+
return {
|
|
2438
|
+
id: randomUUID(),
|
|
2439
|
+
eventId: event.id,
|
|
2440
|
+
channelId: channel.id,
|
|
2441
|
+
transport: channel.transport,
|
|
2442
|
+
status,
|
|
2443
|
+
attempts,
|
|
2444
|
+
createdAt: attempts[0]?.startedAt ?? now(),
|
|
2445
|
+
completedAt: attempts.at(-1)?.completedAt ?? now()
|
|
2446
|
+
};
|
|
2447
|
+
}
|
|
2448
|
+
function createEvent(input) {
|
|
2449
|
+
return {
|
|
2450
|
+
id: input.id ?? randomUUID2(),
|
|
2451
|
+
source: input.source,
|
|
2452
|
+
type: input.type,
|
|
2453
|
+
time: normalizeTime(input.time),
|
|
2454
|
+
subject: input.subject,
|
|
2455
|
+
severity: input.severity ?? "info",
|
|
2456
|
+
data: input.data ?? {},
|
|
2457
|
+
message: input.message,
|
|
2458
|
+
dedupeKey: input.dedupeKey,
|
|
2459
|
+
schemaVersion: input.schemaVersion ?? "1.0",
|
|
2460
|
+
metadata: input.metadata ?? {}
|
|
2461
|
+
};
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
class EventsClient {
|
|
2465
|
+
store;
|
|
2466
|
+
redactors;
|
|
2467
|
+
transportOptions;
|
|
2468
|
+
constructor(options = {}) {
|
|
2469
|
+
this.store = options.store ?? new JsonEventsStore(options.dataDir);
|
|
2470
|
+
this.redactors = options.redactors ?? [];
|
|
2471
|
+
this.transportOptions = { fetchImpl: options.fetchImpl };
|
|
2472
|
+
}
|
|
2473
|
+
async addChannel(input) {
|
|
2474
|
+
const timestamp = new Date().toISOString();
|
|
2475
|
+
return this.store.addChannel({
|
|
2476
|
+
...input,
|
|
2477
|
+
createdAt: input.createdAt ?? timestamp,
|
|
2478
|
+
updatedAt: input.updatedAt ?? timestamp
|
|
2479
|
+
});
|
|
2480
|
+
}
|
|
2481
|
+
async listChannels() {
|
|
2482
|
+
return this.store.listChannels();
|
|
2483
|
+
}
|
|
2484
|
+
async removeChannel(id) {
|
|
2485
|
+
return this.store.removeChannel(id);
|
|
2486
|
+
}
|
|
2487
|
+
async emit(input, options = {}) {
|
|
2488
|
+
const event = options.redactSensitiveData === false ? createEvent(input) : redactSensitiveKeys(createEvent(input));
|
|
2489
|
+
if (options.dedupe !== false) {
|
|
2490
|
+
const existing = await this.store.findEventByIdentity({ id: input.id, dedupeKey: event.dedupeKey });
|
|
2491
|
+
if (existing) {
|
|
2492
|
+
return { event: existing, deliveries: [], deduped: true };
|
|
2493
|
+
}
|
|
2494
|
+
}
|
|
2495
|
+
await this.store.appendEvent(event);
|
|
2496
|
+
const deliveries = options.deliver === false ? [] : await this.deliver(event);
|
|
2497
|
+
return { event, deliveries, deduped: false };
|
|
2498
|
+
}
|
|
2499
|
+
async listEvents() {
|
|
2500
|
+
return this.store.listEvents();
|
|
2501
|
+
}
|
|
2502
|
+
async listDeliveries() {
|
|
2503
|
+
return this.store.listDeliveries();
|
|
2504
|
+
}
|
|
2505
|
+
async deliver(event) {
|
|
2506
|
+
const channels = await this.store.listChannels();
|
|
2507
|
+
const selected = channels.filter((channel) => channelMatchesEvent(channel, event));
|
|
2508
|
+
const deliveries = [];
|
|
2509
|
+
for (const channel of selected) {
|
|
2510
|
+
const eventForChannel = await this.applyRedaction(event, channel);
|
|
2511
|
+
const result = await this.deliverWithRetry(eventForChannel, channel);
|
|
2512
|
+
await this.store.appendDelivery(result);
|
|
2513
|
+
deliveries.push(result);
|
|
2514
|
+
}
|
|
2515
|
+
return deliveries;
|
|
2516
|
+
}
|
|
2517
|
+
async testChannel(id, input = {}) {
|
|
2518
|
+
const channel = await this.store.getChannel(id);
|
|
2519
|
+
if (!channel)
|
|
2520
|
+
throw new Error(`Channel not found: ${id}`);
|
|
2521
|
+
const event = createEvent({
|
|
2522
|
+
source: input.source ?? "hasna.events",
|
|
2523
|
+
type: input.type ?? "events.test",
|
|
2524
|
+
subject: input.subject ?? id,
|
|
2525
|
+
severity: input.severity ?? "info",
|
|
2526
|
+
data: input.data ?? { test: true },
|
|
2527
|
+
message: input.message ?? "Hasna events test delivery",
|
|
2528
|
+
dedupeKey: input.dedupeKey,
|
|
2529
|
+
schemaVersion: input.schemaVersion,
|
|
2530
|
+
metadata: input.metadata,
|
|
2531
|
+
time: input.time,
|
|
2532
|
+
id: input.id
|
|
2533
|
+
});
|
|
2534
|
+
const eventForChannel = await this.applyRedaction(event, channel);
|
|
2535
|
+
const result = await this.deliverWithRetry(eventForChannel, channel);
|
|
2536
|
+
await this.store.appendDelivery(result);
|
|
2537
|
+
return result;
|
|
2538
|
+
}
|
|
2539
|
+
async replay(options = {}) {
|
|
2540
|
+
const events = (await this.store.listEvents()).filter((event) => {
|
|
2541
|
+
if (options.eventId && event.id !== options.eventId)
|
|
2542
|
+
return false;
|
|
2543
|
+
if (options.source && event.source !== options.source)
|
|
2544
|
+
return false;
|
|
2545
|
+
if (options.type && event.type !== options.type)
|
|
2546
|
+
return false;
|
|
2547
|
+
return true;
|
|
2548
|
+
});
|
|
2549
|
+
if (options.dryRun)
|
|
2550
|
+
return { events, deliveries: [] };
|
|
2551
|
+
const deliveries = [];
|
|
2552
|
+
for (const event of events) {
|
|
2553
|
+
deliveries.push(...await this.deliver(event));
|
|
2554
|
+
}
|
|
2555
|
+
return { events, deliveries };
|
|
2556
|
+
}
|
|
2557
|
+
async applyRedaction(event, channel) {
|
|
2558
|
+
let next = redactPaths(event, channel.redact?.paths ?? [], channel.redact?.replacement ?? "[REDACTED]");
|
|
2559
|
+
for (const redactor of this.redactors) {
|
|
2560
|
+
next = await redactor(next, channel);
|
|
2561
|
+
}
|
|
2562
|
+
return next;
|
|
2563
|
+
}
|
|
2564
|
+
async deliverWithRetry(event, channel) {
|
|
2565
|
+
const policy = normalizeRetryPolicy(channel.retry);
|
|
2566
|
+
const attempts = [];
|
|
2567
|
+
for (let index = 0;index < policy.maxAttempts; index += 1) {
|
|
2568
|
+
const attempt = await dispatchChannel(event, channel, this.transportOptions);
|
|
2569
|
+
attempt.attempt = index + 1;
|
|
2570
|
+
if (attempt.status === "failed" && index + 1 < policy.maxAttempts) {
|
|
2571
|
+
attempt.nextBackoffMs = Math.round(policy.backoffMs * policy.multiplier ** index);
|
|
2572
|
+
}
|
|
2573
|
+
attempts.push(attempt);
|
|
2574
|
+
if (attempt.status !== "failed")
|
|
2575
|
+
break;
|
|
2576
|
+
if (attempt.nextBackoffMs)
|
|
2577
|
+
await Bun.sleep(attempt.nextBackoffMs);
|
|
2578
|
+
}
|
|
2579
|
+
return createDeliveryResult(event, channel, attempts);
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
function redactPaths(event, paths, replacement = "[REDACTED]") {
|
|
2583
|
+
if (paths.length === 0)
|
|
2584
|
+
return event;
|
|
2585
|
+
const copy = structuredClone(event);
|
|
2586
|
+
for (const path of paths) {
|
|
2587
|
+
setPath(copy, path, replacement);
|
|
2588
|
+
}
|
|
2589
|
+
return copy;
|
|
2590
|
+
}
|
|
2591
|
+
function sanitizeChannelForOutput(channel) {
|
|
2592
|
+
const copy = structuredClone(channel);
|
|
2593
|
+
if (copy.webhook?.secret)
|
|
2594
|
+
copy.webhook.secret = "[REDACTED]";
|
|
2595
|
+
if (copy.command?.env) {
|
|
2596
|
+
copy.command.env = Object.fromEntries(Object.entries(copy.command.env).map(([key, value]) => [key, shouldRedactKey(key) ? "[REDACTED]" : value]));
|
|
2597
|
+
}
|
|
2598
|
+
return copy;
|
|
2599
|
+
}
|
|
2600
|
+
function sanitizeChannelsForOutput(channels) {
|
|
2601
|
+
return channels.map(sanitizeChannelForOutput);
|
|
2602
|
+
}
|
|
2603
|
+
function redactSensitiveKeys(event, replacement = "[REDACTED]") {
|
|
2604
|
+
return redactValue(event, replacement);
|
|
2605
|
+
}
|
|
2606
|
+
function shouldRedactKey(key) {
|
|
2607
|
+
return /secret|token|password|api[_-]?key|authorization/i.test(key);
|
|
2608
|
+
}
|
|
2609
|
+
function redactValue(value, replacement) {
|
|
2610
|
+
if (Array.isArray(value))
|
|
2611
|
+
return value.map((item) => redactValue(item, replacement));
|
|
2612
|
+
if (!value || typeof value !== "object")
|
|
2613
|
+
return value;
|
|
2614
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
2615
|
+
key,
|
|
2616
|
+
shouldRedactKey(key) ? replacement : redactValue(item, replacement)
|
|
2617
|
+
]));
|
|
2618
|
+
}
|
|
2619
|
+
function setPath(input, path, replacement) {
|
|
2620
|
+
const parts = path.split(".");
|
|
2621
|
+
let cursor = input;
|
|
2622
|
+
for (const part of parts.slice(0, -1)) {
|
|
2623
|
+
const next = cursor[part];
|
|
2624
|
+
if (!next || typeof next !== "object")
|
|
2625
|
+
return;
|
|
2626
|
+
cursor = next;
|
|
2627
|
+
}
|
|
2628
|
+
const last = parts.at(-1);
|
|
2629
|
+
if (last && last in cursor)
|
|
2630
|
+
cursor[last] = replacement;
|
|
2631
|
+
}
|
|
2632
|
+
function normalizeTime(value) {
|
|
2633
|
+
if (!value)
|
|
2634
|
+
return new Date().toISOString();
|
|
2635
|
+
return value instanceof Date ? value.toISOString() : value;
|
|
2636
|
+
}
|
|
2637
|
+
function normalizeRetryPolicy(policy) {
|
|
2638
|
+
return {
|
|
2639
|
+
maxAttempts: Math.max(1, policy?.maxAttempts ?? 1),
|
|
2640
|
+
backoffMs: Math.max(0, policy?.backoffMs ?? 250),
|
|
2641
|
+
multiplier: Math.max(1, policy?.multiplier ?? 2)
|
|
2642
|
+
};
|
|
2643
|
+
}
|
|
2644
|
+
function parseJsonObject(value, fallback) {
|
|
2645
|
+
if (!value)
|
|
2646
|
+
return fallback;
|
|
2647
|
+
const parsed = JSON.parse(value);
|
|
2648
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
2649
|
+
throw new Error("Expected a JSON object");
|
|
2650
|
+
}
|
|
2651
|
+
return parsed;
|
|
2652
|
+
}
|
|
2653
|
+
function parseHeaders(values) {
|
|
2654
|
+
if (!values?.length)
|
|
2655
|
+
return;
|
|
2656
|
+
const headers = {};
|
|
2657
|
+
for (const value of values) {
|
|
2658
|
+
const separator = value.indexOf("=");
|
|
2659
|
+
if (separator === -1)
|
|
2660
|
+
throw new Error(`Invalid header, expected name=value: ${value}`);
|
|
2661
|
+
headers[value.slice(0, separator)] = value.slice(separator + 1);
|
|
2662
|
+
}
|
|
2663
|
+
return headers;
|
|
2664
|
+
}
|
|
2665
|
+
function parseFilter(options) {
|
|
2666
|
+
const filter2 = {};
|
|
2667
|
+
if (options.source)
|
|
2668
|
+
filter2.source = options.source;
|
|
2669
|
+
if (options.type)
|
|
2670
|
+
filter2.type = options.type;
|
|
2671
|
+
if (options.subject)
|
|
2672
|
+
filter2.subject = options.subject;
|
|
2673
|
+
if (options.severity)
|
|
2674
|
+
filter2.severity = options.severity;
|
|
2675
|
+
return Object.keys(filter2).length > 0 ? [filter2] : undefined;
|
|
2676
|
+
}
|
|
2677
|
+
function createClient(options) {
|
|
2678
|
+
if (options.createClient)
|
|
2679
|
+
return options.createClient();
|
|
2680
|
+
return new EventsClient({ store: new JsonEventsStore(options.dataDir) });
|
|
2681
|
+
}
|
|
2682
|
+
function print(value, json, text) {
|
|
2683
|
+
if (json)
|
|
2684
|
+
console.log(JSON.stringify(value, null, 2));
|
|
2685
|
+
else
|
|
2686
|
+
console.log(text);
|
|
2687
|
+
}
|
|
2688
|
+
function hasJsonOption(options) {
|
|
2689
|
+
return Boolean(options?.json || options?.opts?.().json || options?.optsWithGlobals?.().json || options?.parent?.opts?.().json || options?.parent?.optsWithGlobals?.().json);
|
|
2690
|
+
}
|
|
2691
|
+
function wantsJson(actionOptions, command) {
|
|
2692
|
+
return hasJsonOption(actionOptions) || hasJsonOption(command);
|
|
2693
|
+
}
|
|
2694
|
+
function registerWebhookCommands(program, options) {
|
|
2695
|
+
const webhooks = program.command(options.webhooksCommandName ?? "webhooks").description("Manage Hasna event webhook subscriptions");
|
|
2696
|
+
webhooks.command("add").description("Add or replace a webhook or command subscription").argument("<target>", "Webhook URL or command binary").requiredOption("--id <id>", "Subscription/channel identifier").option("--transport <kind>", "Transport kind: webhook or command", "webhook").option("--name <name>", "Display name").option("--type <pattern>", "Event type filter, e.g. todos.task.*").option("--source <pattern>", "Event source filter").option("--subject <pattern>", "Event subject filter").option("--severity <pattern>", "Event severity filter").option("--secret <secret>", "Webhook HMAC secret").option("--header <name=value...>", "Webhook header", collectValues, []).option("--arg <arg...>", "Command argument", collectValues, []).option("--timeout-ms <ms>", "Transport timeout in milliseconds", parseNumber).option("--retry-attempts <n>", "Maximum delivery attempts", parseNumber).option("--retry-backoff-ms <ms>", "Initial retry backoff in milliseconds", parseNumber).option("--redact <path...>", "Event field path to redact before delivery", collectValues, []).option("--disabled", "Create channel disabled", false).option("-j, --json", "Print JSON output", false).action(async (target, actionOptions, command) => {
|
|
2697
|
+
const timestamp = new Date().toISOString();
|
|
2698
|
+
const channel = {
|
|
2699
|
+
id: actionOptions.id,
|
|
2700
|
+
name: actionOptions.name,
|
|
2701
|
+
enabled: !actionOptions.disabled,
|
|
2702
|
+
transport: actionOptions.transport,
|
|
2703
|
+
filters: parseFilter(actionOptions),
|
|
2704
|
+
retry: actionOptions.retryAttempts || actionOptions.retryBackoffMs ? { maxAttempts: actionOptions.retryAttempts, backoffMs: actionOptions.retryBackoffMs } : undefined,
|
|
2705
|
+
redact: actionOptions.redact?.length ? { paths: actionOptions.redact } : undefined,
|
|
2706
|
+
createdAt: timestamp,
|
|
2707
|
+
updatedAt: timestamp
|
|
2708
|
+
};
|
|
2709
|
+
if (actionOptions.transport === "webhook") {
|
|
2710
|
+
channel.webhook = { url: target, secret: actionOptions.secret, headers: parseHeaders(actionOptions.header), timeoutMs: actionOptions.timeoutMs };
|
|
2711
|
+
} else if (actionOptions.transport === "command") {
|
|
2712
|
+
channel.command = { command: target, args: actionOptions.arg ?? [], timeoutMs: actionOptions.timeoutMs };
|
|
2713
|
+
} else {
|
|
2714
|
+
throw new Error(`Transport ${actionOptions.transport} is reserved for future use and cannot be added yet`);
|
|
2715
|
+
}
|
|
2716
|
+
const saved = await createClient(options).addChannel(channel);
|
|
2717
|
+
print(sanitizeChannelForOutput(saved), wantsJson(actionOptions, command), `Added ${saved.transport} channel ${saved.id}`);
|
|
2718
|
+
});
|
|
2719
|
+
webhooks.command("list").description("List configured subscriptions").option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
|
|
2720
|
+
const channels = await createClient(options).listChannels();
|
|
2721
|
+
if (wantsJson(actionOptions, command)) {
|
|
2722
|
+
console.log(JSON.stringify(sanitizeChannelsForOutput(channels), null, 2));
|
|
2723
|
+
return;
|
|
2724
|
+
}
|
|
2725
|
+
if (!channels.length) {
|
|
2726
|
+
console.log("No channels configured.");
|
|
2727
|
+
return;
|
|
2728
|
+
}
|
|
2729
|
+
for (const channel of channels) {
|
|
2730
|
+
console.log(`${channel.id} ${channel.enabled ? "enabled" : "disabled"} ${channel.transport} ${channel.webhook?.url ?? channel.command?.command ?? channel.transport}`);
|
|
2731
|
+
}
|
|
2732
|
+
});
|
|
2733
|
+
webhooks.command("remove").description("Remove a subscription").argument("<id>", "Subscription/channel identifier").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions, command) => {
|
|
2734
|
+
const removed = await createClient(options).removeChannel(id);
|
|
2735
|
+
print({ removed }, wantsJson(actionOptions, command), removed ? `Removed ${id}` : `Channel not found: ${id}`);
|
|
2736
|
+
});
|
|
2737
|
+
webhooks.command("test").description("Send a test event to one subscription").argument("<id>", "Subscription/channel identifier").option("--type <type>", "Event type", "events.test").option("--subject <subject>", "Event subject").option("--message <message>", "Event message", "Hasna events test delivery").option("--data <json>", "Event data JSON object").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions, command) => {
|
|
2738
|
+
const result = await createClient(options).testChannel(id, {
|
|
2739
|
+
source: options.source,
|
|
2740
|
+
type: actionOptions.type,
|
|
2741
|
+
subject: actionOptions.subject ?? id,
|
|
2742
|
+
message: actionOptions.message,
|
|
2743
|
+
data: parseJsonObject(actionOptions.data, { test: true })
|
|
2744
|
+
});
|
|
2745
|
+
print(result, wantsJson(actionOptions, command), `${result.status}: ${result.channelId}`);
|
|
2746
|
+
});
|
|
2747
|
+
return webhooks;
|
|
2748
|
+
}
|
|
2749
|
+
function registerEventCommands(program, options) {
|
|
2750
|
+
const events = program.command(options.eventsCommandName ?? "events").description("Emit, list, and replay Hasna events");
|
|
2751
|
+
events.command("emit").description("Emit an event from this app").argument("<type>", "Event type").option("--source <source>", "Event source override").option("--subject <subject>", "Event subject").option("--severity <severity>", "Event severity", "info").option("--message <message>", "Event message").option("--dedupe-key <key>", "Dedupe key").option("--data <json>", "Event data JSON object").option("--metadata <json>", "Event metadata JSON object").option("--no-deliver", "Record without delivering").option("--no-dedupe", "Allow duplicate id/dedupeKey events").option("-j, --json", "Print JSON output", false).action(async (type, actionOptions, command) => {
|
|
2752
|
+
const result = await createClient(options).emit({
|
|
2753
|
+
source: actionOptions.source ?? options.source,
|
|
2754
|
+
type,
|
|
2755
|
+
subject: actionOptions.subject,
|
|
2756
|
+
severity: actionOptions.severity,
|
|
2757
|
+
message: actionOptions.message,
|
|
2758
|
+
dedupeKey: actionOptions.dedupeKey,
|
|
2759
|
+
data: parseJsonObject(actionOptions.data, {}),
|
|
2760
|
+
metadata: parseJsonObject(actionOptions.metadata, {})
|
|
2761
|
+
}, { deliver: actionOptions.deliver, dedupe: actionOptions.dedupe });
|
|
2762
|
+
print(result, wantsJson(actionOptions, command), `${result.deduped ? "Deduped" : "Emitted"} ${result.event.id} to ${result.deliveries.length} channel(s)`);
|
|
2763
|
+
});
|
|
2764
|
+
events.command("list").description("List recorded events").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--limit <n>", "Limit results", parseNumber).option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
|
|
2765
|
+
let rows = await createClient(options).listEvents();
|
|
2766
|
+
if (actionOptions.source)
|
|
2767
|
+
rows = rows.filter((event) => event.source === actionOptions.source);
|
|
2768
|
+
if (actionOptions.type)
|
|
2769
|
+
rows = rows.filter((event) => event.type === actionOptions.type);
|
|
2770
|
+
if (actionOptions.limit)
|
|
2771
|
+
rows = rows.slice(-actionOptions.limit);
|
|
2772
|
+
if (wantsJson(actionOptions, command)) {
|
|
2773
|
+
console.log(JSON.stringify(rows, null, 2));
|
|
2774
|
+
return;
|
|
2775
|
+
}
|
|
2776
|
+
if (!rows.length) {
|
|
2777
|
+
console.log("No events recorded.");
|
|
2778
|
+
return;
|
|
2779
|
+
}
|
|
2780
|
+
for (const event of rows)
|
|
2781
|
+
console.log(`${event.time} ${event.id} ${event.source} ${event.type} ${event.severity}`);
|
|
2782
|
+
});
|
|
2783
|
+
events.command("replay").description("Replay recorded events").option("--id <id>", "Replay one event id").option("--source <source>", "Filter by source").option("--type <type>", "Filter by type").option("--dry-run", "Preview without delivery", false).option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
|
|
2784
|
+
const result = await createClient(options).replay({
|
|
2785
|
+
eventId: actionOptions.id,
|
|
2786
|
+
source: actionOptions.source,
|
|
2787
|
+
type: actionOptions.type,
|
|
2788
|
+
dryRun: actionOptions.dryRun
|
|
2789
|
+
});
|
|
2790
|
+
print(result, wantsJson(actionOptions, command), `Replayed ${result.events.length} event(s), ${result.deliveries.length} delivery result(s)`);
|
|
2791
|
+
});
|
|
2792
|
+
return events;
|
|
2793
|
+
}
|
|
2794
|
+
function registerEventsCommands(program, options) {
|
|
2795
|
+
registerWebhookCommands(program, options);
|
|
2796
|
+
registerEventCommands(program, options);
|
|
2797
|
+
}
|
|
2798
|
+
function parseNumber(value) {
|
|
2799
|
+
const parsed = Number(value);
|
|
2800
|
+
if (!Number.isFinite(parsed))
|
|
2801
|
+
throw new Error(`Expected a number, got ${value}`);
|
|
2802
|
+
return parsed;
|
|
2803
|
+
}
|
|
2804
|
+
function collectValues(value, previous) {
|
|
2805
|
+
previous.push(value);
|
|
2806
|
+
return previous;
|
|
2807
|
+
}
|
|
2808
|
+
|
|
2125
2809
|
// node_modules/commander/esm.mjs
|
|
2126
2810
|
var import__ = __toESM(require_commander(), 1);
|
|
2127
2811
|
var {
|
|
@@ -2526,4 +3210,5 @@ function parseRelativeTime(val) {
|
|
|
2526
3210
|
const ms = Number(n) * (unit === "h" ? 3600 : unit === "d" ? 86400 : 60) * 1000;
|
|
2527
3211
|
return new Date(Date.now() - ms).toISOString();
|
|
2528
3212
|
}
|
|
3213
|
+
registerEventsCommands(program2, { source: "logs" });
|
|
2529
3214
|
program2.parse();
|