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.
- package/dist/cjs/BaseEvent.d.ts +68 -23
- package/dist/cjs/BaseEvent.js +131 -14
- package/dist/cjs/BaseEvent.js.map +2 -2
- package/dist/cjs/base_event.d.ts +2 -2
- package/dist/cjs/bridge_ipc.d.ts +45 -0
- package/dist/cjs/event_handler.d.ts +1 -0
- package/dist/cjs/events_suck.d.ts +1 -1
- package/dist/cjs/events_suck.js.map +2 -2
- package/dist/cjs/jsonschema.d.ts +6 -0
- package/dist/cjs/jsonschema.js +155 -0
- package/dist/cjs/jsonschema.js.map +7 -0
- package/dist/cjs/middleware_otel_tracing.d.ts +49 -0
- package/dist/cjs/types.d.ts +3 -4
- package/dist/cjs/types.js +8 -15
- package/dist/cjs/types.js.map +2 -2
- package/dist/esm/BaseEvent.js +131 -14
- package/dist/esm/BaseEvent.js.map +2 -2
- package/dist/esm/events_suck.js.map +2 -2
- package/dist/esm/jsonschema.js +135 -0
- package/dist/esm/jsonschema.js.map +7 -0
- package/dist/esm/types.js +7 -14
- package/dist/esm/types.js.map +2 -2
- package/dist/types/BaseEvent.d.ts +68 -23
- package/dist/types/base_event.d.ts +2 -2
- package/dist/types/bridge_ipc.d.ts +45 -0
- package/dist/types/event_handler.d.ts +1 -0
- package/dist/types/events_suck.d.ts +1 -1
- package/dist/types/jsonschema.d.ts +6 -0
- package/dist/types/middleware_otel_tracing.d.ts +49 -0
- package/dist/types/types.d.ts +3 -4
- package/package.json +1 -1
- package/src/BaseEvent.ts +277 -54
- package/src/events_suck.ts +3 -2
- package/src/jsonschema.ts +146 -0
- package/src/types.ts +6 -14
- package/dist/cjs/CoreClient.d.ts +0 -167
- package/dist/cjs/CoreEventBus.d.ts +0 -334
- package/dist/types/CoreClient.d.ts +0 -167
- package/dist/types/CoreEventBus.d.ts +0 -334
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var jsonschema_exports = {};
|
|
20
|
+
__export(jsonschema_exports, {
|
|
21
|
+
fromJsonSchema: () => fromJsonSchema,
|
|
22
|
+
isJsonSchema: () => isJsonSchema,
|
|
23
|
+
normalizeJsonSchema: () => normalizeJsonSchema,
|
|
24
|
+
toJsonSchema: () => toJsonSchema
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(jsonschema_exports);
|
|
27
|
+
var import_zod = require("zod");
|
|
28
|
+
const isJsonSchemaObject = (value) => {
|
|
29
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
30
|
+
};
|
|
31
|
+
const isJsonSchema = (value) => {
|
|
32
|
+
if (typeof value === "boolean") {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (!isJsonSchemaObject(value)) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
if ("_zod" in value || "_def" in value || "~standard" in value) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
42
|
+
};
|
|
43
|
+
const nullUnionCandidates = (schema) => {
|
|
44
|
+
if (Array.isArray(schema.type) && schema.type.includes("null")) {
|
|
45
|
+
const non_null_types = schema.type.filter((item) => typeof item === "string" && item !== "null");
|
|
46
|
+
if (non_null_types.length > 0) {
|
|
47
|
+
return [{ type: non_null_types.length === 1 ? non_null_types[0] : non_null_types }, { type: "null" }];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return null;
|
|
51
|
+
};
|
|
52
|
+
const normalizeJsonSchema = (schema) => {
|
|
53
|
+
const normalized = normalizeJsonSchemaValue(schema);
|
|
54
|
+
if (!isJsonSchemaObject(normalized)) {
|
|
55
|
+
return normalized;
|
|
56
|
+
}
|
|
57
|
+
const schema_record = { ...normalized };
|
|
58
|
+
const definitions = schema_record.$defs;
|
|
59
|
+
const root_ref = rootRefForSchema(schema_record, definitions);
|
|
60
|
+
if (!root_ref || !definitions) {
|
|
61
|
+
schema_record.$schema ??= "https://json-schema.org/draft/2020-12/schema";
|
|
62
|
+
return schema_record;
|
|
63
|
+
}
|
|
64
|
+
const root_name = root_ref.slice("#/$defs/".length);
|
|
65
|
+
const root_schema = definitions[root_name];
|
|
66
|
+
if (!isJsonSchemaObject(root_schema)) {
|
|
67
|
+
schema_record.$schema ??= "https://json-schema.org/draft/2020-12/schema";
|
|
68
|
+
return schema_record;
|
|
69
|
+
}
|
|
70
|
+
const rewritten_root = rewriteJsonSchemaRefs(root_schema, { [root_ref]: "#" });
|
|
71
|
+
const remaining_defs = Object.fromEntries(Object.entries(definitions).filter(([name]) => name !== root_name));
|
|
72
|
+
if (Object.keys(remaining_defs).length > 0) {
|
|
73
|
+
rewritten_root.$defs = rewriteJsonSchemaRefs(remaining_defs, { [root_ref]: "#" });
|
|
74
|
+
}
|
|
75
|
+
rewritten_root.$schema ??= schema_record.$schema ?? "https://json-schema.org/draft/2020-12/schema";
|
|
76
|
+
setTitleFromInlinedRootDefinition(rewritten_root, root_name);
|
|
77
|
+
return rewritten_root;
|
|
78
|
+
};
|
|
79
|
+
const rootRefForSchema = (schema, definitions) => {
|
|
80
|
+
if (typeof schema.$ref === "string" && schema.$ref.startsWith("#/$defs/")) {
|
|
81
|
+
return schema.$ref;
|
|
82
|
+
}
|
|
83
|
+
if (!definitions) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const root = schemaWithoutSchemaAndDefinitions(schema);
|
|
87
|
+
for (const [name, definition] of Object.entries(definitions)) {
|
|
88
|
+
if (JSON.stringify(definition) === JSON.stringify(root)) {
|
|
89
|
+
return `#/$defs/${name}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
};
|
|
94
|
+
const schemaWithoutSchemaAndDefinitions = (schema) => {
|
|
95
|
+
const root = {};
|
|
96
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
97
|
+
if (key !== "$schema" && key !== "$defs") {
|
|
98
|
+
root[key] = value;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return root;
|
|
102
|
+
};
|
|
103
|
+
const setTitleFromInlinedRootDefinition = (schema, root_name) => {
|
|
104
|
+
if (root_name.startsWith("__schema")) return;
|
|
105
|
+
schema.title ??= root_name;
|
|
106
|
+
};
|
|
107
|
+
const rewriteJsonSchemaRefs = (schema, refs) => {
|
|
108
|
+
if (Array.isArray(schema)) {
|
|
109
|
+
return schema.map((item) => rewriteJsonSchemaRefs(item, refs));
|
|
110
|
+
}
|
|
111
|
+
if (!isJsonSchemaObject(schema)) {
|
|
112
|
+
return schema;
|
|
113
|
+
}
|
|
114
|
+
const rewritten = {};
|
|
115
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
116
|
+
rewritten[key] = rewriteJsonSchemaRefs(value, refs);
|
|
117
|
+
}
|
|
118
|
+
if (typeof rewritten.$ref === "string" && rewritten.$ref in refs) {
|
|
119
|
+
rewritten.$ref = refs[rewritten.$ref];
|
|
120
|
+
}
|
|
121
|
+
return rewritten;
|
|
122
|
+
};
|
|
123
|
+
const normalizeJsonSchemaValue = (schema) => {
|
|
124
|
+
if (Array.isArray(schema)) {
|
|
125
|
+
return schema.map((item) => normalizeJsonSchemaValue(item));
|
|
126
|
+
}
|
|
127
|
+
if (!isJsonSchemaObject(schema)) {
|
|
128
|
+
return schema;
|
|
129
|
+
}
|
|
130
|
+
const normalized = {};
|
|
131
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
132
|
+
normalized[key] = normalizeJsonSchemaValue(value);
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(normalized.required) && normalized.required.every((item) => typeof item === "string")) {
|
|
135
|
+
normalized.required = [...normalized.required].sort();
|
|
136
|
+
}
|
|
137
|
+
const null_union_candidates = nullUnionCandidates(normalized);
|
|
138
|
+
if (null_union_candidates !== null) {
|
|
139
|
+
const merged = { anyOf: normalizeJsonSchemaValue(null_union_candidates) };
|
|
140
|
+
for (const [key, value] of Object.entries(normalized)) {
|
|
141
|
+
if (key !== "type") {
|
|
142
|
+
merged[key] = value;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return merged;
|
|
146
|
+
}
|
|
147
|
+
return normalized;
|
|
148
|
+
};
|
|
149
|
+
const toJsonSchema = (schema) => {
|
|
150
|
+
return normalizeJsonSchema(import_zod.z.toJSONSchema(schema));
|
|
151
|
+
};
|
|
152
|
+
const fromJsonSchema = (schema) => {
|
|
153
|
+
return import_zod.z.fromJSONSchema(schema);
|
|
154
|
+
};
|
|
155
|
+
//# 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": "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",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -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 {};
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -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 |
|
|
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
|
|
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/dist/cjs/types.js
CHANGED
|
@@ -20,14 +20,13 @@ var types_exports = {};
|
|
|
20
20
|
__export(types_exports, {
|
|
21
21
|
eventResultTypeFromConstructor: () => eventResultTypeFromConstructor,
|
|
22
22
|
extractZodShape: () => extractZodShape,
|
|
23
|
-
fromJsonSchema: () => fromJsonSchema,
|
|
24
23
|
isZodSchema: () => isZodSchema,
|
|
25
24
|
normalizeEventPattern: () => normalizeEventPattern,
|
|
26
|
-
normalizeEventResultType: () => normalizeEventResultType
|
|
27
|
-
toJsonSchema: () => toJsonSchema
|
|
25
|
+
normalizeEventResultType: () => normalizeEventResultType
|
|
28
26
|
});
|
|
29
27
|
module.exports = __toCommonJS(types_exports);
|
|
30
28
|
var import_zod = require("zod");
|
|
29
|
+
var import_jsonschema = require("./jsonschema.js");
|
|
31
30
|
const normalizeEventPattern = (event_pattern) => {
|
|
32
31
|
if (event_pattern === "*") {
|
|
33
32
|
return "*";
|
|
@@ -79,16 +78,7 @@ const extractZodShape = (raw) => {
|
|
|
79
78
|
}
|
|
80
79
|
return shape;
|
|
81
80
|
};
|
|
82
|
-
|
|
83
|
-
if (!schema || !isZodSchema(schema)) return schema;
|
|
84
|
-
const zod_any = import_zod.z;
|
|
85
|
-
return zod_any.toJSONSchema(schema);
|
|
86
|
-
};
|
|
87
|
-
const fromJsonSchema = (schema) => {
|
|
88
|
-
const zod_any = import_zod.z;
|
|
89
|
-
return zod_any.fromJSONSchema(schema);
|
|
90
|
-
};
|
|
91
|
-
const normalizeEventResultType = (value) => {
|
|
81
|
+
function normalizeEventResultType(value) {
|
|
92
82
|
if (value === void 0 || value === null) {
|
|
93
83
|
return void 0;
|
|
94
84
|
}
|
|
@@ -99,6 +89,9 @@ const normalizeEventResultType = (value) => {
|
|
|
99
89
|
if (constructor_schema) {
|
|
100
90
|
return constructor_schema;
|
|
101
91
|
}
|
|
102
|
-
|
|
103
|
-
};
|
|
92
|
+
if (!(0, import_jsonschema.isJsonSchema)(value)) {
|
|
93
|
+
throw new Error(`event_result_type must be a Zod schema, constructor shorthand, or JSON Schema value, got: ${typeof value}`);
|
|
94
|
+
}
|
|
95
|
+
return (0, import_jsonschema.fromJsonSchema)(value);
|
|
96
|
+
}
|
|
104
97
|
//# sourceMappingURL=types.js.map
|
package/dist/cjs/types.js.map
CHANGED
|
@@ -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 |
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAkB;AAElB,wBAA8D;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,aAAE,OAAO;AAAA,EAClB;AACA,MAAI,UAAU,QAAQ;AACpB,WAAO,aAAE,OAAO;AAAA,EAClB;AACA,MAAI,UAAU,SAAS;AACrB,WAAO,aAAE,QAAQ;AAAA,EACnB;AACA,MAAI,UAAU,OAAO;AACnB,WAAO,aAAE,MAAM,aAAE,QAAQ,CAAC;AAAA,EAC5B;AACA,MAAI,UAAU,QAAQ;AACpB,WAAO,aAAE,OAAO,aAAE,OAAO,GAAG,aAAE,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,KAAC,gCAAa,KAAK,GAAG;AACxB,UAAM,IAAI,MAAM,6FAA6F,OAAO,KAAK,EAAE;AAAA,EAC7H;AACA,aAAO,kCAAe,KAAK;AAC7B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/BaseEvent.js
CHANGED
|
@@ -10,7 +10,8 @@ import {
|
|
|
10
10
|
withResolvers
|
|
11
11
|
} from "./LockManager.js";
|
|
12
12
|
import { _runWithTimeout } from "./timing.js";
|
|
13
|
-
import {
|
|
13
|
+
import { toJsonSchema } from "./jsonschema.js";
|
|
14
|
+
import { isZodSchema, normalizeEventResultType } from "./types.js";
|
|
14
15
|
import { monotonicDatetime } from "./helpers.js";
|
|
15
16
|
const RESERVED_USER_EVENT_FIELDS = /* @__PURE__ */ new Set([
|
|
16
17
|
"bus",
|
|
@@ -83,6 +84,16 @@ const BaseEventSchema = z.object({
|
|
|
83
84
|
event_handler_completion: z.enum(EVENT_HANDLER_COMPLETION_MODES).nullable().optional()
|
|
84
85
|
}).loose();
|
|
85
86
|
const KNOWN_BASE_EVENT_FIELDS = new Set(Object.keys(BaseEventSchema.shape));
|
|
87
|
+
const EVENT_FACTORY_METADATA_FIELDS = /* @__PURE__ */ new Set([
|
|
88
|
+
"class",
|
|
89
|
+
"fromJSON",
|
|
90
|
+
"prototype",
|
|
91
|
+
"event_schema",
|
|
92
|
+
"model_fields",
|
|
93
|
+
"event_type",
|
|
94
|
+
"event_version",
|
|
95
|
+
"event_result_type"
|
|
96
|
+
]);
|
|
86
97
|
const ROOT_EVENTBUS_ID = "00000000-0000-0000-0000-000000000000";
|
|
87
98
|
function baseEventDefaultShape(event_type) {
|
|
88
99
|
return {
|
|
@@ -112,11 +123,67 @@ function baseEventDefaultShape(event_type) {
|
|
|
112
123
|
function missingBaseFields(event_type, user_shape) {
|
|
113
124
|
return Object.fromEntries(Object.entries(baseEventDefaultShape(event_type)).filter(([key]) => !(key in user_shape)));
|
|
114
125
|
}
|
|
126
|
+
function isZodLiteralValue(value) {
|
|
127
|
+
return value === null || value === void 0 || ["string", "number", "bigint", "boolean"].includes(typeof value);
|
|
128
|
+
}
|
|
129
|
+
function isPlainShortcutLiteralObject(value) {
|
|
130
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
const prototype = Object.getPrototypeOf(value);
|
|
134
|
+
return prototype === Object.prototype || prototype === null;
|
|
135
|
+
}
|
|
136
|
+
function alreadyComparedShortcutLiteralPair(left, right, seen) {
|
|
137
|
+
let right_values = seen.get(left);
|
|
138
|
+
if (right_values?.has(right)) {
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (!right_values) {
|
|
142
|
+
right_values = /* @__PURE__ */ new WeakSet();
|
|
143
|
+
seen.set(left, right_values);
|
|
144
|
+
}
|
|
145
|
+
right_values.add(right);
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
function shortcutLiteralValuesEqual(left, right, seen = /* @__PURE__ */ new WeakMap()) {
|
|
149
|
+
if (Object.is(left, right)) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
if (typeof left !== "object" || left === null || typeof right !== "object" || right === null) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (alreadyComparedShortcutLiteralPair(left, right, seen)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
159
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
return left.every((item, index) => shortcutLiteralValuesEqual(item, right[index], seen));
|
|
163
|
+
}
|
|
164
|
+
if (!isPlainShortcutLiteralObject(left) || !isPlainShortcutLiteralObject(right)) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
const left_keys = Object.keys(left);
|
|
168
|
+
const right_keys = Object.keys(right);
|
|
169
|
+
if (left_keys.length !== right_keys.length) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
return left_keys.every(
|
|
173
|
+
(key) => Object.prototype.hasOwnProperty.call(right, key) ? shortcutLiteralValuesEqual(left[key], right[key], seen) : false
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
function shortcutLiteralSchema(value) {
|
|
177
|
+
if (isZodLiteralValue(value)) {
|
|
178
|
+
return z.literal(value);
|
|
179
|
+
}
|
|
180
|
+
return z.custom((candidate) => shortcutLiteralValuesEqual(candidate, value), "Invalid literal value");
|
|
181
|
+
}
|
|
115
182
|
function shortcutDefaultSchema(base_field_schema, value) {
|
|
116
183
|
if (!base_field_schema) {
|
|
117
|
-
return
|
|
184
|
+
return shortcutLiteralSchema(value).default(value);
|
|
118
185
|
}
|
|
119
|
-
return base_field_schema.
|
|
186
|
+
return base_field_schema.default(base_field_schema.parse(value));
|
|
120
187
|
}
|
|
121
188
|
function schemaDefaultsForShortcut(event_type, raw_shape) {
|
|
122
189
|
const defaults = {};
|
|
@@ -139,10 +206,42 @@ function zodFieldsForShortcut(raw_shape) {
|
|
|
139
206
|
}
|
|
140
207
|
return fields;
|
|
141
208
|
}
|
|
209
|
+
function modelFieldsForShortcut(raw_shape, shortcut_shape) {
|
|
210
|
+
const event_result_type = normalizeEventResultType(raw_shape.event_result_type);
|
|
211
|
+
return event_result_type ? { ...shortcut_shape, event_result_type } : shortcut_shape;
|
|
212
|
+
}
|
|
213
|
+
function staticEventDefaultsFromModelFields(model_fields) {
|
|
214
|
+
const fields = {};
|
|
215
|
+
for (const [key, value] of Object.entries(model_fields)) {
|
|
216
|
+
if (EVENT_FACTORY_METADATA_FIELDS.has(key)) {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const parsed = value.safeParse(void 0);
|
|
220
|
+
if (parsed.success && parsed.data !== void 0) {
|
|
221
|
+
fields[key] = parsed.data;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return fields;
|
|
225
|
+
}
|
|
226
|
+
function defineStaticEventFields(target, fields) {
|
|
227
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
228
|
+
Object.defineProperty(target, key, {
|
|
229
|
+
value,
|
|
230
|
+
writable: false,
|
|
231
|
+
enumerable: true,
|
|
232
|
+
configurable: true
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
142
236
|
function eventResultTypeFromObjectSchema(schema) {
|
|
143
237
|
const raw_event_result_type = schema.shape.event_result_type;
|
|
144
238
|
return raw_event_result_type === void 0 ? void 0 : normalizeEventResultType(raw_event_result_type);
|
|
145
239
|
}
|
|
240
|
+
function eventParseSchemaFromEventSchema(schema) {
|
|
241
|
+
return schema.safeExtend({
|
|
242
|
+
event_result_type: z.unknown().optional()
|
|
243
|
+
});
|
|
244
|
+
}
|
|
146
245
|
function buildFullEventSchema(event_type, spec) {
|
|
147
246
|
if (isZodObjectSchema(spec)) {
|
|
148
247
|
const user_shape = spec.shape;
|
|
@@ -150,11 +249,12 @@ function buildFullEventSchema(event_type, spec) {
|
|
|
150
249
|
assertNoUnknownEventPrefixedFields(user_shape, `BaseEvent.extend(${event_type})`);
|
|
151
250
|
assertNoModelPrefixedFields(user_shape, `BaseEvent.extend(${event_type})`);
|
|
152
251
|
const full_schema2 = spec.safeExtend({
|
|
153
|
-
event_result_type: z.unknown().optional(),
|
|
154
252
|
...missingBaseFields(event_type, user_shape)
|
|
155
253
|
});
|
|
156
254
|
return {
|
|
157
255
|
event_schema: full_schema2,
|
|
256
|
+
event_parse_schema: eventParseSchemaFromEventSchema(full_schema2),
|
|
257
|
+
static_field_defaults: staticEventDefaultsFromModelFields(full_schema2.shape),
|
|
158
258
|
event_result_type: eventResultTypeFromObjectSchema(spec)
|
|
159
259
|
};
|
|
160
260
|
}
|
|
@@ -166,9 +266,12 @@ function buildFullEventSchema(event_type, spec) {
|
|
|
166
266
|
...schemaDefaultsForShortcut(event_type, raw_shape),
|
|
167
267
|
...zodFieldsForShortcut(raw_shape)
|
|
168
268
|
};
|
|
169
|
-
const
|
|
269
|
+
const model_fields = modelFieldsForShortcut(raw_shape, shortcut_shape);
|
|
270
|
+
const full_schema = z.object(model_fields).safeExtend(missingBaseFields(event_type, model_fields)).loose();
|
|
170
271
|
return {
|
|
171
272
|
event_schema: full_schema,
|
|
273
|
+
event_parse_schema: eventParseSchemaFromEventSchema(full_schema),
|
|
274
|
+
static_field_defaults: staticEventDefaultsFromModelFields(full_schema.shape),
|
|
172
275
|
event_result_type: normalizeEventResultType(raw_shape.event_result_type),
|
|
173
276
|
event_version: typeof raw_shape.event_version === "string" ? raw_shape.event_version : void 0
|
|
174
277
|
};
|
|
@@ -229,12 +332,15 @@ class BaseEvent {
|
|
|
229
332
|
event_handler_completion;
|
|
230
333
|
// completion strategy: 'all' (default) waits for every handler, 'first' returns earliest non-undefined result and cancels the rest
|
|
231
334
|
event_schema;
|
|
335
|
+
_event_parse_schema;
|
|
232
336
|
static event_type;
|
|
233
337
|
// class name of the event, e.g. BaseEvent.extend("MyEvent").event_type === "MyEvent"
|
|
234
338
|
static event_version = "0.0.1";
|
|
235
339
|
static event_result_type;
|
|
236
340
|
static event_schema = BaseEventSchema;
|
|
237
341
|
// generated Zod schema for local TS event data validation; never sent over the wire
|
|
342
|
+
static model_fields = BaseEventSchema.shape;
|
|
343
|
+
static _event_parse_schema = BaseEventSchema;
|
|
238
344
|
// internal runtime state
|
|
239
345
|
event_bus;
|
|
240
346
|
// bus that dispatched this event, also used by event.emit(child)
|
|
@@ -257,6 +363,7 @@ class BaseEvent {
|
|
|
257
363
|
const raw_event_result_type = merged_data.event_result_type ?? ctor.event_result_type;
|
|
258
364
|
const event_result_type = normalizeEventResultType(raw_event_result_type);
|
|
259
365
|
const event_schema = ctor.event_schema ?? BaseEventSchema;
|
|
366
|
+
const event_parse_schema = ctor._event_parse_schema ?? event_schema;
|
|
260
367
|
const base_data = {
|
|
261
368
|
...merged_data,
|
|
262
369
|
event_id: merged_data.event_id ?? uuidv7(),
|
|
@@ -265,11 +372,11 @@ class BaseEvent {
|
|
|
265
372
|
event_version,
|
|
266
373
|
event_result_type
|
|
267
374
|
};
|
|
268
|
-
if (
|
|
375
|
+
if (event_parse_schema === BaseEventSchema) {
|
|
269
376
|
base_data.event_timeout ??= null;
|
|
270
377
|
base_data.event_blocks_parent_completion ??= false;
|
|
271
378
|
}
|
|
272
|
-
const parsed = decodeEventSchema(
|
|
379
|
+
const parsed = decodeEventSchema(event_parse_schema, base_data);
|
|
273
380
|
Object.assign(this, parsed);
|
|
274
381
|
Object.defineProperty(this, "event_schema", {
|
|
275
382
|
value: event_schema,
|
|
@@ -277,6 +384,12 @@ class BaseEvent {
|
|
|
277
384
|
enumerable: false,
|
|
278
385
|
configurable: true
|
|
279
386
|
});
|
|
387
|
+
Object.defineProperty(this, "_event_parse_schema", {
|
|
388
|
+
value: event_parse_schema,
|
|
389
|
+
writable: true,
|
|
390
|
+
enumerable: false,
|
|
391
|
+
configurable: true
|
|
392
|
+
});
|
|
280
393
|
Object.defineProperty(this, "_event_fields_set", {
|
|
281
394
|
value: explicit_event_fields,
|
|
282
395
|
writable: true,
|
|
@@ -306,10 +419,14 @@ class BaseEvent {
|
|
|
306
419
|
static extend(event_type, shape) {
|
|
307
420
|
const built = buildFullEventSchema(event_type, shape ?? {});
|
|
308
421
|
const full_schema = built.event_schema;
|
|
422
|
+
const event_parse_schema = built.event_parse_schema;
|
|
423
|
+
const static_field_defaults = built.static_field_defaults;
|
|
309
424
|
const event_result_type = built.event_result_type;
|
|
310
425
|
const event_version = built.event_version;
|
|
311
426
|
class ExtendedEvent extends BaseEvent {
|
|
312
427
|
static event_schema = full_schema;
|
|
428
|
+
static model_fields = full_schema.shape;
|
|
429
|
+
static _event_parse_schema = event_parse_schema;
|
|
313
430
|
static event_type = event_type;
|
|
314
431
|
static event_version = event_version ?? BaseEvent.event_version;
|
|
315
432
|
static event_result_type = event_result_type;
|
|
@@ -321,19 +438,22 @@ class BaseEvent {
|
|
|
321
438
|
return new ExtendedEvent(data);
|
|
322
439
|
}
|
|
323
440
|
EventFactory.event_schema = full_schema;
|
|
441
|
+
EventFactory.model_fields = EventFactory.event_schema.shape;
|
|
324
442
|
EventFactory.event_type = event_type;
|
|
325
443
|
EventFactory.event_version = event_version ?? BaseEvent.event_version;
|
|
326
444
|
EventFactory.event_result_type = event_result_type;
|
|
327
445
|
EventFactory.class = ExtendedEvent;
|
|
328
446
|
EventFactory.fromJSON = (data) => ExtendedEvent.fromJSON(data);
|
|
329
447
|
EventFactory.prototype = ExtendedEvent.prototype;
|
|
448
|
+
defineStaticEventFields(ExtendedEvent, static_field_defaults);
|
|
449
|
+
defineStaticEventFields(EventFactory, static_field_defaults);
|
|
330
450
|
EVENT_TYPE_REGISTRY.set(event_type, ExtendedEvent);
|
|
331
451
|
return EventFactory;
|
|
332
452
|
}
|
|
333
453
|
static fromJSON(data) {
|
|
334
454
|
if (!data || typeof data !== "object") {
|
|
335
|
-
const
|
|
336
|
-
const parsed = decodeEventSchema(
|
|
455
|
+
const event_parse_schema = this._event_parse_schema ?? this.event_schema ?? BaseEventSchema;
|
|
456
|
+
const parsed = decodeEventSchema(event_parse_schema, data);
|
|
337
457
|
return new this(parsed);
|
|
338
458
|
}
|
|
339
459
|
const record = { ...data };
|
|
@@ -350,9 +470,6 @@ class BaseEvent {
|
|
|
350
470
|
if (this !== BaseEvent && ctor.event_result_type && record.event_result_type !== void 0) {
|
|
351
471
|
delete record.event_result_type;
|
|
352
472
|
}
|
|
353
|
-
if (record.event_result_type !== void 0 && record.event_result_type !== null) {
|
|
354
|
-
record.event_result_type = normalizeEventResultType(record.event_result_type);
|
|
355
|
-
}
|
|
356
473
|
return new this(record);
|
|
357
474
|
}
|
|
358
475
|
static toJSONArray(events) {
|
|
@@ -377,8 +494,8 @@ class BaseEvent {
|
|
|
377
494
|
const event_results = Object.fromEntries(
|
|
378
495
|
Array.from(this.event_results.entries()).map(([handler_id, result]) => [handler_id, result.toJSON()])
|
|
379
496
|
);
|
|
380
|
-
const
|
|
381
|
-
const encoded = encodeEventSchema(
|
|
497
|
+
const event_parse_schema = this.constructor._event_parse_schema ?? this._event_parse_schema ?? this.constructor.event_schema ?? this.event_schema ?? BaseEventSchema;
|
|
498
|
+
const encoded = encodeEventSchema(event_parse_schema, {
|
|
382
499
|
...record,
|
|
383
500
|
event_id: this.event_id,
|
|
384
501
|
event_type: this.event_type,
|