abxbus 2.5.4 → 2.5.6

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.
Files changed (39) hide show
  1. package/dist/cjs/BaseEvent.d.ts +68 -23
  2. package/dist/cjs/BaseEvent.js +131 -14
  3. package/dist/cjs/BaseEvent.js.map +2 -2
  4. package/dist/cjs/base_event.d.ts +2 -2
  5. package/dist/cjs/bridge_ipc.d.ts +45 -0
  6. package/dist/cjs/event_handler.d.ts +1 -0
  7. package/dist/cjs/events_suck.d.ts +1 -1
  8. package/dist/cjs/events_suck.js.map +2 -2
  9. package/dist/cjs/jsonschema.d.ts +6 -0
  10. package/dist/cjs/jsonschema.js +155 -0
  11. package/dist/cjs/jsonschema.js.map +7 -0
  12. package/dist/cjs/middleware_otel_tracing.d.ts +49 -0
  13. package/dist/cjs/types.d.ts +3 -4
  14. package/dist/cjs/types.js +8 -15
  15. package/dist/cjs/types.js.map +2 -2
  16. package/dist/esm/BaseEvent.js +131 -14
  17. package/dist/esm/BaseEvent.js.map +2 -2
  18. package/dist/esm/events_suck.js.map +2 -2
  19. package/dist/esm/jsonschema.js +135 -0
  20. package/dist/esm/jsonschema.js.map +7 -0
  21. package/dist/esm/types.js +7 -14
  22. package/dist/esm/types.js.map +2 -2
  23. package/dist/types/BaseEvent.d.ts +68 -23
  24. package/dist/types/base_event.d.ts +2 -2
  25. package/dist/types/bridge_ipc.d.ts +45 -0
  26. package/dist/types/event_handler.d.ts +1 -0
  27. package/dist/types/events_suck.d.ts +1 -1
  28. package/dist/types/jsonschema.d.ts +6 -0
  29. package/dist/types/middleware_otel_tracing.d.ts +49 -0
  30. package/dist/types/types.d.ts +3 -4
  31. package/package.json +1 -1
  32. package/src/BaseEvent.ts +277 -54
  33. package/src/events_suck.ts +3 -2
  34. package/src/jsonschema.ts +146 -0
  35. package/src/types.ts +6 -14
  36. package/dist/cjs/CoreClient.d.ts +0 -167
  37. package/dist/cjs/CoreEventBus.d.ts +0 -334
  38. package/dist/types/CoreClient.d.ts +0 -167
  39. package/dist/types/CoreEventBus.d.ts +0 -334
@@ -0,0 +1,135 @@
1
+ import { z } from "zod";
2
+ const isJsonSchemaObject = (value) => {
3
+ return typeof value === "object" && value !== null && !Array.isArray(value);
4
+ };
5
+ const isJsonSchema = (value) => {
6
+ if (typeof value === "boolean") {
7
+ return true;
8
+ }
9
+ if (!isJsonSchemaObject(value)) {
10
+ return false;
11
+ }
12
+ if ("_zod" in value || "_def" in value || "~standard" in value) {
13
+ return false;
14
+ }
15
+ return true;
16
+ };
17
+ const nullUnionCandidates = (schema) => {
18
+ if (Array.isArray(schema.type) && schema.type.includes("null")) {
19
+ const non_null_types = schema.type.filter((item) => typeof item === "string" && item !== "null");
20
+ if (non_null_types.length > 0) {
21
+ return [{ type: non_null_types.length === 1 ? non_null_types[0] : non_null_types }, { type: "null" }];
22
+ }
23
+ }
24
+ return null;
25
+ };
26
+ const normalizeJsonSchema = (schema) => {
27
+ const normalized = normalizeJsonSchemaValue(schema);
28
+ if (!isJsonSchemaObject(normalized)) {
29
+ return normalized;
30
+ }
31
+ const schema_record = { ...normalized };
32
+ const definitions = schema_record.$defs;
33
+ const root_ref = rootRefForSchema(schema_record, definitions);
34
+ if (!root_ref || !definitions) {
35
+ schema_record.$schema ??= "https://json-schema.org/draft/2020-12/schema";
36
+ return schema_record;
37
+ }
38
+ const root_name = root_ref.slice("#/$defs/".length);
39
+ const root_schema = definitions[root_name];
40
+ if (!isJsonSchemaObject(root_schema)) {
41
+ schema_record.$schema ??= "https://json-schema.org/draft/2020-12/schema";
42
+ return schema_record;
43
+ }
44
+ const rewritten_root = rewriteJsonSchemaRefs(root_schema, { [root_ref]: "#" });
45
+ const remaining_defs = Object.fromEntries(Object.entries(definitions).filter(([name]) => name !== root_name));
46
+ if (Object.keys(remaining_defs).length > 0) {
47
+ rewritten_root.$defs = rewriteJsonSchemaRefs(remaining_defs, { [root_ref]: "#" });
48
+ }
49
+ rewritten_root.$schema ??= schema_record.$schema ?? "https://json-schema.org/draft/2020-12/schema";
50
+ setTitleFromInlinedRootDefinition(rewritten_root, root_name);
51
+ return rewritten_root;
52
+ };
53
+ const rootRefForSchema = (schema, definitions) => {
54
+ if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/$defs/")) {
55
+ return schema.$ref;
56
+ }
57
+ if (!definitions) {
58
+ return null;
59
+ }
60
+ const root = schemaWithoutSchemaAndDefinitions(schema);
61
+ for (const [name, definition] of Object.entries(definitions)) {
62
+ if (JSON.stringify(definition) === JSON.stringify(root)) {
63
+ return `#/$defs/${name}`;
64
+ }
65
+ }
66
+ return null;
67
+ };
68
+ const schemaWithoutSchemaAndDefinitions = (schema) => {
69
+ const root = {};
70
+ for (const [key, value] of Object.entries(schema)) {
71
+ if (key !== "$schema" && key !== "$defs") {
72
+ root[key] = value;
73
+ }
74
+ }
75
+ return root;
76
+ };
77
+ const setTitleFromInlinedRootDefinition = (schema, root_name) => {
78
+ if (root_name.startsWith("__schema")) return;
79
+ schema.title ??= root_name;
80
+ };
81
+ const rewriteJsonSchemaRefs = (schema, refs) => {
82
+ if (Array.isArray(schema)) {
83
+ return schema.map((item) => rewriteJsonSchemaRefs(item, refs));
84
+ }
85
+ if (!isJsonSchemaObject(schema)) {
86
+ return schema;
87
+ }
88
+ const rewritten = {};
89
+ for (const [key, value] of Object.entries(schema)) {
90
+ rewritten[key] = rewriteJsonSchemaRefs(value, refs);
91
+ }
92
+ if (typeof rewritten.$ref === "string" && rewritten.$ref in refs) {
93
+ rewritten.$ref = refs[rewritten.$ref];
94
+ }
95
+ return rewritten;
96
+ };
97
+ const normalizeJsonSchemaValue = (schema) => {
98
+ if (Array.isArray(schema)) {
99
+ return schema.map((item) => normalizeJsonSchemaValue(item));
100
+ }
101
+ if (!isJsonSchemaObject(schema)) {
102
+ return schema;
103
+ }
104
+ const normalized = {};
105
+ for (const [key, value] of Object.entries(schema)) {
106
+ normalized[key] = normalizeJsonSchemaValue(value);
107
+ }
108
+ if (Array.isArray(normalized.required) && normalized.required.every((item) => typeof item === "string")) {
109
+ normalized.required = [...normalized.required].sort();
110
+ }
111
+ const null_union_candidates = nullUnionCandidates(normalized);
112
+ if (null_union_candidates !== null) {
113
+ const merged = { anyOf: normalizeJsonSchemaValue(null_union_candidates) };
114
+ for (const [key, value] of Object.entries(normalized)) {
115
+ if (key !== "type") {
116
+ merged[key] = value;
117
+ }
118
+ }
119
+ return merged;
120
+ }
121
+ return normalized;
122
+ };
123
+ const toJsonSchema = (schema) => {
124
+ return normalizeJsonSchema(z.toJSONSchema(schema));
125
+ };
126
+ const fromJsonSchema = (schema) => {
127
+ return z.fromJSONSchema(schema);
128
+ };
129
+ export {
130
+ fromJsonSchema,
131
+ isJsonSchema,
132
+ normalizeJsonSchema,
133
+ toJsonSchema
134
+ };
135
+ //# sourceMappingURL=jsonschema.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/jsonschema.ts"],
4
+ "sourcesContent": ["import { z } from 'zod'\n\nexport type JsonSchema = boolean | z.core.JSONSchema.JSONSchema\ntype JsonSchemaObject = z.core.JSONSchema.JSONSchema\n\nconst isJsonSchemaObject = (value: unknown): value is JsonSchemaObject => {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nexport const isJsonSchema = (value: unknown): value is JsonSchema => {\n if (typeof value === 'boolean') {\n return true\n }\n if (!isJsonSchemaObject(value)) {\n return false\n }\n if ('_zod' in value || '_def' in value || '~standard' in value) {\n return false\n }\n return true\n}\n\nconst nullUnionCandidates = (schema: Record<string, unknown>): Record<string, unknown>[] | null => {\n if (Array.isArray(schema.type) && schema.type.includes('null')) {\n const non_null_types = schema.type.filter((item): item is string => typeof item === 'string' && item !== 'null')\n if (non_null_types.length > 0) {\n return [{ type: non_null_types.length === 1 ? non_null_types[0] : non_null_types }, { type: 'null' }]\n }\n }\n\n return null\n}\n\nexport const normalizeJsonSchema = (schema: JsonSchema): JsonSchema => {\n const normalized = normalizeJsonSchemaValue(schema)\n if (!isJsonSchemaObject(normalized)) {\n return normalized as JsonSchema\n }\n const schema_record = { ...normalized } as JsonSchemaObject\n const definitions = schema_record.$defs\n const root_ref = rootRefForSchema(schema_record, definitions)\n if (!root_ref || !definitions) {\n schema_record.$schema ??= 'https://json-schema.org/draft/2020-12/schema'\n return schema_record\n }\n const root_name = root_ref.slice('#/$defs/'.length)\n const root_schema = definitions[root_name]\n if (!isJsonSchemaObject(root_schema)) {\n schema_record.$schema ??= 'https://json-schema.org/draft/2020-12/schema'\n return schema_record\n }\n const rewritten_root = rewriteJsonSchemaRefs(root_schema, { [root_ref]: '#' }) as JsonSchemaObject\n const remaining_defs = Object.fromEntries(Object.entries(definitions).filter(([name]) => name !== root_name))\n if (Object.keys(remaining_defs).length > 0) {\n rewritten_root.$defs = rewriteJsonSchemaRefs(remaining_defs, { [root_ref]: '#' }) as Record<string, JsonSchemaObject>\n }\n rewritten_root.$schema ??= schema_record.$schema ?? 'https://json-schema.org/draft/2020-12/schema'\n setTitleFromInlinedRootDefinition(rewritten_root, root_name)\n return rewritten_root\n}\n\nconst rootRefForSchema = (schema: JsonSchemaObject, definitions: Record<string, JsonSchemaObject> | undefined): string | null => {\n if (typeof schema.$ref === 'string' && schema.$ref.startsWith('#/$defs/')) {\n return schema.$ref\n }\n if (!definitions) {\n return null\n }\n const root = schemaWithoutSchemaAndDefinitions(schema)\n for (const [name, definition] of Object.entries(definitions)) {\n if (JSON.stringify(definition) === JSON.stringify(root)) {\n return `#/$defs/${name}`\n }\n }\n return null\n}\n\nconst schemaWithoutSchemaAndDefinitions = (schema: JsonSchemaObject): Record<string, unknown> => {\n const root: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(schema)) {\n if (key !== '$schema' && key !== '$defs') {\n root[key] = value\n }\n }\n return root\n}\n\nconst setTitleFromInlinedRootDefinition = (schema: JsonSchemaObject, root_name: string): void => {\n if (root_name.startsWith('__schema')) return\n schema.title ??= root_name\n}\n\nconst rewriteJsonSchemaRefs = (schema: unknown, refs: Record<string, string>): unknown => {\n if (Array.isArray(schema)) {\n return schema.map((item) => rewriteJsonSchemaRefs(item, refs))\n }\n if (!isJsonSchemaObject(schema)) {\n return schema\n }\n const rewritten: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(schema)) {\n rewritten[key] = rewriteJsonSchemaRefs(value, refs)\n }\n if (typeof rewritten.$ref === 'string' && rewritten.$ref in refs) {\n rewritten.$ref = refs[rewritten.$ref]\n }\n return rewritten\n}\n\nconst normalizeJsonSchemaValue = (schema: unknown): unknown => {\n if (Array.isArray(schema)) {\n return schema.map((item) => normalizeJsonSchemaValue(item))\n }\n if (!isJsonSchemaObject(schema)) {\n return schema\n }\n\n const normalized: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(schema)) {\n normalized[key] = normalizeJsonSchemaValue(value)\n }\n if (Array.isArray(normalized.required) && normalized.required.every((item) => typeof item === 'string')) {\n normalized.required = [...normalized.required].sort()\n }\n\n const null_union_candidates = nullUnionCandidates(normalized)\n if (null_union_candidates !== null) {\n const merged: Record<string, unknown> = { anyOf: normalizeJsonSchemaValue(null_union_candidates) }\n for (const [key, value] of Object.entries(normalized)) {\n if (key !== 'type') {\n merged[key] = value\n }\n }\n return merged\n }\n\n return normalized\n}\n\nexport const toJsonSchema = (schema: z.core.$ZodType): JsonSchema => {\n return normalizeJsonSchema(z.toJSONSchema(schema) as JsonSchema)\n}\n\nexport const fromJsonSchema = (schema: JsonSchema): z.ZodTypeAny => {\n return z.fromJSONSchema(schema)\n}\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAKlB,MAAM,qBAAqB,CAAC,UAA8C;AACxE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AAEO,MAAM,eAAe,CAAC,UAAwC;AACnE,MAAI,OAAO,UAAU,WAAW;AAC9B,WAAO;AAAA,EACT;AACA,MAAI,CAAC,mBAAmB,KAAK,GAAG;AAC9B,WAAO;AAAA,E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,KAAK,UAAU,IAAI;AAAA,EACtC;AACA,SAAO;AACT;AAEA,MAAM,2BAA2B,CAAC,WAA6B;AAC7D,MAAI,MAAM,QAAQ,MAAM,GAAG;AACzB,WAAO,OAAO,IAAI,CAAC,SAAS,yBAAyB,IAAI,CAAC;AAAA,EAC5D;AACA,MAAI,CAAC,mBAAmB,MAAM,GAAG;AAC/B,WAAO;AAAA,EACT;AAEA,QAAM,aAAsC,CAAC;AAC7C,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,GAAG;AACjD,eAAW,GAAG,IAAI,yBAAyB,KAAK;AAAA,EAClD;AACA,MAAI,MAAM,QAAQ,WAAW,QAAQ,KAAK,WAAW,SAAS,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AACvG,eAAW,WAAW,CAAC,GAAG,WAAW,QAAQ,EAAE,KAAK;AAAA,EACtD;AAEA,QAAM,wBAAwB,oBAAoB,UAAU;AAC5D,MAAI,0BAA0B,MAAM;AAClC,UAAM,SAAkC,EAAE,OAAO,yBAAyB,qBAAqB,EAAE;AACjG,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,UAAU,GAAG;AACrD,UAAI,QAAQ,QAAQ;AAClB,eAAO,GAAG,IAAI;AAAA,MAChB;AAAA,IACF;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,MAAM,eAAe,CAAC,WAAwC;AACnE,SAAO,oBAAoB,EAAE,aAAa,MAAM,CAAe;AACjE;AAEO,MAAM,iBAAiB,CAAC,WAAqC;AAClE,SAAO,EAAE,eAAe,MAAM;AAChC;",
6
+ "names": []
7
+ }
package/dist/esm/types.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ import { fromJsonSchema, isJsonSchema } from "./jsonschema.js";
2
3
  const normalizeEventPattern = (event_pattern) => {
3
4
  if (event_pattern === "*") {
4
5
  return "*";
@@ -50,16 +51,7 @@ const extractZodShape = (raw) => {
50
51
  }
51
52
  return shape;
52
53
  };
53
- const toJsonSchema = (schema) => {
54
- if (!schema || !isZodSchema(schema)) return schema;
55
- const zod_any = z;
56
- return zod_any.toJSONSchema(schema);
57
- };
58
- const fromJsonSchema = (schema) => {
59
- const zod_any = z;
60
- return zod_any.fromJSONSchema(schema);
61
- };
62
- const normalizeEventResultType = (value) => {
54
+ function normalizeEventResultType(value) {
63
55
  if (value === void 0 || value === null) {
64
56
  return void 0;
65
57
  }
@@ -70,15 +62,16 @@ const normalizeEventResultType = (value) => {
70
62
  if (constructor_schema) {
71
63
  return constructor_schema;
72
64
  }
65
+ if (!isJsonSchema(value)) {
66
+ throw new Error(`event_result_type must be a Zod schema, constructor shorthand, or JSON Schema value, got: ${typeof value}`);
67
+ }
73
68
  return fromJsonSchema(value);
74
- };
69
+ }
75
70
  export {
76
71
  eventResultTypeFromConstructor,
77
72
  extractZodShape,
78
- fromJsonSchema,
79
73
  isZodSchema,
80
74
  normalizeEventPattern,
81
- normalizeEventResultType,
82
- toJsonSchema
75
+ normalizeEventResultType
83
76
  };
84
77
  //# sourceMappingURL=types.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/types.ts"],
4
- "sourcesContent": ["import { z } from 'zod'\nimport type { BaseEvent } from './BaseEvent.js'\n\nexport type EventStatus = 'pending' | 'started' | 'completed'\n\nexport type EventClass<T extends BaseEvent = BaseEvent> = { event_type?: string } & (new (...args: any[]) => T)\n\nexport type EventPattern<T extends BaseEvent = BaseEvent> = string | EventClass<T>\n\nexport type EventWithResultSchema<TResult> = BaseEvent & { __event_result_type__?: TResult }\n\nexport type EventResultType<TEvent extends BaseEvent> = TEvent extends { __event_result_type__?: infer TResult } ? TResult : unknown\n\nexport type EventResultTypeConstructor = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor\n\nexport type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | unknown\n\nexport type EventHandlerReturn<T extends BaseEvent = BaseEvent> = EventResultType<T> | BaseEvent | null | void\n\nexport type EventHandlerCallable<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | Promise<EventHandlerReturn<T>>\n\n// For string and wildcard subscriptions we cannot reliably infer which event\n// type will arrive, so return type checking intentionally degrades to unknown.\nexport type UntypedEventHandlerFunction<T extends BaseEvent = BaseEvent> = (\n event: T\n) => EventHandlerReturn<T> | unknown | Promise<EventHandlerReturn<T> | unknown>\n\nexport type FindWindow = boolean | number\n\ntype FindReservedOptionKeys = 'past' | 'future' | 'child_of'\n\ntype EventFilterFields<T extends BaseEvent> = {\n [K in keyof T as string extends K\n ? never\n : number extends K\n ? never\n : symbol extends K\n ? never\n : K extends FindReservedOptionKeys\n ? never\n : T[K] extends (...args: any[]) => any\n ? never\n : K]?: T[K]\n}\n\nexport type FindOptions<T extends BaseEvent = BaseEvent> = {\n past?: FindWindow\n future?: FindWindow\n child_of?: BaseEvent | null\n} & EventFilterFields<T> &\n Record<string, unknown>\n\nexport type FilterOptions<T extends BaseEvent = BaseEvent> = FindOptions<T> & { limit?: number | null }\n\nexport const normalizeEventPattern = (event_pattern: EventPattern | '*'): string | '*' => {\n if (event_pattern === '*') {\n return '*'\n }\n if (typeof event_pattern === 'string') {\n return event_pattern\n }\n const event_type = (event_pattern as { event_type?: unknown }).event_type\n if (typeof event_type === 'string' && event_type.length > 0 && event_type !== 'BaseEvent') {\n return event_type\n }\n const class_name = (event_pattern as { name?: unknown }).name\n if (typeof class_name === 'string' && class_name.length > 0 && class_name !== 'BaseEvent') {\n return class_name\n }\n let preview: string\n try {\n const encoded = JSON.stringify(event_pattern)\n preview = typeof encoded === 'string' ? encoded.slice(0, 30) : String(event_pattern).slice(0, 30)\n } catch {\n preview = String(event_pattern).slice(0, 30)\n }\n throw new Error('bus.on(match_pattern, ...) must be a string event type, \"*\", or a BaseEvent class, got: ' + preview)\n}\n\nexport const isZodSchema = (value: unknown): value is z.ZodTypeAny => !!value && typeof (value as z.ZodTypeAny).safeParse === 'function'\n\nexport const eventResultTypeFromConstructor = (value: unknown): z.ZodTypeAny | undefined => {\n if (value === String) {\n return z.string()\n }\n if (value === Number) {\n return z.number()\n }\n if (value === Boolean) {\n return z.boolean()\n }\n if (value === Array) {\n return z.array(z.unknown())\n }\n if (value === Object) {\n return z.record(z.string(), z.unknown())\n }\n return undefined\n}\n\nexport const extractZodShape = (raw: Record<string, unknown>): z.ZodRawShape => {\n const shape: Record<string, z.ZodTypeAny> = {}\n for (const [key, value] of Object.entries(raw)) {\n if (key === 'event_result_type') continue\n if (isZodSchema(value)) shape[key] = value\n }\n return shape as z.ZodRawShape\n}\n\nexport const toJsonSchema = (schema: unknown): unknown => {\n if (!schema || !isZodSchema(schema)) return schema\n const zod_any = z as unknown as { toJSONSchema: (input: z.ZodTypeAny) => unknown }\n // Cross-language roundtrips preserve core structural types; constraint keywords may not roundtrip exactly.\n return zod_any.toJSONSchema(schema)\n}\n\nexport const fromJsonSchema = (schema: unknown): z.ZodTypeAny => {\n const zod_any = z as unknown as { fromJSONSchema: (input: unknown) => z.ZodTypeAny }\n return zod_any.fromJSONSchema(schema)\n}\n\nexport const normalizeEventResultType = (value: EventResultTypeInput): z.ZodTypeAny | undefined => {\n if (value === undefined || value === null) {\n return undefined\n }\n if (isZodSchema(value)) {\n return value\n }\n const constructor_schema = eventResultTypeFromConstructor(value)\n if (constructor_schema) {\n return constructor_schema\n }\n return fromJsonSchema(value)\n}\n"],
5
- "mappings": "AAAA,SAAS,SAAS;AAsDX,MAAM,wBAAwB,CAAC,kBAAoD;AACxF,MAAI,kBAAkB,KAAK;AACzB,WAAO;AAAA,EACT;AACA,MAAI,OAAO,kBAAkB,UAAU;AACrC,WAAO;AAAA,EACT;AACA,QAAM,aAAc,cAA2C;AAC/D,MAAI,OAAO,eAAe,YAAY,WAAW,SAAS,KAAK,eAAe,aAAa;AACzF,WAAO;AAAA,EACT;AACA,QAAM,aAAc,cAAqC;AACzD,MAAI,OAAO,eAAe,YAAY,WAAW,SA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,SAAO,QAAQ,eAAe,MAAM;AACtC;AAEO,MAAM,2BAA2B,CAAC,UAA0D;AACjG,MAAI,UAAU,UAAa,UAAU,MAAM;AACzC,WAAO;AAAA,EACT;AACA,MAAI,YAAY,KAAK,GAAG;AACtB,WAAO;AAAA,EACT;AACA,QAAM,qBAAqB,+BAA+B,KAAK;AAC/D,MAAI,oBAAoB;AACtB,WAAO;AAAA,EACT;AACA,SAAO,eAAe,KAAK;AAC7B;",
4
+ "sourcesContent": ["import { z } from 'zod'\nimport type { BaseEvent } from './BaseEvent.js'\nimport { fromJsonSchema, isJsonSchema, type JsonSchema } from './jsonschema.js'\n\nexport type EventStatus = 'pending' | 'started' | 'completed'\n\nexport type EventClass<T extends BaseEvent = BaseEvent> = { event_type?: string } & (new (...args: any[]) => T)\n\nexport type EventPattern<T extends BaseEvent = BaseEvent> = string | EventClass<T>\n\nexport type EventWithResultSchema<TResult> = BaseEvent & { __event_result_type__?: TResult }\n\nexport type EventResultType<TEvent extends BaseEvent> = TEvent extends { __event_result_type__?: infer TResult } ? TResult : unknown\n\nexport type EventResultTypeConstructor = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor\n\nexport type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | JsonSchema\n\nexport type EventHandlerReturn<T extends BaseEvent = BaseEvent> = EventResultType<T> | BaseEvent | null | void\n\nexport type EventHandlerCallable<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | Promise<EventHandlerReturn<T>>\n\n// For string and wildcard subscriptions we cannot reliably infer which event\n// type will arrive, so return type checking intentionally degrades to unknown.\nexport type UntypedEventHandlerFunction<T extends BaseEvent = BaseEvent> = (\n event: T\n) => EventHandlerReturn<T> | unknown | Promise<EventHandlerReturn<T> | unknown>\n\nexport type FindWindow = boolean | number\n\ntype FindReservedOptionKeys = 'past' | 'future' | 'child_of'\n\ntype EventFilterFields<T extends BaseEvent> = {\n [K in keyof T as string extends K\n ? never\n : number extends K\n ? never\n : symbol extends K\n ? never\n : K extends FindReservedOptionKeys\n ? never\n : T[K] extends (...args: any[]) => any\n ? never\n : K]?: T[K]\n}\n\nexport type FindOptions<T extends BaseEvent = BaseEvent> = {\n past?: FindWindow\n future?: FindWindow\n child_of?: BaseEvent | null\n} & EventFilterFields<T> &\n Record<string, unknown>\n\nexport type FilterOptions<T extends BaseEvent = BaseEvent> = FindOptions<T> & { limit?: number | null }\n\nexport const normalizeEventPattern = (event_pattern: EventPattern | '*'): string | '*' => {\n if (event_pattern === '*') {\n return '*'\n }\n if (typeof event_pattern === 'string') {\n return event_pattern\n }\n const event_type = (event_pattern as { event_type?: unknown }).event_type\n if (typeof event_type === 'string' && event_type.length > 0 && event_type !== 'BaseEvent') {\n return event_type\n }\n const class_name = (event_pattern as { name?: unknown }).name\n if (typeof class_name === 'string' && class_name.length > 0 && class_name !== 'BaseEvent') {\n return class_name\n }\n let preview: string\n try {\n const encoded = JSON.stringify(event_pattern)\n preview = typeof encoded === 'string' ? encoded.slice(0, 30) : String(event_pattern).slice(0, 30)\n } catch {\n preview = String(event_pattern).slice(0, 30)\n }\n throw new Error('bus.on(match_pattern, ...) must be a string event type, \"*\", or a BaseEvent class, got: ' + preview)\n}\n\nexport const isZodSchema = (value: unknown): value is z.ZodTypeAny => !!value && typeof (value as z.ZodTypeAny).safeParse === 'function'\n\nexport const eventResultTypeFromConstructor = (value: unknown): z.ZodTypeAny | undefined => {\n if (value === String) {\n return z.string()\n }\n if (value === Number) {\n return z.number()\n }\n if (value === Boolean) {\n return z.boolean()\n }\n if (value === Array) {\n return z.array(z.unknown())\n }\n if (value === Object) {\n return z.record(z.string(), z.unknown())\n }\n return undefined\n}\n\nexport const extractZodShape = (raw: Record<string, unknown>): z.ZodRawShape => {\n const shape: Record<string, z.ZodTypeAny> = {}\n for (const [key, value] of Object.entries(raw)) {\n if (key === 'event_result_type') continue\n if (isZodSchema(value)) shape[key] = value\n }\n return shape as z.ZodRawShape\n}\n\nexport function normalizeEventResultType(value: unknown): z.ZodTypeAny | undefined {\n if (value === undefined || value === null) {\n return undefined\n }\n if (isZodSchema(value)) {\n return value\n }\n const constructor_schema = eventResultTypeFromConstructor(value)\n if (constructor_schema) {\n return constructor_schema\n }\n if (!isJsonSchema(value)) {\n throw new Error(`event_result_type must be a Zod schema, constructor shorthand, or JSON Schema value, got: ${typeof value}`)\n }\n return fromJsonSchema(value)\n}\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAElB,SAAS,gBAAgB,oBAAqC;AAqDvD,MAAM,wBAAwB,CAAC,kBAAoD;AACxF,MAAI,kBAAkB,KAAK;AACzB,WAAO;AAAA,EACT;AACA,MAAI,OAAO,kBAAkB,UAAU;AACrC,WAAO;AAAA,EACT;AACA,QAAM,aAAc,cAA2C;AAC/D,MAAI,OAAO,eAAe,YAAY,WAAW,SAAS,KAAK,eAAe,aAAa;AACzF,WAAO;AAAA,EACT;AACA,QAAM,aAAc,cAAqC;AACzD,MAAI,OAAO,eAAe,YAAY,WAAW,SAAS,KAAK,eAAe,aAAa;AACzF,WAAO;AAAA,EACT;AACA,MAAI;AACJ,MAAI;AACF,UAAM,UAAU,KAAK,UAAU,aAAa;AAC5C,cAAU,OAAO,YAAY,WAAW,QAAQ,MAAM,GAAG,EAAE,IAAI,OAAO,aAAa,EAAE,MAAM,GAAG,EAAE;AAAA,EAClG,QAAQ;AACN,cAAU,OAAO,aAAa,EAAE,MAAM,GAAG,EAAE;AAAA,EAC7C;AACA,QAAM,IAAI,MAAM,6FAA6F,OAAO;AACtH;AAEO,MAAM,cAAc,CAAC,UAA0C,CAAC,CAAC,SAAS,OAAQ,MAAuB,cAAc;AAEvH,MAAM,iCAAiC,CAAC,UAA6C;AAC1F,MAAI,UAAU,QAAQ;AACpB,WAAO,EAAE,OAAO;AAAA,EAClB;AACA,MAAI,UAAU,QAAQ;AACpB,WAAO,EAAE,OAAO;AAAA,EAClB;AACA,MAAI,UAAU,SAAS;AACrB,WAAO,EAAE,QAAQ;AAAA,EACnB;AACA,MAAI,UAAU,OAAO;AACnB,WAAO,EAAE,MAAM,EAAE,QAAQ,CAAC;AAAA,EAC5B;AACA,MAAI,UAAU,QAAQ;AACpB,WAAO,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC;AAAA,EACzC;AACA,SAAO;AACT;AAEO,MAAM,kBAAkB,CAAC,QAAgD;AAC9E,QAAM,QAAsC,CAAC;AAC7C,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,QAAI,QAAQ,oBAAqB;AACjC,QAAI,YAAY,KAAK,EAAG,OAAM,GAAG,IAAI;AAAA,EACvC;AACA,SAAO;AACT;AAEO,SAAS,yBAAyB,OAA0C;AACjF,MAAI,UAAU,UAAa,UAAU,MAAM;AACzC,WAAO;AAAA,EACT;AACA,MAAI,YAAY,KAAK,GAAG;AACtB,WAAO;AAAA,EACT;AACA,QAAM,qBAAqB,+BAA+B,KAAK;AAC/D,MAAI,oBAAoB;AACtB,WAAO;AAAA,EACT;AACA,MAAI,CAAC,aAAa,KAAK,GAAG;AACxB,UAAM,IAAI,MAAM,6FAA6F,OAAO,KAAK,EAAE;AAAA,EAC7H;AACA,SAAO,eAAe,KAAK;AAC7B;",
6
6
  "names": []
7
7
  }
@@ -4,6 +4,7 @@ import { EventResult } from './EventResult.js';
4
4
  import { EventHandler } from './EventHandler.js';
5
5
  import type { EventConcurrencyMode, EventHandlerConcurrencyMode, EventHandlerCompletionMode, Deferred } from './LockManager.js';
6
6
  import { AsyncLock } from './LockManager.js';
7
+ import { type JsonSchema } from './jsonschema.js';
7
8
  import type { EventHandlerCallable, EventResultType } from './types.js';
8
9
  export declare const BaseEventSchema: z.ZodObject<{
9
10
  event_id: z.ZodString;
@@ -42,7 +43,7 @@ export declare const BaseEventSchema: z.ZodObject<{
42
43
  first: "first";
43
44
  }>>>;
44
45
  }, z.core.$loose>;
45
- type AnyEventSchema = z.ZodTypeAny;
46
+ type AnyEventSchema = z.ZodObject<z.ZodRawShape>;
46
47
  export type BaseEventData = z.infer<typeof BaseEventSchema>;
47
48
  export type BaseEventJSON = BaseEventData & Record<string, unknown>;
48
49
  type BaseEventFieldName = 'event_id' | 'event_created_at' | 'event_type' | 'event_version' | 'event_timeout' | 'event_slow_timeout' | 'event_handler_timeout' | 'event_handler_slow_timeout' | 'event_blocks_parent_completion' | 'event_parent_id' | 'event_path' | 'event_result_type' | 'event_emitted_by_handler_id' | 'event_pending_bus_count' | 'event_status' | 'event_started_at' | 'event_completed_at' | 'event_results' | 'event_concurrency' | 'event_handler_concurrency' | 'event_handler_completion';
@@ -52,20 +53,54 @@ type BaseEventFields = {
52
53
  export type BaseEventInit<TFields extends Record<string, unknown>> = TFields & Partial<BaseEventFields>;
53
54
  type BaseEventSchemaShape = typeof BaseEventSchema.shape;
54
55
  export type EventSchema<TShape extends z.ZodRawShape> = z.ZodObject<BaseEventSchemaShape & TShape>;
55
- type EventPayload<TShape extends z.ZodRawShape> = TShape extends Record<string, never> ? {} : z.infer<z.ZodObject<TShape>>;
56
+ type EventPayloadShape<TShape extends z.ZodRawShape> = {
57
+ [K in keyof TShape as K extends BaseEventFieldName ? never : K]: TShape[K];
58
+ };
59
+ type EventPayload<TShape extends z.ZodRawShape> = EventPayloadShape<TShape> extends Record<string, never> ? {} : z.infer<z.ZodObject<EventPayloadShape<TShape>>>;
60
+ type EventFactoryMetadataFieldName = 'class' | 'fromJSON' | 'prototype' | 'event_schema' | 'model_fields' | 'event_type' | 'event_version' | 'event_result_type';
61
+ type StaticDefaultSchema = z.ZodDefault<z.ZodTypeAny> | z.ZodPrefault<z.ZodTypeAny> | z.ZodCatch<z.ZodTypeAny>;
62
+ type EventModelFields<TShape extends z.ZodRawShape> = {
63
+ readonly [K in keyof TShape]: TShape[K];
64
+ };
65
+ type StaticEventDefaultValues<TShape extends z.ZodRawShape> = {
66
+ readonly [K in keyof TShape as K extends EventFactoryMetadataFieldName ? never : TShape[K] extends StaticDefaultSchema ? K : never]: z.output<TShape[K]>;
67
+ };
68
+ type StaticEventDefaultValuesFromSchema<TSchema extends AnyEventSchema> = TSchema extends z.ZodObject<infer TShape> ? StaticEventDefaultValues<TShape> : {};
69
+ type EventModelFieldsFromSchema<TSchema extends AnyEventSchema> = TSchema extends z.ZodObject<infer TShape> ? TSchema['shape'] & EventModelFields<TShape> : {};
70
+ type OptionalFactoryArgs<TData> = {} extends TData ? [data?: TData] : [data: TData];
56
71
  type EventInput<TShape extends z.ZodRawShape> = z.input<EventSchema<TShape>>;
57
72
  export type EventInit<TShape extends z.ZodRawShape> = Omit<EventInput<TShape>, keyof BaseEventFields> & Partial<BaseEventFields>;
58
- type EventPayloadFromSchema<TSchema extends AnyEventSchema> = z.output<TSchema> extends Record<string, unknown> ? z.output<TSchema> : {};
73
+ type EventPayloadFromSchema<TSchema extends AnyEventSchema> = z.output<TSchema> extends Record<string, unknown> ? Omit<z.output<TSchema>, keyof BaseEventFields> : {};
59
74
  type EventInputFromSchema<TSchema extends AnyEventSchema> = z.input<TSchema> extends Record<string, unknown> ? z.input<TSchema> : never;
60
75
  export type EventInitFromSchema<TSchema extends AnyEventSchema> = Omit<EventInputFromSchema<TSchema>, keyof BaseEventFields> & Partial<BaseEventFields>;
61
76
  type EventWithResultSchema<TResult> = BaseEvent & {
62
77
  __event_result_type__?: TResult;
63
78
  };
64
- type ResultTypeFromEventResultTypeInput<TInput> = TInput extends z.ZodTypeAny ? z.infer<TInput> : TInput extends StringConstructor ? string : TInput extends NumberConstructor ? number : TInput extends BooleanConstructor ? boolean : TInput extends ArrayConstructor ? unknown[] : TInput extends ObjectConstructor ? Record<string, unknown> : unknown;
79
+ type NormalizedEventResultSchema<TInput> = TInput extends z.ZodTypeAny ? TInput : TInput extends z.core.$ZodType ? z.ZodType<z.output<TInput>> : TInput extends StringConstructor ? z.ZodString : TInput extends NumberConstructor ? z.ZodNumber : TInput extends BooleanConstructor ? z.ZodBoolean : TInput extends ArrayConstructor ? z.ZodArray<z.ZodUnknown> : TInput extends ObjectConstructor ? z.ZodRecord<z.ZodString, z.ZodUnknown> : TInput extends JsonSchema ? z.ZodTypeAny : z.ZodTypeAny;
80
+ type ResultTypeSchemaFromShape<TShape> = TShape extends {
81
+ event_result_type: infer S;
82
+ } ? NormalizedEventResultSchema<S> : z.ZodTypeAny | undefined;
83
+ type ResultTypeSchemaFromEventSchema<TSchema> = TSchema extends z.ZodObject<infer TShape> ? ResultTypeSchemaFromShape<TShape> : z.ZodTypeAny | undefined;
84
+ type ResultTypeFromEventResultTypeInput<TInput> = TInput extends z.ZodTypeAny ? z.infer<TInput> : TInput extends z.core.$ZodType ? z.output<TInput> : TInput extends StringConstructor ? string : TInput extends NumberConstructor ? number : TInput extends BooleanConstructor ? boolean : TInput extends ArrayConstructor ? unknown[] : TInput extends ObjectConstructor ? Record<string, unknown> : TInput extends JsonSchema ? unknown : unknown;
65
85
  type ResultSchemaFromShape<TShape> = TShape extends {
66
86
  event_result_type: infer S;
67
87
  } ? ResultTypeFromEventResultTypeInput<S> : unknown;
68
88
  type ResultSchemaFromEventSchema<TSchema> = TSchema extends z.ZodObject<infer TShape> ? ResultSchemaFromShape<TShape> : unknown;
89
+ type ZodLiteralValue = string | number | bigint | boolean | null | undefined;
90
+ type ShortcutDefaultModelField<K, TValue> = K extends keyof BaseEventSchemaShape ? z.ZodDefault<BaseEventSchemaShape[K]> : z.ZodDefault<TValue extends ZodLiteralValue ? z.ZodLiteral<TValue> : z.ZodType<TValue>>;
91
+ type ShortcutModelFields<TShape> = {
92
+ [K in keyof TShape as K extends 'event_result_type' ? never : K]: TShape[K] extends z.ZodTypeAny ? TShape[K] : ShortcutDefaultModelField<K, TShape[K]>;
93
+ } & (TShape extends {
94
+ event_result_type: infer TResultType;
95
+ } ? {
96
+ event_result_type: NormalizedEventResultSchema<TResultType>;
97
+ } : {});
98
+ type ShortcutZodModelFields<TShape> = {
99
+ [K in keyof ShortcutModelFields<TShape>]: ShortcutModelFields<TShape>[K] extends z.ZodTypeAny ? ShortcutModelFields<TShape>[K] : never;
100
+ };
101
+ type ShortcutStaticDefaultValues<TShape, TModelFields extends z.ZodRawShape> = StaticEventDefaultValues<TModelFields> & {
102
+ readonly [K in keyof TShape as K extends EventFactoryMetadataFieldName ? never : TShape[K] extends z.ZodTypeAny ? never : K]: TShape[K];
103
+ };
69
104
  export type EventResultInclude<TEvent extends BaseEvent> = (result: EventResult<TEvent>['result'], event_result: EventResult<TEvent>) => boolean;
70
105
  export type EventResultOptions<TEvent extends BaseEvent> = {
71
106
  include?: EventResultInclude<TEvent>;
@@ -86,28 +121,35 @@ type EventResultUpdateOptions<TEvent extends BaseEvent> = {
86
121
  result?: EventResultType<TEvent> | BaseEvent | undefined;
87
122
  error?: unknown;
88
123
  };
89
- export type EventFactory<TShape extends z.ZodRawShape, TResult = unknown> = {
90
- (data: EventInit<TShape>): EventWithResultSchema<TResult> & EventPayload<TShape>;
91
- new (data: EventInit<TShape>): EventWithResultSchema<TResult> & EventPayload<TShape>;
124
+ export type EventFactoryClass<TShape extends z.ZodRawShape, TResult = unknown> = (new (...args: OptionalFactoryArgs<EventInit<TShape>>) => EventWithResultSchema<TResult> & EventPayload<TShape>) & StaticEventDefaultValues<TShape> & {
92
125
  event_schema: EventSchema<TShape>;
93
- class?: new (data: EventInit<TShape>) => EventWithResultSchema<TResult> & EventPayload<TShape>;
94
- event_type?: string;
95
- event_version?: string;
96
- event_result_type?: z.ZodTypeAny;
97
- fromJSON?: (data: unknown) => EventWithResultSchema<TResult> & EventPayload<TShape>;
126
+ model_fields: EventModelFields<TShape>;
98
127
  };
99
- export type SchemaEventFactory<TSchema extends AnyEventSchema, TResult = unknown> = {
100
- (data: EventInitFromSchema<TSchema>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
101
- new (data: EventInitFromSchema<TSchema>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
128
+ export type EventFactory<TShape extends z.ZodRawShape, TResult = unknown, TStaticFields = StaticEventDefaultValues<TShape>> = TStaticFields & {
129
+ (...args: OptionalFactoryArgs<EventInit<TShape>>): EventWithResultSchema<TResult> & EventPayload<TShape>;
130
+ new (...args: OptionalFactoryArgs<EventInit<TShape>>): EventWithResultSchema<TResult> & EventPayload<TShape>;
131
+ event_schema: EventSchema<TShape>;
132
+ model_fields: EventModelFields<TShape>;
133
+ class: EventFactoryClass<TShape, TResult>;
134
+ event_type: string;
135
+ event_version: string;
136
+ event_result_type: ResultTypeSchemaFromShape<TShape>;
137
+ fromJSON: (data: unknown) => EventWithResultSchema<TResult> & EventPayload<TShape>;
138
+ };
139
+ export type SchemaEventFactoryClass<TSchema extends AnyEventSchema, TResult = unknown> = (new (...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>) & StaticEventDefaultValuesFromSchema<TSchema> & {
102
140
  event_schema: TSchema;
103
- class?: new (data: EventInitFromSchema<TSchema>) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
104
- event_type?: string;
105
- event_version?: string;
106
- event_result_type?: z.ZodTypeAny;
107
- fromJSON?: (data: unknown) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
141
+ model_fields: EventModelFieldsFromSchema<TSchema>;
108
142
  };
109
- type ZodShapeFrom<TShape extends Record<string, unknown>> = {
110
- [K in keyof TShape as K extends 'event_result_type' ? never : TShape[K] extends z.ZodTypeAny ? K : never]: Extract<TShape[K], z.ZodTypeAny>;
143
+ export type SchemaEventFactory<TSchema extends AnyEventSchema, TResult = unknown> = StaticEventDefaultValuesFromSchema<TSchema> & {
144
+ (...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
145
+ new (...args: OptionalFactoryArgs<EventInitFromSchema<TSchema>>): EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
146
+ event_schema: TSchema;
147
+ model_fields: EventModelFieldsFromSchema<TSchema>;
148
+ class: SchemaEventFactoryClass<TSchema, TResult>;
149
+ event_type: string;
150
+ event_version: string;
151
+ event_result_type: ResultTypeSchemaFromEventSchema<TSchema>;
152
+ fromJSON: (data: unknown) => EventWithResultSchema<TResult> & EventPayloadFromSchema<TSchema>;
111
153
  };
112
154
  export declare class BaseEvent {
113
155
  event_id: string;
@@ -132,10 +174,13 @@ export declare class BaseEvent {
132
174
  event_handler_concurrency?: EventHandlerConcurrencyMode | null;
133
175
  event_handler_completion?: EventHandlerCompletionMode | null;
134
176
  event_schema?: z.ZodTypeAny;
177
+ _event_parse_schema?: z.ZodTypeAny;
135
178
  static event_type?: string;
136
179
  static event_version: string;
137
180
  static event_result_type?: z.ZodTypeAny;
138
181
  static event_schema: AnyEventSchema;
182
+ static model_fields: z.ZodRawShape;
183
+ static _event_parse_schema: AnyEventSchema;
139
184
  event_bus?: EventBus;
140
185
  _event_original?: BaseEvent;
141
186
  _event_dispatch_context?: unknown | null;
@@ -145,8 +190,8 @@ export declare class BaseEvent {
145
190
  constructor(data?: BaseEventInit<Record<string, unknown>>);
146
191
  toString(): string;
147
192
  static extend<TSchema extends z.ZodObject<z.ZodRawShape>>(event_type: string, event_schema: TSchema): SchemaEventFactory<TSchema, ResultSchemaFromEventSchema<TSchema>>;
193
+ static extend<const TShape extends Record<string, unknown>>(event_type: string, shape?: TShape): EventFactory<ShortcutZodModelFields<TShape>, ResultSchemaFromShape<ShortcutZodModelFields<TShape>>, ShortcutStaticDefaultValues<TShape, ShortcutZodModelFields<TShape>>>;
148
194
  static extend<TShape extends z.ZodRawShape>(event_type: string, shape?: TShape): EventFactory<TShape, ResultSchemaFromShape<TShape>>;
149
- static extend<TShape extends Record<string, unknown>>(event_type: string, shape?: TShape): EventFactory<ZodShapeFrom<TShape>, ResultSchemaFromShape<TShape>>;
150
195
  static fromJSON<T extends typeof BaseEvent>(this: T, data: unknown): InstanceType<T>;
151
196
  static toJSONArray(events: Iterable<BaseEvent>): BaseEventJSON[];
152
197
  static fromJSONArray(data: unknown): BaseEvent[];
@@ -27,7 +27,7 @@ export declare const BaseEventSchema: z.ZodObject<{
27
27
  }>>;
28
28
  event_started_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
29
29
  event_completed_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
30
- event_results: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
30
+ event_results: z.ZodOptional<z.ZodArray<z.ZodUnknown>>;
31
31
  event_concurrency: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
32
32
  "global-serial": "global-serial";
33
33
  "bus-serial": "bus-serial";
@@ -133,7 +133,7 @@ export declare class BaseEvent {
133
133
  }>>;
134
134
  event_started_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
135
135
  event_completed_at: z.ZodOptional<z.ZodNullable<z.ZodString>>;
136
- event_results: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
136
+ event_results: z.ZodOptional<z.ZodArray<z.ZodUnknown>>;
137
137
  event_concurrency: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
138
138
  "global-serial": "global-serial";
139
139
  "bus-serial": "bus-serial";
@@ -0,0 +1,45 @@
1
+ import { BaseEvent } from './base_event.js';
2
+ import { EventBus } from './event_bus.js';
3
+ import type { EventClass, EventHandlerCallable, UntypedEventHandlerFunction } from './types.js';
4
+ type EndpointScheme = 'unix' | 'http' | 'https';
5
+ type ParsedEndpoint = {
6
+ raw: string;
7
+ scheme: EndpointScheme;
8
+ host?: string;
9
+ port?: number;
10
+ path?: string;
11
+ };
12
+ export type HTTPEventBridgeOptions = {
13
+ send_to?: string | null;
14
+ listen_on?: string | null;
15
+ name?: string;
16
+ };
17
+ export declare class EventBridge {
18
+ readonly send_to: ParsedEndpoint | null;
19
+ readonly listen_on: ParsedEndpoint | null;
20
+ readonly name: string;
21
+ protected readonly inbound_bus: EventBus;
22
+ private start_promise;
23
+ private node_server;
24
+ constructor(send_to?: string | null, listen_on?: string | null, name?: string);
25
+ on<T extends BaseEvent>(event_pattern: EventClass<T>, handler: EventHandlerCallable<T>): void;
26
+ on<T extends BaseEvent>(event_pattern: string | '*', handler: UntypedEventHandlerFunction<T>): void;
27
+ emit<T extends BaseEvent>(event: T): Promise<void>;
28
+ dispatch<T extends BaseEvent>(event: T): Promise<void>;
29
+ start(): Promise<void>;
30
+ close(): Promise<void>;
31
+ private ensureListenerStarted;
32
+ private handleIncomingPayload;
33
+ private sendHttp;
34
+ private sendUnix;
35
+ private startHttpListener;
36
+ private startUnixListener;
37
+ }
38
+ export declare class HTTPEventBridge extends EventBridge {
39
+ constructor(send_to?: string | null, listen_on?: string | null, name?: string);
40
+ constructor(options?: HTTPEventBridgeOptions);
41
+ }
42
+ export declare class SocketEventBridge extends EventBridge {
43
+ constructor(path?: string | null, name?: string);
44
+ }
45
+ export {};
@@ -61,6 +61,7 @@ export declare class EventHandler {
61
61
  eventbus_id: string;
62
62
  });
63
63
  get _handler_async(): EventHandlerCallable;
64
+ static handlerNameFromCallable(handler: EventHandlerCallable): string;
64
65
  static computeHandlerId(params: {
65
66
  eventbus_id: string;
66
67
  handler_name: string;
@@ -22,7 +22,7 @@ export type GeneratedEvents<TEvents extends FunctionMap> = {
22
22
  } & {
23
23
  [K in keyof TEvents]: GeneratedEvent<TEvents[K]>;
24
24
  };
25
- type EventInit<TEventClass extends EventClass<BaseEvent>> = ConstructorParameters<TEventClass> extends [infer TInit, ...unknown[]] ? TInit : never;
25
+ type EventInit<TEventClass extends EventClass<BaseEvent>> = [ConstructorParameters<TEventClass>[0]] extends [undefined] ? {} : NonNullable<ConstructorParameters<TEventClass>[0]>;
26
26
  type EventMethodArgs<TEventClass extends EventClass<BaseEvent>> = {} extends EventInit<TEventClass> ? [init?: EventInit<TEventClass>, extra?: Record<string, unknown>] : [init: EventInit<TEventClass>, extra?: Record<string, unknown>];
27
27
  type EventMethodResult<TEventClass extends EventClass<BaseEvent>> = EventResultType<InstanceType<TEventClass>> | undefined;
28
28
  export type EventsSuckClient<TEvents extends EventMap> = {
@@ -0,0 +1,6 @@
1
+ import { z } from 'zod';
2
+ export type JsonSchema = boolean | z.core.JSONSchema.JSONSchema;
3
+ export declare const isJsonSchema: (value: unknown) => value is JsonSchema;
4
+ export declare const normalizeJsonSchema: (schema: JsonSchema) => JsonSchema;
5
+ export declare const toJsonSchema: (schema: z.core.$ZodType) => JsonSchema;
6
+ export declare const fromJsonSchema: (schema: JsonSchema) => z.ZodTypeAny;
@@ -0,0 +1,49 @@
1
+ import { trace, type Span, type SpanAttributes, type SpanContext, type TimeInput, type Tracer } from '@opentelemetry/api';
2
+ import type { BaseEvent } from './base_event.js';
3
+ import type { EventBus } from './event_bus.js';
4
+ import type { EventResult } from './event_result.js';
5
+ import type { EventBusMiddleware } from './middlewares.js';
6
+ import type { EventStatus } from './types.js';
7
+ type OpenTelemetryTraceApi = Pick<typeof trace, 'getTracer' | 'setSpan'> & Partial<Pick<typeof trace, 'setSpanContext'>>;
8
+ export type OtelTracingSpanFactoryInput = {
9
+ name: string;
10
+ span_context: SpanContext;
11
+ parent_span_context?: SpanContext;
12
+ attributes: SpanAttributes;
13
+ start_time?: TimeInput;
14
+ };
15
+ export type OtelTracingSpanFactory = (input: OtelTracingSpanFactoryInput) => Span;
16
+ export type OtelTracingSpanProvider = object;
17
+ export type OtelTracingMiddlewareOptions = {
18
+ tracer?: Tracer;
19
+ trace_api?: OpenTelemetryTraceApi;
20
+ span_provider?: OtelTracingSpanProvider;
21
+ span_factory?: OtelTracingSpanFactory;
22
+ otlp_endpoint?: string;
23
+ service_name?: string;
24
+ instrumentation_name?: string;
25
+ root_span_attributes?: SpanAttributes | ((eventbus: EventBus, event: BaseEvent) => SpanAttributes);
26
+ };
27
+ export declare class OtelTracingMiddleware implements EventBusMiddleware {
28
+ private readonly tracer;
29
+ private readonly trace_api;
30
+ private readonly span_factory?;
31
+ private readonly span_provider?;
32
+ private readonly root_span_attributes;
33
+ private readonly event_spans;
34
+ private readonly event_contexts;
35
+ private readonly handler_spans;
36
+ private readonly handler_contexts;
37
+ constructor(options?: OtelTracingMiddlewareOptions);
38
+ onEventChange(eventbus: EventBus, event: BaseEvent, status: EventStatus): void;
39
+ onEventResultChange(eventbus: EventBus, event: BaseEvent, event_result: EventResult, status: EventStatus): void;
40
+ private startEventSpan;
41
+ private completeEventSpan;
42
+ private startHandlerSpan;
43
+ private completeHandlerSpan;
44
+ private parentContextForEvent;
45
+ private completeEventSpanWithFactory;
46
+ private exportEventTreeWithFactory;
47
+ private exportHandlerSpanWithFactory;
48
+ }
49
+ export {};
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import type { BaseEvent } from './BaseEvent.js';
3
+ import { type JsonSchema } from './jsonschema.js';
3
4
  export type EventStatus = 'pending' | 'started' | 'completed';
4
5
  export type EventClass<T extends BaseEvent = BaseEvent> = {
5
6
  event_type?: string;
@@ -12,7 +13,7 @@ export type EventResultType<TEvent extends BaseEvent> = TEvent extends {
12
13
  __event_result_type__?: infer TResult;
13
14
  } ? TResult : unknown;
14
15
  export type EventResultTypeConstructor = StringConstructor | NumberConstructor | BooleanConstructor | ArrayConstructor | ObjectConstructor;
15
- export type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | unknown;
16
+ export type EventResultTypeInput = z.ZodTypeAny | EventResultTypeConstructor | JsonSchema;
16
17
  export type EventHandlerReturn<T extends BaseEvent = BaseEvent> = EventResultType<T> | BaseEvent | null | void;
17
18
  export type EventHandlerCallable<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | Promise<EventHandlerReturn<T>>;
18
19
  export type UntypedEventHandlerFunction<T extends BaseEvent = BaseEvent> = (event: T) => EventHandlerReturn<T> | unknown | Promise<EventHandlerReturn<T> | unknown>;
@@ -33,7 +34,5 @@ export declare const normalizeEventPattern: (event_pattern: EventPattern | "*")
33
34
  export declare const isZodSchema: (value: unknown) => value is z.ZodTypeAny;
34
35
  export declare const eventResultTypeFromConstructor: (value: unknown) => z.ZodTypeAny | undefined;
35
36
  export declare const extractZodShape: (raw: Record<string, unknown>) => z.ZodRawShape;
36
- export declare const toJsonSchema: (schema: unknown) => unknown;
37
- export declare const fromJsonSchema: (schema: unknown) => z.ZodTypeAny;
38
- export declare const normalizeEventResultType: (value: EventResultTypeInput) => z.ZodTypeAny | undefined;
37
+ export declare function normalizeEventResultType(value: unknown): z.ZodTypeAny | undefined;
39
38
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abxbus",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "description": "Event bus library for browsers and ESM Node.js",
5
5
  "type": "module",
6
6
  "sideEffects": false,