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