diff --git a/package.json b/package.json index db02511e..c6f875c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fluence", - "version": "0.7.68", + "version": "0.7.77", "description": "the browser js-libp2p client for the Fluence network", "main": "./dist/fluence.js", "typings": "./dist/fluence.d.ts", diff --git a/src/aqua/scripts.ts b/src/aqua/scripts.ts new file mode 100644 index 00000000..10251dd7 --- /dev/null +++ b/src/aqua/scripts.ts @@ -0,0 +1,123 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Experimental attempts to generate aqua code through typescript functions. + */ +export interface Value { + name: string, + value: T +} + +export function value(name: string, v: T): Value { + return { name, value: v } +} + +function updateData(value: Value, data: Map): void { + if (!data.get(value.name)) { + data.set(value.name, value.value) + } +} + +function isValue(value: string | Value): value is Value { + return (value as Value).name !== undefined; +} + +/** + * Generate a script with a call. Data is modified. + * @param target + * @param service + * @param functionV + * @param args + * @param returnName + * @param data + */ +export function call(target: string | Value, service: string | Value, + functionV: string | Value, args: (string | Value)[], + returnName: string | undefined, data: Map): string { + + let targetName = target; + if (isValue(target)) { + updateData(target, data) + targetName = target.name + } + + let serviceName = service; + if (isValue(service)) { + updateData(service, data) + serviceName = service.name; + } + + let functionName = functionV; + if (isValue(functionV)) { + updateData(functionV, data) + functionName = functionV.name; + } + + let argsStr: string[] = [] + args.forEach((v) => { + if (isValue(v)) { + updateData(v, data) + argsStr.push(v.name) + } else { + argsStr.push(v) + } + }) + + if (!returnName) { + returnName = "" + } + + return `(call ${targetName} ("${serviceName}" "${functionName}") [${argsStr.join(" ")}] ${returnName})` +} + +function wrap(command: string, scripts: string[]): string { + if (scripts.length === 2) { + return `(${command} + ${scripts[0]} + ${scripts[1]} +)` + } else { + let first = scripts.shift() + return `(${command} + ${first} + ${wrap(command, scripts)} +)` + } +} + +/** + * Wrap an array of scripts with multiple 'seq' commands + * @param scripts + */ +export function seq(scripts: string[]): string { + if (scripts.length < 2) { + throw new Error("For 'seq' there must be at least 2 scripts") + } + + return wrap("seq", scripts) +} + +/** + * Wrap a script with 'par' command + * @param script + */ +export function par(script: string): string { + return `par( + ${script} +) + ` +} diff --git a/src/fluence.ts b/src/fluence.ts index b311f8d7..feb9ef93 100644 --- a/src/fluence.ts +++ b/src/fluence.ts @@ -41,7 +41,12 @@ export default class Fluence { * @param multiaddr must contain host peer id * @param peerId your peer id. Should be with a private key. Could be generated by `generatePeerId()` function */ - static async connect(multiaddr: string | Multiaddr, peerId: PeerId): Promise { + static async connect(multiaddr: string | Multiaddr, peerId?: PeerId): Promise { + + if (!peerId) { + peerId = await Fluence.generatePeerId() + } + let client = new FluenceClient(peerId); await client.connect(multiaddr); diff --git a/src/fluenceConnection.ts b/src/fluenceConnection.ts index 1ba7a2a0..109739c0 100644 --- a/src/fluenceConnection.ts +++ b/src/fluenceConnection.ts @@ -23,7 +23,7 @@ import pipe from "it-pipe"; import Multiaddr from "multiaddr"; import PeerId from "peer-id"; import * as log from 'loglevel'; -import {parseParticle, Particle, stringifyParticle} from "./particle"; +import {build, parseParticle, Particle, stringifyParticle} from "./particle"; export const PROTOCOL_NAME = '/fluence/faas/1.0.0'; @@ -119,6 +119,10 @@ export class FluenceConnection { this.status = Status.Disconnected; } + async buildParticle(script: string, data: Map, ttl?: number): Promise { + return build(this.selfPeerId, script, data, ttl) + } + async sendParticle(particle: Particle): Promise { this.checkConnectedOrThrow();