@pezkuwi/rpc-provider 16.5.5 → 16.5.8
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/LICENSE +201 -0
- package/README.md +10 -10
- package/bizinikiwi-connect/Health.js +259 -0
- package/{build/substrate-connect → bizinikiwi-connect}/index.d.ts +3 -3
- package/bizinikiwi-connect/index.js +319 -0
- package/bizinikiwi-connect/types.js +1 -0
- package/build/LICENSE +201 -0
- package/build/README.md +68 -0
- package/build/bizinikiwi-connect/Health.d.ts +7 -0
- package/build/bizinikiwi-connect/Health.js +259 -0
- package/build/bizinikiwi-connect/index.d.ts +22 -0
- package/build/bizinikiwi-connect/index.js +319 -0
- package/build/bizinikiwi-connect/types.d.ts +12 -0
- package/build/bizinikiwi-connect/types.js +1 -0
- package/build/bundle.d.ts +1 -1
- package/build/bundle.js +5 -0
- package/build/cjs/bizinikiwi-connect/Health.d.ts +7 -0
- package/build/cjs/bizinikiwi-connect/Health.js +264 -0
- package/build/cjs/bizinikiwi-connect/index.d.ts +22 -0
- package/build/cjs/bizinikiwi-connect/index.js +323 -0
- package/build/cjs/bizinikiwi-connect/types.d.ts +12 -0
- package/build/cjs/bizinikiwi-connect/types.js +2 -0
- package/build/cjs/bundle.d.ts +5 -0
- package/build/cjs/bundle.js +14 -0
- package/build/cjs/coder/error.d.ts +29 -0
- package/build/cjs/coder/error.js +53 -0
- package/build/cjs/coder/index.d.ts +8 -0
- package/build/cjs/coder/index.js +63 -0
- package/build/cjs/defaults.d.ts +5 -0
- package/build/cjs/defaults.js +8 -0
- package/build/cjs/http/index.d.ts +81 -0
- package/build/cjs/http/index.js +196 -0
- package/build/cjs/http/types.d.ts +7 -0
- package/build/cjs/http/types.js +2 -0
- package/build/cjs/index.d.ts +2 -0
- package/build/cjs/index.js +5 -0
- package/build/cjs/lru.d.ts +15 -0
- package/build/cjs/lru.js +150 -0
- package/build/cjs/mock/index.d.ts +35 -0
- package/build/cjs/mock/index.js +196 -0
- package/build/cjs/mock/mockHttp.d.ts +9 -0
- package/build/cjs/mock/mockHttp.js +17 -0
- package/build/cjs/mock/mockWs.d.ts +26 -0
- package/build/cjs/mock/mockWs.js +47 -0
- package/build/cjs/mock/types.d.ts +23 -0
- package/build/cjs/mock/types.js +2 -0
- package/build/cjs/package.json +3 -0
- package/build/cjs/packageDetect.d.ts +1 -0
- package/build/cjs/packageDetect.js +6 -0
- package/build/cjs/packageInfo.d.ts +6 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/types.d.ts +85 -0
- package/build/cjs/types.js +2 -0
- package/build/cjs/ws/errors.d.ts +1 -0
- package/build/cjs/ws/errors.js +41 -0
- package/build/cjs/ws/index.d.ts +121 -0
- package/build/cjs/ws/index.js +529 -0
- package/build/coder/error.js +50 -0
- package/build/coder/index.js +58 -0
- package/build/defaults.js +6 -0
- package/build/http/index.d.ts +1 -1
- package/build/http/index.js +191 -0
- package/build/http/types.js +1 -0
- package/build/index.js +2 -0
- package/build/lru.js +146 -0
- package/build/mock/index.js +191 -0
- package/build/mock/mockHttp.js +12 -0
- package/build/mock/mockWs.js +43 -0
- package/build/mock/types.js +1 -0
- package/build/package.json +344 -0
- package/build/packageDetect.js +4 -0
- package/build/packageInfo.js +1 -0
- package/build/types.js +1 -0
- package/build/ws/errors.js +38 -0
- package/build/ws/index.d.ts +1 -1
- package/build/ws/index.js +524 -0
- package/build-deno/README.md +66 -0
- package/build-deno/bizinikiwi-connect/Health.ts +323 -0
- package/build-deno/bizinikiwi-connect/index.ts +417 -0
- package/build-deno/bizinikiwi-connect/types.ts +14 -0
- package/build-deno/bundle.ts +6 -0
- package/build-deno/coder/error.ts +64 -0
- package/build-deno/coder/index.ts +86 -0
- package/build-deno/defaults.ts +8 -0
- package/build-deno/http/index.ts +236 -0
- package/build-deno/http/types.ts +9 -0
- package/build-deno/index.ts +4 -0
- package/build-deno/lru.ts +189 -0
- package/build-deno/mock/index.ts +257 -0
- package/build-deno/mock/mockHttp.ts +33 -0
- package/build-deno/mock/mockWs.ts +87 -0
- package/build-deno/mock/types.ts +34 -0
- package/build-deno/mod.ts +2 -0
- package/build-deno/packageDetect.ts +8 -0
- package/build-deno/packageInfo.ts +3 -0
- package/build-deno/types.ts +99 -0
- package/build-deno/ws/errors.ts +38 -0
- package/build-deno/ws/index.ts +650 -0
- package/build-tsc/bizinikiwi-connect/Health.d.ts +7 -0
- package/build-tsc/bizinikiwi-connect/index.d.ts +22 -0
- package/build-tsc/bizinikiwi-connect/types.d.ts +12 -0
- package/build-tsc/bundle.d.ts +5 -0
- package/build-tsc/coder/error.d.ts +29 -0
- package/build-tsc/coder/index.d.ts +8 -0
- package/build-tsc/defaults.d.ts +5 -0
- package/build-tsc/http/index.d.ts +81 -0
- package/build-tsc/http/types.d.ts +7 -0
- package/build-tsc/index.d.ts +2 -0
- package/build-tsc/lru.d.ts +15 -0
- package/build-tsc/mock/index.d.ts +35 -0
- package/build-tsc/mock/mockHttp.d.ts +9 -0
- package/build-tsc/mock/mockWs.d.ts +26 -0
- package/build-tsc/mock/types.d.ts +23 -0
- package/build-tsc/packageDetect.d.ts +1 -0
- package/build-tsc/packageInfo.d.ts +6 -0
- package/build-tsc/types.d.ts +85 -0
- package/build-tsc/ws/errors.d.ts +1 -0
- package/build-tsc/ws/index.d.ts +121 -0
- package/build-tsc-cjs/bizinikiwi-connect/Health.js +264 -0
- package/build-tsc-cjs/bizinikiwi-connect/index.js +323 -0
- package/build-tsc-cjs/bizinikiwi-connect/types.js +2 -0
- package/build-tsc-cjs/bundle.js +14 -0
- package/build-tsc-cjs/coder/error.js +53 -0
- package/build-tsc-cjs/coder/index.js +63 -0
- package/build-tsc-cjs/defaults.js +8 -0
- package/build-tsc-cjs/http/index.js +196 -0
- package/build-tsc-cjs/http/types.js +2 -0
- package/build-tsc-cjs/index.js +5 -0
- package/build-tsc-cjs/lru.js +150 -0
- package/build-tsc-cjs/mock/index.js +196 -0
- package/build-tsc-cjs/mock/mockHttp.js +17 -0
- package/build-tsc-cjs/mock/mockWs.js +47 -0
- package/build-tsc-cjs/mock/types.js +2 -0
- package/build-tsc-cjs/packageDetect.js +6 -0
- package/build-tsc-cjs/packageInfo.js +4 -0
- package/build-tsc-cjs/types.js +2 -0
- package/build-tsc-cjs/ws/errors.js +41 -0
- package/build-tsc-cjs/ws/index.js +529 -0
- package/build-tsc-esm/bizinikiwi-connect/Health.js +259 -0
- package/build-tsc-esm/bizinikiwi-connect/index.js +319 -0
- package/build-tsc-esm/bizinikiwi-connect/types.js +1 -0
- package/build-tsc-esm/bundle.js +5 -0
- package/build-tsc-esm/coder/error.js +50 -0
- package/build-tsc-esm/coder/index.js +58 -0
- package/build-tsc-esm/defaults.js +6 -0
- package/build-tsc-esm/http/index.js +191 -0
- package/build-tsc-esm/http/types.js +1 -0
- package/build-tsc-esm/index.js +2 -0
- package/build-tsc-esm/lru.js +146 -0
- package/build-tsc-esm/mock/index.js +191 -0
- package/build-tsc-esm/mock/mockHttp.js +12 -0
- package/build-tsc-esm/mock/mockWs.js +43 -0
- package/build-tsc-esm/mock/types.js +1 -0
- package/build-tsc-esm/packageDetect.js +4 -0
- package/build-tsc-esm/packageInfo.js +1 -0
- package/build-tsc-esm/types.js +1 -0
- package/build-tsc-esm/ws/errors.js +38 -0
- package/build-tsc-esm/ws/index.js +524 -0
- package/bundle.d.ts +5 -0
- package/bundle.js +5 -0
- package/cjs/bizinikiwi-connect/Health.d.ts +7 -0
- package/cjs/bizinikiwi-connect/Health.js +264 -0
- package/cjs/bizinikiwi-connect/index.d.ts +22 -0
- package/cjs/bizinikiwi-connect/index.js +323 -0
- package/cjs/bizinikiwi-connect/types.d.ts +12 -0
- package/cjs/bizinikiwi-connect/types.js +2 -0
- package/cjs/bundle.d.ts +5 -0
- package/cjs/bundle.js +14 -0
- package/cjs/coder/error.d.ts +29 -0
- package/cjs/coder/error.js +53 -0
- package/cjs/coder/index.d.ts +8 -0
- package/cjs/coder/index.js +63 -0
- package/cjs/defaults.d.ts +5 -0
- package/cjs/defaults.js +8 -0
- package/cjs/http/index.d.ts +81 -0
- package/cjs/http/index.js +196 -0
- package/cjs/http/types.d.ts +7 -0
- package/cjs/http/types.js +2 -0
- package/cjs/index.d.ts +2 -0
- package/cjs/index.js +5 -0
- package/cjs/lru.d.ts +15 -0
- package/cjs/lru.js +150 -0
- package/cjs/mock/index.d.ts +35 -0
- package/cjs/mock/index.js +196 -0
- package/cjs/mock/mockHttp.d.ts +9 -0
- package/cjs/mock/mockHttp.js +17 -0
- package/cjs/mock/mockWs.d.ts +26 -0
- package/cjs/mock/mockWs.js +47 -0
- package/cjs/mock/types.d.ts +23 -0
- package/cjs/mock/types.js +2 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.d.ts +1 -0
- package/cjs/packageDetect.js +6 -0
- package/cjs/packageInfo.d.ts +6 -0
- package/cjs/packageInfo.js +4 -0
- package/cjs/types.d.ts +85 -0
- package/cjs/types.js +2 -0
- package/cjs/ws/errors.d.ts +1 -0
- package/cjs/ws/errors.js +41 -0
- package/cjs/ws/index.d.ts +121 -0
- package/cjs/ws/index.js +529 -0
- package/coder/error.d.ts +29 -0
- package/coder/error.js +50 -0
- package/coder/index.d.ts +8 -0
- package/coder/index.js +58 -0
- package/defaults.d.ts +5 -0
- package/defaults.js +6 -0
- package/http/index.d.ts +81 -0
- package/http/index.js +191 -0
- package/http/types.d.ts +7 -0
- package/http/types.js +1 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/lru.d.ts +15 -0
- package/lru.js +146 -0
- package/mock/index.d.ts +35 -0
- package/mock/index.js +191 -0
- package/mock/mockHttp.d.ts +9 -0
- package/mock/mockHttp.js +12 -0
- package/mock/mockWs.d.ts +26 -0
- package/mock/mockWs.js +43 -0
- package/mock/types.d.ts +23 -0
- package/mock/types.js +1 -0
- package/package.json +316 -15
- package/packageDetect.d.ts +1 -0
- package/packageDetect.js +4 -0
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/src/{substrate-connect → bizinikiwi-connect}/Health.ts +1 -1
- package/src/{substrate-connect → bizinikiwi-connect}/index.spec.ts +55 -22
- package/src/{substrate-connect → bizinikiwi-connect}/index.ts +27 -15
- package/src/{substrate-connect → bizinikiwi-connect}/types.ts +1 -1
- package/src/bundle.ts +2 -2
- package/src/coder/decodeResponse.spec.ts +1 -1
- package/src/coder/encodeJson.spec.ts +1 -1
- package/src/coder/encodeObject.spec.ts +1 -1
- package/src/coder/error.spec.ts +1 -1
- package/src/coder/error.ts +1 -1
- package/src/coder/index.ts +1 -1
- package/src/defaults.ts +1 -1
- package/src/http/index.spec.ts +1 -1
- package/src/http/index.ts +2 -2
- package/src/http/send.spec.ts +1 -1
- package/src/http/types.ts +1 -1
- package/src/index.ts +1 -1
- package/src/lru.spec.ts +1 -1
- package/src/lru.ts +2 -2
- package/src/mock/index.ts +2 -2
- package/src/mock/mockHttp.ts +1 -1
- package/src/mock/mockWs.ts +1 -1
- package/src/mock/on.spec.ts +1 -1
- package/src/mock/send.spec.ts +1 -1
- package/src/mock/subscribe.spec.ts +1 -1
- package/src/mock/types.ts +1 -1
- package/src/mock/unsubscribe.spec.ts +1 -1
- package/src/mod.ts +1 -1
- package/src/packageDetect.ts +2 -2
- package/src/packageInfo.ts +3 -3
- package/src/types.ts +1 -1
- package/src/ws/connect.spec.ts +1 -1
- package/src/ws/errors.ts +1 -1
- package/src/ws/index.spec.ts +1 -1
- package/src/ws/index.ts +2 -2
- package/src/ws/send.spec.ts +1 -1
- package/src/ws/state.spec.ts +1 -1
- package/src/ws/subscribe.spec.ts +1 -1
- package/src/ws/unsubscribe.spec.ts +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.spec.tsbuildinfo +1 -0
- package/types.d.ts +85 -0
- package/types.js +1 -0
- package/ws/errors.d.ts +1 -0
- package/ws/errors.js +38 -0
- package/ws/index.d.ts +121 -0
- package/ws/index.js +524 -0
- /package/{build/substrate-connect → bizinikiwi-connect}/Health.d.ts +0 -0
- /package/{build/substrate-connect → bizinikiwi-connect}/types.d.ts +0 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const util_1 = require("@pezkuwi/util");
|
|
4
|
+
const UNKNOWN = -99999;
|
|
5
|
+
function extend(that, name, value) {
|
|
6
|
+
Object.defineProperty(that, name, {
|
|
7
|
+
configurable: true,
|
|
8
|
+
enumerable: false,
|
|
9
|
+
value
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @name RpcError
|
|
14
|
+
* @summary Extension to the basic JS Error.
|
|
15
|
+
* @description
|
|
16
|
+
* The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (any types) parameters are available on the object.
|
|
17
|
+
* @example
|
|
18
|
+
* <BR>
|
|
19
|
+
*
|
|
20
|
+
* ```javascript
|
|
21
|
+
* const { RpcError } from '@pezkuwi/util');
|
|
22
|
+
*
|
|
23
|
+
* throw new RpcError('some message', RpcError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class RpcError extends Error {
|
|
27
|
+
code;
|
|
28
|
+
data;
|
|
29
|
+
message;
|
|
30
|
+
name;
|
|
31
|
+
stack;
|
|
32
|
+
constructor(message = '', code = UNKNOWN, data) {
|
|
33
|
+
super();
|
|
34
|
+
extend(this, 'message', String(message));
|
|
35
|
+
extend(this, 'name', this.constructor.name);
|
|
36
|
+
extend(this, 'data', data);
|
|
37
|
+
extend(this, 'code', code);
|
|
38
|
+
if ((0, util_1.isFunction)(Error.captureStackTrace)) {
|
|
39
|
+
Error.captureStackTrace(this, this.constructor);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const { stack } = new Error(message);
|
|
43
|
+
stack && extend(this, 'stack', stack);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
static CODES = {
|
|
47
|
+
ASSERT: -90009,
|
|
48
|
+
INVALID_JSONRPC: -99998,
|
|
49
|
+
METHOD_NOT_FOUND: -32601, // Rust client
|
|
50
|
+
UNKNOWN
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.default = RpcError;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { JsonRpcRequest, JsonRpcResponse } from '../types.js';
|
|
2
|
+
/** @internal */
|
|
3
|
+
export declare class RpcCoder {
|
|
4
|
+
#private;
|
|
5
|
+
decodeResponse<T>(response?: JsonRpcResponse<T>): T;
|
|
6
|
+
encodeJson(method: string, params: unknown[]): [number, string];
|
|
7
|
+
encodeObject(method: string, params: unknown[]): [number, JsonRpcRequest];
|
|
8
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RpcCoder = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const util_1 = require("@pezkuwi/util");
|
|
6
|
+
const error_js_1 = tslib_1.__importDefault(require("./error.js"));
|
|
7
|
+
function formatErrorData(data) {
|
|
8
|
+
if ((0, util_1.isUndefined)(data)) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
const formatted = `: ${(0, util_1.isString)(data)
|
|
12
|
+
? data.replace(/Error\("/g, '').replace(/\("/g, '(').replace(/"\)/g, ')').replace(/\(/g, ', ').replace(/\)/g, '')
|
|
13
|
+
: (0, util_1.stringify)(data)}`;
|
|
14
|
+
// We need some sort of cut-off here since these can be very large and
|
|
15
|
+
// very nested, pick a number and trim the result display to it
|
|
16
|
+
return formatted.length <= 256
|
|
17
|
+
? formatted
|
|
18
|
+
: `${formatted.substring(0, 255)}…`;
|
|
19
|
+
}
|
|
20
|
+
function checkError(error) {
|
|
21
|
+
if (error) {
|
|
22
|
+
const { code, data, message } = error;
|
|
23
|
+
throw new error_js_1.default(`${code}: ${message}${formatErrorData(data)}`, code, data);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** @internal */
|
|
27
|
+
class RpcCoder {
|
|
28
|
+
#id = 0;
|
|
29
|
+
decodeResponse(response) {
|
|
30
|
+
if (!response || response.jsonrpc !== '2.0') {
|
|
31
|
+
throw new Error('Invalid jsonrpc field in decoded object');
|
|
32
|
+
}
|
|
33
|
+
const isSubscription = !(0, util_1.isUndefined)(response.params) && !(0, util_1.isUndefined)(response.method);
|
|
34
|
+
if (!(0, util_1.isNumber)(response.id) &&
|
|
35
|
+
(!isSubscription || (!(0, util_1.isNumber)(response.params.subscription) &&
|
|
36
|
+
!(0, util_1.isString)(response.params.subscription)))) {
|
|
37
|
+
throw new Error('Invalid id field in decoded object');
|
|
38
|
+
}
|
|
39
|
+
checkError(response.error);
|
|
40
|
+
if (response.result === undefined && !isSubscription) {
|
|
41
|
+
throw new Error('No result found in jsonrpc response');
|
|
42
|
+
}
|
|
43
|
+
if (isSubscription) {
|
|
44
|
+
checkError(response.params.error);
|
|
45
|
+
return response.params.result;
|
|
46
|
+
}
|
|
47
|
+
return response.result;
|
|
48
|
+
}
|
|
49
|
+
encodeJson(method, params) {
|
|
50
|
+
const [id, data] = this.encodeObject(method, params);
|
|
51
|
+
return [id, (0, util_1.stringify)(data)];
|
|
52
|
+
}
|
|
53
|
+
encodeObject(method, params) {
|
|
54
|
+
const id = ++this.#id;
|
|
55
|
+
return [id, {
|
|
56
|
+
id,
|
|
57
|
+
jsonrpc: '2.0',
|
|
58
|
+
method,
|
|
59
|
+
params
|
|
60
|
+
}];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.RpcCoder = RpcCoder;
|
package/cjs/defaults.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted, ProviderStats } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* # @pezkuwi/rpc-provider
|
|
4
|
+
*
|
|
5
|
+
* @name HttpProvider
|
|
6
|
+
*
|
|
7
|
+
* @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* <BR>
|
|
11
|
+
*
|
|
12
|
+
* ```javascript
|
|
13
|
+
* import Api from '@pezkuwi/api/promise';
|
|
14
|
+
* import { HttpProvider } from '@pezkuwi/rpc-provider';
|
|
15
|
+
*
|
|
16
|
+
* const provider = new HttpProvider('http://127.0.0.1:9933');
|
|
17
|
+
* const api = new Api(provider);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @see [[WsProvider]]
|
|
21
|
+
*/
|
|
22
|
+
export declare class HttpProvider implements ProviderInterface {
|
|
23
|
+
#private;
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} endpoint The endpoint url starting with http://
|
|
26
|
+
* @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
|
|
27
|
+
* @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
|
|
28
|
+
* @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
|
|
29
|
+
*/
|
|
30
|
+
constructor(endpoint?: string, headers?: Record<string, string>, cacheCapacity?: number, cacheTtl?: number | null);
|
|
31
|
+
/**
|
|
32
|
+
* @summary `true` when this provider supports subscriptions
|
|
33
|
+
*/
|
|
34
|
+
get hasSubscriptions(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @description Returns a clone of the object
|
|
37
|
+
*/
|
|
38
|
+
clone(): HttpProvider;
|
|
39
|
+
/**
|
|
40
|
+
* @description Manually connect from the connection
|
|
41
|
+
*/
|
|
42
|
+
connect(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* @description Manually disconnect from the connection
|
|
45
|
+
*/
|
|
46
|
+
disconnect(): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* @description Returns the connection stats
|
|
49
|
+
*/
|
|
50
|
+
get stats(): ProviderStats;
|
|
51
|
+
/**
|
|
52
|
+
* @description Returns the connection stats
|
|
53
|
+
*/
|
|
54
|
+
get ttl(): number | null | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* @summary `true` when this provider supports clone()
|
|
57
|
+
*/
|
|
58
|
+
get isClonable(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* @summary Whether the node is connected or not.
|
|
61
|
+
* @return {boolean} true if connected
|
|
62
|
+
*/
|
|
63
|
+
get isConnected(): boolean;
|
|
64
|
+
/**
|
|
65
|
+
* @summary Events are not supported with the HttpProvider, see [[WsProvider]].
|
|
66
|
+
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
|
|
67
|
+
*/
|
|
68
|
+
on(_type: ProviderInterfaceEmitted, _sub: ProviderInterfaceEmitCb): () => void;
|
|
69
|
+
/**
|
|
70
|
+
* @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
|
|
71
|
+
*/
|
|
72
|
+
send<T>(method: string, params: unknown[], isCacheable?: boolean): Promise<T>;
|
|
73
|
+
/**
|
|
74
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
75
|
+
*/
|
|
76
|
+
subscribe(_types: string, _method: string, _params: unknown[], _cb: ProviderInterfaceCallback): Promise<number>;
|
|
77
|
+
/**
|
|
78
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
79
|
+
*/
|
|
80
|
+
unsubscribe(_type: string, _method: string, _id: number): Promise<boolean>;
|
|
81
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpProvider = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const util_1 = require("@pezkuwi/util");
|
|
6
|
+
const x_fetch_1 = require("@pezkuwi/x-fetch");
|
|
7
|
+
const index_js_1 = require("../coder/index.js");
|
|
8
|
+
const defaults_js_1 = tslib_1.__importDefault(require("../defaults.js"));
|
|
9
|
+
const lru_js_1 = require("../lru.js");
|
|
10
|
+
const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
|
|
11
|
+
const l = (0, util_1.logger)('api-http');
|
|
12
|
+
/**
|
|
13
|
+
* # @pezkuwi/rpc-provider
|
|
14
|
+
*
|
|
15
|
+
* @name HttpProvider
|
|
16
|
+
*
|
|
17
|
+
* @description The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. It does not support subscriptions so you won't be able to listen to events such as new blocks or balance changes. It is usually preferable using the [[WsProvider]].
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* <BR>
|
|
21
|
+
*
|
|
22
|
+
* ```javascript
|
|
23
|
+
* import Api from '@pezkuwi/api/promise';
|
|
24
|
+
* import { HttpProvider } from '@pezkuwi/rpc-provider';
|
|
25
|
+
*
|
|
26
|
+
* const provider = new HttpProvider('http://127.0.0.1:9933');
|
|
27
|
+
* const api = new Api(provider);
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @see [[WsProvider]]
|
|
31
|
+
*/
|
|
32
|
+
class HttpProvider {
|
|
33
|
+
#callCache;
|
|
34
|
+
#cacheCapacity;
|
|
35
|
+
#coder;
|
|
36
|
+
#endpoint;
|
|
37
|
+
#headers;
|
|
38
|
+
#stats;
|
|
39
|
+
#ttl;
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} endpoint The endpoint url starting with http://
|
|
42
|
+
* @param {Record<string, string>} headers The headers provided to the underlying Http Endpoint
|
|
43
|
+
* @param {number} [cacheCapacity] Custom size of the HttpProvider LRUCache. Defaults to `DEFAULT_CAPACITY` (1024)
|
|
44
|
+
* @param {number} [cacheTtl] Custom TTL of the HttpProvider LRUCache. Determines how long an object can live in the cache. Defaults to `DEFAULT_TTL` (30000)
|
|
45
|
+
*/
|
|
46
|
+
constructor(endpoint = defaults_js_1.default.HTTP_URL, headers = {}, cacheCapacity, cacheTtl) {
|
|
47
|
+
if (!/^(https|http):\/\//.test(endpoint)) {
|
|
48
|
+
throw new Error(`Endpoint should start with 'http://' or 'https://', received '${endpoint}'`);
|
|
49
|
+
}
|
|
50
|
+
this.#coder = new index_js_1.RpcCoder();
|
|
51
|
+
this.#endpoint = endpoint;
|
|
52
|
+
this.#headers = headers;
|
|
53
|
+
this.#cacheCapacity = cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY;
|
|
54
|
+
const ttl = cacheTtl === undefined ? lru_js_1.DEFAULT_TTL : cacheTtl;
|
|
55
|
+
this.#callCache = new lru_js_1.LRUCache(cacheCapacity === 0 ? 0 : cacheCapacity || lru_js_1.DEFAULT_CAPACITY, ttl);
|
|
56
|
+
this.#ttl = cacheTtl;
|
|
57
|
+
this.#stats = {
|
|
58
|
+
active: { requests: 0, subscriptions: 0 },
|
|
59
|
+
total: { bytesRecv: 0, bytesSent: 0, cached: 0, errors: 0, requests: 0, subscriptions: 0, timeout: 0 }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @summary `true` when this provider supports subscriptions
|
|
64
|
+
*/
|
|
65
|
+
get hasSubscriptions() {
|
|
66
|
+
return !!false;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @description Returns a clone of the object
|
|
70
|
+
*/
|
|
71
|
+
clone() {
|
|
72
|
+
return new HttpProvider(this.#endpoint, this.#headers);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @description Manually connect from the connection
|
|
76
|
+
*/
|
|
77
|
+
async connect() {
|
|
78
|
+
// noop
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @description Manually disconnect from the connection
|
|
82
|
+
*/
|
|
83
|
+
async disconnect() {
|
|
84
|
+
// noop
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @description Returns the connection stats
|
|
88
|
+
*/
|
|
89
|
+
get stats() {
|
|
90
|
+
return this.#stats;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @description Returns the connection stats
|
|
94
|
+
*/
|
|
95
|
+
get ttl() {
|
|
96
|
+
return this.#ttl;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @summary `true` when this provider supports clone()
|
|
100
|
+
*/
|
|
101
|
+
get isClonable() {
|
|
102
|
+
return !!true;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @summary Whether the node is connected or not.
|
|
106
|
+
* @return {boolean} true if connected
|
|
107
|
+
*/
|
|
108
|
+
get isConnected() {
|
|
109
|
+
return !!true;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @summary Events are not supported with the HttpProvider, see [[WsProvider]].
|
|
113
|
+
* @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead.
|
|
114
|
+
*/
|
|
115
|
+
on(_type, _sub) {
|
|
116
|
+
l.error('HTTP Provider does not have \'on\' emitters, use WebSockets instead');
|
|
117
|
+
return util_1.noop;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
|
|
121
|
+
*/
|
|
122
|
+
async send(method, params, isCacheable) {
|
|
123
|
+
this.#stats.total.requests++;
|
|
124
|
+
const [, body] = this.#coder.encodeJson(method, params);
|
|
125
|
+
if (this.#cacheCapacity === 0) {
|
|
126
|
+
return this.#send(body);
|
|
127
|
+
}
|
|
128
|
+
const cacheKey = isCacheable ? `${method}::${(0, util_1.stringify)(params)}` : '';
|
|
129
|
+
let resultPromise = isCacheable
|
|
130
|
+
? this.#callCache.get(cacheKey)
|
|
131
|
+
: null;
|
|
132
|
+
if (!resultPromise) {
|
|
133
|
+
resultPromise = this.#send(body);
|
|
134
|
+
if (isCacheable) {
|
|
135
|
+
this.#callCache.set(cacheKey, resultPromise);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
this.#stats.total.cached++;
|
|
140
|
+
}
|
|
141
|
+
return resultPromise;
|
|
142
|
+
}
|
|
143
|
+
async #send(body) {
|
|
144
|
+
this.#stats.active.requests++;
|
|
145
|
+
this.#stats.total.bytesSent += body.length;
|
|
146
|
+
try {
|
|
147
|
+
const response = await (0, x_fetch_1.fetch)(this.#endpoint, {
|
|
148
|
+
body,
|
|
149
|
+
headers: {
|
|
150
|
+
Accept: 'application/json',
|
|
151
|
+
'Content-Length': `${body.length}`,
|
|
152
|
+
'Content-Type': 'application/json',
|
|
153
|
+
...this.#headers
|
|
154
|
+
},
|
|
155
|
+
method: 'POST'
|
|
156
|
+
});
|
|
157
|
+
if (!response.ok) {
|
|
158
|
+
throw new Error(`[${response.status}]: ${response.statusText}`);
|
|
159
|
+
}
|
|
160
|
+
const result = await response.text();
|
|
161
|
+
this.#stats.total.bytesRecv += result.length;
|
|
162
|
+
const decoded = this.#coder.decodeResponse(JSON.parse(result));
|
|
163
|
+
this.#stats.active.requests--;
|
|
164
|
+
return decoded;
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
this.#stats.active.requests--;
|
|
168
|
+
this.#stats.total.errors++;
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
170
|
+
const { method, params } = JSON.parse(body);
|
|
171
|
+
const rpcError = e;
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
173
|
+
const failedRequest = `\nFailed HTTP Request: ${JSON.stringify({ method, params })}`;
|
|
174
|
+
// Provide HTTP Request alongside the error
|
|
175
|
+
rpcError.message = `${rpcError.message}${failedRequest}`;
|
|
176
|
+
throw rpcError;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
181
|
+
*/
|
|
182
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
183
|
+
async subscribe(_types, _method, _params, _cb) {
|
|
184
|
+
l.error(ERROR_SUBSCRIBE);
|
|
185
|
+
throw new Error(ERROR_SUBSCRIBE);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
|
189
|
+
*/
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
191
|
+
async unsubscribe(_type, _method, _id) {
|
|
192
|
+
l.error(ERROR_SUBSCRIBE);
|
|
193
|
+
throw new Error(ERROR_SUBSCRIBE);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.HttpProvider = HttpProvider;
|
package/cjs/index.d.ts
ADDED
package/cjs/index.js
ADDED
package/cjs/lru.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const DEFAULT_CAPACITY = 1024;
|
|
2
|
+
export declare const DEFAULT_TTL = 30000;
|
|
3
|
+
export declare class LRUCache {
|
|
4
|
+
#private;
|
|
5
|
+
readonly capacity: number;
|
|
6
|
+
constructor(capacity?: number, ttl?: number | null);
|
|
7
|
+
get ttl(): number | null;
|
|
8
|
+
get length(): number;
|
|
9
|
+
get lengthData(): number;
|
|
10
|
+
get lengthRefs(): number;
|
|
11
|
+
entries(): [string, unknown][];
|
|
12
|
+
keys(): string[];
|
|
13
|
+
get<T>(key: string): T | null;
|
|
14
|
+
set<T>(key: string, value: T): void;
|
|
15
|
+
}
|
package/cjs/lru.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LRUCache = exports.DEFAULT_TTL = exports.DEFAULT_CAPACITY = void 0;
|
|
4
|
+
exports.DEFAULT_CAPACITY = 1024;
|
|
5
|
+
exports.DEFAULT_TTL = 30000; // 30 seconds
|
|
6
|
+
const MAX_TTL = 1800_000; // 30 minutes
|
|
7
|
+
const DISABLED_TTL = 31_536_000_000;
|
|
8
|
+
class LRUNode {
|
|
9
|
+
key;
|
|
10
|
+
#expires;
|
|
11
|
+
#ttl;
|
|
12
|
+
createdAt;
|
|
13
|
+
next;
|
|
14
|
+
prev;
|
|
15
|
+
constructor(key, ttl) {
|
|
16
|
+
this.key = key;
|
|
17
|
+
this.#ttl = ttl;
|
|
18
|
+
this.#expires = Date.now() + ttl;
|
|
19
|
+
this.createdAt = Date.now();
|
|
20
|
+
this.next = this.prev = this;
|
|
21
|
+
}
|
|
22
|
+
refresh() {
|
|
23
|
+
this.#expires = Date.now() + this.#ttl;
|
|
24
|
+
}
|
|
25
|
+
get expiry() {
|
|
26
|
+
return this.#expires;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
class LRUCache {
|
|
30
|
+
capacity;
|
|
31
|
+
#data = new Map();
|
|
32
|
+
#refs = new Map();
|
|
33
|
+
#length = 0;
|
|
34
|
+
#head;
|
|
35
|
+
#tail;
|
|
36
|
+
#ttl;
|
|
37
|
+
constructor(capacity = exports.DEFAULT_CAPACITY, ttl = exports.DEFAULT_TTL) {
|
|
38
|
+
// Validate capacity
|
|
39
|
+
if (!Number.isInteger(capacity) || capacity < 0) {
|
|
40
|
+
throw new Error(`LRUCache initialization error: 'capacity' must be a non-negative integer. Received: ${capacity}`);
|
|
41
|
+
}
|
|
42
|
+
// Validate ttl
|
|
43
|
+
if (ttl !== null && (!Number.isFinite(ttl) || ttl < 0 || ttl > MAX_TTL)) {
|
|
44
|
+
throw new Error(`LRUCache initialization error: 'ttl' must be between 0 and ${MAX_TTL} ms or null to disable. Received: ${ttl}`);
|
|
45
|
+
}
|
|
46
|
+
this.capacity = capacity;
|
|
47
|
+
ttl ? this.#ttl = ttl : this.#ttl = DISABLED_TTL;
|
|
48
|
+
this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
|
|
49
|
+
}
|
|
50
|
+
get ttl() {
|
|
51
|
+
return this.#ttl;
|
|
52
|
+
}
|
|
53
|
+
get length() {
|
|
54
|
+
return this.#length;
|
|
55
|
+
}
|
|
56
|
+
get lengthData() {
|
|
57
|
+
return this.#data.size;
|
|
58
|
+
}
|
|
59
|
+
get lengthRefs() {
|
|
60
|
+
return this.#refs.size;
|
|
61
|
+
}
|
|
62
|
+
entries() {
|
|
63
|
+
const keys = this.keys();
|
|
64
|
+
const count = keys.length;
|
|
65
|
+
const entries = new Array(count);
|
|
66
|
+
for (let i = 0; i < count; i++) {
|
|
67
|
+
const key = keys[i];
|
|
68
|
+
entries[i] = [key, this.#data.get(key)];
|
|
69
|
+
}
|
|
70
|
+
return entries;
|
|
71
|
+
}
|
|
72
|
+
keys() {
|
|
73
|
+
const keys = [];
|
|
74
|
+
if (this.#length) {
|
|
75
|
+
let curr = this.#head;
|
|
76
|
+
while (curr !== this.#tail) {
|
|
77
|
+
keys.push(curr.key);
|
|
78
|
+
curr = curr.next;
|
|
79
|
+
}
|
|
80
|
+
keys.push(curr.key);
|
|
81
|
+
}
|
|
82
|
+
return keys;
|
|
83
|
+
}
|
|
84
|
+
get(key) {
|
|
85
|
+
const data = this.#data.get(key);
|
|
86
|
+
if (data) {
|
|
87
|
+
this.#toHead(key);
|
|
88
|
+
// Evict TTL once data is refreshed
|
|
89
|
+
this.#evictTTL();
|
|
90
|
+
return data;
|
|
91
|
+
}
|
|
92
|
+
this.#evictTTL();
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
set(key, value) {
|
|
96
|
+
if (this.#data.has(key)) {
|
|
97
|
+
this.#toHead(key);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const node = new LRUNode(key, this.#ttl);
|
|
101
|
+
this.#refs.set(node.key, node);
|
|
102
|
+
if (this.length === 0) {
|
|
103
|
+
this.#head = this.#tail = node;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
this.#head.prev = node;
|
|
107
|
+
node.next = this.#head;
|
|
108
|
+
this.#head = node;
|
|
109
|
+
}
|
|
110
|
+
if (this.#length === this.capacity) {
|
|
111
|
+
this.#data.delete(this.#tail.key);
|
|
112
|
+
this.#refs.delete(this.#tail.key);
|
|
113
|
+
this.#tail = this.#tail.prev;
|
|
114
|
+
this.#tail.next = this.#head;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this.#length += 1;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Evict TTL once data is refreshed or added
|
|
121
|
+
this.#evictTTL();
|
|
122
|
+
this.#data.set(key, value);
|
|
123
|
+
}
|
|
124
|
+
#evictTTL() {
|
|
125
|
+
// Find last node to keep
|
|
126
|
+
// traverse map to find the expired nodes
|
|
127
|
+
while (this.#tail.expiry && this.#tail.expiry < Date.now() && this.#length > 0) {
|
|
128
|
+
this.#refs.delete(this.#tail.key);
|
|
129
|
+
this.#data.delete(this.#tail.key);
|
|
130
|
+
this.#length -= 1;
|
|
131
|
+
this.#tail = this.#tail.prev;
|
|
132
|
+
this.#tail.next = this.#head;
|
|
133
|
+
}
|
|
134
|
+
if (this.#length === 0) {
|
|
135
|
+
this.#head = this.#tail = new LRUNode('<empty>', this.#ttl);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
#toHead(key) {
|
|
139
|
+
const ref = this.#refs.get(key);
|
|
140
|
+
if (ref && ref !== this.#head) {
|
|
141
|
+
ref.refresh();
|
|
142
|
+
ref.prev.next = ref.next;
|
|
143
|
+
ref.next.prev = ref.prev;
|
|
144
|
+
ref.next = this.#head;
|
|
145
|
+
this.#head.prev = ref;
|
|
146
|
+
this.#head = ref;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.LRUCache = LRUCache;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Registry } from '@pezkuwi/types/types';
|
|
2
|
+
import type { ProviderInterface, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types.js';
|
|
3
|
+
import type { MockStateSubscriptions } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* A mock provider mainly used for testing.
|
|
6
|
+
* @return {ProviderInterface} The mock provider
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export declare class MockProvider implements ProviderInterface {
|
|
10
|
+
private db;
|
|
11
|
+
private emitter;
|
|
12
|
+
private intervalId?;
|
|
13
|
+
isUpdating: boolean;
|
|
14
|
+
private registry;
|
|
15
|
+
private prevNumber;
|
|
16
|
+
private requests;
|
|
17
|
+
subscriptions: MockStateSubscriptions;
|
|
18
|
+
private subscriptionId;
|
|
19
|
+
private subscriptionMap;
|
|
20
|
+
constructor(registry: Registry);
|
|
21
|
+
get hasSubscriptions(): boolean;
|
|
22
|
+
clone(): MockProvider;
|
|
23
|
+
connect(): Promise<void>;
|
|
24
|
+
disconnect(): Promise<void>;
|
|
25
|
+
get isClonable(): boolean;
|
|
26
|
+
get isConnected(): boolean;
|
|
27
|
+
on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
|
|
28
|
+
send<T = any>(method: string, params: unknown[]): Promise<T>;
|
|
29
|
+
subscribe(_type: string, method: string, ...params: unknown[]): Promise<number>;
|
|
30
|
+
unsubscribe(_type: string, _method: string, id: number): Promise<boolean>;
|
|
31
|
+
private init;
|
|
32
|
+
private makeBlockHeader;
|
|
33
|
+
private setStateBn;
|
|
34
|
+
private updateSubs;
|
|
35
|
+
}
|