From 1c0b06f85e97cac70a30fa474f2010eda451a70c Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Wed, 21 Apr 2021 18:41:06 +0300 Subject: [PATCH 1/3] Replace withDefaults() logic to withoutDefaults --- src/FluenceClient.ts | 1 - src/__test__/integration/client.spec.ts | 7 +------ src/__test__/unit/air.spec.ts | 8 ++------ src/api.ts | 2 -- src/internal/RequestFlowBuilder.ts | 27 ++++++++++++++++++------- src/internal/builtins.ts | 4 ---- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/FluenceClient.ts b/src/FluenceClient.ts index 45a7c04ee..495c07d95 100644 --- a/src/FluenceClient.ts +++ b/src/FluenceClient.ts @@ -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) diff --git a/src/__test__/integration/client.spec.ts b/src/__test__/integration/client.spec.ts index 675d72357..5ab3c61da 100644 --- a/src/__test__/integration/client.spec.ts +++ b/src/__test__/integration/client.spec.ts @@ -19,7 +19,6 @@ describe('Typescript usage suite', () => { // act const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript( `(seq (call init_relay ("op" "identity") ["hello world!"] result) @@ -77,9 +76,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']); @@ -189,7 +186,6 @@ describe('Typescript usage suite', () => { it('xor handling should work with connected client', async function () { // arrange const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript( ` (seq @@ -214,7 +210,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") []) diff --git a/src/__test__/unit/air.spec.ts b/src/__test__/unit/air.spec.ts index 22a147798..75663fe90 100644 --- a/src/__test__/unit/air.spec.ts +++ b/src/__test__/unit/air.spec.ts @@ -19,7 +19,6 @@ describe('== AIR suite', () => { // prettier-ignore const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript(script) .buildAsFetch(serviceId, fnName); @@ -60,7 +59,6 @@ describe('== AIR suite', () => { const script = `(incorrect)`; // prettier-ignore const [request, error] = new RequestFlowBuilder() - .withDefaults() .withRawScript(script) .buildWithErrorHandling(); @@ -77,7 +75,6 @@ describe('== AIR suite', () => { const script = `(null)`; // prettier-ignore const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withTTL(1) .withRawScript(script) .buildAsFetch(); @@ -99,7 +96,6 @@ describe('== AIR suite', () => { // prettier-ignore const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript(script) .withVariable('arg1', 'hello') .buildAsFetch(serviceId, fnName); @@ -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]; @@ -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); diff --git a/src/api.ts b/src/api.ts index ef9da0d4a..cffe12734 100644 --- a/src/api.ts +++ b/src/api.ts @@ -45,7 +45,6 @@ export const sendParticle = async ( onError?: (err) => void, ): Promise => { const [req, errorPromise] = new RequestFlowBuilder() - .withDefaults() .withRawScript(particle.script) .withVariables(particle.data) .withTTL(particle.ttl) @@ -149,7 +148,6 @@ export const sendParticleAsFetch = async ( callbackServiceId: string = '_callback', ): Promise => { const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript(particle.script) .withVariables(particle.data) .withTTL(particle.ttl) diff --git a/src/internal/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index acb34de2d..64551e073 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -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 shouldWrapWithXor: boolean = true; + private shouldInjectRelay: boolean = true; + private ttl: number = DEFAULT_TTL; private variables = new Map(); private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = []; @@ -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.shouldWrapWithXor) { + this.wrapWithXor(); + } + const sb = new ScriptBuilder(); for (let action of this.buildScriptActions) { action(sb); @@ -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(); - + withOutDefaults(): RequestFlowBuilder { + this.shouldInjectRelay = false; + this.shouldInjectVariables = false; + this.shouldWrapWithXor = false; return this; } diff --git a/src/internal/builtins.ts b/src/internal/builtins.ts index 4b8707384..391aa729a 100644 --- a/src/internal/builtins.ts +++ b/src/internal/builtins.ts @@ -55,7 +55,6 @@ const requestResponse = async ( `; const [request, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript(script) .withVariables(data) .withTTL(ttl) @@ -73,7 +72,6 @@ const requestResponse = async ( export const getModules = async (client: FluenceClient, ttl?: number): Promise => { let callbackFn = 'getModules'; const [req, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript( ` (seq @@ -102,7 +100,6 @@ export const getModules = async (client: FluenceClient, ttl?: number): Promise => { let callbackFn = 'getInterfaces'; const [req, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript( ` (seq @@ -169,7 +166,6 @@ export const uploadModule = async ( data.set('myPeerId', client.selfPeerId); const [req, promise] = new RequestFlowBuilder() - .withDefaults() .withRawScript( ` (seq From 4632016e7a69f3de4c4f6cd450fb288eec2f4d00 Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Wed, 21 Apr 2021 18:49:56 +0300 Subject: [PATCH 2/3] fix test --- src/__test__/integration/client.spec.ts | 9 ++++++--- src/internal/RequestFlowBuilder.ts | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/__test__/integration/client.spec.ts b/src/__test__/integration/client.spec.ts index 5ab3c61da..12b5f7ea4 100644 --- a/src/__test__/integration/client.spec.ts +++ b/src/__test__/integration/client.spec.ts @@ -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; @@ -239,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', + }); }); }); diff --git a/src/internal/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index 64551e073..b3934bc95 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -146,7 +146,7 @@ export class RequestFlowBuilder { * 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 */ - withOutDefaults(): RequestFlowBuilder { + withoutDefaults(): RequestFlowBuilder { this.shouldInjectRelay = false; this.shouldInjectVariables = false; this.shouldWrapWithXor = false; From 89db40012823536f38d5d3697d27f24d6a2f6ffc Mon Sep 17 00:00:00 2001 From: Pavel Murygin Date: Wed, 21 Apr 2021 19:40:42 +0300 Subject: [PATCH 3/3] Rename function accroding to comments --- src/internal/RequestFlowBuilder.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/internal/RequestFlowBuilder.ts b/src/internal/RequestFlowBuilder.ts index b3934bc95..092e3b582 100644 --- a/src/internal/RequestFlowBuilder.ts +++ b/src/internal/RequestFlowBuilder.ts @@ -96,7 +96,7 @@ const wrapWithInjectRelayScript = (script: string): string => { */ export class RequestFlowBuilder { private shouldInjectVariables: boolean = true; - private shouldWrapWithXor: boolean = true; + private shouldInjectErrorHandling: boolean = true; private shouldInjectRelay: boolean = true; private ttl: number = DEFAULT_TTL; @@ -116,7 +116,7 @@ export class RequestFlowBuilder { if (this.shouldInjectVariables) { this.injectVariables(); } - if (this.shouldWrapWithXor) { + if (this.shouldInjectErrorHandling) { this.wrapWithXor(); } @@ -146,10 +146,10 @@ export class RequestFlowBuilder { * 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 */ - withoutDefaults(): RequestFlowBuilder { + disableInjections(): RequestFlowBuilder { this.shouldInjectRelay = false; this.shouldInjectVariables = false; - this.shouldWrapWithXor = false; + this.shouldInjectErrorHandling = false; return this; }