@toa.io/core 1.0.0-alpha.292 → 1.0.0-alpha.299
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/package.json +10 -4
- package/transpiled/call.js +12 -0
- package/transpiled/call.js.map +1 -1
- package/transpiled/component.js +31 -4
- package/transpiled/component.js.map +1 -1
- package/transpiled/context.d.ts +5 -0
- package/transpiled/context.js +6 -0
- package/transpiled/context.js.map +1 -1
- package/transpiled/emission.d.ts +4 -2
- package/transpiled/emission.js +4 -2
- package/transpiled/emission.js.map +1 -1
- package/transpiled/entities/entity.js +34 -6
- package/transpiled/entities/entity.js.map +1 -1
- package/transpiled/entities/factory.d.ts +5 -0
- package/transpiled/entities/factory.js +6 -2
- package/transpiled/entities/factory.js.map +1 -1
- package/transpiled/event.d.ts +2 -2
- package/transpiled/event.js +6 -1
- package/transpiled/event.js.map +1 -1
- package/transpiled/exceptions.d.ts +19 -0
- package/transpiled/exceptions.js +74 -2
- package/transpiled/exceptions.js.map +1 -1
- package/transpiled/index.d.ts +2 -1
- package/transpiled/index.js +2 -1
- package/transpiled/index.js.map +1 -1
- package/transpiled/{outbox/outbox.d.ts → outbox.d.ts} +6 -7
- package/transpiled/outbox.js +424 -0
- package/transpiled/outbox.js.map +1 -0
- package/transpiled/query/options.js +1 -1
- package/transpiled/query/options.js.map +1 -1
- package/transpiled/receiver.js +14 -1
- package/transpiled/receiver.js.map +1 -1
- package/transpiled/remote.js +2 -3
- package/transpiled/remote.js.map +1 -1
- package/transpiled/state.d.ts +1 -1
- package/transpiled/trail.d.ts +49 -0
- package/transpiled/trail.js +98 -0
- package/transpiled/trail.js.map +1 -0
- package/transpiled/types/bindings.d.ts +22 -0
- package/transpiled/types/extensions.d.ts +13 -2
- package/transpiled/types/message.d.ts +5 -0
- package/transpiled/types/outbox.d.ts +29 -3
- package/transpiled/types/request.d.ts +5 -0
- package/transpiled/types/storages.d.ts +20 -0
- package/CHANGELOG.md +0 -43
- package/source/assignment.ts +0 -27
- package/source/call.ts +0 -63
- package/source/cascade.ts +0 -32
- package/source/component.ts +0 -83
- package/source/composition.ts +0 -27
- package/source/connector.ts +0 -217
- package/source/context.ts +0 -68
- package/source/contract/contract.ts +0 -23
- package/source/contract/index.ts +0 -2
- package/source/contract/reply.ts +0 -56
- package/source/contract/request.ts +0 -140
- package/source/contract/schemas.ts +0 -39
- package/source/discovery.ts +0 -53
- package/source/effect.ts +0 -14
- package/source/emission.ts +0 -21
- package/source/entities/changeset.ts +0 -37
- package/source/entities/entity.ts +0 -137
- package/source/entities/factory.ts +0 -52
- package/source/entities/index.ts +0 -4
- package/source/entities/newid.ts +0 -9
- package/source/entities/set.ts +0 -27
- package/source/event.ts +0 -57
- package/source/exceptions.ts +0 -133
- package/source/exposition.ts +0 -33
- package/source/guard.ts +0 -16
- package/source/index.ts +0 -36
- package/source/locator.ts +0 -37
- package/source/observation.ts +0 -16
- package/source/operation.ts +0 -139
- package/source/outbox/index.ts +0 -1
- package/source/outbox/outbox.ts +0 -341
- package/source/query/criteria.ts +0 -90
- package/source/query/options.ts +0 -35
- package/source/query.ts +0 -67
- package/source/receiver.ts +0 -120
- package/source/reflection.ts +0 -20
- package/source/remote.ts +0 -20
- package/source/state.ts +0 -179
- package/source/transition.ts +0 -79
- package/source/transmission.ts +0 -38
- package/source/types/atomicity.ts +0 -63
- package/source/types/bindings.ts +0 -59
- package/source/types/bridges.ts +0 -54
- package/source/types/extensions.ts +0 -89
- package/source/types/index.ts +0 -14
- package/source/types/message.ts +0 -5
- package/source/types/operations.ts +0 -31
- package/source/types/outbox.ts +0 -35
- package/source/types/receiver.ts +0 -10
- package/source/types/request.ts +0 -54
- package/source/types/state.ts +0 -9
- package/source/types/storages.ts +0 -134
- package/source/unmanaged.ts +0 -8
- package/test/call.fixtures.js +0 -25
- package/test/call.test.js +0 -79
- package/test/component.fixtures.js +0 -18
- package/test/component.test.js +0 -57
- package/test/connector.fixtures.js +0 -56
- package/test/connector.test.js +0 -266
- package/test/context.fixtures.js +0 -14
- package/test/context.test.js +0 -40
- package/test/contract/conditions.test.js +0 -32
- package/test/contract/contract.fixtures.js +0 -28
- package/test/contract/request.test.js +0 -196
- package/test/discovery.test.js +0 -71
- package/test/emission.fixtures.js +0 -13
- package/test/emission.test.js +0 -60
- package/test/entities/entity.fixtures.js +0 -24
- package/test/entities/entity.test.js +0 -137
- package/test/entities/factory.fixtures.js +0 -31
- package/test/entities/factory.test.js +0 -99
- package/test/entities/set.fixtures.js +0 -9
- package/test/entities/set.test.js +0 -13
- package/test/event.fixtures.js +0 -24
- package/test/event.test.js +0 -162
- package/test/locator.test.js +0 -120
- package/test/outbox.test.js +0 -202
- package/test/query.fixtures.js +0 -96
- package/test/query.test.js +0 -183
- package/test/receiver.fixtures.js +0 -20
- package/test/receiver.test.js +0 -144
- package/test/reflection.test.js +0 -31
- package/test/state.fixtures.js +0 -49
- package/test/state.test.js +0 -162
- package/test/transmission.fixtures.js +0 -13
- package/test/transmission.test.js +0 -77
- package/transpiled/outbox/index.d.ts +0 -1
- package/transpiled/outbox/index.js +0 -2
- package/transpiled/outbox/index.js.map +0 -1
- package/transpiled/outbox/outbox.js +0 -268
- package/transpiled/outbox/outbox.js.map +0 -1
- package/tsconfig.json +0 -10
- package/tsconfig.tsbuildinfo +0 -1
package/source/receiver.ts
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { console, decode, run, type SpanOptions } from 'openspan'
|
|
2
|
-
import { add } from '@toa.io/generic'
|
|
3
|
-
import { Connector } from './connector.js'
|
|
4
|
-
import type { Component } from './component.js'
|
|
5
|
-
import type { Receiver as Bridge } from './types/bridges.js'
|
|
6
|
-
import type { Message } from './types/message.js'
|
|
7
|
-
import type { Request, Source } from './types/request.js'
|
|
8
|
-
|
|
9
|
-
export interface Definition {
|
|
10
|
-
conditioned?: boolean
|
|
11
|
-
adaptive?: boolean
|
|
12
|
-
operation: string
|
|
13
|
-
label?: string
|
|
14
|
-
destination?: string
|
|
15
|
-
arguments?: unknown[]
|
|
16
|
-
origin?: Source
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class Receiver extends Connector {
|
|
20
|
-
readonly #conditioned: boolean | undefined
|
|
21
|
-
readonly #adaptive: boolean | undefined
|
|
22
|
-
readonly #endpoint: string
|
|
23
|
-
readonly #label: string
|
|
24
|
-
readonly #destination: string
|
|
25
|
-
readonly #arguments: unknown[] | undefined
|
|
26
|
-
readonly #origin: Source | undefined
|
|
27
|
-
readonly #local: Component
|
|
28
|
-
readonly #bridge: Bridge | undefined
|
|
29
|
-
readonly #delivery: SpanOptions
|
|
30
|
-
readonly #processing: SpanOptions
|
|
31
|
-
|
|
32
|
-
public constructor(definition: Definition, local: Component, bridge?: Bridge) {
|
|
33
|
-
super()
|
|
34
|
-
|
|
35
|
-
const { conditioned, adaptive, operation } = definition
|
|
36
|
-
|
|
37
|
-
this.#conditioned = conditioned
|
|
38
|
-
this.#adaptive = adaptive
|
|
39
|
-
this.#endpoint = operation
|
|
40
|
-
this.#label = definition.label ?? operation
|
|
41
|
-
this.#destination = definition.destination ?? this.#label
|
|
42
|
-
this.#arguments = definition.arguments
|
|
43
|
-
this.#origin = definition.origin
|
|
44
|
-
|
|
45
|
-
this.#local = local
|
|
46
|
-
this.#bridge = bridge
|
|
47
|
-
|
|
48
|
-
this.depends(local)
|
|
49
|
-
if (bridge !== undefined) this.depends(bridge)
|
|
50
|
-
|
|
51
|
-
/*
|
|
52
|
-
* The delivery span is created on behalf of the messaging destination, so that
|
|
53
|
-
* each consumer forms its own complete producer/consumer pair, and service graphs
|
|
54
|
-
* display fan-out correctly: producer -> destination -> each consumer
|
|
55
|
-
* (Tempo pairs spans one-to-one, thus multiple consumers can not pair
|
|
56
|
-
* with a single producer span, see grafana/tempo#5408)
|
|
57
|
-
*/
|
|
58
|
-
this.#delivery = {
|
|
59
|
-
name: `${this.#label} deliver`,
|
|
60
|
-
kind: 'producer',
|
|
61
|
-
service: this.#destination,
|
|
62
|
-
attributes: { 'messaging.destination.name': this.#destination }
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
this.#processing = {
|
|
66
|
-
name: `${this.#label} process`,
|
|
67
|
-
kind: 'consumer',
|
|
68
|
-
service: local.locator.id,
|
|
69
|
-
attributes: { 'messaging.destination.name': this.#destination }
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** @hot */
|
|
74
|
-
public async receive(message: Message): Promise<void> {
|
|
75
|
-
const { payload, telemetry, ...extensions } = message
|
|
76
|
-
|
|
77
|
-
if (this.#conditioned === true && (await this.#bridge?.condition(payload)) === false)
|
|
78
|
-
return
|
|
79
|
-
|
|
80
|
-
const request = await this.#request(payload)
|
|
81
|
-
|
|
82
|
-
add(request, extensions)
|
|
83
|
-
|
|
84
|
-
// set after `add`, so that a message field can not spoof the origin
|
|
85
|
-
if (this.#origin !== undefined) request.source = this.#origin
|
|
86
|
-
|
|
87
|
-
// continue the trace from the producer span
|
|
88
|
-
const remote = telemetry === undefined ? null : decode(telemetry)
|
|
89
|
-
const task = async (): Promise<void> => {
|
|
90
|
-
await this.#process(request)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (remote === null) await task()
|
|
94
|
-
else await run(remote, task)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async #process(request: Request): Promise<void> {
|
|
98
|
-
return console.span(this.#delivery, async () =>
|
|
99
|
-
console.span(this.#processing, async () => {
|
|
100
|
-
try {
|
|
101
|
-
await this.#local.invoke(this.#endpoint, request)
|
|
102
|
-
} catch (error) {
|
|
103
|
-
console.error('Receiver error', {
|
|
104
|
-
component: this.#local.locator.id,
|
|
105
|
-
endpoint: this.#endpoint,
|
|
106
|
-
error
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
throw error
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async #request(payload: object): Promise<Request> {
|
|
116
|
-
return this.#adaptive === true
|
|
117
|
-
? await this.#bridge!.request(payload, ...(this.#arguments ?? []))
|
|
118
|
-
: { input: payload }
|
|
119
|
-
}
|
|
120
|
-
}
|
package/source/reflection.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Connector } from './connector.js'
|
|
2
|
-
|
|
3
|
-
/** What a reflection reads, once, as it connects. */
|
|
4
|
-
export type Source<T = any> = () => Promise<T>
|
|
5
|
-
|
|
6
|
-
export class Reflection<T = any> extends Connector {
|
|
7
|
-
public value: T | undefined
|
|
8
|
-
|
|
9
|
-
readonly #source: Source<T>
|
|
10
|
-
|
|
11
|
-
public constructor(source: Source<T>) {
|
|
12
|
-
super()
|
|
13
|
-
|
|
14
|
-
this.#source = source
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
protected override async open(): Promise<void> {
|
|
18
|
-
this.value = await this.#source()
|
|
19
|
-
}
|
|
20
|
-
}
|
package/source/remote.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert'
|
|
2
|
-
import { Component } from './component.js'
|
|
3
|
-
import type { Invocable } from './component.js'
|
|
4
|
-
|
|
5
|
-
/** A call knows what its endpoint declares, which is what an explanation is read from. */
|
|
6
|
-
export interface Explicable extends Invocable {
|
|
7
|
-
explain(): any
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class Remote extends Component<Explicable> {
|
|
11
|
-
protected override kind = 'client' as const
|
|
12
|
-
|
|
13
|
-
public explain(endpoint: string): any {
|
|
14
|
-
if (!(endpoint in this.operations))
|
|
15
|
-
// `assert.fail`, not `assert.ok`: the message is built only when it is needed
|
|
16
|
-
assert.fail(`Endpoint '${endpoint}' is not provided by '${this.locator.id}'`)
|
|
17
|
-
|
|
18
|
-
return this.operations[endpoint].explain()
|
|
19
|
-
}
|
|
20
|
-
}
|
package/source/state.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { EntitySet } from './entities/set.js'
|
|
2
|
-
import { StatePreconditionException, StateNotFoundException } from './exceptions.js'
|
|
3
|
-
import type { Readable } from 'node:stream'
|
|
4
|
-
import type { Factory } from './entities/factory.js'
|
|
5
|
-
import type { Entity } from './entities/entity.js'
|
|
6
|
-
import type { Changeset } from './entities/changeset.js'
|
|
7
|
-
import type { Outbox } from './outbox/outbox.js'
|
|
8
|
-
import type { Row } from './types/outbox.js'
|
|
9
|
-
import type { Query, Record, Storage } from './types/storages.js'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* What an operation acquires and commits. One method per scope a manifest may declare, which
|
|
13
|
-
* is how an operation reaches the one it was declared with.
|
|
14
|
-
*/
|
|
15
|
-
export class State {
|
|
16
|
-
public readonly storage: Storage
|
|
17
|
-
|
|
18
|
-
readonly #entities: Factory
|
|
19
|
-
readonly #outbox: Outbox | undefined
|
|
20
|
-
|
|
21
|
-
/** whether an entity exists by virtue of the identity it is looked up by */
|
|
22
|
-
readonly #associated: boolean
|
|
23
|
-
|
|
24
|
-
// eslint-disable-next-line max-params
|
|
25
|
-
public constructor(
|
|
26
|
-
storage: Storage,
|
|
27
|
-
entities: Factory,
|
|
28
|
-
outbox: Outbox | undefined,
|
|
29
|
-
associated?: boolean
|
|
30
|
-
) {
|
|
31
|
-
this.storage = storage
|
|
32
|
-
|
|
33
|
-
this.#entities = entities
|
|
34
|
-
this.#outbox = outbox
|
|
35
|
-
this.#associated = associated === true
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public init(id?: string): Entity {
|
|
39
|
-
return this.#entities.init(id)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public fit(values: object): void {
|
|
43
|
-
this.#entities.fit(values)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** scope `object` */
|
|
47
|
-
public async object(query: Query, mutable = true): Promise<Entity | null> {
|
|
48
|
-
const record = await this.storage.get(query)
|
|
49
|
-
|
|
50
|
-
if (record !== null) return this.#entities.object(record, mutable)
|
|
51
|
-
|
|
52
|
-
// an associated entity is whatever the identity it is looked up by names, so a miss on
|
|
53
|
-
// that identity alone is a blank rather than an absence
|
|
54
|
-
if (
|
|
55
|
-
this.#associated &&
|
|
56
|
-
query.id !== undefined &&
|
|
57
|
-
query.criteria === undefined &&
|
|
58
|
-
query.version === undefined
|
|
59
|
-
)
|
|
60
|
-
return this.init(query.id)
|
|
61
|
-
|
|
62
|
-
if (query.version !== undefined) throw new StatePreconditionException()
|
|
63
|
-
|
|
64
|
-
return null
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** scope `objects` */
|
|
68
|
-
public async objects(query: Query, mutable = true): Promise<EntitySet> {
|
|
69
|
-
const recordset = await this.storage.find(query)
|
|
70
|
-
const ids = query.ids
|
|
71
|
-
|
|
72
|
-
const missing = this.#associated && ids !== undefined && recordset.length < ids.length
|
|
73
|
-
|
|
74
|
-
const init = missing
|
|
75
|
-
? ids.filter((id) => !recordset.some((record) => record.id === id))
|
|
76
|
-
: undefined
|
|
77
|
-
|
|
78
|
-
return this.#entities.objects(recordset, init, mutable)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** scope `stream` */
|
|
82
|
-
public async stream(query: Query): Promise<Readable> {
|
|
83
|
-
return this.storage.stream(query)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** scope `changeset` */
|
|
87
|
-
public changeset(query: Query): Changeset {
|
|
88
|
-
return this.#entities.changeset(query)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** scope `none`: an operation that acquires nothing still asks for its scope */
|
|
92
|
-
public none(): null {
|
|
93
|
-
return null
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** get-or-create, in one indivisible step */
|
|
97
|
-
public async ensure(
|
|
98
|
-
query: Query | undefined,
|
|
99
|
-
properties: object,
|
|
100
|
-
input?: object
|
|
101
|
-
): Promise<Entity> {
|
|
102
|
-
const object = this.#entities.init()
|
|
103
|
-
const blank = object.get()
|
|
104
|
-
|
|
105
|
-
Object.assign(blank, properties)
|
|
106
|
-
object.set(blank)
|
|
107
|
-
|
|
108
|
-
const row = this.#outbox?.row(object.event(input))
|
|
109
|
-
const record = await this.storage.ensure(query, properties, object.get(), row)
|
|
110
|
-
|
|
111
|
-
// whatever came back under another id was already there, and an effect never commits it
|
|
112
|
-
if (record.id !== blank.id) return this.#entities.object(record, NOT_MUTABLE)
|
|
113
|
-
|
|
114
|
-
await this.#publish(row)
|
|
115
|
-
|
|
116
|
-
return object
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public async commit(state: Entity | EntitySet, input?: object): Promise<boolean> {
|
|
120
|
-
if (state instanceof EntitySet) return this.massCommit(state, input)
|
|
121
|
-
|
|
122
|
-
// the row is built before the write so that the storage can commit it in the same
|
|
123
|
-
// transaction, closing the window this used to have
|
|
124
|
-
const row = this.#outbox?.row(state.event(input))
|
|
125
|
-
const ok = await this.storage.store(state.get(), row)
|
|
126
|
-
|
|
127
|
-
if (ok) await this.#publish(row)
|
|
128
|
-
|
|
129
|
-
return ok
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
public async massCommit(state: EntitySet, input?: object): Promise<boolean> {
|
|
133
|
-
const outbox = this.#outbox
|
|
134
|
-
|
|
135
|
-
const rows =
|
|
136
|
-
outbox === undefined
|
|
137
|
-
? undefined
|
|
138
|
-
: state.events(input).map((event) => outbox.row(event))
|
|
139
|
-
|
|
140
|
-
const ok = await this.storage.massStore(state.get(), rows)
|
|
141
|
-
|
|
142
|
-
if (ok && rows !== undefined)
|
|
143
|
-
await Promise.all(rows.map(async (row) => this.#publish(row)))
|
|
144
|
-
|
|
145
|
-
return ok
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public async apply(state: Changeset, input?: object): Promise<Record> {
|
|
149
|
-
// an assignment's images are the write's own, so the storage fills them in
|
|
150
|
-
const row = this.#outbox?.row({ input })
|
|
151
|
-
const result = await this.storage.upsert(state.query, state.export(), row)
|
|
152
|
-
|
|
153
|
-
if (result === null)
|
|
154
|
-
throw state.query.version === undefined
|
|
155
|
-
? new StateNotFoundException()
|
|
156
|
-
: new StatePreconditionException()
|
|
157
|
-
|
|
158
|
-
if (row !== undefined) {
|
|
159
|
-
// a storage that does not know how leaves the event with what it was given
|
|
160
|
-
row.event.state ??= result
|
|
161
|
-
row.event.origin ??= null
|
|
162
|
-
|
|
163
|
-
await this.#publish(row)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return result
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Without a durable outbox this is the emission itself, and the operation waits for it;
|
|
171
|
-
* with one the row is already committed and this returns at once.
|
|
172
|
-
*/
|
|
173
|
-
async #publish(row: Row | undefined): Promise<void> {
|
|
174
|
-
if (row !== undefined) await this.#outbox?.publish(row)
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** an effect never commits what `ensure` hands it, see `Effect` */
|
|
179
|
-
const NOT_MUTABLE = false
|
package/source/transition.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { retry } from '@toa.io/generic'
|
|
2
|
-
import { Operation } from './operation.js'
|
|
3
|
-
import { StateConcurrencyException, StateNotFoundException } from './exceptions.js'
|
|
4
|
-
import type { Contracts, Definition, Store } from './operation.js'
|
|
5
|
-
import type { Cascade } from './cascade.js'
|
|
6
|
-
import type { State } from './state.js'
|
|
7
|
-
import type { Query as Translator } from './query.js'
|
|
8
|
-
import type { Entity } from './entities/entity.js'
|
|
9
|
-
|
|
10
|
-
export class Transition extends Operation {
|
|
11
|
-
/** a transition is the only operation that commits */
|
|
12
|
-
protected override mutable = true
|
|
13
|
-
|
|
14
|
-
readonly #concurrency: string | undefined
|
|
15
|
-
|
|
16
|
-
// eslint-disable-next-line max-params
|
|
17
|
-
public constructor(
|
|
18
|
-
cascade: Cascade,
|
|
19
|
-
scope: State,
|
|
20
|
-
contract: Contracts,
|
|
21
|
-
query: Translator,
|
|
22
|
-
definition: Definition
|
|
23
|
-
) {
|
|
24
|
-
super(cascade, scope, contract, query, definition)
|
|
25
|
-
|
|
26
|
-
this.#concurrency = definition.concurrency
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
protected override async process(store: Store): Promise<any> {
|
|
30
|
-
return retry(async (retry) => this.#retry(store, retry), RETRY)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
protected override async acquire(store: Store): Promise<void> {
|
|
34
|
-
const { request } = store
|
|
35
|
-
|
|
36
|
-
store.scope =
|
|
37
|
-
request.query === undefined ? this.scope.init() : await this.query(request.query)
|
|
38
|
-
|
|
39
|
-
const entity = store.scope as Entity | null
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
entity === null ||
|
|
43
|
-
(entity.deleted && (request.query as any)?.options?.deleted !== true)
|
|
44
|
-
)
|
|
45
|
-
throw new StateNotFoundException()
|
|
46
|
-
|
|
47
|
-
store.state = entity.get()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
protected override async commit(store: Store): Promise<void> {
|
|
51
|
-
const { scope, state, reply, retry } = store
|
|
52
|
-
|
|
53
|
-
if (reply.error !== undefined) return
|
|
54
|
-
|
|
55
|
-
const entity = scope as Entity
|
|
56
|
-
|
|
57
|
-
entity.set(state)
|
|
58
|
-
|
|
59
|
-
const result = await this.scope.commit(entity, store.request.input)
|
|
60
|
-
|
|
61
|
-
if (result !== false) return
|
|
62
|
-
|
|
63
|
-
if (this.#concurrency === 'retry') await retry?.()
|
|
64
|
-
else throw new StateConcurrencyException()
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async #retry(store: Store, retry: () => Promise<any>): Promise<any> {
|
|
68
|
-
store.retry = retry
|
|
69
|
-
|
|
70
|
-
return super.process(store)
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const RETRY = {
|
|
75
|
-
base: 10,
|
|
76
|
-
max: 5000,
|
|
77
|
-
dispersion: 1,
|
|
78
|
-
retries: 32
|
|
79
|
-
}
|
package/source/transmission.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Connector } from './connector.js'
|
|
2
|
-
import { TransmissionException } from './exceptions.js'
|
|
3
|
-
import type { Consumer } from './types/bindings.js'
|
|
4
|
-
import type { Request } from './types/request.js'
|
|
5
|
-
|
|
6
|
-
export class Transmission extends Connector {
|
|
7
|
-
readonly #bindings: Consumer[]
|
|
8
|
-
|
|
9
|
-
public constructor(bindings: Consumer[]) {
|
|
10
|
-
super()
|
|
11
|
-
|
|
12
|
-
this.#bindings = bindings
|
|
13
|
-
this.depends(bindings)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public async request(request: Request): Promise<any> {
|
|
17
|
-
let reply: any = false
|
|
18
|
-
let i = 0
|
|
19
|
-
|
|
20
|
-
while (reply === false && i < this.#bindings.length) {
|
|
21
|
-
const binding = this.#bindings[i]
|
|
22
|
-
|
|
23
|
-
i++
|
|
24
|
-
|
|
25
|
-
if (request?.task === true) {
|
|
26
|
-
if (binding.task === undefined) continue
|
|
27
|
-
|
|
28
|
-
await binding.task(request)
|
|
29
|
-
reply = null
|
|
30
|
-
} else reply = await binding.request(request)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (reply === false)
|
|
34
|
-
throw new TransmissionException(`All (${this.#bindings.length}) bindings rejected.`)
|
|
35
|
-
|
|
36
|
-
return reply
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import type { Connector } from '../connector.js'
|
|
2
|
-
|
|
3
|
-
/** Which of the group this replica is, and how many of them there are. */
|
|
4
|
-
export interface Assignment {
|
|
5
|
-
i: number
|
|
6
|
-
n: number
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* What one group of replicas decides together, in one place. The decisions here are the ones
|
|
11
|
-
* processes cannot arrange by talking to each other: they need a single arbiter and a step
|
|
12
|
-
* indivisible from its point of view.
|
|
13
|
-
*/
|
|
14
|
-
export interface Atom extends Connector {
|
|
15
|
-
/**
|
|
16
|
-
* An exclusive claim on slots of `0..total`: while this replica holds one, no other
|
|
17
|
-
* replica of the group does. Answered from memory, so it costs nothing to ask.
|
|
18
|
-
*
|
|
19
|
-
* `null` while this replica owns nothing — after a restart, during a rollout, or while
|
|
20
|
-
* coordination is unreachable. Whoever asks must be able to stand down: acting on a claim
|
|
21
|
-
* that cannot be supported is a different guarantee, not a degraded one.
|
|
22
|
-
*/
|
|
23
|
-
slots(total: number): number[] | null
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Calls `listener` with the assignment this replica holds, and again whenever it changes —
|
|
27
|
-
* one arrived, was lost, or the group resized. Answers with what removes the listener
|
|
28
|
-
* again.
|
|
29
|
-
*
|
|
30
|
-
* A change and not a heartbeat: a group that stays as it is never calls back. It is called
|
|
31
|
-
* once as it is added, with the claim as it stands.
|
|
32
|
-
*/
|
|
33
|
-
onassigned(listener: (assignment: Assignment | null) => void): () => void
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Debt the group has run up under each key, in milliseconds. Every call adds its own deltas
|
|
37
|
-
* and reads back where the group stands, so a replica reports what it alone has spent and
|
|
38
|
-
* still decides on what all of them have.
|
|
39
|
-
*
|
|
40
|
-
* Rejects where there is nothing to arbitrate through.
|
|
41
|
-
*/
|
|
42
|
-
meter(keys: string[], deltas: number[]): Promise<number[]>
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Runs `routine` holding `keys`, and while it holds them no other replica of the group
|
|
46
|
-
* does. Waits for as long as it takes to acquire them.
|
|
47
|
-
*
|
|
48
|
-
* The lease is extended for as long as the routine runs. An extension that fails aborts the
|
|
49
|
-
* signal the routine is given, which is the only way it learns it no longer holds what it
|
|
50
|
-
* is working under.
|
|
51
|
-
*
|
|
52
|
-
* Rejects where there is nothing to arbitrate through.
|
|
53
|
-
*/
|
|
54
|
-
lock<T>(
|
|
55
|
-
keys: string | string[],
|
|
56
|
-
routine: (signal: AbortSignal, context: unknown) => Promise<T>
|
|
57
|
-
): Promise<T>
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface Factory {
|
|
61
|
-
/** @param group what the replicas deciding together have in common */
|
|
62
|
-
atom(group: string, options?: { interval?: number }): Atom
|
|
63
|
-
}
|
package/source/types/bindings.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream'
|
|
2
|
-
import type { Connector } from '../connector.js'
|
|
3
|
-
import type { Locator } from '../locator.js'
|
|
4
|
-
import type { Component } from '../component.js'
|
|
5
|
-
import type { Receiver } from './receiver.js'
|
|
6
|
-
import type { Message } from './message.js'
|
|
7
|
-
import type { Reply, Request } from './request.js'
|
|
8
|
-
|
|
9
|
-
/** What a binding module exports beside its factory. */
|
|
10
|
-
export interface Properties {
|
|
11
|
-
/** delivers asynchronously; an event's binding must */
|
|
12
|
-
async?: boolean
|
|
13
|
-
/** in-process: its producers are connected first and torn down last */
|
|
14
|
-
local?: boolean
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface Consumer extends Connector {
|
|
18
|
-
/**
|
|
19
|
-
* `false` says this binding does not carry the endpoint, and the transmission tries the
|
|
20
|
-
* next one. A `Readable` is a streamed reply.
|
|
21
|
-
*/
|
|
22
|
-
request(request: Request): Promise<Reply | Readable | false>
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Absent where the binding cannot enqueue — a transmission skips a binding that offers
|
|
26
|
-
* no `task` rather than failing over it.
|
|
27
|
-
*/
|
|
28
|
-
task?(request: Request): Promise<void>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface Emitter extends Connector {
|
|
32
|
-
emit(message: Message): Promise<void>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface Broadcast<L extends string = string> extends Connector {
|
|
36
|
-
transmit<T>(label: L, payload: T): Promise<void>
|
|
37
|
-
|
|
38
|
-
receive<T>(label: L, callback: (payload: T) => void | Promise<void>): Promise<void>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface Factory {
|
|
42
|
-
producer(locator: Locator, endpoints: string[], component: Component): Connector
|
|
43
|
-
|
|
44
|
-
consumer(locator: Locator, endpoint: string): Consumer
|
|
45
|
-
|
|
46
|
-
/** only the binding an event declares is asked for one */
|
|
47
|
-
emitter?(locator: Locator, label: string): Emitter
|
|
48
|
-
|
|
49
|
-
/** `group` is absent for an exclusive subscription */
|
|
50
|
-
// eslint-disable-next-line max-params
|
|
51
|
-
receiver?(
|
|
52
|
-
locator: Locator,
|
|
53
|
-
label: string,
|
|
54
|
-
group: string | undefined,
|
|
55
|
-
receiver: Receiver
|
|
56
|
-
): Connector
|
|
57
|
-
|
|
58
|
-
broadcast?(name: string, group?: string): Broadcast
|
|
59
|
-
}
|
package/source/types/bridges.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { Readable } from 'node:stream'
|
|
2
|
-
import type { Connector } from '../connector.js'
|
|
3
|
-
import type { Context } from '../context.js'
|
|
4
|
-
import type { Event as StateEvent } from './state.js'
|
|
5
|
-
import type { Reply, Request } from './request.js'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* What runs one endpoint. `mount` and `unmount` are the component author's, called by the
|
|
9
|
-
* bridge as it opens and closes; core only ever asks for `execute`.
|
|
10
|
-
*/
|
|
11
|
-
export interface Algorithm extends Connector {
|
|
12
|
-
execute(input: any, scope?: object | object[]): Promise<Reply | Readable>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface Event extends Connector {
|
|
16
|
-
condition(event: StateEvent): Promise<boolean>
|
|
17
|
-
|
|
18
|
-
payload(event: StateEvent): Promise<object>
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface Receiver extends Connector {
|
|
22
|
-
condition(payload: object): Promise<boolean>
|
|
23
|
-
|
|
24
|
-
/** the trailing arguments are what the receiver declaration named */
|
|
25
|
-
request(payload: object, ...args: unknown[]): Promise<Request>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Runs on every change to an entity's state, before the contract is applied. */
|
|
29
|
-
export interface Guard {
|
|
30
|
-
fit(state: object, origin: object | null): boolean
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** Connectors whose lifecycle moments are the component's. */
|
|
34
|
-
export interface RunCommands {
|
|
35
|
-
preflight?: Connector
|
|
36
|
-
settle?: Connector
|
|
37
|
-
dispose?: Connector
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface Factory {
|
|
41
|
-
algorithm(
|
|
42
|
-
path: string,
|
|
43
|
-
endpoint: string,
|
|
44
|
-
context: Context
|
|
45
|
-
): Algorithm | Promise<Algorithm>
|
|
46
|
-
|
|
47
|
-
event?(path: string, label: string, context: Context): Event | Promise<Event>
|
|
48
|
-
|
|
49
|
-
receiver?(path: string, label: string): Receiver | Promise<Receiver>
|
|
50
|
-
|
|
51
|
-
guard?(path: string, name: string, context: Context): Guard | Promise<Guard>
|
|
52
|
-
|
|
53
|
-
rc?(path: string, context: Context): Promise<RunCommands | undefined>
|
|
54
|
-
}
|