@toa.io/core 1.0.0-alpha.32 → 1.0.0-alpha.321

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 (286) hide show
  1. package/package.json +28 -12
  2. package/transpiled/abandon.d.ts +9 -0
  3. package/transpiled/abandon.js +37 -0
  4. package/transpiled/abandon.js.map +1 -0
  5. package/transpiled/assignment.d.ts +6 -0
  6. package/transpiled/assignment.js +25 -0
  7. package/transpiled/assignment.js.map +1 -0
  8. package/transpiled/call.d.ts +10 -0
  9. package/transpiled/call.js +187 -0
  10. package/transpiled/call.js.map +1 -0
  11. package/transpiled/cascade.d.ts +7 -0
  12. package/transpiled/cascade.js +28 -0
  13. package/transpiled/cascade.js.map +1 -0
  14. package/transpiled/component.d.ts +17 -0
  15. package/transpiled/component.js +151 -0
  16. package/transpiled/component.js.map +1 -0
  17. package/transpiled/composition.d.ts +6 -0
  18. package/transpiled/composition.js +20 -0
  19. package/transpiled/composition.js.map +1 -0
  20. package/transpiled/connector.d.ts +71 -0
  21. package/transpiled/connector.js +248 -0
  22. package/transpiled/connector.js.map +1 -0
  23. package/transpiled/context.d.ts +25 -0
  24. package/transpiled/context.js +55 -0
  25. package/transpiled/context.js.map +1 -0
  26. package/transpiled/contract/component.d.ts +37 -0
  27. package/transpiled/contract/component.js +94 -0
  28. package/transpiled/contract/component.js.map +1 -0
  29. package/transpiled/contract/contract.d.ts +10 -0
  30. package/transpiled/contract/contract.js +14 -0
  31. package/transpiled/contract/contract.js.map +1 -0
  32. package/transpiled/contract/index.d.ts +4 -0
  33. package/transpiled/contract/index.js +5 -0
  34. package/transpiled/contract/index.js.map +1 -0
  35. package/transpiled/contract/reply.d.ts +7 -0
  36. package/transpiled/contract/reply.js +48 -0
  37. package/transpiled/contract/reply.js.map +1 -0
  38. package/transpiled/contract/request.d.ts +49 -0
  39. package/transpiled/contract/request.js +152 -0
  40. package/transpiled/contract/request.js.map +1 -0
  41. package/transpiled/contract/schemas.d.ts +4 -0
  42. package/transpiled/contract/schemas.js +38 -0
  43. package/transpiled/contract/schemas.js.map +1 -0
  44. package/transpiled/contract/system.d.ts +33 -0
  45. package/transpiled/contract/system.js +76 -0
  46. package/transpiled/contract/system.js.map +1 -0
  47. package/transpiled/deliveries.d.ts +22 -0
  48. package/transpiled/deliveries.js +31 -0
  49. package/transpiled/deliveries.js.map +1 -0
  50. package/transpiled/effect.d.ts +7 -0
  51. package/transpiled/effect.js +13 -0
  52. package/transpiled/effect.js.map +1 -0
  53. package/transpiled/emission.d.ts +10 -0
  54. package/transpiled/emission.js +16 -0
  55. package/transpiled/emission.js.map +1 -0
  56. package/transpiled/encoded.d.ts +16 -0
  57. package/transpiled/encoded.js +25 -0
  58. package/transpiled/encoded.js.map +1 -0
  59. package/transpiled/entities/changeset.d.ts +10 -0
  60. package/transpiled/entities/changeset.js +27 -0
  61. package/transpiled/entities/changeset.js.map +1 -0
  62. package/transpiled/entities/entity.d.ts +19 -0
  63. package/transpiled/entities/entity.js +137 -0
  64. package/transpiled/entities/entity.js.map +1 -0
  65. package/transpiled/entities/factory.d.ts +27 -0
  66. package/transpiled/entities/factory.js +38 -0
  67. package/transpiled/entities/factory.js.map +1 -0
  68. package/transpiled/entities/index.d.ts +5 -0
  69. package/transpiled/entities/index.js +6 -0
  70. package/transpiled/entities/index.js.map +1 -0
  71. package/transpiled/entities/newid.d.ts +11 -0
  72. package/transpiled/entities/newid.js +41 -0
  73. package/transpiled/entities/newid.js.map +1 -0
  74. package/transpiled/entities/set.d.ts +10 -0
  75. package/transpiled/entities/set.js +19 -0
  76. package/transpiled/entities/set.js.map +1 -0
  77. package/transpiled/event.d.ts +20 -0
  78. package/transpiled/event.js +65 -0
  79. package/transpiled/event.js.map +1 -0
  80. package/transpiled/exceptions.d.ts +110 -0
  81. package/transpiled/exceptions.js +215 -0
  82. package/transpiled/exceptions.js.map +1 -0
  83. package/transpiled/gate.d.ts +40 -0
  84. package/transpiled/gate.js +84 -0
  85. package/transpiled/gate.js.map +1 -0
  86. package/transpiled/guard.d.ts +7 -0
  87. package/transpiled/guard.js +12 -0
  88. package/transpiled/guard.js.map +1 -0
  89. package/transpiled/halting.d.ts +16 -0
  90. package/transpiled/halting.js +25 -0
  91. package/transpiled/halting.js.map +1 -0
  92. package/transpiled/index.d.ts +45 -0
  93. package/transpiled/index.js +37 -0
  94. package/transpiled/index.js.map +1 -0
  95. package/transpiled/instance.d.ts +10 -0
  96. package/transpiled/instance.js +30 -0
  97. package/transpiled/instance.js.map +1 -0
  98. package/transpiled/locator.d.ts +12 -0
  99. package/transpiled/locator.js +32 -0
  100. package/transpiled/locator.js.map +1 -0
  101. package/transpiled/measurements.d.ts +20 -0
  102. package/transpiled/measurements.js +40 -0
  103. package/transpiled/measurements.js.map +1 -0
  104. package/transpiled/observation.d.ts +7 -0
  105. package/transpiled/observation.js +14 -0
  106. package/transpiled/observation.js.map +1 -0
  107. package/transpiled/operation.d.ts +77 -0
  108. package/transpiled/operation.js +216 -0
  109. package/transpiled/operation.js.map +1 -0
  110. package/transpiled/outbox.d.ts +45 -0
  111. package/transpiled/outbox.js +448 -0
  112. package/transpiled/outbox.js.map +1 -0
  113. package/transpiled/outbox.measurements.d.ts +4 -0
  114. package/transpiled/outbox.measurements.js +38 -0
  115. package/transpiled/outbox.measurements.js.map +1 -0
  116. package/transpiled/parts.d.ts +20 -0
  117. package/transpiled/parts.js +23 -0
  118. package/transpiled/parts.js.map +1 -0
  119. package/transpiled/query/criteria.d.ts +6 -0
  120. package/transpiled/query/criteria.js +177 -0
  121. package/transpiled/query/criteria.js.map +1 -0
  122. package/transpiled/query/options.d.ts +3 -0
  123. package/transpiled/query/options.js +34 -0
  124. package/transpiled/query/options.js.map +1 -0
  125. package/transpiled/query.d.ts +9 -0
  126. package/transpiled/query.js +57 -0
  127. package/transpiled/query.js.map +1 -0
  128. package/transpiled/receiver.d.ts +20 -0
  129. package/transpiled/receiver.js +113 -0
  130. package/transpiled/receiver.js.map +1 -0
  131. package/transpiled/reflection.d.ts +9 -0
  132. package/transpiled/reflection.js +13 -0
  133. package/transpiled/reflection.js.map +1 -0
  134. package/transpiled/regional.d.ts +22 -0
  135. package/transpiled/regional.js +55 -0
  136. package/transpiled/regional.js.map +1 -0
  137. package/transpiled/remote.d.ts +10 -0
  138. package/transpiled/remote.js +11 -0
  139. package/transpiled/remote.js.map +1 -0
  140. package/transpiled/rendering.d.ts +13 -0
  141. package/transpiled/rendering.js +21 -0
  142. package/transpiled/rendering.js.map +1 -0
  143. package/transpiled/safety.d.ts +5 -0
  144. package/transpiled/safety.js +26 -0
  145. package/transpiled/safety.js.map +1 -0
  146. package/transpiled/state.d.ts +36 -0
  147. package/transpiled/state.js +131 -0
  148. package/transpiled/state.js.map +1 -0
  149. package/transpiled/trail.d.ts +78 -0
  150. package/transpiled/trail.js +112 -0
  151. package/transpiled/trail.js.map +1 -0
  152. package/transpiled/transition.d.ts +14 -0
  153. package/transpiled/transition.js +51 -0
  154. package/transpiled/transition.js.map +1 -0
  155. package/transpiled/transmission.d.ts +8 -0
  156. package/transpiled/transmission.js +30 -0
  157. package/transpiled/transmission.js.map +1 -0
  158. package/transpiled/types/atomicity.d.ts +58 -0
  159. package/transpiled/types/atomicity.js +2 -0
  160. package/transpiled/types/atomicity.js.map +1 -0
  161. package/transpiled/types/bindings.d.ts +82 -0
  162. package/transpiled/types/bindings.js +2 -0
  163. package/transpiled/types/bindings.js.map +1 -0
  164. package/transpiled/types/bridges.d.ts +41 -0
  165. package/transpiled/types/bridges.js +2 -0
  166. package/transpiled/types/bridges.js.map +1 -0
  167. package/transpiled/types/extensions.d.ts +121 -0
  168. package/transpiled/types/extensions.js +2 -0
  169. package/transpiled/types/extensions.js.map +1 -0
  170. package/transpiled/types/inbox.d.ts +27 -0
  171. package/transpiled/types/inbox.js +2 -0
  172. package/transpiled/types/inbox.js.map +1 -0
  173. package/transpiled/types/index.d.ts +14 -0
  174. package/transpiled/types/index.js +9 -0
  175. package/transpiled/types/index.js.map +1 -0
  176. package/transpiled/types/message.d.ts +16 -0
  177. package/transpiled/types/message.js +2 -0
  178. package/transpiled/types/message.js.map +1 -0
  179. package/transpiled/types/operations.d.ts +15 -0
  180. package/transpiled/types/operations.js +2 -0
  181. package/transpiled/types/operations.js.map +1 -0
  182. package/transpiled/types/outbox.d.ts +93 -0
  183. package/transpiled/types/outbox.js +2 -0
  184. package/transpiled/types/outbox.js.map +1 -0
  185. package/transpiled/types/receiver.d.ts +9 -0
  186. package/transpiled/types/receiver.js +2 -0
  187. package/transpiled/types/receiver.js.map +1 -0
  188. package/transpiled/types/request.d.ts +119 -0
  189. package/transpiled/types/request.js +2 -0
  190. package/transpiled/types/request.js.map +1 -0
  191. package/transpiled/types/state.d.ts +9 -0
  192. package/transpiled/types/state.js +2 -0
  193. package/transpiled/types/state.js.map +1 -0
  194. package/transpiled/types/storages.d.ts +158 -0
  195. package/transpiled/types/storages.js +2 -0
  196. package/transpiled/types/storages.js.map +1 -0
  197. package/transpiled/unmanaged.d.ts +5 -0
  198. package/transpiled/unmanaged.js +7 -0
  199. package/transpiled/unmanaged.js.map +1 -0
  200. package/src/assignment.js +0 -31
  201. package/src/call.js +0 -42
  202. package/src/cascade.js +0 -35
  203. package/src/component.js +0 -41
  204. package/src/composition.js +0 -25
  205. package/src/connector.js +0 -183
  206. package/src/context.js +0 -55
  207. package/src/contract/conditions.js +0 -21
  208. package/src/contract/index.js +0 -7
  209. package/src/contract/reply.js +0 -25
  210. package/src/contract/request.js +0 -67
  211. package/src/contract/schemas/error.yaml +0 -9
  212. package/src/contract/schemas/index.js +0 -7
  213. package/src/contract/schemas/query.yaml +0 -30
  214. package/src/discovery.js +0 -44
  215. package/src/emission.js +0 -23
  216. package/src/entities/changeset.js +0 -38
  217. package/src/entities/entity.js +0 -74
  218. package/src/entities/factory.js +0 -33
  219. package/src/entities/index.js +0 -5
  220. package/src/entities/set.js +0 -15
  221. package/src/event.js +0 -40
  222. package/src/exceptions.js +0 -100
  223. package/src/exposition.js +0 -27
  224. package/src/index.js +0 -47
  225. package/src/locator.js +0 -54
  226. package/src/observation.js +0 -20
  227. package/src/operation.js +0 -69
  228. package/src/query/criteria.js +0 -41
  229. package/src/query/options.js +0 -40
  230. package/src/query.js +0 -41
  231. package/src/receiver.js +0 -61
  232. package/src/reflection.js +0 -28
  233. package/src/remote.js +0 -17
  234. package/src/state.js +0 -99
  235. package/src/transition.js +0 -67
  236. package/src/transmission.js +0 -33
  237. package/test/call.fixtures.js +0 -27
  238. package/test/call.test.js +0 -46
  239. package/test/component.fixtures.js +0 -21
  240. package/test/component.test.js +0 -40
  241. package/test/connector.fixtures.js +0 -40
  242. package/test/connector.test.js +0 -176
  243. package/test/context.fixtures.js +0 -18
  244. package/test/context.test.js +0 -29
  245. package/test/contract/conditions.test.js +0 -26
  246. package/test/contract/contract.fixtures.js +0 -33
  247. package/test/contract/request.test.js +0 -112
  248. package/test/emission.fixtures.js +0 -17
  249. package/test/emission.test.js +0 -35
  250. package/test/entities/entity.fixtures.js +0 -29
  251. package/test/entities/entity.test.js +0 -65
  252. package/test/entities/factory.fixtures.js +0 -18
  253. package/test/entities/factory.test.js +0 -48
  254. package/test/entities/set.fixtures.js +0 -11
  255. package/test/entities/set.test.js +0 -12
  256. package/test/event.fixtures.js +0 -29
  257. package/test/event.test.js +0 -118
  258. package/test/locator.test.js +0 -113
  259. package/test/query.fixtures.js +0 -100
  260. package/test/query.test.js +0 -86
  261. package/test/receiver.fixtures.js +0 -23
  262. package/test/receiver.test.js +0 -95
  263. package/test/reflection.test.js +0 -30
  264. package/test/state.fixtures.js +0 -47
  265. package/test/state.test.js +0 -46
  266. package/test/transmission.fixtures.js +0 -15
  267. package/test/transmission.test.js +0 -46
  268. package/types/bindings.d.ts +0 -40
  269. package/types/bridges.ts +0 -30
  270. package/types/component.d.ts +0 -9
  271. package/types/connector.d.ts +0 -18
  272. package/types/context.d.ts +0 -21
  273. package/types/entity.d.ts +0 -46
  274. package/types/event.d.ts +0 -11
  275. package/types/exception.d.ts +0 -4
  276. package/types/extensions.d.ts +0 -29
  277. package/types/index.ts +0 -14
  278. package/types/locator.d.ts +0 -13
  279. package/types/message.d.ts +0 -3
  280. package/types/operations.d.ts +0 -2
  281. package/types/query.d.ts +0 -15
  282. package/types/receiver.d.ts +0 -12
  283. package/types/reflection.d.ts +0 -14
  284. package/types/request.d.ts +0 -23
  285. package/types/state.d.ts +0 -35
  286. package/types/storages.d.ts +0 -71
@@ -0,0 +1,158 @@
1
+ import type { Readable } from 'node:stream';
2
+ import type { Connector } from '../connector.ts';
3
+ import type { Locator } from '../locator.ts';
4
+ import type { Call, Inbox } from './inbox.ts';
5
+ import type { Row, Storage as Outbox } from './outbox.ts';
6
+ /** the RSQL tree `@rsql/parser` produces; a storage translates it into its own dialect */
7
+ export interface Node {
8
+ type: 'LOGIC' | 'COMPARISON' | 'SELECTOR' | 'VALUE';
9
+ left?: Node;
10
+ right?: Node;
11
+ operator?: string;
12
+ selector?: string;
13
+ value?: unknown;
14
+ }
15
+ /** What a storage stores. `id` is the key; the upper-cased names are core's. */
16
+ export interface Record {
17
+ id: string;
18
+ VERSION: number;
19
+ CREATED?: number;
20
+ UPDATED?: number;
21
+ /** a tombstone's timestamp; `null` on a live record */
22
+ DELETED?: number | null;
23
+ /** the rank of the region that last wrote it; `0` where there is one region */
24
+ REGION?: number;
25
+ [key: string]: any;
26
+ }
27
+ /** Everything a request query carried that was not a selector. */
28
+ export interface Options {
29
+ omit?: number;
30
+ limit?: number;
31
+ /** normalised by `query/options.ts` from `'name:asc'` into pairs */
32
+ sort?: Array<[property: string, direction: string]>;
33
+ /** always includes `VERSION`, `CREATED`, `UPDATED`, `DELETED` */
34
+ projection?: string[];
35
+ sample?: number;
36
+ /** include tombstones; without it an observation answers `null` over one */
37
+ deleted?: boolean;
38
+ /** where a stream continues from: the token the one before it ended with */
39
+ token?: string;
40
+ /** a stream read in windows ends its first window with where changes continue from, not deeper */
41
+ stop?: boolean;
42
+ }
43
+ /** What core hands a storage, written by `Query.parse`. */
44
+ export interface Query {
45
+ id?: string;
46
+ /** many identities at once; `State.entries` inits whichever of them are missing */
47
+ ids?: string[];
48
+ version?: number;
49
+ criteria?: Node;
50
+ search?: string;
51
+ options?: Options;
52
+ }
53
+ /**
54
+ * What core requires of a storage. Every member but `outbox` is called unguarded, so a
55
+ * storage that leaves one out throws where an operation needs it — a storage may serve
56
+ * only some scopes, and says so by failing rather than by declaring less.
57
+ */
58
+ export interface Storage extends Connector {
59
+ /** scope `entry` */
60
+ get(query: Query): Promise<Record | null>;
61
+ /** scope `entries` */
62
+ find(query: Query): Promise<Record[]>;
63
+ /**
64
+ * scope `stream`: the parts of the collection the query selects, or of what changed in it since
65
+ * `options.token` — see `parts.ts`. The last part is the token; a stream that ends without it
66
+ * was cut.
67
+ */
68
+ stream(query?: Query): Promise<Readable>;
69
+ /**
70
+ * A transition's commit. `false` is a lost compare-and-swap, not an error. Where `row` is
71
+ * given, it is committed in the same transaction as the record or not at all, and where
72
+ * `call` is, so is that — and an identity already recorded raises `DuplicateCall` instead,
73
+ * having written nothing.
74
+ */
75
+ store(record: Record, row?: Row, call?: Call): Promise<boolean>;
76
+ /**
77
+ * A transition's commit over `entries`, one compare-and-swap for the whole set: every record is
78
+ * written, with every row, or none is. `false` is the swap lost — a record changed since it was
79
+ * read, or one being created that exists already — and not an error.
80
+ */
81
+ massStore(records: Record[], rows?: Row[]): Promise<boolean>;
82
+ /** an assignment; `null` where the query matched nothing */
83
+ upsert(query: Query, changeset: object, row?: Row, call?: Call): Promise<Record | null>;
84
+ /** get-or-create, in one indivisible step */
85
+ ensure(query: Query | undefined, properties: object, record: Record, row?: Row, call?: Call): Promise<Record>;
86
+ /** the driver's own handle, which an unmanaged operation is given as its state */
87
+ readonly raw: unknown;
88
+ /**
89
+ * Present only where a row can be committed atomically with the entity. Its absence is
90
+ * what makes the runtime publish inline, so a storage that cannot do this must not offer
91
+ * it: a row written outside the transaction would be a second write with a crash window
92
+ * in front of it, which is the defect the outbox exists to close.
93
+ */
94
+ readonly outbox?: Outbox;
95
+ /**
96
+ * Present only where a call can be recorded atomically with the entity, and only where a
97
+ * component declares `once`. Unlike the outbox there is no weaker thing to fall back to —
98
+ * a record written outside the transaction guarantees nothing — so a storage that cannot
99
+ * says so through `claims`, and a component that asked for it does not boot.
100
+ */
101
+ readonly inbox?: Inbox;
102
+ /**
103
+ * Whether this storage can record a call at all, which is a property of the storage rather
104
+ * than of the deployment it is pointed at, and so is answerable before it is connected.
105
+ */
106
+ readonly claims?: boolean;
107
+ /**
108
+ * Whether this storage applies `entity.migrations`. Absent is what a storage that does not
109
+ * says, and a component declaring migrations against one is refused at boot rather than
110
+ * starting with a structure nothing has made.
111
+ */
112
+ readonly migrates?: boolean;
113
+ /**
114
+ * Writes `record` as it stands — its `VERSION`, its timestamps, its `REGION` and whatever
115
+ * else it carries — where what it would replace precedes it: a lower `VERSION`, or the same
116
+ * `VERSION` written by a region this one outranks. `false` where it does not: nothing is
117
+ * written, and that is not an error.
118
+ *
119
+ * Both sides of the comparison are on the two records, so this takes nothing else. What it
120
+ * is for is a record that was written somewhere else and has to land here as it was, which
121
+ * is neither a transition nor an assignment: it is not the writer's version to increment,
122
+ * nor its timestamps to set.
123
+ */
124
+ converge?(record: Record): Promise<boolean>;
125
+ /**
126
+ * Whether this storage converges. Absent is what a storage that does not says, and a
127
+ * component of a context that converges stands down rather than running where it would
128
+ * never take a record from another region.
129
+ */
130
+ readonly converges?: boolean;
131
+ }
132
+ /**
133
+ * The subset of a component's entity declaration a storage reads. Structural, so that core
134
+ * names no type of `@toa.io/norm`, which depends on core.
135
+ */
136
+ export interface Entity {
137
+ associated?: boolean;
138
+ custom?: boolean;
139
+ /** in the order they are applied; what a step is belongs to the storage that reads it */
140
+ migrations?: Migration[];
141
+ }
142
+ /**
143
+ * One file of a `migrations` directory, named after it — the component's own, or a prototype's,
144
+ * which every descendant applies to its collection ahead of its own.
145
+ */
146
+ export interface Migration {
147
+ id: string;
148
+ steps: unknown[];
149
+ /** the prototype it is inherited from; absent on the component's own */
150
+ prototype?: string;
151
+ }
152
+ export interface StorageOptions {
153
+ /** whether the component publishes anything, and so needs a place to commit a row */
154
+ outbox?: boolean;
155
+ }
156
+ export interface Factory {
157
+ storage(locator: Locator, entity: Entity, options?: StorageOptions): Storage;
158
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=storages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storages.js","sourceRoot":"","sources":["../../source/types/storages.ts"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ import { Operation } from './operation.ts';
2
+ import type { Store } from './operation.ts';
3
+ export declare class Unmanaged extends Operation {
4
+ protected acquire(store: Store): Promise<void>;
5
+ }
@@ -0,0 +1,7 @@
1
+ import { Operation } from './operation.js';
2
+ export class Unmanaged extends Operation {
3
+ async acquire(store) {
4
+ store.state = this.scope.storage.raw;
5
+ }
6
+ }
7
+ //# sourceMappingURL=unmanaged.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unmanaged.js","sourceRoot":"","sources":["../source/unmanaged.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG1C,MAAM,OAAO,SAAU,SAAQ,SAAS;IACnB,KAAK,CAAC,OAAO,CAAC,KAAY;QAC3C,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAA;IACtC,CAAC;CACF"}
package/src/assignment.js DELETED
@@ -1,31 +0,0 @@
1
- 'use strict'
2
-
3
- const { Operation } = require('./operation')
4
-
5
- class Assignment extends Operation {
6
- async acquire (store) {
7
- store.scope = this.scope.changeset(store.request.query)
8
- store.state = store.scope.get()
9
- }
10
-
11
- async commit (store) {
12
- const {
13
- scope,
14
- state,
15
- reply
16
- } = store
17
-
18
- if (reply.error !== undefined) return
19
-
20
- scope.set(state)
21
-
22
- const output = await this.scope.apply(scope)
23
-
24
- // assignment returns new state by default
25
- if (store.reply.output === undefined) {
26
- store.reply.output = output
27
- }
28
- }
29
- }
30
-
31
- exports.Assignment = Assignment
package/src/call.js DELETED
@@ -1,42 +0,0 @@
1
- 'use strict'
2
-
3
- const { Readable } = require('node:stream')
4
- const { Connector } = require('./connector')
5
- const { Err } = require('error-value')
6
-
7
- class Call extends Connector {
8
- #transmitter
9
- #contract
10
-
11
- constructor (transmitter, contract) {
12
- super()
13
-
14
- this.#transmitter = transmitter
15
- this.#contract = contract
16
-
17
- this.depends(transmitter)
18
- }
19
-
20
- async invoke (request = {}) {
21
- this.#contract.fit(request)
22
-
23
- // avoid validation on the recipient's side
24
- request.authentic = true
25
-
26
- const reply = await this.#transmitter.request(request)
27
-
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
33
-
34
- if (reply.error !== undefined)
35
- return Err(reply.error.code, reply.error)
36
- else
37
- return reply.output
38
- }
39
- }
40
- }
41
-
42
- exports.Call = Call
package/src/cascade.js DELETED
@@ -1,35 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('./connector')
4
-
5
- class Cascade extends Connector {
6
- // #bridges
7
- #last
8
-
9
- constructor (bridges) {
10
- super()
11
-
12
- // this.#bridges = bridges
13
- this.#last = bridges[bridges.length - 1]
14
-
15
- this.depends(bridges)
16
- }
17
-
18
- async run (...args) {
19
- // const reply = {}
20
- //
21
- // for (const bridge of this.#bridges) {
22
- // const partial = await bridge.execute(...args)
23
- //
24
- // if (partial.error) return { error: partial.error }
25
- //
26
- // merge(reply, partial)
27
- // }
28
- //
29
- // return reply
30
-
31
- return this.#last.execute(...args)
32
- }
33
- }
34
-
35
- exports.Cascade = Cascade
package/src/component.js DELETED
@@ -1,41 +0,0 @@
1
- 'use strict'
2
-
3
- const { console } = require('@toa.io/console')
4
- const { Connector } = require('./connector')
5
- const { NotImplementedException } = require('./exceptions')
6
-
7
- /**
8
- * @implements {toa.core.Component}
9
- */
10
- class Component extends Connector {
11
- locator
12
-
13
- #operations
14
-
15
- constructor (locator, operations) {
16
- super()
17
-
18
- this.locator = locator
19
- this.#operations = operations
20
-
21
- Object.values(operations).forEach((operation) => this.depends(operation))
22
- }
23
-
24
- async open () {
25
- console.info(`Runtime '${this.locator.id}' connected`)
26
- }
27
-
28
- async dispose () {
29
- console.info(`Runtime '${this.locator.id}' disconnected`)
30
- }
31
-
32
- async invoke (endpoint, request) {
33
- if (!(endpoint in this.#operations)) {
34
- throw new NotImplementedException(`Endpoint '${endpoint}' not found in '${this.locator.id}'`)
35
- }
36
-
37
- return this.#operations[endpoint].invoke(request)
38
- }
39
- }
40
-
41
- exports.Component = Component
@@ -1,25 +0,0 @@
1
- 'use strict'
2
-
3
- const { console } = require('@toa.io/console')
4
- const { Connector } = require('./connector')
5
-
6
- class Composition extends Connector {
7
- constructor (expositions, producers, receivers, tenants) {
8
- super()
9
-
10
- if (expositions.length > 0) this.depends(expositions)
11
- if (producers.length > 0) this.depends(producers)
12
- if (receivers.length > 0) this.depends(receivers)
13
- if (tenants.length > 0) this.depends(tenants)
14
- }
15
-
16
- async open () {
17
- console.info('Composition complete')
18
- }
19
-
20
- async dispose () {
21
- console.info('Composition shutdown complete')
22
- }
23
- }
24
-
25
- exports.Composition = Composition
package/src/connector.js DELETED
@@ -1,183 +0,0 @@
1
- 'use strict'
2
-
3
- const { console } = require('@toa.io/console')
4
- const { newid } = require('@toa.io/generic')
5
-
6
- /**
7
- * Abstract connections hierarchy
8
- * @implements {toa.core.Connector}
9
- */
10
- class Connector {
11
- /** @type {Array<Connector>} */
12
- #dependencies = []
13
-
14
- /** @type {Array<Connector>} */
15
- #links = []
16
-
17
- /** @type {Promise} */
18
- #connecting
19
-
20
- /** @type {Promise} */
21
- #disconnecting
22
-
23
- /** @type {string} */
24
- id
25
- /** @type {boolean} */
26
- connected = false
27
-
28
- constructor () {
29
- this.id = this.constructor.name + '#' + newid().substring(0, 8)
30
- }
31
-
32
- /**
33
- * Creates a dependency and backlink with another Connector or a set of Connectors
34
- *
35
- * See .connect() and .disconnect()
36
- *
37
- * @param connector {Connector | any | Array<Connector | any>}
38
- * @returns {Connector}
39
- */
40
- depends (connector) {
41
- /** @type {Connector} */
42
- let next
43
-
44
- if (connector instanceof Array) {
45
- if (connector.length === 0) throw new Error('Connectors array must not be empty')
46
-
47
- if (connector.length > 1) {
48
- next = new Connector()
49
-
50
- for (const item of connector) {
51
- this.#dependencies.push(item)
52
- item.depends(next)
53
- }
54
- } else next = connector[0]
55
- } else next = connector
56
-
57
- this.#dependencies.push(next)
58
- next.link(this)
59
-
60
- return next
61
- }
62
-
63
- /**
64
- * Creates a backlink to another Connector
65
- *
66
- * See .connect() and .disconnect()
67
- *
68
- * @param connector {Connector}
69
- * @returns {void}
70
- */
71
- link (connector) {
72
- this.#links.push(connector)
73
- }
74
-
75
- /**
76
- * Connects dependants then self
77
- *
78
- * In case of exception disconnects with current connection interruption
79
- *
80
- * Method is idempotent
81
- *
82
- * @returns {Promise<void>}
83
- */
84
- async connect () {
85
- if (this.#connecting) return this.#connecting
86
-
87
- this.#disconnecting = undefined
88
-
89
- console.debug(`Connecting '${this.id}' with ${this.#dependencies.length} dependencies...`)
90
-
91
- this.#connecting = (async () => {
92
- await Promise.all(this.#dependencies.map((connector) => connector.connect()))
93
- await this.open()
94
- })()
95
-
96
- try {
97
- await this.#connecting
98
- this.connected = true
99
- } catch (e) {
100
- await this.disconnect(true)
101
- throw e
102
- }
103
-
104
- console.debug(`Connector '${this.id}' connected`)
105
- }
106
-
107
- /**
108
- * Disconnects self then dependants
109
- *
110
- * Does nothing if there are connected linked Connectors
111
- *
112
- * Method is idempotent
113
- *
114
- * @param [interrupt] {boolean}
115
- * @returns {Promise<void>}
116
- */
117
- async disconnect (interrupt) {
118
- if (interrupt !== true) await this.#connecting
119
-
120
- if (this.#disconnecting) return this.#disconnecting
121
-
122
- const linked = this.#links.reduce((acc, parent) => acc || parent.connected, false)
123
-
124
- if (linked && interrupt !== true) return
125
-
126
- this.connected = false
127
- this.#connecting = undefined
128
-
129
- this.#disconnecting = (async () => {
130
- const start = +new Date()
131
-
132
- const interval = setInterval(() => {
133
- const delay = +new Date() - start
134
-
135
- if (delay > DELAY) console.warn(`Connector ${this.id} still disconnecting (${delay})`)
136
- }, DELAY)
137
-
138
- if (interrupt !== true) await this.close()
139
-
140
- clearInterval(interval)
141
-
142
- await Promise.all(this.#dependencies.map(connector => connector.disconnect()))
143
-
144
- await this.dispose()
145
- })()
146
-
147
- await this.#disconnecting
148
-
149
- console.debug(`Connector '${this.id}' disconnected`)
150
- }
151
-
152
- async reconnect () {
153
- await this.disconnect()
154
- await this.connect()
155
- }
156
-
157
- debug (node = {}) {
158
- node[this.id] = { connected: this.connected }
159
-
160
- if (this.#dependencies.length > 0) for (const connector of this.#dependencies) connector.debug?.(node[this.id])
161
-
162
- return node
163
- }
164
-
165
- /**
166
- * Called on connection
167
- */
168
- async open () {}
169
-
170
- /**
171
- * Called on disconnection
172
- */
173
- async close () {}
174
-
175
- /**
176
- * Called after self and dependants disconnection is complete
177
- */
178
- async dispose () {}
179
- }
180
-
181
- const DELAY = 5000
182
-
183
- exports.Connector = Connector
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