mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 09:52:12 +00:00
Fix zero to empty object conversion error (#40)
This commit is contained in:
parent
f14baeb618
commit
8ddccd3c60
6
package-lock.json
generated
6
package-lock.json
generated
@ -435,9 +435,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@fluencelabs/aquamarine-interpreter": {
|
"@fluencelabs/aquamarine-interpreter": {
|
||||||
"version": "0.8.0",
|
"version": "0.7.9",
|
||||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aquamarine-interpreter/-/aquamarine-interpreter-0.8.0.tgz",
|
"resolved": "https://registry.npmjs.org/@fluencelabs/aquamarine-interpreter/-/aquamarine-interpreter-0.7.9.tgz",
|
||||||
"integrity": "sha512-EoHqReMk6gqcBR8ZJuurXbCyB3t6vrOjsEBX5XgVhdfSYrUvmBvmKy4Elq5ONY2JVec9HCR/n/SfTuxYsnY8PQ=="
|
"integrity": "sha512-VXbHm0d05XMjTSzOTcb+spVRrIuMcrw8/3dl197wH0jx1C3Wou+vAapQLvGNcKzqDhktPOOzJTE4UARYd0lFMw=="
|
||||||
},
|
},
|
||||||
"@istanbuljs/load-nyc-config": {
|
"@istanbuljs/load-nyc-config": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
|
@ -241,7 +241,7 @@ describe('Typescript usage suite', () => {
|
|||||||
client = await createClient();
|
client = await createClient();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
const res = getPeerExternalAddresses(client);
|
const res = callIdentifyOnInitPeerId(client);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
await expect(res).rejects.toMatch(
|
await expect(res).rejects.toMatch(
|
||||||
@ -250,46 +250,16 @@ describe('Typescript usage suite', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
async function getPeerExternalAddresses(client: FluenceClient): Promise<string[]> {
|
async function callIdentifyOnInitPeerId(client: FluenceClient): Promise<string[]> {
|
||||||
let request;
|
let request;
|
||||||
const promise = new Promise<string[]>((resolve, reject) => {
|
const promise = new Promise<string[]>((resolve, reject) => {
|
||||||
request = new RequestFlowBuilder()
|
request = new RequestFlowBuilder()
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(seq
|
|
||||||
(seq
|
|
||||||
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
|
|
||||||
(call %init_peer_id% ("peer" "identify") [] res)
|
(call %init_peer_id% ("peer" "identify") [] res)
|
||||||
)
|
|
||||||
(call %init_peer_id% ("callbackSrv" "response") [res.$.external_addresses!])
|
|
||||||
)
|
|
||||||
|
|
||||||
`,
|
`,
|
||||||
)
|
)
|
||||||
.configHandler((h) => {
|
|
||||||
h.on('getDataSrv', 'relay', () => {
|
|
||||||
return client.relayPeerId;
|
|
||||||
});
|
|
||||||
h.on('getRelayService', 'hasReleay', () => {
|
|
||||||
// Not Used
|
|
||||||
return client.relayPeerId !== undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
h.on('callbackSrv', 'response', (args) => {
|
|
||||||
const [res] = args;
|
|
||||||
resolve(res);
|
|
||||||
});
|
|
||||||
|
|
||||||
h.on('nameOfServiceWhereToSendXorError', 'errorProbably', (args) => {
|
|
||||||
// assuming error is the single argument
|
|
||||||
const [err] = args;
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.handleScriptError(reject)
|
.handleScriptError(reject)
|
||||||
.handleTimeout(() => {
|
|
||||||
reject('Request timed out');
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
});
|
});
|
||||||
await client.initiateFlow(request);
|
await client.initiateFlow(request);
|
||||||
|
@ -71,11 +71,11 @@ describe('Aqua handler tests', () => {
|
|||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
.use((req, res, next) => {
|
.use((req, res, next) => {
|
||||||
res.result.name = 'john';
|
(res.result as any).name = 'john';
|
||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
.use((req, res, next) => {
|
.use((req, res, next) => {
|
||||||
res.result.color = 'red';
|
(res.result as any).color = 'red';
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,20 +1,17 @@
|
|||||||
import {FluenceConnection} from "../../internal/FluenceConnection";
|
import { FluenceConnection } from '../../internal/FluenceConnection';
|
||||||
import Peer from "libp2p";
|
import Peer from 'libp2p';
|
||||||
import Multiaddr = require("multiaddr");
|
import Multiaddr = require('multiaddr');
|
||||||
import {generatePeerId} from "../../internal/peerIdUtils";
|
import { generatePeerId } from '../../internal/peerIdUtils';
|
||||||
|
|
||||||
describe('Ws Transport', () => {
|
describe('Ws Transport', () => {
|
||||||
// TODO: fix this test
|
// TODO:: fix test
|
||||||
test.skip('Should work with ws schema', async () => {
|
test.skip('Should work with ws schema', async () => {
|
||||||
// arrange
|
// arrange
|
||||||
let multiaddr = new Multiaddr("/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs");
|
let multiaddr = new Multiaddr(
|
||||||
let peerId = await generatePeerId();
|
'/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs',
|
||||||
const connection = new FluenceConnection(
|
|
||||||
multiaddr,
|
|
||||||
peerId,
|
|
||||||
peerId,
|
|
||||||
_ => {},
|
|
||||||
);
|
);
|
||||||
|
let peerId = await generatePeerId();
|
||||||
|
const connection = new FluenceConnection(multiaddr, peerId, peerId, (_) => {});
|
||||||
await (connection as any).createPeer();
|
await (connection as any).createPeer();
|
||||||
let node = (connection as any).node as Peer;
|
let node = (connection as any).node as Peer;
|
||||||
|
|
||||||
@ -22,6 +19,6 @@ describe('Ws Transport', () => {
|
|||||||
let transport = node.transportManager.transportForMultiaddr(multiaddr);
|
let transport = node.transportManager.transportForMultiaddr(multiaddr);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
expect(transport).not.toBeDefined();
|
expect(transport).toBeDefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { SecurityTetraplet } from './internal/commonTypes';
|
import { SecurityTetraplet } from './internal/commonTypes';
|
||||||
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
|
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
|
||||||
import { FluenceClient } from './FluenceClient';
|
import { FluenceClient } from './FluenceClient';
|
||||||
|
import { AquaResultType } from './internal/AquaHandler';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class representing Particle - a data structure used to perform operations on Fluence Network. It originates on some peer in the network, travels the network through a predefined path, triggering function execution along its way.
|
* The class representing Particle - a data structure used to perform operations on Fluence Network. It originates on some peer in the network, travels the network through a predefined path, triggering function execution along its way.
|
||||||
@ -78,7 +79,7 @@ export const registerServiceFunction = (
|
|||||||
client: FluenceClient,
|
client: FluenceClient,
|
||||||
serviceId: string,
|
serviceId: string,
|
||||||
fnName: string,
|
fnName: string,
|
||||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => object | boolean | number | string,
|
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||||
) => {
|
) => {
|
||||||
const unregister = client.aquaCallHandler.on(serviceId, fnName, handler);
|
const unregister = client.aquaCallHandler.on(serviceId, fnName, handler);
|
||||||
handlersUnregistratorsMap.set(makeKey(client, serviceId, fnName), unregister);
|
handlersUnregistratorsMap.set(makeKey(client, serviceId, fnName), unregister);
|
||||||
|
@ -43,6 +43,11 @@ interface AquaCall {
|
|||||||
[x: string]: any;
|
[x: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type for all the possible ovjects that can be return to the Aquamarine interpreter
|
||||||
|
*/
|
||||||
|
export type AquaResultType = object | boolean | number | string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the result of the `call` air instruction to be returned into Aquamarine interpreter
|
* Represents the result of the `call` air instruction to be returned into Aquamarine interpreter
|
||||||
*/
|
*/
|
||||||
@ -53,9 +58,9 @@ interface AquaCallResult {
|
|||||||
retCode: ResultCodes;
|
retCode: ResultCodes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resul object to be returned to Aquamarine interpreter
|
* Result object to be returned to Aquamarine interpreter
|
||||||
*/
|
*/
|
||||||
result?: any;
|
result: AquaResultType;
|
||||||
[x: string]: any;
|
[x: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,7 +85,7 @@ export type Middleware = (req: AquaCall, resp: AquaCallResult, next: Function) =
|
|||||||
export const fnHandler = (
|
export const fnHandler = (
|
||||||
serviceId: string,
|
serviceId: string,
|
||||||
fnName: string,
|
fnName: string,
|
||||||
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => any,
|
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||||
) => {
|
) => {
|
||||||
return (req: AquaCall, resp: AquaCallResult, next: Function): void => {
|
return (req: AquaCall, resp: AquaCallResult, next: Function): void => {
|
||||||
if (req.fnName === fnName && req.serviceId === serviceId) {
|
if (req.fnName === fnName && req.serviceId === serviceId) {
|
||||||
@ -175,7 +180,11 @@ export class AquaCallHandler {
|
|||||||
/**
|
/**
|
||||||
* Convinience method for registring @see { @link fnHandler } middleware
|
* Convinience method for registring @see { @link fnHandler } middleware
|
||||||
*/
|
*/
|
||||||
on(serviceId: string, fnName: string, handler: (args: any[], tetraplets: SecurityTetraplet[][]) => any): Function {
|
on(
|
||||||
|
serviceId: string,
|
||||||
|
fnName: string,
|
||||||
|
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
|
||||||
|
): Function {
|
||||||
const mw = fnHandler(serviceId, fnName, handler);
|
const mw = fnHandler(serviceId, fnName, handler);
|
||||||
this.use(mw);
|
this.use(mw);
|
||||||
return () => {
|
return () => {
|
||||||
|
@ -161,9 +161,10 @@ export class ClientImpl implements FluenceClient {
|
|||||||
particleId: request.id,
|
particleId: request.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ret_code: res.retCode,
|
ret_code: res.retCode,
|
||||||
result: JSON.stringify(res.result || {}),
|
result: JSON.stringify(res.result),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user