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