@hasna/calendar 0.1.3 → 0.1.5

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/dist/cli/index.js CHANGED
@@ -978,7 +978,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
978
978
  this._exitCallback = (err) => {
979
979
  if (err.code !== "commander.executeSubCommandAsync") {
980
980
  throw err;
981
- } else {}
981
+ }
982
982
  };
983
983
  }
984
984
  return this;
@@ -2066,6 +2066,690 @@ var require_commander = __commonJS((exports) => {
2066
2066
  exports.InvalidOptionArgumentError = InvalidArgumentError;
2067
2067
  });
2068
2068
 
2069
+ // node_modules/@hasna/events/dist/commander.js
2070
+ import { chmod, mkdir, readFile, rename, writeFile } from "fs/promises";
2071
+ import { existsSync } from "fs";
2072
+ import { homedir } from "os";
2073
+ import { join } from "path";
2074
+ import { createHmac, timingSafeEqual } from "crypto";
2075
+ import { randomUUID } from "crypto";
2076
+ import { spawn } from "child_process";
2077
+ import { randomUUID as randomUUID2 } from "crypto";
2078
+ function getPathValue(input, path) {
2079
+ return path.split(".").reduce((value, part) => {
2080
+ if (value && typeof value === "object" && part in value) {
2081
+ return value[part];
2082
+ }
2083
+ return;
2084
+ }, input);
2085
+ }
2086
+ function wildcardToRegExp(pattern) {
2087
+ const escaped = pattern.replace(/[|\\{}()[\]^$+?.]/g, "\\$&").replace(/\*/g, ".*");
2088
+ return new RegExp(`^${escaped}$`);
2089
+ }
2090
+ function matchString(value, matcher) {
2091
+ if (matcher === undefined)
2092
+ return true;
2093
+ if (value === undefined)
2094
+ return false;
2095
+ const matchers = Array.isArray(matcher) ? matcher : [matcher];
2096
+ return matchers.some((item) => wildcardToRegExp(item).test(value));
2097
+ }
2098
+ function matchRecord(input, matcher) {
2099
+ if (!matcher)
2100
+ return true;
2101
+ return Object.entries(matcher).every(([path, expected]) => {
2102
+ const actual = getPathValue(input, path);
2103
+ if (typeof expected === "string" || Array.isArray(expected)) {
2104
+ return matchString(actual === undefined ? undefined : String(actual), expected);
2105
+ }
2106
+ return actual === expected;
2107
+ });
2108
+ }
2109
+ function eventMatchesFilter(event, filter) {
2110
+ 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);
2111
+ }
2112
+ function channelMatchesEvent(channel, event) {
2113
+ if (!channel.enabled)
2114
+ return false;
2115
+ if (!channel.filters || channel.filters.length === 0)
2116
+ return true;
2117
+ return channel.filters.some((filter) => eventMatchesFilter(event, filter));
2118
+ }
2119
+ var HASNA_EVENTS_DIR_ENV = "HASNA_EVENTS_DIR";
2120
+ var HASNA_EVENTS_HOME_ENV = "HASNA_EVENTS_HOME";
2121
+ function getEventsDataDir(override) {
2122
+ return override || process.env[HASNA_EVENTS_DIR_ENV] || process.env[HASNA_EVENTS_HOME_ENV] || join(homedir(), ".hasna", "events");
2123
+ }
2124
+
2125
+ class JsonEventsStore {
2126
+ dataDir;
2127
+ channelsPath;
2128
+ eventsPath;
2129
+ deliveriesPath;
2130
+ constructor(dataDir = getEventsDataDir()) {
2131
+ this.dataDir = dataDir;
2132
+ this.channelsPath = join(dataDir, "channels.json");
2133
+ this.eventsPath = join(dataDir, "events.json");
2134
+ this.deliveriesPath = join(dataDir, "deliveries.json");
2135
+ }
2136
+ async init() {
2137
+ await mkdir(this.dataDir, { recursive: true, mode: 448 });
2138
+ await chmod(this.dataDir, 448).catch(() => {
2139
+ return;
2140
+ });
2141
+ await this.ensureArrayFile(this.channelsPath);
2142
+ await this.ensureArrayFile(this.eventsPath);
2143
+ await this.ensureArrayFile(this.deliveriesPath);
2144
+ }
2145
+ async addChannel(channel) {
2146
+ await this.init();
2147
+ const channels = await this.readJson(this.channelsPath, []);
2148
+ const index = channels.findIndex((item) => item.id === channel.id);
2149
+ if (index >= 0) {
2150
+ channels[index] = { ...channel, createdAt: channels[index].createdAt, updatedAt: new Date().toISOString() };
2151
+ } else {
2152
+ channels.push(channel);
2153
+ }
2154
+ await this.writeJson(this.channelsPath, channels);
2155
+ return index >= 0 ? channels[index] : channel;
2156
+ }
2157
+ async listChannels() {
2158
+ await this.init();
2159
+ return this.readJson(this.channelsPath, []);
2160
+ }
2161
+ async getChannel(id) {
2162
+ const channels = await this.listChannels();
2163
+ return channels.find((channel) => channel.id === id);
2164
+ }
2165
+ async removeChannel(id) {
2166
+ await this.init();
2167
+ const channels = await this.readJson(this.channelsPath, []);
2168
+ const next = channels.filter((channel) => channel.id !== id);
2169
+ await this.writeJson(this.channelsPath, next);
2170
+ return next.length !== channels.length;
2171
+ }
2172
+ async appendEvent(event) {
2173
+ await this.init();
2174
+ const events = await this.readJson(this.eventsPath, []);
2175
+ events.push(event);
2176
+ await this.writeJson(this.eventsPath, events);
2177
+ return event;
2178
+ }
2179
+ async listEvents() {
2180
+ await this.init();
2181
+ return this.readJson(this.eventsPath, []);
2182
+ }
2183
+ async findEventByIdentity(identity) {
2184
+ const events = await this.listEvents();
2185
+ return events.find((event) => identity.id !== undefined && event.id === identity.id || identity.dedupeKey !== undefined && event.dedupeKey === identity.dedupeKey);
2186
+ }
2187
+ async appendDelivery(result) {
2188
+ await this.init();
2189
+ const deliveries = await this.readJson(this.deliveriesPath, []);
2190
+ deliveries.push(result);
2191
+ await this.writeJson(this.deliveriesPath, deliveries);
2192
+ return result;
2193
+ }
2194
+ async listDeliveries() {
2195
+ await this.init();
2196
+ return this.readJson(this.deliveriesPath, []);
2197
+ }
2198
+ async exportData() {
2199
+ return {
2200
+ channels: await this.listChannels(),
2201
+ events: await this.listEvents(),
2202
+ deliveries: await this.listDeliveries()
2203
+ };
2204
+ }
2205
+ async ensureArrayFile(path) {
2206
+ if (!existsSync(path)) {
2207
+ await writeFile(path, `[]
2208
+ `, { encoding: "utf-8", mode: 384 });
2209
+ }
2210
+ await chmod(path, 384).catch(() => {
2211
+ return;
2212
+ });
2213
+ }
2214
+ async readJson(path, fallback) {
2215
+ try {
2216
+ const raw = await readFile(path, "utf-8");
2217
+ if (!raw.trim())
2218
+ return fallback;
2219
+ return JSON.parse(raw);
2220
+ } catch (error) {
2221
+ if (error.code === "ENOENT")
2222
+ return fallback;
2223
+ throw error;
2224
+ }
2225
+ }
2226
+ async writeJson(path, value) {
2227
+ const tempPath = `${path}.${process.pid}.${Date.now()}.tmp`;
2228
+ await writeFile(tempPath, `${JSON.stringify(value, null, 2)}
2229
+ `, { encoding: "utf-8", mode: 384 });
2230
+ await rename(tempPath, path);
2231
+ await chmod(path, 384).catch(() => {
2232
+ return;
2233
+ });
2234
+ }
2235
+ }
2236
+ var DEFAULT_SIGNATURE_TOLERANCE_MS = 5 * 60 * 1000;
2237
+ function buildSignatureBase(timestamp, body) {
2238
+ return `${timestamp}.${body}`;
2239
+ }
2240
+ function signPayload(secret, timestamp, body) {
2241
+ const digest = createHmac("sha256", secret).update(buildSignatureBase(timestamp, body)).digest("hex");
2242
+ return `sha256=${digest}`;
2243
+ }
2244
+ function now() {
2245
+ return new Date().toISOString();
2246
+ }
2247
+ function truncate(value, max = 4096) {
2248
+ return value.length > max ? `${value.slice(0, max)}...` : value;
2249
+ }
2250
+ function buildWebhookRequest(event, channel) {
2251
+ if (!channel.webhook)
2252
+ throw new Error(`Channel ${channel.id} has no webhook config`);
2253
+ const body = JSON.stringify(event);
2254
+ const timestamp = event.time;
2255
+ const headers = {
2256
+ "Content-Type": "application/json",
2257
+ "User-Agent": "@hasna/events",
2258
+ "X-Hasna-Event-Id": event.id,
2259
+ "X-Hasna-Event-Type": event.type,
2260
+ "X-Hasna-Timestamp": timestamp,
2261
+ ...channel.webhook.headers
2262
+ };
2263
+ if (channel.webhook.secret) {
2264
+ headers["X-Hasna-Signature"] = signPayload(channel.webhook.secret, timestamp, body);
2265
+ }
2266
+ return { body, headers };
2267
+ }
2268
+ async function dispatchWebhook(event, channel, options = {}) {
2269
+ if (!channel.webhook)
2270
+ throw new Error(`Channel ${channel.id} has no webhook config`);
2271
+ const startedAt = now();
2272
+ const { body, headers } = buildWebhookRequest(event, channel);
2273
+ const controller = new AbortController;
2274
+ const timeout = setTimeout(() => controller.abort(), channel.webhook.timeoutMs ?? 15000);
2275
+ try {
2276
+ const response = await (options.fetchImpl ?? fetch)(channel.webhook.url, {
2277
+ method: "POST",
2278
+ headers,
2279
+ body,
2280
+ signal: controller.signal
2281
+ });
2282
+ const responseBody = truncate(await response.text());
2283
+ return {
2284
+ attempt: 1,
2285
+ status: response.ok ? "success" : "failed",
2286
+ startedAt,
2287
+ completedAt: now(),
2288
+ responseStatus: response.status,
2289
+ responseBody,
2290
+ error: response.ok ? undefined : `Webhook returned HTTP ${response.status}`
2291
+ };
2292
+ } catch (error) {
2293
+ return {
2294
+ attempt: 1,
2295
+ status: "failed",
2296
+ startedAt,
2297
+ completedAt: now(),
2298
+ error: error instanceof Error ? error.message : String(error)
2299
+ };
2300
+ } finally {
2301
+ clearTimeout(timeout);
2302
+ }
2303
+ }
2304
+ async function dispatchCommand(event, channel) {
2305
+ if (!channel.command)
2306
+ throw new Error(`Channel ${channel.id} has no command config`);
2307
+ const startedAt = now();
2308
+ const eventJson = JSON.stringify(event);
2309
+ const env = {
2310
+ ...process.env,
2311
+ ...channel.command.env,
2312
+ HASNA_CHANNEL_ID: channel.id,
2313
+ HASNA_EVENT_ID: event.id,
2314
+ HASNA_EVENT_TYPE: event.type,
2315
+ HASNA_EVENT_SOURCE: event.source,
2316
+ HASNA_EVENT_SUBJECT: event.subject ?? "",
2317
+ HASNA_EVENT_SEVERITY: event.severity,
2318
+ HASNA_EVENT_TIME: event.time,
2319
+ HASNA_EVENT_DEDUPE_KEY: event.dedupeKey ?? "",
2320
+ HASNA_EVENT_SCHEMA_VERSION: event.schemaVersion,
2321
+ HASNA_EVENT_JSON: eventJson
2322
+ };
2323
+ return new Promise((resolve) => {
2324
+ const child = spawn(channel.command.command, channel.command.args ?? [], {
2325
+ cwd: channel.command.cwd,
2326
+ env,
2327
+ stdio: ["pipe", "pipe", "pipe"]
2328
+ });
2329
+ let stdout = "";
2330
+ let stderr = "";
2331
+ const timeout = setTimeout(() => child.kill("SIGTERM"), channel.command.timeoutMs ?? 15000);
2332
+ child.stdin.end(eventJson);
2333
+ child.stdout.on("data", (chunk) => {
2334
+ stdout += chunk.toString();
2335
+ });
2336
+ child.stderr.on("data", (chunk) => {
2337
+ stderr += chunk.toString();
2338
+ });
2339
+ child.on("error", (error) => {
2340
+ clearTimeout(timeout);
2341
+ resolve({
2342
+ attempt: 1,
2343
+ status: "failed",
2344
+ startedAt,
2345
+ completedAt: now(),
2346
+ stdout: truncate(stdout),
2347
+ stderr: truncate(stderr),
2348
+ error: error.message
2349
+ });
2350
+ });
2351
+ child.on("close", (code, signal) => {
2352
+ clearTimeout(timeout);
2353
+ const success = code === 0;
2354
+ resolve({
2355
+ attempt: 1,
2356
+ status: success ? "success" : "failed",
2357
+ startedAt,
2358
+ completedAt: now(),
2359
+ stdout: truncate(stdout),
2360
+ stderr: truncate(stderr),
2361
+ error: success ? undefined : `Command exited with ${signal ? `signal ${signal}` : `code ${code}`}`
2362
+ });
2363
+ });
2364
+ });
2365
+ }
2366
+ async function dispatchChannel(event, channel, options = {}) {
2367
+ if (channel.transport === "webhook")
2368
+ return dispatchWebhook(event, channel, options);
2369
+ if (channel.transport === "command")
2370
+ return dispatchCommand(event, channel);
2371
+ return {
2372
+ attempt: 1,
2373
+ status: "skipped",
2374
+ startedAt: now(),
2375
+ completedAt: now(),
2376
+ error: `Unsupported transport: ${channel.transport}`
2377
+ };
2378
+ }
2379
+ function createDeliveryResult(event, channel, attempts) {
2380
+ const status = attempts.some((attempt) => attempt.status === "success") ? "success" : attempts.every((attempt) => attempt.status === "skipped") ? "skipped" : "failed";
2381
+ return {
2382
+ id: randomUUID(),
2383
+ eventId: event.id,
2384
+ channelId: channel.id,
2385
+ transport: channel.transport,
2386
+ status,
2387
+ attempts,
2388
+ createdAt: attempts[0]?.startedAt ?? now(),
2389
+ completedAt: attempts.at(-1)?.completedAt ?? now()
2390
+ };
2391
+ }
2392
+ function createEvent(input) {
2393
+ return {
2394
+ id: input.id ?? randomUUID2(),
2395
+ source: input.source,
2396
+ type: input.type,
2397
+ time: normalizeTime(input.time),
2398
+ subject: input.subject,
2399
+ severity: input.severity ?? "info",
2400
+ data: input.data ?? {},
2401
+ message: input.message,
2402
+ dedupeKey: input.dedupeKey,
2403
+ schemaVersion: input.schemaVersion ?? "1.0",
2404
+ metadata: input.metadata ?? {}
2405
+ };
2406
+ }
2407
+
2408
+ class EventsClient {
2409
+ store;
2410
+ redactors;
2411
+ transportOptions;
2412
+ constructor(options = {}) {
2413
+ this.store = options.store ?? new JsonEventsStore(options.dataDir);
2414
+ this.redactors = options.redactors ?? [];
2415
+ this.transportOptions = { fetchImpl: options.fetchImpl };
2416
+ }
2417
+ async addChannel(input) {
2418
+ const timestamp = new Date().toISOString();
2419
+ return this.store.addChannel({
2420
+ ...input,
2421
+ createdAt: input.createdAt ?? timestamp,
2422
+ updatedAt: input.updatedAt ?? timestamp
2423
+ });
2424
+ }
2425
+ async listChannels() {
2426
+ return this.store.listChannels();
2427
+ }
2428
+ async removeChannel(id) {
2429
+ return this.store.removeChannel(id);
2430
+ }
2431
+ async emit(input, options = {}) {
2432
+ const event = options.redactSensitiveData === false ? createEvent(input) : redactSensitiveKeys(createEvent(input));
2433
+ if (options.dedupe !== false) {
2434
+ const existing = await this.store.findEventByIdentity({ id: input.id, dedupeKey: event.dedupeKey });
2435
+ if (existing) {
2436
+ return { event: existing, deliveries: [], deduped: true };
2437
+ }
2438
+ }
2439
+ await this.store.appendEvent(event);
2440
+ const deliveries = options.deliver === false ? [] : await this.deliver(event);
2441
+ return { event, deliveries, deduped: false };
2442
+ }
2443
+ async listEvents() {
2444
+ return this.store.listEvents();
2445
+ }
2446
+ async listDeliveries() {
2447
+ return this.store.listDeliveries();
2448
+ }
2449
+ async deliver(event) {
2450
+ const channels = await this.store.listChannels();
2451
+ const selected = channels.filter((channel) => channelMatchesEvent(channel, event));
2452
+ const deliveries = [];
2453
+ for (const channel of selected) {
2454
+ const eventForChannel = await this.applyRedaction(event, channel);
2455
+ const result = await this.deliverWithRetry(eventForChannel, channel);
2456
+ await this.store.appendDelivery(result);
2457
+ deliveries.push(result);
2458
+ }
2459
+ return deliveries;
2460
+ }
2461
+ async testChannel(id, input = {}) {
2462
+ const channel = await this.store.getChannel(id);
2463
+ if (!channel)
2464
+ throw new Error(`Channel not found: ${id}`);
2465
+ const event = createEvent({
2466
+ source: input.source ?? "hasna.events",
2467
+ type: input.type ?? "events.test",
2468
+ subject: input.subject ?? id,
2469
+ severity: input.severity ?? "info",
2470
+ data: input.data ?? { test: true },
2471
+ message: input.message ?? "Hasna events test delivery",
2472
+ dedupeKey: input.dedupeKey,
2473
+ schemaVersion: input.schemaVersion,
2474
+ metadata: input.metadata,
2475
+ time: input.time,
2476
+ id: input.id
2477
+ });
2478
+ const eventForChannel = await this.applyRedaction(event, channel);
2479
+ const result = await this.deliverWithRetry(eventForChannel, channel);
2480
+ await this.store.appendDelivery(result);
2481
+ return result;
2482
+ }
2483
+ async replay(options = {}) {
2484
+ const events = (await this.store.listEvents()).filter((event) => {
2485
+ if (options.eventId && event.id !== options.eventId)
2486
+ return false;
2487
+ if (options.source && event.source !== options.source)
2488
+ return false;
2489
+ if (options.type && event.type !== options.type)
2490
+ return false;
2491
+ return true;
2492
+ });
2493
+ if (options.dryRun)
2494
+ return { events, deliveries: [] };
2495
+ const deliveries = [];
2496
+ for (const event of events) {
2497
+ deliveries.push(...await this.deliver(event));
2498
+ }
2499
+ return { events, deliveries };
2500
+ }
2501
+ async applyRedaction(event, channel) {
2502
+ let next = redactPaths(event, channel.redact?.paths ?? [], channel.redact?.replacement ?? "[REDACTED]");
2503
+ for (const redactor of this.redactors) {
2504
+ next = await redactor(next, channel);
2505
+ }
2506
+ return next;
2507
+ }
2508
+ async deliverWithRetry(event, channel) {
2509
+ const policy = normalizeRetryPolicy(channel.retry);
2510
+ const attempts = [];
2511
+ for (let index = 0;index < policy.maxAttempts; index += 1) {
2512
+ const attempt = await dispatchChannel(event, channel, this.transportOptions);
2513
+ attempt.attempt = index + 1;
2514
+ if (attempt.status === "failed" && index + 1 < policy.maxAttempts) {
2515
+ attempt.nextBackoffMs = Math.round(policy.backoffMs * policy.multiplier ** index);
2516
+ }
2517
+ attempts.push(attempt);
2518
+ if (attempt.status !== "failed")
2519
+ break;
2520
+ if (attempt.nextBackoffMs)
2521
+ await Bun.sleep(attempt.nextBackoffMs);
2522
+ }
2523
+ return createDeliveryResult(event, channel, attempts);
2524
+ }
2525
+ }
2526
+ function redactPaths(event, paths, replacement = "[REDACTED]") {
2527
+ if (paths.length === 0)
2528
+ return event;
2529
+ const copy = structuredClone(event);
2530
+ for (const path of paths) {
2531
+ setPath(copy, path, replacement);
2532
+ }
2533
+ return copy;
2534
+ }
2535
+ function sanitizeChannelForOutput(channel) {
2536
+ const copy = structuredClone(channel);
2537
+ if (copy.webhook?.secret)
2538
+ copy.webhook.secret = "[REDACTED]";
2539
+ if (copy.command?.env) {
2540
+ copy.command.env = Object.fromEntries(Object.entries(copy.command.env).map(([key, value]) => [key, shouldRedactKey(key) ? "[REDACTED]" : value]));
2541
+ }
2542
+ return copy;
2543
+ }
2544
+ function sanitizeChannelsForOutput(channels) {
2545
+ return channels.map(sanitizeChannelForOutput);
2546
+ }
2547
+ function redactSensitiveKeys(event, replacement = "[REDACTED]") {
2548
+ return redactValue(event, replacement);
2549
+ }
2550
+ function shouldRedactKey(key) {
2551
+ return /secret|token|password|api[_-]?key|authorization/i.test(key);
2552
+ }
2553
+ function redactValue(value, replacement) {
2554
+ if (Array.isArray(value))
2555
+ return value.map((item) => redactValue(item, replacement));
2556
+ if (!value || typeof value !== "object")
2557
+ return value;
2558
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
2559
+ key,
2560
+ shouldRedactKey(key) ? replacement : redactValue(item, replacement)
2561
+ ]));
2562
+ }
2563
+ function setPath(input, path, replacement) {
2564
+ const parts = path.split(".");
2565
+ let cursor = input;
2566
+ for (const part of parts.slice(0, -1)) {
2567
+ const next = cursor[part];
2568
+ if (!next || typeof next !== "object")
2569
+ return;
2570
+ cursor = next;
2571
+ }
2572
+ const last = parts.at(-1);
2573
+ if (last && last in cursor)
2574
+ cursor[last] = replacement;
2575
+ }
2576
+ function normalizeTime(value) {
2577
+ if (!value)
2578
+ return new Date().toISOString();
2579
+ return value instanceof Date ? value.toISOString() : value;
2580
+ }
2581
+ function normalizeRetryPolicy(policy) {
2582
+ return {
2583
+ maxAttempts: Math.max(1, policy?.maxAttempts ?? 1),
2584
+ backoffMs: Math.max(0, policy?.backoffMs ?? 250),
2585
+ multiplier: Math.max(1, policy?.multiplier ?? 2)
2586
+ };
2587
+ }
2588
+ function parseJsonObject(value, fallback) {
2589
+ if (!value)
2590
+ return fallback;
2591
+ const parsed = JSON.parse(value);
2592
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
2593
+ throw new Error("Expected a JSON object");
2594
+ }
2595
+ return parsed;
2596
+ }
2597
+ function parseHeaders(values) {
2598
+ if (!values?.length)
2599
+ return;
2600
+ const headers = {};
2601
+ for (const value of values) {
2602
+ const separator = value.indexOf("=");
2603
+ if (separator === -1)
2604
+ throw new Error(`Invalid header, expected name=value: ${value}`);
2605
+ headers[value.slice(0, separator)] = value.slice(separator + 1);
2606
+ }
2607
+ return headers;
2608
+ }
2609
+ function parseFilter(options) {
2610
+ const filter2 = {};
2611
+ if (options.source)
2612
+ filter2.source = options.source;
2613
+ if (options.type)
2614
+ filter2.type = options.type;
2615
+ if (options.subject)
2616
+ filter2.subject = options.subject;
2617
+ if (options.severity)
2618
+ filter2.severity = options.severity;
2619
+ return Object.keys(filter2).length > 0 ? [filter2] : undefined;
2620
+ }
2621
+ function createClient(options) {
2622
+ if (options.createClient)
2623
+ return options.createClient();
2624
+ return new EventsClient({ store: new JsonEventsStore(options.dataDir) });
2625
+ }
2626
+ function print(value, json, text) {
2627
+ if (json)
2628
+ console.log(JSON.stringify(value, null, 2));
2629
+ else
2630
+ console.log(text);
2631
+ }
2632
+ function hasJsonOption(options) {
2633
+ return Boolean(options?.json || options?.opts?.().json || options?.optsWithGlobals?.().json || options?.parent?.opts?.().json || options?.parent?.optsWithGlobals?.().json);
2634
+ }
2635
+ function wantsJson(actionOptions, command) {
2636
+ return hasJsonOption(actionOptions) || hasJsonOption(command);
2637
+ }
2638
+ function registerWebhookCommands(program, options) {
2639
+ const webhooks = program.command(options.webhooksCommandName ?? "webhooks").description("Manage Hasna event webhook subscriptions");
2640
+ 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) => {
2641
+ const timestamp = new Date().toISOString();
2642
+ const channel = {
2643
+ id: actionOptions.id,
2644
+ name: actionOptions.name,
2645
+ enabled: !actionOptions.disabled,
2646
+ transport: actionOptions.transport,
2647
+ filters: parseFilter(actionOptions),
2648
+ retry: actionOptions.retryAttempts || actionOptions.retryBackoffMs ? { maxAttempts: actionOptions.retryAttempts, backoffMs: actionOptions.retryBackoffMs } : undefined,
2649
+ redact: actionOptions.redact?.length ? { paths: actionOptions.redact } : undefined,
2650
+ createdAt: timestamp,
2651
+ updatedAt: timestamp
2652
+ };
2653
+ if (actionOptions.transport === "webhook") {
2654
+ channel.webhook = { url: target, secret: actionOptions.secret, headers: parseHeaders(actionOptions.header), timeoutMs: actionOptions.timeoutMs };
2655
+ } else if (actionOptions.transport === "command") {
2656
+ channel.command = { command: target, args: actionOptions.arg ?? [], timeoutMs: actionOptions.timeoutMs };
2657
+ } else {
2658
+ throw new Error(`Transport ${actionOptions.transport} is reserved for future use and cannot be added yet`);
2659
+ }
2660
+ const saved = await createClient(options).addChannel(channel);
2661
+ print(sanitizeChannelForOutput(saved), wantsJson(actionOptions, command), `Added ${saved.transport} channel ${saved.id}`);
2662
+ });
2663
+ webhooks.command("list").description("List configured subscriptions").option("-j, --json", "Print JSON output", false).action(async (actionOptions, command) => {
2664
+ const channels = await createClient(options).listChannels();
2665
+ if (wantsJson(actionOptions, command)) {
2666
+ console.log(JSON.stringify(sanitizeChannelsForOutput(channels), null, 2));
2667
+ return;
2668
+ }
2669
+ if (!channels.length) {
2670
+ console.log("No channels configured.");
2671
+ return;
2672
+ }
2673
+ for (const channel of channels) {
2674
+ console.log(`${channel.id} ${channel.enabled ? "enabled" : "disabled"} ${channel.transport} ${channel.webhook?.url ?? channel.command?.command ?? channel.transport}`);
2675
+ }
2676
+ });
2677
+ webhooks.command("remove").description("Remove a subscription").argument("<id>", "Subscription/channel identifier").option("-j, --json", "Print JSON output", false).action(async (id, actionOptions, command) => {
2678
+ const removed = await createClient(options).removeChannel(id);
2679
+ print({ removed }, wantsJson(actionOptions, command), removed ? `Removed ${id}` : `Channel not found: ${id}`);
2680
+ });
2681
+ 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) => {
2682
+ const result = await createClient(options).testChannel(id, {
2683
+ source: options.source,
2684
+ type: actionOptions.type,
2685
+ subject: actionOptions.subject ?? id,
2686
+ message: actionOptions.message,
2687
+ data: parseJsonObject(actionOptions.data, { test: true })
2688
+ });
2689
+ print(result, wantsJson(actionOptions, command), `${result.status}: ${result.channelId}`);
2690
+ });
2691
+ return webhooks;
2692
+ }
2693
+ function registerEventCommands(program, options) {
2694
+ const events = program.command(options.eventsCommandName ?? "events").description("Emit, list, and replay Hasna events");
2695
+ 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) => {
2696
+ const result = await createClient(options).emit({
2697
+ source: actionOptions.source ?? options.source,
2698
+ type,
2699
+ subject: actionOptions.subject,
2700
+ severity: actionOptions.severity,
2701
+ message: actionOptions.message,
2702
+ dedupeKey: actionOptions.dedupeKey,
2703
+ data: parseJsonObject(actionOptions.data, {}),
2704
+ metadata: parseJsonObject(actionOptions.metadata, {})
2705
+ }, { deliver: actionOptions.deliver, dedupe: actionOptions.dedupe });
2706
+ print(result, wantsJson(actionOptions, command), `${result.deduped ? "Deduped" : "Emitted"} ${result.event.id} to ${result.deliveries.length} channel(s)`);
2707
+ });
2708
+ 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) => {
2709
+ let rows = await createClient(options).listEvents();
2710
+ if (actionOptions.source)
2711
+ rows = rows.filter((event) => event.source === actionOptions.source);
2712
+ if (actionOptions.type)
2713
+ rows = rows.filter((event) => event.type === actionOptions.type);
2714
+ if (actionOptions.limit)
2715
+ rows = rows.slice(-actionOptions.limit);
2716
+ if (wantsJson(actionOptions, command)) {
2717
+ console.log(JSON.stringify(rows, null, 2));
2718
+ return;
2719
+ }
2720
+ if (!rows.length) {
2721
+ console.log("No events recorded.");
2722
+ return;
2723
+ }
2724
+ for (const event of rows)
2725
+ console.log(`${event.time} ${event.id} ${event.source} ${event.type} ${event.severity}`);
2726
+ });
2727
+ 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) => {
2728
+ const result = await createClient(options).replay({
2729
+ eventId: actionOptions.id,
2730
+ source: actionOptions.source,
2731
+ type: actionOptions.type,
2732
+ dryRun: actionOptions.dryRun
2733
+ });
2734
+ print(result, wantsJson(actionOptions, command), `Replayed ${result.events.length} event(s), ${result.deliveries.length} delivery result(s)`);
2735
+ });
2736
+ return events;
2737
+ }
2738
+ function registerEventsCommands(program, options) {
2739
+ registerWebhookCommands(program, options);
2740
+ registerEventCommands(program, options);
2741
+ }
2742
+ function parseNumber(value) {
2743
+ const parsed = Number(value);
2744
+ if (!Number.isFinite(parsed))
2745
+ throw new Error(`Expected a number, got ${value}`);
2746
+ return parsed;
2747
+ }
2748
+ function collectValues(value, previous) {
2749
+ previous.push(value);
2750
+ return previous;
2751
+ }
2752
+
2069
2753
  // node_modules/commander/esm.mjs
2070
2754
  var import__ = __toESM(require_commander(), 1);
2071
2755
  var {
@@ -2087,16 +2771,16 @@ import chalk from "chalk";
2087
2771
 
2088
2772
  // src/db/database.ts
2089
2773
  import { Database } from "bun:sqlite";
2090
- import { existsSync, mkdirSync, unlinkSync } from "fs";
2091
- import { dirname, join, resolve } from "path";
2774
+ import { existsSync as existsSync2, mkdirSync, unlinkSync } from "fs";
2775
+ import { dirname, join as join2, resolve } from "path";
2092
2776
  function isInMemoryDb(path) {
2093
2777
  return path === ":memory:" || path.startsWith("file::memory:");
2094
2778
  }
2095
2779
  function findNearestCalendarDb(startDir) {
2096
2780
  let dir = resolve(startDir);
2097
2781
  while (true) {
2098
- const candidate = join(dir, ".calendar", "calendar.db");
2099
- if (existsSync(candidate))
2782
+ const candidate = join2(dir, ".calendar", "calendar.db");
2783
+ if (existsSync2(candidate))
2100
2784
  return candidate;
2101
2785
  const parent = dirname(dir);
2102
2786
  if (parent === dir)
@@ -2108,7 +2792,7 @@ function findNearestCalendarDb(startDir) {
2108
2792
  function findGitRoot(startDir) {
2109
2793
  let dir = resolve(startDir);
2110
2794
  while (true) {
2111
- if (existsSync(join(dir, ".git")))
2795
+ if (existsSync2(join2(dir, ".git")))
2112
2796
  return dir;
2113
2797
  const parent = dirname(dir);
2114
2798
  if (parent === dir)
@@ -2131,13 +2815,13 @@ function getDbPath() {
2131
2815
  if (process.env["CALENDAR_DB_SCOPE"] === "project") {
2132
2816
  const gitRoot = findGitRoot(cwd);
2133
2817
  if (gitRoot) {
2134
- return join(gitRoot, ".calendar", "calendar.db");
2818
+ return join2(gitRoot, ".calendar", "calendar.db");
2135
2819
  }
2136
2820
  }
2137
2821
  const home = process.env["HOME"] || process.env["USERPROFILE"] || "~";
2138
- const newPath = join(home, ".hasna", "calendar", "calendar.db");
2139
- const legacyPath = join(home, ".calendar", "calendar.db");
2140
- if (!existsSync(newPath) && existsSync(legacyPath)) {
2822
+ const newPath = join2(home, ".hasna", "calendar", "calendar.db");
2823
+ const legacyPath = join2(home, ".calendar", "calendar.db");
2824
+ if (!existsSync2(newPath) && existsSync2(legacyPath)) {
2141
2825
  return legacyPath;
2142
2826
  }
2143
2827
  return newPath;
@@ -2146,7 +2830,7 @@ function ensureDir(filePath) {
2146
2830
  if (isInMemoryDb(filePath))
2147
2831
  return;
2148
2832
  const dir = dirname(resolve(filePath));
2149
- if (!existsSync(dir)) {
2833
+ if (!existsSync2(dir)) {
2150
2834
  mkdirSync(dir, { recursive: true });
2151
2835
  }
2152
2836
  }
@@ -2581,7 +3265,7 @@ function rowToEvent(row) {
2581
3265
  updated_at: row.updated_at
2582
3266
  };
2583
3267
  }
2584
- function createEvent(input, db) {
3268
+ function createEvent2(input, db) {
2585
3269
  db = db || getDatabase();
2586
3270
  const id = crypto.randomUUID().slice(0, 8);
2587
3271
  db.run(`INSERT INTO events (id, calendar_id, org_id, title, description, location, start_at, end_at, all_day, timezone, status, busy_type, visibility, recurrence_rule, recurrence_exception_dates, source_task_id, created_by, metadata)
@@ -2900,7 +3584,7 @@ program2.command("add <title>").description("Create an event").requiredOption("-
2900
3584
  output(chalk.red("Calendar not found"));
2901
3585
  process.exit(1);
2902
3586
  }
2903
- const evt = createEvent({
3587
+ const evt = createEvent2({
2904
3588
  title,
2905
3589
  calendar_id: opts.calendar,
2906
3590
  org_id: opts.org || cal.org_id,
@@ -3025,4 +3709,5 @@ program2.command("agent-orgs <agentId>").description("List orgs an agent belongs
3025
3709
  function output(text) {
3026
3710
  console.log(text);
3027
3711
  }
3712
+ registerEventsCommands(program2, { source: "calendar" });
3028
3713
  program2.parse();
@@ -3641,7 +3641,7 @@ class JSONSchemaGenerator {
3641
3641
  if (val === undefined) {
3642
3642
  if (this.unrepresentable === "throw") {
3643
3643
  throw new Error("Literal `undefined` cannot be represented in JSON Schema");
3644
- } else {}
3644
+ }
3645
3645
  } else if (typeof val === "bigint") {
3646
3646
  if (this.unrepresentable === "throw") {
3647
3647
  throw new Error("BigInt literals cannot be represented in JSON Schema");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hasna/calendar",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Universal calendar management for AI coding agents - CLI + MCP server + interactive TUI",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -61,6 +61,7 @@
61
61
  "license": "Apache-2.0",
62
62
  "dependencies": {
63
63
  "@hasna/cloud": "0.1.28",
64
+ "@hasna/events": "^0.1.6",
64
65
  "@modelcontextprotocol/sdk": "^1.12.1",
65
66
  "chalk": "^5.4.1",
66
67
  "commander": "^13.1.0",