@rootintf/protocol-2phw 2.3.7-api.2 → 3.1.2-api.4
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/README.md +427 -1
- package/dist/twoPhW.cjs.js +379 -1218
- package/dist/twoPhW.d.ts +83 -49
- package/dist/twoPhW.es.mjs +365 -1210
- package/package.json +27 -19
- package/src/twoPhW.ts +865 -833
- package/dist/ajv-gen-validate.d.cts +0 -1
- package/dist/validate-esm.d.mts +0 -48
- package/schema/schema_2PhW.json +0 -61
- package/schema/schema_MsgOptions.json +0 -12
- package/schema/schema_Options_delay.json +0 -17
- package/src/ajv-gen-validate.cjs +0 -40
- package/src/validate-esm.mjs +0 -1
package/README.md
CHANGED
|
@@ -1 +1,427 @@
|
|
|
1
|
-
# npm-protocol-2PhW
|
|
1
|
+
# npm-protocol-2PhW (`@rootintf/protocol-2phw`)
|
|
2
|
+
|
|
3
|
+
The **two-phase warp** protocol (2PhW) for IPSME participants: a sender asks a receiver to take a
|
|
4
|
+
user over (`Warp` → `AckWarp`), then commits (`Engage` → `AckEngage`).
|
|
5
|
+
|
|
6
|
+
2PhW is more than an interface: it has **order and time windows**. So it comes in two parts, and a
|
|
7
|
+
participant uses both:
|
|
8
|
+
|
|
9
|
+
| part | what it is | where it comes from |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| the **interface** | the five messages on the wire: validators, constructors, `VERSION`, types | **generated** in your project by `iface-gen` from `schema_TwoPhW.json` |
|
|
12
|
+
| the **protocol** | the state machine: which step is legal now, inside which window, and the expiry timers | **this package** |
|
|
13
|
+
|
|
14
|
+
This package has **no interface and no schema of its own**. You hand it yours, once:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import * as iface_TwoPhW from '../generated/iface-TwoPhW'; // generated in your project
|
|
18
|
+
import { protocol_TwoPhW } from '@rootintf/protocol-2phw';
|
|
19
|
+
|
|
20
|
+
const { TwoPhW, JSON_MsgWarp, JSON_EffEngage, JSON_AckWarp, JSON_AckEngage } = protocol_TwoPhW(iface_TwoPhW);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
So the version you validate is the version the package builds its messages with; the two cannot
|
|
24
|
+
disagree.
|
|
25
|
+
|
|
26
|
+
The generated responder in your project is where the two meet: its dispatch validates a message with
|
|
27
|
+
the generated interface, then hands it to this package's typed handler.
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
ME -> bridge -> Responder_TwoPhW.handler_json_{msg,eff,hold,ack} generated: validate as a type
|
|
31
|
+
-> Responder_TwoPhW.handler_json_<message> you: one forwarding line
|
|
32
|
+
-> twoPhW.handler_json_<message> this package: legal? in its window? log it
|
|
33
|
+
-> your callback you: what the step means for the app
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## The exchange
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
sender receiver
|
|
42
|
+
| -- JSON_MsgWarp --------------------> | phase 1: "take this user to this hyperport"
|
|
43
|
+
| <------------------ JSON_HoldWarp --- | any number of times: "working on it, wait this long"
|
|
44
|
+
| <------------------- JSON_AckWarp --- | ok:true, or ok:false (ends the exchange)
|
|
45
|
+
| -- JSON_EffEngage ------------------> | phase 2: "commit"
|
|
46
|
+
| <----------------- JSON_AckEngage --- | concludes the exchange
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
| message | base | body key | `_cause` |
|
|
50
|
+
|---|---|---|---|
|
|
51
|
+
| `JSON_MsgWarp` | `JSON_Msg` | `warp: { id, user, hyperport, ttl_msec, instantaneous? }` | — |
|
|
52
|
+
| `JSON_AckWarp` | `JSON_Ack` | `ack: { ok }` | the `JSON_MsgWarp` |
|
|
53
|
+
| `JSON_HoldWarp` | `JSON_Hold` | `hold: { ttl_msec, stage?, percent?, eta_msec? }` | the `JSON_MsgWarp` |
|
|
54
|
+
| `JSON_EffEngage` | `JSON_Eff` | `engage: { ok }` | the `JSON_MsgWarp` (not its ack) |
|
|
55
|
+
| `JSON_AckEngage` | `JSON_Ack` | `ack: { ok }` | the `JSON_EffEngage` |
|
|
56
|
+
|
|
57
|
+
Every message carries the marker `"2PhW": "3"`. `warp.id` names the whole exchange: every later
|
|
58
|
+
message reaches it through its `_cause` chain.
|
|
59
|
+
|
|
60
|
+
**Timing.** Every step is timed from the message it answers, by that message's own `dt`; no step
|
|
61
|
+
looks further back than the message before it. There are two durations, each stating one cause of
|
|
62
|
+
lateness and set by the side that knows it:
|
|
63
|
+
|
|
64
|
+
- `TTL` = `warp.ttl_msec`, set by the sender: the longest a message of the exchange may take to
|
|
65
|
+
travel **one way**. Network delay and its variability, nothing else.
|
|
66
|
+
- **the lease** = `hold.ttl_msec` of a `JSON_HoldWarp`, set by the receiver: "my next word comes
|
|
67
|
+
within this long". Work, nothing else.
|
|
68
|
+
|
|
69
|
+
A message that hands the turn over is answered within its `dt` + `2·TTL`, there and back. After a
|
|
70
|
+
hold the next word is due within the hold's `dt` + its lease + `TTL`:
|
|
71
|
+
|
|
72
|
+
| step | accepted until |
|
|
73
|
+
|---|---|
|
|
74
|
+
| `JSON_MsgWarp` | the warp's `dt` + `TTL` (one way) |
|
|
75
|
+
| `JSON_HoldWarp`, `JSON_AckWarp` | the warp's `dt` + `2·TTL`, or where a hold moved that: the hold's `dt` + lease + `TTL` |
|
|
76
|
+
| `JSON_EffEngage` | the ack's `dt` + `2·TTL` |
|
|
77
|
+
| `JSON_AckEngage` | the engage's `dt` + `2·TTL` |
|
|
78
|
+
|
|
79
|
+
Of several holds the one that ends latest counts; nothing is added up, so a hold delivered twice
|
|
80
|
+
changes nothing. A hold whose lease ends inside the window in force is logged and moves nothing. A
|
|
81
|
+
window that has closed is not reopened. A hold is not a step: the exchange stays where it is, waiting
|
|
82
|
+
for the `JSON_AckWarp`, and holds stop with it.
|
|
83
|
+
|
|
84
|
+
So silence is noticed within `2·TTL`, or within a lease + `TTL` while work is under way, however
|
|
85
|
+
long the work is. The exchange as a whole has no limit: it stays open for as long as leases are
|
|
86
|
+
renewed, and the package replays the event log back to the warp's own `dt`. A receiver that parks a
|
|
87
|
+
warp (a user is asked, a world is installed) registers and publishes its first hold at once and renews
|
|
88
|
+
well inside its lease; work then never has to fit inside `ttl_msec`.
|
|
89
|
+
|
|
90
|
+
`ttl_msec` and a lease must be above 0 (the schemas refuse anything else).
|
|
91
|
+
|
|
92
|
+
A step that arrives out of order, twice, late, or after a nack is refused: its handler returns
|
|
93
|
+
`false` and nothing is logged. So is a step whose `dt` does not read as a time: every window is
|
|
94
|
+
measured from a `dt`, and the schema only asks it to be a string.
|
|
95
|
+
|
|
96
|
+
**Single phase.** A warp with `instantaneous: true` has no phase 2: its `JSON_AckWarp` concludes the
|
|
97
|
+
exchange, and an engage for it is refused (received, or registered). The receiver acts on the warp
|
|
98
|
+
when it acknowledges it; the sender is done when the ack arrives.
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Procedure
|
|
103
|
+
|
|
104
|
+
### 1. Install
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
npm i @rootintf/protocol-2phw @rootintf/json-msg @rootintf/eventlog
|
|
108
|
+
npm i -D @rootintf/iface-gen
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### 2. Add the schema to your `ifacegenconfig.json`
|
|
112
|
+
|
|
113
|
+
Register `TwoPhW` once, pinned by hash, and list it in the target of the participant that warps:
|
|
114
|
+
|
|
115
|
+
```jsonc
|
|
116
|
+
{
|
|
117
|
+
"schemas": {
|
|
118
|
+
"TwoPhW": { "src": "~/protocols.git/schemas.readonly/schema_TwoPhW.json",
|
|
119
|
+
"sha256": "<the hash in schema_TwoPhW.json.sha256>" }
|
|
120
|
+
},
|
|
121
|
+
"targets": [
|
|
122
|
+
{ "root": "src/renderer",
|
|
123
|
+
"IPSME": { "main": "App.js", "object": "App", "runtime": "electron-renderer", "shape": "component", "out": "IPSME" },
|
|
124
|
+
"schema-cache": "schema-cache", "out-npm": "generated",
|
|
125
|
+
"scope": "@rootintf", "prefix": "iface-", "delete": true,
|
|
126
|
+
"schemas": [ "Discovery", "TwoPhW" ] }
|
|
127
|
+
],
|
|
128
|
+
"version": 4
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
The pin is yours alone: this package carries no schema and is not built against one. Whatever
|
|
133
|
+
interface you generate here is the one you hand to `protocol_TwoPhW`.
|
|
134
|
+
|
|
135
|
+
### 3. Generate
|
|
136
|
+
|
|
137
|
+
```sh
|
|
138
|
+
npx iface-gen --config ifacegenconfig.json
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
This gives you, with nothing to write by hand:
|
|
142
|
+
|
|
143
|
+
- `generated/iface-TwoPhW/`: `JSON_MsgWarp`, `JSON_AckWarp`, `JSON_EffEngage`, `JSON_AckEngage`
|
|
144
|
+
(`.validate`, and `.merge` / `.create_with_cause_merge` where the base has one) and `VERSION`;
|
|
145
|
+
- `IPSME/Responder_TwoPhW.js`: a stub with one `handler_json_<message>` per message and the base
|
|
146
|
+
dispatch (`handler_json_ack`, `handler_json_eff`, `handler_json_msg`);
|
|
147
|
+
- the bridge wired to it (import, logging, one call per base level, the `reprocess` list), and
|
|
148
|
+
`TwoPhW` in the introspection table.
|
|
149
|
+
|
|
150
|
+
If the bridge comes back with merge markers, resolve them as described in the iface-gen README.
|
|
151
|
+
|
|
152
|
+
### 4. Fill in the responder
|
|
153
|
+
|
|
154
|
+
Everything below goes in the generated `IPSME/Responder_TwoPhW.js`. **Leave the generated dispatch
|
|
155
|
+
(`handler_json_ack` / `_eff` / `_msg`) as it is.**
|
|
156
|
+
|
|
157
|
+
#### Imports and logging
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
161
|
+
import { LOGR, l_array } from '@knev/bitlogr';
|
|
162
|
+
|
|
163
|
+
import { JSON_MsgWarp, JSON_AckWarp, JSON_EffEngage, JSON_AckEngage } from '../generated/iface-TwoPhW'; // generated
|
|
164
|
+
|
|
165
|
+
// bind the package to YOUR generated interface. What it returns are the generated namespaces
|
|
166
|
+
// extended with the protocol's own constructors, so take the ones you need under an alias
|
|
167
|
+
import * as iface_TwoPhW from '../generated/iface-TwoPhW';
|
|
168
|
+
import { protocol_TwoPhW, logr as logr_twoPhW_ } from '@rootintf/protocol-2phw';
|
|
169
|
+
|
|
170
|
+
const { TwoPhW, JSON_EffEngage: TwoPhW_EffEngage } = protocol_TwoPhW(iface_TwoPhW);
|
|
171
|
+
|
|
172
|
+
const logr_= LOGR_.create({ name: "Responder_TwoPhW", labels : obj_labels_ }); // generated
|
|
173
|
+
const l_= logr_.l; // generated
|
|
174
|
+
|
|
175
|
+
// export this responder's logr AND the package's as one, so the bridge wires both
|
|
176
|
+
const logr_wired_= LOGR_.wire([ logr_, logr_twoPhW_ ]);
|
|
177
|
+
export { logr_wired_ as logr };
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### Constructor: one `TwoPhW` over this participant's event log, configured per role
|
|
181
|
+
|
|
182
|
+
```js
|
|
183
|
+
constructor(interface_IPSME, interface_App) {
|
|
184
|
+
this.interface_IPSME = interface_IPSME || {};
|
|
185
|
+
this.interface_App = interface_App || {};
|
|
186
|
+
|
|
187
|
+
this._callback_ackWarp = this._callback_ackWarp.bind(this);
|
|
188
|
+
this._callback_ackEngage = this._callback_ackEngage.bind(this);
|
|
189
|
+
this._callback_msg_sent_expired = this._callback_msg_sent_expired.bind(this);
|
|
190
|
+
|
|
191
|
+
this.twoPhW = new TwoPhW(this.interface_IPSME.eventLog);
|
|
192
|
+
this.twoPhW.config.sender(this._callback_ackWarp, this._callback_ackEngage, this._callback_msg_sent_expired);
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Configure only the role(s) the participant plays: `config.sender(...)`, `config.receiver(...)`.
|
|
197
|
+
|
|
198
|
+
#### Sender
|
|
199
|
+
|
|
200
|
+
The operation the app calls, and the two callbacks the package calls when it has accepted a step:
|
|
201
|
+
|
|
202
|
+
```js
|
|
203
|
+
// phase 1
|
|
204
|
+
warp(json_user, json_hyperport) {
|
|
205
|
+
const json_msgWarp = JSON_MsgWarp.merge({ id: uuidv4(), user: json_user, hyperport: json_hyperport, ttl_msec: k_TTL_MSEC_ });
|
|
206
|
+
json_msgWarp.referer = this.interface_IPSME.karr_referer;
|
|
207
|
+
const str_msgWarp = JSON.stringify(json_msgWarp);
|
|
208
|
+
|
|
209
|
+
if (! this.twoPhW.register_msgWarp(str_msgWarp, json_msgWarp)) // legality + log + expiry timer
|
|
210
|
+
return false;
|
|
211
|
+
|
|
212
|
+
this.interface_IPSME.MsgEnv.publish(str_msgWarp);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// phase 1 answered: a nack ends the exchange; an ack is where phase 2 starts
|
|
217
|
+
_callback_ackWarp(msg, json_ackWarp) {
|
|
218
|
+
if (json_ackWarp.ack.ok !== true) {
|
|
219
|
+
this.interface_App.cancel_modal();
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const json_effEngage = TwoPhW_EffEngage.create_with_Warp(json_ackWarp._cause);
|
|
224
|
+
json_effEngage.referer = this.interface_IPSME.karr_referer;
|
|
225
|
+
const str_effEngage = JSON.stringify(json_effEngage);
|
|
226
|
+
|
|
227
|
+
if (! this.twoPhW.register_effEngage(str_effEngage, json_effEngage)) {
|
|
228
|
+
this.interface_App.cancel_modal();
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
this.interface_IPSME.MsgEnv.publish(str_effEngage);
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// phase 2 answered
|
|
237
|
+
_callback_ackEngage(msg, json_ackEngage) {
|
|
238
|
+
if (json_ackEngage.ack.ok !== true) {
|
|
239
|
+
this.interface_App.cancel_modal();
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
this.interface_App.close_main_window();
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// a warp or an engage we sent was never answered inside its window
|
|
248
|
+
_callback_msg_sent_expired(msg, json_msg) {
|
|
249
|
+
this.interface_App.cancel_modal();
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
This sender only sends two-phase warps. One that also sends `instantaneous` warps must treat an
|
|
254
|
+
`ok` ack of such a warp as the end of the exchange in `_callback_ackWarp`
|
|
255
|
+
(`json_ackWarp._cause.warp.instantaneous`), instead of building an engage: `register_effEngage`
|
|
256
|
+
refuses it.
|
|
257
|
+
|
|
258
|
+
The generated stubs for the messages a sender receives are one forwarding line each:
|
|
259
|
+
|
|
260
|
+
```js
|
|
261
|
+
handler_json_ackWarp(msg, json_ackWarp) { return this.twoPhW.handler_json_ackWarp(msg, json_ackWarp); }
|
|
262
|
+
handler_json_ackEngage(msg, json_ackEngage) { return this.twoPhW.handler_json_ackEngage(msg, json_ackEngage); }
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
#### Receiver
|
|
266
|
+
|
|
267
|
+
```js
|
|
268
|
+
// in the constructor
|
|
269
|
+
this.twoPhW.config.receiver(this._callback_msgWarp, this._callback_effEngage, this._callback_msg_sent_expired);
|
|
270
|
+
|
|
271
|
+
// a legal warp arrived: decide, answer
|
|
272
|
+
_callback_msgWarp(msg, json_msgWarp) {
|
|
273
|
+
const b_ok = this.interface_App.can_admit(json_msgWarp.warp.user, json_msgWarp.warp.hyperport);
|
|
274
|
+
|
|
275
|
+
const json_ackWarp = TwoPhW_AckWarp.create_with_Warp_Ok(json_msgWarp, b_ok);
|
|
276
|
+
json_ackWarp.referer = this.interface_IPSME.karr_referer;
|
|
277
|
+
const str_ackWarp = JSON.stringify(json_ackWarp);
|
|
278
|
+
|
|
279
|
+
if (this.twoPhW.register_ackWarp(str_ackWarp, json_ackWarp))
|
|
280
|
+
this.interface_IPSME.MsgEnv.publish(str_ackWarp);
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// a legal engage arrived for a warp we acknowledged: commit, answer
|
|
285
|
+
_callback_effEngage(msg, json_effEngage) {
|
|
286
|
+
const b_ok = this.interface_App.admit(json_effEngage._cause.warp.user);
|
|
287
|
+
|
|
288
|
+
const json_ackEngage = TwoPhW_AckEngage.create_with_Engage_Ok(json_effEngage, b_ok);
|
|
289
|
+
json_ackEngage.referer = this.interface_IPSME.karr_referer;
|
|
290
|
+
const str_ackEngage = JSON.stringify(json_ackEngage);
|
|
291
|
+
|
|
292
|
+
if (this.twoPhW.register_ackEngage(str_ackEngage, json_ackEngage))
|
|
293
|
+
this.interface_IPSME.MsgEnv.publish(str_ackEngage);
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// the generated stubs for the messages a receiver receives
|
|
298
|
+
handler_json_msgWarp(msg, json_msgWarp) { return this.twoPhW.handler_json_msgWarp(msg, json_msgWarp); }
|
|
299
|
+
handler_json_effEngage(msg, json_effEngage) { return this.twoPhW.handler_json_effEngage(msg, json_effEngage); }
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
(`TwoPhW_AckWarp` / `TwoPhW_AckEngage` are this package's `JSON_AckWarp` / `JSON_AckEngage`, imported
|
|
303
|
+
under an alias like `TwoPhW_EffEngage` above. `can_admit` / `admit` stand for your app interface.)
|
|
304
|
+
|
|
305
|
+
For an `instantaneous` warp there is no engage: a receiver that accepts one commits in
|
|
306
|
+
`_callback_msgWarp`, when it acknowledges with `ok: true`.
|
|
307
|
+
|
|
308
|
+
#### `reprocess()`
|
|
309
|
+
|
|
310
|
+
Leave it empty. The package's expiries are timers on the event log, not parked events.
|
|
311
|
+
|
|
312
|
+
### 5. Rules
|
|
313
|
+
|
|
314
|
+
- **Configure only the role(s) you play.** A step's callback is what makes the step yours: the
|
|
315
|
+
package refuses a step whose callback was never set, before logging anything. A sender-only
|
|
316
|
+
participant (only `config.sender(...)` called) does not handle or log a warp or an engage; a
|
|
317
|
+
receiver-only one does not handle an ack. A handler of an unplayed role may therefore stay the
|
|
318
|
+
generated stub or forward to the package; both return `false`.
|
|
319
|
+
- **An exchange is a dialogue.** The package accepts a step you receive only after a step you sent,
|
|
320
|
+
and lets you register a send only after a step you received. Every participant hears every
|
|
321
|
+
exchange on the ME; this is what keeps a warp you merely heard from being "answered" by the ack
|
|
322
|
+
its real receiver sent, also when you play both roles.
|
|
323
|
+
- **Register, then publish, and only if registration succeeded.** `register_*` is the legality test
|
|
324
|
+
for your own send, the log entry the answers are matched against, and the expiry timer. A message
|
|
325
|
+
published without it can never be answered.
|
|
326
|
+
- **Stamp `referer` before you stringify and register.** The package identifies an exchange by the
|
|
327
|
+
warp's `id`, `dt` and `referer`.
|
|
328
|
+
- **The typed handlers do not validate.** They trust that the dispatch validated the message as that
|
|
329
|
+
type, which the generated dispatch does.
|
|
330
|
+
- **The callback's return value is the handler's return value** to the chain: `true` = handled. The
|
|
331
|
+
step is already logged when the callback runs, so returning `false` does not undo it.
|
|
332
|
+
- **Expiry is an outcome, not an error.** `_callback_msg_sent_expired` is how the app learns that a
|
|
333
|
+
send was never answered; there is no other signal. Each role has its own: the one passed to
|
|
334
|
+
`config.sender` fires for a warp or an engage you sent, the one passed to `config.receiver` for
|
|
335
|
+
an ack-warp you sent that an engage should have followed. A nack, and the ack of an
|
|
336
|
+
`instantaneous` warp, await nothing and never expire; neither does an ack-engage. A participant
|
|
337
|
+
playing both roles may pass two different functions.
|
|
338
|
+
- **Never build a 2PhW message by hand.** Use the generated constructors, and this package's for
|
|
339
|
+
what the schema cannot express (below).
|
|
340
|
+
|
|
341
|
+
---
|
|
342
|
+
|
|
343
|
+
## What this package exports
|
|
344
|
+
|
|
345
|
+
| export | what |
|
|
346
|
+
|---|---|
|
|
347
|
+
| `protocol_TwoPhW(iface_TwoPhW)` | binds the package to your generated interface. Returns `{ TwoPhW, VERSION, JSON_MsgWarp, JSON_AckWarp, JSON_HoldWarp, JSON_EffEngage, JSON_AckEngage }`. Throws, naming what is missing, when what it is given is not a generated 2PhW interface |
|
|
348
|
+
| `TwoPhW` (returned) | `new TwoPhW(eventLog)`: the protocol over one participant's event log |
|
|
349
|
+
| `twoPhW.config.sender(callback_ackWarp, callback_ackEngage, callback_msg_sent_expired, callback_holdWarp = null)` | the sender's callbacks. The last is optional: a hold moves the warp's expiry whether or not you want to be told |
|
|
350
|
+
| `twoPhW.config.receiver(callback_msgWarp, callback_effEngage, callback_msg_sent_expired)` | the receiver's callbacks |
|
|
351
|
+
| `twoPhW.register_msgWarp` / `register_effEngage` | sender: register a send. Returns `true`, or `false` when the step is not legal now |
|
|
352
|
+
| `twoPhW.register_ackWarp` / `register_ackEngage` | receiver: the same for its acks |
|
|
353
|
+
| `twoPhW.register_holdWarp` | receiver: the same for a hold. Legal for a warp it received, has not answered, and whose window is still open |
|
|
354
|
+
| `twoPhW.handler_json_holdWarp` | sender: a validated hold arrived. Moves the expiry of the warp to the end of the lease + `TTL` |
|
|
355
|
+
| `twoPhW.handler_json_ackWarp` / `handler_json_ackEngage` | sender: a validated ack arrived |
|
|
356
|
+
| `twoPhW.handler_json_msgWarp` / `handler_json_effEngage` | receiver: a validated request arrived |
|
|
357
|
+
| `JSON_MsgWarp`, `JSON_EffEngage`, `JSON_AckWarp`, `JSON_AckEngage` (returned) | your generated namespaces, extended (next table) |
|
|
358
|
+
| `JSON_HoldWarp` (returned) | your generated namespace, as it is: `create_with_cause_ttl(json_msgWarp, ttl_msec, body = {})` |
|
|
359
|
+
| `VERSION` (returned) | the protocol marker's value: your generated interface's |
|
|
360
|
+
| `window_msec(ttl_msec)` | the window in which a message that hands the turn over is answered: `2·TTL` |
|
|
361
|
+
| `StateInitialState`, `StateWarp`, `StateAckWarp`, `StateEngage`, `StateAckEngage`, `StateINVALID` | the states `twoPhW.eventLog.traverse_State(json_msgWarp).state` replays to |
|
|
362
|
+
| `JSON_Msg`, `JSON_Eff`, `JSON_Ack` | re-exported from `@rootintf/json-msg` |
|
|
363
|
+
| `logr` | the package's logger (labels `PROTOCOL_STATE`, `SENDER`, `RECVER`, `HANDLERS`, `DROPS`) |
|
|
364
|
+
|
|
365
|
+
The constructors the schema cannot express:
|
|
366
|
+
|
|
367
|
+
| constructor | why it is here |
|
|
368
|
+
|---|---|
|
|
369
|
+
| `JSON_EffEngage.create_with_Warp(json_msgWarp)` | the commit: `engage: { ok: true }`, caused by the warp |
|
|
370
|
+
| `JSON_AckWarp.create_with_Warp_Ok(json_msgWarp, b_ok = true)` | iface-gen emits no constructor for a `JSON_Ack`; stamps the marker |
|
|
371
|
+
| `JSON_AckEngage.create_with_Engage_Ok(json_effEngage, b_ok = true)` | the same |
|
|
372
|
+
| `JSON_MsgWarp.create_with_id_user_hyperport(id, user, hyperport, ttl_msec)` | shorthand for the generated `merge` |
|
|
373
|
+
| `JSON_MsgWarp.compare(json_L, json_R)` | are these the same warp (`dt`, `warp.id`, `referer`)? |
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## Testing your responder
|
|
378
|
+
|
|
379
|
+
Test at the message level, under a mocked clock: give the responder a context whose `publish`
|
|
380
|
+
records, a fresh `EventLog`, a fixed `karr_referer` and a recording app interface. Call the operation,
|
|
381
|
+
build the other side's answer with this package's constructors on what was published, pass it to the
|
|
382
|
+
responder's base dispatch, and assert the return value, what was published, and what reached the app.
|
|
383
|
+
|
|
384
|
+
```js
|
|
385
|
+
const responder = new Responder_TwoPhW(interface_IPSME, interface_App);
|
|
386
|
+
responder.warp('', json_hyperport);
|
|
387
|
+
|
|
388
|
+
const json_ackWarp = TwoPhW_AckWarp.create_with_Warp_Ok(JSON.parse(arr_str_published[0]), true);
|
|
389
|
+
json_ackWarp.referer = karr_referer_other;
|
|
390
|
+
expect(responder.handler_json_ack(JSON.stringify(json_ackWarp), json_ackWarp)).toBe(true); // consumed
|
|
391
|
+
expect(JSON_EffEngage.validate(JSON.parse(arr_str_published[1]))).toBe(true); // the engage went out
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Cases worth covering: the step at its last legal instant and at its deadline, a duplicate, a nack,
|
|
395
|
+
the expiry firing, and a stranger's exchange (not handled, nothing logged).
|
|
396
|
+
|
|
397
|
+
---
|
|
398
|
+
|
|
399
|
+
## Known limitations
|
|
400
|
+
|
|
401
|
+
- **A receiver logs a warp before it knows the warp is addressed to it.** The warp is in the log
|
|
402
|
+
when `_callback_msgWarp` runs, and stays there if the callback returns `false`. Nothing follows
|
|
403
|
+
from it (no ack of yours means no engage is accepted), but the event is there.
|
|
404
|
+
- **The windows assume the two clocks agree.** Every window compares the local clock with a `dt`
|
|
405
|
+
the other side stamped. If the receiver's clock is ahead of the sender's by more than `ttl_msec`,
|
|
406
|
+
the warp is already expired when it arrives; if it is behind, the windows stay open that much
|
|
407
|
+
longer.
|
|
408
|
+
2PhW does not measure or correct skew: keep participants on synchronised clocks (NTP), and pick a
|
|
409
|
+
`ttl_msec` well above the skew you expect.
|
|
410
|
+
- **A held warp is not cancelled by anybody.** The sender waits for as long as the receiver renews;
|
|
411
|
+
there is no message yet by which the user's "cancel" reaches the receiver.
|
|
412
|
+
- **Lost messages are not retried.** IPSME gives no delivery guarantee; the expiry is the only hook.
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## Developing this package
|
|
417
|
+
|
|
418
|
+
```sh
|
|
419
|
+
npm ci
|
|
420
|
+
npm run build # rollup -> dist
|
|
421
|
+
npm test # generates the tests' interface, then jasmine, against dist
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
The package ships no interface, so its tests play the host: `npm test` first generates one into
|
|
425
|
+
`tests/generated` from `tests/ifacegenconfig.json` (the generator is a dev dependency, for the tests
|
|
426
|
+
only) and hands it to `protocol_TwoPhW`. When `schema_TwoPhW.json` changes, re-pin its hash in
|
|
427
|
+
`tests/ifacegenconfig.json`; nothing under `src` or `dist` depends on it.
|