@toa.io/core 1.0.0-alpha.282 → 1.0.0-alpha.284

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/CHANGELOG.md CHANGED
@@ -3,6 +3,30 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [1.0.0-alpha.284](https://github.com/toa-io/toa/compare/v1.0.0-alpha.283...v1.0.0-alpha.284) (2026-09-05)
7
+
8
+ ### Bug Fixes
9
+
10
+ * **core:** a criteria list coerces every value in it ([37f57ee](https://github.com/toa-io/toa/commit/37f57ee8b50907a13722b7a558b881cccf105442))
11
+
12
+ ### Features
13
+
14
+ * an operation states what it is ([b5e2f66](https://github.com/toa-io/toa/commit/b5e2f66c8bf67924e2eaaaa11f283dfb4d810981))
15
+ * **atomicity:** a replica can be told what it owns, not only ask ([f7b56b9](https://github.com/toa-io/toa/commit/f7b56b99cc43313f3f855df658a80fdc0659689e))
16
+
17
+
18
+ # [1.0.0-alpha.283](https://github.com/toa-io/toa/compare/v1.0.0-alpha.282...v1.0.0-alpha.283) (2026-09-04)
19
+
20
+ ### Bug Fixes
21
+
22
+ * **core:** an operation returns the errors it declares, and nothing else ([1c2b819](https://github.com/toa-io/toa/commit/1c2b8193ae7fbe86bc1a25053a6044ee4dd16a4a))
23
+ * **storages:** take `Maybe` from core, not from the deprecated package ([bcaeeb3](https://github.com/toa-io/toa/commit/bcaeeb3c76c89af34e94ff569a210d21b7e87f64))
24
+
25
+ ### Features
26
+
27
+ * **extensions:** an extension declares what it puts on a component's context ([133bc2a](https://github.com/toa-io/toa/commit/133bc2aad1e45a2d4aa68a08a5e44ca48d3b7414))
28
+
29
+
6
30
  # [1.0.0-alpha.282](https://github.com/toa-io/toa/compare/v1.0.0-alpha.281...v1.0.0-alpha.282) (2026-09-03)
7
31
 
8
32
  ### Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "1.0.0-alpha.282",
3
+ "version": "1.0.0-alpha.284",
4
4
  "type": "module",
5
5
  "description": "Toa Core",
6
6
  "author": "temich <tema.gurtovoy@gmail.com>",
@@ -22,10 +22,10 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@rsql/parser": "1.6.0",
25
- "@toa.io/generic": "1.0.0-alpha.282",
25
+ "@toa.io/generic": "1.0.0-alpha.283",
26
26
  "js-yaml": "5.4.1",
27
27
  "openspan": "1.0.0-alpha.277",
28
28
  "uuid": "14.0.2"
29
29
  },
30
- "gitHead": "4007c814725dd015d3bbebb9a28e28a247645c4f"
30
+ "gitHead": "006755556879e91e8d68fa86753aa3b475bdf4bf"
31
31
  }
@@ -1,4 +1,3 @@
1
- import * as schemas from './schemas/index.js'
2
1
  import { Contract } from './contract.js'
3
2
  import { ResponseContractException } from '../exceptions.js'
4
3
 
@@ -25,21 +24,25 @@ export class Reply extends Contract {
25
24
  schema.properties.output = output
26
25
  }
27
26
 
28
- if (errors !== undefined)
29
- schema.properties.error = {
30
- type: 'object',
31
- properties: {
32
- code: {
33
- enum: errors
27
+ /*
28
+ * An error a caller is meant to handle is one the operation states. Where none are
29
+ * stated, an error is not a reply this operation makes — it is a mistake, and the
30
+ * contract says so rather than passing an undeclared code on to whoever called.
31
+ */
32
+ schema.properties.error = errors === undefined
33
+ ? false
34
+ : {
35
+ type: 'object',
36
+ properties: {
37
+ code: {
38
+ enum: errors
39
+ },
40
+ message: {
41
+ type: 'string'
42
+ }
34
43
  },
35
- message: {
36
- type: 'string'
37
- }
38
- },
39
- required: ['code']
40
- }
41
- else
42
- schema.properties.error = schemas.error
44
+ required: ['code']
45
+ }
43
46
 
44
47
  return schema
45
48
  }
@@ -9,7 +9,7 @@ export class Request extends Contract {
9
9
  constructor (schema, definition) {
10
10
  super(schema)
11
11
 
12
- for (const key of ['input', 'output', 'errors'])
12
+ for (const key of ['description', 'input', 'output', 'errors'])
13
13
  if (definition[key] !== undefined)
14
14
  this.discovery[key] = definition[key]
15
15
  }
@@ -33,6 +33,7 @@ export class Outbox extends Connector {
33
33
  #publishing = new Set()
34
34
 
35
35
  #timer
36
+ #off
36
37
  #pumping = false
37
38
  #closing = false
38
39
 
@@ -101,11 +102,20 @@ export class Outbox extends Connector {
101
102
 
102
103
  this.#timer = setInterval(() => this.#tick(), this.#interval)
103
104
  this.#timer.unref()
105
+
106
+ /*
107
+ * A lane changing hands is exactly when rows stranded in it become this replica's to
108
+ * publish, and the cycle would not notice for up to an interval. Being told costs a cycle
109
+ * that finds nothing in the usual case, where the claim arrives once and never changes.
110
+ */
111
+ this.#off = this.#atom.onassigned(() => this.#tick())
104
112
  }
105
113
 
106
114
  async close () {
107
115
  this.#closing = true
108
116
 
117
+ this.#off?.()
118
+
109
119
  if (this.#timer !== undefined) clearInterval(this.#timer)
110
120
 
111
121
  await this.#drain()
@@ -23,7 +23,14 @@ const coerce = (node, properties) => {
23
23
  throw new QuerySyntaxException(`Criteria selector '${node.left.selector}' is not defined`)
24
24
  }
25
25
 
26
- if (COERCE[property.type] !== undefined) { node.right.value = COERCE[property.type](node.right.value) }
26
+ const coerce = COERCE[property.type]
27
+
28
+ // `=in=` and `=out=` carry a list, and coercing that as one value gives whatever
29
+ // `parseInt` makes of a comma-separated string
30
+ if (coerce !== undefined)
31
+ node.right.value = Array.isArray(node.right.value)
32
+ ? node.right.value.map((value) => coerce(value))
33
+ : coerce(node.right.value)
27
34
  } else {
28
35
  if (node.left !== undefined) coerce(node.left, properties)
29
36
  if (node.right !== undefined) coerce(node.right, properties)
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict'
3
3
 
4
4
  import { Outbox } from '../src/outbox/index.js'
5
5
 
6
- let emission, storage, atom, outbox
6
+ let emission, storage, atom, outbox, listeners
7
7
 
8
8
  const BATCH = 4
9
9
 
@@ -28,7 +28,18 @@ beforeEach(() => {
28
28
  }
29
29
  }
30
30
 
31
- atom = { slots: mock.fn(() => [0]), link: mock.fn() }
31
+ listeners = []
32
+
33
+ atom = {
34
+ slots: mock.fn(() => [0]),
35
+ onassigned: (listener) => {
36
+ listeners.push(listener)
37
+ listener({ i: 0, n: 1 })
38
+
39
+ return () => { listeners = listeners.filter((one) => one !== listener) }
40
+ },
41
+ link: mock.fn()
42
+ }
32
43
  outbox = new Outbox(emission, storage, atom, { interval: 1000, batch: BATCH })
33
44
  })
34
45
 
@@ -36,6 +47,11 @@ afterEach(() => {
36
47
  mock.timers.reset()
37
48
  })
38
49
 
50
+ /** everything the current turn awaited, without moving the clock */
51
+ const settled = async () => {
52
+ for (let i = 0; i < 20; i++) await Promise.resolve()
53
+ }
54
+
39
55
  /** one cycle, and everything it awaited */
40
56
  const cycle = async () => {
41
57
  mock.timers.tick(1000)
@@ -119,3 +135,34 @@ function resetCalls (target = [assert, BATCH, page, cycle], seen = new Set()) {
119
135
  if (typeof value === 'function' && value.mock !== undefined) value.mock.resetCalls()
120
136
  else resetCalls(value, seen)
121
137
  }
138
+
139
+ it('should read as soon as a claim arrives, rather than on its next cycle', async () => {
140
+ atom.slots.mock.mockImplementation(() => null)
141
+
142
+ await outbox.open()
143
+ await settled()
144
+
145
+ assert.strictEqual(storage.outbox.pending.mock.callCount(), 0, 'nothing is owned yet')
146
+
147
+ atom.slots.mock.mockImplementation(() => [0])
148
+
149
+ for (const listener of listeners) listener({ i: 0, n: 1 })
150
+
151
+ await settled()
152
+
153
+ assert.strictEqual(storage.outbox.pending.mock.callCount(), 1,
154
+ 'the lane is its own now, and the cycle is up to five seconds away')
155
+ })
156
+
157
+ it('should read the lanes it inherits when the group resizes', async () => {
158
+ await outbox.open()
159
+ await settled()
160
+
161
+ const before = storage.outbox.pending.mock.callCount()
162
+
163
+ for (const listener of listeners) listener({ i: 0, n: 2 })
164
+
165
+ await settled()
166
+
167
+ assert.strictEqual(storage.outbox.pending.mock.callCount(), before + 1)
168
+ })
@@ -23,6 +23,13 @@ describe('criteria', () => {
23
23
  assert.deepStrictEqual(query.criteria, fixtures.samples.simple.parsed.criteria)
24
24
  })
25
25
 
26
+ it('should coerce every value of a list', () => {
27
+ const instance = new Query({ n: { type: 'integer' } })
28
+ const query = instance.parse({ criteria: 'n=in=(1,2,3)' })
29
+
30
+ assert.deepStrictEqual(query.criteria.right.value, [1, 2, 3])
31
+ })
32
+
26
33
  it('should keep a parsed criteria', () => {
27
34
  const instance = new Query(fixtures.samples.simple.properties)
28
35
 
@@ -22,6 +22,20 @@ declare namespace toa.core {
22
22
  */
23
23
  slots (total: number): number[] | null
24
24
 
25
+ /**
26
+ * Calls `listener` with the assignment this replica holds, and again whenever it
27
+ * changes — one arrived, was lost, or the group resized. Answers with what removes the
28
+ * listener again.
29
+ *
30
+ * A change and not a heartbeat: a group that stays as it is never calls back. It is
31
+ * called once as it is added, with the claim as it stands.
32
+ *
33
+ * What `slots` cannot say. Reading serves a consumer that asks often, where a stale
34
+ * answer costs it one cycle; a consumer that asks rarely has to be told, or it samples
35
+ * the one moment a rollout was passing through and stands down for a whole cycle.
36
+ */
37
+ onassigned (listener: (assignment: Assignment | null) => void): () => void
38
+
25
39
  /**
26
40
  * Debt the group has run up under each key, in milliseconds. Every call adds its own
27
41
  * deltas and reads back where the group stands, so a replica reports what it alone has
@@ -45,6 +59,12 @@ declare namespace toa.core {
45
59
  routine: (signal: AbortSignal, context: unknown) => Promise<T>): Promise<T>
46
60
  }
47
61
 
62
+ /** Which of the group this replica is, and how many of them there are. */
63
+ interface Assignment {
64
+ i: number
65
+ n: number
66
+ }
67
+
48
68
  interface Factory {
49
69
  /** @param group what the replicas deciding together have in common */
50
70
  atom (group: string, options?: object): Atom
@@ -55,4 +75,5 @@ declare namespace toa.core {
55
75
  }
56
76
 
57
77
  export type Atom = toa.core.atomicity.Atom
78
+ export type Assignment = toa.core.atomicity.Assignment
58
79
  export type Factory = toa.core.atomicity.Factory
@@ -0,0 +1,13 @@
1
+ import type { Request } from './request.js'
2
+
3
+ /**
4
+ * How an endpoint is called. What it resolves to is what the operation declares — `toa types`
5
+ * writes that per operation, and these are what a hand-written context says instead.
6
+ */
7
+ export type Call<Output = any, Input = any> = (request: Request<Input>) => Promise<Output>
8
+
9
+ export type Observation<Output = any, Input = never, Entity = unknown> =
10
+ (request: Request<Input, Entity>) => Promise<Output extends unknown[] ? Output : Output | null>
11
+
12
+ export type Transition<Output = any, Input = never, Entity = unknown> =
13
+ (request: Request<Input, Entity>) => Promise<Output | null>
@@ -47,6 +47,25 @@ export interface Factory {
47
47
  receiver? (receiver: _core.Receiver, locator: _core.Locator): _core.Receiver
48
48
  }
49
49
 
50
+ /**
51
+ * What an extension puts on a component's context, as the extension states it. How it is
52
+ * presented there is the bridge's — a bash bridge has no context to put anything on — and
53
+ * what is declared here is the key and what it holds.
54
+ */
55
+ export interface Contribution {
56
+ /** the key on the context */
57
+ name: string
58
+
59
+ /** what the key holds, as TypeScript */
60
+ type?: string
61
+
62
+ /** what `type` names, by the module it comes from */
63
+ imports?: Record<string, string[]>
64
+
65
+ /** a JSON Schema to read the type from instead, where a component states one */
66
+ schema?: object
67
+ }
68
+
50
69
  export interface Aspect extends _core.Connector {
51
70
  name: string
52
71
 
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Runs on every change to an entity's state, before the contract is applied. `false` refuses
3
+ * the change as `EntityGuard`.
4
+ */
5
+ export type Guard<T, C = unknown> = (state: T, origin: T | null, context: C) => boolean
package/types/index.ts CHANGED
@@ -14,4 +14,8 @@ export type { Exception } from './exception.js'
14
14
  export { Locator } from './locator.js'
15
15
  export type { Receiver } from './receiver.js'
16
16
  export type { Message } from './message.js'
17
- export type { Request, Query, Reply, Source } from './request.js'
17
+ export type { Maybe, Request, Query, Reply, RemoteError, Source } from './request.js'
18
+ export type { Event } from './state.js'
19
+ export type { Guard } from './guard.js'
20
+ export type { Call, Observation, Transition } from './call.js'
21
+ export type { Contribution } from './extensions.js'
package/types/remote.d.ts CHANGED
@@ -5,6 +5,7 @@ export class Remote extends Component {
5
5
  }
6
6
 
7
7
  interface Explanation {
8
+ description?: string
8
9
  input: Schema | null
9
10
  output: Schema | null
10
11
  errors?: string[]
@@ -1,6 +1,10 @@
1
1
  import { Exception } from './exception.js'
2
2
 
3
- export interface Query {
3
+ /**
4
+ * What a call asks for. `Entity` is the record it is about, which narrows the projection;
5
+ * left out, any name is accepted.
6
+ */
7
+ export interface Query<Entity = any> {
4
8
  id?: string
5
9
  ids?: Array<string>
6
10
  criteria?: string
@@ -9,7 +13,7 @@ export interface Query {
9
13
  omit?: number
10
14
  limit?: number
11
15
  sort?: Array<string>
12
- projection?: Array<string>
16
+ projection?: Array<string & keyof Entity>
13
17
  version?: number
14
18
  deleted?: boolean
15
19
  }
@@ -22,15 +26,28 @@ export type Source =
22
26
  | { namespace: string, component: string, event: string }
23
27
  | { service: string }
24
28
 
25
- export interface Request {
26
- input?: any
27
- query?: Query
29
+ export interface Request<Input = any, Entity = any> {
30
+ input?: Input
31
+ query?: Query<Entity>
32
+ /** What the operation acquires, where the caller supplies it rather than the storage. */
33
+ entity?: Entity
28
34
  authentic?: boolean
29
35
  task?: boolean
30
36
  telemetry?: string // W3C traceparent
31
37
  source?: Source
32
38
  }
33
39
 
40
+ /**
41
+ * An error an operation declares and returns. A call resolves to it rather than throwing:
42
+ * only an exception is thrown.
43
+ */
44
+ export interface RemoteError<Code extends string = string> extends Error {
45
+ code: Code
46
+ }
47
+
48
+ /** What an operation returns where it may refuse: the value, or the error it refused with. */
49
+ export type Maybe<T> = T | Error
50
+
34
51
  export interface Reply {
35
52
  output?: any
36
53
  error?: object
package/types/state.d.ts CHANGED
@@ -5,12 +5,12 @@ declare namespace toa.core {
5
5
 
6
6
  namespace transition {
7
7
 
8
- type Event = {
8
+ type Event<State = Object, Trailers = Object> = {
9
9
  /** the pre-image; null when the entity did not exist before */
10
- origin: Object | null
11
- state: Object
10
+ origin: State | null
11
+ state: State
12
12
  /** out-of-band values an algorithm wrote into `state._trailers`; must be serializable */
13
- trailers?: Object
13
+ trailers?: Trailers
14
14
  input?: Object
15
15
  }
16
16
 
@@ -35,4 +35,4 @@ declare namespace toa.core {
35
35
  }
36
36
 
37
37
  export type State = toa.core.State
38
- export type Event = toa.core.transition.Event
38
+ export type Event<State = Object, Trailers = Object> = toa.core.transition.Event<State, Trailers>