@isoloop/bus 0.1.0-next.0 → 0.1.0-next.10
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/bus-worker.js +512 -35
- package/dist/index.d.ts +107 -12
- package/dist/index.js +301 -37
- package/dist/{standard-schema-DnJtEmJw.js → standard-schema-BtwRaC9-.js} +2 -0
- package/dist/testing.d.ts +12 -4
- package/dist/testing.js +8 -4
- package/dist/topic-BpgHePIE.d.ts +203 -0
- package/package.json +3 -3
- package/dist/topic-BQbes-gX.d.ts +0 -127
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { BusRecord, ErrorCode, Issue as Issue$1, Retention, Retention as Retention$1, ToolSpec, ToolSpec as ToolSpec$1, TopicConfig } from "@isoloop/protocol";
|
|
2
|
+
/**
|
|
3
|
+
* The Standard Schema V1 interface (https://standardschema.dev), copied from
|
|
4
|
+
* the spec so the bus depends on no schema library. Zod 4, Valibot and ArkType
|
|
5
|
+
* implement it, so a schema from any of them works as a topic's schema.
|
|
6
|
+
*/
|
|
7
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
8
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
9
|
+
}
|
|
10
|
+
declare namespace StandardSchemaV1 {
|
|
11
|
+
export interface Props<Input = unknown, Output = Input> {
|
|
12
|
+
readonly version: 1;
|
|
13
|
+
readonly vendor: string;
|
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
+
readonly types?: Types<Input, Output> | undefined;
|
|
16
|
+
}
|
|
17
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
18
|
+
export interface SuccessResult<Output> {
|
|
19
|
+
readonly value: Output;
|
|
20
|
+
readonly issues?: undefined;
|
|
21
|
+
}
|
|
22
|
+
export interface FailureResult {
|
|
23
|
+
readonly issues: ReadonlyArray<Issue>;
|
|
24
|
+
}
|
|
25
|
+
export interface Issue {
|
|
26
|
+
readonly message: string;
|
|
27
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
28
|
+
}
|
|
29
|
+
export interface PathSegment {
|
|
30
|
+
readonly key: PropertyKey;
|
|
31
|
+
}
|
|
32
|
+
export interface Types<Input = unknown, Output = Input> {
|
|
33
|
+
readonly input: Input;
|
|
34
|
+
readonly output: Output;
|
|
35
|
+
}
|
|
36
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
37
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
|
|
41
|
+
* (ADR 0003, decision 10). Symbol keys become their description.
|
|
42
|
+
*
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
declare function toIssues(issues: ReadonlyArray<StandardSchemaV1.Issue>): Issue$1[];
|
|
46
|
+
/** The default `maxRecordBytes`: 256 KiB. */
|
|
47
|
+
declare const DEFAULT_MAX_RECORD_BYTES: number;
|
|
48
|
+
/**
|
|
49
|
+
* A topic: its name, its schemas and what its log keeps. Publishers are typed
|
|
50
|
+
* with `In` and subscribers with `Out`, because the log stores what the schema
|
|
51
|
+
* returns, not what was published. `RIn` and `ROut` type the replies to
|
|
52
|
+
* `bus.request`; a topic without `reply` can't be requested.
|
|
53
|
+
*/
|
|
54
|
+
type TopicDef<In, Out = In, RIn = never, ROut = RIn> = {
|
|
55
|
+
/** The topic's name. By convention it ends in the major version, for example `cart.items.v1`. */
|
|
56
|
+
readonly name: string;
|
|
57
|
+
/** The Standard Schema that checks each published value. Required: `unchecked<T>()` is the explicit opt-out. */
|
|
58
|
+
readonly schema: StandardSchemaV1<In, Out>;
|
|
59
|
+
/** The Standard Schema for replies. Required for `bus.request`. @experimental */
|
|
60
|
+
readonly reply?: StandardSchemaV1<RIn, ROut>;
|
|
61
|
+
/** What the log keeps: the newest `n` records, the records younger than `ms`, or the latest record per key. Required. */
|
|
62
|
+
readonly retention: Retention$1;
|
|
63
|
+
/** The largest record, by its rough structured size. A bigger one is rejected with `too_large`. Default 256 KiB. */
|
|
64
|
+
readonly maxRecordBytes?: number;
|
|
65
|
+
/**
|
|
66
|
+
* Default `memory`. `persistent` keeps the topic in OPFS, so it survives a
|
|
67
|
+
* bus crash and a reload. Where OPFS isn't available, or another tab holds
|
|
68
|
+
* it, it behaves as `memory` and the kernel emits `bus:durability_degraded`.
|
|
69
|
+
*/
|
|
70
|
+
readonly durability?: "memory" | "persistent";
|
|
71
|
+
/**
|
|
72
|
+
* Starts this topic from an older one. On the first boot where this topic
|
|
73
|
+
* is empty and `from` has records, the bus copies them through `map` and
|
|
74
|
+
* this topic's schema before it opens: its publishes wait and its
|
|
75
|
+
* subscribers get nothing until then. `map` must be pure and synchronous.
|
|
76
|
+
* `follow` keeps mapping new `from` records afterwards.
|
|
77
|
+
*/
|
|
78
|
+
readonly seed?: {
|
|
79
|
+
/** The older topic to copy from. */
|
|
80
|
+
readonly from: AnyTopicDef;
|
|
81
|
+
/** Turns an older record's value into this topic's input. A record whose `map` throws or returns a promise is skipped, and the kernel emits `bus:seed_rejected`. */
|
|
82
|
+
readonly map: (value: any, record: BusRecord) => In;
|
|
83
|
+
/** Keeps mapping new `from` records after the first copy. Default `false`. */
|
|
84
|
+
readonly follow?: boolean;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Describes this topic as a tool for agents. Only a topic with `reply` can
|
|
88
|
+
* have one: without it this field's type is `never`. It exposes nothing by
|
|
89
|
+
* itself; the app's `agents.expose` does. It stays on the host: the bus
|
|
90
|
+
* worker never sees it.
|
|
91
|
+
* @experimental
|
|
92
|
+
*/
|
|
93
|
+
readonly tool?: [RIn] extends [never] ? never : ToolSpec$1;
|
|
94
|
+
};
|
|
95
|
+
/** A topic of any types, as `contracts()` takes them. */
|
|
96
|
+
type AnyTopicDef = TopicDef<any, any, any, any>;
|
|
97
|
+
/** What a publisher passes. */
|
|
98
|
+
type InputOf<D> = D extends TopicDef<infer In, any, any, any> ? In : never;
|
|
99
|
+
/** What a subscriber receives. */
|
|
100
|
+
type OutputOf<D> = D extends TopicDef<any, infer Out, any, any> ? Out : never;
|
|
101
|
+
/** What a responder passes to `reply`. */
|
|
102
|
+
type ReplyInputOf<D> = D extends TopicDef<any, any, infer RIn, any> ? RIn : never;
|
|
103
|
+
/** What a requester receives. */
|
|
104
|
+
type ReplyOutputOf<D> = D extends TopicDef<any, any, any, infer ROut> ? ROut : never;
|
|
105
|
+
/**
|
|
106
|
+
* Defines a topic. Its types come from its schemas.
|
|
107
|
+
*
|
|
108
|
+
* @param name - The topic's name, for example `cart.items.v1`.
|
|
109
|
+
* @param def - The topic's schemas, retention and options.
|
|
110
|
+
* @returns The topic, frozen. List it in `contracts()` to use it.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { topic } from '@isoloop/bus';
|
|
115
|
+
* import { z } from 'zod';
|
|
116
|
+
*
|
|
117
|
+
* export const cartItems = topic('cart.items.v1', {
|
|
118
|
+
* schema: z.object({ sku: z.string(), qty: z.number().int().positive() }),
|
|
119
|
+
* retention: { kind: 'compacted' },
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare function topic<In, Out = In, RIn = never, ROut = RIn>(name: string, def: Omit<TopicDef<In, Out, RIn, ROut>, "name">): TopicDef<In, Out, RIn, ROut>;
|
|
124
|
+
/**
|
|
125
|
+
* A Standard Schema that accepts any value unchanged and only carries the type.
|
|
126
|
+
* Tools list the topics that use it.
|
|
127
|
+
*
|
|
128
|
+
* @returns A schema whose input and output type is `T`.
|
|
129
|
+
*/
|
|
130
|
+
declare function unchecked<T>(): StandardSchemaV1<T, T>;
|
|
131
|
+
/**
|
|
132
|
+
* True if the schema came from `unchecked()`.
|
|
133
|
+
*
|
|
134
|
+
* @internal
|
|
135
|
+
*/
|
|
136
|
+
declare function isUnchecked(schema: StandardSchemaV1): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* The default export of a contracts module. Built only by `contracts()`, so
|
|
139
|
+
* the bus worker can tell it from any other export.
|
|
140
|
+
*/
|
|
141
|
+
type Contracts = {
|
|
142
|
+
/** Brand, so the bus worker can tell a contracts list from any other export. */
|
|
143
|
+
readonly "~isoloop": "contracts";
|
|
144
|
+
/** The topics, in the order given to `contracts()`. */
|
|
145
|
+
readonly topics: readonly AnyTopicDef[];
|
|
146
|
+
/**
|
|
147
|
+
* Looks a topic up by name.
|
|
148
|
+
*
|
|
149
|
+
* @param name - The topic's name.
|
|
150
|
+
* @returns The topic, or `undefined` if the list doesn't have it.
|
|
151
|
+
*/
|
|
152
|
+
get(name: string): AnyTopicDef | undefined;
|
|
153
|
+
};
|
|
154
|
+
/** A contracts list that can't be built: duplicate or reserved names, or a bad definition. */
|
|
155
|
+
declare class ContractsError extends Error {
|
|
156
|
+
/** `reserved_topic` for an `isoloop.*` name, `contracts_invalid` for anything else. */
|
|
157
|
+
readonly code: ErrorCode;
|
|
158
|
+
/** The topics at fault. */
|
|
159
|
+
readonly topics: readonly string[];
|
|
160
|
+
/**
|
|
161
|
+
* @param code - The error code.
|
|
162
|
+
* @param message - What is wrong, naming the topics.
|
|
163
|
+
* @param topics - The topics at fault.
|
|
164
|
+
*/
|
|
165
|
+
constructor(code: ErrorCode, message: string, topics: readonly string[]);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Builds the contracts list, the default export of a contracts module.
|
|
169
|
+
*
|
|
170
|
+
* @param topics - Every topic the app uses.
|
|
171
|
+
* @returns The list, frozen.
|
|
172
|
+
* @throws {ContractsError} `reserved_topic` for an `isoloop.*` name, which
|
|
173
|
+
* belongs to the kernel. `contracts_invalid` for a duplicate name, a topic
|
|
174
|
+
* without a valid schema or retention, a bad `tool` or `seed`, or seeds that
|
|
175
|
+
* form a cycle.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { contracts, topic } from '@isoloop/bus';
|
|
180
|
+
* import { z } from 'zod';
|
|
181
|
+
*
|
|
182
|
+
* const prices = topic('markets.prices.v1', {
|
|
183
|
+
* schema: z.object({ symbol: z.string(), last: z.number() }),
|
|
184
|
+
* retention: { kind: 'last-n', n: 1_000 },
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* export default contracts(prices);
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
declare function contracts(...topics: AnyTopicDef[]): Contracts;
|
|
191
|
+
/**
|
|
192
|
+
* True for a value built by `contracts()`, from any copy of this package.
|
|
193
|
+
*
|
|
194
|
+
* @internal
|
|
195
|
+
*/
|
|
196
|
+
declare function isContracts(x: unknown): x is Contracts;
|
|
197
|
+
/**
|
|
198
|
+
* The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5).
|
|
199
|
+
*
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
declare function topicConfig(t: AnyTopicDef): TopicConfig;
|
|
203
|
+
export { unchecked as _, InputOf as a, ReplyOutputOf as c, TopicDef as d, contracts as f, topicConfig as g, topic as h, DEFAULT_MAX_RECORD_BYTES as i, Retention as l, isUnchecked as m, Contracts as n, OutputOf as o, isContracts as p, ContractsError as r, ReplyInputOf as s, AnyTopicDef as t, ToolSpec as u, StandardSchemaV1 as v, toIssues as y };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/bus",
|
|
3
|
-
"version": "0.1.0-next.
|
|
3
|
+
"version": "0.1.0-next.10",
|
|
4
4
|
"description": "Log bus that runs in its own worker: topics, schemas, offsets, replay.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@isoloop/codec": "0.1.0-next.
|
|
31
|
-
"@isoloop/protocol": "0.1.0-next.
|
|
30
|
+
"@isoloop/codec": "0.1.0-next.10",
|
|
31
|
+
"@isoloop/protocol": "0.1.0-next.10"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"fast-check": "4.10.2",
|
package/dist/topic-BQbes-gX.d.ts
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { BusRecord, ErrorCode, Issue as Issue$1, Retention, Retention as Retention$1, TopicConfig } from "@isoloop/protocol";
|
|
2
|
-
/**
|
|
3
|
-
* The Standard Schema V1 interface (https://standardschema.dev), vendored so
|
|
4
|
-
* the bus depends on no schema library (ADR 0003, decision 1). Zod 4, Valibot
|
|
5
|
-
* and ArkType implement it. Copied from the spec; keep it in step with it.
|
|
6
|
-
*/
|
|
7
|
-
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
8
|
-
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
9
|
-
}
|
|
10
|
-
declare namespace StandardSchemaV1 {
|
|
11
|
-
export interface Props<Input = unknown, Output = Input> {
|
|
12
|
-
readonly version: 1;
|
|
13
|
-
readonly vendor: string;
|
|
14
|
-
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
-
readonly types?: Types<Input, Output> | undefined;
|
|
16
|
-
}
|
|
17
|
-
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
18
|
-
export interface SuccessResult<Output> {
|
|
19
|
-
readonly value: Output;
|
|
20
|
-
readonly issues?: undefined;
|
|
21
|
-
}
|
|
22
|
-
export interface FailureResult {
|
|
23
|
-
readonly issues: ReadonlyArray<Issue>;
|
|
24
|
-
}
|
|
25
|
-
export interface Issue {
|
|
26
|
-
readonly message: string;
|
|
27
|
-
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
28
|
-
}
|
|
29
|
-
export interface PathSegment {
|
|
30
|
-
readonly key: PropertyKey;
|
|
31
|
-
}
|
|
32
|
-
export interface Types<Input = unknown, Output = Input> {
|
|
33
|
-
readonly input: Input;
|
|
34
|
-
readonly output: Output;
|
|
35
|
-
}
|
|
36
|
-
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
37
|
-
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
|
|
41
|
-
* (ADR 0003, decision 10). Symbol keys become their description.
|
|
42
|
-
*/
|
|
43
|
-
declare function toIssues(issues: ReadonlyArray<StandardSchemaV1.Issue>): Issue$1[];
|
|
44
|
-
/** Default `maxRecordBytes`: 256 KiB (design §6.1). */
|
|
45
|
-
declare const DEFAULT_MAX_RECORD_BYTES: number;
|
|
46
|
-
/**
|
|
47
|
-
* A topic: its name, schema and log policy (design §6.1). Publishers are typed
|
|
48
|
-
* with `In`, subscribers with `Out`, because the log stores the schema's
|
|
49
|
-
* validated output (ADR 0003, decision 11). `RIn`/`ROut` type `bus.request`
|
|
50
|
-
* replies; a topic without `reply` can't be requested.
|
|
51
|
-
*/
|
|
52
|
-
type TopicDef<In, Out = In, RIn = never, ROut = RIn> = {
|
|
53
|
-
/** Carries the major version, e.g. `cart.items.v1` (ADR 0003, decision 17). */
|
|
54
|
-
readonly name: string;
|
|
55
|
-
/** Required. `unchecked<T>()` is the explicit opt-out. */
|
|
56
|
-
readonly schema: StandardSchemaV1<In, Out>;
|
|
57
|
-
/** Required for `bus.request`. */
|
|
58
|
-
readonly reply?: StandardSchemaV1<RIn, ROut>;
|
|
59
|
-
readonly retention: Retention$1;
|
|
60
|
-
/** Rough structured size limit per record. Default 256 KiB. */
|
|
61
|
-
readonly maxRecordBytes?: number;
|
|
62
|
-
/**
|
|
63
|
-
* Default `memory`. `persistent` keeps the topic in OPFS (ADR 0007): it
|
|
64
|
-
* survives a bus crash and a reload. Where OPFS isn't available, or another
|
|
65
|
-
* tab holds it, it behaves as `memory` and the kernel emits
|
|
66
|
-
* `bus:durability-degraded`.
|
|
67
|
-
*/
|
|
68
|
-
readonly durability?: "memory" | "persistent";
|
|
69
|
-
/**
|
|
70
|
-
* Starts this topic from an older one (ADR 0003, decision 18). On the first
|
|
71
|
-
* boot where this topic is empty and `from` has records, the bus copies
|
|
72
|
-
* them through `map` and this topic's schema before it opens: its publishes
|
|
73
|
-
* wait and its subscribers get nothing until then. `map` must be pure and
|
|
74
|
-
* synchronous. `follow` keeps mapping new `from` records afterwards.
|
|
75
|
-
*/
|
|
76
|
-
readonly seed?: {
|
|
77
|
-
readonly from: AnyTopicDef;
|
|
78
|
-
readonly map: (value: any, record: BusRecord) => In;
|
|
79
|
-
readonly follow?: boolean;
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
type AnyTopicDef = TopicDef<any, any, any, any>;
|
|
83
|
-
/** What a publisher passes. */
|
|
84
|
-
type InputOf<D> = D extends TopicDef<infer In, any, any, any> ? In : never;
|
|
85
|
-
/** What a subscriber receives. */
|
|
86
|
-
type OutputOf<D> = D extends TopicDef<any, infer Out, any, any> ? Out : never;
|
|
87
|
-
/** What a responder passes to `reply`. */
|
|
88
|
-
type ReplyInputOf<D> = D extends TopicDef<any, any, infer RIn, any> ? RIn : never;
|
|
89
|
-
/** What a requester receives. */
|
|
90
|
-
type ReplyOutputOf<D> = D extends TopicDef<any, any, any, infer ROut> ? ROut : never;
|
|
91
|
-
/** Defines a topic. Types come from the schemas. */
|
|
92
|
-
declare function topic<In, Out = In, RIn = never, ROut = RIn>(name: string, def: Omit<TopicDef<In, Out, RIn, ROut>, "name">): TopicDef<In, Out, RIn, ROut>;
|
|
93
|
-
/**
|
|
94
|
-
* A Standard Schema that accepts any value unchanged and only carries the type
|
|
95
|
-
* (ADR 0003, decision 2). Tools list topics that use it.
|
|
96
|
-
*/
|
|
97
|
-
declare function unchecked<T>(): StandardSchemaV1<T, T>;
|
|
98
|
-
/** True if the schema came from `unchecked()`. */
|
|
99
|
-
declare function isUnchecked(schema: StandardSchemaV1): boolean;
|
|
100
|
-
/**
|
|
101
|
-
* The default export of a contracts module (ADR 0003, decision 4). Built only
|
|
102
|
-
* by `contracts()`, so the bus worker can tell it from any other export.
|
|
103
|
-
*/
|
|
104
|
-
type Contracts = {
|
|
105
|
-
/** Brand, so the bus worker can tell a contracts list from any other export. */
|
|
106
|
-
readonly "~isoloop": "contracts";
|
|
107
|
-
readonly topics: readonly AnyTopicDef[];
|
|
108
|
-
/** Looks a topic up by name. */
|
|
109
|
-
get(name: string): AnyTopicDef | undefined;
|
|
110
|
-
};
|
|
111
|
-
/** A contracts list that can't be built: duplicate or reserved names, or a bad definition. */
|
|
112
|
-
declare class ContractsError extends Error {
|
|
113
|
-
readonly code: ErrorCode;
|
|
114
|
-
/** The topics at fault. */
|
|
115
|
-
readonly topics: readonly string[];
|
|
116
|
-
constructor(code: ErrorCode, message: string, topics: readonly string[]);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Builds the contracts list. Rejects duplicate names and `isoloop.*` topics,
|
|
120
|
-
* which belong to the kernel (ADR 0003, decisions 4 and 6).
|
|
121
|
-
*/
|
|
122
|
-
declare function contracts(...topics: AnyTopicDef[]): Contracts;
|
|
123
|
-
/** True for a value built by `contracts()`, from any copy of this package. */
|
|
124
|
-
declare function isContracts(x: unknown): x is Contracts;
|
|
125
|
-
/** The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5). */
|
|
126
|
-
declare function topicConfig(t: AnyTopicDef): TopicConfig;
|
|
127
|
-
export { StandardSchemaV1 as _, InputOf as a, ReplyOutputOf as c, contracts as d, isContracts as f, unchecked as g, topicConfig as h, DEFAULT_MAX_RECORD_BYTES as i, Retention as l, topic as m, Contracts as n, OutputOf as o, isUnchecked as p, ContractsError as r, ReplyInputOf as s, AnyTopicDef as t, TopicDef as u, toIssues as v };
|