@toa.io/core 1.0.0-alpha.8 → 1.0.0-alpha.81

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/src/operation.js CHANGED
@@ -1,7 +1,9 @@
1
1
  'use strict'
2
2
 
3
+ const { console } = require('openspan')
3
4
  const { Connector } = require('./connector')
4
- const { SystemException } = require('./exceptions')
5
+ const { SystemException, RequestContractException } = require('./exceptions')
6
+ const { Readable } = require('node:stream')
5
7
 
6
8
  class Operation extends Connector {
7
9
  scope
@@ -26,8 +28,15 @@ class Operation extends Connector {
26
28
 
27
29
  async invoke (request) {
28
30
  try {
29
- if (request.authentic !== true) this.#contracts.request.fit(request)
30
- if ('query' in request) request.query = this.#query.parse(request.query)
31
+ if (request.authentic !== true)
32
+ this.#contracts.request.fit(request)
33
+
34
+ if ('query' in request)
35
+ request.query = this.#query.parse(request.query)
36
+
37
+ // validate entity
38
+ if ('entity' in request)
39
+ this.scope.fit(request.entity)
31
40
 
32
41
  const store = { request }
33
42
 
@@ -47,14 +56,24 @@ class Operation extends Connector {
47
56
  return store.reply
48
57
  }
49
58
 
50
- async acquire () {}
59
+ async acquire (store) {
60
+ if (this.#scope === 'none')
61
+ return
62
+
63
+ const scope = await this.query(store.request.query)
64
+ const raw = scope === null || scope instanceof Readable
65
+
66
+ store.scope = scope
67
+ store.state = raw ? scope : scope.get()
68
+ }
51
69
 
52
70
  async run (store) {
53
71
  const { request, state } = store
54
- // noinspection UnnecessaryLocalVariableJS
55
- const reply = await this.#cascade.run(request.input, state) || {}
72
+ const reply = await this.#cascade.run(request.input, state)
56
73
 
57
- // this.#contracts.reply.fit(reply)
74
+ // validate reply only on local environments
75
+ if (process.env.TOA_ENV === 'local' && !(reply instanceof Readable))
76
+ this.#contracts.reply.fit(reply)
58
77
 
59
78
  store.reply = reply
60
79
  }
@@ -62,6 +81,9 @@ class Operation extends Connector {
62
81
  async commit () {}
63
82
 
64
83
  async query (query) {
84
+ if (query === undefined)
85
+ throw new RequestContractException('Request query is required')
86
+
65
87
  return this.scope[this.#scope](query)
66
88
  }
67
89
  }
package/src/receiver.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { console } = require('openspan')
3
4
  const { add } = require('@toa.io/generic')
4
5
  const { Connector } = require('./connector')
5
6
 
@@ -16,18 +17,13 @@ class Receiver extends Connector {
16
17
  /** @type {string} */
17
18
  #endpoint
18
19
 
19
- /** @type {toa.core.Component} */
20
+ /** @type {unknown[]} */
21
+ #arguments
22
+
20
23
  #local
21
24
 
22
- /** @type {toa.core.bridges.Receiver} */
23
25
  #bridge
24
26
 
25
- /**
26
- *
27
- * @param {toa.norm.component.Receiver} definition
28
- * @param {toa.core.Component} local
29
- * @param {toa.core.bridges.Receiver} bridge
30
- */
31
27
  constructor (definition, local, bridge) {
32
28
  super()
33
29
 
@@ -36,6 +32,7 @@ class Receiver extends Connector {
36
32
  this.#conditioned = conditioned
37
33
  this.#adaptive = adaptive
38
34
  this.#endpoint = operation
35
+ this.#arguments = definition.arguments
39
36
 
40
37
  this.#local = local
41
38
  this.#bridge = bridge
@@ -54,11 +51,17 @@ class Receiver extends Connector {
54
51
 
55
52
  add(request, extensions)
56
53
 
57
- await this.#local.invoke(this.#endpoint, request)
54
+ try {
55
+ await this.#local.invoke(this.#endpoint, request)
56
+ } catch (error) {
57
+ console.error('Receiver error', error)
58
+
59
+ throw error
60
+ }
58
61
  }
59
62
 
60
63
  async #request (payload) {
61
- return this.#adaptive ? await this.#bridge.request(payload) : { input: payload }
64
+ return this.#adaptive ? await this.#bridge.request(payload, ...(this.#arguments ?? [])) : { input: payload }
62
65
  }
63
66
  }
64
67
 
package/src/remote.js CHANGED
@@ -1,16 +1,14 @@
1
1
  'use strict'
2
2
 
3
- const { console } = require('@toa.io/console')
4
-
3
+ const assert = require('node:assert')
5
4
  const { Component } = require('./component')
6
5
 
7
6
  class Remote extends Component {
8
- async open () {
9
- console.info(`Remote '${this.locator.id}' connected`)
10
- }
7
+ explain (endpoint) {
8
+ assert.ok(endpoint in this.operations,
9
+ `Endpoint '${endpoint}' is not provided by '${this.locator.id}'`)
11
10
 
12
- async dispose () {
13
- console.info(`Remote '${this.locator.id}' disconnected`)
11
+ return this.operations[endpoint].explain()
14
12
  }
15
13
  }
16
14
 
package/src/state.js CHANGED
@@ -1,61 +1,83 @@
1
1
  'use strict'
2
2
 
3
- const { empty } = require('@toa.io/generic')
4
-
5
- const {
6
- StatePreconditionException,
7
- StateNotFoundException
8
- } = require('./exceptions')
3
+ const { empty, newid } = require('@toa.io/generic')
4
+ const { StatePreconditionException, StateNotFoundException } = require('./exceptions')
9
5
 
10
6
  class State {
11
- #dependent
7
+ #associated
12
8
  #storage
13
- #entity
9
+ #entities
14
10
  #emission
15
11
 
16
- constructor (storage, entity, emission, dependent) {
12
+ constructor (storage, entity, emission, associated) {
17
13
  this.#storage = storage
18
- this.#entity = entity
14
+ this.#entities = entity
19
15
  this.#emission = emission
20
- this.#dependent = dependent === true
16
+ this.#associated = associated === true
21
17
  }
22
18
 
23
19
  init (id) {
24
- return this.#entity.init(id)
20
+ return this.#entities.init(id)
21
+ }
22
+
23
+ fit (values) {
24
+ return this.#entities.fit(values)
25
25
  }
26
26
 
27
27
  async object (query) {
28
28
  const record = await this.#storage.get(query)
29
29
 
30
30
  if (record === null) {
31
- if (this.#dependent && query.id !== undefined && query.version === undefined) {
31
+ if (this.#associated && query.id !== undefined && query.version === undefined)
32
32
  return this.init(query.id)
33
- } else if (query.version !== undefined) throw new StatePreconditionException()
34
- }
33
+ else if (query.version !== undefined)
34
+ throw new StatePreconditionException()
35
35
 
36
- if (record === null) {
37
36
  return null
38
- } else {
39
- return this.#entity.object(record)
40
- }
37
+ } else
38
+ return this.#entities.object(record)
41
39
  }
42
40
 
43
41
  async objects (query) {
44
42
  const recordset = await this.#storage.find(query)
45
43
 
46
- return this.#entity.objects(recordset)
44
+ return this.#entities.objects(recordset)
45
+ }
46
+
47
+ async stream (query) {
48
+ return this.#storage.stream(query)
47
49
  }
48
50
 
49
51
  changeset (query) {
50
- return this.#entity.changeset(query)
52
+ return this.#entities.changeset(query)
51
53
  }
52
54
 
53
55
  none () {
54
56
  return null
55
57
  }
56
58
 
57
- async commit (state) {
58
- const event = state.event()
59
+ async ensure (query, properties, input) {
60
+ const object = this.#entities.init()
61
+ const blank = object.get()
62
+
63
+ Object.assign(blank, properties)
64
+
65
+ object.set(blank)
66
+
67
+ const record = await this.#storage.ensure(query, properties, object.get())
68
+
69
+ if (record.id !== blank.id) // exists
70
+ return this.#entities.object(record)
71
+
72
+ const event = object.event(input)
73
+
74
+ await this.#emission.emit(event)
75
+
76
+ return object
77
+ }
78
+
79
+ async commit (state, input) {
80
+ const event = state.event(input)
59
81
 
60
82
  let ok = true
61
83
 
@@ -65,25 +87,17 @@ class State {
65
87
  ok = await this.#storage.store(object)
66
88
 
67
89
  // #20
68
- await this.#emission.emit(event)
90
+ if (ok === true)
91
+ await this.#emission.emit(event)
69
92
  }
70
93
 
71
94
  return ok
72
95
  }
73
96
 
74
97
  async apply (state) {
75
- const {
76
- changeset,
77
- insert
78
- } = state.export()
79
-
80
- let upsert
81
-
82
- if (this.#dependent && state.query.id !== undefined && state.query.version === undefined) {
83
- upsert = insert
84
- }
98
+ const changeset = state.export()
85
99
 
86
- const result = await this.#storage.upsert(state.query, changeset, upsert)
100
+ const result = await this.#storage.upsert(state.query, changeset)
87
101
 
88
102
  if (result === null) {
89
103
  if (state.query.version !== undefined) {
package/src/transition.js CHANGED
@@ -1,14 +1,8 @@
1
1
  'use strict'
2
2
 
3
3
  const { retry } = require('@toa.io/generic')
4
-
5
4
  const { Operation } = require('./operation')
6
- const {
7
- StateConcurrencyException,
8
- StateNotFoundException,
9
- DuplicateException,
10
- Exception
11
- } = require('./exceptions')
5
+ const { StateConcurrencyException, StateNotFoundException } = require('./exceptions')
12
6
 
13
7
  class Transition extends Operation {
14
8
  #concurrency
@@ -28,35 +22,27 @@ class Transition extends Operation {
28
22
 
29
23
  store.scope = request.query ? await this.query(request.query) : this.scope.init()
30
24
 
31
- if (store.scope === null) throw new StateNotFoundException()
25
+ if (store.scope === null || store.scope.deleted === true)
26
+ throw new StateNotFoundException()
32
27
 
33
28
  store.state = store.scope.get()
34
29
  }
35
30
 
36
31
  async commit (store) {
37
- const {
38
- scope,
39
- state,
40
- reply,
41
- retry
42
- } = store
32
+ const { scope, state, reply, retry } = store
43
33
 
44
34
  if (reply.error !== undefined) return
45
35
 
46
36
  scope.set(state)
47
37
 
48
- const result = await this.scope.commit(scope)
38
+ const result = await this.scope.commit(scope, store.request.input)
49
39
 
50
- if (result === false ||
51
- (result instanceof DuplicateException && result.message.includes('_id'))) {
52
- if (this.#concurrency === 'retry') {
40
+ if (result === false) {
41
+ if (this.#concurrency === 'retry')
53
42
  return retry()
54
- } else {
43
+ else
55
44
  throw new StateConcurrencyException()
56
- }
57
45
  }
58
-
59
- if (result instanceof Exception) throw result
60
46
  }
61
47
 
62
48
  async #retry (store, retry) {
@@ -70,7 +56,7 @@ const RETRY = {
70
56
  base: 10,
71
57
  max: 5000,
72
58
  dispersion: 1,
73
- retries: Infinity
59
+ retries: 32
74
60
  }
75
61
 
76
62
  exports.Transition = Transition
@@ -18,13 +18,22 @@ class Transmission extends Connector {
18
18
  let i = 0
19
19
 
20
20
  while (reply === false && i < this.#bindings.length) {
21
- reply = await this.#bindings[i].request(request)
21
+ const binding = this.#bindings[i]
22
+
22
23
  i++
24
+
25
+ if (request?.task === true) {
26
+ if (binding.task === undefined)
27
+ continue
28
+
29
+ await binding.task(request)
30
+ reply = null
31
+ } else
32
+ reply = await binding.request(request)
23
33
  }
24
34
 
25
- if (reply === false) {
35
+ if (reply === false)
26
36
  throw new TransmissionException(`All (${this.#bindings.length}) bindings rejected.`)
27
- }
28
37
 
29
38
  return reply
30
39
  }
@@ -3,6 +3,7 @@
3
3
  const { Component } = require('../src/component')
4
4
  const { codes } = require('../src/exceptions')
5
5
  const fixtures = require('./component.fixtures')
6
+ const { AssertionError } = require('node:assert')
6
7
 
7
8
  describe('Invocations', () => {
8
9
  const name = ['foo', 'bar'][Math.floor(2 * Math.random())]
@@ -21,7 +22,7 @@ describe('Invocations', () => {
21
22
 
22
23
  it('should throw on unknown invocation name', async () => {
23
24
  await expect(() => component.invoke('baz'))
24
- .rejects.toMatchObject({ code: codes.NotImplemented })
25
+ .rejects.toThrow(AssertionError)
25
26
  })
26
27
 
27
28
  it('should invoke input and query', async () => {
@@ -2,19 +2,19 @@
2
2
 
3
3
  const { generate } = require('randomstring')
4
4
 
5
- const { Conditions } = require('../../src/contract/conditions')
5
+ const { Contract } = require('../../src/contract/contract')
6
6
  const fixtures = require('./contract.fixtures')
7
7
 
8
- let conditions
8
+ let contract
9
9
 
10
10
  beforeEach(() => {
11
- conditions = new Conditions(fixtures.schema)
11
+ contract = new Contract(fixtures.schema)
12
12
  })
13
13
 
14
14
  it('should fit value', () => {
15
15
  const value = { foo: generate() }
16
16
 
17
- conditions.fit(value)
17
+ contract.fit(value)
18
18
 
19
19
  expect(fixtures.schema.fit).toHaveBeenCalledWith(value)
20
20
  })
@@ -22,5 +22,5 @@ it('should fit value', () => {
22
22
  it('should throw on invalid value', () => {
23
23
  const value = { invalid: true }
24
24
 
25
- expect(() => conditions.fit(value)).toThrow()
25
+ expect(() => contract.fit(value)).toThrow()
26
26
  })
@@ -3,10 +3,10 @@
3
3
  const clone = require('clone-deep')
4
4
  const { generate } = require('randomstring')
5
5
 
6
- jest.mock('../../src/contract/conditions')
6
+ jest.mock('../../src/contract/contract')
7
7
 
8
8
  const { Request } = require('../../src/contract/request')
9
- const { Conditions } = require('../../src/contract/conditions')
9
+ const { Contract } = require('../../src/contract/contract')
10
10
  const fixtures = require('./contract.fixtures')
11
11
 
12
12
  let contract
@@ -14,14 +14,14 @@ let contract
14
14
  beforeEach(() => {
15
15
  jest.clearAllMocks()
16
16
 
17
- contract = new Request(fixtures.schema)
17
+ contract = new Request(fixtures.schema, {})
18
18
  })
19
19
 
20
20
  const dummy = { schema: { properties: {} } }
21
21
 
22
22
  it('should extend Conditions', () => {
23
- expect(contract).toBeInstanceOf(Conditions)
24
- expect(Conditions).toHaveBeenCalledWith(fixtures.schema)
23
+ expect(contract).toBeInstanceOf(Contract)
24
+ expect(Contract).toHaveBeenCalledWith(fixtures.schema)
25
25
  })
26
26
 
27
27
  it('should fit request', () => {
@@ -29,7 +29,7 @@ it('should fit request', () => {
29
29
 
30
30
  contract.fit(request)
31
31
 
32
- expect(Conditions.mock.instances[0].fit).toHaveBeenCalledWith(request)
32
+ expect(Contract.mock.instances[0].fit).toHaveBeenCalledWith(request)
33
33
  })
34
34
 
35
35
  describe('schema', () => {
@@ -59,7 +59,7 @@ describe('schema', () => {
59
59
 
60
60
  it('should not contain query if declaration.query is false', () => {
61
61
  schema.properties.query = { type: 'null' }
62
- expect(Request.schema({ query: false }, dummy)).toStrictEqual(schema)
62
+ expect(Request.schema({ query: false }, dummy)).toMatchObject(schema)
63
63
  })
64
64
 
65
65
  it('should require query if declaration.query is true', () => {
@@ -7,35 +7,7 @@ beforeEach(() => {
7
7
  jest.clearAllMocks()
8
8
  })
9
9
 
10
- describe('new', () => {
11
- it('should throw on schema error', () => {
12
- const entity = new Entity(fixtures.schema)
13
-
14
- expect(() => entity.set(fixtures.failed())).toThrow()
15
- })
16
-
17
- it('should provide state', () => {
18
- const entity = new Entity(fixtures.schema)
19
- const state = fixtures.state()
20
-
21
- entity.set(state)
22
-
23
- expect(entity.get()).toEqual(state)
24
- })
25
- })
26
-
27
10
  describe('argument', () => {
28
- it('should provide initial state if no argument passed', () => {
29
- const entity = new Entity(fixtures.schema)
30
- const defaults = fixtures.schema.defaults.mock.results[0].value
31
- const expected = {
32
- ...defaults,
33
- _version: 0
34
- }
35
-
36
- expect(entity.get()).toStrictEqual(expected)
37
- })
38
-
39
11
  it('should set state', () => {
40
12
  const state = fixtures.state()
41
13
  const entity = new Entity(fixtures.schema, state)
@@ -54,29 +26,12 @@ it('should provide event', () => {
54
26
 
55
27
  const event = entity.event()
56
28
 
57
- expect(event).toEqual({
29
+ expect(event).toEqual(expect.objectContaining({
58
30
  state,
59
31
  origin,
60
- changeset: {
32
+ changeset: expect.objectContaining({
61
33
  foo: 'new value',
62
34
  _version: 1
63
- }
64
- })
65
- })
66
-
67
- it('should define `id` as readonly', async () => {
68
- const origin = fixtures.state()
69
- const entity = new Entity(fixtures.schema, origin)
70
- const state = entity.get()
71
-
72
- expect(() => (state.id = 1)).toThrow('assign to read only property')
73
- })
74
-
75
- it('should seal id', async () => {
76
- const origin = fixtures.state()
77
- const entity = new Entity(fixtures.schema, origin)
78
- const state = entity.get()
79
- const redefine = () => Object.defineProperty(state, 'id', { writable: true })
80
-
81
- expect(redefine).toThrow('redefine property')
35
+ })
36
+ }))
82
37
  })
@@ -1,26 +1,28 @@
1
1
  import * as _core from './index'
2
2
 
3
- declare namespace toa.core.bindings{
3
+ declare namespace toa.core.bindings {
4
4
 
5
5
  type Properties = {
6
6
  async?: boolean
7
7
  }
8
8
 
9
- interface Consumer extends _core.Connector{
9
+ interface Consumer extends _core.Connector {
10
10
  request (request: Request): Promise<_core.Reply>
11
+
12
+ task (request: Request): Promise<void>
11
13
  }
12
14
 
13
- interface Emitter extends _core.Connector{
15
+ interface Emitter extends _core.Connector {
14
16
  emit (message: _core.Message): Promise<void>
15
17
  }
16
18
 
17
- interface Broadcast<L> extends _core.Connector{
19
+ interface Broadcast<L> extends _core.Connector {
18
20
  transmit<T> (label: L, payload: T): Promise<void>
19
21
 
20
22
  receive<T> (label: L, callback: (payload: T) => void | Promise<void>): Promise<void>
21
23
  }
22
24
 
23
- interface Factory{
25
+ interface Factory {
24
26
  producer? (locator: _core.Locator, endpoints: Array<string>, producer: _core.Component): _core.Connector
25
27
 
26
28
  consumer? (locator: _core.Locator, endpoint: string): Consumer
@@ -1,9 +1,12 @@
1
1
  import { Connector } from './connector'
2
2
  import { Locator } from './locator'
3
3
  import { Request } from './request'
4
+ import { Operation } from './operations'
4
5
 
5
- export interface Component extends Connector{
6
+ export class Component extends Connector {
6
7
  locator: Locator
7
8
 
9
+ constructor (locator: Locator, operations: Record<string, Operation>)
10
+
8
11
  invoke<T = any> (endpoint: string, request: Request): Promise<T>
9
12
  }
@@ -3,9 +3,10 @@ import * as _component from './component'
3
3
  import * as _context from './context'
4
4
  import * as _storages from './storages'
5
5
  import * as _bindings from './bindings'
6
+ import { Manifest } from '@toa.io/norm'
6
7
 
7
- export interface Factory{
8
- tenant? (locator: _core.Locator, manifest: object): _core.Connector
8
+ export interface Factory {
9
+ tenant? (locator: _core.Locator, manifest: object, component: Manifest): _core.Connector
9
10
 
10
11
  aspect? (locator: _core.Locator, manifest: object | null): Aspect | Aspect[]
11
12
 
@@ -22,7 +23,7 @@ export interface Factory{
22
23
  receiver? (receiver: _core.Receiver, locator: _core.Locator): _core.Receiver
23
24
  }
24
25
 
25
- export interface Aspect extends _core.Connector{
26
+ export interface Aspect extends _core.Connector {
26
27
  name: string
27
28
 
28
29
  invoke (...args: any[]): any
package/types/index.ts CHANGED
@@ -5,6 +5,7 @@ export * as bridges from './bridges'
5
5
  export * as operations from './operations'
6
6
 
7
7
  export type { Component } from './component'
8
+ export type { Remote } from './remote'
8
9
  export { Connector } from './connector'
9
10
  export type { Context } from './context'
10
11
  export type { Exception } from './exception'
@@ -1,10 +1,11 @@
1
- export class Locator {
1
+ export class Locator{
2
2
  public readonly name: string
3
3
  public readonly namespace: string
4
4
 
5
5
  public readonly id: string
6
6
  public readonly label: string
7
7
  public readonly uppercase: string
8
+ public readonly lowercase: string
8
9
 
9
10
  constructor (name: string, namespace?: string)
10
11
 
@@ -1,2 +1,8 @@
1
+ import { Request } from './request'
2
+
1
3
  export type type = 'transition' | 'observation' | 'assignment' | 'computation' | 'effect'
2
4
  export type scope = 'object' | 'objects' | 'changeset'
5
+
6
+ export class Operation {
7
+ invoke<T = any> (request: Request): Promise<T>
8
+ }
@@ -0,0 +1,18 @@
1
+ import { Component } from './component'
2
+
3
+ export class Remote extends Component {
4
+ explain (endpoint: string): Promise<Explanation>
5
+ }
6
+
7
+ interface Explanation {
8
+ input: Schema | null
9
+ output: Schema | null
10
+ errors?: string[]
11
+ }
12
+
13
+ interface Schema {
14
+ type: string
15
+ properties: {
16
+ [key: string]: Schema
17
+ }
18
+ }
@@ -14,6 +14,7 @@ export interface Request {
14
14
  input?: any
15
15
  query?: Query
16
16
  authentic?: boolean
17
+ task?: boolean
17
18
  }
18
19
 
19
20
  export interface Reply {