@harmony-js/network
Advanced tools
Comparing version 0.1.7 to 0.1.12
@@ -7,2 +7,8 @@ import { HarmonyCore, ChainType, ChainID } from '@harmony-js/utils'; | ||
import { SubscribeReturns } from '../types'; | ||
export interface ShardingProvider { | ||
current: boolean; | ||
shardID: number; | ||
http: HttpProvider; | ||
ws: WSProvider; | ||
} | ||
/** | ||
@@ -19,4 +25,7 @@ * @class Messenger | ||
Network_ID: string; | ||
shardProviders: Map<number, ShardingProvider>; | ||
JsonRpc: JsonRpc; | ||
constructor(provider: HttpProvider | WSProvider, chainType?: ChainType, chainId?: ChainID, config?: object); | ||
readonly currentShard: number | undefined; | ||
readonly shardCount: number; | ||
/** | ||
@@ -29,3 +38,3 @@ * @function send | ||
*/ | ||
send: (method: string, params?: string | any[] | undefined, rpcPrefix?: string | undefined) => Promise<any>; | ||
send: (method: string, params?: string | any[] | undefined, rpcPrefix?: string | undefined, shardID?: number) => Promise<any>; | ||
/** | ||
@@ -52,3 +61,3 @@ * @function setProvider | ||
*/ | ||
setReqMiddleware(middleware: any, method?: string): void; | ||
setReqMiddleware(middleware: any, method: string | undefined, provider: HttpProvider | WSProvider): void; | ||
/** | ||
@@ -61,3 +70,3 @@ * @function setResMiddleware | ||
*/ | ||
setResMiddleware(middleware: any, method?: string): void; | ||
setResMiddleware(middleware: any, method: string | undefined, provider: HttpProvider | WSProvider): void; | ||
/** | ||
@@ -71,6 +80,9 @@ * @function setNetworkID | ||
setRPCPrefix(method: RPCMethod | string, prefix: string): string; | ||
subscribe: (method: string, params?: string | any[] | undefined, returnType?: SubscribeReturns, rpcPrefix?: string) => Promise<any>; | ||
unsubscribe: (method: string, params?: string | any[] | undefined, rpcPrefix?: string | undefined) => Promise<any>; | ||
subscribe: (method: string, params?: string | any[] | undefined, returnType?: SubscribeReturns, rpcPrefix?: string, shardID?: number) => Promise<any>; | ||
unsubscribe: (method: string, params?: string | any[] | undefined, rpcPrefix?: string | undefined, shardID?: number) => Promise<any>; | ||
setShardingProviders(): Promise<void>; | ||
getShardProvider(shardID: number): HttpProvider | WSProvider; | ||
getCurrentShardID(): number | undefined; | ||
} | ||
export { Messenger }; | ||
//# sourceMappingURL=messenger.d.ts.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
var responseMiddleware_1 = require("./responseMiddleware"); | ||
var http_1 = require("../providers/http"); | ||
var ws_1 = require("../providers/ws"); | ||
@@ -21,2 +22,3 @@ /** | ||
if (chainId === void 0) { chainId = utils_1.defaultConfig.Default.Chain_ID; } | ||
if (config === void 0) { config = utils_1.defaultConfig; } | ||
var _this = _super.call(this, chainType, chainId) || this; | ||
@@ -32,43 +34,48 @@ // tslint:disable-next-line: variable-name | ||
*/ | ||
_this.send = function (method, params, rpcPrefix) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var rpcMethod, payload, result, e_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.providerCheck(); | ||
rpcMethod = method; | ||
if (rpcPrefix && utils_1.isString(rpcPrefix) && rpcPrefix !== this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, rpcPrefix); | ||
} | ||
else if (!rpcPrefix || rpcPrefix === this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, this.chainPrefix); | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
this.setResMiddleware(function (data) { | ||
if (!(data instanceof responseMiddleware_1.ResponseMiddleware)) { | ||
return new responseMiddleware_1.ResponseMiddleware(data); | ||
_this.send = function (method, params, rpcPrefix, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var rpcMethod, payload, provider, result, e_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
this.providerCheck(); | ||
rpcMethod = method; | ||
if (rpcPrefix && utils_1.isString(rpcPrefix) && rpcPrefix !== this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, rpcPrefix); | ||
} | ||
else { | ||
return data; | ||
else if (!rpcPrefix || rpcPrefix === this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, this.chainPrefix); | ||
} | ||
}); | ||
return [4 /*yield*/, this.provider.send(payload)]; | ||
case 2: | ||
result = _a.sent(); | ||
return [2 /*return*/, result]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
throw new Error(e_1); | ||
case 4: return [2 /*return*/]; | ||
} | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
provider = this.getShardProvider(shardID); | ||
this.setResMiddleware(function (data) { | ||
if (!(data instanceof responseMiddleware_1.ResponseMiddleware)) { | ||
return new responseMiddleware_1.ResponseMiddleware(data); | ||
} | ||
else { | ||
return data; | ||
} | ||
}, '*', provider); | ||
return [4 /*yield*/, provider.send(payload)]; | ||
case 2: | ||
result = _a.sent(); | ||
return [2 /*return*/, result]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
throw new Error(e_1); | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); }; | ||
_this.subscribe = function (method, params, returnType, rpcPrefix) { | ||
}; | ||
_this.subscribe = function (method, params, returnType, rpcPrefix, shardID) { | ||
if (returnType === void 0) { returnType = "all" /* all */; } | ||
if (rpcPrefix === void 0) { rpcPrefix = _this.chainPrefix; } | ||
if (shardID === void 0) { shardID = 0; } | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var rpcMethod, id, provider_1, payload, error_1; | ||
var rpcMethod, id, provider, reProvider_1, payload, error_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
@@ -85,4 +92,5 @@ switch (_a.label) { | ||
id = null; | ||
if (!(this.provider instanceof ws_1.WSProvider)) return [3 /*break*/, 5]; | ||
provider_1 = this.provider; | ||
provider = this.getShardProvider(shardID); | ||
if (!(provider instanceof ws_1.WSProvider)) return [3 /*break*/, 5]; | ||
reProvider_1 = provider; | ||
_a.label = 1; | ||
@@ -92,12 +100,12 @@ case 1: | ||
payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
return [4 /*yield*/, provider_1.subscribe(payload)]; | ||
return [4 /*yield*/, reProvider_1.subscribe(payload)]; | ||
case 2: | ||
id = _a.sent(); | ||
provider_1.on(id, function (result) { | ||
provider_1.emitter.emit('data', result); | ||
reProvider_1.on(id, function (result) { | ||
reProvider_1.emitter.emit('data', result); | ||
}); | ||
provider_1.once('error', function (error) { | ||
provider_1.removeEventListener(id); | ||
provider_1.emitter.emit('error', error); | ||
provider_1.removeEventListener('*'); | ||
reProvider_1.once('error', function (error) { | ||
reProvider_1.removeEventListener(id); | ||
reProvider_1.emitter.emit('error', error); | ||
reProvider_1.removeEventListener('*'); | ||
}); | ||
@@ -107,11 +115,11 @@ return [3 /*break*/, 4]; | ||
error_1 = _a.sent(); | ||
provider_1.emitter.emit('error', error_1); | ||
provider_1.removeEventListener('*'); | ||
reProvider_1.emitter.emit('error', error_1); | ||
reProvider_1.removeEventListener('*'); | ||
return [3 /*break*/, 4]; | ||
case 4: | ||
if (returnType === "all" /* all */) { | ||
return [2 /*return*/, [provider_1, id]]; | ||
return [2 /*return*/, [reProvider_1, id]]; | ||
} | ||
else if (returnType === "method" /* method */) { | ||
return [2 /*return*/, provider_1]; | ||
return [2 /*return*/, reProvider_1]; | ||
} | ||
@@ -131,33 +139,37 @@ else if (returnType === "id" /* id */) { | ||
}; | ||
_this.unsubscribe = function (method, params, rpcPrefix) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var rpcMethod, provider, payload, response, error_2; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
rpcMethod = method; | ||
if (rpcPrefix && utils_1.isString(rpcPrefix) && rpcPrefix !== this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, rpcPrefix); | ||
} | ||
else if (!rpcPrefix || rpcPrefix === this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, this.chainPrefix); | ||
} | ||
if (!(this.provider instanceof ws_1.WSProvider)) return [3 /*break*/, 5]; | ||
provider = this.provider; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
return [4 /*yield*/, provider.unsubscribe(payload)]; | ||
case 2: | ||
response = _a.sent(); | ||
return [2 /*return*/, response]; | ||
case 3: | ||
error_2 = _a.sent(); | ||
throw error_2; | ||
case 4: return [3 /*break*/, 6]; | ||
case 5: throw new Error('HttpProvider does not support this'); | ||
case 6: return [2 /*return*/]; | ||
} | ||
_this.unsubscribe = function (method, params, rpcPrefix, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var rpcMethod, provider, reProvider, payload, response, error_2; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
rpcMethod = method; | ||
if (rpcPrefix && utils_1.isString(rpcPrefix) && rpcPrefix !== this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, rpcPrefix); | ||
} | ||
else if (!rpcPrefix || rpcPrefix === this.chainPrefix) { | ||
rpcMethod = this.setRPCPrefix(method, this.chainPrefix); | ||
} | ||
provider = this.getShardProvider(shardID); | ||
if (!(provider instanceof ws_1.WSProvider)) return [3 /*break*/, 5]; | ||
reProvider = this.provider; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
return [4 /*yield*/, reProvider.unsubscribe(payload)]; | ||
case 2: | ||
response = _a.sent(); | ||
return [2 /*return*/, response]; | ||
case 3: | ||
error_2 = _a.sent(); | ||
throw error_2; | ||
case 4: return [3 /*break*/, 6]; | ||
case 5: throw new Error('HttpProvider does not support this'); | ||
case 6: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); }; | ||
}; | ||
/** | ||
@@ -174,3 +186,3 @@ * @var {Provider} provider | ||
*/ | ||
_this.config = config || utils_1.defaultConfig; | ||
_this.config = config; | ||
/** | ||
@@ -189,4 +201,21 @@ * @var {Number} Network_ID | ||
_this.setNetworkID(utils_1.defaultConfig.Default.Network_ID); | ||
// set shardingProviders | ||
_this.shardProviders = new Map(); | ||
return _this; | ||
// this.setShardingProviders(); | ||
} | ||
Object.defineProperty(Messenger.prototype, "currentShard", { | ||
get: function () { | ||
return this.getCurrentShardID(); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(Messenger.prototype, "shardCount", { | ||
get: function () { | ||
return this.shardProviders.size; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -219,5 +248,5 @@ * @function setProvider | ||
*/ | ||
Messenger.prototype.setReqMiddleware = function (middleware, method) { | ||
Messenger.prototype.setReqMiddleware = function (middleware, method, provider) { | ||
if (method === void 0) { method = '*'; } | ||
this.provider.middlewares.request.use(middleware, method); | ||
provider.middlewares.request.use(middleware, method); | ||
}; | ||
@@ -231,5 +260,5 @@ /** | ||
*/ | ||
Messenger.prototype.setResMiddleware = function (middleware, method) { | ||
Messenger.prototype.setResMiddleware = function (middleware, method, provider) { | ||
if (method === void 0) { method = '*'; } | ||
this.provider.middlewares.response.use(middleware, method); | ||
provider.middlewares.response.use(middleware, method); | ||
}; | ||
@@ -253,2 +282,76 @@ /** | ||
}; | ||
Messenger.prototype.setShardingProviders = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var response, shardingStructures, shardingStructures_1, shardingStructures_1_1, shard, shardID, error_3; | ||
var e_2, _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (this.chainPrefix !== "hmy" /* Harmony */) { | ||
return [2 /*return*/]; | ||
} | ||
_b.label = 1; | ||
case 1: | ||
_b.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.send("hmy_getShardingStructure" /* GetShardingStructure */, [], this.chainPrefix)]; | ||
case 2: | ||
response = _b.sent(); | ||
if (response.result) { | ||
shardingStructures = response.result; | ||
try { | ||
for (shardingStructures_1 = tslib_1.__values(shardingStructures), shardingStructures_1_1 = shardingStructures_1.next(); !shardingStructures_1_1.done; shardingStructures_1_1 = shardingStructures_1.next()) { | ||
shard = shardingStructures_1_1.value; | ||
shardID = typeof shard.shardID === 'string' ? Number.parseInt(shard.shardID, 10) : shard.shardID; | ||
this.shardProviders.set(shardID, { | ||
current: shard.current, | ||
shardID: shardID, | ||
http: new http_1.HttpProvider(shard.http), | ||
ws: new ws_1.WSProvider(shard.ws), | ||
}); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (shardingStructures_1_1 && !shardingStructures_1_1.done && (_a = shardingStructures_1.return)) _a.call(shardingStructures_1); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
error_3 = _b.sent(); | ||
return [2 /*return*/]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Messenger.prototype.getShardProvider = function (shardID) { | ||
var provider = this.shardProviders.get(shardID); | ||
if (provider) { | ||
return this.provider instanceof http_1.HttpProvider ? provider.http : provider.ws; | ||
} | ||
return this.provider; | ||
}; | ||
Messenger.prototype.getCurrentShardID = function () { | ||
var e_3, _a; | ||
try { | ||
for (var _b = tslib_1.__values(this.shardProviders), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var shard = _c.value; | ||
if (shard[1].current === true || | ||
shard[1].http.url === this.provider.url || | ||
shard[1].ws.url === this.provider.url) { | ||
return shard[1].shardID; | ||
} | ||
} | ||
} | ||
catch (e_3_1) { e_3 = { error: e_3_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_3) throw e_3.error; } | ||
} | ||
}; | ||
return Messenger; | ||
@@ -255,0 +358,0 @@ }(utils_1.HarmonyCore)); |
@@ -18,3 +18,3 @@ "use strict"; | ||
var _this = _super.call(this, url) || this; | ||
_this.url = url || 'http://localhost:9128'; | ||
_this.url = url || 'http://localhost:9500'; | ||
_this.fetcher = fetcher || defaultFetcher_1.fetchRPC; | ||
@@ -84,5 +84,3 @@ if (options) { | ||
HttpProvider.prototype.endpointHandler = function (obj, endpoint) { | ||
return tslib_1.__assign({}, obj, { url: endpoint !== null && endpoint !== undefined | ||
? "" + this.url + endpoint | ||
: this.url }); | ||
return tslib_1.__assign({}, obj, { url: endpoint !== null && endpoint !== undefined ? "" + this.url + endpoint : this.url }); | ||
}; | ||
@@ -89,0 +87,0 @@ /** |
@@ -195,6 +195,6 @@ "use strict"; | ||
} | ||
// tslint:disable-next-line: prefer-conditional-expression | ||
if (typeof result.id === 'undefined') { | ||
event_1 = | ||
this.getSubscriptionEvent(result.params.subscription) || | ||
result.params.subscription; | ||
this.getSubscriptionEvent(result.params.subscription) || result.params.subscription; | ||
// result = result.params; | ||
@@ -201,0 +201,0 @@ } |
@@ -32,3 +32,4 @@ export declare const enum RPCMethod { | ||
NetVersion = "net_version", | ||
ProtocolVersion = "hmy_protocolVersion" | ||
ProtocolVersion = "hmy_protocolVersion", | ||
GetShardingStructure = "hmy_getShardingStructure" | ||
} | ||
@@ -35,0 +36,0 @@ export declare const enum RPCErrorCode { |
@@ -68,2 +68,4 @@ "use strict"; | ||
RPCMethod["ProtocolVersion"] = "hmy_protocolVersion"; | ||
// 32. hmy_getShardingStructure | ||
RPCMethod["GetShardingStructure"] = "hmy_getShardingStructure"; | ||
})(RPCMethod = exports.RPCMethod || (exports.RPCMethod = {})); | ||
@@ -70,0 +72,0 @@ var RPCErrorCode; |
import { Messenger } from '../messenger/messenger'; | ||
import { SubscriptionMethod } from './Subscription'; | ||
export declare class LogSub extends SubscriptionMethod { | ||
constructor(options: any, messenger: Messenger); | ||
constructor(options: any, messenger: Messenger, shardID?: number); | ||
subscribe(): Promise<this>; | ||
@@ -6,0 +6,0 @@ onNewSubscriptionItem(subscriptionItem: any): any; |
@@ -7,4 +7,5 @@ "use strict"; | ||
tslib_1.__extends(LogSub, _super); | ||
function LogSub(options, messenger) { | ||
var _this = _super.call(this, 'logs', options, messenger) || this; | ||
function LogSub(options, messenger, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
var _this = _super.call(this, 'logs', options, messenger, shardID) || this; | ||
_this.subscribe(); | ||
@@ -25,3 +26,3 @@ return _this; | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, this.messenger.send("hmy_getLogs" /* GetPastLogs */, tslib_1.__spread(this.options))]; | ||
return [4 /*yield*/, this.messenger.send("hmy_getLogs" /* GetPastLogs */, tslib_1.__spread(this.options), this.messenger.chainType, this.shardID)]; | ||
case 2: | ||
@@ -28,0 +29,0 @@ getPastLogs = _a.sent(); |
import { Messenger } from '../messenger/messenger'; | ||
import { SubscriptionMethod } from './Subscription'; | ||
export declare class NewHeaders extends SubscriptionMethod { | ||
constructor(messenger: Messenger); | ||
constructor(messenger: Messenger, shardID?: number); | ||
} | ||
//# sourceMappingURL=NewHeadersSub.d.ts.map |
@@ -7,4 +7,5 @@ "use strict"; | ||
tslib_1.__extends(NewHeaders, _super); | ||
function NewHeaders(messenger) { | ||
var _this = _super.call(this, 'newHeads', undefined, messenger) || this; | ||
function NewHeaders(messenger, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
var _this = _super.call(this, 'newHeads', undefined, messenger, shardID) || this; | ||
_this.start(); | ||
@@ -11,0 +12,0 @@ return _this; |
import { Messenger } from '../messenger/messenger'; | ||
import { SubscriptionMethod } from './Subscription'; | ||
export declare class NewPendingTransactions extends SubscriptionMethod { | ||
constructor(messenger: Messenger); | ||
constructor(messenger: Messenger, shardID?: number); | ||
} | ||
//# sourceMappingURL=NewPendingTransactionsSub.d.ts.map |
@@ -7,4 +7,5 @@ "use strict"; | ||
tslib_1.__extends(NewPendingTransactions, _super); | ||
function NewPendingTransactions(messenger) { | ||
var _this = _super.call(this, 'newPendingTransactions', undefined, messenger) || this; | ||
function NewPendingTransactions(messenger, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
var _this = _super.call(this, 'newPendingTransactions', undefined, messenger, shardID) || this; | ||
_this.start(); | ||
@@ -11,0 +12,0 @@ return _this; |
@@ -7,5 +7,6 @@ import { Messenger } from '../messenger/messenger'; | ||
options: any; | ||
shardID: number; | ||
messenger: Messenger; | ||
subscriptionId: any; | ||
constructor(param: any, options: any, messenger: Messenger); | ||
constructor(param: any, options: any, messenger: Messenger, shardID?: number); | ||
constructPayload(method: string, param: any, options?: any): RPCRequestPayload<any>; | ||
@@ -12,0 +13,0 @@ start(): Promise<this>; |
@@ -7,5 +7,7 @@ "use strict"; | ||
tslib_1.__extends(SubscriptionMethod, _super); | ||
function SubscriptionMethod(param, options, messenger) { | ||
var _this = _super.call(this, messenger.provider.url) || this; | ||
function SubscriptionMethod(param, options, messenger, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
var _this = _super.call(this, shardID !== 0 ? messenger.getShardProvider(shardID).url : messenger.provider.url) || this; | ||
_this.subscriptionId = null; | ||
_this.shardID = shardID; | ||
_this.param = param; | ||
@@ -12,0 +14,0 @@ _this.options = options; |
@@ -5,5 +5,5 @@ import { Messenger } from '../messenger/messenger'; | ||
isSyncing: boolean | null; | ||
constructor(messenger: Messenger); | ||
constructor(messenger: Messenger, shardID?: number); | ||
onNewSubscriptionItem(subscriptionItem: any): any; | ||
} | ||
//# sourceMappingURL=SyncingSub.d.ts.map |
@@ -7,4 +7,5 @@ "use strict"; | ||
tslib_1.__extends(Syncing, _super); | ||
function Syncing(messenger) { | ||
var _this = _super.call(this, 'syncing', undefined, messenger) || this; | ||
function Syncing(messenger, shardID) { | ||
if (shardID === void 0) { shardID = 0; } | ||
var _this = _super.call(this, 'syncing', undefined, messenger, shardID) || this; | ||
_this.isSyncing = null; | ||
@@ -11,0 +12,0 @@ _this.start(); |
@@ -51,2 +51,8 @@ import { RPCMethod, RPCErrorCode } from './rpcMethod/rpc'; | ||
} | ||
export interface ShardingItem { | ||
current: boolean; | ||
shardID: number | string; | ||
http: string; | ||
ws: string; | ||
} | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@harmony-js/network", | ||
"version": "0.1.7", | ||
"version": "0.1.12", | ||
"description": "network suites for harmony", | ||
@@ -21,3 +21,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@harmony-js/utils": "0.1.6", | ||
"@harmony-js/utils": "0.1.12", | ||
"cross-fetch": "^3.0.2", | ||
@@ -27,3 +27,3 @@ "mitt": "^1.1.3", | ||
}, | ||
"gitHead": "b2300964739641778b379b575fdaf45bff34740b" | ||
"gitHead": "b6b3ff2a6830cfae9db3301b88e1fe636657c7ae" | ||
} |
@@ -1,16 +0,17 @@ | ||
import { | ||
HarmonyCore, | ||
ChainType, | ||
isString, | ||
ChainID, | ||
defaultConfig, | ||
} from '@harmony-js/utils'; | ||
import {JsonRpc} from '../rpcMethod/builder'; | ||
import {ResponseMiddleware} from './responseMiddleware'; | ||
import {HttpProvider} from '../providers/http'; | ||
import {WSProvider} from '../providers/ws'; | ||
import { HarmonyCore, ChainType, isString, ChainID, defaultConfig } from '@harmony-js/utils'; | ||
import { JsonRpc } from '../rpcMethod/builder'; | ||
import { ResponseMiddleware } from './responseMiddleware'; | ||
import { HttpProvider } from '../providers/http'; | ||
import { WSProvider } from '../providers/ws'; | ||
// import { getResultForData } from '../util'; | ||
import {RPCMethod} from '../rpcMethod/rpc'; | ||
import {SubscribeReturns} from '../types'; | ||
import { RPCMethod } from '../rpcMethod/rpc'; | ||
import { SubscribeReturns, ShardingItem } from '../types'; | ||
export interface ShardingProvider { | ||
current: boolean; | ||
shardID: number; | ||
http: HttpProvider; | ||
ws: WSProvider; | ||
} | ||
/** | ||
@@ -28,2 +29,4 @@ * @class Messenger | ||
Network_ID: string = 'Default'; | ||
shardProviders: Map<number, ShardingProvider>; | ||
JsonRpc: JsonRpc; | ||
@@ -35,5 +38,6 @@ | ||
chainId: ChainID = defaultConfig.Default.Chain_ID, | ||
config?: object, | ||
config: object = defaultConfig, | ||
) { | ||
super(chainType, chainId); | ||
/** | ||
@@ -51,3 +55,3 @@ * @var {Provider} provider | ||
*/ | ||
this.config = config || defaultConfig; | ||
this.config = config; | ||
/** | ||
@@ -68,4 +72,15 @@ * @var {Number} Network_ID | ||
this.setNetworkID(defaultConfig.Default.Network_ID); | ||
// set shardingProviders | ||
this.shardProviders = new Map(); | ||
// this.setShardingProviders(); | ||
} | ||
get currentShard(): number | undefined { | ||
return this.getCurrentShardID(); | ||
} | ||
get shardCount(): number { | ||
return this.shardProviders.size; | ||
} | ||
/** | ||
@@ -82,2 +97,3 @@ * @function send | ||
rpcPrefix?: string, | ||
shardID: number = 0, | ||
) => { | ||
@@ -93,10 +109,17 @@ this.providerCheck(); | ||
const payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
this.setResMiddleware((data: any) => { | ||
if (!(data instanceof ResponseMiddleware)) { | ||
return new ResponseMiddleware(data); | ||
} else { | ||
return data; | ||
} | ||
}); | ||
const result = await this.provider.send(payload); | ||
const provider = this.getShardProvider(shardID); | ||
this.setResMiddleware( | ||
(data: any) => { | ||
if (!(data instanceof ResponseMiddleware)) { | ||
return new ResponseMiddleware(data); | ||
} else { | ||
return data; | ||
} | ||
}, | ||
'*', | ||
provider, | ||
); | ||
const result = await provider.send(payload); | ||
return result; | ||
@@ -138,4 +161,4 @@ // return getResultForData(result); // getResultForData(result) | ||
*/ | ||
setReqMiddleware(middleware: any, method = '*') { | ||
this.provider.middlewares.request.use(middleware, method); | ||
setReqMiddleware(middleware: any, method = '*', provider: HttpProvider | WSProvider) { | ||
provider.middlewares.request.use(middleware, method); | ||
} | ||
@@ -150,4 +173,4 @@ | ||
*/ | ||
setResMiddleware(middleware: any, method = '*') { | ||
this.provider.middlewares.response.use(middleware, method); | ||
setResMiddleware(middleware: any, method = '*', provider: HttpProvider | WSProvider) { | ||
provider.middlewares.response.use(middleware, method); | ||
} | ||
@@ -179,2 +202,3 @@ | ||
rpcPrefix: string = this.chainPrefix, | ||
shardID: number = 0, | ||
) => { | ||
@@ -188,24 +212,25 @@ let rpcMethod = method; | ||
let id: any = null; | ||
if (this.provider instanceof WSProvider) { | ||
const provider = this.provider; | ||
const provider = this.getShardProvider(shardID); | ||
if (provider instanceof WSProvider) { | ||
const reProvider = provider; | ||
try { | ||
const payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
id = await provider.subscribe(payload); | ||
provider.on(id, (result: any) => { | ||
provider.emitter.emit('data', result); | ||
id = await reProvider.subscribe(payload); | ||
reProvider.on(id, (result: any) => { | ||
reProvider.emitter.emit('data', result); | ||
}); | ||
provider.once('error', (error) => { | ||
provider.removeEventListener(id); | ||
provider.emitter.emit('error', error); | ||
provider.removeEventListener('*'); | ||
reProvider.once('error', (error) => { | ||
reProvider.removeEventListener(id); | ||
reProvider.emitter.emit('error', error); | ||
reProvider.removeEventListener('*'); | ||
}); | ||
} catch (error) { | ||
provider.emitter.emit('error', error); | ||
provider.removeEventListener('*'); | ||
reProvider.emitter.emit('error', error); | ||
reProvider.removeEventListener('*'); | ||
} | ||
if (returnType === SubscribeReturns.all) { | ||
return [provider, id]; | ||
return [reProvider, id]; | ||
} else if (returnType === SubscribeReturns.method) { | ||
return provider; | ||
return reProvider; | ||
} else if (returnType === SubscribeReturns.id) { | ||
@@ -225,2 +250,3 @@ return id; | ||
rpcPrefix?: string, | ||
shardID: number = 0, | ||
) => { | ||
@@ -233,7 +259,8 @@ let rpcMethod = method; | ||
} | ||
if (this.provider instanceof WSProvider) { | ||
const provider = this.provider; | ||
const provider = this.getShardProvider(shardID); | ||
if (provider instanceof WSProvider) { | ||
const reProvider = this.provider; | ||
try { | ||
const payload = this.JsonRpc.toPayload(rpcMethod, params); | ||
const response = await provider.unsubscribe(payload); | ||
const response = await reProvider.unsubscribe(payload); | ||
return response; | ||
@@ -247,3 +274,46 @@ } catch (error) { | ||
}; | ||
async setShardingProviders() { | ||
if (this.chainPrefix !== ChainType.Harmony) { | ||
return; | ||
} | ||
try { | ||
const response = await this.send(RPCMethod.GetShardingStructure, [], this.chainPrefix); | ||
if (response.result) { | ||
const shardingStructures: ShardingItem[] = response.result; | ||
for (const shard of shardingStructures) { | ||
const shardID = | ||
typeof shard.shardID === 'string' ? Number.parseInt(shard.shardID, 10) : shard.shardID; | ||
this.shardProviders.set(shardID, { | ||
current: shard.current, | ||
shardID, | ||
http: new HttpProvider(shard.http), | ||
ws: new WSProvider(shard.ws), | ||
}); | ||
} | ||
} | ||
} catch (error) { | ||
return; | ||
} | ||
} | ||
getShardProvider(shardID: number): HttpProvider | WSProvider { | ||
const provider = this.shardProviders.get(shardID); | ||
if (provider) { | ||
return this.provider instanceof HttpProvider ? provider.http : provider.ws; | ||
} | ||
return this.provider; | ||
} | ||
getCurrentShardID() { | ||
for (const shard of this.shardProviders) { | ||
if ( | ||
shard[1].current === true || | ||
shard[1].http.url === this.provider.url || | ||
shard[1].ws.url === this.provider.url | ||
) { | ||
return shard[1].shardID; | ||
} | ||
} | ||
} | ||
} | ||
export {Messenger}; | ||
export { Messenger }; |
import { BaseProvider } from './baseProvider'; | ||
import { fetchRPC } from './defaultFetcher'; | ||
import { | ||
composeMiddleware, | ||
performRPC, | ||
DEFAULT_TIMEOUT, | ||
DEFAULT_HEADERS, | ||
} from '../rpcMethod/net'; | ||
import { composeMiddleware, performRPC, DEFAULT_TIMEOUT, DEFAULT_HEADERS } from '../rpcMethod/net'; | ||
@@ -26,3 +21,3 @@ import { RPCRequestPayload } from '../types'; | ||
super(url); | ||
this.url = url || 'http://localhost:9128'; | ||
this.url = url || 'http://localhost:9500'; | ||
this.fetcher = fetcher || fetchRPC; | ||
@@ -61,7 +56,3 @@ if (options) { | ||
*/ | ||
sendServer( | ||
endpoint: string, | ||
payload: RPCRequestPayload<object>, | ||
callback: any, | ||
): Promise<any> { | ||
sendServer(endpoint: string, payload: RPCRequestPayload<object>, callback: any): Promise<any> { | ||
return this.requestFunc({ endpoint, payload, callback }); | ||
@@ -116,6 +107,3 @@ } | ||
...obj, | ||
url: | ||
endpoint !== null && endpoint !== undefined | ||
? `${this.url}${endpoint}` | ||
: this.url, | ||
url: endpoint !== null && endpoint !== undefined ? `${this.url}${endpoint}` : this.url, | ||
}; | ||
@@ -122,0 +110,0 @@ } |
// TODO: implement Websocket Provider | ||
import {w3cwebsocket as W3CWebsocket} from 'websocket'; | ||
import { w3cwebsocket as W3CWebsocket } from 'websocket'; | ||
import { | ||
@@ -9,6 +9,6 @@ BaseSocket, | ||
} from './baseSocket'; | ||
import {isWs, isObject, isArray} from '@harmony-js/utils'; | ||
import {JsonRpc} from '../rpcMethod/builder'; | ||
import {composeMiddleware} from '../rpcMethod/net'; | ||
import {RPCRequestPayload} from '../types'; | ||
import { isWs, isObject, isArray } from '@harmony-js/utils'; | ||
import { JsonRpc } from '../rpcMethod/builder'; | ||
import { composeMiddleware } from '../rpcMethod/net'; | ||
import { RPCRequestPayload } from '../types'; | ||
@@ -74,5 +74,5 @@ class WSProvider extends BaseSocket { | ||
if (!headers.authorization && urlObject.username && urlObject.password) { | ||
const authToken = Buffer.from( | ||
`${urlObject.username}:${urlObject.password}`, | ||
).toString('base64'); | ||
const authToken = Buffer.from(`${urlObject.username}:${urlObject.password}`).toString( | ||
'base64', | ||
); | ||
headers.authorization = `Basic ${authToken}`; | ||
@@ -145,2 +145,3 @@ } | ||
}; | ||
return response.result; | ||
@@ -163,5 +164,3 @@ } | ||
return Promise.reject( | ||
new Error( | ||
`Provider error: Subscription with ID ${subscriptionId} does not exist.`, | ||
), | ||
new Error(`Provider error: Subscription with ID ${subscriptionId} does not exist.`), | ||
); | ||
@@ -176,5 +175,3 @@ } | ||
unsubscribePromises.push( | ||
this.unsubscribe( | ||
this.jsonRpc.toPayload(unsubscribeMethod, this.subscriptions[key].id), | ||
), | ||
this.unsubscribe(this.jsonRpc.toPayload(unsubscribeMethod, this.subscriptions[key].id)), | ||
); | ||
@@ -185,5 +182,3 @@ }); | ||
if (results.includes(false)) { | ||
throw new Error( | ||
`Could not unsubscribe all subscriptions: ${JSON.stringify(results)}`, | ||
); | ||
throw new Error(`Could not unsubscribe all subscriptions: ${JSON.stringify(results)}`); | ||
} | ||
@@ -211,6 +206,6 @@ return true; | ||
} | ||
// tslint:disable-next-line: prefer-conditional-expression | ||
if (typeof result.id === 'undefined') { | ||
event = | ||
this.getSubscriptionEvent(result.params.subscription) || | ||
result.params.subscription; | ||
this.getSubscriptionEvent(result.params.subscription) || result.params.subscription; | ||
// result = result.params; | ||
@@ -223,3 +218,2 @@ } else { | ||
} | ||
this.emitter.emit(SocketState.SOCKET_MESSAGE, result); | ||
@@ -239,5 +233,3 @@ this.emitter.emit(`${event}`, result); | ||
for (const key of subscriptionKeys) { | ||
const subscriptionId: any = await this.subscribe( | ||
this.subscriptions[key].payload, | ||
); | ||
const subscriptionId: any = await this.subscribe(this.subscriptions[key].payload); | ||
delete this.subscriptions[subscriptionId]; | ||
@@ -295,2 +287,2 @@ this.subscriptions[key].id = subscriptionId; | ||
export {WSProvider}; | ||
export { WSProvider }; |
@@ -67,2 +67,4 @@ export const enum RPCMethod { | ||
ProtocolVersion = 'hmy_protocolVersion', | ||
// 32. hmy_getShardingStructure | ||
GetShardingStructure = 'hmy_getShardingStructure', | ||
} | ||
@@ -69,0 +71,0 @@ |
@@ -6,4 +6,4 @@ import { Messenger } from '../messenger/messenger'; | ||
export class LogSub extends SubscriptionMethod { | ||
constructor(options: any, messenger: Messenger) { | ||
super('logs', options, messenger); | ||
constructor(options: any, messenger: Messenger, shardID: number = 0) { | ||
super('logs', options, messenger, shardID); | ||
this.subscribe(); | ||
@@ -18,5 +18,8 @@ } | ||
try { | ||
const getPastLogs = await this.messenger.send(RPCMethod.GetPastLogs, [ | ||
...this.options, | ||
]); | ||
const getPastLogs = await this.messenger.send( | ||
RPCMethod.GetPastLogs, | ||
[...this.options], | ||
this.messenger.chainType, | ||
this.shardID, | ||
); | ||
@@ -23,0 +26,0 @@ if (getPastLogs.isError()) { |
@@ -5,6 +5,6 @@ import { Messenger } from '../messenger/messenger'; | ||
export class NewHeaders extends SubscriptionMethod { | ||
constructor(messenger: Messenger) { | ||
super('newHeads', undefined, messenger); | ||
constructor(messenger: Messenger, shardID: number = 0) { | ||
super('newHeads', undefined, messenger, shardID); | ||
this.start(); | ||
} | ||
} |
@@ -5,6 +5,6 @@ import { Messenger } from '../messenger/messenger'; | ||
export class NewPendingTransactions extends SubscriptionMethod { | ||
constructor(messenger: Messenger) { | ||
super('newPendingTransactions', undefined, messenger); | ||
constructor(messenger: Messenger, shardID: number = 0) { | ||
super('newPendingTransactions', undefined, messenger, shardID); | ||
this.start(); | ||
} | ||
} |
@@ -9,7 +9,9 @@ import { Messenger } from '../messenger/messenger'; | ||
options: any; | ||
shardID: number; | ||
messenger: Messenger; | ||
subscriptionId: any = null; | ||
constructor(param: any, options: any, messenger: Messenger) { | ||
super(messenger.provider.url); | ||
constructor(param: any, options: any, messenger: Messenger, shardID: number = 0) { | ||
super(shardID !== 0 ? messenger.getShardProvider(shardID).url : messenger.provider.url); | ||
this.shardID = shardID; | ||
this.param = param; | ||
@@ -20,7 +22,3 @@ this.options = options; | ||
constructPayload( | ||
method: string, | ||
param: any, | ||
options?: any, | ||
): RPCRequestPayload<any> { | ||
constructPayload(method: string, param: any, options?: any): RPCRequestPayload<any> { | ||
let rpcMethod = method; | ||
@@ -37,13 +35,9 @@ const payload: any = []; | ||
async start() { | ||
const subscribePayload = this.constructPayload( | ||
RPCMethod.Subscribe, | ||
this.param, | ||
this.options, | ||
); | ||
const subscribePayload = this.constructPayload(RPCMethod.Subscribe, this.param, this.options); | ||
try { | ||
const id = await super.subscribe(subscribePayload); | ||
this.subscriptionId = id; | ||
this.on(id, (result: any) => { | ||
const output = this.onNewSubscriptionItem(result); | ||
this.emitter.emit('data', output); | ||
@@ -63,6 +57,3 @@ }); | ||
unsubscribe() { | ||
const unsubscribePayload = this.constructPayload( | ||
RPCMethod.UnSubscribe, | ||
this.subscriptionId, | ||
); | ||
const unsubscribePayload = this.constructPayload(RPCMethod.UnSubscribe, this.subscriptionId); | ||
return super.unsubscribe(unsubscribePayload); | ||
@@ -69,0 +60,0 @@ } |
@@ -6,4 +6,4 @@ import { Messenger } from '../messenger/messenger'; | ||
isSyncing: boolean | null; | ||
constructor(messenger: Messenger) { | ||
super('syncing', undefined, messenger); | ||
constructor(messenger: Messenger, shardID: number = 0) { | ||
super('syncing', undefined, messenger, shardID); | ||
this.isSyncing = null; | ||
@@ -10,0 +10,0 @@ this.start(); |
@@ -62,1 +62,8 @@ import { RPCMethod, RPCErrorCode } from './rpcMethod/rpc'; | ||
} | ||
export interface ShardingItem { | ||
current: boolean; | ||
shardID: number | string; | ||
http: string; | ||
ws: string; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
840399
10520
+ Added@harmony-js/utils@0.1.12(transitive)
- Removed@harmony-js/utils@0.1.6(transitive)
Updated@harmony-js/utils@0.1.12