@toa.io/core 1.0.0-alpha.29 → 1.0.0-alpha.291

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 (304) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/package.json +22 -12
  3. package/source/assignment.ts +27 -0
  4. package/source/call.ts +63 -0
  5. package/source/cascade.ts +32 -0
  6. package/source/component.ts +83 -0
  7. package/source/composition.ts +27 -0
  8. package/source/connector.ts +217 -0
  9. package/source/context.ts +68 -0
  10. package/source/contract/contract.ts +23 -0
  11. package/source/contract/index.ts +2 -0
  12. package/source/contract/reply.ts +56 -0
  13. package/source/contract/request.ts +140 -0
  14. package/source/contract/schemas.ts +39 -0
  15. package/source/discovery.ts +53 -0
  16. package/source/effect.ts +14 -0
  17. package/source/emission.ts +21 -0
  18. package/source/entities/changeset.ts +37 -0
  19. package/source/entities/entity.ts +137 -0
  20. package/source/entities/factory.ts +52 -0
  21. package/source/entities/index.ts +4 -0
  22. package/source/entities/newid.ts +9 -0
  23. package/source/entities/set.ts +27 -0
  24. package/source/event.ts +57 -0
  25. package/source/exceptions.ts +133 -0
  26. package/source/exposition.ts +33 -0
  27. package/source/guard.ts +16 -0
  28. package/source/index.ts +36 -0
  29. package/source/locator.ts +37 -0
  30. package/source/observation.ts +16 -0
  31. package/source/operation.ts +139 -0
  32. package/source/outbox/index.ts +1 -0
  33. package/source/outbox/outbox.ts +341 -0
  34. package/source/query/criteria.ts +90 -0
  35. package/source/query/options.ts +35 -0
  36. package/source/query.ts +67 -0
  37. package/source/receiver.ts +120 -0
  38. package/source/reflection.ts +20 -0
  39. package/source/remote.ts +20 -0
  40. package/source/state.ts +179 -0
  41. package/source/transition.ts +79 -0
  42. package/source/transmission.ts +38 -0
  43. package/source/types/atomicity.ts +63 -0
  44. package/source/types/bindings.ts +59 -0
  45. package/source/types/bridges.ts +54 -0
  46. package/source/types/extensions.ts +89 -0
  47. package/source/types/index.ts +14 -0
  48. package/{types/message.d.ts → source/types/message.ts} +2 -0
  49. package/source/types/operations.ts +31 -0
  50. package/source/types/outbox.ts +35 -0
  51. package/source/types/receiver.ts +10 -0
  52. package/source/types/request.ts +54 -0
  53. package/source/types/state.ts +9 -0
  54. package/source/types/storages.ts +134 -0
  55. package/source/unmanaged.ts +8 -0
  56. package/test/call.fixtures.js +10 -12
  57. package/test/call.test.js +42 -9
  58. package/test/component.fixtures.js +5 -8
  59. package/test/component.test.js +27 -10
  60. package/test/connector.fixtures.js +29 -13
  61. package/test/connector.test.js +128 -38
  62. package/test/context.fixtures.js +8 -12
  63. package/test/context.test.js +19 -8
  64. package/test/contract/conditions.test.js +15 -9
  65. package/test/contract/contract.fixtures.js +11 -16
  66. package/test/contract/request.test.js +130 -46
  67. package/test/discovery.test.js +71 -0
  68. package/test/emission.fixtures.js +6 -10
  69. package/test/emission.test.js +37 -12
  70. package/test/entities/entity.fixtures.js +11 -16
  71. package/test/entities/entity.test.js +105 -50
  72. package/test/entities/factory.fixtures.js +26 -13
  73. package/test/entities/factory.test.js +68 -17
  74. package/test/entities/set.fixtures.js +6 -8
  75. package/test/entities/set.test.js +6 -5
  76. package/test/event.fixtures.js +9 -14
  77. package/test/event.test.js +78 -34
  78. package/test/locator.test.js +32 -25
  79. package/test/outbox.test.js +202 -0
  80. package/test/query.fixtures.js +1 -5
  81. package/test/query.test.js +111 -14
  82. package/test/receiver.fixtures.js +9 -12
  83. package/test/receiver.test.js +80 -31
  84. package/test/reflection.test.js +9 -8
  85. package/test/state.fixtures.js +33 -31
  86. package/test/state.test.js +135 -19
  87. package/test/transmission.fixtures.js +4 -6
  88. package/test/transmission.test.js +47 -16
  89. package/transpiled/assignment.d.ts +6 -0
  90. package/transpiled/assignment.js +20 -0
  91. package/transpiled/assignment.js.map +1 -0
  92. package/transpiled/call.d.ts +10 -0
  93. package/transpiled/call.js +52 -0
  94. package/transpiled/call.js.map +1 -0
  95. package/transpiled/cascade.d.ts +7 -0
  96. package/transpiled/cascade.js +28 -0
  97. package/transpiled/cascade.js.map +1 -0
  98. package/transpiled/component.d.ts +15 -0
  99. package/transpiled/component.js +61 -0
  100. package/transpiled/component.js.map +1 -0
  101. package/transpiled/composition.d.ts +6 -0
  102. package/transpiled/composition.js +23 -0
  103. package/transpiled/composition.js.map +1 -0
  104. package/transpiled/connector.d.ts +47 -0
  105. package/transpiled/connector.js +185 -0
  106. package/transpiled/connector.js.map +1 -0
  107. package/transpiled/context.d.ts +18 -0
  108. package/transpiled/context.js +42 -0
  109. package/transpiled/context.js.map +1 -0
  110. package/transpiled/contract/contract.d.ts +10 -0
  111. package/transpiled/contract/contract.js +14 -0
  112. package/transpiled/contract/contract.js.map +1 -0
  113. package/transpiled/contract/index.d.ts +2 -0
  114. package/transpiled/contract/index.js +3 -0
  115. package/transpiled/contract/index.js.map +1 -0
  116. package/transpiled/contract/reply.d.ts +7 -0
  117. package/transpiled/contract/reply.js +48 -0
  118. package/transpiled/contract/reply.js.map +1 -0
  119. package/transpiled/contract/request.d.ts +29 -0
  120. package/transpiled/contract/request.js +103 -0
  121. package/transpiled/contract/request.js.map +1 -0
  122. package/transpiled/contract/schemas.d.ts +4 -0
  123. package/transpiled/contract/schemas.js +36 -0
  124. package/transpiled/contract/schemas.js.map +1 -0
  125. package/transpiled/discovery.d.ts +12 -0
  126. package/transpiled/discovery.js +34 -0
  127. package/transpiled/discovery.js.map +1 -0
  128. package/transpiled/effect.d.ts +5 -0
  129. package/transpiled/effect.js +11 -0
  130. package/transpiled/effect.js.map +1 -0
  131. package/transpiled/emission.d.ts +8 -0
  132. package/transpiled/emission.js +14 -0
  133. package/transpiled/emission.js.map +1 -0
  134. package/transpiled/entities/changeset.d.ts +10 -0
  135. package/transpiled/entities/changeset.js +27 -0
  136. package/transpiled/entities/changeset.js.map +1 -0
  137. package/transpiled/entities/entity.d.ts +19 -0
  138. package/transpiled/entities/entity.js +109 -0
  139. package/transpiled/entities/entity.js.map +1 -0
  140. package/transpiled/entities/factory.d.ts +22 -0
  141. package/transpiled/entities/factory.js +34 -0
  142. package/transpiled/entities/factory.js.map +1 -0
  143. package/transpiled/entities/index.d.ts +4 -0
  144. package/transpiled/entities/index.js +5 -0
  145. package/transpiled/entities/index.js.map +1 -0
  146. package/transpiled/entities/newid.d.ts +1 -0
  147. package/transpiled/entities/newid.js +7 -0
  148. package/transpiled/entities/newid.js.map +1 -0
  149. package/transpiled/entities/set.d.ts +10 -0
  150. package/transpiled/entities/set.js +19 -0
  151. package/transpiled/entities/set.js.map +1 -0
  152. package/transpiled/event.d.ts +14 -0
  153. package/transpiled/event.js +38 -0
  154. package/transpiled/event.js.map +1 -0
  155. package/transpiled/exceptions.d.ts +61 -0
  156. package/transpiled/exceptions.js +93 -0
  157. package/transpiled/exceptions.js.map +1 -0
  158. package/transpiled/exposition.d.ts +18 -0
  159. package/transpiled/exposition.js +18 -0
  160. package/transpiled/exposition.js.map +1 -0
  161. package/transpiled/guard.d.ts +7 -0
  162. package/transpiled/guard.js +12 -0
  163. package/transpiled/guard.js.map +1 -0
  164. package/transpiled/index.d.ts +35 -0
  165. package/transpiled/index.js +29 -0
  166. package/transpiled/index.js.map +1 -0
  167. package/transpiled/locator.d.ts +12 -0
  168. package/transpiled/locator.js +32 -0
  169. package/transpiled/locator.js.map +1 -0
  170. package/transpiled/observation.d.ts +5 -0
  171. package/transpiled/observation.js +12 -0
  172. package/transpiled/observation.js.map +1 -0
  173. package/transpiled/operation.d.ts +49 -0
  174. package/transpiled/operation.js +78 -0
  175. package/transpiled/operation.js.map +1 -0
  176. package/transpiled/outbox/index.d.ts +1 -0
  177. package/transpiled/outbox/index.js +2 -0
  178. package/transpiled/outbox/index.js.map +1 -0
  179. package/transpiled/outbox/outbox.d.ts +46 -0
  180. package/transpiled/outbox/outbox.js +268 -0
  181. package/transpiled/outbox/outbox.js.map +1 -0
  182. package/transpiled/query/criteria.d.ts +6 -0
  183. package/transpiled/query/criteria.js +74 -0
  184. package/transpiled/query/criteria.js.map +1 -0
  185. package/transpiled/query/options.d.ts +3 -0
  186. package/transpiled/query/options.js +27 -0
  187. package/transpiled/query/options.js.map +1 -0
  188. package/transpiled/query.d.ts +9 -0
  189. package/transpiled/query.js +57 -0
  190. package/transpiled/query.js.map +1 -0
  191. package/transpiled/receiver.d.ts +20 -0
  192. package/transpiled/receiver.js +92 -0
  193. package/transpiled/receiver.js.map +1 -0
  194. package/transpiled/reflection.d.ts +9 -0
  195. package/transpiled/reflection.js +13 -0
  196. package/transpiled/reflection.js.map +1 -0
  197. package/transpiled/remote.d.ts +10 -0
  198. package/transpiled/remote.js +12 -0
  199. package/transpiled/remote.js.map +1 -0
  200. package/transpiled/state.d.ts +33 -0
  201. package/transpiled/state.js +126 -0
  202. package/transpiled/state.js.map +1 -0
  203. package/transpiled/transition.d.ts +14 -0
  204. package/transpiled/transition.js +51 -0
  205. package/transpiled/transition.js.map +1 -0
  206. package/transpiled/transmission.d.ts +8 -0
  207. package/transpiled/transmission.js +30 -0
  208. package/transpiled/transmission.js.map +1 -0
  209. package/transpiled/types/atomicity.d.ts +56 -0
  210. package/transpiled/types/atomicity.js +2 -0
  211. package/transpiled/types/atomicity.js.map +1 -0
  212. package/transpiled/types/bindings.d.ts +42 -0
  213. package/transpiled/types/bindings.js +2 -0
  214. package/transpiled/types/bindings.js.map +1 -0
  215. package/transpiled/types/bridges.d.ts +38 -0
  216. package/transpiled/types/bridges.js +2 -0
  217. package/transpiled/types/bridges.js.map +1 -0
  218. package/transpiled/types/extensions.d.ts +62 -0
  219. package/transpiled/types/extensions.js +2 -0
  220. package/transpiled/types/extensions.js.map +1 -0
  221. package/transpiled/types/index.d.ts +13 -0
  222. package/transpiled/types/index.js +8 -0
  223. package/transpiled/types/index.js.map +1 -0
  224. package/transpiled/types/message.d.ts +5 -0
  225. package/transpiled/types/message.js +2 -0
  226. package/transpiled/types/message.js.map +1 -0
  227. package/transpiled/types/operations.d.ts +15 -0
  228. package/transpiled/types/operations.js +2 -0
  229. package/transpiled/types/operations.js.map +1 -0
  230. package/transpiled/types/outbox.d.ts +28 -0
  231. package/transpiled/types/outbox.js +2 -0
  232. package/transpiled/types/outbox.js.map +1 -0
  233. package/transpiled/types/receiver.d.ts +9 -0
  234. package/transpiled/types/receiver.js +2 -0
  235. package/transpiled/types/receiver.js.map +1 -0
  236. package/transpiled/types/request.d.ts +55 -0
  237. package/transpiled/types/request.js +2 -0
  238. package/transpiled/types/request.js.map +1 -0
  239. package/transpiled/types/state.d.ts +9 -0
  240. package/transpiled/types/state.js +2 -0
  241. package/transpiled/types/state.js.map +1 -0
  242. package/transpiled/types/storages.d.ts +111 -0
  243. package/transpiled/types/storages.js +2 -0
  244. package/transpiled/types/storages.js.map +1 -0
  245. package/transpiled/unmanaged.d.ts +5 -0
  246. package/transpiled/unmanaged.js +7 -0
  247. package/transpiled/unmanaged.js.map +1 -0
  248. package/tsconfig.json +10 -0
  249. package/tsconfig.tsbuildinfo +1 -0
  250. package/src/assignment.js +0 -31
  251. package/src/call.js +0 -42
  252. package/src/cascade.js +0 -36
  253. package/src/component.js +0 -41
  254. package/src/composition.js +0 -25
  255. package/src/connector.js +0 -183
  256. package/src/context.js +0 -55
  257. package/src/contract/conditions.js +0 -21
  258. package/src/contract/index.js +0 -7
  259. package/src/contract/reply.js +0 -25
  260. package/src/contract/request.js +0 -67
  261. package/src/contract/schemas/error.yaml +0 -9
  262. package/src/contract/schemas/index.js +0 -7
  263. package/src/contract/schemas/query.yaml +0 -30
  264. package/src/discovery.js +0 -44
  265. package/src/emission.js +0 -23
  266. package/src/entities/changeset.js +0 -43
  267. package/src/entities/entity.js +0 -73
  268. package/src/entities/factory.js +0 -33
  269. package/src/entities/index.js +0 -5
  270. package/src/entities/set.js +0 -15
  271. package/src/event.js +0 -40
  272. package/src/exceptions.js +0 -100
  273. package/src/exposition.js +0 -27
  274. package/src/index.js +0 -47
  275. package/src/locator.js +0 -54
  276. package/src/observation.js +0 -20
  277. package/src/operation.js +0 -69
  278. package/src/query/criteria.js +0 -41
  279. package/src/query/options.js +0 -40
  280. package/src/query.js +0 -41
  281. package/src/receiver.js +0 -65
  282. package/src/reflection.js +0 -28
  283. package/src/remote.js +0 -17
  284. package/src/state.js +0 -99
  285. package/src/transition.js +0 -73
  286. package/src/transmission.js +0 -33
  287. package/types/bindings.d.ts +0 -40
  288. package/types/bridges.ts +0 -30
  289. package/types/component.d.ts +0 -9
  290. package/types/connector.d.ts +0 -18
  291. package/types/context.d.ts +0 -21
  292. package/types/entity.d.ts +0 -46
  293. package/types/event.d.ts +0 -11
  294. package/types/exception.d.ts +0 -4
  295. package/types/extensions.d.ts +0 -29
  296. package/types/index.ts +0 -14
  297. package/types/locator.d.ts +0 -13
  298. package/types/operations.d.ts +0 -2
  299. package/types/query.d.ts +0 -15
  300. package/types/receiver.d.ts +0 -12
  301. package/types/reflection.d.ts +0 -14
  302. package/types/request.d.ts +0 -23
  303. package/types/state.d.ts +0 -35
  304. package/types/storages.d.ts +0 -71
package/src/context.js DELETED
@@ -1,55 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('./connector')
4
-
5
- /**
6
- * @implements {toa.core.Context}
7
- */
8
- class Context extends Connector {
9
- aspects
10
-
11
- #local
12
- #discover
13
- #remotes = {}
14
-
15
- constructor (local, discover, aspects = []) {
16
- super()
17
-
18
- this.aspects = aspects
19
-
20
- this.#local = local
21
- this.#discover = discover
22
-
23
- this.depends(local)
24
-
25
- if (aspects.length > 0) this.depends(aspects)
26
- }
27
-
28
- async apply (endpoint, request) {
29
- return this.#local.invoke(endpoint, request)
30
- }
31
-
32
- async call (namespace, name, endpoint, request) {
33
- const remote = await this.#remote(namespace, name)
34
-
35
- return remote.invoke(endpoint, request)
36
- }
37
-
38
- async #remote (namespace, name) {
39
- const key = namespace + '.' + name
40
-
41
- this.#remotes[key] ??= this.#connect(namespace, name)
42
-
43
- return this.#remotes[key]
44
- }
45
-
46
- async #connect (namespace, name) {
47
- const remote = await this.#discover(namespace, name)
48
-
49
- this.depends(remote)
50
-
51
- return remote
52
- }
53
- }
54
-
55
- exports.Context = Context
@@ -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
@@ -1,7 +0,0 @@
1
- 'use strict'
2
-
3
- const { Request } = require('./request')
4
- const { Reply } = require('./reply')
5
-
6
- exports.Request = Request
7
- exports.Reply = Reply
@@ -1,25 +0,0 @@
1
- 'use strict'
2
-
3
- const schemas = require('./schemas')
4
- const { Conditions } = require('./conditions')
5
- const { ResponseContractException } = require('../exceptions')
6
-
7
- class Reply extends Conditions {
8
- static Exception = ResponseContractException
9
-
10
- /**
11
- * @returns {toa.schema.JSON}
12
- */
13
- static schema (output, error) {
14
- const schema = { type: 'object', properties: {}, additionalProperties: false }
15
-
16
- if (output !== undefined) schema.properties.output = output
17
-
18
- if (error !== undefined) schema.properties.error = error
19
- else schema.properties.error = schemas.error
20
-
21
- return schema
22
- }
23
- }
24
-
25
- exports.Reply = Reply
@@ -1,67 +0,0 @@
1
- 'use strict'
2
-
3
- const schemas = require('./schemas')
4
- const { RequestContractException } = require('../exceptions')
5
- const { Conditions } = require('./conditions')
6
-
7
- class Request extends Conditions {
8
- static Exception = RequestContractException
9
-
10
- /**
11
- * @returns {toa.schema.JSON}
12
- */
13
- static schema (definition, entity) {
14
- const schema = {
15
- type: 'object',
16
- properties: { authentic: { type: 'boolean' } },
17
- additionalProperties: true
18
- }
19
-
20
- const required = []
21
-
22
- if (definition.input !== undefined) {
23
- schema.properties.input = definition.input
24
- required.push('input')
25
- } else {
26
- schema.properties.input = { type: 'null' }
27
- }
28
-
29
- if (entity === undefined)
30
- definition.query = false
31
-
32
- if (definition.query === true)
33
- required.push('query')
34
-
35
- if (definition.query === false)
36
- schema.properties.query = { type: 'null' }
37
-
38
- if (definition.query !== false) {
39
- const query = structuredClone(schemas.query)
40
-
41
- query.properties.id = entity.schema.properties.id
42
-
43
- if (definition.type === 'observation') {
44
- delete query.properties.version
45
- } else {
46
- delete query.properties.projection
47
- }
48
-
49
- if (definition.type !== 'observation' || definition.scope !== 'objects') {
50
- delete query.properties.omit
51
- delete query.properties.limit
52
- } else {
53
- if (query.required === undefined) query.required = ['limit']
54
- else query.required.push('limit')
55
- }
56
-
57
- schema.properties.query = query
58
- }
59
-
60
- if (required.length > 0)
61
- schema.required = required
62
-
63
- return schema
64
- }
65
- }
66
-
67
- exports.Request = Request
@@ -1,9 +0,0 @@
1
- type: object
2
- properties:
3
- code:
4
- anyOf:
5
- - type: integer
6
- - type: string
7
- message:
8
- type: string
9
- required: [code]
@@ -1,7 +0,0 @@
1
- 'use strict'
2
-
3
- const { resolve } = require('path')
4
- const { load } = require('@toa.io/yaml')
5
-
6
- exports.query = load.sync(resolve(__dirname, './query.yaml'))
7
- exports.error = load.sync(resolve(__dirname, './error.yaml'))
@@ -1,30 +0,0 @@
1
- type: object
2
- properties:
3
- id: { }
4
- version:
5
- type: integer
6
- minimum: 0
7
- criteria:
8
- type: string
9
- omit:
10
- type: integer
11
- minimum: 0
12
- limit:
13
- type: integer
14
- minimum: 0
15
- sort:
16
- type: array
17
- uniqueItems: true
18
- minItems: 1
19
- items:
20
- type: string
21
- pattern: ^\w{1,32}(?::(?:asc|desc))?$
22
- projection:
23
- type: array
24
- uniqueItems: true
25
- minItems: 1
26
- items:
27
- type: string
28
- not:
29
- const: id
30
- additionalProperties: false
package/src/discovery.js DELETED
@@ -1,44 +0,0 @@
1
- 'use strict'
2
-
3
- const { console } = require('@toa.io/console')
4
- const { Connector } = require('./connector')
5
-
6
- class Discovery extends Connector {
7
- #lookup
8
- #lookups
9
-
10
- constructor (lookup) {
11
- super()
12
-
13
- this.#lookup = lookup
14
- }
15
-
16
- async open () {
17
- this.#lookups = {}
18
- }
19
-
20
- async lookup (locator) {
21
- const id = locator.id
22
-
23
- if (this.#lookups[id] === undefined) {
24
- this.#lookups[id] = await this.#lookup(locator)
25
- this.depends(this.#lookups[id])
26
- }
27
-
28
- console.debug(`Sending lookup request to '${id}'`)
29
-
30
- const warning = () => console.warn(`Waiting for lookup response from '${id}'...`)
31
- const timeout = setTimeout(warning, TIMEOUT)
32
-
33
- const output = await this.#lookups[id].invoke()
34
-
35
- console.debug(`Lookup response from '${id}' received`)
36
- clearTimeout(timeout)
37
-
38
- return output
39
- }
40
- }
41
-
42
- const TIMEOUT = 5000
43
-
44
- exports.Discovery = Discovery
package/src/emission.js DELETED
@@ -1,23 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('./connector')
4
-
5
- class Emission extends Connector {
6
- #events
7
-
8
- constructor (events) {
9
- super()
10
-
11
- this.#events = events
12
-
13
- this.depends(events)
14
- }
15
-
16
- async emit (event) {
17
- const emission = this.#events.map((e) => e.emit(event))
18
-
19
- await Promise.all(emission)
20
- }
21
- }
22
-
23
- exports.Emission = Emission
@@ -1,43 +0,0 @@
1
- 'use strict'
2
-
3
- const {
4
- merge,
5
- overwrite,
6
- newid
7
- } = require('@toa.io/generic')
8
- const { EntityContractException } = require('../exceptions')
9
-
10
- class Changeset {
11
- /** @type {toa.core.Query} */
12
- query
13
-
14
- #schema
15
- #state
16
-
17
- constructor (schema, query) {
18
- this.query = query
19
-
20
- this.#schema = schema
21
- this.#state = schema.system()
22
- }
23
-
24
- get () {
25
- return this.#state
26
- }
27
-
28
- set (value) {
29
- const error = this.#schema.adapt(value)
30
-
31
- if (error !== null) throw new EntityContractException(error)
32
-
33
- value._updated = Date.now()
34
-
35
- this.#state = value
36
- }
37
-
38
- export () {
39
- return this.#state
40
- }
41
- }
42
-
43
- exports.Changeset = Changeset
@@ -1,73 +0,0 @@
1
- 'use strict'
2
-
3
- const {
4
- difference,
5
- newid
6
- } = require('@toa.io/generic')
7
-
8
- const { EntityContractException } = require('../exceptions')
9
-
10
- class Entity {
11
- #schema
12
- #origin = null
13
- #state
14
-
15
- constructor (schema, argument) {
16
- this.#schema = schema
17
-
18
- if (typeof argument === 'object') {
19
- const object = structuredClone(argument)
20
- this.set(object)
21
- this.#origin = argument
22
- } else {
23
- const id = argument === undefined ? newid() : argument
24
- this.#init(id)
25
- }
26
- }
27
-
28
- get () {
29
- return this.#state
30
- }
31
-
32
- set (value) {
33
- const error = this.#schema.fit(value)
34
-
35
- if (error !== null) throw new EntityContractException(error)
36
-
37
- this.#set(value)
38
- }
39
-
40
- event () {
41
- return {
42
- origin: this.#origin,
43
- state: this.#state,
44
- changeset: this.#origin === null ? this.#state : difference(this.#origin, this.#state)
45
- }
46
- }
47
-
48
- #init (id) {
49
- const value = {
50
- ...this.#schema.defaults({ id }),
51
- _version: 0,
52
- _created: Date.now()
53
- }
54
-
55
- this.#set(value)
56
- }
57
-
58
- #set (value) {
59
- Object.defineProperty(value, 'id', {
60
- writable: false,
61
- configurable: false
62
- })
63
-
64
- if (this.#state !== undefined) {
65
- value._updated = Date.now()
66
- value._version++
67
- }
68
-
69
- this.#state = value
70
- }
71
- }
72
-
73
- exports.Entity = Entity
@@ -1,33 +0,0 @@
1
- 'use strict'
2
-
3
- const { Entity } = require('./entity')
4
- const { EntitySet } = require('./set')
5
- const { Changeset } = require('./changeset')
6
-
7
- class Factory {
8
- #schema
9
-
10
- constructor (schema) {
11
- this.#schema = schema
12
- }
13
-
14
- init (id) {
15
- return new Entity(this.#schema, id)
16
- }
17
-
18
- object (record) {
19
- return new Entity(this.#schema, record)
20
- }
21
-
22
- objects (recordset) {
23
- const set = recordset.map((record) => this.object(record))
24
-
25
- return new EntitySet(set)
26
- }
27
-
28
- changeset (query) {
29
- return new Changeset(this.#schema, query)
30
- }
31
- }
32
-
33
- exports.Factory = Factory
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- const { Factory } = require('./factory')
4
-
5
- exports.Factory = Factory
@@ -1,15 +0,0 @@
1
- 'use strict'
2
-
3
- class EntitySet {
4
- #set
5
-
6
- constructor (set) {
7
- this.#set = set
8
- }
9
-
10
- get () {
11
- return this.#set.map((entity) => entity.get())
12
- }
13
- }
14
-
15
- exports.EntitySet = EntitySet
package/src/event.js DELETED
@@ -1,40 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('./connector')
4
-
5
- /**
6
- * @implements {toa.core.Event}
7
- */
8
- class Event extends Connector {
9
- /** @type {toa.core.bindings.Emitter} */
10
- #emitter
11
- #bridge
12
- #conditioned
13
- #subjective
14
-
15
- constructor (definition, emitter, bridge = undefined) {
16
- super()
17
-
18
- this.#conditioned = definition.conditioned
19
- this.#subjective = definition.subjective
20
- this.#emitter = emitter
21
- this.#bridge = bridge
22
-
23
- this.depends(emitter)
24
-
25
- if (bridge !== undefined) this.depends(bridge)
26
- }
27
-
28
- async emit (event) {
29
- if (this.#conditioned === false || await this.#bridge.condition(event) === true) {
30
- const payload = this.#subjective ? await this.#bridge.payload(event) : event.state
31
-
32
- /** @type {toa.core.Message} */
33
- const message = { payload }
34
-
35
- await this.#emitter.emit(message)
36
- }
37
- }
38
- }
39
-
40
- exports.Event = Event
package/src/exceptions.js DELETED
@@ -1,100 +0,0 @@
1
- 'use strict'
2
-
3
- const { swap } = require('@toa.io/generic')
4
-
5
- const codes = {
6
- System: 0,
7
- NotImplemented: 10,
8
-
9
- Contract: 200,
10
- RequestSyntax: 201,
11
- RequestContract: 202,
12
- RequestConflict: 203,
13
- ResponseContract: 211,
14
- EntityContract: 212,
15
- QuerySyntax: 221,
16
-
17
- State: 300,
18
- StateNotFound: 302,
19
- StatePrecondition: 303,
20
- StateConcurrency: 304,
21
- StateInitialization: 305,
22
- Duplicate: 306,
23
-
24
- Communication: 400,
25
- Transmission: 401
26
- }
27
-
28
- /**
29
- * @implements {toa.core.Exception}
30
- */
31
- class Exception {
32
- code
33
- message
34
-
35
- constructor (code, message) {
36
- this.code = code
37
- this.message = message
38
- }
39
- }
40
-
41
- class SystemException extends Exception {
42
- stack
43
-
44
- constructor (error) {
45
- super(codes.System, error.message)
46
-
47
- if (error.stack !== undefined) this.stack = error.stack
48
- }
49
- }
50
-
51
- 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
64
- }
65
- }
66
-
67
- class RequestContractException extends ContractException {
68
- constructor (error) { super(codes.RequestContract, error) }
69
- }
70
-
71
- class ResponseContractException extends ContractException {
72
- constructor (error) { super(codes.ResponseContract, error) }
73
- }
74
-
75
- class EntityContractException extends ContractException {
76
- constructor (error) { super(codes.EntityContract, error) }
77
- }
78
-
79
- // #region exports
80
- exports.Exception = Exception
81
- exports.SystemException = SystemException
82
- exports.RequestContractException = RequestContractException
83
- exports.ResponseContractException = ResponseContractException
84
- exports.EntityContractException = EntityContractException
85
-
86
- for (const [name, code] of Object.entries(codes)) {
87
- const classname = name + 'Exception'
88
-
89
- if (exports[classname] === undefined) {
90
- exports[classname] = class extends Exception {
91
- constructor (message) {
92
- super(code, message ?? classname)
93
- }
94
- }
95
- }
96
- }
97
-
98
- exports.codes = codes
99
- exports.names = swap(codes)
100
- // #endregion
package/src/exposition.js DELETED
@@ -1,27 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('./connector')
4
-
5
- class Exposition extends Connector {
6
- locator
7
-
8
- #exposition
9
-
10
- constructor (locator, manifest) {
11
- super()
12
-
13
- this.locator = locator
14
- this.#exposition = expose(manifest)
15
- }
16
-
17
- async invoke () {
18
- return { output: this.#exposition }
19
- }
20
- }
21
-
22
- const expose = (manifest) => {
23
- const { namespace, name, operations, events, entity } = manifest
24
- return { namespace, name, operations, events, entity }
25
- }
26
-
27
- exports.Exposition = Exposition
package/src/index.js DELETED
@@ -1,47 +0,0 @@
1
- const { Assignment } = require('./assignment')
2
- const { Call } = require('./call')
3
- const { Cascade } = require('./cascade')
4
- const { Composition } = require('./composition')
5
- const { Connector } = require('./connector')
6
- const { Context } = require('./context')
7
- const { Discovery } = require('./discovery')
8
- const { Emission } = require('./emission')
9
- const { Event } = require('./event')
10
- const { Exposition } = require('./exposition')
11
- const { Locator } = require('./locator')
12
- const { Observation } = require('./observation')
13
- const { Operation } = require('./operation')
14
- const { Query } = require('./query')
15
- const { Receiver } = require('./receiver')
16
- const { Reflection } = require('./reflection')
17
- const { Remote } = require('./remote')
18
- const { Component } = require('./component')
19
- const { State } = require('./state')
20
- const { Transition } = require('./transition')
21
- const { Transmission } = require('./transmission')
22
-
23
- exports.entities = require('./entities')
24
- exports.exceptions = require('./exceptions')
25
- exports.contract = require('./contract')
26
-
27
- exports.Assignment = Assignment
28
- exports.Call = Call
29
- exports.Cascade = Cascade
30
- exports.Component = Component
31
- exports.Composition = Composition
32
- exports.Connector = Connector
33
- exports.Context = Context
34
- exports.Discovery = Discovery
35
- exports.Emission = Emission
36
- exports.Event = Event
37
- exports.Exposition = Exposition
38
- exports.Locator = Locator
39
- exports.Observation = Observation
40
- exports.Operation = Operation
41
- exports.Query = Query
42
- exports.Receiver = Receiver
43
- exports.Reflection = Reflection
44
- exports.Remote = Remote
45
- exports.State = State
46
- exports.Transition = Transition
47
- exports.Transmission = Transmission