@toa.io/core 0.20.0-dev.9 → 0.20.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "0.20.0-dev.9",
3
+ "version": "0.20.0",
4
4
  "description": "Toa Core",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -12,18 +12,22 @@
12
12
  "url": "https://github.com/toa-io/toa/issues"
13
13
  },
14
14
  "main": "src/index.js",
15
+ "types": "types/index.d.ts",
15
16
  "publishConfig": {
16
17
  "access": "public"
17
18
  },
18
19
  "scripts": {
19
20
  "test": "echo \"Error: run tests from root\" && exit 1"
20
21
  },
22
+ "peerDependencies": {
23
+ "nopeable": "*"
24
+ },
21
25
  "dependencies": {
22
26
  "@rsql/parser": "1.2.4",
23
- "@toa.io/console": "0.20.0-dev.9",
24
- "@toa.io/generic": "0.20.0-dev.9",
25
- "@toa.io/yaml": "0.20.0-dev.9",
27
+ "@toa.io/console": "0.20.0",
28
+ "@toa.io/generic": "0.20.0",
29
+ "@toa.io/yaml": "0.20.0",
26
30
  "clone-deep": "4.0.1"
27
31
  },
28
- "gitHead": "4e503ffe4fe6dfab1165e795832d3d4fba711582"
32
+ "gitHead": "28fc4b45c224c3683acaaf0e4abd1eb04e07b408"
29
33
  }
package/src/call.js CHANGED
@@ -1,6 +1,8 @@
1
1
  'use strict'
2
2
 
3
3
  const { Connector } = require('./connector')
4
+ const { Nope } = require('nopeable')
5
+ const { Readable } = require('stream')
4
6
 
5
7
  class Call extends Connector {
6
8
  #transmitter
@@ -15,19 +17,25 @@ class Call extends Connector {
15
17
  this.depends(transmitter)
16
18
  }
17
19
 
18
- /**
19
- * @param {toa.core.Request} request
20
- */
21
20
  async invoke (request = {}) {
22
21
  this.#contract.fit(request)
23
22
 
23
+ // avoid validation on the recipient's side
24
24
  request.authentic = true
25
25
 
26
- const { exception, ...reply } = await this.#transmitter.request(request)
26
+ const reply = await this.#transmitter.request(request)
27
27
 
28
- if (exception) throw exception
28
+ if (reply === null) return null
29
+ else if (reply instanceof Readable) return reply
30
+ else {
31
+ if (reply.exception !== undefined)
32
+ throw reply.exception
29
33
 
30
- return reply
34
+ if (reply.error !== undefined)
35
+ return new Nope(reply.error)
36
+
37
+ return reply.output
38
+ }
31
39
  }
32
40
  }
33
41
 
package/src/cascade.js CHANGED
@@ -5,27 +5,31 @@ const { Connector } = require('./connector')
5
5
 
6
6
  class Cascade extends Connector {
7
7
  #bridges
8
+ #last
8
9
 
9
10
  constructor (bridges) {
10
11
  super()
11
12
 
12
13
  this.#bridges = bridges
14
+ this.#last = bridges[bridges.length - 1]
13
15
 
14
16
  this.depends(bridges)
15
17
  }
16
18
 
17
19
  async run (...args) {
18
- const reply = {}
19
-
20
- for (const bridge of this.#bridges) {
21
- const partial = await bridge.execute(...args)
22
-
23
- if (partial.error) return { error: partial.error }
24
-
25
- merge(reply, partial)
26
- }
27
-
28
- return reply
20
+ // const reply = {}
21
+ //
22
+ // for (const bridge of this.#bridges) {
23
+ // const partial = await bridge.execute(...args)
24
+ //
25
+ // if (partial.error) return { error: partial.error }
26
+ //
27
+ // merge(reply, partial)
28
+ // }
29
+ //
30
+ // return reply
31
+
32
+ return this.#last.execute(...args)
29
33
  }
30
34
  }
31
35
 
package/src/context.js CHANGED
@@ -38,7 +38,7 @@ class Context extends Connector {
38
38
  async #remote (namespace, name) {
39
39
  const key = namespace + '.' + name
40
40
 
41
- if (this.#remotes[key] === undefined) this.#remotes[key] = this.#connect(namespace, name)
41
+ this.#remotes[key] ??= this.#connect(namespace, name)
42
42
 
43
43
  return this.#remotes[key]
44
44
  }
@@ -1,8 +1,9 @@
1
1
  type: object
2
2
  properties:
3
3
  code:
4
- type: integer
5
- minimum: 0
4
+ anyOf:
5
+ - type: integer
6
+ - type: string
6
7
  message:
7
8
  type: string
8
- required: [ code, message ]
9
+ required: [code]
package/src/discovery.js CHANGED
@@ -30,7 +30,7 @@ class Discovery extends Connector {
30
30
  const warning = () => console.warn(`Waiting for lookup response from '${id}'...`)
31
31
  const timeout = setTimeout(warning, TIMEOUT)
32
32
 
33
- const { output } = await this.#lookups[id].invoke()
33
+ const output = await this.#lookups[id].invoke()
34
34
 
35
35
  console.debug(`Lookup response from '${id}' received`)
36
36
  clearTimeout(timeout)
@@ -14,6 +14,11 @@ class Observation extends Operation {
14
14
  store.scope = scope
15
15
  store.state = state
16
16
  }
17
+
18
+ async run (store) {
19
+ if (store.scope === null) store.reply = null
20
+ else await super.run(store)
21
+ }
17
22
  }
18
23
 
19
24
  exports.Observation = Observation
package/src/operation.js CHANGED
@@ -24,10 +24,6 @@ class Operation extends Connector {
24
24
  this.depends(cascade)
25
25
  }
26
26
 
27
- /**
28
- * @param {toa.core.Request} request
29
- * @return {Promise<toa.core.Reply>}
30
- */
31
27
  async invoke (request) {
32
28
  try {
33
29
  if (request.authentic !== true) this.#contracts.request.fit(request)
@@ -57,7 +53,7 @@ class Operation extends Connector {
57
53
  const { request, state } = store
58
54
  const reply = await this.#cascade.run(request.input, state) || {}
59
55
 
60
- this.#contracts.reply.fit(reply)
56
+ // this.#contracts.reply.fit(reply)
61
57
 
62
58
  store.reply = reply
63
59
  }
package/src/state.js CHANGED
@@ -28,9 +28,9 @@ class State {
28
28
  }
29
29
 
30
30
  init (id) {
31
- if (this.#initialized === true && id === undefined) {
32
- throw new StateInitializationException('Cannot initialize entity which is initialized. Use request.query.id to access.')
33
- }
31
+ // if (this.#initialized === true && id === undefined) {
32
+ // throw new StateInitializationException('Cannot initialize entity which is initialized. Use request.query.id to access.')
33
+ // }
34
34
 
35
35
  return this.#entity.init(id)
36
36
  }
@@ -41,10 +41,10 @@ class State {
41
41
  if (record === null) {
42
42
  if (this.#initialized && query.id !== undefined && query.version === undefined) return this.init(query.id)
43
43
  else if (query.version !== undefined) throw new StatePreconditionException()
44
- else throw new StateNotFoundException()
45
44
  }
46
45
 
47
- return this.#entity.object(record)
46
+ if (record === null) return null
47
+ else return this.#entity.object(record)
48
48
  }
49
49
 
50
50
  async objects (query) {
@@ -94,7 +94,7 @@ class State {
94
94
  else throw new StateNotFoundException()
95
95
  }
96
96
 
97
- // TODO: same as above
97
+ // same as above
98
98
  await this.#emission.emit({ changeset, state: result })
99
99
  }
100
100
  }
package/src/transition.js CHANGED
@@ -3,7 +3,7 @@
3
3
  const { retry } = require('@toa.io/generic')
4
4
 
5
5
  const { Operation } = require('./operation')
6
- const { StateConcurrencyException } = require('./exceptions')
6
+ const { StateConcurrencyException, StateNotFoundException } = require('./exceptions')
7
7
 
8
8
  class Transition extends Operation {
9
9
  #concurrency
@@ -22,6 +22,9 @@ class Transition extends Operation {
22
22
  const { request } = store
23
23
 
24
24
  store.scope = request.query ? await this.query(request.query) : this.scope.init()
25
+
26
+ if (store.scope === null) throw new StateNotFoundException()
27
+
25
28
  store.state = store.scope.get()
26
29
  }
27
30
 
@@ -47,7 +50,6 @@ class Transition extends Operation {
47
50
  }
48
51
  }
49
52
 
50
- /** @type {toa.generic.retry.Options} */
51
53
  const RETRY = {
52
54
  base: 10,
53
55
  max: 5000,
@@ -4,7 +4,8 @@ const { generate } = require('randomstring')
4
4
 
5
5
  // noinspection JSCheckFunctionSignatures
6
6
  const transmission = {
7
- request: jest.fn((request) => ({ [request.invalid ? 'exception' : generate()]: generate() }))
7
+ request: jest.fn((request) => ({ [request.invalid ? 'exception' : 'output']: generate() })),
8
+ link: jest.fn()
8
9
  }
9
10
 
10
11
  const contract = {
package/test/call.test.js CHANGED
@@ -1,11 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const { Call } = require('../src/call')
4
3
  const fixtures = require('./call.fixtures')
5
-
6
- jest.mock('../src/connector')
7
-
8
- const { Connector } = require('../src/connector')
4
+ const { Call } = require('../src/call')
9
5
 
10
6
  let call
11
7
 
@@ -16,9 +12,7 @@ beforeEach(() => {
16
12
  })
17
13
 
18
14
  it('should depend on transmission', () => {
19
- expect(call).toBeInstanceOf(Connector)
20
- expect(call).toBe(Connector.mock.instances[0])
21
- expect(Connector.mock.instances[0].depends).toHaveBeenCalledWith(fixtures.transmission)
15
+ expect(fixtures.transmission.link).toHaveBeenLastCalledWith(call)
22
16
  })
23
17
 
24
18
  it('should call transmission', async () => {
@@ -42,7 +36,7 @@ it('should return reply', async () => {
42
36
 
43
37
  const reply = await call.invoke(request)
44
38
 
45
- expect(reply).toStrictEqual(fixtures.transmission.request.mock.results[0].value)
39
+ expect(reply).toStrictEqual(fixtures.transmission.request.mock.results[0].value.output)
46
40
  })
47
41
 
48
42
  it('should throw received exceptions', async () => {
@@ -7,29 +7,29 @@ declare namespace toa.core.bindings {
7
7
  }
8
8
 
9
9
  interface Consumer extends _core.Connector {
10
- request(request: Request): Promise<_core.Reply>
10
+ request (request: Request): Promise<_core.Reply>
11
11
  }
12
12
 
13
13
  interface Emitter extends _core.Connector {
14
- emit(message: _core.Message): Promise<void>
14
+ emit (message: _core.Message): Promise<void>
15
15
  }
16
16
 
17
- interface Broadcast extends _core.Connector {
18
- transmit(label: string, payload: object): Promise<void>
17
+ interface Broadcast<L> extends _core.Connector {
18
+ transmit<T> (label: L, payload: T): Promise<void>
19
19
 
20
- receive(label: string, callback: (payload: object) => Promise<void>): Promise<void>
20
+ receive<T> (label: L, callback: (payload: T) => void | Promise<void>): Promise<void>
21
21
  }
22
22
 
23
23
  interface Factory {
24
- producer?(locator: _core.Locator, endpoints: Array<string>, producer: _core.Component): _core.Connector
24
+ producer? (locator: _core.Locator, endpoints: Array<string>, producer: _core.Component): _core.Connector
25
25
 
26
- consumer?(locator: _core.Locator, endpoint: string): Consumer
26
+ consumer? (locator: _core.Locator, endpoint: string): Consumer
27
27
 
28
- emitter?(locator: _core.Locator, label: string): Emitter
28
+ emitter? (locator: _core.Locator, label: string): Emitter
29
29
 
30
- receiver?(locator: _core.Locator, label: string, group: string, receiver: _core.Receiver): _core.Connector
30
+ receiver? (locator: _core.Locator, label: string, group: string, receiver: _core.Receiver): _core.Connector
31
31
 
32
- broadcast?(name: string, group?: string): Broadcast
32
+ broadcast? (name: string, group?: string): Broadcast
33
33
  }
34
34
 
35
35
  }
@@ -37,3 +37,4 @@ declare namespace toa.core.bindings {
37
37
  export type Emitter = toa.core.bindings.Emitter
38
38
  export type Factory = toa.core.bindings.Factory
39
39
  export type Properties = toa.core.bindings.Properties
40
+ export type Broadcast<L> = toa.core.bindings.Broadcast<L>
@@ -0,0 +1,9 @@
1
+ import { Connector } from './connector'
2
+ import { Locator } from './locator'
3
+ import { Request } from './request'
4
+
5
+ export interface Component extends Connector {
6
+ locator: Locator
7
+
8
+ invoke<T> (endpoint: string, request: Request): Promise<T>
9
+ }
@@ -1,24 +1,18 @@
1
- declare namespace toa.core {
1
+ export class Connector {
2
+ public id: string
3
+ public connected: boolean
2
4
 
3
- interface Connector {
4
- id: string
5
- connected: boolean
5
+ public connect (): Promise<void>
6
6
 
7
- depends(connector: Connector): Connector
7
+ public disconnect (interrupt?: boolean): Promise<void>
8
8
 
9
- link(connector: Connector): void
9
+ public depends (connector: Connector | Connector[]): Connector
10
10
 
11
- connect(): Promise<void>
11
+ link (connector: Connector): void
12
12
 
13
- disconnect(interrupt?: boolean): Promise<void>
13
+ protected open (): Promise<void> | void
14
14
 
15
- open(): Promise<void>
16
-
17
- close(): Promise<void>
18
-
19
- dispose(): Promise<void>
20
- }
15
+ protected close (): Promise<void> | void
21
16
 
17
+ protected dispose (): Promise<void> | void
22
18
  }
23
-
24
- export type Connector = toa.core.Connector
@@ -1,10 +1,4 @@
1
- declare namespace toa.core {
2
-
3
- interface Exception {
4
- code: number
5
- message: string
6
- }
7
-
1
+ export interface Exception {
2
+ code: number
3
+ message: string
8
4
  }
9
-
10
- export type Exception = toa.core.Exception
@@ -7,21 +7,21 @@ import * as _bindings from './bindings'
7
7
  declare namespace toa.core.extensions {
8
8
 
9
9
  interface Factory {
10
- tenant?(locator: _core.Locator, declaration: object): _core.Connector
10
+ tenant? (locator: _core.Locator, manifest: object): _core.Connector
11
11
 
12
- aspect?(locator: _core.Locator, declaration: object): Aspect | Aspect[]
12
+ aspect? (locator: _core.Locator, manifest: object | null): Aspect | Aspect[]
13
13
 
14
- service?(name?: string): _core.Connector
14
+ service? (name?: string): _core.Connector | null
15
15
 
16
- component?(component: _component.Component): _component.Component
16
+ component? (component: _component.Component): _component.Component
17
17
 
18
- context?(context: _context.Context): _context.Context
18
+ context? (context: _context.Context): _context.Context
19
19
 
20
- storage?(storage: _storages.Storage): _storages.Storage
20
+ storage? (storage: _storages.Storage): _storages.Storage
21
21
 
22
- emitter?(emitter: _bindings.Emitter, label: string): _bindings.Emitter
22
+ emitter? (emitter: _bindings.Emitter, label: string): _bindings.Emitter
23
23
 
24
- receiver?(receiver: _core.Receiver, locator: _core.Locator): _core.Receiver
24
+ receiver? (receiver: _core.Receiver, locator: _core.Locator): _core.Receiver
25
25
  }
26
26
 
27
27
  interface Aspect extends _core.Connector {
package/types/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * as bindings from './bindings'
2
2
  export * as extensions from './extensions'
3
3
  export * as storages from './storages'
4
4
  export * as bridges from './bridges'
5
+ export * as operations from './operations'
5
6
 
6
7
  export { Component } from './component'
7
8
  export { Connector } from './connector'
@@ -10,5 +11,4 @@ export { Exception } from './exception'
10
11
  export { Locator } from './locator'
11
12
  export { Receiver } from './receiver'
12
13
  export { Message } from './message'
13
- export { Reply } from './reply'
14
- export { Request, Query } from './request'
14
+ export { Request, Query, Reply } from './request'
@@ -1,16 +1,12 @@
1
- declare namespace toa.core {
1
+ export class Locator {
2
+ public readonly name: string
3
+ public readonly namespace: string
2
4
 
3
- interface Locator {
4
- name: string
5
- namespace: string
5
+ public readonly id: string
6
+ public readonly label: string
7
+ public readonly uppercase: string
6
8
 
7
- id: string
8
- label: string
9
- uppercase: string
10
-
11
- hostname(type?: string): string
12
- }
9
+ constructor (name: string, namespace?: string)
13
10
 
11
+ hostname (type?: string): string
14
12
  }
15
-
16
- export type Locator = toa.core.Locator
@@ -1,9 +1,3 @@
1
- declare namespace toa.core {
2
-
3
- type Message = {
4
- payload: Object
5
- }
6
-
1
+ export interface Message<T = any> {
2
+ payload: T
7
3
  }
8
-
9
- export type Message = toa.core.Message
@@ -0,0 +1,2 @@
1
+ export type type = 'transition' | 'observation' | 'assignment' | 'computation' | 'effect'
2
+ export type scope = 'object' | 'objects' | 'changeset'
@@ -1,26 +1,23 @@
1
- declare namespace toa.core {
1
+ import { Exception } from './exception'
2
2
 
3
- namespace request {
4
-
5
- interface Query {
6
- id?: string
7
- criteria?: string
8
- omit?: number
9
- limit?: number
10
- sort?: Array<string>
11
- projection?: Array<string>
12
- version?: number
13
- }
14
-
15
- }
16
-
17
- type Request = {
18
- input?: any
19
- query?: request.Query
20
- authentic?: boolean
21
- }
3
+ export interface Query {
4
+ id?: string
5
+ criteria?: string
6
+ omit?: number
7
+ limit?: number
8
+ sort?: Array<string>
9
+ projection?: Array<string>
10
+ version?: number
11
+ }
22
12
 
13
+ export interface Request {
14
+ input?: any
15
+ query?: Query
16
+ authentic?: boolean
23
17
  }
24
18
 
25
- export type Request = toa.core.Request
26
- export type Query = toa.core.request.Query
19
+ export interface Reply {
20
+ output?: any
21
+ error?: object
22
+ exception?: Exception
23
+ }
@@ -1,11 +0,0 @@
1
- 'use strict'
2
-
3
- const bridges = ['a', 'b', 'c'].map((index) => ({
4
- execute: jest.fn((request) => {
5
- if (request?.error === index) return { error: index }
6
-
7
- return { output: { [index]: true } }
8
- })
9
- }))
10
-
11
- exports.bridges = bridges
@@ -1,42 +0,0 @@
1
- 'use strict'
2
-
3
- jest.mock('../src/connector')
4
-
5
- const clone = require('clone-deep')
6
-
7
- const { Connector } = require('../src/connector')
8
- const { Cascade } = require('../src/cascade')
9
- const fixtures = require('./cascade.fixtures')
10
-
11
- let cascade
12
-
13
- beforeEach(() => {
14
- jest.clearAllMocks()
15
-
16
- cascade = new Cascade(clone(fixtures.bridges))
17
- })
18
-
19
- it('should depend on bridges', () => {
20
- expect(cascade).toBeInstanceOf(Connector)
21
- expect(Connector.mock.instances[0].depends).toHaveBeenCalledWith(fixtures.bridges)
22
- })
23
-
24
- it('should call bridges.execute', async () => {
25
- const args = [1, 2]
26
- await cascade.run(...args)
27
-
28
- for (const bridge of fixtures.bridges) expect(bridge.execute).toHaveBeenCalledWith(...args)
29
- })
30
-
31
- it('should merge output', async () => {
32
- const reply = await cascade.run()
33
-
34
- expect(reply).toStrictEqual({ output: { a: true, b: true, c: true } })
35
- })
36
-
37
- it('should interrupt on error', async () => {
38
- const reply = await cascade.run({ error: 'b' })
39
-
40
- expect(reply).toStrictEqual({ error: 'b' })
41
- expect(fixtures.bridges[2].execute).not.toHaveBeenCalled()
42
- })
@@ -1,16 +0,0 @@
1
- import * as _connector from './connector'
2
- import * as _locator from './locator'
3
- import * as _request from './request'
4
- import * as _reply from './reply'
5
-
6
- declare namespace toa.core {
7
-
8
- interface Component extends _connector.Connector {
9
- locator: _locator.Locator
10
-
11
- invoke(endpoint: string, request: _request.Request): Promise<_reply.Reply>
12
- }
13
-
14
- }
15
-
16
- export type Component = toa.core.Component
package/types/reply.d.ts DELETED
@@ -1,15 +0,0 @@
1
- // noinspection ES6UnusedImports
2
-
3
- import { Exception } from './exception'
4
-
5
- declare namespace toa.core {
6
-
7
- interface Reply {
8
- output?: Object
9
- error?: Object
10
- exception?: Exception
11
- }
12
-
13
- }
14
-
15
- export type Reply = toa.core.Reply