@isoloop/bus 0.1.0-next.8 → 0.1.0

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/testing.d.ts CHANGED
@@ -1,11 +1,13 @@
1
- import { n as Contracts } from "./topic-BgqMD0WA.js";
1
+ import { n as Contracts } from "./topic-BkBLtTpT.js";
2
2
  import { Issue } from "@isoloop/protocol";
3
3
  /** Sample values for one topic. An array is shorthand for `{ values }`. */
4
4
  export type TopicSamples = readonly unknown[] | {
5
5
  values: readonly unknown[];
6
6
  replies?: readonly unknown[];
7
7
  };
8
+ /** One problem `testContracts` found. */
8
9
  export type ContractProblem = {
10
+ /** The topic's name. */
9
11
  topic: string;
10
12
  /** `value` for the topic schema, `reply` for its reply schema. */
11
13
  schema: "value" | "reply";
@@ -16,13 +18,19 @@ export type ContractProblem = {
16
18
  * plain, structured-cloneable data. `no_samples`: the topic has none.
17
19
  */
18
20
  kind: "invalid" | "not_plain" | "no_samples";
21
+ /** What is wrong, in one line. */
19
22
  message: string;
23
+ /** The schema's issues, for `invalid`. */
20
24
  issues?: Issue[];
21
25
  };
22
26
  /**
23
27
  * Runs sample values through every schema in a contracts list and reports
24
- * samples the schema rejects and outputs that aren't plain, cloneable data
25
- * (ADR 0003, decision 11). The broker only checks cloneability in dev builds,
26
- * so run this in the contracts package's tests. An empty result means all good.
28
+ * samples the schema rejects and outputs that aren't plain, cloneable data.
29
+ * The bus only checks cloneability in dev builds, so run this in the contracts
30
+ * package's tests.
31
+ *
32
+ * @param list - The contracts list.
33
+ * @param samples - Sample values per topic name. A topic without samples is reported as `no_samples`.
34
+ * @returns Every problem found. An empty array means all good.
27
35
  */
28
36
  export declare function testContracts(list: Contracts, samples: Readonly<Record<string, TopicSamples>>): Promise<ContractProblem[]>;
package/dist/testing.js CHANGED
@@ -1,4 +1,20 @@
1
- import { t as toIssues } from "./standard-schema-DnJtEmJw.js";
1
+ // src/contracts/standard-schema.ts
2
+ const segmentKey = (s) => typeof s === "object" ? s.key : s;
3
+ /**
4
+ * Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
5
+ * (ADR 0003, decision 10). Symbol keys become their description.
6
+ *
7
+ * @internal
8
+ */
9
+ function toIssues(issues) {
10
+ return issues.map((i) => ({
11
+ message: i.message,
12
+ path: (i.path ?? []).map((s) => {
13
+ const k = segmentKey(s);
14
+ return typeof k === "symbol" ? String(k) : k;
15
+ })
16
+ }));
17
+ }
2
18
  // src/testing.ts
3
19
  const PLAIN_TAGS = /* @__PURE__ */ new Set([
4
20
  "[object Date]",
@@ -71,9 +87,13 @@ async function check(topic, which, schema, samples) {
71
87
  const isList = (s) => Array.isArray(s);
72
88
  /**
73
89
  * Runs sample values through every schema in a contracts list and reports
74
- * samples the schema rejects and outputs that aren't plain, cloneable data
75
- * (ADR 0003, decision 11). The broker only checks cloneability in dev builds,
76
- * so run this in the contracts package's tests. An empty result means all good.
90
+ * samples the schema rejects and outputs that aren't plain, cloneable data.
91
+ * The bus only checks cloneability in dev builds, so run this in the contracts
92
+ * package's tests.
93
+ *
94
+ * @param list - The contracts list.
95
+ * @param samples - Sample values per topic name. A topic without samples is reported as `no_samples`.
96
+ * @returns Every problem found. An empty array means all good.
77
97
  */
78
98
  async function testContracts(list, samples) {
79
99
  return (await Promise.all(list.topics.map(async (t) => {
@@ -0,0 +1,184 @@
1
+ import { BusRecord, ErrorCode, Retention, Retention as Retention$1, ToolSpec, ToolSpec as ToolSpec$1 } 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
+ /** The default `maxRecordBytes`: 256 KiB. */
40
+ declare const DEFAULT_MAX_RECORD_BYTES: number;
41
+ /**
42
+ * A topic: its name, its schemas and what its log keeps. Publishers are typed
43
+ * with `In` and subscribers with `Out`, because the log stores what the schema
44
+ * returns, not what was published. `RIn` and `ROut` type the replies to
45
+ * `bus.request`; a topic without `reply` can't be requested.
46
+ */
47
+ type TopicDef<In, Out = In, RIn = never, ROut = RIn> = {
48
+ /** The topic's name. By convention it ends in the major version, for example `cart.items.v1`. */
49
+ readonly name: string;
50
+ /** The Standard Schema that checks each published value. Required: `unchecked<T>()` is the explicit opt-out. */
51
+ readonly schema: StandardSchemaV1<In, Out>;
52
+ /** The Standard Schema for replies. Required for `bus.request`. @experimental */
53
+ readonly reply?: StandardSchemaV1<RIn, ROut>;
54
+ /** What the log keeps: the newest `n` records, the records younger than `ms`, or the latest record per key. Required. */
55
+ readonly retention: Retention$1;
56
+ /** The largest record, by its rough structured size. A bigger one is rejected with `too_large`. Default 256 KiB. */
57
+ readonly maxRecordBytes?: number;
58
+ /**
59
+ * Default `memory`. `persistent` keeps the topic in OPFS, so it survives a
60
+ * bus crash and a reload. Where OPFS isn't available, or another tab holds
61
+ * it, it behaves as `memory` and the kernel emits `bus:durability_degraded`.
62
+ */
63
+ readonly durability?: "memory" | "persistent";
64
+ /**
65
+ * Starts this topic from an older one. On the first boot where this topic
66
+ * is empty and `from` has records, the bus copies them through `map` and
67
+ * this topic's schema before it opens: its publishes wait and its
68
+ * subscribers get nothing until then. `map` must be pure and synchronous.
69
+ * `follow` keeps mapping new `from` records afterwards.
70
+ */
71
+ readonly seed?: {
72
+ /** The older topic to copy from. */
73
+ readonly from: AnyTopicDef;
74
+ /** 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`. */
75
+ readonly map: (value: any, record: BusRecord) => In;
76
+ /** Keeps mapping new `from` records after the first copy. Default `false`. */
77
+ readonly follow?: boolean;
78
+ };
79
+ /**
80
+ * Describes this topic as a tool for agents. Only a topic with `reply` can
81
+ * have one: without it this field's type is `never`. It exposes nothing by
82
+ * itself; the app's `agents.expose` does. It stays on the host: the bus
83
+ * worker never sees it.
84
+ * @experimental
85
+ */
86
+ readonly tool?: [RIn] extends [never] ? never : ToolSpec$1;
87
+ };
88
+ /** A topic of any types, as `contracts()` takes them. */
89
+ type AnyTopicDef = TopicDef<any, any, any, any>;
90
+ /** What a publisher passes. */
91
+ type InputOf<D> = D extends TopicDef<infer In, any, any, any> ? In : never;
92
+ /** What a subscriber receives. */
93
+ type OutputOf<D> = D extends TopicDef<any, infer Out, any, any> ? Out : never;
94
+ /** What a responder passes to `reply`. */
95
+ type ReplyInputOf<D> = D extends TopicDef<any, any, infer RIn, any> ? RIn : never;
96
+ /** What a requester receives. */
97
+ type ReplyOutputOf<D> = D extends TopicDef<any, any, any, infer ROut> ? ROut : never;
98
+ /**
99
+ * Defines a topic. Its types come from its schemas.
100
+ *
101
+ * @param name - The topic's name, for example `cart.items.v1`.
102
+ * @param def - The topic's schemas, retention and options.
103
+ * @returns The topic, frozen. List it in `contracts()` to use it.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * import { topic } from '@isoloop/bus';
108
+ * import { z } from 'zod';
109
+ *
110
+ * export const cartItems = topic('cart.items.v1', {
111
+ * schema: z.object({ sku: z.string(), qty: z.number().int().positive() }),
112
+ * retention: { kind: 'compacted' },
113
+ * });
114
+ * ```
115
+ */
116
+ 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>;
117
+ /**
118
+ * A Standard Schema that accepts any value unchanged and only carries the type.
119
+ * Tools list the topics that use it.
120
+ *
121
+ * @returns A schema whose input and output type is `T`.
122
+ */
123
+ declare function unchecked<T>(): StandardSchemaV1<T, T>;
124
+ /**
125
+ * The default export of a contracts module. Built only by `contracts()`, so
126
+ * the bus worker can tell it from any other export.
127
+ */
128
+ type Contracts = {
129
+ /** Brand, so the bus worker can tell a contracts list from any other export. */
130
+ readonly "~isoloop": "contracts";
131
+ /** The topics, in the order given to `contracts()`. */
132
+ readonly topics: readonly AnyTopicDef[];
133
+ /**
134
+ * Looks a topic up by name.
135
+ *
136
+ * @param name - The topic's name.
137
+ * @returns The topic, or `undefined` if the list doesn't have it.
138
+ */
139
+ get(name: string): AnyTopicDef | undefined;
140
+ };
141
+ /** A contracts list that can't be built: duplicate or reserved names, or a bad definition. */
142
+ declare class ContractsError extends Error {
143
+ /** `reserved_topic` for an `isoloop.*` name, `contracts_invalid` for anything else. */
144
+ readonly code: ErrorCode;
145
+ /** The topics at fault. */
146
+ readonly topics: readonly string[];
147
+ /**
148
+ * @param code - The error code.
149
+ * @param message - What is wrong, naming the topics.
150
+ * @param topics - The topics at fault.
151
+ */
152
+ constructor(code: ErrorCode, message: string, topics: readonly string[]);
153
+ }
154
+ /**
155
+ * Builds the contracts list, the default export of a contracts module.
156
+ *
157
+ * @param topics - Every topic the app uses.
158
+ * @returns The list, frozen.
159
+ * @throws {ContractsError} `reserved_topic` for an `isoloop.*` name, which
160
+ * belongs to the kernel. `contracts_invalid` for a duplicate name, a topic
161
+ * without a valid schema or retention, a bad `tool` or `seed`, or seeds that
162
+ * form a cycle.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import { contracts, topic } from '@isoloop/bus';
167
+ * import { z } from 'zod';
168
+ *
169
+ * const prices = topic('markets.prices.v1', {
170
+ * schema: z.object({ symbol: z.string(), last: z.number() }),
171
+ * retention: { kind: 'last-n', n: 1_000 },
172
+ * });
173
+ *
174
+ * export default contracts(prices);
175
+ * ```
176
+ */
177
+ declare function contracts(...topics: AnyTopicDef[]): Contracts;
178
+ /**
179
+ * True for a value built by `contracts()`, from any copy of this package.
180
+ *
181
+ * @internal
182
+ */
183
+ declare function isContracts(x: unknown): x is Contracts;
184
+ export { InputOf as a, ReplyOutputOf as c, TopicDef as d, contracts as f, StandardSchemaV1 as g, unchecked as h, DEFAULT_MAX_RECORD_BYTES as i, Retention as l, topic as m, Contracts as n, OutputOf as o, isContracts as p, ContractsError as r, ReplyInputOf as s, AnyTopicDef as t, ToolSpec as u };
package/package.json CHANGED
@@ -1,10 +1,24 @@
1
1
  {
2
2
  "name": "@isoloop/bus",
3
- "version": "0.1.0-next.8",
4
- "description": "Log bus that runs in its own worker: topics, schemas, offsets, replay.",
3
+ "version": "0.1.0",
4
+ "description": "The Isoloop message bus: typed topics in a worker, with retention, replay and durable history.",
5
+ "keywords": [
6
+ "isoloop",
7
+ "web-workers",
8
+ "event-loop",
9
+ "isolation",
10
+ "message-bus",
11
+ "pubsub",
12
+ "event-log",
13
+ "replay"
14
+ ],
15
+ "homepage": "https://isoloop.dev/docs/api/bus/",
5
16
  "license": "MIT",
6
17
  "type": "module",
7
- "sideEffects": false,
18
+ "sideEffects": [
19
+ "./src/worker.ts",
20
+ "./dist/bus-worker.js"
21
+ ],
8
22
  "repository": {
9
23
  "type": "git",
10
24
  "url": "git+https://github.com/isoloop/isoloop.git",
@@ -27,8 +41,8 @@
27
41
  }
28
42
  },
29
43
  "dependencies": {
30
- "@isoloop/codec": "0.1.0-next.8",
31
- "@isoloop/protocol": "0.1.0-next.8"
44
+ "@isoloop/codec": "0.1.0",
45
+ "@isoloop/protocol": "0.1.0"
32
46
  },
33
47
  "devDependencies": {
34
48
  "fast-check": "4.10.2",
@@ -1,16 +0,0 @@
1
- // src/standard-schema.ts
2
- const segmentKey = (s) => typeof s === "object" ? s.key : s;
3
- /**
4
- * Maps schema issues to the cloneable `{ message, path }[]` sent with `invalid`
5
- * (ADR 0003, decision 10). Symbol keys become their description.
6
- */
7
- function toIssues(issues) {
8
- return issues.map((i) => ({
9
- message: i.message,
10
- path: (i.path ?? []).map((s) => {
11
- const k = segmentKey(s);
12
- return typeof k === "symbol" ? String(k) : k;
13
- })
14
- }));
15
- }
16
- export { toIssues as t };
@@ -1,135 +0,0 @@
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), 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
- * Describes this topic as a tool for agents (ADR 0018, decision 1). Only a
83
- * topic with `reply` can have one: without it this field's type is `never`.
84
- * It exposes nothing by itself; the app's `agents.expose` does. The bus
85
- * never sees it (not part of `topicConfig`).
86
- * @experimental
87
- */
88
- readonly tool?: [RIn] extends [never] ? never : ToolSpec$1;
89
- };
90
- type AnyTopicDef = TopicDef<any, any, any, any>;
91
- /** What a publisher passes. */
92
- type InputOf<D> = D extends TopicDef<infer In, any, any, any> ? In : never;
93
- /** What a subscriber receives. */
94
- type OutputOf<D> = D extends TopicDef<any, infer Out, any, any> ? Out : never;
95
- /** What a responder passes to `reply`. */
96
- type ReplyInputOf<D> = D extends TopicDef<any, any, infer RIn, any> ? RIn : never;
97
- /** What a requester receives. */
98
- type ReplyOutputOf<D> = D extends TopicDef<any, any, any, infer ROut> ? ROut : never;
99
- /** Defines a topic. Types come from the schemas. */
100
- 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>;
101
- /**
102
- * A Standard Schema that accepts any value unchanged and only carries the type
103
- * (ADR 0003, decision 2). Tools list topics that use it.
104
- */
105
- declare function unchecked<T>(): StandardSchemaV1<T, T>;
106
- /** True if the schema came from `unchecked()`. */
107
- declare function isUnchecked(schema: StandardSchemaV1): boolean;
108
- /**
109
- * The default export of a contracts module (ADR 0003, decision 4). Built only
110
- * by `contracts()`, so the bus worker can tell it from any other export.
111
- */
112
- type Contracts = {
113
- /** Brand, so the bus worker can tell a contracts list from any other export. */
114
- readonly "~isoloop": "contracts";
115
- readonly topics: readonly AnyTopicDef[];
116
- /** Looks a topic up by name. */
117
- get(name: string): AnyTopicDef | undefined;
118
- };
119
- /** A contracts list that can't be built: duplicate or reserved names, or a bad definition. */
120
- declare class ContractsError extends Error {
121
- readonly code: ErrorCode;
122
- /** The topics at fault. */
123
- readonly topics: readonly string[];
124
- constructor(code: ErrorCode, message: string, topics: readonly string[]);
125
- }
126
- /**
127
- * Builds the contracts list. Rejects duplicate names and `isoloop.*` topics,
128
- * which belong to the kernel (ADR 0003, decisions 4 and 6).
129
- */
130
- declare function contracts(...topics: AnyTopicDef[]): Contracts;
131
- /** True for a value built by `contracts()`, from any copy of this package. */
132
- declare function isContracts(x: unknown): x is Contracts;
133
- /** The structured-cloneable part of a topic, sent at `boot` for the contracts check (ADR 0003, decision 5). */
134
- declare function topicConfig(t: AnyTopicDef): TopicConfig;
135
- 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 };