@stateworks/integration 1.0.0 → 2.0.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/dist/client.js +151 -1
- package/dist/index.js +19 -1
- package/dist/sdk.js +89 -1
- package/dist/types.js +2 -1
- package/package.json +1 -1
package/dist/client.js
CHANGED
|
@@ -1 +1,151 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Client = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
/**
|
|
6
|
+
* Client
|
|
7
|
+
*/
|
|
8
|
+
class Client {
|
|
9
|
+
/**
|
|
10
|
+
* Constructor
|
|
11
|
+
*
|
|
12
|
+
* @param ws The Websocket instance
|
|
13
|
+
*/
|
|
14
|
+
constructor(ws) {
|
|
15
|
+
/**
|
|
16
|
+
* Promises
|
|
17
|
+
*/
|
|
18
|
+
this.promises = new Map();
|
|
19
|
+
/**
|
|
20
|
+
* Channels
|
|
21
|
+
*/
|
|
22
|
+
this.channels = new Map();
|
|
23
|
+
/**
|
|
24
|
+
* Handlers
|
|
25
|
+
*/
|
|
26
|
+
this.handlers = new Map();
|
|
27
|
+
this.ws = ws;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Subscribe
|
|
31
|
+
*
|
|
32
|
+
* @param channel The channel that will be subscribed
|
|
33
|
+
* @param callback The handler that will be called on channel message
|
|
34
|
+
*/
|
|
35
|
+
async subscribe(channel, callback) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const transaction_id = (0, uuid_1.v4)();
|
|
38
|
+
this.channels.set(channel, callback);
|
|
39
|
+
this.promises.set(transaction_id, {
|
|
40
|
+
on_success: resolve,
|
|
41
|
+
on_error: reject,
|
|
42
|
+
});
|
|
43
|
+
this.ws.send(JSON.stringify({
|
|
44
|
+
transaction_id: transaction_id,
|
|
45
|
+
action: 'subscribe',
|
|
46
|
+
params: {
|
|
47
|
+
channel: channel,
|
|
48
|
+
},
|
|
49
|
+
}));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Unsubscribe
|
|
54
|
+
*
|
|
55
|
+
* @param channel The channel that will be unsubscribed
|
|
56
|
+
*/
|
|
57
|
+
async unsubscribe(channel) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
const transaction_id = (0, uuid_1.v4)();
|
|
60
|
+
this.channels.delete(channel);
|
|
61
|
+
this.promises.set(transaction_id, {
|
|
62
|
+
on_success: resolve,
|
|
63
|
+
on_error: reject,
|
|
64
|
+
});
|
|
65
|
+
this.ws.send(JSON.stringify({
|
|
66
|
+
transaction_id: transaction_id,
|
|
67
|
+
action: 'unsubscribe',
|
|
68
|
+
params: {
|
|
69
|
+
channel: channel,
|
|
70
|
+
},
|
|
71
|
+
}));
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Publish
|
|
76
|
+
*
|
|
77
|
+
* @param channel The channel that will be used
|
|
78
|
+
* @param payload The payload that will be sent
|
|
79
|
+
*/
|
|
80
|
+
async publish(channel, payload) {
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
const transaction_id = (0, uuid_1.v4)();
|
|
83
|
+
this.promises.set(transaction_id, {
|
|
84
|
+
on_success: resolve,
|
|
85
|
+
on_error: reject,
|
|
86
|
+
});
|
|
87
|
+
this.ws.send(JSON.stringify({
|
|
88
|
+
transaction_id: transaction_id,
|
|
89
|
+
action: 'publish',
|
|
90
|
+
params: {
|
|
91
|
+
channel: channel,
|
|
92
|
+
payload: payload,
|
|
93
|
+
},
|
|
94
|
+
}));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Send
|
|
99
|
+
*
|
|
100
|
+
* @param client_id The client id that will be used
|
|
101
|
+
* @param payload The payload that will be sent
|
|
102
|
+
*/
|
|
103
|
+
async send(client_id, payload) {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const transaction_id = (0, uuid_1.v4)();
|
|
106
|
+
this.promises.set(transaction_id, {
|
|
107
|
+
on_success: resolve,
|
|
108
|
+
on_error: reject,
|
|
109
|
+
});
|
|
110
|
+
this.ws.send(JSON.stringify({
|
|
111
|
+
transaction_id: transaction_id,
|
|
112
|
+
action: 'send',
|
|
113
|
+
params: {
|
|
114
|
+
to_client_id: client_id,
|
|
115
|
+
payload: payload,
|
|
116
|
+
},
|
|
117
|
+
}));
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Broadcast
|
|
122
|
+
*
|
|
123
|
+
* @param payload The payload that will be broadcast
|
|
124
|
+
*/
|
|
125
|
+
async broadcast(payload) {
|
|
126
|
+
return new Promise((resolve, reject) => {
|
|
127
|
+
const transaction_id = (0, uuid_1.v4)();
|
|
128
|
+
this.promises.set(transaction_id, {
|
|
129
|
+
on_success: resolve,
|
|
130
|
+
on_error: reject,
|
|
131
|
+
});
|
|
132
|
+
this.ws.send(JSON.stringify({
|
|
133
|
+
transaction_id: transaction_id,
|
|
134
|
+
action: 'broadcast',
|
|
135
|
+
params: {
|
|
136
|
+
payload: payload,
|
|
137
|
+
},
|
|
138
|
+
}));
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* On
|
|
143
|
+
*
|
|
144
|
+
* @param event The event that will be used
|
|
145
|
+
* @param callback The callback that will be called on event
|
|
146
|
+
*/
|
|
147
|
+
on(event, callback) {
|
|
148
|
+
this.handlers.set(event, callback);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
exports.Client = Client;
|
package/dist/index.js
CHANGED
|
@@ -1 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./sdk"), exports);
|
|
19
|
+
__exportStar(require("./client"), exports);
|
package/dist/sdk.js
CHANGED
|
@@ -1 +1,89 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Sdk = void 0;
|
|
7
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const https_1 = __importDefault(require("https"));
|
|
10
|
+
const client_1 = require("./client");
|
|
11
|
+
/**
|
|
12
|
+
* Sdk
|
|
13
|
+
*/
|
|
14
|
+
class Sdk {
|
|
15
|
+
/**
|
|
16
|
+
* Connect
|
|
17
|
+
*
|
|
18
|
+
* @param url The URL of the server
|
|
19
|
+
*/
|
|
20
|
+
async connect(url) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const ca = fs_1.default.readFileSync('ca.crt');
|
|
23
|
+
const cert = fs_1.default.readFileSync('client.crt');
|
|
24
|
+
const key = fs_1.default.readFileSync('client.key');
|
|
25
|
+
const agent = new https_1.default.Agent({
|
|
26
|
+
ca,
|
|
27
|
+
cert,
|
|
28
|
+
key,
|
|
29
|
+
rejectUnauthorized: true,
|
|
30
|
+
passphrase: 'd96ab300',
|
|
31
|
+
});
|
|
32
|
+
const ws = new isomorphic_ws_1.default(url, {
|
|
33
|
+
agent,
|
|
34
|
+
});
|
|
35
|
+
const client = new client_1.Client(ws);
|
|
36
|
+
client.ws.onerror = reject;
|
|
37
|
+
client.ws.onopen = () => { };
|
|
38
|
+
client.ws.onmessage = (message) => {
|
|
39
|
+
const welcome = JSON.parse(message.data);
|
|
40
|
+
client.id = welcome.data.client_id;
|
|
41
|
+
client.ws.onmessage = (message) => {
|
|
42
|
+
const event = JSON.parse(message.data);
|
|
43
|
+
switch (event.action) {
|
|
44
|
+
case 'ack':
|
|
45
|
+
if (client.promises.has(event.transaction_id)) {
|
|
46
|
+
const transaction = client.promises.get(event.transaction_id);
|
|
47
|
+
if (event.status == 'success') {
|
|
48
|
+
transaction.on_success(event);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
transaction.on_error(event);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (client.handlers.has('ack')) {
|
|
55
|
+
const handler = client.handlers.get('ack');
|
|
56
|
+
handler(event);
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
case 'broadcast':
|
|
60
|
+
if (client.handlers.has('broadcast')) {
|
|
61
|
+
const handler = client.handlers.get('broadcast');
|
|
62
|
+
handler(event);
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
case 'send':
|
|
66
|
+
if (client.handlers.has('send')) {
|
|
67
|
+
const handler = client.handlers.get('send');
|
|
68
|
+
handler(event);
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case 'publish':
|
|
72
|
+
if (client.handlers.has('publish')) {
|
|
73
|
+
const handler = client.handlers.get('publish');
|
|
74
|
+
handler(event);
|
|
75
|
+
}
|
|
76
|
+
const publish = event;
|
|
77
|
+
if (client.channels.has(publish.params.channel)) {
|
|
78
|
+
const handler = client.channels.get(publish.params.channel);
|
|
79
|
+
handler(event);
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
resolve(client);
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.Sdk = Sdk;
|
package/dist/types.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|