feat!: Unify all packages (#327)

* * Separate marine worker as a package
* Trying to fix tests

* Finalizing test fixes

* fix: rename back to Fluence CLI (#320)

chore: rename back to Fluence CLI

* fix(deps): update dependency @fluencelabs/avm to v0.43.1 (#322)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* chore: release master (#324)

* chore: release master

* chore: Regenerate pnpm lock file

* feat: use marine-js 0.7.2 (#321)

* use marine-js 0.5.0

* increace some timeouts

* increace some timeouts

* use latest marine + remove larger timeouts

* propagate CallParameters type

* use marine 0.7.2

* Temp use node 18 and 20

* Comment out node 20.x

---------

Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev>

* chore: Fix test with node 18/20 error message (#323)

* Fix test with node 18/20 error message

* Run tests on node 18 and 20

* Enhance description

* Fix type and obj property

---------

Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev>

* * Separate marine worker as a package
* Trying to fix tests

* Finalizing test fixes

* * Refactoring packages.
* Using CDN to load .wasm deps.
* Setting up tests for new architecture

* Fix almost all tests

* Fix last strange test

* Remove package specific packages

* Remove avm class as it looks excessive

* marine worker new version

* misc refactoring/remove console.log's

* Rename package js-peer to js-client

* Move service info to marine worker

* Change CDN path

* Fix worker race confition

* Remove buffer type

* Remove turned off headless mode in platform tests

* Remove async keyword to make tests pass

* Remove util package

* Make js-client.api package just reexport interface from js-client main package

* Update package info in CI

* Fix review comments

* Remove test entry from marine-worker package

* Misc fixes

* Fix worker type

* Add fetchers

* Specify correct versions for js-client package

* Set first ver for js-client

* Update libp2p and related dep versions to the latest

* Build all deps into package itself

* Fix review

* Refine package

* Fix comment

* Update packages/core/js-client/src/fetchers/browser.ts

* Update packages/core/js-client/src/fetchers/index.ts

* Update packages/core/js-client/src/fetchers/node.ts

* Update packages/core/js-client/src/jsPeer/FluencePeer.ts

* Update packages/core/js-client/src/keypair/__test__/KeyPair.spec.ts

* Update packages/core/js-client/src/jsPeer/FluencePeer.ts

Co-authored-by: shamsartem <shamsartem@gmail.com>

* Delete outdated file

* Need types for build to work

* Inline func call

* Add comments to replacement lines.
P.S. we can remove some of them after update libp2p

---------

Co-authored-by: shamsartem <shamsartem@gmail.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: fluencebot <116741523+fluencebot@users.noreply.github.com>
Co-authored-by: Valery Antopol <valery.antopol@gmail.com>
Co-authored-by: Anatoly Laskaris <github_me@nahsi.dev>
This commit is contained in:
Akim 2023-08-25 00:15:49 +07:00 committed by GitHub
parent 2d2f5591cf
commit 97c24918d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
130 changed files with 3350 additions and 3119 deletions

View File

@ -9,11 +9,8 @@
],
"packages": {
"packages/client/api": {},
"packages/client/js-client.node": {},
"packages/client/js-client.web": {},
"packages/client/js-client.web.standalone": {},
"packages/client/tools": {},
"packages/core/js-client": {},
"packages/core/interfaces": {},
"packages/core/js-peer": {}
"packages/core/marine-worker": {}
}
}

View File

@ -1,9 +1,6 @@
{
"packages/client/api": "0.12.1",
"packages/client/js-client.node": "0.7.1",
"packages/client/js-client.web": "0.2.5",
"packages/client/js-client.web.standalone": "0.14.1",
"packages/client/tools": "0.2.1",
"packages/core/interfaces": "0.8.1",
"packages/core/js-peer": "0.9.1"
"packages/core/js-client": "0.0.10",
"packages/core/marine-worker": "0.2.10",
"packages/core/interfaces": "0.8.1"
}

View File

@ -51,4 +51,4 @@ jobs:
uses: fluencelabs/flox/.github/workflows/tests.yml@main
with:
js-client-snapshots: "${{ needs.js-client.outputs.js-client-snapshots }}"
nox-image: "fluencelabs/nox:unstable_minimal"
nox-image: "fluencelabs/nox:unstable_minimal"

View File

@ -18,6 +18,7 @@
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-client.api": "workspace:^",
"@fluencelabs/js-client": "workspace:^",
"base64-js": "1.5.1"
},
"devDependencies": {

View File

@ -1,6 +1,6 @@
import { fromByteArray } from 'base64-js';
import { Fluence } from '@fluencelabs/js-client.api';
import type { ClientConfig } from '@fluencelabs/js-client.api';
import { Fluence } from '@fluencelabs/js-client';
import type { ClientConfig } from '@fluencelabs/js-client';
import { registerHelloWorld, helloTest, marineTest, resourceTest } from './_aqua/smoke_test.js';
import { wasm } from './wasmb64.js';

View File

@ -17,9 +17,7 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-client.api": "workspace:^",
"@fluencelabs/js-client.node": "workspace:^",
"@test/aqua_for_test": "workspace:^"
},
"devDependencies": {}
"@fluencelabs/js-client": "workspace:*",
"@test/aqua_for_test": "workspace:../../aqua"
}
}

View File

@ -1,4 +1,4 @@
import '@fluencelabs/js-client.node';
import '@fluencelabs/js-client';
import { runTest } from '@test/aqua_for_test';
runTest().then(() => console.log('Smoke tests succeed!'));

View File

@ -7,7 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<script src='http://localhost:8766/js-client.min.js' async></script>
<!-- Ideally we want to use 'async' here. Currently, it's not supported. -->
<script src="js-client.min.js"></script>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a

View File

@ -2,7 +2,8 @@ import puppeteer from 'puppeteer';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { startCdn, startContentServer, stopServer } from '@test/test-utils';
import { CDN_PUBLIC_PATH, startContentServer, stopServer } from '@test/test-utils';
import { access, symlink } from 'fs/promises';
const port = 3001;
const uri = `http://localhost:${port}/`;
@ -10,12 +11,16 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const publicPath = join(__dirname, '../build/');
const test = async () => {
const cdn = await startCdn(8766);
const localServer = await startContentServer(port, publicPath);
try {
await access(join(publicPath, 'source'))
} catch {
await symlink(CDN_PUBLIC_PATH, join(publicPath, 'source'));
}
console.log('starting puppeteer...');
const browser = await puppeteer.launch();
const page = await browser.newPage();
const browser = await puppeteer.launch({ headless: false });
const page = (await browser.pages())[0];
// uncomment to debug what's happening inside the browser
// page.on('console', (msg) => console.log('// from console: ', msg.text()));
@ -34,7 +39,6 @@ const test = async () => {
console.log('raw result: ', content);
await browser.close();
await stopServer(cdn);
await stopServer(localServer);
if (!content) {

View File

@ -19,8 +19,8 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-client.web.standalone": "workspace:^",
"@test/test-utils": "workspace:^"
"@fluencelabs/js-client": "workspace:^",
"@test/test-utils": "workspace:../../test-utils"
},
"devDependencies": {}
}

View File

@ -16,8 +16,8 @@
<div id="res-placeholder"></div>
<script src='http://localhost:8765/js-client.min.js'></script>
<script src="./index.js"></script>
<script src="js-client.min.js"></script>
<script src="index.js"></script>
</main>
</body>

View File

@ -84,7 +84,7 @@ const getRelayTime = () => {
const main = async () => {
console.log('starting fluence...');
await fluence.defaultClient.connect(relay);
fluence.defaultClient = await fluence.clientFactory(relay);
console.log('started fluence');
console.log('getting relay time...');
@ -104,7 +104,7 @@ btn.addEventListener('click', () => {
main().then((res) => {
const inner = document.createElement('div');
inner.id = 'res';
inner.innerText = 'res';
inner.innerText = res;
document.getElementById('res-placeholder').appendChild(inner);
});
});

View File

@ -2,7 +2,8 @@ import puppeteer from 'puppeteer';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { startCdn, startContentServer, stopServer } from '@test/test-utils';
import { CDN_PUBLIC_PATH, startCdn, startContentServer, stopServer } from '@test/test-utils';
import { symlink, access } from 'fs/promises';
const port = 3000;
const uri = `http://localhost:${port}/`;
@ -10,12 +11,16 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const publicPath = join(__dirname, '../public/');
const test = async () => {
const cdn = await startCdn(8765);
const localServer = await startContentServer(port, publicPath);
try {
await access(join(publicPath, 'source'))
} catch {
await symlink(CDN_PUBLIC_PATH, join(publicPath, 'source'));
}
console.log('starting puppeteer...');
const browser = await puppeteer.launch();
const page = await browser.newPage();
const page = (await browser.pages())[0];
// uncomment to debug what's happening inside the browser
// page.on('console', (msg) => console.log('// from console: ', msg.text()));
@ -34,7 +39,6 @@ const test = async () => {
console.log('raw result: ', content);
await browser.close();
await stopServer(cdn);
await stopServer(localServer);
if (!content) {

View File

@ -16,8 +16,6 @@
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-client.api": "workspace:^",
"@fluencelabs/js-client.node": "workspace:^",
"serve-handler": "6.1.5"
},
"devDependencies": {

View File

@ -7,7 +7,7 @@ import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const CDN_PUBLIC_PATH = join(__dirname, '../../../client/js-client.web.standalone/dist/');
export const CDN_PUBLIC_PATH = join(__dirname, '../../../core/js-client/dist/browser');
export const startCdn = (port: number) => startContentServer(port, CDN_PUBLIC_PATH);
@ -15,6 +15,17 @@ export const startContentServer = (port: number, publicDir: string): Promise<Ser
const server = createServer((request, response) => {
return handler(request, response, {
public: publicDir,
rewrites: [{
source: '/js-client.min.js',
destination: '/source/index.umd.cjs'
}],
headers: [{
source: '**/*',
headers: [
{ key: 'Cross-Origin-Opener-Policy', value: 'same-origin' },
{ key: 'Cross-Origin-Embedder-Policy', value: 'require-corp' }
]
}]
});
});

View File

@ -2,6 +2,11 @@
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
## Notes
This package just reexports everything from main library for backward compatibility
`export * from '@fluencelabs/js-client';`
## Contributing
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).

View File

@ -24,13 +24,14 @@
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/interfaces": "0.8.1"
},
"devDependencies": {
"@fluencelabs/avm": "0.43.1",
"@fluencelabs/interfaces": "workspace:*",
"@fluencelabs/marine-js": "0.7.2",
"microbundle": "0.15.1",
"dts-bundle-generator": "7.2.0"
"dts-bundle-generator": "7.2.0",
"microbundle": "0.15.1"
},
"dependencies": {
"@fluencelabs/js-client": "0.0.10"
}
}

View File

@ -13,120 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getFluenceInterface, getFluenceInterfaceFromGlobalThis } from './util.js';
import {
IFluenceClient,
ClientConfig,
RelayOptions,
ConnectionState,
CallAquaFunctionType,
RegisterServiceType,
} from '@fluencelabs/interfaces';
export type { IFluenceClient, ClientConfig, CallParams } from '@fluencelabs/interfaces';
export {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
CallAquaFunctionType,
CallAquaFunctionArgs,
PassedArgs,
FnConfig,
RegisterServiceType,
RegisterServiceArgs,
} from '@fluencelabs/interfaces';
export { v5_callFunction, v5_registerService } from './compilerSupport/implementation.js';
/**
* Public interface to Fluence Network
*/
export const Fluence = {
/**
* Connect to the Fluence network
* @param relay - relay node to connect to
* @param config - client configuration
*/
connect: async (relay: RelayOptions, config?: ClientConfig): Promise<void> => {
const fluence = await getFluenceInterface();
const client = await fluence.clientFactory(relay, config);
fluence.defaultClient = client;
},
/**
* Disconnect from the Fluence network
*/
disconnect: async (): Promise<void> => {
const fluence = await getFluenceInterface();
await fluence.defaultClient?.disconnect();
fluence.defaultClient = undefined;
},
/**
* Handle connection state changes. Immediately returns the current connection state
*/
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState {
const optimisticResult = getFluenceInterfaceFromGlobalThis();
if (optimisticResult && optimisticResult.defaultClient) {
return optimisticResult.defaultClient.onConnectionStateChange(handler);
}
getFluenceInterface().then((fluence) => {
fluence.defaultClient?.onConnectionStateChange(handler);
});
return 'disconnected';
},
/**
* Low level API. Get the underlying client instance which holds the connection to the network
* @returns IFluenceClient instance
*/
getClient: async (): Promise<IFluenceClient> => {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
throw new Error('Fluence client is not initialized. Call Fluence.connect() first');
}
return fluence.defaultClient;
},
};
/**
* Low level API. Generally you need Fluence.connect() instead.
* @returns IFluenceClient instance
*/
export const createClient = async (relay: RelayOptions, config?: ClientConfig): Promise<IFluenceClient> => {
const fluence = await getFluenceInterface();
return await fluence.clientFactory(relay, config);
};
/**
* Low level API. Generally you should use code generated by the Aqua compiler.
*/
export const callAquaFunction: CallAquaFunctionType = async (args) => {
const fluence = await getFluenceInterface();
return await fluence.callAquaFunction(args);
};
/**
* Low level API. Generally you should use code generated by the Aqua compiler.
*/
export const registerService: RegisterServiceType = async (args) => {
const fluence = await getFluenceInterface();
return await fluence.registerService(args);
};
export * from '@fluencelabs/js-client';

View File

@ -1,21 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
bundle/
dist
esm
types
# Dependency directories
node_modules/
jspm_packages/
.idea

View File

@ -1,169 +0,0 @@
# Changelog
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.1 to 0.8.2
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.3 to 0.8.4
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.4 to 0.8.5
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.7 to 0.8.8
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.8 to 0.8.9
## [0.7.1](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.7.0...js-client.node-v0.7.1) (2023-08-08)
### Bug Fixes
* **deps:** update dependency @fluencelabs/avm to v0.43.1 ([#322](https://github.com/fluencelabs/js-client/issues/322)) ([c1d1fa6](https://github.com/fluencelabs/js-client/commit/c1d1fa6659b6dc2c6707786748b3410fab7f1bcd))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.9.0 to 0.9.1
* @fluencelabs/interfaces bumped from 0.8.0 to 0.8.1
## [0.7.0](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.11...js-client.node-v0.7.0) (2023-06-29)
### ⚠ BREAKING CHANGES
* **avm:** avm 0.40.0 (https://github.com/fluencelabs/js-client/pull/315)
### Features
* **avm:** avm 0.40.0 (https://github.com/fluencelabs/js-client/pull/315) ([8bae6e2](https://github.com/fluencelabs/js-client/commit/8bae6e24e62153b567f320ccecc7bce76bc826d1))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.10 to 0.9.0
* @fluencelabs/interfaces bumped from 0.7.6 to 0.8.0
## [0.6.11](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.10...js-client.node-v0.6.11) (2023-06-20)
### Features
* support signatures [fixes DXJ-389] ([#310](https://github.com/fluencelabs/js-client/issues/310)) ([a60dfe0](https://github.com/fluencelabs/js-client/commit/a60dfe0d680b4d9ac5092dec64e2ebf478bf80eb))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.9 to 0.8.10
* @fluencelabs/interfaces bumped from 0.7.5 to 0.7.6
## [0.6.8](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.7...js-client.node-v0.6.8) (2023-04-04)
### Features
* Cleaning up technical debts ([#295](https://github.com/fluencelabs/js-client/issues/295)) ([0b2f12d](https://github.com/fluencelabs/js-client/commit/0b2f12d8ac223db341d6c30ff403166b3eae2e56))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.6 to 0.8.7
* @fluencelabs/interfaces bumped from 0.7.4 to 0.7.5
## [0.6.7](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.6...js-client.node-v0.6.7) (2023-03-31)
### Features
* **logs:** Use `debug.js` library for logging [DXJ-327] ([#285](https://github.com/fluencelabs/js-client/issues/285)) ([e95c34a](https://github.com/fluencelabs/js-client/commit/e95c34a79220bd8ecdcee806802ac3d69a2af0cb))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.5 to 0.8.6
## [0.6.4](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.3...js-client.node-v0.6.4) (2023-02-16)
### Bug Fixes
* Add .npmignore to js-client.node ([#264](https://github.com/fluencelabs/js-client/issues/264)) ([a899a53](https://github.com/fluencelabs/js-client/commit/a899a531fdfd855268ba47170ace9e281b53f0fd))
## [0.6.3](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.2...js-client.node-v0.6.3) (2023-02-16)
### Bug Fixes
* Trigger release to publish packages that were built ([#262](https://github.com/fluencelabs/js-client/issues/262)) ([47abf38](https://github.com/fluencelabs/js-client/commit/47abf3882956ffbdc52df372db26ba6252e8306b))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.2 to 0.8.3
## [0.6.1](https://github.com/fluencelabs/js-client/compare/js-client.node-v0.6.0...js-client.node-v0.6.1) (2023-02-16)
### Features
* Simplify JS Client public API ([#257](https://github.com/fluencelabs/js-client/issues/257)) ([9daaf41](https://github.com/fluencelabs/js-client/commit/9daaf410964d43228192c829c7ff785db6e88081))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.0 to 0.8.1
## [0.6.0](https://github.com/fluencelabs/fluence-js/compare/js-client.node-v0.5.0...js-client.node-v0.6.0) (2023-02-15)
### ⚠ BREAKING CHANGES
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/fluence-js/issues/246))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243))
### Features
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/fluence-js/issues/246)) ([d4bb8fb](https://github.com/fluencelabs/fluence-js/commit/d4bb8fb42964b3ba25154232980b9ae82c21e627))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243)) ([9667c4f](https://github.com/fluencelabs/fluence-js/commit/9667c4fec6868f984bba13249f3c47d293396406))
### Bug Fixes
* NodeJS package building ([#248](https://github.com/fluencelabs/fluence-js/issues/248)) ([0d05e51](https://github.com/fluencelabs/fluence-js/commit/0d05e517d89529af513fcb96cfa6c722ccc357a7))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.7.0 to 0.8.0

View File

@ -1,11 +0,0 @@
# JS Client node
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
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
## License
[Apache 2.0](LICENSE)

View File

@ -1,34 +0,0 @@
{
"name": "@fluencelabs/js-client.node",
"version": "0.7.1",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"type": "module",
"scripts": {
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "0.9.1",
"@fluencelabs/interfaces": "0.8.1",
"@fluencelabs/avm": "0.43.1",
"@fluencelabs/marine-js": "0.7.2",
"platform": "1.3.6"
},
"devDependencies": {
"@types/platform": "1.3.4"
}
}

View File

@ -1,80 +0,0 @@
/*
* Copyright 2023 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 * as platform from 'platform';
import type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker/index.js';
import { WasmLoaderFromNpm } from '@fluencelabs/js-peer/dist/marine/deps-loader/node.js';
import { WorkerLoader } from '@fluencelabs/js-peer/dist/marine/worker-script/workerLoader.js';
import { doRegisterNodeUtils } from '@fluencelabs/js-peer/dist/services/NodeUtils.js';
throwIfNotSupported();
export const defaultNames = {
avm: {
file: 'avm.wasm',
package: '@fluencelabs/avm',
},
marine: {
file: 'marine-js.wasm',
package: '@fluencelabs/marine-js',
},
};
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
const workerLoader = new WorkerLoader();
const controlModuleLoader = new WasmLoaderFromNpm(defaultNames.marine.package, defaultNames.marine.file);
const avmModuleLoader = new WasmLoaderFromNpm(defaultNames.avm.package, defaultNames.avm.file);
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
registerNodeOnlyServices(client);
await client.connect();
return client;
};
function registerNodeOnlyServices(client: IFluenceClient) {
doRegisterNodeUtils(client);
}
const publicFluenceInterface = {
clientFactory: createClient,
callAquaFunction,
registerService,
};
// @ts-ignore
globalThis.fluence = publicFluenceInterface;
function throwIfNotSupported() {
if (platform.name === 'Node.js' && platform.version) {
const version = platform.version.split('.').map(Number);
const major = version[0];
if (major < 16) {
throw new Error(
'Fluence JS Client requires node.js version >= "16.x"; Detected ' +
platform.description +
' Please update node.js to version 16 or higher.\nYou can use https://nvm.sh utility to update node.js version: "nvm install 17 && nvm use 17 && nvm alias default 17"',
);
}
}
}

View File

@ -1 +0,0 @@
tmp/

View File

@ -1,170 +0,0 @@
# Changelog
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.1 to 0.8.2
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.4 to 0.8.5
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.7 to 0.8.8
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.8 to 0.8.9
## [0.14.1](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.14.0...js-client.web.standalone-v0.14.1) (2023-08-08)
### Bug Fixes
* **deps:** update dependency @fluencelabs/avm to v0.43.1 ([#322](https://github.com/fluencelabs/js-client/issues/322)) ([c1d1fa6](https://github.com/fluencelabs/js-client/commit/c1d1fa6659b6dc2c6707786748b3410fab7f1bcd))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.9.0 to 0.9.1
* @fluencelabs/interfaces bumped from 0.8.0 to 0.8.1
## [0.14.0](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.10...js-client.web.standalone-v0.14.0) (2023-06-29)
### ⚠ BREAKING CHANGES
* **avm:** avm 0.40.0 (https://github.com/fluencelabs/js-client/pull/315)
### Features
* **avm:** avm 0.40.0 (https://github.com/fluencelabs/js-client/pull/315) ([8bae6e2](https://github.com/fluencelabs/js-client/commit/8bae6e24e62153b567f320ccecc7bce76bc826d1))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.10 to 0.9.0
* @fluencelabs/interfaces bumped from 0.7.6 to 0.8.0
## [0.13.10](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.9...js-client.web.standalone-v0.13.10) (2023-06-20)
### Features
* support signatures [fixes DXJ-389] ([#310](https://github.com/fluencelabs/js-client/issues/310)) ([a60dfe0](https://github.com/fluencelabs/js-client/commit/a60dfe0d680b4d9ac5092dec64e2ebf478bf80eb))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.9 to 0.8.10
* @fluencelabs/interfaces bumped from 0.7.5 to 0.7.6
## [0.13.7](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.6...js-client.web.standalone-v0.13.7) (2023-04-04)
### Features
* Cleaning up technical debts ([#295](https://github.com/fluencelabs/js-client/issues/295)) ([0b2f12d](https://github.com/fluencelabs/js-client/commit/0b2f12d8ac223db341d6c30ff403166b3eae2e56))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.6 to 0.8.7
* @fluencelabs/interfaces bumped from 0.7.4 to 0.7.5
## [0.13.6](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.5...js-client.web.standalone-v0.13.6) (2023-03-31)
### Features
* **logs:** Use `debug.js` library for logging [DXJ-327] ([#285](https://github.com/fluencelabs/js-client/issues/285)) ([e95c34a](https://github.com/fluencelabs/js-client/commit/e95c34a79220bd8ecdcee806802ac3d69a2af0cb))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.5 to 0.8.6
## [0.13.4](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.3...js-client.web.standalone-v0.13.4) (2023-02-22)
### Bug Fixes
* `nodenext` moduleResolution for js peer ([#271](https://github.com/fluencelabs/js-client/issues/271)) ([78d98f1](https://github.com/fluencelabs/js-client/commit/78d98f15c12431dee9fdd7b9869d57760503f8c7))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.3 to 0.8.4
## [0.13.3](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.2...js-client.web.standalone-v0.13.3) (2023-02-16)
### Bug Fixes
* Trigger release to publish packages that were built ([#262](https://github.com/fluencelabs/js-client/issues/262)) ([47abf38](https://github.com/fluencelabs/js-client/commit/47abf3882956ffbdc52df372db26ba6252e8306b))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.2 to 0.8.3
## [0.13.1](https://github.com/fluencelabs/js-client/compare/js-client.web.standalone-v0.13.0...js-client.web.standalone-v0.13.1) (2023-02-16)
### Features
* Simplify JS Client public API ([#257](https://github.com/fluencelabs/js-client/issues/257)) ([9daaf41](https://github.com/fluencelabs/js-client/commit/9daaf410964d43228192c829c7ff785db6e88081))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.0 to 0.8.1
## [0.13.0](https://github.com/fluencelabs/fluence-js/compare/js-client.web.standalone-v0.12.0...js-client.web.standalone-v0.13.0) (2023-02-15)
### ⚠ BREAKING CHANGES
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/fluence-js/issues/246))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243))
### Features
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/fluence-js/issues/246)) ([d4bb8fb](https://github.com/fluencelabs/fluence-js/commit/d4bb8fb42964b3ba25154232980b9ae82c21e627))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243)) ([9667c4f](https://github.com/fluencelabs/fluence-js/commit/9667c4fec6868f984bba13249f3c47d293396406))
### Bug Fixes
* NodeJS package building ([#248](https://github.com/fluencelabs/fluence-js/issues/248)) ([0d05e51](https://github.com/fluencelabs/fluence-js/commit/0d05e517d89529af513fcb96cfa6c722ccc357a7))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.7.0 to 0.8.0

View File

@ -1,11 +0,0 @@
# JS Client web
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
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
## License
[Apache 2.0](LICENSE)

View File

@ -1,127 +0,0 @@
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/js-peer/dist/marine/worker-script/index.js');
if (!fs.existsSync(scriptPath)) {
console.error('Worker script not found, looking at: ' + scriptPath);
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: [
// @ts-ignore
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

@ -1,37 +0,0 @@
{
"name": "@fluencelabs/js-client.web.standalone",
"version": "0.14.1",
"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": "0.9.1",
"@fluencelabs/interfaces": "0.8.1",
"buffer": "6.0.3",
"process": "0.11.10"
},
"devDependencies": {
"@fluencelabs/avm": "0.43.1",
"@fluencelabs/marine-js": "0.7.2",
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2",
"js-base64": "3.7.5",
"@rollup/plugin-inject": "5.0.3",
"vite-plugin-replace": "0.1.1",
"vite": "4.0.4",
"vite-tsconfig-paths": "4.0.3"
}
}

View File

@ -1,44 +0,0 @@
/*
* Copyright 2023 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 type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker/index.js';
import { InlinedWorkerLoader, InlinedWasmLoader } from '@fluencelabs/js-peer/dist/marine/deps-loader/common.js';
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
const workerLoader = new InlinedWorkerLoader('___worker___');
const controlModuleLoader = new InlinedWasmLoader('___marine___');
const avmModuleLoader = new InlinedWasmLoader('___avm___');
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
await client.connect();
return client;
};
const publicFluenceInterface = {
clientFactory: createClient,
callAquaFunction,
registerService,
};
// @ts-ignore
globalThis.fluence = publicFluenceInterface;

View File

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

View File

@ -1,21 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
bundle/
dist
esm
types
# Dependency directories
node_modules/
jspm_packages/
.idea

View File

@ -1,69 +0,0 @@
# Changelog
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.7 to 0.8.8
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.8 to 0.8.9
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.10 to 0.9.0
* @fluencelabs/interfaces bumped from 0.7.6 to 0.8.0
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.9.0 to 0.9.1
* @fluencelabs/interfaces bumped from 0.8.0 to 0.8.1
## [0.2.3](https://github.com/fluencelabs/js-client/compare/js-client.web-v0.2.2...js-client.web-v0.2.3) (2023-06-20)
### Features
* support signatures [fixes DXJ-389] ([#310](https://github.com/fluencelabs/js-client/issues/310)) ([a60dfe0](https://github.com/fluencelabs/js-client/commit/a60dfe0d680b4d9ac5092dec64e2ebf478bf80eb))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.9 to 0.8.10
* @fluencelabs/interfaces bumped from 0.7.5 to 0.7.6
## [0.2.0](https://github.com/fluencelabs/js-client/compare/js-client.web-v0.1.0...js-client.web-v0.2.0) (2023-04-04)
### ⚠ BREAKING CHANGES
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/js-client/issues/246))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/js-client/issues/243))
### Features
* Cleaning up technical debts ([#295](https://github.com/fluencelabs/js-client/issues/295)) ([0b2f12d](https://github.com/fluencelabs/js-client/commit/0b2f12d8ac223db341d6c30ff403166b3eae2e56))
* Expose updated JS Client API via `js-client.api` package ([#246](https://github.com/fluencelabs/js-client/issues/246)) ([d4bb8fb](https://github.com/fluencelabs/js-client/commit/d4bb8fb42964b3ba25154232980b9ae82c21e627))
* Standalone web JS Client ([#243](https://github.com/fluencelabs/js-client/issues/243)) ([9667c4f](https://github.com/fluencelabs/js-client/commit/9667c4fec6868f984bba13249f3c47d293396406))
### Bug Fixes
* NodeJS package building ([#248](https://github.com/fluencelabs/js-client/issues/248)) ([0d05e51](https://github.com/fluencelabs/js-client/commit/0d05e517d89529af513fcb96cfa6c722ccc357a7))
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @fluencelabs/js-peer bumped from 0.8.6 to 0.8.7
* @fluencelabs/interfaces bumped from 0.7.4 to 0.7.5

View File

@ -1,11 +0,0 @@
# JS Client web
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
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
## License
[Apache 2.0](LICENSE)

View File

@ -1,28 +0,0 @@
{
"name": "@fluencelabs/js-client.web",
"version": "0.2.5",
"description": "TypeScript implementation of Fluence Peer",
"main": "./dist/index.js",
"typings": "./dist/index.d.ts",
"engines": {
"node": ">=10",
"pnpm": ">=3"
},
"type": "module",
"scripts": {
"build": "tsc"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@fluencelabs/js-peer": "0.9.1",
"@fluencelabs/interfaces": "0.8.1"
},
"devDependencies": {
"@types/node": "16.11.59",
"@types/jest": "28.1.0",
"jest": "28.1.0",
"ts-jest": "28.0.2"
}
}

View File

@ -1,50 +0,0 @@
/*
* Copyright 2023 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 type { RelayOptions, ClientConfig, IFluenceClient } from '@fluencelabs/interfaces';
import { ClientPeer, makeClientPeerConfig } from '@fluencelabs/js-peer/dist/clientPeer/ClientPeer.js';
import { callAquaFunction } from '@fluencelabs/js-peer/dist/compilerSupport/callFunction.js';
import { registerService } from '@fluencelabs/js-peer/dist/compilerSupport/registerService.js';
import { MarineBasedAvmRunner } from '@fluencelabs/js-peer/dist/jsPeer/avm.js';
import { MarineBackgroundRunner } from '@fluencelabs/js-peer/dist/marine/worker/index.js';
import { WasmLoaderFromUrl, WorkerLoaderFromUrl } from '@fluencelabs/js-peer/dist/marine/deps-loader/web.js';
const defaultNames = {
marine: 'marine-js.wasm',
avm: 'avm.wasm',
worker: 'worker-script.js',
};
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
const workerLoader = new WorkerLoaderFromUrl(defaultNames.worker);
const controlModuleLoader = new WasmLoaderFromUrl(defaultNames.marine);
const avmModuleLoader = new WasmLoaderFromUrl(defaultNames.avm);
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine, avm);
await client.connect();
return client;
};
const publicFluenceInterface = {
clientFactory: createClient,
callAquaFunction,
registerService,
};
// @ts-ignore
globalThis.fluence = publicFluenceInterface;

View File

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

View File

@ -1,21 +0,0 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
bundle/
dist
esm
types
# Dependency directories
node_modules/
jspm_packages/
.idea

View File

@ -1 +0,0 @@
/dist/

View File

@ -1,19 +0,0 @@
# Changelog
## [0.2.1](https://github.com/fluencelabs/js-client/compare/tools-v0.2.0...tools-v0.2.1) (2023-02-16)
### Bug Fixes
* Trigger release to publish packages that were built ([#262](https://github.com/fluencelabs/js-client/issues/262)) ([47abf38](https://github.com/fluencelabs/js-client/commit/47abf3882956ffbdc52df372db26ba6252e8306b))
## [0.2.0](https://github.com/fluencelabs/fluence-js/compare/tools-v0.1.0...tools-v0.2.0) (2023-02-15)
### ⚠ BREAKING CHANGES
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243))
### Features
* Standalone web JS Client ([#243](https://github.com/fluencelabs/fluence-js/issues/243)) ([9667c4f](https://github.com/fluencelabs/fluence-js/commit/9667c4fec6868f984bba13249f3c47d293396406))

View File

@ -1,11 +0,0 @@
# Fluence JS Client tools
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
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
## License
[Apache 2.0](LICENSE)

View File

@ -1,24 +0,0 @@
{
"name": "@fluencelabs/tools",
"version": "0.2.1",
"description": "Fluence JS Client tools",
"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",
"bin": {
"copy-marine": "dist/copyMarine.js"
},
"dependencies": {},
"devDependencies": {
"@types/node": "16.11.59"
}
}

View File

@ -1,46 +0,0 @@
#! /usr/bin/env node
import * as fs from 'fs';
import * as path from 'path';
const firstArgument = process.argv[2];
if (!firstArgument) {
console.log(`Expected exactly 1 argument, got 0. Usage: ${path.basename(process.argv[1])} <destination directory>`);
process.exit(1);
}
let destPath = firstArgument;
if (!path.isAbsolute(destPath)) {
destPath = path.join(process.cwd(), destPath);
}
async function copyFile(packageName: string, fileName: string) {
const modulePath = require.resolve(packageName);
const source = path.join(path.dirname(modulePath), fileName);
const dest = path.join(destPath, fileName);
console.log(`copying ${fileName}`);
console.log('from: ', source);
console.log('to: ', dest);
await fs.promises.copyFile(source, dest);
}
async function main() {
console.log('ensure directory exists: ', destPath);
await fs.promises.mkdir(destPath, { recursive: true });
await Promise.all([
copyFile('@fluencelabs/marine.worker-script', 'marine-js.web.js'),
copyFile('@fluencelabs/marine-js', 'marine-js.wasm'),
copyFile('@fluencelabs/avm', 'avm.wasm'),
]);
}
main()
.then(() => {
console.log('done!');
})
.catch((err) => {
console.error('Something went wrong!', err);
});

View File

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

View File

@ -0,0 +1,102 @@
import path, { dirname } from 'path';
import type { InlineConfig, PluginOption } from 'vite';
import { build } from 'vite';
import { builtinModules, createRequire } from 'module';
import tsconfigPaths from 'vite-tsconfig-paths';
import inject from '@rollup/plugin-inject';
import stdLibBrowser from 'node-stdlib-browser';
import { fileURLToPath } from 'url';
import { rm, rename } from 'fs/promises';
import { replaceCodePlugin } from 'vite-plugin-replace';
import pkg from './package.json' assert { type: 'json' };
import libAssetsPlugin from '@laynezh/vite-plugin-lib-assets';
const require = createRequire(import.meta.url);
const commonConfig = (isNode: boolean): InlineConfig & Required<Pick<InlineConfig, 'build'>> => {
const esbuildShim = require.resolve('node-stdlib-browser/helpers/esbuild/shim');
return {
build: {
target: 'modules',
minify: 'esbuild',
lib: {
entry: './src/index.ts',
name: 'js-client',
fileName: `${isNode ? 'node' : 'browser'}/index`,
},
outDir: './dist',
emptyOutDir: false,
...(isNode ? {
rollupOptions: {
external: [...builtinModules, ...builtinModules.map(bm => `node:${bm}`)],
plugins: [
// @ts-ignore
inject({
self: 'global',
'WorkerScope': ['worker_threads', '*'],
'Worker': ['worker_threads', 'Worker'],
'isMainThread': ['worker_threads', 'isMainThread'],
})
]
}
} : {
rollupOptions: {
plugins: [
{
// @ts-ignore
...inject({
global: [esbuildShim, 'global'],
process: [esbuildShim, 'process'],
Buffer: [esbuildShim, 'Buffer']
}), enforce: 'post'
}
],
}
})
},
plugins: [tsconfigPaths(), libAssetsPlugin({
include: ['**/*.wasm*', '**/marine-worker.umd.cjs*'],
publicUrl: '/',
}), ...(isNode ? [replaceCodePlugin({
replacements: [
// After 'threads' package is built, it produces wrong output, which throws runtime errors.
// This code aims to fix such places.
// Should remove this after we move from threads to other package.
{ from: 'eval("require")("worker_threads")', to: 'WorkerScope' },
{ from: 'eval("require")("worker_threads")', to: 'WorkerScope' },
]
})] : [])] as PluginOption[],
optimizeDeps: {
esbuildOptions: {
define: {
global: 'globalThis',
},
},
},
resolve: {
browserField: !isNode,
conditions: isNode ? ['node'] : ['browser']
},
// Used only by browser
define: {
__JS_CLIENT_VERSION__: pkg.version,
__ENV__: isNode ? 'node' : 'browser'
},
};
};
const buildClient = async () => {
const nodeConfig = commonConfig(true);
const browserConfig = commonConfig(false);
try {
await rm('./dist', { recursive: true });
} catch {}
await build(nodeConfig);
await build(browserConfig);
};
buildClient()
.then(() => console.log('Built successfully'))
.catch((err) => console.error('failed', err));

View File

@ -0,0 +1,70 @@
{
"name": "@fluencelabs/js-client",
"version": "0.0.10",
"description": "Client for interacting with Fluence network",
"engines": {
"node": ">=10",
"pnpm": ">=8"
},
"files": [
"dist"
],
"main": "./dist/browser/index.js",
"unpkg": "./dist/browser/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
"types": "./dist/types/index.d.ts",
"node": "./dist/node/index.js",
"default": "./dist/browser/index.js"
},
"type": "module",
"scripts": {
"build": "node --loader ts-node/esm build.ts && tsc --emitDeclarationOnly",
"test": "vitest --threads false run"
},
"repository": "https://github.com/fluencelabs/fluence-js",
"author": "Fluence Labs",
"license": "Apache-2.0",
"dependencies": {
"@chainsafe/libp2p-noise": "13.0.0",
"@fluencelabs/interfaces": "0.8.1",
"@libp2p/crypto": "2.0.3",
"@libp2p/interface": "0.1.2",
"@libp2p/mplex": "9.0.4",
"@libp2p/peer-id": "3.0.2",
"@libp2p/peer-id-factory": "3.0.3",
"@libp2p/websockets": "7.0.4",
"@multiformats/multiaddr": "11.3.0",
"async": "3.2.4",
"bs58": "5.0.0",
"buffer": "6.0.3",
"debug": "4.3.4",
"it-length-prefixed": "8.0.4",
"it-map": "2.0.0",
"it-pipe": "2.0.5",
"js-base64": "3.7.5",
"libp2p": "0.46.6",
"multiformats": "11.0.1",
"rxjs": "7.5.5",
"threads": "1.7.0",
"ts-pattern": "3.3.3",
"uint8arrays": "4.0.3",
"uuid": "8.3.2"
},
"devDependencies": {
"@fluencelabs/aqua-api": "0.9.3",
"@fluencelabs/avm": "0.43.1",
"@fluencelabs/marine-js": "0.7.2",
"@fluencelabs/marine-worker": "workspace:*",
"@laynezh/vite-plugin-lib-assets": "0.5.2",
"@rollup/plugin-inject": "5.0.3",
"@types/bs58": "4.0.1",
"@types/debug": "4.1.7",
"@types/uuid": "8.3.2",
"node-stdlib-browser": "1.2.0",
"vite": "4.0.4",
"vite-plugin-replace": "0.1.1",
"vite-tsconfig-paths": "4.0.3",
"vitest": "0.29.7"
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2022 Fluence Labs Limited
* Copyright 2023 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.
@ -18,8 +18,7 @@ import type { FnConfig, FunctionCallDef, ServiceDef } from '@fluencelabs/interfa
import type { IFluenceClient } from '@fluencelabs/interfaces';
import { getArgumentTypes } from '@fluencelabs/interfaces';
import { isFluencePeer } from '@fluencelabs/interfaces';
import { getFluenceInterface } from '../util.js';
import { callAquaFunction, Fluence, registerService } from './index.js';
/**
* Convenience function to support Aqua `func` generation backend
@ -35,9 +34,8 @@ export const v5_callFunction = async (
script: string,
): Promise<unknown> => {
const { args, client: peer, config } = await extractFunctionArgs(rawFnArgs, def);
const fluence = await getFluenceInterface();
return fluence.callAquaFunction({
return callAquaFunction({
args,
def,
script,
@ -54,9 +52,8 @@ export const v5_callFunction = async (
*/
export const v5_registerService = async (args: any[], def: ServiceDef): Promise<unknown> => {
const { peer, service, serviceId } = await extractServiceArgs(args, def.defaultServiceId);
const fluence = await getFluenceInterface();
return fluence.registerService({
return registerService({
def,
service,
serviceId,
@ -94,13 +91,12 @@ const extractFunctionArgs = async (
structuredArgs = args.slice(1, numberOfExpectedArgs + 1);
config = args[numberOfExpectedArgs + 1];
} else {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
if (!Fluence.defaultClient) {
throw new Error(
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
);
}
peer = fluence.defaultClient;
peer = Fluence.defaultClient;
structuredArgs = args.slice(0, numberOfExpectedArgs);
config = args[numberOfExpectedArgs];
}
@ -140,13 +136,12 @@ const extractServiceArgs = async (
if (isFluencePeer(args[0])) {
peer = args[0];
} else {
const fluence = await getFluenceInterface();
if (!fluence.defaultClient) {
if (!Fluence.defaultClient) {
throw new Error(
'Could not register Aqua service because the client is not initialized. Did you forget to call Fluence.connect()?',
);
}
peer = fluence.defaultClient;
peer = Fluence.defaultClient;
}
if (typeof args[0] === 'string') {

View File

@ -63,11 +63,10 @@ export class ClientPeer extends FluencePeer implements IFluenceClient {
relayConfig: RelayConnectionConfig,
keyPair: KeyPair,
marine: IMarineHost,
avmRunner: IAvmRunner,
) {
const relayConnection = new RelayConnection(relayConfig);
super(peerConfig, keyPair, marine, new JsServiceHost(), avmRunner, relayConnection);
super(peerConfig, keyPair, marine, new JsServiceHost(), relayConnection);
this.relayPeerId = relayConnection.getRelayPeerId();
this.relayConnection = relayConnection;
}

View File

@ -16,7 +16,7 @@
import { PeerIdB58 } from '@fluencelabs/interfaces';
import { pipe } from 'it-pipe';
import { encode, decode } from 'it-length-prefixed';
import type { PeerId } from '@libp2p/interface-peer-id';
import type { PeerId } from '@libp2p/interface/peer-id';
import { createLibp2p, Libp2p } from 'libp2p';
import { noise } from '@chainsafe/libp2p-noise';
@ -115,6 +115,10 @@ export class RelayConnection implements IStartable, IConnection {
connectionManager: {
dialTimeout: this.config.dialTimeoutMs,
},
connectionGater: {
// By default, this function forbids connections to private peers. For example multiaddr with ip 127.0.0.1 isn't allowed
denyDialMultiaddr: () => Promise.resolve(false)
}
});
this.lib2p2Peer = lib2p2Peer;

View File

@ -14,7 +14,6 @@
* limitations under the License.
*/
import { PeerIdB58 } from '@fluencelabs/interfaces';
import { MarineBasedAvmRunner } from '../jsPeer/avm.js';
import { FluencePeer, PeerConfig } from '../jsPeer/FluencePeer.js';
import { KeyPair } from '../keypair/index.js';
import { WasmLoaderFromNpm } from '../marine/deps-loader/node.js';
@ -31,9 +30,8 @@ export class EphemeralNetworkClient extends FluencePeer {
const workerLoader = new WorkerLoader();
const controlModuleLoader = new WasmLoaderFromNpm('@fluencelabs/marine-js', 'marine-js.wasm');
const avmModuleLoader = new WasmLoaderFromNpm('@fluencelabs/avm', 'avm.wasm');
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader);
const avm = new MarineBasedAvmRunner(marine, avmModuleLoader);
const marine = new MarineBackgroundRunner(workerLoader, controlModuleLoader, avmModuleLoader);
const conn = network.getRelayConnection(keyPair.getPeerId(), relay);
super(config, keyPair, marine, new JsServiceHost(), avm, conn);
super(config, keyPair, marine, new JsServiceHost(), conn);
}
}

View File

@ -24,7 +24,6 @@ import { Subject } from 'rxjs';
import { Particle } from '../particle/Particle.js';
import { WasmLoaderFromNpm } from '../marine/deps-loader/node.js';
import { MarineBasedAvmRunner } from '../jsPeer/avm.js';
import { DEFAULT_CONFIG, FluencePeer } from '../jsPeer/FluencePeer.js';
import { IConnection } from '../connection/interfaces.js';
import { IAvmRunner, IMarineHost } from '../marine/interfaces.js';
@ -194,9 +193,9 @@ export class EphemeralConnection implements IConnection, IEphemeralConnection {
class EphemeralPeer extends FluencePeer {
ephemeralConnection: EphemeralConnection;
constructor(keyPair: KeyPair, marine: IMarineHost, avm: IAvmRunner) {
constructor(keyPair: KeyPair, marine: IMarineHost) {
const conn = new EphemeralConnection(keyPair.getPeerId());
super(DEFAULT_CONFIG, keyPair, marine, new JsServiceHost(), avm, conn);
super(DEFAULT_CONFIG, keyPair, marine, new JsServiceHost(), conn);
this.ephemeralConnection = conn;
}
@ -228,14 +227,13 @@ export class EphemeralNetwork {
const promises = this.config.peers.map(async (x) => {
const kp = await fromBase64Sk(x.sk);
const marine = new MarineBackgroundRunner(this.workerLoader, this.controlModuleLoader);
const avm = new MarineBasedAvmRunner(marine, this.avmModuleLoader);
const marine = new MarineBackgroundRunner(this.workerLoader, this.controlModuleLoader, this.avmModuleLoader);
const peerId = kp.getPeerId();
if (peerId !== x.peerId) {
throw new Error(`Invalid config: peer id ${x.peerId} does not match the secret key ${x.sk}`);
}
return new EphemeralPeer(kp, marine, avm);
return new EphemeralPeer(kp, marine);
});
const peers = await Promise.all(promises);

View File

@ -0,0 +1,19 @@
/*
* Copyright 2023 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.
*/
export async function fetchResource(assetPath: string, version: string) {
return fetch(new globalThis.URL(`@fluencelabs/js-client@${version}/dist` + assetPath, `https://unpkg.com/`));
}

View File

@ -0,0 +1,30 @@
/*
* Copyright 2023 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 { fetchResource as fetchResourceBrowser } from './browser.js';
import { fetchResource as fetchResourceNode } from './node.js';
import process from 'process';
const isNode = typeof process !== 'undefined' && process?.release?.name === 'node';
export async function fetchResource(assetPath: string, version: string) {
switch (true) {
case isNode:
return fetchResourceNode(assetPath, version);
default:
return fetchResourceBrowser(assetPath, version);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2023 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 fs from 'fs';
import url from 'url';
import path from 'path';
export async function fetchResource(assetPath: string, version: string) {
const file = await new Promise<ArrayBuffer>((resolve, reject) => {
// Cannot use 'fs/promises' with current vite config. This module is not polyfilled by default.
const root = path.dirname(url.fileURLToPath(import.meta.url));
const workerFilePath = path.join(root, '..', assetPath);
fs.readFile(workerFilePath, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
});
});
return new Response(file, {
headers: {
'Content-type':
assetPath.endsWith('.wasm')
? 'application/wasm'
: assetPath.endsWith('.js')
? 'application/javascript'
: 'application/text'
}
});
}

View File

@ -0,0 +1,165 @@
/*
* Copyright 2023 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 type { ClientConfig, IFluenceClient, RelayOptions, ConnectionState, CallAquaFunctionType, RegisterServiceType } from '@fluencelabs/interfaces';
import { ClientPeer, makeClientPeerConfig } from './clientPeer/ClientPeer.js';
import { callAquaFunction } from './compilerSupport/callFunction.js';
import { registerService } from './compilerSupport/registerService.js';
import { MarineBackgroundRunner } from './marine/worker/index.js';
// @ts-ignore
import { BlobWorker, Worker } from 'threads';
import { doRegisterNodeUtils } from './services/NodeUtils.js';
import { fetchResource } from './fetchers/index.js';
import process from 'process';
import avmWasmUrl from '../node_modules/@fluencelabs/avm/dist/avm.wasm?url';
import marineJsWasmUrl from '../node_modules/@fluencelabs/marine-js/dist/marine-js.wasm?url';
import workerCodeUrl from '../node_modules/@fluencelabs/marine-worker/dist/__ENV__/marine-worker.umd.cjs?url';
const JS_CLIENT_VERSION = '__JS_CLIENT_VERSION__';
const isNode = typeof process !== 'undefined' && process?.release?.name === 'node';
const fetchWorkerCode = () => fetchResource(workerCodeUrl, JS_CLIENT_VERSION).then(res => res.text());
const fetchMarineJsWasm = () => fetchResource(marineJsWasmUrl, JS_CLIENT_VERSION).then(res => res.arrayBuffer());
const fetchAvmWasm = () => fetchResource(avmWasmUrl, JS_CLIENT_VERSION).then(res => res.arrayBuffer());
const createClient = async (relay: RelayOptions, config: ClientConfig): Promise<IFluenceClient> => {
const workerCode = await fetchWorkerCode();
const marineJsWasm = await fetchMarineJsWasm();
const avmWasm = await fetchAvmWasm();
const marine = new MarineBackgroundRunner({
getValue() {
return BlobWorker.fromText(workerCode)
},
start() {
return Promise.resolve(undefined);
},
stop() {
return Promise.resolve(undefined);
},
}, {
getValue() {
return marineJsWasm;
}, start(): Promise<void> {
return Promise.resolve(undefined);
}, stop(): Promise<void> {
return Promise.resolve(undefined);
}
}, {
getValue() {
return avmWasm;
}, start(): Promise<void> {
return Promise.resolve(undefined);
}, stop(): Promise<void> {
return Promise.resolve(undefined);
}
});
const { keyPair, peerConfig, relayConfig } = await makeClientPeerConfig(relay, config);
const client: IFluenceClient = new ClientPeer(peerConfig, relayConfig, keyPair, marine);
if (isNode) {
doRegisterNodeUtils(client);
}
await client.connect();
return client;
};
/**
* Public interface to Fluence Network
*/
export const Fluence = {
defaultClient: undefined as (IFluenceClient | undefined),
/**
* Connect to the Fluence network
* @param relay - relay node to connect to
* @param config - client configuration
*/
connect: async function(relay: RelayOptions, config: ClientConfig): Promise<void> {
const client = await createClient(relay, config);
this.defaultClient = client;
},
/**
* Disconnect from the Fluence network
*/
disconnect: async function(): Promise<void> {
await this.defaultClient?.disconnect();
this.defaultClient = undefined;
},
/**
* Handle connection state changes. Immediately returns the current connection state
*/
onConnectionStateChange(handler: (state: ConnectionState) => void): ConnectionState {
return this.defaultClient?.onConnectionStateChange(handler) || 'disconnected';
},
/**
* Low level API. Get the underlying client instance which holds the connection to the network
* @returns IFluenceClient instance
*/
getClient: async function(): Promise<IFluenceClient> {
if (!this.defaultClient) {
throw new Error('Fluence client is not initialized. Call Fluence.connect() first');
}
return this.defaultClient;
},
};
export type { IFluenceClient, ClientConfig, CallParams } from '@fluencelabs/interfaces';
export type {
ArrayType,
ArrowType,
ArrowWithCallbacks,
ArrowWithoutCallbacks,
BottomType,
FunctionCallConstants,
FunctionCallDef,
LabeledProductType,
NilType,
NonArrowType,
OptionType,
ProductType,
ScalarNames,
ScalarType,
ServiceDef,
StructType,
TopType,
UnlabeledProductType,
CallAquaFunctionType,
CallAquaFunctionArgs,
PassedArgs,
FnConfig,
RegisterServiceType,
RegisterServiceArgs,
} from '@fluencelabs/interfaces';
export { v5_callFunction, v5_registerService } from './api.js';
// @ts-ignore
globalThis.new_fluence = Fluence;
// @ts-ignore
globalThis.fluence = {
clientFactory: createClient,
callAquaFunction,
registerService,
};
export { createClient, callAquaFunction, registerService };
export { getFluenceInterface, getFluenceInterfaceFromGlobalThis } from './util/loadClient.js';

View File

@ -16,7 +16,7 @@
import { KeyPair } from '../keypair/index.js';
import type { PeerIdB58 } from '@fluencelabs/interfaces';
import { KeyPairFormat } from '@fluencelabs/avm';
import { deserializeAvmResult, InterpreterResult, KeyPairFormat, serializeAvmArgs } from '@fluencelabs/avm';
import {
cloneWithNewData,
getActualTTL,
@ -91,7 +91,6 @@ export abstract class FluencePeer {
public readonly keyPair: KeyPair,
protected readonly marineHost: IMarineHost,
protected readonly jsServiceHost: IJsServiceHost,
protected readonly avmRunner: IAvmRunner,
protected readonly connection: IConnection,
) {
this._initServices();
@ -110,9 +109,7 @@ export abstract class FluencePeer {
if (this.config?.debug?.printParticleId) {
this.printParticleId = true;
}
await this.marineHost.start();
await this.avmRunner.start();
this._startParticleProcessing();
this.isInitialized = true;
@ -128,7 +125,6 @@ export abstract class FluencePeer {
this._particleSourceSubscription?.unsubscribe();
this._stopParticleProcessing();
await this.marineHost.stop();
await this.avmRunner.stop();
this.isInitialized = false;
log_peer.trace('stopped Fluence peer');
@ -159,8 +155,8 @@ export abstract class FluencePeer {
* Removes the specified marine service from the Fluence peer
* @param serviceId - the service id to remove
*/
removeMarineService(serviceId: string): void {
this.marineHost.removeService(serviceId);
async removeMarineService(serviceId: string): Promise<void> {
await this.marineHost.removeService(serviceId);
}
// internal api
@ -385,7 +381,8 @@ export abstract class FluencePeer {
log_particle.debug('id %s. sending particle to interpreter', item.particle.id);
log_particle.trace('id %s. prevData: %a', item.particle.id, prevData);
const avmCallResult = await this.avmRunner.run(
const args = serializeAvmArgs(
{
initPeerId: item.particle.initPeerId,
currentPeerId: this.keyPair.getPeerId(),
@ -401,6 +398,14 @@ export abstract class FluencePeer {
item.callResults,
);
let avmCallResult: InterpreterResult | Error;
try {
const res = await this.marineHost.callService('avm', 'invoke', args, defaultCallParameters);
avmCallResult = deserializeAvmResult(res);
} catch (e) {
avmCallResult = e instanceof Error ? e : new Error(String(e));
}
if (!(avmCallResult instanceof Error) && avmCallResult.retCode === 0) {
const newData = Buffer.from(avmCallResult.data);
prevData = newData;
@ -521,7 +526,7 @@ export abstract class FluencePeer {
const particleId = req.particleContext.particleId;
log_particle.trace('id %s. executing call service handler %j', particleId, req);
if (this.marineHost && this.marineHost.hasService(req.serviceId)) {
if (this.marineHost && await this.marineHost.hasService(req.serviceId)) {
// TODO build correct CallParameters instead of default ones
const result = await this.marineHost.callService(req.serviceId, req.fnName, req.args, defaultCallParameters);

View File

@ -64,9 +64,8 @@ describe('KeyPair tests', () => {
// act
const res = await keyPair.signBytes(testData);
// assert
expect(res).toStrictEqual(testDataSig);
expect(new Uint8Array(res)).toStrictEqual(testDataSig);
});
it('verify', async function () {

View File

@ -14,10 +14,10 @@
* limitations under the License.
*/
import type { PeerId } from '@libp2p/interface-peer-id';
import type { PeerId } from '@libp2p/interface/peer-id';
import { generateKeyPairFromSeed, generateKeyPair } from '@libp2p/crypto/keys';
import { createFromPrivKey } from '@libp2p/peer-id-factory';
import type { PrivateKey } from '@libp2p/interface-keys';
import type { PrivateKey } from '@libp2p/interface/keys';
import { toUint8Array } from 'js-base64';
import * as bs58 from 'bs58';
import { KeyPairOptions } from '@fluencelabs/interfaces';

View File

@ -15,7 +15,6 @@
*/
import { CallResultsArray, InterpreterResult, RunParameters } from '@fluencelabs/avm';
import { IStartable, JSONArray, JSONObject, CallParameters } from '../util/commonTypes.js';
import { Buffer } from 'buffer';
// @ts-ignore
import type { WorkerImplementation } from 'threads/dist/types/master';
@ -26,17 +25,17 @@ export interface IMarineHost extends IStartable {
/**
* Creates marine service from the given module and service id
*/
createService(serviceModule: SharedArrayBuffer | Buffer, serviceId: string): Promise<void>;
createService(serviceModule: ArrayBuffer | SharedArrayBuffer, serviceId: string): Promise<void>;
/**
* Removes marine service with the given service id
*/
removeService(serviceId: string): void;
removeService(serviceId: string): Promise<void>;
/**
* Returns true if any service with the specified service id is registered
*/
hasService(serviceId: string): boolean;
hasService(serviceId: string): Promise<boolean>;
/**
* Calls the specified function of the specified service with the given arguments
@ -75,7 +74,7 @@ export interface IValueLoader<T> {
/**
* Interface for something which can load wasm files
*/
export interface IWasmLoader extends IValueLoader<SharedArrayBuffer | Buffer>, IStartable {}
export interface IWasmLoader extends IValueLoader<ArrayBuffer | SharedArrayBuffer>, IStartable {}
/**
* Interface for something which can thread.js based worker

View File

@ -21,6 +21,6 @@ import { LazyLoader } from '../interfaces.js';
export class WorkerLoader extends LazyLoader<WorkerImplementation> {
constructor() {
super(() => new Worker('./'));
super(() => new Worker('../../../node_modules/@fluencelabs/marine-worker/dist/node/marine-worker.umd.cjs'));
}
}

View File

@ -16,43 +16,50 @@
import type { JSONArray, JSONObject, CallParameters } from '@fluencelabs/marine-js/dist/types';
import { LogFunction, logLevelToEnv } from '@fluencelabs/marine-js/dist/types';
import type { MarineBackgroundInterface } from '../worker-script/index.js';
import type { MarineBackgroundInterface } from '@fluencelabs/marine-worker';
// @ts-ignore
import { spawn, Thread } from 'threads';
// @ts-ignore
import type { ModuleThread } from 'threads';
import { Buffer } from 'buffer';
import { ModuleThread, spawn, Thread } from 'threads';
import { MarineLogger, marineLogger } from '../../util/logger.js';
import { IMarineHost, IWasmLoader, IWorkerLoader } from '../interfaces.js';
export class MarineBackgroundRunner implements IMarineHost {
private marineServices = new Set<string>();
private workerThread?: ModuleThread<MarineBackgroundInterface>;
private workerThread?: MarineBackgroundInterface;
private loggers: Map<string, MarineLogger> = new Map();
private loggers = new Map<string, MarineLogger>();
constructor(private workerLoader: IWorkerLoader, private controlModuleLoader: IWasmLoader) {}
constructor(private workerLoader: IWorkerLoader, private controlModuleLoader: IWasmLoader, private avmWasmLoader: IWasmLoader) {}
hasService(serviceId: string): boolean {
return this.marineServices.has(serviceId);
async hasService(serviceId: string) {
if (!this.workerThread) {
throw new Error('Worker is not initialized');
}
return this.workerThread.hasService(serviceId);
}
removeService(serviceId: string): void {
this.marineServices.delete(serviceId);
async removeService(serviceId: string) {
if (!this.workerThread) {
throw new Error('Worker is not initialized');
}
await this.workerThread.removeService(serviceId);
}
async start(): Promise<void> {
if (this.workerThread) {
return;
throw new Error('Worker thread already initialized');
}
this.marineServices = new Set();
await this.workerLoader.start();
await this.controlModuleLoader.start();
const worker = this.workerLoader.getValue();
const wasm = this.controlModuleLoader.getValue();
this.workerThread = await spawn<MarineBackgroundInterface>(worker, { timeout: 99999999 });
await this.avmWasmLoader.start();
await this.workerLoader.start();
const worker = this.workerLoader.getValue();
const workerThread = await spawn<MarineBackgroundInterface>(worker);
const logfn: LogFunction = (message) => {
const serviceLogger = this.loggers.get(message.service);
if (!serviceLogger) {
@ -60,13 +67,15 @@ export class MarineBackgroundRunner implements IMarineHost {
}
serviceLogger[message.level](message.message);
};
this.workerThread.onLogMessage().subscribe(logfn);
await this.workerThread.init(wasm);
workerThread.onLogMessage().subscribe(logfn);
await workerThread.init(wasm);
this.workerThread = workerThread;
await this.createService(this.avmWasmLoader.getValue(), 'avm');
}
async createService(serviceModule: SharedArrayBuffer | Buffer, serviceId: string): Promise<void> {
async createService(serviceModule: ArrayBuffer | SharedArrayBuffer, serviceId: string): Promise<void> {
if (!this.workerThread) {
throw 'Worker is not initialized';
throw new Error('Worker is not initialized');
}
// The logging level is controlled by the environment variable passed to enable debug logs.
@ -74,10 +83,9 @@ export class MarineBackgroundRunner implements IMarineHost {
const env = logLevelToEnv('trace');
this.loggers.set(serviceId, marineLogger(serviceId));
await this.workerThread.createService(serviceModule, serviceId, env);
this.marineServices.add(serviceId);
}
callService(
async callService(
serviceId: string,
functionName: string,
args: JSONArray | JSONObject,
@ -94,8 +102,7 @@ export class MarineBackgroundRunner implements IMarineHost {
if (!this.workerThread) {
return;
}
this.marineServices.clear();
await this.workerThread.terminate();
await Thread.terminate(this.workerThread);
}

View File

@ -18,8 +18,9 @@ import { CallParams, IFluenceInternalApi } from '@fluencelabs/interfaces';
import { defaultGuard } from './SingleModuleSrv.js';
import { NodeUtilsDef, registerNodeUtils } from './_aqua/node-utils.js';
import { SecurityGuard } from './securityGuard.js';
import { readFile } from 'fs/promises';
import * as fs from 'fs';
import { FluencePeer } from '../jsPeer/FluencePeer.js';
import { Buffer } from 'buffer';
export class NodeUtils implements NodeUtilsDef {
constructor(private peer: FluencePeer) {
@ -39,10 +40,18 @@ export class NodeUtils implements NodeUtilsDef {
try {
// Strange enough, but Buffer type works here, while reading with encoding 'utf-8' doesn't
const data: any = await readFile(path);
const data = await new Promise<Buffer>((resolve, reject) => {
fs.readFile(path, (err, data) => {
if (err) {
reject(err);
return;
}
resolve(data);
})
});
return {
success: true,
content: data,
content: data as unknown as string,
error: null,
};
} catch (err: any) {

Some files were not shown because too many files have changed in this diff Show More