@schukai/monster 3.1.2 → 3.3.0
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/package.json +1 -1
- package/source/data/datasource/websocket.mjs +163 -187
- package/source/net/webconnect/message.mjs +55 -0
- package/source/net/webconnect.mjs +346 -0
- package/source/types/observablequeue.mjs +110 -0
- package/source/types/proxyobserver.mjs +2 -2
- package/source/types/uniquequeue.mjs +9 -6
- package/source/types/version.mjs +1 -1
- package/test/cases/data/datasource/websocket.mjs +118 -33
- package/test/cases/monster.mjs +1 -1
- package/test/cases/net/webconnect/message.mjs +50 -0
- package/test/cases/net/webconnect.mjs +116 -0
- package/test/cases/types/observablequeue.mjs +17 -0
- package/test/cases/types/queue.mjs +4 -1
|
@@ -24,16 +24,22 @@ describe('Websocket', function () {
|
|
|
24
24
|
if (ds) {
|
|
25
25
|
ds.close()
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
// without this, the node test will hang
|
|
27
|
+
|
|
28
|
+
// workaround: without this, the node test will hang
|
|
29
29
|
for (const sym of Object.getOwnPropertySymbols(ds)) {
|
|
30
|
-
if (sym.toString() ==='Symbol(connection)') {
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
if (sym.toString() === 'Symbol(connection)') {
|
|
31
|
+
const connection = ds[sym]
|
|
32
|
+
for (const sym2 of Object.getOwnPropertySymbols(connection)) {
|
|
33
|
+
if (sym2.toString() === 'Symbol(connection)') {
|
|
34
|
+
const socket = connection[sym2]?.socket;
|
|
35
|
+
if (socket) {
|
|
36
|
+
socket.terminate()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
33
39
|
}
|
|
34
40
|
}
|
|
35
41
|
}
|
|
36
|
-
|
|
42
|
+
|
|
37
43
|
done()
|
|
38
44
|
});
|
|
39
45
|
|
|
@@ -41,12 +47,90 @@ describe('Websocket', function () {
|
|
|
41
47
|
|
|
42
48
|
ds = new WebSocketDatasource(testUrl)
|
|
43
49
|
const clone = ds.getClone()
|
|
44
|
-
|
|
50
|
+
|
|
45
51
|
expect(clone).to.be.an.instanceof(WebSocketDatasource)
|
|
46
52
|
|
|
47
|
-
|
|
53
|
+
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('should transform data', function (done) {
|
|
57
|
+
|
|
58
|
+
let writeCallbackCalled = false
|
|
59
|
+
let readCallbackCalled = false
|
|
60
|
+
|
|
61
|
+
ds = new WebSocketDatasource({
|
|
62
|
+
url: testUrl,
|
|
63
|
+
write: {
|
|
64
|
+
mapping: {
|
|
65
|
+
transformer: "call:onWrite",
|
|
66
|
+
callbacks: {
|
|
67
|
+
onWrite: (data) => {
|
|
68
|
+
writeCallbackCalled = true
|
|
69
|
+
return data
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
sheathing: {
|
|
74
|
+
object: {
|
|
75
|
+
demo: 1,
|
|
76
|
+
data: {
|
|
77
|
+
xyz: undefined
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
path: "data.xyz",
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
read: {
|
|
84
|
+
mapping: {
|
|
85
|
+
transformer: "call:onRead",
|
|
86
|
+
callbacks: {
|
|
87
|
+
onRead: (data) => {
|
|
88
|
+
readCallbackCalled = true
|
|
89
|
+
return data
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
path: 'data.xyz',
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
ds.connect().then(() => {
|
|
98
|
+
ds.set({
|
|
99
|
+
envelop: {
|
|
100
|
+
message: "Hello World"
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
ds.write().then(() => {
|
|
105
|
+
|
|
106
|
+
ds.set({})
|
|
107
|
+
expect(ds.get()).to.be.deep.equal({});
|
|
108
|
+
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
ds.read().then(() => {
|
|
113
|
+
expect(ds.get()).to.be.deep.equal({envelop:{message: "Hello World"}});
|
|
114
|
+
expect(writeCallbackCalled).to.be.true
|
|
115
|
+
expect(readCallbackCalled).to.be.true
|
|
116
|
+
done()
|
|
117
|
+
}).catch((e) => {
|
|
118
|
+
done(e)
|
|
119
|
+
})
|
|
120
|
+
}, 200)
|
|
121
|
+
|
|
122
|
+
}).catch((err) => {
|
|
123
|
+
done(new Error(err));
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
}).catch((e) => {
|
|
128
|
+
done(e)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
|
|
48
132
|
})
|
|
49
|
-
|
|
133
|
+
|
|
50
134
|
it('should connect', function (done) {
|
|
51
135
|
ds = new WebSocketDatasource({
|
|
52
136
|
url: testUrl,
|
|
@@ -54,11 +138,11 @@ describe('Websocket', function () {
|
|
|
54
138
|
enabled: false
|
|
55
139
|
}
|
|
56
140
|
});
|
|
57
|
-
ds.connect()
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
done()
|
|
61
|
-
}
|
|
141
|
+
ds.connect().then(() => {
|
|
142
|
+
done()
|
|
143
|
+
}).catch((e) => {
|
|
144
|
+
done(e)
|
|
145
|
+
})
|
|
62
146
|
|
|
63
147
|
|
|
64
148
|
})
|
|
@@ -70,36 +154,37 @@ describe('Websocket', function () {
|
|
|
70
154
|
enabled: false
|
|
71
155
|
}
|
|
72
156
|
});
|
|
73
|
-
ds.connect()
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
setTimeout(() => {
|
|
157
|
+
ds.connect().then(() => {
|
|
158
|
+
ds.set({
|
|
159
|
+
envelop: {
|
|
160
|
+
message: "Hello World"
|
|
161
|
+
}
|
|
162
|
+
})
|
|
82
163
|
|
|
83
164
|
ds.write().then(() => {
|
|
84
165
|
|
|
85
166
|
ds.set({})
|
|
86
167
|
expect(ds.get()).to.be.deep.equal({});
|
|
87
168
|
|
|
88
|
-
|
|
89
169
|
setTimeout(() => {
|
|
90
170
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
},
|
|
171
|
+
ds.read().then(() => {
|
|
172
|
+
expect(ds.get()).to.be.deep.equal({envelop:{message: "Hello World"}});
|
|
173
|
+
done()
|
|
174
|
+
}).catch((e) => {
|
|
175
|
+
done(e)
|
|
176
|
+
})
|
|
177
|
+
},500)
|
|
178
|
+
|
|
179
|
+
|
|
98
180
|
}).catch((err) => {
|
|
99
181
|
done(new Error(err));
|
|
100
182
|
})
|
|
101
|
-
|
|
102
|
-
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
}).catch((e) => {
|
|
186
|
+
done(e)
|
|
187
|
+
})
|
|
103
188
|
|
|
104
189
|
|
|
105
190
|
}).timeout(10000);
|
package/test/cases/monster.mjs
CHANGED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {expect} from "chai"
|
|
2
|
+
import {Message} from "../../../../../application/source/net/webconnect/message.mjs";
|
|
3
|
+
|
|
4
|
+
describe('Message', function () {
|
|
5
|
+
|
|
6
|
+
it('construct withouth parameters should throw', function (done) {
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
new Message();
|
|
10
|
+
done(new Error('should throw'));
|
|
11
|
+
} catch (e) {
|
|
12
|
+
done();
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('from json should ' , function (done) {
|
|
19
|
+
const json = {
|
|
20
|
+
"id": "123",
|
|
21
|
+
"type": "test",
|
|
22
|
+
"data": {
|
|
23
|
+
"test": "test"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const message = Message.fromJSON(JSON.stringify(json));
|
|
27
|
+
const data = message.getData();
|
|
28
|
+
expect(data.id).to.equal(json.id);
|
|
29
|
+
expect(data.type).to.equal(json.type);
|
|
30
|
+
expect(data.data).to.deep.equal(json.data);
|
|
31
|
+
done();
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it ("to json should", function (done) {
|
|
35
|
+
const obj = {
|
|
36
|
+
"id": "123",
|
|
37
|
+
"type": "test",
|
|
38
|
+
"data": {
|
|
39
|
+
"test": "test"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const message = new Message(obj);
|
|
43
|
+
const data = JSON.stringify(message);
|
|
44
|
+
expect(data).to.equal('{"id":"123","type":"test","data":{"test":"test"}}');
|
|
45
|
+
done();
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {expect} from "chai"
|
|
2
|
+
import {WebConnect} from "../../../../application/source/net/webconnect.mjs";
|
|
3
|
+
import {Message} from "../../../../application/source/net/webconnect/message.mjs";
|
|
4
|
+
import {Observer} from "../../../../application/source/types/observer.mjs";
|
|
5
|
+
import {initWebSocket} from "../../util/websocket.mjs";
|
|
6
|
+
|
|
7
|
+
const testUrl = "wss://ws.postman-echo.com/raw"
|
|
8
|
+
|
|
9
|
+
describe('Websocket', function () {
|
|
10
|
+
|
|
11
|
+
let ds = undefined
|
|
12
|
+
|
|
13
|
+
before(function (done) {
|
|
14
|
+
initWebSocket().then(() => {
|
|
15
|
+
done()
|
|
16
|
+
}).catch((e) => {
|
|
17
|
+
done(e)
|
|
18
|
+
})
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(function (done) {
|
|
22
|
+
if (ds) {
|
|
23
|
+
ds.close()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// without this, the node test will hang
|
|
27
|
+
for (const sym of Object.getOwnPropertySymbols(ds)) {
|
|
28
|
+
if (sym.toString() === 'Symbol(connection)') {
|
|
29
|
+
if (ds[sym]?.socket?.['terminate']) {
|
|
30
|
+
ds[sym]?.socket?.['terminate']()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
done()
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
it('should transform data', function (done) {
|
|
40
|
+
|
|
41
|
+
ds = new WebConnect( {
|
|
42
|
+
url: testUrl,
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
ds.connect().then(() => {
|
|
46
|
+
|
|
47
|
+
ds.attachObserver(new Observer(()=> {
|
|
48
|
+
done()
|
|
49
|
+
}))
|
|
50
|
+
|
|
51
|
+
ds.send({
|
|
52
|
+
data: {
|
|
53
|
+
message: "Hello World"
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
}).catch((e) => {
|
|
58
|
+
done(e)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should connect', function (done) {
|
|
65
|
+
ds = new WebConnect({
|
|
66
|
+
url: testUrl,
|
|
67
|
+
reconnect: {
|
|
68
|
+
enabled: false
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
ds.connect().then(() => {
|
|
72
|
+
done()
|
|
73
|
+
}).catch((e) => {
|
|
74
|
+
done(e)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should send message', function (done) {
|
|
81
|
+
ds = new WebConnect({
|
|
82
|
+
url: testUrl,
|
|
83
|
+
reconnect: {
|
|
84
|
+
enabled: false
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
ds.connect().then(() => {
|
|
88
|
+
|
|
89
|
+
ds.attachObserver(new Observer(()=> {
|
|
90
|
+
|
|
91
|
+
expect(ds.dataReceived()).to.be.true
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const msg = ds.poll()
|
|
95
|
+
expect(msg).to.be.instanceOf(Message)
|
|
96
|
+
const data = msg.getData()
|
|
97
|
+
expect(data).to.be.deep.equal({message: "Hello World"})
|
|
98
|
+
} catch (e) {
|
|
99
|
+
done(e)
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
done()
|
|
103
|
+
}))
|
|
104
|
+
|
|
105
|
+
ds.send({
|
|
106
|
+
message: "Hello World"
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
}).catch((e) => {
|
|
110
|
+
done(e)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
}).timeout(10000);
|
|
115
|
+
|
|
116
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {expect} from "chai"
|
|
2
|
+
import {ObservableQueue} from "../../../../application/source/types/observablequeue.mjs";
|
|
3
|
+
import {Observer} from "../../../../application/source/types/observer.mjs";
|
|
4
|
+
|
|
5
|
+
describe('ObservableQueue', function () {
|
|
6
|
+
describe('Observer', function () {
|
|
7
|
+
|
|
8
|
+
it('should notify', function (done) {
|
|
9
|
+
let queue = new ObservableQueue;
|
|
10
|
+
let o = new Observer((q) => {
|
|
11
|
+
done()
|
|
12
|
+
});
|
|
13
|
+
queue.attachObserver(o);
|
|
14
|
+
expect(queue.add('a')).to.be.instanceOf(ObservableQueue);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
})
|