diff --git a/package-lock.json b/package-lock.json index c5e4f0a5..bff848cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "fluence", - "version": "0.7.6", + "version": "0.7.9", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -4511,10 +4511,9 @@ } }, "loglevel": { - "version": "1.6.8", - "resolved": "https://registry.npmjs.org/loglevel/-/loglevel-1.6.8.tgz", - "integrity": "sha512-bsU7+gc9AJ2SqpzxwU3+1fedl8zAntbtC5XYlt3s2j1hJcn2PsXSmgN8TaLG/J1/2mod4+cE/3vNL70/c1RNCA==", - "dev": true + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/loglevel/-/loglevel-1.7.0.tgz", + "integrity": "sha512-i2sY04nal5jDcagM3FMfG++T69GEEM8CYuOfeOIvmXzOIcwE9a/CJPR0MFM97pYMj/u10lzz7/zd7+qwhrBTqQ==" }, "lru-cache": { "version": "5.1.1", diff --git a/package.json b/package.json index 81a93634..656ac0e8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fluence", - "version": "0.7.7", + "version": "0.7.11", "description": "the browser js-libp2p client for the Fluence network", "main": "./dist/fluence.js", "typings": "./dist/fluence.d.ts", @@ -16,6 +16,7 @@ "license": "Apache 2.0", "dependencies": { "async": "3.2.0", + "loglevel": "1.7.0", "base64-js": "1.3.1", "bs58": "4.0.1", "cids": "0.8.1", diff --git a/src/fluence.ts b/src/fluence.ts index a850d61f..f6f74805 100644 --- a/src/fluence.ts +++ b/src/fluence.ts @@ -17,10 +17,17 @@ import * as PeerId from "peer-id"; import Multiaddr from "multiaddr" import {FluenceClient} from "./fluenceClient"; +import * as log from "loglevel"; +import {LogLevelDesc} from "loglevel"; +log.setLevel('info') export default class Fluence { + setLogLevel(level: LogLevelDesc): void { + log.setLevel(level); + } + /** * Generates new peer id with Ed25519 private key. */ diff --git a/src/fluenceClient.ts b/src/fluenceClient.ts index 4797892c..cecbb23d 100644 --- a/src/fluenceClient.ts +++ b/src/fluenceClient.ts @@ -31,6 +31,7 @@ import {FluenceConnection} from "./fluenceConnection"; import {checkInterface, Interface} from "./Interface"; import {Service} from "./service"; import {Blueprint, checkBlueprint} from "./blueprint"; +import * as log from 'loglevel'; /** * @param target receiver @@ -180,7 +181,7 @@ export class FluenceClient { let _this = this; return (call: FunctionCall) => { - console.log("FunctionCall received:"); + log.debug("FunctionCall received:"); // if other side return an error - handle it // TODO do it in the protocol @@ -205,8 +206,8 @@ export class FluenceClient { return undefined; case ProtocolType.Client: if (lastProtocol.value === _this.selfPeerIdStr) { - console.log(`relay call:`); - console.log(JSON.stringify(call, undefined, 2)); + log.debug(`relay call:`); + log.debug(JSON.stringify(call, undefined, 2)); if (call.module) { try { // call of the service, service should handle response sending, error handling, requests to other services @@ -214,7 +215,7 @@ export class FluenceClient { // if the request hasn't been applied, there is no such service. Return an error. if (!applied) { - console.log(`there is no service ${lastProtocol.value}`); + log.warn(`there is no service ${lastProtocol.value}`); return this.responseCall(call.reply_to, { reason: `there is no such service`, msg: call @@ -229,7 +230,7 @@ export class FluenceClient { } } } else { - console.warn(`this relay call is not for me: ${callToString(call)}`); + log.warn(`this relay call is not for me: ${callToString(call)}`); return this.responseCall(call.reply_to, { reason: `this relay call is not for me`, msg: call @@ -238,9 +239,9 @@ export class FluenceClient { return undefined; case ProtocolType.Peer: if (lastProtocol.value === this.selfPeerIdStr) { - console.log(`peer call: ${call}`); + log.debug(`peer call: ${call}`); } else { - console.warn(`this peer call is not for me: ${callToString(call)}`); + log.warn(`this peer call is not for me: ${callToString(call)}`); return this.responseCall(call.reply_to, { reason: `this relay call is not for me`, msg: call @@ -265,18 +266,18 @@ export class FluenceClient { /** * Sends a call to create a service on remote node. */ - async createService(peerId: string, blueprint: string): Promise { + async createService(blueprint: string, peerId?: string): Promise { let resp = await this.callPeer("create", {blueprint_id: blueprint}, undefined, peerId); if (resp && resp.service_id) { return resp.service_id } else { - console.error("Unknown response type on `createService`: ", resp) + log.error("Unknown response type on `createService`: ", resp) throw new Error("Unknown response type on `createService`"); } } - async addBlueprint(peerId: string, name: string, dependencies: string[]): Promise { + async addBlueprint(name: string, dependencies: string[], peerId?: string): Promise { let id = genUUID(); let blueprint = { @@ -382,7 +383,7 @@ export class FluenceClient { */ async unregisterService(moduleId: string) { if (this.services.deleteService(moduleId)) { - console.warn("unregister is not implemented yet (service: ${serviceId}") + log.warn("unregister is not implemented yet (service: ${serviceId}") // TODO unregister in fluence network when it will be supported // let regMsg = makeRegisterMessage(serviceId, PeerId.createFromB58String(this.nodePeerId)); // await this.sendFunctionCall(regMsg); diff --git a/src/fluenceConnection.ts b/src/fluenceConnection.ts index e7597855..e1cdb68d 100644 --- a/src/fluenceConnection.ts +++ b/src/fluenceConnection.ts @@ -32,6 +32,7 @@ import {decode, encode} from "it-length-prefixed"; import pipe from "it-pipe"; import Multiaddr from "multiaddr"; import PeerId from "peer-id"; +import * as log from 'loglevel'; export const PROTOCOL_NAME = '/fluence/faas/1.0.0'; @@ -97,7 +98,7 @@ export class FluenceConnection { if (this.status === Status.Initializing) { await this.node.start(); - console.log("dialing to the node with address: " + this.node.peerId.toB58String()); + log.debug("dialing to the node with address: " + this.node.peerId.toB58String()); await this.node.dial(this.address); @@ -110,7 +111,7 @@ export class FluenceConnection { async function (source: AsyncIterable) { for await (const msg of source) { try { - console.log(_this.selfPeerIdStr); + log.debug(_this.selfPeerIdStr); let call = parseFunctionCall(msg); let response = _this.handleCall(call); @@ -119,7 +120,7 @@ export class FluenceConnection { await _this.sendCall(response); } } catch(e) { - console.log("error on handling a new incoming message: " + e); + log.error("error on handling a new incoming message: " + e); } } } @@ -145,8 +146,8 @@ export class FluenceConnection { private async sendCall(call: FunctionCall) { let callStr = callToString(call); - console.log("send function call: " + JSON.stringify(JSON.parse(callStr), undefined, 2)); - console.log(call); + log.debug("send function call: " + JSON.stringify(JSON.parse(callStr), undefined, 2)); + log.debug(call); // create outgoing substream const conn = await this.node.dialProtocol(this.address, PROTOCOL_NAME) as {stream: Stream; protocol: string}; diff --git a/src/functionCall.ts b/src/functionCall.ts index 7a9f322a..ed8f1c76 100644 --- a/src/functionCall.ts +++ b/src/functionCall.ts @@ -18,6 +18,7 @@ import { Address, addressToString, parseAddress } from "./address"; import { v4 as uuidv4 } from 'uuid'; +import * as log from 'loglevel'; export interface FunctionCall { uuid: string, @@ -61,7 +62,7 @@ export function makeFunctionCall(uuid: string, target: Address, sender: Address, export function parseFunctionCall(str: string): FunctionCall { let json = JSON.parse(str); - console.log(JSON.stringify(json, undefined, 2)); + log.debug(JSON.stringify(json, undefined, 2)); let replyTo: Address; if (json.reply_to) replyTo = parseAddress(json.reply_to); diff --git a/src/test/address.spec.ts b/src/test/address.spec.ts index 489da4dc..a68e0c0a 100644 --- a/src/test/address.spec.ts +++ b/src/test/address.spec.ts @@ -182,6 +182,9 @@ export async function testCerts() { expect(certs[0].chain[1].signature).to.be.equal(extended.chain[1].signature) expect(certs[0].chain[1].expiresAt).to.be.equal(extended.chain[1].expiresAt) expect(certs[0].chain[1].issuedAt).to.be.equal(extended.chain[1].issuedAt) + + await cl1.disconnect(); + await cl2.disconnect(); } export async function testUploadWasm() { @@ -196,7 +199,7 @@ export async function testUploadWasm() { let peerId1 = "12D3KooWPnLxnY71JDxvB3zbjKu9k1BCYNthGZw6iGrLYsR1RnWM" - let blueprintId = await cl1.addBlueprint(peerId1, "some test blueprint", [moduleName]) + let blueprintId = await cl1.addBlueprint("some test blueprint", [moduleName], peerId1) let blueprints = await cl1.getAvailableBlueprints(peerId1) console.log(blueprints); @@ -208,6 +211,8 @@ export async function testUploadWasm() { let resp = await service.call(moduleName, {name: argName}, "greeting") expect(resp.result).to.be.equal(`Hi, ${argName}`) + + await cl1.disconnect(); } export async function testServicesAndInterfaces() { @@ -220,7 +225,7 @@ export async function testServicesAndInterfaces() { let peerId1 = "12D3KooWPnLxnY71JDxvB3zbjKu9k1BCYNthGZw6iGrLYsR1RnWM" - let blueprintId = await cl1.addBlueprint(peerId1, "some test blueprint", ["ipfs_node"]) + let blueprintId = await cl1.addBlueprint("some test blueprint", ["ipfs_node"], peerId1) let serviceId = await cl2.createService(peerId1, blueprintId); let resp = await cl2.callService(peerId1, serviceId, "ipfs_node", {}, "get_address") @@ -234,6 +239,9 @@ export async function testServicesAndInterfaces() { let availableModules = await cl1.getAvailableModules(peerId1); console.log(availableModules); + + await cl1.disconnect(); + await cl2.disconnect(); } // Shows how to register and call new service in Fluence network @@ -282,6 +290,9 @@ export async function testProvide() { let result2 = await response2.result; console.log("RESULT:"); console.log(response2); - expect(result2).to.be.equal(35) + expect(result2).to.be.equal(35); + + await cl1.disconnect(); + await cl2.disconnect(); } diff --git a/src/trust/trust_graph.ts b/src/trust/trust_graph.ts index be01dcfb..458bb36d 100644 --- a/src/trust/trust_graph.ts +++ b/src/trust/trust_graph.ts @@ -16,6 +16,7 @@ import {FluenceClient} from "../fluenceClient"; import {Certificate, certificateFromString, certificateToString} from "./certificate"; +import * as log from 'loglevel'; // The client to interact with the Fluence trust graph API export class TrustGraph { @@ -56,7 +57,7 @@ export class TrustGraph { let certificatesRaw = resp.certificates if (!(certificatesRaw && Array.isArray(certificatesRaw))) { - console.log(Array.isArray(certificatesRaw)) + log.error(Array.isArray(certificatesRaw)) throw Error("Unexpected. Certificates should be presented in the response as an array.") }