@xelis/sdk 0.11.15 → 0.11.17
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/cjs/address/bech32.js +47 -56
- package/dist/cjs/address/index.js +20 -21
- package/dist/cjs/config.js +26 -38
- package/dist/cjs/contract/contract.js +178 -0
- package/dist/cjs/contract/typed_contract.js +259 -0
- package/dist/cjs/contract/xvm_serializer.js +170 -0
- package/dist/cjs/daemon/rpc.js +157 -168
- package/dist/cjs/daemon/types.js +4 -1
- package/dist/cjs/daemon/websocket.js +170 -181
- package/dist/cjs/data/element.js +39 -41
- package/dist/cjs/data/value.js +106 -111
- package/dist/cjs/react/daemon.js +33 -43
- package/dist/cjs/rpc/http.js +75 -132
- package/dist/cjs/rpc/parse_json/parse_json.js +4 -4
- package/dist/cjs/rpc/types.js +1 -1
- package/dist/cjs/rpc/websocket.js +131 -201
- package/dist/cjs/wallet/rpc.js +98 -117
- package/dist/cjs/wallet/types.js +1 -1
- package/dist/cjs/wallet/websocket.js +105 -126
- package/dist/cjs/xswd/relayer/app.js +57 -36
- package/dist/cjs/xswd/relayer/index.js +25 -27
- package/dist/cjs/xswd/types.js +1 -1
- package/dist/cjs/xswd/websocket.js +15 -33
- package/dist/esm/address/bech32.js +46 -55
- package/dist/esm/address/index.js +16 -17
- package/dist/esm/config.js +25 -37
- package/dist/esm/contract/contract.js +172 -0
- package/dist/esm/contract/typed_contract.js +251 -0
- package/dist/esm/contract/xvm_serializer.js +163 -0
- package/dist/esm/daemon/rpc.js +153 -165
- package/dist/esm/daemon/types.js +3 -0
- package/dist/esm/daemon/websocket.js +166 -179
- package/dist/esm/data/element.js +37 -40
- package/dist/esm/data/value.js +104 -112
- package/dist/esm/react/daemon.js +30 -40
- package/dist/esm/rpc/http.js +73 -131
- package/dist/esm/rpc/parse_json/parse_json.js +1 -1
- package/dist/esm/rpc/websocket.js +126 -197
- package/dist/esm/wallet/rpc.js +93 -113
- package/dist/esm/wallet/websocket.js +101 -124
- package/dist/esm/xswd/relayer/app.js +54 -34
- package/dist/esm/xswd/relayer/index.js +22 -24
- package/dist/esm/xswd/websocket.js +10 -29
- package/dist/types/contract/contract.d.ts +80 -0
- package/dist/types/contract/typed_contract.d.ts +94 -0
- package/dist/types/contract/xvm_serializer.d.ts +69 -0
- package/dist/types/daemon/rpc.d.ts +5 -2
- package/dist/types/daemon/types.d.ts +97 -18
- package/dist/types/daemon/websocket.d.ts +5 -2
- package/package.json +1 -1
package/dist/cjs/rpc/http.js
CHANGED
|
@@ -1,141 +1,84 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
exports.__esModule = true;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
3
|
exports.HttpRPC = void 0;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
4
|
+
const parse_json_1 = require("./parse_json/parse_json");
|
|
5
|
+
class HttpRPC {
|
|
6
|
+
constructor(endpoint) {
|
|
43
7
|
this.endpoint = endpoint;
|
|
44
8
|
this.timeout = 3000;
|
|
45
9
|
this.headers = new Headers();
|
|
46
|
-
this.headers.set(
|
|
10
|
+
this.headers.set(`Content-Type`, `application/json`);
|
|
47
11
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
controller_1 = new AbortController();
|
|
56
|
-
id_1 = 0;
|
|
57
|
-
requests.forEach(function (request) {
|
|
58
|
-
request.id = ++id_1;
|
|
59
|
-
request.jsonrpc = "2.0";
|
|
60
|
-
});
|
|
61
|
-
body = JSON.stringify(requests);
|
|
62
|
-
timeoutId = setTimeout(function () {
|
|
63
|
-
controller_1.abort();
|
|
64
|
-
}, this.timeout);
|
|
65
|
-
return [4 /*yield*/, fetch(this.endpoint, {
|
|
66
|
-
headers: this.headers,
|
|
67
|
-
method: "POST",
|
|
68
|
-
body: body,
|
|
69
|
-
signal: controller_1.signal
|
|
70
|
-
})];
|
|
71
|
-
case 1:
|
|
72
|
-
res = _a.sent();
|
|
73
|
-
clearTimeout(timeoutId);
|
|
74
|
-
if (!res.ok) return [3 /*break*/, 3];
|
|
75
|
-
return [4 /*yield*/, res.text()];
|
|
76
|
-
case 2:
|
|
77
|
-
jsonString = _a.sent();
|
|
78
|
-
items_1 = [];
|
|
79
|
-
data = (0, parse_json_1.parseJSON)(jsonString);
|
|
80
|
-
data.forEach(function (item) {
|
|
81
|
-
if (item.error) {
|
|
82
|
-
items_1.push(new Error(item.error.message));
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
items_1.push(item.result);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
return [2 /*return*/, Promise.resolve(items_1)];
|
|
89
|
-
case 3: return [2 /*return*/, Promise.reject(new Error("".concat(res.status, " - ").concat(res.statusText)))];
|
|
90
|
-
case 4: return [3 /*break*/, 6];
|
|
91
|
-
case 5:
|
|
92
|
-
err_1 = _a.sent();
|
|
93
|
-
return [2 /*return*/, Promise.reject(err_1)];
|
|
94
|
-
case 6: return [2 /*return*/];
|
|
95
|
-
}
|
|
12
|
+
async batchRequest(requests) {
|
|
13
|
+
try {
|
|
14
|
+
const controller = new AbortController();
|
|
15
|
+
let id = 0;
|
|
16
|
+
requests.forEach((request) => {
|
|
17
|
+
request.id = ++id;
|
|
18
|
+
request.jsonrpc = "2.0";
|
|
96
19
|
});
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
20
|
+
const body = JSON.stringify(requests);
|
|
21
|
+
const timeoutId = setTimeout(() => {
|
|
22
|
+
controller.abort();
|
|
23
|
+
}, this.timeout);
|
|
24
|
+
const res = await fetch(this.endpoint, {
|
|
25
|
+
headers: this.headers,
|
|
26
|
+
method: `POST`,
|
|
27
|
+
body,
|
|
28
|
+
signal: controller.signal
|
|
29
|
+
});
|
|
30
|
+
clearTimeout(timeoutId);
|
|
31
|
+
if (res.ok) {
|
|
32
|
+
const jsonString = await res.text();
|
|
33
|
+
let items = [];
|
|
34
|
+
const data = (0, parse_json_1.parseJSON)(jsonString);
|
|
35
|
+
data.forEach((item) => {
|
|
36
|
+
if (item.error) {
|
|
37
|
+
items.push(new Error(item.error.message));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
items.push(item.result);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return Promise.resolve(items);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return Promise.reject(new Error(`${res.status} - ${res.statusText}`));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
return Promise.reject(err);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async request(method, params) {
|
|
54
|
+
try {
|
|
55
|
+
const controller = new AbortController();
|
|
56
|
+
const body = JSON.stringify({ id: 1, jsonrpc: '2.0', method: method, params });
|
|
57
|
+
const timeoutId = setTimeout(() => {
|
|
58
|
+
controller.abort();
|
|
59
|
+
}, this.timeout);
|
|
60
|
+
const res = await fetch(this.endpoint, {
|
|
61
|
+
headers: this.headers,
|
|
62
|
+
method: `POST`,
|
|
63
|
+
body,
|
|
64
|
+
signal: controller.signal
|
|
136
65
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
66
|
+
clearTimeout(timeoutId);
|
|
67
|
+
if (res.ok) {
|
|
68
|
+
const jsonString = await res.text();
|
|
69
|
+
const data = (0, parse_json_1.parseJSON)(jsonString);
|
|
70
|
+
if (data.error) {
|
|
71
|
+
return Promise.reject(new Error(data.error.message));
|
|
72
|
+
}
|
|
73
|
+
return Promise.resolve(data.result);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return Promise.reject(new Error(`${res.status} - ${res.statusText}`));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
return Promise.reject(err);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
141
84
|
exports.HttpRPC = HttpRPC;
|
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
|
-
exports
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.parseJSON = void 0;
|
|
7
|
-
|
|
7
|
+
const json_bigint_1 = __importDefault(require("json-bigint"));
|
|
8
8
|
// do not use JSON.parse() or we loose precision on big numbers
|
|
9
9
|
// for ex: the API returns the nonce as a number instead of a string and JSON.parse() is rounding the number because of overflow
|
|
10
10
|
// instead we will return any BigInt as a string and avoid precision loss
|
|
11
11
|
// NOTE: Previously was using lossless-json with isSafeNumber, but its too slow for big json data
|
|
12
|
-
|
|
13
|
-
return (0, json_bigint_1
|
|
12
|
+
const parseJSON = (data) => {
|
|
13
|
+
return (0, json_bigint_1.default)({ storeAsString: true }).parse(data);
|
|
14
14
|
};
|
|
15
15
|
exports.parseJSON = parseJSON;
|
package/dist/cjs/rpc/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -1,237 +1,167 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
4
|
};
|
|
41
|
-
exports
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
6
|
exports.WSRPC = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
this.socket = new isomorphic_ws_1["default"](endpoint, options);
|
|
7
|
+
const await_to_js_1 = __importDefault(require("await-to-js"));
|
|
8
|
+
const parse_json_1 = require("./parse_json/parse_json");
|
|
9
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
10
|
+
class WSRPC {
|
|
11
|
+
constructor(endpoint, options) {
|
|
12
|
+
this.socket = new isomorphic_ws_1.default(endpoint, options);
|
|
50
13
|
this.methodIdIncrement = 0;
|
|
51
14
|
this.callTimeout = 3000;
|
|
52
15
|
this.events = new Map();
|
|
53
|
-
this.socket.addEventListener(
|
|
54
|
-
|
|
16
|
+
this.socket.addEventListener(`close`, () => {
|
|
17
|
+
this.events = new Map();
|
|
55
18
|
});
|
|
56
19
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
eventData =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
try {
|
|
95
|
-
var data_1 = (0, parse_json_1.parseJSON)(msgEvent.data);
|
|
96
|
-
// event result will contain an event parameter with the event name defined
|
|
97
|
-
if (data_1.result["event"] === event && data_1.id === idRefObject_1.id) {
|
|
98
|
-
eventData.listeners.forEach(function (listener) {
|
|
99
|
-
if (data_1.error) {
|
|
100
|
-
listener(undefined, new Error(data_1.error.message));
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
listener(data_1.result, undefined);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
20
|
+
async closeListener(event, listener) {
|
|
21
|
+
const eventData = this.events.get(event);
|
|
22
|
+
if (eventData) {
|
|
23
|
+
if (eventData.listeners.length > 1) {
|
|
24
|
+
eventData.listeners = eventData.listeners.filter(l => l !== listener);
|
|
25
|
+
this.events.set(event, eventData);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.events.delete(event);
|
|
29
|
+
this.socket.removeEventListener(`message`, eventData.onMessage);
|
|
30
|
+
this.dataCall(`unsubscribe`, { notify: event });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async listen(event, listener) {
|
|
35
|
+
const listenEvent = () => {
|
|
36
|
+
const eventData = this.events.get(event);
|
|
37
|
+
if (eventData) {
|
|
38
|
+
eventData.listeners.push(listener);
|
|
39
|
+
this.events.set(event, eventData);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
let idRefObject = {};
|
|
43
|
+
this.dataCall(`subscribe`, { notify: event }, idRefObject);
|
|
44
|
+
const onMessage = (msgEvent) => {
|
|
45
|
+
const eventData = this.events.get(event);
|
|
46
|
+
if (eventData && typeof msgEvent.data === `string`) {
|
|
47
|
+
try {
|
|
48
|
+
const data = (0, parse_json_1.parseJSON)(msgEvent.data);
|
|
49
|
+
// event result will contain an event parameter with the event name defined
|
|
50
|
+
if (data.result["event"] === event && data.id === idRefObject.id) {
|
|
51
|
+
eventData.listeners.forEach((listener) => {
|
|
52
|
+
if (data.error) {
|
|
53
|
+
listener(undefined, new Error(data.error.message));
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
listener(data.result, undefined);
|
|
106
57
|
}
|
|
107
|
-
}
|
|
108
|
-
catch (_a) {
|
|
109
|
-
// can't parse json -- do nothing
|
|
110
|
-
}
|
|
58
|
+
});
|
|
111
59
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// can't parse json -- do nothing
|
|
63
|
+
}
|
|
115
64
|
}
|
|
116
65
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return new Promise(
|
|
135
|
-
if (
|
|
136
|
-
return reject(new Error(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (typeof msgEvent.data ===
|
|
140
|
-
|
|
141
|
-
|
|
66
|
+
this.socket.addEventListener(`message`, onMessage);
|
|
67
|
+
this.events.set(event, { onMessage, listeners: [listener] });
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
// make sure connection is open or wait
|
|
71
|
+
if (this.socket.readyState === isomorphic_ws_1.default.OPEN) {
|
|
72
|
+
listenEvent();
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const wait_for_open = () => {
|
|
76
|
+
listenEvent();
|
|
77
|
+
this.socket.removeEventListener(`open`, wait_for_open);
|
|
78
|
+
};
|
|
79
|
+
this.socket.addEventListener(`open`, wait_for_open);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
rawCall(id, body) {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
if (this.socket.readyState !== isomorphic_ws_1.default.OPEN)
|
|
85
|
+
return reject(new Error(`Can't send msg. Socket is not opened.`));
|
|
86
|
+
let timeoutId = null;
|
|
87
|
+
const onMessage = (msgEvent) => {
|
|
88
|
+
if (typeof msgEvent.data === `string`) {
|
|
89
|
+
const data = (0, parse_json_1.parseJSON)(msgEvent.data);
|
|
90
|
+
const evaluate_data = () => {
|
|
142
91
|
clearTimeout(timeoutId);
|
|
143
|
-
|
|
144
|
-
if (
|
|
145
|
-
reject(new Error(
|
|
92
|
+
this.socket.removeEventListener(`message`, onMessage);
|
|
93
|
+
if (data.error && data.error.message) {
|
|
94
|
+
reject(new Error(data.error.message));
|
|
146
95
|
return;
|
|
147
96
|
}
|
|
148
|
-
return resolve(
|
|
97
|
+
return resolve(data);
|
|
149
98
|
};
|
|
150
99
|
// this is for batch call we match the id with first item id
|
|
151
|
-
if (Array.isArray(
|
|
100
|
+
if (Array.isArray(data) && data.length > 0 && data[0].id === id) {
|
|
152
101
|
return evaluate_data();
|
|
153
102
|
}
|
|
154
103
|
// the msg id is matching so we can evaluate the data
|
|
155
|
-
if (
|
|
104
|
+
if (data.id === id) {
|
|
156
105
|
return evaluate_data();
|
|
157
106
|
}
|
|
158
107
|
// special XSWD case - sending first call will return null id
|
|
159
|
-
if (
|
|
108
|
+
if (data.id === null && id === 0) {
|
|
160
109
|
return evaluate_data();
|
|
161
110
|
}
|
|
162
111
|
}
|
|
163
112
|
};
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
timeoutId = setTimeout(
|
|
167
|
-
|
|
168
|
-
reject(new Error(
|
|
169
|
-
},
|
|
113
|
+
this.socket.addEventListener(`message`, onMessage);
|
|
114
|
+
if (this.callTimeout > 0) {
|
|
115
|
+
timeoutId = setTimeout(() => {
|
|
116
|
+
this.socket && this.socket.removeEventListener(`message`, onMessage);
|
|
117
|
+
reject(new Error(`timeout`));
|
|
118
|
+
}, this.callTimeout);
|
|
170
119
|
}
|
|
171
|
-
if (
|
|
172
|
-
|
|
120
|
+
if (this.socket.readyState === isomorphic_ws_1.default.OPEN) {
|
|
121
|
+
this.socket.send(body);
|
|
173
122
|
}
|
|
174
123
|
});
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
124
|
+
}
|
|
125
|
+
dataCall(method, params, idRefObj) {
|
|
126
|
+
return new Promise(async (resolve, reject) => {
|
|
127
|
+
const id = this.methodIdIncrement++;
|
|
128
|
+
if (idRefObj)
|
|
129
|
+
idRefObj.id = id;
|
|
130
|
+
const request = { id, jsonrpc: `2.0`, method };
|
|
131
|
+
if (params)
|
|
132
|
+
request.params = params;
|
|
133
|
+
const data = JSON.stringify(request);
|
|
134
|
+
const [err, res] = await (0, await_to_js_1.default)(this.rawCall(id, data));
|
|
135
|
+
if (err)
|
|
136
|
+
return reject(err);
|
|
137
|
+
if (res.error) {
|
|
138
|
+
return reject(res.error.message);
|
|
139
|
+
}
|
|
140
|
+
return resolve(res.result);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
batchCall(requests) {
|
|
144
|
+
return new Promise(async (resolve, reject) => {
|
|
145
|
+
let id = this.methodIdIncrement++;
|
|
146
|
+
requests.forEach((request) => {
|
|
147
|
+
request.id = id;
|
|
148
|
+
request.jsonrpc = "2.0";
|
|
200
149
|
});
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
request.id = id;
|
|
213
|
-
request.jsonrpc = "2.0";
|
|
214
|
-
});
|
|
215
|
-
data = JSON.stringify(requests);
|
|
216
|
-
return [4 /*yield*/, (0, await_to_js_1["default"])(this.rawCall(id, data))];
|
|
217
|
-
case 1:
|
|
218
|
-
_a = _b.sent(), err = _a[0], res = _a[1];
|
|
219
|
-
if (err)
|
|
220
|
-
return [2 /*return*/, reject(err)];
|
|
221
|
-
items = [];
|
|
222
|
-
res.forEach(function (v) {
|
|
223
|
-
if (v.error) {
|
|
224
|
-
items.push(new Error(v.error.message));
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
items.push(v.result);
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
return [2 /*return*/, resolve(items)];
|
|
150
|
+
const data = JSON.stringify(requests);
|
|
151
|
+
const [err, res] = await (0, await_to_js_1.default)(this.rawCall(id, data));
|
|
152
|
+
if (err)
|
|
153
|
+
return reject(err);
|
|
154
|
+
let items = [];
|
|
155
|
+
res.forEach((v) => {
|
|
156
|
+
if (v.error) {
|
|
157
|
+
items.push(new Error(v.error.message));
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
items.push(v.result);
|
|
231
161
|
}
|
|
232
162
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
163
|
+
return resolve(items);
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
237
167
|
exports.WSRPC = WSRPC;
|