@stateworks/integration 2.0.0 → 3.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 CHANGED
@@ -1,151 +1 @@
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;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Client=void 0;const uuid_1=require("uuid");class Client{constructor(s){this.promises=new Map,this.channels=new Map,this.handlers=new Map,this.ws=s}async subscribe(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.channels.set(s,n),this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"subscribe",params:{channel:s}}))})}async unsubscribe(s){return new Promise((n,e)=>{const i=(0,uuid_1.v4)();this.channels.delete(s),this.promises.set(i,{on_success:n,on_error:e}),this.ws.send(JSON.stringify({transaction_id:i,action:"unsubscribe",params:{channel:s}}))})}async publish(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"publish",params:{channel:s,payload:n}}))})}async send(s,n){return new Promise((e,i)=>{const t=(0,uuid_1.v4)();this.promises.set(t,{on_success:e,on_error:i}),this.ws.send(JSON.stringify({transaction_id:t,action:"send",params:{to_client_id:s,payload:n}}))})}async broadcast(s){return new Promise((n,e)=>{const i=(0,uuid_1.v4)();this.promises.set(i,{on_success:n,on_error:e}),this.ws.send(JSON.stringify({transaction_id:i,action:"broadcast",params:{payload:s}}))})}on(s,n){this.handlers.set(s,n)}}exports.Client=Client;
package/dist/index.js CHANGED
@@ -1,19 +1 @@
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);
1
+ "use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var o=Object.getOwnPropertyDescriptor(t,r);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,o)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./types"),exports),__exportStar(require("./sdk"),exports),__exportStar(require("./client"),exports);
package/dist/sdk.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Client } from './client';
2
+ import { Configuration } from './types';
2
3
  /**
3
4
  * Sdk
4
5
  */
@@ -7,6 +8,7 @@ export declare class Sdk {
7
8
  * Connect
8
9
  *
9
10
  * @param url The URL of the server
11
+ * @param configuration The Configuration used on client
10
12
  */
11
- connect(url: string): Promise<Client>;
13
+ connect(url: string, configuration?: Configuration): Promise<Client>;
12
14
  }
package/dist/sdk.js CHANGED
@@ -1,89 +1 @@
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;
1
+ "use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Sdk=void 0;const isomorphic_ws_1=__importDefault(require("isomorphic-ws")),fs_1=__importDefault(require("fs")),https_1=__importDefault(require("https")),client_1=require("./client");class Sdk{async connect(e,s={}){return new Promise((a,t)=>{const r=new https_1.default.Agent({ca:fs_1.default.readFileSync(s.ca_file??"ca.crt"),cert:fs_1.default.readFileSync(s.cert_file??"client.crt"),key:fs_1.default.readFileSync(s.key_file??"client.key"),rejectUnauthorized:!0,passphrase:s.passphrase??"d96ab300"}),n=new isomorphic_ws_1.default(e,{agent:r}),c=new client_1.Client(n);c.ws.onerror=t,c.ws.onopen=()=>{},c.ws.onmessage=e=>{const s=JSON.parse(e.data);c.id=s.data.client_id,c.ws.onmessage=e=>{const s=JSON.parse(e.data);switch(s.action){case"ack":if(c.promises.has(s.transaction_id)){const e=c.promises.get(s.transaction_id);"success"==s.status?e.on_success(s):e.on_error(s)}if(c.handlers.has("ack")){c.handlers.get("ack")(s)}break;case"broadcast":if(c.handlers.has("broadcast")){c.handlers.get("broadcast")(s)}break;case"send":if(c.handlers.has("send")){c.handlers.get("send")(s)}break;case"publish":if(c.handlers.has("publish")){c.handlers.get("publish")(s)}const e=s;if(c.channels.has(e.params.channel)){c.channels.get(e.params.channel)(s)}}},a(c)}})}}exports.Sdk=Sdk;
package/dist/types.d.ts CHANGED
@@ -14,6 +14,27 @@ export type Statuses = 'success' | 'failed';
14
14
  * Statuses
15
15
  */
16
16
  export type Messages = 'ok' | 'no effect' | 'unprocessable entity' | 'pong';
17
+ /**
18
+ * Configuration
19
+ */
20
+ export interface Configuration {
21
+ /**
22
+ * CA File
23
+ */
24
+ ca_file?: string;
25
+ /**
26
+ * Cert File
27
+ */
28
+ cert_file?: string;
29
+ /**
30
+ * Key File
31
+ */
32
+ key_file?: string;
33
+ /**
34
+ * Passphrase
35
+ */
36
+ passphrase?: string;
37
+ }
17
38
  /**
18
39
  * Event
19
40
  */
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stateworks/integration",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Ian Torres",
6
6
  "type": "commonjs",