@rootintf/protocol-2phw 0.6.9

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.
@@ -0,0 +1,159 @@
1
+
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import { TwoPhW, JSON_MsgOptions, JSON_MsgWarp, JSON_MsgEngage, JSON_MsgAckWarp, JSON_MsgAckEngage, l as l_2PhW } from '../src/twoPhW.mjs';
4
+ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '@rootintf/json-msg'
5
+
6
+ const k_VERSION= "0.6";
7
+
8
+ var twoPhW= new TwoPhW();
9
+ twoPhW.config.logr= {
10
+ VALIDATION : 1
11
+ }
12
+
13
+ const str_uuid_ID_ = uuidv4();
14
+ console.log('str_uuid_ID_= ' + str_uuid_ID_);
15
+
16
+ const json_msgWarp_ = {
17
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
18
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
19
+ dt: "2022-10-28T06:26:19.692Z",
20
+ warp: {
21
+ user: {},
22
+ hyperport: {},
23
+ ttl_msec : 30
24
+ },
25
+ "2PhW": k_VERSION
26
+ }
27
+
28
+ const json_msgEngage_ = {
29
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
30
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
31
+ dt: "2022-10-28T06:26:19.692Z",
32
+ engage: {
33
+ user: {},
34
+ hyperport: {}
35
+ },
36
+ "2PhW": k_VERSION
37
+ }
38
+
39
+ const json_msgAckWarp_str_ = {
40
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
41
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
42
+ dt: "2022-10-28T06:26:19.692Z",
43
+ ack: {
44
+ "ok" : true,
45
+ },
46
+ "_msg" : JSON.stringify( json_msgWarp_ ),
47
+ "2PhW": k_VERSION
48
+ }
49
+
50
+ const json_msgAckWarp_obj_ = {
51
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
52
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
53
+ dt: "2022-10-28T06:26:19.692Z",
54
+ ack: {
55
+ "ok" : true,
56
+ },
57
+ "_msg" : json_msgWarp_,
58
+ "2PhW": k_VERSION
59
+ }
60
+
61
+ const json_msgAckEngage_ = {
62
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
63
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
64
+ dt: "2022-10-28T06:26:19.692Z",
65
+ ack: {
66
+ "ok" : false,
67
+ },
68
+ "_msg" : json_msgEngage_,
69
+ "2PhW": k_VERSION
70
+ }
71
+
72
+ const json_msgOptions_ = {
73
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
74
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
75
+ dt: "2022-10-28T06:26:19.692Z",
76
+ options: {
77
+ },
78
+ "_msg" : json_msgWarp_,
79
+ "2PhW": k_VERSION
80
+ }
81
+
82
+
83
+ describe("Validation", function () {
84
+ it("JSON_Msg.validate(json_msgWarp_)", function () {
85
+ expect( JSON_Msg.validate(json_msgWarp_) ).toBe(true);
86
+ });
87
+ });
88
+
89
+ describe("Validation", function () {
90
+ it("twoPhW.JSON_MsgOptions.validate(json_msgOptions_)", function () {
91
+ expect( JSON_MsgOptions.validate(json_msgOptions_) ).toBe(true);
92
+ });
93
+ });
94
+
95
+ describe("Validation", function () {
96
+ it("twoPhW.JSON_MsgWarp.validate(json_msgWarp_)", function () {
97
+ expect( JSON_MsgWarp.validate(json_msgWarp_) ).toBe(true);
98
+ });
99
+ });
100
+
101
+ describe("Validation", function () {
102
+ it("twoPhW.JSON_MsgEngage.validate(json_msgEngage_)", function () {
103
+ expect( JSON_MsgEngage.validate(json_msgEngage_) ).toBe(true);
104
+ });
105
+ });
106
+
107
+ describe("Validation", function () {
108
+ it("JSON_MsgAckWarp.validate(json_msgAckWarp_str_)", function () {
109
+ expect( JSON_MsgAckWarp.validate(json_msgAckWarp_str_) ).toBe(false);
110
+ });
111
+ it("JSON_MsgAckWarp.validate(json_msgAckWarp_obj_)", function () {
112
+ expect( JSON_MsgAckWarp.validate(json_msgAckWarp_obj_) ).toBe(true);
113
+ });
114
+ });
115
+
116
+ describe("Validation", function () {
117
+ it("JSON_MsgAckEngage.validate(json_msgAckEngage_)", function () {
118
+ expect( JSON_MsgAckEngage.validate(json_msgAckEngage_) ).toBe(true);
119
+ });
120
+ });
121
+
122
+ // beforeEach(function (done) {
123
+ // setTimeout(function () {
124
+
125
+ // function callback_Ack_Warp_(msg, json_Ack, json_Warp, json_Engage) {
126
+ // // console.log('callback_Ack_Warp: Ack: ', json_Ack);
127
+
128
+ // // console.log('callback_Ack_Warp: publish: Engage: ', json_Engage);
129
+ // // IPSME_MsgEnv.publish( JSON.stringify(json_Engage) );
130
+
131
+ // done();
132
+
133
+ // return true;
134
+ // }
135
+
136
+ // twoPhW.config.options= {
137
+ // str_referer_id : str_uuid_ID_,
138
+ // callback_Warp : callback_Ack_Warp_,
139
+ // logr : {
140
+ // // HANDLERS : 1,
141
+ // VALIDATION : 1
142
+ // }
143
+ // }
144
+
145
+ // twoPhW.handler_msg(json_Warp_);
146
+
147
+ // // done();
148
+
149
+ // }, 100);
150
+ // });
151
+
152
+ // it('does a thing', function (done) {
153
+ // someAsyncFunction(function (result) {
154
+ // expect(result).toEqual(someExpectedValue);
155
+ // done();
156
+ // });
157
+ // });
158
+
159
+ //-------------------------------------------------------------------------------------------------
@@ -0,0 +1,175 @@
1
+
2
+ // throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
3
+ // https://stackoverflow.com/questions/69118216/crypto-getrandomvalues-not-supported-in-react-js
4
+ // - https://stackoverflow.com/questions/52612122/how-to-use-jest-to-test-functions-using-crypto-or-window-mscrypto
5
+ import { v4 as uuidv4 } from 'uuid';
6
+ import crypto from 'crypto'
7
+ Object.defineProperty(globalThis, 'crypto', {
8
+ value: {
9
+ getRandomValues: arr => crypto.randomBytes(arr.length)
10
+ }
11
+ });
12
+
13
+ import { TwoPhW, JSON_MsgOptions, JSON_MsgWarp, JSON_MsgEngage, l as l_2PhW } from '../src/twoPhW.mjs';
14
+
15
+ console.log('l_2PhW', l_2PhW, Object.keys(l_2PhW).length);
16
+
17
+ const recv_str_uuid_ID_ = uuidv4();
18
+ console.log('recv_str_uuid_ID_= ' + recv_str_uuid_ID_);
19
+
20
+ const send_str_uuid_ID_ = uuidv4();
21
+ console.log('send_str_uuid_ID_= ' + send_str_uuid_ID_);
22
+
23
+ var twoPhW_recv_= new TwoPhW();
24
+ var twoPhW_send_= new TwoPhW();
25
+
26
+ twoPhW_recv_.config.receiver( callback_msgWarp_, callback_msgEngage_ );
27
+ twoPhW_recv_.config.callback_msgOptions= callback_msgOptions_;
28
+ twoPhW_recv_.config.logr= {
29
+ // HANDLERS : 1,
30
+ // VALIDATION : 1
31
+ };
32
+
33
+ twoPhW_send_.config.sender( callback_msgAckWarp_, callback_msgAckEngage_ );
34
+ twoPhW_send_.config.callback_msgOptions= callback_msgOptions_;
35
+ twoPhW_send_.config.logr= {
36
+ HANDLERS : 1,
37
+ // VALIDATION : 1
38
+ };
39
+
40
+ //-------------------------------------------------------------------------------------------------
41
+
42
+ function callback_msgOptions_(msg, json_msgOptions)
43
+ {
44
+ console.log('callback_msgOptions_: ', json_msgOptions);
45
+
46
+ return true;
47
+ }
48
+
49
+ //-------------------------------------------------------------------------------------------------
50
+ // Sender
51
+
52
+ function callback_msgAckWarp_(msg, json_msgAckWarp)
53
+ {
54
+ console.log('callback_msgAckWarp_: Ack: ', json_msgAckWarp);
55
+
56
+ // console.log('callback_msgAckWarp_: publish: Engage: ', json_Engage);
57
+ // IPSME_MsgEnv.publish( JSON.stringify(json_Engage) );
58
+
59
+ return true;
60
+ }
61
+
62
+ function callback_msgAckEngage_(msg, json_msgAckEngage)
63
+ {
64
+ console.log('callback_msgAckEngage_: Ack: ', json_msgAckEngage);
65
+
66
+ return true;
67
+ }
68
+
69
+ //-------------------------------------------------------------------------------------------------
70
+ // Receiver
71
+
72
+ function callback_msgWarp_(msg, json_msgWarp)
73
+ {
74
+ // console.log('callback_msgWarp_: Warp: ', json_Warp);
75
+ console.log('callback_msgWarp_: Warp: ...');
76
+
77
+ // console.log('callback_msgWarp_: publish Ack Warp: ', json_Ack);
78
+ // IPSME_MsgEnv.publish( JSON.stringify(json_Ack) );
79
+
80
+ return true;
81
+ }
82
+
83
+ function callback_msgEngage_(msg, json_msgEngage)
84
+ {
85
+ // console.log('callback_msgEngage_: Engage: ', json_Engage);
86
+ console.log('callback_msgEngage_: Engage: ...');
87
+
88
+ // console.log('callback_msgEngage_: publish: Ack Engage: ', json_Ack);
89
+ // IPSME_MsgEnv.publish( JSON.stringify(json_Ack) );
90
+
91
+ return true;
92
+ }
93
+
94
+ //-------------------------------------------------------------------------------------------------
95
+
96
+ function callback_Abort_()
97
+ {
98
+ console.log('callback_Abort: ... : ');
99
+
100
+ return true;
101
+ }
102
+
103
+ //-------------------------------------------------------------------------------------------------
104
+
105
+ const json_Warp_ = {
106
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
107
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
108
+ dt: "2022-10-28T06:26:19.692Z",
109
+ warp: {
110
+ user: {},
111
+ hyperport: {},
112
+ ttl_msec: 4000
113
+ },
114
+ "2PhW" : "0.6"
115
+ }
116
+
117
+ const json_Engage_ = {
118
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
119
+ dt: "2022-10-28T06:26:19.692Z",
120
+ engage: {
121
+ id: "f5d6c2b8-1481-49b2-882c-262bb1c9ef8c",
122
+ user: {},
123
+ hyperport: {}
124
+ }
125
+ }
126
+
127
+ const json_msgWarp_= twoPhW_send_.create_MsgWarp_out(recv_str_uuid_ID_, { "user": "user"}, { "hyperport" : "hyperport" }, 4000);
128
+ json_msgWarp_.referer= send_str_uuid_ID_;
129
+
130
+ const ttl_warp= json_msgWarp_.warp.ttl_msec;
131
+
132
+ const json_msgOptions= JSON_MsgOptions.create_with_msg_options(json_msgWarp_, {
133
+ "ttl_msec_incr" : [ ttl_warp, 3000 ]
134
+ });
135
+ json_msgOptions.referer= recv_str_uuid_ID_;
136
+
137
+ // https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep
138
+ await new Promise(r => setTimeout(r, 1500));
139
+
140
+ twoPhW_send_.handler_msg(json_msgOptions);
141
+
142
+
143
+ // twoPhW_recv_.handler_msg(json_msgWarp_);
144
+ // twoPhW.handler_msg(json_Engage_);
145
+
146
+ //-------------------------------------------------------------------------------------------------
147
+
148
+ const json_Ack_str_ = {
149
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
150
+ dt: "2022-10-28T06:26:19.692Z",
151
+ ack: {
152
+ "ok" : true,
153
+ "_msg" : JSON.stringify( json_Warp_ )
154
+ }
155
+ }
156
+
157
+ const json_Ack_obj_ = {
158
+ referer: "af9447e3-4b36-424f-8d07-976a9216a298",
159
+ dt: "2022-10-28T06:26:19.692Z",
160
+ ack: {
161
+ "ok" : true,
162
+ "_msg" : json_Warp_.warp
163
+ }
164
+ }
165
+
166
+ // twoPhW.handler_msg(json_Ack_str_);
167
+
168
+ //-------------------------------------------------------------------------------------------------
169
+
170
+ twoPhW_recv_.config.options= {
171
+ logr : {
172
+ DROPS : 1
173
+ }
174
+ }
175
+ // twoPhW.handler_msg('THIS msg SHOULD DROP');