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