mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-04-25 09:52:12 +00:00
Opt out defaults (#43)
* Change the wat the withDefaults() words. Now it is "disable default injections" instead of "enable default injections"
This commit is contained in:
parent
f9d7a1c875
commit
d84d9fd7ea
@ -123,7 +123,6 @@ export const checkConnection = async (client: FluenceClient, ttl?: number): Prom
|
|||||||
const callbackService = '_callback';
|
const callbackService = '_callback';
|
||||||
|
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`(seq
|
`(seq
|
||||||
(call init_relay ("op" "identity") [msg] result)
|
(call init_relay ("op" "identity") [msg] result)
|
||||||
|
@ -2,6 +2,7 @@ import { checkConnection, createClient, FluenceClient } from '../../FluenceClien
|
|||||||
import Multiaddr from 'multiaddr';
|
import Multiaddr from 'multiaddr';
|
||||||
import { nodes } from '../connection';
|
import { nodes } from '../connection';
|
||||||
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder';
|
import { RequestFlowBuilder } from '../../internal/RequestFlowBuilder';
|
||||||
|
import { error } from 'loglevel';
|
||||||
|
|
||||||
let client: FluenceClient;
|
let client: FluenceClient;
|
||||||
|
|
||||||
@ -19,7 +20,6 @@ describe('Typescript usage suite', () => {
|
|||||||
|
|
||||||
// act
|
// act
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`(seq
|
`(seq
|
||||||
(call init_relay ("op" "identity") ["hello world!"] result)
|
(call init_relay ("op" "identity") ["hello world!"] result)
|
||||||
@ -77,9 +77,7 @@ describe('Typescript usage suite', () => {
|
|||||||
data.set('c', 'some c');
|
data.set('c', 'some c');
|
||||||
data.set('d', 'some d');
|
data.set('d', 'some d');
|
||||||
|
|
||||||
await client1.initiateFlow(
|
await client1.initiateFlow(new RequestFlowBuilder().withRawScript(script).withVariables(data).build());
|
||||||
new RequestFlowBuilder().withDefaults().withRawScript(script).withVariables(data).build(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let res = await resMakingPromise;
|
let res = await resMakingPromise;
|
||||||
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
|
expect(res).toEqual(['some a', 'some b', 'some c', 'some d']);
|
||||||
@ -189,7 +187,6 @@ describe('Typescript usage suite', () => {
|
|||||||
it('xor handling should work with connected client', async function () {
|
it('xor handling should work with connected client', async function () {
|
||||||
// arrange
|
// arrange
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(seq
|
(seq
|
||||||
@ -214,7 +211,6 @@ describe('Typescript usage suite', () => {
|
|||||||
it('xor handling should work with local client', async function () {
|
it('xor handling should work with local client', async function () {
|
||||||
// arrange
|
// arrange
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(call %init_peer_id% ("service" "fails") [])
|
(call %init_peer_id% ("service" "fails") [])
|
||||||
@ -244,9 +240,11 @@ describe('Typescript usage suite', () => {
|
|||||||
const res = callIdentifyOnInitPeerId(client);
|
const res = callIdentifyOnInitPeerId(client);
|
||||||
|
|
||||||
// assert
|
// assert
|
||||||
await expect(res).rejects.toMatch(
|
await expect(res).rejects.toMatchObject({
|
||||||
"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=''",
|
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',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ describe('== AIR suite', () => {
|
|||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(script)
|
.withRawScript(script)
|
||||||
.buildAsFetch<string[]>(serviceId, fnName);
|
.buildAsFetch<string[]>(serviceId, fnName);
|
||||||
|
|
||||||
@ -60,7 +59,6 @@ describe('== AIR suite', () => {
|
|||||||
const script = `(incorrect)`;
|
const script = `(incorrect)`;
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const [request, error] = new RequestFlowBuilder()
|
const [request, error] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(script)
|
.withRawScript(script)
|
||||||
.buildWithErrorHandling();
|
.buildWithErrorHandling();
|
||||||
|
|
||||||
@ -77,7 +75,6 @@ describe('== AIR suite', () => {
|
|||||||
const script = `(null)`;
|
const script = `(null)`;
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withTTL(1)
|
.withTTL(1)
|
||||||
.withRawScript(script)
|
.withRawScript(script)
|
||||||
.buildAsFetch();
|
.buildAsFetch();
|
||||||
@ -99,7 +96,6 @@ describe('== AIR suite', () => {
|
|||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(script)
|
.withRawScript(script)
|
||||||
.withVariable('arg1', 'hello')
|
.withVariable('arg1', 'hello')
|
||||||
.buildAsFetch<string[]>(serviceId, fnName);
|
.buildAsFetch<string[]>(serviceId, fnName);
|
||||||
@ -142,7 +138,7 @@ describe('== AIR suite', () => {
|
|||||||
(call %init_peer_id% ("${makeDataServiceId}" "${makeDataFnName}") [] result)
|
(call %init_peer_id% ("${makeDataServiceId}" "${makeDataFnName}") [] result)
|
||||||
(call %init_peer_id% ("${getDataServiceId}" "${getDataFnName}") [result.$.field])
|
(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
|
// assert
|
||||||
const tetraplet = res.tetraplets[0][0];
|
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% ("${serviceId2}" "${fnName2}") ["${arg2}"] result2))
|
||||||
(call %init_peer_id% ("${serviceId3}" "${fnName3}") [result1 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
|
// assert
|
||||||
expect(res1).toEqual(arg1);
|
expect(res1).toEqual(arg1);
|
||||||
|
@ -45,7 +45,6 @@ export const sendParticle = async (
|
|||||||
onError?: (err) => void,
|
onError?: (err) => void,
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
const [req, errorPromise] = new RequestFlowBuilder()
|
const [req, errorPromise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(particle.script)
|
.withRawScript(particle.script)
|
||||||
.withVariables(particle.data)
|
.withVariables(particle.data)
|
||||||
.withTTL(particle.ttl)
|
.withTTL(particle.ttl)
|
||||||
@ -149,7 +148,6 @@ export const sendParticleAsFetch = async <T>(
|
|||||||
callbackServiceId: string = '_callback',
|
callbackServiceId: string = '_callback',
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(particle.script)
|
.withRawScript(particle.script)
|
||||||
.withVariables(particle.data)
|
.withVariables(particle.data)
|
||||||
.withTTL(particle.ttl)
|
.withTTL(particle.ttl)
|
||||||
|
@ -95,6 +95,10 @@ const wrapWithInjectRelayScript = (script: string): string => {
|
|||||||
* Builder class for configuring and creating Request Flows
|
* Builder class for configuring and creating Request Flows
|
||||||
*/
|
*/
|
||||||
export class RequestFlowBuilder {
|
export class RequestFlowBuilder {
|
||||||
|
private shouldInjectVariables: boolean = true;
|
||||||
|
private shouldInjectErrorHandling: boolean = true;
|
||||||
|
private shouldInjectRelay: boolean = true;
|
||||||
|
|
||||||
private ttl: number = DEFAULT_TTL;
|
private ttl: number = DEFAULT_TTL;
|
||||||
private variables = new Map<string, any>();
|
private variables = new Map<string, any>();
|
||||||
private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = [];
|
private handlerConfigs: Array<(handler: AquaCallHandler, request: RequestFlow) => void> = [];
|
||||||
@ -106,6 +110,16 @@ export class RequestFlowBuilder {
|
|||||||
* Builds the Request flow with current configuration
|
* Builds the Request flow with current configuration
|
||||||
*/
|
*/
|
||||||
build() {
|
build() {
|
||||||
|
if (this.shouldInjectRelay) {
|
||||||
|
this.injectRelay();
|
||||||
|
}
|
||||||
|
if (this.shouldInjectVariables) {
|
||||||
|
this.injectVariables();
|
||||||
|
}
|
||||||
|
if (this.shouldInjectErrorHandling) {
|
||||||
|
this.wrapWithXor();
|
||||||
|
}
|
||||||
|
|
||||||
const sb = new ScriptBuilder();
|
const sb = new ScriptBuilder();
|
||||||
for (let action of this.buildScriptActions) {
|
for (let action of this.buildScriptActions) {
|
||||||
action(sb);
|
action(sb);
|
||||||
@ -129,14 +143,13 @@ export class RequestFlowBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides necessary defaults when building requests by hand without the Aquamarine language compiler
|
* Removes necessary defaults when building requests by hand without the Aquamarine language compiler
|
||||||
* Includes: relay and variable injection, error handling with top-level xor wrap
|
* Removed features include: relay and variable injection, error handling with top-level xor wrap
|
||||||
*/
|
*/
|
||||||
withDefaults(): RequestFlowBuilder {
|
disableInjections(): RequestFlowBuilder {
|
||||||
this.injectRelay();
|
this.shouldInjectRelay = false;
|
||||||
this.injectVariables();
|
this.shouldInjectVariables = false;
|
||||||
this.wrapWithXor();
|
this.shouldInjectErrorHandling = false;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,6 @@ const requestResponse = async <T>(
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
const [request, promise] = new RequestFlowBuilder()
|
const [request, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(script)
|
.withRawScript(script)
|
||||||
.withVariables(data)
|
.withVariables(data)
|
||||||
.withTTL(ttl)
|
.withTTL(ttl)
|
||||||
@ -73,7 +72,6 @@ const requestResponse = async <T>(
|
|||||||
export const getModules = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
export const getModules = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
||||||
let callbackFn = 'getModules';
|
let callbackFn = 'getModules';
|
||||||
const [req, promise] = new RequestFlowBuilder()
|
const [req, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(seq
|
(seq
|
||||||
@ -102,7 +100,6 @@ export const getModules = async (client: FluenceClient, ttl?: number): Promise<s
|
|||||||
export const getInterfaces = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
export const getInterfaces = async (client: FluenceClient, ttl?: number): Promise<string[]> => {
|
||||||
let callbackFn = 'getInterfaces';
|
let callbackFn = 'getInterfaces';
|
||||||
const [req, promise] = new RequestFlowBuilder()
|
const [req, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(seq
|
(seq
|
||||||
@ -169,7 +166,6 @@ export const uploadModule = async (
|
|||||||
data.set('myPeerId', client.selfPeerId);
|
data.set('myPeerId', client.selfPeerId);
|
||||||
|
|
||||||
const [req, promise] = new RequestFlowBuilder()
|
const [req, promise] = new RequestFlowBuilder()
|
||||||
.withDefaults()
|
|
||||||
.withRawScript(
|
.withRawScript(
|
||||||
`
|
`
|
||||||
(seq
|
(seq
|
||||||
|
Loading…
x
Reference in New Issue
Block a user