Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.

Commit 8ddccd3

Browse files
authored
Fix zero to empty object conversion error (#40)
1 parent f14baeb commit 8ddccd3

File tree

7 files changed

+34
-56
lines changed

7 files changed

+34
-56
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__test__/integration/client.spec.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('Typescript usage suite', () => {
241241
client = await createClient();
242242

243243
// act
244-
const res = getPeerExternalAddresses(client);
244+
const res = callIdentifyOnInitPeerId(client);
245245

246246
// assert
247247
await expect(res).rejects.toMatch(
@@ -250,46 +250,16 @@ describe('Typescript usage suite', () => {
250250
});
251251
});
252252

253-
async function getPeerExternalAddresses(client: FluenceClient): Promise<string[]> {
253+
async function callIdentifyOnInitPeerId(client: FluenceClient): Promise<string[]> {
254254
let request;
255255
const promise = new Promise<string[]>((resolve, reject) => {
256256
request = new RequestFlowBuilder()
257257
.withRawScript(
258258
`
259-
(seq
260-
(seq
261-
(call %init_peer_id% ("getDataSrv" "relay") [] relay)
262259
(call %init_peer_id% ("peer" "identify") [] res)
263-
)
264-
(call %init_peer_id% ("callbackSrv" "response") [res.$.external_addresses!])
265-
)
266-
267260
`,
268261
)
269-
.configHandler((h) => {
270-
h.on('getDataSrv', 'relay', () => {
271-
return client.relayPeerId;
272-
});
273-
h.on('getRelayService', 'hasReleay', () => {
274-
// Not Used
275-
return client.relayPeerId !== undefined;
276-
});
277-
278-
h.on('callbackSrv', 'response', (args) => {
279-
const [res] = args;
280-
resolve(res);
281-
});
282-
283-
h.on('nameOfServiceWhereToSendXorError', 'errorProbably', (args) => {
284-
// assuming error is the single argument
285-
const [err] = args;
286-
reject(err);
287-
});
288-
})
289262
.handleScriptError(reject)
290-
.handleTimeout(() => {
291-
reject('Request timed out');
292-
})
293263
.build();
294264
});
295265
await client.initiateFlow(request);

src/__test__/unit/AquaHandler.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ describe('Aqua handler tests', () => {
7171
next();
7272
})
7373
.use((req, res, next) => {
74-
res.result.name = 'john';
74+
(res.result as any).name = 'john';
7575
next();
7676
})
7777
.use((req, res, next) => {
78-
res.result.color = 'red';
78+
(res.result as any).color = 'red';
7979
next();
8080
});
8181

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
import {FluenceConnection} from "../../internal/FluenceConnection";
2-
import Peer from "libp2p";
3-
import Multiaddr = require("multiaddr");
4-
import {generatePeerId} from "../../internal/peerIdUtils";
1+
import { FluenceConnection } from '../../internal/FluenceConnection';
2+
import Peer from 'libp2p';
3+
import Multiaddr = require('multiaddr');
4+
import { generatePeerId } from '../../internal/peerIdUtils';
55

66
describe('Ws Transport', () => {
7-
// TODO: fix this test
7+
// TODO:: fix test
88
test.skip('Should work with ws schema', async () => {
99
// arrange
10-
let multiaddr = new Multiaddr("/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs");
11-
let peerId = await generatePeerId();
12-
const connection = new FluenceConnection(
13-
multiaddr,
14-
peerId,
15-
peerId,
16-
_ => {},
10+
let multiaddr = new Multiaddr(
11+
'/ip4/127.0.0.1/tcp/1234/ws/p2p/12D3KooWMJ78GJrtCxVUpjLEedbPtnLDxkFQJ2wuefEdrxq6zwSs',
1712
);
13+
let peerId = await generatePeerId();
14+
const connection = new FluenceConnection(multiaddr, peerId, peerId, (_) => {});
1815
await (connection as any).createPeer();
1916
let node = (connection as any).node as Peer;
2017

2118
// act
2219
let transport = node.transportManager.transportForMultiaddr(multiaddr);
2320

2421
// assert
25-
expect(transport).not.toBeDefined();
22+
expect(transport).toBeDefined();
2623
});
2724
});

src/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SecurityTetraplet } from './internal/commonTypes';
22
import { RequestFlowBuilder } from './internal/RequestFlowBuilder';
33
import { FluenceClient } from './FluenceClient';
4+
import { AquaResultType } from './internal/AquaHandler';
45

56
/**
67
* 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 = (
7879
client: FluenceClient,
7980
serviceId: string,
8081
fnName: string,
81-
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => object | boolean | number | string,
82+
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
8283
) => {
8384
const unregister = client.aquaCallHandler.on(serviceId, fnName, handler);
8485
handlersUnregistratorsMap.set(makeKey(client, serviceId, fnName), unregister);

src/internal/AquaHandler.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ interface AquaCall {
4343
[x: string]: any;
4444
}
4545

46+
/**
47+
* Type for all the possible ovjects that can be return to the Aquamarine interpreter
48+
*/
49+
export type AquaResultType = object | boolean | number | string;
50+
4651
/**
4752
* Represents the result of the `call` air instruction to be returned into Aquamarine interpreter
4853
*/
@@ -53,9 +58,9 @@ interface AquaCallResult {
5358
retCode: ResultCodes;
5459

5560
/**
56-
* Resul object to be returned to Aquamarine interpreter
61+
* Result object to be returned to Aquamarine interpreter
5762
*/
58-
result?: any;
63+
result: AquaResultType;
5964
[x: string]: any;
6065
}
6166

@@ -80,7 +85,7 @@ export type Middleware = (req: AquaCall, resp: AquaCallResult, next: Function) =
8085
export const fnHandler = (
8186
serviceId: string,
8287
fnName: string,
83-
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => any,
88+
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
8489
) => {
8590
return (req: AquaCall, resp: AquaCallResult, next: Function): void => {
8691
if (req.fnName === fnName && req.serviceId === serviceId) {
@@ -175,7 +180,11 @@ export class AquaCallHandler {
175180
/**
176181
* Convinience method for registring @see { @link fnHandler } middleware
177182
*/
178-
on(serviceId: string, fnName: string, handler: (args: any[], tetraplets: SecurityTetraplet[][]) => any): Function {
183+
on(
184+
serviceId: string,
185+
fnName: string,
186+
handler: (args: any[], tetraplets: SecurityTetraplet[][]) => AquaResultType,
187+
): Function {
179188
const mw = fnHandler(serviceId, fnName, handler);
180189
this.use(mw);
181190
return () => {

src/internal/ClientImpl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ export class ClientImpl implements FluenceClient {
161161
particleId: request.id,
162162
},
163163
});
164+
164165
return {
165166
ret_code: res.retCode,
166-
result: JSON.stringify(res.result || {}),
167+
result: JSON.stringify(res.result),
167168
};
168169
};
169170

0 commit comments

Comments
 (0)