@toa.io/core 1.0.0-alpha.19 → 1.0.0-alpha.194

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.
Files changed (44) hide show
  1. package/package.json +6 -9
  2. package/src/call.js +6 -0
  3. package/src/cascade.js +2 -3
  4. package/src/component.js +15 -18
  5. package/src/composition.js +1 -1
  6. package/src/connector.js +3 -8
  7. package/src/contract/contract.js +22 -0
  8. package/src/contract/reply.js +26 -9
  9. package/src/contract/request.js +15 -5
  10. package/src/contract/schemas/query.yaml +7 -1
  11. package/src/discovery.js +2 -5
  12. package/src/effect.js +19 -0
  13. package/src/entities/changeset.js +5 -8
  14. package/src/entities/entity.js +47 -23
  15. package/src/entities/factory.js +11 -3
  16. package/src/exceptions.js +26 -19
  17. package/src/exposition.js +3 -2
  18. package/src/guard.js +17 -0
  19. package/src/index.js +6 -0
  20. package/src/observation.js +1 -9
  21. package/src/operation.js +28 -7
  22. package/src/query/options.js +3 -2
  23. package/src/query.js +2 -1
  24. package/src/receiver.js +13 -10
  25. package/src/remote.js +5 -7
  26. package/src/state.js +50 -33
  27. package/src/transition.js +8 -19
  28. package/src/transmission.js +12 -3
  29. package/src/unmanaged.js +11 -0
  30. package/test/component.test.js +4 -3
  31. package/test/contract/conditions.test.js +5 -5
  32. package/test/contract/request.test.js +7 -7
  33. package/test/entities/entity.test.js +0 -45
  34. package/test/state.test.js +0 -6
  35. package/types/bindings.d.ts +7 -5
  36. package/types/component.d.ts +4 -1
  37. package/types/extensions.d.ts +4 -3
  38. package/types/index.ts +1 -0
  39. package/types/operations.d.ts +6 -0
  40. package/types/query.d.ts +2 -0
  41. package/types/remote.d.ts +18 -0
  42. package/types/request.d.ts +3 -0
  43. package/types/storages.d.ts +11 -9
  44. package/src/contract/conditions.js +0 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "1.0.0-alpha.19",
3
+ "version": "1.0.0-alpha.194",
4
4
  "description": "Toa Core",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -21,13 +21,10 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@rsql/parser": "1.2.4",
24
- "@toa.io/console": "1.0.0-alpha.19",
25
- "@toa.io/generic": "1.0.0-alpha.19",
26
- "@toa.io/yaml": "1.0.0-alpha.19",
27
- "error-value": "0.3.0"
24
+ "@toa.io/generic": "1.0.0-alpha.173",
25
+ "@toa.io/yaml": "1.0.0-alpha.182",
26
+ "error-value": "0.3.0",
27
+ "openspan": "1.0.0-alpha.173"
28
28
  },
29
- "devDependencies": {
30
- "clone-deep": "4.0.1"
31
- },
32
- "gitHead": "03c7af7e331dd5774ee53c0e9530535357bac8d2"
29
+ "gitHead": "b9f5743c5ee483c2b293db60b22048d3c2cf6107"
33
30
  }
package/src/call.js CHANGED
@@ -18,6 +18,8 @@ class Call extends Connector {
18
18
  }
19
19
 
20
20
  async invoke (request = {}) {
21
+ request.input ??= null
22
+
21
23
  this.#contract.fit(request)
22
24
 
23
25
  // avoid validation on the recipient's side
@@ -37,6 +39,10 @@ class Call extends Connector {
37
39
  return reply.output
38
40
  }
39
41
  }
42
+
43
+ explain () {
44
+ return this.#contract.discovery
45
+ }
40
46
  }
41
47
 
42
48
  exports.Call = Call
package/src/cascade.js CHANGED
@@ -1,16 +1,15 @@
1
1
  'use strict'
2
2
 
3
- const { merge } = require('@toa.io/generic')
4
3
  const { Connector } = require('./connector')
5
4
 
6
5
  class Cascade extends Connector {
7
- #bridges
6
+ // #bridges
8
7
  #last
9
8
 
10
9
  constructor (bridges) {
11
10
  super()
12
11
 
13
- this.#bridges = bridges
12
+ // this.#bridges = bridges
14
13
  this.#last = bridges[bridges.length - 1]
15
14
 
16
15
  this.depends(bridges)
package/src/component.js CHANGED
@@ -1,40 +1,37 @@
1
1
  'use strict'
2
2
 
3
- const { console } = require('@toa.io/console')
3
+ const assert = require('node:assert')
4
+ const { console } = require('openspan')
4
5
  const { Connector } = require('./connector')
5
- const { NotImplementedException } = require('./exceptions')
6
6
 
7
- /**
8
- * @implements {toa.core.Component}
9
- */
10
7
  class Component extends Connector {
11
8
  locator
12
9
 
13
- #operations
10
+ /** @protected */
11
+ operations
14
12
 
15
13
  constructor (locator, operations) {
16
14
  super()
17
15
 
18
16
  this.locator = locator
19
- this.#operations = operations
17
+ this.operations = operations
20
18
 
21
19
  Object.values(operations).forEach((operation) => this.depends(operation))
22
20
  }
23
21
 
24
- async open () {
25
- console.info(`Runtime '${this.locator.id}' connected`)
26
- }
22
+ async invoke (endpoint, request) {
23
+ assert.ok(endpoint in this.operations,
24
+ `Endpoint '${endpoint}' is not provided by '${this.locator.id}'`)
27
25
 
28
- async dispose () {
29
- console.info(`Runtime '${this.locator.id}' disconnected`)
30
- }
26
+ const reply = await this.operations[endpoint].invoke(request)
31
27
 
32
- async invoke (endpoint, request) {
33
- if (!(endpoint in this.#operations)) {
34
- throw new NotImplementedException(`Endpoint '${endpoint}' not found in '${this.locator.id}'`)
35
- }
28
+ if (reply?.exception !== undefined)
29
+ console.error('Failed to execute operation', {
30
+ endpoint: `${this.locator.id}.${endpoint}`,
31
+ exception: reply.exception
32
+ })
36
33
 
37
- return this.#operations[endpoint].invoke(request)
34
+ return reply
38
35
  }
39
36
  }
40
37
 
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { console } = require('@toa.io/console')
3
+ const { console } = require('openspan')
4
4
  const { Connector } = require('./connector')
5
5
 
6
6
  class Composition extends Connector {
package/src/connector.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { console } = require('@toa.io/console')
3
+ const { console } = require('openspan')
4
4
  const { newid } = require('@toa.io/generic')
5
5
 
6
6
  /**
@@ -86,8 +86,6 @@ class Connector {
86
86
 
87
87
  this.#disconnecting = undefined
88
88
 
89
- console.debug(`Connecting '${this.id}' with ${this.#dependencies.length} dependencies...`)
90
-
91
89
  this.#connecting = (async () => {
92
90
  await Promise.all(this.#dependencies.map((connector) => connector.connect()))
93
91
  await this.open()
@@ -100,8 +98,6 @@ class Connector {
100
98
  await this.disconnect(true)
101
99
  throw e
102
100
  }
103
-
104
- console.debug(`Connector '${this.id}' connected`)
105
101
  }
106
102
 
107
103
  /**
@@ -132,7 +128,8 @@ class Connector {
132
128
  const interval = setInterval(() => {
133
129
  const delay = +new Date() - start
134
130
 
135
- if (delay > DELAY) console.warn(`Connector ${this.id} still disconnecting (${delay})`)
131
+ if (delay > DELAY)
132
+ console.warn(`Connector ${this.id} still disconnecting (${delay})`)
136
133
  }, DELAY)
137
134
 
138
135
  if (interrupt !== true) await this.close()
@@ -145,8 +142,6 @@ class Connector {
145
142
  })()
146
143
 
147
144
  await this.#disconnecting
148
-
149
- console.debug(`Connector '${this.id}' disconnected`)
150
145
  }
151
146
 
152
147
  async reconnect () {
@@ -0,0 +1,22 @@
1
+ 'use strict'
2
+
3
+ const { SystemException } = require('../exceptions')
4
+
5
+ class Contract {
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)
16
+ throw new this.constructor.Exception(error, value)
17
+ }
18
+
19
+ static Exception = SystemException
20
+ }
21
+
22
+ exports.Contract = Contract
@@ -1,22 +1,39 @@
1
1
  'use strict'
2
2
 
3
3
  const schemas = require('./schemas')
4
- const { Conditions } = require('./conditions')
4
+ const { Contract } = require('./contract')
5
5
  const { ResponseContractException } = require('../exceptions')
6
6
 
7
- class Reply extends Conditions {
7
+ class Reply extends Contract {
8
8
  static Exception = ResponseContractException
9
9
 
10
- /**
11
- * @returns {toa.schema.JSON}
12
- */
13
- static schema (output, error) {
10
+ static schema (output, errors) {
14
11
  const schema = { type: 'object', properties: {}, additionalProperties: false }
15
12
 
16
- if (output !== undefined) schema.properties.output = output
13
+ if (output !== undefined) {
14
+ if (output.type === 'object')
15
+ output.additionalProperties = true
16
+ else if (output.type === 'array' && output.items?.type === 'object')
17
+ output.items.additionalProperties = true
17
18
 
18
- if (error !== undefined) schema.properties.error = error
19
- else schema.properties.error = schemas.error
19
+ schema.properties.output = output
20
+ }
21
+
22
+ if (errors !== undefined)
23
+ schema.properties.error = {
24
+ type: 'object',
25
+ properties: {
26
+ code: {
27
+ enum: errors
28
+ },
29
+ message: {
30
+ type: 'string'
31
+ }
32
+ },
33
+ required: ['code']
34
+ }
35
+ else
36
+ schema.properties.error = schemas.error
20
37
 
21
38
  return schema
22
39
  }
@@ -2,9 +2,20 @@
2
2
 
3
3
  const schemas = require('./schemas')
4
4
  const { RequestContractException } = require('../exceptions')
5
- const { Conditions } = require('./conditions')
5
+ const { Contract } = require('./contract')
6
+
7
+ class Request extends Contract {
8
+ /** @readonly */
9
+ discovery = {}
10
+
11
+ constructor (schema, definition) {
12
+ super(schema)
13
+
14
+ for (const key of ['input', 'output', 'errors'])
15
+ if (definition[key] !== undefined)
16
+ this.discovery[key] = definition[key]
17
+ }
6
18
 
7
- class Request extends Conditions {
8
19
  static Exception = RequestContractException
9
20
 
10
21
  /**
@@ -13,7 +24,7 @@ class Request extends Conditions {
13
24
  static schema (definition, entity) {
14
25
  const schema = {
15
26
  type: 'object',
16
- properties: { authentic: { type: 'boolean' } },
27
+ properties: { authentic: { type: 'boolean' }, task: { type: 'boolean' } },
17
28
  additionalProperties: true
18
29
  }
19
30
 
@@ -22,9 +33,8 @@ class Request extends Conditions {
22
33
  if (definition.input !== undefined) {
23
34
  schema.properties.input = definition.input
24
35
  required.push('input')
25
- } else {
36
+ } else
26
37
  schema.properties.input = { type: 'null' }
27
- }
28
38
 
29
39
  if (entity === undefined)
30
40
  definition.query = false
@@ -1,11 +1,15 @@
1
1
  type: object
2
2
  properties:
3
- id: { }
3
+ id: {}
4
4
  version:
5
5
  type: integer
6
6
  minimum: 0
7
7
  criteria:
8
8
  type: string
9
+ search:
10
+ type: string
11
+ sample:
12
+ type: number
9
13
  omit:
10
14
  type: integer
11
15
  minimum: 0
@@ -27,4 +31,6 @@ properties:
27
31
  type: string
28
32
  not:
29
33
  const: id
34
+ deleted:
35
+ type: boolean
30
36
  additionalProperties: false
package/src/discovery.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { console } = require('@toa.io/console')
3
+ const { console } = require('openspan')
4
4
  const { Connector } = require('./connector')
5
5
 
6
6
  class Discovery extends Connector {
@@ -25,14 +25,11 @@ class Discovery extends Connector {
25
25
  this.depends(this.#lookups[id])
26
26
  }
27
27
 
28
- console.debug(`Sending lookup request to '${id}'`)
29
-
30
- const warning = () => console.warn(`Waiting for lookup response from '${id}'...`)
28
+ const warning = () => console.warn(`Waiting for lookup response`, { component: id })
31
29
  const timeout = setTimeout(warning, TIMEOUT)
32
30
 
33
31
  const output = await this.#lookups[id].invoke()
34
32
 
35
- console.debug(`Lookup response from '${id}' received`)
36
33
  clearTimeout(timeout)
37
34
 
38
35
  return output
package/src/effect.js ADDED
@@ -0,0 +1,19 @@
1
+ 'use strict'
2
+
3
+ const { Observation } = require('./observation')
4
+
5
+ class Effect extends Observation {
6
+
7
+ async acquire (store) {
8
+ const { query, entity, input } = store.request
9
+
10
+ if (entity === undefined)
11
+ return super.acquire(store)
12
+
13
+ store.scope = await this.scope.ensure(query, entity, input)
14
+ store.state = store.scope.get()
15
+ }
16
+
17
+ }
18
+
19
+ exports.Effect = Effect
@@ -1,10 +1,5 @@
1
1
  'use strict'
2
2
 
3
- const {
4
- merge,
5
- overwrite,
6
- newid
7
- } = require('@toa.io/generic')
8
3
  const { EntityContractException } = require('../exceptions')
9
4
 
10
5
  class Changeset {
@@ -18,7 +13,7 @@ class Changeset {
18
13
  this.query = query
19
14
 
20
15
  this.#schema = schema
21
- this.#state = schema.system()
16
+ this.#state = {}
22
17
  }
23
18
 
24
19
  get () {
@@ -26,10 +21,12 @@ class Changeset {
26
21
  }
27
22
 
28
23
  set (value) {
29
- const error = this.#schema.adapt(value)
24
+ const error = this.#schema.match(value)
30
25
 
31
- if (error !== null) throw new EntityContractException(error)
26
+ if (error !== null)
27
+ throw new EntityContractException(error, value)
32
28
 
29
+ delete value._version
33
30
  value._updated = Date.now()
34
31
 
35
32
  this.#state = value
@@ -1,23 +1,22 @@
1
1
  'use strict'
2
2
 
3
- const {
4
- difference,
5
- newid
6
- } = require('@toa.io/generic')
7
-
8
- const { EntityContractException } = require('../exceptions')
3
+ const { difference, newid } = require('@toa.io/generic')
4
+ const { EntityContractException, EntityGuardException } = require('../exceptions')
9
5
 
10
6
  class Entity {
7
+ deleted = false
11
8
  #schema
9
+ #guards
12
10
  #origin = null
13
11
  #state
14
12
 
15
- constructor (schema, argument) {
13
+ constructor (schema, argument, guards) {
16
14
  this.#schema = schema
15
+ this.#guards = guards
17
16
 
18
17
  if (typeof argument === 'object') {
19
18
  const object = structuredClone(argument)
20
- this.set(object)
19
+ this.#set(object)
21
20
  this.#origin = argument
22
21
  } else {
23
22
  const id = argument === undefined ? newid() : argument
@@ -29,37 +28,62 @@ class Entity {
29
28
  return this.#state
30
29
  }
31
30
 
32
- set (value) {
33
- const error = this.#schema.fit(value)
31
+ set (value, optional = false) {
32
+ const error = optional ? this.#schema.fitOptional(value) : this.#schema.fit(value)
33
+
34
+ if (error !== null)
35
+ throw new EntityContractException(error, value)
34
36
 
35
- if (error !== null) throw new EntityContractException(error)
37
+ if (!optional)
38
+ this.#guard(value)
36
39
 
37
40
  this.#set(value)
38
41
  }
39
42
 
40
- event () {
43
+ event (input = undefined) {
41
44
  return {
42
45
  origin: this.#origin,
43
46
  state: this.#state,
44
- changeset: this.#origin === null ? this.#state : difference(this.#origin, this.#state)
47
+ changeset: this.#origin === null ? this.#state : difference(this.#origin, this.#state),
48
+ trailers: this.#state._trailers,
49
+ input
45
50
  }
46
51
  }
47
52
 
48
53
  #init (id) {
49
- const value = {
50
- ...this.#schema.defaults({ id }),
51
- _version: 0,
52
- _created: Date.now()
53
- }
54
+ const value = { id, _version: 0 }
54
55
 
55
- this.#set(value)
56
+ this.set(value, true)
57
+ }
58
+
59
+ #guard (value) {
60
+ if (this.#guards === undefined)
61
+ return
62
+
63
+ for (const guard of this.#guards) {
64
+ const ok = guard.fit(value)
65
+
66
+ if (ok === false)
67
+ throw new EntityGuardException(guard.name, value)
68
+ }
56
69
  }
57
70
 
58
71
  #set (value) {
59
- Object.defineProperty(value, 'id', {
60
- writable: false,
61
- configurable: false
62
- })
72
+ if (!('_trailers' in value))
73
+ Object.defineProperty(value, '_trailers', {
74
+ writable: false,
75
+ configurable: false,
76
+ enumerable: false,
77
+ value: {}
78
+ })
79
+
80
+ if (!('_created' in value)) {
81
+ value._created = Date.now()
82
+ value._updated ??= value._created
83
+ }
84
+
85
+ if ('_deleted' in value && value._deleted !== null)
86
+ this.deleted = true
63
87
 
64
88
  if (this.#state !== undefined) {
65
89
  value._updated = Date.now()
@@ -1,22 +1,30 @@
1
1
  'use strict'
2
2
 
3
+ const { newid } = require('@toa.io/generic')
4
+
3
5
  const { Entity } = require('./entity')
4
6
  const { EntitySet } = require('./set')
5
7
  const { Changeset } = require('./changeset')
6
8
 
7
9
  class Factory {
8
10
  #schema
11
+ #guards
9
12
 
10
- constructor (schema) {
13
+ constructor (schema, guards) {
11
14
  this.#schema = schema
15
+ this.#guards = guards
16
+ }
17
+
18
+ fit (values) {
19
+ this.#schema.validate({ id: newid(), ...values }, 'Entity')
12
20
  }
13
21
 
14
22
  init (id) {
15
- return new Entity(this.#schema, id)
23
+ return new Entity(this.#schema, id, this.#guards)
16
24
  }
17
25
 
18
26
  object (record) {
19
- return new Entity(this.#schema, record)
27
+ return new Entity(this.#schema, record, this.#guards)
20
28
  }
21
29
 
22
30
  objects (recordset) {
package/src/exceptions.js CHANGED
@@ -4,7 +4,6 @@ const { swap } = require('@toa.io/generic')
4
4
 
5
5
  const codes = {
6
6
  System: 0,
7
- NotImplemented: 10,
8
7
 
9
8
  Contract: 200,
10
9
  RequestSyntax: 201,
@@ -12,6 +11,7 @@ const codes = {
12
11
  RequestConflict: 203,
13
12
  ResponseContract: 211,
14
13
  EntityContract: 212,
14
+ EntityGuard: 213,
15
15
  QuerySyntax: 221,
16
16
 
17
17
  State: 300,
@@ -32,9 +32,12 @@ class Exception {
32
32
  code
33
33
  message
34
34
 
35
- constructor (code, message) {
35
+ constructor (code, message, cause) {
36
36
  this.code = code
37
37
  this.message = message
38
+
39
+ if (cause !== undefined)
40
+ this.cause = cause
38
41
  }
39
42
  }
40
43
 
@@ -49,31 +52,30 @@ class SystemException extends Exception {
49
52
  }
50
53
 
51
54
  class ContractException extends Exception {
52
- keyword
53
- property
54
- schema
55
- path
56
-
57
- constructor (code, error) {
58
- super(code || codes.Contract, error.message)
59
-
60
- this.keyword = error.keyword
61
- this.property = error.property
62
- this.schema = error.schema
63
- this.path = error.path
55
+ constructor (code, error, cause) {
56
+ super(code || codes.Contract, typeof error === 'string' ? error : error?.message, cause)
57
+
58
+ if (typeof error === 'object' && error !== null)
59
+ for (const k of ['keyword', 'property', 'schema', 'path', 'params'])
60
+ if (k in error)
61
+ this[k] = error[k]
64
62
  }
65
63
  }
66
64
 
67
65
  class RequestContractException extends ContractException {
68
- constructor (error) { super(codes.RequestContract, error) }
66
+ constructor (error, cause) { super(codes.RequestContract, error, cause) }
69
67
  }
70
68
 
71
69
  class ResponseContractException extends ContractException {
72
- constructor (error) { super(codes.ResponseContract, error) }
70
+ constructor (error, cause) { super(codes.ResponseContract, error, cause) }
73
71
  }
74
72
 
75
73
  class EntityContractException extends ContractException {
76
- constructor (error) { super(codes.EntityContract, error) }
74
+ constructor (error, cause) { super(codes.EntityContract, error, cause) }
75
+ }
76
+
77
+ class EntityGuardException extends ContractException {
78
+ constructor (name, cause) { super(codes.EntityGuard, name, cause) }
77
79
  }
78
80
 
79
81
  // #region exports
@@ -82,14 +84,19 @@ exports.SystemException = SystemException
82
84
  exports.RequestContractException = RequestContractException
83
85
  exports.ResponseContractException = ResponseContractException
84
86
  exports.EntityContractException = EntityContractException
87
+ exports.EntityGuardException = EntityGuardException
85
88
 
86
89
  for (const [name, code] of Object.entries(codes)) {
87
90
  const classname = name + 'Exception'
88
91
 
89
92
  if (exports[classname] === undefined) {
90
93
  exports[classname] = class extends Exception {
91
- constructor (message) {
92
- super(code, message ?? classname)
94
+ constructor (message, cause) {
95
+ message = message
96
+ ? `${classname}: ${message}`
97
+ : classname
98
+
99
+ super(code, message ?? classname, cause)
93
100
  }
94
101
  }
95
102
  }
package/src/exposition.js CHANGED
@@ -20,8 +20,9 @@ class Exposition extends Connector {
20
20
  }
21
21
 
22
22
  const expose = (manifest) => {
23
- const { namespace, name, operations, events, entity } = manifest
24
- return { namespace, name, operations, events, entity }
23
+ const { namespace, name, entity, operations, events } = manifest
24
+
25
+ return { namespace, name, entity, operations, events }
25
26
  }
26
27
 
27
28
  exports.Exposition = Exposition
package/src/guard.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ class Guard {
4
+ name
5
+ #bridge
6
+
7
+ constructor (name, bridge) {
8
+ this.name = name
9
+ this.#bridge = bridge
10
+ }
11
+
12
+ fit (state) {
13
+ return this.#bridge.fit(state)
14
+ }
15
+ }
16
+
17
+ exports.Guard = Guard