feat: Update Libp2p to latest version. Add standalone bundled JS Client (#239)

This commit is contained in:
Pavel
2023-02-06 15:18:04 +03:00
committed by GitHub
parent 267ebb687f
commit a1265f4d7a
158 changed files with 3854 additions and 5236 deletions

View File

@@ -1,8 +0,0 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false
};

View File

@@ -15,14 +15,12 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "workspace:0.1.0"
"@fluencelabs/js-peer": "workspace:*"
},
"devDependencies": {
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"ts-node": "10.9.1",
"typescript": "4.6.4"
"ts-jest": "28.0.2"
}
}

View File

@@ -1,21 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/",
"baseUrl": ".",
"downlevelIteration": true,
"sourceMap": true,
"inlineSources": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": true
"outDir": "./dist"
},
"exclude": ["node_modules", "dist", "bundle"],
"include": ["src/**/*"]
"exclude": ["node_modules", "dist"]
}

View File

@@ -1,8 +0,0 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false
};

View File

@@ -1,13 +0,0 @@
## Contribute Code
You are welcome to contribute to Fluence.
Things you need to know:
1. You need to **agree to the Contributors License Agreement**. This is a common practice in all major Open Source projects. At the current moment we are unable to accept contributions made on behalf of a company. Only individual contributions will be accepted.
2. **Not all proposed contributions can be accepted**. Some features may e.g. just fit a third-party add-on better. The contribution must fit the overall direction of Fluence and really improve it. The more effort you invest, the better you should clarify in advance whether the contribution fits: the best way would be to just open an issue to discuss the contribution you plan to make.
### Contributor License Agreement
When you contribute, you have to be aware that your contribution is covered by **Apache License 2.0**, but might relicensed under few other software licenses mentioned in the **Contributor License Agreement**.
In particular you need to agree to the [Contributor License Agreement](https://gist.github.com/fluencelabs-org/3f4cbb3cc14c1c0fb9ad99d8f7316ed7). If you agree to its content, you simply have to click on the link posted by the CLA assistant as a comment to the pull request. Click it to check the CLA, then accept it on the following screen if you agree to it. CLA assistant will save this decision for upcoming contributions and will notify you if there is any change to the CLA in the meantime.

View File

@@ -1,6 +0,0 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 10000,
testPathIgnorePatterns: ['dist'],
};

View File

@@ -1,32 +0,0 @@
{
"name": "@fluencelabs/fluence",
"version": "0.28.0",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"scripts": {
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"loglevel": "1.8.1",
"@fluencelabs/keypair": "workspace:0.2.0",
"@fluencelabs/avm": "0.32.1",
"@fluencelabs/marine-js": "0.3.38",
"@fluencelabs/js-peer": "workspace:0.1.0"
},
"devDependencies": {
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"ts-node": "10.9.1",
"typescript": "4.6.4"
}
}

View File

@@ -1,92 +0,0 @@
/*
* 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.
*/
import log, { LogLevelDesc } from 'loglevel';
export { KeyPair } from '@fluencelabs/keypair';
import { FluencePeer, PeerConfig } from '@fluencelabs/js-peer/dist/FluencePeer';
export { PeerStatus } from '@fluencelabs/js-peer/dist/FluencePeer';
export { FluencePeer, PeerConfig } from '@fluencelabs/js-peer/dist/FluencePeer';
export { PeerIdB58, CallParams } from '@fluencelabs/js-peer/dist/commonTypes';
export const setLogLevel = (level: LogLevelDesc) => {
log.setLevel(level);
};
log.setDefaultLevel('WARN');
// @ts-ignore
const defaultPeer = globalThis.defaultPeer;
/**
* Public interface to Fluence JS
*/
export const Fluence = {
/**
* Initializes the default peer: starts the Aqua VM, initializes the default call service handlers
* and (optionally) connect to the Fluence network
* @param config - object specifying peer configuration
*/
start: (config?: PeerConfig): Promise<void> => {
return defaultPeer.start(config);
},
/**
* Un-initializes the default peer: stops all the underlying workflows, stops the Aqua VM
* and disconnects from the Fluence network
*/
stop: (): Promise<void> => {
return defaultPeer.stop();
},
/**
* Get the default peer's status
* @returns Default peer's status
*/
getStatus: () => {
return defaultPeer.getStatus();
},
/**
* Get the default peer instance
* @returns the default peer instance
*/
getPeer: (): FluencePeer => {
return defaultPeer;
},
/**
* Registers marine service within the default Fluence peer from wasm file.
* Following helper functions can be used to load wasm files:
* * loadWasmFromFileSystem
* * loadWasmFromNpmPackage
* * loadWasmFromServer
* @param wasm - buffer with the wasm file for service
* @param serviceId - the service id by which the service can be accessed in aqua
*/
registerMarineService: (wasm: SharedArrayBuffer | Buffer, serviceId: string): Promise<void> => {
return defaultPeer.registerMarineService(wasm, serviceId);
},
/**
* Removes the specified marine service from the default Fluence peer
* @param serviceId - the service id to remove
*/
removeMarineService: (serviceId: string): void => {
defaultPeer.removeMarineService(serviceId);
},
};

View File

@@ -1,2 +0,0 @@
export * from '@fluencelabs/js-peer/dist/builtins/Sig';
export { registerSig } from '@fluencelabs/js-peer/dist/_aqua/services';

View File

@@ -1,21 +0,0 @@
{
"compilerOptions": {
"outDir": "./dist/",
"baseUrl": ".",
"downlevelIteration": true,
"sourceMap": true,
"inlineSources": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": true
},
"exclude": ["node_modules", "dist", "bundle"],
"include": ["src/**/*"]
}

View File

@@ -1,8 +0,0 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false
};

View File

@@ -15,16 +15,12 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "workspace:0.1.0",
"@fluencelabs/marine.deps-loader.node": "workspace:0.1.0",
"@fluencelabs/marine.background-runner": "workspace:0.1.0"
"@fluencelabs/js-peer": "workspace:*"
},
"devDependencies": {
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"ts-node": "10.9.1",
"typescript": "4.6.4"
}
}

View File

@@ -1,21 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/",
"baseUrl": ".",
"downlevelIteration": true,
"sourceMap": true,
"inlineSources": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": true
"outDir": "./dist"
},
"exclude": ["node_modules", "dist", "bundle"],
"include": ["src/**/*"]
"exclude": ["node_modules", "dist"]
}

View File

@@ -9,6 +9,7 @@ lerna-debug.log*
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
bundle/
tmp/
dist
esm

View File

@@ -1,12 +1,6 @@
# Fluence JS
# JS Client web
[![npm](https://img.shields.io/npm/v/@fluencelabs/fluence)](https://www.npmjs.com/package/@fluencelabs/fluence)
Official TypeScript implementation of the Fluence Peer.
## Getting started
To start developing applications with Fluence JS refer to the official [documentation](https://fluence.dev/docs/build/fluence-js/)
This package is a part of FluenceJS, the official implementation of the Fluence Peer in typescript. See the [FluenceJS repo](https://github.com/fluencelabs/fluence-js) for more info
## Contributing

View File

@@ -0,0 +1,126 @@
import * as fs from 'fs';
import * as path from 'path';
import { fromUint8Array } from 'js-base64';
import { build } from 'vite';
import { createRequire } from 'module';
import type { InlineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import * as inject from '@rollup/plugin-inject';
import { replaceCodePlugin } from 'vite-plugin-replace';
const require = createRequire(import.meta.url);
const getWorkerScriptPathOrDie = () => {
const scriptPath = path.resolve('../../core/dist/marine/worker-script/index.js');
if (!fs.existsSync(scriptPath)) {
console.error();
process.exit(1);
}
return scriptPath;
};
const commonConfig = (opts: {
outDir: string;
name: string;
entry: string;
}): InlineConfig & Required<Pick<InlineConfig, 'build'>> => {
return {
mode: 'production',
build: {
minify: 'esbuild',
lib: {
entry: opts.entry,
name: opts.name,
fileName: opts.name,
},
outDir: opts.outDir,
},
base: '',
plugins: [tsconfigPaths()],
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
},
},
};
};
const readAsBase64 = async (filePath: string): Promise<string> => {
const scriptRaw = await fs.promises.readFile(filePath);
const b64 = fromUint8Array(scriptRaw);
return b64;
};
const readWasmFromNpmAsBase64 = (pkg: string, wasmFileName: string): Promise<string> => {
const pkgPath = require.resolve(pkg);
const wasmFilePath = path.join(path.dirname(pkgPath), wasmFileName);
return readAsBase64(wasmFilePath);
};
const buildClient = async () => {
await fs.promises.mkdir('tmp', { recursive: true });
// build worker script
const workerConfig = commonConfig({
outDir: './tmp',
entry: getWorkerScriptPathOrDie(),
name: 'worker-script',
});
workerConfig.build!.rollupOptions = {
plugins: [
inject.default({
Buffer: ['buffer', 'Buffer'],
process: 'process',
}),
],
};
await build(workerConfig);
// build js-client
const jsClientConfig = commonConfig({
outDir: './dist',
entry: './src/index.ts',
name: 'js-client',
});
const workerScriptB64 = await readAsBase64('./tmp/worker-script.umd.cjs');
const avmBase64 = await readWasmFromNpmAsBase64('@fluencelabs/avm', 'avm.wasm');
const marineBase64 = await readWasmFromNpmAsBase64('@fluencelabs/marine-js', 'marine-js.wasm');
jsClientConfig.plugins!.push(
replaceCodePlugin({
replacements: [
{
from: '___worker___',
to: workerScriptB64,
},
{
from: '___avm___',
to: avmBase64,
},
{
from: '___marine___',
to: marineBase64,
},
],
}),
);
await build(jsClientConfig);
// We should exclude the script with type=module because
// - it might be confusing (i.e won't work in browsers that do not support ESM, or if you miss the `type` attribute)
// - there is a problem when using `self.crypto` in web workers
await fs.promises.rm('./dist/js-client.js');
// browsers don't understand `.cjs` extensions, just use `.js`
await fs.promises.rename('./dist/js-client.umd.cjs', './dist/js-client.min.js');
};
buildClient()
.then(() => console.log('Built successfully'))
.catch((err) => console.error('failed', err));

View File

@@ -0,0 +1,39 @@
{
"name": "@fluencelabs/js-client.web.standalone",
"version": "0.1.0",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"type": "module",
"scripts": {
"build": "node --loader ts-node/esm ./build.ts"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "workspace:*",
"buffer": "6.0.3",
"process": "0.11.10"
},
"devDependencies": {
"@fluencelabs/avm": "0.35.3",
"@fluencelabs/marine-js": "0.3.42",
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"js-base64": "3.7.2",
"@rollup/plugin-inject": "5.0.3",
"vite-plugin-replace": "0.1.1",
"vite": "4.0.4",
"vite-tsconfig-paths": "4.0.3"
},
"peerDependencies": {
"esbuild": "0.16.7"
}
}

View File

@@ -0,0 +1,35 @@
import { FluencePeer } from '@fluencelabs/js-peer/dist/js-peer/FluencePeer';
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/js-peer/avm';
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker';
import { checkConnection, marineLogFunction } from '@fluencelabs/js-peer/dist/js-peer/utils';
import { InlinedWorkerLoader, InlinedWasmLoader } from '@fluencelabs/js-peer/dist/marine/deps-loader/common';
export const makeDefaultPeer = () => {
const workerLoader = new InlinedWorkerLoader('___worker___');
const controlModuleLoader = new InlinedWasmLoader('___marine___');
const avmModuleLoader = new InlinedWasmLoader('___avm___');
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader, marineLogFunction);
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader, undefined);
return new FluencePeer(marine, avm);
};
// @ts-ignore
globalThis.defaultPeer = makeDefaultPeer();
// TODO! remove after implementing smoke test (DXJ-253)
// @ts-ignore
window.demo = async () => {
// @ts-ignore
const peer: FluencePeer = globalThis.defaultPeer;
await peer.start({
connectTo: {
multiaddr: '/ip4/127.0.0.1/tcp/4310/ws/p2p/12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
peerId: '12D3KooWKEprYXUXqoV5xSBeyqrWLpQLLH4PXfvVkDJtmcqmh5V3',
},
});
const res = await checkConnection(peer);
console.log('Check connection res', res);
};

View File

@@ -0,0 +1,8 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node"
},
"exclude": ["node_modules", "dist"]
}

View File

@@ -1,8 +0,0 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false
};

View File

@@ -15,16 +15,12 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "workspace:0.1.0",
"@fluencelabs/marine.deps-loader.web": "workspace:0.1.0",
"@fluencelabs/marine.background-runner": "workspace:0.1.0"
"@fluencelabs/js-peer": "workspace:*"
},
"devDependencies": {
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"ts-node": "10.9.1",
"typescript": "4.6.4"
"ts-jest": "28.0.2"
}
}

View File

@@ -1,21 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/",
"baseUrl": ".",
"downlevelIteration": true,
"sourceMap": true,
"inlineSources": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": true
"outDir": "./dist"
},
"exclude": ["node_modules", "dist", "bundle"],
"include": ["src/**/*"]
"exclude": ["node_modules", "dist"]
}

View File

@@ -1,8 +0,0 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 4,
useTabs: false
};

View File

@@ -19,7 +19,6 @@
},
"dependencies": {},
"devDependencies": {
"@types/node": "16.11.59",
"typescript": "4.6.4"
"@types/node": "16.11.59"
}
}

View File

@@ -1,7 +1,7 @@
#! /usr/bin/env node
import fs from 'fs';
import path from 'path';
import * as fs from 'fs';
import * as path from 'path';
const firstArgument = process.argv[2];

View File

@@ -1,21 +1,7 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/",
"baseUrl": ".",
"downlevelIteration": true,
"sourceMap": true,
"inlineSources": true,
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"esModuleInterop": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": true
"outDir": "./dist"
},
"exclude": ["node_modules", "dist", "bundle"],
"include": ["src/**/*"]
"exclude": ["node_modules", "dist"]
}