mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-30 16:21:36 +00:00
'sender' field in FunctionCall (#908)
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "fluence",
|
||||
"version": "0.5.4",
|
||||
"version": "0.5.5",
|
||||
"description": "the browser js-libp2p client for the Fluence network",
|
||||
"main": "./dist/fluence.js",
|
||||
"typings": "./dist/fluence.d.ts",
|
||||
|
@ -41,8 +41,8 @@ export class FluenceClient {
|
||||
/**
|
||||
* Makes call with response from function. Without reply_to field.
|
||||
*/
|
||||
private static responseCall(target: Address, args: any): FunctionCall {
|
||||
return makeFunctionCall(genUUID(), target, args, undefined, "response");
|
||||
private responseCall(target: Address, args: any): FunctionCall {
|
||||
return makeFunctionCall(genUUID(), target, this.connection.sender, args, undefined, "response");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -155,14 +155,14 @@ export class FluenceClient {
|
||||
// if the request hasn't been applied, there is no such service. Return an error.
|
||||
if (!applied) {
|
||||
console.log(`there is no service ${lastProtocol.value}`);
|
||||
return FluenceClient.responseCall(call.reply_to, {
|
||||
return this.responseCall(call.reply_to, {
|
||||
reason: `there is no such service`,
|
||||
msg: call
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
// if service throw an error, return it to the sender
|
||||
return FluenceClient.responseCall(call.reply_to, {
|
||||
return this.responseCall(call.reply_to, {
|
||||
reason: `error on execution: ${e}`,
|
||||
msg: call
|
||||
});
|
||||
@ -174,7 +174,7 @@ export class FluenceClient {
|
||||
console.log(`relay call: ${call}`);
|
||||
} else {
|
||||
console.warn(`this relay call is not for me: ${callToString(call)}`);
|
||||
return FluenceClient.responseCall(call.reply_to, {
|
||||
return this.responseCall(call.reply_to, {
|
||||
reason: `this relay call is not for me`,
|
||||
msg: call
|
||||
});
|
||||
@ -185,7 +185,7 @@ export class FluenceClient {
|
||||
console.log(`peer call: ${call}`);
|
||||
} else {
|
||||
console.warn(`this peer call is not for me: ${callToString(call)}`);
|
||||
return FluenceClient.responseCall(call.reply_to, {
|
||||
return this.responseCall(call.reply_to, {
|
||||
reason: `this relay call is not for me`,
|
||||
msg: call
|
||||
});
|
||||
|
@ -47,7 +47,7 @@ enum Status {
|
||||
export class FluenceConnection {
|
||||
|
||||
private readonly selfPeerInfo: PeerInfo;
|
||||
readonly replyToAddress: Address;
|
||||
readonly sender: Address;
|
||||
private node: LibP2p;
|
||||
private readonly address: Multiaddr;
|
||||
private readonly nodePeerId: PeerId;
|
||||
@ -60,7 +60,7 @@ export class FluenceConnection {
|
||||
this.selfPeerId = selfPeerInfo.id.toB58String();
|
||||
this.address = multiaddr;
|
||||
this.nodePeerId = hostPeerId;
|
||||
this.replyToAddress = replyToAddress
|
||||
this.sender = replyToAddress
|
||||
}
|
||||
|
||||
async connect() {
|
||||
@ -90,7 +90,7 @@ export class FluenceConnection {
|
||||
* Sends remote service_id call.
|
||||
*/
|
||||
async sendServiceCall(serviceId: string, args: any, name?: string) {
|
||||
let regMsg = makeCall(serviceId, args, this.replyToAddress, name);
|
||||
let regMsg = makeCall(serviceId, args, this.sender, this.sender, name);
|
||||
await this.sendCall(regMsg);
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ export class FluenceConnection {
|
||||
* Sends custom message to the peer.
|
||||
*/
|
||||
async sendPeerCall(peer: string, msg: any, name?: string) {
|
||||
let regMsg = makePeerCall(PeerId.createFromB58String(peer), msg, this.replyToAddress, name);
|
||||
let regMsg = makePeerCall(PeerId.createFromB58String(peer), msg, this.sender, this.sender, name);
|
||||
await this.sendCall(regMsg);
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ export class FluenceConnection {
|
||||
* Sends custom message to the peer through relay.
|
||||
*/
|
||||
async sendRelayCall(peer: string, relay: string, msg: any, name?: string) {
|
||||
let regMsg = await makeRelayCall(PeerId.createFromB58String(peer), PeerId.createFromB58String(relay), msg, this.replyToAddress, name);
|
||||
let regMsg = await makeRelayCall(PeerId.createFromB58String(peer), PeerId.createFromB58String(relay), msg, this.sender, this.sender, name);
|
||||
await this.sendCall(regMsg);
|
||||
}
|
||||
|
||||
@ -184,9 +184,9 @@ export class FluenceConnection {
|
||||
this.checkConnectedOrThrow();
|
||||
|
||||
let replyTo;
|
||||
if (reply) replyTo = this.replyToAddress;
|
||||
if (reply) replyTo = this.sender;
|
||||
|
||||
let call = makeFunctionCall(genUUID(), target, args, replyTo, name);
|
||||
let call = makeFunctionCall(genUUID(), target, args, this.sender, replyTo, name);
|
||||
|
||||
await this.sendCall(call);
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ export interface FunctionCall {
|
||||
uuid: string,
|
||||
target: Address,
|
||||
reply_to?: Address,
|
||||
sender: Address,
|
||||
arguments: any,
|
||||
name?: string,
|
||||
action: "FunctionCall"
|
||||
@ -43,12 +44,13 @@ export function callToString(call: FunctionCall) {
|
||||
return JSON.stringify(obj)
|
||||
}
|
||||
|
||||
export function makeFunctionCall(uuid: string, target: Address, args: object, replyTo?: Address, name?: string): FunctionCall {
|
||||
export function makeFunctionCall(uuid: string, target: Address, sender: Address, args: object, replyTo?: Address, name?: string): FunctionCall {
|
||||
|
||||
return {
|
||||
uuid: uuid,
|
||||
target: target,
|
||||
reply_to: replyTo,
|
||||
sender: sender,
|
||||
arguments: args,
|
||||
name: name,
|
||||
action: "FunctionCall"
|
||||
@ -64,13 +66,16 @@ export function parseFunctionCall(str: string): FunctionCall {
|
||||
|
||||
if (!json.uuid) throw Error(`there is no 'uuid' field in json.\n${str}`);
|
||||
if (!json.target) throw Error(`there is no 'uuid' field in json.\n${str}`);
|
||||
if (!json.sender) throw Error(`there is no 'sender' field in json.\n${str}`);
|
||||
|
||||
let target = parseAddress(json.target);
|
||||
let sender = parseAddress(json.sender);
|
||||
|
||||
return {
|
||||
uuid: json.uuid,
|
||||
target: target,
|
||||
reply_to: replyTo,
|
||||
sender: sender,
|
||||
arguments: json.arguments,
|
||||
name: json.name,
|
||||
action: "FunctionCall"
|
||||
@ -85,28 +90,28 @@ export function genUUID() {
|
||||
/**
|
||||
* Message to peer through relay
|
||||
*/
|
||||
export async function makeRelayCall(client: PeerId, relay: PeerId, msg: any, replyTo?: Address, name?: string): Promise<FunctionCall> {
|
||||
export async function makeRelayCall(client: PeerId, relay: PeerId, msg: any, sender: Address, replyTo?: Address, name?: string): Promise<FunctionCall> {
|
||||
let relayAddress = await createRelayAddress(relay.toB58String(), client, false);
|
||||
|
||||
return makeFunctionCall(genUUID(), relayAddress, msg, replyTo, name);
|
||||
return makeFunctionCall(genUUID(), relayAddress, sender, msg, replyTo, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Message to peer
|
||||
*/
|
||||
export function makePeerCall(client: PeerId, msg: any, replyTo?: Address, name?: string): FunctionCall {
|
||||
export function makePeerCall(client: PeerId, msg: any, sender: Address, replyTo?: Address, name?: string): FunctionCall {
|
||||
let peerAddress = createPeerAddress(client.toB58String());
|
||||
|
||||
return makeFunctionCall(genUUID(), peerAddress, msg, replyTo, name);
|
||||
return makeFunctionCall(genUUID(), peerAddress, msg, sender, replyTo, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Message to call remote service_id
|
||||
*/
|
||||
export function makeCall(functionId: string, args: any, replyTo?: Address, name?: string): FunctionCall {
|
||||
export function makeCall(functionId: string, args: any, sender: Address, replyTo?: Address, name?: string): FunctionCall {
|
||||
let target = createServiceAddress(functionId);
|
||||
|
||||
return makeFunctionCall(genUUID(), target, args, replyTo, name);
|
||||
return makeFunctionCall(genUUID(), target, args, sender, replyTo, name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -116,11 +121,12 @@ export async function makeRegisterMessage(serviceId: string, relayPeerId: PeerId
|
||||
let target = createServiceAddress("provide");
|
||||
let replyTo = await createRelayAddress(relayPeerId.toB58String(), selfPeerId, true);
|
||||
|
||||
return makeFunctionCall(genUUID(), target, {service_id: serviceId}, replyTo, "provide service_id");
|
||||
return makeFunctionCall(genUUID(), target, replyTo, {service_id: serviceId}, replyTo, "provide service_id");
|
||||
}
|
||||
|
||||
export function makeUnregisterMessage(serviceId: string, peerId: PeerId): FunctionCall {
|
||||
// TODO uncomment when this will be implemented in Fluence network
|
||||
/*export function makeUnregisterMessage(serviceId: string, peerId: PeerId): FunctionCall {
|
||||
let target = createPeerAddress(peerId.toB58String());
|
||||
|
||||
return makeFunctionCall(genUUID(), target, {key: serviceId}, undefined, "unregister");
|
||||
}
|
||||
return makeFunctionCall(genUUID(), target, target, {key: serviceId}, undefined, "unregister");
|
||||
}*/
|
||||
|
@ -61,6 +61,7 @@ describe("Typescript usage suite", () => {
|
||||
let functionCall = makeFunctionCall(
|
||||
"123",
|
||||
addr2,
|
||||
addr2,
|
||||
{
|
||||
arg1: "123",
|
||||
arg2: 3,
|
||||
@ -79,6 +80,7 @@ describe("Typescript usage suite", () => {
|
||||
let functionCallWithOptional = makeFunctionCall(
|
||||
"123",
|
||||
addr,
|
||||
addr,
|
||||
{
|
||||
arg1: "123",
|
||||
arg2: 3,
|
||||
|
Reference in New Issue
Block a user