Skip to content
This repository was archived by the owner on Jul 10, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/FluenceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export const checkConnection = async (client: FluenceClient, ttl?: number): Prom
const callbackService = '_callback';

const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`(seq
(call init_relay ("op" "identity") [msg] result)
Expand Down
16 changes: 7 additions & 9 deletions src/__test__/integration/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { checkConnection, createClient, FluenceClient } from '../../FluenceClien
import Multiaddr from 'multiaddr';
import { nodes } from '../connection';
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder';
import { error } from 'loglevel';

let client: FluenceClient;

Expand All @@ -19,7 +20,6 @@ describe('Typescript usage suite', () => {

// act
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`(seq
(call init_relay ("op" "identity") ["hello world!"] result)
Expand Down Expand Up @@ -77,9 +77,7 @@ describe('Typescript usage suite', () => {
data.set('c', 'some c');
data.set('d', 'some d');

await client1.initiateFlow(
new RequestFlowBuilder().withDefaults().withRawScript(script).withVariables(data).build(),
);
await client1.initiateFlow(new RequestFlowBuilder().withRawScript(script).withVariables(data).build());

let res = await resMakingPromise;
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
Expand Down Expand Up @@ -189,7 +187,6 @@ describe('Typescript usage suite', () => {
it('xor handling should work with connected client', async function () {
// arrange
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
Expand All @@ -214,7 +211,6 @@ describe('Typescript usage suite', () => {
it('xor handling should work with local client', async function () {
// arrange
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(call %init_peer_id% ("service" "fails") [])
Expand Down Expand Up @@ -244,9 +240,11 @@ describe('Typescript usage suite', () => {
const res = callIdentifyOnInitPeerId(client);

// assert
await expect(res).rejects.toMatch(
"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''",
);
await expect(res).rejects.toMatchObject({
error:
"Local service error: ret_code is 1024, error message is '\"The handler did not set any result. Make sure you are calling the right peer and the handler has been registered. Original request data was: serviceId='peer' fnName='identify' args=''\"'",
instruction: 'call %init_peer_id% ("peer" "identify") [] res',
});
});
});

Expand Down
8 changes: 2 additions & 6 deletions src/__test__/unit/air.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('== AIR suite', () => {

// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.buildAsFetch<string[]>(serviceId, fnName);

Expand Down Expand Up @@ -60,7 +59,6 @@ describe('== AIR suite', () => {
const script = `(incorrect)`;
// prettier-ignore
const [request, error] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.buildWithErrorHandling();

Expand All @@ -77,7 +75,6 @@ describe('== AIR suite', () => {
const script = `(null)`;
// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withTTL(1)
.withRawScript(script)
.buildAsFetch();
Expand All @@ -99,7 +96,6 @@ describe('== AIR suite', () => {

// prettier-ignore
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.withVariable('arg1', 'hello')
.buildAsFetch<string[]>(serviceId, fnName);
Expand Down Expand Up @@ -142,7 +138,7 @@ describe('== AIR suite', () => {
(call %init_peer_id% ("${makeDataServiceId}" "${makeDataFnName}") [] result)
(call %init_peer_id% ("${getDataServiceId}" "${getDataFnName}") [result.$.field])
)`;
await client.initiateFlow(new RequestFlowBuilder().withDefaults().withRawScript(script).build());
await client.initiateFlow(new RequestFlowBuilder().withRawScript(script).build());

// assert
const tetraplet = res.tetraplets[0][0];
Expand Down Expand Up @@ -191,7 +187,7 @@ describe('== AIR suite', () => {
(call %init_peer_id% ("${serviceId2}" "${fnName2}") ["${arg2}"] result2))
(call %init_peer_id% ("${serviceId3}" "${fnName3}") [result1 result2]))
`;
await client.initiateFlow(new RequestFlowBuilder().withDefaults().withRawScript(script).build());
await client.initiateFlow(new RequestFlowBuilder().withRawScript(script).build());

// assert
expect(res1).toEqual(arg1);
Expand Down
2 changes: 0 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const sendParticle = async (
onError?: (err) => void,
): Promise<string> => {
const [req, errorPromise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(particle.script)
.withVariables(particle.data)
.withTTL(particle.ttl)
Expand Down Expand Up @@ -149,7 +148,6 @@ export const sendParticleAsFetch = async <T>(
callbackServiceId: string = '_callback',
): Promise<T> => {
const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(particle.script)
.withVariables(particle.data)
.withTTL(particle.ttl)
Expand Down
27 changes: 20 additions & 7 deletions src/internal/RequestFlowBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ const wrapWithInjectRelayScript = (script: string): string => {
* Builder class for configuring and creating Request Flows
*/
export class RequestFlowBuilder {
private shouldInjectVariables: boolean = true;
private shouldInjectErrorHandling: boolean = true;
private shouldInjectRelay: boolean = true;

private ttl: number = DEFAULT_TTL;
private variables = new Map<string, any>();
private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = [];
Expand All @@ -106,6 +110,16 @@ export class RequestFlowBuilder {
* Builds the Request flow with current configuration
*/
build() {
if (this.shouldInjectRelay) {
this.injectRelay();
}
if (this.shouldInjectVariables) {
this.injectVariables();
}
if (this.shouldInjectErrorHandling) {
this.wrapWithXor();
}

const sb = new ScriptBuilder();
for (let action of this.buildScriptActions) {
action(sb);
Expand All @@ -129,14 +143,13 @@ export class RequestFlowBuilder {
}

/**
* Provides necessary defaults when building requests by hand without the Aquamarine language compiler
* Includes: relay and variable injection, error handling with top-level xor wrap
* Removes necessary defaults when building requests by hand without the Aquamarine language compiler
* Removed features include: relay and variable injection, error handling with top-level xor wrap
*/
withDefaults(): RequestFlowBuilder {
this.injectRelay();
this.injectVariables();
this.wrapWithXor();

disableInjections(): RequestFlowBuilder {
this.shouldInjectRelay = false;
this.shouldInjectVariables = false;
this.shouldInjectErrorHandling = false;
return this;
}

Expand Down
4 changes: 0 additions & 4 deletions src/internal/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const requestResponse = async <T>(
`;

const [request, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(script)
.withVariables(data)
.withTTL(ttl)
Expand All @@ -73,7 +72,6 @@ const requestResponse = async <T>(
export const getModules = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
let callbackFn = 'getModules';
const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
Expand Down Expand Up @@ -102,7 +100,6 @@ export const getModules = async (client: FluenceClient, ttl?: number): Promise<s
export const getInterfaces = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
let callbackFn = 'getInterfaces';
const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
Expand Down Expand Up @@ -169,7 +166,6 @@ export const uploadModule = async (
data.set('myPeerId', client.selfPeerId);

const [req, promise] = new RequestFlowBuilder()
.withDefaults()
.withRawScript(
`
(seq
Expand Down