@toa.io/core 1.0.0-alpha.6 → 1.0.0-alpha.64

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,13 +28,22 @@ 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
 
34
43
  return await this.process(store)
35
44
  } catch (e) {
45
+ console.error('Failed to execute operation', e)
46
+
36
47
  const exception = e instanceof Error ? new SystemException(e) : e
37
48
 
38
49
  return { exception }
@@ -47,14 +58,24 @@ class Operation extends Connector {
47
58
  return store.reply
48
59
  }
49
60
 
50
- async acquire () {}
61
+ async acquire (store) {
62
+ if (this.#scope === 'none')
63
+ return
64
+
65
+ const scope = await this.query(store.request.query)
66
+ const raw = scope === null || scope instanceof Readable
67
+
68
+ store.scope = scope
69
+ store.state = raw ? scope : scope.get()
70
+ }
51
71
 
52
72
  async run (store) {
53
73
  const { request, state } = store
54
- // noinspection UnnecessaryLocalVariableJS
55
- const reply = await this.#cascade.run(request.input, state) || {}
74
+ const reply = await this.#cascade.run(request.input, state)
56
75
 
57
- // this.#contracts.reply.fit(reply)
76
+ // validate reply only on local environments
77
+ if (process.env.TOA_ENV === 'local' && !(reply instanceof Readable))
78
+ this.#contracts.reply.fit(reply)
58
79
 
59
80
  store.reply = reply
60
81
  }
@@ -62,6 +83,9 @@ class Operation extends Connector {
62
83
  async commit () {}
63
84
 
64
85
  async query (query) {
86
+ if (query === undefined)
87
+ throw new RequestContractException('Request query is required')
88
+
65
89
  return this.scope[this.#scope](query)
66
90
  }
67
91
  }
package/src/receiver.js CHANGED
@@ -16,18 +16,13 @@ class Receiver extends Connector {
16
16
  /** @type {string} */
17
17
  #endpoint
18
18
 
19
- /** @type {toa.core.Component} */
19
+ /** @type {unknown[]} */
20
+ #arguments
21
+
20
22
  #local
21
23
 
22
- /** @type {toa.core.bridges.Receiver} */
23
24
  #bridge
24
25
 
25
- /**
26
- *
27
- * @param {toa.norm.component.Receiver} definition
28
- * @param {toa.core.Component} local
29
- * @param {toa.core.bridges.Receiver} bridge
30
- */
31
26
  constructor (definition, local, bridge) {
32
27
  super()
33
28
 
@@ -36,6 +31,7 @@ class Receiver extends Connector {
36
31
  this.#conditioned = conditioned
37
32
  this.#adaptive = adaptive
38
33
  this.#endpoint = operation
34
+ this.#arguments = definition.arguments
39
35
 
40
36
  this.#local = local
41
37
  this.#bridge = bridge
@@ -58,7 +54,7 @@ class Receiver extends Connector {
58
54
  }
59
55
 
60
56
  async #request (payload) {
61
- return this.#adaptive ? await this.#bridge.request(payload) : { input: payload }
57
+ return this.#adaptive ? await this.#bridge.request(payload, ...(this.#arguments ?? [])) : { input: payload }
62
58
  }
63
59
  }
64
60
 
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,62 +1,79 @@
1
1
  'use strict'
2
2
 
3
- const { empty } = require('@toa.io/generic')
3
+ const { empty, newid } = require('@toa.io/generic')
4
+ const { StatePreconditionException, StateNotFoundException } = require('./exceptions')
4
5
 
5
- const {
6
- StatePreconditionException,
7
- StateNotFoundException,
8
- StateInitializationException
9
- } = require('./exceptions')
10
-
11
- /**
12
- * @implements {toa.core.State}
13
- */
14
6
  class State {
15
- /** @type {toa.core.Storage} */
7
+ #associated
16
8
  #storage
17
-
18
- /** @type {toa.core.entity.Factory} */
19
- #entity
9
+ #entities
20
10
  #emission
21
- #dependent
22
11
 
23
- constructor (storage, entity, emission, dependent) {
12
+ constructor (storage, entity, emission, associated) {
24
13
  this.#storage = storage
25
- this.#entity = entity
14
+ this.#entities = entity
26
15
  this.#emission = emission
27
- this.#dependent = dependent === true
16
+ this.#associated = associated === true
28
17
  }
29
18
 
30
19
  init (id) {
31
- return this.#entity.init(id)
20
+ return this.#entities.init(id)
21
+ }
22
+
23
+ fit (values) {
24
+ return this.#entities.fit(values)
32
25
  }
33
26
 
34
27
  async object (query) {
35
28
  const record = await this.#storage.get(query)
36
29
 
37
30
  if (record === null) {
38
- if (this.#dependent && query.id !== undefined && query.version === undefined) return this.init(query.id)
39
- else if (query.version !== undefined) throw new StatePreconditionException()
40
- }
41
-
42
- if (record === null) return null
43
- else return this.#entity.object(record)
31
+ if (this.#associated && query.id !== undefined && query.version === undefined)
32
+ return this.init(query.id)
33
+ else if (query.version !== undefined)
34
+ throw new StatePreconditionException()
35
+
36
+ return null
37
+ } else
38
+ return this.#entities.object(record)
44
39
  }
45
40
 
46
41
  async objects (query) {
47
42
  const recordset = await this.#storage.find(query)
48
43
 
49
- return this.#entity.objects(recordset)
44
+ return this.#entities.objects(recordset)
45
+ }
46
+
47
+ async stream (query) {
48
+ return this.#storage.stream(query)
50
49
  }
51
50
 
52
51
  changeset (query) {
53
- return this.#entity.changeset(query)
52
+ return this.#entities.changeset(query)
54
53
  }
55
54
 
56
55
  none () {
57
56
  return null
58
57
  }
59
58
 
59
+ async ensure (query, properties) {
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
+ await this.#emission.emit(object.event())
73
+
74
+ return object
75
+ }
76
+
60
77
  async commit (state) {
61
78
  const event = state.event()
62
79
 
@@ -68,30 +85,34 @@ class State {
68
85
  ok = await this.#storage.store(object)
69
86
 
70
87
  // #20
71
- await this.#emission.emit(event)
88
+ if (ok === true) {
89
+ await this.#emission.emit(event)
90
+ }
72
91
  }
73
92
 
74
93
  return ok
75
94
  }
76
95
 
77
96
  async apply (state) {
78
- const { changeset, insert } = state.export()
79
-
80
- let upsert
81
-
82
- if (this.#dependent && state.query.id !== undefined && state.query.version === undefined) {
83
- upsert = insert
84
- }
97
+ const changeset = state.export()
85
98
 
86
- const result = await this.#storage.upsert(state.query, changeset, upsert)
99
+ const result = await this.#storage.upsert(state.query, changeset)
87
100
 
88
101
  if (result === null) {
89
- if (state.query.version !== undefined) throw new StatePreconditionException()
90
- else throw new StateNotFoundException()
102
+ if (state.query.version !== undefined) {
103
+ throw new StatePreconditionException()
104
+ } else {
105
+ throw new StateNotFoundException()
106
+ }
107
+ } else {
108
+ // same as above
109
+ await this.#emission.emit({
110
+ changeset,
111
+ state: result
112
+ })
91
113
  }
92
114
 
93
- // same as above
94
- await this.#emission.emit({ changeset, state: result })
115
+ return result
95
116
  }
96
117
  }
97
118
 
package/src/transition.js CHANGED
@@ -3,7 +3,10 @@
3
3
  const { retry } = require('@toa.io/generic')
4
4
 
5
5
  const { Operation } = require('./operation')
6
- const { StateConcurrencyException, StateNotFoundException } = require('./exceptions')
6
+ const {
7
+ StateConcurrencyException,
8
+ StateNotFoundException
9
+ } = require('./exceptions')
7
10
 
8
11
  class Transition extends Operation {
9
12
  #concurrency
@@ -23,7 +26,9 @@ class Transition extends Operation {
23
26
 
24
27
  store.scope = request.query ? await this.query(request.query) : this.scope.init()
25
28
 
26
- if (store.scope === null) throw new StateNotFoundException()
29
+ if (store.scope === null) {
30
+ throw new StateNotFoundException()
31
+ }
27
32
 
28
33
  store.state = store.scope.get()
29
34
  }
@@ -35,11 +40,13 @@ class Transition extends Operation {
35
40
 
36
41
  scope.set(state)
37
42
 
38
- const ok = await this.scope.commit(scope)
43
+ const result = await this.scope.commit(scope)
39
44
 
40
- if (ok !== true) {
41
- if (this.#concurrency === 'retry') retry()
42
- else throw new StateConcurrencyException()
45
+ if (result === false) {
46
+ if (this.#concurrency === 'retry')
47
+ return retry()
48
+ else
49
+ throw new StateConcurrencyException()
43
50
  }
44
51
  }
45
52
 
@@ -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
  }
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 {
@@ -1,21 +0,0 @@
1
- 'use strict'
2
-
3
- const { SystemException } = require('../exceptions')
4
-
5
- class Conditions {
6
- #schema
7
-
8
- constructor (schema) {
9
- this.#schema = schema
10
- }
11
-
12
- fit (value) {
13
- const error = this.#schema.fit(value)
14
-
15
- if (error !== null) throw new this.constructor.Exception(error)
16
- }
17
-
18
- static Exception = SystemException
19
- }
20
-
21
- exports.Conditions = Conditions