mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 10:42:16 +00:00
update gateway to new js client
This commit is contained in:
parent
0fa3eac2c4
commit
e8fc89f28a
@ -5,15 +5,15 @@
|
|||||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||||
* Aqua version: 0.9.3
|
* Aqua version: 0.10.1
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import { FluencePeer } from '@fluencelabs/fluence';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
callFunction$$,
|
v5_callFunction as callFunction$$,
|
||||||
registerService$$,
|
v5_registerService as registerService$$,
|
||||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4.js';
|
} from '@fluencelabs/js-client.api';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
@ -23,30 +23,30 @@ export interface LoggerDef {
|
|||||||
}
|
}
|
||||||
export function registerLogger(service: LoggerDef): void;
|
export function registerLogger(service: LoggerDef): void;
|
||||||
export function registerLogger(serviceId: string, service: LoggerDef): void;
|
export function registerLogger(serviceId: string, service: LoggerDef): void;
|
||||||
export function registerLogger(peer: FluencePeer, service: LoggerDef): void;
|
export function registerLogger(peer: IFluenceClient$$, service: LoggerDef): void;
|
||||||
export function registerLogger(peer: FluencePeer, serviceId: string, service: LoggerDef): void;
|
export function registerLogger(peer: IFluenceClient$$, serviceId: string, service: LoggerDef): void;
|
||||||
|
|
||||||
export interface EthCallerDef {
|
export interface EthCallerDef {
|
||||||
eth_call: (uri: string, method: string, jsonArgs: string[], callParams: CallParams$$<'uri' | 'method' | 'jsonArgs'>) => { error: string; success: boolean; value: string; } | Promise<{ error: string; success: boolean; value: string; }>;
|
eth_call: (uri: string, method: string, jsonArgs: string[], callParams: CallParams$$<'uri' | 'method' | 'jsonArgs'>) => { error: string; success: boolean; value: string; } | Promise<{ error: string; success: boolean; value: string; }>;
|
||||||
}
|
}
|
||||||
export function registerEthCaller(serviceId: string, service: EthCallerDef): void;
|
export function registerEthCaller(serviceId: string, service: EthCallerDef): void;
|
||||||
export function registerEthCaller(peer: FluencePeer, serviceId: string, service: EthCallerDef): void;
|
export function registerEthCaller(peer: IFluenceClient$$, serviceId: string, service: EthCallerDef): void;
|
||||||
|
|
||||||
export interface CounterDef {
|
export interface CounterDef {
|
||||||
incrementAndReturn: (callParams: CallParams$$<null>) => number | Promise<number>;
|
incrementAndReturn: (callParams: CallParams$$<null>) => number | Promise<number>;
|
||||||
}
|
}
|
||||||
export function registerCounter(service: CounterDef): void;
|
export function registerCounter(service: CounterDef): void;
|
||||||
export function registerCounter(serviceId: string, service: CounterDef): void;
|
export function registerCounter(serviceId: string, service: CounterDef): void;
|
||||||
export function registerCounter(peer: FluencePeer, service: CounterDef): void;
|
export function registerCounter(peer: IFluenceClient$$, service: CounterDef): void;
|
||||||
export function registerCounter(peer: FluencePeer, serviceId: string, service: CounterDef): void;
|
export function registerCounter(peer: IFluenceClient$$, serviceId: string, service: CounterDef): void;
|
||||||
|
|
||||||
export interface NumOpDef {
|
export interface NumOpDef {
|
||||||
identity: (n: number, callParams: CallParams$$<'n'>) => number | Promise<number>;
|
identity: (n: number, callParams: CallParams$$<'n'>) => number | Promise<number>;
|
||||||
}
|
}
|
||||||
export function registerNumOp(service: NumOpDef): void;
|
export function registerNumOp(service: NumOpDef): void;
|
||||||
export function registerNumOp(serviceId: string, service: NumOpDef): void;
|
export function registerNumOp(serviceId: string, service: NumOpDef): void;
|
||||||
export function registerNumOp(peer: FluencePeer, service: NumOpDef): void;
|
export function registerNumOp(peer: IFluenceClient$$, service: NumOpDef): void;
|
||||||
export function registerNumOp(peer: FluencePeer, serviceId: string, service: NumOpDef): void;
|
export function registerNumOp(peer: IFluenceClient$$, serviceId: string, service: NumOpDef): void;
|
||||||
|
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
@ -63,7 +63,7 @@ export function roundRobinEth(
|
|||||||
): Promise<RoundRobinEthResult>;
|
): Promise<RoundRobinEthResult>;
|
||||||
|
|
||||||
export function roundRobinEth(
|
export function roundRobinEth(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
uris: string[],
|
uris: string[],
|
||||||
method: string,
|
method: string,
|
||||||
jsonArgs: string[],
|
jsonArgs: string[],
|
||||||
@ -80,7 +80,7 @@ export function empty(
|
|||||||
): Promise<EmptyResult>;
|
): Promise<EmptyResult>;
|
||||||
|
|
||||||
export function empty(
|
export function empty(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
config?: {ttl?: number}
|
config?: {ttl?: number}
|
||||||
): Promise<EmptyResult>;
|
): Promise<EmptyResult>;
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ export function randomLoadBalancing(
|
|||||||
): Promise<RandomLoadBalancingResult>;
|
): Promise<RandomLoadBalancingResult>;
|
||||||
|
|
||||||
export function randomLoadBalancing(
|
export function randomLoadBalancing(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
uris: string[],
|
uris: string[],
|
||||||
method: string,
|
method: string,
|
||||||
jsonArgs: string[],
|
jsonArgs: string[],
|
||||||
@ -116,7 +116,7 @@ export function randomLoadBalancingEth(
|
|||||||
): Promise<RandomLoadBalancingEthResult>;
|
): Promise<RandomLoadBalancingEthResult>;
|
||||||
|
|
||||||
export function randomLoadBalancingEth(
|
export function randomLoadBalancingEth(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
uris: string[],
|
uris: string[],
|
||||||
method: string,
|
method: string,
|
||||||
jsonArgs: string[],
|
jsonArgs: string[],
|
||||||
@ -138,7 +138,7 @@ export function roundRobin(
|
|||||||
): Promise<RoundRobinResult>;
|
): Promise<RoundRobinResult>;
|
||||||
|
|
||||||
export function roundRobin(
|
export function roundRobin(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
uris: string[],
|
uris: string[],
|
||||||
method: string,
|
method: string,
|
||||||
jsonArgs: string[],
|
jsonArgs: string[],
|
||||||
@ -160,7 +160,7 @@ export function call(
|
|||||||
): Promise<CallResult>;
|
): Promise<CallResult>;
|
||||||
|
|
||||||
export function call(
|
export function call(
|
||||||
peer: FluencePeer,
|
peer: IFluenceClient$$,
|
||||||
uri: string,
|
uri: string,
|
||||||
method: string,
|
method: string,
|
||||||
jsonArgs: string[],
|
jsonArgs: string[],
|
||||||
|
@ -1,17 +1,19 @@
|
|||||||
|
/* eslint-disable */
|
||||||
|
// @ts-nocheck
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* This file is auto-generated. Do not edit manually: changes may be erased.
|
* This file is auto-generated. Do not edit manually: changes may be erased.
|
||||||
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
* Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|
||||||
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
* If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|
||||||
* Aqua version: 0.9.3
|
* Aqua version: 0.10.1
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
import { FluencePeer } from '@fluencelabs/fluence';
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
callFunction$$,
|
v5_callFunction as callFunction$$,
|
||||||
registerService$$,
|
v5_registerService as registerService$$,
|
||||||
} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4.js';
|
} from '@fluencelabs/js-client.api';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Services
|
// Services
|
||||||
@ -243,8 +245,11 @@ export function roundRobinEth(...args) {
|
|||||||
(xor
|
(xor
|
||||||
(seq
|
(seq
|
||||||
(seq
|
(seq
|
||||||
(call %init_peer_id% ("op" "array_length") [uris] array_length)
|
(seq
|
||||||
(call %init_peer_id% ("math" "rem") [requestNumber array_length] rem)
|
(ap uris uris_to_functor)
|
||||||
|
(ap uris_to_functor.length uris_length)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("math" "rem") [requestNumber uris_length] rem)
|
||||||
)
|
)
|
||||||
(call %init_peer_id% ("logger" "logCall") [uris.$.[rem]!])
|
(call %init_peer_id% ("logger" "logCall") [uris.$.[rem]!])
|
||||||
)
|
)
|
||||||
@ -747,8 +752,11 @@ export function roundRobin(...args) {
|
|||||||
(xor
|
(xor
|
||||||
(seq
|
(seq
|
||||||
(seq
|
(seq
|
||||||
(call %init_peer_id% ("op" "array_length") [uris] array_length)
|
(seq
|
||||||
(call %init_peer_id% ("math" "rem") [requestNumber array_length] rem)
|
(ap uris uris_to_functor)
|
||||||
|
(ap uris_to_functor.length uris_length)
|
||||||
|
)
|
||||||
|
(call %init_peer_id% ("math" "rem") [requestNumber uris_length] rem)
|
||||||
)
|
)
|
||||||
(call %init_peer_id% ("logger" "logCall") [uris.$.[rem]!])
|
(call %init_peer_id% ("logger" "logCall") [uris.$.[rem]!])
|
||||||
)
|
)
|
||||||
@ -997,3 +1005,5 @@ export function call(...args) {
|
|||||||
script
|
script
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* eslint-enable */
|
@ -44,7 +44,7 @@ func roundRobin(uris: []string, method: string, jsonArgs: []string, serviceId: s
|
|||||||
Counter counterServiceId
|
Counter counterServiceId
|
||||||
requestNumber <- Counter.incrementAndReturn()
|
requestNumber <- Counter.incrementAndReturn()
|
||||||
on INIT_PEER_ID:
|
on INIT_PEER_ID:
|
||||||
providerNumber = requestNumber % Op.array_length(uris)
|
providerNumber = requestNumber % uris.length
|
||||||
Logger.logCall(uris[providerNumber])
|
Logger.logCall(uris[providerNumber])
|
||||||
<- callFunc(uris[providerNumber], method, jsonArgs, serviceId)
|
<- callFunc(uris[providerNumber], method, jsonArgs, serviceId)
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -25,6 +25,7 @@
|
|||||||
"@fluencelabs/fluence": "0.28.0"
|
"@fluencelabs/fluence": "0.28.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@fluencelabs/aqua-api": "0.9.3"
|
"@fluencelabs/js-client.node": "0.6.4",
|
||||||
|
"@fluencelabs/js-client.api": "0.11.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import bodyParser from "body-parser";
|
import bodyParser from "body-parser";
|
||||||
import {JSONRPCServer} from "json-rpc-2.0";
|
import {JSONRPCServer} from "json-rpc-2.0";
|
||||||
import {FluencePeer} from "@fluencelabs/fluence";
|
import {Fluence} from '@fluencelabs/js-client.api';
|
||||||
|
import "@fluencelabs/js-client.node"
|
||||||
import {randomLoadBalancingEth, registerCounter, registerLogger, roundRobinEth} from "../aqua-compiled/rpc.js";
|
import {randomLoadBalancingEth, registerCounter, registerLogger, roundRobinEth} from "../aqua-compiled/rpc.js";
|
||||||
import {readArguments} from "./arguments.js";
|
import {readArguments} from "./arguments.js";
|
||||||
import {readConfig} from "./config.js";
|
import {readConfig} from "./config.js";
|
||||||
@ -34,11 +35,11 @@ const route = "/";
|
|||||||
const server = new JSONRPCServer();
|
const server = new JSONRPCServer();
|
||||||
|
|
||||||
// initialize fluence client
|
// initialize fluence client
|
||||||
const fluence = new FluencePeer();
|
await Fluence.connect(config.relay);
|
||||||
await fluence.start({connectTo: config.relay});
|
const peerId = (await Fluence.getClient()).getPeerId()
|
||||||
|
|
||||||
// handler for logger
|
// handler for logger
|
||||||
registerLogger(fluence, {
|
registerLogger({
|
||||||
log: s => {
|
log: s => {
|
||||||
console.log("log: " + s);
|
console.log("log: " + s);
|
||||||
},
|
},
|
||||||
@ -48,7 +49,7 @@ registerLogger(fluence, {
|
|||||||
})
|
})
|
||||||
|
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
registerCounter(fluence, "counter", {
|
registerCounter("counter", {
|
||||||
incrementAndReturn: () => {
|
incrementAndReturn: () => {
|
||||||
counter++;
|
counter++;
|
||||||
console.log("Counter: " + counter)
|
console.log("Counter: " + counter)
|
||||||
@ -57,16 +58,16 @@ registerCounter(fluence, "counter", {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const counterServiceId = config.counterServiceId || 'counter'
|
const counterServiceId = config.counterServiceId || 'counter'
|
||||||
const counterPeerId = config.counterPeerId || fluence.getStatus().peerId
|
const counterPeerId = config.counterPeerId || peerId
|
||||||
|
|
||||||
async function methodHandler(req, method) {
|
async function methodHandler(req, method) {
|
||||||
console.log(`Receiving request '${method}'`);
|
console.log(`Receiving request '${method}'`);
|
||||||
let result;
|
let result;
|
||||||
if (!config.mode || config.mode === "random") {
|
if (!config.mode || config.mode === "random") {
|
||||||
result = await randomLoadBalancingEth(fluence, config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId);
|
result = await randomLoadBalancingEth(config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId);
|
||||||
} else if (config.mode === "round-robin") {
|
} else if (config.mode === "round-robin") {
|
||||||
console.log("peerId: " + fluence.getStatus().peerId)
|
console.log("peerId: " + peerId)
|
||||||
result = await roundRobinEth(fluence, config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId, counterServiceId, counterPeerId,
|
result = await roundRobinEth(config.providers, method, req.map((s) => JSON.stringify(s)), config.serviceId, counterServiceId, counterPeerId,
|
||||||
config.serviceId);
|
config.serviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user